mirror of https://github.com/01-edu/public.git
Browse Source
So we will have to look at the state of the files before and after this PR if we want to understand things. I think it will be better afterwards, except for the tests I broke.pull/533/head
Xavier Petit
4 years ago
committed by
xpetit
407 changed files with 1960 additions and 2480 deletions
@ -1,10 +1,12 @@ |
|||||||
package main |
package main |
||||||
|
|
||||||
import ( |
type Node struct { |
||||||
"../lib" |
Data int |
||||||
"./correct" |
} |
||||||
"./student" |
|
||||||
) |
func CreateElem(n *Node, value int) { |
||||||
|
n.Data = value |
||||||
|
} |
||||||
|
|
||||||
// the structs from the other packages
|
// the structs from the other packages
|
||||||
// struct just for the first exercise
|
// struct just for the first exercise
|
@ -1,14 +1,27 @@ |
|||||||
package main |
package correct |
||||||
|
|
||||||
import ( |
type TNode struct { |
||||||
"fmt" |
Val int |
||||||
"io" |
Left *TNode |
||||||
"math/rand" |
Right *TNode |
||||||
"strconv" |
} |
||||||
|
|
||||||
|
func Invert(root *TNode) { |
||||||
|
if root != nil { |
||||||
|
temp := root.Left |
||||||
|
root.Left = root.Right |
||||||
|
root.Right = temp |
||||||
|
|
||||||
|
Invert(root.Left) |
||||||
|
Invert(root.Right) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func InvertTree(root *TNode) *TNode { |
||||||
|
Invert(root) |
||||||
|
return root |
||||||
|
} |
||||||
|
|
||||||
"../lib" |
|
||||||
"./correct" |
|
||||||
) |
|
||||||
|
|
||||||
type stuNode = TNode |
type stuNode = TNode |
||||||
type solNode = correct.TNode |
type solNode = correct.TNode |
@ -1,12 +1,24 @@ |
|||||||
package main |
package correct |
||||||
|
|
||||||
import ( |
type TreeNodeM struct { |
||||||
"math/rand" |
Left *TreeNodeM |
||||||
"strconv" |
Val int |
||||||
|
Right *TreeNodeM |
||||||
|
} |
||||||
|
|
||||||
|
func MergeTrees(t1 *TreeNodeM, t2 *TreeNodeM) *TreeNodeM { |
||||||
|
if t1 == nil { |
||||||
|
return t2 |
||||||
|
} |
||||||
|
if t2 == nil { |
||||||
|
return t1 |
||||||
|
} |
||||||
|
t1.Val += t2.Val |
||||||
|
t1.Left = MergeTrees(t1.Left, t2.Left) |
||||||
|
t1.Right = MergeTrees(t1.Right, t2.Right) |
||||||
|
return t1 |
||||||
|
} |
||||||
|
|
||||||
"../lib" |
|
||||||
"./correct" |
|
||||||
) |
|
||||||
|
|
||||||
type stuTreeNode = TreeNodeM |
type stuTreeNode = TreeNodeM |
||||||
type solTreeNode = correct.TreeNodeM |
type solTreeNode = correct.TreeNodeM |
@ -1,9 +1,4 @@ |
|||||||
package main |
|
||||||
|
|
||||||
import ( |
|
||||||
"../lib" |
|
||||||
"./correct" |
|
||||||
) |
|
||||||
|
|
||||||
type stuNode = NodeAddL |
type stuNode = NodeAddL |
||||||
type solNode = correct.NodeAddL |
type solNode = correct.NodeAddL |
@ -1,12 +1,28 @@ |
|||||||
package main |
package correct |
||||||
|
|
||||||
import ( |
type TreeNodeL struct { |
||||||
"math/rand" |
Left *TreeNodeL |
||||||
"strconv" |
Val int |
||||||
|
Right *TreeNodeL |
||||||
|
} |
||||||
|
|
||||||
|
func IsSameTree(p *TreeNodeL, q *TreeNodeL) bool { |
||||||
|
if p == nil && q == nil { |
||||||
|
return true |
||||||
|
} |
||||||
|
return checkIfEq(p, q) |
||||||
|
} |
||||||
|
|
||||||
|
func checkIfEq(t1 *TreeNodeL, t2 *TreeNodeL) bool { |
||||||
|
if t1 == nil && t2 == nil { |
||||||
|
return true |
||||||
|
} |
||||||
|
if t1 == nil || t2 == nil { |
||||||
|
return false |
||||||
|
} |
||||||
|
return t1.Val == t2.Val && checkIfEq(t1.Right, t2.Right) && checkIfEq(t1.Left, t2.Left) |
||||||
|
} |
||||||
|
|
||||||
"../lib" |
|
||||||
"./correct" |
|
||||||
) |
|
||||||
|
|
||||||
type stuTreeNode = TreeNodeL |
type stuTreeNode = TreeNodeL |
||||||
type solTreeNode = correct.TreeNodeL |
type solTreeNode = correct.TreeNodeL |
@ -1,12 +1,37 @@ |
|||||||
package main |
package correct |
||||||
|
|
||||||
import ( |
type Nodelist struct { |
||||||
"strconv" |
Data int |
||||||
|
Next *Nodelist |
||||||
|
} |
||||||
|
|
||||||
|
func SortList(l *Nodelist, cmp func(a, b int) bool) *Nodelist { |
||||||
|
head := l |
||||||
|
if head == nil { |
||||||
|
return nil |
||||||
|
} |
||||||
|
head.Next = SortList(head.Next, cmp) |
||||||
|
|
||||||
|
if head.Next != nil && cmp(head.Data, head.Next.Data) { |
||||||
|
head = moveValue(head, cmp) |
||||||
|
} |
||||||
|
return head |
||||||
|
} |
||||||
|
|
||||||
|
func moveValue(l *Nodelist, cmp func(a, b int) bool) *Nodelist { |
||||||
|
p := l |
||||||
|
n := l.Next |
||||||
|
ret := n |
||||||
|
|
||||||
|
for n != nil && cmp(l.Data, n.Data) { |
||||||
|
p = n |
||||||
|
n = n.Next |
||||||
|
} |
||||||
|
p.Next = l |
||||||
|
l.Next = n |
||||||
|
return ret |
||||||
|
} |
||||||
|
|
||||||
"../lib" |
|
||||||
"./correct" |
|
||||||
"./student" |
|
||||||
) |
|
||||||
|
|
||||||
func listToString4(n *correct.Nodelist) (res string) { |
func listToString4(n *correct.Nodelist) (res string) { |
||||||
it := n |
it := n |
@ -0,0 +1,26 @@ |
|||||||
|
package main |
||||||
|
|
||||||
|
import ( |
||||||
|
"sort" |
||||||
|
|
||||||
|
"github.com/01-edu/public/tests/go/lib" |
||||||
|
|
||||||
|
"./student" |
||||||
|
) |
||||||
|
|
||||||
|
// Receives 5 ints and returns the number in the middle
|
||||||
|
func abort(a, b, c, d, e int) int { |
||||||
|
arg := []int{a, b, c, d, e} |
||||||
|
sort.Ints(arg) |
||||||
|
return arg[2] |
||||||
|
} |
||||||
|
|
||||||
|
func main() { |
||||||
|
arg := lib.MultRandInt() |
||||||
|
arg = append(arg, lib.RandInt()) |
||||||
|
for i := 0; i < 15; i++ { |
||||||
|
lib.Challenge("Abort", student.Abort, abort, arg[0], arg[1], arg[2], arg[3], arg[4]) |
||||||
|
arg = lib.MultRandInt() |
||||||
|
arg = append(arg, lib.RandInt()) |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,22 @@ |
|||||||
|
package main |
||||||
|
|
||||||
|
import "./student" |
||||||
|
|
||||||
|
// Function that return the number of active bits in the number passed as the argument
|
||||||
|
func activeBits(n int) (total int) { |
||||||
|
for ; n > 1; n = n / 2 { |
||||||
|
total += n % 2 |
||||||
|
} |
||||||
|
total += n |
||||||
|
return |
||||||
|
} |
||||||
|
|
||||||
|
func main() { |
||||||
|
args := []int{lib.RandIntBetween(2, 20)} |
||||||
|
args = append(args, lib.MultRandIntBetween(2, 20)...) |
||||||
|
args = append(args, lib.MultRandIntBetween(2, 20)...) |
||||||
|
|
||||||
|
for _, v := range args { |
||||||
|
lib.Challenge("ActiveBits", student.ActiveBits, activeBits, v) |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,39 @@ |
|||||||
|
package main |
||||||
|
|
||||||
|
import ( |
||||||
|
"strings" |
||||||
|
"unicode" |
||||||
|
|
||||||
|
"./student" |
||||||
|
) |
||||||
|
|
||||||
|
func capitalize(s string) string { |
||||||
|
r := []rune(strings.ToLower(s)) |
||||||
|
|
||||||
|
if unicode.IsLower(r[0]) { |
||||||
|
r[0] = unicode.ToUpper(r[0]) |
||||||
|
} |
||||||
|
|
||||||
|
for i := 1; i < len(r); i++ { |
||||||
|
if !unicode.Is(unicode.ASCII_Hex_Digit, r[i-1]) && unicode.IsLower(r[i]) { |
||||||
|
r[i] = unicode.ToUpper(r[i]) |
||||||
|
} |
||||||
|
} |
||||||
|
return string(r) |
||||||
|
} |
||||||
|
|
||||||
|
func main() { |
||||||
|
table := append( |
||||||
|
lib.MultRandASCII(), |
||||||
|
"Hello! How are you? How+are+things+4you?", |
||||||
|
"Hello! How are you?", |
||||||
|
"a", |
||||||
|
"z", |
||||||
|
"!", |
||||||
|
"9a", |
||||||
|
"9a LALALA!", |
||||||
|
) |
||||||
|
for _, arg := range table { |
||||||
|
lib.Challenge("Capitalize", student.Capitalize, capitalize, arg) |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,37 @@ |
|||||||
|
package common |
||||||
|
|
||||||
|
import ( |
||||||
|
"math/big" |
||||||
|
"unicode" |
||||||
|
) |
||||||
|
|
||||||
|
func IsNumeric(s string) bool { |
||||||
|
for _, r := range s { |
||||||
|
if !unicode.IsDigit(r) { |
||||||
|
return false |
||||||
|
} |
||||||
|
} |
||||||
|
return true |
||||||
|
} |
||||||
|
|
||||||
|
func IsLower(s string) bool { |
||||||
|
for _, r := range s { |
||||||
|
if !unicode.IsLower(r) { |
||||||
|
return false |
||||||
|
} |
||||||
|
} |
||||||
|
return true |
||||||
|
} |
||||||
|
|
||||||
|
func IsUpper(s string) bool { |
||||||
|
for _, r := range s { |
||||||
|
if !(r >= 'A' && r <= 'Z') { |
||||||
|
return false |
||||||
|
} |
||||||
|
} |
||||||
|
return true |
||||||
|
} |
||||||
|
|
||||||
|
func IsPrime(i int) bool { |
||||||
|
return big.NewInt(int64(i)).ProbablyPrime(0) |
||||||
|
} |
@ -1,10 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
import "sort" |
|
||||||
|
|
||||||
// Receives 5 ints and returns the number in the middle
|
|
||||||
func Abort(a, b, c, d, e int) int { |
|
||||||
arg := []int{a, b, c, d, e} |
|
||||||
sort.Ints(arg) |
|
||||||
return arg[2] |
|
||||||
} |
|
@ -1,10 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
// Function that return the number of active bits in the number passed as the argument
|
|
||||||
func ActiveBits(n int) (total int) { |
|
||||||
for ; n > 1; n = n / 2 { |
|
||||||
total += n % 2 |
|
||||||
} |
|
||||||
total += n |
|
||||||
return |
|
||||||
} |
|
@ -1,9 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
import "sort" |
|
||||||
|
|
||||||
func AdvancedSortWordArr(a []string, f func(a, b string) int) { |
|
||||||
sort.Slice(a, func(i, j int) bool { |
|
||||||
return f(a[i], a[j]) < 0 |
|
||||||
}) |
|
||||||
} |
|
@ -1,12 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
import "unicode" |
|
||||||
|
|
||||||
func AlphaCount(s string) (i int) { |
|
||||||
for _, r := range s { |
|
||||||
if unicode.IsLetter(r) { |
|
||||||
i++ |
|
||||||
} |
|
||||||
} |
|
||||||
return i |
|
||||||
} |
|
@ -1,11 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
func Any(f func(string) bool, arr []string) bool { |
|
||||||
for _, el := range arr { |
|
||||||
if f(el) { |
|
||||||
return true |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
return false |
|
||||||
} |
|
@ -1,16 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
func AppendRange(min, max int) []int { |
|
||||||
size := max - min |
|
||||||
answer := []int{} |
|
||||||
|
|
||||||
if size <= 0 { |
|
||||||
return nil |
|
||||||
} |
|
||||||
|
|
||||||
for i := min; i < max; i++ { |
|
||||||
answer = append(answer, i) |
|
||||||
} |
|
||||||
|
|
||||||
return answer |
|
||||||
} |
|
@ -1,8 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
import "strconv" |
|
||||||
|
|
||||||
func Atoi(s string) int { |
|
||||||
n, _ := strconv.Atoi(s) |
|
||||||
return n |
|
||||||
} |
|
@ -1,8 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
import "strconv" |
|
||||||
|
|
||||||
func BasicAtoi(s string) int { |
|
||||||
n, _ := strconv.Atoi(s) |
|
||||||
return n |
|
||||||
} |
|
@ -1,8 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
import "strconv" |
|
||||||
|
|
||||||
func BasicAtoi2(s string) int { |
|
||||||
n, _ := strconv.Atoi(s) |
|
||||||
return n |
|
||||||
} |
|
@ -1,8 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
func BasicJoin(a []string) (b string) { |
|
||||||
for _, s := range a { |
|
||||||
b += s |
|
||||||
} |
|
||||||
return b |
|
||||||
} |
|
@ -1,21 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
import ( |
|
||||||
"strings" |
|
||||||
"unicode" |
|
||||||
) |
|
||||||
|
|
||||||
func Capitalize(s string) string { |
|
||||||
r := []rune(strings.ToLower(s)) |
|
||||||
|
|
||||||
if unicode.IsLower(r[0]) { |
|
||||||
r[0] = unicode.ToUpper(r[0]) |
|
||||||
} |
|
||||||
|
|
||||||
for i := 1; i < len(r); i++ { |
|
||||||
if !unicode.Is(unicode.ASCII_Hex_Digit, r[i-1]) && unicode.IsLower(r[i]) { |
|
||||||
r[i] = unicode.ToUpper(r[i]) |
|
||||||
} |
|
||||||
} |
|
||||||
return string(r) |
|
||||||
} |
|
@ -1,20 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
import "fmt" |
|
||||||
|
|
||||||
func Chunk(a []int, ch int) { |
|
||||||
slice := []int{} |
|
||||||
if ch <= 0 { |
|
||||||
fmt.Println() |
|
||||||
return |
|
||||||
} |
|
||||||
result := make([][]int, 0, len(a)/ch+1) |
|
||||||
for len(a) >= ch { |
|
||||||
slice, a = a[:ch], a[ch:] |
|
||||||
result = append(result, slice) |
|
||||||
} |
|
||||||
if len(a) > 0 { |
|
||||||
result = append(result, a[:len(a)]) |
|
||||||
} |
|
||||||
fmt.Println(result) |
|
||||||
} |
|
@ -1,20 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
func CollatzCountdown(start int) int { |
|
||||||
if start <= 0 { |
|
||||||
return -1 |
|
||||||
} |
|
||||||
|
|
||||||
steps := 0 |
|
||||||
|
|
||||||
for start != 1 { |
|
||||||
if start%2 == 0 { |
|
||||||
start = start / 2 |
|
||||||
} else { |
|
||||||
start = 3*start + 1 |
|
||||||
} |
|
||||||
steps++ |
|
||||||
} |
|
||||||
|
|
||||||
return steps |
|
||||||
} |
|
@ -1,14 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
func Compact(slice *[]string) int { |
|
||||||
count := 0 |
|
||||||
var compacted []string |
|
||||||
for _, v := range *slice { |
|
||||||
if v != "" { |
|
||||||
count++ |
|
||||||
compacted = append(compacted, v) |
|
||||||
} |
|
||||||
} |
|
||||||
*slice = compacted |
|
||||||
return count |
|
||||||
} |
|
@ -1,7 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
import "strings" |
|
||||||
|
|
||||||
func Compare(a, b string) int { |
|
||||||
return strings.Compare(a, b) |
|
||||||
} |
|
@ -1,5 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
func Concat(a, b string) string { |
|
||||||
return a + b |
|
||||||
} |
|
@ -1,7 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
import "strings" |
|
||||||
|
|
||||||
func ConcatParams(args []string) string { |
|
||||||
return strings.Join(args, "\n") |
|
||||||
} |
|
@ -1,12 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
func CountIf(f func(string) bool, arr []string) int { |
|
||||||
counter := 0 |
|
||||||
for _, el := range arr { |
|
||||||
if f(el) { |
|
||||||
counter++ |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
return counter |
|
||||||
} |
|
@ -1,9 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
type Node struct { |
|
||||||
Data int |
|
||||||
} |
|
||||||
|
|
||||||
func CreateElem(n *Node, value int) { |
|
||||||
n.Data = value |
|
||||||
} |
|
@ -1,6 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
func DivMod(a, b int, div, mod *int) { |
|
||||||
*div = a / b |
|
||||||
*mod = a % b |
|
||||||
} |
|
@ -1,7 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
import "strings" |
|
||||||
|
|
||||||
func DoppelGanger(s, substr string) int { |
|
||||||
return strings.LastIndex(s, substr) |
|
||||||
} |
|
@ -1,23 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
// this function will put a in c; c in d; d in b and b in a
|
|
||||||
func Enigma(a ***int, b *int, c *******int, d ****int) { |
|
||||||
valc := *******c |
|
||||||
*******c = ***a |
|
||||||
vald := ****d |
|
||||||
****d = valc |
|
||||||
valb := *b |
|
||||||
*b = vald |
|
||||||
***a = valb |
|
||||||
} |
|
||||||
|
|
||||||
// Helper function used in the test for checking the function Enigma()
|
|
||||||
func Decript(a ***int, b *int, c *******int, d ****int) { |
|
||||||
vala := ***a |
|
||||||
***a = *******c |
|
||||||
valb := *b |
|
||||||
*b = vala |
|
||||||
vald := ****d |
|
||||||
****d = valb |
|
||||||
*******c = vald |
|
||||||
} |
|
@ -1,16 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
func Fib(n int) int { |
|
||||||
if n <= 0 { |
|
||||||
return 0 |
|
||||||
} |
|
||||||
t1 := 0 |
|
||||||
t2 := 1 |
|
||||||
for i := 2; i <= n; i++ { |
|
||||||
t1 += t2 |
|
||||||
tmp := t1 |
|
||||||
t1 = t2 |
|
||||||
t2 = tmp |
|
||||||
} |
|
||||||
return t2 |
|
||||||
} |
|
@ -1,14 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
func Fibonacci(value int) int { |
|
||||||
if value < 0 { |
|
||||||
return -1 |
|
||||||
} |
|
||||||
if value == 0 { |
|
||||||
return 0 |
|
||||||
} |
|
||||||
if value == 1 { |
|
||||||
return 1 |
|
||||||
} |
|
||||||
return Fibonacci(value-1) + Fibonacci(value-2) |
|
||||||
} |
|
@ -1,8 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
func FindNextPrime(nb int) int { |
|
||||||
if isPrime(nb) { |
|
||||||
return nb |
|
||||||
} |
|
||||||
return FindNextPrime(nb + 1) |
|
||||||
} |
|
@ -1,24 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
func isPrime(nb int) bool { |
|
||||||
if nb <= 0 || nb == 1 { |
|
||||||
return false |
|
||||||
} |
|
||||||
|
|
||||||
for i := 2; i <= nb/2; i++ { |
|
||||||
if nb%i == 0 { |
|
||||||
return false |
|
||||||
} |
|
||||||
} |
|
||||||
return true |
|
||||||
} |
|
||||||
|
|
||||||
func FindPrevPrime(nb int) int { |
|
||||||
if nb < 2 { |
|
||||||
return 0 |
|
||||||
} |
|
||||||
if isPrime(nb) { |
|
||||||
return nb |
|
||||||
} |
|
||||||
return FindPrevPrime(nb - 1) |
|
||||||
} |
|
@ -1,6 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
func FirstRune(s string) rune { |
|
||||||
runes := []rune(s) |
|
||||||
return runes[0] |
|
||||||
} |
|
@ -1,11 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
import "fmt" |
|
||||||
|
|
||||||
func FoldInt(f func(int, int) int, a []int, n int) { |
|
||||||
result := n |
|
||||||
for _, v := range a { |
|
||||||
result = f(result, v) |
|
||||||
} |
|
||||||
fmt.Println(result) |
|
||||||
} |
|
@ -1,7 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
func ForEach(f func(int), a []int) { |
|
||||||
for _, el := range a { |
|
||||||
f(el) |
|
||||||
} |
|
||||||
} |
|
@ -1,17 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
func Game23(a, b int) int { |
|
||||||
if a > b { |
|
||||||
return -1 |
|
||||||
} |
|
||||||
if a == b { |
|
||||||
return 0 |
|
||||||
} |
|
||||||
if Game23(a*2, b) != -1 { |
|
||||||
return 1 + Game23(a*2, b) |
|
||||||
} |
|
||||||
if Game23(a*3, b) != -1 { |
|
||||||
return 1 + Game23(a*3, b) |
|
||||||
} |
|
||||||
return -1 |
|
||||||
} |
|
@ -1,10 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
func HalfContest(h1, m1, h2, m2 int) int { |
|
||||||
t1 := h1*60 + m1 |
|
||||||
t2 := h2*60 + m2 |
|
||||||
t2 = (t2 + t1) / 2 |
|
||||||
h2 = t2 / 60 |
|
||||||
m2 = t2 % 60 |
|
||||||
return h2*100 + m2 |
|
||||||
} |
|
@ -1,7 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
import "strings" |
|
||||||
|
|
||||||
func Index(s string, substr string) int { |
|
||||||
return strings.Index(s, substr) |
|
||||||
} |
|
@ -1,19 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
func isInteresting(n int) bool { |
|
||||||
s := 0 |
|
||||||
for n > 0 { |
|
||||||
s += n % 10 |
|
||||||
n /= 10 |
|
||||||
} |
|
||||||
return s%7 == 0 |
|
||||||
} |
|
||||||
|
|
||||||
func InterestingNumber(n int) int { |
|
||||||
for { |
|
||||||
if isInteresting(n) { |
|
||||||
return n |
|
||||||
} |
|
||||||
n++ |
|
||||||
} |
|
||||||
} |
|
@ -1,23 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
type TNode struct { |
|
||||||
Val int |
|
||||||
Left *TNode |
|
||||||
Right *TNode |
|
||||||
} |
|
||||||
|
|
||||||
func Invert(root *TNode) { |
|
||||||
if root != nil { |
|
||||||
temp := root.Left |
|
||||||
root.Left = root.Right |
|
||||||
root.Right = temp |
|
||||||
|
|
||||||
Invert(root.Left) |
|
||||||
Invert(root.Right) |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
func InvertTree(root *TNode) *TNode { |
|
||||||
Invert(root) |
|
||||||
return root |
|
||||||
} |
|
@ -1,12 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
import "unicode" |
|
||||||
|
|
||||||
func IsAlpha(s string) bool { |
|
||||||
for _, r := range s { |
|
||||||
if !unicode.Is(unicode.Hex_Digit, r) { |
|
||||||
return false |
|
||||||
} |
|
||||||
} |
|
||||||
return true |
|
||||||
} |
|
@ -1,23 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
func IsAnagram(s, t string) bool { |
|
||||||
alph := make([]int, 26) |
|
||||||
for i := 0; i < len(s); i++ { |
|
||||||
if s[i] < 'a' || s[i] > 'z' { |
|
||||||
continue |
|
||||||
} |
|
||||||
alph[s[i]-'a']++ |
|
||||||
} |
|
||||||
for i := 0; i < len(t); i++ { |
|
||||||
if t[i] < 'a' || t[i] > 'z' { |
|
||||||
continue |
|
||||||
} |
|
||||||
alph[t[i]-'a']-- |
|
||||||
} |
|
||||||
for i := 0; i < 26; i++ { |
|
||||||
if alph[i] != 0 { |
|
||||||
return false |
|
||||||
} |
|
||||||
} |
|
||||||
return true |
|
||||||
} |
|
@ -1,12 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
import "unicode" |
|
||||||
|
|
||||||
func IsLower(s string) bool { |
|
||||||
for _, r := range s { |
|
||||||
if !unicode.IsLower(r) { |
|
||||||
return false |
|
||||||
} |
|
||||||
} |
|
||||||
return true |
|
||||||
} |
|
@ -1,11 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
import "fmt" |
|
||||||
|
|
||||||
func IsNegative(n int) { |
|
||||||
if n < 0 { |
|
||||||
fmt.Println("T") |
|
||||||
} else { |
|
||||||
fmt.Println("F") |
|
||||||
} |
|
||||||
} |
|
@ -1,12 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
import "unicode" |
|
||||||
|
|
||||||
func IsNumeric(s string) bool { |
|
||||||
for _, r := range s { |
|
||||||
if !unicode.IsDigit(r) { |
|
||||||
return false |
|
||||||
} |
|
||||||
} |
|
||||||
return true |
|
||||||
} |
|
@ -1,18 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
import "math" |
|
||||||
|
|
||||||
func IsPrime(value int) bool { |
|
||||||
if value < 2 { |
|
||||||
return false |
|
||||||
} |
|
||||||
limit := int(math.Floor(math.Sqrt(float64(value)))) |
|
||||||
i := 2 |
|
||||||
for i <= limit { |
|
||||||
if value%i == 0 { |
|
||||||
return false |
|
||||||
} |
|
||||||
i++ |
|
||||||
} |
|
||||||
return true |
|
||||||
} |
|
@ -1,12 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
import "unicode" |
|
||||||
|
|
||||||
func IsPrintable(s string) bool { |
|
||||||
for _, r := range s { |
|
||||||
if !unicode.IsPrint(r) { |
|
||||||
return false |
|
||||||
} |
|
||||||
} |
|
||||||
return true |
|
||||||
} |
|
@ -1,20 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
func IsSorted(f func(int, int) int, arr []int) bool { |
|
||||||
ascendingOrdered := true |
|
||||||
descendingOrdered := true |
|
||||||
|
|
||||||
for i := 1; i < len(arr); i++ { |
|
||||||
if !(f(arr[i-1], arr[i]) >= 0) { |
|
||||||
ascendingOrdered = false |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
for i := 1; i < len(arr); i++ { |
|
||||||
if !(f(arr[i-1], arr[i]) <= 0) { |
|
||||||
descendingOrdered = false |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
return ascendingOrdered || descendingOrdered |
|
||||||
} |
|
@ -1,10 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
func IsUpper(s string) bool { |
|
||||||
for _, r := range s { |
|
||||||
if !(r >= 'A' && r <= 'Z') { |
|
||||||
return false |
|
||||||
} |
|
||||||
} |
|
||||||
return true |
|
||||||
} |
|
@ -1,17 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
import "math/bits" |
|
||||||
|
|
||||||
func IterativeFactorial(nb int) int { |
|
||||||
limit := 12 |
|
||||||
if bits.UintSize == 64 { |
|
||||||
limit = 20 |
|
||||||
} |
|
||||||
if nb < 0 || nb > limit { |
|
||||||
return 0 |
|
||||||
} |
|
||||||
if nb == 0 { |
|
||||||
return 1 |
|
||||||
} |
|
||||||
return nb * IterativeFactorial(nb-1) |
|
||||||
} |
|
@ -1,11 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
import "math" |
|
||||||
|
|
||||||
func IterativePower(nb int, power int) int { |
|
||||||
if power < 0 { |
|
||||||
return 0 |
|
||||||
} |
|
||||||
result := math.Pow(float64(nb), float64(power)) |
|
||||||
return int(result) |
|
||||||
} |
|
@ -1,7 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
import "strconv" |
|
||||||
|
|
||||||
func Itoa(i int) string { |
|
||||||
return strconv.Itoa(i) |
|
||||||
} |
|
@ -1,14 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
import ( |
|
||||||
"strconv" |
|
||||||
"strings" |
|
||||||
) |
|
||||||
|
|
||||||
func ItoaBase(value, base int) string { |
|
||||||
if base < 2 || base > 16 { |
|
||||||
return "" |
|
||||||
} |
|
||||||
|
|
||||||
return strings.ToUpper(strconv.FormatInt(int64(value), base)) |
|
||||||
} |
|
@ -1,7 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
import "strings" |
|
||||||
|
|
||||||
func Join(elems []string, sep string) string { |
|
||||||
return strings.Join(elems, sep) |
|
||||||
} |
|
@ -1,7 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
func LastRune(s string) rune { |
|
||||||
runes := []rune(s) |
|
||||||
index := len(runes) - 1 |
|
||||||
return runes[index] |
|
||||||
} |
|
@ -1,12 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
func gcd(first, second int) int { |
|
||||||
if second == 0 { |
|
||||||
return first |
|
||||||
} |
|
||||||
return gcd(second, first%second) |
|
||||||
} |
|
||||||
|
|
||||||
func Lcm(first, second int) int { |
|
||||||
return first / gcd(second, first%second) * second |
|
||||||
} |
|
@ -1,15 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
func MakeRange(min, max int) []int { |
|
||||||
size := max - min |
|
||||||
|
|
||||||
if size <= 0 { |
|
||||||
return nil |
|
||||||
} |
|
||||||
answer := make([]int, size) |
|
||||||
for i := range answer { |
|
||||||
answer[i] = min |
|
||||||
min++ |
|
||||||
} |
|
||||||
return answer |
|
||||||
} |
|
@ -1,19 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
func IsPositive(value int) bool { |
|
||||||
return value > 0 |
|
||||||
} |
|
||||||
|
|
||||||
func IsNegative0(value int) bool { |
|
||||||
return value < 0 |
|
||||||
} |
|
||||||
|
|
||||||
func Map(f func(int) bool, arr []int) []bool { |
|
||||||
arrBool := make([]bool, len(arr)) |
|
||||||
|
|
||||||
for i, el := range arr { |
|
||||||
arrBool[i] = f(el) |
|
||||||
} |
|
||||||
|
|
||||||
return arrBool |
|
||||||
} |
|
@ -1,11 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
import "sort" |
|
||||||
|
|
||||||
func Max(a []int) int { |
|
||||||
if len(a) == 0 { |
|
||||||
return 0 |
|
||||||
} |
|
||||||
sort.Ints(a) |
|
||||||
return a[len(a)-1] |
|
||||||
} |
|
@ -1,20 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
type TreeNodeM struct { |
|
||||||
Left *TreeNodeM |
|
||||||
Val int |
|
||||||
Right *TreeNodeM |
|
||||||
} |
|
||||||
|
|
||||||
func MergeTrees(t1 *TreeNodeM, t2 *TreeNodeM) *TreeNodeM { |
|
||||||
if t1 == nil { |
|
||||||
return t2 |
|
||||||
} |
|
||||||
if t2 == nil { |
|
||||||
return t1 |
|
||||||
} |
|
||||||
t1.Val += t2.Val |
|
||||||
t1.Left = MergeTrees(t1.Left, t2.Left) |
|
||||||
t1.Right = MergeTrees(t1.Right, t2.Right) |
|
||||||
return t1 |
|
||||||
} |
|
@ -1,28 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
func Nauuo(plus, minus, rand int) string { |
|
||||||
if rand == 0 { |
|
||||||
if plus > minus { |
|
||||||
return "+" |
|
||||||
} |
|
||||||
if plus < minus { |
|
||||||
return "-" |
|
||||||
} |
|
||||||
if plus == minus { |
|
||||||
return "0" |
|
||||||
} |
|
||||||
} |
|
||||||
if plus > minus+rand { |
|
||||||
return "+" |
|
||||||
} |
|
||||||
if plus+rand < minus { |
|
||||||
return "-" |
|
||||||
} |
|
||||||
if plus+rand >= minus && plus-rand <= minus { |
|
||||||
return "?" |
|
||||||
} |
|
||||||
if minus+rand >= plus && minus-rand <= plus { |
|
||||||
return "?" |
|
||||||
} |
|
||||||
return "?" |
|
||||||
} |
|
@ -1,9 +0,0 @@ |
|||||||
package correct |
|
||||||
|
|
||||||
func NRune(s string, n int) rune { |
|
||||||
if n > len(s) || n < 1 { |
|
||||||
return 0 |
|
||||||
} |
|
||||||
runes := []rune(s) |
|
||||||
return runes[n-1] |
|
||||||
} |
|
Some files were not shown because too many files changed in this diff diff.show_more
Loading…
Reference in new issue