thatmattbone / twitiwt

A twitter bot for detecting palindromes.

Clone this repository (size: 16.8 KB): HTTPS / SSH
$ hg clone http://bitbucket.org/thatmattbone/twitiwt/
commit 4: 73f1c4dba5a6
parent 3: a83a9173320f
branch: default
adding palindrome and sarahpalindrome generator, first randomized test
thatmattbone
8 weeks ago

Changed (Δ1.5 KB):

raw changeset »

palindrome.py (13 lines added, 0 lines removed)

tests.py (40 lines added, 0 lines removed)

Up to file-list palindrome.py:

@@ -52,6 +52,7 @@ def palindrome_sentence(sentence):
52
52
    formatted_sentence = format_string(sentence)
53
53
    pass
54
54
55
55
56
def palindrome_list(sentence):
56
57
    """Split a sentence into words and test each word individually
57
58
    to determine whether or not it is a palindrome.  
@@ -61,3 +62,15 @@ def palindrome_list(sentence):
61
62
    words = [word.strip() for word in sentence.split()]
62
63
    palindrome_list = [palindrome(word) for word in words]
63
64
    return [w for w in palindrome_list if w!=False]
65
66
67
def reverse_and_compare(word):
68
    """First attempt at sarahpalindrome detection."""
69
    forwards = list(word)
70
    backwards = list(word)
71
    backwards.reverse()
72
73
    zipped = zip(forwards, backwards)
74
    print zipped    
75
76
    return [x[0] == x[1] for x in zipped]

Up to file-list tests.py:

1
1
# -*- coding: utf-8 -*-
2
2
3
3
import unittest
4
import random
4
5
from palindrome import formatted_compare, format_string, palindrome, palindrome_list, palindrome_sentence
5
6
7
letters = 'abcdefghijklmnopqrstuvwxyz'
8
def palindrome_generator():
9
    letter_list = list(letters)
10
    
11
    while(True):
12
        p_length = random.randint(2, 10)
13
        palindrome_forward = random.sample(letter_list, p_length)
14
        palindrome_backward = list(palindrome_forward)
15
        palindrome_backward.reverse()
16
        yield "%s%s" % ("".join(palindrome_forward), "".join(palindrome_backward))
17
18
19
def sarahpalindrome_generator(no_palindromes=True):
20
    gen = palindrome_generator()
21
    for palindrome in palindrome_generator():
22
        letter_list = list(palindrome)
23
        swapme = random.randint(0, len(letter_list)-2)
24
        l1 = letter_list[swapme]
25
        l2 = letter_list[swapme+1]
26
        letter_list[swapme] = l2
27
        letter_list[swapme+1] = l1
28
        possible_palindrome = "".join(letter_list)
29
        
30
        if(no_palindromes and formatted_compare(possible_palindrome)):
31
            continue
32
33
        yield possible_palindrome
34
35
6
36
class TestPalindrome(unittest.TestCase):
7
37
8
38
    def setUp(self):
@@ -99,6 +129,16 @@ Not to care…
99
129
100
130
        self.assertTrue(palindrome_sentence("""Race fast, safe car.""") != False)
101
131
132
class TestPalindrome(unittest.TestCase):
133
134
    def testRandomPalindromes(self):
135
        count = 0
136
        gen = palindrome_generator()
137
        while(count<1000):
138
            word = gen.next()
139
            #returns word if it is a palindrome
140
            self.assertTrue(formatted_compare(word) == word)
141
            count += 1
102
142
103
143
if __name__ == '__main__':
104
144
    unittest.main()