Programming

MATH 230 : Fall 2023

Department of Mathematics - SUNY Geneseo
⇐ Back

Homework 6 - More with Lists

Due Date: November 3, 2023

Upload

To solve the following problems, use only what we have learned so far. Do not use built-in functions to solve any of the problems unless you are asked to do so. Submit your file by 11:59 pm on the due date.

Problems

  1. How random is the randint function of the random module? Let's find out! Write a function that takes one integer parameter, say \(N\), and generates \(N\) random numbers \(x_1,x_2,\ldots,x_N\) where each number \(x_i\) is either \(0\) or \(1\), and returns the percentage of the numbers that were equal to \(1\). Call your function my_experiment. Call your function several times with some choice for \(N\); choose at least \(N\geq 1000\). Are the results of your experiment surprising? Explain. You can load the randint function using the following code:

    from random import randint
  2. What does the built-in function isinstance do? Give examples that use a list, tuple, integer, float, and string data types.
  3. Write a function called find_indices that has two parameters, say a and B where B is supposed to be a list, and returns a list of the indices where B has value equal to a. Your function should check that the parameter B is a list object (use the isinstance function) and if it is not then an error message is returned. If a is not in the list B then your function should return False. For example, here are some outputs of find_indices:

    >>> find_indices(3, [4, -1, 3, 4, 7, 5, 3])
    [2, 6]
    
    >>> find_indices('yes', ['bob', 'joe', 'bob', 'two'])
    False
    
    >>> find_indices('rabbit', 'Once a upon a time')
    Error: The second argument is not a list
  4. See video instructions. Your game should include the following:
    1. The "turn" or "play" number should be displayed.
    2. Allow the game to end in a draw.
    3. Allow the user to continue playing after the end of a game.
    4. Display a message if a user enters a row/column that already has a symbol and allow the user to enter a new row/column.
    5. Your code should be thoroughly commented. All if-elif-else, for loops, while loops, etc. should be preceded by comments that describe what the forthcoming code is doing. Uncommented code will receive little credit.
    This might be the most complicated program that you code so far. You can easily find solutions to this problem on the web, however, avoid the temptation to copy-paste a solution that you find. Try to this problem on your own; you will learn a lot if you do. Good luck!