Exemple #1
0
def euler4(mul_len):
	#mul_len = 2
	mul = mul_len*[9]
	mul = palindrome.listToNumber(mul)
	piv = mul
	mul_num = mul ** 2
	all_pals = []


	while (mul > 0):
		mul_num = mul * piv
		pal = palindrome.palindrome(mul_num)

		# The pal is multiple of piv and mul
		if pal == mul_num:
			all_pals.append(pal)

		piv = piv - 1

		if len(palindrome.numberToList(piv)) < mul_len:
				mul = mul - 1
				piv = mul

		if len(palindrome.numberToList(mul)) < mul_len:
			break

	all_pals.sort()
	return all_pals[-1]
Exemple #2
0
def main():
    print("Welcome to Palindrome Checker!")
    print("Enter a word. The program will tell you if it is a palindrome.")
    print("To quit enter a blank line.")
    while True:
        s = input("Enter a word to check: ")
        if s == "":
            break
        print("The word is a palindrome:", palindrome(s))
Exemple #3
0
def detect_all_palindromes(sequence):
    """takes a sequences and returns all palindromes in it"""
    palindromes = []
    start = 0
    first = 0
    for i in range(2, len(sequence)):
        if palindrome(sequence[start:i]):
            extra_palindromes = expand_palindrome(sequence, start, i)
            palindromes.extend(extra_palindromes)
            #palindromes.append(sequence[start:i])
        start += 1
        if palindrome(sequence[first:i + 1]):
            palindromes.append(sequence[first:i + 1])
            if first > 0 and i + 1 < len(sequence):
                more_palindromes = expand_palindrome(sequence, first, i + 1)
                palindromes.extend(more_palindromes)
        first += 1
    return palindromes
    def test_returns_true_if_word_is_palindrome(self):
        #ARRANGE
        word = 'abba'

        #ACT
        result = palindrome(word)

        #ASSERT
        self.assertTrue(result)
    def test_returns_true_if_passed_digit(self):
        #ARRANGE
        word = 1

        #ACT
        result = palindrome(word)

        #ASSERT
        self.assertTrue(result)
    def test_returns_true_if_passed_nothing(self):
        #ARRANGE
        word = ''

        #ACT
        result = palindrome(word)

        #ASSERT
        self.assertTrue(result)
    def test_returns_true_if_num_is_palindrome(self):
        #ARRANGE
        number = 121

        #ACT
        result = palindrome(number)

        #ASSERT
        self.assertTrue(result)
 def testPalindrome(self):
     self.assertTrue(pal.palindrome(self.palin1))
     self.assertTrue(pal.palindrome(self.palin2))
     self.assertTrue(pal.palindrome(self.palin3))
     self.assertFalse(pal.palindrome(self.nonpalin1))
     self.assertFalse(pal.palindrome(self.nonpalin2))
     self.assertFalse(pal.palindrome(self.nonpalin3))
     self.assertFalse(pal.palindrome(self.nonpalin4))
def test_if_linked_list_is_palindrome():
    """testing if linked list is a palindrome"""

    L = [1, 2, 3, 2, 1]

    ll = LinkedList()
    for i in L:
        ll.append(Node(i))

    assert palindrome(ll) is True
def test_if_linked_list_is_palindrome_wrong_input():
    """testing if linked list is a palindrome"""

    L = [1, 4, 5, 3, 2, 1]

    ll = LinkedList()
    for i in L:
        ll.append(Node(i))

    assert palindrome(ll) is False
Exemple #11
0
def total_iterations(n):
    #count iterations of n that it takes to make a palindrome
    n = iterate(n)
    count = 1
    for i in range(1, 50):
        #according to rules, stop at 50 or you've gone too far
        if not palindrome(str(n)):
            n = iterate(n)
            count += 1
    return count
def palindrome_in_string(string):
    """ Trying out using doctest.
	>>> palindrome_in_string("aba")
	'aba'
	"""
    for i in range(len(string)):
        for j in range(i + 1):
            substring = string[j:len(string) - (i - j)]
            if palindrome(substring) == True:
                return substring
def get_largest_palindrome(n):
    if n < 0:
        raise ValueError('Negative numbers cannot be palindromes!')

    while n > 0:
        n -= 1
        if palindrome(n):
            return n
        
    return 0
Exemple #14
0
def total_iterations(n):
	#count iterations of n that it takes to make a palindrome
	n = iterate(n)
	count = 1
	for i in range(1, 50):	
		#according to rules, stop at 50 or you've gone too far
		if not palindrome(str(n)):
			n = iterate(n)
			count += 1
	return count
