If you are enrolled in the class, make sure you use the GitHub Classroom link from the Ed Discussion Board rather than downloading directly.
Problems 1-4 consist of a series of code snippets for you to interpret and debug. For Problems 1-3, you will be asked to identify relevant error(s) and fix the code. For Problem 4, the code works as intended; your goal is to identify the code's purpose by following its logic.
Problem 5 asks you to rewrite a "script" into a function, which you will then use to conduct an experiment.
You've been tasked with writing code to identify the minimum value in an array. You cannot use a predefined function. Your colleague suggested the function below, but it does not return the minimum value.
function minimum(array)
min_value = 0
for i in 1:length(array)
if array[i] < min_value
min_value = array[i]
end
end
return min_value
end
array_values = [89, 90, 95, 100, 100, 78, 99, 98, 100, 95]
@show minimum(array_values);
minimum(array_values) = 0
Describe the logic error.
Write a fixed version of the function.
Use your fixed function to find the minimum value of array_values
.
Your team is trying to compute the average grade for your class, but the following code produces an error.
student_grades = [89, 90, 95, 100, 100, 78, 99, 98, 100, 95]
function class_average(grades)
average_grade = mean(student_grades)
return average_grade
end
@show average_grade;
Describe the logic and/or syntax error.
Write a fixed version of the code.
Use your fixed code to compute the average grade for the class.
Your team has collected data on the mileage of different car models. You want to calculate the average mileage per gallon (MPG) for the different cars, but your code produces the same value for all of the vehicles, which makes you suspicious.
function calculate_MPG((miles, gallons))
return miles / gallons
end
car_miles = [(334, 11), (289, 15), (306, 12), (303, 20), (350, 20), (294, 14)]
mpg = zeros(length(car_miles))
for i in 1:length(car_miles)
miles = car_miles[1][1]
gallon = car_miles[1][2]
mpg[i] = calculate_MPG((miles, gallon))
end
@show mpg;
mpg = [30.363636363636363, 30.363636363636363, 30.363636363636363, 30.363636363636363, 30.363636363636363, 30.363636363636363]
Describe the logic error.
Write a fixed version of the code.
Use your fixed code to compute the MPGs.
You've been handed some code to analyze. The original coder was not very considerate of other potential users: the function is called mystery_function
and there are no comments explaining the purpose of the code. It appears to take in an array and return some numbers, and you've been assured that the code works as intended.
function mystery_function(values)
y = []
for v in values
if !(v in y)
append!(y, v)
end
end
return y
end
list_of_values = [1, 2, 3, 4, 3, 4, 2, 1]
@show mystery_function(list_of_values);
mystery_function(list_of_values) = Any[1, 2, 3, 4]
Explain the purpose of mystery_function
.
Add comments to the code, explaining why and how it works. Refer to "Best Practices for Writing Code Comments", and remember that bad comments can be just as bad as no comments at all. You do not need to add comments to every line (in fact, this is very bad practice), but you should note the purpose of every "section" of code, and add comments explaining any code sequences that you don't immediately understand.
You and your classmate are trying to determine the expected payoff of the following game:
Flip a coin (which has probability of heads) 10 times;
If 3 consecutive heads occur one or more times, pay 1 dollar;
If not, pay nothing.
Your classmate has written the following code, which correctly simulates one realization of this game with a fair coin.
# initialize variables, as this code works by iterating over each coin flip and adding
let payoff = 0, count = 0
# loop over coin flips and compute payoff: this code is pretty literal with the rules of the game other than just using `rand()` to produce the heads vs. tails coin outcome.
for i in 1:10
U = rand()
if U < 0.5
count += 1
else
count = 0
end
if count == 3
payoff = 1
end
end
@show payoff;
end
payoff = 1
1
However, you would like to conduct multiple trials to compute the expected payoff, and might want to look at coins which are not fair (). To minimize copying-and-pasting code, you decide to turn this script into a function.
Rewrite the above code in a function which accepts two arguments: the number of trials and the probability of heads , and returns the expected payoff.
Use your function to compare the expected payoff after 1000 trials of this game with a fair coin vs. a coin which has a 70% probability of heads.