Notes and Assignments

2017-04-28 CSV to HTML - HW

posted Apr 28, 2017, 6:55 AM by Samuel Konstantinovich   [ updated Apr 28, 2017, 8:03 AM ]

Your python programs can output HTML.

We can write a python program that prints a valid (includes proper HTML skeleton) blank website (the body is empty).

TITLE = "INSERT TITLE HERE"
BODY = "Something here"

print """<!DOCTYPE html><html>
<html>
<head><title>"""+TITLE +"""</title></head>
<body>

""" +  BODY + """

</body></html>"""

Notice that you can use variables in your HTML string that make it easy to replace segments of your website.



CLASSWORK 1:
Write a python function that converts a 2D list, into an html table. 
L = ['a','b','c','d'], [1,2,3,12], ['fa','bu','lo','us'] ]
toHTMLTable( L ) 
Would return the string:

'''<table>
<tr>
   <th>a</th> <th>b</th> <th>c</th> <th>d</th> 
</tr>
<tr>
   <td>1</td> <td>2</td> <td>3</td> <td>12</td> 
</tr>
<tr>
   <td>fa</td> <td>bu</td> <td>lo</td> <td>us</td> 
</tr>
</table>'''

BEFORE WE CODE:
-If you could ask for a built in function to help you with this task , what would that function be?
-Is there a repeated non-trivial task in this problem that would be best put into a helper function?

NOW:
a) Write the helper function first
b) Write the toHTMLTable() function using that helper method.



Goal 2 - Homework due Tuesday May 2nd 8am. 


2010 SAT results:
https://data.cityofnewyork.us/Education/SAT-College-Board-2010-School-Level-Results/zt9s-n5aj/data
2012 SAT results:
https://data.cityofnewyork.us/Education/SAT-Results/f9bf-2cp4/data

1 Export these as a CSV. 
2 Read the data into python. Don't forget to include your header on this assignment!
3 Split it up appropriately.
4 Add a new column! "Total Score", and calculate the total score for each row of the data.
5 Convert the to an HTML table.
6 Output a complete website with your name on top and the SAT stats in an HTML table. Do this to both CSV files and output the text to SAT2010.html, SAT2012.html.
7. Place the html files into the folder:  ~/public_html/02SAT/ 
( ~ means your home directory)

2017-04-27 CSV

posted Apr 27, 2017, 7:16 AM by Samuel Konstantinovich   [ updated Apr 27, 2017, 8:31 AM ]

More CSV file parsing!

Attached to this post are two files, one for linux, one for windows.


From Yesterday: 
Convert a csv string into a list of lists:
text =  '''a,b,c
d,e,f
1,2,3'''

1. Break it up line by line (split on new lines)
2. replace each line with a list (split on commas)

These 2 steps are absolutely critical. They are common, and should not take very long. If you dont remember study the following:

Lists = text.split("\n")
i = 0
while i < len(Lists):
  Lists[i] = Lists[i].split(",")
  i+=1

This is not a very long block of code, but if you don't understand a  part of it, then it might take you a long time to replicate.
altrernate loop:

Lists = text.split("\n")
for i in range(len(Lists)):
  Lists[i] = Lists[i].split(",")


Classwork (finish at home!)

Look at the data files that are included on the post (at the bottom), which columns are numerical?

We have:
first,last,age,height,score

Your goals:
1. Calculate the total height of all people in the list. (sum a column)
2. Calculate the average age. (average a column)
3. Calculate which person has the highest score (find which row) Then print out that person's information.


How do we convert the data file into some structure in python that would be easy to work with?

"""first_name,last_name,age(years),height(inches),score
Carin,Marritt,82,46.6,6108
Orelie,Josifovitz,8,48.8,1936
Erena,Cottham,80,75.5,5163
..."""

Hints: 
1. After splitting the data, take a small (3-4 line) slice of the list to work with. 
2. Make a function to calculate the sum of a column in a 2d list. Test it on some lists that you make before you test it on the real data.

Try to make a function to find the sum of the nth column of this list:
[[1,2,3,4,5],
[8,8,2,2,2],
[4,3,2,1,1],
[23,44,11,11,1]]

or, leave them as strings:
[['1','2','3','4','5'],
['8','8','2','2','2'],
['4','3','2','1','1']]

sumCol ( ListOfLists, n)  

This will make the whole lab much easier. 

2017-04-26 CSV Files

posted Apr 26, 2017, 6:47 AM by Samuel Konstantinovich

