You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
139 lines
1.7 KiB
Go
139 lines
1.7 KiB
Go
package cses
|
|
|
|
import "fmt"
|
|
|
|
func WeirdAlgo(n int) string {
|
|
seq := []int{}
|
|
|
|
for n != 1 {
|
|
seq = append(seq, n)
|
|
|
|
if n%2 == 0 {
|
|
n /= 2
|
|
} else {
|
|
n = n*3 + 1
|
|
}
|
|
}
|
|
|
|
seq = append(seq, 1)
|
|
|
|
return fmt.Sprint(seq)
|
|
}
|
|
|
|
func MissingNumber(n int, numbers []int) int {
|
|
|
|
if n != len(numbers)+1 {
|
|
return -1
|
|
}
|
|
|
|
present := make([]bool, n)
|
|
|
|
for _, i := range numbers {
|
|
|
|
if i > n {
|
|
return -2
|
|
}
|
|
|
|
present[i-1] = true
|
|
}
|
|
|
|
for i, b := range present {
|
|
if !b {
|
|
return i + 1
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
func NumberSpiral(row, col int) int64 {
|
|
result := int64(1)
|
|
var size int
|
|
|
|
if row > col {
|
|
size = row
|
|
} else {
|
|
size = col
|
|
}
|
|
|
|
for i := 2; i <= size; i++ {
|
|
result = result + int64(2*(i-1))
|
|
}
|
|
|
|
switch {
|
|
case col > row:
|
|
if col%2 == 1 {
|
|
result = result + int64(size-row)
|
|
} else {
|
|
result = result + int64(row-size)
|
|
}
|
|
case col < row:
|
|
if row%2 == 1 {
|
|
result = result + int64(col-size)
|
|
} else {
|
|
result = result + int64(size-col)
|
|
}
|
|
}
|
|
|
|
return result
|
|
}
|