Browse Source

Merge branch 'master' into fixBtreetransplant

content-update
augusto-mantilla 5 years ago committed by GitHub
parent
commit
a90c722b77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 20
      subjects/btreeapplybylevel.en.md
  2. 20
      subjects/btreeapplybylevel.fr.md
  3. 18
      subjects/btreeisbinary.en.md
  4. 18
      subjects/btreeisbinary.fr.md
  5. 21
      subjects/btreelevelcount.en.md
  6. 21
      subjects/btreelevelcount.fr.md
  7. 21
      subjects/btreemax.en.md
  8. 21
      subjects/btreemax.fr.md
  9. 21
      subjects/btreemin.en.md
  10. 21
      subjects/btreemin.fr.md
  11. 1
      subjects/btreetransplant.en.md
  12. 63
      subjects/listforeach.en.md
  13. 21
      subjects/listpushfront.en.md
  14. 41
      subjects/listreverse.en.md

20
subjects/btreeapplybylevel.en.md

@ -2,7 +2,7 @@
### Instructions ### 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. This function must have the following signature.
@ -23,26 +23,26 @@ package main
import ( import (
"fmt" "fmt"
student ".." piscine ".."
) )
func main() { func main() {
root := &student.TreeNode{Data: "4"} root := &piscine.TreeNode{Data: "4"}
student.BTreeInsertData(root, "1") piscine.BTreeInsertData(root, "1")
student.BTreeInsertData(root, "7") piscine.BTreeInsertData(root, "7")
student.BTreeInsertData(root, "5") piscine.BTreeInsertData(root, "5")
student.BTreeApplyByLevel(root, fmt.Println) piscine.BTreeApplyByLevel(root, fmt.Println)
} }
``` ```
And its output : And its output :
```console ```console
student@ubuntu:~/student/btreeapplybylevel$ go build student@ubuntu:~/student/test$ go build
student@ubuntu:~/student/btreeapplybylevel$ ./btreeapplybylevel student@ubuntu:~/student/test$ ./test
4 4
1 1
7 7
5 5
student@ubuntu:~/student/btreeapplybylevel$ student@ubuntu:~/student/test$
``` ```

20
subjects/btreeapplybylevel.fr.md

@ -2,7 +2,7 @@
### Instructions ### 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. This function must have the following signature.
@ -23,26 +23,26 @@ package main
import ( import (
"fmt" "fmt"
student ".." piscine ".."
) )
func main() { func main() {
root := &student.TreeNode{Data: "4"} root := &piscine.TreeNode{Data: "4"}
student.BTreeInsertData(root, "1") piscine.BTreeInsertData(root, "1")
student.BTreeInsertData(root, "7") piscine.BTreeInsertData(root, "7")
student.BTreeInsertData(root, "5") piscine.BTreeInsertData(root, "5")
student.BTreeApplyByLevel(root, fmt.Println) piscine.BTreeApplyByLevel(root, fmt.Println)
} }
``` ```
And its output : And its output :
```console ```console
student@ubuntu:~/student/btreeapplybylevel$ go build student@ubuntu:~/student/test$ go build
student@ubuntu:~/student/btreeapplybylevel$ ./btreeapplybylevel student@ubuntu:~/student/test$ ./test
4 4
1 1
7 7
5 5
student@ubuntu:~/student/btreeapplybylevel$ student@ubuntu:~/student/test$
``` ```

18
subjects/btreeisbinary.en.md

@ -23,23 +23,23 @@ package main
import ( import (
"fmt" "fmt"
student ".." piscine ".."
) )
func main() { func main() {
root := &student.TreeNode{Data: "4"} root := &piscine.TreeNode{Data: "4"}
student.BTreeInsertData(root, "1") piscine.BTreeInsertData(root, "1")
student.BTreeInsertData(root, "7") piscine.BTreeInsertData(root, "7")
student.BTreeInsertData(root, "5") piscine.BTreeInsertData(root, "5")
fmt.Println(student.BTreeIsBinary(root)) fmt.Println(piscine.BTreeIsBinary(root))
} }
``` ```
And its output : And its output :
```console ```console
student@ubuntu:~/student/btreeisbinary$ go build student@ubuntu:~/student/test$ go build
student@ubuntu:~/student/btreeisbinary$ ./btreeisbinary student@ubuntu:~/student/test$ ./test
true true
student@ubuntu:~/student/btreeisbinary$ student@ubuntu:~/student/test$
``` ```

