CS 240: Data Structures and Algorithms
James Madison University, Fall 2012

In Class Activity #2: Lists, Strings and Control (and Possibly Dictionaries)

Objectives

The goal of today's activity is to get comfortable working with basic features of the Python programming language including Python lists, strings, dictionaries, and selection constructs.

Resources

Everything you need to know to complete today's activities is covered in appendix A of our textbook. There are also several relevant sections from the official Python tutorial:

You may also want to take a look at the complete list of string methods.

Exercises

  1. Removing Vowels
    • Create a new .py file to contain today's work. Include your name and today's date in a comment at the top of the file.
    • Create a new function with the signature removeVowels(string). This function should take a string as an argument, and return a copy of that string with all of the vowels (a, e, i, o and u) removed.

      Hint:
      • Remember that the "+" symbol is used for string concatenation. I can create the string "Hi" as follows:
        		    greeting = ""
        		    greeting = greeting + "H"
        		    greeting = greeting + "i"
        		  
    • Add a main function to your program, and use it to test removeVowels.
  2. Removing Short Words
    • Write a function with the signature removeShortWords(string, minLength). This function should take a string and a length as arguments, and should return a new string in which all of the words shorter than the specified length have been removed. For example,
      		removeShortWords("I am happy to meet you", 4)
      	      
      should return the string "happy meet". (Don't worry about handling punctuation correctly. Punctuation marks may be considered part of the word that they follow.)

      Hints:
      • Python strings have a split method that splits the string into a list of words. For example, after the following code segment:
         
        		    greeting = "hello there"
        		    words = greeting.split()
        		  
        words will contain the list ['hello', 'there'].
    • Test your new function from main. I suggest testing this function in conjunction with removeVowels as follows:
      		noShort = removeShortWords("I am happy to meet you", 4)
      		noVowels = removeVowels(noShort)
      		print(noVowels)
      	      
      The result should be "hppy mt".
  3. Reversing a String
    • Create a new function with the signature reverseString(string). This function should take a string as an argument, and return a new string in which the order of the individual characters has been reversed.

      Hints:
      • There are at least two reasonable ways to solve this problem: 1) You could iterate forward through the existing string, and append each subsequent character to the beginning of a new string, or 2) You could iterate backward through the existing string and append each subsequent character to the end of a new string. Iterating backward can be accomplished either by passing a negative third argument to the range function or by using negative string indexing.
  4. Removing Repeated Words (If you have time.)
    • Write a function with the signature removeInfrequent(string, minCount). This function should take a string as an argument, and should return a new string in which all words that occur fewer than minCount times have been removed. For example,
      		removeInfrequent("This is great really really really great", 2)
      	      
      should return the string "great really really really great ".

      Hints:
      • The best way to accomplish this is to iterate through the list of words twice. The first time though, you can use a Python dict to create a count for all the words. The second time through, you can build the new string from all of the words that have enough occurrences.

Finishing Up

There is nothing to hand in for this assignment. Make sure that you save a copy of your code, either on a thumb drive, the N: drive, or by e-mailing it to yourself. If you worked with a partner, make sure both of you get a copy.