Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In python What can and can't I use or do? Many built-in functions in python would make our tasks so trivial that you wouldn't really

image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
In python
What can and can't I use or do? Many built-in functions in python would make our tasks so trivial that you wouldn't really be learning, just "phoning it in". The following restrictions are in place for the entire project, and individual functions may list further restrictions (or pointed reminders). The whole point of this assignment is to practice writing loops, and seeing lists in the most basic way possible. There are indeed many different approaches that can "hide the loop" inside a function call, conversion to a different type, and other ways, but we want to make sure you're getting the practice intended from this assignment. Learning to code has different goals than finishing programs. Restrictions: you can't import anything you can't use recursion, recursion will be used in future assignments none of these functions need to call any of the other functions you've created no built-in functions other than those in the "allowed" list. no usage of sets, dictionaries, file 1/0, tist comprehensions, or self-made classes. Just focus on lists/loops. . . Allowed: variables, assignments, selection statements (if-elif-else), loops (of course!), indexing/slicing. basic operators + - 7./7.%, -, !, , , in, not in, and, or not. when you need to build up a list, you can start with the empty list [] and .append( values into it. only these built-in functions/methods can be used: abs(), len(). range().str(), int() .append(). extend(), .pop() Hints In my solution, I used the following things basic operators, assignment, branching/loops, indexing, built-ins functions abs(), len() range(). when the answer is a list, I build it up from an initial ) by calling append() repeatedly . I used pop() in one function, Hints In my solution, I used the following things: basic operators, assignment, branching/loops, indexing, built-ins functions abs(), len(), range(). when the answer is a list, I build it up from an initial [] by calling .append() repeatedly. . I used .pop() in one function. "Bubble sort" for sorting. you'll need to use at least one loop per function, if not more! Functions I def dif_fibcumsum(n): Given a positive integer, calculate the absolute difference between its corresponding Fibonacci number and the sum of the first n natural numbers {0,1,...,n). A Fibonacci number F. is the sum of the two preceding ones, starting with Fo - O and F1I. Formula: Fo=F1+F2 Serie: F. - 0, F, - 1,F2-1,F3-2, Fa= 3. Fs - 5.F. - 8. F.-13.... For instance, for w=2. its Fibonacci number is F2 = F + Foi ie, F2 -1 +0. therefore F1 The sum of the first i natural numbers S, is given by 1 + 2 + ... + NOTE: Carl Gauss proposed a formula to calculate the sum of the first natural numbers. Assume: n is a positive integer. dif fibcumsum (5) #F5FF): 5-15 (1+2+3+4+5), then 15-151-10 dif fibcumsum(1) #F1 S1then 11-11-0 dif fibcunsum(3) 4 * 2 ): 5,6 (1+2+3), then 12-614 10 def dif_oddsumcount(n): Given a positive integer n, calculate the absolute difference between the cumulative sum of the odd numbers in the range {1,..., n} and the total odd numbers in the same range. o Assume: n is a positive integer. dif_oddsumcount(5) # How many odd numbers? 3 (1,3,5), Sum = 9, then 19-31 dif_oddsumcount(1) # How many odd numbers? 1 [1], Sum = 1, then 11-11 dif_oddsumcount(3) # How many odd numbers? 2 (1,3), Sum = 4, then 14-21 6 2 The Sunrise Bakery" offers different flavor cakes. Next functions relate to sales in the bakery. . def is promotion(cake, promotions): Given an item cake and a list promotions, return True if cake is include in promotions, False if not. Assume: cake is a string and promotions is a list of strings. is_promotion("carrot", ("chocolate", "almond", "carrot"]) True is promotion("carrot", ("ch-ca", "lemon", "velvet")) - False is_promotion("carrot", 0)) - False def biggest_sale(cakes, sales, flavor_cakes): Given a list cakes, a corresponding list sales of sold cakes, and a list of individual flavors, flavor cakes, return what kind of cake ("single" or "marble") has the largest number of sales. If the cake with the most sales is not in the list flavor_cakes then it is a "marble" cake. If either sales or cakes are empty, return None. Assume: cakes and sales are lists of the same length; cakes only holds strings, and sales only holds non-negative ints. flavor_cakes is a list of strings. biggest_sale(["vanilla","ch-ca"], [4,15], ["vanilla", "almond"]) "marble biggest_sale(["chocolate"), D. ("chocolate","carrot","vanilla"]) + None biggest_sale(0, 0,["lemon") - None def marble_cakes (flavor, flavor_cakes): Given an item flavorand a list flavor_cakes return how many marble cakes can be included in the marble cakes meni". Assume a marble cake is two different flavors. The list flavor_cakes contains different flavors but could also contain flavor Assume: flavor is a string, flavor cakes is a list of strings O . def marble_cakes (flavor, flavor_cakes): Given an item flavor, and a list flavor_cakes return how many marble cakes can be included in the "marble cakes menu". Assume a marble cake is two different flavors. The list flavor_cakes contains different flavors but could also contain flavor. Assume: flavor is a string, flavor_cakes is a list of strings. o Reminder: you must not hard-code the flavor_cakes items; use the given list argument! marble_cakes("chocolate", ["vanilla", "almond"]) 3 # dere, chal, vaal marble_cakes ("chocolate", ("chocolate"]) - # Posable combination ch-ch, marble_cakes ("chocolate", ("carrot", "chocolate"])+1 #dha 2 I def get_cakewithtopping(cake, toppings): Given an item cake, and a list of toppings, create and return a list of possible combinations, using "" for the combination name, Preserve ordering, o Assume: cake is a string, and topings is a list of strings. The list might be empty get_cakewithtopping("carrot". ("MM","oreo")) -- [ "carrot-M", "carrot-oreo"] get_cakewithtopping ("pistachio",["sprinkles"]) ["pistachio-sprinkles" get_cakewithtopping("velvet",0) - . def sales_sort(cakes, sales): Given a list of strings cakes, a corresponding list of ints sales, return a list of the cakes sorted from highest to lowest number of sales. Preserve the original order in case of tie Assume: cakes is a list of strinys, sales is a list of positive ints, cakes and sales are the same length (which may be zero), . def sales sort(cakes, sales): Given a list of strings cakes, a corresponding list of ints sales, return a list of the cakes sorted from highest to lowest number of sales. Preserve the original order in case of tie. Assume: cakes is a list of strings, sales is a list of positive ints. cakes and sales are the same length (which may be zero). o Restrictions: remember, you can't call any built-in sorting functions. sales_sort(["pistachio", "velvet"],[8,7]) ["pistachio", "velvet"] sales_sort([],[]) 11 sales sort(["lemon", "carrot","dark"],[2,50,50]) ["carrot", "dark", "lemon"] def dif_sales(cakes, sales): Given a list cakes, and a corresponding list sales, return a list of cakes which sales differ from other cakes (i.e. its sale appears only once in sales). Preserve ordering, Assume: cakes is a list of strings, and sales is a list of ints. Both lists are same length (which may be zero). dif sales(("carrot", "velvet", "lemon"), [1,5,1]) - ["velvet"] dif sales(("chocolate", "vanilla"], [9,1]) - ("chocolate", "vanilla") dif_sales(["velvet","white","dark", "banana"), (2,5,5,2])--[]

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design For Mere Mortals

Authors: Michael J Hernandez

4th Edition

978-0136788041

More Books

Students also viewed these Databases questions