|
|
|
// A maximum of 3 points will be given for the first test execution, if the program correctly prints the sorted slice
|
|
|
|
// after entering three distinct integers. **Points are awarded incrementally each time that an integer
|
|
|
|
// is added and it correctly prints the sorted slice.
|
|
|
|
|
|
|
|
//A maximum of 2 points will be given for the second test execution,
|
|
|
|
// if the program correctly prints the sorted slice after entering four distinct integers.
|
|
|
|
// **Points are awarded if it correctly prints the sorted slice after adding the fourth integer.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func sortIntegers(integers []int) {
|
|
|
|
// Insertion sort O(n^2)
|
|
|
|
i := 1
|
|
|
|
for i < len(integers) {
|
|
|
|
insert := integers[i]
|
|
|
|
j := i - 1
|
|
|
|
for j >= 0 {
|
|
|
|
if insert < integers[j] {
|
|
|
|
integers[j+1] = integers[j]
|
|
|
|
j -= 1
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
integers[j+1] = insert
|
|
|
|
i += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
inputReader := bufio.NewReader(os.Stdin)
|
|
|
|
var integers []int
|
|
|
|
|
|
|
|
for {
|
|
|
|
fmt.Println("Enter a integer of text: ")
|
|
|
|
line, err := inputReader.ReadString('\n')
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "An error occurred with your input.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(line, "X") {
|
|
|
|
fmt.Println("Closing...")
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
inputInteger, err := strconv.Atoi(strings.TrimSpace(line))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Could not convert %s to integer", strings.TrimSpace(line))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
integers = append(integers, inputInteger)
|
|
|
|
sortIntegers(integers)
|
|
|
|
fmt.Println(integers)
|
|
|
|
}
|
|
|
|
}
|