Iterative and a recursive function

84 views 10:54 am 0 Comments March 20, 2023

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