BYU logo Computer Science

To start this assignment, download this zip file.

Lab 6a — Grouping

Reminder, you should work in teams of 2 or 3 when solving the lab problems. Learning to code together is an important part of this class.

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 lab6a.

Indexing Practice

5 minutes

We have given you code in indexing-practice.py that provides some simple practice with list indexing. This program contains a list of fruits:

fruits = ['apple', 'banana', 'cantaloupe', 'date', 'elderberry', 'fig',
          'grapefruit', 'hackberry', 'jackfruit', 'kiwi', 'lime', 'mango']

There are instructions in the code asking you to do the following:

  1. Print the third fruit in the list

  2. Print the last fruit in the list

  3. Print the 5th fruit in the list

You can see the guide on indexing for help.

After you finish and run the program, it should print:

cantaloupe
mango
elderberry

Discuss with the TA

  • How did you write this code? Show a solution and discuss.
  • Is there anything you don’t understand about indexing?

Range Practice

5 minutes

We have given you code in range-practice.py that provides some simple practice with range(). Currently, when you run the program, it prints the following:

Should print YES three times:
YES

Should print YES three times:
YES

Should print YES three times:
NO

However, as indicated, it should print YES three times in each case.

There are instructions in the code asking you to modify the range() statement in the following lines:

# Modify each range so that YES is printed 3 times in each case.
print_list(['YES', 'YES', 'YES', 'NO', 'NO', 'NO'], range(0,1))
print_list(['YES', 'NO', 'NO' ,'YES', 'NO', 'NO', 'YES'], range(0,1))
print_list(['NO', 'YES', 'NO', 'YES', 'NO', 'YES'], range(0,1))

The print_list() function will take your range and print out only the items in the list that are specified by your range.

Remember, range takes three arguments: range(start, stop, step):

  • start — Start counting at this index.
  • stop — Stop counting at stop - 1.
  • step — Count by ones (0, 1, 2, 3) by default if this parameter is not given, otherwise count by the number indicated. For example, if step is 2, then it may count by twos (0, 2, 4).

You can see the guide on range for more details.

After you fix and run the program, it should print:

Should print YES three times:
YES
YES
YES

Should print YES three times:
YES
YES
YES

Should print YES three times:
YES
YES
YES

Discuss with the TA

  • How did you write this code? Show a solution and discuss.
  • Is there anything you don’t understand about range?

Redact

15 minutes

We have a partially-written program in redact.py that will print out the contents of a file but redact certain words. The program takes one argument on the command line:

  • input file

The program comes with a set of words that should be redacted:

replace = ['Dr.', 'Bean', 'Page', 'Zappala']

Each of these words should be replaced with “REDACTED”. When you complete and run the program, it should work like this:

% python redact.py evidence.txt
Our office interviewed REDACTED REDACTED about his recent
purchase of 1,000 hours of ChatGPT time. He claimed
that he was working with REDACTED REDACTED to create a bot
that could work as a CS 110 TA. However, records
recovered from the office of REDACTED REDACTED indicate
that the bot was in the process of learning CS
so well that REDACTED REDACTED was planning to use it to
teach all of his courses. This scheme involved
teaching remotely with an automated video creating
bot, perfectly simulating the likeness of REDACTED Bean.
Furthermore, our office discovered evidence of bribes to REDACTED
REDACTED and REDACTED REDACTED so they would not reveal his plans
to the CS Department.

To complete this code, you only need to write one function, replace_words():

def replace_words(words, replace):
    """
    <words> -- list of words
    <replace> -- a list of words to replace

    Goes through every word in <words>. If it finds a word
    that matches one of the words in <replace>, then it replaces
    that word with "REDACTED".
    """
    # Write code here
    pass

For example, if you are given:

words = 'I gave the money to John'
replace = ['Emma', 'John']
replace_words(words, replace)

Then after you run this function, you should have:

words = 'I gave the money to REDACTED'

See the guide on functions that mutate for help. It has code very similar to this.

Discuss with the TA

  • How did you implement this function? Show a solution and discuss.
  • Make sure everyone understands every line of code we supplied.

Drafting Pokemon

15 minutes

We are really sorry if you don’t like Pokemon, but they are back. :-) This problem will especially help you with the homework.

We have a partially-written program in pokemon-draft.py that is designed to let you pick a set of three Pokemon to be on your team. When you finish the program, it should look like this:

Choose your Pokemon!
Available Pokemon:
(0) bulbasaur
(1) charmander
(2) squirtle
(3) pikachu
(4) jigglypuff
(5) meowth
(6) machop
(7) drowzee
(8) staryu
(9) magikarp
(10) dragonite

Choice: 4
Available Pokemon:
(0) bulbasaur
(1) charmander
(2) squirtle
(3) pikachu
(4) jigglypuff
(5) meowth
(6) machop
(7) drowzee
(8) staryu
(9) magikarp
(10) dragonite

Choice: 10
Available Pokemon:
(0) bulbasaur
(1) charmander
(2) squirtle
(3) pikachu
(4) jigglypuff
(5) meowth
(6) machop
(7) drowzee
(8) staryu
(9) magikarp
(10) dragonite

Choice: 3
Your Pokemon:
(0) jigglypuff
(1) dragonite
(2) pikachu

The program gives you a list of slots to fill:

slots = ['empty' ,'empty', 'empty']

As you choose Pokemon, you will fill up each of these slots. To complete this program, there are three functions to write:

Check if slots are full

This function returns True if any of the slots is empty:

def not_full(slots):
    """
    <slots> -- a list of slots -- either "empty" or a Pokemon
    Returns true if any slot is equal to "empty"
    """
    # Write code here
    pass

Can you remember something we covered from this semester to write this function?

Choose a Pokemon

This function lets you choose Pokemon by entering a number representing its index in the list of available Pokemon:

def get_choice(available):
    """
    <available> -- a list of Pokemon

    Gets a choice of Pokemon (an integer entered with input()).
    Returns the Pokemon in this place on the list.
    """
    # Write code here
    pass

Remember, the program shows you a list of all the Pokemon with numbers to the left. You are supposed to let a person enter a number, and then return the Pokemon in this place in the list. This is a simple list indexing problem.

Add a Pokemon

This function will add a Pokemon in the next empty slot:

def add_pokemon(slots, pokemon):
    """
    <slots> -- a list of slots -- either "empty" or a Pokemon
    <pokemon> -- the name of a Pokemon

    Modifies <slots> so that <pokemon> is in the first slot
    that is equal to "empty".
    """
    # Write code here
    pass

Discuss with the TA

  • How did you implement these functions? Show a solution and discuss.
  • Make sure everyone understands every line of code we supplied.

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. :-)