18
subjects/btreeisbinary.fr.md

@ -23,23 +23,23 @@ package main
import ( import (
"fmt" "fmt"
student ".." piscine ".."
) )
func main() { func main() {
root := &student.TreeNode{Data: "4"} root := &piscine.TreeNode{Data: "4"}
student.BTreeInsertData(root, "1") piscine.BTreeInsertData(root, "1")
student.BTreeInsertData(root, "7") piscine.BTreeInsertData(root, "7")
student.BTreeInsertData(root, "5") piscine.BTreeInsertData(root, "5")
fmt.Println(student.BTreeIsBinary(root)) fmt.Println(piscine.BTreeIsBinary(root))
} }
``` ```
And its output : And its output :
```console ```console
student@ubuntu:~/student/btreeisbinary$ go build student@ubuntu:~/student/test$ go build
student@ubuntu:~/student/btreeisbinary$ ./btreeisbinary student@ubuntu:~/student/test$ ./test
true true
student@ubuntu:~/student/btreeisbinary$ student@ubuntu:~/student/test$
``` ```

21
subjects/btreelevelcount.en.md

@ -7,7 +7,7 @@ Write a function, BTreeLevelCount, that return the number of levels of the tree
### Expected function ### Expected function
```go ```go
func BTreeLevelCount(root *piscine.TreeNode) int { func BTreeLevelCount(root *TreeNode) int {
} }
``` ```
@ -21,23 +21,24 @@ package main
import ( import (
"fmt" "fmt"
student ".."
piscine ".."
) )
func main() { func main() {
root := &student.TreeNode{Data: "4"} root := &piscine.TreeNode{Data: "4"}
student.BTreeInsertData(root, "1") piscine.BTreeInsertData(root, "1")
student.BTreeInsertData(root, "7") piscine.BTreeInsertData(root, "7")
student.BTreeInsertData(root, "5") piscine.BTreeInsertData(root, "5")
fmt.Println(BTreeLevelCount(root)) fmt.Println(piscine.BTreeLevelCount(root))
} }
``` ```
And its output : And its output :
```console ```console
student@ubuntu:~/student/btreesearchitem$ go build student@ubuntu:~/student/test$ go build
student@ubuntu:~/student/btreesearchitem$ ./btreesearchitem student@ubuntu:~/student/test$ ./test
3 3
student@ubuntu:~/student/btreesearchitem$ student@ubuntu:~/student/test$
``` ```

21
subjects/btreelevelcount.fr.md

@ -7,7 +7,7 @@ Write a function, BTreeLevelCount, that return the number of levels of the tree
### Expected function ### Expected function
```go ```go
func BTreeLevelCount(root *piscine.TreeNode) int { func BTreeLevelCount(root *TreeNode) int {
} }
``` ```
@ -21,23 +21,24 @@ package main
import ( import (
"fmt" "fmt"
student ".."
piscine ".."
) )
func main() { func main() {
root := &student.TreeNode{Data: "4"} root := &piscine.TreeNode{Data: "4"}
student.BTreeInsertData(root, "1") piscine.BTreeInsertData(root, "1")
student.BTreeInsertData(root, "7") piscine.BTreeInsertData(root, "7")
student.BTreeInsertData(root, "5") piscine.BTreeInsertData(root, "5")
fmt.Println(BTreeLevelCount(root)) fmt.Println(piscine.BTreeLevelCount(root))
} }
``` ```
And its output : And its output :
```console ```console
student@ubuntu:~/student/btreesearchitem$ go build student@ubuntu:~/student/test$ go build
student@ubuntu:~/student/btreesearchitem$ ./btreesearchitem student@ubuntu:~/student/test$ ./test
3 3
student@ubuntu:~/student/btreesearchitem$ student@ubuntu:~/student/test$
``` ```

21
subjects/btreemax.en.md

