Difference between revisions of "CSC111 Lab 8 2014"

From dftwiki3
Jump to: navigation, search
(Challenge 2)
Line 87: Line 87:
  
  
China 1,339,190,000 9,596,960.00 139.54 3,705,405.45 361.42
+
China 1,339,190,000 9,596,960.00 139.54 3,705,405.45 361.42
India 1,184,639,000 3,287,590.00 360.34 1,269,345.07 933.27
+
India 1,184,639,000 3,287,590.00 360.34 1,269,345.07 933.27
United States of America 309,975,000 9,629,091.00 32.19 3,717,811.29 83.38
+
United-States 309,975,000 9,629,091.00 32.19 3,717,811.29 83.38
Indonesia 234,181,400 1,919,440.00 122.01 741,099.62 315.99
+
Indonesia 234,181,400 1,919,440.00 122.01 741,099.62 315.99
Brazil 193,364,000 8,511,965.00 22.72 3,286,486.71 58.84
+
Brazil 193,364,000 8,511,965.00 22.72 3,286,486.71 58.84
Pakistan 170,260,000 803,940.00 211.78 310,402.84 548.51
+
Pakistan 170,260,000 803,940.00 211.78 310,402.84 548.51
Nigeria 170,123,000 923,768.00 171.32 356,668.67 443.71
+
Nigeria 170,123,000 923,768.00 171.32 356,668.67 443.71
Bangladesh 164,425,000 144,000.00 1,141.84 55,598.69 2,957.35
+
Bangladesh 164,425,000 144,000.00 1,141.84 55,598.69 2,957.35
Russia 141,927,297 17,075,200.00 8.31 6,592,768.87 21.53
+
Russia 141,927,297 17,075,200.00 8.31 6,592,768.87 21.53
Japan 127,380,000 377,835.00 337.13 145,882.85 873.17
+
Japan 127,380,000 377,835.00 337.13 145,882.85 873.17
Mexico 108,396,211 1,972,550.00 54.95 761,605.50 142.33
+
Mexico 108,396,211 1,972,550.00 54.95 761,605.50 142.33
Phillipines 94,013,200 300,000.00 313.38 115,830.60 811.64
+
Phillipines 94,013,200 300,000.00 313.38 115,830.60 811.64
Vietnam 85,789,573 329,560.00 260.32 127,243.78 674.21
+
Vietnam 85,789,573 329,560.00 260.32 127,243.78 674.21
Germany 81,757,600 357,021.00 229.00 137,846.52 593.11
+
Germany 81,757,600 357,021.00 229.00 137,846.52 593.11
Ethiopia 79,221,000 1,127,127.00 70.29 435,185.99 182.04
+
Ethiopia 79,221,000 1,127,127.00 70.29 435,185.99 182.04
Egypt 78,848,000 1,001,450.00 78.73 386,661.85 203.92
+
Egypt 78,848,000 1,001,450.00 78.73 386,661.85 203.92
Iran 75,078,000 1,648,000.00 45.56 636,296.10 117.99
+
Iran 75,078,000 1,648,000.00 45.56 636,296.10 117.99
  
  
Line 115: Line 115:
 
|-
 
|-
 
|
 
|
 +
 
==Challenge x==
 
==Challenge x==
 
|}
 
|}

Revision as of 13:41, 24 March 2014

--D. Thiebaut (talk) 14:01, 24 March 2014 (EDT)


This lab deals with strings and list operations, and transforming strings into lists and lists into strings.


Splitting Strings


Work in the console, and try these different commands. Observe what the different operations do.

>>> line = "The quick, red fox jumped.  It jumped over the lazy, sleepy, brown dog."
>>> line

>>> line.split()
>>> words = line.split()
>>> words

>>> words[0]

>>> words[1]

>>> words[-1]

>>> words[-2]

>>> chunks = line.split( ',' )      # split on commas
>>> chunks

>>> chunks = line.split( '.' )      # split on periods
>>> chunks

>>> words

>>> separator = "+"
>>> newLine = separator.join( words )    # join the words into a new string and use separator as the glue
>>> newLine

>>> separator = "$$$"
>>> newLine = separator.join( words )    # same but use $$$ as the glue
>>> newLine

>>> words       # verify that you still have individual words in this list

>>> newWords = [ words[0], words[3], words[4], words[7], words[8], words[12] ] # create a new list 
>>> newWords

>>> " ".join( newWords )      # join strings in newWords list with a space

Mini Assignments


Challenge 1

QuestionMark1.jpg
  • Use a judicious mix() of split() and join operations to convert the string
"1	China	1,339,190,000	9,596,960.00	139.54	3,705,405.45	361.42"
into a new string:
"China 1339190000"
Note 1: the lack of commas in the number! (Hints: string objects have replace methods that could prove useful here!)
Note 2: that this line is taken from a table from this URL where the numbers after the country indicate a) the population, the area and population density expressed with square-kilometers, and the area and population density expressed with square-miles.




Challenge 2

QuestionMark1.jpg
  • Given the following list, split it into individual lines, and apply your transformation to each line so that your program outputs only the country names and their populations.


China	1,339,190,000	9,596,960.00	139.54	3,705,405.45	361.42
India	1,184,639,000	3,287,590.00	360.34	1,269,345.07	933.27
United-States	309,975,000	9,629,091.00	32.19	3,717,811.29	83.38
Indonesia	234,181,400	1,919,440.00	122.01	741,099.62	315.99
Brazil	193,364,000	8,511,965.00	22.72	3,286,486.71	58.84
Pakistan	170,260,000	803,940.00	211.78	310,402.84	548.51
Nigeria	170,123,000	923,768.00	171.32	356,668.67	443.71
Bangladesh	164,425,000	144,000.00	1,141.84	55,598.69	2,957.35
Russia	141,927,297	17,075,200.00	8.31	6,592,768.87	21.53
Japan	127,380,000	377,835.00	337.13	145,882.85	873.17
Mexico	108,396,211	1,972,550.00	54.95	761,605.50	142.33
Phillipines	94,013,200	300,000.00	313.38	115,830.60	811.64
Vietnam	85,789,573	329,560.00	260.32	127,243.78	674.21
Germany	81,757,600	357,021.00	229.00	137,846.52	593.11
Ethiopia	79,221,000	1,127,127.00	70.29	435,185.99	182.04
Egypt	78,848,000	1,001,450.00	78.73	386,661.85	203.92
Iran	75,078,000	1,648,000.00	45.56	636,296.10	117.99





Challenge x

QuestionMark1.jpg





Challenge x

QuestionMark1.jpg





Challenge x

QuestionMark1.jpg





Challenge x

QuestionMark1.jpg





Challenge x

QuestionMark1.jpg





Challenge x

QuestionMark1.jpg





Challenge x

QuestionMark1.jpg





Challenge x

QuestionMark1.jpg





Challenge x

QuestionMark1.jpg





Challenge x

QuestionMark1.jpg





Challenge x

QuestionMark1.jpg





Challenge x

QuestionMark1.jpg





Challenge x

QuestionMark1.jpg





Challenge x

QuestionMark1.jpg




  • Figure out a way to take a string of the form "Pakistan 108 166 226" where the first word is a country name, and the following three numbers are estimated populations of this country in 1900, 2008, and 2025, into a new string with only the first and last words, i.e. "Pakistan 226".


China 1,458 India 1,398 United-States 352 Indonesia 273 Brazil 223 Pakistan 226 Bangladesh 198 Nigeria 208 Russia 137 Japan 126