Coding practice #1: Due midnight, September 29
file needed = None
Answer the questions below in a jupyter notebook. You can simply add cells to this notebook and enter your answers. When you are finished, upload the completed notebook to canvas.
My office hours are Tuesdays 9:00AM-10:00AM and Tuesdays 3:30PM-4:30PM in 7444 Soc Sciences. Satyen's are Mondays 3:00PM-4:00PM in 6413 Soc Sciences and Mitchell's are Thursdays 3:00PM-4:00PM in 7308 Soc Sciences.
You should feel free to discuss the coding practice with your classmates, but the work you turn in should be your own.
Cite any code that is not yours: badgerdata.org/pages/citing-code
Read the class policy on AI: badgerdata.org/pages/ai
Exercise 1: Last, First
Replace 'Last, First' above with your actual name. Enter it as your last name, first name.
Exercise 2: Warm-ups
- What is the result of each of the following lines of code? Edit this cell and type your answer next to the statement in bold.
-
type('hello')
-
type(['hello'])
-
x = [1, '1', 3.333, ['a', 'b', [5, 6, 4.89]]]
type(x[3][2][2])
-
type(str(int('5')))
-
Explain your answer to D.
-
Take the list below, and write code to print it as a sentence, so that it reads: Why are there so many questions? Use a loop.
words = ['Why', 'are', 'there', 'so', 'many', 'questions', '?']
- You are given the the
data
dictionary below. Notice that the gdp data are stored as strings. Fixdata
by replacing the strings with the approriate floats and then print the dict. You might try using a loop or a list comprehension.
data = {'year':[1990, 2000, 2010], 'gdp':['8.95', '12.56', '14.78']}
- Use slicing to extract each word from the string below. (Do not extract the spaces.) Store each word in a string variable of the form: word_1, word_2...so that the print statement replicates the string. [This is an exhausting way to print a string!]
truth = 'No matter how you slice it'
# Add the definitions of word_1,...,word_6 using slices.
word_1 =
word_2 =
word_3 =
word_4 =
word_5 =
word_6 =
print(word_1, word_2, word_3, word_4, word_5, word_6)
No matter how you slice it
-
Print the sum of the integers between 0 and 50 that are multiples of 5: e.g., 0, 5, 10,...50.
-
The two lists below contain the most popular female and male names found in the U.S. Social Security database. Write a code that compares the lengths of the male and female names at each rank (e.g. James v. Mary, Robert v. Jennifer, etc.) and prints a statement for each rank of the form: 'The name James is longer than the name Mary.'
names_f = ['Mary', 'Patricia', 'Jennifer', 'Linda', 'Elizabeth']
names_m = ['James', 'John', 'Robert', 'Michael', 'William']
[Optional: If you would like to learn a more 'python-y' way to do this, look up the zip()
function.]
Exercise 3: U.S. Exports
The following code cell contains a dictionary with nominal exports of goods (in millions of dollars) from the United States to every other country in the world in 2017.
Run the code cell below to read the dictionary into memory.
us_exports_2017 = {'Argentina': 9585.957928, 'Serbia': 125.604552, 'Bulgaria': 381.490292, 'Bangladesh': 1473.961033,
'Sierra Leone': 79.185252, 'Guyana': 377.349484, 'Turkey': 9741.491909, 'Syrian Arab Republic': 6.961023,
'Saint Vincent and the Grenadines': 81.348131, 'Eritrea': 5.021254, 'Kosovo': 10.070786, 'Bermuda': 693.65006,
'Zimbabwe': 39.926035, 'Suriname': 359.542397, 'Uganda': 107.869624, 'Palau': 19.432665, 'Guinea': 103.022431,
'Heard Island and Mcdonald Islands': 0.125829, 'Mongolia': 82.192362, 'Botswana': 93.045279, 'Cameroon': 158.845783,
'Ecuador': 4820.700634, 'Falkland Islands (Malvinas)': 0.482089, 'Iceland': 399.258756, 'South Korea': 48326.408702,
'Hungary': 1888.233492, 'Comoros': 1.375097, 'Malta': 293.009573, 'Luxembourg': 1078.074731, 'Tonga': 16.719457,
'Tanzania; United Republic of': 145.242068, 'Liberia': 138.340637, 'Mali': 62.01555, 'Algeria': 1059.840617,
'South Africa': 5020.062464, 'Kyrgyzstan': 26.066859, 'Swaziland': 24.073435, 'Central African Republic': 11.178603,
'Andorra': 3.285167, 'Seychelles': 15.488847, 'Other Countries': 0.0, 'Cook Islands': 5.243873,
"Lao People's Democratic Republic": 25.67166, 'Ghana': 859.971235, 'Malaysia': 12964.461912,
'Micronesia (Federated States of)': 44.827548, 'Indonesia': 6863.831289, 'Tajikistan': 17.911115, 'Gambia': 39.235769,
'Thailand': 10991.613094, 'Cape Verde': 9.146393, 'West Bank': 2.19236, 'Sri Lanka': 336.25559,
'Burma (Myanmar)': 211.368799, 'Lebanon': 1221.529041, 'Poland': 4523.318939, 'Austria': 4275.26315,
'Panama': 6301.303156, 'Lithuania': 609.272207, 'Hong Kong': 39939.130227, 'Kenya': 454.453344, 'Netherlands': 41510.332962,
'Dominican Republic': 7827.520352, 'Cayman Islands': 885.800982, 'Reunion': 8.376879, 'Belarus': 72.668182,
'Burundi': 8.13276, 'Nigeria': 2170.069073, 'Martinique': 168.255979, 'Equatorial Guinea': 111.93615, 'Vatican City': 1.146853,
'Taiwan': 25729.504439, 'Georgia': 382.865271, 'Aruba': 966.305683, 'Lesotho': 1.901616, 'Colombia': 13312.122946,
'Curacao': 692.167798, 'Cuba': 291.276873, 'French Southern Territories': 2.842721, 'China': 129893.586716,
'Norway': 5453.37072, 'Svalbard and Jan Mayen Island': 1.986719, "Côte d'Ivoire": 319.964133, 'Uruguay': 1599.284155,
'Montserrat': 9.708109, 'Madagascar': 53.367175, 'Vanuatu': 7.762425, 'Bhutan': 34.892754, 'Greenland': 9.884715,
'Kuwait': 5143.414006, 'Iraq': 1204.721644, 'Senegal': 207.93405, 'Ukraine': 1787.9993, 'Haiti': 1410.575337,
'Mauritius': 61.421605, 'Nicaragua': 1589.398811, 'Qatar': 3124.095821, 'Moldova; Republic of': 17.614764,
'Samoa': 38.309271, 'Saint Kitts and Nevis': 243.537203, 'Ethiopia': 877.152409, 'French Polynesia': 119.798493,
'Uzbekistan': 136.094699, 'East Germany': 0.0, 'Switzerland': 21684.835238, 'Rwanda': 66.140569, 'Solomon Islands': 9.303881,
'Macedonia': 41.225784, 'Christmas Island': 0.476695, 'Sao Tome and Principe': 3.140792, 'Papua New Guinea': 105.851898,
'Venezuela': 4133.114819, 'Iran': 136.000618, 'Grenada': 102.991413, 'New Caledonia': 59.362579, 'Kazakhstan': 552.092011,
'Italy': 18404.689805, 'Paraguay': 2719.236832, 'Guinea Bissau': 3.751655, 'India': 25688.861698, 'Slovenia': 371.602049,
'Canada': 282265.135262, 'United Arab Emirates': 20019.696481, 'Turks and Caicos Islands': 369.866125,
'Croatia': 447.955755, 'Nepal': 75.851857, 'Congo': 117.378013, 'Saint Helena': 0.587754,
'British Indian Ocean Territory': 36.334868, 'Singapore': 29805.915154, 'Slovakia': 441.620058, 'Faroe Islands': 2.034537,
'Gaza Strip': 0.555101, 'Saint Lucia': 572.438902, 'Dominica': 178.447184, 'Bolivia': 594.703634, 'Chad': 31.338485,
'British Virgin Islands': 377.227746, 'Wallis and Futuna': 0.300526, 'South Sudan': 13.57431, 'Kiribati': 4.928546,
'Fiji': 66.551898, 'French Guiana': 162.74323, 'United Kingdom': 56257.922547, 'Finland': 1534.930626, 'Jordan': 1920.88633,
'Serbia and Montenegro': 0.0, 'Norfolk Island': 0.138341, 'Pakistan': 2808.16696, 'Bahamas': 3057.271998, 'Mexico': 243314.438647,
'Honduras': 5079.663953, 'Afghanistan': 941.42806, 'Anguilla': 59.409382, 'Romania': 954.684233, 'Spain': 11063.740551,
'Tuvalu': 0.796156, 'Portugal': 1192.459324, 'Brunei': 121.111606, 'France': 33595.514399, 'Togo': 481.810854,
'Chile': 13605.342592, 'Marshall Islands': 556.988101, 'Morocco': 2219.661823, 'Peru': 8662.61201,
'Western Sahara': 0.418356, 'Angola': 809.444868, 'Malawi': 27.88084, 'Netherlands Antilles': 0.0, 'Belize': 294.920783,
'Azerbaijan': 354.555299, 'Macau; SAR of China': 502.136131, 'Bahrain': 898.1564, 'Niue': 0.086722, 'North Korea': 0.00266,
'Nauru': 0.198927, 'Cyprus': 82.40193, 'Gibraltar': 1126.417629, 'Somalia': 70.594917, 'Greece': 960.728649,
'Gabon': 89.00649, 'Saudi Arabia': 16348.000104, 'Japan': 67605.076964, 'Mauritania': 127.77739, 'Estonia': 273.958646,
'Tokelau Islands': 1.162557, 'Sint Maarten': 545.15619, 'Namibia': 118.561677, 'Australia': 24526.835921,
'Maldives': 34.373287, 'Sudan': 69.654352, 'Djibouti': 157.467862, 'New Zealand': 3924.611227, 'East Timor': 2.565401,
'Antigua and Barbuda': 425.623529, 'Czech Republic': 2273.780613, 'Guatemala': 6895.337671, 'Liechtenstein': 27.744402,
'Belgium': 29923.692464, 'Mozambique': 179.126068, 'Monaco': 42.263572, 'Brazil': 37221.566249, 'Germany': 53896.753486,
'Egypt': 3991.832766, 'San Marino': 2.109451, 'Yemen': 199.030638, 'Cocos (Keeling) Islands': 0.452079,
'Israel': 12550.082579, 'Mayotte': 0.493107, 'Barbados': 525.452357, 'Philippines': 8450.925039,
'Burkina Faso': 60.978397, 'Guadeloupe': 244.842196, 'Benin': 250.13587, 'Armenia': 55.005606,
'Democratic Republic of Congo': 76.260999, 'Niger': 44.082049, 'Viet Nam': 8133.364685, 'Albania': 62.086804,
'Montenegro': 9.148718, 'Cambodia': 400.220962, 'Bosnia and Herzegovina': 27.142924, 'El Salvador': 3057.896029,
'Pitcairn Islands': 0.001431, 'Trinidad and Tobago': 1816.745419, 'Tunisia': 543.575125, 'Sweden': 3734.457331,
'Zambia': 85.149483, 'Turkmenistan': 282.178944, 'Oman': 1985.049832, 'Denmark': 2210.191808, 'Latvia': 381.621395,
'Ireland': 10707.555883, 'Libyan Arab Jamahiriya': 134.952674, 'Saint Pierre and Miquelon': 0.390569,
'Costa Rica': 6169.60881, 'Jamaica': 2105.505325, 'Russia': 6998.497148}
-
Report the value of U.S. exports of goods to China. Print it as 'The United States exported \$X billion of goods to China in 2017.' Format your string so that the number has one digit to the right of the decimal point. [Notice that the values in the dictionary are reported in millions.]
You should look into
str.format()
. -
Report the total value of U.S. exports of goods to the entire world. Print it as 'The United States exported $X trillion dollars of goods in 2017.' Format your string so that the number has one digit to the right of the decimal point. [Hint: use the
.values()
method.] -
Report the total value of U.S. exports of goods to the "BRICS" countries: Brazil, Russia, India, China, and South Africa as a share of total exports. Print it as 'U.S. exports to BRICS countries accounted for X percent of total U.S. exports in 2017.' Format your string so that the number has two digits to the right of the decimal point.
Exercise 4: Morse code (challenging)
Morse code was created to transmit messages over the telegraph. It uses dots and dashes to signal letters. Below are two lists. letters
lists the 26 letters of the alphabet and some punctuation marks. morse
lists the Morse code for each letter. Take this example:
test = ['-.-.', '.-..', '---', '...', '.', '.......', '--.', '.-', '-', '.']
The first code is '-.-.'
(dash-dot-dash-dot). That is the third entry in the morse
list, which is 'c'
. The second is '.-..'
which is 'l'
. Continuing this way, test
translates to: 'close gate'.
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
' ', ',', '.']
morse = ['.-', '-...', '-.-.', '-..', '.', '..-.', '--.', '....', '..', '.---', '-.-', '.-..', '--',
'-.', '---', '.--.', '--.-', '.-.', '...', '-', '..-', '...-', '.--', '-..-', '-.--', '--..',
'.......', '---..--', '.-.-.-']
-
Create a decoder dict that holds the roman letters and their Morse codes. We will use this to translate Morse code to roman letters. Should the Morse codes be the keys or should the roman letters be the keys?
-
Use your dict to decode the following. Print out the result.
to_decode = ['-', '....', '.', '.......', '.--', '---', '---', '-..', '...', '.......', '.-', '.-.', '.', '.......', '.-..', '---', '...-', '.', '.-..', '-.--', '---..--', '.......', '-..', '.-', '.-.', '-.-', '.......', '.-', '-.', '-..', '.......', '-..', '.', '.', '.--.', '---..--', '.......', '-...', '..-', '-', '.......', '..', '.......', '....', '.-', '...-', '.', '.......', '.--.', '.-.', '---', '--', '..', '...', '.', '...', '.......', '-', '---', '.......', '-.-', '.', '.', '.--.', '---..--', '.......', '.-', '-.', '-..', '.......', '--', '..', '.-..', '.', '...', '.......', '-', '---', '.......', '--.', '---', '.......', '-...', '.', '..-.', '---', '.-.', '.', '.......', '..', '.......', '...', '.-..', '.', '.', '.--.', '---..--', '.......', '.-', '-.', '-..', '.......', '--', '..', '.-..', '.', '...', '.......', '-', '---', '.......', '--.', '---', '.......', '-...', '.', '..-.', '---', '.-.', '.', '.......', '..', '.......', '...', '.-..', '.', '.', '.--.', '.-.-.-']
-
Create an encoder dict that hold the roman letters and their Morse codes. We will use this to translate roman letters to Morse code. Should the Morse codes be the keys or should the roman letters be the keys?
-
Encode the following string into Morse code. Your result should be a list, where each element of the list is the Morse code for one letter. Print out the list.
to_encode = 'the troubles of our proud and angry dust are from eternity, and shall not fail. bear them we can, and if we can we must. shoulder the sky, my lad, and drink your ale.'
- Encode the following string into Morse code. Your result should be a list, where each element of the list is the Morse code for one letter. Print out the list. Notice the difference in capitalization from the part 4.
to_encode = 'The troubles of our proud and angry dust are from eternity, and shall not fail. Bear them we can, and if we can we must. Shoulder the sky, my lad, and drink your ale.'