初心者向けのPythonのプログラミング問題です。入門編としてチャレンジしてください。Pythonの正答例は以下になります。
問題493
def float_range(lst):
floats = [x for x in lst if isinstance(x, float)]
if not floats:
return None
return max(floats) – min(floats)
# 使用例
numbers = [1.5, 2.3, 4, 7.1]
print(float_range(numbers)) # 出力: 5.6
問題494
def lowercase_and_count_vowels(s):
vowels = “aeiou”
lower_s = s.lower()
count = sum(1 for c in lower_s if c in vowels)
return lower_s, count
# 使用例
text = “Hello World”
print(lowercase_and_count_vowels(text)) # 出力: (‘hello world’, 3)
問題495
def all_numbers(lst):
return all(isinstance(x, (int, float)) for x in lst)
numbers = [1, 2, 3]
print(all_numbers(numbers)) # True
mixed = [1, 2, “3”]
print(all_numbers(mixed)) # False



コメント