def main():
    stack = [0]
    for x in range(100,1000):
        for y in range(100,1000):
            string = str(x*y)
            
            if palindrome(string):
                if int(stack[0]) < int(string):
                    stack[0] = int(string)
                
    print stack
Exemple #16
0
def test_palindrome():
    # Positive cases. These are the cases that are supposed to return True. But what is the output of an "assert"?
    # KEIR - Assert has no output. If the expression is True, then nothing
    # happens. If it is false, then an AssertionException is thrown. Try it by
    # opening an interactive session and typing 'assert 0'
    assert palindrome('a')
    assert palindrome('aa')
    assert palindrome('aba')
    assert palindrome('elle')
    assert palindrome('amelilema')

    # Negative cases. These are the cases that are supposed to return False.
    assert not palindrome('ab')
    assert not palindrome('abx')
    assert not palindrome('abzy')
    assert not palindrome('abbb')
    assert not palindrome('ameliailemb')


# if you didn't want to use nose, you could also just add this line to the bottom of your test file
# test_palindrome()
def palindrome_score(number):
    if palindrome(number):
        return 1
    else:
        nums = []
        for x in str(number):
            nums.append(x)
        nums.reverse()
        str_num = ""
        for x in nums:
            str_num += x
        num = int(str_num)
        return 1 + palindrome_score(number + num)
Exemple #18
0
def expand_palindrome(sequence, start_index, finish_index):
    """takes sequence and start and end indexes for the palindrome we want to expand"""
    palindromes = []
    second_start = start_index
    second_end = finish_index
    print("expanding {}".format(str(sequence[second_start:second_end])))
    while second_start >= 0 and second_end <= len(sequence):
        #checking surrounding indexes to see if longer palindrome
        if palindrome(sequence[second_start:second_end]):
            palindromes.append(sequence[second_start:second_end])
        second_start -= 1
        second_end += 1
    return palindromes
Exemple #19
0
def search(start, end):
    res = list()
    i=1
    chosen = dict()
    #while len(res) <5:
    for i in range(1, 50000):
        palind = palindrome(i)
        right_no = candidate(palind)
        if right_no:
            chosen[i] = palind
            res.append(palind)
            print(chosen)
        i = i+1
    print(sum(res))
    return chosen
Exemple #20
0
def euler4_brute(mul_len):
	mul = mul_len*[9]
	mul = palindrome.listToNumber(mul)
	pal = []

	for i in reversed(range(1,mul)):
		if len(palindrome.numberToList(i)) < mul_len:
			break
		for j in reversed(range(1, mul)):
			if len(palindrome.numberToList(j)) < mul_len:
				break

			val = i * j
			temp = palindrome.palindrome(val)

			if (temp == val):
				pal.append(temp)

	pal.sort()
	return pal[-1]
Exemple #21
0
 def test_fibonacci(self):
     self.assertEqual(palindrome.palindrome("6969"), False)
     self.assertEqual(palindrome.palindrome("Malayalam"), True)
Exemple #22
0
	def test_almost_palindrome(self):
		self.assertTrue(not palindrome("nipsonanomematamemonanopsin"))
Exemple #23
0
	def test_empty(self):
		self.assertTrue(palindrome("") == True)
def palingram(sentence):
    replaced = sentence.replace(" ", "")
    return palindrome.palindrome(replaced)
	def test_empty(self):
		self.assertEqual(palindrome.palindrome(""), False)
Exemple #26
0
 def test_non_palindrome(self):
     self.assertFalse(palindrome("i am not a palindrome"))
Exemple #27
0
 def test_odd(self):
     self.assertTrue(palindrome("tot"))
 def test_hard_3(self):
     s = "ghgaxybtltcmdinnefuvjegkodpcwozbzqqarjj"
     self.assertEqual(palindrome.palindrome(s), "abcdefedcba")
def test_not_palindrome():
    assert not palindrome.palindrome('brian')
def test_if_palindrome():
    assert palindrome.palindrome('racecar')
def test_palindrome_full_sentence():
    assert palindrome.palindrome('A Man, A Plan, A Canal-Panama!')
 def test_empty_string(self):
     """Should return true when given an empty string."""
     self.assertEqual(palindrome(""), True)
 def test_non_palindrome(self):
     """Should return false when given a non palindromes."""
     self.assertEqual(palindrome("yolo"), False)
     self.assertEqual(palindrome("magic"), False)
 def test_palindrome(self):
     """Should return true when given the string 'racecar'."""
     self.assertEqual(palindrome("racecar"), True)
Exemple #35
0
 def test_complete_sentences(self):
     self.assertTrue(palindrome("Lisa Bonet ate no basil."))
