public/subjects/trimatoi/README.md

59 lines
1.0 KiB
Markdown
Raw Permalink Normal View History

2019-10-21 14:35:44 +00:00
## trimatoi
### Instructions
- Write a function that transforms numbers within a `string`, into an `int`.
2019-10-21 14:35:44 +00:00
- If the `-` sign is encountered before any number it should determine the sign of the returned `int`.
2019-10-21 14:35:44 +00:00
- This function should **only** return an `int`. In the case of an invalid input, the function should return `0`.
2019-10-21 14:35:44 +00:00
- **Note**: There will never be more than one sign in a `string` in the tests.
2019-12-10 14:30:06 +00:00
2019-10-21 14:35:44 +00:00
### Expected function
```go
func TrimAtoi(s string) int {
}
```
### Usage
2020-02-25 12:02:16 +00:00
Here is a possible program to test your function :
2019-10-21 14:35:44 +00:00
```go
package main
import (
"fmt"
"piscine"
2019-10-21 14:35:44 +00:00
)
func main() {
2020-05-17 13:04:41 +00:00
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"))
2021-06-07 17:05:53 +00:00
fmt.Println(piscine.TrimAtoi("sdx1+fa2W3s4"))
2019-10-21 14:35:44 +00:00
}
```
And its output :
```console
$ go run .
2019-10-21 14:35:44 +00:00
12345
12345
12345
0
1234
-1234
1234
2021-06-07 17:05:53 +00:00
1234
$
2019-10-21 14:35:44 +00:00
```