BYU logo Computer Science

To start this assignment, download this zip file.

The following guide pages cover material needed for this assignment:

Lab 4e — Split and join

Preparation

1 minute

Download the zip file for this lab, located above. This zip file has code that you will use for this assignment. Extract the files and put them in your cs110 directory in a folder called lab4e.

Review

10 minutes

Review this `cats_to_cougars.py” code from the guide on split and join:

def cats_to_cougars(line: str) -> str:
    words = line.split()
    new_words = []
    for word in words:
        if word == 'cat':
            word = 'cougar'
        new_words.append(word)
    return ' '.join(new_words)


def main():
    lines = ["BYU's mascot is cool, like a cat or something.",
             "Just like a cat can, he can catch stuff."]
    for line in lines:
        print(cats_to_cougars(line))


if __name__ == '__main__':
    main()

Discuss with the TA:

  • How does cats_to_cougars() work? Be sure everyone understands every line of this code.
  • What does split() do in this code?
  • What does join() do in this code?
  • What changes would we make to use replace()?
  • Would using replace() change the functionality?
  • Could we use string concatenation instead of a list?
  • Note that split() is not needed to iterate for each character in a string, or for each string in a list. split() is used to convert a string into a list of tokens.

Cats and dogs

20 minutes

Write a program that:

  • reads lines from input
  • changes the lines so that
    • words containing ‘cat’ are surrounded with a cat emoji
    • words containing ‘dog’ are surrounded with a dog emoji
  • prints out the new lines

You can find starter code in cats_and_dogs.py, which also has comments guiding you through the exerise.

When you run the finished program, you should see something like this:

python cats_and_dogs.py
> I like cats and dogs.
I like 🐈cats🐈 and 🐕dogs.🐕
> You should doggedly concatenate your strings!
You should 🐕doggedly🐕 🐈concatenate🐈 your strings!
> There is peace today -- dogs are sleeping with cats.
There is peace today -- 🐕dogs🐕 are sleeping with 🐈cats.🐈
>

This program has two pieces:

  • The main() function handles the input loop. You should recognize this from Unit 3. Review the guide on input loops as needed.

  • The do_subs() function takes the line and changes it as needed. This follows a new pattern, where you:

    • split() the line into a list of words
    • change some of the words
    • accumulate original and changed words in a new list
    • convert the list of words back into a string using join()

You can review the code and guide links above to see how to do this.

Discuss with the TA:

  • How do you modify a word to have something before and after it?
  • Share a solution and see if anyone did it differently.

Pet Population

20 minutes

Mike breeds pets and runs a pet store, but he hasn’t been receiving many customers lately. Out of curiosity, he wonders how his animal population will change over time if no one buys any pets.

Write a program to simulate a pet population over time. This program will take two arguments:

  • the input file
  • the output file

The input file looks like this, and shows how many animals there are:

animal,babies,juveniles,adults
bird,0,0,13
fish,19,0,0
rabbit,0,12,0
dog,7,3,2
cat,2,5,1

Notice how the first line is a header.

We have provided a main() function.

  • First, it reads the lines of the input file.
  • It calls the time_passes_for_store() once, which changes the lines as if one year had passed.
  • It writes the new lines to the first output file.
  • It calls the time_passes_for_store() function again, as if second year had passed.
  • It writes the new lines to the second output file.

Follow these rules to change each line:

  • If the first word in the column is animal, keep the whole line as it is.
  • Babies become juveniles
  • Juveniles become adults
  • The new number of babies is equal to twice the previous number of adults.

For example if the line looks like this:

dog,3,4,7\n

You should change it to this:

dog,14,3,4\n

You can call the program like this:

python pet_population.py pets.txt year_one.txt year_two.txt

We have provided code in pet_population.py that decomposes the problem and has some comments to guide you. There are two main parts:

  • time_passes_for_store should go through all the lines and call a function to do the doubling. It returns the new lines.

  • change_pet_population takes just one line and does the modifications needed to swap the order and double the new babies quantities in that line.

Discuss with the TA:

  • How do you split each line of a csv file?
  • What are two different ways to access a specific item in a list?
  • Share a solution and see if anyone did it differently.

Grading

To finish this lab and receive a grade, take the canvas quiz.

Solution

We are providing a solution so you can check your work. Please look at this after you complete the assignment. 😊