secret
Last active

String that can record access to and comparison of individual characters

  • Download Gist
gistfile1.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
import json
 
class RecString(str):
def __init__(self, text):
self.steps = [text]
self.index = 0
def __eq__(self, other):
if other is '':
return not len(self)
if len(self) != 1 or len(other) != 1 or not isinstance(other, RecString):
self.error()
equal = str.__eq__(self, other)
self.steps.append(['c', self.index, other.index, 1 if equal else 0])
return equal
def __ne__(self, other):
return not self.__eq__(other)
def __getitem__(self, *args):
if (len(args) > 1):
self.error()
else:
return self.baby(str.__getitem__(self, *args), args[0])
def __getslice__(self, *args):
self.error()
def lower(self):
return self.baby(str.lower(self), self.index)
def upper(self):
return self.baby(str.upper(self), self.index)
def baby(self, text, index):
baby = RecString(text)
baby.steps = self.steps
baby.index = index
return baby
def error(self):
raise Exception("""
Please access only individual characters: e.g. text[a]
Comparisons such as text == text[::-1] are O(n),
do them explicitly one character at a time.
""")
def get_recording_link(self):
return ('http://explored.tk/experiments/palindrome#[%s]' %
json.dumps(self.steps, separators=(',',':')))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.