Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, can someone answer all parts? Thank you. What can and can't I use or do? Many built-in functions in python would make our tasks

Hello, can someone answer all parts? Thank you.

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

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, list 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, 11, %, ==, !=, , =, 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. "Bubble sort" for sorting. you'll *need* to use at least one loop per function, if not more! 1 Functions . def dif_fibcumsum(n): Given a positive integer n, 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 = 0 and F1 =1. Formula: F=Fn-1 + F1-2 Serie: Fo=0, F1 = 1, F2 = 1, F3 = 2, F4 = 3, F5 = 5, F6 = 8, F1 =13, ... For instance, for n=2, its Fibonacci number is F2 = F + Fo; i.e., F2 = 1 + 0, therefore F2 = 1 The sum of the first n natural numbers Sn is given by 1+2+...+n NOTE: Carl Gauss proposed a formula to calculate the sum of the first n natural numbers. Assume: n is a positive integer. dif_fibcumsum(5) 10 # Fs=5 (F4 + F3); Ss=15 (1+2+3+4+5), then 5-15|=10 dif_fibcumsum(1) # F2=1; S = 1, then 1-1|=0 dif_fibcumsum(3) 4 # F3=2 (F2 + Fu); S3=6 (1+2+3), then 12-6=4 O 0 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. Assume: n is a positive integer. dif_oddsumcount(5) # How many odd numbers? 3 (1,3,5], Sum = 9, then 19-3| dif_oddsumcount(1) # How many odd numbers? 1 [1], Sum = 1, then 1-1|| dif_oddsumcount(3) # How many odd numbers? 2 [1,3], Sum = 4, then 4-2|| 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. o 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",[]) 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. o 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"], [], ["chocolate", "carrot", "vanilla"]) + None biggest_sale([],[],["lemon"]) + None 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. o Assume: flavor is a string, flavor_cakes is a list of strings. Reminder: you must not hard-code the flavor_cakes items; use the given list argument! marble_cakes ("chocolate", ["vanilla", "almond"]) + 3 #ch-va, ch-al, va-al marble_cakes ("chocolate", ["chocolate"]) # Possible combination ch-ch, marble_cakes ("chocolate", ["carrot", "chocolate"]) +1 #ch-ca 2 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-MM","carrot-oreo"] get_cakewithtopping ("pistachio", ["sprinkles"]) ["pistachio-sprinkles"] get_cakewithtopping("velvet",[]) [] O . 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). Restrictions: remember, you can't call any built-in sorting functions. sales_sort(["pistachio", "velvet"],[8,7]) ["pistachio", "velvet"] sales_sort([],[]) [] sales_sort(["lemon","carrot","dark"],[2,50,50]) ["carrot","dark", "lemon"] O 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_2

Step: 3

blur-text-image_3

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 And Expert Systems Applications 22nd International Conference Dexa 2011 Toulouse France August/September 2011 Proceedings Part 1 Lncs 6860

Authors: Abdelkader Hameurlain ,Stephen W. Liddle ,Klaus-Dieter Schewe ,Xiaofang Zhou

2011th Edition

3642230873, 978-3642230875

More Books

Students also viewed these Databases questions