palindrome

Count number of palindromes in a list of strings, Haskell

﹥>﹥吖頭↗ 提交于 2019-12-11 16:42:23
问题 The function countPalindromes receives a list of strings and returns a count of how many of the strings are palindromes. isPalindrome :: String -> Bool isPalindrome w = w == reverse w countPalindromes :: [String] -> Int countPalindromes ss = length filter (== isPalindrome) ss I know that the function length is applied to two arguments instead of one. I just don't know how to fix this? 回答1: You may use parentheses to affect function application: countPalindromes ss = length (filter (==

Java Palindrome Program (am I on track)?

北城余情 提交于 2019-12-11 13:07:46
问题 I have only 6 months of Java experience (and I'm also new here) so please bear with me if things don't look entirely right in my code. Please note that it's still a work in progress. I'm trying to write a program that takes in strings and prints only the ones that are palindromes. I'm supposed to: - create a method named isPalindrome , which has a String parameter and - returns a Boolean based on whether the string is a palindrome or not. Then - modify the main method to use isPalindrome to

Decrease the complexity of isPalindrome solution?

耗尽温柔 提交于 2019-12-11 11:56:57
问题 Here is my isPalindrome method public static boolean isPalindrome(String s){ for(int i = 0; i < s.length()/2; i++){ int j = s.length() - 1 - i; if(s.charAt(i) != s.charAt(j)) return false; } return true; } my teacher says I can decrease the complexity, but I can't see how. I already am only going through half of the string. Is there any way to decrease the complexity of this solution? 回答1: You could try something like this: public static boolean isPalindrome (String str) { int left = 0; int

Java Number Palindrom

瘦欲@ 提交于 2019-12-11 08:31:53
问题 I want to write a palindrome program which will print all palindrome numbers created by multiplying two-digit numbers (10-99)? Here is my code so far: public class PrintPalindrom { public int printPalindrom (int a, int b) { int result = a*b; int reverse = 0; if (a >= 10 && a <= 99 && b >= 10 && b <= 99) { while (result != 0) { reverse = reverse * 10; reverse = reverse + result % 10; result = result/10; System.out.println("palindrom is " + result); } } else { System.out.println("Wrong numbers"

Get Odd Length Palindrome

元气小坏坏 提交于 2019-12-11 06:09:46
问题 I'm trying to find the longest odd length palindrome, but the code I've written isn't giving me the full palindrome, just part of it. Any help would be great! def get_odd_palindrome_at(s, index): ''' (str, int) -> str > get_odd_palindrome_at('tbababt', 3) 'tbababt' > get_odd_palindrome_at('color', 2) 'olo' ''' palindrome = s[index] i = index for char in s: if s[i - 1] == s[i + 1]: palindrome = s[i - 1] + palindrome + s[i + 1] i = i + 1 return palindrome 回答1: Make i the distance from the index

Checking if a number is a palindrome

不打扰是莪最后的温柔 提交于 2019-12-10 19:20:58
问题 I've tried to check whether a number is a palindrome with the following code: unsigned short digitsof (unsigned int x) { unsigned short n = 0; while (x) { x /= 10; n++; } return n; } bool ispalindrome (unsigned int x) { unsigned short digits = digitsof (x); for (unsigned short i = 1; i <= digits / 2; i++) { if (x % (unsigned int)pow (10, i) != x % (unsigned int)pow (10, digits - 1 + i)) { return false; } } return true; } However, the following code isn't able to check for palindromes - false

Proving that a reversible list is a palindrome in Coq

断了今生、忘了曾经 提交于 2019-12-10 12:55:04
问题 Here is my inductive definition of palindromes: Inductive pal { X : Type } : list X -> Prop := | pal0 : pal [] | pal1 : forall ( x : X ), pal [x] | pal2 : forall ( x : X ) ( l : list X ), pal l -> pal ( x :: l ++ [x] ). And the theorem I want to prove, from Software Foundations : Theorem rev_eq_pal : forall ( X : Type ) ( l : list X ), l = rev l -> pal l. My informal outlines of the proof are as follows: Suppose l0 is an arbitrary list such that l0 = rev l0 . Then one of the following three

Recursive Function : Check for palindrome in Java

泄露秘密 提交于 2019-12-10 06:06:28
问题 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

Puzzled over palindromic product problem

浪尽此生 提交于 2019-12-09 13:11:35
问题 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

Check if a permutation of a string can become a palindrome

依然范特西╮ 提交于 2019-12-09 04:31:59
问题 Write a method to test if a string meets the preconditions to become a palindrome. Eg: Input | Output mmo | True yakak | True travel | False I'm thinking of this approach: Make a suffix tree for all permutation of T such that T$Reverse(T)# Check for all permutation for same node Am I missing anything? 回答1: Really all you're looking for is if all (or all but one) of the letters are paired off. As long as they are, then they will be able to be turned into a palindrome. So it would be something