given the string:
data = """1,2,3,4,5
6,7,8,9,10
12,0,-20,5
1,2,3,4,99"""
#remember three quotes will include newlines as \n, using one set of quotes will not. 

1. How would you convert that into a list of strings:  (newlines are just for ease of reading)
['1,2,3,4,5',
'6,7,8,9,10',
'12,0,-20,5',
'1,2,3,4,99']

2. How would you convert that into a list of lists of NUMBERS:(newlines are just for ease of reading)
[[1,2,3,4,5],
[6,7,8,9,10],
[12,0,-20,5],
[1,2,3,4,99]]

3. What if the 1st line of the data was a list of words:
data = """ID,age,height,width,score
1,2,3,4,5
6,7,8,9,10
12,0,-20,5
1,2,3,4,99"""

How can you make a special case for the first line such that you get the list:
[['ID','age','height','width','score'],
[1,2,3,4,5],
[6,7,8,9,10],
[12,0,-20,5],
[1,2,3,4,99]]

CSV Files are: Comma Separated Values
Some examples:

id,first_name,last_name,email,ip_address
1,Pablo,Gowlett,pgowlett0@google.com.au,102.186.140.76
2,Belvia,Kilbee,bkilbee1@cornell.edu,194.163.255.56
3,Hale,Brandli,hbrandli2@earthlink.net,249.248.161.249
4,Deonne,Horbath,dhorbath3@arizona.edu,68.48.203.96
5,Miles,Seery,mseery4@webmd.com,123.8.74.130
6,Cordi,Fitter,cfitter5@shinystat.com,18.174.34.11
7,Eran,Parkeson,eparkeson6@smh.com.au,128.206.255.200
8,Victor,Orr,vorr7@bbc.co.uk,16.103.244.100

CreditCardNumber,balance,DueDate
5610484586073360,$18564.57,5/25/2017
3553760169678731,$2641.36,1/16/2018
3538164882766651,$7930.95,7/3/2017
50180576168386114,$16275.92,3/12/2018
5108758453108683,$5801.57,1/16/2018
4917542453911013,$17801.02,4/9/2018
6304113713676600,$6276.45,9/20/2017
5002352103560175,$14832.72,9/4/2017

Real data can be found here:

2017-04-25 Files

posted Apr 25, 2017, 6:13 AM by Samuel Konstantinovich

Attached are two files with the same contents. The difference is that they are formatted for windows or linux/mac respectively.

DO NOT open/edit the files! It will potentially change the formatting.


Python can open a file using


Make a new file data.txt and paste in the following:

___________________copy everything after this line------------------
This is a text file.
There are things inside of it!
20 30 40 50
30.4 1.0 3.33 0.6668
___________________copy everything before this line------------------

in the same directory make a python file: loadfile.py

___________________copy everything after this line------------------
#Paste in this code, then test the 3 different read commands.
file = open("data.txt","r")   # r means read mode

#UNCOMMENT ONE of these:
#text = file.read()
#text = file.readlines()
#text = file.readline()
print text
___________________copy everything before this line------------------

In your notes:
Write a description of  what each of the read commands does. 

Now in python:
Use readlines, and you will notice that 2 of the list elements has a string of numbers.

Write some code to split those lines, and try to add the values together.
"20 30 40 50" 

Put that string (from your list of strings) in another variable to make it easier to play around with. 
It can be split up using either : split()   or split(" ")  

Your goal:
Try to add up the values. 

How do you convert a string to an int or floating point number?

Your final goal:
Download the text file attached to this post.
Find the sum of all of the values.


Extra goal:
How would you find the sum of all of these numbers if they were in a text file?
1,2,3,4,5
6,7,8,9,10
12,0,-20,5
1,2,3,4,99




2017-04-24 Pair Programming

posted Apr 24, 2017, 5:35 AM by Samuel Konstantinovich   [ updated Apr 24, 2017, 10:34 AM ]

Classwork: This is meant to be started in class and finished at home, you should have it working by Wednesday morning. (You have 2 dojo days and 2 nights to make it work)

Homework: Submit these 3 questions on CodingBat.


Refresh your memory on python lists here:

PAIR PROGRAMMING! One keyboard/computer, one of you drives the problem solving, the other is typing. Every 8 minutes swap! 


note: that these functions do not return copies! They change the list. If you print the functions the output will be 'None'.

1. Write a function:   removeValuesFromXtoYList(L,x,y)  that will modify the list such that any values n in the list will, such that x < n < y, will be removed. Assume x<=y. (You may use the built in methods to take out the value, as that is a small part of this problem. Read the list method page to help out.)

bigHint: if you remove the first element, does the list have a first element now? What is it? Where did it come from?

Examples:
L=[ 1, 3, -5, 10, -2, 0, -6.0, 0 , 1]
removeValuesFromXtoYList(L,-3,3)  
print L # would print [ 3, -5, 10, -6.0]

L=[ 1, 3, -5, 10, -2, 0, 2, 2, 3, -6.0]
removeValuesFromXtoYList(L,0,10)  
print L # would print [ -5, 10, -2, 0, -6.0]

2. Write a function moveNegativeToEnd(L) that takes a list as a parameter. It moves all negative values to the end of the list. All non-negative values stay in the same relative order, and all negative values stay in the same relative order. The precondition is that all values in L are real numbers. You can use any list methods you find, as well as slices. I strongly urge you to look at the methods! 

BIG hint: If you get an infinite loop, what happens when you test it on [-1, -2] ? Trace through your code and see.

Examples:
x=[ 1, 3, -5, 10, -2, 0, -6.0]
moveNegativeToEnd(x)  
print x # would print [ 1, 3, 10, 0, -5, -2, -6.0]

y=[0, 1 , -3, 4.5]
moveNegativeToEnd(y) 
print y #would print out:  [ 0, 1, 4.5, -3]

3. Write a function reverseWordsWithCapitals(L) that takes a list of strings. If any of the letters in a string are capital letters, that string should be replaced with the reversed string. Before you write this function, think about a good way to break it up. Strings with no capital letters are left alone. The order of the strings should not change. 
Example:
x = ['fish','Hey!','oOps','this is Sparta!']
reverseWordsWithCapitals( x ) 
print x # would print ['fish','!yeH','spOo','!atrapS si siht']

2017-04-20 HW

posted Apr 20, 2017, 6:20 AM by Samuel Konstantinovich

External Access to Sites:
http://clyde.stuy.edu/~USERNAME/

Internal Access to Sites:
http://homer.stuy.edu/~USERNAME/
http://lisa.stuy.edu/~USERNAME/
http://bart.stuy.edu/~USERNAME/

Windows:
WinSCP

Homework:
Everyone must fill this out:
https://goo.gl/forms/gB9fKM5sJd2bK8Ll2

2017-04-19

posted Apr 19, 2017, 7:55 AM by Samuel Konstantinovich

Terminal Commands:
cd ..
cd directory_name
mkdir

Create a public_html directory in your home directory.
Test this by connecting to  homer.stuy.edu/~YOUR_USER_NAME
e.g.
http://homer.stuy.edu/~kchen999/

Create a 01homepage directory inside that directory.
place your html/css files in the homepage directory.

2017-04-07 HW

posted Apr 6, 2017, 7:54 PM by Samuel Konstantinovich

Codingbat problems:
ALL of the old problems should be done by the end of the break. 
However: Keep in mind the median score is completing them all, so your grade does get affected if you start to miss more than one or two.

New Codingbat problems: These should be done by the Friday after the break. You should have enough time in class to complete most of them.
Processing text:
 pigLatinSimple 
 pigLatin 
 pigLatinBest 
 capitalizeSentence   (hint, split and a built-in string function can help you with this)
 pigLatinMultiple   (hint, split and your pig latin function from before can help you with this)

2017-04-05 Break Assignment

posted Apr 5, 2017, 6:57 AM by Samuel Konstantinovich   [ updated Apr 5, 2017, 8:02 AM ]

Break assignment Counts as a quiz.

Due Thurs after break 
Make an "about me" webpage
-Include basic "public friendly" information about you and things you like. No sensitive information. *discussion point*
-Use CSS to style the page and show off your creativity and technical skills.

Technical Requirements:
-There should be 3-4 paragraphs about you, and at least 2 different lists of things. Writing the content should be super easy, just write about stuff you like and things -Format the information using the HTML tags you have learned in a sensible way.
-Include images
-Do NOT include sounds
-Please make it a single page, not several linked together!
you find interesting. 
-Most of the style should be in the style tag, or in an external css file, NOT in individual tags.
-Demonstrate you understand how to research and apply style to your website

Asthetic requirements:
-Pick a color theme that makes sense, not random colors. https://coolors.co/ can help you.
-Size your images to make sense in a browser window.

Optionally: You can ask to be judged and I will pick out the best web pages!



