Problem Solving Methodology in IT (COMP1001) Assignment Three




5/5 - (1 vote)

1. [Weight = 4] (Bitwise left shifting) A very useful function for fast multiplication and division is bit
shifting. The usage of Python’s Bitwise Left Shift is given in

Write a
function for this Bitwise Left Shift operator. The function signature is given below.
Function bitLeftShift(binIn, n):

– binIn: a binary number stored as a string. The least most
significant bit is stored as the first character in the
string and so forth.
– n: the number of bits to be shifted and n >= 0.

Output: bin(binIn << n)
In your .py file, you must include the following code for testing.
print(“Test cases:”)
print(“Input: “, “00011, 3”, “Output: “, bitLeftShift(“00011”, 3))
print(“Input: “, “11111, 3”, “Output: “, bitLeftShift(“11111”, 3))
print(“Input: “, “00000, 3”, “Output: “, bitLeftShift(“00000”, 3))
print(“Input: “, “11111, 0”, “Output: “, bitLeftShift(“11111”, 0))
print(“Input: “, “10101, 5”, “Output: “, bitLeftShift(“10101”, 5))
print(“Input: “, “00001, 8”, “Output: “, bitLeftShift(“00001”, 8))

The outputs should be
You cannot use any built-in functions, such as int(), eval(), and len(). You also cannot use any
string methods. You also should not convert it to decimal number and so on. Therefore, the
remaining choice is to use a control loop to process the input string.

2. [Weight = 3] (Computing √2) √2 is a very interesting number. It has been analyzed and studied
for thousands of years. One of the problems is to estimate its value. One of the methods is the
Babylonian method which first picks an initial guess, a0 > 0. Starting from a0, the subsequent ai,
i > 0, will be computed based on

Given a0 = 1, you can easily verify that
• a1 = 1/2 3/2 = 1.5
• a2 = 17/12 = 1.416…
• a3 = 577/408 = 1.414215…
• a4 = 665857/470832 = 1.4142135623746…

The approximation is getting more accurate as more terms are obtained. Using 65 decimal places,
√2 = 1.41421356237309504880168872420969807856967187537694807317667973799…. In
order to increase the accuracy, in this question we will compute the rational numbers.

Write a
function called sequareRootTwo() squareRootTwo() to compute the rational number for √2).
Function sequareRootTwo(n,a)
Input: n is a non-negative integer and a is the initial
value (i.e., a0) which is a positive integer.
Output: a print out of a0, a1, a2, …, an in the form of
rational number and the form of using radix points. The
exact format is given below for n = 6.

In your .py file, you must include the following code.
What are the differences in their results? Answer this in a comment at the end of your .py file.

3. [Weight = 3] (A number-guessing game) This game is played by two parties. The first one comes
up a number, and the second will guess it. The first player can only reply whether the guessed
number is too big/small compared with the actual number. Implement the following function for
this game. You may import the random module and use random.randint() to generate a random

Function guessNumber():
Input: None

o The user will first be asked to input the lower and upper
integers of a range of integers. Both the lower and upper
integers are included.

o The program will generate a random number from this range.
o The program will then ask the user for a guess.
o If the guess is outside the range, the program will print
“out of range” and prompts the user for another input.

o The program will respond to the user’s guess by printing
“the number is too small/big.”
o The program will exit when the user guesses it correctly
and print a congratulation message with the total number
of attempts.

A sample game rounds is given below.
Please make sure that you put guessNumber() as the last line in the .py file.