Skip to content

Instantly share code, notes, and snippets.

@mentix02
Created November 3, 2019 22:13
Show Gist options
  • Save mentix02/28f961bbf3986e7714126626fc68686a to your computer and use it in GitHub Desktop.
Save mentix02/28f961bbf3986e7714126626fc68686a to your computer and use it in GitHub Desktop.
Generates spongebob meme text.
#!/usr/bin/env python3
"""
About -
=======
Generates a randomly upper/lowercased
text from a given input. For example -
Input : Hello World
Output : hEllO wORld
Input : Spam and Eggs
Output : SPAm And eggS
API Usage -
===========
import spongebob as sponge
text = "Hello world"
meme_text = sponge.gen_random_str_upper(text)
print(meme_text)
CLI Usage -
===========
$ ./spongebob.py Hello world
HeLLO wOrLD
Interactive Usage -
===================
$ ./spongebob.py
> Hello world
HelLO wOrLd
> exit
bye
"""
import sys
import random
def generate_random_char_upper(s: str) -> str:
"""
Takes in a string and returns with
a random character uppercased.
"""
char_index = random.randint(0, len(s)-1)
if s[char_index].isupper():
s = s.replace(s[char_index], s[char_index].lower())
else:
s = s.replace(s[char_index], s[char_index].upper())
return s
def gen_random_str_upper(s: str) -> str:
"""
Goes over a string exactly half + 1 times
and keeps updating (or rather creating a
new copy) of itself with a random character
uppercased because that's how the meme goes.
"""
original = s
if not isinstance(s, str):
return s
if s.isdigit():
return s
else:
for i in range(int(len(s) / 2)+1):
s = generate_random_char_upper(s)
if s == original:
if not s.isalnum():
return s
return gen_random_str_upper(s)
return s
if __name__ == '__main__':
if len(sys.argv) >= 2:
inp = ' '.join(sys.argv[1:])
print(gen_random_str_upper(inp))
quit(0)
else:
try:
while True:
inp = input("> ")
if inp == 'exit':
break
print(gen_random_str_upper(inp))
except (EOFError, KeyboardInterrupt):
pass
print('bye')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment