Browse Source

5: files and dictionaries

Tianxiao He 1 year ago
parent
commit
825ac67f3f

BIN
files/.DS_Store


BIN
files/exercise_texts/.DS_Store


+ 7 - 0
files/exercise_texts/text1.txt

@@ -0,0 +1,7 @@
+medals.csv is a data file where each row describes a medal won in summer Olympics since 
+2010. The data is organized into columns, and the values in each column are separated by 
+comma
+Extract the following information from this file:
+What are top 5 countries that won maximum number of medals for the entire period from 2010?
+For each year, what are the top 5 countries with the largest number of medals? Did the 
+list of top performers change from game to game?

+ 4 - 0
files/exercise_texts/text2.txt

@@ -0,0 +1,4 @@
+For each country in medals.csv produce a medal score for the entire period from 2010.
+The medal score is defined as number of gold medals multiplied by 3 plus number of silver
+medals multiplied by 2 plus number of bronze medals•Produce the list of 10 countries with
+the highest medal score

+ 6 - 0
files/exercise_texts/text3.txt

@@ -0,0 +1,6 @@
+In file countries.csv there is some information about countries
+Using both medals.csv and countries.csv, produce a real medal score for each country
+A real score will be the medal score computed in the previous exercise divided by country
+population, thus producing the Olympic success rate per capita
+What are the top 10 countries with a real medal score? 
+Are they the same as in the previous exercise?

+ 4 - 0
files/files_intro.html

@@ -0,0 +1,4 @@
+<div>
+<p>In this lab you will exercise using files and dictionaries.</p>
+<p>Before you begin, review lectures about dictionaries: Slides 05.01 Slides 05.02 Slides 05.03
+</div>

BIN
files/hints/.DS_Store


+ 5 - 0
files/hints/hint1.html

@@ -0,0 +1,5 @@
+<div>
+<p><strong>Hint</strong></p>
+<p>How can we make a list of all the word in the given dictionary?</p>
+<p>How can we compare the given word with each word in the word dictionary?</p>
+</div>

+ 14 - 0
files/hints/hint2.html

@@ -0,0 +1,14 @@
+<div>
+<p><strong>Hint</strong></p>
+<p>For the total performance: </p>
+<p>How can we organize data with each unique category of country?</p>
+<p>What shall we do if we see a country for the first time? </p>
+<p>What happen to the value of medals compare to the last iteration for a certain country?</p>
+<p>How can we access the key and value at the same time?</p>
+<p>In order to do that, we need to convert both keys and values into list of tuples first.</p>
+
+<p>For the each year performance: </p>
+<p>What is the similiar algorithm that is used before? </p>
+<p>How can we separate the data into different dictionaries?</p>
+
+</div>

+ 6 - 0
files/hints/hint3.html

@@ -0,0 +1,6 @@
+<div>
+<p><strong>Hint</strong></p>
+<p>What are some similiar algorithms in this problem as the last one?</p>
+<p>How to get rid of the \n after we split the sentence?</p>
+<p>Within each line, how can we add a different value depending on the gold, silver or bronze medal?</p>
+</div>

+ 6 - 0
files/hints/hint4.html

@@ -0,0 +1,6 @@
+<div>
+<p><strong>Hint</strong></p>
+<p>What are some similiar algorithms in this problem as the last one?</p>
+<p>How can we pass the result of the last question to this one?</p>
+<p>How can we calculate the scores per capita and where can we get those data?</p>
+</div>

+ 4 - 0
files/hints/hint5.html

@@ -0,0 +1,4 @@
+<div>
+<p><strong>Hint</strong></p>
+<p>How can we access key and value at the same time?</p>
+<p>What can we do if we can not directly change the value of the dictionary?</p>

+ 4 - 0
files/hints/hint6.html

@@ -0,0 +1,4 @@
+<div>
+<p><strong>Hint</strong></p>
+<p>How can we access values of the dictionary?</p>
+<p>How can we add up some values in the dictionary?</p>

+ 8 - 0
files/hints/hint7.html

@@ -0,0 +1,8 @@
+<div>
+<p><strong>Hint</strong></p>
+<p>Part1</p>
+<p>How can we select the word with '@'?</p>
+<p>How can we add up the words, given the initial condition of 0 if not created before?(Similiar like exercise 2)</p>
+<p>Part2</p>
+<p>How can we access the dictionary that we created before?</p>
+<p>How can we keep the value and key at the same time?(similiar like exercise 5)</p>

+ 50 - 0
files/index.html

@@ -0,0 +1,50 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
+<html>
+<head>
+<title>Lab 5: Files and dictionaries</title>
+<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
+<link rel="stylesheet" type="text/css" href="../css/style.css" />
+<script src="js/main.js"></script>
+</head>
+<body>
+<div id="container">
+
+<div id="header"><br><a href="../index.html">Home</a>
+<h1><a href="https://hope.simons-rock.edu/~mbarsky/intro18/tutorials/files_dictionaries/FileExercises.pdf">Lab 5: Files and dictionaries</a></h1></div>
+  <div id="wrapper">
+    <div id="content">
+<iframe id="contentframe" height="400px" width="100%" src="files_intro.html" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe>
+  <div id="buttons" style="display: none;">
+  <button onclick="show_task()">Solve</button>
+  <button onclick="show_hint()">Hint</button>
+  <button onclick="show_solution()">Solution</button>
+  <button onclick="show_explanation()">Explanation</button>&nbsp;&nbsp;&nbsp;
+  <button id="completed" onclick="mark_completed()">Do not show again</button>
+  </div>
+ </div>
+  </div>
+  <div id="navigation">
+  <a href="">Files</a>
+			<ol>
+			<li><a href="javascript:load_problem(0)">Correct word</a>
+			</li>
+			<li><a href="javascript:load_problem(1)">Total medals</a>
+			</li>
+			<li><a href="javascript:load_problem(2)">Medal score</a>
+			</li>
+			<li><a href="javascript:load_problem(3)">Real winners</a>
+			</li>
+			<li><a href="javascript:load_problem(4)">Increment values</a>
+			</li>
+			<li><a href="javascript:load_problem(5)">Express checkout</a>
+			</li>
+			<li><a href="javascript:load_problem(6)">Most mentioned</a>
+      </li>
+			</ol>
+  </div>
+  <div id="footer">
+    <p>Footer</p>
+  </div>
+</div>
+</body>
+</html>

BIN
files/js/.DS_Store


+ 102 - 0
files/js/main.js

@@ -0,0 +1,102 @@
+var data = [
+{problem:"https://hope.simons-rock.edu/~mbarsky/intro18/tutorials/files_dictionaries/dict_is_correct.py",
+solution:"solutions/solution1.py",
+hint:"hints/hint1.html",
+explanation:"https://hope.simons-rock.edu/~mbarsky/intro18/tutorials/files_dictionaries/IsCorrectExplanation.mp4"},
+
+{problem: "exercise_texts/text1.txt",
+solution:"solutions/solution2.py",
+hint:"hints/hint2.html",
+explanation:""},
+
+{problem: "exercise_texts/text2.txt",
+solution:"solutions/solution3.py",
+hint:"hints/hint3.html",
+explanation:""},
+
+{problem:"exercise_texts/text3.txt",
+solution:"solutions/solution4.py",
+hint:"hints/hint4.html",
+explanation:""},
+
+{problem:"https://hope.simons-rock.edu/~mbarsky/intro18/tutorials/files_dictionaries/dict_increment_values.py",
+solution:"solutions/solution5.py",
+hint:"hints/hint5.html",
+explanation:""},
+
+{problem: "https://hope.simons-rock.edu/~mbarsky/intro18/tutorials/files_dictionaries/dict_express_checkout.py",
+solution:"solutions/solution6.py",
+hint:"hints/hint6.html",
+explanation:"https://hope.simons-rock.edu/~mbarsky/intro18/tutorials/files_dictionaries/ExpressCheckoutExplanation.mp4"},
+
+{problem: "https://hope.simons-rock.edu/~mbarsky/intro18/tutorials/files_dictionaries/dict_most_mentioned.py",
+solution:"solutions/solution7.py",
+hint:"hints/hint7.html",
+explanation:""}];
+
+var current_problem = -1;
+var current_included = 1;
+
+var problems = new Array(data.length);
+
+
+for (var i=0; i<problems.length; i++){
+	problems[i] = 1;
+}
+
+function load_problem(id){
+	current_problem = id;
+	current_included = problems[current_problem];
+	var iframe_elem = document.getElementById("contentframe");
+	iframe_elem.src = data[current_problem].problem;
+	show_buttons();
+}
+
+
+function show_buttons(){
+	var buttons_elem = document.getElementById("buttons");
+	if (current_problem >=0){
+		buttons_elem.style.display = "inline-block";
+	}
+	else {
+		buttons_elem.style.display = "none";
+	}
+}
+
+function show_task(){
+	var iframe_elem = document.getElementById("contentframe");
+	iframe_elem.src = data[current_problem].problem;
+	show_buttons();
+}
+
+function show_hint(){
+	var iframe_elem = document.getElementById("contentframe");
+	iframe_elem.src = data[current_problem].hint;
+	show_buttons();
+}
+
+function show_solution(){
+	var iframe_elem = document.getElementById("contentframe");
+	iframe_elem.src = data[current_problem].solution;
+	show_buttons();
+}
+
+function show_explanation(){
+	var iframe_elem = document.getElementById("contentframe");
+	iframe_elem.src = data[current_problem].explanation;
+	show_buttons();
+}
+
+function mark_completed(){
+	var include_elem = document.getElementById("completed");
+	if (current_included == 1){
+		include_elem.innerHTML = "Show again";
+		current_included = 0;
+		problems[current_problem] = 0;
+	}
+	else{
+		include_elem.innerHTML = "Do not show again";
+		current_included = 1;
+		problems[current_problem] = 1;
+	}
+}

BIN
files/solutions/.DS_Store


+ 197 - 0
files/solutions/countries.csv

@@ -0,0 +1,197 @@
+Country,Code,Population,GDP per Capita
+Afghanistan,AFG,32526562,594.323081219966
+Albania,ALB,2889167,3945.21758150914
+Algeria,ALG,39666519,4206.03123244958
+American Samoa*,ASA,55538,
+Andorra,AND,70473,
+Angola,ANG,25021974,4101.47215182964
+Antigua and Barbuda,ANT,91818,13714.7319616988
+Argentina,ARG,43416755,13431.8783398577
+Armenia,ARM,3017712,3489.12768956995
+Aruba*,ARU,103889,
+Australia,AUS,23781169,56310.9629933721
+Austria,AUT,8611088,43774.985173612
+Azerbaijan,AZE,9651349,5496.34464026248
+Bahamas,BAH,388019,22817.2308572518
+Bahrain,BRN,1377237,22600.2140981035
+Bangladesh,BAN,160995642,1211.70153057661
+Barbados,BAR,284215,15429.3404640853
+Belarus,BLR,9513000,5740.45649479562
+Belgium,BEL,11285721,40324.0277657215
+Belize,BIZ,359287,4878.72126724745
+Bermuda*,BER,65235,
+Benin,BEN,10879829,762.051205441965
+Bhutan,BHU,774830,2655.99889161858
+Bolivia,BOL,10724705,3076.79181060881
+Bosnia and Herzegovina,BIH,3810416,4249.3303131949
+Botswana,BOT,2262485,6360.13822018837
+Brazil,BRA,207847528,8538.5899749574
+British Virgin Islands,IVB,30117,
+Brunei,BRU,423188,30554.7296658073
+Bulgaria,BUL,7177991,6993.47735975728
+Burkina Faso,BUR,18105570,589.774414136617
+Burundi,BDI,11178921,277.068309170914
+Cambodia,CAM,15577899,1158.6899035244
+Cameroon,CMR,23344179,1217.26067048427
+Canada,CAN,35851774,43248.529909341
+Cape Verde,CPV,520502,3080.17881445563
+Cayman Islands*,CAY,59967,
+Central African Republic,CAF,4900274,323.201674024141
+Chad,CHA,14037472,775.695090525313
+Chile,CHI,17948141,13416.2300390185
+China,CHN,1371220000,8027.68381013907
+Colombia,COL,48228704,6056.14771720405
+Comoros,COM,788474,717.448849960912
+Congo,CGO,4620330,1851.19991555492
+"Congo, Dem Rep",COD,77266814,456.052740548027
+Costa Rica,CRC,4807850,11260.0921598775
+Cote d'Ivoire,CIV,22701556,1398.98995768413
+Croatia,CRO,4224404,11535.8293558997
+Cuba,CUB,11389562,
+Cyprus,CYP,1165300,23242.8400685313
+Czech Republic,CZE,10551219,17548.3382129701
+Denmark,DEN,5676002,51989.2934712354
+Djibouti,DJI,887861,1945.12429310444
+Dominica,DMA,72680,7116.38639189547
+Dominican Republic,DOM,10528391,6468.4734915433
+East Timor (Timor-Leste),TLS,1245015,1157.99295590816
+Ecuador,ECU,16144363,6205.06414529951
+Egypt,EGY,91508084,3614.74676616271
+El Salvador,ESA,6126583,4219.35032953932
+Equatorial Guinea,GEQ,845060,14439.5944478516
+Estonia,EST,1311998,17118.5042003236
+Ethiopia,ETH,99390750,619.169406475891
+Fiji,FIJ,892145,4960.51995438419
+Finland,FIN,5482013,42311.0362306446
+France,FRA,66808385,36205.5681017036
+Gabon,GAB,1725292,8266.4456050624
+Gambia,GAM,1990924,471.537195472347
+Georgia,GEO,3679000,3795.97330845042
+Germany,GER,81413145,41313.3139945434
+Ghana,GHA,27409893,1369.70112227585
+Greece,GRE,10823732,18002.2305776688
+Grenada,GRN,106825,9212.02035173484
+Guam,GUM,169885,
+Guatemala,GUA,16342897,3903.47885604613
+Guinea,GUI,12608590,531.320595188714
+Guinea-Bissau,GBS,1844325,572.988428543071
+Guyana,GUY,767085,4127.35101806198
+Haiti,HAI,10711067,818.343297634549
+Honduras,HON,8075060,2528.89354988523
+Hong Kong*,HKG,7305700,42327.8399570345
+Hungary,HUN,9844686,12363.5434596539
+Iceland,ISL,330823,50173.3399156473
+India,IND,1311050527,1598.25903421916
+Indonesia,INA,257563815,3346.48703949478
+Iran,IRI,79109272,
+Iraq,IRQ,36423395,4943.76038832056
+Ireland,IRL,4640703,61133.672427322
+Israel,ISR,8380400,35728.0934951515
+Italy,ITA,60802085,29957.8043154372
+Jamaica,JAM,2725941,5232.02458271187
+Japan,JPN,126958472,32477.2151449234
+Jordan,JOR,7594547,4940.04583574111
+Kazakhstan,KAZ,17544126,10509.9810699442
+Kenya,KEN,46050302,1376.71282894881
+"Korea, North",PRK,25155317,
+"Korea, South",KOR,50617045,27221.5240509661
+Kuwait,KUW,3892115,29300.5755750333
+Kyrgyzstan,KGZ,5957000,1103.2153515202
+Laos,LAO,6802023,1818.44137293236
+Latvia,LAT,1978440,13648.5475564772
+Lebanon,LIB,5850743,8047.64508557496
+Lesotho,LES,2135022,1066.98562625017
+Liberia,LBR,4503438,455.873934536237
+Libya,LBA,6278438,
+Liechtenstein,LIE,37531,
+Lithuania,LTU,2910199,14147.0493792403
+Luxembourg,LUX,569676,101449.968168042
+Macedonia,MKD,2078453,4852.6578476699
+Madagascar,MAD,24235390,401.836006029745
+Malawi,MAW,17215232,371.985747810344
+Malaysia,MAS,30331007,9768.32686011881
+Maldives,MDV,409163,8395.78519750383
+Mali,MLI,17599694,724.256283193304
+Malta,MLT,431333,22596.1817742659
+Mauritania,MTN,4067564,
+Mauritius,MRI,1262605,9252.11072429051
+Mexico,MEX,127017224,9005.02426497766
+Micronesia,FSM,104460,3015.23166762397
+Moldova,MDA,3554150,1848.06180430428
+Monaco,MON,37731,
+Mongolia,MGL,2959134,3967.82938559498
+Morocco,MAR,34377511,2878.20134215919
+Mozambique,MOZ,27977863,529.242556068943
+Burma,MYA,53897154,1161.48815791087
+Namibia,NAM,2458830,4673.56724769495
+Nauru,NRU,10222,9827.54428148233
+Nepal,NEP,28513700,743.322965726435
+Netherlands,NED,16936520,44299.768085383
+New Zealand,NZL,4595700,37807.9672760442
+Nicaragua,NCA,6082032,2086.89500277099
+Niger,NIG,19899120,358.958152040005
+Nigeria,NGR,182201962,2640.2907388575
+Norway,NOR,5195921,74400.3697770928
+Oman,OMA,4490541,15550.6762514328
+Pakistan,PAK,188924874,1434.69666504969
+Palau,PLW,21291,13498.661406228
+Panama,PAN,3929141,13268.1137549335
+Papua New Guinea,PNG,7619321,
+Paraguay,PAR,6639123,4080.95144785437
+Peru,PER,31376670,6027.12585529551
+Philippines,PHI,100699395,2904.20208191528
+Poland,POL,37999494,12554.5475536313
+Portugal,POR,10348648,19222.1500764153
+Puerto Rico*,PUR,3474182,
+Qatar,QAT,2235355,73653.3944346574
+Romania,ROM,19832389,8972.92251840971
+Russia,RUS,144096812,9092.58053606884
+Rwanda,RWA,11609666,697.34822808289
+Saint Kitts and Nevis,SKN,55572,15771.9455041308
+Saint Lucia,LCA,184999,7735.91048440102
+Saint Vincent and the Grenadines,VIN,109462,6739.17483286945
+Samoa,SAM,193228,3938.54884570316
+San Marino,SMR,31781,
+Sao Tome and Principe,STP,190344,1669.06326801326
+Saudi Arabia,KSA,31540372,20481.7453220484
+Senegal,SEN,15129273,899.579879484977
+Serbia,SCG,7098247,5235.14220696525
+Seychelles,SEY,92900,15476.01944443
+Sierra Leone,SLE,6453184,653.131815940648
+Singapore,SIN,5535002,52888.7446717529
+Slovakia,SVK,5424050,16088.2775872723
+Slovenia,SLO,2063768,20726.5398863708
+Solomon Islands,SOL,583591,1934.85629287353
+Somalia,SOM,10787104,549.266976567576
+South Africa,RSA,54956920,5723.97335690212
+Spain,ESP,46418269,25831.5823052954
+Sri Lanka,SRI,20966000,3926.17439589454
+Sudan,SUD,40234882,2414.72360102858
+Suriname,SUR,542975,9485.31924429496
+Swaziland,SWZ,1286970,3200.14301756487
+Sweden,SWE,9798871,50579.6736486777
+Switzerland,SUI,8286976,80945.0792194742
+Syria,SYR,18502413,
+Tajikistan,TJK,8481855,925.91188767081
+Tanzania,TAN,53470420,878.97510552608
+Thailand,THA,67959359,5814.76976382355
+Togo,TOG,7304578,559.635876661945
+Tonga,TGA,106170,4098.54392925825
+Trinidad and Tobago,TRI,1360088,17321.884675056
+Tunisia,TUN,11107800,3872.51208364171
+Turkey,TUR,78665830,9125.68758972936
+Turkmenistan,TKM,5373502,6672.47754417351
+Uganda,UGA,39032383,705.292569535068
+Ukraine,UKR,45198200,2114.95471628444
+United Arab Emirates,UAE,9156963,40438.7629344394
+United Kingdom,GBR,65138232,43875.9696143686
+United States,USA,321418820,56115.7184261955
+Uruguay,URU,3431555,15573.9009191818
+Uzbekistan,UZB,31299500,2132.07036847857
+Vanuatu,VAN,264652,2805.31464353567
+Venezuela,VEN,31108083,
+Vietnam,VIE,91703800,2111.13802366815
+Virgin Islands*,ISV,103574,
+Yemen,YEM,26832215,1406.2916511457
+Zambia,ZAM,16211767,1304.87901447726
+Zimbabwe,ZIM,15602751,924.143819253412

File diff suppressed because it is too large
+ 8005 - 0
files/solutions/medals.txt


+ 20 - 0
files/solutions/solution1.py

@@ -0,0 +1,20 @@
+def is_correct(dictionary_file, word):
+    """ (File open for reading, str) -> bool
+
+    Return True iff word is a correctly-spelled word
+    in dictionary.
+    >>> dict1 = open('words', 'r')
+    >>> is_correct(dict1, "Zyrtec")
+    True
+    >>> dict1.close()
+
+    >>> dict1 = open('words', 'r')
+    >>> is_correct(dict1, "lolz")
+    False
+    >>> dict1.close()
+    """
+    for line in dictionary_file:
+        if line.strip() == word:
+            return True
+    return False
+

+ 35 - 0
files/solutions/solution2.py

@@ -0,0 +1,35 @@
+def charts():
+    f = open('medals.txt','r')
+    d = {}
+    d00 = {}
+    d04 = {}
+    d08 = {}
+    d12 = {}
+    scores = []
+
+    for line in f:
+        words = line.split(',')
+        country = words[4]
+        d[country] = d.get(country, 0) + 1
+        for (yr,dic) in (('2000',d00), ('2004',d04), ('2008',d08), ('2012',d12)):
+            if words[0] == yr:
+                dic[country] = dic.get(country, 0) + 1
+
+    for key,val in d.items():
+        scores.append((val,key))
+    scores.sort(reverse=True)
+    print("Top 5 country for the whole period:")
+    for i in range(5):
+        print(scores[i][1])
+
+    for (yr, dic) in ((2000, d00), (2004, d04), (2008, d08), (2012, d12)):
+        scores = []
+        for key,val in dic.items():
+            scores.append((val,key))
+        scores.sort(reverse=True)
+        print("Top 5 country for", yr)
+        for i in range(5):
+            print(scores[i][1])
+
+
+charts()

+ 28 - 0
files/solutions/solution3.py

@@ -0,0 +1,28 @@
+def charts():
+    f = open('medals.txt','r')
+    d = {}
+    scores = []
+
+    for line in f:
+        words = line.split(',')
+        country = words[4]
+        words[-1] = words[-1][0:-1]
+        if words[0] == '2012':
+            for (medal, sc) in (("Gold", 3), ("Silver", 2), ("Bronze", 1)):
+                if words[-1] == medal:
+                    d[country] = d.get(country, 0) + sc
+
+    for key,val in d.items():
+        scores.append((val,key))
+    scores.sort(reverse=True)
+
+    return scores
+
+def main():
+    scores = charts()
+    print("Top 10 country for the whole period:")
+    for i in range(10):
+        print(scores[i][1])
+
+if __name__ == '__main__':
+    main()

+ 23 - 0
files/solutions/solution4.py

@@ -0,0 +1,23 @@
+from solution3 import *
+
+def country():
+    scores = charts()
+    d = {}
+    ls = []
+
+    f = open('countries.csv')
+    for line in f:
+        words = line.split(',')
+        d[words[1]] = words[2]
+    d.pop('Code')
+    for i in range(len(scores)):
+        for coun, pop in d.items():
+            if coun == scores[i][1]:
+                ls.append((scores[i][0]/int(pop), coun))
+
+    ls.sort(reverse=True)
+    print("The top 10 country:")
+    for i in range(10):
+        print(ls[i][1])
+
+country()

