├── README.md ├── Week-1 └── Assignment6.5.py ├── Week-3 ├── Assignment7.1.py └── Assignment7.2.py ├── Week-4 ├── Assignment8.4.py └── Assignment8.5.py ├── Week-5 └── Assignment9.4.py └── Week-6 └── Assignment10.2.py /README.md: -------------------------------------------------------------------------------- 1 | # Coursera---Python-Data-Structures 2 | Here is All Weeks Assignment for Python Data Structures Course on Coursera 3 | python2.7 Used 4 | -------------------------------------------------------------------------------- /Week-1/Assignment6.5.py: -------------------------------------------------------------------------------- 1 | ''' 2 | 6.5 Write code using find() and string slicing (see section 6.10) to extract the 3 | number at the end of the line below. Convert the extracted value to a floating 4 | point number and print it out. 5 | ''' 6 | text = "X-DSPAM-Confidence: 0.8475"; 7 | 8 | t = text.find('0') 9 | 10 | x=text[t:] 11 | 12 | print float(x) 13 | -------------------------------------------------------------------------------- /Week-3/Assignment7.1.py: -------------------------------------------------------------------------------- 1 | ''' 2 | 7.1 Write a program that prompts for a file name, then opens that file and reads 3 | through the file, and print the contents of the file in upper case. Use the file 4 | words.txt to produce the output below. 5 | You can download the sample data at http://www.pythonlearn.com/code/words.txt 6 | ''' 7 | # Use words.txt as the file name 8 | fname = raw_input("Enter file name: ") 9 | fh = open(fname) 10 | for line in fh: 11 | 12 | l = line.upper() 13 | print l.rstrip() 14 | -------------------------------------------------------------------------------- /Week-3/Assignment7.2.py: -------------------------------------------------------------------------------- 1 | ''' 2 | 7.2 Write a program that prompts for a file name, then opens that file and reads through the file, looking for lines of the form: 3 | X-DSPAM-Confidence: 0.8475 4 | Count these lines and extract the floating point values from each of the lines and 5 | compute the average of those values and produce an output as shown below. Do 6 | not use the sum() function or a variable named sum in your solution. 7 | You can download the sample data at http://www.pythonlearn.com/code/mbox-short.txt when you are testing below enter mbox-short.txt as the file name. 8 | ''' 9 | # Use the file name mbox-short.txt as the file name 10 | fname = raw_input("Enter file name: ") 11 | fh = open(fname) 12 | count = 0 13 | y=0.0 14 | for line in fh: 15 | if not line.startswith("X-DSPAM-Confidence:") : continue 16 | count = count+1 17 | t = line.find('0') 18 | x=float(line[t:]) 19 | y = y + x 20 | avg = y / count 21 | 22 | print "Average spam confidence:",avg 23 | -------------------------------------------------------------------------------- /Week-4/Assignment8.4.py: -------------------------------------------------------------------------------- 1 | ''' 2 | 8.4 Open the file romeo.txt and read it line by line. For each line, split the 3 | line into a list of words using the split() method. The program should build a 4 | list of words. For each word on each line check to see if the word is already 5 | in the list and if not append it to the list. When the program completes, 6 | sort and print the resulting words in alphabetical order. 7 | You can download the sample data at http://www.pythonlearn.com/code/romeo.txt 8 | ''' 9 | fname = raw_input("Enter file name: ") 10 | fh = open(fname) 11 | lst = list() 12 | count=0 13 | for line in fh: 14 | line = line.rstrip() 15 | 16 | for word in line.split(): 17 | 18 | if word not in lst: 19 | 20 | lst.append(word) 21 | 22 | 23 | 24 | 25 | 26 | 27 | lst.sort() 28 | print lst 29 | 30 | -------------------------------------------------------------------------------- /Week-4/Assignment8.5.py: -------------------------------------------------------------------------------- 1 | ''' 2 | 8.5 Open the file mbox-short.txt and read it line by line. When you find a line 3 | that starts with 'From ' like the following line: 4 | From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008 5 | You will parse the From line using split() and print out the second word in the 6 | line (i.e. the entire address of the person who sent the message). Then print 7 | out a count at the end. 8 | Hint: make sure not to include the lines that start with 'From:'. 9 | 10 | You can download the sample data at http://www.pythonlearn.com/code/mbox-short.txt 11 | ''' 12 | fname = raw_input("Enter file name: ") 13 | if len(fname) < 1 : fname = "mbox-short.txt" 14 | 15 | fh = open(fname) 16 | count = 0 17 | for line in fh: 18 | 19 | if not line.startswith('From'):continue 20 | line =line.split() 21 | 22 | if line[0] == 'From': 23 | count = count +1 24 | print line[1] 25 | 26 | print "There were", count, "lines in the file with From as the first word" 27 | -------------------------------------------------------------------------------- /Week-5/Assignment9.4.py: -------------------------------------------------------------------------------- 1 | ''' 2 | 9.4 Write a program to read through the mbox-short.txt and figure out who has 3 | the sent the greatest number of mail messages. The program looks for 'From ' 4 | lines and takes the second word of those lines as the person who sent the mail. 5 | The program creates a Python dictionary that maps the sender's mail address to 6 | a count of the number of times they appear in the file. After the dictionary is 7 | produced, the program reads through the dictionary using a maximum loop 8 | to find the most prolific committer. 9 | ''' 10 | name = raw_input("Enter file:") 11 | if len(name) < 1 : name = "mbox-short.txt" 12 | handle = open(name) 13 | 14 | count = 0 15 | d = dict() 16 | 17 | for line in handle: 18 | 19 | if not line.startswith('From'):continue 20 | 21 | line = line.split() 22 | 23 | if line[0] == 'From': 24 | 25 | line1 = line[1] 26 | 27 | for word in line1.split(): 28 | 29 | if word not in d: 30 | 31 | d[word] = 1 32 | else: 33 | d[word]+=1 34 | 35 | count = count + 1 36 | 37 | maximum = None 38 | k = None 39 | for key,value in d.items(): 40 | 41 | if maximum is None or value>maximum: 42 | maximum = value 43 | k=key 44 | print k,maximum 45 | -------------------------------------------------------------------------------- /Week-6/Assignment10.2.py: -------------------------------------------------------------------------------- 1 | ''' 2 | 10.2 Write a program to read through the mbox-short.txt and figure out 3 | the distribution by hour of the day for each of the messages. 4 | You can pull the hour out from the 'From ' line by finding the time and then 5 | splitting the string a second time using a colon. 6 | From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008 7 | Once you have accumulated the counts for each hour, print out the counts, 8 | sorted by hour as shown below. 9 | ''' 10 | name = raw_input("Enter file:") 11 | if len(name) < 1 : name = "mbox-short.txt" 12 | handle = open(name) 13 | d=dict() 14 | count = 0 15 | for line in handle: 16 | 17 | if not line.startswith('From'):continue 18 | 19 | line=line.split() 20 | 21 | if line[0]=='From': 22 | 23 | line = line[5].split(':') 24 | 25 | for hrs in line[0].split(): 26 | 27 | if hrs not in d: 28 | 29 | d[hrs]=1 30 | else: 31 | d[hrs]+=1 32 | 33 | for key,value in sorted(d.items()): 34 | print key,value 35 | --------------------------------------------------------------------------------