forked from root/public
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
|
||
5 years ago
|
|
||
5 years ago
|
### Instructions
|
||
5 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`.
|
||
5 years ago
|
|
||
5 years ago
|
- This function receives two functions:
|
||
5 years ago
|
|
||
5 years ago
|
- `f` is a function that is applied to the node.
|
||
5 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.
|
||
5 years ago
|
|
||
5 years ago
|
### Expected function and structure
|
||
5 years ago
|
|
||
|
```go
|
||
![]()
5 years ago
|
type NodeL struct {
|
||
|
Data interface{}
|
||
|
Next *NodeL
|
||
5 years ago
|
}
|
||
|
|
||
![]()
5 years ago
|
type List struct {
|
||
|
Head *NodeL
|
||
|
Tail *NodeL
|
||
5 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
|
||
|
}
|
||
5 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
|
||
5 years ago
|
}
|
||
|
|
||
![]()
5 years ago
|
func ListForEachIf(l *List, f func(*NodeL), cond func(*NodeL) bool) {
|
||
5 years ago
|
|
||
|
}
|
||
|
```
|
||
|
|
||
5 years ago
|
### Usage
|
||
5 years ago
|
|
||
5 years ago
|
Here is a possible program to test your function :
|
||
5 years ago
|
|
||
|
```go
|
||
|
package main
|
||
|
|
||
|
import (
|
||
|
piscine ".."
|
||
![]()
5 years ago
|
"fmt"
|
||
5 years ago
|
)
|
||
|
|
||
![]()
5 years ago
|
func PrintElem(node *piscine.NodeL) {
|
||
|
fmt.Println(node.Data)
|
||
5 years ago
|
}
|
||
|
|
||
![]()
5 years ago
|
func StringToInt(node *piscine.NodeL) {
|
||
|
node.Data = 2
|
||
5 years ago
|
}
|
||
|
|
||
![]()
5 years ago
|
func PrintList(l *piscine.List) {
|
||
|
it := l.Head
|
||
|
for it != nil {
|
||
|
fmt.Print(it.Data, "->")
|
||
|
it = it.Next
|
||
5 years ago
|
}
|
||
5 years ago
|
fmt.Print("nil","\n")
|
||
5 years ago
|
}
|
||
![]()
5 years ago
|
|
||
5 years ago
|
func main() {
|
||
![]()
5 years ago
|
link := &piscine.List{}
|
||
5 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)
|
||
5 years ago
|
|
||
|
fmt.Println("--------function applied--------")
|
||
![]()
5 years ago
|
piscine.ListForEachIf(link, PrintElem, piscine.IsPositive_node)
|
||
5 years ago
|
|
||
![]()
5 years ago
|
piscine.ListForEachIf(link, StringToInt, piscine.IsNotNumeric_node)
|
||
5 years ago
|
|
||
|
fmt.Println("--------function applied--------")
|
||
![]()
5 years ago
|
PrintList(link)
|
||
5 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
|
||
5 years ago
|
--------function applied--------
|
||
5 years ago
|
1
|
||
|
3
|
||
|
23
|
||
|
54
|
||
5 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$
|
||
5 years ago
|
```
|