Skip to content

Commit fe9d533

Browse files
author
unknown
committed
Modify to pythonic code
1 parent 8f5d482 commit fe9d533

File tree

1 file changed

+31
-36
lines changed

1 file changed

+31
-36
lines changed

dic.py

+31-36
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
11
# -*- coding: utf-8 -*-
2+
import sys
23

34
class MyDic:
45
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)
512

6-
self.dicBook = [[] for i in range(26)]
7-
f = open('words.txt','r')
813
words = f.read().lower().split()
914
words.sort()
10-
11-
for i in range(0,len(words)):
12-
self.dicBook[ord(words[i][0]) - 97].append(words[i])
1315
f.close()
1416

17+
for get in words:
18+
self.dicBook[get[0]].append(get)
19+
20+
1521
def start(self):
1622
menu =0
1723
while(menu !=6):
1824
menu = self.showMenu()
1925
if menu == 1:
2026
word = input("찾을 단어를 입력 해주세요 : ")
21-
self.showSearchResult(self.search(word))
27+
self.search(word)
2228
elif menu == 2:
2329
word = input("추가할 단어를 입력 해주세요 : ")
2430
self.insert(word)
@@ -40,65 +46,54 @@ def showMenu(self):
4046
print("1. Search 2. Insert 3. Delete 4. Save 5. select print 6. exit")
4147
return int(input("메뉴를 선택해 주세요 : "))
4248

43-
def showSearchResult(self,result):
44-
if result == -1:
45-
print("단어를 찾지 못하였습니다")
46-
return
47-
print("{}단어를 찾았습니다".format(result))
48-
49-
5049
def search(self, getWord):
51-
index = ord(getWord[0])-97
50+
index = getWord[0]
5251
tempList = self.dicBook[index]
5352

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+
5858

5959
def insert(self , getWord):
60-
if(self.search(getWord)!=-1):
60+
index = getWord[0]
61+
if getWord in self.dicBook[index]:
6162
print("원래 있는 단어입니다")
6263
else:
63-
index = ord(getWord[0])-97
6464
self.dicBook[index].append(getWord)
6565
self.dicBook[index].sort()
6666
print(self.dicBook[index])
6767
print("{} 단어 추가에 성공했습니다".format(getWord))
6868

6969
def deleteWord(self,getWord):
70-
if(self.search(getWord)==-1):
70+
index = getWord[0]
71+
if getWord not in self.dicBook[index]:
7172
print("단어가 존재하지 않습니다")
7273
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))
8176

8277
def saveDic(self):
8378
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)]))
8782
f.write("\n\n")
8883
f.close()
8984

9085
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)]))
9388

9489
f.close()
9590

9691
def selectPrint(self,getCh):
97-
if(len(getCh)>1):
92+
if(len(getCh) > 1):
9893
print("올바르지 않은 입력입니다")
9994
return
10095
print("{} : ".format(getCh))
101-
tempList= self.dicBook[ord(getCh)-97]
96+
tempList= self.dicBook[getCh]
10297
print(" ".join(tempList))
10398
print("\n\n")
10499

0 commit comments

Comments
 (0)