palindrome

Finding the largest palindrome of the product of two three digit numbers problem

南笙酒味 提交于 2019-12-01 05:25:56
So on Project Euler the Problem 4 states the following: A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99. Find the largest palindrome made from the product of two 3-digit numbers. I have tried the following: #include <stdio.h> #include <stdlib.h> int check(int result) { char b[7]; sprintf(b, "%d", result); if (b[0] == b[5] && b[1] == b[4] && b[2] == b[3]) { return 1; } else { return 0; } } int main () { int i; int g; int final; for (i = 999; i > 99; i--) { for (g = 999; g > 99; g--) { if (check(g*i) == 1) { final

Where's the bug in this function to check for palindrome?

血红的双手。 提交于 2019-12-01 04:55:34
问题 Given below is the code to check if a list is a palindrome or not. It is giving correct output for 983. Where am I going wrong? def palindrome(num): flag=0 r=num[::-1] for i in range (0, len(num)-1): if(r[i]==num[i]): flag=1 else: flag=0 return flag 回答1: You should return as soon as there is a mismatch. Also, you just need to iterate till half the length: def function(...): ... for i in range (0, (len(num) + 1) / 2): if r[i] != num[i]: return False return True BTW, you don't need that loop.

longest palindromic substring recursive solution

橙三吉。 提交于 2019-12-01 03:14:02
I am aware of solutions that uses the bottom up dynamic programing approach to solve this problem in O(n^2). I am specifically looking for a top down dp approach. Is it possible to achieve longest palindromic substring using a recursive solution? Here is what I have tried but it fails for certain cases, but I feel I am almost on the right track. #include <iostream> #include <string> using namespace std; string S; int dp[55][55]; int solve(int x,int y,int val) { if(x>y)return val; int &ret = dp[x][y]; if(ret!=0){ret = val + ret;return ret;} //cout<<"x: "<<x<<" y: "<<y<<" val: "<<val<<endl; if(S

Check if given string is a palindrome using stack [closed]

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 02:01:41
Folks, I was recently interviewed and got a question on Palindrome. Given a string ( which might represent a date ), check if it's a palindrome or not using Stack. I tried to come up with solution, but he didn't like that. Can anyone show me the code snippet for it in Java ? Thanks PS : This is not a homework, actual interview question. import java.util.Stack; public class PalindromeTest { public static void main(String[] args) { String input = "test"; Stack<Character> stack = new Stack<Character>(); for (int i = 0; i < input.length(); i++) { stack.push(input.charAt(i)); } String reverseInput

Efficient way to verify if a string is a rotated palindrome?

人走茶凉 提交于 2019-12-01 01:08:03
问题 A rotated palindrome is like "1234321", "3432112". The naive method will be cut the string into different pieces and concate them back and see if the string is a palindrome. That would take O(n^2) since there are n cuts and for each cut we need O(n) to check if the string is a palindrome. I'm wondering if there's a better solution than this. I guess so, please advice. Thanks! 回答1: According to this wikipedia article it is possible for each string S of length n in time O(n) compute an array A

Palindrome tester with Java, ignoring spaces and punctuation

青春壹個敷衍的年華 提交于 2019-12-01 01:01:59
I have the program made up until the point where it has to ignore and punctuations and spaces in the thread and I was wondering if anyone could help me with the coding for that? What I've been trying out doesn't seem to be working. Here is what I have so far: import java.util.Scanner; public class PalindromeTester { public static void main (String[] args) { String str, another = "y"; int left, right; char charLeft, charRight; Scanner scan = new Scanner (System.in); while (another.equalsIgnoreCase("y")) // allows y or Y { System.out.println ("Enter a potential palindrome: "); str = scan

Why will this recursive regex only match when a character repeats 2^n - 1 times?

只愿长相守 提交于 2019-11-30 20:31:12
After reading polygenelubricants 's series of articles on advanced regular expressions techniques (particularly How does this Java regex detect palindromes? ), I decided to attempt to create my own PCRE regex to parse a palindrome, using recursion (in PHP). What I came up with was: ^(([a-z])(?1)\2|[a-z]?)$ My understanding of this expression is that it should either match zero or one characters (every string of less than 2 characters is implicitly a palindrome, as well as to account for palindromes with odd lengths in the recursion), or two of the same character separated by a recursion of the

Palindrome tester with Java, ignoring spaces and punctuation

ε祈祈猫儿з 提交于 2019-11-30 19:27:32
问题 I have the program made up until the point where it has to ignore and punctuations and spaces in the thread and I was wondering if anyone could help me with the coding for that? What I've been trying out doesn't seem to be working. Here is what I have so far: import java.util.Scanner; public class PalindromeTester { public static void main (String[] args) { String str, another = "y"; int left, right; char charLeft, charRight; Scanner scan = new Scanner (System.in); while (another

Next higher prime and palindrome number

谁说胖子不能爱 提交于 2019-11-30 16:29:49
Is there any suggestion on solving next higher prime and palindrome number from a given int. Here is the snippet I am trying but its a kind of slow, please suggest if you ve any good algorithm that i can test. #!/usr/bin/python def next_higher(n): while True: s = str(n) if not any([n % i == 0 \ for i in range(2, int(n**0.5))]) and s == s[::-1]: return n n = n + 1 print next_higher(2004) print next_higher(20) Output: 10201 101 Updated code testing for palindrome before prime. much faster than my previous code. I am implementing the suggestion from user2357112. #!/usr/bin/python def next_higher

Euler problem number #4

我与影子孤独终老i 提交于 2019-11-30 15:59:55
Using Python, I am trying to solve problem #4 of the Project Euler problems. Can someone please tell me what I am doing incorrectly? The problem is to Find the largest palindrome made from the product of two 3-digit numbers . Here is what I have thus far. import math def main(): for z in range(100, 1000): for y in range(100, 1000): for x in range(1, 1000000): x = str(x) if x == x[::-1] and x == z*y: print x if __name__ == '__main__': main() Some efficiency issues: start at the top (since we can use this in skipping a lot of calculations) don't double-calculate def is_palindrome(n): s = str(n)