Skip to content

Lab 1: Pre-lab

Refresh on Python

  1. Fibonacci sequence is a series of numbers:

    0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

    The first two numbers are 0 and 1. The rest of the numbers in the sequence is found by summing up two numbers before it.

  2. Create a function, in Python, which takes the sequence length sl as the input of the function and prints the Fibonacci sequence of the length sl.

    1
    2
    3
    def fibonacci(sl):
      ...
      return fib_seq
    

Random selection based on probability

For this section. assume the random.random() function selects the random number with even probability.

  1. Consider a coin tossing event. If the probabilities of getting a head or a tail are even, i.e. 50%. Create a Python function which will simulate the coin tossing event and return the result as head or tail.

    1
    2
    3
    def tossCoin():
      ...
      return headOrTail
    
  2. If the probabilities of getting a head or a tail are not even, with head as 20% and tail as 80%, how would you change the Python function you created previously to adapt to this coin?

  3. Consider the event of selecting one option out of three options randomly. The probability of choosing option A is 20%, B is 50%, and C is 30%. Create a Python funciton to simulate the random selection of the options.

    1
    2
    3
    def chooseFromThree():
      ...
      return selectedOption