コード例 #1
0
    def test_non_empty_string(self):
        """Test for palindrome in non empty string"""

        store = []
        expected_store = ["madam", "Level", "Brian"]
        arg1 = "madam"
        actual = is_palindrome(arg1, store)
        expected = True
        message = "Expected {}, but returned {}".format(expected, actual)
        self.assertEqual(actual, expected, message)

        arg2 = "Level"
        actual = is_palindrome(arg2, store)
        expected = True
        message = "Expected {}, but returned {}".format(expected, actual)
        self.assertEqual(actual, expected, message)

        arg3 = "Brian"
        actual = is_palindrome(arg3, store)
        expected = False
        message = "Expected {}, but returned {}".format(expected, actual)
        self.assertEqual(actual, expected)
        message = "Expected {} as record, but returned {}".format(
            expected_store, store)
        self.assertEqual(store, expected_store, message)
コード例 #2
0
def sum_double_base_palindrome(num):
    sum = 0
    for i in range(num+1):
        if palindrome.is_palindrome(i,10) and palindrome.is_palindrome(i,2):
           sum += i

    return sum
コード例 #3
0
    def test_return_true_if_palindrome(self):
        expected = True

        result1 = p.is_palindrome(self.example2)
        result2 = p.is_palindrome(self.example1)

        self.assertEqual(expected, result1)
        self.assertEqual(expected, result2)
コード例 #4
0
    def test_palindromes(self):
        otto = 'otto'
        hannah = 'hannah'
        tacocat = 'tacocat'

        self.assertTrue(is_palindrome(otto))
        self.assertTrue(is_palindrome(hannah))
        self.assertTrue(is_palindrome(tacocat))
コード例 #5
0
def test_is_string():
    """
    Test Requirement: `is_palindrome` raises a `ValueError` when not provided with a value
      that is  an instance of `str`.
    """

    with pytest.raises(ValueError):
        is_palindrome(1881)
コード例 #6
0
def p_score(n):
    score = 1
    if is_palindrome(n):
        return score
    else:
        while is_palindrome(n) == False:
            score += 1
            n = int(reverse_obj(n)) + n
    return score
コード例 #7
0
ファイル: 036.py プロジェクト: mackorone/euler
def ans():
    sum_ = 0
    for i in range(1000000):
        if (
            is_palindrome(str(i)) and
            is_palindrome("{0:b}".format(i))
        ):
            sum_ += i
    return sum_
コード例 #8
0
def test_basic():
    """ Basic test for palindrome """

    # True positives
    for test in ('Rotator','bob','madam','mAlAyAlam', '1'):
        assert palindrome.is_palindrome(test)==True

    # True negatives
    for test in ('xyz','elephant', 'Country'):
        assert palindrome.is_palindrome(test)==False        
コード例 #9
0
    def test_last_five_records(self):
        """Test for keeping of record of last five entries
        """

        store = ["a", "b", "Hello", "try", "level"]
        expected_store = ["b", "Hello", "try", "level", "bayesian nets"]
        arg1 = "bayesian nets"
        is_palindrome(arg1, store)
        message = "Expected {} as record, but returned {}".format(
            expected_store, store)
        self.assertEqual(store, expected_store, message)
コード例 #10
0
def test_with_spaces():
    """ Testing palindrome strings with extra spaces """

    # True positives
    for test in ('Able was I ere I saw Elba', 'Madam Im Adam',
                 'Step on no pets', 'Top spot'):
        assert palindrome.is_palindrome(test) == True

    # True negatives
    for test in ('Top post', 'Wonderful fool', 'Wild Imagination'):
        assert palindrome.is_palindrome(test) == False
コード例 #11
0
def test_with_punctuations():
    """ Testing palindrome strings with extra punctuations """

    # True positives
    for test in ('Able was I, ere I saw Elba',
                 "Madam I'm Adam",
                 'Step on no pets.',
                 'Top spot!'):
        assert palindrome.is_palindrome(test)==True

    # True negatives
    for test in ('Top . post','Wonderful-fool','Wild Imagination!!'):
        assert palindrome.is_palindrome(test)==False            
コード例 #12
0
def next_hack(n):
    while True:
        n += 1
        binary = bin_num_to_str(n)
        if is_palindrome(binary) and is_odd_one(n):
            break
    return n
