Question

In: Computer Science

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.

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.

In Python

Solutions

Expert Solution

Here is the code:


budget = 50

prices={'codelab':25, 'textbook':20, 'ipad':15, 'cd':5}

shopping_list = ('codelab', 'textbook', 'ipad', 'cd')

number_of_items = 0

for item in shopping_list:
if(budget break
else:
number_of_items = number_of_items + 1
budget = budget - prices[item]

if(number_of_items==len(shopping_list)):
print('All', len(shopping_list), 'items can be purchased')
else:
print('Only', number_of_items, 'items can be purchased')

Output:


Related Solutions

C++ 3a)Assume you have an integer pointer variable named intpoint2. It contains the address 1e6f24. You...
C++ 3a)Assume you have an integer pointer variable named intpoint2. It contains the address 1e6f24. You execute intpoint2++. What does intpoint2 now contain? a)1e6f24 b)1e6f25 c)1e6f28 d)1e6f2c 3b)You have an array of integers: int myints[10]; and the address of the first integer is be4350. What is the address of myints[1]? myints[2]?You have an array of integers: int myints[10]; and the address of the first integer is be4350. What is the address of myints[1]? myints[2]? a)Both are b34350 b)be4350 and b34354...
Name the script while.sh. Ask the user to enter a positive integer. You can assume that...
Name the script while.sh. Ask the user to enter a positive integer. You can assume that the user will enter a positive integer (input validation for a positive number not required). Use a while loop to print all integers from 0 up to and including the integer entered. Each integer should be printed on a line by itself and nothing else should print. This is for Linux. I have not done anything on this script. Please help with simplistic commands...
Assume that you have an array of 100 elements representing the grades students are stored in...
Assume that you have an array of 100 elements representing the grades students are stored in memory. Suppose the grades are in IEEE single precision format. Write a MIPS program to compute the average of the students grades and store the result in register $f0. Assume the array base address is stored in register $s0 and a floating point value of 100 is stored in memory with it address given in register $s2.
Suppose you have a list containing k integer numbers. Each number could be positive, negative, or...
Suppose you have a list containing k integer numbers. Each number could be positive, negative, or zero. Write pseudocode for a program that first asks the user for each of the numbers. After that, it should determine and display the number from the list that is nearest to the positive number five (5). Notes: 1. Try working this out for k = 4 and k = 5. See if you can find an algorithmic pattern that can be extended for...
Assume you have two processes, P1 and P2. P1 has a high priority, P2 has a...
Assume you have two processes, P1 and P2. P1 has a high priority, P2 has a low priority. P1 and P2 have one shared semaphore (i.e., they both carry out waits and posts on the same semaphore). The processes can be interleaved in any arbitrary order (e.g. P2 could be started before P1).             i.             Explain the problem with priority inversion Briefly explain whether the processes could deadlock when:            ii. both processes run on a Linux system as...
For this case assume: You have located a home that you wish to purchase and wish...
For this case assume: You have located a home that you wish to purchase and wish to evaluate bank financing options in order to determine your budget. You’ve been working with a few banks on potential mortgage terms and wish to determine for yourself the payment schedule, monthly payment, and most importantly, just how much interest you will pay over the life of the mortgage. You also want to run a few scenarios to determine which is the best option...
You have a budget of $2,500 to purchase a laptop computer and Microsoft Office 365 to...
You have a budget of $2,500 to purchase a laptop computer and Microsoft Office 365 to use for a small business office setup. 2. Gather the specifications of 3 laptops (CPU speed and chip, RAM, WIFI hardware, hard drive capacity, screen resolution, type of connection ports available and graphics card). You can also have PC and Mac computers for comparison. It does not have to be strictly PC or Mac to make the comparisons. 3. Which PC offers the best...
Assume you have the following jobs to execute with one processor: i t(pi) Priority 0 80...
Assume you have the following jobs to execute with one processor: i t(pi) Priority 0 80 2 1 25 4 2 15 3 3 20 4 4 45 1 The jobs are assumed to arrive at the same time. Using priority scheduling followed by FCFS, do the following: Create a Gantt chart illustrating the execution of these processes. What is the turnaround time for process p1? What is the average wait time for the processes?
Assume that you have a budget of $30 to spend on milk and cereal only. The...
Assume that you have a budget of $30 to spend on milk and cereal only. The price of milk is PM = $4.00 and the price of cereal is PC = $5.00. Answer the following quesitons. 2(a) Graph the budget constraint. Be sure to put the quantity of milk (QM) on the horizontal axis. 2(b) What is the most you can purchase of cereal? 2(c) If you purchase 4 units of cereal (QC = 4), then how many units of...
Assume you have a superscalar CPU with in-order issue and in-order instructions that uses 8 registers...
Assume you have a superscalar CPU with in-order issue and in-order instructions that uses 8 registers (R0-R7). The usual rules include: up to two instructions can be issued in one cycle; instructions have to complete in the order they are issued; an instruction attempting to write to a register that is being read by any incomplete instruction cannot be issued until the incomplete instruction completes; any instruction attempting to read a register that is being written to by any incomplete...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT