Computer Science I W Addison Woods Assistant Teaching















- Slides: 15
Computer Science I W. Addison Woods Assistant Teaching Professor Georgetown University Department of Computer Science May 23, 2018 1
Computer Science I – Lecture 11 Control Structures for Repetition • Outline for today – Control Structures for Repetition (review) • while loop • for loop • which loop to use? • hand tracing – Divisibility by 3 (review) – Programming Challenge • problem description • analyze the problem • design algorithm(s) • test logic of design • implement solution • test solution – Questions May 23, 2018 2
Control Structures for Repetition review while condition: statement(s) for i in range(<start, > stop <, stride>): statement(s) initialization code condition true statement(s) false update code test false true statement(s) May 23, 2018 3
val = 5 output = "" Hand Tracing (review) while val >= 0: output = output + str(val) + " " val = val – 1 print(output) What output does the code above generate? Iteration val (val >= 0) output 1 5 true 5 4 2 4 true 54 3 3 3 true 543 2 4 2 true 5432 1 5 1 true 54321 0 6 0 true 543210 -1 7 -1 false -- -- Cumulative Output: 5 5 -4 May 23, 2018 val - 1 4 3 2 1 0 See Section 3. 11 Focus on Debugging: Hand Tracing a Program
Divisibility by 3 review Problem Statement: Having long been fascinated by numbers and division, you routinely wonder if a randomly observed number might be divisible by 3. For small numbers this is easy enough to solve by inspection. Certainly for 3, 6, 9, and even 25635984. But what about very large numbers? We need to design an algorithm that will provide the answer for numbers of any value. Start Input num. To. Test ✗ answer = num. To. Test modulo 3 false answer is equal to 0? num. To. Test is divisible by 3 num. To. Test is NOT divisible by 3 5 -5 May 23, 2018 true End
Divisibility By 3 – Problem Analysis Focus on one digit at a time Is this number divisible by 3? If the sum of the digits of a number is divisible by three, then so is the original number 123 123 123 6 1 2 5 3 Keep a running total May 23, 2018 6
Divisibility By 3 – Algorithm Design May 23, 2018 Source: Computer History Museum (http: //www. computerhistory. org/revolution/the-art-of-programming/9/357/2216) 7
value = num. To. Test total = 0 Input num. To. Test Start divisibility by 3 design version 1 false value > 0 true a = last digit of value total = total + a value = value / 10 num. To. Test is NOT divisible by 3 May 23, 5 -82018 false total is 3, 6, or 9 End true num. To. Test is divisible by 3
Divisibility By 3 – Design Testing Focus on one digit at a time Is this number divisible by 3? This is not good! 923 923 923 142 3 5 9 Keep a running total May 23, 2018 9
Divisibility By 3 – Problem Analysis Focus on one digit at a time Is this number divisible by 3? Repeatedly summing the digits of 923 results in a number not divisible by three; therefore, 923 is not divisible by three. May 23, 2018 14 14 1 4 5 Keep a running total 10
Start Input original. Num num. To. Test = original. Num value = num. To. Test total = 0 false true num. To. Test >= 10 false value > 0 num. To. Test = total true a = last digit of value total = total + a value = value / 10 divisibility by 3 design version 2 false num. To. Test is 3, 6, or 9 original. Num is divisible by 3 original. Num is NOT divisible by 3 May 23, 2018 5 -11 true End
Divisibility By 3 – Write the Code a = last digit of value total = total + a value = value / 10 Use the modulus operator to retrieve the value of the last digit: ✗ 123 % 10 = 3 ) ⏌ b / a ⎿ * Use the division operator to remove the last digit: b ( – a = b % a ” n 123 / 10 = 12 o i t a u q e l o “co Use the “cool equation” to retrieve the value of the last digit: 123 – ( 10 * (floor( 123 / 10 )) ) May 23, 2018 12
Divisibility By 3 – Write the Code while num. To. Test >= 10: value = num. To. Test; total = 0 while value > 0: total += value - ( 10 * ( floor( value/10 ) ) ) value = value / 10 # END inner loop num. To. Test = total # END outer loop if ( num. To. Test == 3 or num. To. Test == 6 or num. To. Test == 9 ): print original. Number, "is divisible by 3!" else: print original. Number, "is NOT divisible by 3!" May 23, 2018 13
Divisibility By 3 – Test the Code May 23, 2018 14
Questions? 5 -15 May 23, 2018















