Python MyProgrammingLab 4 questions 1 Assume you have a vari
Python (MyProgrammingLab): 4 questions...
1. Assume you have a variable , budget, that is associated with a positive integer. Assume you have another variable , shopping_list, that is a tuple of strings representing items to purchase in order of priority. (For example: (\"codelab\", \"textbook\", \"ipod\", \"cd\", \"bike\")) Furthermore, assume you have a variable , prices that is a dictionary that maps items (strings such as those in your shopping_list) to positive integers that are the prices of the items.
Write the necessary code to determine the number of items you can purchase, given the value associated with budget, and given that you will buy items in the order that they appear in the tuple associated with shopping_list. Associate the number of items that can be bought with the variable number_of_items.
What I have so far:
budget = 500
moneyToSpend = budget
shopping_list = (\"codelab\", \"textbook\", \"ipod\", \"cd\", \"bike\")
prices = { \"codelab\":20, \"textbook\":18, \"ipod\":15, \"cd\":23, \"bike\":50 }
number_of_items = 0
min_price = min(prices.values());
while moneyToSpend > min_price:
for item in shopping_list:
if prices[item] <= moneyToSpend:
number_of_items += 1
moneyToSpend -= prices[item]
print \"with a budget of %s we can buy %s items\" %(budget,number_of_items)
2. Given a set, weights, and an integer desired_weight, remove the element of the set that is closest to desired_weight (the closest element can be less than, equal to OR GREATER THAN desired_weight), and associate it with the variable actual_weight. For example, if weights is (12, 19, 6, 14, 22, 7) and desired_weight is 18, then the resulting set would be (12, 6, 14, 22, 7) and actual_weight would be 19. If there is a tie, the element LESS THAN desired_weight is to be chosen. Thus if the set is (2, 4, 6, 8, 10) and desired_weight is 7, the value chosen would be 6, not 8. Assume there is at least one value in the set.
What I have so far:
3. Suppose there is a class Roster. Roster has one variable , roster, which is a List of tuples containing the names of students and their number grades--for example, [(\'Richard\', 90), (\'Bella\', 67), (\'Christine\', 85), (\'Francine\', 97)].
Roster has a function findValedictorian that returns the name of the student with the highest grade. Find the valedictorian of the Roster object englishClass and store it in the variable valedictorian.
What I have so far:
4. There is a class called Roster whose constructor takes a List of tuples with the names of students and their grades in the class, for example -- [(\'Richard\', 98), (\'Benny\', 55), (\'Katie\', 87), (\'Sally\', 76)].
Create an instance of the Roster class, including the following students and their respective grades, and store it in the variable mathClass: Jacob, 65 Frankie, 86 Lina, 94 Arnold, 63 Amanda, 87
What I have so far:
Assume you have a variable , budget, that is associated with a positive integer. Assume you have another variable , shopping_list, that is a tuple of strings representing items to purchase in order of priority. (For example: (\"codelab\", \"textbook\", \"ipod\", \"cd\", \"bike\")) Furthermore, assume you have a variable , prices that is a dictionary that maps items (strings such as those in your shopping_list) to positive integers that are the prices of the items.
Write the necessary code to determine the number of items you can purchase, given the value associated with budget, and given that you will buy items in the order that they appear in the tuple associated with shopping_list. Associate the number of items that can be bought with the variable number_of_items.
What I have so far:
budget = 500
moneyToSpend = budget
shopping_list = (\"codelab\", \"textbook\", \"ipod\", \"cd\", \"bike\")
prices = { \"codelab\":20, \"textbook\":18, \"ipod\":15, \"cd\":23, \"bike\":50 }
number_of_items = 0
min_price = min(prices.values());
while moneyToSpend > min_price:
for item in shopping_list:
if prices[item] <= moneyToSpend:
number_of_items += 1
moneyToSpend -= prices[item]
print \"with a budget of %s we can buy %s items\" %(budget,number_of_items)
Given a set, weights, and an integer desired_weight, remove the element of the set that is closest to desired_weight (the closest element can be less than, equal to OR GREATER THAN desired_weight), and associate it with the variable actual_weight. For example, if weights is (12, 19, 6, 14, 22, 7) and desired_weight is 18, then the resulting set would be (12, 6, 14, 22, 7) and actual_weight would be 19. If there is a tie, the element LESS THAN desired_weight is to be chosen. Thus if the set is (2, 4, 6, 8, 10) and desired_weight is 7, the value chosen would be 6, not 8. Assume there is at least one value in the set.
What I have so far:
Suppose there is a class Roster. Roster has one variable , roster, which is a List of tuples containing the names of students and their number grades--for example, [(\'Richard\', 90), (\'Bella\', 67), (\'Christine\', 85), (\'Francine\', 97)].
Roster has a function findValedictorian that returns the name of the student with the highest grade. Find the valedictorian of the Roster object englishClass and store it in the variable valedictorian.
What I have so far:
There is a class called Roster whose constructor takes a List of tuples with the names of students and their grades in the class, for example -- [(\'Richard\', 98), (\'Benny\', 55), (\'Katie\', 87), (\'Sally\', 76)].
Create an instance of the Roster class, including the following students and their respective grades, and store it in the variable mathClass: Jacob, 65 Frankie, 86 Lina, 94 Arnold, 63 Amanda, 87
What I have so far:
Solution
Here is your code with outputs.
1. Answer:
# your code
budget = 500 #varaible golding the budget
moneyToSpend = budget
shopping_list = (\"codelab\", \"textbook\", \"ipod\", \"cd\", \"bike\")
prices = { \"codelab\":20, \"textbook\":18, \"ipod\":15, \"cd\":23, \"bike\":50 }#doctionary for the tuples of shoppinglist
number_of_items = 0
min_price = min(prices.values());
while moneyToSpend > min_price: #using while loop for looping through shoppinglist
for item in shopping_list:
if prices[item] <= moneyToSpend: # if price of item is less than the budget
number_of_items += 1 # number of items is increased
moneyToSpend -= prices[item] #budget money is decreased with the amout that we just used up to buy the item
print \"with a budget of %s we can buy %s items\" %(budget,number_of_items) #printing on screen
OUTPUT:
with a budget of 500 we can buy 21 items
2. Answer:
# your code
weights = [2,4,6,8,10]
desired_weight = 7
actual_weight = weights[0]
ind = 0
weightDiff = abs(desired_weight - actual_weight)
weightToRemove = weights[0]
sign = (abs(desired_weight - actual_weight))/(desired_weight - actual_weight)
for w in weights:
if abs(desired_weight - w) < weightDiff:
weightDiff = abs(desired_weight - w) #abs() for getting the positive value
weightToRemove = w
sign = (abs(desired_weight - w))/(desired_weight - w)
elif abs(desired_weight - w) == weightDiff:
if w - desired_weight < 0 and sign > 0:
weightDiff = abs(desired_weight - w)
weightToRemove = w
sign = (abs(desired_weight - w))/(desired_weight - w)
weights.remove(weightToRemove)
print weights
OUTPUT:
[2, 4, 8, 10]
3. Answer:
# your code
class Roster:
def __init__(self, rostr):
self.roster = rostr
def findValedictorian(self):
ind = 0
maxInd = 0
maxi = self.roster[0][1]
for ros in self.roster:
if ros[0][1] > maxi: #checking if the mark is greater than the previous mark
maxi = ros[0][1]
maxInd = ind
ind = ind+1
return self.roster[maxInd]
englishClass = Roster([(\'Richard\', 90), (\'Bella\', 67), (\'Christine\', 85), (\'Francine\', 97)])
valedictorian = englishClass.findValedictorian()
print valedictorian
OUTPUT:
(\'Francine\', 97)
4. Answer :
# your code
class Roster:
def __init__(self, rostr):
self.roster = rostr # assigning the passed value to a variable of the class
def getRoster(self):
return self.roster
mathClass = Roster([(\'Jacob\', 65), (\'Frankie\', 86), (\'Lina\', 94), (\'Arnold\', 63), (\'Amanda\', 87)])
print(\"mathClass.roster :\",mathClass.getRoster())
OUTPUT:
mathClass.roster : [(\'Jacob\', 65), (\'Frankie\', 86), (\'Lina\', 94), (\'Arnold\', 63), (\'Amanda\', 87)]



