Using python Create a file called quote.txt and put the following inside: Contra
ID: 3738821 • Letter: U
Question
Using python
Create a file called quote.txt and put the following inside: Contrary to popular belief, Unix is user friendly. It just happens to be very selective about who it decides to make friends with.' -unknown Create a script to: Try and open a file that doesn't exist. Print an error open the quote.txt file create a frequency dictionary of all the letters in the quote (same as task 2) create a list that is sorted by the frequency of the letters from highest to lowest Expected Output The file doesn't exist.Explanation / Answer
# sort srr2 according to arr1
def sort_arr1_according_to_arr2(arr2, arr1):
temp=sorted(zip(arr1,arr2),key=lambda x : x[1], reverse = True)
arr=[i[0] for i in temp]
dep=[i[1] for i in temp]
temp=sorted(zip(arr,dep),key=lambda x : x[0], reverse = True)
arr=[i[0] for i in temp]
dep=[i[1] for i in temp]
for i in range( 0, len(arr) ):
arr1[i] = arr[i]
arr2[i] = dep[i]
try:
#open file in read mode
ptr = open('input.txt', 'r')
# in case of exception
except:
print('The file doesn't exist')
#open file in read mode
fptr = open('quote.txt', 'r')
# store the frequence of each alphabet
# index 0 for a
# index 1 for b
# .
# .
# .
# index 25 for z
freq = [0] * 27
letter = []
for i in range(97, 123):
letter.append(chr(i))
# read the content of file into strng
strng = fptr.read()
# convert to lower case
strng = strng.lower()
for ch in strng:
# if current character is alphaber
if ch.isalpha():
# ord gives the ascii value of character
ascii = ord(ch)
# get the index of alphabet in freq
index = ascii - 97
freq[index] += 1
sort_arr1_according_to_arr2(letter, freq)
i = len(freq) - 1
while freq[i] == 0:
i -= 1
# remove the elements with frequency 0
freq = freq[ : i + 1]
letter = letter[ : i + 1]
print(letter)
print(freq)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.