CSC 111: Intro to Computer Science through Programming

Homework 4

Due: Tuesday, Feb. 28 at 11:59pm on Moodle

The goal of this homework is to practice writing and using functions. It will also help solidify some of the techniques we have been learning recently: random numbers, working with lists, modifying variables, and nested code structures. For this assignment, you are welcome to discuss your work with other students, but all code you submit should be your own original work. It should be produced and understood by you, without looking at anyone else's assignment.

Quiz 4

Please take the short Week 4 quiz on Moodle (also due by Tuesday night). This is to help me understand what areas need work, and also help you understand what concepts might need extra practice. It is not a large part of the overall homework grade. Just for the quiz part, do not discuss the questions with other students, TAs, etc. (Note: quiz usually posted Wednesday afternoon.)

Part A: Random Partners

For this part of the assignment, you will write a random partners generator, similar to what we use during lab. There are built-in functions to randomize lists, but for this assignment, you will write your own from scratch using several functions.

The following steps will help you with the specific details and program requirements. As you're developing your code, make sure to use comments and informative variables names.

  1. Get the lists of names

    First, begin from the Python file hw4A.py, which contains the lists of lab students so you don't have to type them all out. Right now these lists are alphabetized, but the goal is to shuffle the names around to generate random partnerships.

  2. Swap function

    Inside this file, below the lists, write a function called swap(i, j, lst), which will take in two integer indices (i and j) and a list of elements lst, and swap the ith and jth elements. An example of how this method should work is shown below:

    swap

    Note that the swap method will not RETURN or PRINT anything. It will MODIFY the parameter (lst). Make sure to test out your method in the shell to make sure it is working. You should be able to reproduce the examples above exactly.

  3. Shuffle function

    The next method to write is shuffle(lst). This method will also not return or print anything, but instead modify the list. This method will generate random indices and call swap using these indides. Inside this method, perform at least 100 random swaps so that the list is well shuffled. Think about this part like shuffling a deck of cards, but the only operation you can do it to swap two randomly chosen chards. An example is shown below:

    shuffle

    Since it is random, your shuffle method will likely produce different results than shown above.

  4. Random_pairs function

    Next, write a method called random_pairs(lst) that will call your shuffle function on the list, and then print out the pairs. This method will not return anything either, but it will print out the pairs. After shuffling the list, take the first two elements as the first pair, the next two as the second pair, etc (using a for-loop). An example is shown below (add a few spaces when printing so the pairs are indented slightly):

    print

    Note that some lists will have an odd number of names. In this case, print the last name on the same line as the last pair to form a group of three (think about how to do a special case for this part).

  5. Main function

    Finally, write a main() function that calls random_pairs on each of the four lab sections (not within a for-loop). Print out some custom header information for each section as shown in the example output below. Invoke main inside your file, so that each time you run your code, a new set of random partnerships is generated.

    output output

    Note that here Wednesday and Thursday lab partners appear side by side so everything is on one screen, but in your output all of Thursday will follow all of Wednesday.

    Part B: Rock-paper-scissors

    You may have played a game before called Rock-paper-scissors. In this two-player game, each player simultaneously chooses one of rock, paper, or scissors. This "winner" is determined by these rules:
    
    Rock beats ("smashes") scissors
    Scissors beats ("cuts") paper
    Paper beats ("covers") rock
    Same choices: tie
    
    After this "battle", the winner receives one point. Then this process is repeated for the desired number of rounds. At the end, the total score determines the overall winner. Your task is to program a rock-paper-scissors game where the user plays the computer.

    Here is an example of what your program should do (I did not do very well against the computer):

    rockpaperscissors

    This part of the assignment is more open-ended, but I've provided a few guidelines below. The main requirement is that you use at least one helper function, but I might suggest thinking about that after you've gotten the basic structure of your program working.

    1. Create a new file called hw4B.py and create a main method. For now, think about how to program ONE round of the game (inside-out design). Ask the user to choose rock, paper, or scissors. Then choose randomly for the computer. Print out both the user's choice and the computer's choice to make sure this is working.

    2. Then start to build up some conditionals for the different cases (tie, rock and scissors, paper and rock, etc). Think about how to construct them in a logical manner, and include the information about who won. For now, ignore the score and think about how to print the relevant information about the round (i.e. "rock and rock is a tie", or "scissors cuts paper, you win!")

    3. Now create the for-loop over the rounds (ask the user how many rounds they want). Indent the code for each round inside this for-loop, and test your code to make sure it is working (you should be able to produce all the output besides the score information).

    4. At this point, start thinking about how to encapsulate part of your code as a helper function to make your code cleaner and more modular. It doesn't have to be a large part, but you should use at least one helper function. One suggestion is to create a function that returns the winner for one round (either "user", "computer", or "tie"). (Such a function could also do the printing for the different cases.) Then you can use the returned winner information to modify the score.

    5. Now think about how to keep track of the score for the user and the score for the computer. You can use your helper function for this part if you like (or have a helper function for something else). For a tie round, neither score should be updated.
    6. Finally, print the final score and the overall winner. Make sure to test your code thoroughly since with random computer choices, not every case will come up each time. Also make sure you are using a helper function, but the exact task of this function is up to you.

    Also, an example where I won :-)

    rockpaperscissors

    Extensions

    Optional ideas to extend your code:

    For Part A, generate lab partners for each week of the semester (we'll have roughly 10 labs total). Now add a condition: no one should have the same partner twice. Think about how to store the previous partner information for each person, and only sample from the remaining students.

    For Part B, use a Boolean variable to keep track of whether or not the user still wants to play. If the user types "quit" at any point instead of "rock", "paper", or "scissors", the program should quit and print the final scores. Set the number of rounds very high to avoid running out of rounds before the user wants to quit (this is not very desirable, but later on we'll see another loop structure for this type of behavior).

    Transcript and Submit

    Finally, create a transcript that demonstrates your code working (for Part A, run your program a few times to show different random partnerships, and for Part B show a few complete games with different numbers of rounds). Save this file as hw4_transcript.txt. Also make sure your code is commented, and that you have given some thought to the header, variable names, and whitespace.

    TO SUBMIT ON MOODLE:

    • hw4A.py

    • hw4B.py

    • hw4_transcript.txt