diff --git a/introductory.go b/introductory.go index e8e4f63..4c57c66 100644 --- a/introductory.go +++ b/introductory.go @@ -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) +} diff --git a/introductory_test.go b/introductory_test.go index e648f48..e88f0d4 100644 --- a/introductory_test.go +++ b/introductory_test.go @@ -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))) +}