Given two strings ransomNote and magazine, return true if ransomNote can be constructed by using the letters from magazine and false otherwise. Each letter in magazine can only be used once in ransomNote.
![Given two strings ransomNote and magazine, return true if
ransomNote can be
constructed by using the letters from magazine and false otherwise.
Each letter in magazine can only be used once in ransomNote.
your program reads from file lab2_note_input.txt, and outputs to
lab2_encrypted.txt. You have to use both set and dict class.
sample lab2_encrypted.txt
Sample lab2_note_input.txt
aa ba
aa aba
aac bcasa
teer retire
retire teer
None
[0,2]
[2,4,1]
[2,1,5,0]
None
Then
your program reads from file lab2_codeBook_input.txt, and outputs to
lab2_decrypted.txt. You have to use both set and dict class.
Sample lab2_codeBook_input.txt
sample lab2_decrypted.txt
[0,2] aba
[2,4,1] bcasa
[2,1,5,0] retire
[2,1,5,7] retire
aa
aac
teer
None](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F145994db-462b-4941-a360-b6875a31b914%2Fe71d45ef-9f6c-4c2f-b5aa-ef456d4b5604%2F9kgy6z_processed.png&w=3840&q=75)

Trending now
This is a popular solution!
Step by step
Solved in 5 steps with 5 images

Is it possible to do it with this Method
def canConstruct(ransomNote: str, magazine: str) -> bool:
if len(ransomNote) > len(magazine):
return False
magazinedict = dict()
for letter in magazine:
if letter not in magazinedict:
magazinedict[letter] = 1
else:
magazinedict[letter] += 1
for letter in ransomNote:
if letter not in magazinedict:
return False
if letter in magazinedict and magazinedict[letter] <= 0:
return False
magazinedict[letter] -= 1
return True
with open('lab1_input.txt') as text:
for line in text.readlines():
strings = list(map(str,line.split()))
print(strings, end="\t")
ransomNote = strings[0]
magazine = strings[1]
output = canConstruct(ransomNote,magazine)
print('-\t'+str(output))
outfile = open('lab1_output.txt', 'a')
outfile.write(str(output)+'\n')
outfile.close()








