初心者向けのPythonのプログラミング問題です。入門編としてチャレンジしてください。Pythonの正答例は以下になります。
問題469
def uppercase_with_length(lst):
return [(s.upper(), len(s)) for s in lst]
words = [“apple”, “banana”, “cherry”]
print(uppercase_with_length(words))
# [(‘APPLE’, 5), (‘BANANA’, 6), (‘CHERRY’, 6)]
問題470
def count_odds(lst):
return sum(1 for x in lst if x % 2 != 0)
numbers = [1, 2, 3, 4, 5]
print(count_odds(numbers)) # 3
問題471
def nth_index_of_char(s, char, n):
count = 0
for i, c in enumerate(s):
if c == char:
count += 1
if count == n:
return i
return -1 # n回登場しなければ-1を返す
text = “hello world”
print(nth_index_of_char(text, ‘l’, 2)) # 3



コメント