2018-03-16 HW

posted Mar 16, 2018, 5:52 AM by Konstantinovich Samuel   [ updated Mar 16, 2018, 6:26 AM ]
Goal Final pig latin lab:

Nice to know stuff:
punctuation can be identified easily using a string variable:

You must import string first! 
Afterwards you can use  string.punctuation

>>> import string
>>> string.punctuation
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'

The basic idea of breaking up a file:

f = open("filename","r")
text = f.read()
data = text.split()  # or text.split("SEPARATOR")
for i in range(len(data)):
   #process data[i]

#ALTERNATE LOOP:
for line in data:
   #process line



Classwork:
2. Work on your Alice Pig Latin Lab:

Download Alice-simple.txt from yesterday's post. 

Part 1 easy: (required)
-Read the entire file using read() 
-Split it up into words using split() with no parameters. This avoids many issues.
- Test your code by printing the first 100 words of the list

Part 2 easy: (required)

a) Test first: By converting words and printing them out you can see if your function is working.
- Test your code by using a loop to test your pig latin on each word. Print each word that is converted.
Use your pigLatinBest function. (DO NOT USE PIGLATIN SENTENCE!)
Make sure your pig latin function does not crash. It is OK if some words do not turn out OK!

b) You may need to tweak your pig latin function to handle strings that crash. 
Empty strings and non-alpha strings should return the original string. 

c) Modify the code :
-Do not print as you convert
-Convert each word in the list to pig latin inside the list.
- Test your code by printing the first 100 words of the list.

#Remember you can replace list elements
e.g.
d = [1,2,3]     # here is a list.
d[0] = f(d[0])  # run a function on d[0] and store the result in d[0]



Part 3 moderate: 
You should now look at making your pig latin function work better.
Write more functions to process your text better:

a)- Any punctuation on the outside of the word should stay on the outside. (required)
This can be 1 character on each side to start like quotes or parenthesis.
This punctuation removal should happen before you check if the word is-alpha for your pig latin. In other words, you handle punctuation BEFORE you call piglatin.

- Test your code on words you know have punctuation, then test it on the book.

b)- Any word with a hyphen should have each HALF of the word processed separately then glued back together. (suggested)
hint: split the word, then pig latin each part, then glue together with a hyphen.
This hypen handling should happen before you check if the word is-alpha for your pig latin. In other words, you handle hyphens AND punctuation BEFORE you call piglatin.

Part 4:
Join your list and print it out!

Part 5: (optional)
Modify your code such that you split the book on new lines.  Try split("\r\n")
Now convert and print each line the same way you handled the book before.
This will retain most of the original formatting of the book and look much nicer. 
Comments