Skip to content

Instantly share code, notes, and snippets.

@les-peters
Last active November 20, 2019 21:08
Show Gist options
  • Save les-peters/50cad91305d08a0fff209788cdc27e68 to your computer and use it in GitHub Desktop.
Save les-peters/50cad91305d08a0fff209788cdc27e68 to your computer and use it in GitHub Desktop.
cassidoo-1118.py
def mySubstring(w, *a):
O = ""
C = [char for char in w]
for i in range(len(C)):
if len(a) == 1:
O += C[i] if i >= a[0] else ''
else:
O += C[i] if i >= a[0] and i < (a[0]+a[1]) else ''
print(O)
class X(str):
def mySubstring(self, *a):
O = ""
C = [char for char in self]
for i in range(len(C)):
if len(a) == 1:
O += C[i] if i >= a[0] else ''
else:
O += C[i] if i >= a[0] and i < (a[0]+a[1]) else ''
print(O)
# simple function
a = 'hello world!'
mySubstring(a,1,5)
mySubstring(a,6)
# class extension, more in tune with the ask
s = X("hello world!")
s.mySubstring(1,5)
s.mySubstring(6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment