程序代写代做代考 ‘


4. (14 points) Write a short but complete program to do a little sorting. Ask the user for a material and a length in inches for an object. Then print out the identity of the object based on the following:
• If the material is wood, then print out peg
• If the material is metal and less than 6 inches long then print out bolt
• if the material is metal is at least 6 inches but no more than 12 inches long then print out rod • otherwise, print out unknown
You can use if constructs including elif and else clauses, but do not use loops. Make sure your program handles both upper and lower case input.
Here are some example runs (“Material: ” and “Length: ” are the input prompts):
Material: wOOd
Length: 124
peg
Solution:
material = input(“Material: “).upper().strip()
length = float(input(“Length: “))
if material == “WOOD”:
print(‘peg’)
elif material == “METAL” and length < 6: print("bolt") elif material == "METAL" and length <= 12: print("rod") else: print("unknown") Material: MEtal Length: 124 unknown Material: METAL Length: 6.0 rod 6 5. (18 points) This question is in 2 parts, but you can assume all of the code is in a single file and can put both parts in the single answer box below. Part a. (8 of 18 points) Write a function evolve(astring) that takes in a string of dots "." and stars "*" and returns the result of applying the following string transformations in order: (a) Change all occurences of the substring "*.*" to "***" (b) Change all occurences of the substring "**.**" to "..*.." (c) Change all occurences of the substring "..*.." to ".*.*." (d) Change all occurences of the substring "*.*" to "***" (e) Change all occurences of the substring "*****" to "*.*.*" Part b. (10 of 18 points) Now write a short program to input a string from the user and call your function 3 times with the output from the first call becoming the input to the second call and the output from the second call becoming the input to the third call. So if the input string is ”**********”, the sequence of calls would be: (a) evolve("**********") returns '* * ** * *' (b) evolve('* * ** * *') returns '*** * * **' (c) evolve('*** * * **') returns '* * * ****' At the end, print out the final string followed by "It grew" if there are more "*"s at the end than at the beginning, "It shrunk" if there are fewer, and "Stable" if it is the same. Here are three runs. Note that Enter *s: is the input prompt. Enter *s: ...*... ..***.. It grew Enter *s: ********** *.*.*.**** It shrunk Enter *s: *.*.*...** *.*.*...** Stable Write your answer to a and b on the next page. 7 Solution: def evolve(pattern): return (pattern.replace("*.*", "***"). replace("**.**", "..*.."). replace("..*..", ".*.*."). replace("*.*", "***"). replace("*****", "*.*.*")) x = input("Enter *s: ") start = x.count("*") x = evolve(x) x = evolve(x) x = evolve(x) end = x.count("*") print(x) if end > start:
print(“It grew”)
elif end < start: print("It shrunk") else: print("Stable") 8 6. (18 points) Tuples are often used to represent points. This question will require you to use tuples in functions to represent (x,y) coordinates. Part a. (6 of 18 points) Write a function midpoint(a, b) in file segment.py that takes two tuples in parameters a = (x0,y0) and b = (x1,y1) and returns a new tuple with the location of the midpoint of the segment connecting a and b. Note: the midpoint is just ( x0 +x1 , y0 +y1 ). 22 For example: >>> print(midpoint((20, -10),(40, 11))) Should print:
(30.0, 0.5)
Solution:
def midpoint(a, b):
return (a[0]+b[0])/2, (a[1]+b[1])/2
9

Part b. (12 of 18 points) Now write a short program in a separate file main.py to call your function. You should first prompt the user for 4 float values, x0, y0, x1 ans y1, then call your function to calculate the midpoint and print it out. Remember that your midpoint function is in the segment module in the file segment.py.
A possible run of your program is shown below. Note that the first 4 lines are the input prompts and values entered.
x0: 20
y0: -10
x1: 40
y1: 11
Midpoint: (30.0, 0.5)
Solution:
import segment
x0 = float(input(“x0: “))
y0 = float(input(“y0: “))
x1 = float(input(“x1: “))
y1 = float(input(“y1: “))
print()
print(“Midpoint:”,segment.midpoint((x0, y0), (x1, y1)))
10

This page is left blank for scratch work. Do not put your solutions here. Put them in the boxes provided for each question.
11

This page is left blank for scratch work. Do not put your solutions here. Put them in the boxes provided for each question.
12