CS计算机代考程序代写 Digression: Scoring Matrices

Digression: Scoring Matrices

Lecture 18
Loop Examples

L18 Loop Examples – 2

Objectives

• Loop revision

• Break statement

• Continue statement

• Loop examples

L18 Loop Examples – 3

Revision: for Loop
for i in range(10):

# do something

#—————————————————–

myList = [2,3,4,9,10]

for x in myList:

# iterates through the list elements
# do something that involves the list elements

#—————————————————–

myString = “hello there, hello world!”

for ch in myString:

# iterates through the string characters

#—————————————————–

infile = open(someFile, “r”)
for line in infile:

# iterate through the lines of the file
infile.close()

L18 Loop Examples – 4

Revision: while Loop

while :

# do something

#—————————————————–

# program to list first 10 numbers

# valid but poor use of while

i = 0

while i < 10: print(i) i += 1 #----------------------------------------------------- # program to guess a secret number n = 7 # secret number guess = 1 while guess != n: guess = int(input(“Please guess a number between 0 and 10”)) print(“You guessed correctly”) L18 Loop Examples - 5 Nested Loops • Designing nested loops – – Design the outer loop without worrying about what goes inside – Design what goes inside, ignoring the outer loop. – Put the pieces together, preserving the nesting. L18 Loop Examples - 6 Loop and a Half • Stylistically, some programmers prefer the following approach: while True: number = float(input("Enter a positive number: ")) if number >= 0: break # if valid number exit loop
print(“The number you entered was not positive”)

• Here the loop exit is in the middle of the loop body. This
is what we mean by a loop and a half.

L18 Loop Examples – 7

Revision: break and continue comparison

break statement continue statement

break
Yes

L18 Loop Examples – 8

Loop Example: Prime number

• Find whether a number is prime or not

• Find list of prime number up to N

• Find N prime numbers

• Find N prime numbers using break

L18 Loop Examples – 9

Finding whether a number is prime or not ?

def primestatus(N):

if N < 2: return False elif N < 4: return True else: for i in range(2,N//2+1): if N % i == 0: return False return True L18 Loop Examples - 10 Find list of prime number up to N def primelist(N): if N < 2: return [] elif N == 2: return [2] else: plist = [2,3] status = True for num in range(4,N+1): for i in range(2,num//2 + 1): if num % i == 0: status = False if status: plist.append(num) status = True return plist L18 Loop Examples - 11 Find N prime numbers # Find the first N prime numbers # Author: Michael J Wise def primes(N) : primelist = [2,3] for pno in range(2,N) : i = primelist[-1] + 2 # start search for next one where primefound = False # where last one left off while not primefound : # Test successive odd numbers factorfound = False for divisor in primelist : #Only use previous primes if i % divisor == 0 : factorfound = True if factorfound : # not prime i += 2 else : primelist.append(i) primefound = True return(primelist) L18 Loop Examples - 12 Finding N primes numbers – with break # Find the first N prime numbers (further optimised) import math def primes(N) : primelist = [2,3] for pno in range(2,N) : i = primelist[-1] + 2 # start search for next one where left off while True : factorfound = False if N > 100 : # time for sqrt not worth it for N<=100 stopat = int(math.sqrt(i)) for divisor in primelist : # Only test previous primes if N > 100 and divisor > stopat :
break # From divisor search loop

if i % divisor == 0 :
factorfound = True
break

if factorfound : # not prime, keep searching
i += 2

else :
primelist.append(i)
break # Got a prime, break from this prime search

return(primelist)

L18 Loop Examples – 13

Summary

• Revision of loops

• Revision of break and continue statements

• Example: prime numbers

Lecture 18�Loop Examples
Objectives
Revision: for Loop
Revision: while Loop
Nested Loops
Loop and a Half
Revision: break and continue comparison
Loop Example: Prime number
Finding whether a number is prime or not ?
Find list of prime number up to N
Find N prime numbers
Finding N primes numbers – with break
Summary