mirror of https://github.com/01-edu/public.git
Xavier Petit
4 years ago
committed by
xpetit
100 changed files with 50 additions and 722 deletions
@ -1,39 +0,0 @@
|
||||
package main |
||||
|
||||
type NodeAddL struct { |
||||
Next *NodeAddL |
||||
Num int |
||||
} |
||||
|
||||
func pushFront(node *NodeAddL, num int) *NodeAddL { |
||||
tmp := &NodeAddL{Num: num} |
||||
if node == nil { |
||||
return tmp |
||||
} |
||||
tmp.Next = node |
||||
return tmp |
||||
} |
||||
|
||||
func AddLinkedNumbers(num1, num2 *NodeAddL) *NodeAddL { |
||||
var n1, n2, r int |
||||
var result *NodeAddL |
||||
|
||||
for tmp := num1; tmp != nil; tmp = tmp.Next { |
||||
n1 = n1*10 + tmp.Num |
||||
} |
||||
|
||||
for tmp := num2; tmp != nil; tmp = tmp.Next { |
||||
n2 = n2*10 + tmp.Num |
||||
} |
||||
|
||||
r = n1 + n2 |
||||
for r > 0 { |
||||
mod := r % 10 |
||||
r /= 10 |
||||
result = pushFront(result, mod) |
||||
} |
||||
return result |
||||
} |
||||
|
||||
func main() { |
||||
} |
@ -1,57 +0,0 @@
|
||||
package main |
||||
|
||||
type NodeAddL struct { |
||||
Next *NodeAddL |
||||
Num int |
||||
} |
||||
|
||||
func pushBack(node *NodeAddL, num int) *NodeAddL { |
||||
nw := &NodeAddL{Num: num} |
||||
if node == nil { |
||||
return nw |
||||
} |
||||
for tmp := node; tmp != nil; tmp = tmp.Next { |
||||
if tmp.Next == nil { |
||||
tmp.Next = nw |
||||
return node |
||||
} |
||||
} |
||||
return node |
||||
} |
||||
|
||||
func Changeorder(node *NodeAddL) *NodeAddL { |
||||
if node == nil { |
||||
return node |
||||
} |
||||
ans := &NodeAddL{Num: node.Num} |
||||
for tmp := node; tmp != nil; { |
||||
tmp = tmp.Next |
||||
if tmp == nil { |
||||
break |
||||
} |
||||
tmp = tmp.Next |
||||
if tmp == nil { |
||||
break |
||||
} |
||||
ans = pushBack(ans, tmp.Num) |
||||
} |
||||
|
||||
if node.Next == nil { |
||||
return ans |
||||
} |
||||
for tmp := node.Next; tmp != nil; { |
||||
ans = pushBack(ans, tmp.Num) |
||||
tmp = tmp.Next |
||||
if tmp == nil { |
||||
break |
||||
} |
||||
tmp = tmp.Next |
||||
if tmp == nil { |
||||
break |
||||
} |
||||
} |
||||
return ans |
||||
} |
||||
|
||||
func main() { |
||||
} |
@ -1,23 +0,0 @@
|
||||
package main |
||||
|
||||
import "fmt" |
||||
|
||||
func Chunk(slice []int, size int) { |
||||
newSlice := []int{} |
||||
if size <= 0 { |
||||
fmt.Println() |
||||
return |
||||
} |
||||
result := make([][]int, 0, len(slice)/size+1) |
||||
for len(slice) >= size { |
||||
newSlice, slice = slice[:size], slice[size:] |
||||
result = append(result, newSlice) |
||||
} |
||||
if len(slice) > 0 { |
||||
result = append(result, slice[:len(slice)]) |
||||
} |
||||
fmt.Println(result) |
||||
} |
||||
|
||||
func main() { |
||||
} |
@ -1,19 +0,0 @@
|
||||
package main |
||||
|
||||
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 |
||||
} |
||||
|
||||
func main() { |
||||
} |
@ -1,14 +0,0 @@
|
||||
package main |
||||
|
||||
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) |
||||
} |
||||
|
||||
func main() { |
||||
} |
@ -1,20 +0,0 @@
|
||||
package main |
||||
|
||||
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 |
||||
} |
||||
|
||||
func main() { |
||||
} |
@ -1,22 +0,0 @@
|
||||
package main |
||||
|
||||
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++ |
||||
} |
||||
} |
||||
|
||||
func main() { |
||||
} |
@ -1,26 +0,0 @@
|
||||
package main |
||||
|
||||
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 |
||||
} |
||||
|
||||
func main() { |
||||
} |
@ -1,23 +0,0 @@
|
||||
package main |
||||
|
||||
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 |
||||
} |
||||
|
||||
func main() { |
||||
} |
@ -1,31 +0,0 @@
|
||||
package main |
||||
|
||||
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 "?" |
||||
} |
||||
|
||||
func main() { |
||||
} |
@ -1,35 +0,0 @@
|
||||
package main |
||||
|
||||
import ( |
||||
"strconv" |
||||
|
||||
"github.com/01-edu/z01" |
||||
) |
||||
|
||||
func main() { |
||||
arg1 := []string{"2", "5", "u", "19"} |
||||
arg2 := []string{"2"} |
||||
arg3 := []string{"1", "2", "3", "5", "7", "24"} |
||||
arg4 := []string{"6", "12", "24"} |
||||
|
||||
args := [][]string{arg1, arg2, arg3, arg4} |
||||
|
||||
for i := 0; i < 10; i++ { |
||||
var arg []string |
||||
init := z01.RandIntBetween(0, 10) |
||||
for i := init; i < init+z01.RandIntBetween(5, 10); i++ { |
||||
arg = append(arg, strconv.Itoa(i)) |
||||
} |
||||
args = append(args, arg) |
||||
} |
||||
|
||||
for i := 0; i < 1; i++ { |
||||
args = append(args, z01.MultRandWords()) |
||||
} |
||||
|
||||
for _, v := range args { |
||||
z01.ChallengeMain("paramcount", v...) |
||||
} |
||||
|
||||
z01.ChallengeMain("paramcount") |
||||
} |
@ -1,87 +0,0 @@
|
||||
package main |
||||
|
||||
import "github.com/01-edu/z01" |
||||
|
||||
func printBase(nbr int) int { |
||||
var result []rune |
||||
base := "0123456789abcdef" |
||||
typeBase := []rune(base) |
||||
a := 0 |
||||
pos := 0 |
||||
i := 0 |
||||
if nbr == 0 { |
||||
result = append(result, '0', '0') |
||||
i = 2 |
||||
} |
||||
for nbr > 0 { |
||||
pos = nbr % 16 |
||||
nbr = nbr / 16 |
||||
result = append(result, typeBase[pos]) |
||||
i++ |
||||
} |
||||
if i == 1 { |
||||
result = append(result, '0') |
||||
i = 2 |
||||
} |
||||
for j := i - 1; j >= 0; j-- { |
||||
z01.PrintRune(result[j]) |
||||
a++ |
||||
} |
||||
return a |
||||
} |
||||
|
||||
func printascii(a int) { |
||||
if a > 31 && a < 127 { |
||||
z01.PrintRune(rune(a)) |
||||
} else { |
||||
z01.PrintRune('.') |
||||
} |
||||
} |
||||
|
||||
func printLine(arr [10]int, start int, max int) { |
||||
a := 0 |
||||
a = start |
||||
aux := 0 |
||||
var b int |
||||
|
||||
for a < start+16 && a < max { |
||||
if a%4 == 0 && a != 0 { |
||||
z01.PrintRune('\n') |
||||
} |
||||
b = 8 - printBase(arr[a]) |
||||
for aux != b { |
||||
if b == 6 { |
||||
z01.PrintRune('0') |
||||
} |
||||
if aux == 1 { |
||||
z01.PrintRune(' ') |
||||
} |
||||
if b < 6 { |
||||
z01.PrintRune('0') |
||||
} |
||||
aux++ |
||||
} |
||||
z01.PrintRune(' ') |
||||
aux = 0 |
||||
a++ |
||||
} |
||||
z01.PrintRune('\n') |
||||
c := start |
||||
for c < start+16 && c < max { |
||||
printascii(arr[c]) |
||||
c++ |
||||
} |
||||
z01.PrintRune('\n') |
||||
} |
||||
|
||||
func PrintMemory(arr [10]int) { |
||||
i := 0 |
||||
size := len(arr) |
||||
for i < size { |
||||
printLine(arr, i, size) |
||||
i += 16 |
||||
} |
||||
} |
||||
|
||||
func main() { |
||||
} |
@ -1,21 +0,0 @@
|
||||
package main |
||||
|
||||
func PriorPrime(x int) int { |
||||
ans := 0 |
||||
ok := 0 |
||||
for i := 2; i < x; i++ { |
||||
ok = 1 |
||||
for j := 2; j*j <= i; j++ { |
||||
if i%j == 0 { |
||||
ok = 0 |
||||
} |
||||
} |
||||
if ok == 1 { |
||||
ans += i |
||||
} |
||||
} |
||||
return ans |
||||
} |
||||
|
||||
func main() { |
||||
} |
@ -1,14 +0,0 @@
|
||||
package main |
||||
|
||||
import "fmt" |
||||
|
||||
func ReduceInt(f func(int, int) int, arr []int) { |
||||
acc := arr[0] |
||||
for i := 1; i < len(arr); i++ { |
||||
acc = f(acc, arr[i]) |
||||
} |
||||
fmt.Println(acc) |
||||
} |
||||
|
||||
func main() { |
||||
} |
@ -1,29 +0,0 @@
|
||||
package main |
||||
|
||||
type NodeAddL struct { |
||||
Next *NodeAddL |
||||
Num int |
||||
} |
||||
|
||||
func pushFront(node *NodeAddL, num int) *NodeAddL { |
||||
tmp := &NodeAddL{Num: num} |
||||
if node == nil { |
||||
return tmp |
||||
} |
||||
tmp.Next = node |
||||
return tmp |
||||
} |
||||
|
||||
func Reverse(node *NodeAddL) *NodeAddL { |
||||
if node == nil { |
||||
return node |
||||
} |
||||
ans := &NodeAddL{Num: node.Num} |
||||
for tmp := node.Next; tmp != nil; tmp = tmp.Next { |
||||
ans = pushFront(ans, tmp.Num) |
||||
} |
||||
return ans |
||||
} |
||||
|
||||
func main() { |
||||
} |
@ -1,44 +0,0 @@
|
||||
package main |
||||
|
||||
func more(a, b int) int { |
||||
if a < b { |
||||
return b |
||||
} |
||||
return a |
||||
} |
||||
|
||||
func max(a, b, c, d int) int { |
||||
if a >= b && a >= c && a >= d { |
||||
return a |
||||
} |
||||
if b >= a && b >= c && b >= d { |
||||
return b |
||||
} |
||||
if c >= a && c >= b && c >= d { |
||||
return c |
||||
} |
||||
if d >= a && d >= b && d >= c { |
||||
return d |
||||
} |
||||
return -1 |
||||
} |
||||
|
||||
func ReviveThreeNums(a, b, c, d int) int { |
||||
maxi := -111 |
||||
if a != max(a, b, c, d) { |
||||
maxi = more(maxi, max(a, b, c, d)-a) |
||||
} |
||||
if b != max(a, b, c, d) { |
||||
maxi = more(maxi, max(a, b, c, d)-b) |
||||
} |
||||
if c != max(a, b, c, d) { |
||||
maxi = more(maxi, max(a, b, c, d)-c) |
||||
} |
||||
if d != max(a, b, c, d) { |
||||
maxi = more(maxi, max(a, b, c, d)-d) |
||||
} |
||||
return maxi |
||||
} |
||||
|
||||
func main() { |
||||
} |
@ -1,27 +0,0 @@
|
||||
package main |
||||
|
||||
type TreeNodeL struct { |
||||
Left *TreeNodeL |
||||
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) |
||||
} |
||||
|
||||
func main() { |
||||
} |
@ -1,40 +0,0 @@
|
||||
package main |
||||
|
||||
type NodeAddL struct { |
||||
Next *NodeAddL |
||||
Num int |
||||
} |
||||
|
||||
func pushBack(node *NodeAddL, num int) *NodeAddL { |
||||
nw := &NodeAddL{Num: num} |
||||
if node == nil { |
||||
return nw |
||||
} |
||||
for tmp := node; tmp != nil; tmp = tmp.Next { |
||||
if tmp.Next == nil { |
||||
tmp.Next = nw |
||||
return node |
||||
} |
||||
} |
||||
return node |
||||
} |
||||
|
||||
func Sortll(node *NodeAddL) *NodeAddL { |
||||
if node == nil { |
||||
return node |
||||
} |
||||
|
||||
for first := node; first != nil; first = first.Next { |
||||
for second := first.Next; second != nil; second = second.Next { |
||||
if first.Num < second.Num { |
||||
tmp := first.Num |
||||
first.Num = second.Num |
||||
second.Num = tmp |
||||
} |
||||
} |
||||
} |
||||
return node |
||||
} |
||||
|
||||
func main() { |
||||
} |
@ -1,8 +0,0 @@
|
||||
package main |
||||
|
||||
func SwapBits(n byte) byte { |
||||
return ((n >> 4) | (n << 4)) |
||||
} |
||||
|
||||
func main() { |
||||
} |
@ -1,35 +0,0 @@
|
||||
package main |
||||
|
||||
import ( |
||||
"reflect" |
||||
|
||||
"github.com/01-edu/z01" |
||||
|
||||
correct "./correct" |
||||
) |
||||
|
||||
func challengeBytes(fn1, fn2 interface{}, args ...interface{}) { |
||||
st1 := z01.Monitor(fn1, args) |
||||
st2 := z01.Monitor(fn2, args) |
||||
if !reflect.DeepEqual(st1.Results, st2.Results) { |
||||
z01.Fatalf("%s(%08b) == %08b instead of %08b\n", |
||||
"SwapBits", |
||||
args[0].(byte), |
||||
st1.Results[0].(byte), |
||||
st2.Results[0].(byte), |
||||
) |
||||
} |
||||
} |
||||
|
||||
func main() { |
||||
args := []byte{0x24, 0x14, 0x11, 0x22, 0xd2, 0x15, 0xff, 0x0, 0x35, 0x58, 0x43} |
||||
|
||||
for i := 0; i < 10; i++ { |
||||
n := z01.RandIntBetween(0, 255) |
||||
args = append(args, byte(n)) |
||||
} |
||||
|
||||
for _, v := range args { |
||||
challengeBytes(SwapBits, correct.SwapBits, v) |
||||
} |
||||
} |
@ -1,60 +0,0 @@
|
||||
package main |
||||
|
||||
func min3(a, b, c int) int { |
||||
if a <= b && a <= c { |
||||
return a |
||||
} |
||||
if b <= a && b <= c { |
||||
return b |
||||
} |
||||
return c |
||||
} |
||||
|
||||
func max3(a, b, c int) int { |
||||
if a >= b && a >= c { |
||||
return a |
||||
} |
||||
if b >= a && b >= c { |
||||
return b |
||||
} |
||||
return c |
||||
} |
||||
|
||||
func min2(a, b int) int { |
||||
if a <= b { |
||||
return a |
||||
} |
||||
return b |
||||
} |
||||
|
||||
func Sweetproblem(a, b, c int) int { |
||||
if a > b { |
||||
f := a |
||||
a = b |
||||
b = f |
||||
} |
||||
if a > c { |
||||
f := a |
||||
a = c |
||||
c = f |
||||
} |
||||
if b > c { |
||||
f := b |
||||
b = c |
||||
c = f |
||||
} |
||||
ans := a |
||||
if c-b >= a { |
||||
c -= a |
||||
} else { |
||||
a -= c - b |
||||
half := a / 2 |
||||
c -= half |
||||
b -= a - half |
||||
} |
||||
ans += min2(b, c) |
||||
return ans |
||||
} |
||||
|
||||
func main() { |
||||
} |
@ -1,15 +0,0 @@
|
||||
package main |
||||
|
||||
func TwoSum(nums []int, target int) []int { |
||||
for i := 0; i < len(nums); i++ { |
||||
for j := i + 1; j < len(nums); j++ { |
||||
if nums[i]+nums[j] == target { |
||||
return []int{i, j} |
||||
} |
||||
} |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
func main() { |
||||
} |
@ -1,15 +0,0 @@
|
||||
package main |
||||
|
||||
func abs(a int) int { |
||||
if a < 0 { |
||||
return -a |
||||
} |
||||
return a |
||||
} |
||||
|
||||
func Volumechanger(a, b int) int { |
||||
return abs(a-b)/5 + abs(a-b)%5/2 + abs(a-b)%5%2 |
||||
} |
||||
|
||||
func main() { |
||||
} |
Loading…
Reference in new issue