Produce a palindrome from this text

2

  • Copy and paste or curl this text: http://lorem-ipsum.me/api/text
  • Using any language make the text palindromic.
  • You may remove letters.
  • You may rearrange letters.
  • The goal is to make the text palindromic by the sequence of the letters and spaces. The end result might not contain words. It will be merely a sequence of letters and spaces.
  • Capital letters are considered the same as lowercase letters.
  • You may use each letter only once, or you may remove the letter.
  • You may also use each space only once. Or you may remove the space.
  • The result should be the longest palindrome you can programmatically create from the text.
  • While you could technically print just the letters AaA or something similar to it, that is not what I am looking for.
  • Your palindrome creator should work with any text.
  • A palindrome, by definition, is a word, phrase, number, or other sequence of symbols that reads the same backward as forward. A sequence of symbols that reads the same backward as forward is the acceptable palindromic definition I am looking for in this CodeGolf example.
  • Scoring will be Length of palindrome / Length of code. The winner is the one with the highest score.

Text:

Lorem ipsum dolor sit amet, consectetur adipisicing elit, possimus voluptatem do ut vel deserunt laborum. cupidatat libero minim at a aute minus cupiditate excepturi reiciendis blanditiis, atque vero. dolorem dolores dolore corrupti aute recusandae repellat recusandae modi deleniti libero nostrud aute delectus quidem alias odio nostrum pariatur. placeat minus similique a aliquip facere ullamco officiis eligendi ad at magnam commodi maxime.

Adipisicing modi enim similique maxime ex nisi sed eu suscipit earum corrupti assumenda perferendis cumque maiores qui praesentium nobis. Sed voluptas consequatur voluptates rerum facilis qui dolor commodi minim provident, magnam facere, tenetur. Aliqua voluptatibus delectus illum dolorem excepturi aliquip duis odio. Excepturi similique hic iusto eu consectetur laboriosam quidem esse eiusmod, anim. Eu consequatur magnam tempor vel.

Nobis incididunt eius cum distinctio magnam velit facilis libero ullam, tenetur distinctio eius eveniet. Anim possimus expedita fugiat facilis vel consequatur in temporibus laboriosam at molestiae velit cupidatat. Vel consequatur voluptate cum sint dolores ullamco quas omnis quibusdam id officiis tenetur voluptas, tempore tenetur aliquam debitis. Nostrud, do in numquam magnam quia voluptate incidunt accusamus distinctio officia molestiae fuga. Nam corporis ex quos recusandae sed.

Ullamco incidunt cupidatat eos voluptate delectus nostrud nisi irure quod quis alias. Fuga quam autem mollit ex dolores quod non tempor mollitia quas labore sapiente a anim deserunt expedita minus. Ex pariatur accusamus alias deleniti quo, quia similique voluptas. In voluptatibus esse perferendis commodo aute repellendus accusamus laboriosam deserunt aute possimus laboriosam.

Labore hic tempora, ad iusto hic aliqua ducimus. Distinctio omnis tempore incididunt ut sed mollit, quidem, aute sunt eius eveniet, vel distinctio cupiditate eius atque. Fuga asperiores tenetur enim, provident similique aliqua nostrum ad dolorum, molestiae, repudiandae ad sunt. Consequatur ullamco a nisi aliqua non omnis officia.

Labore sapiente obcaecati libero quas eos incididunt. Saepe quas voluptas mollitia consectetur dolores dolores officiis id, quo, laboriosam reprehenderit alias. Voluptates cumque dignissimos, cumque dolor. Incididunt minima consectetur harum et.

Johnston

Posted 2014-01-13T02:07:34.903

Reputation: 139

1What if the text isn't symmetric? For example, the text of this comment in this question can't be palindromed as it has more than one letter that occurs an odd number of times, hence it can be flipped without duplicating a letter. – None – 2014-01-13T02:18:03.297

1Can you give an example input and output here? – jscs – 2014-01-13T04:33:16.327

2@LegoStormtroopr I believe that's what he means by "you may remove letters". My interpretation is: every character in that file is going into a bag, and you pull characters from the bag to create a palindrome? – Bob – 2014-01-13T05:10:53.053

5How will this be scored? Length of palindrome / Length of code? – Bob – 2014-01-13T05:11:10.103

1Please explain why you're emphasising you in "the longest palindrome you can programmatically create". – Peter Taylor – 2014-01-13T08:31:57.820

