Compsci 101 Chad Jenkins Pancakes, While Loops, Parallel

2y ago
29 Views
2 Downloads
1.06 MB
10 Pages
Last View : 22d ago
Last Download : 3m ago
Upload by : Ophelia Arruda
Transcription

Compsci 101Pancakes, While loops, Parallel ListsPart 1 of 3Susan RodgerNikki WashingtonFebruary 25, 2021while BOOL CONDITION:LOOP BODY# modify variables, affect expressionChad Jenkins Professor at Univ of Michigan Ph.D at USC Robotics problems in interactive robotics and human-robotinteraction Several committees such as CRA-WP“For robots to be useful in the real world,anyone, not only technical specialists, must beable to easily train and control them”“We’re moving past treating robots as remotecontrol devices. We’re helping them learn”2/25/2021Compsci 101, Spring 202112/25/2021Compsci 101, Spring 20212https://xkcd.com/710/When is a game of chess over?Collatz Conjecture(Hailstone) If you were to write a program to play chess how many rounds in a game?If number is evenDivide by 2If number is oddmultiply by 3 and add 12/25/2021Compsci 101, Spring 202132/25/2021Compsci 101, Spring 20214

Why Solve This? In Python?Developing and Reasoning aboutWhile Loops https://en.wikipedia.org/wiki/Collatz conjecture We want to illustrate an indefinite loop One of many mathematical sequences, but Don't know: how many times loop executes a priori knowledge, we'll know afterword There's an XKCD comic about it! Not everyone enjoys XKCD, but Do know: condition that should be true after loop Its negation is the expression forBOOL CONDITION (loop guard) Mathematics is foundational in computerscience, but Not everyone enjoys logic/math puzzles, but 2/25/2021Compsci 101, Spring 20215History: From while to for loopswhile loop (sum list)lst [4,1,8,9]s 0i 0while i len(lst):s lst[i]i 1print(s)2/25/2021for loop (sum list)lst [4,1,8,9]s 0for n in lst:s nprint(s)Compsci 101, Spring 2021while BOOL CONDITION:LOOP BODY# modify variables, affect expression2/25/2021Compsci 101, Spring 20216Concrete Example: Collatz/Hailstone Don't know: how many times loop executes some numbers: long sequences, others short Do know: condition that should be true after loop It's negation is the expression for loop guard! What is true after loop below finishes?while value ! 1:loop body# modify value somehow72/25/2021Compsci 101, Spring 20218

Collatz codeCollatz CodeWhat is new in thiscode? What doesthat new stuff do?What is this codedoing? What getsupdated? Is the loopguaranteed to stop?2/25/2021Compsci 101, Spring 202192/25/2021Compsci 101Pancakes, While loops, Parallel ListsPart 2 of 3Compsci 101, Spring 202110Collatz CodeSusan RodgerNikki WashingtonFebruary 25, 2021while BOOL CONDITION:LOOP BODY# modify variables, affect expression2/25/2021Compsci 101, Spring 2021112/25/2021Compsci 101, Spring 202112

Collatz: New stuffCollatz: Guaranteed to stop?Default value, ifno argumentcurrent influences thestopping conditionSince current isalways changed,this shouldeventually stopSyntax for nicerformatting2/25/2021Compsci 101, Spring 2021132/25/2021Compsci 101, Spring 202114Think: Analysis in Collatz.pyCollatz Data – Average no. of stepsWhy do both rangecalls have 1? How do we gather data for numbers 10,000? In general for numbers in range(low,high) ? Call function, store result, store 10,000 results?Why noprinting whenthis is called? We'd like counts[k] to be length of sequence for k How do we allocate 10,000 list elements? Like there is "hello" * 3 There is [0] * 100002/25/2021Compsci 101, Spring 2021152/25/2021Compsci 101, Spring 202116

Analysis in Collatz.pycounts list when limit is 8? Counts is of size 8 1, we ignore slot 0 hailstone(1), get 0 hailstone(2), get 1 step, just divide by 22/25/2021Compsci 101, Spring 2021172/25/2021counts list when limit is 8? Counts is of size 8 1, we ignore slot 0 hailstone(1), get 0 hailstone(2), get 1 step, just divide by 22/25/2021Compsci 101,1, SpSpringpring 202119Storeanswer forhailstone(2)in index 2Compsci 101,1, SpringSppring 202118counts list when limit is 8? hailstone(3), get 7 (10, 5, 16, 8, 4, 2, 1) hailstone(4), get 22/25/2021Compsci 101, Spring 202120

counts list when limit is 8?counts list when limit is 8? hailstone(3), get 7Storeanswer forhailstone(3)in index 3 hailstone(5), get 5 (16, 8, 4, 2, 1) hailstone(4), get 2Storeanswer forhailstone(4)in index 4 And so on . Hailstone(6) is 8, hailstone(7) is 16, hailstone(8) is 32/25/2021Compsci 101, Spring 2021212/25/2021Compsci 101Pancakes, While loops, Parallel ListsPart 3 of 3Susan RodgerNikki WashingtonFebruary 25, 2021while BOOL CONDITION:LOOP BODY# modify variables, affect expressionCompsci 101101, Spring 2021Storeanswer forhailstone(5)in index 522Parallel Lists Case Study: FileFrequency.py We'd like to analyze word occurrences Google N-Gram, it's easy to do, but What about occurrences of "cgat" in genome? What about Rotten Tomatoes? This code is built using the tools that we have In the future, learn of more efficient structures We'll use an API for opening files2/25/2021Compsci 101, Spring 2021232/25/2021Compsci 101, Spring 202124

High Level ViewHigh Level View We will use parallel lists to track data Each word is stored in a list named words Word’s count is stored in a list named counts # occurrences of words[k] is in counts[k] We will use parallel lists to track data Each word is stored in a list named words Word’s count is stored in a list named counts # occurrences of words[k] is in counts[k]["apple", "fox", "vacuum", "lime"][5,2,25,15]["apple", "fox", "vacuum", "lime"][6,2,25,15][6, What happens when we read a word? What happens when we read a word?Read word “apple”?2/25/2021Compsci 101, Spring 202125Read word “apple”?2/25/2021High Level ViewCompsci 101, Spring 202126High Level View We will use parallel lists to track data Each word is stored in a list named words Word’s count is stored in a list named counts # occurrences of words[k] is in counts[k] We will use parallel lists to track data Each word is stored in a list named words Word’s count is stored in a list named counts # occurrences of words[k] is in counts[k]["apple", "fox", "vacuum", "lime"][6,2,25,15]["apple", "fox", "vacuum", "lime”,"banana"]Add into words[6,2,25,15] What happens when we read a word? What happens when we read a word?Read word “banana”?2/25/2021Compsci 101, Spring 202127Read word “banana”?2/25/2021Compsci 101, Spring 202128

High Level ViewHigh Level View We will use parallel lists to track data Each word is stored in a list named words Word’s count is stored in a list named counts # occurrences of words[k] is in counts[k] We will use parallel lists to track data Each word is stored in a list named words Word’s count is stored in a list named counts # occurrences of words[k] is in counts[k]["apple", "fox", "vacuum", "lime”,"banana"]Expand counts,0][6,2,25,15,0]["apple", "fox", "vacuum", "lime”,"banana"]Add one,1][6,2,25,15,1] What happens when we read a word? What happens when we read a word?Read word “banana”?2/25/2021Compsci 101, Spring 202129Pseudo-code for getFileData Let user choose a file to open Read each line of the file Process each word on the lineRead word “banana”?2/25/2021Step 3 of 7 steps:Generalize If word never seen before? Add to words and counts Update # occurrences using .index and locationCompsci 101, Spring 20213130Pseudo-code for getFileData Let user choose a file to open SOME KIND OF CODE CHOOSES A FILE Read each line of the file FOR LOOP Process each word on the line SPLIT, FOR LOOP If word never seen before? Add to words and counts– IF STATEMENT, UPDATE LIST Think: What would we do for each color whendoing step 5 (translate to code) of the 7 steps?2/25/2021Compsci 101, Spring 2021 Update # occurrences using .index and location– UPDATE LIST, USE INDEX FUNCTION2/25/2021Compsci 101, Spring 202132

Pseudo-code for getFileDataFrom Pseudo to Code Let user choose a file to open SOME KIND OF CODE CHOOSES A FILE Read each line of the file FOR LOOP Process each word on the lineProcess line in fileProcessword in line SPLIT, FOR LOOP If word never seen before? Add to words and counts– IF STATEMENT, UPDATE LIST Update # occurrences using .index and location– UPDATE LIST, USE INDEX FUNCTION2/25/2021Compsci 101, Spring 202133 Why do we have a loop in a loop? Code mirrors structure: file has lines, lines have words Notice: .strip .split .lower not in .append .index 2/25/2021Outer loopfor line in f:data line.strip().split()Inner loopfor word in data:word word.lower()if word not in words:words.append(word)counts.append(0)location words.index(word)counts[location] 1Compsci 101, Spring 202135What is guaranteedabout words andcounts?2/25/2021Comparing Two ApproachesUpdatecountAdd if notseen beforeCompsci 101, Spring 202134Comparing Two Approaches Why do we have only one loop? Code mirrors structure, which is better? File is a sequence of characters!!for word in f.read().lower().split():if word not in words:words.append(word)counts.append(0)location words.index(word)counts[location] 12/25/2021Compsci 101, Spring 202136

Comparing Two Approaches Why do we have only one loop? Code mirrors structure, which is better?Same in File is a sequence of characters!!bothfor word in f.read().lower().split():if word not in words:words.append(word)counts.append(0)location words.index(word)counts[location] 12/25/2021Compsci 101, Spring 202137

2/25/2021 Compsci 101, Spring 2021 10 Compsci 101 Pancakes, While loops, Parallel Lists Part 2 of 3 2/25/2021 Compsci 101, Spring 2021 11 Susan Rodger Nikki Washington February 25, 2021 while BOOL_CONDITION: LOOP_BODY # modify variables, affect expression Collatz Code 2/25/2021 Compsci 101, Spring 2021 12

Related Documents:

9/5/22 Compsci 201, Fall 2022, OOP 3. Recapping some Java Themes 9/5/22 Compsci 201, Fall 2022, OOP 4. Comments on Java Style Code blocks: Opening {ends first line of if, for, while, or method . Aside: Python uses objects too 9/5/22 Compsci 201, Fall 2022, OOP 15 Split is a methodwe are calling on this String object, not a

Plugin progress Summary 8. Testing and Debugging Jenkins Plugins Running tests with Maven Debugging Jenkins Server debugging – a quick recap Debugging with IntelliJ Debugging with Eclipse mvnDebug The Jenkins Logger Console Summary 9. Putting Things Together The Jenkins script console and Groovy G

CI and CD. Also, the need to undustrialize Jenkins jobs has been growing and classic Jenkins 1.x Freestyle/Maven jobs started to be too limited for certain needs. Under Jenkins 1.x a plugin called workflow-plugin appeared to allow developers to write code to describe jobs. Jenkins 2 goes further by adding built-in support for Pipeline as Code .

How the Dictionary is made Using a dictionary is reasonably straight -forward We will be clients, not implementers Efficiency not a large concern in 101 Our goal is to just get stuff done 10/8/2020 Compsci 101, Fall 2020 4 This Photo by Unknown Author is licensed under CC B

Verkehrszeichen in Deutschland 05 101 Gefahrstelle 101-10* Flugbetrieb 101-11* Fußgängerüberweg 101-12* Viehtrieb, Tiere 101-15* Steinschlag 101-51* Schnee- oder Eisglätte 101-52* Splitt, Schotter 101-53* Ufer 101-54* Unzureichendes Lichtraumprofil 101-55* Bewegliche Brücke 102 Kreuzung oder Einmündung mit Vorfahrt von rechts 103 Kurve (rechts) 105 Doppelkurve (zunächst rechts)

BNL Demonstration Coil Stored Energy 1.7 MJ Currrent 700 Amperes Inductance 7 Henry Maximum Field 25 Tesla Operating Temperature 4.2 Kelvin Overall Ramp Rate 1.2 Amp/sec Number of Inner Pancakes 28 Number of Outer Pancakes 18 Total Number of Pancakes 46 Inner dia of Inner Pancake 102 mm Outer dia of Inner Pancake 194 mm Inner dia of Outer .

Buttermilk Pancakes A stack of three buttermilk pancakes. 4.99 Pancake Platter* Three pancakes, three eggs and your choice of two slices of bacon or sausage links.

VOLUME 99 OCTOBER 2018 NUMBER 4 SUPPLEMENT Supplement to The American Journal of Tropical Medicine and Hygiene ANNUAL MEETING SIXTY-SEVENTH “There will be epidemics ” Malaria Cases on the Rise in Last 3 Years-2016 Ebola Out of Control-2014 Zika Spreads Worldwide-2016 Island Declares State of Emergency Over Zika Virus, Dengue Fever Outbreak-2016 EBOLA: WORLD GOES ON RED ALERT-2014 An .