Sheet 04: Loops and Iterations Solution

$30.00

Description

State whether true or false:-

  1. A Python while implements a definite loop.

  2. The counted loop pattern uses a definite loop.

  3. A sentinel loop asks the user whether to continue on each iteration.

  4. A sentinel loop should not actually process the sentinel value.

  1. The easiest way to iterate through the lines of a file in Python is to use a while loop.

  1. The Boolean operator or returns True when both of its operands are true.

  2. a and (b or c) == (a and b) or (a and c)

  1. not(a or b) == (not a) or not(b)

  2. True or False

Multiple Choice

  1. A loop pattern that asks the user whether to continue on each iteration is called a(n)

    1. interactive loop

    2. end-of-file loop

    3. sentinel loop

    4. infinite loop

  2. A loop pattern that continues until a special value is input is called a(n)

    1. interactive loop

    1. end-of-file loop

    2. sentinel loop

    3. infinite loop

  1. A loop structure that tests the loop condition after executing the loop body is called a

    1. pre-test loop

    2. loop and a half

    3. sentinel loop

    4. post -test loop

  2. A priming read is part of the pattern for a(n)

    1. interactive loop

    2. end-of-file loop

    3. sentinel loop

    4. infinite loop

  3. What statement can be executed in the body of a loop to cause it to terminate?

    1. if

    2. input

    3. break

    4. exit

  1. Which of the following is not a valid rule of Boolean algebra?

    1. (True or x) == True

    2. (False and x) == False

    3. not(a and b)== not(a) and not(b)

    4. (True or False) == True

  1. A loop that never terminates is called

    1. Busy

    1. Indefinite

    2. Tight

    3. infinite

  1. Which line would not be found in a truth table for and?

    1. T T T

    2. T F T

    3. F T F

    4. F F F

  1. Which line would not be found in a truth table for or?

    1. T T T

    2. T F T

    3. F T F

    1. F F F

  1. The term for an operator that may not evaluate one of its subexpressions IS

    1. short –circuit

    2. faulty

    3. exclusive

    4. indefinite

Discussion:

  1. Compare and contrast the following pairs of terms:

    1. definite loop vs. indefinite loop

    2. for loop vs. while loop

    1. interactive loop vs. sentinel loop

    2. sentinel loop vs. end -of-file loop

  1. Give a truth table that shows the Boolean value of each of the following Boolean expressions, for every possible combination of “input” values. Hint: Including columns for intermediate expressions is helpful.

    1. not (P and Q)

    2. (not P) and Q

    1. (not P) or (not Q)

    2. (P and Q) or R

e) (P or R) and (Q or R)

  1. Show the sequence of numbers that would be generated by each of the following range expressions.

    1. range (5)

    2. range (3, 10)

    3. range (4, 13, 3)

    4. range (15, 5, -2)

    5. range (5, 3)

  1. Show the output that would be generated by each of the following program fragments. You must show step by step.

a) for i in range (1, 11) : print (i*i)

  1. for i in [1,3,5,7,9]: print (i, “:”, i**3)

print (i)

  1. x = 2 y = 10

for j in range (0, y, x) : print (j, end=””) print (x + y)

print (“done”)

  1. ans = 0

for i in range (1, 11) : ans = ans + i*i print (i)

print (ans)

  1. Write a while loop fragment that calculates the following values:

    1. Sum of the first n counting numbers: 1 + 2 + 3 + . . . + n

    2. Sum of the first n odd numbers: 1 + 3 + 5 + . . . + 2n – 1

    1. Sum of a series of numbers entered by the user until the value 999 is entered. Note: 999 should not be part of the sum.

    1. The number of times a whole number n can be divided by 2 (using integer division) before reaching 1.

Programming Exercises:

  1. The Fibonacci sequence starts 1, 1, 2, 3, 5, 8,. … Each number in the sequence (after the first two) is the sum of the previous two. Write a program that

computes and outputs the nth Fibonacci number, where n is a value entered by the user.

  1. Write a program that uses a while loop to determine how long it takes for an investment to double at a given interest rate. The input will be an annualized interest rate, and the output is the number of years it takes an investment to double. Note: The amount of the initial investment does not matter; you can use $ 1.

3. A positive whole number n > 2 is prime if no number between 2 and

(inclusive)

evenly divides n. Write a program that accepts a value of n as

input and

determines if the value is prime. If n is not prime, your program should quit as soon as it finds a value that evenly divides n.

  1. Modify the previous program to find every prime number less than or equal to n.

  1. Write a program to find the maximum and minimum of n numbers entered by the user.

  1. Evaluate the following program manually (step by step) for the following cases:-

    1. = int(input(‘Enter x>’))

    2. = int(input(‘Enter y>’))

if x > y:

smaller = y

else:

smaller = x

for i in range(1, smaller+1):

if((x % i == 0) and (y % i == 0)):

hcf = i

print(hcf)

x

10

10

23

1000

y

50

53

17

1010

__________________________________________________________________________


error: Content is protected !!