Assignment 3 Part 4 (submitted by Varun on Feb 13, 20:31)

Beautiful Code
Ka-Ping Yee

AUTHORS:   Adam   Calvin   Chris   David   Derek   Hunter   Jacob   Jason   Jun   Karl   Kevin   Michael   Morgan   Nadia   Nerissa   Omair   Peter   Peterson   Ping   Richard   Scott   Thanh   Varun

Download this file.

COMMENTS

Feb 17, 09:36 - Jun (line 36): Very cool how you use a built in underscored function to check if a word is in the list or not. I would have probably built a function of my own to do the same thing you've done in one line... very nice.

Feb 17, 09:38 - Jun (line 39): the only thing I would change is that OPEN is defaulted to read-mode, so the extra "r" can be ommited

Feb 17, 12:23 - Ping (line 36): Actually, he didn't have to do this. __contains__ is a long way of saying in. The expression word in global_word_list would be equivalent.

Please log in if you would like to add comments.

   

  1 # Will have one global list which will store the words that are valid. It will read the words from file and keep adding the words in. 
  2 
  3 global_word_list = []
  4 
  5 def add(word):
  6         dict_file = open("dict.txt", "a")
  7         dict_file.write(word + "\n")
  8         global_word_list.append(word)
  9         print " added to word list " + str(global_word_list.__contains__(word))
 10         dict_file.close()
 11         global_word_list.sort()
 12 
 13 def remove(word):
 14         dict_file = open("dict.txt", "r")
 15         temp_file = open("temp.txt", "w")
 16         for line in dict_file:
 17                 current_word = line[:len(line)-1]       # removing the newline so that the word can be compared to the one in the file.
 18                 if word != current_word:
 19                         temp_file.write(line)
 20                 else:
 21                         5
 22         if global_word_list.__contains__(word):
 23                 global_word_list.remove(word)
 24         else:   
 25                 print word + " is not in the dictionary."
 26         dict_file.close()
 27         temp_file.close()
 28         dict_file = open("dict.txt", "w")
 29         temp_file = open("temp.txt", "r")
 30         for line in temp_file:
 31                 dict_file.write(line)   
 32         dict_file.close()
 33         temp_file.close()
 34 
 35 def check(word):
 36         return global_word_list.__contains__(word)
 37 
 38 def initialize():
 39         dict_file = open("dict.txt", "r")
 40         for line in dict_file:
 41                 global_word_list.append(line[:len(line) - 1])
 42         global_word_list.sort() 
 43         dict_file.close()