Skip to content

Instantly share code, notes, and snippets.

@puneetlakhina
Created October 20, 2011 07:33
Show Gist options
  • Save puneetlakhina/1300613 to your computer and use it in GitHub Desktop.
Save puneetlakhina/1300613 to your computer and use it in GitHub Desktop.
Python reverse a string with alternate letter capitalization
#!/usr/bin/python
import sys
s = sys.argv[1]
n=len(s)
print ''.join([s[i] if i%2 == 0 else s[i].upper() for i in range(n-1,-1,-1)])
@ravizombie
Copy link

it wont work on the sentences, if thats what you were trying to.

input :
To be, or not to be: that is the question.
Whether 'tis nobler in the mind to suffer
The slings and arrows of outrageous fortune,
Or to take arms against a sea of troubles,
And by opposing end them? To die: to sleep.

output:
To Be, Or NoT tO bE: tHaT iS tHe QuEsTiOn.
WhEtHeR 'tIs NoBlEr In ThE mInD tO sUfFeR
ThE sLiNgS aNd ArRoWs Of OuTrAgEoUs FoRtUnE,
Or To TaKe ArMs AgAiNsT a SeA oF tRoUbLeS,
AnD bY oPpOsInG eNd ThEm? To DiE: tO sLeEp.

yours gives:
.pEeLs oT :eId oT ?mEhT DnE GnIsOpPo yB DnA
,sElBuOrT Fo aEs a tSnIaGa sMrA EkAt oT RO
,EnUtRoF SuOeGaRtUo fO SwOrRa dNa sGnIlS EhT
ReFfUs oT DnIm eHt nI ReLbOn sIt' ReHtEhW
.nOiTsEuQ EhT Si tAhT :eB Ot tOn rO ,eB OT

even if the code range is reversed,: results in (which is not quite the same as output desired)

TO Be, Or nOt tO Be: ThAt iS ThE QuEsTiOn.
WhEtHeR 'tIs nObLeR In tHe mInD To sUfFeR
ThE SlInGs aNd aRrOwS Of oUtRaGeOuS FoRtUnE,
OR To tAkE ArMs aGaInSt a sEa oF TrOuBlEs,
AnD By oPpOsInG EnD ThEm? To dIe: To sLeEp.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment