Answered step by step
Verified Expert Solution
Question
1 Approved Answer
in python code, please help Exercise 4 Similar to the previous solid, filled-in ASCII art shapes of last week and the week before, this time
in python code, please help
Exercise 4 Similar to the previous solid, filled-in ASCII art shapes of last week and the week before, this time you have to draw only the border of a rectangle. The + plus character is used for the corners, the - dash character for horizontal lines, and the I pipe character for vertical lines. Write a drawBorder(width, height) function that draws the border of a rectangle with the given integer sizes, assuming both dimensions are 2 or more. Don't forget to add an appropriate docstring. See below sample runs. 3>> dramBorder (1,5) Incorrect paraneters, cannot draw such a rectangle! 3 drawBorder (4,4) Incorrect parameters, cannot draw such a rectangle! 3> drawBorder (2,2) + ++ drawBorder (10,5) help(drawBorder) Help on function drawborder in module _main_: draviorder(width, height) Draws the borders of a rectangle using ASCII art. Exercise 5 Write a median function that has a numbers parameter. numbers is a list of numbers. This function returns the statistical median of the numbers list. The median of an odd-length list is the number in the middlemost number when the list is in sorted order. If the list has an even length, the median is the average of the two middlemost numbers when the list is in sorted order. You can use Python's builtin sort() method to sort the numbers list. Passing an empty list to medi an should cause it to return None. Don't forget to add an appropriate docstring. To test your function try the below assert statements (copy paste them to your code in the main function). assert median ([])= None, "Empty list test failed!" assert median ([1,2,3])=2, "Test 2 failed." assert median ([3,7,10,4,1,9,6,5,2,8])=5.5, "Test 3 failed." assert median ([3,7,10,4,1,9,6,2,8])=6, "Test 4 failed." import random random.seed (42) testData =[3,7,10,4,1,9,6,2,8] for i in range (106e): random, shuffle(testData) assert median(testData) ==6 print("All tests succesfull!") Exercise 6 Write a closest Average function that has a numbers parameter. numbers is a list of numbers. This function returns the element closest to the average of the numbers list. You can use Python's built-in sum() methodif you wish. Passing an empty list to closestAverage should cause it to return None. Don't forget to add an appropriate docstring. To test your function try the below assert statements (copy paste them to your code in the main function). assert closestAverage ([])== None, "Empty list test failed!" assert closestAverage ([1,2,3])==2, "Test 2 failed." assert closestAverage ([3,7,10,4,1,9,5,6,2,8])=5, "Test 3 failed." assert closestAverage ([3,7,10,4,1,9,6,2,8])=6, "Test 4 failed." import random random,seed (42) testData =[3,7,10,4,1,9,6,2,8] for i in range(16e0): random. shuffle(testData) assert closestAverage(testData) ==6 print("All tests succesfull!") are forbidden to use random. shuffle. Other functions from the random module would be useful of course, such as random. randrange( ) or random. randint (). Optional: It could be easier to start by a function that shuffles a list, then use the function shuffleList to shuffle the indices of the string. Don't forget docstrings! To check that your function works well, run it several times on the same input and check that you get different results each time, as per the below sample runs: shufflestring("programming") 'miargpmngor' shufflestring("programming") 'rimmogprgan' Exercise 8 . The goal of this exercise is to program a game of rock paper scissor. You should ask the user how many games he wants to play and, in each game he should choose rock or paper or scissors, the computer will randomly choose rock, paper or scissors and then decide who wirs or if it is a tie. At the end the program should display the total score (in each game, if the computer wins, you score 0 , if it is a tie you score 1 , if you win you score 3 ). You need to validate the input as well. If you know how to do it as in the sample run below you can solve it directly, otherwise you can look at a step-by-step approach on the other lab document. Sample run: How many games of Rock, Paper scissors do you want to play? Five This not a valid number of ganes! Number of ganes should be a positive Integer How many games of Rock, Paper Scissors do you want to play? 5.5 This not a valid nunber of games! Number of games should be a positlve integer How many games of Rock, Paper Scissors do you want to play? 5 Game I ** What do you choose (rock or paper or scissors)?r Choose rock or paper or scissorsl What do you choose (rock or paper or scissors)?1 Choose rock or paper or scissorst What do you choose (rock or paper or scissors)?rock Computer chose scissors. You win this round! Game 2 What do you choose (rock or paper or scissors)?rock Computer chose scissors You win this round! Game 3 *** What do you choose (rock or paper or scissors)?rock Computer chose paper Computer wins this round! *** Game 4 *** What do you choose (rock or paper or scissors)?rock Computer chose rock This round is a tie... Game 5 *** What do you choose (rock or paper or scissors)?rock Computer chose paper Computer wins this round! End of the game Your score is 7 Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started