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)
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
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)
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))
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)
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
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_
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
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)
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
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
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
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))
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
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
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
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
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
def test_is_palindrome_ShouldRetrunFalseIfEmpty(): # arrange sut = LinkedList() # act result = is_palindrome(sut) # assert assert result == False
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
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
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
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
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
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)
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))
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] + '---')
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
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"))
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"))
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
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
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
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
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
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
def test_palindrome_is_palindrome(self): """is_palindrome should recognize a palindrome""" expected = True self.assertEqual(expected, is_palindrome('racecar'))
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'))
def test4(self): self.assertEqual(False, is_palindrome('My name is Keith.'))
def test_one_english_quotes(self): self.assertTrue(is_palindrome('Madam, I’m Adam'))
def test_one_int_non(self): self.assertFalse(is_palindrome(543))
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]
def test3(self): self.assertEqual(True, is_palindrome('Madam, I\'m Adam'))
def test_one_word2(self): self.assertTrue(is_palindrome('Коллок'))
def test_simple_values(self): self.assertTrue(is_palindrome('stunt nuts'))
def test2(self): self.assertEqual(True, is_palindrome('A man, a plan, a canal - Panama!'))
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)
def test_one_prase_non(self): self.assertFalse(is_palindrome('А роза упала 1на лапу Азора'))
def test1(self): self.assertEqual(True, is_palindrome('Able was I ere I saw Elba'))
def test_one_prase1(self): self.assertTrue(is_palindrome('А роза упала на лапу Азора'))
def test_non_palindromes(self): self.assertFalse(is_palindrome('i am not a palindrome'))
def test_one_non(self): self.assertFalse(is_palindrome('Просто'))
def test_even_numbers(self): self.assertTrue(is_palindrome('toot'))
def test_one_word1(self): self.assertTrue(is_palindrome('Шалаш'))
def test_odd_numbers(self): self.assertTrue(is_palindrome('tot'))
def test_one_int(self): self.assertTrue(is_palindrome(404))
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!'))
def test_complete_sentences(self): self.assertTrue(is_palindrome('Lisa Bonet ate no basil.'))
def test_multiple_sentences(self): self.assertTrue(is_palindrome('Doc, note, I dissent. A fast never prevents a fatness. I diet on cod.'))