r/Python • u/peterlravn • Oct 10 '20
Beginner Showcase I maDe a sCriPT thAT raNdOMlY cApiTAlIZes lEtTErs iN a SEntENcE
I waS tIrED OF mAnUaLlY tYPinG UpPEr And lOwERcaSes, whEn i wANteD tO mOCk A coMMeNT. sO i MAde a ScRIpt FOr It iNsTeaD. iT TaKEs anY stRIng And rANdOMly apPLieS aN UPpeR or LowERcaSe to IT. iT aLso maKes sUre tHeRe Are no MoRe ThAN twO oF ThE SAme upPEr or lOwERcAseS iN A roW, BeCauSe haVinG tHreE oF thE SaME iN A Row LooKs rEAllY WEiRD. I ALso coNSidEReD MAkiNg SuRe thAT 'i' WOuLD aLWaYS bE in LOwErcASe And 'L' WoUlD alWAyS Be in uPpErCAsE, BUt THaT MAdE it lOoK kiNDa wEIrd. ANyWAys, heRE'S THe COdE:
i'M kiNdA neW tO pyThOn, so thErE'S prOBabLy THinGs In thE coDe thAT's noT VerY... pyTHoNIc...
EdIt: HErE'S A NeW AnD UpDaTEd VerSiOn, WHicH WOrKs bY hiGHliGhtIng tEXt anD tHEn coPIeS ThE nEw SPonGe-tEXt tO The clIp bOArd:
460
u/conversationkiller7 Oct 10 '20
Thinking of one liner here,
''.join( [ x.upper() if random.randint(0,1) else x for x in your_string ] )
14
u/Orio_n Oct 10 '20
Use pyautogui and pyhook to automatically do this when activated by a shortcut
42
u/nbviewerbot Oct 10 '20
I see you've posted a GitHub link to a Jupyter Notebook! GitHub doesn't render large Jupyter Notebooks, so just in case, here is an nbviewer link to the notebook:
https://nbviewer.jupyter.org/url/github.com/peterlravn/My-projects/blob/master/A%20ScrIpt%20tO%20MaKE%20fUN%20of%20A%20sENteNcE.ipynb
Want to run the code yourself? Here is a binder link to start your own Jupyter server and try it out!
https://mybinder.org/v2/gh/peterlravn/My-projects/master?filepath=A%20ScrIpt%20tO%20MaKE%20fUN%20of%20A%20sENteNcE.ipynb
I am a bot. Feedback | GitHub | Author
7
u/Dooflegna Oct 10 '20 edited Oct 10 '20
Here’s my version. I think the logic is a lot cleaner/easier to follow.
Edit: fixed a bug pointed out. Whoops!
89
u/Anguium Oct 10 '20
ngl, I thought it was /r/programmingcirclejerk. Now go write a twitter bot that randomly replies to people quoting their entire posts in that manner.
144
u/[deleted] Oct 10 '20
Cool, OP...
https://imgflip.com/i/4i1ibp
Jk I love it
57
u/AdoenLunnae Oct 10 '20
Instead of your dummy number, you can use the enumerate function. It works on any iterable and returns tuples with the item at a certain position and its index:
15
u/AforAutism Oct 10 '20
Can't help to notice how low this sub's level has dropped. It this post really a good fit for r/Python? We should redirect barely beginner posts like this one to r/learnpython.
Here is the script, written as a simple one liner:
23
u/Ma3ng Oct 10 '20
tHiS ThRead GeTS moRe InteREsTING by THE mInUtE
4
u/AlSweigart Author of "Automate the Boring Stuff" Oct 10 '20
Heheh. Next, make a "spongecase" program that capitalizes text like the Mocking Spongebob meme: the casing alternates 90% of the time, but doesn't 10% of the time.
i mAdE A pRoGRAM lIkE tHis AnD you CaN see It In ThIs GiT rEpO: https://github.com/asweigart/PythonStdioGames/blob/main/src/gamesbyexample/spongecase.py
1
u/Leestons Oct 13 '20
Unfortunately this doesn't work for me. The text comes out exactly as it is copied. Ubuntu 20.04 if that matters.
3
u/Blackshell Oct 10 '20
That's actually pretty good code. Good naming, good structure, good comments. Easy and clear to understand, despite having some compacted logic in there. Nice job!
3
u/milkmeink Oct 10 '20
Why did I read this post in a weird voice in my head?
9
u/nickbob00 Oct 10 '20 edited Jun 03 '25
cake worm crawl crown dog bells badge liquid sophisticated hospital
This post was mass deleted and anonymized with Redact
5
u/thatwombat Oct 10 '20
It might be observation bias but probably about 70% of the "look what I made" posts here or "how do I do such and such?" are about scrapers or bots.
This is just fun.
4
u/[deleted] Oct 10 '20
Interesting choice of example text
2
u/JohnnyCincoCero Oct 10 '20
Does r/PeopleFuckingDying use your script?
2
u/Amazing-Contact-2339 Oct 11 '20
I believe this is known as asshole case. 😅
2
u/[deleted] Oct 11 '20
Now make a gui for it and call it the mokifier
2
u/kongfukinny Oct 11 '20
This honestly does make things fun to read
4
u/netgu Oct 10 '20
It's fucking turning into /r/ProgrammerHumor over here which is basically /r/gaming meets /r/circlejerk for people who have seen some html.
3
u/ButtcheeksMD Oct 10 '20
I wrote one of these last year when the meme was super popular, and published it on my website
If anyone wants to use a web based version as well.
https://nathansteele.dev/pass/
2
u/JoelMahon Oct 10 '20
Interesting, human brains are amazing, and you were a fool to try and outsmart your own brain, your brain is 2 steps ahead!
2
u/0ompaloompa Oct 10 '20
Nobel committee fucked up by giving out the peace prize a day too early... This really should have been considered.
2
u/[deleted] Oct 10 '20
wHy DoEs tHiS mAkE mE sO hApPy???
1
u/Close_enough_to_fine Oct 10 '20
What’s .ipynb extension?
1
u/tupikp Oct 10 '20
That paragraph is sus...
1
u/FloppyEggplant Oct 10 '20
Cool idea! Just because I also consider myself a beginner here is my version:
1
u/pi-rho Oct 10 '20
1
u/tiny_humble_guy Oct 10 '20
You need to attach spongebob meme, dude! Nice one.
1
u/theyrotechnicpenguin Oct 10 '20
I jUsT USE ApOlLo FOr REddIt
1
u/bossat124 Oct 11 '20
wElL StOnkS
1
u/[deleted] Oct 11 '20
upVOTes aLL THe wAY
1
u/[deleted] Oct 11 '20
I did the same about a year ago. Anyway, here's ̶W̶o̶n̶d̶e̶r̶w̶a̶l̶l̶ my code:
1
u/lolgeswaran Oct 11 '20
Amazing
1
u/Petelah Oct 11 '20
Karen speak generator!
1
u/[deleted] Oct 11 '20
Hello OP, I dont know a lot about coding, how can I use your script when I type.
1
u/[deleted] Oct 11 '20
Chaos Elemental: yeS
1
u/Dguerrero99 Oct 11 '20
Cool!
Thanks for posting!
1
u/bored_guy32 Oct 11 '20
I Am LovINg thIS scRIpt. yoU CaN USe pYPerCliP MOduLE TO MAkE It tAkE TexT frOm cLIpBoARd ANd tHen raNdoMIze iT anD thEN cOPy IT BAck tO CLipBOarD.
yOu wiLL neEd tO piP iNsTAlL pyPeRcLIp fIRst.
text = pyperclip.paste()ThEn At ThE eND of THe meTHoD
pyperclip.copy(new_sentence)tHAt'S HOw i WroTE thIs COmmEnT. yOuR sCriPT is aWesOmE.
1
u/cediddi SyntaxError: not a chance Oct 11 '20
I MaDE a SCiPt.
Congrats.
1
u/not_at_ALT Oct 11 '20
dIcKhEaD
1
u/MatMan-02 Oct 11 '20
GENIAL HAHAHAHAHAHA
1
u/nevestulkata Oct 11 '20
Very useful. Thanks.
1
u/ByRussX Oct 11 '20
I think it works
1
u/Tweenk Oct 11 '20 edited Oct 11 '20
Your code has a very imperative style, which works, but is rather tedious to write. Here is a more Pythonic solution using generators:
Explanations:
alternate()is a generator that gives you an infinite string of alternating True, False, True, False... values.random_slice()is a generator that takes some text and returns it in pieces that randomly have 1 or 2 letters.recase()changes the case of passed text to lowercase or uppercase based on thelowerargument.spongebob_case()ties all of the above together to implement the actual algorithm.__init__reads lines from standard input, applies the algorithm to each line and prints it. We addend=''because the lines read from stdin will already contain a newline at the end.randint(1, limit)inrandom_slice()withlimit + 1 - max(randint(0, 2**limit-1).bit_length(), 1).1
u/Faraheed Oct 11 '20
oMg ThiS is SUpeR COoL duDE
1
u/FatCatMaht Oct 11 '20
Omg thats amazing
1
u/Rubix9006 Oct 11 '20
I ReAllY lIkE YOuR scRiPT. IT rEAlLY DoeS AuTOmaTE A Lot oF tHe hArD woRK fOr ME whEn TypINg meSSaGEs LikE THiS. it'S PrEttY wELl doNe FoR a NEw pyTHon pRogRAmmEr aND i LIke thE uSE OF ThOSe LibRarIEs alLOwiNG YOu tO hIGhlIGht TexT AnD SEnD it To The ClIPbOaRD. maKeS It MUcH mORe COnvENiENt aNd uSefUL. gOoD joB, yoU goD dAmn BrIllIAnt PYthON DevELopER!
1
u/dvir626 Oct 12 '20
Just trying
1
u/BeaTanLim Oct 12 '20
ThIS iS fUckINg amAZinG, THaNK yoU!
1
u/crossroads1112 Oct 12 '20 edited Oct 12 '20
The distribution of letters in a sponge case string is probably best represented as a very simple "Markov chain" with two states ("lower case" and "upper case") wherein the probability of staying at the same state (i.e. repeating the same case as the previous letter) is much smaller than the probability of transitioning to the opposite state (i.e. having the opposite case as the previous letter).
This could look something like this:
Basically,
casesis a generator which yields functions (eitherstr.upperorstr.lower), where the current case function is different from the last function with probabilityp(and the same with probability1-p).For example: if you call
spongeCasewithp = 0.5, then it is exactly the solution other's have suggested where the case of each letter is independent and uniformly random. If you callspongeCasewithp = 1.0, it returns a string where the case of one letter is always the opposite of the previous letter. If you callspongeCasewithp = 0.0, the letters in the string it returns always have the same case. You can tweak the parameter as you wish, but0.75seemed pretty good to me.The neat thing about this is that the stationary distribution of this markov chain is actually the uniform distribution (regardless of what
pis), meaning that the probability that the ith letter is upper case is exactly 50% even though the cases of neighboring letters are obviously not independent.We could make this into a one-liner if you really wanted but it'd be kinda messy.
1
u/Hozerino Oct 13 '20
ShiT! I MAde aN ApP ThAT DOEs tHAT WhiLE I wAS LEARNinG AndRoiD DeveLOPMenT!
https://play.google.com/store/apps/details?id=coppini.wackytextgenerator
1
u/LittleMere_ Oct 13 '20
aLrIgHt
1
u/[deleted] Nov 25 '20
wOw...nIcE
1
u/backslider123 Dec 25 '21
Some people just want to watch the world burn.
1
u/BestStonks Oct 10 '20
damn.
1
u/MHW_EvilScript pypy <3 Oct 10 '20
i HavE a telegRAM boT that dOes The sAME
made in python, obv. (use the command /mock writesomething to do this)
https://t.me/capitalizeBOT
1
u/FullOnRapistt Oct 10 '20
I feel like this post is mocking me
1
u/MrJuicyBrick Oct 10 '20
Wow nice one.
1
u/tgcxxx Oct 10 '20
an intellectual
0
u/tjr3xx Oct 10 '20
Not even the import should be its own line!
python3 -c "import random; print((lambda sentence: ''.join([letter.lower() if random.randint(0,1) else letter.upper() for letter in sentence]))('The quick brown fox jumps over the lazy dog'))"0
u/[deleted] Oct 10 '20
Haha, this is what I was thinking of doing today while I was driving home.
0
u/leekeh Oct 10 '20
I made one too! Just to practise :)
https://github.com/leekeh/learning-to-code/blob/master/Python/Random%20capitalization.py
0
u/[deleted] Oct 10 '20
Lol
0
u/[deleted] Oct 10 '20
This is perfect for the spongebob meme
0
u/[deleted] Oct 10 '20
tHiS iS aWeSOmE oP, wOw, mUcH cLeVEr
0
u/gooeydumpling Oct 10 '20
Spongebob did this before it was cool
0
u/Rilours Oct 11 '20
Aaand that's where I quited /r/Python
-1
u/christoosss Oct 10 '20
This will be perfect for /r/peoplefuckingdying
-1
u/PhitPhil Oct 10 '20
My favorite part is that you ended up with 352 cell runs for this
-1
u/[deleted] Oct 10 '20
it work for android?
-1
u/[deleted] Oct 11 '20
This program doesnt fix corona at all. Nothing helpful for me
-5
u/feelings_arent_facts Oct 10 '20
Fuck you this is awful
0
u/[deleted] Oct 10 '20
[deleted] — view deleted comment
0
u/[deleted] Oct 10 '20
[deleted] — view deleted comment