Question

In: Computer Science

Please write a python code for the following. Use dictionaries and list comprehensions to implement the...

Please write a python code for the following. Use dictionaries and list comprehensions to implement the functions defined below. You are expected to re-use these functions in implementing other functions in the file. Include a triple-quoted string at the bottom displaying your output.

Here is the starter outline for the homework:

g.

def big_words(text, min_length=10):

""" Return a list of big words whose length is at least min_length """

return []

h.

def common_words(text, min_frequency=10):

""" Return words occurring at least min_frequency times

as a list of (word, count) pairs """

return []

i.

def most_common_big_word(text, min_word_size=5):

""" Returns the most common BIG word and its frequency

as a tuple. A big word is defined as having a

length >= min_word_size """

return ("some_big_word", 1000)

j.

def avg_word_length(text):

""" Returns the average length (number of letters) of the words in text """

return 0.0

gettysburg_address = """

Four score and seven years ago our fathers brought forthon this continent, a new nation, conceived in Liberty,and dedicated to the proposition that all men are createdequal. Now we are engaged in a great civil war, testingwhether that nation, or any nation so conceived and sodedicated, can long endure. We are met on a greatbattlefield of that war. We have come to dedicate aportion of that field, as a final resting place forthose who here gave their lives that that nation mightlive. It is altogether fitting and proper that we shoulddo this. But, in a larger sense, we can not dedicate, wecan not consecrate, we can not hallow this ground.The brave men, living and dead, who struggled here,have consecrated it, far above our poor power to addor detract. The world will little note, nor longremember what we say here, but it can never forget whatthey did here. It is for us the living, rather, to bededicated here to the unfinished work which they whofought here have thus far so nobly advanced. It israther for us to be here dedicated to the great taskremaining before us—that from these honored dead wetake increased devotion to that cause for which theygave the last full measure of devotion that we herehighly resolve that these dead shall not have died invain that this nation, under God, shall have a newbirth of freedom and that government of the people, bythe people, for the people, shall not perish from theearth.

"""

Solutions

Expert Solution

Python Code:

def big_words(text, min_length=10):

    """ Return a list of big words whose length is at least min_length """

    text = text.replace('.', ',').replace(' ', ',').replace('\n',',')

    returnList = [string for string in text.split(',') if len(string)>=min_length]

    return returnList

def common_words(text, min_frequency=10):

    """ Return words occurring at least min_frequency times

    as a list of (word, count) pairs """

    countDict = {}

    text = text.replace('.', ',').replace(' ', ',').replace('\n',',')

    for string in text.split(','):

        if string in countDict.keys():

            countDict[string] = countDict[string] + 1

        else:

            countDict[string] = 0

    returnList = [(key,countDict[key]) for key in countDict.keys() if countDict[key]>=min_frequency and key != '']

    return returnList

def most_common_big_word(text, min_word_size=5):

    """ Returns the most common BIG word and its frequency

    as a tuple. A big word is defined as having a

    length >= min_word_size """

    bigWords = big_words(text,min_word_size)

    comwords = common_words(text, 0)

    comBigCount = [count for word,count in comwords if word in bigWords]

    mostComBigWord = [word for word,count in comwords if count == max(comBigCount) and word in bigWords ]

    mostComBigWordCount = [count for word,count in comwords if count == max(comBigCount) and word in bigWords ]

    if len(mostComBigWord)>0:

        return (mostComBigWord[0], mostComBigWordCount[0])

    else:

        return ('','')

def avg_word_length(text):

    """ Returns the average length (number of letters) of the words in text """

    text = text.replace('.', ',').replace(' ', ',')

    strings = [len(string) for string in text.split(',') if string != '']

    return sum(strings)/len(strings)

gettysburg_address = """

Four score and seven years ago our fathers brought forthon this continent, a new nation, conceived in Liberty,and dedicated to the proposition that all men are createdequal. Now we are engaged in a great civil war, testingwhether that nation, or any nation so conceived and sodedicated, can long endure. We are met on a greatbattlefield of that war. We have come to dedicate aportion of that field, as a final resting place forthose who here gave their lives that that nation mightlive. It is altogether fitting and proper that we shoulddo this. But, in a larger sense, we can not dedicate, wecan not consecrate, we can not hallow this ground.The brave men, living and dead, who struggled here,have consecrated it, far above our poor power to addor detract. The world will little note, nor longremember what we say here, but it can never forget whatthey did here. It is for us the living, rather, to bededicated here to the unfinished work which they whofought here have thus far so nobly advanced. It israther for us to be here dedicated to the great taskremaining before us—that from these honored dead wetake increased devotion to that cause for which theygave the last full measure of devotion that we herehighly resolve that these dead shall not have died invain that this nation, under God, shall have a newbirth of freedom and that government of the people, bythe people, for the people, shall not perish from theearth.

"""

