Home Schedule Guides Study Material

Homework 2: String Info (20 pts)

Let's create a program to answer the following questions about a string:

Print the results of each function call in the main function only. None of the functions that you define below should use the print statement.

You may NOT use the Python len(), find(), rfind(), or lfind() functions.

Setup

  1. Locate your "CS102" folder
  2. Create a folder inside "CS102" called "hw02"
  3. Download hw02.py
  4. Move the "hw02.py" file into your hw02 folder
  5. Open "hw02.py" with Thonny
  6. Submit hw02.py to Moodle when you are finished

Part I: Length (1 pt)

  1. Write a function named length that takes a string as a parameter
  2. Count each character in the string parameter
  3. Return the character count
  4. In the main function call your length function on the string "Hello World"

Part II: Has Character (2 pts)

  1. Write a function called has_character that takes a string and a single character as parameters
  2. Check if the character matches at least one character in the string
    • If the character is found in the string return True
    • If the character was not found in the string return False
  3. In the main function call your has_character to check for the character "d" in "Hello World"

Part III Has Upper (2 pts)

  1. Write a function called has_upper that takes a string as a paramter
  2. Check that each character is between 'A' and 'Z' (the capital letters)
  3. Return True if any letter is capital and False otherwise
  4. In the main function call your has_upper on the string "hello world"

Part IV: Find Vowels (1 pt)

  1. Write a function called find_vowels that takes a string as a parameter
  2. Check each character to see if it is an 'a', 'e', 'i', 'o', 'u' (a vowel)
    • Make sure that it works for both lower and upper case letters
    • If the characer is a vowel, concatenate it to a new string containing all the vowels you have found
      • The original case of the letter should be preserved
      • Example: find_vowels('HI ThEre') should return IEe
  3. Return the string of vowels
  4. In the main function call your find_vowels on the string "Hello World"
  5. Make another function call to find_vowels on the string "COmputErScience"
  6. HINT: Chapter 3.2

Part V: Finding Substring (1 pt)

  1. Write a new function called find_substring that takes two string parameters
  2. Check to see if the second string parameter is present in the first string parameter
    • Example: "foobar" contains the substring "foo"
  3. Return True if the substring is found otherwise, return False
  4. In the main function call your find_substring on the string "Hello World" to find "lo Wo"

Part VI: Is Palindrome (4 pts)

  1. Make a function called is_palindrome that takes a string parameter
  2. A palindrome is a string that is the same when read left to right or right to left
  3. Use what you have learned to determine if the string is a palindrome
    • return True if the string is a palindrome, or False otherwise
  4. In the main function call your is_palindrome on the string "racecar"
  5. Now make your function work if you wanted to be able to ignore spaces
  6. In the main function call your is_palindrome function on at least one of the these examples:
    • "taco cat", "rat star", "evil olive"
  7. HINT: Chapter 3.2

Part VII: Find Character (2 pts)

  1. Write a function called find_character that takes a string and a single character as parameters
  2. Check if the character is in the string
  3. Return the position of the first occurance of that character (its index) in the string
    • If the character does not exist in the string, return -1
  4. In the main function call your find_character to find the character "r" in the string "Hello World"
  5. HINT: Chapter 3.2

Part VIII: Skip (4 pts)

  1. Make a function called skip that takes a string parameter
  2. This function will return a new string made up of every other character in the string starting with the first one.
    • Examples
      • skip('abcde') will return the string ace
  3. HINT: Chapter 3.2
  4. HINT: Use your length function (Part 1)

Part IX: Longest String (3 pts)

  1. Make a function called longest_string that takes a string parameter
  2. Check the length of each word in the string and and return the longest word
    • A word is any sequence of characters separated by a space
  3. In the main function call your longest_string on the string "Computer Science is neat!"
  4. HINT: Chapter 3.3