-
Notifications
You must be signed in to change notification settings - Fork 1
/
fromto.go
56 lines (50 loc) · 896 Bytes
/
fromto.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
51
52
53
54
55
56
package main
import (
"fmt"
"strconv"
)
func fromto(from int, to int) string {
var result string
if from <= 0 || to <= 0 || from > 99 || to > 99 {
return "invalid\n"
}
if from > to {
for i := from; i >= to; i-- {
if i < 10 {
result += "0" + strconv.Itoa(i)
} else {
result += strconv.Itoa(i)
}
if i != to {
result += ", "
}
}
}
if from < to {
for i := from; i <= to; i++ {
if i < 10 {
result = result + "0" + strconv.Itoa(i)
} else {
result = result + strconv.Itoa(i)
}
if i != to {
result += ", "
}
}
}
if from == to {
result = strconv.Itoa(to)
}
return result + "\n"
}
func main() {
fmt.Print(fromto(1, 10))
fmt.Print(fromto(10, 1))
fmt.Print(fromto(1, 20))
fmt.Print(fromto(10, 10))
fmt.Print(fromto(11, 13))
fmt.Print(fromto(9, 13))
fmt.Print(fromto(15, 5))
fmt.Print(fromto(-1, 10))
}
// checkpoint