ahmet / Python-for-Data-Science
Herkese Açık Ahmet Kerem Piscine Final
array2D.py 1.3 KB
def slice_me(family: list, start: int, end: int) -> list:
"""Print the shape of a 2D list and return a sliced copy of it."""
if not isinstance(family, list):
raise TypeError("family must be a list")
if not family:
raise ValueError("family must not be empty")
if not all(isinstance(row, list) for row in family):
raise TypeError("every element of family must be a list")
width = len(family[0])
if not all(len(row) == width for row in family):
raise ValueError("every row must have the same length")
if not isinstance(start, int) or not isinstance(end, int):
raise TypeError("start and end must be int")
print(f"My shape is : ({len(family)}, {width})")
result = family[start:end]
new_width = len(result[0]) if result else 0
print(f"My new shape is : ({len(result)}, {new_width})")
return result
def main() -> None:
"""Demonstrate slice_me on a small 2D list."""
try:
family = [[1.80, 78.4],
[2.15, 102.7],
[2.10, 98.5],
[1.88, 75.2]]
print(slice_me(family, 0, 2))
print(slice_me(family, 1, -2))
except (TypeError, ValueError) as error:
print(f"{type(error).__name__}: {error}")
if __name__ == "__main__":
main()