Write an iterative and a recursive function that calculate the standard deviation given a list of numbers, assume it's a population standard deviation Iterative function import mathdef...

1 answer below »
I did attempt the question i needed to get reviewed within 1 hours 15 min


Write an iterative and a recursive function that calculate the standard deviation given a list of numbers, assume it's a population standard deviation Iterative function import math def population_std_deviation(data): n = len(data) mean = sum(data) / n deviation_sum = 0 for x in data: deviation_sum += (x - mean)**2 std_dev = math.sqrt(deviation_sum / n) return std_dev recursive function import math def population_std_deviation_recursive(data): n = len(data) if n == 1: return 0 mean = sum(data) / n # calculate sum of squared deviations sum_sq_dev = 0 for x in data: sum_sq_dev += (x - mean)**2 # calculate variance variance = sum_sq_dev / n # calculate standard deviation std_dev = math.sqrt(variance) return std_dev 2. Write a function sum of white primes minus black primes that accepts a 2d list assume it's patterned like a chess board with alternating white/black squares return the sum of prime values in white squares minus the sum of prime values in black squares, ignore other non prime values def sum_white_minus_black_primes(chess_board): # Define a helper function to check if a number is prime def is_prime(n): if n < 2:="" return="" false="" for="" i="" in="" range(2,="" int(n**0.5)+1):="" if="" n="" %="" i="=" 0:="" return="" false="" return="" true="" #="" loop="" through="" the="" rows="" and="" columns="" of="" the="" chess="" board="" sum_white_primes="0" sum_black_primes="0" for="" i,="" row="" in="" enumerate(chess_board):="" for="" j,="" square="" in="" enumerate(row):="" #="" if="" the="" square="" is="" white="" and="" prime,="" add="" to="" the="" white="" sum="" if="" (i+j)="" %="" 2="=" 0="" and="" is_prime(square):="" sum_white_primes="" +="square" #="" if="" the="" square="" is="" black="" and="" prime,="" add="" to="" the="" black="" sum="" elif="" (i+j)="" %="" 2="=" 1="" and="" is_prime(square):="" sum_black_primes="" +="square" #="" return="" the="" difference="" between="" the="" white="" and="" black="" prime="" sums="" return="" sum_white_primes="" -="" sum_black_primes="" 3.="" given="" the="" following="" uml,="" write="" the="" class="" pizza="" diameter_in_centimeters="" :="" int="" toppings="" :="" []="" base_cost="" :="" float="" cost_in_cents_per_centimeter="" :="" float="" base_diameter_in_centimeters="" :="" int=""  ="" add="" a="" method="" add_topping="" that="" accepts="" a="" string="" value="" and="" adds="" it="" to="" the="" toppings="" list="" add="" a="" method="" get="" _total_cost="" which="" return="" shte="" base="" price="" +="" the="" cost="" in="" cents="" per="" centimeter="" *="" #="" of="" centimeters="" over="" the="" base="" diameter="" add="" a="" set="" method="" for="" diameter_in_centimeters="" that="" raises="" a="" value="" error="" if="" the="" value="" isn't=""> 0 class Pizza: def __init__(self, diameter_in_centimeters, toppings, base_cost, cost_in_cents_per_centimeter, base_diameter_in_centimeters): self.diameter_in_centimeters = diameter_in_centimeters self.toppings = toppings self.base_cost = base_cost self.cost_in_cents_per_centimeter = cost_in_cents_per_centimeter self.base_diameter_in_centimeters = base_diameter_in_centimeters def add_topping(self, topping): self.toppings.append(topping) def get_total_cost(self): extra_diameter = self.diameter_in_centimeters - self.base_diameter_in_centimeters extra_cost = extra_diameter * self.cost_in_cents_per_centimeter total_cost = self.base_cost + extra_cost return total_cost def set_diameter_in_centimeters(self, diameter): if diameter <= 0: raise valueerror("diameter must be greater than zero") self.diameter_in_centimeters = diameter 0:="" raise="" valueerror("diameter="" must="" be="" greater="" than="" zero")="" self.diameter_in_centimeters="">
Answered Same DayMar 04, 2023

Answer To: Write an iterative and a recursive function that calculate the standard deviation given a list of...

Vikas answered on Mar 04 2023
33 Votes
Review
Question 1:
1. Iterative: correct.
2. Recursive:
One minor comment I would make is that
the base case for the recursive solution should return a standard deviation of 0.0 instead of 0 to ensure that the returned value is a float. So, the base case should be ():
if n == 1:
return 0.0
Other than that, your solutions look good!
Question 2:
One small suggestion is to handle the case where a square contains a non-integer value. Currently, your solution assumes that all squares contain integer values, but this may not always be the case. To handle this, you...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here