Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

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...

1 answer below »
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:
XXXXXXXXXXdeviation_sum += (x - mean)**2

std_dev = math.sqrt(deviation_sum / n)
return std_dev
ecursive function
import math
def population_std_deviation_recursive(data):
n = len(data)
if n == 1:
XXXXXXXXXXreturn 0

mean = sum(data) / n

# calculate sum of squared deviations
sum_sq_dev = 0
for x in data:
XXXXXXXXXXsum_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
lack 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):
XXXXXXXXXXif n < 2:
XXXXXXXXXXreturn False
XXXXXXXXXXfor i in range(2, int(n**0.5)+1):
XXXXXXXXXXif n % i == 0:
XXXXXXXXXXreturn False
XXXXXXXXXXreturn 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):
XXXXXXXXXXfor j, square in enumerate(row):
# If the square is white and prime, add to the white sum
XXXXXXXXXXif (i+j) % 2 == 0 and is_prime(square):
XXXXXXXXXXsum_white_primes += square
# If the square is black and prime, add to the black sum
XXXXXXXXXXelif (i+j) % 2 == 1 and is_prime(square):
XXXXXXXXXXsum_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 : []
ase_cost : float
cost_in_cents_per_centimeter : float
ase_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 diamete
add a set method for diameter_in_centimeters that raises a value e
or 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):
XXXXXXXXXXself.diameter_in_centimeters = diameter_in_centimeters
XXXXXXXXXXself.toppings = toppings
XXXXXXXXXXself.base_cost = base_cost
XXXXXXXXXXself.cost_in_cents_per_centimeter = cost_in_cents_per_centimete
XXXXXXXXXXself.base_diameter_in_centimeters = base_diameter_in_centimeters

def add_topping(self, topping):
XXXXXXXXXXself.toppings.append(topping)

def get_total_cost(self):
XXXXXXXXXXextra_diameter = self.diameter_in_centimeters - self.base_diameter_in_centimeters
XXXXXXXXXXextra_cost = extra_diameter * self.cost_in_cents_per_centimete
XXXXXXXXXXtotal_cost = self.base_cost + extra_cost
XXXXXXXXXXreturn total_cost

def set_diameter_in_centimeters(self, diameter):
XXXXXXXXXXif diameter <= 0:
XXXXXXXXXXraise ValueE
or("Diameter must be greater than zero")
XXXXXXXXXXself.diameter_in_centimeters = diameter
Answered Same Day Mar 04, 2023

Solution

Vikas answered on Mar 04 2023
33 Votes
Review
Question 1:
1. Iterative: co
ect.
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. Cu
ently, 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