Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added __pycache__/exercise_1.cpython-311.pyc
Binary file not shown.
Binary file added __pycache__/exercise_1.cpython-312.pyc
Binary file not shown.
Binary file added __pycache__/exercise_1.cpython-313.pyc
Binary file not shown.
Binary file added __pycache__/exercise_2.cpython-312.pyc
Binary file not shown.
Binary file added __pycache__/exercise_2.cpython-313.pyc
Binary file not shown.
Binary file added __pycache__/exercise_3.cpython-312.pyc
Binary file not shown.
Binary file added __pycache__/exercise_4.cpython-311.pyc
Binary file not shown.
Binary file added __pycache__/exercise_4.cpython-312.pyc
Binary file not shown.
Binary file added __pycache__/exercise_4.cpython-313.pyc
Binary file not shown.
Binary file added __pycache__/test_1.cpython-311-pytest-7.4.0.pyc
Binary file not shown.
Binary file added __pycache__/test_1.cpython-312-pytest-8.3.2.pyc
Binary file not shown.
Binary file added __pycache__/test_1.cpython-313-pytest-8.3.4.pyc
Binary file not shown.
Binary file added __pycache__/test_2.cpython-312-pytest-8.3.2.pyc
Binary file not shown.
Binary file added __pycache__/test_2.cpython-313-pytest-8.3.4.pyc
Binary file not shown.
Binary file added __pycache__/test_3.cpython-312-pytest-8.3.2.pyc
Binary file not shown.
Binary file added __pycache__/test_4.cpython-311-pytest-7.4.0.pyc
Binary file not shown.
Binary file added __pycache__/test_4.cpython-311-pytest-8.3.5.pyc
Binary file not shown.
Binary file added __pycache__/test_4.cpython-312-pytest-8.3.2.pyc
Binary file not shown.
Binary file added __pycache__/test_4.cpython-313-pytest-8.3.4.pyc
Binary file not shown.
Binary file added __pycache__/test_5.cpython-312-pytest-8.3.2.pyc
Binary file not shown.
2 changes: 0 additions & 2 deletions exercise-1.py

This file was deleted.

2 changes: 0 additions & 2 deletions exercise-2.py

This file was deleted.

2 changes: 0 additions & 2 deletions exercise-3.py

This file was deleted.

2 changes: 0 additions & 2 deletions exercise-4.py

This file was deleted.

2 changes: 0 additions & 2 deletions exercise-5.py

This file was deleted.

15 changes: 15 additions & 0 deletions exercise_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def replace_last(numbers):
if len(numbers) <= 1:
return numbers

last_item = numbers[-1]

other_numbers = numbers[:-1]

return [last_item] + other_numbers

# Testing the function
print(replace_last([2, 3, 4, 1]))
print(replace_last([1, 2, 3, 4]))
print(replace_last([1]))
print(replace_last([]))
12 changes: 12 additions & 0 deletions exercise_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def index_power(numbers, n):
if n < 0 or n >= len(numbers):
return -1

f = numbers[n]

return f ** n

print(index_power([1, 2, 3, 4], 2))
print(index_power([1, 3, 10, 100], 3))
print(index_power([0, 1], 0))
print(index_power([1, 2], 3))
12 changes: 12 additions & 0 deletions exercise_3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def remove_all_after(numbers, n):
if not numbers:
return numbers

if n in numbers:
index = numbers.index(n)
return numbers[:index + 1]
else:
return numbers

print(remove_all_after([1, 2, 3, 4, 5], 3))
print(remove_all_after([1, 1, 2, 2, 3, 3], 2))
14 changes: 14 additions & 0 deletions exercise_4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def chunking_by(numbers, chunk):
new_list = []
if not isinstance(chunk, int):
return "invalid size"
elif chunk <= 0:
return []

for i in range(0, len(numbers), chunk):
chunks = numbers[i:i + chunk]
new_list.append(chunks)
return new_list

call = chunking_by([1,2,3,4,5,6,7], 3)
print(call)
32 changes: 32 additions & 0 deletions exercise_5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
def reverse_ascending(items):
result = []

if len(items) == 0:
return result

sublist = [items[0]]

i = 1
while i < len(items):
if items[i] > items[i - 1]:
sublist.append(items[i])
else:
# reverse the sublist manually
reversed_sublist = []
j = len(sublist) - 1
while j >= 0:
reversed_sublist.append(sublist[j])
j = j - 1
result += reversed_sublist
sublist = [items[i]]
i = i + 1

# handle last sublist
reversed_sublist = []
j = len(sublist) - 1
while j >= 0:
reversed_sublist.append(sublist[j])
j = j - 1
result += reversed_sublist

return result
24 changes: 24 additions & 0 deletions test_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from exercise_1 import replace_last

def test_replace_last_multiple_items():
assert replace_last([2, 3, 4, 1]) == [1, 2, 3, 4]
assert replace_last([1, 2, 3, 4]) == [4, 1, 2, 3]
assert replace_last([10, 20, 30]) == [30, 10, 20]

def test_replace_last_single_item():
assert replace_last([1]) == [1]
assert replace_last([100]) == [100]

def test_replace_last_empty_list():
assert replace_last([]) == []

