Slices
-
Implement a function
func removeEven(s []int) []int
that removes even numbers from the input slice, and returns a new slice with the remaining numbers in the same order as a result.Example:
removeEven([]int{5, 2, 3, 6, 2, 3}) -> []int{5, 3, 3} removeEven([]int{1, 3, 5}) -> []int{1, 3, 5} removeEven([]int{2, 4, 6}) -> []int{}
-
Implement a function
func generateFibonacci(n int) []int
that returns a slice containing the firstn
elements of the Fibonacci sequence (wiki).The Fibonacci sequence is a sequence of integers, where the next number is calculated as the sum of two previous numbers, i.e. $F_n = F_{n-2} + F_{n-1}$. Assume that $F_1 = 0$ and $F_2 = 1$.
Example:
generateFibonacci(10) -> []int{0, 1, 1, 2, 3, 5, 8, 13, 21, 34}
-
Implement a function
func removeAdjacent(s []int) []int
that removes adjacent duplicate elements from the input slice.Example:
removeAdjacent([]int{1, 3, 3, 2, 1, 3, 5, 3, 4, 4, 2}) -> []int{1, 3, 2, 1, 3, 5, 3, 4, 2}
-
Implement a function
func mergeIncreasingSlices(a, b []int) []int
, which is given two slices containing integers in an increasing order as the input. The function must return a slices that contains integers from the input array in the increasing order.Example:
mergeIncreasingSlices([]int{1, 5, 5, 7}, []int{1, 2, 4, 6, 9}) -> []int{1, 1, 2, 4, 5, 5, 6, 7, 9}
-
Implement a function
func josephus(soldiers, skip int) int
that solves the Josephus problem (wiki):Soldiers are standing in a circle waiting to be executed. Counting begins from the first soldier in the circle and proceeds around the circle in a specified direction. After a specified number of soldiers are skipped, the next soldier is executed. The procedure is repeated with the remaining soldiers, starting with the next soldier, going in the same direction and skipping the same number of soldiers, until only one soldier remains, and is freed.
The problem — given the number of soldiers and number to be skipped — is to choose the position counted from the selected first soldier in the initial circle to avoid execution.
Examples:
josephus(13, 0) -> 11 # Example for the illustration above josephus(1, 0) -> 1 josephus(13, 0) -> 11 josephus(41, 1) -> 31 josephus(5, 1) -> 4 josephus(0, 0) -> 0 josephus(-1, 0) -> 0 josephus(1, -1) -> 0