@extends('template') @section('title') Lab 6: Strings with For Loops @stop @section('head') @stop @section('content') # Lab 6: Strings with For Loops ## Handling strings A necklace strung with 5 beads. Each bead has a letter on it, and the letters spell out C S 1 1 1. ## Table of some string operations:
symbol operation example
+ concatenation str1 = 'I am '
str2 = 'your father '
darth = str1 + str2
yoda = str2 + str1
* repetition 'hee'*3 ==> 'heeheehee'
[] index str2[5]==> 'f'
[:] slice str2[2:5]==> 'ur '
in in 'dad' in str2 ==> False
'at' in str2 ==> True
not in not in str1 not in str2 ==> True
str1 not in darth ==> False
### A couple of handy string tips: + `yourString[-1]` returns the last letter of a string, eg `'hello'[-1] ==> 'o'` + `.lower()` returns a lowercase version of the given string, eg `'HeLLo.lower() ==> 'hello'` ## String functions with loops **Create a file named `lab06.py` for this part of the lab.** ### `betterStarify` Write a function called `betterStarify(word)` that takes in a word, and **returns** a new string with a `*` after each letter in the original word, but **not** after the last letter in the word. ```py print(betterStarify('OMG')) # should print O*M*G print(betterStarify('wicked')) # should print w*i*c*k*e*d print(betterStarify('Starry')) # should print S*t*a*r*r*y ``` ### `hasDoubleVowel` Write a predicate called `hasDoubleVowel` that takes a word as its only parameter, and returns `True` if the word has two of the same vowel in a row, or `False` if it doesn't. For example, it should return `True` for `eel` and `pool` but `False` for `feather` and `tell` ('feather' has two vowels in a row, but they're not the same vowel; 'tell' has two of the same letter in a row, but they're not vowels). - Here's an `isVowel` function that you can use (or, you can write your own): ```py def isVowel(s): """returns True if s is a vowel; false otherwise""" return len(s) == 1 and s.lower() in 'aeiou' ``` - Here are some test calls you can copy into the shell: ```py hasDoubleVowel('eel') # should be True hasDoubleVowel('pool') # should be True hasDoubleVowel('breadroot') # should be True hasDoubleVowel('kelp') # should be False hasDoubleVowel('feather') # should be False hasDoubleVowel('knight') # should be False hasDoubleVowel('oreo') # should be False ```
Feeling lost? Click here for a hint! For this problem, an **index loop** is helpful, since it allows you to refer to a previous or upcoming letter in addition to the letter at the current index within the loop (just subtract or add 1 to the index). Just be careful to either start at index 1 or stop before the normal final index so that you don't try to access a letter before the beginning or after the end of the word.
### `starryRow` and `starrySky` In the box below, you're given a predicate called `starTime` that returns `True` twenty percent of the time and `False` eighty percent of the time.
You can copy/paste the `starTime` predicate below into your file (and add the `import` while you're at it): ```py import random def starTime(): """returns True 20% of the time; False otherwise. Note: random.random() returns a random number between 0 and 1 """ return random.random() > 0.80 ``` Write a fruitful function called `starryRow` that takes one parameter `rowLength`, and returns a string with length `rowLength` that is made up of stars (`*`) and dashes (`-`). When generating the row, you can use the `starTime` predicate so that 20% of the row is stars (on average). For each spot in the row, use the predicate `starTime`: if it returns `True`, add a star to your row, otherwise, add a dash. Here are some sample invocations of `starryRow`: ```py print(starryRow(20)) # might print -*------*--*----**-- print(starryRow(20)) # might print ----*----*---------- print(starryRow(20)) # might print ---***-*--**------*- print(starryRow(20)) # might print *--**----*--**--*--- ``` Now write another fruitful function called `starrySky` that takes two parameters: `width` and `height`. `starrySky` returns a string that contains a 'sky' that has `width` and `height` dimensions and each row is created by a call to `starryRow`. The sky is built by adding each row and a newline character (`\n`) at the end. Here are some sample invocations along with what their output might look like: ```py print(starrySky(10, 10)) *-------*- --*------- ---------- ---**----- **--*--*-- --------*- ---------- -----*---- ------*--- --*------- print(starrySky(5, 50)) ----------------**-----**-*----------*---------*-- *--*----*---*---*-----**------*------------*--*--- -*---*---*--------*----------**--------*---*--*--- ---**----*-**---------**---*-------**--*-------*-- ------**-----------------*--------*-----*---*--*-- print(starrySky(3, 60)) --------*-**-**---*---*---------*--*--**-------------------* ----*---------*--*-*--*-**---*----*--------*---*-*----*----* ----*---------*--*-*--*-**---*----*--------*---*-*----*----* ``` ### `funnyVoice` Write a function called `funnyVoice` that takes in a sentence as a parameter, and **returns** a new sentence, based on the original sentence, with each letter randomly capitalized (with a 50% chance of being capitalized. You will need to use the [`random.randint`](/reference/quickref#randint) and/or [`random.random`](/reference/quickref#random) functions. Use those links to read our quick-reference documentation for a refresher if you need to. Here are the [general python documentation pages for random numbers](https://docs.python.org/3.10/library/random.html) if you're curious about more details. ```py print(funnyVoice("Hello")) # might print: heLlO print(funnyVoice("SpongeBob SquarePants")) # might print: sPOnGeBOB sqUArepANtS ``` ### @include('/labs/lab06/_toc') @stop