From 97821c09b953239892bfe65b80c7a8a7207dc66d Mon Sep 17 00:00:00 2001 From: JoeC718 Date: Thu, 12 May 2016 16:18:50 -0400 Subject: [PATCH] added 11&12 --- Assignments/Assignment_11.py | 34 ++++++++++++++++++++++++++++++++++ Assignments/Assignment_12.py | 18 ++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 Assignments/Assignment_11.py create mode 100644 Assignments/Assignment_12.py diff --git a/Assignments/Assignment_11.py b/Assignments/Assignment_11.py new file mode 100644 index 0000000..ae938cd --- /dev/null +++ b/Assignments/Assignment_11.py @@ -0,0 +1,34 @@ +Finding Numbers in a Haystack +In this assignment you will read through and parse a file with text and numbers. You will extract all the numbers in the file and compute the sum of the numbers. +Data Files +We provide two files for this assignment. One is a sample file where we give you the sum for your testing and the other is the actual data you need to process for the assignment. +Sample data: http://python-data.dr-chuck.net/regex_sum_42.txt (There are 87 values with a sum=445822) +Actual data: http://python-data.dr-chuck.net/regex_sum_242232.txt (There are 101 values and the sum ends with 786) + +These links open in a new window. Make sure to save the file into the same folder as you will be writing your Python program. Note: Each student will have a distinct data file for the assignment - so only use your own data file for analysis. +Data Format +The file contains much of the text from the introduction of the textbook except that random numbers are inserted throughout the text. Here is a sample of the output you might see: +Why should you learn to write programs? 7746 +12 1929 8827 +Writing programs (or programming) is a very creative +7 and rewarding activity. You can write programs for +many reasons, ranging from making your living to solving +8837 a difficult data analysis problem to having fun to helping 128 +someone else solve a problem. This book assumes that +everyone needs to know how to program ... + + +The sum for the sample text above is 27486. The numbers can appear anywhere in the line. There can be any number of numbers in each line (including none). +Handling The Data +The basic outline of this problem is to read the file, look for integers using the re.findall(), looking for a regular expression of '[0-9]+' and then converting the extracted strings to integers and summing up the integers. +Turn in Assignent +Enter the sum from the actual data and your Python code below: +Sum: (ends with 786) +Python code: + +Optional: Just for Fun +There are a number of different ways to approach this problem. While we don't recommend trying to write the most compact code possible, it can sometimes be a fun exercise. Here is a a redacted version of two-line version of this program using list comprehension: +import re +print sum( [ ****** *** * in **********('[0-9]+',**************************.read()) ] ) + + diff --git a/Assignments/Assignment_12.py b/Assignments/Assignment_12.py new file mode 100644 index 0000000..de19c3c --- /dev/null +++ b/Assignments/Assignment_12.py @@ -0,0 +1,18 @@ +'''Exploring the HyperText Transport Protocol +You are to retrieve the following document using the HTTP protocol in a way that you can examine the HTTP Response headers. +http://www.pythonlearn.com/code/intro-short.txt +There are three ways that you might retrieve this web page and look at the response headers: +Preferred: Modify the socket1.py program to retrieve the above URL and print out the headers and data. +Open the URL in a web browser with a developer console or FireBug and manually examine the headers that are returned. +Use the telnet program as shown in lecture to retrieve the headers and content. +Enter the header values in each of the fields below and press "Submit". +Last-Modified: + +ETag: + +Content-Length: + +Cache-Control: + +Content-Type: +''' \ No newline at end of file