@Johnston, since you have tagged this as [tag:code-golf] then are you prepared to award the win to the shortest code, even if the palindrome it generates is extremely suboptimal? – Darren Stone – 2014-01-13T09:07:00.067

1I would suggest tightening up Rule 10 a bit. "Or something similar" is vague and you're already seeing submissions that are extremely short, generating extremely short palindromes. – Darren Stone – 2014-01-13T09:45:26.490

@JoshCaswell The Mathmatica answer below is a good example. – Johnston – 2014-01-13T11:58:58.620

1@Bob "...into a bag..." Exactly. Except that you may also discard letters you pick from the bag. – Johnston – 2014-01-13T12:00:14.280

@Bob: "How will this be scored?" Yes that is perfect. Length of palindrome / Length of code. – Johnston – 2014-01-13T12:02:35.917

@DarrenStone The scoring should fix that, no? Length of palindrome / Length of code. – Johnston – 2014-01-13T12:06:55.013

I'm still a bit confused. Is the winner the one with the highest score, or the lowest? Code golf is usually lowest score wins, but this scoring mechanism encourages very long programs which create extremely short palindromes - and that seems contrary to the intent of this challenge. – Iszi – 2014-01-13T14:41:13.223

@Iszi Highest score. Largest palindrome and shortest program. It encourages short programs with long palindromes. – Johnston – 2014-01-13T14:51:00.087

How does a score of NaN sort? – John Dvorak – 2014-01-13T15:51:03.100

Why is this on hold and what do you mean NaN score? NaN will be 0. Lowest score. How would you come to NaN? Also what else is unclear? – Johnston – 2014-01-13T16:10:16.837

You still haven't answered my question, so that's still unclear for a start. – Peter Taylor – 2014-01-13T16:23:09.750

@PeterTaylor: The objective is to find the longest palindrome with the least amount of code. I was attempting to point out that there is 1 palindrome that is longest by definition, but I will accept all palindromes, however the scoring will make smaller palindromes equate to less points. Emphasis on the word you was unnecessary and possibly confusing . I have unbolded the word "you". – Johnston – 2014-01-13T17:17:35.000