Goal: CSS is your friend
css file:
body {
background-color: black;
color: blanchedalmond;
}
h1{
    color:aquamarine;
}
p {
    font-size: 18px;
color:aqua;
    font-family: cursive;
}
table {
    color: yellow;
    font-family:monospace;
    font-size: 32px;
}

HTML FILE:

<!DOCTYPE html>
<html>
    <head>
        <title>Wow!</title>
        <!--
        <link rel="stylesheet" href="stylesheet.css">
            -->
        <!--
        <style>
            body {
                background-color: tan;
            }
            p{
                font-family: sans-serif;
                color: black;
                
            }
            
            h1{
                font-family: sans-serif;
                color: darkgoldenrod;
            }
            
            table,th,td{
                font-size: 140%;
                font-family: monospace;
                border: 2px solid black;
                border-collapse: collapse;
                background-color: antiquewhite;
                color: saddlebrown;
            }
            th{
                color: chocolate;
            }
        </style> 
        -->
        
    </head>
    <body>
        <h1>B - Not a visa....</h1>
        <center>This is not in a paragraph.</center>
        
        <p>Bacon ipsum dolor amet tail shankle hamburger, pork loin kielbasa leberkas sirloin flank doner pork corned beef ham hock cow filet mignon pancetta. Salami picanha alcatra pork. Short loin chuck turkey kevin pancetta.</p>
        
        Also not a paragraph... default stuff here.
        <table>
        <tr>
            <th>A</td>
            <th>Big</td>
            <th>Fish</td>
        </tr>
        <tr>
            <td>aadfdXdsf</td>
            <td><img width="100" src="https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png"></td>
            <td>Xdfasdfsdf</td>
        </tr>
        <tr>
            <td>X</td>
            <td>Xasdfsf</td>
            <td>X</td>
        </tr>
        </table>
        <h1>WHOA! HTML</h1>
        <h3>smaller header</h3>
        
        <p>Frankfurter pastrami ball tip, landjaeger bresaola ground round burgdoggen. Shankle pig shoulder ground round strip steak hamburger spare ribs brisket tri-tip boudin ribeye bresaola kielbasa frankfurter ham.</p>
    </body>
</html>

2017-04-04

posted Apr 4, 2017, 7:26 AM by Samuel Konstantinovich   [ updated Apr 4, 2017, 7:31 AM ]

Small Group activity: 
New seats for today! 
1st, 3rd, and 5th student in each row, swap with the person in FRONT of you (They are behind your computer)


Common issues:

The value of the expression 
"a"+"b" 
is:  "ab"  (with quotes)

The output of the statement:  
print "a"+"b" 
is:   ab    (without quotes)

print x    will print the value inside variable x.

print "x"  will print an x on the output. 

Classwork: You are collaborating on a fake quiz! You may use your reference sheet, but NOT the computer.
DIRECTIONS:
1. Work with 3-4 of your neighbors. 
2. Do not run the code! Do not run the code!
3. Agree with each other on the answers and EVERYONE write it down. (include the question)
4. After completing a section, compare that ENTIRE section with 2 neighboring groups.
5. Move on to the next section only after 2 other groups agree with you.

By the end of this everyone should have the same answers, and should be convinced that they are correct. 


EVALUATE EACH OF THESE: (The actual problems)
#I starting list and strings
s = "two fish?"
L = ["two","fish?"]
L2 = ["one","fish","two","fishs?"]

#II len
print "---LEN:---"
print len(s)
print len(L)
print len(L2)

#III indexing
print "---INDEX:---"
print s[1]
print L[1]
print L2[1]

#IV slicing
print "---SLICES:---"
print L2[1:3]
print L2[2:3]
print L2[:2]
print L2[::2]
print L2[1::2]

#V split
print "---SPLIT:---"
print "this is a test".split()
print "this is a test".split("s ")

#VI join
print "---JOIN:---"
print "wh".join(["o"," are"," you"])
print "an".join(["b","n","a"])

#VII split and join
print "---SPLIT+JOIN:---"
print "!".join("the only way to go?".split())
print "".join("bannana".split("an"))


END OF ACTIVITY!


#New Python Stuff (not on quiz yet)
#range
print "---range:---"
print range(5)
print range(2,5)
print range(2,10,2)
#in
print "---in:---"
print "a" in "fish"
print "i" in "fish"
print 5 in [1,2,3]
print 5 in [3,4,5]

1-10 of 43