Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

python Functions def sum_divisors (n): Given a positive integer n, calculate and return the total of all of its positive divisors. Note: n is also

image text in transcribed

python

Functions def sum_divisors (n): Given a positive integer n, calculate and return the total of all of its positive divisors. Note: n is also a divisor of itself Assume: n is a positive integer Restrictions: remember, you may not call sum() sum-divisors(6) sum-divisors(1) sum-divisors(7) o o 12 1 8 # divisors : 1,2,3,6 #only divisor: 1 # divisors : 1,7 def pi(precision): One can approximate the value of pi by using the Leibniz formula (given below) that calculates a theoretically infinite sum. In practice, though, we run a finite summation where the larger the number of terms the better the approximation of pi. Given precision as a float, this function approximates the value of pi using the Leibniz formula. The function should stop the infinite summation when the improvement of the approximation becomes smaller than the provided precision. (-1).. 44 4 4 4 4 4, -=--+--+ +-. Leibniz formula (source: Assume: precision is a positive float value. Restrictions: you must use a loop in your implementation to receive credit. pi(1.0) pi (8.5) p (8.1) o o 3.466666666666667 # total of the first three terms since 4/5c1. 3.3396825396825403# total of the first five terms since 4/3c0.5 3.189184782277596 # total of the terms up to and including 4/41 -+ - def span (nums): Given a list of numbers, return the difference between the largest and smallest number If the list is empty, return zero. Assume: nums is a list of any length, and all values are numbers. o o Restrictions: remember, you may not call min(),max) or other related built-in functions. largest span([1,0,-10,8.5)) span ([3,3,3] span() 18.5 # 9 9 -+ is 8.5; smallest is .10 # 3 is the only value # no value at all. -+ def single_steps (nums): Given a list of numbers, count and return how many neighboring values in the list differ by one Assume: nums is a list of any length, and all values are integers. Restrictions: no extra restrictions. single-steps([e, 1, 8, 7, 61) single_steps ([e, -3,8,6]) single steps() o 3 #three pairs differ by 1: # no neighboring values differ by 1 # empty list . * (e, 1), (8,7), (7,6) def remove_echo(xs): An echo is a contiguous sequence of identical values. Given xs as a list of values, this function returns a copy of xs with all but one value for each echo removed o Assume: xs is a sequence of any length. o Restrictions: remember, you may not call.index); do not modify xs. remove echo([1,1,3,3,3,2,1,2,2]) remove echo([1,3,2,1,2]) remove-echo([ "B", "a","1","1","o", "o", "n"D-+ remove echo([) # no echo ["B","a", "1","o", "n"] # a list of strings def even_product 2d(grid): Given a list of lists of numbers, named grid, multiply all the even numbers in grid and return the product. If there are no even numbers, return 1. o Assume: grid is a list of lists of numbers, e.g. [[1,2,31,[4,5,6]] o Restrictions: no extra restrictions. even-product-2d([ [1,2,3],[4,5,6]]) even-product-2d([ [2,1,7],[],[-3,5]]) even_product_2d(L]]) 48 2 1 # no even value #evens are: 2,4,6 # 2 is the only even value -+ -+

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

Relational Database And SQL

Authors: Lucy Scott

3rd Edition

1087899699, 978-1087899695

More Books

Students also viewed these Databases questions