From e999393ba00f96d8ec225a6a69108c09e04f3095 Mon Sep 17 00:00:00 2001 From: Selina Delgado Date: Sat, 24 Feb 2024 21:41:42 -0800 Subject: [PATCH 1/7] translating Slice.py --- python/selfie-lib/selfie_lib/Slice.py | 73 +++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 python/selfie-lib/selfie_lib/Slice.py diff --git a/python/selfie-lib/selfie_lib/Slice.py b/python/selfie-lib/selfie_lib/Slice.py new file mode 100644 index 00000000..0d55e3c9 --- /dev/null +++ b/python/selfie-lib/selfie_lib/Slice.py @@ -0,0 +1,73 @@ +from typing import Optional +class Slice: + """Represents a slice of a base string from startIndex to endIndex.""" + + def __init__(self, base, startIndex=0, endIndex=None): # type: (str, int, Optional[int]) -> None + self.base = base + self.startIndex = startIndex + self.endIndex = endIndex if endIndex is not None else len(base) + + assert 0 <= self.startIndex <= self.endIndex <= len(base), "Invalid start or end index" + + def __len__(self): # type: () -> int + return self.endIndex - self.startIndex + + def __getitem__(self, index): # type: (int) -> str + if not (0 <= index < len(self)): + raise IndexError("Index out of range") + return self.base[self.startIndex + index] + + def subSequence(self, start, end): # type: (int, int) -> 'Slice' + return Slice(self.base, self.startIndex + start, self.startIndex + end) + + def trim(self): # type: () -> 'Slice' + start, end = 0, len(self) + while start < end and self[start].isspace(): + start += 1 + while start < end and self[end - 1].isspace(): + end -= 1 + return self.subSequence(start, end) if start > 0 or end < len(self) else self + + def __str__(self): # type: () -> str + return self.base[self.startIndex:self.endIndex] + + def sameAs(self, other): # type: (str) -> bool + if len(self) != len(other): + return False + for i in range(len(self)): + if self[i] != other[i]: + return False + return True + + def indexOf(self, lookingFor, startOffset=0): # type: (str, int) -> int + result = self.base.find(lookingFor, self.startIndex + startOffset, self.endIndex) + return -1 if result == -1 else result - self.startIndex + + def unixLine(self, count): # type: (int) -> 'Slice' + assert count > 0, "Count must be positive" + lineStart = 0 + for i in range(1, count): + lineStart = self.indexOf('\n', lineStart) + assert lineStart >= 0, f"This string has only {i - 1} lines, not {count}" + lineStart += 1 + lineEnd = self.indexOf('\n', lineStart) + return Slice(self.base, self.startIndex + lineStart, self.endIndex if lineEnd == -1 else self.startIndex + lineEnd) + + def __eq__(self, other): # type: (object) -> bool + if self is other: + return True + if isinstance(other, Slice): + return self.sameAs(other) + return False + + def __hash__(self): # type: () -> int + h = 0 + for i in range(len(self)): + h = 31 * h + ord(self[i]) + return h + + def replaceSelfWith(self, s): # type: (str) -> str + return self.base[:self.startIndex] + s + self.base[self.endIndex:] + + def baseLineAtOffset(self, index): # type: (int) -> int + return 1 + Slice(self.base, 0, index).count('\n') From 2c6d3a489e150446aee4b59dcb50760d2613f164 Mon Sep 17 00:00:00 2001 From: Selina Delgado Date: Sat, 24 Feb 2024 22:00:21 -0800 Subject: [PATCH 2/7] modified sameAs function --- python/selfie-lib/selfie_lib/Slice.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/python/selfie-lib/selfie_lib/Slice.py b/python/selfie-lib/selfie_lib/Slice.py index 0d55e3c9..56100c32 100644 --- a/python/selfie-lib/selfie_lib/Slice.py +++ b/python/selfie-lib/selfie_lib/Slice.py @@ -1,4 +1,6 @@ from typing import Optional +from typing import Union + class Slice: """Represents a slice of a base string from startIndex to endIndex.""" @@ -31,13 +33,17 @@ def trim(self): # type: () -> 'Slice' def __str__(self): # type: () -> str return self.base[self.startIndex:self.endIndex] - def sameAs(self, other): # type: (str) -> bool - if len(self) != len(other): - return False - for i in range(len(self)): - if self[i] != other[i]: + def sameAs(self, other): # type: (Union['Slice', str]) -> bool + if isinstance(other, Slice): + return str(self) == str(other) + elif isinstance(other, str): + if len(self) != len(other): return False - return True + for i in range(len(self)): + if self[i] != other[i]: + return False + return True + return False def indexOf(self, lookingFor, startOffset=0): # type: (str, int) -> int result = self.base.find(lookingFor, self.startIndex + startOffset, self.endIndex) From 44ef0093daeff15310e8af3ab8d03439373f1bd4 Mon Sep 17 00:00:00 2001 From: Selina Delgado Date: Sat, 24 Feb 2024 22:09:31 -0800 Subject: [PATCH 3/7] implemented count function --- python/selfie-lib/selfie_lib/Slice.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/python/selfie-lib/selfie_lib/Slice.py b/python/selfie-lib/selfie_lib/Slice.py index 56100c32..9c9504d7 100644 --- a/python/selfie-lib/selfie_lib/Slice.py +++ b/python/selfie-lib/selfie_lib/Slice.py @@ -1,5 +1,6 @@ -from typing import Optional +from typing import Optional from typing import Union +from collections import Counter class Slice: """Represents a slice of a base string from startIndex to endIndex.""" @@ -10,7 +11,7 @@ def __init__(self, base, startIndex=0, endIndex=None): # type: (str, int, Optio self.endIndex = endIndex if endIndex is not None else len(base) assert 0 <= self.startIndex <= self.endIndex <= len(base), "Invalid start or end index" - + def __len__(self): # type: () -> int return self.endIndex - self.startIndex @@ -74,6 +75,9 @@ def __hash__(self): # type: () -> int def replaceSelfWith(self, s): # type: (str) -> str return self.base[:self.startIndex] + s + self.base[self.endIndex:] - + + def count(self, char): + return Counter(self.base[self.startIndex:self.endIndex])[char] + def baseLineAtOffset(self, index): # type: (int) -> int return 1 + Slice(self.base, 0, index).count('\n') From 06bf63f5d27f9f714ca20ea904ba70c4b676fcc5 Mon Sep 17 00:00:00 2001 From: Selina Delgado Date: Sat, 24 Feb 2024 22:29:55 -0800 Subject: [PATCH 4/7] ignore unused imports b/c they are being used --- python/selfie-lib/selfie_lib/Slice.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/selfie-lib/selfie_lib/Slice.py b/python/selfie-lib/selfie_lib/Slice.py index 9c9504d7..287e3224 100644 --- a/python/selfie-lib/selfie_lib/Slice.py +++ b/python/selfie-lib/selfie_lib/Slice.py @@ -1,5 +1,5 @@ -from typing import Optional -from typing import Union +from typing import Optional # noqa: F401 +from typing import Union # noqa: F401 from collections import Counter class Slice: From 227bea68c0492f477d2c042e387ab9c4d567440f Mon Sep 17 00:00:00 2001 From: Selina Delgado Date: Sat, 24 Feb 2024 22:45:21 -0800 Subject: [PATCH 5/7] first commit for SliceTest.py --- python/selfie-lib/tests/SliceTest.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 python/selfie-lib/tests/SliceTest.py diff --git a/python/selfie-lib/tests/SliceTest.py b/python/selfie-lib/tests/SliceTest.py new file mode 100644 index 00000000..e69de29b From 0697b7529c08fe88d5f8f775b711cbe8f27aed03 Mon Sep 17 00:00:00 2001 From: Selina Delgado Date: Sat, 24 Feb 2024 23:06:17 -0800 Subject: [PATCH 6/7] testing working locally --- python/selfie-lib/selfie_lib/__init__.py | 1 + python/selfie-lib/tests/SliceTest.py | 0 python/selfie-lib/tests/Slice_test.py | 13 +++++++++++++ 3 files changed, 14 insertions(+) delete mode 100644 python/selfie-lib/tests/SliceTest.py create mode 100644 python/selfie-lib/tests/Slice_test.py diff --git a/python/selfie-lib/selfie_lib/__init__.py b/python/selfie-lib/selfie_lib/__init__.py index 59513e0d..5ba69133 100644 --- a/python/selfie-lib/selfie_lib/__init__.py +++ b/python/selfie-lib/selfie_lib/__init__.py @@ -3,3 +3,4 @@ from .selina import get_interesting_fact as get_interesting_fact from .harvir import silly_addition as silly_addition from .edwin import simple_subtraction as simple_subtraction +from .Slice import Slice as Slice diff --git a/python/selfie-lib/tests/SliceTest.py b/python/selfie-lib/tests/SliceTest.py deleted file mode 100644 index e69de29b..00000000 diff --git a/python/selfie-lib/tests/Slice_test.py b/python/selfie-lib/tests/Slice_test.py new file mode 100644 index 00000000..fad1af52 --- /dev/null +++ b/python/selfie-lib/tests/Slice_test.py @@ -0,0 +1,13 @@ +from selfie_lib import Slice + +def test_unixLine(): + slice_1 = Slice("A single line") + assert str(slice_1.unixLine(1)) == "A single line" + + one_two_three = Slice("\nI am the first\nI, the second\n\nFOURTH\n") + assert str(one_two_three.unixLine(1)) == "" + assert str(one_two_three.unixLine(2)) == "I am the first" + assert str(one_two_three.unixLine(3)) == "I, the second" + assert str(one_two_three.unixLine(4)) == "" + assert str(one_two_three.unixLine(5)) == "FOURTH" + assert str(one_two_three.unixLine(6)) == "" \ No newline at end of file From 1cc0a97e903844155d0460335e99aeda5879a274 Mon Sep 17 00:00:00 2001 From: Selina Delgado Date: Mon, 26 Feb 2024 19:39:24 -0800 Subject: [PATCH 7/7] changed comment typing to inline --- python/selfie-lib/selfie_lib/Slice.py | 33 ++++++++++++++------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/python/selfie-lib/selfie_lib/Slice.py b/python/selfie-lib/selfie_lib/Slice.py index 287e3224..63b15cdd 100644 --- a/python/selfie-lib/selfie_lib/Slice.py +++ b/python/selfie-lib/selfie_lib/Slice.py @@ -1,29 +1,30 @@ -from typing import Optional # noqa: F401 -from typing import Union # noqa: F401 +from typing import Optional +from typing import Union from collections import Counter class Slice: """Represents a slice of a base string from startIndex to endIndex.""" - def __init__(self, base, startIndex=0, endIndex=None): # type: (str, int, Optional[int]) -> None + def __init__(self, base: str, startIndex: int = 0, endIndex: Optional[int] = None) -> None: + self.base = base self.base = base self.startIndex = startIndex self.endIndex = endIndex if endIndex is not None else len(base) assert 0 <= self.startIndex <= self.endIndex <= len(base), "Invalid start or end index" - def __len__(self): # type: () -> int + def __len__(self) -> int: return self.endIndex - self.startIndex - def __getitem__(self, index): # type: (int) -> str + def __getitem__(self, index: int) -> str: if not (0 <= index < len(self)): raise IndexError("Index out of range") return self.base[self.startIndex + index] - def subSequence(self, start, end): # type: (int, int) -> 'Slice' + def subSequence(self, start: int, end: int) -> 'Slice': return Slice(self.base, self.startIndex + start, self.startIndex + end) - def trim(self): # type: () -> 'Slice' + def trim(self) -> 'Slice': start, end = 0, len(self) while start < end and self[start].isspace(): start += 1 @@ -31,10 +32,10 @@ def trim(self): # type: () -> 'Slice' end -= 1 return self.subSequence(start, end) if start > 0 or end < len(self) else self - def __str__(self): # type: () -> str + def __str__(self) -> str: return self.base[self.startIndex:self.endIndex] - def sameAs(self, other): # type: (Union['Slice', str]) -> bool + def sameAs(self, other: Union['Slice', str]) -> bool: if isinstance(other, Slice): return str(self) == str(other) elif isinstance(other, str): @@ -46,11 +47,11 @@ def sameAs(self, other): # type: (Union['Slice', str]) -> bool return True return False - def indexOf(self, lookingFor, startOffset=0): # type: (str, int) -> int + def indexOf(self, lookingFor: str, startOffset: int = 0) -> int: result = self.base.find(lookingFor, self.startIndex + startOffset, self.endIndex) return -1 if result == -1 else result - self.startIndex - def unixLine(self, count): # type: (int) -> 'Slice' + def unixLine(self, count: int) -> 'Slice': assert count > 0, "Count must be positive" lineStart = 0 for i in range(1, count): @@ -60,24 +61,24 @@ def unixLine(self, count): # type: (int) -> 'Slice' lineEnd = self.indexOf('\n', lineStart) return Slice(self.base, self.startIndex + lineStart, self.endIndex if lineEnd == -1 else self.startIndex + lineEnd) - def __eq__(self, other): # type: (object) -> bool + def __eq__(self, other: object) -> bool: if self is other: return True if isinstance(other, Slice): return self.sameAs(other) return False - def __hash__(self): # type: () -> int + def __hash__(self) -> int: h = 0 for i in range(len(self)): h = 31 * h + ord(self[i]) return h - def replaceSelfWith(self, s): # type: (str) -> str + def replaceSelfWith(self, s: str) -> str: return self.base[:self.startIndex] + s + self.base[self.endIndex:] - def count(self, char): + def count(self, char: str) -> int: return Counter(self.base[self.startIndex:self.endIndex])[char] - def baseLineAtOffset(self, index): # type: (int) -> int + def baseLineAtOffset(self, index: int) -> int: return 1 + Slice(self.base, 0, index).count('\n')