CSc 7003, Programming for Data Science (Summer 2023)
Program 2 - Bulls and Cows
Consider numbers in the range 1000 to 9999 which do not have repeating digits. Given two such numbers, we define the number of BULLS and COWS for the pair of numbers as follows:BULLS = number of digits that appear in the same positions in the two numbers. COWS = number of digits that appear in both the numbers, but at different positions.For example, if the two numbers were 2367 and 1327, we have 2 BULLS (for the exact matches for digits 3 and 7) and 1 COW (for the remaining common digit 2 which is in different positions in the two numbers). If the numbers were 9852 and 8926, we have 0 BULLS and 3 COWS and if the numbers were 1234 and 4321, we have 0 BULLS and 4 COWS. It is quite clear that if the two numbers are the same, we have 4 BULLS and 0 COWS.
Write a Python program to play the Bulls and Cows game: Here are two sample runs of the program:
$ python3 Bulls.py Welcome to the game of BULLS and COWS. The objective in this game is for you to guess a 4-digit number The computer responds with how close your guess is to the target BULLS = # common digits with exact matches and COWS = # common digits in wrong position. Enter guess number 1: 1876 Bulls = 1 Cows = 1 Enter guess number 2: 1183 Your guess should not contain repeating digits Enter guess number 2: 1596 Bulls = 1 Cows = 2 Enter guess number 3: 1956 Bulls = 3 Cows = 0 Enter guess number 4: 1954 Bulls = 3 Cows = 0 Enter guess number 5: 1958 Bulls = 4 Cows = 0 Congratulations You Won!! $ python3 Bulls.py Welcome to the game of BULLS and COWS. The objective in this game is for you to guess a 4-digit number The computer responds with how close your guess is to the target BULLS = # common digits with exact matches and COWS = # common digits in wrong position. Enter guess number 1: 1234 Bulls = 0 Cows = 1 Enter guess number 2: 2345 Bulls = 0 Cows = 0 Enter guess number 3: 1678 Bulls = 0 Cows = 3 Enter guess number 4: 1679 Bulls = 0 Cows = 3 Enter guess number 5: 1670 Bulls = 0 Cows = 4 Enter guess number 6: 6170 Bulls = 0 Cows = 4 Enter guess number 7: 6710 Bulls = 1 Cows = 3 Enter guess number 8: 7610 Bulls = 2 Cows = 2 Enter guess number 9: 7601 Bulls = 1 Cows = 3 Enter guess number 10: 6701 Bulls = 0 Cows = 4 Sorry You ran out of guesses!! The correct number was: 7016Writing the following functions will be helpful:
- def extractDigits(num):
This function takes as input a 4-digit number and returns a tuple of its 4 digits. - def noRepeatingDigits(num):
This function takes as input a 4-digit number and returns True is it has no repeating digits and False otherwise. - def produceTarget():
This function returns a 4-digit random number that contains no repeating digits. You will need to import the random package.from random import * num = randint(1000,9999)
- def numberOfBulls(num1,num2):
This function takes as input two 4-digit numbers num1 and num2, each between 1000 and 9999 with no repeating digits and returns the number of BULLS for the two numbers. - def numberOfCows(num1,num2):
This function takes as input two 4-digit numbers num1 and num2, each between 1000 and 9999 with no repeating digits and returns the number of COWS for the two numbers.