Programming Challenges Attempt these programming challenges in order. No. Challenge 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. Ask the user for their name. Display the text ‘Hello’ followed by the name given. So, if the user answered ‘Bob’ your program will display ‘Hello Bob’. Make sure there’s a space! Ask the user for the length and width of a rectangle then calculate and display its area. Test your program with several different rectangles. Ask the user for two numbers. Print out the biggest one. Ask the user for two numbers. Divide the biggest one by the smallest one. Ask the user for the radius of a circle and calculate its area. Pi = 3.14159 Ask the user for a number and display on the screen whether the number is odd or even. Ask the user for a temperature in degrees Fahrenheit and convert it to degrees Celsius. Subtract 32, then multiply by 5, then divide by 9 Ask the user for the distance and time that a journey was completed in. Calculate the average speed that the journey was travelled in. Ask the user for some input and display the number of characters in their response. Write a subroutine that takes two numbers. The first number indicates the number of spaces that should be displayed and the second indicates the number of Xs that should be displayed. These should both be displayed on the same line. For example drawX(3,4) would display _ _ _ X X X X (_ indicates a space). Now write another subroutine that makes multiple calls to your program above and draws a picture with Xs. So you could draw: X XXX XXXXX XXX XXXXX XXXXXXX XXXXXXXXX XXXXX XXXXXXX XXXXXXXXX XXXXXXXXXXX X X A gardener needs to buy some turf for a project they are working on. The garden is rectangular with a circular flower bed in the middle. Ask for the dimensions of the lawn and the radius of the circle. Calculate and display the amount of turf needed to re turf the garden. Test your program with several different sets of dimensions. The same gardener wants to determine the cheapest way of achieving this job. Turf costs £1.20 per square metre and grass seed costs £0.20 per square metre. However, seed takes 6 weeks to be hardy enough to be untended and so there would be an extra cost of £10 per week for the gardener to tend the garden. Modify your program from the previous challenge to calculate the costs of using both turf and seed with the gardener tending it for 6 weeks. Test your program with several different garden and flower bed sizes. Ask the user for some input and convert it to UPPER case. Done Programming Challenges No. Challenge 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. A user is signing up for your latest and greatest service and needs to provide a string password. Ask the user for a password and determine its strength on the following scale. Password contains only letters or is less than 5 characters long – WEAK. Password contains a mixture of letters, numbers and symbols and is less than 8 characters long – MEDIUM Password is 8 or more characters long - STRONG Write a program to model a six sided die. Modify your program to ask the user how many sides the die should have. Ask the user for a number. Validate that they have actually provided a number. Ask the user for a number and perform a count down from that number to zero – displaying each value on the screen as you go. At the end print ‘Blast Off!’. Ask the user for a number. If the number they enter is zero then stop the program and display what the biggest number that had been typed. If the number is not zero, ask for another. Ask the user for two numbers and display if the smallest is a factor of the biggest – i.e. there is no remainder when the biggest is divided by the smallest. Ask the user for a number and display all of its factors. Ask the user for a number which must be between 20 and 30. The user and the computer take it in turns to subtract 1, 2 or 3 from the current value. The last player to subtract a value loses. Ask the user for a number and work out if it’s a prime. Ask the user for a number and calculate the product of all the numbers from 2 to the number provided. For example, if the user provides the number 6 you should calculate 2 x 3 x 4 x 5 x 6 = 720 Ask the user for two numbers and then work out the greatest common divisor. Example: 36 and 63 are both divisible by 9 but there is no other number that is bigger than nine that both numbers can be divided by. Ask the user for some input then count and display the number of letters in their response. Ask the user for a sentence and count the number of words in it. Display a 10 x 10 grid on the screen which displays the times tables from 1 x 1 in the upper left to 10 x 10 in the bottom right. Write a program to display the Fibonacci sequence starting with 0, 1. In the Fibonacci sequence each number is the sum of the preceding two numbers so the next numbers in the sequence would be 1, 2, 3, 5, 8, 13…. Now modify the program to calculate the nth value in the sequence. So if the user asks for the 6th number in the sequence the response would be 5. Generate a random response of Rock, Paper or Scissors. Now modify your program to ask the user for their choice of Rock, Paper or Scissors and then get the computer to generate a random go. Determine and display whether the computer or the player wins. Rules: Rock beats Scissors but loses to Paper. Scissors beats Paper but loses to Rock. Paper beats Rock but loses to Scissors. Research the rules of Rock, Paper, Scissors, Lizard, Spock and modify your program to play this instead. Generate a random number and ask the user to guess what the number is. Tell them higher or lower until they get the correct number. Done Programming Challenges No. Challenge 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. Playing cards are made up of four suits (Hearts, Diamonds, Clubs and Spades) of 13 cards each (Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King). Write a program to randomly generate a single card. Run your program several times to make sure it generates a range of suits and values for the cards. The game ‘Play Your Cards Right’ is played by showing the player a single card. They must then guess whether the next card will be higher or lower than the one they’ve just seen. If the player guesses correctly they get another go. If they get it wrong they lose. Write a program using your ‘random card generator’ program from earlier to play the game ‘Play Your Cards Right’. Write a program to shuffle a deck of cards. Output the shuffled deck. Each card MUST only appear once! Hint. Use a two dimensional list of cards where the additional dimension indicates whether the card has been used already or not. Build a new list to store the shuffled deck. Use the new shuffled deck in the ‘Play Your Cards Right’ game you wrote earlier. The user should be given a score – how many cards they guessed correctly. If they guess all 51 cards correctly they can have a bonus of 10 points before they’re given a new deck to work through. Display the players score at the end of the game. Ask the user for an input. Validate this input and generate some output to state if the input was a valid number or not. A valid number must begin with either a digit or the symbol + or – and must then only contain digits and possibly one decimal point. Ask the user to enter a hexadecimal number and convert it to and display it in binary. Extend your program to also display the denary equivalent. Ask the user to choose a logic gate from AND, OR and NOT and then provide the correct number of binary inputs. Display the message: <first input> <logic gate> <second input> is <result> where <result> will be either TRUE or FALSE. Research how the gates XOR, NOR and NAND work and modify your program to include these too. Ask the user to provide a list of words – the list should be terminated by a key word such as STOP. Was the list provided in order? Your solution should deal with words provided in any case (UPPER, lower and Mixed). Get the computer to generate a random four digit code. All the digits must be different. So 1234 would be acceptable but 1134 would not! Now that you’ve generated the random code you should ask the user to guess what it is. Tell them how many digits in their guess are in the code. Only finish the game when they have guessed all four digits. Now modify the program again to not only tell the user how many of their digits are correct but also tell them how many are in the correct places. Limit their number of guesses to 15. Return to the ‘Play Your Cards Right’ game you wrote earlier. Modify the program to read the best score from a file (you’ll need to create the file!). If the player beats this score, tell them and replace the value in the file with the new highest score. You could extend this further by asking them for their name and storing this too. Write a program that asks the user for the names and number of goals scored by each team in a match. Display the message ‘x beat y by z goals’ where x is the name of the winning team, y is the name of the losing team and z is the difference in the number of goals. Now modify the program to deal with drawn matches. Done Programming Challenges No. Challenge 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. Write a program that asks the user to choose between the options of entering some football results or showing the results for a given team. If option 1 is chosen ask for the name and number of goals scored by the teams in a football match and store this in a list. If option 2 is selected ask the user for the name of a team and display all the matches that this team has played in. Hint: Use a two dimensional list with four columns. Create a text file called dictionary.txt and put at least 20 different words in it, in order. Now write a function that asks the user for a word and checks to see if the word exists in the dictionary. If it does return True otherwise return False. Using your spell check program from above allow the user to type in a sentence. Print each word from the sentence with a statement of whether it was found in the dictionary or not. Modify your program further to ask the user if any words that were not found in the dictionary should be added. If the answer is yes then add the word to the dictionary but make sure it’s inserted in the correct place – the order of the words should be correct. ROT13 is an encryption system which adds 13 to the ASCII code of any letters in a string. So A becomes N, B becomes O, C becomes P and so on. Once we get to N we start back at the beginning of the alphabet again so N becomes A, O becomes B etc… You can calculate this using remainder arithmetic (%). Write a routine that will take a string and convert all the alphabetic characters to their encoded equivalents leaving all other characters untouched. Output the cipher text. Now write the counterpart to this routine that will take the cipher text and decrypt it to give you back the original plain text message. Write a program that takes a string and replaces every letter with a *. All other characters should stay the same. Now write a program to determine if a given letter exists in a string. For example, does A exist in HELLO WORLD? If the letter does exist print out the position of each occurrence – so O in HELLO WORLD would print 4 and 7. Write a hangman game. Get the program to pick a random word from the dictionary file you created earlier. You get extra credit if your program also draws the gallows. The datetime module allows you to get the current date and time. The following code will give you the time. import datetime timestamp =datetime.datetime.now() print(timestamp.hour, timestamp.minute, timestamp.second) Using this as a starting point, write a program to display the current time as a binary clock. So if it’s currently 11:38:10 your clock would look something like this: 32 16 8 4 2 1 Hour X X X Minute X X Second X X The datetime module also lets you create a date object: birthday = datetime.date(2001, 5, 27) This creates an object with a date of 27th May 2001. You can subtract this from the current date (you got that in the last task) to work out how many days there are between the two dates. Write a program to ask someone for their date of birth and tell them how many days old they are. Done Programming Challenges No. Challenge 63. 64. 65. 66. Modify your program to ask for two dates. Tell the user how many days there are between the two dates. The answer should be a positive number. Hint: Use the ABS function! Write a program to allow two players to play noughts and crosses. Display the play board on the screen between each go. Modify your program to let the computer play against a human. In its simplest form the computer could just take a random valid move. But for extra credit try and make the computer block a possible winning move by the human. For extra extra credit, see if you can make the computer also pick moves that would cause it to win. Write a program to play battleships. The computer should generate a random 10 x 10 board with 6 ships. Done