コード例 #13
0
    def test_with_palindromes_and_non_palindromes(self):
        """is_palindrome returns True for a palindrome and False for a non-palindrome. (1p)"""
        palindromes = ("dad", "121", "GAATTCCTTAAG",
                       "rats live on no evil star")
        for palindrome in palindromes:
            self.assertTrue(
                is_palindrome(palindrome),
                "{0!r} is a palindrome but your function says it is not.".
                format(palindrome))

        not_palindromes = ("aabb", "12", "da", "certainly not a palindrome")
        for not_palindrome in not_palindromes:
            self.assertFalse(
                is_palindrome(not_palindrome),
                "{0!r} is not a palindrome but your function says it is.".
                format(not_palindrome))
コード例 #14
0
def test_this_sentence_is_looooong():
    """
    `(/1)` `is_palindrome` returns `True` when called with `"Able was I ere I saw
      Elba"`.
    """

    assert is_palindrome("Able was I ere I saw Elba") is True
コード例 #15
0
def main():
	for line in open('words.txt'):
		#remove whitespace from the beginning and end
		word = line.strip()
		#only print palindromes
		if is_palindrome(word):
			print word
コード例 #16
0
def ans():
    largest = 0
    for x in range(100, 999):
        for y in range(x, 999):
            product = x * y
            if is_palindrome(str(product)) and largest < product:
                largest = product
    return largest
コード例 #17
0
 def test_is_palindrome_with_mirrored_strings(self):
     # palindromes that are perfectly mirrored strings
     assert is_palindrome('') is True  # base case
     assert is_palindrome('A') is True  # base case
     assert is_palindrome('BB') is True  # even length
     assert is_palindrome('LOL') is True  # odd length
     assert is_palindrome('noon') is True
     assert is_palindrome('radar') is True
     assert is_palindrome('doggod') is True
     assert is_palindrome('racecar') is True
コード例 #18
0
 def test_is_palindrome_with_mixed_casing_and_punctuation(self):
     # palindromes with whitespace, punctuation and mixed letter casing
     assert is_palindrome('No, On!') is True
     assert is_palindrome('Dog God?') is True
     assert is_palindrome('Taco? Cat.') is True
     assert is_palindrome('Race-Car!!!') is True
     assert is_palindrome('Race Fast, Safe Car...') is True
     assert is_palindrome('Was it a car or a cat I saw?') is True
     assert is_palindrome("Go hang a salami, I'm a lasagna hog.") is True
     assert is_palindrome('A man, a plan, a canal - Panama!') is True
コード例 #19
0
def test_is_palindrome_ShouldRetrunFalseIfEmpty():
    # arrange
    sut = LinkedList()

    # act
    result = is_palindrome(sut)

    # assert
    assert result == False
コード例 #20
0
ファイル: problem004.py プロジェクト: jeek/projecteuler
def problem004(digits=3):
    """Brute force the search space."""
    answers = set()
    for i in range(10 ** (digits - 1), 10 ** digits):
        for j in range(i, 10 ** digits):
            answers.add(i * j)
    for i in sorted(list(answers), key=lambda score: -score):
        if is_palindrome(i):
            return i
コード例 #21
0
def main():
    for line in open('words.txt'):

        # remove whitespace from the beginning and end
        word = line.strip()

        # only print palindromes
        if is_palindrome(word):
            print word
コード例 #22
0
ファイル: 055.py プロジェクト: mackorone/euler
def is_lychrel(n):
    iterations = 0
    while True:
        n += int(''.join(reversed(str(n))))
        iterations += 1
        if is_palindrome(str(n)):
            return False
        if 50 < iterations:
            return True
コード例 #23
0
ファイル: euler_4.py プロジェクト: shivekkhurana/learning
def lar_3_palindrome():
    d3 = list(range(100, 1000))
    d3 = list(permutations(d3, 2))
    a = 0
    for elem in d3:
        n = elem[0] * elem[1]
        if is_palindrome(n):
            if n > a:
                a = n
    return a
コード例 #24
0
ファイル: euler_4.py プロジェクト: shivekkhurana/learning
def lar_3_palindrome():
	d3 = list(range(100,1000))
	d3 = list(permutations(d3,2))
	a = 0
	for elem in d3:
		n = elem[0] * elem[1]
		if is_palindrome(n):
			if n>a:
				a = n
	return a
コード例 #25
0
ファイル: test_palindrome.py プロジェクト: AAAleg/learning
    def test_palindrome(self):
        strings = {
            'dovod': True,
            'nedovod': False,
            'parrap': True,
            'neparrap': False
        }

        for string, prediction in strings.items():
            self.assertEqual(is_palindrome(string), prediction)