The spec still seems a bit confusing. Particularly: "A palindrome is a word, phrase, number, or other sequence of symbols." (not at all minding the fact that it's an incomplete definition to begin with) is self-contradictory when paired with several other portions of the challenge which specify that we're only dealing with letters and spaces. Which is it? Do we need to count for non-letter/space characters or not - and if so, which ones? – Iszi – 2014-01-13T17:21:35.570

In any case, the specification that the palindrome can be any combination of the characters in the given text input, without any regards to having the output make any sort of sense at all, makes this relatively trivial. Simply count the occurrence of each character, rounded down to the nearest even number, then output a string that has half of those occurrences sorted ascending followed by half of them descending. If you want to get a bonus point on length, take one of the odd characters and put it in the middle (though this may make your code long enough to nullify that bonus). – Iszi – 2014-01-13T17:23:54.970

@Johnston Looks like it's already done. I figured someone else had probably gotten ahead of me on that - just hadn't bothered to look yet.

– Iszi – 2014-01-13T17:29:44.167

With this latest edit, your definition of a palindrome still remains incomplete. In fact, by explicitly stating that this incomplete definition is the one to be used for this challenge, one might simply echo the text as-is and be done because it is "a sequence of symbols". – Iszi – 2014-01-13T17:33:06.867

Per general StackExchange quality guidelines, shouldn't the Lorem Ipsum text be included in the question so that the question remains valid if the link goes dead? – Iszi – 2014-01-13T17:57:08.170

Answers

3

Mathematica 146 92 chars (score 2130/92 = 23.15)

Shorter Version (92 chars)

This palindrome has the characters grouped,

""<>{s=({#[[1]],Length@#~Quotient~2}&/@ Gather@Characters@t)
/.{c_,n_}:>c~Table~{n},Reverse@s}

Longer Version (146 chars)

This will return a maximum length, randomized palindrome from t. Because capital letters are equivalent to lowercase letters, I converted all capitals to lower case. (This can be changed if necessary.)

f@t_ := "" <> Join[s = RandomSample[ Flatten[({#[[1]], Quotient[Length[#], 2]} & /@ 
     Gather[Characters@ToLowerCase@t]) /. {c_, n_} :> ConstantArray[c, n]]], Reverse@s]

Example:

When t is the suggested, Lorem ipsum, text. (Ignore line returns; all line returns were generated via the SE editor.)

"ti,iopexri rpiuu opaat tismimetnesa lvuot oiaiu oaaittcoar nc muapua elhpibemu utin ia f ihide sdourgeeic e oaasa.marsae dstircd ci ndn rs t egoaiq.aeatntreusng xi a lumalnoiieqqeq cc otiueasniieu nrpupdlmnai o cdsm t gt.iccmittli i t tu.rust.li maui bea gqiceeo pncu eonttecvtutqun eaaariusoquppsedlqu scrddmt esiafidurs hitlpna nlamltire ivmumbabi nbuderup td x uuualsoq.eiib li olel ru aeelau
idbc lrop iil ssur anda o scnmsr ,m uri,teo esmiaveuomrt ldqqaid ihi snsr spueeoevcre oe msirulaleaisormm.oicolarsstteo td aatncn ieqrsuam boid te utcf eccp siacu m etnqutlacniiereltiulqemltaqx a qtdlxmliibileoheesimittlusqemooamuiippeaiilcgudsatfsiemamsc irn rsgoi, ie nmteiaiiptraned alsomsui noru ic,mditi ic ouud oadmv iudinrfuludet eeiidueel nndaemcrepnioe sials unetuamconn,i ubpo eiu ei dsn equi,ue ostmp udiatqu dalmau nueeiit ln e mioancteop ntiiao ts noeefubtefrit ldeop earnmsm oas
d.catirotulvliodiellmesurotiomsmduia.nauenb iuiroimduriohs.lrndsaroc t inidrssq apeuionnlaa eqdroounosmotidr arxr o taouarut ussce uamae is.dcadaimuoaprad iifl i lmml i lfii darpaoumiadacd.si eamau ecssu turauoat o rxra rditomsonuoordqe aalnnoiuepa qssrdini t corasdnrl.shoirudmioriui bneuan.aiudmsmoitorusemlleidoilvlutoritac.d
sao msmnrae poedl tirfetbufeeon st oaiitn poetcnaoim e nl tiieeun uamlad uqtaidu pmtso eu,iuqe nsd ie uie opbu i,nnocmautenu slais eoinpercmeadnn leeudiiee tedulufrnidui vmdao duuo ci itidm,ci uron iusmosla denartpiiaietmn ei ,iogsr nri csmameisftasdugcliiaeppiiumaoomeqsulttimiseehoelibiilmxldtq a xqatlmeqluitlereiincaltuqnte m ucais pcce fctu et diob mausrqei ncntaa dt oettssralocio.mmrosiaelalurism eo ercveoeeups rsns ihi diaqqdl trmouevaimse oet,iru m, rsmncs o adna russ lii porl cbdi ualeea ur lelo il biie.qoslauuu x dt puredubn ibabmumvi eritlmaln anpltih srudifaise tmddrcs uqldesppuqosuiraaae nuqtutvcettnoe ucnp oeeciqg aeb iuam il.tsur.ut t i ilttimcci.tg t msdc o ianmldpuprn ueiinsaeuito cc qeqqeiionlamul a ix gnsuertntaea.qiaoge t sr ndn ic dcritsd easram.asaao e cieegruods edihi f ai nitu umebiphle aupaum cn raocttiaao uiaio touvl asentemimsit taapo uuipr irxepoi,it"

DavidC

Posted 2014-01-13T02:07:34.903

Reputation: 24 524

It is possible that Chron and Lego Stormtroopr have more efficient solutions than mine. – DavidC – 2014-01-14T00:21:41.123

2

Python, 1/4

A single character is a palindrome, so...

s[0]

presuming the target text is stored in s.

boothby

Posted 2014-01-13T02:07:34.903

Reputation: 9 038

2Rule 10 says this is not what he wants. Also, did you mean s[0]? s[1] will not work if s has less than 2 characters, while s[0] works as long as s is not empty. – None – 2014-01-13T05:05:08.153

1I thought my solution sufficiently different from AaA. No idea why I used s[1] initially. – boothby – 2014-01-13T05:14:31.970

1

Ruby, 78 chars

Reads from standard input.

s=gets.downcase.chars.group_by{|c|c}.map{|k,v|k*(v.size/2)}*''
$><<s+s.reverse

For example, passing the text from @LegoStormtrooper's first comment on the question:

What if the text isn't symmetric? For example, the text of this comment in this question can't be palindromed as it has more than one letter that occurs an odd number of times, hence it can be flipped without duplicating a letter.

The program will return this:

whhhhhaaaaaattttttttttttt                    iiiiiiiffeeeeeeeeeeexssssnnnnnn'mmmmrrrrccccooooopplll,uubddddddbuu,lllppoooooccccrrrrmmmm'nnnnnnssssxeeeeeeeeeeeffiiiiiii                    tttttttttttttaaaaaahhhhhw

If you don't want the punctuation at all (on re-reading the question it sounds like you only want letters and spaces) this will work (87 chars):

s=gets.downcase.scan(/[a-z ]/).group_by{|c|c}.map{|k,v|k*(v.size/2)}*''
$><<s+s.reverse

Which returns:

whhhhhaaaaaattttttttttttt                    iiiiiiiffeeeeeeeeeeexssssnnnnnnmmmmrrrrccccoooooppllluubddddddbuulllppoooooccccrrrrmmmmnnnnnnssssxeeeeeeeeeeeffiiiiiii                    tttttttttttttaaaaaahhhhhw

Paul Prestidge

Posted 2014-01-13T02:07:34.903

Reputation: 2 390

1

Python 2.7 - 82

Takes an input, converts it all to lower case, makes an array with half (rounded down) of each character, then prints that out concatenated to itself in reverse.

Worst case scenario, it outputs an empty string if every character is unique.

L=raw_input().lower()
o=[x*(L.count(x)/2)for x in set(L)]
print''.join(o+o[::-1])

user8777

Posted 2014-01-13T02:07:34.903

Reputation:

1

Python, 107

s="""Lorem ipsum dolor sit amet, consectetur adipisicing elit, hic a sunt odio eius culpa labore. vero vero possimus irure quidem est saepe ut do quis excepturi incididunt. magna eveniet nisi enim quidem distinctio.

Ullam saepe officiis consequatur, provident soluta blanditiis sunt possimus minima impedit corporis voluptatibus occaecat distinctio mollitia, commodo et. Facilis autem cum dolor dignissimos magna modi nisi labore animi incididunt numquam quam voluptatem optio tempor provident fugiat ullam. Consequat tempora numquam minim tempore eius perferendis ullamco labore corporis officiis. Saepe deserunt a asperiores rerum quis excepteur odio voluptatibus ducimus itaque.

Qui nam provident corrupti sunt velit est laboris. Delectus tempora possimus quas cillum exercitation debitis cupidatat omnis voluptatibus. A dignissimos saepe repellat nostrum quaerat rerum molestias, alias dolorum provident fuga repellat sed, esse cum illum culpa animi. Placeat atque necessitatibus nostrud saepe maxime cupiditate, impedit elit iure hic.

Excepturi id numquam odio aliquam maiores, expedita eveniet. Repellendus eos voluptates quaerat excepteur sunt velit consectetur vel eius ad voluptatem corporis cillum tempore incidunt molestiae. Atque tempora, duis vero, tempore et sint nostrud molestiae laboris eligendi maxime nam reprehenderit.

Fugiat anim commodo sed incidunt, et aut, labore corrupti velit incididunt quaerat. Aliquam saepe minima sunt minima eos, nobis, eu delectus itaque optio consequatur voluptatibus delectus ex sapiente deserunt dolor. Optio doloribus officiis ducimus dolores.

Voluptas commodo cupidatat molestiae tempora quia consequatur. Repellendus ea rerum exercitationem, odio voluptatem ullam perferendis ea animi deleniti deleniti quaerat magnam repellat voluptates. Obcaecati aut exercitation dolore nisi eos."""

import itertools
t=''.join(''.join(list(g[1])[1::2]) for g in itertools.groupby(sorted(s)))
print t+t[::-1]

itertools.groupby(sorted(s)) groups identical characters. list(g[1])[1::2] takes each group and cuts it in half, rounding down for odd-length groups. It then joins the lists together and prints the resulting string concatenated with its reverse. Could be improved in a number of ways, as already demonstrated by others.

Fraxtil

Posted 2014-01-13T02:07:34.903

Reputation: 2 495