Difference between revisions of "CSC111 Exercises with Lists"
(→ListExercises.py) |
|||
(2 intermediate revisions by the same user not shown) | |||
Line 45: | Line 45: | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
+ | =Solution Program= | ||
+ | <br /> | ||
+ | ==videoFiles.py== | ||
+ | <br /> | ||
+ | <source lang="python"> | ||
+ | text = """-rw-r--r-- 1 thiebaut staff 31512938 Jan 21 19:10 ComputerScienceAnalytics_6.mp4 | ||
+ | -rw-r--r-- 1 thiebaut staff 36326928 Feb 21 19:10 ComputerScienceAnalytics_7.mp4 | ||
+ | -rw-r--r-- 1 thiebaut staff 25889113 Feb 21 19:06 ComputerScienceAnalytics_8.mp4 | ||
+ | -rw-r--r-- 1 thiebaut staff 23700210 Jan 17 19:08 ComputerScienceAnalytics_9.mp4 | ||
+ | -rw-r--r-- 1 thiebaut staff 45115774 Jan 13 19:13 ComputerScienceFinancialApplications_Plank_01.mp4 | ||
+ | -rw-r--r-- 1 thiebaut staff 36406334 Dec 16 13:42 ComputerScienceFinancialApplications_Plank_02.mp4 | ||
+ | -rw-r--r-- 1 thiebaut staff 46622104 Dec 16 19:10 ComputerScienceFinancialApplications_Plank_03.mp4 | ||
+ | -rw-r--r-- 1 thiebaut staff 53252084 Jan 17 19:11 ComputerScienceFinancialApplications_Plank_04.mp4 | ||
+ | -rw-r--r-- 1 thiebaut staff 45092698 Jan 21 13:46 ComputerScienceFinancialApplications_Plank_05.mp4 | ||
+ | -rw-r--r-- 1 thiebaut staff 61646571 Dec 30 19:11 ComputerScienceFinancialApplications_Plank_06.mp4 | ||
+ | -rw-r--r-- 1 thiebaut staff 59283490 Jan 21 13:48 ComputerScienceMedicine_Monvieux_01.mp4 | ||
+ | -rw-r--r-- 1 thiebaut staff 76205259 Jan 31 13:42 ComputerScienceMedicine_Monvieux_02.mp4 | ||
+ | -rw-r--r-- 1 thiebaut staff 72940315 Dec 17 20:04 ComputerScienceMedicine_Monvieux_03.mp4 | ||
+ | -rw-r--r-- 1 thiebaut staff 48119432 Dec 31 19:14 ComputerScienceMedicine_Monvieux_04.mp4 | ||
+ | -rw-r--r-- 1 thiebaut staff 100468403 Dec 16 19:26 ComputerScienceMedicine_Monvieux_05.mp4 | ||
+ | -rw-r--r-- 1 thiebaut staff 100468403 Dec 16 19:26 ComputerScienceMedicine_Monvieux_06.mp4 | ||
+ | -rw-r--r-- 1 thiebaut staff 63308062 Dec 16 13:46 ComputerScienceMedicine_Monvieux_07.mp4 | ||
+ | -rw-r--r-- 1 thiebaut staff 57665921 Dec 30 19:09 ComputerScienceSystemsScalability_Zardoz_1.mp4 | ||
+ | -rw-r--r-- 1 thiebaut staff 84255667 Jan 17 19:13 ComputerScienceSystemsScalability_Zardoz_2.mp4 | ||
+ | -rw-r--r-- 1 thiebaut staff 91981675 Jan 17 19:13 ComputerScienceSystemsScalability_Zardoz_3.mp4 | ||
+ | -rw-r--r-- 1 thiebaut staff 53601252 Dec 16 19:12 ComputerScienceSystemsScalability_Zardoz_4.mp4 | ||
+ | -rw-r--r-- 1 thiebaut staff 61131921 Jan 21 13:46 AnalyticsFastAlgorithms_Lowenthal_1.mp4 | ||
+ | -rw-r--r-- 1 thiebaut staff 42672117 Jan 21 13:46 AnalyticsFastAlgorithms_Lowenthal_2.mp4 | ||
+ | -rw-r--r-- 1 thiebaut staff 31806559 Jan 15 16:23 AnalyticsFastAlgorithms_Lowenthal_3.mp4 | ||
+ | -rw-r--r-- 1 thiebaut staff 43803673 Jan 15 16:24 AnalyticsFastAlgorithms_Lowenthal_4.mp4 | ||
+ | -rw-r--r-- 1 thiebaut staff 37293159 Jan 15 19:09 AnalyticsFastAlgorithms_Lowenthal_5.mp4 | ||
+ | -rw-r--r-- 1 thiebaut staff 46556009 Jan 29 19:12 AnalyticsFastAlgorithms_Lowenthal_6.mp4 | ||
+ | -rw-r--r-- 1 thiebaut staff 38307419 Jan 29 19:10 AnalyticsFastAlgorithms_Lowenthal_7.mp4 | ||
+ | -rw-r--r-- 1 thiebaut staff 41391629 Feb 21 13:34 ComputerScienceAnalytics_1.mp4 | ||
+ | -rw-r--r-- 1 thiebaut staff 56999320 Feb 21 19:11 ComputerScienceAnalytics_2.mp4 | ||
+ | -rw-r--r-- 1 thiebaut staff 48847747 Jan 21 13:50 ComputerScienceAnalytics_3.mp4 | ||
+ | -rw-r--r-- 1 thiebaut staff 21625121 Feb 21 19:06 ComputerScienceAnalytics_4.mp4 | ||
+ | -rw-r--r-- 1 thiebaut staff 26003908 Jan 23 13:43 ComputerScienceAnalytics_5.mp4 | ||
+ | |||
+ | """ | ||
+ | </source> | ||
+ | <br /> | ||
+ | ==ListExercises.py== | ||
+ | <br /> | ||
+ | <source lang="python"> | ||
+ | # ListExercises.py | ||
+ | # D. Thiebaut | ||
+ | # answers to Exercises given in | ||
+ | # http://cs.smith.edu/dftwiki/index.php/CSC111_Exercises_with_Lists | ||
+ | # | ||
+ | from videoFiles import text | ||
+ | |||
+ | |||
+ | # box(): prints a caption inside a box | ||
+ | def box( caption ): | ||
+ | length = len( caption )+2 | ||
+ | print( "\n\n\n+" + "-"*length + "+" ) | ||
+ | print( "| " + caption + " |" ) | ||
+ | print( "+" + "-"*length + "+" ) | ||
+ | |||
+ | # answer to Exercise 1 | ||
+ | def exo1(): | ||
+ | box( "Exercise 1" ) | ||
+ | list = [] | ||
+ | for i in range( 1, 100, 2 ): | ||
+ | list.append( i ) | ||
+ | |||
+ | print( "list = ", list ) | ||
+ | |||
+ | list = [] | ||
+ | for i in range( 1, 10000000, 2 ): | ||
+ | list.append( i ) | ||
+ | print( "list = ", list[0],"...", list[-1] ) | ||
+ | |||
+ | # answer to Exercise 2 | ||
+ | def exo2(): | ||
+ | box( "Exercise 2" ) | ||
+ | line = "The quick red fox jumped over the brown sleeping dog" | ||
+ | words = line.split() | ||
+ | print( "words = ", words ) | ||
+ | separator = "|" | ||
+ | newLine = separator.join( words ) | ||
+ | print( "newLine = ", newLine ) | ||
+ | |||
+ | # answer to Exercise 3 | ||
+ | def exo3(): | ||
+ | # Question 1 | ||
+ | box( "Exercise 3, Question 1" ) | ||
+ | list = [] | ||
+ | for line in text.split( "\n" ): | ||
+ | if len( line ) <= 1: | ||
+ | continue | ||
+ | words = line.split() | ||
+ | list.append( words[-1] ) | ||
+ | |||
+ | print( "list = ", "\n".join( list ) ) | ||
+ | |||
+ | # Question 2 | ||
+ | box( "Exercise 3, Question 2" ) | ||
+ | max = 0 | ||
+ | fileMax = "" | ||
+ | for line in text.split( "\n" ): | ||
+ | if len( line ) <= 1: | ||
+ | continue | ||
+ | words = line.split() | ||
+ | size = int( words[4] ) | ||
+ | if size > max: | ||
+ | max = size | ||
+ | fileMax = words[-1] | ||
+ | |||
+ | print( "Largest file = %s (%d bytes)" % (fileMax, max) ) | ||
+ | |||
+ | # Question 3 | ||
+ | box( "Exercise 3, Question 3" ) | ||
+ | for line in text.split( "\n" ): | ||
+ | if len( line ) <= 1: | ||
+ | continue | ||
+ | words = line.split() | ||
+ | print( "%50s %10d" % ( words[-1], int(words[4])) ) | ||
+ | |||
+ | # Question 5 | ||
+ | box( "Exercise 3, Question 4" ) | ||
+ | newList = [] | ||
+ | for line in text.split( "\n" ): | ||
+ | if len( line ) <= 1: | ||
+ | continue | ||
+ | words = line.split() | ||
+ | pair = ( words[-1], words[4] ) | ||
+ | newList.append( pair ) | ||
+ | |||
+ | newList.sort() | ||
+ | for name, size in newList: | ||
+ | print( name, size ) | ||
+ | |||
+ | def main(): | ||
+ | #exo1() | ||
+ | #exo2() | ||
+ | exo3() | ||
+ | |||
+ | main() | ||
+ | |||
+ | |||
+ | </source> | ||
<br /> | <br /> | ||
<br /> | <br /> |
Latest revision as of 11:52, 26 March 2014
--D. Thiebaut (talk) 09:53, 24 March 2014 (EDT)
Contents
Exercise 1
- Question 1
- Create a list with all the odd numbers between 1 and 100.
- Question 2
- Create a list with all the odd numbers less than 10,000,000
- Figure out a way to print the list in an efficient and elegant way
Exercise 2
- Question 1
- Split a line of text into a list of words.
- Question 2
- Reformat the list of words as a string with the |-character as separator instead of a space
Exercise 3
- Question 1
- Given the text in the first list of this page which represents lists of media files, create a list of just the names of the files.
- Question 2
- Given the list on the same page, find the file that takes the most amount of disk space.
- Question 3
- List the files as a name followed by a size, one per line
- Question 4
- Same as Question 3, but sort the list alphabetically
- Question 5
- Same as Question 3, but sort the list by file size
- Question 6
- Create a new list that contains only the size, month, day, year and file name for each file. In other words, drop the first 4 fields of each line.
Solution Program
videoFiles.py
text = """-rw-r--r-- 1 thiebaut staff 31512938 Jan 21 19:10 ComputerScienceAnalytics_6.mp4
-rw-r--r-- 1 thiebaut staff 36326928 Feb 21 19:10 ComputerScienceAnalytics_7.mp4
-rw-r--r-- 1 thiebaut staff 25889113 Feb 21 19:06 ComputerScienceAnalytics_8.mp4
-rw-r--r-- 1 thiebaut staff 23700210 Jan 17 19:08 ComputerScienceAnalytics_9.mp4
-rw-r--r-- 1 thiebaut staff 45115774 Jan 13 19:13 ComputerScienceFinancialApplications_Plank_01.mp4
-rw-r--r-- 1 thiebaut staff 36406334 Dec 16 13:42 ComputerScienceFinancialApplications_Plank_02.mp4
-rw-r--r-- 1 thiebaut staff 46622104 Dec 16 19:10 ComputerScienceFinancialApplications_Plank_03.mp4
-rw-r--r-- 1 thiebaut staff 53252084 Jan 17 19:11 ComputerScienceFinancialApplications_Plank_04.mp4
-rw-r--r-- 1 thiebaut staff 45092698 Jan 21 13:46 ComputerScienceFinancialApplications_Plank_05.mp4
-rw-r--r-- 1 thiebaut staff 61646571 Dec 30 19:11 ComputerScienceFinancialApplications_Plank_06.mp4
-rw-r--r-- 1 thiebaut staff 59283490 Jan 21 13:48 ComputerScienceMedicine_Monvieux_01.mp4
-rw-r--r-- 1 thiebaut staff 76205259 Jan 31 13:42 ComputerScienceMedicine_Monvieux_02.mp4
-rw-r--r-- 1 thiebaut staff 72940315 Dec 17 20:04 ComputerScienceMedicine_Monvieux_03.mp4
-rw-r--r-- 1 thiebaut staff 48119432 Dec 31 19:14 ComputerScienceMedicine_Monvieux_04.mp4
-rw-r--r-- 1 thiebaut staff 100468403 Dec 16 19:26 ComputerScienceMedicine_Monvieux_05.mp4
-rw-r--r-- 1 thiebaut staff 100468403 Dec 16 19:26 ComputerScienceMedicine_Monvieux_06.mp4
-rw-r--r-- 1 thiebaut staff 63308062 Dec 16 13:46 ComputerScienceMedicine_Monvieux_07.mp4
-rw-r--r-- 1 thiebaut staff 57665921 Dec 30 19:09 ComputerScienceSystemsScalability_Zardoz_1.mp4
-rw-r--r-- 1 thiebaut staff 84255667 Jan 17 19:13 ComputerScienceSystemsScalability_Zardoz_2.mp4
-rw-r--r-- 1 thiebaut staff 91981675 Jan 17 19:13 ComputerScienceSystemsScalability_Zardoz_3.mp4
-rw-r--r-- 1 thiebaut staff 53601252 Dec 16 19:12 ComputerScienceSystemsScalability_Zardoz_4.mp4
-rw-r--r-- 1 thiebaut staff 61131921 Jan 21 13:46 AnalyticsFastAlgorithms_Lowenthal_1.mp4
-rw-r--r-- 1 thiebaut staff 42672117 Jan 21 13:46 AnalyticsFastAlgorithms_Lowenthal_2.mp4
-rw-r--r-- 1 thiebaut staff 31806559 Jan 15 16:23 AnalyticsFastAlgorithms_Lowenthal_3.mp4
-rw-r--r-- 1 thiebaut staff 43803673 Jan 15 16:24 AnalyticsFastAlgorithms_Lowenthal_4.mp4
-rw-r--r-- 1 thiebaut staff 37293159 Jan 15 19:09 AnalyticsFastAlgorithms_Lowenthal_5.mp4
-rw-r--r-- 1 thiebaut staff 46556009 Jan 29 19:12 AnalyticsFastAlgorithms_Lowenthal_6.mp4
-rw-r--r-- 1 thiebaut staff 38307419 Jan 29 19:10 AnalyticsFastAlgorithms_Lowenthal_7.mp4
-rw-r--r-- 1 thiebaut staff 41391629 Feb 21 13:34 ComputerScienceAnalytics_1.mp4
-rw-r--r-- 1 thiebaut staff 56999320 Feb 21 19:11 ComputerScienceAnalytics_2.mp4
-rw-r--r-- 1 thiebaut staff 48847747 Jan 21 13:50 ComputerScienceAnalytics_3.mp4
-rw-r--r-- 1 thiebaut staff 21625121 Feb 21 19:06 ComputerScienceAnalytics_4.mp4
-rw-r--r-- 1 thiebaut staff 26003908 Jan 23 13:43 ComputerScienceAnalytics_5.mp4
"""
ListExercises.py
# ListExercises.py
# D. Thiebaut
# answers to Exercises given in
# http://cs.smith.edu/dftwiki/index.php/CSC111_Exercises_with_Lists
#
from videoFiles import text
# box(): prints a caption inside a box
def box( caption ):
length = len( caption )+2
print( "\n\n\n+" + "-"*length + "+" )
print( "| " + caption + " |" )
print( "+" + "-"*length + "+" )
# answer to Exercise 1
def exo1():
box( "Exercise 1" )
list = []
for i in range( 1, 100, 2 ):
list.append( i )
print( "list = ", list )
list = []
for i in range( 1, 10000000, 2 ):
list.append( i )
print( "list = ", list[0],"...", list[-1] )
# answer to Exercise 2
def exo2():
box( "Exercise 2" )
line = "The quick red fox jumped over the brown sleeping dog"
words = line.split()
print( "words = ", words )
separator = "|"
newLine = separator.join( words )
print( "newLine = ", newLine )
# answer to Exercise 3
def exo3():
# Question 1
box( "Exercise 3, Question 1" )
list = []
for line in text.split( "\n" ):
if len( line ) <= 1:
continue
words = line.split()
list.append( words[-1] )
print( "list = ", "\n".join( list ) )
# Question 2
box( "Exercise 3, Question 2" )
max = 0
fileMax = ""
for line in text.split( "\n" ):
if len( line ) <= 1:
continue
words = line.split()
size = int( words[4] )
if size > max:
max = size
fileMax = words[-1]
print( "Largest file = %s (%d bytes)" % (fileMax, max) )
# Question 3
box( "Exercise 3, Question 3" )
for line in text.split( "\n" ):
if len( line ) <= 1:
continue
words = line.split()
print( "%50s %10d" % ( words[-1], int(words[4])) )
# Question 5
box( "Exercise 3, Question 4" )
newList = []
for line in text.split( "\n" ):
if len( line ) <= 1:
continue
words = line.split()
pair = ( words[-1], words[4] )
newList.append( pair )
newList.sort()
for name, size in newList:
print( name, size )
def main():
#exo1()
#exo2()
exo3()
main()