diff --git a/subjects/btreeapplybylevel.en.md b/subjects/btreeapplybylevel.en.md index 85cd3eb0..e964f86a 100644 --- a/subjects/btreeapplybylevel.en.md +++ b/subjects/btreeapplybylevel.en.md @@ -2,7 +2,7 @@ ### Instructions -Write a function, BTreeApplyByLevel, that applies the function given by fn to each node of the tree given by root. +Write a function, `BTreeApplyByLevel`, that applies the function given by fn to each node of the tree given by root. This function must have the following signature. @@ -23,26 +23,26 @@ package main import ( "fmt" - student ".." + piscine ".." ) func main() { - root := &student.TreeNode{Data: "4"} - student.BTreeInsertData(root, "1") - student.BTreeInsertData(root, "7") - student.BTreeInsertData(root, "5") - student.BTreeApplyByLevel(root, fmt.Println) + root := &piscine.TreeNode{Data: "4"} + piscine.BTreeInsertData(root, "1") + piscine.BTreeInsertData(root, "7") + piscine.BTreeInsertData(root, "5") + piscine.BTreeApplyByLevel(root, fmt.Println) } ``` And its output : ```console -student@ubuntu:~/student/btreeapplybylevel$ go build -student@ubuntu:~/student/btreeapplybylevel$ ./btreeapplybylevel +student@ubuntu:~/student/test$ go build +student@ubuntu:~/student/test$ ./test 4 1 7 5 -student@ubuntu:~/student/btreeapplybylevel$ +student@ubuntu:~/student/test$ ``` diff --git a/subjects/btreeapplybylevel.fr.md b/subjects/btreeapplybylevel.fr.md index 85cd3eb0..e964f86a 100644 --- a/subjects/btreeapplybylevel.fr.md +++ b/subjects/btreeapplybylevel.fr.md @@ -2,7 +2,7 @@ ### Instructions -Write a function, BTreeApplyByLevel, that applies the function given by fn to each node of the tree given by root. +Write a function, `BTreeApplyByLevel`, that applies the function given by fn to each node of the tree given by root. This function must have the following signature. @@ -23,26 +23,26 @@ package main import ( "fmt" - student ".." + piscine ".." ) func main() { - root := &student.TreeNode{Data: "4"} - student.BTreeInsertData(root, "1") - student.BTreeInsertData(root, "7") - student.BTreeInsertData(root, "5") - student.BTreeApplyByLevel(root, fmt.Println) + root := &piscine.TreeNode{Data: "4"} + piscine.BTreeInsertData(root, "1") + piscine.BTreeInsertData(root, "7") + piscine.BTreeInsertData(root, "5") + piscine.BTreeApplyByLevel(root, fmt.Println) } ``` And its output : ```console -student@ubuntu:~/student/btreeapplybylevel$ go build -student@ubuntu:~/student/btreeapplybylevel$ ./btreeapplybylevel +student@ubuntu:~/student/test$ go build +student@ubuntu:~/student/test$ ./test 4 1 7 5 -student@ubuntu:~/student/btreeapplybylevel$ +student@ubuntu:~/student/test$ ``` diff --git a/subjects/btreeisbinary.en.md b/subjects/btreeisbinary.en.md index e57a11a9..2a93d314 100644 --- a/subjects/btreeisbinary.en.md +++ b/subjects/btreeisbinary.en.md @@ -23,23 +23,23 @@ package main import ( "fmt" - student ".." + piscine ".." ) func main() { - root := &student.TreeNode{Data: "4"} - student.BTreeInsertData(root, "1") - student.BTreeInsertData(root, "7") - student.BTreeInsertData(root, "5") - fmt.Println(student.BTreeIsBinary(root)) + root := &piscine.TreeNode{Data: "4"} + piscine.BTreeInsertData(root, "1") + piscine.BTreeInsertData(root, "7") + piscine.BTreeInsertData(root, "5") + fmt.Println(piscine.BTreeIsBinary(root)) } ``` And its output : ```console -student@ubuntu:~/student/btreeisbinary$ go build -student@ubuntu:~/student/btreeisbinary$ ./btreeisbinary +student@ubuntu:~/student/test$ go build +student@ubuntu:~/student/test$ ./test true -student@ubuntu:~/student/btreeisbinary$ +student@ubuntu:~/student/test$ ``` diff --git a/subjects/btreeisbinary.fr.md b/subjects/btreeisbinary.fr.md index e57a11a9..2a93d314 100644 --- a/subjects/btreeisbinary.fr.md +++ b/subjects/btreeisbinary.fr.md @@ -23,23 +23,23 @@ package main import ( "fmt" - student ".." + piscine ".." ) func main() { - root := &student.TreeNode{Data: "4"} - student.BTreeInsertData(root, "1") - student.BTreeInsertData(root, "7") - student.BTreeInsertData(root, "5") - fmt.Println(student.BTreeIsBinary(root)) + root := &piscine.TreeNode{Data: "4"} + piscine.BTreeInsertData(root, "1") + piscine.BTreeInsertData(root, "7") + piscine.BTreeInsertData(root, "5") + fmt.Println(piscine.BTreeIsBinary(root)) } ``` And its output : ```console -student@ubuntu:~/student/btreeisbinary$ go build -student@ubuntu:~/student/btreeisbinary$ ./btreeisbinary +student@ubuntu:~/student/test$ go build +student@ubuntu:~/student/test$ ./test true -student@ubuntu:~/student/btreeisbinary$ +student@ubuntu:~/student/test$ ``` diff --git a/subjects/btreelevelcount.en.md b/subjects/btreelevelcount.en.md index 2576e109..ef9c7806 100644 --- a/subjects/btreelevelcount.en.md +++ b/subjects/btreelevelcount.en.md @@ -7,7 +7,7 @@ Write a function, BTreeLevelCount, that return the number of levels of the tree ### Expected function ```go -func BTreeLevelCount(root *piscine.TreeNode) int { +func BTreeLevelCount(root *TreeNode) int { } ``` @@ -21,23 +21,24 @@ package main import ( "fmt" - student ".." + + piscine ".." ) func main() { - root := &student.TreeNode{Data: "4"} - student.BTreeInsertData(root, "1") - student.BTreeInsertData(root, "7") - student.BTreeInsertData(root, "5") - fmt.Println(BTreeLevelCount(root)) + root := &piscine.TreeNode{Data: "4"} + piscine.BTreeInsertData(root, "1") + piscine.BTreeInsertData(root, "7") + piscine.BTreeInsertData(root, "5") + fmt.Println(piscine.BTreeLevelCount(root)) } ``` And its output : ```console -student@ubuntu:~/student/btreesearchitem$ go build -student@ubuntu:~/student/btreesearchitem$ ./btreesearchitem +student@ubuntu:~/student/test$ go build +student@ubuntu:~/student/test$ ./test 3 -student@ubuntu:~/student/btreesearchitem$ +student@ubuntu:~/student/test$ ``` diff --git a/subjects/btreelevelcount.fr.md b/subjects/btreelevelcount.fr.md index 2576e109..ef9c7806 100644 --- a/subjects/btreelevelcount.fr.md +++ b/subjects/btreelevelcount.fr.md @@ -7,7 +7,7 @@ Write a function, BTreeLevelCount, that return the number of levels of the tree ### Expected function ```go -func BTreeLevelCount(root *piscine.TreeNode) int { +func BTreeLevelCount(root *TreeNode) int { } ``` @@ -21,23 +21,24 @@ package main import ( "fmt" - student ".." + + piscine ".." ) func main() { - root := &student.TreeNode{Data: "4"} - student.BTreeInsertData(root, "1") - student.BTreeInsertData(root, "7") - student.BTreeInsertData(root, "5") - fmt.Println(BTreeLevelCount(root)) + root := &piscine.TreeNode{Data: "4"} + piscine.BTreeInsertData(root, "1") + piscine.BTreeInsertData(root, "7") + piscine.BTreeInsertData(root, "5") + fmt.Println(piscine.BTreeLevelCount(root)) } ``` And its output : ```console -student@ubuntu:~/student/btreesearchitem$ go build -student@ubuntu:~/student/btreesearchitem$ ./btreesearchitem +student@ubuntu:~/student/test$ go build +student@ubuntu:~/student/test$ ./test 3 -student@ubuntu:~/student/btreesearchitem$ +student@ubuntu:~/student/test$ ``` diff --git a/subjects/btreemax.en.md b/subjects/btreemax.en.md index dc389960..3fa2bc5c 100644 --- a/subjects/btreemax.en.md +++ b/subjects/btreemax.en.md @@ -2,7 +2,7 @@ ### Instructions -Write a function, BTreeMax, that returns the node with the maximum value in the tree given by root +Write a function, `BTreeMax`, that returns the node with the maximum value in the tree given by root This function must have the following signature. @@ -23,15 +23,16 @@ package main import ( "fmt" - student ".." + + piscine ".." ) func main() { - root := &student.TreeNode{Data: "4"} - student.BTreeInsertData(root, "1") - student.BTreeInsertData(root, "7") - student.BTreeInsertData(root, "5") - max := student.BTreeMax(root) + root := &piscine.TreeNode{Data: "4"} + piscine.BTreeInsertData(root, "1") + piscine.BTreeInsertData(root, "7") + piscine.BTreeInsertData(root, "5") + max := piscine.BTreeMax(root) fmt.Println(max.Data) } ``` @@ -39,8 +40,8 @@ func main() { And its output : ```console -student@ubuntu:~/student/btreemax$ go build -student@ubuntu:~/student/btreemax$ ./btreemax +student@ubuntu:~/student/test$ go build +student@ubuntu:~/student/test$ ./test 7 -student@ubuntu:~/student/btreemax$ +student@ubuntu:~/student/test$ ``` diff --git a/subjects/btreemax.fr.md b/subjects/btreemax.fr.md index dc389960..3fa2bc5c 100644 --- a/subjects/btreemax.fr.md +++ b/subjects/btreemax.fr.md @@ -2,7 +2,7 @@ ### Instructions -Write a function, BTreeMax, that returns the node with the maximum value in the tree given by root +Write a function, `BTreeMax`, that returns the node with the maximum value in the tree given by root This function must have the following signature. @@ -23,15 +23,16 @@ package main import ( "fmt" - student ".." + + piscine ".." ) func main() { - root := &student.TreeNode{Data: "4"} - student.BTreeInsertData(root, "1") - student.BTreeInsertData(root, "7") - student.BTreeInsertData(root, "5") - max := student.BTreeMax(root) + root := &piscine.TreeNode{Data: "4"} + piscine.BTreeInsertData(root, "1") + piscine.BTreeInsertData(root, "7") + piscine.BTreeInsertData(root, "5") + max := piscine.BTreeMax(root) fmt.Println(max.Data) } ``` @@ -39,8 +40,8 @@ func main() { And its output : ```console -student@ubuntu:~/student/btreemax$ go build -student@ubuntu:~/student/btreemax$ ./btreemax +student@ubuntu:~/student/test$ go build +student@ubuntu:~/student/test$ ./test 7 -student@ubuntu:~/student/btreemax$ +student@ubuntu:~/student/test$ ``` diff --git a/subjects/btreemin.en.md b/subjects/btreemin.en.md index f3471856..8de2f9a0 100644 --- a/subjects/btreemin.en.md +++ b/subjects/btreemin.en.md @@ -2,7 +2,7 @@ ### Instructions -Write a function, BTreeMin, that returns the node with the minimum value in the tree given by root +Write a function, `BTreeMin`, that returns the node with the minimum value in the tree given by root This function must have the following signature. @@ -23,15 +23,16 @@ package main import ( "fmt" - student ".." + + piscine ".." ) func main() { - root := &student.TreeNode{Data: "4"} - student.BTreeInsertData(root, "1") - student.BTreeInsertData(root, "7") - student.BTreeInsertData(root, "5") - min := student.BTreeMin(root) + root := &piscine.TreeNode{Data: "4"} + piscine.BTreeInsertData(root, "1") + piscine.BTreeInsertData(root, "7") + piscine.BTreeInsertData(root, "5") + min := piscine.BTreeMin(root) fmt.Println(min.Data) } ``` @@ -39,8 +40,8 @@ func main() { And its output : ```console -student@ubuntu:~/student/btreemin$ go build -student@ubuntu:~/student/btreemin$ ./btreemin +student@ubuntu:~/student/test$ go build +student@ubuntu:~/student/test$ ./test 1 -student@ubuntu:~/student/btreemin$ +student@ubuntu:~/student/test$ ``` diff --git a/subjects/btreemin.fr.md b/subjects/btreemin.fr.md index f3471856..8de2f9a0 100644 --- a/subjects/btreemin.fr.md +++ b/subjects/btreemin.fr.md @@ -2,7 +2,7 @@ ### Instructions -Write a function, BTreeMin, that returns the node with the minimum value in the tree given by root +Write a function, `BTreeMin`, that returns the node with the minimum value in the tree given by root This function must have the following signature. @@ -23,15 +23,16 @@ package main import ( "fmt" - student ".." + + piscine ".." ) func main() { - root := &student.TreeNode{Data: "4"} - student.BTreeInsertData(root, "1") - student.BTreeInsertData(root, "7") - student.BTreeInsertData(root, "5") - min := student.BTreeMin(root) + root := &piscine.TreeNode{Data: "4"} + piscine.BTreeInsertData(root, "1") + piscine.BTreeInsertData(root, "7") + piscine.BTreeInsertData(root, "5") + min := piscine.BTreeMin(root) fmt.Println(min.Data) } ``` @@ -39,8 +40,8 @@ func main() { And its output : ```console -student@ubuntu:~/student/btreemin$ go build -student@ubuntu:~/student/btreemin$ ./btreemin +student@ubuntu:~/student/test$ go build +student@ubuntu:~/student/test$ ./test 1 -student@ubuntu:~/student/btreemin$ +student@ubuntu:~/student/test$ ``` diff --git a/subjects/btreetransplant.en.md b/subjects/btreetransplant.en.md index 50beed3a..dd4ed4fa 100644 --- a/subjects/btreetransplant.en.md +++ b/subjects/btreetransplant.en.md @@ -2,6 +2,7 @@ ### Instructions + In order to move subtrees around within the binary search tree, write a function, `BTreeTransplant`, which replaces the subtree started by `node` with the node called `rplc` in the tree given by `root`. This function must have the following signature. diff --git a/subjects/listforeach.en.md b/subjects/listforeach.en.md index 025c0e52..76359adb 100644 --- a/subjects/listforeach.en.md +++ b/subjects/listforeach.en.md @@ -4,22 +4,42 @@ Write a function `ListForEach` that applies a function given as argument to the information within each of the list's links. -- The function given as argument must have a pointer as argument: `l *list` +- The function given as argument must have a pointer as argument: `l *List` + +- Copy the functions `Add2_node` and `Subtract3_node` in the same file you defined the function `ListForEach`. ### Expected function and structure ```go -type node struct { - data interface{} - next *node +type NodeL struct { + Data interface{} + Next *NodeL +} + +type List struct { + Head *NodeL + Tail *NodeL } -type list struct { - head *node - tail *node +func ListForEach(l *List, f func(*NodeL)) { } -func ListForEach(l *list, f func(l *list)) { +func Add2_node(node *NodeL) { + switch node.Data.(type) { + case int: + node.Data = node.Data.(int) + 2 + case string: + node.Data = node.Data.(string) + "2" + } +} + +func Subtract3_node(node *NodeL) { + switch node.Data.(type) { + case int: + node.Data = node.Data.(int) - 3 + case string: + node.Data = node.Data.(string) + "-3" + } } ``` @@ -36,18 +56,19 @@ import ( ) func main() { - link := &list{} + link := &piscine.List{} - piscine.ListPushBack(link, 1) - piscine.ListPushBack(link, 2) - piscine.ListPushBack(link, 3) - piscine.ListPushBack(link, 4) + piscine.ListPushBack(link, "1") + piscine.ListPushBack(link, "2") + piscine.ListPushBack(link, "3") + piscine.ListPushBack(link, "5") - piscine.ListForEach(link, piscine.ListReverse) + piscine.ListForEach(link, piscine.Add2) - for link.head != nil { - fmt.Println(link.head.data) - link.head = link.head.next + it := link.Head + for it != nil { + fmt.Println(it.Data) + it = it.Next } } ``` @@ -57,9 +78,9 @@ And its output : ```console student@ubuntu:~/piscine/test$ go build student@ubuntu:~/piscine/test$ ./test -4 -3 -2 -1 +12 +22 +32 +52 student@ubuntu:~/piscine/test$ ``` diff --git a/subjects/listpushfront.en.md b/subjects/listpushfront.en.md index 0e1e511b..35901b23 100644 --- a/subjects/listpushfront.en.md +++ b/subjects/listpushfront.en.md @@ -7,17 +7,17 @@ Write a function `ListPushBack` that inserts a new element `node` at the beginni ### Expected function and structure ```go -type Node struct { +type NodeL struct { Data interface{} - Next *Node + Next *NodeL } type List struct { - Head *Node - Tail *Node + Head *NodeL + Tail *NodeL } -func ListPushFront(l *list, data interface{}) { +func ListPushFront(l *List, data interface{}) { } ``` @@ -29,21 +29,22 @@ Here is a possible [program](TODO-LINK) to test your function : package main import ( - "fmt" piscine ".." + "fmt" ) func main() { - link := &list{} + link := &piscine.List{} piscine.ListPushFront(link, "Hello") piscine.ListPushFront(link, "man") piscine.ListPushFront(link, "how are you") - for link.head != nil { - fmt.Println(link.head.data) - link.head = link.head.next + it := link.Head + for it != nil { + fmt.Println(it.Data) + it = it.Next } } ``` diff --git a/subjects/listreverse.en.md b/subjects/listreverse.en.md index a602a052..f661ef47 100644 --- a/subjects/listreverse.en.md +++ b/subjects/listreverse.en.md @@ -9,17 +9,17 @@ Write a function `ListReverse` that reverses the elements order of a given linke ### Expected function and structure ```go -type node struct { - data interface{} - next *node +type NodeL struct { + Data interface{} + Next *NodeL } -type list struct { - head *node - tail *node +type List struct { + Head *NodeL + Tail *NodeL } -func ListReverse(l *list) { +func ListReverse(l *List) { } ``` @@ -36,19 +36,24 @@ import ( ) func main() { - link := &list{} + link := &piscine.List{} - listPushBack(link, 1) - listPushBack(link, 2) - listPushBack(link, 3) - listPushBack(link, 4) + piscine.ListPushBack(link, 1) + piscine.ListPushBack(link, 2) + piscine.ListPushBack(link, 3) + piscine.ListPushBack(link, 4) - listReverse(link) + piscine.ListReverse(link) - for link.head != nil { - fmt.Println(link.head.data) - link.head = link.head.next + it := link.Head + + for it != nil { + fmt.Println(it.Data) + it = it.Next } + + fmt.Println("Tail", link.Tail) + fmt.Println("Head", link.Head) } ``` @@ -61,5 +66,7 @@ student@ubuntu:~/piscine/test$ ./test 3 2 1 +Tail &{1 } +Head &{4 0xc42000a140} student@ubuntu:~/piscine/test$ -``` +``` \ No newline at end of file