diff --git a/exercise-1.py b/exercise-1.py index 91a8563..5d21832 100644 --- a/exercise-1.py +++ b/exercise-1.py @@ -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([])) \ No newline at end of file diff --git a/exercise-2.py b/exercise-2.py index 037cd6a..967ba0b 100644 --- a/exercise-2.py +++ b/exercise-2.py @@ -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) ) + + diff --git a/exercise-3.py b/exercise-3.py index c185b7b..2042b10 100644 --- a/exercise-3.py +++ b/exercise-3.py @@ -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)) + + + + diff --git a/exercise-4.py b/exercise-4.py index de21f7d..f3c9752 100644 --- a/exercise-4.py +++ b/exercise-4.py @@ -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)) + + + + diff --git a/exercise-5.py b/exercise-5.py index d49bd43..cca371d 100644 --- a/exercise-5.py +++ b/exercise-5.py @@ -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([]))