From 3d631e9f0ca3f134e15034aa6a28c6edfec8b6cc Mon Sep 17 00:00:00 2001 From: ashwinmaran <amaran@wisc.edu> Date: Wed, 6 Mar 2024 11:10:43 -0600 Subject: [PATCH] lec18 solution update --- .../Lec_18_Dictionaries2_Solution.ipynb | 31 +++++++++++++------ 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/s24/AmFam_Ashwin/18_Dictionaries2/Lecture Code/Lec_18_Dictionaries2_Solution.ipynb b/s24/AmFam_Ashwin/18_Dictionaries2/Lecture Code/Lec_18_Dictionaries2_Solution.ipynb index fdd9136..0c07ac4 100644 --- a/s24/AmFam_Ashwin/18_Dictionaries2/Lecture Code/Lec_18_Dictionaries2_Solution.ipynb +++ b/s24/AmFam_Ashwin/18_Dictionaries2/Lecture Code/Lec_18_Dictionaries2_Solution.ipynb @@ -181,10 +181,20 @@ } ], "source": [ + "non_alpha_chars = [] # empty list\n", + "for word in english_dict:\n", + " word_def = english_dict[word]\n", + " for char in word_def:\n", + " if not char.isalpha():\n", + " non_alpha_chars.append(char) # append all non-alphabetical characters to list\n", + "non_alpha_chars = list(set(non_alpha_chars)) # remove duplicates\n", + "\n", "defs_with_or = 0\n", "for word in english_dict:\n", " word_def = english_dict[word]\n", - " if \"or\" in word_def.split(\" \"): # this is still imperfect; ideally, we should remove all non-alphabetical characters first\n", + " for char in non_alpha_chars: # loop through list of non-alphabetical characters\n", + " word_def = word_def.replace(char, \" \") # replace them with the whitespace character\n", + " if \"or\" in word_def.split(\" \"):\n", " defs_with_or += 1\n", "\n", "defs_with_or" @@ -44340,36 +44350,37 @@ "name": "stdin", "output_type": "stream", "text": [ - "enter a cmd (type \"help\" for descriptions): set bob 3\n", - "enter a cmd (type \"help\" for descriptions): get chang\n" + "enter a cmd (type \"help\" for descriptions): set bob 4\n", + "enter a cmd (type \"help\" for descriptions): get bob\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "unknown name\n" + "4\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ - "enter a cmd (type \"help\" for descriptions): get bob\n" + "enter a cmd (type \"help\" for descriptions): print\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "3\n" + "bob : 4\n", + "alice : 0\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ - "enter a cmd (type \"help\" for descriptions): set chang 5\n", + "enter a cmd (type \"help\" for descriptions): set chang 10\n", "enter a cmd (type \"help\" for descriptions): print\n" ] }, @@ -44377,9 +44388,9 @@ "name": "stdout", "output_type": "stream", "text": [ - "bob : 3\n", + "bob : 4\n", "alice : 0\n", - "chang : 5\n" + "chang : 10\n" ] }, { @@ -44400,7 +44411,7 @@ "name": "stdin", "output_type": "stream", "text": [ - "enter a cmd (type \"help\" for descriptions): set alice 5\n", + "enter a cmd (type \"help\" for descriptions): set alice 10\n", "enter a cmd (type \"help\" for descriptions): high\n" ] }, -- GitLab