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.
58 lines
1.2 KiB
58 lines
1.2 KiB
5 years ago
|
## trimatoi
|
||
|
|
||
|
### Instructions
|
||
|
|
||
5 years ago
|
- Write a function that transforms a number within a `string` in a number represented as an `int`.
|
||
5 years ago
|
|
||
5 years ago
|
- For this exercise the handling of the signs + or - **has** to be taken into account. If one of the signs is encountered before any number it should determine the sign of the returned `int`.
|
||
5 years ago
|
|
||
5 years ago
|
- This function will **only** have to return the `int`. In case of invalid input, the function should return `0`.
|
||
5 years ago
|
|
||
5 years ago
|
- Note: There will never be more than one sign by string in the tests.
|
||
5 years ago
|
|
||
5 years ago
|
### Expected function
|
||
|
|
||
|
```go
|
||
|
func TrimAtoi(s string) int {
|
||
|
|
||
|
}
|
||
|
```
|
||
|
|
||
|
### Usage
|
||
|
|
||
5 years ago
|
Here is a possible program to test your function :
|
||
5 years ago
|
|
||
|
```go
|
||
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
piscine ".."
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
5 years ago
|
fmt.Println(piscine.TrimAtoi("12345"))
|
||
|
fmt.Println(piscine.TrimAtoi("str123ing45"))
|
||
|
fmt.Println(piscine.TrimAtoi("012 345"))
|
||
|
fmt.Println(piscine.TrimAtoi("Hello World!"))
|
||
|
fmt.Println(piscine.TrimAtoi("sd+x1fa2W3s4"))
|
||
|
fmt.Println(piscine.TrimAtoi("sd-x1fa2W3s4"))
|
||
|
fmt.Println(piscine.TrimAtoi("sdx1-fa2W3s4"))
|
||
5 years ago
|
}
|
||
|
```
|
||
|
|
||
|
And its output :
|
||
|
|
||
|
```console
|
||
5 years ago
|
student@ubuntu:~/[[ROOT]]/test$ go build
|
||
|
student@ubuntu:~/[[ROOT]]/test$ ./test
|
||
5 years ago
|
12345
|
||
|
12345
|
||
|
12345
|
||
|
0
|
||
|
1234
|
||
|
-1234
|
||
|
1234
|
||
5 years ago
|
student@ubuntu:~/[[ROOT]]/test$
|
||
5 years ago
|
```
|