Docsity

codehs unit 7 python questions and answers Graded A+, Exams of Algorithms and Programming

Toggle sidebar
bg1
codehs unit 7 python questions and answers
Graded A+
7.1.7 Fix This Tuple - ✅✅my_tuple = (0, 1, 2, "hi", 4, 5)
# Your code here...
my_tuple = my_tuple[:3] + (3,) + my_tuple[4:]
print(my_tuple)
7.1.8: Citation - ✅✅def citation(names):
author_name = ((names))
name = str(names[2]) + ", " + str(names[0]) + " " + str(names[1])
return name
print(citation(["Martin", "Luther", "King, Jr."]))
7.1.9: Diving Contest - ✅✅# fill in this function to return the total of the three
judges' scores!
judges_scores=(10,10,10)
def calculate_score(judges_scores):
return judges_scores[0]+judges_scores[1]+judges_scores[2]
print (calculate_score(judges_scores))
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download codehs unit 7 python questions and answers Graded A+ and more Exams Algorithms and Programming in PDF only on Docsity!

codehs unit 7 python questions and answers

Graded A+

7.1.7 Fix This Tuple - ✅✅my_tuple = (0, 1, 2, "hi", 4, 5)

Your code here...

my_tuple = my_tuple[:3] + (3,) + my_tuple[4:] print(my_tuple) 7.1.8: Citation - ✅✅def citation(names): author_name = ((names)) name = str(names[2]) + ", " + str(names[0]) + " " + str(names[1]) return name print(citation(["Martin", "Luther", "King, Jr."])) 7.1.9: Diving Contest - ✅✅# fill in this function to return the total of the three judges' scores! judges_scores=(10,10,10) def calculate_score(judges_scores): return judges_scores[0]+judges_scores[1]+judges_scores[2] print (calculate_score(judges_scores))

7.1.10: Coordinate Pairs - ✅✅import math

fill in this function to return the distance between the two points!

first_point = (1, 1) second_point = (4, 5) def distance(first_point, second_point): x1= first_point[0] x2= second_point[0] y1= first_point[1] y2= second_point[1] power1 = pow(y2 - y1, 2) power2 = pow(x2 - x1, 2) return math.sqrt (power1 + power2) print (distance(first_point, second_point)) 7.2.6: Spell It Out - ✅✅# fill in this function to return a list containing each character in the name name = "Jessica" def spell_name(name): spell_out = list(name)

return 0 my_list = [1, 3, 456, 2038] def max_int_in_list(my_list): highest = my_list[-1] for num in my_list: if num > highest: highest = num return highest biggest_int = max_int_in_list(my_list) print (biggest_int) 7.3.7: Owls - ✅✅# this function should return the number of words that contain "owl"! def owl_count(text): return 0 text ="I really like owls. Did you know that an owl's eyes are more than twice as big as the eyes of other birds of comparable weight? And that when an owl partially closes its eyes during the day, it is just blocking out light? Sometimes I wish I could be an owl."

word ="owl" def owl_count(text): text=text.lower() owlist=list(text.split()) count=text.count(word) return count print (owl_count(text)) 7.3.8: Exclamat!on Po!nts - ✅✅def exclamation(text): return text text = "I like music. What is iodine?" def exclamation(text): my_list = list(text) output = "" for item in my_list: if item == "i": output = output + "!" else: output = output +item return output

if len(user_letter) > 1: print("Must be exactly one character!") continue return user_letter while True: user_string = list(user_string) returned_index = get_index() if returned_index == - 1: break returned_string = get_string() user_string[returned_index] = returned_string print("".join(user_string)) 7.4.4 How Many Names? - ✅✅nn = int(input("How many names do you have?:")) namelist = [] for i in range (nn): name = input("Name:") namelist.append(name) print ("First name:" + namelist[0]) print ("Middle names:" + str(namelist[1:-1]))

print ("Last name:" + namelist[-1]) 7.4.5 Five Numbers - ✅✅list=[] for i in range(5): number=int(input("Number:")) list.append(number) print(list) final=sum(list) print(final) 7.4.7 Librarian - ✅✅list=[] for i in range(5): name=input("Name:") list.append(name) list.sort() print(list) 7.4.12 Empty List (Append and Remove) - ✅✅my_list = []

7.4.14 Librarian, Part 2 - ✅✅list=[] for i in range (5): name = input("Name: ") list.append(name.split()[-1]) list.sort() print(list) 7.4.15 Owls, Part 2 - ✅✅text = input("Enter some text: ") text.lower() text.split() print("There are " + str(text.count("owl")) + " words that contained "owl".") print("They occured at indices: for c, value in enumerate(text, 1):") print("They occured at indices: ") for c, value in enumerate(text, 1): print(c, value)

7.5.4 Simulating a Coin Flip - ✅✅import numpy as np import random N = 10 epsilon = np.zeros(N) def coin_flip(): #flip coin for each n = 1,...,N flip = random.randint(0, 1) if (flip == 0): result = "Heads" else: result = "Tails" return result for n in range(N): result = coin_flip() if result == "Heads": epsilon[n] = 1 else: epsilon[n] = - 1 print(epsilon) coin_flip() print(result)