160 likes | 301 Vues
In this lecture, Dr. Patricia J. Riddle guides students through the principles of nested loops in programming. By the end of the session, participants will learn how to effectively use nested loops to process data structures like lists. The lecture covers practical examples, including functions for changing case, identifying isograms, finding prime numbers, and other exercises to reinforce the concept. Additionally, students will explore the power of nested loops in manipulating sequences of data, preparing them for more advanced programming challenges.
E N D
COMPSCI 101Principles 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 COMPSCI 101 - Principles of Programming
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
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
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
Answer COMPSCI 101 - Principles of Programming
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
Answer COMPSCI 101 - Principles of Programming
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
Answer COMPSCI 101 - Principles of Programming
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
Answer COMPSCI 101 - Principles of Programming
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
Answer COMPSCI 101 - Principles of Programming
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
Tomorrow • Random Number Generation, • User Input, • While Loops COMPSCI 101 - Principles of Programming