print(big_words(gettysburg_address))

print(common_words(gettysburg_address))

print(most_common_big_word(gettysburg_address))

print(avg_word_length(gettysburg_address))

Sample Output:

['proposition', 'createdequal', 'testingwhether', 'sodedicated', 'greatbattlefield', 'altogether', 'consecrate', 'consecrated', 'longremember', 'bededicated', 'unfinished', 'taskremaining', 'herehighly', 'government']
[('that', 11)]
('nation', 4)
4.663967611336032


Related Solutions

Please write a python code for the following. Use dictionaries and list comprehensions to implement the...
Please write a python code for the following. Use dictionaries and list comprehensions to implement the functions defined below. You are expected to re-use these functions in implementing other functions in the file. Include a triple-quoted string at the bottom displaying your output. Here is the starter outline for the homework: a. def count_character(text, char): """ Count the number of times a character occurs in some text. Do not use the count() method. """ return 0 b. def count_sentences(text): """...
Write a code to implement a python queue class using a linked list. use these operations...
Write a code to implement a python queue class using a linked list. use these operations isEmpty • enqueue. • dequeue    • size Time and compare the performances of the operations ( this is optional but I would appreciate it)
Write a code to implement a python stack class using linked list. use these operations isEmpty...
Write a code to implement a python stack class using linked list. use these operations isEmpty   • push. • pop.   • peek. • size Time and compare the performances ( this is optional but I would appreciate it)
1. Dictionaries and Lists. a) Implement a list of student dictionaries containing the following data: name:...
1. Dictionaries and Lists. a) Implement a list of student dictionaries containing the following data: name: Andy, classes: ET580 MA471 ET574 name: Tim, classes: ET574 MA441 ET575 name: Diane, classes: MA441 MA471 ET574 name: Lucy, classes: ET574 ET575 MA471 name: Steven, classes: ET574 MA441 ET580 b) Implement a dictionary of courses and set each courses enrollment to 0: ET580: 0 ET574: 0 ET575: 0 MA441: 0 MA471: 0 c) Use a loop and if statements to read class data from...
Please write python code for the following. Implement the functions defined below. Include a triple-quoted comments...
Please write python code for the following. Implement the functions defined below. Include a triple-quoted comments string at the bottom displaying your output. Using sets (described in Deitel chapter 6) will simplify your work. Below is the starter template for the code: def overlap(user1, user2, interests): """ Return the number of interests that user1 and user2 have in common """ return 0    def most_similar(user, interests): """ Determine the name of user who is most similar to the input user...
Please write in Python code Write a program that stores the following data in a tuple:...
Please write in Python code Write a program that stores the following data in a tuple: 54,76,32,14,29,12,64,97,50,86,43,12 The program needs to display a menu to the user, with the following 4 options: 1 – Display minimum 2 – Display maximum 3 – Display total 4 – Display average 5 – Quit Make your program loop back to this menu until the user chooses option 5. Write code for all 4 other menu choices
Using python as the coding language please write the code for the following problem. Write a...
Using python as the coding language please write the code for the following problem. Write a function called provenance that takes two string arguments and returns another string depending on the values of the arguments according to the table below. This function is based on the geologic practice of determining the distance of a sedimentary rock from the source of its component grains by grain size and smoothness. First Argument Value Second Argument Value Return Value "coarse" "rounded" "intermediate" "coarse"...
in python please Q1) Create a Singly link list and write Python Programs for the following...
in python please Q1) Create a Singly link list and write Python Programs for the following tasks: a. Delete the first node/item from the beginning of the link list b. Insert a node/item at the end of the link list c. Delete a node/item from a specific position in the link list Q2) Create a Singly link list and write a Python Program for the following tasks: a. Search a specific item in the linked list and return true if...
In python please write the following code the problem. Write a function called play_round that simulates...
In python please write the following code the problem. Write a function called play_round that simulates two people drawing cards and comparing their values. High card wins. In the case of a tie, draw more cards. Repeat until someone wins the round. The function has two parameters: the name of player 1 and the name of player 2. It returns a string with format '<winning player name> wins!'. For instance, if the winning player is named Rocket, return 'Rocket wins!'.
Please write in beginner level PYTHON code! Your job is to write a Python program that...
Please write in beginner level PYTHON code! Your job is to write a Python program that asks the user to make one of two choices: destruct or construct. - If the user chooses to destruct, prompt them for an alternade, and then output the 2 words from that alternade. - If the user chooses construct, prompt them for 2 words, and then output the alternade that would have produced those words. - You must enforce that the users enter real...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT