Lab 4: Strings
Goals and Objectives
The main goal this lab is for you to get a more practice with Pythons's strings, loops, and comparisons.
Task
You will write several functions that perform some simple computations on strings.
Background
Strings are used to store textual information. Nearly all text you see on a computer uses strings. Strings are sequences of Unicode characters. A font defines a visual presentation (a set of glyphs) for the individual characters. Not all fonts contain glyphs for all Unicode characters.
Introductions
Introduce yourself to your lab partner(s). Tell your lab partner(s) about your favorite fall activities.
Download the lab
Download the source file Lab04.py. It contains the stubs of Python functions. Open it in the online Python environment.
Getting Started
In this lab, you will practice writing functions that process strings.
You should start off with s1 driving and s2 navigating.
Complete the functions in Lab04.py
. You should change driver/navigator roles
after each method is completed.
# Lab 4
# Your names and email addresses here
import string
import random
# returns a copy of the input string s
# study this copy function and understand why it works!
# Talk to your instructor if you are confused about any of it.
# These are the basis of several later functions
def copy(s):
t = ""
for c in s:
t = t + c
return t
def copy2(s):
t = ""
for i in range(len(s)):
t = t + s[i]
return t
def copy3(s):
t = ""
i = 0
while i < len(s):
t = t + s[i]
i = i + 1
return t
# Create a function that takes a single string parameter
# and returns a new string that is
# the same as the original string except all vowels are
# replaced with a '*' character.
# Hint: use the repeated calls to the replace method of string.
# do not use any string functions other than len()
def stars(s):
return None
# Create a function removePunctuation that takes a single
# string parameter and returns a string containing just
# letters (no spaces, digits, or other special characters).
def removePunctuation(s):
return None
# Create a function reverse that takes a single string parameter
# and returns a string that contains the orginal string's characters
# in reverse order.
def reverse(s):
return None
# phrase same forward of backward, excluding case,
# whitespace, and punctuation.
# use removePunctuation
def palindrome(s):
return None
# Create a function that takes a single string parameter
# and returns true if all the letters are lowercase
# Hint: use a loop, the in operator, string.ascii_letters,
# and string.ascii_lowercase
# do not use any string functions other than len()
def allLower(s):
return None
# Create a function that takes a single string parameter
# and returns true if it contains at least one uppercase letter
# and at least one lowercase letter
# Hint: use a loop, the in operator, string.ascii_letters,
# string.ascii_lowercase, and string.ascii_uppercase
# do not use any string functions
def hasMixedCase(s):
return None
# Create a function that takes a single string parameter
# and returns true if it might be considered a strong
# password:
# 1. it is at least 8 characters long
# 2. it contains at least one upper and one lower case letter
# 3. it contains at least one digit (string.digits)
# 4. At least one punctuation character (string.punctuation)
def strongPassword(s):
return None
# Create a function that takes a single string parameter
# and returns a new string that is 5 characters long
# with each character in the new string coming from a
# randomly generated position in the original string.
def randomChar(s):
return None
# Create a function that takes a single string parameter and
# returns a new string that is 5 characters long with each
# character in the new string coming from a randomly generated
# position in the original string.
def rand5(s):
return None
# returns binary representation of n
# Use the + operator for strings
# use /2 and %2
# 6 (ten) = 110 (two)
def bits(n):
return None
# returns the scrabble score of the string, nonletters have value 0
# other letters have the values indicated below
# ignore case
# 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
# 1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10
def scrabbleScore(s):
return None
# Don't change main
def main():
print("items of note")
print(string.ascii_letters)
print(string.ascii_lowercase)
print(string.ascii_uppercase)
print(string.digits)
print(string.punctuation)
print()
print("copy:")
print(copy("computer"))
print("#"+copy("")+"#")
print(copy2("2: computer"))
print("#"+copy2("")+"#")
print(copy3("3: computer"))
print("#"+copy3("")+"#")
print("stars:")
print(stars("shot")) # sh*t
print(stars("FORT")) # F*RT
print(stars("pass")) # p*ss
print(stars("sequoia")) # s*q****
print(stars("Why!!!")) # Why!!! - don't change y
print("removePunctuation:")
print(removePunctuation("speed of art!")) # not at the pool!
print(removePunctuation("childrens wear!")) # uh-oh
print("reverse:")
print(reverse("live")) # evil
print(reverse("reknits")) # stinker
print(reverse("desserts")) # stressed
print(reverse("12345")) # 54321
print("palindrome:")
print(palindrome("bob")) # true
print(palindrome("dave")) # false
print(palindrome("A man, a plan, a canal: Panama.")) # true
print(palindrome("Oozy rat in a sanitary zoo.")) # true
print(palindrome("computer science")) # false
print("allLower:")
print(allLower("")) # true
print(allLower("computer")) # true
print(allLower("cOmPuTeR")) # false
print(allLower("Computer")) # false
print(allLower("computeR")) # false
print("hasMixedCase:")
print(hasMixedCase("")) # false
print(hasMixedCase("computer")) # false
print(hasMixedCase("cOmPuTeR")) # true
print(hasMixedCase("Computer")) # true
print(hasMixedCase("computeR")) # true
print(hasMixedCase("COMPUTER")) # false
print("strongPassword:")
print(strongPassword("hexagon")) # false
print(strongPassword("Hexagon")) # false
print(strongPassword("abcd1234")) # false
print(strongPassword("Abcd1234")) # false
print(strongPassword("ABC-1234")) # false
print(strongPassword("ABC-1234")) # false
print(strongPassword("Triangle123!")) # true
print("randomChar:")
print(randomChar(string.ascii_lowercase)) # a - maybe
print(randomChar(string.ascii_uppercase)) # B - maybe
print(randomChar(string.ascii_letters)) # c - maybe
print(randomChar(string.ascii_letters)) # D - maybe
print(randomChar(string.digits)) # 7 - maybe
print(randomChar("albion")) # n - maybe
print("rand5:")
print(rand5(string.ascii_lowercase)) # bkfst - maybe
print(rand5(string.ascii_uppercase)) # BKFST - maybe
print(rand5(string.ascii_letters)) # BkFsT - maybe
print(rand5(string.digits)) # 13579 - maybe
print(rand5("abcdefghijklmnopqurstuvwxyz")) # lunch - maybe
print(rand5("abcdefghijklmnopqurstuvwxyz")) # super - maybe
print(rand5("abcdefghijklmnopqurstuvwxyz")) # snack - maybe
print(rand5("a")) # aaaaa
print("bits(0) =", bits(0)) # 0
print("bits(1) =", bits(1)) # 1
print("bits(2) =", bits(2)) # 10
print("bits(6) =", bits(6)) # 110
print("bits(10) =", bits(10)) # 1010
print("bits(127) =", bits(127)) # 1111111
print("scrabbleScore:")
print(scrabbleScore("compute")) # 13
print(scrabbleScore("quiz")) # 22
print(scrabbleScore("Pack my box with five dozen liquor jugs.")) # 90
main()
Have Fun!
Final Steps
When done, email you lab partner and instructor your lab (.py file).