mirror of https://github.com/01-edu/public.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
134 lines
2.4 KiB
134 lines
2.4 KiB
5 years ago
|
## listforeachif
|
||
6 years ago
|
|
||
6 years ago
|
### Instructions
|
||
6 years ago
|
|
||
5 years ago
|
Write a function `ListForEachIf` that applies a function given as argument to the data within some of the nodes of the list `l`.
|
||
6 years ago
|
|
||
5 years ago
|
- This function receives two functions:
|
||
6 years ago
|
|
||
5 years ago
|
- `f` is a function that is applied to the node.
|
||
6 years ago
|
|
||
5 years ago
|
- `cond` is a function that returns a `boolean` and it will be used to determine if the function `f` should be applied to the node.
|
||
5 years ago
|
|
||
5 years ago
|
- The function given as argument must have a pointer `*NodeL` as argument.
|
||
6 years ago
|
|
||
6 years ago
|
### Expected function and structure
|
||
6 years ago
|
|
||
|
```go
|
||
5 years ago
|
type NodeL struct {
|
||
|
Data interface{}
|
||
|
Next *NodeL
|
||
6 years ago
|
}
|
||
|
|
||
5 years ago
|
type List struct {
|
||
|
Head *NodeL
|
||
|
Tail *NodeL
|
||
6 years ago
|
}
|
||
|
|
||
5 years ago
|
func IsPositive_node(node *NodeL) bool {
|
||
|
switch node.Data.(type) {
|
||
|
case int, float32, float64, byte:
|
||
|
return node.Data.(int) > 0
|
||
|
case string, rune:
|
||
|
return false
|
||
|
}
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
func IsNegative_node(node *NodeL) bool {
|
||
|
switch node.Data.(type) {
|
||
|
case int, float32, float64, byte:
|
||
5 years ago
|
return node.Data.(int) < 0
|
||
5 years ago
|
case string, rune:
|
||
|
return false
|
||
|
}
|
||
|
return false
|
||
|
}
|
||
6 years ago
|
|
||
5 years ago
|
func IsNotNumeric_node(node *NodeL) bool {
|
||
|
switch node.Data.(type) {
|
||
|
case int, float32, float64, byte:
|
||
|
return false
|
||
|
case string, rune:
|
||
|
return true
|
||
|
}
|
||
|
return true
|
||
6 years ago
|
}
|
||
|
|
||
5 years ago
|
func ListForEachIf(l *List, f func(*NodeL), cond func(*NodeL) bool) {
|
||
6 years ago
|
|
||
|
}
|
||
|
```
|
||
|
|
||
6 years ago
|
### Usage
|
||
6 years ago
|
|
||
5 years ago
|
Here is a possible program to test your function :
|
||
6 years ago
|
|
||
|
```go
|
||
|
package main
|
||
|
|
||
|
import (
|
||
|
piscine ".."
|
||
5 years ago
|
"fmt"
|
||
6 years ago
|
)
|
||
|
|
||
5 years ago
|
func PrintElem(node *piscine.NodeL) {
|
||
|
fmt.Println(node.Data)
|
||
6 years ago
|
}
|
||
|
|
||
5 years ago
|
func StringToInt(node *piscine.NodeL) {
|
||
|
node.Data = 2
|
||
6 years ago
|
}
|
||
|
|
||
5 years ago
|
func PrintList(l *piscine.List) {
|
||
|
it := l.Head
|
||
|
for it != nil {
|
||
|
fmt.Print(it.Data, "->")
|
||
|
it = it.Next
|
||
6 years ago
|
}
|
||
5 years ago
|
fmt.Print("nil","\n")
|
||
6 years ago
|
}
|
||
5 years ago
|
|
||
6 years ago
|
func main() {
|
||
5 years ago
|
link := &piscine.List{}
|
||
6 years ago
|
|
||
|
piscine.ListPushBack(link, 1)
|
||
|
piscine.ListPushBack(link, "hello")
|
||
|
piscine.ListPushBack(link, 3)
|
||
|
piscine.ListPushBack(link, "there")
|
||
|
piscine.ListPushBack(link, 23)
|
||
|
piscine.ListPushBack(link, "!")
|
||
|
piscine.ListPushBack(link, 54)
|
||
|
|
||
5 years ago
|
PrintList(link)
|
||
6 years ago
|
|
||
|
fmt.Println("--------function applied--------")
|
||
5 years ago
|
piscine.ListForEachIf(link, PrintElem, piscine.IsPositive_node)
|
||
6 years ago
|
|
||
5 years ago
|
piscine.ListForEachIf(link, StringToInt, piscine.IsNotNumeric_node)
|
||
6 years ago
|
|
||
|
fmt.Println("--------function applied--------")
|
||
5 years ago
|
PrintList(link)
|
||
6 years ago
|
|
||
|
fmt.Println()
|
||
|
}
|
||
|
```
|
||
|
|
||
|
And its output :
|
||
|
|
||
|
```console
|
||
5 years ago
|
student@ubuntu:~/[[ROOT]]/test$ go build
|
||
|
student@ubuntu:~/[[ROOT]]/test$ ./test
|
||
5 years ago
|
1->hello->3->there->23->!->54->nil
|
||
6 years ago
|
--------function applied--------
|
||
5 years ago
|
1
|
||
|
3
|
||
|
23
|
||
|
54
|
||
6 years ago
|
--------function applied--------
|
||
5 years ago
|
1->2->3->2->23->2->54->nil
|
||
|
|
||
5 years ago
|
student@ubuntu:~/[[ROOT]]/test$
|
||
|
student@ubuntu:~/[[ROOT]]/test$
|
||
6 years ago
|
```
|