@extends('template') @section('title') Lab 5 Warm-up @stop @section('head') @stop @section('content') # Lab 5, Warm-up A boy and a girl wearing gym clothes and stretching their arms to one side. They are warming up. - [Here is the worksheet as a google doc](https://docs.google.com/document/d/1szysM4Ytr2BaOSo2QaTHTPmLW9YH5jPCJfxulNcL3Ck/edit?usp=sharing) - Open the document, then File -> Make a Copy. - Then you can edit your copy with your answers. {{-- ## We'll start with some simple loops to get warmed up. In examples 1-3, predict the output of each code snippet. ### Example 1: Repeating code a certain number of times ```py for _ in range(5): print('cs') ``` ### Example 2a: Stepping through each number in range ```py for num in range(4): print(num) ``` ### Example 2b: Stepping backward through each number in range ```py for num in range(4,0,-1): print(num) ``` ### Example 3a: Stepping through each letter in a string ```py for letter in 'happy': print(letter) ``` ### Example 3b: Stepping through the string a different way ```py for letter in 'happy'[::-1]: print(letter) ``` ### Example 4: Given the function `addEmUp`, predict the outcome of the two invocations below. ```py # accumulator (gathering stuff up inside the loop) def addEmUp(number): """ What would you write here for the docstring?""" count = 0 for num in range(number+1): count = count + num # accumulating here return count ``` ```py >>> addEmUp(0) >>> addEmUp(4) ``` ### OPTIONAL Example 5: Given the function `counter`, predict the outcome of the two invocations below. ```py # count letter occurrences def counter(word,letter): """ What would you write here for the docstring? """ count = 0 for let in word: if let == letter: count = count + 1 return count ``` ```py >>> counter('cocoa','o') >>> counter('cocoa','x') ``` --}} {{-- commenting out for Spring 2021, short lab --}} {{-- ### OPTIONAL Example 6: Given the function `search`, predict the outcome of the invocations below. ```py def search(word,letter): """ """ for let in word: if let == letter: return True return False ``` ```py >>> search('coke','k') >>> search('coke','a') >>> search('metropolitan','m') ``` ### OPTIONAL Example 6: ooh! A nested loop! (one loop inside another) Predict the output of the code snippet below: ```py for letter1 in 'yes': for letter2 in 'no': print(letter1 + letter2) ``` --}} @include('/labs/lab05/_toc') @stop