@ -2,7 +2,7 @@
### Instructions ### 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. This function must have the following signature.
@ -23,15 +23,16 @@ package main
import ( import (
"fmt" "fmt"
student ".."
piscine ".."
) )
func main() { func main() {
root := &student.TreeNode{Data: "4"} root := &piscine.TreeNode{Data: "4"}
student.BTreeInsertData(root, "1") piscine.BTreeInsertData(root, "1")
student.BTreeInsertData(root, "7") piscine.BTreeInsertData(root, "7")
student.BTreeInsertData(root, "5") piscine.BTreeInsertData(root, "5")
max := student.BTreeMax(root) max := piscine.BTreeMax(root)
fmt.Println(max.Data) fmt.Println(max.Data)
} }
``` ```
@ -39,8 +40,8 @@ func main() {
And its output : And its output :
```console ```console
student@ubuntu:~/student/btreemax$ go build student@ubuntu:~/student/test$ go build
student@ubuntu:~/student/btreemax$ ./btreemax student@ubuntu:~/student/test$ ./test
7 7
student@ubuntu:~/student/btreemax$ student@ubuntu:~/student/test$
``` ```

21
subjects/btreemax.fr.md

@ -2,7 +2,7 @@
### Instructions ### 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. This function must have the following signature.
@ -23,15 +23,16 @@ package main
import ( import (
"fmt" "fmt"
student ".."
piscine ".."
) )
func main() { func main() {
root := &student.TreeNode{Data: "4"} root := &piscine.TreeNode{Data: "4"}
student.BTreeInsertData(root, "1") piscine.BTreeInsertData(root, "1")
student.BTreeInsertData(root, "7") piscine.BTreeInsertData(root, "7")
student.BTreeInsertData(root, "5") piscine.BTreeInsertData(root, "5")
max := student.BTreeMax(root) max := piscine.BTreeMax(root)
fmt.Println(max.Data) fmt.Println(max.Data)
} }
``` ```
@ -39,8 +40,8 @@ func main() {
And its output : And its output :
```console ```console
student@ubuntu:~/student/btreemax$ go build student@ubuntu:~/student/test$ go build
student@ubuntu:~/student/btreemax$ ./btreemax student@ubuntu:~/student/test$ ./test
7 7
student@ubuntu:~/student/btreemax$ student@ubuntu:~/student/test$
``` ```

21
subjects/btreemin.en.md

@ -2,7 +2,7 @@
### Instructions ### 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. This function must have the following signature.
@ -23,15 +23,16 @@ package main
import ( import (
"fmt" "fmt"
student ".."
piscine ".."
) )
func main() { func main() {
root := &student.TreeNode{Data: "4"} root := &piscine.TreeNode{Data: "4"}
student.BTreeInsertData(root, "1") piscine.BTreeInsertData(root, "1")
student.BTreeInsertData(root, "7") piscine.BTreeInsertData(root, "7")
student.BTreeInsertData(root, "5") piscine.BTreeInsertData(root, "5")
min := student.BTreeMin(root) min := piscine.BTreeMin(root)
fmt.Println(min.Data) fmt.Println(min.Data)
} }
``` ```
@ -39,8 +40,8 @@ func main() {
And its output : And its output :
```console ```console
student@ubuntu:~/student/btreemin$ go build student@ubuntu:~/student/test$ go build
student@ubuntu:~/student/btreemin$ ./btreemin student@ubuntu:~/student/test$ ./test
1 1
student@ubuntu:~/student/btreemin$ student@ubuntu:~/student/test$
``` ```

21
subjects/btreemin.fr.md

@ -2,7 +2,7 @@
### Instructions ### 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. This function must have the following signature.
@ -23,15 +23,16 @@ package main
import ( import (
"fmt" "fmt"
student ".."
piscine ".."
) )
func main() { func main() {
root := &student.TreeNode{Data: "4"} root := &piscine.TreeNode{Data: "4"}
student.BTreeInsertData(root, "1") piscine.BTreeInsertData(root, "1")
student.BTreeInsertData(root, "7") piscine.BTreeInsertData(root, "7")
student.BTreeInsertData(root, "5") piscine.BTreeInsertData(root, "5")
min := student.BTreeMin(root) min := piscine.BTreeMin(root)
fmt.Println(min.Data) fmt.Println(min.Data)
} }
``` ```
@ -39,8 +40,8 @@ func main() {
And its output : And its output :
```console ```console
student@ubuntu:~/student/btreemin$ go build student@ubuntu:~/student/test$ go build
student@ubuntu:~/student/btreemin$ ./btreemin student@ubuntu:~/student/test$ ./test
1 1
student@ubuntu:~/student/btreemin$ student@ubuntu:~/student/test$
``` ```

1
subjects/btreetransplant.en.md

@ -2,6 +2,7 @@
### Instructions ### 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`. 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. This function must have the following signature.

63
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. 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 ### Expected function and structure
```go ```go
type node struct { type NodeL struct {
data interface{} Data interface{}
next *node Next *NodeL
}
type List struct {
Head *NodeL
Tail *NodeL
} }
type list struct { func ListForEach(l *List, f func(*NodeL)) {
head *node
tail *node
} }
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() { func main() {
link := &list{} link := &piscine.List{}
piscine.ListPushBack(link, 1) piscine.ListPushBack(link, "1")
piscine.ListPushBack(link, 2) piscine.ListPushBack(link, "2")
piscine.ListPushBack(link, 3) piscine.ListPushBack(link, "3")
piscine.ListPushBack(link, 4) piscine.ListPushBack(link, "5")
piscine.ListForEach(link, piscine.ListReverse) piscine.ListForEach(link, piscine.Add2)
for link.head != nil { it := link.Head
fmt.Println(link.head.data) for it != nil {
link.head = link.head.next fmt.Println(it.Data)
it = it.Next
} }
} }
``` ```
@ -57,9 +78,9 @@ And its output :
```console ```console
student@ubuntu:~/piscine/test$ go build student@ubuntu:~/piscine/test$ go build
student@ubuntu:~/piscine/test$ ./test student@ubuntu:~/piscine/test$ ./test
4 12
3 22
2 32
1 52
student@ubuntu:~/piscine/test$ student@ubuntu:~/piscine/test$
``` ```

21
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 ### Expected function and structure
```go ```go
type Node struct { type NodeL struct {
Data interface{} Data interface{}
Next *Node Next *NodeL
} }
type List struct { type List struct {
Head *Node Head *NodeL
Tail *Node 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 package main
import ( import (
"fmt"
piscine ".." piscine ".."
"fmt"
) )
func main() { func main() {
link := &list{} link := &piscine.List{}
piscine.ListPushFront(link, "Hello") piscine.ListPushFront(link, "Hello")
piscine.ListPushFront(link, "man") piscine.ListPushFront(link, "man")
piscine.ListPushFront(link, "how are you") piscine.ListPushFront(link, "how are you")
for link.head != nil { it := link.Head
fmt.Println(link.head.data) for it != nil {
link.head = link.head.next fmt.Println(it.Data)
it = it.Next
} }
} }
``` ```

41
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 ### Expected function and structure
```go ```go
type node struct { type NodeL struct {
data interface{} Data interface{}
next *node Next *NodeL
} }
type list struct { type List struct {
head *node Head *NodeL
tail *node Tail *NodeL
} }
func ListReverse(l *list) { func ListReverse(l *List) {
} }
``` ```
@ -36,19 +36,24 @@ import (
) )
func main() { func main() {
link := &list{} link := &piscine.List{}
listPushBack(link, 1) piscine.ListPushBack(link, 1)
listPushBack(link, 2) piscine.ListPushBack(link, 2)
listPushBack(link, 3) piscine.ListPushBack(link, 3)
listPushBack(link, 4) piscine.ListPushBack(link, 4)
listReverse(link) piscine.ListReverse(link)
for link.head != nil { it := link.Head
fmt.Println(link.head.data)
link.head = link.head.next 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 3
2 2
1 1
Tail &{1 <nil>}
Head &{4 0xc42000a140}
student@ubuntu:~/piscine/test$ student@ubuntu:~/piscine/test$
``` ```
Loading…
Cancel
Save