mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-02 06:46:18 -04:00
reorganize dir
Signed-off-by: Mia Steinkirch <mia.steinkirch@gmail.com>
This commit is contained in:
parent
1b6f705e7c
commit
a8e71c50db
276 changed files with 23954 additions and 0 deletions
34
ebook_src/builtin_structures/ransom_note.py
Executable file
34
ebook_src/builtin_structures/ransom_note.py
Executable file
|
@ -0,0 +1,34 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
|
||||
from collections import Counter
|
||||
|
||||
def check_if_ransom_note(magazines, note):
|
||||
count = Counter()
|
||||
pm, pn = 0, 0
|
||||
|
||||
while pn < len(note) and pm < len(magazines):
|
||||
char_note = note[pn]
|
||||
if count[char_note]>0:
|
||||
count[char_note] -= 1
|
||||
pn += 1
|
||||
else:
|
||||
char_magazine = magazines[pm]
|
||||
count[char_magazine] += 1
|
||||
pm +=1
|
||||
|
||||
return pn == len(note)
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
magazines1 = "avfegthhgrebvkdsvnijnvyijfdmckdsmovkmmfvskumvl;cdkmioswckofjbkreenyukjemjgnmkmvkmnvdkmvkr g gmvdvmldm vldfkmbldkmlvdkm"
|
||||
magazines2 = "adfsfa"
|
||||
note = "you should disobey"
|
||||
|
||||
print(check_if_ransom_note(magazines1, note))
|
||||
print(check_if_ransom_note(magazines2, note))
|
Loading…
Add table
Add a link
Reference in a new issue