Problem 5.3) Write the following functions

  • def firstDigit(n) (returning the first digit of the argument)
  • def lastDigit(n) (returning the last digit of the argument)
  • def digits(n) (returningthenumberofdigitsoftheargument)

For example, firstDigit(1729) is 1, lastDigit(1729) is 9, and digits(1729) is 4. Provide a program that tests your functions.

Problem 5.8)

It is a well-known phenomenon that most people are easily able to read a text whose words have two characters flipped, provided the first and last letter of each word are not changed. For example,

I dn’ot gvie a dman for a man taht can olny sepll a wrod one way. (Mrak Taiwn)

Write a function scramble(word) that constructs a scrambled version of a given word, randomly flipping two characters other than the first and last one. Then write a program that reads words and prints the scrambled words.

Problem 3

Write an application in Python that allows a user to play the game Bulls and Cows against a computer. The game works as follows: The computer chooses a 4-digit number in secret. The digits must all be different. The user then guesses the number and the computer provides the number of matching digits. If the matching digit is in the right position it is a "bull", if it is on a different position it is a "cow". For example:

Computer chooses: 3691
User guesses: 1649
Computer answers: 1 bull and 2 cows

If the user guesses a number with repeat digits that is partially correct the rule is that a correct digit can only count once and bulls count before cows. So for example

Computer chooses: 3691
User guesses: 4211
Computer answers: 1 bull and 0 cows

Your program should report the number of attempts the user needed to guess the number, it should let a user play as many times as they wish in a single session, and it should report their best, worst, and average performance over the course of the session. Hint: you may find it easier to store these numbers as strings or in lists rather than as plain integers.

To accomplish this you will create two files: bulls_and_cows.py and game.py . I have attached templates for these two modules with outlines of the functions they should contain. You must use the attached templates. Write the code that is needed to complete the three functions in the bulls_and_cows module and the play_game function in the game module. The play_game function should make use of the functions you write in the bulls_and_cows module. Do not modify the main function in the game module. Your code must work with the main function that is provided as is.

Challenge Work

Write a function that allows a computerized player to play and takes on average less than 8 turns to guess the secret number. Write a simulation program to demonstrate this ability by playing 1000 games of computer versus computerized user and reporting the average number of tries. Provide a separate file with its own main function for this part.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.