Double
Write the function double(numbers)
that doubles all
of the values in a list of numbers. The
parameter numbers is a list of numbers of any length.
The function should not return anything. Instead it
should modify numbers so that the value of each number
in the list is doubled.
Test Cases
numbers = [1, 2, 3, 4] double(numbers) print('Input: [1, 2, 3, 4]\tActual: ', numbers, '\tExpected: [2, 4, 6, 8]') numbers = [1, 1, -1] double(numbers) print('Input: [1, 1, -1]\tActual: ', numbers, '\tExpected: [2, 2, -2]')
Above Average
Write the function above_average(numbers)
that
filters a list of numbers . The parameter numbers is a
list of numbers of any length. The function should not
return anything. Instead it should modify numbers so
that all values below the average are removed.
Test Cases
numbers = [1, 2, 3, 4] above_average(numbers) print('Input: [1, 2, 3, 4]\tActual: ', numbers, '\tExpected: [3, 4]') numbers = [1, 1, -1] above_average(numbers) print('Input: [1, 1, -1]\tActual: ', numbers, '\tExpected: [1, 1]')
Nim
Nim is an ancient game where players take turns picking up stones. The stones are arranged in a circle and each turn a player must remove 1, 2, or 3 stones from the circle. The player that picks up the last stone loses. In this activity you will write a command line version of the game.
Details
Create a Python program that plays a two-player game of nim with 10 stones in a circle. Drawing a circle of stones using the command line is difficult, so instead display the circle as a line of period characters. The function should prompt the players to enter the index of a stone and the number of stones to remove. The removed stones should be displayed with underscore characters. The winner of the game should be displayed when there are no stones left.
Example
>>> nim(10) .......... Player 1, enter a stone index (0-9): 8 Player 1, enter a number of stones (1-3): 3 _.......__ Player 2, enter a stone index (0-9): 10 Invalid index, enter a stone index (0-9): 4 Player 2, enter a number of stones (1-3): 3 _...___.__ Player 1, enter a stone index (0-9): 1 Player 1, enter a number of stones (1-3): 3 _______.__ Player 2, enter a stone index (0-9): 7 Player 2, enter a number of stones (1-3): 1 __________ Player 1 wins!
-
The circle of stones should be stored as a string. To initialize the string use the string * operator which creates a string by repeatedly concatenating another string.
-
The program will be easier to write and easier to test if there is a function
remove_stones(stone_string, index, number)
. The function should return a string that is equivalent tostone_string
with the characters at the indices specified byindex
andnumber
replaced with underscore, '_', characters.Note that this can not be done with a single slice if
index
andnumber
specify indices that wrap around to the beginning of the string. It would be easier to use a loop that runsnumber
times and wrap the index if it goes out of range.Test this function thoroughly before proceeding!
-
With the above function, writing the game is simple. Create a loop that runs while there are still stones in the stone string. Use the string
count
method to determine the number of stones remaining.For each iteration of the loop, print the stone string and prompt the user to enter an index and a number of stones. Call the
remove_stones
function with the numbers that the user entered. Don't forget to convert the user input to ints. -
Verify that the user's input is valid by adding a while loop after getting the user's input that runs while the user's input is invalid. Inside the loop re-prompt the user for input.
-
Finally, print who won after the main loop. To determine who won, the program will need an integer variable that keeps track of who's turn it was last. Every iteration of the loop, this variable should flip-flop between 1 and 2.
Challenge
Create a 1-player version of the game that uses a very simple artificial intelligence. The computer's move should be randomly generated, but it should always be valid. It is not sufficient to generate an index and number of stones in the correct range. The computer should only produce moves that correspond to indices that contain only stones.