Initial Commit

This commit is contained in:
Brian Buller 2018-09-13 10:18:53 -05:00
commit 335a821ed9
3 changed files with 98 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
p1.1/p1.1
p1.2/p1.2

43
p1.1/main.go Normal file
View File

@ -0,0 +1,43 @@
package main
import (
"fmt"
"log"
"os"
"strconv"
)
func main() {
if len(os.Args) < 4 {
fmt.Println("Usage: p1.1 x y z")
os.Exit(1)
}
x, y, z := atoi(os.Args[1]), atoi(os.Args[2]), atoi(os.Args[3])
i, v1, v2 := 1, x, x-y
fmt.Println(z, v1, v2)
for i = 1; ; i = i + 2 {
if z == v1 {
done(i)
} else if z == v2 {
done(i + 1)
} else if z < v1 && z < v2 {
done(-1)
}
v1 = v1 + x - y
v2 = v2 + x - y
}
}
func done(v int) {
fmt.Println(v)
os.Exit(0)
}
func atoi(v string) int {
i, err := strconv.Atoi(v)
if err != nil {
log.Fatal("Invalid integer given: " + v)
}
return i
}

53
p1.2/main.go Normal file
View File

@ -0,0 +1,53 @@
package main
import (
"fmt"
"log"
"os"
"strconv"
)
func main() {
if len(os.Args) < 3 {
log.Fatal("Usage: p1.2 length list...")
}
length := atoi(os.Args[1])
var list []int
var top, topIdx, numTop int
for idx, v := range os.Args[2:] {
wrk := atoi(v)
list = append(list, wrk)
if wrk == top {
numTop++
if numTop <= 3 {
topIdx = idx
}
} else if wrk > top {
top = wrk
numTop = 1
topIdx = idx
}
if len(list) == length {
break
}
}
for idx, v := range list {
if idx != topIdx {
fmt.Print(v, " ")
}
}
fmt.Print("\n")
}
func done(v int) {
fmt.Println(v)
os.Exit(0)
}
func atoi(v string) int {
i, err := strconv.Atoi(v)
if err != nil {
log.Fatal("Invalid integer given: " + v)
}
return i
}