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:
14
u/Orio_n Oct 10 '20
Use pyautogui and pyhook to automatically do this when activated by a shortcut
26
u/peterlravn Oct 10 '20
Aaaaah, yes, I'll pretend like I understand any of that
10
u/LilBabyVirus5 Oct 10 '20
Basically pyhook lets python grab inputs while running in the background, and pyautogui can type, move your mouse, etc. basically making a relay for your text to convert to cursed text
2
u/hoppla1232 Oct 10 '20
How would you get selected text though?
9
u/Orio_n Oct 10 '20
the beauty of it is that you dont need to. use the random module to select a random number between a threshold 0-5 for example. use pyhook to hook keyboard events and check what keys are being pressed. If random returns an int 3 for example get pyhook to wait for 3 keys to be pressed before you inject the shift key or caps lock key keystroke into the event handler for keyboard events. Then after the next key is pressed depress or toggle back the key. What you get is a program that auto capitalizes after a random set of key events you dont have to copy paste text. it randomly capitalizes literally while you are typing into a textbox!
3
42
u/nbviewerbot Oct 10 '20
104
u/peterlravn 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
102
u/peterlravn Oct 10 '20
Damn I already regret making this
58
u/nbviewerbot Oct 10 '20
i lOVe iT
73
u/peterlravn Oct 10 '20
waIT, hOw thE fuCK DId THis BOt sUdDenLY GaIn coNsCIouSNeSS?
8
Oct 10 '20
!emojify
42
u/EmojifierBot Oct 10 '20
waIT š, hOw thE fuCK ā š DId THis BOt š¤ sUdDenLY š±š¤·āāļø GaIn šŖš coNsCIouSNeSS šµ?
8
u/SnowdenIsALegend Oct 10 '20
Good bot
2
u/B0tRank Oct 10 '20
Thank you, SnowdenIsALegend, for voting on EmojifierBot.
This bot wants to find the best and worst bots on Reddit. You can view results here.
Even if I don't reply to your comment, I'm still listening for votes. Check the webpage to see if your vote registered!
3
1
1
0
Oct 10 '20
[deleted]
2
u/EmojifierBot 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:Want š to run š the code š± yourself? Here is a binder
link š to start š your šš» own Jupyter server ā¢š¦ and try š it out!
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!
import random
def meme_sentence(sentence):
new_sentence = []
upper_count = 0
lower_count = 0
for char in sentence:
case = random.choice(["UPPER", "LOWER"])
if case == "UPPER":
upper_count += 1
lower_count = 0
new_sentence.append(char.upper())
else:
lower_count += 1
upper_count = 0
new_sentence.append(char.lower())
if upper_count > 2 or lower_count > 2:
new_sentence[-1] = new_sentence[-1].swapcase()
upper_count = 1
lower_count = 1
return "".join(new_sentence)
for i in range(10):
print(meme_sentence("what did you just say about me"))8
u/peterlravn Oct 10 '20
Line 17 is messed up, I think? Shouldn't it be
new_sentence[-1] = new_sentence[-1].swapcase()Or else it doesn't work. Maybe I'm just dumb though
Also, that's some clever code
4
1
u/Samuel457 Oct 10 '20
Here's mine. I'm alternating the case of (only) letters each time, starting with lower case:
def meme(sentence): new_sentence = [] upper = False for character in sentence: if character.isalpha(): if upper: character = character.upper() else: character = character.lower() upper = not upper new_sentence.append(character) return "".join(new_sentence)Using the sample text from the OP's project gives me:
wHaT tHe FuCk DiD yOu JuSt FuCkInG sAy AbOuT mE, yOu LiTtLe BiTcH?And his gives:
WhAT thE FUcK dID yOU JusT fUckINg saY aBOuT me, YoU LIttLE BItCH?
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.
35
u/theyrotechnicpenguin Oct 11 '20
HErE I FiXeD It FOr yOU:
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
9
144
Oct 10 '20
25
u/ANetworkEngineer Network Engineer Oct 10 '20
We need a script which randomly capitalises letters in a sentence then applies it to this meme template.
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:
for index, value in enumerate(iterable):
do_something()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:
do_random = lambda x: ''.join(map(lambda y: y.upper() if random.choice([True, False]) else y, x))24
u/duncan-udaho Oct 10 '20
This one liner has different behavior than OP's though. The posted version prohibits runs of three or more characters with the same case. They have that extra logic to make sure that it looks random instead of actually being random.
But I do agree that there are many optimizations to be had in OP's implementation.
14
u/peterlravn Oct 10 '20
Yup, you got it! It looks hella ugly if it's all completely random, so it doesn't work as a one-liner. And yes, the code is not clean, but u/Dooflegna made a much simpler and cleaner version in the other comments that's worth having a look at.
15
u/JoelMahon Oct 10 '20
for someone acting all high and mighty you sure are stupid, your one line "solution" is shit, as OP explains themselves, true random looks bad, you want to limit the max number of the same case in a row to two.
11
u/PM_ME_UR_THONG_N_ASS Oct 10 '20
for someone acting all high and mighty you sure are stupid,
Ahh the summary of all programmers everywhere.
3
u/fartbaker13 Oct 11 '20 edited Oct 11 '20
It's not the subs level that has dropped. OP's post was upvoted by many ppl cuz they liked the idea of using a python script for this thing. Is this sub only for sharing complex programs or machine learning projects?
Simple python is still python and it's ok for it to be posted.
1
u/echofang Oct 10 '20
I tried this and must have missed something because it didnāt work. Do I need a print function somewhere for input?
1
u/gargar070402 Oct 10 '20
do_randomis a lambda function in this case, so if you have a stringmy_str, for example, you'd need to doprint(do_random(my_str))for an output.0
Oct 10 '20
[deleted]
2
u/AforAutism Oct 10 '20
Doesn't matter here, just easier to type inside of the interactive Python shell
1
0
Oct 10 '20
[deleted]
-3
u/AforAutism Oct 10 '20
Someone should create something like r/AdvacedPython, and actually moderate it properly
15
1
Oct 10 '20
Thatās how it should always have been. /r/python should be a central place that welcomes everyone. Want something special, go to a more specialized subreddit.
23
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.
1
u/peterlravn Oct 13 '20
Have you remembered to install Pyperclip and Pyautogui?
1
u/Leestons Oct 13 '20
woRKs fInE oN wiNdoWS 10 But Not UbuNtU 20.04... WeIrD.
2
u/peterlravn Oct 13 '20
iT WOrkS fINe ON My rAspBeRry pi oS, buT TRy cHAngIng The TImE.SlEeP() fuNTiON FrOm 0.01 TO sOmEthINg LIkE 2 (iNsiDE deF COpy_clIpbOArD). tHaT miGht HElP it OuT.
1
1
u/Leestons Oct 14 '20
Still doesn't work lol. It copies fine but either it doesn't alter the text, or more likely it doesn't add it to my clipboard afterwards. Nevermind, Thanks anyway.
1
u/Leestons Oct 13 '20
I definitely installed pyautogui but I might be missing pyperclip. I didn't get any module not found errors though.
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
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
6
u/theyrotechnicpenguin Oct 11 '20
HErE I FiXED It fOr yOU:
a quiCk hInT, wHeNeVeR YoUāRe deaLiNg wItH raNdOM nUmBers iN Your cOdE, yOu nEeD To bE Be sUpER cArEful wIth HOW YoU SeEd THe ranDoM NuMBEr gEnERATOr. thE DeFaULT BeHaViOuR If yOU dOnāt dO ANYtHiNG is To uSe alwayS A RaNdOM sEeD UsInG DaTa yOu pUlL FrOm tHe oPERaTiNg sYsTEM (E.g. soMetiMeS this cOULd bE ThE ComPUtEr tImE In mIcRoSeCoNdS PuT ThRoUgH SoMe HAsH AlGORiThM). THIs MEaNs eVeR TiME yOu rUN, YOU GeT A CoMpLeTeLy dIfFerENt AND nOnRePrOdUcIbLe rEsUlT, sO In tHe CAsE ThaT SOmEtHiNg ODd iS GoIng On yOuālL NeVeR be ABlE To gO BaCk and INvESTiGaTE a tRoUbLESoME case (iMaGiNE 1 tImE In 10,000 YoUr pRoGrAM CRAShEs). yOu oUgHt tO EiThER stoRe whAT ThE RaNDOm SEeD UsEd Was sO yoU CaN Go bAcK AnD Run AGAiN, or YOU SHouLd sET iT In SOmE MoRe rEpRoDuciBlE WaY So yOu knOW BeFOReHaNd what It iS. yOu cAn tHeN AlSo ensUrE ThAt mUlTiPlE coPies oF YoUr pRoGrAm dOnāT eNd uP WiTh tHe sAmE RanDOm sEeD. hEre Itās oBvIoUsLY sIlLy, BUT If YOuārE CodiNg uP E.G. a mOntE CArLo sImULAtIoN, iT MaTtErS.
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
2
2
2
2
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.
2
u/JoelMahon Oct 10 '20
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:
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
1
1
1
u/FloppyEggplant Oct 10 '20
Cool idea! Just because I also consider myself a beginner here is my version:
import random
text = "What the fuck did you just fucking say about me, you little bitch?"
def capitalize_letter(letter):
"""Randomly capitalize a letter."""
return letter.upper() if random.randint(0, 1) == 1 else letter.lower()
def capitalize_sentence(sentence):
"""Randomly capitalize each letter in a sentence.
If there are 2 capitalized letters in a row, the following letter is
garanteed to be lower case. This works for both all upper case sentences
and all lower case sentences.
"""
new_sentence = ""
capitalized_in_a_row = 0
for letter in sentence:
if capitalized_in_a_row < 3:
new_sentence += capitalize_letter(letter)
capitalized_in_a_row += 1
else:
new_sentence += letter.lower()
capitalized_in_a_row = 0
return new_sentence
print(capitalize_sentence(text))
print(capitalize_sentence(text.upper()))2
u/CnidariaScyphozoa Oct 10 '20
I don't think this is quite correct. You are adding one to your counter everytime you randomly capitalize a letter. But what happens when it is always lower case. Then you would have your three iterations of always lower case the counter is reached and you append another lowercase letter. So in theory your code could yield a completely lowercase sentence
1
u/FloppyEggplant Oct 11 '20
You are right. I wrote a new version, but I guess it is a bit more confusing:
import random text = "What the fuck did you just fucking say about me, you little bitch?" def capitalize_sentence(sentence, max_in_a_row=2): """Randomly capitalize each letter in a sentence. If there are 2 capitalized letters in a row, the following letter is garanteed to be lower case. This works for both all upper case sentences and all lower case sentences. """ new_sentence = "" upper_in_a_row = 0 lower_in_a_row = 0 for letter in sentence: new_letter = letter.upper() if random.randint(0, 1) == 1 else letter.lower() if new_letter.isupper() and upper_in_a_row < max_in_a_row: new_sentence += new_letter upper_in_a_row += 1 lower_in_a_row = 0 elif new_letter.islower() and lower_in_a_row < max_in_a_row: new_sentence += new_letter upper_in_a_row = 0 lower_in_a_row += 1 # Switch to lower case if there are too many upper in a row elif upper_in_a_row >= max_in_a_row: new_sentence += letter.lower() upper_in_a_row = 0 lower_in_a_row += 1 # Switch to upper case if there too manu lower in a row elif lower_in_a_row >= max_in_a_row: new_sentence += letter.upper() upper_in_a_row += 1 lower_in_a_row += 0 # If is not a letter, i.g., spaces and punctuation else: new_sentence += letter return new_sentence print(capitalize_sentence(text, 1)) print(capitalize_sentence(text, 2)) print(capitalize_sentence(text, 3))The output looks like this:
WhAt tHe fUcK DiD YoU JuSt fUcKiNg sAy aBoUt mE, yOu lItTlE BiTcH?
WHaT The FuCk dID yOu jUsT FucKIng SaY abOUt Me, You LiTtlE bITcH?
WhaT thE FUck Did yOU jUST fuCKInG sAY AboUt me, YoU lItTLe BitcH?
-1
u/CoolDownBot Oct 11 '20
Hello.
I noticed you dropped 8 f-bombs in this comment. This might be necessary, but using nicer language makes the whole world a better place.
Maybe you need to blow off some steam - in which case, go get a drink of water and come back later. This is just the internet and sometimes it can be helpful to cool down for a second.
3
u/FuckCoolDownBot2 Oct 11 '20
Fuck Off CoolDownBot Do you not fucking understand that the fucking world is fucking never going to fucking be a perfect fucking happy place? Seriously, some people fucking use fucking foul language, is that really fucking so bad? People fucking use it for emphasis or sometimes fucking to be hateful. It is never fucking going to go away though. This is fucking just how the fucking world, and the fucking internet is. Oh, and your fucking PSA? Don't get me fucking started. Don't you fucking realize that fucking people can fucking multitask and fucking focus on multiple fucking things? People don't fucking want to focus on the fucking important shit 100% of the fucking time. Sometimes it's nice to just fucking sit back and fucking relax. Try it sometimes, you might fucking enjoy it. I am a bot
1
u/pi-rho Oct 10 '20
def randcap(s):
return b''.join([c.encode('utf8') ^ (randint(1) << 5) for c in s]).decode('utf8')1
u/Isvara Oct 11 '20
You missed the part about not having more than two the same in a row. Also, that code will fuck up anything that's not a letter.
1
u/pi-rho Oct 14 '20
def randcap(s): return bytes( ord(x) ^ (32*randint(0, 1)) if 'A' <= x <= 'Z' or 'a' <= x <= 'z' else ord(x) for x in s ).decode('utf8')Does that feel better?
1
u/Isvara Oct 15 '20
You still didn't get it. Think about what that randint will return on successive calls.
1
u/pi-rho Oct 15 '20
0 or 1, randomly. Rtfm.
1
u/Isvara Oct 15 '20
Which means it can return more than two 1s in a row, right?
1
u/pi-rho Oct 15 '20
Yep. R a n d o m.
1
u/pi-rho Oct 15 '20
Now, if you wanted something that wasn't random ...
#!/usr/bin/python3 from random import randint def f(): f.l = ((getattr(f, "l", 0) << 1) | randint(0, 1)) & 0b111 f.l ^= 1 if f.l in (0b111, 0b000) else 0 return ((f.l & 1) << 0b101) def randycase(s): a, z = ord('a'), ord('z') return bytes( c ^ f() if a <= (c | 0x20) <= z else c for c in s.encode('utf8') ).decode('utf8') if __name__ == '__main__': import fileinput for line in fileinput.input(): print(randycase(line.strip()))1
1
1
1
1
1
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:
from random import choice
values = [True, False]
sentence = input("Input a sentence: ")
output = ""
for letter in sentence:
if choice(values): # There's no need to write 'if choice(values) is True:'
output += letter.upper()
else:
output += letter.lower()
print(output)
# To-do: Endless loop and Pyperclip. May copy automatically text from the clipboard.1
u/Tweenk Oct 11 '20
This doesn't have OP's behavior of limiting same-case runs to a maximum of 2, it just randomizes the case of each letter.
1
1
1
1
1
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
1
1
1
1
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:
import sys, random
def alternate(x=False):
while True:
yield x
x = not x
def random_slice(text, start=0, limit=2):
while start < len(text):
end = min(start + random.randint(1, limit), len(text))
yield text[start:end]
start = end
def recase(text, lower):
return text.lower() if lower else text.upper()
def spongebob_case(text):
seq = zip(random_slice(text), alternate())
return ''.join(recase(t, b) for t, b in seq)
def __init__():
for line in sys.stdin:
print(spongebob_case(line), end='')Explanations:
The script uses the observation that you have to change the case every 1 or 2 letters, so it randomizes whether the next 1 or 2 letters should be taken from the input, then changes the case of these segments in an alternating pattern.
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.I believe this will have the same output distribution as the lookbehind solution only for limit=2. For higher limits on same-case runs, the distribution will be different. Out of 8 possible 3-letter case combinations, only 2 are same-case, so 3-letter runs will be over-represented when limit=3. This can be corrected by using a different distribution: the probability of a run of length M is 1/2**M, with the exception of a run of length N, which is double that (we cut any longer run back to N). We can get this distribution by replacing
randint(1, limit)inrandom_slice()withlimit + 1 - max(randint(0, 2**limit-1).bit_length(), 1).
1
1
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
1
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:
import random
def cases(p):
isUpper = random.choice([True, False])
while True:
yield str.upper if isUpper else str.lower
isUpper = random.choices([isUpper, not isUpper], weights=[1-p, p], k=1)[0]
def spongeCase(s, p=0.75):
return "".join(toCase(char) for toCase, char in zip(cases(p), s))Basically, cases is a generator which yields functions (either str.upper or str.lower), where the current case function is different from the last function with probability p (and the same with probability 1-p).
For example: if you call spongeCase with p = 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 call spongeCase with p = 1.0, it returns a string where the case of one letter is always the opposite of the previous letter. If you call spongeCase with p = 0.0, the letters in the string it returns always have the same case. You can tweak the parameter as you wish, but 0.75 seemed 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 p is), 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
1
1
1
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)
1
1
1
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
0
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
0
0
0
0
-1
-1
-1
-1
-5
0
Oct 10 '20
[deleted]
945
u/peterlravn Oct 10 '20
yOU geT GoLD ANd i geT a 'heLPfuL' RewARd. thIs Has bEEn tHe WOrsT tRAdE DEaL In ThE HIsToRy oF TRadE DEaLs, maYbE EVer
68
9
2
0
Oct 10 '20
[deleted]
11
u/Slimxshadyx Oct 10 '20
Why would this be posted to r/learnpython ? OP isn't asking a question in this post, but is sharing their project.
0
Oct 10 '20
[deleted]
5
u/ohlordwhywhy Oct 10 '20
Have you spent as much contributing to quality posts as you have shitting on this one?
0
Oct 10 '20
[deleted]
0
u/ohlordwhywhy Oct 11 '20 edited Oct 11 '20
This is an open forum, not a newspaper. Complaining as a reader is the contribution, but is it a good one?
You can add positively with the same amount of effort it takes to shit on a post.
It might seem like complaining pushes for a positive change but it's most likely to discourage others from participating (people don't know what your quality bar is) or just make noise (which is what's happening now).
Pointing out what is bad, actually, aimlessly pointing out because we don't even know if a mod is reading this, doesn't improve the quality of the sub nearly as much as, for example, the top contributions on this post. Which is why they're at the top.
-2
u/Isvara Oct 11 '20
It's to no use to me
Guys, before you post things, remember to check whether they're useful to zalanzalanda
0
u/Tweenk Oct 11 '20
This is actually a pretty good interview question. Random-casing a line of text is trivial, but limiting same-case runs to no more than N letters makes it interesting. There are several possible implementations and their outputs will have different distributions of same-case runs.
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 ] )