Teach jenn coding with python
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

983 lines
29 KiB

11 months ago
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "id": "76cc981d",
  6. "metadata": {
  7. "tags": []
  8. },
  9. "source": [
  10. "# Wordle Master\n",
  11. "\n",
  12. "This notebook is meant ot teach you how to become a wordle master! In it we will run through various exercises in python to analyze words and do some simple analysis of the wordle dictionary and english words in general.\n",
  13. "\n",
  14. "The wordle dataset is from here: https://www.kaggle.com/bcruise/wordle-valid-words\n",
  15. "\n",
  16. "Let's get started with some python review!\n",
  17. "\n",
  18. "Strings are values which are wrapped in quotation marks, either single or double. Strings also act as lists - you can treat them as a sequence of individual characters which is really useful for analysis."
  19. ]
  20. },
  21. {
  22. "cell_type": "code",
  23. "execution_count": 1,
  24. "id": "ae40bab8",
  25. "metadata": {},
  26. "outputs": [
  27. {
  28. "name": "stdout",
  29. "output_type": "stream",
  30. "text": [
  31. "wordle\n"
  32. ]
  33. }
  34. ],
  35. "source": [
  36. "# Here is a variable called word which contains the string \"wordle\"\n",
  37. "name = \"wordle\"\n",
  38. "\n",
  39. "# You can print any value in python with the print() function\n",
  40. "print(name)"
  41. ]
  42. },
  43. {
  44. "cell_type": "code",
  45. "execution_count": 2,
  46. "id": "8accc4ba",
  47. "metadata": {
  48. "scrolled": true
  49. },
  50. "outputs": [
  51. {
  52. "name": "stdout",
  53. "output_type": "stream",
  54. "text": [
  55. "w\n"
  56. ]
  57. }
  58. ],
  59. "source": [
  60. "# Since strings are sequences we can index them to get individual letters.\n",
  61. "# Remember that indexing starts at 0\n",
  62. "first_letter = name[0]\n",
  63. "\n",
  64. "print(first_letter)"
  65. ]
  66. },
  67. {
  68. "cell_type": "markdown",
  69. "id": "18ac848a",
  70. "metadata": {},
  71. "source": [
  72. "### Exercise 1: Try and print the last letter of name using indexing"
  73. ]
  74. },
  75. {
  76. "cell_type": "code",
  77. "execution_count": 3,
  78. "id": "45e2c86e",
  79. "metadata": {},
  80. "outputs": [],
  81. "source": [
  82. "# Your code goes here"
  83. ]
  84. },
  85. {
  86. "cell_type": "markdown",
  87. "id": "e9ad82b7",
  88. "metadata": {},
  89. "source": [
  90. "## Indexing review\n",
  91. "\n",
  92. "Great job with that exercise! Indexing is a valuable tool when working with sequences and we'll be relying on it heavily in the rest of the module. \n",
  93. "\n",
  94. "Let's learn a little bit more about indexing. The first problem we'll look at is how to get the last character in a string programatically. Above since you know the value ahead of time you can simply count and hard code the index of the last character. But what if you're not working on data which a known size? Here's a small demo - enter your name in the box below:"
  95. ]
  96. },
  97. {
  98. "cell_type": "code",
  99. "execution_count": 4,
  100. "id": "107d44de-1303-47e7-aa8b-170803de5812",
  101. "metadata": {},
  102. "outputs": [
  103. {
  104. "name": "stdin",
  105. "output_type": "stream",
  106. "text": [
  107. " Christopher\n"
  108. ]
  109. }
  110. ],
  111. "source": [
  112. "student_name = input()"
  113. ]
  114. },
  115. {
  116. "cell_type": "code",
  117. "execution_count": 5,
  118. "id": "2b9c286e-0492-430e-914a-0125e975592c",
  119. "metadata": {},
  120. "outputs": [
  121. {
  122. "name": "stdout",
  123. "output_type": "stream",
  124. "text": [
  125. "11\n"
  126. ]
  127. }
  128. ],
  129. "source": [
  130. "length = len(student_name)\n",
  131. "print(length)"
  132. ]
  133. },
  134. {
  135. "cell_type": "markdown",
  136. "id": "fc123a72-0270-4371-b46e-34928fed1107",
  137. "metadata": {},
  138. "source": [
  139. "If you want to print the last character of a string you don't know the length of ahead of time you can use the length to index the last character"
  140. ]
  141. },
  142. {
  143. "cell_type": "code",
  144. "execution_count": 6,
  145. "id": "02d46132-d407-47c5-ac5f-f81a5154d8b7",
  146. "metadata": {},
  147. "outputs": [
  148. {
  149. "name": "stdout",
  150. "output_type": "stream",
  151. "text": [
  152. "r\n"
  153. ]
  154. }
  155. ],
  156. "source": [
  157. "last_index = length - 1\n",
  158. "print(student_name[last_index])"
  159. ]
  160. },
  161. {
  162. "cell_type": "markdown",
  163. "id": "aa2d7d9c-5c0c-460e-b692-cc7d15d31ebc",
  164. "metadata": {},
  165. "source": [
  166. "### Exercise 2: Why do we use `length - 1` for the index of the last character instead of `length`?"
  167. ]
  168. },
  169. {
  170. "cell_type": "markdown",
  171. "id": "016ece47-878f-409d-9100-356634677c2a",
  172. "metadata": {},
  173. "source": [
  174. "Answer: "
  175. ]
  176. },
  177. {
  178. "cell_type": "markdown",
  179. "id": "812f8a3d-4df6-439f-b6c2-252bcdfb308e",
  180. "metadata": {},
  181. "source": [
  182. "### One More Indexing Trick\n",
  183. "\n",
  184. "This code is powerful because it works for strings of any length, not just 6-letter strings. Python also has a more idomatic method of indexing the last character in a list."
  185. ]
  186. },
  187. {
  188. "cell_type": "code",
  189. "execution_count": 7,
  190. "id": "edd384f0-af77-41ad-a09e-5df0024041bb",
  191. "metadata": {},
  192. "outputs": [
  193. {
  194. "name": "stdout",
  195. "output_type": "stream",
  196. "text": [
  197. "r\n"
  198. ]
  199. }
  200. ],
  201. "source": [
  202. "print(student_name[-1])"
  203. ]
  204. },
  205. {
  206. "cell_type": "markdown",
  207. "id": "4ba81bcf-316a-47e0-addd-5c28c65bdfe7",
  208. "metadata": {},
  209. "source": [
  210. "Using negative indices starts from the end of a sequence and moves backwards, so `-2` is the second to last character and so on"
  211. ]
  212. },
  213. {
  214. "cell_type": "markdown",
  215. "id": "11957a5d-84fb-4f00-8c49-19f32567943c",
  216. "metadata": {},
  217. "source": [
  218. "## Iterating through sequences\n",
  219. "\n",
  220. "A very important part of working with data is iteration, or going through the items in a list one by one until you find one that you need or to do some analysis of each one. Let's examine a wordle letter by letter"
  221. ]
  222. },
  223. {
  224. "cell_type": "code",
  225. "execution_count": 8,
  226. "id": "f0de4d19-d951-42e7-9fb1-0d833aa5cfbb",
  227. "metadata": {},
  228. "outputs": [
  229. {
  230. "name": "stdout",
  231. "output_type": "stream",
  232. "text": [
  233. "c\n",
  234. "a\n",
  235. "r\n",
  236. "r\n",
  237. "y\n"
  238. ]
  239. }
  240. ],
  241. "source": [
  242. "wordle = \"carry\"\n",
  243. "\n",
  244. "for letter in wordle:\n",
  245. " print(letter)"
  246. ]
  247. },
  248. {
  249. "cell_type": "markdown",
  250. "id": "e86f7ae2-eacf-4418-ad2a-9918488b10d0",
  251. "metadata": {},
  252. "source": [
  253. "The syntax for a `for` loop is:\n",
  254. "\n",
  255. "```\n",
  256. "for NEW_VARIABLE in SEQUENCE:\n",
  257. " # Code which gets called once for each item in SEQUENCE with NEW_VARIBLE being updated to \n",
  258. " # the next item in the list after the block inside the for loop is run\n",
  259. "```\n",
  260. "\n",
  261. "`for` loops are great for whenever you want to run a bit of code for each element in a sequence. Lets do somehting more interesting interesting with the block of code!"
  262. ]
  263. },
  264. {
  265. "cell_type": "markdown",
  266. "id": "2c8c761a-1f40-4bda-8513-7f814cd7eac2",
  267. "metadata": {},
  268. "source": [
  269. "### Exercise 1: Print a message each time you see an R in the wordle\n",
  270. "\n",
  271. "Hint: use an if statement"
  272. ]
  273. },
  274. {
  275. "cell_type": "code",
  276. "execution_count": 9,
  277. "id": "dd2ca3a4-7477-46e2-ad02-3a2ed8319081",
  278. "metadata": {},
  279. "outputs": [],
  280. "source": [
  281. "for letter in wordle:\n",
  282. " # Your code goes here\n",
  283. " pass"
  284. ]
  285. },
  286. {
  287. "cell_type": "markdown",
  288. "id": "c825e624-b6f9-4a34-b3e9-2dacfd921ac9",
  289. "metadata": {},
  290. "source": [
  291. "### Exercise 2: Count how many R's you see in the wordle and print the result after the for loop\n",
  292. "\n",
  293. "Hint: This builds upon your previous exercise. Don't be afraid to create a new variable"
  294. ]
  295. },
  296. {
  297. "cell_type": "code",
  298. "execution_count": 10,
  299. "id": "c92147e9-0d55-43c0-a006-931fdafc70b6",
  300. "metadata": {},
  301. "outputs": [],
  302. "source": [
  303. "\n",
  304. "\n",
  305. "for letter in wordle:\n",
  306. " pass"
  307. ]
  308. },
  309. {
  310. "cell_type": "markdown",
  311. "id": "7736c145-d4e1-4c8d-be6d-b3ba6ef10250",
  312. "metadata": {},
  313. "source": [
  314. "### Advanced looping\n",
  315. "\n",
  316. "Brief reprieve: remember that loops can be broken out of with the `break` keyword. This is useful if you would just like to act on the first instance of something you find, you can just break after you act on it. This is useful for answering questions like \"Are there any R's in this word\". Since you only need to find one to answer that question, continuing to go through the rest of the word after you found on is redundant work and will save you time to stop as soon as possible."
  317. ]
  318. },
  319. {
  320. "cell_type": "markdown",
  321. "id": "8a123c63-f43c-4f4f-bafd-38342b09a55b",
  322. "metadata": {},
  323. "source": [
  324. "### Exercise 3: Print only one message for the first R in the wordle\n",
  325. "\n",
  326. "Hint: Copy your code from the first exercise and figure out how to modify it to only print once"
  327. ]
  328. },
  329. {
  330. "cell_type": "code",
  331. "execution_count": 11,
  332. "id": "3bebbabd-1d4d-4425-aed0-7be1c71187b2",
  333. "metadata": {},
  334. "outputs": [],
  335. "source": [
  336. "for letter in wordle:\n",
  337. " # Your code goes here\n",
  338. " pass"
  339. ]
  340. },
  341. {
  342. "cell_type": "markdown",
  343. "id": "9dd77f33-3563-452b-9988-2682bcf37c61",
  344. "metadata": {},
  345. "source": [
  346. "Did you know that if you just want to find if an element is in a list python has an easy shortcut for that?"
  347. ]
  348. },
  349. {
  350. "cell_type": "code",
  351. "execution_count": 12,
  352. "id": "7c51731e-567d-44e7-bd80-b40ec860f3bc",
  353. "metadata": {},
  354. "outputs": [
  355. {
  356. "name": "stdout",
  357. "output_type": "stream",
  358. "text": [
  359. "We have an R!\n"
  360. ]
  361. }
  362. ],
  363. "source": [
  364. "if 'r' in wordle:\n",
  365. " print(\"We have an R!\")\n",
  366. "else:\n",
  367. " print(\"No R in the wordle\")"
  368. ]
  369. },
  370. {
  371. "cell_type": "markdown",
  372. "id": "3681535e-8317-4cef-96de-c01517ea07c2",
  373. "metadata": {},
  374. "source": [
  375. "Neat right? Feel free to change the code above and play around with it. The in operator with if statements will be very useful later in this notebook so keep it in mind"
  376. ]
  377. },
  378. {
  379. "cell_type": "markdown",
  380. "id": "0a8e8603-1856-4d94-86f5-c2477cb5aedd",
  381. "metadata": {},
  382. "source": [
  383. "## List of lists\n",
  384. "\n",
  385. "A lot of data is multidimensional. If we have a list of words, to python that looks like a bunch of sequences inside one large sequence. We'll go through some simple examples to build some skills before we work with the full wordle dictionary\n"
  386. ]
  387. },
  388. {
  389. "cell_type": "code",
  390. "execution_count": 13,
  391. "id": "b0022962-fd8f-4d92-ab23-f0f0289d03d5",
  392. "metadata": {},
  393. "outputs": [],
  394. "source": [
  395. "dictionary = ['apple', 'butt', 'carp', 'dick']"
  396. ]
  397. },
  398. {
  399. "cell_type": "markdown",
  400. "id": "021c377d-ba82-4363-a1e8-934d44dbea63",
  401. "metadata": {},
  402. "source": [
  403. "If loop through dictionary with a for loop, each word will be accessed one after another.\n",
  404. "\n",
  405. "### Exercise 1: Print each word in the dictionary\n",
  406. "\n",
  407. "Hint: use a for loop"
  408. ]
  409. },
  410. {
  411. "cell_type": "code",
  412. "execution_count": 14,
  413. "id": "6bf8411e-bbaa-40b2-ab8b-4f7a1bc3f6c0",
  414. "metadata": {},
  415. "outputs": [],
  416. "source": [
  417. "# Your code here"
  418. ]
  419. },
  420. {
  421. "cell_type": "markdown",
  422. "id": "879cb3c6-ac0e-41a7-8865-23bb861dcb89",
  423. "metadata": {},
  424. "source": [
  425. "But now, how are you supposed to access the letters in each word now, if you wanted to process each letter, not each word? Easy! Use a for loop inside of your first for loop :)\n",
  426. "\n",
  427. "### Exercise 2: Print each letter in the dictionary (in order)\n",
  428. "\n",
  429. "Hint: your code from the previous answer should be very useful here"
  430. ]
  431. },
  432. {
  433. "cell_type": "code",
  434. "execution_count": 15,
  435. "id": "6288c1ef-e057-4b6c-b4ef-e0459fc55dc0",
  436. "metadata": {},
  437. "outputs": [],
  438. "source": [
  439. "# Your code here"
  440. ]
  441. },
  442. {
  443. "cell_type": "markdown",
  444. "id": "c064d435-2717-4296-98bf-802be9162cbe",
  445. "metadata": {},
  446. "source": [
  447. "### Multidimensional indexing\n",
  448. "\n",
  449. "Congratulations! You just did multidimensional indexing! What now? Multidimensional indexing is just a fancy way of saying we'll need to index a sequence which contains sequences multiple times to get one single item from our dictionary.\n",
  450. "\n",
  451. "Let's take a look at example:"
  452. ]
  453. },
  454. {
  455. "cell_type": "code",
  456. "execution_count": 16,
  457. "id": "eee51a96-90b3-44fa-b822-4a451f6a66e1",
  458. "metadata": {},
  459. "outputs": [
  460. {
  461. "name": "stdout",
  462. "output_type": "stream",
  463. "text": [
  464. "apple\n",
  465. "a\n"
  466. ]
  467. }
  468. ],
  469. "source": [
  470. "first_word = dictionary[0]\n",
  471. "first_letter_of_first_word = first_word[0]\n",
  472. "\n",
  473. "print(first_word)\n",
  474. "print(first_letter_of_first_word)"
  475. ]
  476. },
  477. {
  478. "cell_type": "markdown",
  479. "id": "202d4782-3fa6-4daa-81a5-ac82d2648773",
  480. "metadata": {},
  481. "source": [
  482. "Try modifying the example above to get the second letter of the first word, the second letter of the second word, or even the last letter of the last word. (Bonus points if you remember the trick from before)\n",
  483. "\n",
  484. "Here's another trick: indexing is an expression which returns a value. In the example above, we store that value in a temporary variable called `first_word`. We don't need to do that though, we can actually combine the indexing for a particular letter into one line without a temporary variable. Try playing around with the indexes below to get a feel for it."
  485. ]
  486. },
  487. {
  488. "cell_type": "code",
  489. "execution_count": 17,
  490. "id": "bf1a7976-12bf-47f1-a9d1-28ba4da14fec",
  491. "metadata": {},
  492. "outputs": [
  493. {
  494. "name": "stdout",
  495. "output_type": "stream",
  496. "text": [
  497. "a\n"
  498. ]
  499. }
  500. ],
  501. "source": [
  502. "a_cool_letter = dictionary[0][0]\n",
  503. "print(a_cool_letter)"
  504. ]
  505. },
  506. {
  507. "cell_type": "code",
  508. "execution_count": 18,
  509. "id": "269e56a4-ee9b-4875-9f27-19e5b1231bc8",
  510. "metadata": {},
  511. "outputs": [
  512. {
  513. "name": "stdout",
  514. "output_type": "stream",
  515. "text": [
  516. "a\n"
  517. ]
  518. }
  519. ],
  520. "source": [
  521. "# Technically we don't even need the letter variable either. As the programmer its up to you to decide how explicit\n",
  522. "# you want to write your code. There is no solution or \"right\" amount of explicitness\n",
  523. "print(dictionary[0][0])"
  524. ]
  525. },
  526. {
  527. "cell_type": "markdown",
  528. "id": "dc7d9799-a9e4-47d7-84ab-c70ad35126d6",
  529. "metadata": {},
  530. "source": [
  531. "## Indexing with numbers\n",
  532. "\n",
  533. "Instead of using a for loop to automatically go through each element in a sequence, occasionally it's useful to use the for loop to produce indices instead values. That's a lot of words which probably invokes a why but let's jump into an example to see why:"
  534. ]
  535. },
  536. {
  537. "cell_type": "code",
  538. "execution_count": 19,
  539. "id": "d3f9dceb-09ac-4bac-a127-4a2b1dd84b23",
  540. "metadata": {},
  541. "outputs": [
  542. {
  543. "name": "stdout",
  544. "output_type": "stream",
  545. "text": [
  546. "c\n",
  547. "a\n",
  548. "r\n",
  549. "r\n",
  550. "y\n"
  551. ]
  552. }
  553. ],
  554. "source": [
  555. "for index in range(5):\n",
  556. " print(wordle[index])"
  557. ]
  558. },
  559. {
  560. "cell_type": "markdown",
  561. "id": "dbc79aec-123b-4fa5-a675-d317f36d2611",
  562. "metadata": {},
  563. "source": [
  564. "Well that didn't explain anything but trust me it will in due time. Really this probably just looks like a complicated and annoying way to print each letter. Before we get into why this is useful I'd like to highlight that index is a variable that gets assigned the values 0-4 (If you like an illustration, feel free to add a `print(index)`). The indexing operator, `[]`, can take any expression, not just integer literals. These facts are why this works.\n",
  565. "\n",
  566. "Now to answer the question I've been avoiding. Why is this ever useful? It's a niche thing but it's useful if you ever want to go through multiple sequences in lock step at the same time. Modify the example above to print each guessed letter alongside the actual letter in that position in the wordle:"
  567. ]
  568. },
  569. {
  570. "cell_type": "code",
  571. "execution_count": 20,
  572. "id": "25e12f2b-89b1-45b9-b158-d6a627e691ee",
  573. "metadata": {},
  574. "outputs": [
  575. {
  576. "name": "stdout",
  577. "output_type": "stream",
  578. "text": [
  579. "c\n",
  580. "a\n",
  581. "r\n",
  582. "r\n",
  583. "y\n"
  584. ]
  585. }
  586. ],
  587. "source": [
  588. "# Here is a wordle guess a user input\n",
  589. "wordle_guess = \"carts\"\n",
  590. "\n",
  591. "for index in range(5):\n",
  592. " print(wordle[index])"
  593. ]
  594. },
  595. {
  596. "cell_type": "markdown",
  597. "id": "e98ca837-ee33-4a1d-9e96-918a894f3ba5",
  598. "metadata": {},
  599. "source": [
  600. "### Exercise 1: Print how many letter are in the correct position\n",
  601. "\n",
  602. "Building on top of your previous solution, if you have the guess's letter at position x and the wordle's letter at position x, now you can compare them and count how many are correct. This builds off of a lot of previous examples so don't be afraid to look back"
  603. ]
  604. },
  605. {
  606. "cell_type": "code",
  607. "execution_count": 21,
  608. "id": "0129cef2-97c7-4fb5-8b98-27663625d3d8",
  609. "metadata": {},
  610. "outputs": [],
  611. "source": [
  612. "# Your code goes here"
  613. ]
  614. },
  615. {
  616. "cell_type": "markdown",
  617. "id": "cd04271b-c0ee-4574-9909-e72d526c8c3e",
  618. "metadata": {},
  619. "source": [
  620. "## Validating data"
  621. ]
  622. },
  623. {
  624. "cell_type": "markdown",
  625. "id": "6ac64228-7bbb-4ec0-b0d5-a4032d2c681e",
  626. "metadata": {},
  627. "source": [
  628. "Imagine you didn't get the wordle dictionary from a reputible site and instead got the wordle dictionary with a bunch of extra crap in it. Data cleaning and validation is a huge part of data analysis. Answering questions like \"Is this a valid word\", \"Is it the right size\". Or for other datasets, \"Does every person have an email address or is anyone's email address empty\".\n",
  629. "\n",
  630. "Sometimes you just need answers: \"Is the data ok\". Other times you'll need to decide how you want to fix problems in your data.\n",
  631. "\n",
  632. "Let's start with a simple example. Your sketchy wordle dictionary has words that are too short and too long in it. A very simple solution to this problem is to go through every entry in the sketchy dictionary and add it to a \"known good wordles\" list if it's the appropriate size. Let's try this below:\n",
  633. "\n",
  634. "Hint: `.append()` is a method to add values to a list"
  635. ]
  636. },
  637. {
  638. "cell_type": "code",
  639. "execution_count": 22,
  640. "id": "45605090-89dc-4661-b617-decb32db9172",
  641. "metadata": {},
  642. "outputs": [
  643. {
  644. "name": "stdout",
  645. "output_type": "stream",
  646. "text": [
  647. "[]\n"
  648. ]
  649. }
  650. ],
  651. "source": [
  652. "sketchy_dictionary = [\"carts\", \"carry\", \"carriage\", \"ropes\", \"tights\", \"dude\", \"horse\"]\n",
  653. "validated = []\n",
  654. "\n",
  655. "# Your code goes here\n",
  656. "\n",
  657. " \n",
  658. "print(validated)"
  659. ]
  660. },
  661. {
  662. "cell_type": "markdown",
  663. "id": "34f6e526-1bd0-4bf5-ab3d-67646f7c86aa",
  664. "metadata": {},
  665. "source": [
  666. "Congrats! You successfully cleaned and validated that bad dictionary"
  667. ]
  668. },
  669. {
  670. "cell_type": "markdown",
  671. "id": "e1f46c02-9ba5-489e-9abc-9d6f5ab58749",
  672. "metadata": {},
  673. "source": [
  674. "## Analyzing some wordles\n",
  675. "\n",
  676. "Finally! The main event :) First thing's first, let's talk about CSV (comman seperated values) files. Our wordle datasets are stored in two different CSV files. Please take the opportunity to go look at them by clicking the folder icon on the sidebar and double clicking either of the two csv files to explore them. When you're done, click on Wordle Master tab to come back here.\n",
  677. "\n",
  678. "CSV files are a very simple \"spreadsheet\" format. They give you rows and columns. These files have one column called \"word\". If you were to open the file in a text editor, it would look like:\n",
  679. "\n",
  680. "```\n",
  681. "word,\n",
  682. "aback,\n",
  683. "abase,\n",
  684. "abate,\n",
  685. "abbey,\n",
  686. "abbot,\n",
  687. "abhor,\n",
  688. "abide,\n",
  689. "abled,\n",
  690. "abode,\n",
  691. "...\n",
  692. "```\n",
  693. "\n",
  694. "Each column in a CSV file is separated with a comma (Where it gets it's name from) and each row of the spreadsheet is separated with a newline character. It's a very simple text format but it's very easy to work with in any programming language and is therefore prety ubiquitous. Sometimes, but not all the times, the first row of a CSV file is a header row which provides a label for each column. If you are to process a CSV file it's important to know whether the first line is a header or part of the data so you can process it accordingly.\n",
  695. "\n",
  696. "Below is some python code which reads these csv files into multiple arrays, we can go through these after you take a look at them. See if you can take a guess as to what's happening:"
  697. ]
  698. },
  699. {
  700. "cell_type": "code",
  701. "execution_count": 23,
  702. "id": "89f2ada2-a7bf-4423-a279-32a07858a387",
  703. "metadata": {},
  704. "outputs": [],
  705. "source": [
  706. "valid_solutions = []\n",
  707. "\n",
  708. "import csv\n",
  709. "\n",
  710. "with open(\"valid_solutions.csv\", \"r\") as csvfile:\n",
  711. " csvreader = csv.reader(csvfile)\n",
  712. "\n",
  713. " # This skips the first header row of the CSV file.\n",
  714. " next(csvreader)\n",
  715. " \n",
  716. " for row in csvreader:\n",
  717. " valid_solutions.append(row[0])"
  718. ]
  719. },
  720. {
  721. "cell_type": "code",
  722. "execution_count": 24,
  723. "id": "270f638c-6aa0-4909-8e50-9fd8beda06d5",
  724. "metadata": {},
  725. "outputs": [],
  726. "source": [
  727. "valid_guesses = []\n",
  728. "\n",
  729. "import csv\n",
  730. "\n",
  731. "with open(\"valid_guesses.csv\", \"r\") as csvfile:\n",
  732. " csvreader = csv.reader(csvfile)\n",
  733. "\n",
  734. " # This skips the first header row of the CSV file.\n",
  735. " next(csvreader)\n",
  736. " \n",
  737. " for row in csvreader:\n",
  738. " valid_guesses.append(row[0])"
  739. ]
  740. },
  741. {
  742. "cell_type": "markdown",
  743. "id": "58e93d6c-4468-4cef-978a-485f1c812482",
  744. "metadata": {},
  745. "source": [
  746. "Now the variables `valid_solutions` and `valid_guesses` are usable in other cells below! The block of code is pretty straightforward but introduces a lot of new things we won't dive into too much in this module. All this code does is use the built in python csv library to read a CSV file (opened with the `open` function). We use that reader to read each entry from the first column of each row into a list called `valid_guesses` and `valid_solutions`.\n",
  747. "\n",
  748. "Wordle has two word lists, one for all possible valid guesses (every 5 letter word in the dictionary) and a hand curated one from which the solution is chosen (so the player doesn't feel gypped because of a esoteric word they never heard of before. Since our word lists are in python lists now, lets use our data processing skills to see how long is each dictionary, and then after you figure that out, determine how many more words there are in the `guesses` list than there are in the `solutions` list:"
  749. ]
  750. },
  751. {
  752. "cell_type": "code",
  753. "execution_count": 25,
  754. "id": "9082a2e7-5737-49f0-8fc2-b175f11cf3b6",
  755. "metadata": {},
  756. "outputs": [],
  757. "source": [
  758. "# Your code goes here"
  759. ]
  760. },
  761. {
  762. "cell_type": "markdown",
  763. "id": "29212900-28d3-43c5-86cf-fa43a3acd92d",
  764. "metadata": {},
  765. "source": [
  766. "Look at you! Answering interesting questions about the wordle dataset and we've barely got started! Let's try something harder!\n",
  767. "\n",
  768. "### Exercise 1: Count how many solutions start with the letter A"
  769. ]
  770. },
  771. {
  772. "cell_type": "code",
  773. "execution_count": 26,
  774. "id": "773df9e2-523d-4f6b-9702-54c83a104a73",
  775. "metadata": {},
  776. "outputs": [],
  777. "source": [
  778. "# Your code goes here"
  779. ]
  780. },
  781. {
  782. "cell_type": "markdown",
  783. "id": "ccf344b4-8d73-43d0-9833-474662e5eef5",
  784. "metadata": {},
  785. "source": [
  786. "### Exercise 2: Count how many solutions contain an A"
  787. ]
  788. },
  789. {
  790. "cell_type": "code",
  791. "execution_count": 27,
  792. "id": "df333e84-0fdc-40dd-8976-7c15e82fb913",
  793. "metadata": {},
  794. "outputs": [],
  795. "source": [
  796. "# Your code goes here"
  797. ]
  798. },
  799. {
  800. "cell_type": "markdown",
  801. "id": "2db3b332-9e6a-47fe-a444-56f2f054f837",
  802. "metadata": {},
  803. "source": [
  804. "### Exercise 3: Pick a wordle from the solutions list at random\n",
  805. "\n",
  806. "Hint: You've done this before but this is a good time to break out google and see if you can figure it out again if you forgot ;)"
  807. ]
  808. },
  809. {
  810. "cell_type": "code",
  811. "execution_count": 28,
  812. "id": "c544cc7d-89ea-4df0-996f-d18bf76e6b53",
  813. "metadata": {},
  814. "outputs": [],
  815. "source": [
  816. "# Your code goes here"
  817. ]
  818. },
  819. {
  820. "cell_type": "markdown",
  821. "id": "34b52e3f-aca0-4efe-8713-e3d7000963b2",
  822. "metadata": {},
  823. "source": [
  824. "### Exercise 4: Determine if a given word is in the solution list\n",
  825. "\n",
  826. "Hint: There's an operator we've used before in this module which let's you test to see if a value is contained in a given sequence. If you can't find it or remember it there's always the trusty for loop"
  827. ]
  828. },
  829. {
  830. "cell_type": "code",
  831. "execution_count": 29,
  832. "id": "4414c453-d867-40f0-a95d-8a7ced923e45",
  833. "metadata": {},
  834. "outputs": [],
  835. "source": [
  836. "is_valid_solution1 = \"grass\"\n",
  837. "is_valid_solution2 = \"aiyee\"\n",
  838. "\n",
  839. "# Your code goes here"
  840. ]
  841. },
  842. {
  843. "cell_type": "markdown",
  844. "id": "e53344a6-e612-451e-9757-4d76f4f56d54",
  845. "metadata": {},
  846. "source": [
  847. "### Exercise 5: Print all the words that start with \"sh\"\n",
  848. "\n",
  849. "This one is the first exercise which has a bit of practical value for you. If you have a word that starts with a green 'sh', this will let you see what they are"
  850. ]
  851. },
  852. {
  853. "cell_type": "code",
  854. "execution_count": 30,
  855. "id": "0b2a35f6-2e52-4308-af55-cc10b03a5ad9",
  856. "metadata": {},
  857. "outputs": [],
  858. "source": [
  859. "### Your code goes here"
  860. ]
  861. },
  862. {
  863. "cell_type": "markdown",
  864. "id": "bee81005-a11f-4857-8707-a27eaf14cd4e",
  865. "metadata": {},
  866. "source": [
  867. "### Aside about comparing more than just one letter\n",
  868. "\n",
  869. "Slicing a list is something which goes handing in hand with indexing but we have not touched on it yet. If you remember, slicing is a way to return a subset of a list. This can make a problem like the one above easier"
  870. ]
  871. },
  872. {
  873. "cell_type": "code",
  874. "execution_count": 31,
  875. "id": "1b661897-031f-4781-9cdf-4b8942f2279a",
  876. "metadata": {},
  877. "outputs": [
  878. {
  879. "name": "stdout",
  880. "output_type": "stream",
  881. "text": [
  882. "s\n",
  883. "sh\n",
  884. "sh\n",
  885. "\n",
  886. "ell\n",
  887. "ell\n"
  888. ]
  889. }
  890. ],
  891. "source": [
  892. "to_slice = \"shell\"\n",
  893. "\n",
  894. "print(to_slice[0:1])\n",
  895. "print(to_slice[0:2])\n",
  896. "print(to_slice[:2])\n",
  897. "print()\n",
  898. "print(to_slice[2:5])\n",
  899. "print(to_slice[2:])"
  900. ]
  901. },
  902. {
  903. "cell_type": "markdown",
  904. "id": "7876848f-61e6-41bb-957c-ac3e063e92e4",
  905. "metadata": {},
  906. "source": [
  907. "Play around with the indicies above and try and get a feel for it! What happens if the starting index is after the ending index? What happens if the starting index is negative and you don't provide an ending index? Can you use two negative indexes? Can you use negative indexes to the the last two characters of the word?\n",
  908. "\n",
  909. "As a refresher, the slicing syntax appears inside the indexing operator and instructs python to return a subset of the list where the number before the colon is the starting index and the number after the colon is where python goes up to but doesn't include. If the starting number is left out, that means start from the beginning and go to the ending index. If the ending index is left out that means start at the starting index and go to the end. If they are both left out, it returns a copy of the list from the beginning to the end. Not the most useful thing but possible.\n",
  910. "\n",
  911. "The reason I bring all of this up is because of the previous exercise. An easy way to get the first two letters is with slicing. Take a look at the solution to Exercise 5 below:"
  912. ]
  913. },
  914. {
  915. "cell_type": "code",
  916. "execution_count": 32,
  917. "id": "183fdc2a-1110-486f-b134-6b9d6cd7ddbf",
  918. "metadata": {},
  919. "outputs": [
  920. {
  921. "name": "stdout",
  922. "output_type": "stream",
  923. "text": [
  924. "shack\n",
  925. "shade\n",
  926. "shady\n",
  927. "shaft\n",
  928. "shake\n",
  929. "shaky\n",
  930. "shale\n",
  931. "shall\n",
  932. "shalt\n",
  933. "shame\n",
  934. "shank\n",
  935. "shape\n",
  936. "shard\n",
  937. "share\n",
  938. "shark\n",
  939. "sharp\n",
  940. "shave\n",
  941. "shawl\n"
  942. ]
  943. }
  944. ],
  945. "source": [
  946. "for solution in valid_solutions:\n",
  947. " if solution[:3] == \"sha\":\n",
  948. " print(solution)"
  949. ]
  950. },
  951. {
  952. "cell_type": "markdown",
  953. "id": "5685b7ba-eca7-427a-b036-564c48c49fa2",
  954. "metadata": {},
  955. "source": [
  956. "The alternative would be writing a much larger if statement with and and explicit indexing. This is a fairly compact and readable solution, so long as the reader understands slicing syntax."
  957. ]
  958. }
  959. ],
  960. "metadata": {
  961. "kernelspec": {
  962. "display_name": "Python 3 (ipykernel)",
  963. "language": "python",
  964. "name": "python3"
  965. },
  966. "language_info": {
  967. "codemirror_mode": {
  968. "name": "ipython",
  969. "version": 3
  970. },
  971. "file_extension": ".py",
  972. "mimetype": "text/x-python",
  973. "name": "python",
  974. "nbconvert_exporter": "python",
  975. "pygments_lexer": "ipython3",
  976. "version": "3.10.1"
  977. },
  978. "toc-autonumbering": true,
  979. "toc-showmarkdowntxt": false
  980. },
  981. "nbformat": 4,
  982. "nbformat_minor": 5
  983. }