Done with course 1
parent
9f1bb5ddc0
commit
c7de4c2f93
@ -0,0 +1,60 @@
|
|||||||
|
// 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)
|
||||||
|
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)
|
||||||
|
}
|
@ -1,13 +1,58 @@
|
|||||||
|
// HASHTABLES
|
||||||
|
// Hashtables contain key/value pairs
|
||||||
|
// Key must be unique (Arbitrary, must be hashable by the hash function though)
|
||||||
|
// Hash function is used to compute a slot for a key
|
||||||
|
// This goes on behind the scenes when you want to insert
|
||||||
|
// a value for a particular key.
|
||||||
|
// There can be collosions in a Hashtable, that occurs when two keys hash to the same
|
||||||
|
// slot. There are strategies to handle those collisions like putting them in a list and tracking them...
|
||||||
|
// When you get a collision like this your access and insert are a little slower.
|
||||||
|
// Allows for access in constant O(1) time. Lists are linear O(N) time for a lookup.
|
||||||
|
|
||||||
|
// MAPS
|
||||||
|
// GOLangs specific implementation of a Hashtable
|
||||||
|
// can use make function to create a map.
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
var myArray [5]int
|
// map declaration
|
||||||
|
var myMap map[string]int
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Println(myArray)
|
// Using make
|
||||||
myArray[0] = 100
|
m2 := make(map[string]int)
|
||||||
fmt.Println(myArray)
|
fmt.Println(m2)
|
||||||
myArray[len(myArray)-1] = 1000
|
// map literal
|
||||||
fmt.Println(myArray)
|
m3 := map[string]int{"Drew": 1337, "Margot": 9000}
|
||||||
|
fmt.Println(m3, len(m3))
|
||||||
|
fmt.Printf("Margot's power level: %d\n", m3["Margot"])
|
||||||
|
m3["Laura"] = 100_000 // like python you can use _ to see places
|
||||||
|
fmt.Println(m3, len(m3))
|
||||||
|
// delete value from map
|
||||||
|
delete(m3, "Drew")
|
||||||
|
fmt.Println(m3, len(m3))
|
||||||
|
|
||||||
|
// Two value assignment
|
||||||
|
// p will be a boolean if the key is in the map
|
||||||
|
val, p := m3["Margot"]
|
||||||
|
if p {
|
||||||
|
fmt.Println("Found key Margot in map with value: ", val)
|
||||||
|
} else {
|
||||||
|
fmt.Println("Key Margot not present in map. Got back: ", val)
|
||||||
|
}
|
||||||
|
|
||||||
|
val, p = m3["Drew"]
|
||||||
|
if p {
|
||||||
|
fmt.Println("Found key Drew in map with value: ", val)
|
||||||
|
} else {
|
||||||
|
fmt.Println("Key Drew not present in map. Got back: ", val)
|
||||||
|
}
|
||||||
|
|
||||||
|
// iterating through a map
|
||||||
|
for key, value := range m3 {
|
||||||
|
fmt.Printf("Key: %s, Value: %d\n", key, value)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,48 @@
|
|||||||
|
// A slice is a window on an underlying array
|
||||||
|
// Pointer, Length, Capacity
|
||||||
|
// They can be variable in length!
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
// file & package level variable
|
||||||
|
var myArray = [...]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(myArray)
|
||||||
|
//slices can overlap. Since they are a pointer to an underlying arrary. They are mutable.
|
||||||
|
s1 := myArray[0:6]
|
||||||
|
s2 := myArray[5:len(myArray)]
|
||||||
|
fmt.Println(s1)
|
||||||
|
fmt.Println(s2)
|
||||||
|
myArray[5] = 1337
|
||||||
|
fmt.Println(myArray)
|
||||||
|
fmt.Println(s1)
|
||||||
|
fmt.Println(s2)
|
||||||
|
// Writing to a slice changes the underlying arrary
|
||||||
|
s1[5] = 7331
|
||||||
|
fmt.Println(myArray)
|
||||||
|
fmt.Println(s1)
|
||||||
|
fmt.Println(s2)
|
||||||
|
|
||||||
|
// Initializing a slice creates and underlying arrary
|
||||||
|
var s3 []string
|
||||||
|
s3 = append(s3, "Hello")
|
||||||
|
s3 = append(s3, "Dirp")
|
||||||
|
fmt.Println(s3, len(s3), cap(s3))
|
||||||
|
|
||||||
|
// Another way to make an empty slice with a particular starting size
|
||||||
|
s4 := make([]int, 100) // third arg is capacity otherwise len = cap in this invocation
|
||||||
|
fmt.Println(s4)
|
||||||
|
|
||||||
|
// gotcha
|
||||||
|
fmt.Printf("S2 Len: %d Cap: %d\n", len(s2), cap(s2))
|
||||||
|
// This increased the cap of the slice which meant a new array was created.
|
||||||
|
// Now what was the overlapping element between s1 and s2 point to different arrays.
|
||||||
|
// So changing the first element of s2 no longer updates the last elment of s1.
|
||||||
|
s2 = append(s2, 100)
|
||||||
|
fmt.Printf("S2 Len: %d Cap: %d\n", len(s2), cap(s2))
|
||||||
|
s2[0] = 9000
|
||||||
|
fmt.Println(s1)
|
||||||
|
fmt.Println(s2)
|
||||||
|
}
|
@ -0,0 +1,35 @@
|
|||||||
|
// Aggregated Datatype
|
||||||
|
// Groups together objects of arbitray types into one object
|
||||||
|
// No inheritance!
|
||||||
|
// Straight from C
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
type Person struct {
|
||||||
|
name string
|
||||||
|
addr string
|
||||||
|
phone string
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// Empty
|
||||||
|
var p1 Person
|
||||||
|
fmt.Println(p1)
|
||||||
|
p1.name = "Drew"
|
||||||
|
p1.addr = "123 Fake Street"
|
||||||
|
p1.phone = "8675309"
|
||||||
|
fmt.Println(p1)
|
||||||
|
|
||||||
|
// Literal
|
||||||
|
p2 := Person{"Margot", "123 Fake Street", "8675309"}
|
||||||
|
fmt.Println(p2)
|
||||||
|
|
||||||
|
// Other literal
|
||||||
|
p3 := Person{name: "Harri", addr: "123 Fake Street", phone: "8675309"}
|
||||||
|
fmt.Println(p3)
|
||||||
|
|
||||||
|
// New func, returns a pointer to the
|
||||||
|
p4 := new(Person)
|
||||||
|
fmt.Println(*p4) //derefence
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
myMap := make(map[string]string)
|
||||||
|
|
||||||
|
inputReader := bufio.NewReader(os.Stdin)
|
||||||
|
fmt.Println("Please enter your name:")
|
||||||
|
name, _ := inputReader.ReadString('\n')
|
||||||
|
|
||||||
|
myMap["name"] = name[:len(name)-1]
|
||||||
|
fmt.Println("Please enter your address:")
|
||||||
|
address, _ := inputReader.ReadString('\n')
|
||||||
|
|
||||||
|
myMap["address"] = address[:len(address)-1]
|
||||||
|
|
||||||
|
barr, _ := json.Marshal(myMap)
|
||||||
|
fmt.Println(string(barr))
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
Drew Bednar
|
||||||
|
Margot Robbie
|
||||||
|
Sean Carey
|
||||||
|
invalid
|
||||||
|
Margot Rae Bednar
|
@ -0,0 +1,100 @@
|
|||||||
|
// Redo this one!
|
||||||
|
|
||||||
|
// // Please enter path to name file:
|
||||||
|
// /home/toor/names.txt
|
||||||
|
// Printing scanned names:
|
||||||
|
// Drew Bednar
|
||||||
|
// Margot Robbie
|
||||||
|
// Sean Carey
|
||||||
|
|
||||||
|
// Exiting...
|
||||||
|
// toor@toor-jammy-jellifish:~/tmp$ cat read.go
|
||||||
|
// package main
|
||||||
|
// import (
|
||||||
|
// "fmt"
|
||||||
|
// "os"
|
||||||
|
// "bufio"
|
||||||
|
// "strings"
|
||||||
|
// )
|
||||||
|
// type Person struct {
|
||||||
|
// fname string
|
||||||
|
// lname string
|
||||||
|
// }
|
||||||
|
// func main (){
|
||||||
|
// fmt.Println("Please enter path to name file:")
|
||||||
|
// reader := bufio.NewReader(os.Stdin)
|
||||||
|
// inputBytes, _, _ := reader.ReadLine()
|
||||||
|
// path := string(inputBytes)
|
||||||
|
// file, err := os.Open(path)
|
||||||
|
// if err != nil {
|
||||||
|
// fmt.Println("Error: ", err)
|
||||||
|
// } else {
|
||||||
|
// slice := make([]Person, 0)
|
||||||
|
// scanner := bufio.NewScanner(file)
|
||||||
|
// for scanner.Scan() {
|
||||||
|
// line := scanner.Text()
|
||||||
|
// sarr := strings.Split(line, " ")
|
||||||
|
// slice = append(slice, Person{sarr[0], sarr[1]})
|
||||||
|
// }
|
||||||
|
// if err := scanner.Err(); err != nil {
|
||||||
|
// fmt.Println("Error: ", err)
|
||||||
|
// }
|
||||||
|
// file.Close()
|
||||||
|
// fmt.Println("Printing scanned names:")
|
||||||
|
// for _, p := range slice {
|
||||||
|
// fmt.Println(p.fname, " ", p.lname)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// fmt.Println("\nExiting...")
|
||||||
|
// }
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Person struct {
|
||||||
|
Fname string
|
||||||
|
Lname string
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseName(name string) Person {
|
||||||
|
names := strings.Split(name, " ")
|
||||||
|
p := Person{Fname: names[0], Lname: strings.TrimSpace(names[1])}
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var filePath string
|
||||||
|
var people []Person
|
||||||
|
|
||||||
|
fmt.Println("Please provide the path to a text file of first and last names:")
|
||||||
|
_, err := fmt.Scan(&filePath)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "Error. Something when wrong with the path you provided.")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
data, err := os.ReadFile(filePath)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "Error. Something when wrong with the path you provided.")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
file_lines := strings.Split(string(data), "\n")
|
||||||
|
for _, v := range file_lines {
|
||||||
|
if len(strings.Split(v, " ")) == 2 {
|
||||||
|
people = append(people, parseName(v))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, v := range people {
|
||||||
|
fmt.Printf("First Name: %s, Last Name: %s\n", v.Fname, v.Lname)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Person struct {
|
||||||
|
Name string
|
||||||
|
Addr string
|
||||||
|
Phone string
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
p1 := Person{Name: "Drew", Addr: "123 Fake Street", Phone: "8675309"}
|
||||||
|
byte_array, err := json.Marshal(p1) // returns json as a slice of bytes []bytes
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Damn it")
|
||||||
|
}
|
||||||
|
fmt.Println(byte_array)
|
||||||
|
fmt.Println(p1)
|
||||||
|
|
||||||
|
// Unmarshaling going from a json byte array to a to a go object
|
||||||
|
p2 := Person{}
|
||||||
|
err = json.Unmarshal(byte_array, &p2) // will throw an error if the byte array doesn't fit the struct
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Damn it")
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(p2)
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
resp, _ := http.Get("https://androiddrew.github.io/")
|
||||||
|
resp_code := resp.StatusCode
|
||||||
|
fmt.Println("Status code:", resp_code)
|
||||||
|
body, _ := io.ReadAll(resp.Body)
|
||||||
|
resp.Body.Close() // prevent memory leak
|
||||||
|
|
||||||
|
fmt.Println(string(body))
|
||||||
|
// resp_body, _ := resp_ptr.Body.Read(resp_ptr.)
|
||||||
|
// fmt.Println("Body:", string(resp_body))
|
||||||
|
// resp_ptr.Body.Close()
|
||||||
|
}
|
Loading…
Reference in New Issue