Wyzwanie:
Wykonaj funkcję, która znajdzie najdłuższy palindrom w ciągu.
Uwaga: Jest to kod-trolling pytanie. Proszę nie brać poważnie pytania i / lub odpowiedzi. Więcej informacji tutaj .
code-trolling
Joe Z.
źródło
źródło
code-trolling
mój nowy ulubiony tag.Odpowiedzi:
Udać się
Poniższe rozwiązanie w Go wykorzystuje ukryte możliwości współbieżności, zamykania i rekurencyjności, aby znaleźć najdłuższy palindrom w danym ciągu:
Ponadto opiera się całkowicie na prymitywach językowych i wbudowanych typach - bez standardowej biblioteki - w ten sposób rozpoznajesz oprogramowanie o prawdziwej jakości.
Możesz chcieć nieco zwiększyć limity wielkości wątków, pamięci i stosu dla większych ciągów wejściowych - dzieje się tak, ponieważ to rozwiązanie jest tak szybkie, że Twój system operacyjny będzie na niego zazdrościć.
Edycja - profity:
ponad 160002049186 goroutinami pojawiającymi się na wejściu"345345ABCDEabcde edcbaDEABC12312123"
źródło
Python
Example usage:
Note: this may only work for certain strings.
źródło
Clearly, checking for Palindromes is hard.
So the solution is pretty simple - generate a set of every single possible Palindrome as large as the string you're testing, and see if your string contains it.
C#
(I might need to check my code for correctness, but otherwise its a wonderfully horribly inefficient way to check for Palindromes)
źródło
Perl
Does everything asked for. It is actually better, because it takes into account every possible subsequence. What's the catch? It operates in exponential time, so each additional character in the string doubles the running time. Give it more than 20 characters, and it will take all day.
Input:
iybutrvubiuynug
. Output:ibutubi
.Input:
abcdefghijklmnopqrstuvwxyzzyxwvutsrqponmlkjihgfedcba
. Output: won't happenźródło
Your problem is easily solved by regular expressions, just like in the picture below (but I decided to use java instead). This happens because regex is always the best tool that may be used for anything that involves extracting or analyzing text.
This code is evil because:
źródło
Python
This takes the string, and reorganises it to the longest possible palindrome available.
For example:
Input : Hello
Ouput : lol
źródło
bioinformatics interpretation
Very cool question dude!
Palindromes in normal language are not entirely clearly specified, for example if spaces are allowed or not. So it is not clear if these should be allowed as palindromes or not:
Anyway, I think you are referring to the better specified scientific meaning of palindrome: For a nucleotide sequence to be considered as a palindrome, its complementary strand must read the same in the opposite direction. Both the strands i.e the strand going from 5' to 3' and its complementary strand from 3' to 5' must be complementary (see here).
There is some research done for palindrome sequence recognition and I think you should really read at least this. To solve your problem you can pretty much just copy their approach! The professor even sends out the source code if you ask him.
Well, now to the problem at hand. Suppose you have a nucleotide sequence given as a character string. The best way to find palindromes in such a sequence is by using standard algorithms. I think your best bet is probably using this online tool: http://www.alagu-molbio.net/palin.html
Since you are required to provide a function that does the task, you need to think about how to get your string into this app? Well, there the fun begins. I think you could use selenium for that. Since I don't want to do your homework, i just give you the basic idea. In Java you world start like this:
In case you are interested in language palindromes you can use the same technique with other web services like http://www.jimsabo.com/palindrome.html or http://calculator.tutorvista.com/math/492/palindrome-checker.html
code-trolling technics
omit the truely helpful sources like http://rosettacode.org/wiki/Palindrome_detection
interesting but unhelpful blah about bioinformatics
deliberately misunderstanding this as bioinformatics task
cheating - to solve the problem a web service is used
źródło
Python
The string "the longest palindrome" is extracted from the docstring into
longest_palindrome
.The
reversed()
function returns an iterator, soreversed(substring) == substring
will never be true andlongest_palindrome
will never be overwritten.Hence, the function will literally find "the longest palindrome" inside a string.
źródło
Javascript
Oh, thats easy ;). Here ya go:
:)
źródło
Ruby - The (Optimized and Monkeymized!) Brute Force
I find the best way to do this is through the well known Monkey Algorithm, you can probably find it in BOOST. They always had ways of making you talk...
This is extremely inefficient, but rather cute and ruby-like if you rename everything to their original names: MaxMonkeys = len; MonkeyTalk = result, MonkeySpeed = strlen; monkeyA : a; monkeyB : b; getMonkeys: getMaxPalindrome.
This is of no value to the OP and risks him deciding to actually interface with C, and we all know how that ends...
źródło
Python 2.7
I refuse to use the standard functions, as they are inefficient. Everyone knows that the best way to look up a length is to have a table to reference, so I create a table of all of the possible palindromes, and sort them using a pythonic bogosort, but in order to improve efficiency, I remove duplicates first. At that point, I compute all of the items which are palindromes, and sort them by lengths. You can then simply take the last length in the list, which has an O(n) lookup by iterating the list.
Code:
Note
Not really suitable for strings longer than 4 characters. Does "abba" fine, but I went and bought coffee and cooked lunch before it did abcba
Issues:
Insane variable naming (and inconsistent too)
Ludicrous algorithm choice ( Calculate all possible permutations of every substring of the given string, check if they're palindromes, sort them by length and lookup last value)
Actually contains the solution to the problem
Stupid sorting algorithm (bogosort) and a nutjob method of ensuring the list is sorted.
Also, there's an indentation error in the duplicate checking which actually does nothing at all, it's just a waste of time.
źródło
C
Finding palindromes is an PNP* hard operation, so it must be done with highly optimized code. Here are five optimization tricks which will help find the solution faster.
if this else that
form. (As you go further in your career, you should master Branch Prediction if you want to be a true code ninja.) This code avoids theif
branching problem by usingfor
statements instead, which gives you 3 instructions for the price of one.But don't skimp on variable names, readability is important.
* Palindrome-Not Palindrome
Besides the obvious trolling in the commentary, there are several other issues. The search algorithim is a valid implementation of Boyer-Moore-Horspool, but it never stores the string lengths, instead calling strlen something like N*M times, making it much slower than a simple search. "Searching for the longest string first" is true, but after that it does not search by length order, so an early exit would give a wrong answer, if it was implemented. But is is not, so it searches all the N! possibilities anyway. And almost all the parameter names (needle/haystack; src/dest) are reversed from their standard meanings.
źródło
This is what I have so far in VB6:
But I don't think it works, and I think I can make it better.
źródło
Here's a Java solution for you:
źródło
AutoHotkey
The function returns spaces too as they are part of a palindrome sequence in string. So the above returns
<space>abcdedcba<space>
.źródło
Polyglot
This is trolling because it asks to "find the longest palindrome in a string", so it is finding the longest palindrome in "a string"
źródło
I never knew strings could contain palindromes, can you show me where you learned this? And if you need the longest palindrome, please visit this site: http://www.norvig.com/pal2txt.html
źródło
Iterate through each character of the string. Then check the characters before and after that character. Then the characters two before and two after that character. Keep repeating until you get to characters that are not the same. This will allow you to identify the length of each palindrome in the word. However this method will only work for palindromes of odd length. To check for palindromes of even length, check the character at position i and i-1, then i+1 and i-2, then i+2 and i-3, etc. Hope this helps!!
źródło
The obvious answer is to compare the string with its own inverse and compute the longest common sequence.
The following Perl program does just that. You may need to download the Acme::DonMartin module, it is not usually installed by default.
źródło
Lua/Python
Lua is a very fast language (which you need, because there are a lot of substrings to be checked!), but Python is better with string handling. So why not use both?
Because I've heard it's good to have local variables, I have one. Also, I've separated the function calls from their arguments, because too many arguments make expressions cluttered and unreadable.
Also, I think this will work with any strings you want to try, there probably will not be any problems with strange inputs whatsoever.
(BTW, you won't believe how long this took me to make it work.)
źródło
Python one-liner:
źródło
Python - 126 Characters
Here's my go at this:
This works in both Python 2.x and 3.x, I believe. The variable k holds the answer.
EDIT: I forgot to say, the variable p should hold the string to check for palindromes.
This is a legit implementation, so it'll work for any string.
źródło
Java
Obviously if
aString
is itself a palindrome thenaString
is the longest palindrome insideaString
. You can tell it is working by the assertion statement. Do not think too much about the first line of executable code. That is just standard java boilerplate.źródło
Game Maker Language
źródło
Fortran
Strings are too tough to work with in Fortran, so I opted to use
iachar
to convert them all to integers:It doesn't exactly work. Given the string
aabbaac
it says the longest isaa
, but given the stringacasdabbbaabb
, it says the longest isabbba
. Close enough.źródło
bbaabb
is longer in the second one.You can't compete in today's market by just doing what is asked. This code will also find the shortest palindrome and is case insensitive:
źródło
Lua
źródło
Most efficient Python implementation that trumps all other efforts:
Notes:
This will always find "the longest palindrome"
It is case sensitive.
With some modifications it can also be made to find other strings. However, you will need to create a class, add an appropriate method and then subclass it for each string to be found.
This function could be improved by porting to FORTRAN 77 or hard-coding into Intel 8008 machine code.
źródło
This is my first code trolling answer. It isn't a particularly brutal troll, it just struck me as a silly way to answer the question
Trolls are:
źródło
Python 3
Very efficient program. It searches long palindroms with center in sequential positions (on char and between) and select longest
źródło