2
\$\begingroup\$
  • 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.
\$\endgroup\$
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – user8777 Jan 13 '14 at 2:18
  • 1
    \$\begingroup\$ Can you give an example input and output here? \$\endgroup\$ – Josh Caswell Jan 13 '14 at 4:33
  • 2
    \$\begingroup\$ @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? \$\endgroup\$ – Bob Jan 13 '14 at 5:10
  • 5
    \$\begingroup\$ How will this be scored? Length of palindrome / Length of code? \$\endgroup\$ – Bob Jan 13 '14 at 5:11
  • 1
    \$\begingroup\$ Please explain why you're emphasising you in "the longest palindrome you can programmatically create". \$\endgroup\$ – Peter Taylor Jan 13 '14 at 8:31
3
\$\begingroup\$

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"

\$\endgroup\$
  • \$\begingroup\$ It is possible that Chron and Lego Stormtroopr have more efficient solutions than mine. \$\endgroup\$ – DavidC Jan 14 '14 at 0:21
2
\$\begingroup\$

Python, 1/4

A single character is a palindrome, so...

s[0]

presuming the target text is stored in s.

\$\endgroup\$
  • 2
    \$\begingroup\$ Rule 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. \$\endgroup\$ – Hosch250 Jan 13 '14 at 5:05
  • 1
    \$\begingroup\$ I thought my solution sufficiently different from AaA. No idea why I used s[1] initially. \$\endgroup\$ – boothby Jan 13 '14 at 5:14
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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])
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.