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)

gryta
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 defchange_case2(sentence,my_type): new_sentence= "" uppercase = ["A", "B", "C", "D", "E", "F", "G", "H", "I”, "J”, "K”, "L”, "M”, "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"] lowercase = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"] for i in range(0,len(sentence)): if (my_type == "upper" and sentence[i] in uppercase) or (my_type == "lower" and sentence[i] in lowercase) or (sentence[i] == " "): new_sentence += sentence[i] else: for j in range(0,len(lowercase)): if my_type == "upper" and lowercase[j] == sentence[i]: new_sentence += uppercase[j] elifmy_type == "lower" and uppercase[j] == sentence[i]: new_sentence += lowercase[j] return new_sentence 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 def isogram2(word): num= 0 for letter in word: if word[0] == letter: num = num + 1 for i in range(1,len(word)): count = 0 for letter in word: if word[i] == letter: count = count + 1 if num != count: return False return True 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 deffind_primes(num): primes = [] for i in range(2,num+1): divisible = False for j in range(2,i): if i % j == 0: divisible = True if not divisible: primes += [i] return primes 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 def mutate(filename,word): dictionary_file = open(filename, "r") dictionary = dictionary_file.read() dictionary_list = dictionary.split() word_list = [] alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v” ,"w", "x", "y", "z"] for i in range(0,len(word)): for new_letterin alphabet: if word[i] != new_letter: test_word= replace_letter(word,i,new_letter) if test_word in dictionary_list: word_list+= [test_word] return word_list defreplace_letter(word,i,new_letter): if i == 0: return new_letter + word[i+1:] elifi == len(word)-1: return word[:i] + new_letter else: return word[:i] + new_letter + word[i+1:] 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 defthree_letter_anagram(word): answer= "" answer_list = [] word_range = range(0,len(word)) for index1 in word_range: for index2 in word_range: if index2 != index1: index3 = 3 - index1 - index2 answer = "" + word[index1] + word[index2] + word[index3] answer_list.insert(0,answer) return answer_list 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