コード例 #26
0
 def test_is_palindrome(self):
     test_cases = [("redivider", True), ("deified", True), ("civic", True),
                   ("radar", True), ("level", True),
                   ("Mr Owl ate my metal worm", True),
                   ("Do geese see God", True),
                   ("Was it a car or a cat I saw", True),
                   ("Murder for a jar of red rum", True),
                   ("palindrome", False), ("Python", False)]
     for str_in, expected in test_cases:
         with self.subTest(f"{str_in} -> {expected}"):
             self.assertEqual(expected, palindrome.is_palindrome(str_in))
コード例 #27
0
 def test_Test_Cases_From_File(self):
     f = open(currentdir + '/PalindromesToTest.txt', 'r')
     TestCasesForPalindromes = MakeTestCaseList(f.read())
     f.close()
     for TestCaseForPalindrome in TestCasesForPalindromes:
         with self.subTest():
             self.assertEqual(
                 is_palindrome(TestCaseForPalindrome[0]),
                 TestCaseForPalindrome[1].lower() == "true", '---' +
                 TestCaseForPalindrome[0] + '--- does not test: ---' +
                 TestCaseForPalindrome[1] + '---')
コード例 #28
0
def test_palindrome():
  assert palindrome.is_palindrome("sagas") == True
  assert palindrome.is_palindrome("saga") == False
  assert palindrome.is_palindrome("Sagas") == True
  assert palindrome.is_palindrome("salt an ATLAS") == True
  assert palindrome.is_palindrome("Satan oscillate my metallic sonatas") == True
  assert palindrome.is_palindrome("Satan oscillate my wooden sonatas") == False
コード例 #29
0
def test_palindrome():
    assert(is_palindrome("safdasffsadfas"))
    assert(not is_palindrome("safdasffsadfaq"))
    assert(not is_palindrome("safdasfqsadfas"))
    assert(is_palindrome("s"))
    assert(is_palindrome("asa"))
    assert(not is_palindrome("sa"))
コード例 #30
0
 def test_palindrome(self):
     self.assertTrue(is_palindrome("safdasffsadfas"))
     self.assertFalse(is_palindrome("safdasffsadfaq"))
     self.assertFalse(is_palindrome("safdasfqsadfas"))
     self.assertTrue(is_palindrome("s"))
     self.assertTrue(is_palindrome("asa"))
     self.assertFalse(is_palindrome("sa"))
コード例 #31
0
def test_is_palindrome_ShouldRetrunTrueIfPalindromeEvenLength():
    # arrange
    sut = LinkedList()
    sut.add_first("a")
    sut.add_first("b")
    sut.add_first("b")
    sut.add_first("a")

    # act
    result = is_palindrome(sut)

    # assert
    assert result == True
コード例 #32
0
def test_is_palindrome_ShoulRetrunFalseIfNotPalindrome():
    # arrange
    sut = LinkedList()
    sut.add_first("a")
    sut.add_first("b")
    sut.add_first("b")
    sut.add_first("d")

    # act
    result = is_palindrome(sut)

    # assert
    assert result == False
コード例 #33
0
 def test_is_palindrome_with_whitespace_and_punctuation(self):
     # palindromes with whitespace and punctuation
     assert is_palindrome('B-B') is True
     assert is_palindrome('no, on!') is True
     assert is_palindrome('dog god?') is True
     assert is_palindrome('taco? cat.') is True
     assert is_palindrome('race-car!!!') is True
     assert is_palindrome('race fast, safe car...') is True
コード例 #34
0
 def test_is_palindrome_with_whitespace_and_mixed_casing(self):
     # palindromes with whitespace and mixed letter casing
     assert is_palindrome('B b') is True
     assert is_palindrome('No On') is True
     assert is_palindrome('Dog God') is True
     assert is_palindrome('Taco Cat') is True
     assert is_palindrome('Race Car') is True
     assert is_palindrome('Race Fast Safe Car') is True
コード例 #35
0
 def test_is_palindrome_with_whitespace(self):
     # palindromes with whitespace
     assert is_palindrome('B B') is True
     assert is_palindrome('no on') is True
     assert is_palindrome('dog god') is True
     assert is_palindrome('taco cat') is True
     assert is_palindrome('race car') is True
     assert is_palindrome('race fast safe car') is True
