CS 100

Logo

Scientific Computing

Activity 11

  1. Create a file called activity11.py in cs100/ch2.

  2. Write a function 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.

  3. After you test it and it works, modify it to return the Celsius degrees. How are the two function calls different? Write in comments.

  4. Write a function tossCoin() that simulates the flipping of a coin. Test it with the following code:
    for i in range(10):
        print( tossCoin() )
    
  5. 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.

  6. Write a function 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) )
    

Optional - If you finish early

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

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

How to submit

Submit your activity11.py file to Moodle.