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 fdd91362e61ae72b598612eda443de793311ade9..0c07ac4a28a4b1ce42cb731953c55fb8ea5a1a59 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" ] },