1 / 16

COMPSCI 101 Principles of Programming

COMPSCI 101 Principles of Programming. Lecture 23 – Nested Loops Dr. Patricia J. Riddle. Learning outcomes. At the end of this lecture, students should be able to: Use nested loops. Review of For Loops. For  loop used  to access the elements of a list (a sequence of data)

ham
Download Presentation

COMPSCI 101 Principles of Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMPSCI 101Principles of Programming Lecture 23 – Nested Loops Dr. Patricia J. Riddle

  2. Learning outcomes • At the end of this lecture, students should be able to: • Use nested loops COMPSCI 101 - Principles of Programming

  3. Review of For Loops • For loop • used to access the elements of a list (a sequence of data) • elements are accessed in order • each element is assigned to a variable • a block of instructions is executed after each assignment COMPSCI 101 - Principles of Programming

  4. Nested Loops my_list = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] for i in my_list: for j in i: print(j) my_words = ["one" , "two" , "three"] for word in my_words: for letter in word: print(letter) COMPSCI 101 - Principles of Programming

  5. Exercise • Write a function named change_case2()that accepts a sentence and returns the same sentence in all uppercase or all lowercase, using nested loops. >>> change_case2("pApEr","upper") 'PAPER' >>> change_case2("PapeR","lower") 'paper' >>> change_case2("Can we dO a WHoleSentence","upper") 'CAN WE DO A WHOLE SENTENCE' COMPSCI 101 - Principles of Programming

  6. Answer COMPSCI 101 - Principles of Programming

  7. Exercise • Write a functionnamed isogram2()that uses nested loops and accepts a word as a parameter and returns True if it is an isogram and False otherwise. • An isogram, sometimes known as a nonpattern word, is a word or phrase in which every letter occurs the same number of times. >>> isogram2("subdermatoglyphic") True >>> isogram2("deed") True >>> isogram2("sara") False COMPSCI 101 - Principles of Programming

  8. Answer COMPSCI 101 - Principles of Programming

  9. Exercise • Write a function named find_primes()that accepts a number as a parameter and returns a list of all the primes up to (and including) that number A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. Wikipedia >>> find_primes(4) [2, 3] >>> find_primes(7) [2, 3, 5, 7] COMPSCI 101 - Principles of Programming

  10. Answer COMPSCI 101 - Principles of Programming

  11. Exercise • Write a function named mutate() that accepts the filename of a file containing a small dictionary, and a word as parameters and returns a list of words which differ from the word by only 1 letter. >>> mutate("dict5.txt","cat") ['bat', 'eat', 'fat', 'hat', 'kat', 'lat', 'mat', 'oat', 'pat', 'qat', 'rat', 'sat', 'tat', 'vat', 'cit', 'cot', 'crt', 'cut', 'cab', 'cad', 'cam', 'can', 'cap', 'car', 'caw', 'cay'] >>> mutate("dict5.txt","frog") ['grog', 'prog', 'flog', 'froe', 'from', 'frow'] COMPSCI 101 - Principles of Programming

  12. Answer COMPSCI 101 - Principles of Programming

  13. Exercise • Write a function named three_letter_anagram()that accepts a word and returns all anagrams of it. >>> three_letter_anagram("cat") ['tac', 'tca', 'atc', 'act', 'cta', 'cat'] >>> three_letter_anagram("men") ['nem', 'nme', 'enm', 'emn', 'mne', 'men'] >>> three_letter_anagram("see") ['ees', 'ese', 'ees', 'ese', 'see', 'see'] COMPSCI 101 - Principles of Programming

  14. Answer COMPSCI 101 - Principles of Programming

  15. Summary • Nested Loops • Can put a loop inside another loop • Just like you can put a conditional inside a loop COMPSCI 101 - Principles of Programming

  16. Tomorrow • Random Number Generation, • User Input, • While Loops COMPSCI 101 - Principles of Programming

More Related