Author archives

Greater than neighbours

Greater than neighbours. Given a list of numbers, determine and print the number of elements that are greater than both of their neighbors. The first and the last elements of the list should not be considered because they don’t have two neighbors. Test Case Input (List of Numbers) Expected Output 1 1, 2, 3, 4, …

Neighbors of the same sign

Neighbors of the same sign. Given a list of numbers, find and print the first pair of adjacent elements that have the same sign. If no such pair exists, leave the output blank. Test Case Input (List of Numbers) Expected Output 1 1, -2, -3, 4 -2, -3 2 3, 5, -1, -2 3, 5 …

Greater than previous

Greater than previous. Given a list of numbers, find and print all the elements that are greater than the previous element in the list. Test Case Input (List of Numbers) Expected Output 1 1, 2, 1, 3, 2, 4 2, 3, 4 2 5, 3, 8, 6, 10, 2 8, 10 3 7, 9, 5, …

Even elements

Even elements. Given a list of numbers, find and print all elements that are even numbers. Use a for-loop that iterates directly over the list elements, not over their indices (i.e., do not use range()). Test Case Input (List of Numbers) Expected Output 1 1, 2, 3, 4, 5 2, 4 2 10, 15, 20, …

Even indices

Even indices. Given a list of numbers, find and print all the elements that are located at even index positions (i.e., A[0], A[2], A[4], …). Test Case Input (List of Numbers) Expected Output 1 1, 2, 3, 4, 5 1, 3, 5 2 10, 20, 30, 40, 50, 60 10, 30, 50 3 7, 14, …

The index of a Fibonacci number

The index of a Fibonacci number. The Fibonacci sequence is defined as follows: ϕ0=0, ϕ1=1, ϕn=ϕn−1+ϕn−2. Given an integer a, determine its index among the Fibonacci numbers. Print the number n such that ϕn=a. If a is not a Fibonacci number, print -1. Test Case Input (a) Expected Output 1 0 0 2 1 1 3 10 -1 …

Fibonacci numbers

Fibonacci numbers. The Fibonacci sequence is defined as follows: ϕ0=0, ϕ1=1, ϕn=ϕn−1+ϕn−2. Given a non-negative integer n, print the nth Fibonacci number ϕn. Solve this problem using a for loop. Test Case Input (n) Expected Output 1 0 0 2 1 1 3 10 55  

The second maximum

The second maximum. The sequence consists of distinct positive integers and ends with the number 0. Determine the value of the second largest element in this sequence. It is guaranteed that the sequence has at least two elements. Test Case Input (Sequence) Expected Output 1 1 2 3 0 2 2 10 5 8 0 …