CSC 111: Intro to Computer Science through Programming

Lab 4

Optional: no Lab 4 due to Rally Day

The exercises below provide additional practice with some of our recent topics (lists, conditionals, slicing, functions). I recommend doing them, but they are not required and there is no official lab meeting this week.

1) Lists and nested structures

Create a new file called lab4.py. The goal of this part is to create a function that will change the negative numbers of a list to 0 (the idea being that maybe we don't want negative values for some reason). To start, write a function that will loop through the elements of a list, only printing the indices of the negative numbers. An example of testing this function in the shell is shown below:

list

So in this case, the element -1 was at index 3, the -3 was at index 10, and so on. Here is the list so you don't have to type it in:


[12, 5, 6, -1, 17, 2, 15, 19, 18, 14, -3, 7, -4, 16, 0, 4, 8, -5, 13, 1]
Now modify your function so that it changes the negative numbers to 0. After you're finished, this function shouldn't print or return anything, but modify the original list. An example is shown below:

list

Try a few different lists to make sure your function works in general.


2) Slicing lists

In the same file, create a new function called mid_range, that also takes a list as an argument. The goal of this function is to return the sublist between the first and last negative numbers in the list (slightly strange thing to do, but it is good practice with list indexing and slicing). To create this function, first create a list of all the negative indices (print this list to make sure you code is working).

Then, use list indexing to obtain the first and last elements of this list. Finally, use list slicing to obtain the range of numbers between the first and last negative numbers in the original list. An example is shown below:

list

Note that in this example, the "mid range" list starts at the element 17 and goes to the element 8, since -1 was the first negative number, and -5 was the last. Test your function on other lists to make sure it works in general. When you're sure it's working, remove any print statements (usually I comment them out in case I need them later). Then make sure you can store the return value to another variable (here called middle_lst):

list


3) Slicing strings

In this last part, we'll investigate tweets and how to obtain the hashtags within them. In the file lab4_tweet.py there are 5 tweets from President McCartney. The goal is to print out only the hashtag information (assuming all the hashtags appear at the end). One tool we'll need for this part is the built-in function index, which returns the index of the first occurrence of a given element in a list or string. Here are a few examples with a string:

index

In this example, the character "i" first appears at index 3, the character "t" first appears at index 1, etc.

To use this built-in function with tweets, create a new function called get_hashtag(tweet), which takes one parameter, a string representing the tweet. First find the index of the first occurrence of a hashtag ("#"). Then use list slicing to obtain the portion of the string after (and including) that hashtag. Here is an example of what your code should produce when your method is run on each tweet separately:

tweet

Finally, modify your method so that it prints out the (multiple) hashtags one by one, but not including the symbol "#". You should get something like this:

tweet
Feel free to extend your code to count how many times a certain hashtag is observed, or create a list of all observed hashtags without duplicates.