def test_replace_last_edge_cases():
assert replace_last([5, 6]) == [6, 5]
assert replace_last(['a', 'b', 'c']) == ['c', 'a', 'b']
assert replace_last([True, False]) == [False, True]

def test_replace_last_original_unchanged():
original = [1, 2, 3]
replaced = replace_last(original)
assert original == [1, 2, 3]
assert replaced == [3, 1, 2]
29 changes: 29 additions & 0 deletions test_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from exercise_2 import index_power

def test_valid_index():
assert index_power([1, 2, 3, 4], 2) == 9
assert index_power([1, 3, 10, 100], 3) == 1000000

def test_index_out_of_bounds():
assert index_power([1, 2, 3], 3) == -1
assert index_power([1, 2], 3) == -1

def test_zero_index():
assert index_power([0, 1], 0) == 1
assert index_power([5, 6, 7], 0) == 1

def test_negative_index():
assert index_power([1, 2, 3], -1) == -1
assert index_power([1, 2, 3], -5) == -1

def test_large_index():
assert index_power([1, 2, 3, 4, 5], 10) == -1
assert index_power([10, 20, 30], 5) == -1

def test_single_element_array():
assert index_power([5], 0) == 1
assert index_power([10], 1) == -1

def test_power_of_zero():
assert index_power([0, 1, 2], 0) == 1
assert index_power([0, 1, 2], 1) == 1
26 changes: 26 additions & 0 deletions test_3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from exercise_3 import remove_all_after


def test_remove_all_after():
assert remove_all_after([1, 2, 3, 4, 5], 3) == [1, 2, 3]

assert remove_all_after([1, 2, 3, 4, 5], 6) == [1, 2, 3, 4, 5]

assert remove_all_after([], 1) == []

assert remove_all_after([1, 1, 2, 2, 3, 3], 2) == [1, 1, 2]

assert remove_all_after([1, 2, 3, 4, 5], 5) == [1, 2, 3, 4, 5]

assert remove_all_after([1, 2, 3, 4, 5], 1) == [1]

assert remove_all_after([2, 2, 2, 2], 2) == [2]

assert remove_all_after([2, 2, 2, 2], 3) == [2, 2, 2, 2]

large_list = list(range(1000))
assert len(remove_all_after(large_list, 500)) == 501

print("All Test Have Passed.")

test_remove_all_after()
25 changes: 25 additions & 0 deletions test_4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from exercise_4 import chunking_by

def test_chunking_by():
# Test Case 1: Normal case with chunk size 3
assert chunking_by([5, 4, 7, 3, 4, 5, 4], 3) == [[5, 4, 7], [3, 4, 5], [4]]

# Test Case 2: Chunk size 1
assert chunking_by([3, 4, 5], 1) == [[3], [4], [5]]

# Test Case 3: Empty list should return empty list
assert chunking_by([], 3) == []

# Test Case 4: Chunk size larger than list length
assert chunking_by([1, 2], 5) == [[1, 2]]

# Test Case 5: Chunk size equal to list length
assert chunking_by([1, 2, 3, 4], 4) == [[1, 2, 3, 4]]

# Test Case 6: Chunk size 0 (edge case, but as per problem statement, last chunk can be smaller, but size is positive)
assert chunking_by([1, 2, 3], 0) == []

print("All test cases passed!")

if __name__ == "__main__":
test_chunking_by()
37 changes: 37 additions & 0 deletions test_5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from exercise_5 import reverse_ascending

def test_reverse_ascending():
# Test Case 1: Example from the problem statement
input1 = [1, 2, 3, 2, 4, 5, 6, 3]
expected_output1 = [3, 2, 1, 6, 5, 4, 2, 3]
assert reverse_ascending(input1) == expected_output1, f"Test Case 1 Failed: Expected {expected_output1}, got {reverse_ascending(input1)}"

# Test Case 2: Entire list is strictly descending
input2 = [5, 4, 3, 2, 1]
expected_output2 = [5, 4, 3, 2, 1]
assert reverse_ascending(input2) == expected_output2, f"Test Case 2 Failed: Expected {expected_output2}, got {reverse_ascending(input2)}"

# Test Case 3: All elements are the same
input3 = [1, 1, 1]
expected_output3 = [1, 1, 1]
assert reverse_ascending(input3) == expected_output3, f"Test Case 3 Failed: Expected {expected_output3}, got {reverse_ascending(input3)}"

# Test Case 4: Empty list
input4 = []
expected_output4 = []
assert reverse_ascending(input4) == expected_output4, f"Test Case 4 Failed: Expected {expected_output4}, got {reverse_ascending(input4)}"

# Test Case 5: Single element
input5 = [7]
expected_output5 = [7]
assert reverse_ascending(input5) == expected_output5, f"Test Case 5 Failed: Expected {expected_output5}, got {reverse_ascending(input5)}"

# Test Case 6: Mixed sequence with multiple ascending subsequences
input6 = [1, 3, 2, 4, 6, 5, 3, 7, 8]
expected_output6 = [3, 1, 6, 4, 2, 5, 8, 7, 3]
assert reverse_ascending(input6) == expected_output6, f"Test Case 6 Failed: Expected {expected_output6}, got {reverse_ascending(input6)}"

print("All test cases passed!")

# Run the test cases
test_reverse_ascending()