r/dailyprogrammer • u/nint22 1 2 • Oct 30 '12
[10/30/2012] Challenge #109 [Intermediate]
Description:
A palindrome is a string of characters that are read the same way both ways (forward and backwards). Given two range of integers (a_start, a_end and b_start, b_end), if at least one of the products between the two ranges is a palindrome, print the integer-pair.
For example, if the first range of integers is [90,99] and the second is [90,99], there is at least one palindrome because 91 x 99 = 9009, which is read the same forward and backward. Thus, "91, 99" should br printed.
Formal Inputs & Outputs:
Input Description:
Integer a_start - The starting range of the integer a
Integer a_end - The ending range of the integer a
Integer b_start - The starting range of the integer b
Integer b_end - The ending range of the integer b
Output Description:
Print an integer pair if their product is a palindrome.
Sample Inputs & Outputs:
Let a_start and a_end be 10, 11, and let b_start and b_end be 10, 11. Your code, given these arguments, should print "11, 11", since 11 * 11 is 121, and is a palindrome.
Notes:
This problem is of an easy-intermediate difficulty level; a brute-force solution works well enough, but think of what happens when given a large range of numbers. What is the computational complexity? What can you do to optimize palindrome verification?
7
u/5outh 1 0 Oct 30 '12 edited Oct 30 '12
Haskell:
output:
1
u/ixid 0 0 Oct 30 '12 edited Oct 30 '12
I can't think of anything useful to optimize it other than lazy evaluation (bail the first time something's wrong rather than complete the whole palindrome comparison). Depending on the data memoization might help. And, though I am terrible at computational complexity stuff, is this O(n2) ? (1 + n) / 2 * n operations leaving n2 as the dominant term with the operations of the palindrome test being up to half palindrome length and so probably ignorable as a sort of constant factor.
3
u/skeeto -9 8 Oct 30 '12
As a suggestion, instead of requesting an essay response as you did under your "notes" section you should instead issue a bonus. For example, in this case you could have provided input ranges that can't be brute forced the naive way, requiring us to make that optimization in order to complete the bonus.
In Common Lisp,
Output:
2
u/prondose 0 0 Oct 31 '12
Perl6:
2
u/the_mighty_skeetadon Oct 31 '12 edited Oct 31 '12
Ruby method that only searches unique products combinations. I wrote a more complicated method that tries to search for unique factors and simplify down, but factoring is much more computationally heavy than just brute forcing all of the unique combinations.
I was looking at the patterns trying to determine mathematical relationships, but really couldn't find any that I was able to model. I'm sure someone's figured out at least partial models; share if you have any insight!
2
u/larg3-p3nis Nov 03 '12
Java.
2
u/nagasgura 0 0 Oct 30 '12 edited Oct 31 '12
Python
one-linerusing only list comprehension:1
u/yentup 0 0 Oct 30 '12
Python:
Output:
1
u/theOnliest Oct 30 '12
In Perl:
I tried to optimize for speed using a cache table, but the hash lookups took longer than the calculations did, so I'm sort of stumped on the "notes" section.
1
u/[deleted] Oct 30 '12
Java(Brute Force):
1
u/shandelman Oct 30 '12
I know we're heavy on Python solutions already, but:
1
u/PuffGuru Oct 31 '12 edited Oct 31 '12
Always open to feedback and criticism, here is my implementation in C:
EDIT: Just realized doesn't exactly fit the requirements as it doesn't return the multipliers, so consider this my helper/wrapper function to indicate weather multipliers create palindrome number or not. Its using a brute force implementation so nothing too interesting to show.
1
u/rowenlemming Oct 31 '12 edited Oct 31 '12
Javascript brute force:
Stole my string-reverse function from Stack Overflow
EDIT: misread the question slightly and returned the product, not the integers. Fixed
1
u/dbh937 0 0 Oct 31 '12
Python:
Please pardon my horrible one-letter variable names. Edit: corrected the output
1
u/[deleted] Oct 31 '12
Here's mine in python 2.7, though one was already posted:
output:
1
u/error1f1f 0 0 Oct 31 '12 edited Oct 31 '12
Here is my solution in C++. I assume you do not want to include single digits as palindromes?
1
u/DannyP72 Nov 01 '12 edited Nov 01 '12
Another Ruby solution with some recursion.
1
u/[deleted] Nov 01 '12 edited Nov 01 '12
C++:
1
u/krat0sprakhar 0 0 Nov 01 '12
My Solution in Python -
1
u/Die-Nacht 0 0 Nov 02 '12
Python. It also returns the palindrome itself (as a third element of the truple):
Output (small ranges to keep us sane):
As for the efficiency, what I did was 1, make sure that the "outer" range was smaller than the inner one, and then go through the outer range while deleting the inner one. Don't know how much efficient that makes it; I thought it would since with each iteration, there would be a smaller range to compare the outer range to. I ran the command through time and got this:
No idea if that is good or not. Anyone knows?
1
u/Alca_Pwn Nov 03 '12
perl
1
u/marekkpie Jan 21 '13
Lua. I believe
string.reversetakes O(n), so the palindrome checking will take O(n2 ).0
u/[deleted] Oct 30 '12
[deleted] — view deleted comment