+ 9 - 0
files/solutions/solution5.py

@@ -0,0 +1,9 @@
+def increment_values(d):
+    """ (dict with number values) -> NoneType
+    Increase each value in d by 1.
+    """
+    dic = {}
+    for key,val in d.items():
+        val += 1
+        dic[key] = val
+    print(dic)

+ 14 - 0
files/solutions/solution6.py

@@ -0,0 +1,14 @@
+def express_checkout(product_to_quantity):
+    """ (dict of {str: int}) -> bool
+    Return True iff the grocery order in product_to_quantity qualifies for the
+    express checkout.  product_to_quantity maps products to the numbers of those
+    items in the grocery order.
+    >>> express_checkout({'banana': 3, 'soy milk': 1, 'peanut butter': 1})
+    True
+    >>> express_checkout({'banana': 3, 'soy milk': 1, 'twinkie': 5})
+    False
+    """
+    count = 0
+    for i in product_to_quantity.values():
+        count += i
+    return count <= 8

+ 37 - 0
files/solutions/solution7.py

@@ -0,0 +1,37 @@
+def build_username_to_mention_count(tweet_list):
+    """ (list of str) -> dict of {str: int}
+    Return a dictionary where each key is a username mentioned in tweet_list
+    and each value is the total number of times this username was mentioned in
+    all of the tweets in tweet_list.
+    >>> tweets = ['hi @me and @you', '@me yesterday', 'i saw @you', '@yo @you there']
+    >>> d = build_username_to_mention_count(tweets)
+    >>> d == {'yo': 1, 'me': 2, 'you': 3}
+    True
+    """
+    dic = {}
+    for line in tweet_list:
+        words = line.split()
+        for word in words:
+            if '@' in word:
+                name = word.split('@')
+                dic[name[1]] = dic.get(name[1],0)+1
+    return dic
+
+def most_mentioned_username(tweet_list):
+    """ (list of str) -> str
+    Preconditions:
+    - at least one of the strings in tweet_list will include a mention
+    - each username mentioned in tweet_list will have a unique total number
+    of mentions in tweet_list (i.e., no ties for the number of mentions)
+    Reuse previous function.
+    Return the username with the most mentions in tweet_list.
+    >>> tweets = ['hi @you there', '@me yesterday', 'saw @you']
+    >>> most_mentioned_username(tweets)
+    'you'
+    """
+    d = build_username_to_mention_count(tweet_list)
+    max_so_far = 0
+    for key,val in d.items():
+        if max_so_far == 0 or max_so_far[0] < val:
+            max_so_far = (val, key)
+    return max_so_far[1]