Исправьте код python
код к задаче не работает при первом тесте(выдает результат 2), при втором все работает исправно.
word = input()
n = int(input())
dword = {i: word.count(i) for i in set(word)}
count = 0
for _ in range(n) :
words = input()
tmp = {i: words.count(i) for i in set(words)}
flag = 1
for i in list(tmp.keys()) :
if i not in dword or tmp[i] > dword[i] :
flag = 0
break
count += flag
print(count)
Ответы (1 шт):
Автор решения: Zhihar
→ Ссылка
в задаче за правильное слово надо прибавлять кол-во букв в слове, а вы прибавляете просто 1
count += flag
вот чуть по другому код переписал:
text = input()
letters_total = {i: text.count(i) for i in set(text)}
count = 0
for _ in range(int(input())) :
word = input()
letters_local = {i: word.count(i) for i in set(word)}
is_corrent = True
for letter in letters_local:
if letter not in letters_total or letters_local[letter] > letters_total[letter]:
is_corrent = False
break
count += len(word) if is_corrent else 0
print(count)
немного более короткий, но и более извратный код:
text = input()
letters_total = {i: text.count(i) for i in set(text)}
count = 0
for _ in range(int(input())) :
word = input()
letters_local = {i: word.count(i) for i in set(word)}
count += len(word) if [0 for letter in letters_local if letter not in letters_total or letters_local[letter] > letters_total[letter]] == [] else 0
print(count)
