Is it possible to demonstrate the program with the text file and the output printed in the text files created using class and dict.
Is it possible to demonstrate the program with the text file and the output printed in the text files created using class and dict.
example
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()
This is very simple.
This is how we can demonstrate the program with the text file and output printed in the text files.
----------------------------------------------------- Python ---------------------------------------------------------
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', 'r') 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))
for line in text :
outfile = open('lab1_output.txt', 'a')
outfile.writelines(str(output)+'\n')
outfile.close()
Trending now
This is a popular solution!
Step by step
Solved in 2 steps