Use python code! Jupyter notebook questions. cannot send the file (LOTR.txt) due to the size for first one. Q1: A simple

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

Use python code! Jupyter notebook questions. cannot send the file (LOTR.txt) due to the size for first one. Q1: A simple

Post by answerhappygod »

Use python code! Jupyter notebook questions.
cannot send the file (LOTR.txt) due to the size for first
one.
Use Python Code Jupyter Notebook Questions Cannot Send The File Lotr Txt Due To The Size For First One Q1 A Simple 1
Use Python Code Jupyter Notebook Questions Cannot Send The File Lotr Txt Due To The Size For First One Q1 A Simple 1 (102.13 KiB) Viewed 24 times
Q1: A simple e-book reader
def read_page(page):
with open('LOTR.txt', 'r', encoding='utf8') as fin:
flag = False
next_page = page + 1
text = ''
# INSERT YOUR CODE BELOW
# Hint:
# Use the flag variable to control which lines to be added to
s
# Change the flag to true when we find "page"
# Change the flag to false when we find "next_page"
# ~ 8 lines
...
text = read_page(1)
print(text)
-----------------------------------------------------
Q2: Codebook
def read_codebook(fname = 'words.txt'):
'''return a list of words'''
# INSERT YOUR CODE BELOW
# Store each line as an elment of a list, then retunr the
list
# Hint, use strip() method to remove '\n'
# ~ 3 lines
...
def encrypt_words(words, codebook):
# INSERT YOUR CODE BELOW
# Look up code for each words and store them into a list li
# ~ 3 lines
...
return li
def decrypt_codes(code, codebook):
# INSERT YOUR CODE BELOW
# Look up words from code, and store them into a string
# ~ 4 lines
...
# TESTCASES BELOW
cb = read_codebook()
li = encrypt_words('good morning', cb)
print(li) #[41879, 62760]
code = [44543, 0, 64852, 24327]
s = decrypt_codes(code, cb)
print(s) # have a nice day
Q1: A simple e-book reader (50%) Now, let's integrate all we have learned today! When dealing with the input/output of large files, we usually do not need to hold all contents in the system memory. For example, if we are reading a very long book, we only need to keep the pages the user is currently reading in the memory. In this example, you will create a simple e-book reader that does just that, Please download the LOTR.txt file from MyUni. It is a big text file that contains the first volume of The Lord of The Ring. Your e-book reader should continuously take user input of a page number, then output the text of that particular page. Some notes . Open LOTR.txt with your text editor and figure out the format of page numbers. There are some missing page number, and you can ignore those. • The file is big. You should sequentually read the file using the readline(), instead of using the read() use strip() method to remove leading and trailing white spaces for each line. 3]: 3 4 5 1 def read_page(page): 2 with open ("LOTR.txt', 'r', encoding='utf8') as fin: flag = False next_page = page + 1 text = # INSERT YOUR CODE BELOW # Hint: # Use the flag variable to control which lines to be added to s # Change the flag to true when we find "page" 10 # Change the flag to false when we find "next_page" 11 # ~ 8 Lines 6 7 8 9 12. ... 13 text = read_page (1) 14 print(text)
Q2: Codebook 1 In crypotography, a codebook is a look-up table that translates code into something meaningful. The code is a list of numbers corresponding different lines in the file. Here we use the words.txt file as a codebook. The code 41879 62760 corresponds to the words at line 41879 (good) and 62760 (morning) in the file and thus can be translated to good morning Please complete the code below : 1 def read_codebook (fname = 'words.txt'): 2 ''return a list of words!! 3 # INSERT YOUR CODE BELOW 4 # Store each line as an elment of a list, then retunr the list # Hint, use strip method to remove 'in' 6 #~ 3 Lines 7 8 ... 9 def encrypt_words (words, codebook): 10 # INSERT YOUR CODE BELOW 11 # Look up code for each words and store them into a list Li 12 # N 3 Lines 13 14 return li 15 16 def decrypt_codes (code, codebook): 17 # INSERT YOUR CODE BELOW 18 # Look up words from code, and store them into a string 19 # ~ 4 Lines 20 21 22 # TESTCASES BELOW 23 cb = read_codebook) 24 li = encrypt_words('good morning', cb) 25 print(li) #[41879, 62760] 26 27 code = [44543, 0, 64852, 24327] 28 S = decrypt_codes (code, cb) 29 print(s) # have a nice day
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply