長さがmin_length未満の名前の場合、リストのその項目を、最小の長さの例を実現するために右側にスペースが追加された元の名前を含む新しい文字列に置き換えます
。min_length = 5 // /'dog'変更後='dog'
そしてまたリスト内のmin_lengthを元々超えていた名前の量を返します
def pad_names(list_of_names, min_length):
'''(list of str, int) -> int
Return the number of names that are longer than the minimum length.
>>> pad_names(list_of_names, 5)
2
'''
new_list = []
counter = 0
for word in list_of_names:
if len(word) > min_length:
counter = counter + 1
for word in list_of_names:
if len(word) < min_length:
new_list.append(word.ljust(min_length))
else:
new_list.append(word)
return(counter)
現在、この関数で5つのエラーが発生しています。
すべてのエラーは#incorrect戻り値です
test_02:pad_names_one_name_much_padding_changes_list
原因:1つの名前にパディングが必要な場合、リストは正しく変更されません。
test_04:pad_names_one_name_needs_one_pad_changes_list
原因:1つの名前に1つのパッド変更リストが必要です
test_10:pad_names_all_padded_by_3_changes_list
原因:すべて3つの変更リストが埋め込まれています
test_12:pad_names_all_padded_different_amounts_changes_list
原因:すべてのパディングされた異なる金額の変更リスト
test_20:pad_names_one_empty_name_list_changed
原因:1つの空の名前リストが変更されました
この関数は効率的である必要はなく、問題を増やすことなくこれらのテストに合格する必要があります。