Microsoft Word - homework2.docxData Focused Python XXXXXXXXXXHomework 2 Due: 11:59 PM Sunday, November 6, 2022 This is an individual assignment. Create a text file named _hw2.zip to store...

1 answer below »
in python


Microsoft Word - homework2.docx Data Focused Python Homework 2 Due: 11:59 PM Sunday, November 6, 2022 This is an individual assignment. Create a text file named _hw2.zip to store the two .py files containing the answers to the problems below; for example, if you Andrew id is "[email protected]", then name the file "fredw_hw2.zip". Write your name and andrew id as Python comments in each .py file. 1. Download the file 'dictionary.txt'. This is a text file (*not* a CSV file) of words and their definitions. Write a program (not a script) named hw21.py with the following functions: createDictionary(f): Create a dict named dictionary from this data by reading each line and finding the first left parenthesis '(' in the line, and then finding the first ')' after the left parentheses (in other words, skip over the part-of-speech stuff given in the parentheses). Break the line into two slices at that point: everything before the left parenthesis – but strip out trailing white space – and everything from the right parenthesis to the end of the line. The first part is a word; the rest is its definition. Store the capitalized version of the word to normalize searching. Here's an example: Keel (n.) Fig.: The whole ship. Because words have multiple definitions, your dict's setup should be {word:[list of definitions]} pairs. The first time you encounter a word, enter its first definition with the word as the key and a list with just the first definition as the value. Any subsequent times you encounter that word, append the new definition to the list. Do all of this inside a function named createDictionary(f), where f is the file variable of the already-opened file; it should return the dictionary. This is one definition of the word Keel; it has more definitions that will follow the first entry in the text file. displayDefinitions(word, definitionList): This is for displaying the results of a search. Simply display the word, then loop over the definitionList to display a numbered table of the definitions of that word. For example, when you search for KEEL, this is what should be displayed. Note that the word from the dictionary is in capitalized; the word entered by the user may not be. The user prompt is done in search( ), but the display is done in this function. Enter a word to look up, ALLDONE to quit: Keel KEEL: 1: To cool; to skim or stir. 2: A brewer's cooling vat; a keelfat. 3: A longitudinal timber, or series of timbers scarfed together, extending from stem to stern along the bottom of a vessel. It is the principal timber of the vessel, and, by means of the ribs attached on each side, supports the vessel's frame. In an iron vessel, a combination of plates supplies the place of the keel of a wooden ship. See Illust. of Keelson. 4: Fig.: The whole ship. find this ( to break it into KEEL and the rest: slice the line based on ('s index find this ) to break the rest into the definition based on )’s index JTW 5: A barge or lighter, used on the Type for carrying coal from Newcastle; also, a barge load of coal, twenty-one tons, four cwt. 6: The two lowest petals of the corolla of a papilionaceous flower, united and inclosing the stamens and pistil; a carina. See Carina. 7: A projecting ridge along the middle of a flat or curved surface. 8: To traverse with a keel; to navigate. 9: To turn up the keel; to show the bottom. search(d): takes one parameter, the dictionary. It prompts the user for a word (in any case), then looks up the word in dictionary and calls displayDefinitions( ) to display the word and definitions (as shown above) or prints ' not found' if it's not in the dictionary. Keep prompting the user until they type ALLDONE. The function should return two values: the number of successful searches and the number of unsuccessful searches. main( ): This should first open the data file in a try block – if unsuccessful, display an error message and call sys.exit(0) – you'll have to import sys. Then call createDictionary(f) where f is the opened file; it returns the dictionary. Next, show the word with the largest number of definitions – print the word and that number, like: KEEL has the most definitions, 9. (It’s not KEEL, this is just sample output.) To do this, use dictionary.items( ), which returns a list of [key, [definitions]] sublists. Write a lambda expression that, given two such sublists, compares the length of the definitions list and returns the sublist whose [definitions] has the most entries. Then use reduce( ) to apply the lambda to dictionary.items( ). The result of the reduction will have the word and its definitions; use it to print the display above. You might want to try it out on a small sample dictionary of {key:[list of strings]} first. Next, call search( ). Finally, display the values returned by search with labels – the number of successful and unsuccessful searches. Copy this code *after* main: if __name__ == '__main__': main() This is standard Python code that is required to run your main( ) function. 2. Write a script named hw22.py that implements the following simulation. A first-cut, not very accurate linear model of predator-prey population change uses a 2x2 matrix to store how the populations change in one time period (adapted from Applied Linear Algebra, Noble and Daniel). (Note: the natural process is nonlinear.) For this simulation, foxes and chickens coexist in a closed ecosystem. Foxes like to eat chickens and their population increases if they can do that, but their population decreases if they don't (in this simplified model, foxes don't eat anything else). Chickens are normally fairly prolific but can be decimated by foxes. Given the starting populations of foxes and chickens, the new populations at the next time period are given by the equations: new fox population = (fox growth factor)*old fox population + (eat chickens rate)*old chicken population, new chicken population = (kill rate)*old fox population + (chicken growth factor)*old chicken population The growth factors take into account the birth rate and the death rate for each population. The factor for foxes eating chickens means that the fox population increases if chickens are available to be eaten. The kill rate tells how many chickens are lost to fox predation, and this factor will be a variable set by the user to experiment with the model. All this can be written in matrix form as !?′?′% = ( ? ? −? ? . ( ? ?. where x and y are the initial populations of foxes and chickens (for this problem, use initial populations of 100 foxes and 1000 chickens), x' and y' are the new populations after one time period, f and c are the growth factors for foxes and chickens (for example, f = 0.6 and c = 1.2 might be used: foxes tend to decrease but chickens tend to increase), e is the eat-chicken growth rate (here, e = 0.5 could be used), and k is the kill rate (entered as a positive value by the user, but negated in the matrix to indicate dying off; k = 0.8 could be used). Write a program that implements the above equation using numpy nd-arrays. Set x and y to 100 and 1000 initially. Prompt the user for the values – fox and chicken growth factors (f and c), the eat-chicken growth rate e, kill rate k and n, the number of time periods to run the simulation. Make sure you use -k in the matrix, even though the user will enter a positive value. Then repeat the matrix computation for the number of periods n, with the same 1-D matrix (x, y) as both the right-side and left-side values – that is, you're replacing the old population numbers with the new population numbers. However, if either population becomes non-positive, stop the simulation (it's okay to use a break statement for this) and display a message telling which one (or both) went non-positive. Display the new population values on each iteration, for example: Enter the fox growth rate: 0.6 Enter the chicken growth rate: 1.2 Enter the eat-chicken rate: 0.5 Enter kill rate: 0.1 Enter number of iterations: 2 Time period # foxes # chickens 0 100 1000 1 560 1190 2 931 1372 For this kill rate, both populations are increasing (again, -0.1 is entry [1][0] of the array) – feeding the foxes without decimating the chickens. Try your program out with other kill rates and number of iterations, including k = 0.18, the same f and c values above, and n = 100. If the either population goes negative, that means they've died out; that's why you end the simulation. A () The first letter of the English and of many other alphabets. The capital A of the alphabets of Middle and Western Europe, as also the small letter (a), besides the forms in Italic, black letter, etc., are all descended from the old Latin A, which was borrowed from the Greek Alpha, of the same form; and this was made from the first letter (/) of the Phoenician alphabet, the equivalent of the Hebrew Aleph, and itself from the Egyptian origin. The Aleph was a consonant letter, with a guttural breath sound that was not an element of Greek articulation; and the Greeks took it to represent their vowel Alpha with the a sound, the Phoenician alphabet having no vowel
Answered 2 days AfterNov 04, 2022

Answer To: Microsoft Word - homework2.docxData Focused Python XXXXXXXXXXHomework 2 Due: 11:59 PM Sunday,...

Sathishkumar answered on Nov 06 2022
49 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here