Solved Beautiful Permutations exercise

main
oabrivard 2 years ago
parent 82f7296e6e
commit 98863de0ab

@ -78,3 +78,29 @@ func IncreasingArray(size int, arr []int) int {
return moves
}
func BeautifulPermutations(n int) string {
result := []int{}
if n <= 3 {
return "NO SOLUTION"
}
if n == 4 {
result = []int{3, 1, 4, 2}
} else {
curr := n
switched := false
for i := 1; i <= n; i++ {
result = append(result, curr)
if curr < 3 && !switched {
curr = n - 1
switched = true
} else {
curr = curr - 2
}
}
}
return fmt.Sprint(result)
}

@ -26,3 +26,15 @@ func TestIncreasingArray(t *testing.T) {
assert.Equal(t, 0, IncreasingArray(5, []int{3}))
assert.Equal(t, 5, IncreasingArray(5, []int{3, 2, 5, 1, 7}))
}
func TestBeautifulPermutations(t *testing.T) {
assert.Equal(t, "NO SOLUTION", BeautifulPermutations(2))
assert.Equal(t, "NO SOLUTION", BeautifulPermutations(3))
assert.Equal(t, "[3 1 4 2]", BeautifulPermutations(4))
assert.Equal(t, "[5 3 1 4 2]", BeautifulPermutations(5))
assert.Equal(t, "[6 4 2 5 3 1]", BeautifulPermutations(6))
assert.Equal(t, "[7 5 3 1 6 4 2]", BeautifulPermutations(7))
assert.Equal(t, "[8 6 4 2 7 5 3 1]", BeautifulPermutations(8))
assert.Equal(t, "[9 7 5 3 1 8 6 4 2]", BeautifulPermutations(9))
assert.Equal(t, 48895, len(BeautifulPermutations(10000)))
}

Loading…
Cancel
Save