初心者向けのPythonのプログラミング問題です。入門編としてチャレンジしてください。Pythonの正答例は以下になります。
問題643
def nth_occurrence(s, char, n):
count = 0
for i, c in enumerate(s):
if c == char:
count += 1
if count == n:
return i
return -1
# 呼び出し例
s = “banana”
print(nth_occurrence(s, ‘a’, 2)) # 出力: 3
問題644
def split_words(s):
return s.split()
# 呼び出し例
s = “Python is fun”
print(split_words(s)) # 出力: [‘Python’,’is’,’fun’]
問題645
def split_by_char(s, sep):
return s.split(sep)
# 呼び出し例
s = “a,b,c”
print(split_by_char(s, ‘,’)) # 出力: [‘a’,’b’,’c’]



コメント