From 82f7296e6e32c2127a286e592e31901def24ac28 Mon Sep 17 00:00:00 2001 From: oabrivard Date: Mon, 9 Oct 2023 09:04:34 +0200 Subject: [PATCH] Solved Repetitions and Increasing Array exercises --- introductory.go | 33 +++++++++++++++++++++++++++++++++ introductory_test.go | 12 ++++++++++++ 2 files changed, 45 insertions(+) diff --git a/introductory.go b/introductory.go index ab0688e..e8e4f63 100644 --- a/introductory.go +++ b/introductory.go @@ -45,3 +45,36 @@ func MissingNumber(n int, numbers []int) int { return -3 } + +func Repetitions(dna string) int { + curC := ' ' + maxL := 0 + curL := 0 + + for _, c := range dna { + if c == curC { + curL += 1 + } else { + curC = c + curL = 1 + } + + if curL > maxL { + maxL = curL + } + } + + return maxL +} + +func IncreasingArray(size int, arr []int) int { + moves := 0 + + for i := 0; i < len(arr)-1; i++ { + if arr[i] > arr[i+1] { + moves += arr[i] - arr[i+1] + } + } + + return moves +} diff --git a/introductory_test.go b/introductory_test.go index a20c4fa..e648f48 100644 --- a/introductory_test.go +++ b/introductory_test.go @@ -14,3 +14,15 @@ func TestMissingNumber(t *testing.T) { assert.Equal(t, 4, MissingNumber(5, []int{2, 3, 1, 5})) assert.Equal(t, 7, MissingNumber(9, []int{2, 3, 1, 5, 4, 6, 9, 8})) } + +func TestRepetitions(t *testing.T) { + assert.Equal(t, 0, Repetitions("")) + assert.Equal(t, 3, Repetitions("ATTCGGGA")) + assert.Equal(t, 4, Repetitions("AGGTTCGGGGA")) +} + +func TestIncreasingArray(t *testing.T) { + assert.Equal(t, 0, IncreasingArray(5, []int{})) + assert.Equal(t, 0, IncreasingArray(5, []int{3})) + assert.Equal(t, 5, IncreasingArray(5, []int{3, 2, 5, 1, 7})) +}