from palindrome import palindrome


f = open('C:\Users\Gavin\Documents\GitHub\SummerProgramming\pal.txt', 'r')

y = f.readlines()
for i in range(0,len(y)):
	y[i] = y[i].strip()
	h = palindrome(y[i])
	
	if h:
		print '%s............is a palindrome\n'% y[i]
	else:
		print '%s............is not palindrome\n'% y[i]
	
 def test_simple_2(self):
     s = "babcad"
     self.assertEqual(palindrome.palindrome(s), "bab")
def test_palindrome():
    assert palindrome("eye") == True
    assert palindrome("_eye") == True
    assert palindrome("race car") == True
    assert palindrome("not a palindrome") == False
    assert palindrome("A man, a plan, a canal. Panama") == True
    assert palindrome("never odd or even") == True
    assert palindrome("nope") == False
    assert palindrome("almostomla") == False
    assert palindrome("My age is 0, 0 si ega ym.") == True
    assert palindrome("1 eye for of 1 eye.") == False
    assert palindrome("0_0 (: /-\ :) 0-0") == True
    assert palindrome("five|\_/|four") == False

    print("YOUR CODE IS CORRECT!")
	def test_Anna(self):
		self.assertEqual(palindrome.palindrome("Anna"), True)
 def testa_palindrome_abc(self):
     self.assertEqual(False, palindrome('abc'))
	def test_racecar(self):
		self.assertEqual(palindrome.palindrome("racecar"), True)
 def test_simple_4(self):
     s = "ab"
     self.assertEqual(palindrome.palindrome(s), "a")
Exemple #43
0
 def test_palindrome(self):
     self.assertEqual(palindrome('ressasser'), 'ressasser')
 def test_empty(self):
     s = ""
     self.assertEqual(palindrome.palindrome(s), "")
Exemple #45
0
	def test_spaced_palindrome(self):
		self.assertTrue(not palindrome("ΝΙΨΟΝ ΑΝΟΜΗΜΑΤΑ ΜΗ ΜΟΝΑΝ ΟΨΙΝ"))
Exemple #46
0
 def test_multiple_sentences(self):
     self.assertTrue(palindrome("Doc, note, I dissent. A fast never prevents a fatness. I diet on cod."))
Exemple #47
0
	def test_proper_palindrome(self):
		self.assertTrue(palindrome("ΝΙΨΟΝΑΝΟΜΗΜΑΤΑΜΗΜΟΝΑΝΟΨΙΝ"))
Exemple #48
0
 def test_complex_sentences(self):
     self.assertTrue(palindrome("A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal: Panama!"))
Exemple #49
0
 def test_even(self):
     self.assertTrue(palindrome("toot"))
 def test_one_letter_string(self):
     """Should return true when given a one letter string."""
     self.assertEqual(palindrome("a"), True)
 def test_palindrome(self):
     self.assertTrue(palindrome('32123'))
     self.assertFalse(palindrome('hello'))
     self.assertTrue(palindrome('Sore was I ere I saw Eros'))
Exemple #52
0
 def test_true_statements(self):
     assert palindrome('uWu') is True
     assert palindrome('dad') is True
     assert palindrome('mom') is True
     assert palindrome('SAIPPUAKIVIKAUPPIAS') is True
Exemple #53
0
def main():
    Word = str(input("Enter the string: "))
    if palindrome.palindrome(Word):
        print("Yes")
    else:
        print("No")
def test_not_valid_cases():
    assert palindrome("ACCDDCCA", 3) == "Not valid"
    assert palindrome(773, "1551") == "Not valid"
    assert palindrome(-4505, 15) == "Not valid"
Exemple #55
0
 def test_simple(self):
     self.assertTrue(palindrome("stunt nuts"))
def test_valid_cases():
    assert palindrome(6, 4) == [11, 22, 33, 44]
    assert palindrome(75, 1) == [77]
    assert palindrome(19, 3) == [22, 33, 44]
    assert palindrome(101, 2) == [101, 111]
Exemple #57
0
from palindrome import palindrome

dbl = []
for i in xrange(1, 1000000):
	if palindrome(str(i)):
		if palindrome(str(bin(i))[2:]):
			dbl.append(i)

print sum(dbl)
 def test_simple_1(self):
     s = "abca"
     self.assertEqual(palindrome.palindrome(s), "aba")
Exemple #59
0
def isPalindromeAndPrime(n):
    return palindrome(n) and isPrime(n)
 def testa_palindrome_osso(self):
     self.assertEqual(True, palindrome('osso'))