Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Function printRanges(7, 3) produces the following output: 7 0 1 2 3 4 5 6 6 0 1

ID: 3787472 • Letter: F

Question

Function printRanges(7, 3) produces the following output: 7 0 1 2 3 4 5 6 6 0 1 2 3 4 5 5 0 1 2 3 4 4 0 1 2 3 3 0 1 2 Write function printRanges(high, low) that produces output according to the rule implied by the example output. You may assume high and low are positive integers with high >= low. You function *must* contain two loops, one nested within the other. Your function may not use any comprehensions, string operations, range, or list functions; the goal is to practice loops and incrementing. FOR PYTHON

Explanation / Answer

# As range is not allowed we use while loop and
# increment/decrement loop counter ourself
def printRanges(high, low):
  
   # This loop variable is use to control numbers printed on same line
   i = high
  
   # we decrement i in each loop to print number from 0 to i
   # outer loop print for each i until it is same as low
   while i >= low:
       j = 0
       print i
       # inner loop print number from 0 to i. ',' after print
       # is required so that things are printed on same line
       while j < i:
           print j,
           j = j + 1
       print ""
       i = i - 1


printRanges(7,3)

'''

output

7
0 1 2 3 4 5 6
6
0 1 2 3 4 5
5
0 1 2 3 4
4
0 1 2 3
3
0 1 2

'''