Matplotlib: Difference between revisions

998 bytes removed ,  11 years ago
imported>Jesstess
No edit summary
imported>Jesstess
Line 201:
==Bonus exercises==
 
=== 1. RandomLetter categoryfrequency cluesanalysis of the US Constitution ===
 
# Run <code>python constitution.py</code>. It will generate a bar chart showing the frequency of each letter in the alphabet in the US Constitution.
Write a script that randomly chooses a category and prints clues from that category.
# Open and read through <code>constitution.py</code>. The code for gathering and displaying the frequencies is a bit more complicated than the previous scripts in this projects, but try to trace the general strategy for plotting the data. Be sure to read the comments!
# Try to answer the following questions:
## On line 11, what is <code>string.ascii_lowercase</code>?
## On line 18, what is the purpose of <code>char = char.lower()</code>?
## What are the contents of <code>labels</code> after the <code>for</code> loop on line 30 completes?
## On line 41, what are the two arguments passed to <code>pyplot.xticks</code>
## On line 44, we use <code>pyplot.bar</code> instead of our usual <code>pyplot.plot</code>. What are the 3 arguments passed to <code>pyplot.bar</code>?
 
We've included a mystery text file <code>mystery.txt</code>: an excerpt from an actual novel. Alter <code>constitution.py</code> to process the data in <code>mystery.txt</code> instead of <code>constituation.txt</code>, and re-run the script. What do you notice that is odd about this file?
<br />
<b>tip</b>: SQL supports an "<code>ORDER BY RANDOM()</code>" clause that will return rows in a random order. For example, to randomly pick 1 category id you could use:
 
<pre>SELECT id FROM category ORDER BY RANDOM() LIMIT 1</pre>
 
You can also use <code>ORDER BY</code> to sort the clues by value.
 
<br />
<b>Example output</b>:
 
<pre>5 GUYS NAMED MOE
[$200] Last name of Moe of the Three Stooges
[$400] Moe Strauss founded this auto parts chain along with Manny Rosenfield &amp; Jack Jackson
[$600] Major league catcher Moe Berg was also a WWII spy for this agency, precursor of the CIA
[$800] Term for the type of country music Moe Bandy plays, the clubs where he began, or the &quot;Queen&quot; he sang of in 1981
[$1000] This &quot;Kool&quot; rapper's album &quot;How Ya Like Me Now&quot; began a rivalry with LL Cool J</pre>
 
<b>Exercises resources</b>:
<ul>
<li>
Using ORDER BY: http://www.w3schools.com/sql/sql_orderby.asp
</li>
</ul>
 
 
=== 2. Random game categories ===
 
Write a script to randomly choose a game number and print the categories from that game.
<br />
 
<b>tip</b>: the <code>category</code> table has <code>game</code> and <code>round</code> fields. round 0 is the Jeopardy round, round 1 is the Double Jeopardy round, and round 2 is Final Jeopardy.
 
<br />
 
<b>Example output</b>:
<pre>Categories for game #136:
0 WELCOME TO MY COUNTRY
0 METALS
0 GO GO GAUGUIN
0 FILE UNDER &quot;M&quot;
0 ANIMATED CATS
0 MAO MAO MAO MAO
1 SHAKESPEARE'S OPENING LINES
1 HEY, MARIO!
1 BRIDGE ON THE RIVER....
1 RUNNING MATES
1 13-LETTER WORDS
1 TONY BENNETT'S SONGBOOK
2 IN THE NEWS 2000</pre>
 
 
=== 3. Top 20 Jeopardy categories ===
 
Read about the <code>GROUP BY</code> clause and write a script using it to print the 20 most common Jeopardy categories.
 
An example of using <code>GROUP BY</code> and <code>ORDER BY</code> to produce an ordered list of counts on a hypothetical <code>foo</code> field is:
 
<pre>SELECT foo, COUNT(foo) AS count FROM my_table GROUP BY foo ORDER BY count</pre>
 
<b>Example output</b>:
<pre>81 LITERATURE
79 BEFORE &amp; AFTER
73 WORD ORIGINS
71 SCIENCE
64 BUSINESS &amp; INDUSTRY
63 AMERICAN HISTORY
...</pre>
 
===Congratulations!===
Anonymous user