CS 240: Data Structures and Algorithms
James Madison University, Spring 2013

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 Letters
    • 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 removeLetter(string, letter). This function should take a string and letter as arguments, and return a copy of that string with every instance of the indicated letter removed. For example, removeLetter("Hello there!", "e"), should return the string "Hllo thr!".

      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"
        		  
        Or, equivalently:
        		    greeting = ""
        		    greeting += "H"
        		    greeting += "i"
        		  
    • Add a main function to your program, and use it to test removeLetter.
  2. Find and Replace
    • Write a function with the signature replaceWord(sentence, original, new). This function will take three strings as arguments. The first will contain a sentence, the second will contain a word to replace, and the third will contain the replacement word. The return value should be a copy of sentence with every instance of original replaced with an instance of new. For example,
      		replaceWord("I am happy to meet you!", "happy", "angry")
      	      
      should return the string "I am angry to meet you! ". Don't worry about handling punctuation correctly. Punctuation marks may be considered part of the word that they follow. (Note that Python strings have a built-in replace method that can accomplish this in a single line of code. Don't use it! Solve the problem by iterating over the words in the string.)

      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 removeLetter as follows:
      		angry = replaceWord("I am happy to meet you!", "happy", "angry")
      		noY = removeLetter(angry, "y")
      		print(noY)
      	      
      The result should be "I am angr to meet ou!".
  3. Reversing a Sentence
    • Create a new function with the signature reverseSentence(string). This function should take a string as an argument, and return a new string in which the order of the individual words has been reversed.

      Hints:
      • There are at least two reasonable ways to solve this problem: 1) You could iterate forward through the existing words, and append each subsequent word to the beginning of a new string, or 2) You could iterate backward through the existing words and append each subsequent word 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 list indexing.
  4. Removing Repeated Words (If you have time.)
    • Write a function with the signature removeFrequent(string, maxCount). This function should take a string as an argument, and should return a new string in which all words that occur more than maxCount times have been removed. For example,
      		removeInfrequent("This is great really really really great", 2)
      	      
      should return the string "This is great 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 few enough occurrences.

Finishing Up

There is nothing to hand in for this assignment. Make sure that you save a copy of your code. If you worked with a partner, make sure both of you get a copy.