added string exercise
This commit is contained in:
93
strings/string_manipulation/solution/strings.py
Normal file
93
strings/string_manipulation/solution/strings.py
Normal file
@ -0,0 +1,93 @@
|
||||
from dataclasses import dataclass, InitVar
|
||||
from typing import Iterator
|
||||
|
||||
type Str = 'String' | str
|
||||
|
||||
@dataclass
|
||||
class String:
|
||||
_s: InitVar[str]
|
||||
|
||||
def __post_init__(self, s: str) -> None:
|
||||
self.__s = s
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.__s
|
||||
|
||||
def __add__(self, other: Str) -> 'String':
|
||||
return String(self.__s + str(other))
|
||||
|
||||
def __iadd__(self, other: Str) -> 'String':
|
||||
return self + other
|
||||
|
||||
def __len__(self) -> int:
|
||||
return len(self.__s)
|
||||
|
||||
def __eq__(self, other: Str) -> bool:
|
||||
return str(self) == str(other)
|
||||
|
||||
def concat(self, other: Str) -> None:
|
||||
self + other
|
||||
|
||||
def contains(self, other: Str) -> bool:
|
||||
s = str(other)
|
||||
for c in str(self):
|
||||
if len(s) <= 0:
|
||||
break
|
||||
elif c == s[0]:
|
||||
s: str = s[1:]
|
||||
else:
|
||||
s = str(other)
|
||||
return len(s) == 0
|
||||
|
||||
def concat(self, other: Str) -> None:
|
||||
self.__s += str(other)
|
||||
|
||||
def substring(self, start: int, end: int) -> 'String':
|
||||
substr: str = ""
|
||||
for i, c in enumerate(self.__s):
|
||||
if i >= start and i <= end:
|
||||
substr += c
|
||||
return substr
|
||||
|
||||
def strip(self, chars: Str = ' ' + '\n' + '\t' + '\r') -> None:
|
||||
i = 0
|
||||
while i < len(self) and self.__s[i] in chars:
|
||||
i += 1
|
||||
j: int = len(self) - 1
|
||||
while i <= j and self.__s[j] in chars:
|
||||
j -= 1
|
||||
self.__s: str = str(self)[i:j + 1]
|
||||
|
||||
def replace(self, old: Str, new: Str, count = -1) -> None:
|
||||
o = str(old)
|
||||
n = str(new)
|
||||
j = 0
|
||||
while count > 0 or (count < 0 and j < len(self)):
|
||||
i: int = j
|
||||
while len(o) > 0 and j < len(self):
|
||||
if o[0] == self.__s[j]:
|
||||
o: str = o[1:]
|
||||
j += 1
|
||||
else:
|
||||
j += 1
|
||||
break
|
||||
if len(o) <= 0:
|
||||
self.__s = self.__s[:i] + n + self.__s[j:]
|
||||
j += len(new) - len(old)
|
||||
count -= 1
|
||||
o = str(old)
|
||||
|
||||
def add_prefix(self, prefix: Str) -> None:
|
||||
self.__s = str(prefix) + self.__s
|
||||
|
||||
def add_suffix(self, suffix: Str) -> None:
|
||||
self.__s += str(suffix)
|
||||
|
||||
def join[T](self, xs: Iterator[T]) -> 'String':
|
||||
output = String("")
|
||||
length: int = len(xs)
|
||||
for i, e in enumerate(xs):
|
||||
output += str(e)
|
||||
if i < length - 1:
|
||||
output += self
|
||||
return output
|
49
strings/string_manipulation/solution/test_strings.py
Normal file
49
strings/string_manipulation/solution/test_strings.py
Normal file
@ -0,0 +1,49 @@
|
||||
from strings import String
|
||||
|
||||
def test_eq_for_str():
|
||||
test_str = String("this is a test string!")
|
||||
assert test_str == "this is a test string!", "__eq__ wasn't implemented correctly for `String` and `str`"
|
||||
|
||||
def test_strings_contains():
|
||||
test_str = String("this is a test")
|
||||
assert test_str.contains("this")
|
||||
assert not test_str.contains("release")
|
||||
|
||||
def test_strings_concat():
|
||||
test_str = String("")
|
||||
test_str += "test succesful!"
|
||||
assert test_str == "test succesful!"
|
||||
test_str.concat(" Or is it?")
|
||||
assert test_str == "test succesful! Or is it?"
|
||||
|
||||
def test_strings_strip():
|
||||
test_str = String(" halo? \n")
|
||||
test_str.strip()
|
||||
assert test_str == "halo?"
|
||||
test_str = String(" \n ")
|
||||
test_str.strip()
|
||||
assert test_str == ""
|
||||
|
||||
|
||||
def test_strings_replace():
|
||||
test_str = String("har har har, try replacing thhis")
|
||||
test_str.replace('har ', 'ha')
|
||||
assert test_str == "hahahar, try replacing thhis"
|
||||
test_str.replace('r', '', 1)
|
||||
assert test_str == "hahaha, try replacing thhis"
|
||||
test_str.replace('hh', 'h')
|
||||
assert test_str == "hahaha, try replacing this"
|
||||
test_str.replace('try replacing this', "replaced")
|
||||
assert test_str == "hahaha, replaced"
|
||||
|
||||
|
||||
def test_add_pre_suf():
|
||||
test_str = String(" ")
|
||||
test_str.add_suffix("suff")
|
||||
assert test_str == " suff"
|
||||
test_str.add_prefix("pref")
|
||||
assert test_str == "pref suff"
|
||||
|
||||
def test_join():
|
||||
assert String(", ").join([1, 2, 3, 4]) == "1, 2, 3, 4"
|
||||
assert String(", ").join([]) == ""
|
Reference in New Issue
Block a user