def test_cake(self):
     self.assertEquals(
         collections.Counter(get_permutations("cake")),
         collections.Counter([
             "keca", "acek", "acke", "aeck", "aekc", "akce", "akec", "caek",
             "cake", "ceak", "ceka", "ckae", "ckea", "eack", "eakc", "ecak",
             "ecka", "ekac", "ekca", "kace", "kaec", "kcae", "kcea", "keac"
         ]))
예제 #2
0
def suggestions(string: str, edit_dist: int = 1) -> set:
    """Returns a set of permutations that are in the dictionary.

    :param string: String to perform permutations on.
    :param edit_dist: Maximum number of changes that can be done on the word.
    :return: Every permutation of the string that is in the dictionary.
    """
    return {
        perm
        for perm in permutations.get_permutations(string, edit_dist)
        if perm in dictionary
    }
예제 #3
0
def with_apostrophe(string: str, edit_dist: int = 1) -> set:
    """Returns a set of real permutations including ones with apostrophes.

    :param string: String to perform permutations on.
    :param edit_dist: Maximum number of changes that can be done on the word.
    :return: Every permutation that is available in the dictionary, including
    ones that end with 's, if within edit distance.
    """
    perms = permutations.get_permutations(string, edit_dist)
    perms.update({
        ''.join((perm[:len(perm) - 1], "'", perm[len(perm) - 1]))
        for perm in perms if perm.endswith('s') and len(perm) > 1
    })

    return {perm for perm in perms if perm in dictionary}
예제 #4
0
    def decrypt_message(self):
        '''
        Attempt to decrypt the encrypted message 
        
        Idea is to go through each permutation of the vowels and test it
        on the encrypted message. For each permutation, check how many
        words in the decrypted text are valid English words, and return
        the decrypted message with the most English words.
        
        If no good permutations are found (i.e. no permutations result in 
        at least 1 valid word), return the original string. If there are
        multiple permutations that yield the maximum number of words, return any
        one of them.

        Returns: the best decrypted message    
        
        Hint: use your function from Part 4A
        '''
        
        # list of permutations of vowels
        perms = get_permutations("aeiou")
        # dict to store decryptions of a message to later choose the best one
        decrypt = {}
        # going over each permutation
        for perm in perms:
            # keeping track of all the correct words
            counter = 0
            transpose_dict = SubMessage.build_transpose_dict(self, perm)
            message = SubMessage.apply_transpose(self, transpose_dict)
            # going over words built from one permutation
            for word in message.split():
                if is_word(self.valid_words, word):
                    counter += 1
            # key = counter and value = decrypted text
            decrypt[counter] = message
        # if not a single word is valid
        if max(decrypt.keys()) < 1:
            return self.message_text
        # return the decryption with the most correct words
        return decrypt[max(decrypt.keys())]
예제 #5
0
def test_permutations2():
  test2 = get_permutations('bc')
  assert len(test2) == 2 and set(test2) == {'bc', 'cb'}
예제 #6
0
def test_permutations1():
  test1 = get_permutations('a')
  assert len(test1) == 1 and set(test1) == {'a'}
예제 #7
0
def test_permutations5():
  test5 = get_permutations('noam')
  assert len(test5) == 24 and set(test5) == {'noam',  'onam',  'oanm',  'oamn',  'naom',  'anom',  'aonm',  'aomn',  'namo',  'anmo',
  'amno',  'amon',  'noma',  'onma',  'omna',  'oman',  'nmoa',  'mnoa',  'mona',  'moan',
   'nmao',  'mnao',  'mano',  'maon'}
예제 #8
0
def test_permutations4():
  test4 = get_permutations('zaz')
  assert len(test4) == 6 and test4.count('azz') == 2 and test4.count('zaz') == 2 and test4.count('zza') == 2
예제 #9
0
def test_permutations3():
  test3 = get_permutations('abc')
  assert len(test3) == 6 and set(test3) == {'abc', 'acb', 'bac', 'bca', 'cab', 'cba'}
 def test_empty_string(self):
     self.assertEquals(get_permutations(""), [""])
 def test_bob(self):
     self.assertEquals(collections.Counter(get_permutations("bob")),
                       collections.Counter(["bob", "bbo", "obb"]))
 def test_aaa(self):
     self.assertEquals(get_permutations("aaa"), ["aaa"])