challenges (python 2)
how many of these can you do in Python?
- Write a program that asks a user the colour of a traffic light. If it is a “green” light, output “go”. If it is a “yellow” light, output “wait”. If it is a “red” light, output “stop”. use a function / method
- try to get your program to simulate a 'pause' for x second(s)..
help
import time print("zero") time.sleep(1) # pause 1 second print("one")
- write a program that calculates simple interest. Challenge: put the formula to calculate simple interest into a method
- write a program that counts from 0-50, and for each number counted the program tells me if it is an even or odd number
- write a program that uses a method to find the area of a rectangle
- write a program that adds together all the even numbers from 0 to 100 (use modulo operator: %)
- Write a program that adds all the odd numbers together from 1 to n (where n is a number inputted by a user)
- Write a program that uses a method to calculate the area of a circle
- finish the bank example - so that i can withdraw, deposit, get account balance, and perhaps a list of my last 3 transactions if possible
- Write an algorithm and program that inputs 5 numbers into an array, then reads the contents of the array to calculate the average
- Write an algorithm that lists prime numbers between 1 and 20
- Code the algorithm that lists prime numbers between 1 and 20 (use mod / %)
help
#for all nums 1 to 20 inclusive: for currentNum in range(1,21): #more than 2 divisors and its not prime: countOfDivisors = 0 #search all previous numbers (backwards): for countBackNum in range(currentNum,0,-1): #looking for no remainder: if(currentNum % countBackNum == 0): #if we find a clean division: countOfDivisors += 1 #2 divisors in a prime (1 and itself): if(countOfDivisors==2): print(currentNum)
- sort a simple array of five integer elements into ascending order (try 4 different types of sort - bubble, merge, insertion and quick sort)
- implement a Fisher–Yates shuffle in Python
- write a program that finds the max, min and average of an array of 5 integers
- try and sort an array of n integers into descending order (where n is a whole integer number inputted by the user)
- implement either the nearest neighbour algorithm, or the stable matching algorithm in python
- write a multidimensional String array that stores the location of 3 sunken treasures (use the String value "Sunken Treasure" to designate this) at 3 random co-ordinates on an x and y axis to simulate a map of the ocean (you can limit the axis to values between 0 and 10)
- right a recursive python program to calculate the factorial of a number, e.g. 5! = 5 * 4 * 3 * 2 * 1 = 120
- currency converter: Write a program that converts any integer or floating point value to a currency string, e.g. 4 = “four dollars”
- Naughts and crosses: The game board is represented by three lines of text:
123
456
789
Player “X” goes first:
X23
456
789
Player “O” goes next:
X23
4O6
789
The game continues until all 9 squares are filled.
Challenges: Make Player “O” a computer player (so randomise their move). Make it check for “3 in a row” after each go, and display the winner if there is 3 in a row - card deck: create an OOP class for a deck of cards with the methods shuffle() and drawOneCard(). also create a constructor that populates the deck.
- create a Player class (Object Oriented Programming) with a variable healthset to 100 when an object of the class is instantiated (ie created). add the methods gainHealth(int amt) and loseHealth(int amt) to the class. write code for these, where amt is the amount of health i gain / lose when the functions are called.
- complete the game of "battleships" using the multidimensional array examples. challenge: do it with methods (so make it modular, which means keep the main method short). epic mlg challenge: do it using object oriented programming techniques