Update project structure
parent
58fb633451
commit
441b3a9d54
@ -0,0 +1,66 @@
|
||||
// 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)
|
||||
}
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
// 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 stringsToIntegers(inputStrings []string) []int {
|
||||
integers := make([]int, len(inputStrings))
|
||||
for i, v := range inputStrings {
|
||||
intValue, err := strconv.Atoi(v)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: %s could not be converted to integer", v)
|
||||
os.Exit(1)
|
||||
}
|
||||
integers[i] = intValue
|
||||
}
|
||||
return integers
|
||||
}
|
||||
|
||||
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, _ := inputReader.ReadString('\n')
|
||||
|
||||
}
|
||||
fmt.Println("Enter a line of text: ")
|
||||
line, _ := inputReader.ReadString('\n')
|
||||
|
||||
integersList := stringsToIntegers(strings.Split(line[:len(line)-1], " "))
|
||||
fmt.Println("You entered:", integersList)
|
||||
sortIntegers(integersList)
|
||||
fmt.Println("Those sorted are:", integersList)
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
module hello
|
||||
|
||||
go 1.21.0
|
@ -0,0 +1,12 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Hello prints the string "Hello, world"
|
||||
func Hello(name string) string {
|
||||
return "Hello, " + name
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println(Hello("world"))
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package main
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestHello(t *testing.T) {
|
||||
got := Hello("Drew")
|
||||
expected := "Hello, Drew"
|
||||
|
||||
if got != expected {
|
||||
t.Errorf("got %q, expected %q", got, expected)
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
fmt.Print("Enter request URL: ")
|
||||
url, err := reader.ReadString('\n')
|
||||
if err != nil {
|
||||
fmt.Println("Error:", err)
|
||||
return
|
||||
}
|
||||
url = strings.TrimSpace(url)
|
||||
|
||||
response, err := http.Get(url)
|
||||
if err != nil {
|
||||
fmt.Println("Error:", err)
|
||||
return
|
||||
}
|
||||
defer response.Body.Close()
|
||||
|
||||
body, err := ioutil.ReadAll(response.Body)
|
||||
if err != nil {
|
||||
fmt.Println("Error:", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(string(body))
|
||||
}
|
Loading…
Reference in New Issue