Part 1: Bunnies and Foxes, equilibriumAs promised, we will revisit the bunny and fox example from Lab #3. Remember we had formulafor computing the population of bunnies and foxes in the next year given the current population.Here is a slight variation of the functions from Lab # 3 combined into a single function that returnsthat takes as input the initial population of bunnies and foxes, and then computes the populationof the following year continuously using a WHILE LOOP. Your loop stops and outputs theremaining population of bunnies and foxes until any one of the following is true:(a) No bunnies or foxes (or both) are left, i.e. has population zero (convergence is true),(b) The population of bunnies and foxes in the current year is identical to the previous year, i.e.population equilibrium is reached (convergence is true),(c) None of the above is true, but you have computed exactly 100 generations (convergence isfalse).Your function, when finished computing the while loop must return a 4-tuple:(bpop, fpop, iter, converged)where bpop,fpop are the final populations of bunnies and foxes, iter is the total number ofiterations the while loop repeated to reach this number (i.e. number of times you called thenext_pop function), and converged is either True or False based on whether convergence wasachieved.Your functions must not have any global variables and you must use a while loop in the convergencefunction.Your program will use these functions by first asking the user for the current bunny and foxpopulation. Assume the input is a valid integer.Once a value is given for both populations, you must call function check_convergence(bpop, fpop)from your module and print out the returned values. Sample output from separate runs is givenbelow:Please enter the starting bunny population == 66Please enter the starting fox population == 77(Bunny, Fox): Start (6, 7) End: (30, 30), Converged: True in 8 generationsPlease enter the starting bunny population == 1010Please enter the starting fox population == 1010(Bunny, Fox): Start (10, 10) End: (29, 30), Converged: False in 100 generationsPlease enter the starting bunny population == 10001000Please enter the starting fox population == 22(Bunny, Fox): Start (1000, 2) End: (0, 41), Converged: True in 1 generationsWhen you are sure your program satisfy the requirements stated here, submit it.When you have tested your code, please submit it as hw5_part1.py.Part 2: Election Time in PythoniaIn the spirit of the budding election season, let us examine the current political struggles in thefar-off land of Pythonia. You will run a fantasy election in which candidates are trying to winvillages by obtaining more votes than their competitors. On election day, villagers head to thepolls and cast their vote, and the results pour in state-by-state. Before the next states outcome isannounced, you decide to run a simulation to see which of two running candidates will be rankedhigher based on the states voting turnout and the candidates current standing.Write a program to read from the user a comma-separated string that represents the currentstanding of two candidates in the election. The input is of the form:candidate name, states won, states tied, total representative votesCompute the candidates current standing (or rank) by adding up the following: each state won is worth 5 points each state with a tied-win is worth 2 points count half (rounding down) of the total representative votesOne candidate is ranked higher than another simply if this score is higher than the other candidates.Now you prepare for the different possible outcomes of the next states voting for the candidates.Each candidate can receive 0 to 4 representative votes from the villagers, so the possible outcomeslook like 1-0, 1-1, 0-1, 0-0, 2-0, 2-1, 2-2, 1-3, etc.In these outcomes, the candidate with more representative votes wins the state, and both candidatesreceive a tied win if the score is tied.For any possible outcome, compute which candidate will have the higher standing after winning orlosing and print the first letter of the winning candidates name as the result, or – if the candidatesthen have the same standing. You will print this information in a 5 by 5 board.For simplicity, assume the input will be in the correct format, with no spaces around commas.You must use for loops to accomplish this. The output below uses 5 spaces for each column.Here is a possible run of the program in Wing:Enter candidate 1 stats == Lovelace,5,1,28Lovelace,5,1,28Enter candidate 2 stats == Turing,4,3,29Turing,4,3,29Columns are Lovelaces votes, rows are Turings votesVotes| 0 1 2 3 4|-0 | L L L L L1 | T L L L2 | T T L L L3 | T T T L4 | T T T T LIn this case, Lovelace and Turing have two chances to be tied in rank if the outcome of this nextstate is 1-1 or 3-3. Here is another possible run:Enter candidate 1 stats == Engelbart,6,0,32Engelbart,6,0,32Enter candidate 2 stats == Babbage,3,1,22Babbage,3,1,22Columns are Engelbarts votes, rows are Babbages votesVotes| 0 1 2 3 4|-0 | E E E E E1 | E E E E E2 | E E E E E3 | E E E E E4 | E E E E EIn this case, Engelbart is already doing so well that even losing the state would not lower his rankbelow Babbage.You must use functions for this part. Keep in mind as you break down this part into steps thata function should aim to solve just one task so that main code and other functions can call itwhenever needed.Here are some suggestions of functions you might want to write for this part: make_board(candidate1, candidate2): return the board to print as a string (or list ofstrings) print_board(board): print the board in the above format winner(candidate1, candidate2, outcome): take two candidates information and onepossible outcome, and return the letter to print, the winnerThe idea here is to do calculations in one area, and then use the result for printing in another.Beware that such a winner function must not change the original candidates stats so that youmay call the function multiple times (for each possible outcome). The rest is easily accomplishedwith a double for loop.When you have tested your code, please submit it as hw5_part2.py.Part 3: Baby NamesIn this part, you will use real data from Social Security Administration on the number of timesa certain name is given to a babies born in the United States. The information is given in a file.We provide you with two separate files, one is a much shorter file for testing (babies_short.txt)and the second one is a very long file with all the baby names given between years 2000-2010(babies.txt).Each line in the file looks like this:Marnie,F,164Darrow,M,17This means that Marnie as a female name is given to 164 babies, and Darrow as a male name isgiven to 17 babies. Of course, it is possible for the some names to be given both to male and femalebabies.Write a program that first reads the file name to be used for baby names and a cutoff k from theuser. Your program then should find and print the top k most popular female baby names and thetop k most popular male baby names (and for each name, the number of times it is given). Youmust put a new line after every third name to make your output look pleasant.Here are possible expected outputs:File name = babies_short.txtbabies_short.txtHow many names to display? = 22Top female namesElsa (3353) Marnie (164)Top male namesAmilcar (238) Leshawn (157)File name = babies.txtbabies.txtHow many names to display? = 55Top female namesEmily (223590) Madison (193063) Emma (181156)Olivia (155935) Hannah (155607)Top male namesJacob (273642) Michael (250370) Joshua (231771)Matthew (221411) Daniel (203569)There is a very simple trick to solving this homework. The sort function in Python can sort listsand lists of lists (or tuples). For example, suppose we sort a list of tuples containing 2 elementseach:x = [ (2, a), (6, b), (4, c), (6, a), (1, f) ]x.sort()x[(1, f), (2, a), (4, c), (6, a), (6, b)]x.sort(reverse=True)x[(6, b), (6, a), (4, c), (2, a), (1, f)]As we can see from the above example, when we sort x, it will first sort by the first value in eachtuple. If the first value is equal, then it will sort by the second value. As a result, (6, a) comesbefore (6, b). You can also sort in descending order by using x.sort(reverse=True).If you want to find the top names, you must create a list that you can sort first by the frequencyof the name, then the name. In the unlikely case that two names have the same frequency, theirordering will be descending lexicographic order. We can fix them with a hack, but will skip thatfor now. You will learn very elegant ways to avoid this in your next class.When you have tested your code, please submit it as hw5_part3.py
CSCI 1100
[Solved] CSCI1100 Homework 5 Loops, Lists and Files
$25
File Name: CSCI1100_Homework_5_Loops__Lists_and_Files.zip
File Size: 395.64 KB
Only logged in customers who have purchased this product may leave a review.

![[Solved] CSCI1100 Homework 5 Loops, Lists and Files](https://assignmentchef.com/wp-content/uploads/2022/08/downloadzip.jpg)

![[Solved] CSCI 1100 Computer Science 1 Homework 7 Dictionaries](https://assignmentchef.com/wp-content/uploads/2022/08/downloadzip-1200x1200.jpg)
Reviews
There are no reviews yet.