U E D R , A S I H C RSS

Classify By Anagram/재동

소스

~cpp 
import unittest

class AnagramTestCase(unittest.TestCase):
    def setUp(self):
        self.anagram = Anagram()
        
    def testCreationAnagram(self):
        self.assert_(self.anagram)

    def testSplitWord(self):
        expect = ['a','b','c']
        self.anagram.inputWord('abc')
        self.anagram.splitWord()
        self.assertEquals(expect, self.anagram.getSplitWordList())
        
    def testSortWord(self):
        expect = 'abc'
        self.anagram.inputWord('cba')
        self.anagram.splitWord()
        self.anagram.sortWord()
        self.assertEquals(expect, self.anagram.getSortWordString())

    def testIsWordListInAnagramList(self):
        expect1 = True
        expect2 = False
        
        self.anagram.inputWord('cba')
        self.anagram.splitWord()
        self.anagram.sortWord()
        self.anagram.setAnagramList([['abc','cba']])
        self.assertEquals(expect1, self.anagram.isWordListInAnagramList())
        self.anagram.inputWord('zzz')
        self.anagram.splitWord()
        self.anagram.sortWord()
        self.assertEquals(expect2, self.anagram.isWordListInAnagramList())
        
    def testStoreWord(self):
        expect = [['abc','cba']]
        self.anagram.inputWord('cba')
        self.anagram.process()
        self.assertEquals(expect, self.anagram.getStoreWord())

    def testAcceptence(self):
        expect = [['abc','cba','bac'],['aabb','abab']]
        self.anagram.inputWord('cba')
        self.anagram.process()
        self.anagram.inputWord('bac')
        self.anagram.process()
        self.anagram.inputWord('abab')
        self.anagram.process()
        self.assertEquals(expect, self.anagram.getStoreWord())
    
class Anagram:
    def __init__(self):
        self.anagramList = []

    def inputWord(self, word):
        self.wordString = word

    def splitWord(self):
        self.wordList = []
        for i in range(len(self.wordString)):
            self.wordList.append(self.wordString[i])
            
    def sortWord(self):
        self.wordList.sort()

    def isWordListInAnagramList(self):
        for i in range(len(self.anagramList)):
            if self.anagramList[i][0] == self.getSortWordString():
                self.index = i
                return True
        return False

    def setAnagramList(self,anagramList):
        self.anagramList = anagramList
            
    def storeWord(self):
        if self.isWordListInAnagramList() is True:
            self.anagramList[self.index].append(self.wordString)
        else:
            self.anagramList.append([self.getSortWordString()])
            self.anagramList[len(self.anagramList)-1].append(self.wordString)
            
    def process(self):
        self.splitWord()
        self.sortWord()
        self.storeWord()
        
    def getSplitWordList(self):
        return self.wordList
    
    def getSortWordString(self):
        tempString = ''
        for i in range(len(self.wordList)):
            tempString += self.wordList[i]
        return tempString
    
    def getStoreWord(self):
        return self.anagramList
    
    def printAnagramList(self):
        for i in range(len(self.anagramList)):
            for j in range(1,len(self.anagramList[i])):
                print self.anagramList[i][j],
            print
            
if __name__ == '__main__':
    #unittest.main()
    anagram = Anagram()
    while str:
        str = raw_input()
        anagram.inputWord(str)
        anagram.process()
    anagram.printAnagramList()

Thread

  • 우선은 빠르게 하는 거 생각하지 않고 그냥 생각나는 대로 짰습니다. 이제 이걸 토대로 '빠르게'를 생각해 보아야겠지요 --재동


Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:22:54
Processing time 0.0181 sec