初心者向けのPythonのプログラミング問題です。入門編としてチャレンジしてください。Pythonの正答例は以下になります。
問題484
def find_second_substring(s, sub):
first_index = s.find(sub)
if first_index == -1:
return -1 # サブ文字列が1回もない場合
# 1回目の次の位置から検索
second_index = s.find(sub, first_index + len(sub))
return second_index # 2回目がなければ -1 が返る
# 使用例
text = “abc def abc ghi abc”
print(find_second_substring(text, “abc”)) # 8
print(find_second_substring(text, “def”)) # -1
問題485
def sort_mixed(lst):
nums = sorted([x for x in lst if isinstance(x, (int, float))])
strs = sorted([x for x in lst if isinstance(x, str)])
return nums + strs
items = [3, “apple”, 1, “banana”, 2]
print(sort_mixed(items)) # [1, 2, 3, ‘apple’, ‘banana’]
問題486
def count_char_ignore_case(s, char):
s_lower = s.lower()
char_lower = char.lower()
return s_lower.count(char_lower)
text = “Hello World”
print(count_char_ignore_case(text, “h”)) # 1



コメント