-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsliceutils.go
64 lines (48 loc) · 2.13 KB
/
sliceutils.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
57
58
59
60
61
62
63
64
package myslice
import (
"bufio"
"fmt"
"os"
"sort"
"strconv"
"strings"
)
/*
Assignment:
Write a program which prompts the user to enter integers and stores the integers in a sorted slice.
The program should be written as a loop. Before entering the loop, the program should create an empty integer slice of size (length) 3.
During each pass through the loop, the program prompts the user to enter an integer to be added to the slice.
The program adds the integer to the slice, sorts the slice, and prints the contents of the slice in sorted order.
The slice must grow in size to accommodate any number of integers which the user decides to enter.
The program should only quit (exiting the loop) when the user enters the character ‘X’ instead of an integer.
*/
//SortSliceOfIntAndPrint : adding integers and sorting them into the slice until use enter character X
func SortSliceOfIntAndPrint() {
intSlice := make([]int, 3) // initializing integer slice with length and capcity of 3
inputScanner := bufio.NewScanner(os.Stdin)
var userInputStr string
fmt.Println("Enter a number OR type X to exit the program.")
var index int
for inputScanner.Scan() {
userInputStr = inputScanner.Text()
userInputInt, err := strconv.Atoi(userInputStr) // converting string to int
if strings.EqualFold(userInputStr, "X") { // compares with case sensitivity
fmt.Println("Input is X - existing the program.")
break
} else if err != nil { // err is taking care of any other string than X and not a valid number.
fmt.Println("Input is not a valid number: ", err.Error())
break
} else {
intSlice[index] = userInputInt // assigning user input to slice
index++
fmt.Println("index and len of slice->", index, len(intSlice))
if len(intSlice) == index {
intSlice = append(intSlice, index) // increasing slice lenght & capacity when user input reach to existing cap of slice.
fmt.Println("slice appedned ->", intSlice, len(intSlice), cap(intSlice))
}
sort.Ints(intSlice) // sorting the slice
fmt.Println("sorted slice is->", intSlice)
}
fmt.Println("enter a number OR type X to exit the program.")
}
}