2018-03-15 HW

posted Mar 15, 2018, 5:51 AM by Konstantinovich Samuel   [ updated Mar 15, 2018, 9:55 AM ]
Look at the following python methods:

** sep and maxsplit are optional parmeters!

str.split([ sep[, maxsplit]])
Return a list of the words of the string  

If the optional argument sep is not provided: the words are separated by arbitrary strings of whitespace characters (space, tab, newline, return, formfeed). 

If the argument sep is present, it specifies a string to be used as the word separator. The returned list will then have one more item than the number of non-overlapping occurrences of the separator in the string. 

If maxsplit is given, at most maxsplit number of splits occur, and the remainder of the string is returned as the final element of the list (thus, the list will have at most maxsplit+1 elements). If maxsplit is not specified or -1, then there is no limit on the number of splits (all possible splits are made).

*** Notes [chars] means that chars is an OPTIONAL parameter!

str.strip([ chars])  ***
Return a copy of the string with leading and trailing characters removed. 
    If chars is omitted whitespace characters are removed. 
    If given (chars must be a string); the characters in the chars will be stripped from the both ends of the string this method is called on.

str.lstrip([chars])   ***
Return a copy of the string with leading characters removed. If chars is omitted or None, whitespace characters are removed. If given and not None, chars must be a string; the characters in the string will be stripped from the beginning of the string this method is called on.

str.rstrip([chars])   ***
Return a copy of the string with trailing characters removed. If chars is omitted or None, whitespace characters are removed. If given and not None, chars must be a string; the characters in the string will be stripped from the end of the string this method is called on.

Goal: Figure out how to turn s into the different results below: 
(use python to mess around with the split/strip functions to make the correct output.

Write the following in your notes:

Given:
s = "This is, a block of text, that has commas"

__________#<FILL IN THE EXPRESSION HERE
#results in: ['This is', ' a block of text', ' that has commas']

__________#<FILL IN THE EXPRESSION HERE
#results in: ', a block of text, that has comma'

__________#<FILL IN THE EXPRESSION HERE
#results in: ', a block of text, that has commas'

WARNING:

When splitting on new lines you may find that some files use different. 
"\n\r"  -> windows
"\r"     -> mac
"\n"    ->linux

CLASSWORK:

Phase 1: (10 minutes)

Steps for the classwork:  (hint: after every step PRINT IT so you know it worked.
a) Read the file. 
b) Split the file into values.
c) Use a for loop on the list to print each value
d) change your loop to convert each string number into a floating point and then find the sum.


1. If you were absent or DIDN'T COMPLETE it: start with yesterday's goal of summing the file attached to the post. 

2. Find the sum of the list of numbers attached to this post. 

When opening a file one of the more useful ways is to read the whole file, and then use split on new lines.
This will give a list of each line as an element , but without the trailing "\n" character. 

Steps for the classwork:  (hint: after every step PRINT IT so you know it worked.
a) Read the file. 
b) Split the file into values.
c) Use a for loop on the list to print each value
d) change your for loop to convert each string number into a floating point and then find the sum.

Phase 2: 

Save the text file (alice-simple.txt)

Open it in idle, and split it on whitespace (no parameters)

Change each word to pig latin. -- You may need to add some extra cases for your pig latin function for this to work.

Now print out the words, don't put a new line after each word!











ċ
alice-simple.txt
(144k)
Konstantinovich Samuel,
Mar 15, 2018, 9:18 AM
ċ
alice.txt
(170k)
Konstantinovich Samuel,
Mar 15, 2018, 6:21 AM
ċ
values-windows.txt
(10k)
Konstantinovich Samuel,
Mar 15, 2018, 5:53 AM
ċ
values.txt
(10k)
Konstantinovich Samuel,
Mar 15, 2018, 5:51 AM
Comments