# %% About
# - Name: Star Recap Range
# - Difficulty: medium
# - Lines: 25
# - Minutes: 13
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% English
# 1. Write own implementation of a built-in function `range()`,
# example usage: `myrange(10)`, `myrange(0, 10)` or `myrange(0, 10, 2)`
# 2. Note, that function does not take any keyword arguments
# 3. How to implement passing only stop argument, i.e. `myrange(10)`?
# 4. Use length check of `*args` and `**kwargs`
# 5. Error messages must be consistent with built-in function `range()`:
# - myrange() -> `TypeError: range expected at least 1 argument, got 0`
# - myrange(1,2,3,4) -> `TypeError: range expected at most 3 arguments, got 4`
# - myrange(stop=2) -> `TypeError: range() takes no keyword arguments`
# - myrange(start=1, stop=2) -> `TypeError: range() takes no keyword arguments`
# - myrange(start=1, stop=2, step=2) -> `TypeError: range() takes no keyword arguments`
# 6. Run doctests - all must succeed
# %% Polish
# 1. Zaimplementuj własne rozwiązanie wbudowanej funkcji `range()`,
# przykład użycia: `myrange(10)`, `myrange(0, 10)` lub `myrange(0, 10, 2)`
# 2. Zauważ, że funkcja nie przyjmuje żadnych argumentów nazwanych (keyword)
# 3. Jak zaimplementować możliwość podawania tylko końca, tj. `myrange(10)`?
# 4. Użyj sprawdzania długości `*args` i `**kwargs`
# 5. Komunikaty błędów muszą być zgodne z wbudowaną funkcją `range()`:
# - myrange() -> `TypeError: range expected at least 1 argument, got 0`
# - myrange(1,2,3,4) -> `TypeError: range expected at most 3 arguments, got 4`
# - myrange(stop=2) -> `TypeError: range() takes no keyword arguments`
# - myrange(start=1, stop=2) -> `TypeError: range() takes no keyword arguments`
# - myrange(start=1, stop=2, step=2) -> `TypeError: range() takes no keyword arguments`
# 6. Uruchom doctesty - wszystkie muszą się powieść
# %% Hints
# - https://github.com/python/cpython/blob/main/Objects/rangeobject.c#LC75
# - `raise TypeError('error message')`
# - `if len(args) == ...`
# %% Example
# >>> myrange(0, 10, 2)
# [0, 2, 4, 6, 8]
#
# >>> myrange(0, 5)
# [0, 1, 2, 3, 4]
#
# >>> myrange(5)
# [0, 1, 2, 3, 4]
#
# >>> myrange()
# Traceback (most recent call last):
# TypeError: myrange expected at least 1 argument, got 0
#
# >>> myrange(1,2,3,4)
# Traceback (most recent call last):
# TypeError: myrange expected at most 3 arguments, got 4
#
# >>> myrange(stop=2)
# Traceback (most recent call last):
# TypeError: myrange() takes no keyword arguments
#
# >>> myrange(start=1, stop=2)
# Traceback (most recent call last):
# TypeError: myrange() takes no keyword arguments
#
# >>> myrange(start=1, stop=2, step=2)
# Traceback (most recent call last):
# TypeError: myrange() takes no keyword arguments
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> from inspect import isfunction
>>> assert isfunction(myrange)
>>> myrange(0, 10, 2)
[0, 2, 4, 6, 8]
>>> myrange(0, 5)
[0, 1, 2, 3, 4]
>>> myrange(5)
[0, 1, 2, 3, 4]
>>> myrange()
Traceback (most recent call last):
TypeError: myrange expected at least 1 argument, got 0
>>> myrange(1,2,3,4)
Traceback (most recent call last):
TypeError: myrange expected at most 3 arguments, got 4
>>> myrange(1,2,3,4,5)
Traceback (most recent call last):
TypeError: myrange expected at most 3 arguments, got 5
>>> myrange(stop=2)
Traceback (most recent call last):
TypeError: myrange() takes no keyword arguments
>>> myrange(start=1, stop=2)
Traceback (most recent call last):
TypeError: myrange() takes no keyword arguments
>>> myrange(start=1, stop=2, step=2)
Traceback (most recent call last):
TypeError: myrange() takes no keyword arguments
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% Imports
# %% Types
from typing import Callable
myrange: Callable[[int,int,int], list[int]]
# %% Data
# %% Result
def myrange(*args, **kwargs):
if kwargs:
raise TypeError('myrange() takes no keyword arguments')
current = start
result = []
while current < stop:
result.append(current)
current += step
return result