# Python Program to Implement Bug Fixing Assignment Solution.

## Instructions

Objective
Write a program to implement bug fixing in python.

## Requirements and Specifications

Source Code
```from types import TracebackType import numpy as np ''' QUESTION 1 ''' def prod(S): prod = 1 for x in S: prod = prod*x return prod ''' QUESTION 2 ''' for k in range(len(exacts)): if abs(exacts[k] - approx[k])/approx[k] < 0.001: print(f"Test case {k}: pass") else: print(f"Test case {k}: fail") ''' QUESTION 3 ''' # No question 3 ''' QUESTION 4 ''' def stats(sample): sample_min = min(sample) sample_max = max(sample) sample_mean = sum(sample)/len(sample) sample_variance = sum([s**2 for s in sample])/len(sample) - sample_mean**2 return (sample_min, sample_max, sample_mean, sample_variance) def faststats(sample): N = len(sample) sample_min = 1E+99 sample_max = -1E+99 sum_of_x = 0 sum_of_xx = 0 for x in sample: if x < sample_min: sample_min = x if x > sample_max: sample_max = x sum_of_x += x sum_of_xx += x**2 sample_mean = sum_of_x/N sample_variance = sum_of_xx/N - sample_mean**2 return (sample_min, sample_max, sample_mean, sample_variance) print(stats(sample)) print(faststats(sample)) ''' QUESTION 5 ''' midpts = (x[:-1] + x[1:])/2 ''' QUESTION 6 ''' def trace(A): assert A.shape == A.shape, "Matrix is not square" return sum([A[i][i] for i in range(A.shape)]) ''' QUESTION 7 ''' sorted_sample = np.sort(sample) N = len(sample) if N%2 > 0: sample_median = sorted_sample[int(N/2)] else: sample_median = 0.5*(sorted_sample[int(N/2)]+ sorted_sample[int(N/2)+1]) ''' QUESTION 8 ''' positions = np.loadtxt('output-run-2965.dat', delimiter=',') x = positions[:,0] y = positions[:,1] z = positions[:,2] # Get number of points N = positions.shape dists = np.zeros((N,N)) # Compute for i in range(N): for j in range(N): if i != j: p1 = np.array([x[i], y[i], z[i]]) p2 = np.array([x[j], y[j], z[j]]) distance = np.linalg.norm(p1-p2) # Euclidean distance dists[i,j] = distance ''' QUESTION 9 ''' def expectation(dice): Y = get_value(dice) P = get_probability(dice) assert all(P >= 0) and all(P <= 1) and (sum(P) == 1) return sum(P*Y) ''' QUESTION 10 ''' R2 = R1.copy() # The bug occurs here. If you use R2 = R1, when changing R2 will also change R1 # So the correct way is using the function copy() for r in R2: r = -r r = -r dR = np.zeros(R1.shape) for k in range(R1.shape): dR[k] = np.sqrt(np.sum((R1[k,:] - R2[k,:])**2)) ''' QUESTION 11??? ''' most_emitters = 0 # We need to pre-allocate the variable winner winner = -1 for k in range(len(galaxies)): if len(galaxies[k]) > 0 and galaxies[k].count('x') > most_emitters: winner = k most_emitters = galaxies[k].count('x') if winner != -1: print(f"galaxy {winner} has the most x-ray emitters; {most_emitters} of them") else: print("There is no galaxy with 'x's in it")```