palindrome

Recursive Function : Check for palindrome in Java

两盒软妹~` 提交于 2019-12-05 16:29:53
I have a class that checks whether a string is a palindrome or not. I have two questions. 1) Is this the most efficient way to check for palindrome? 2) Can this be implemented recursively? public class Words { public static boolean isPalindrome(String word) { String pal = null; word = word.replace(" ", ""); pal = new StringBuffer(word).reverse().toString(); if (word.compareTo(pal) == 0) { return true; } else { return false; } } } Have a test class to test this... Doubt its needed but here it is anyways if anyone cares to try it out to be able to help me with any of the two questions above...

Prolog - Palindrome Functor

情到浓时终转凉″ 提交于 2019-12-05 08:41:15
I am trying to write a predicate palindrome/1 in Prolog that is true if and only if its list input consists of a palindromic list. for example: ?- palindrome([1,2,3,4,5,4,3,2,1]). is true. Any ideas or solutions? A palindrome list is a list which reads the same backwards, so you can reverse the list to check whether it yields the same list: palindrome(L):- reverse(L, L). Looks that everybody is voting for a reverse/2 based solution. I guess you guys have a reverse/2 solution in mind that is O(n) of the given list. Something with an accumulator: reverse(X,Y) :- reverse(X,[],Y). reverse([],X,X).

Why my code for checking if a number is a palindrom won't work?

不打扰是莪最后的温柔 提交于 2019-12-04 07:00:01
问题 My Java code is here: import java.util.Scanner; public class task2 { public static void main(String args[]) { System.out.print("Input a 3 digit int"); Scanner scan = new Scanner(System.in); int x = scan.nextInt(); int isPalindrome = 0; while (x != 0) { isPalindrome = isPalindrome*10 + x % 10; x /= 10; } { if (x == isPalindrome){ System.out.print ("Yes, this is a palindrome!"); } else { System.out.print("No, try again"); } } } } The code will only recognize a palindrome if the numbers entered

BASH Finding palindromes in a .txt file

泪湿孤枕 提交于 2019-12-04 05:51:30
问题 I have been given a .txt file in which we have to find all the palindromes in the text (must have at least 3 letters and they cant be the same letters e.g. AAA) it should be displayed with the first column being the amount of times it appears and the second being the word e.g. 123 kayak 3 bob 1 dad #!/bin/bash tmp='mktemp' awk '{for(x=1;$x;++x)print $x}' "${1}" | tr -d [[:punct:]] | tr -s [:space:] | sed -e 's/@//g' -e 's/[0-9]*//g'| sed -r '/^.{,2}$/d' | sort | uniq -c -i > tmp1 This outputs

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

╄→尐↘猪︶ㄣ 提交于 2019-12-04 01:29:04
问题 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;

Number of distinct palindromic substrings

我怕爱的太早我们不能终老 提交于 2019-12-03 17:27:25
Given a string, I know how to find the number of palindromic substrings in linear time using Manacher's algorithm. But now I need to find the number of distinct/unique palindromic substrings. Now, this might lead to an O(n + n^2) algorithm - one 'n' for finding all such substrings, and n^2 for comparing each of these substrings with the ones already found, to check if it is unique. I am sure there is an algorithm with better complexity. I was thinking of maybe trying my luck with suffix trees? Is there an algorithm with better time complexity? I would just put substrings you found into the

Puzzled over palindromic product problem

烂漫一生 提交于 2019-12-03 15:54:15
I've been learning Ruby, so I thought I'd try my hand at some of the project Euler puzzles. Embarrassingly, I only made it to problem 4... Problem 4 goes as follows: 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. So I figured I would loop down from 999 to 100 in a nested for loop and do a test for the palindrome and then break out of the loops when I found the first one (which should be the largest one): final=nil range = 100...1000

Reverse digits in R

百般思念 提交于 2019-12-03 13:27:31
问题 How can you reverse a string of numbers in R? for instance, I have a vector of about 1000 six digit numbers, and I would like to know if they are palindromes. I would like to create a second set which is the exact reverse, so I could do a matchup. 回答1: It is actually the decimial representation of the number that you are testing to be a palindrome, not the number itself (255 is a palendrome in hex and binary, but not decimal). You can do this fairly simply using pattern matching: > tmp <- c

highest palindrome with 3 digit numbers in python

瘦欲@ 提交于 2019-12-03 05:19:38
问题 In problem 4 from http://projecteuler.net/ it says: 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 this code here def isPalindrome(num): return str(num) == str(num)[::-1] def largest(bot, top): for x in range(top, bot, -1): for y in range(top,bot, -1): if isPalindrome(x*y): return x*y print largest(100,999) It should find the

Reverse digits in R

亡梦爱人 提交于 2019-12-03 03:30:19
How can you reverse a string of numbers in R? for instance, I have a vector of about 1000 six digit numbers, and I would like to know if they are palindromes. I would like to create a second set which is the exact reverse, so I could do a matchup. It is actually the decimial representation of the number that you are testing to be a palindrome, not the number itself (255 is a palendrome in hex and binary, but not decimal). You can do this fairly simply using pattern matching: > tmp <- c(100001, 123321, 123456) > grepl( '^([0-9])([0-9])([0-9])\\3\\2\\1$', tmp ) [1] TRUE TRUE FALSE > you could