194 lines
4.5 KiB
Go
194 lines
4.5 KiB
Go
|
package main_test
|
||
|
|
||
|
import (
|
||
|
"testing"
|
||
|
|
||
|
"github.com/matryer/is"
|
||
|
)
|
||
|
|
||
|
// func BenchmarkChan(b *testing.B) {
|
||
|
// sum := 0
|
||
|
|
||
|
// for n := range countChan(b.N) {
|
||
|
// sum += n
|
||
|
// }
|
||
|
// b.StopTimer()
|
||
|
// b.Log(sum)
|
||
|
// }
|
||
|
|
||
|
// func BenchmarkScan(b *testing.B) {
|
||
|
// sum := 0
|
||
|
// scan, value := countScan(b.N)
|
||
|
// for scan() {
|
||
|
// sum += value()
|
||
|
// }
|
||
|
// b.StopTimer()
|
||
|
// b.Log(sum)
|
||
|
// }
|
||
|
|
||
|
// func BenchmarkRange(b *testing.B) {
|
||
|
// sum := 0
|
||
|
// for n := range countRF(b.N) {
|
||
|
// sum += n
|
||
|
// }
|
||
|
// b.StopTimer()
|
||
|
// b.Log(sum)
|
||
|
// }
|
||
|
|
||
|
// func TestRangeS(t *testing.T) {
|
||
|
// for i := range Step(0, 10, 3) {
|
||
|
// t.Log(i)
|
||
|
// }
|
||
|
|
||
|
// for i := range Step(10, 0, -3) {
|
||
|
// t.Log(i)
|
||
|
// }
|
||
|
// }
|
||
|
|
||
|
func TestSelect(t *testing.T) {
|
||
|
done1 := make(chan struct{})
|
||
|
done2 := make(chan struct{})
|
||
|
|
||
|
go func() {
|
||
|
close(done1)
|
||
|
close(done2)
|
||
|
}()
|
||
|
|
||
|
for i, ch := range []chan struct{}{done1, done2} {
|
||
|
<-ch
|
||
|
println("closed", i)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestGCD(t *testing.T) {
|
||
|
is := is.New(t)
|
||
|
is.Equal(gcdOfStrings("ABAB", "ABABAB"), "AB")
|
||
|
is.Equal(gcdOfStrings("ABAB", "ABABABAB"), "ABAB")
|
||
|
is.Equal(gcdOfStrings("ABC", "ABCABC"), "ABC")
|
||
|
is.Equal(gcdOfStrings("ABC", "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AAAAAAAAA"), "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
|
||
|
}
|
||
|
func gcdOfStrings(str1 string, str2 string) string {
|
||
|
if len(str1) == 0 {
|
||
|
return ""
|
||
|
}
|
||
|
runes1 := []rune(str1)
|
||
|
runes2 := []rune(str2)
|
||
|
|
||
|
n := max(len(runes1), len(runes2))
|
||
|
m := min(len(runes1), len(runes2))
|
||
|
|
||
|
if len(runes2) == m {
|
||
|
runes1, runes2 = runes2, runes1
|
||
|
}
|
||
|
|
||
|
if isRepeating(runes1, runes2) {
|
||
|
return string(runes1)
|
||
|
}
|
||
|
|
||
|
for i := n; i > 0; i-- {
|
||
|
if runes1[i%len(runes1)] != runes2[i%len(runes2)] {
|
||
|
return ""
|
||
|
}
|
||
|
gcd := runes2[:i]
|
||
|
if isRepeating(gcd, runes1) && isRepeating(gcd, runes2) {
|
||
|
return string(gcd)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return ""
|
||
|
}
|
||
|
func isRepeating(str1, str2 []rune) bool {
|
||
|
if len(str1) > len(str2) {
|
||
|
return false
|
||
|
}
|
||
|
if len(str2)%len(str1) != 0 {
|
||
|
return false
|
||
|
}
|
||
|
for i := range str2 {
|
||
|
if str1[i%len(str1)] != str2[i] {
|
||
|
return false
|
||
|
}
|
||
|
}
|
||
|
return true
|
||
|
}
|
||
|
|
||
|
func canPlaceFlowers(flowerbed []int, n int) bool {
|
||
|
var c int
|
||
|
|
||
|
for i := 0; i < len(flowerbed); i++ {
|
||
|
if (i == 0 || flowerbed[i-1] == 0) &&
|
||
|
flowerbed[i] == 0 &&
|
||
|
(i == len(flowerbed)-1 || flowerbed[i+1] == 0) {
|
||
|
c++
|
||
|
flowerbed[i] = 1
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return c >= n
|
||
|
}
|
||
|
|
||
|
func TestFlowers(t *testing.T) {
|
||
|
is := is.New(t)
|
||
|
is.Equal(canPlaceFlowers([]int{1, 0, 0, 0, 1}, 1), true)
|
||
|
is.Equal(canPlaceFlowers([]int{0, 0, 1, 0, 1}, 1), true)
|
||
|
is.Equal(canPlaceFlowers([]int{1, 0, 1, 0, 0}, 1), true)
|
||
|
}
|
||
|
|
||
|
func dailyTemperatures(temperatures []int) []int {
|
||
|
answers := make([]int, len(temperatures))
|
||
|
var stack [][2]int
|
||
|
push := func(v int) {
|
||
|
if len(stack) == 0 {
|
||
|
stack = append(stack, [2]int{v, 1})
|
||
|
return
|
||
|
}
|
||
|
end := len(stack)-1
|
||
|
if v < stack[end][0]{
|
||
|
stack = append(stack, [2]int{v, 1})
|
||
|
return
|
||
|
|
||
|
}
|
||
|
if v == stack[end][0] {
|
||
|
stack[len(stack)-1][1]++
|
||
|
return
|
||
|
}
|
||
|
|
||
|
var vs = [2]int{v, 1}
|
||
|
// for ; end>0; end-- {
|
||
|
// if stack[end][0]<vs[0] {
|
||
|
// vs[1]+=stack[end][1]
|
||
|
// continue
|
||
|
// }
|
||
|
// break
|
||
|
// }
|
||
|
stack = append(stack, vs)
|
||
|
}
|
||
|
|
||
|
for i := len(temperatures) - 2; i >= 0; i-- {
|
||
|
push(temperatures[i+1])
|
||
|
currentTemp := temperatures[i]
|
||
|
days := 1
|
||
|
end := len(stack)-1
|
||
|
for j := range stack {
|
||
|
nextTemp := stack[end-j]
|
||
|
if currentTemp < nextTemp[0] {
|
||
|
answers[i] = days
|
||
|
break
|
||
|
}
|
||
|
days+=nextTemp[1]
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return answers
|
||
|
}
|
||
|
|
||
|
func TestTemps(t *testing.T) {
|
||
|
is := is.New(t)
|
||
|
is.Equal(dailyTemperatures(
|
||
|
[]int{73, 74, 75, 71, 69, 72, 76, 73}),
|
||
|
[]int{1, 1, 4, 2, 1, 1, 0, 0})
|
||
|
is.Equal(dailyTemperatures(
|
||
|
[]int{99,99,99,99,100}), []int{4,3,2,1,0})
|
||
|
}
|