Skip to content

Instantly share code, notes, and snippets.

@erikseulean
Created September 18, 2020 16:19
Show Gist options
  • Save erikseulean/cb00262371aaad3c3fe058e49eb37c30 to your computer and use it in GitHub Desktop.
Save erikseulean/cb00262371aaad3c3fe058e49eb37c30 to your computer and use it in GitHub Desktop.
Untangle
sequence="""
SRSRRSSRSRSSRSSRRSSRSSSSSRSSRSSRSRSSRSSRSSSSSSSSRSSRSSSSSRSSRSSRRSSRSSSSSRSSRSSRSSSSSSSSSSSSSSSSSRSSRSSRS
"""
from fractions import Fraction
def decode(seq):
x = 0
for nr in seq:
if nr == 'S':
if x == "inf":
print(x)
continue
x = x + 1
elif nr == "R":
if x == "inf":
x = 0
elif x == 0:
x = "inf"
else:
x = 0 - Fraction(1, x)
print(x)
return x
pq = decode(sequence)
res = []
while pq != 0:
if pq < 0:
res.append("S")
pq = pq + 1
else:
pq = 0 - Fraction(1, pq)
res.append("R")
print("".join(res))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment