[Tutor] python Task
delegbede at dudupay.com
Fri Oct 1 00:38:31 CEST 2010
Kindly help me with the following tasks.
You may want to start with explanations for me and then pseudo-codes,
I should be able to take it from there.
They are exercises from deitel how to program for Python.
I have done a lot of thinking and its almost discouraging me. Please help.
4.4 An integer greater than 1 is said to be prime if it is divisible
by only 1 and itself. For example,
2, 3, 5 and 7 are prime numbers, but 4, 6, 8 and 9 are not.
a) Write a function that determines whether a number is prime.
b) Use this function in a program that determines and prints all the
prime numbers between 2 and 1,000.
c) Initially, you might think that n/2 is the upper limit for which
you must test to see whether a number is prime, but you need go only
as high as the square root of n. Rewrite the program and run it both
ways to show that you get the same result.
4.5 An integer number is said to be a perfect number if the sum of its
factors, including 1 (but not the number itself), is equal to the
number. For example, 6 is a perfect number, because 6 = 1 + 2
+ 3. Write a function perfect that determines whether parameter number
is a perfect number. Use this function in a program that determines
and prints all the perfect numbers between 1 and 1000.
Print the factors of each perfect number to confirm that the number is
indeed perfect. Challenge the power of your computer by testing
numbers much larger than 1000.
4.7 Write a program that plays the game of “guess the number” as
follows: Your program chooses the number to be guessed by selecting an
integer at random in the range 1 to 1000. The program then displays
I have a number between 1 and 1000.
Can you guess my number?
Please type your first guess.
The player then types a first guess. The program responds with one of
1. Excellent! You guessed the number!
Would you like to play again (y or n)?
2. Too low. Try again.
3. Too high. Try again.
If the player's guess is incorrect, your program should loop until the
player finally gets the number right. Your program should keep telling
the player Too high or Too low to help the player “zero in” on the
correct answer. After a game ends, the program should prompt the user
to enter "y" to play again or "n" to exit the game.
4.8 (Towers of Hanoi) Every budding computer scientist must grapple
with certain classic problems.
The Towers of Hanoi (see Fig. 4.23) is one of the most famous of
these. Legend has it that, in a temple in the Far East, priests are
attempting to move a stack of disks from one peg to another. The
initial stack had 64 disks threaded onto one peg and arranged from
bottom to top by decreasing size.
The priests are attempting to move the stack from this peg to a second
peg, under the constraints that exactly one disk is moved at a time
and that at no time may a larger disk be placed above a smaller disk.
A third peg is available for holding disks temporarily. Supposedly,
the world will end when the priests complete their task, so there is
little incentive for us to facilitate their efforts.
Let us assume that the priests are attempting to move the disks from
peg 1 to peg 3. We wish to develop an algorithm that will print the
precise sequence of peg-to-peg disk transfers.
If we were to approach this problem with conventional methods, we
would rapidly find ourselves hopelessly knotted up in managing the
disks. Instead, if we attack the problem with recursion in mind, it
immediately becomes tractable. Moving n disks can be viewed in terms
of moving only n - 1 disks (hence, the recursion), as follows:
a) Move n - 1 disks from peg 1 to peg 2, using peg 3 as a temporary
b) Move the last disk (the largest) from peg 1 to peg 3.
c) Move the n - 1 disks from peg 2 to peg 3, using peg 1 as a
temporary holding area.
The process ends when the last task involves moving n = 1 disk, i.e.,
the base case. This is accomplished trivially by moving the disk
without the need for a temporary holding area.
Write a program to solve the Towers of Hanoi problem. Use a recursive
function with four
a) The number of disks to be moved
b) The peg on which these disks are initially threaded
c) The peg to which this stack of disks is to be moved
d) The peg to be used as a temporary holding area
Your program should print the precise instructions it will take to
move the disks from the starting peg to the destination peg. For
example, to move a stack of three disks from peg 1 to peg 3, your
program should print the following series of moves:
1 → 3 (This means move one disk from peg 1 to peg 3.)
1 → 2
3 → 2
1 → 3
2 → 1
Sent from my mobile device
Elegbede Muhammed Oladipupo
Mobile Banking Solutions | Transaction Processing | Enterprise
More information about the Tutor