Skip to content

Instantly share code, notes, and snippets.

@codecakes
Created September 4, 2022 21:38
Show Gist options
  • Save codecakes/e9964db1c75dba1b0e9a75d38507f47c to your computer and use it in GitHub Desktop.
Save codecakes/e9964db1c75dba1b0e9a75d38507f47c to your computer and use it in GitHub Desktop.
Frame words in a phrase in reverse order the non-idiomatic unpythonic way
# Online Python - IDE, Editor, Compiler, Interpreter
import string
EXCLUDE_CHARS = {each_invalid_char: "" for each_invalid_char in string.punctuation}
SAME_CHARS = {**{w:w for w in string.ascii_letters}, **EXCLUDE_CHARS}
EXCLUDE_CHAR_MAP = str.maketrans(SAME_CHARS)
def reverse_string(sentence: str):
space = " "
final_str = ""
n = len(sentence)
start_idx = 0
astring = ""
for idx in iter(lambda: start_idx, n):
char = sentence[idx]
if char != space:
astring += char
else:
final_str = space + astring.translate(EXCLUDE_CHAR_MAP) + final_str
astring = ""
start_idx += 1
if astring:
final_str = space + astring.translate(EXCLUDE_CHAR_MAP) + final_str
return final_str.strip()
print(reverse_string("The Final Countdown."))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment