Dana Vrajitoru
C151 Multi-User Operating Systems

C151 Lab 12

Due Date: Monday, April 19, 2021.

In this lab we will continue the work started in Lab 11 and see some more advanced string operations.

Ex. 1. String operations

In this first exercise we will import the module string and see some useful functions from it.

Make a copy of the file lab11.py and name it lab12.py. Open this file in the editor of your choice.

a. Importing a module

Copy and paste the function words as a new one (before the main section). Call this function wordsArray.

If at this point you've seen enough of the initial poem, you can replace the 3 lines of text with anything else you want.

Add the following line at the top of the file after the comments:

from string import *

This imports all the functions and constants from the module string in a way that lets us call them without the module name.

Add the following line in the wordsArray function after you define the poem variable:

sentence = split(poem, "\n")

This function will split the string using the new line character as separator and create an array storing the resulting smaller strings.

Print this variable instead of poem and change the function call in the '__main__' section to the new function you've just defined.

b. Adding a loop

Go back to the function wordsArray and add the following instructions after the print statement (inside the function):

for w in sentence:
    print w

Then execute the script again.

Note that the second instruction should be indented by one more tab than the first one. This makes it the body of the for loop.

Add the following instructions in the same function, after closing the loop:

print join(sentence, "\n\n")
pos = find(poem, word2)
print "the second line starts at position", pos, "in the string"

or any other separator ("\n\n") that you think might be interesting for the first one. Execute it to see the result.

The function join takes an array of strings and merges it into one big string, using the second parameter as separator. The function find returns the index of the second parameter inside the first one, assuming that they are both strings. It returns a negative value if the second string is not part of the first.

Ex. 2. Random numbers

In this second exercise we will see some operations using random numbers.

a. Words array

Make another copy of the function wordsand call it wordsRandom. Add another import line after the one for the string, and import the module random with a syntax similar to what we used for the string module above.

This module contains several functions related to pseudo-random number generators.

In the function wordsRandom, replace the separator "\n" in the definition of the variable poem with a simple space. Delete the print statement after that line. Copy the split instruction from the function wordsArray and replace the separator again with a space.

Now we have a single long sentence inside the poem variable.

Add the following lines:

for i in range(len(sentence)):
    sentence[i] = lower(sentence[i])

This converts all the words in the sentence to lowercase characters.

Print the variable sentence. Replace the function call in the '__main__' section with the new one wordsRandom and execute the script.

This should show an array where all the elements are strings.

b. Random words

Add a second loop in this function identical to the first one. On a line before the loop, declare a variable L and store the value of
len(sentence) in it. Replace this function call with this new variable in the loop.

One reason to do this is to avoid having a function call for every iteration of the loop.

As the body of the loop, add the following lines:

j = randint(0, L-1)
print sentence[j],

The comma at the end of the print statement above prevents it from adding a new line after it, but allows the next print statement to continue on the same line instead.

Comment out the printing of the whole sentence before the second loop. Save the file and execute it.

This should print a number of random words from the original sentence, with possible repetition. If you run the script a second time, the result should be different.

c. Printing in sentence case

Add a line before the print statement in the second loop containing the following:

if i==0 :

The goal is to do something different for the first word in the sentence.

Indent the printing statement to be inside this conditional and duplicate it. Add

else:

in between the two printing statements.

We have the conditional structure in place. Let's change what is printed for i equal to 0.

Add a function call to the function capitalize with sentence[j] as parameter (not including the comma) in the first printing statement. Save the file and execute it.

The first word of the sentence should now be capitalized.

If we're going to make this look like a proper sentence, it should end in a period.

Add a print statement where you print just "." by itself, and align it to be inside the body of the function but not inside the for loop nor inside the else. Run the script again a couple of times to see if the result is different.

This should look like a proper sentence now.

Add an example of the execution of the script as a comment at the end of the script.

d. Importing your script

Let's see how the script we just wrote works as a module and how it can be imported by the Python interpreter.

Go back to the terminal and open the Python console (interactive program where you can type Python commands/instructions and see them executed right away) with the command python (like the last week). At the Python prompt, execute the import command:

from lab12 import *

Note that this will import all the functions you have defined without having to use the extension .py in the module name.

Try calling the various functions that you defined in the file directly from the console.

Lab Submission

Upload to Canvas: The file lab12.py.