コード例 #36
0
ファイル: twitiwt.py プロジェクト: thatmattbone/lab
 def tweeted(self, screen_name, tweet):
     """Called with each screen name and tweet"""
     palindrome = is_palindrome_sentence(tweet)
     if(not palindrome is False):
         print("palindrome sentence: %s" %tweet)
         return
     
     words = [word.strip() for word in tweet.split()]
     for word in words:
         palindrome = is_palindrome(word)
         if(palindrome):
             print("palindrome: %s" % palindrome)
             break
         
         palindrome = is_sarahpalindrome(word)
         if(palindrome):
             print("sarahpalindrome: %s" % palindrome)
             break
コード例 #37
0
 def test_palindrome_is_palindrome(self):
     """is_palindrome should recognize a palindrome"""
     expected = True
     self.assertEqual(expected, is_palindrome('racecar'))
コード例 #38
0
 def test_non_palindrome_is_not_palindrome(self):
     """is_palindrome should not think a non-palindrome is a palindrome"""
     expected = False
     self.assertEqual(expected, is_palindrome('not a palindrome'))
コード例 #39
0
 def test4(self):
     self.assertEqual(False, is_palindrome('My name is Keith.'))
コード例 #40
0
 def test_one_english_quotes(self):
     self.assertTrue(is_palindrome('Madam, I’m Adam'))
コード例 #41
0
 def test_one_int_non(self):
     self.assertFalse(is_palindrome(543))
コード例 #42
0
ファイル: 4.py プロジェクト: partkyle/euler-py
from palindrome import is_palindrome

print sorted([m * n for n in xrange(1, 1000) for m in xrange(1, 1000) if is_palindrome(str(m * n))])[-1]
コード例 #43
0
 def test3(self):
     self.assertEqual(True, is_palindrome('Madam, I\'m Adam'))
コード例 #44
0
 def test_one_word2(self):
     self.assertTrue(is_palindrome('Коллок'))
コード例 #45
0
 def test_simple_values(self):
     self.assertTrue(is_palindrome('stunt nuts'))
コード例 #46
0
 def test2(self):
     self.assertEqual(True, is_palindrome('A man, a plan, a canal - Panama!'))
コード例 #47
0
ファイル: problem4.py プロジェクト: hwj8963/Test1
import palindrome
import prime
import itertools
import sys
from functools import reduce

P = prime.Prime()


for n in range(999*999,100*100,-1) :
    if palindrome.is_palindrome(n) :
        #print(n)
        fac = P.factorization(n)
        if fac[-1] > 1000 :
            continue
        for i in range(1,int(len(fac)/2)+1) :
            for comb in itertools.combinations(fac,i) :
                mul = reduce(lambda x,y:x*y,comb)
                remain = n/mul
                if mul>100 and mul<1000 and remain>100 and remain<1000 :
                    print(n)
                    sys.exit(0)
                
                

コード例 #48
0
 def test_one_prase_non(self):
     self.assertFalse(is_palindrome('А роза упала 1на лапу Азора'))
コード例 #49
0
 def test1(self):
     self.assertEqual(True, is_palindrome('Able was I ere I saw Elba'))
コード例 #50
0
 def test_one_prase1(self):
     self.assertTrue(is_palindrome('А роза упала на лапу Азора'))
コード例 #51
0
 def test_non_palindromes(self):
     self.assertFalse(is_palindrome('i am not a palindrome'))
コード例 #52
0
 def test_one_non(self):
     self.assertFalse(is_palindrome('Просто'))
コード例 #53
0
 def test_even_numbers(self):
     self.assertTrue(is_palindrome('toot'))
コード例 #54
0
 def test_one_word1(self):
     self.assertTrue(is_palindrome('Шалаш'))
コード例 #55
0
 def test_odd_numbers(self):
     self.assertTrue(is_palindrome('tot'))
コード例 #56
0
 def test_one_int(self):
     self.assertTrue(is_palindrome(404))
コード例 #57
0
 def test_complex_sentences(self):
     self.assertTrue(is_palindrome('A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal: Panama!'))
コード例 #58
0
 def test_complete_sentences(self):
     self.assertTrue(is_palindrome('Lisa Bonet ate no basil.'))
コード例 #59
0
 def test_multiple_sentences(self):
     self.assertTrue(is_palindrome('Doc, note, I dissent. A fast never prevents a fatness. I diet on cod.'))