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
10 changes: 10 additions & 0 deletions exercise-1.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
def replace_last(numbers):
...
# Check the length of the list
if len(numbers) <= 1: # if length 'numbers' is less than or equal to 1: list = empty or list has only one element
return numbers # Return the list as it is
else:
return [numbers[-1]] + numbers[:-1] # Take last element to a new list, then take all element of origin list except the last one

print(replace_last([2, 3, 4, 1]))
print(replace_last([1, 2, 3, 4]))
print(replace_last([1]))
print(replace_last([]))
18 changes: 18 additions & 0 deletions exercise-2.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,20 @@
def index_power(numnbers, n):
...
result = 0
#condition if n is outside array of numnbers
if n >= len(numnbers):
result -= 1

else:
#caculate if n is inside array of numnbers
result = numnbers[n] ** n

return result


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) )


16 changes: 15 additions & 1 deletion exercise-3.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,16 @@
def remove_all_after(numbers, n):
...
if n in numbers:
# Find the index of 'n'
n_index = numbers.index(n)
# Return the list up to and including 'n'
return numbers[:n_index + 1]
else: # If 'n' is not found or the list is empty, return the original list
return numbers

print(remove_all_after([1, 2, 3, 4, 5], 3))
print(remove_all_after([1, 1, 2, 2, 3, 3], 2))
print(remove_all_after([], 5))




29 changes: 28 additions & 1 deletion exercise-4.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,29 @@
def chunking_by(numbers, chunck):
...

# For empty list
if len(number) == 0:
print("The list is empty!")



# create a list to store the result
chuck_result = []

# loop index in number range from 0 to all numbers with step
for index in range(0, len(numbers), chunck):

# create one chunk: in numbers, take first index till index + step
chuck = numbers[index:index + chunck]
chuck_result.append(chuck)

return chuck_result

# for checking
number = [1, 2, 3, 4, 5]
chunk_size = 3

print(chunking_by(number, chunk_size))




21 changes: 21 additions & 0 deletions exercise-5.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,23 @@
def reverse_ascending(numbers):
...
# CREATE THE EMPTY LIST TO STORE THE FINAL OUTPUT
result = []
# CREATE NEW CURRENT ASCENDING LIST
ascendinglist2 = []
# USE FOR LOOP THAT ITERATE EACH NUMBERS IN INPUTS
for num in numbers:
# CHECK IF CURRENT NUMBERS IS SMALLER
if ascendinglist2 and num <= ascendinglist2[-1]:
# IF CONDITION IS TRUE, IT'S REVERSE AND ADD TO RESULT
result.extend(reversed(ascendinglist2))
# RESET AND READY TO START A NEW LIST
ascendinglist2 = []
ascendinglist2.append(num)
# AFTER LOOP THIS LINE WILL ADD THE LAST SUBSEQUENCES TO THE RESULT
result.extend(reversed(ascendinglist2))
return result

print (reverse_ascending([1, 2, 3, 4, 5]))
print(reverse_ascending([5, 7, 10, 4, 2, 7, 8, 1, 3]))
print (reverse_ascending([5, 4, 3, 2, 1]))
print (reverse_ascending([]))