Scientific Computing
Create a file called activity11.py
in cs100/ch2
.
toCelsius(temperature)
that converts Fahrenheit degrees to Celsius degrees. (Hint: C = (F-32)*(5/9)). Test it with the following data:
F=32 --> C=0
F=15 --> C=-9.44
F=85 --> C=29.44
This function should NOT return the value rather just print the Celsius degree.
After you test it and it works, modify it to return the Celsius degrees. How are the two function calls different? Write in comments.
tossCoin()
that simulates the flipping of a coin. Test it with the following code:
for i in range(10):
print( tossCoin() )
Write Python code that invokes tossCoin()
1,000 times and counts the number of heads (0) and tails (1). Run this simulation again. What do you observe about this large number of coin tossing? Write in comments.
getMax(a,b,c)
that returns the maximum of three numbers. Test it with:
print( getMax(7, -4, 99) )
print( getMax(23, 5, 0) )
print( getMax(17, 74, 9) )
print( getMax(7, 7, -9) )
print( getMax(2, 2, 2) )
print( getMax(7, 9, 9) )
print( getMax(9, 7, 9) )
Each number in the Fibonnaci sequence is the sum of the previous two numbers. The first two numbers in the sequence are 1 and 1. Write a function to compute the Nth Fibonnaci number.
The golden ratio, , is found often in nature and man-made objects (arrangement of leaves, petals of a flower, space, architecture, music, etc). Coined also as the “divine proportion”, it is often associated with beauty - if it is possible to quantify such a thing. There is a fair amount interesting history that you can read about the golden ratio here. Of particular interest is that consecutive Fibonnaci numbers converge to the golden ratio. Use the function you wrote in the previous question to approximate the golden ratio by dividing the next term in the Fibonnaci sequence by the previous term and printing the result. Observe how closely it reaches as the terms increase in value.
Submit your activity11.py
file to Moodle.