-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpt.go
50 lines (40 loc) · 942 Bytes
/
gpt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"fmt"
"strconv"
"unicode"
)
func getFirstAndLastNumbers(input string) (first, last int, err error) {
var firstDigit, lastDigitFound bool
for _, char := range input {
if unicode.IsDigit(char) {
if !firstDigit {
// Convert the first digit found to an integer
first, err = strconv.Atoi(string(char))
if err != nil {
return 0, 0, err
}
firstDigit = true
}
// Convert the current digit to an integer and update the last digit
last, err = strconv.Atoi(string(char))
if err != nil {
return 0, 0, err
}
lastDigitFound = true
}
}
if !lastDigitFound {
return 0, 0, fmt.Errorf("no digits found in the input string")
}
return first, last, nil
}
func main() {
input := "abc123def456"
first, last, err := getFirstAndLastNumbers(input)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Printf("First digit: %d\nLast digit: %d\n", first, last)
}