mirror of https://github.com/01-edu/public.git
readmes for binary trees exercises and some of quest 10
This commit is contained in:
parent
83f7836b84
commit
e8e325ce4f
|
@ -0,0 +1,42 @@
|
|||
# abort
|
||||
## Instructions
|
||||
|
||||
Write a function that returns the the value in the middle of 5 five arguments.
|
||||
|
||||
This function must have the following signature.
|
||||
|
||||
## Expected function
|
||||
|
||||
```go
|
||||
func Abort(a, b, c, d, e int) int {
|
||||
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Here is a possible [program](TODO-LINK) to test your function :
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
student ".."
|
||||
)
|
||||
|
||||
func main() {
|
||||
middle := student.Abort(2, 3, 8, 5, 7)
|
||||
fmt.Println(middle)
|
||||
}
|
||||
```
|
||||
|
||||
And its output :
|
||||
|
||||
```console
|
||||
student@ubuntu:~/student/abort$ go build
|
||||
student@ubuntu:~/student/abort$ ./abort
|
||||
5
|
||||
student@ubuntu:~/student/abort$
|
||||
```
|
|
@ -0,0 +1,43 @@
|
|||
# activebits
|
||||
## Instructions
|
||||
|
||||
Write a function, ActiveBitsthat, that returns the number of active bits (bits with the value 1) in the binary representation of an integer number.
|
||||
|
||||
The function must have the next signature.
|
||||
|
||||
## Expected function
|
||||
|
||||
```go
|
||||
|
||||
func ActiveBits(n int) uint {
|
||||
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Here is a possible [program](TODO-LINK) to test your function :
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
student ".."
|
||||
)
|
||||
|
||||
func main() {
|
||||
nbits := student.ActiveBits(7)
|
||||
fmt.Println(nbits)
|
||||
}
|
||||
```
|
||||
|
||||
And its output :
|
||||
|
||||
```console
|
||||
student@ubuntu:~/student/activebits$ go build
|
||||
student@ubuntu:~/student/activebits$ ./activebits
|
||||
10
|
||||
student@ubuntu:~/student/activebits$
|
||||
```
|
|
@ -0,0 +1,48 @@
|
|||
# btreeapplybylevel
|
||||
## Instructions
|
||||
|
||||
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.
|
||||
|
||||
## Expected function
|
||||
|
||||
```go
|
||||
func BTreeApplyByLevel(root *TreeNode, fn interface{}) {
|
||||
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Here is a possible [program](TODO-LINK) to test your function :
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
student ".."
|
||||
)
|
||||
|
||||
func main() {
|
||||
root := &student.TreeNode{Data: "4"}
|
||||
student.BTreeInsertData(root, "1")
|
||||
student.BTreeInsertData(root, "7")
|
||||
student.BTreeInsertData(root, "5")
|
||||
student.BTreeApplyByLevel(root, fmt.Println)
|
||||
}
|
||||
```
|
||||
|
||||
And its output :
|
||||
|
||||
```console
|
||||
student@ubuntu:~/student/btreeapplybylevel$ go build
|
||||
student@ubuntu:~/student/btreeapplybylevel$ ./btreeapplybylevel
|
||||
4
|
||||
1
|
||||
7
|
||||
5
|
||||
student@ubuntu:~/student/btreeapplybylevel$
|
||||
```
|
|
@ -0,0 +1,60 @@
|
|||
# btreedeletenode
|
||||
## Instructions
|
||||
|
||||
Write a function, BTreeDeleteNode, that deletes 'node' from the tree given by root.
|
||||
|
||||
The resulting tree should still follow the binary search tree rules.
|
||||
|
||||
This function must have the following signature.
|
||||
|
||||
## Expected function
|
||||
|
||||
```go
|
||||
func BTreeDeleteNode(root, node *TreeNode) *TreeNode {
|
||||
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Here is a possible [program](TODO-LINK) to test your function :
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
student ".."
|
||||
)
|
||||
|
||||
func main() {
|
||||
root := &student.TreeNode{Data: "4"}
|
||||
student.BTreeInsertData(root, "1")
|
||||
student.BTreeInsertData(root, "7")
|
||||
student.BTreeInsertData(root, "5")
|
||||
node := student.BTreeSearchItem(root, "4")
|
||||
fmt.Println("Before delete:")
|
||||
student.BTreeApplyInorder(root, fmt.Println)
|
||||
root = student.BTreeDeleteNode(root, node)
|
||||
fmt.Println("After delete:")
|
||||
student.BTreeApplyInorder(root, fmt.Println)
|
||||
}
|
||||
```
|
||||
|
||||
And its output :
|
||||
|
||||
```console
|
||||
student@ubuntu:~/student/btreedeletenode$ go build
|
||||
student@ubuntu:~/student/btreedeletenode$ ./btreedeletenode
|
||||
Before delete:
|
||||
1
|
||||
4
|
||||
5
|
||||
7
|
||||
After delete:
|
||||
1
|
||||
5
|
||||
7
|
||||
student@ubuntu:~/student/btreedeletenode$
|
||||
```
|
|
@ -0,0 +1,45 @@
|
|||
# btreeisbinary
|
||||
## Instructions
|
||||
|
||||
Write a function, BTreeIsBinary, that returns true only if the tree given by root follows the binary search tree properties.
|
||||
|
||||
This function must have the following signature.
|
||||
|
||||
## Expected function
|
||||
|
||||
```go
|
||||
func BTreeIsBinary(root *TreeNode) bool {
|
||||
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Here is a possible [program](TODO-LINK) to test your function :
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
student ".."
|
||||
)
|
||||
|
||||
func main() {
|
||||
root := &student.TreeNode{Data: "4"}
|
||||
student.BTreeInsertData(root, "1")
|
||||
student.BTreeInsertData(root, "7")
|
||||
student.BTreeInsertData(root, "5")
|
||||
fmt.Println(student.BTreeIsBinary(root))
|
||||
}
|
||||
```
|
||||
|
||||
And its output :
|
||||
|
||||
```console
|
||||
student@ubuntu:~/student/btreeisbinary$ go build
|
||||
student@ubuntu:~/student/btreeisbinary$ ./btreeisbinary
|
||||
true
|
||||
student@ubuntu:~/student/btreeisbinary$
|
||||
```
|
|
@ -0,0 +1,43 @@
|
|||
# btreelevelcount
|
||||
## Instructions
|
||||
|
||||
Write a function, BTreeLevelCount, that return the number of levels of the tree (height of the tree)
|
||||
|
||||
## Expected function
|
||||
|
||||
```go
|
||||
func BTreeLevelCount(root *piscine.TreeNode) int {
|
||||
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Here is a possible [program](TODO-LINK) to test your function :
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
student ".."
|
||||
)
|
||||
|
||||
func main() {
|
||||
root := &student.TreeNode{Data: "4"}
|
||||
student.BTreeInsertData(root, "1")
|
||||
student.BTreeInsertData(root, "7")
|
||||
student.BTreeInsertData(root, "5")
|
||||
fmt.Println(BTreeLevelCount(root))
|
||||
}
|
||||
```
|
||||
|
||||
And its output :
|
||||
|
||||
```console
|
||||
student@ubuntu:~/student/btreesearchitem$ go build
|
||||
student@ubuntu:~/student/btreesearchitem$ ./btreesearchitem
|
||||
3
|
||||
student@ubuntu:~/student/btreesearchitem$
|
||||
```
|
|
@ -0,0 +1,46 @@
|
|||
# btreemax
|
||||
## Instructions
|
||||
|
||||
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.
|
||||
|
||||
## Expected function
|
||||
|
||||
```go
|
||||
func BTreeMax(root *TreeNode) *TreeNode {
|
||||
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Here is a possible [program](TODO-LINK) to test your function :
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
student ".."
|
||||
)
|
||||
|
||||
func main() {
|
||||
root := &student.TreeNode{Data: "4"}
|
||||
student.BTreeInsertData(root, "1")
|
||||
student.BTreeInsertData(root, "7")
|
||||
student.BTreeInsertData(root, "5")
|
||||
max := student.BTreeMax(root)
|
||||
fmt.Println(max.Data)
|
||||
}
|
||||
```
|
||||
|
||||
And its output :
|
||||
|
||||
```console
|
||||
student@ubuntu:~/student/btreemax$ go build
|
||||
student@ubuntu:~/student/btreemax$ ./btreemax
|
||||
7
|
||||
student@ubuntu:~/student/btreemax$
|
||||
```
|
|
@ -0,0 +1,46 @@
|
|||
# btreemin
|
||||
## Instructions
|
||||
|
||||
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.
|
||||
|
||||
## Expected function
|
||||
|
||||
```go
|
||||
func BTreeMin(root *TreeNode) *TreeNode {
|
||||
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Here is a possible [program](TODO-LINK) to test your function :
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
student ".."
|
||||
)
|
||||
|
||||
func main() {
|
||||
root := &student.TreeNode{Data: "4"}
|
||||
student.BTreeInsertData(root, "1")
|
||||
student.BTreeInsertData(root, "7")
|
||||
student.BTreeInsertData(root, "5")
|
||||
min := student.BTreeMin(root)
|
||||
fmt.Println(min.Data)
|
||||
}
|
||||
```
|
||||
|
||||
And its output :
|
||||
|
||||
```console
|
||||
student@ubuntu:~/student/btreemin$ go build
|
||||
student@ubuntu:~/student/btreemin$ ./btreemin
|
||||
1
|
||||
student@ubuntu:~/student/btreemin$
|
||||
```
|
|
@ -0,0 +1,51 @@
|
|||
# btreetransplant
|
||||
## 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.
|
||||
|
||||
## Expected function
|
||||
|
||||
```go
|
||||
func BTreeTransplant(root, node, rplc *TreeNode) *TreeNode {
|
||||
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Here is a possible [program](TODO-LINK) to test your function :
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
student ".."
|
||||
)
|
||||
|
||||
func main() {
|
||||
root := &student.TreeNode{Data: "4"}
|
||||
student.BTreeInsertData(root, "1")
|
||||
student.BTreeInsertData(root, "7")
|
||||
student.BTreeInsertData(root, "5")
|
||||
node := student.BTreeSearchItem(root, "1")
|
||||
replacement := &student.TreeNode{Data: "3"}
|
||||
root = student.BTreeTransplant(root, node, replacement)
|
||||
student.BTreeApplyInorder(root, fmt.Println)
|
||||
}
|
||||
```
|
||||
|
||||
And its output :
|
||||
|
||||
```console
|
||||
student@ubuntu:~/student/btreetransplant$ go build
|
||||
student@ubuntu:~/student/btreetrandsplant$ ./btreetransplant
|
||||
3
|
||||
4
|
||||
5
|
||||
7
|
||||
student@ubuntu:~/student/btreetrandsplant$
|
||||
```
|
|
@ -0,0 +1,42 @@
|
|||
# collatzcountdown
|
||||
## Instructions
|
||||
|
||||
Write a function, CollatzCountdown, that returns the number of steps to reach 1 using the collatz countdown.
|
||||
|
||||
The function must have the following signature.
|
||||
|
||||
## Expected function
|
||||
|
||||
```go
|
||||
func CollatzCountdown(start int) int {
|
||||
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Here is a possible [program](TODO-LINK) to test your function :
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
student ".."
|
||||
)
|
||||
|
||||
func main() {
|
||||
steps := student.CollatzCountdown(12)
|
||||
fmt.Println(steps)
|
||||
}
|
||||
```
|
||||
|
||||
And its output :
|
||||
|
||||
```console
|
||||
student@ubuntu:~/student/collatzcountdown$ go build
|
||||
student@ubuntu:~/student/collatzcountdown$ ./collatzcountdown
|
||||
10
|
||||
student@ubuntu:~/student/collatzcountdown$
|
||||
```
|
|
@ -0,0 +1,86 @@
|
|||
# enigma
|
||||
## Instructions
|
||||
|
||||
Write a function called `Enigma` that receives poiters to functions and move its values around to hide them
|
||||
|
||||
This function will put a into c; c into d; d into b and b into a
|
||||
|
||||
This function must have the following signature.
|
||||
|
||||
## Expected function
|
||||
|
||||
```go
|
||||
func Enigma(a ***int, b *int, c *******int, d ****int) {
|
||||
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Here is a possible [program](TODO-LINK) to test your function :
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
student ".."
|
||||
)
|
||||
|
||||
func main() {
|
||||
x := 5
|
||||
y := &x
|
||||
z := &y
|
||||
a := &z
|
||||
|
||||
w := 2
|
||||
b := &w
|
||||
|
||||
u := 7
|
||||
e := &u
|
||||
f := &e
|
||||
g := &f
|
||||
h := &g
|
||||
i := &h
|
||||
j := &i
|
||||
c := &j
|
||||
|
||||
k := 6
|
||||
l := &k
|
||||
m := &l
|
||||
n := &m
|
||||
d := &n
|
||||
|
||||
fmt.Println(***a)
|
||||
fmt.Println(*b)
|
||||
fmt.Println(*******c)
|
||||
fmt.Println(****d)
|
||||
|
||||
student.Enigma(a, b, c, d)
|
||||
|
||||
fmt.Println("After using Enigma")
|
||||
fmt.Println(***a)
|
||||
fmt.Println(*b)
|
||||
fmt.Println(*******c)
|
||||
fmt.Println(****d)
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
And its output :
|
||||
|
||||
```console
|
||||
student@ubuntu:~/student/enigma$ go build
|
||||
student@ubuntu:~/student/enigma$ ./enigma
|
||||
5
|
||||
2
|
||||
7
|
||||
6
|
||||
After using Enigma
|
||||
2
|
||||
6
|
||||
5
|
||||
7
|
||||
student@ubuntu:~/student/enigma$
|
||||
```
|
|
@ -0,0 +1,44 @@
|
|||
# join
|
||||
## Instructions
|
||||
|
||||
Write a function, Join, that returns the elements of a slice strings (arstr) join together in one string. Using the string 'sep' as a separator between each element of the array
|
||||
|
||||
The function must have the next signature.
|
||||
|
||||
## Expected function
|
||||
|
||||
```go
|
||||
|
||||
func Join(arstr []string, sep string) string {
|
||||
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Here is a possible [program](TODO-LINK) to test your function :
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
student ".."
|
||||
)
|
||||
|
||||
func main() {
|
||||
arrStr := []string{"hello", "how", "are", "you"}
|
||||
joined := student.Join(arrStr, "--")
|
||||
fmt.Println(joined)
|
||||
}
|
||||
```
|
||||
|
||||
And its output :
|
||||
|
||||
```console
|
||||
student@ubuntu:~/student/join$ go build
|
||||
student@ubuntu:~/student/join$ ./join
|
||||
hello--how--are--you
|
||||
student@ubuntu:~/student/join$
|
||||
```
|
|
@ -0,0 +1,44 @@
|
|||
# max
|
||||
## Instructions
|
||||
|
||||
Write a function, Max, that returns the maximum value in a slice of integers
|
||||
|
||||
The function must have the next signature.
|
||||
|
||||
## Expected function
|
||||
|
||||
```go
|
||||
|
||||
func Max(arr []int) int {
|
||||
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Here is a possible [program](TODO-LINK) to test your function :
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
student ".."
|
||||
)
|
||||
|
||||
func main() {
|
||||
arrInt := []int{23, 123, 1, 11, 55, 93}
|
||||
max := student.Max(arrInt)
|
||||
fmt.Println(max
|
||||
}
|
||||
```
|
||||
|
||||
And its output :
|
||||
|
||||
```console
|
||||
student@ubuntu:~/student/max$ go build
|
||||
student@ubuntu:~/student/max$ ./max
|
||||
123
|
||||
student@ubuntu:~/student/max$
|
||||
```
|
|
@ -0,0 +1,26 @@
|
|||
# pilot
|
||||
## Instructions
|
||||
|
||||
Write a go file so that the following program compile
|
||||
|
||||
## Usage
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
student ".."
|
||||
)
|
||||
|
||||
func main() {
|
||||
var donnie student.Pilot
|
||||
donnie.Name = "Donnie"
|
||||
donnie.Life = 100.0
|
||||
donnie.Age = 24
|
||||
donnie.Aircraft = student.AIRCRAFT1
|
||||
|
||||
fmt.Println(donnie)
|
||||
}
|
||||
```
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
# join
|
||||
## Instructions
|
||||
|
||||
Write a function, Unmatch, that returns the element of the slice (arr) that does not have a correspondent pair.
|
||||
|
||||
The function must have the next signature.
|
||||
|
||||
## Expected function
|
||||
|
||||
```go
|
||||
|
||||
func Unmatch(arr []int) int {
|
||||
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Here is a possible [program](TODO-LINK) to test your function :
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
student ".."
|
||||
)
|
||||
|
||||
func main() {
|
||||
arr := []int{1, 2, 3, 1, 2, 3, 4}
|
||||
unmatch := student.Unmatch(arr)
|
||||
fmt.Println(unmatch)
|
||||
}
|
||||
```
|
||||
|
||||
And its output :
|
||||
|
||||
```console
|
||||
student@ubuntu:~/student/unmatch$ go build
|
||||
student@ubuntu:~/student/unmatch$ ./unmatch
|
||||
4
|
||||
student@ubuntu:~/student/unmatch$
|
||||
```
|
|
@ -0,0 +1,13 @@
|
|||
# ztail
|
||||
## Instructions
|
||||
|
||||
Write a program called ztail that does the same thing as the system command tail, but witch takes at least one file as argument.
|
||||
|
||||
The only option you have to handle is -c. This option will be used in all tests.
|
||||
|
||||
For this program you can use the "os" package.
|
||||
|
||||
For the program to pass the test you should follow the convention for the return code of program in Unix sistems (see os.Exit)
|
||||
|
||||
For more information consult the man page for tail.
|
||||
|
Loading…
Reference in New Issue