1
1
# -*- coding: utf-8 -*-
2
+ import sys
2
3
3
4
class MyDic :
4
5
def __init__ (self ):
6
+ self .dicBook = {chr (alpa ):[] for alpa in range (97 ,123 )}
7
+ try :
8
+ f = open ('words.txt' ,'r' )
9
+ except Exception :
10
+ print ("파일을 열 수 없습니다" )
11
+ sys .exit (- 1 )
5
12
6
- self .dicBook = [[] for i in range (26 )]
7
- f = open ('words.txt' ,'r' )
8
13
words = f .read ().lower ().split ()
9
14
words .sort ()
10
-
11
- for i in range (0 ,len (words )):
12
- self .dicBook [ord (words [i ][0 ]) - 97 ].append (words [i ])
13
15
f .close ()
14
16
17
+ for get in words :
18
+ self .dicBook [get [0 ]].append (get )
19
+
20
+
15
21
def start (self ):
16
22
menu = 0
17
23
while (menu != 6 ):
18
24
menu = self .showMenu ()
19
25
if menu == 1 :
20
26
word = input ("찾을 단어를 입력 해주세요 : " )
21
- self .showSearchResult ( self . search (word ) )
27
+ self .search (word )
22
28
elif menu == 2 :
23
29
word = input ("추가할 단어를 입력 해주세요 : " )
24
30
self .insert (word )
@@ -40,65 +46,54 @@ def showMenu(self):
40
46
print ("1. Search 2. Insert 3. Delete 4. Save 5. select print 6. exit" )
41
47
return int (input ("메뉴를 선택해 주세요 : " ))
42
48
43
- def showSearchResult (self ,result ):
44
- if result == - 1 :
45
- print ("단어를 찾지 못하였습니다" )
46
- return
47
- print ("{}단어를 찾았습니다" .format (result ))
48
-
49
-
50
49
def search (self , getWord ):
51
- index = ord ( getWord [0 ]) - 97
50
+ index = getWord [0 ]
52
51
tempList = self .dicBook [index ]
53
52
54
- for word in tempList :
55
- if word == getWord :
56
- return word
57
- return - 1
53
+ if getWord in self .dicBook [index ]:
54
+ print ("{}단어를 찾았습니다" .format (getWord ))
55
+ else :
56
+ print ("단어를 찾지 못하였습니다" )
57
+
58
58
59
59
def insert (self , getWord ):
60
- if (self .search (getWord )!= - 1 ):
60
+ index = getWord [0 ]
61
+ if getWord in self .dicBook [index ]:
61
62
print ("원래 있는 단어입니다" )
62
63
else :
63
- index = ord (getWord [0 ])- 97
64
64
self .dicBook [index ].append (getWord )
65
65
self .dicBook [index ].sort ()
66
66
print (self .dicBook [index ])
67
67
print ("{} 단어 추가에 성공했습니다" .format (getWord ))
68
68
69
69
def deleteWord (self ,getWord ):
70
- if (self .search (getWord )== - 1 ):
70
+ index = getWord [0 ]
71
+ if getWord not in self .dicBook [index ]:
71
72
print ("단어가 존재하지 않습니다" )
72
73
else :
73
- index = ord (getWord [0 ]) - 97
74
- tempList = self .dicBook [index ]
75
- for wordInfo in enumerate (tempList ):
76
- if wordInfo [1 ] == getWord :
77
- del self .dicBook [index ][wordInfo [0 ]]
78
- print ("{}번째 원소 삭제에 성공했습니다" .format (wordInfo [0 ]))
79
- print (self .dicBook [index ])
80
- break
74
+ self .dicBook .get (index ).remove (getWord )
75
+ print ("{} 원소 삭제에 성공했습니다" .format (getWord ))
81
76
82
77
def saveDic (self ):
83
78
f = open ("ResultList.txt" ,"w" )
84
- for i in range (26 ):
85
- f .write ("{} : " .format (chr (i + 97 )))
86
- f .write (" " .join (self .dicBook [i ]))
79
+ for i in range (97 , 123 ):
80
+ f .write ("{} : " .format (chr (i )))
81
+ f .write (" " .join (self .dicBook [chr ( i ) ]))
87
82
f .write ("\n \n " )
88
83
f .close ()
89
84
90
85
f = open ("words.txt" ,"w" )
91
- for i in range (26 ):
92
- f .write (" " .join (self .dicBook [i ]))
86
+ for i in range (97 , 123 ):
87
+ f .write (" " .join (self .dicBook [chr ( i ) ]))
93
88
94
89
f .close ()
95
90
96
91
def selectPrint (self ,getCh ):
97
- if (len (getCh )> 1 ):
92
+ if (len (getCh ) > 1 ):
98
93
print ("올바르지 않은 입력입니다" )
99
94
return
100
95
print ("{} : " .format (getCh ))
101
- tempList = self .dicBook [ord ( getCh ) - 97 ]
96
+ tempList = self .dicBook [getCh ]
102
97
print (" " .join (tempList ))
103
98
print ("\n \n " )
104
99
0 commit comments