Tracing Code Exercise and String Processing


Exercise : Code Trace (aka Playing Computer)

Consider this program:

    user_str = input('Enter a string: ')
    result   = ''

    location = 0
    while location < (len(user_str) - 1):
        if user_str[location] > user_str[location+1]:
            result += user_str[location]
        else:
            result = result * 2
        location += 1

    print(result)

What is the program's output if the user enters edcba?

What is the program's output if the user enters abcde?

What is the program's output if the user enters eadbc?

What happens if we delete the - 1 from the while loop line?



Exercise 2: Find All

Let's start with an easy one...

Write a program that takes a string and a character as input and prints out every position the character appears at in the string.

For example:

    Enter a string: Mississippi
    Enter a character: i
    1
    4
    7
    10

    Enter a string: Mississippi
    Enter a character: s
    2
    3
    5
    6

How about this program?

    index = 0
    while index > len(user_str):
        if user_str[index] == user_char:
            print(index)
        index += 1

This is an example of linear search, one of the most common patterns of collection processing.



Exercise 3: Largest Character

We can think of characters being smaller or larger depending on their position in the alphabet.

Write a program that prints the largest character in a given string.

For example:

    Enter a string: Mark
    r
    Enter a string: Jacobson
    s
    Enter a string: Ghostbusters
    u

We could use a while loops, but this is great for applying a for loop.

    for char in user_str:
        if char > max:
            max = char

    print(max)

This looks a lot like a the running total pattern we have seen so many times already. But in that pattern, we initialize the variable that keeps track of our answer to 0, to a value that lets the loop run correctly on the first pass. Here, that variable is max. What is initial value do we need?

Whatever value it is, it needs to be smaller than any character in the string, so that user_str[0] > max on the first pass.

Here are a few candidates. What do you think?

The first works in Python, but not many other languages.

The second works as long as the user enters at least one character.

What does the third do? It works as long as the user enters at least one lowercase alphabetic character.

What does the fourth do? Like the second, it works as long as the user enters at least one character.

... discuss: context, specification.

This program has the for loop and the fourth initial value for max.

How should we handle the situation where the user enters no characters at all? Perhaps guard the output statement to ensure it prints only if there was at least one characters:

    for char in user_str:
        if char > max:
            max = char

    if len(user_str) > 0:
        print(max)
    else:
        print('... empty string ...')

We see patterns like running total all the time. Even so, in each new case, we need to make adjustments in order to address the details of of the new problem.