-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
595 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io" | ||
"os" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
func solve(reader io.Reader, writer io.Writer) { | ||
|
||
// input | ||
|
||
n, _ := strconv.Atoi(readLine(reader)) | ||
L := make([]int, n) | ||
for i, low := range strings.Split(readLine(reader), " ") { | ||
L[i], _ = strconv.Atoi(low) | ||
} | ||
H := make([]int, n) | ||
for i, high := range strings.Split(readLine(reader), " ") { | ||
H[i], _ = strconv.Atoi(high) | ||
} | ||
C := make([]int, n) | ||
for i, close := range strings.Split(readLine(reader), " ") { | ||
C[i], _ = strconv.Atoi(close) | ||
} | ||
|
||
// process | ||
|
||
var gapUp, gapDown int | ||
|
||
for i := 1; i < n; i++ { | ||
if C[i-1] < L[i] { | ||
gapUp++ | ||
} | ||
if C[i-1] > H[i] { | ||
gapDown++ | ||
} | ||
} | ||
|
||
// output | ||
|
||
fmt.Fprintf(writer, "%d %d\n", gapUp, gapDown) | ||
} | ||
|
||
func main() { | ||
|
||
reader := bufio.NewReaderSize(os.Stdin, 1024*1024) | ||
|
||
stdout, err := os.Create(os.Getenv("OUTPUT_PATH")) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer stdout.Close() | ||
|
||
writer := bufio.NewWriterSize(stdout, 1024*1024) | ||
|
||
solve(reader, writer) | ||
|
||
writer.Flush() | ||
} | ||
|
||
func readLine(reader io.Reader) string { | ||
r := bufio.NewReader(reader) | ||
str, _, err := r.ReadLine() | ||
if err == io.EOF { | ||
return "" | ||
} | ||
|
||
return strings.TrimRight(string(str), "\r\n") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func Test_solve(t *testing.T) { | ||
|
||
// your test-case goes here (split by \n) | ||
in := "5\n5 3 7 7 2\n10 9 20 15 10\n6 8 16 11 6" | ||
out := "0 2" | ||
|
||
input := bufio.NewReader(strings.NewReader(in)) | ||
output := new(bytes.Buffer) | ||
|
||
solve(input, output) | ||
|
||
if out != strings.Trim(output.String(), "\r\n") { | ||
t.Fatal("\nexpected:\n", out, "\n\ngot:\n", output) | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
contests/moodys-analytics-2018/2-meeting-profit-target/main.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io" | ||
"os" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
func solve(reader io.Reader, writer io.Writer) { | ||
|
||
q, _ := strconv.ParseInt(readLine(reader), 10, 64) | ||
|
||
for i := int64(0); i < q; i++ { | ||
|
||
n, _ := strconv.ParseInt(readLine(reader), 10, 64) | ||
|
||
var carryover int64 | ||
|
||
for i := int64(0); i < n; i++ { | ||
|
||
ab := strings.Split(readLine(reader), " ") | ||
actual, _ := strconv.ParseInt(ab[0], 10, 64) | ||
estimated, _ := strconv.ParseInt(ab[1], 10, 64) | ||
estimated += carryover | ||
|
||
carryover = 0 | ||
if estimated > actual { | ||
carryover = estimated - actual | ||
} | ||
} | ||
|
||
if carryover > 0 { | ||
fmt.Fprintln(writer, "1") | ||
} else { | ||
fmt.Fprintln(writer, "0") | ||
} | ||
} | ||
} | ||
|
||
func main() { | ||
|
||
reader := bufio.NewReaderSize(os.Stdin, 1024*1024) | ||
|
||
stdout, err := os.Create(os.Getenv("OUTPUT_PATH")) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer stdout.Close() | ||
|
||
writer := bufio.NewWriterSize(stdout, 1024*1024) | ||
|
||
solve(reader, writer) | ||
|
||
writer.Flush() | ||
} | ||
|
||
func readLine(reader io.Reader) string { | ||
r := bufio.NewReader(reader) | ||
str, _, err := r.ReadLine() | ||
if err == io.EOF { | ||
return "" | ||
} | ||
|
||
return strings.TrimRight(string(str), "\r\n") | ||
} |
24 changes: 24 additions & 0 deletions
24
contests/moodys-analytics-2018/2-meeting-profit-target/main_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func Test_solve(t *testing.T) { | ||
|
||
// your test-case goes here (split by \n) | ||
in := "2\n1\n10 20\n2\n15 20\n20 15" | ||
out := "1\n0" | ||
|
||
input := bufio.NewReader(strings.NewReader(in)) | ||
output := new(bytes.Buffer) | ||
|
||
solve(input, output) | ||
|
||
if out != strings.Trim(output.String(), "\r\n") { | ||
t.Fatal("\nexpected:\n", out, "\n\ngot:\n", output) | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
contests/moodys-analytics-2018/3-mean-normalization/main.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io" | ||
"math" | ||
"os" | ||
"sort" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
func solve(reader io.Reader, writer io.Writer) { | ||
|
||
n, _ := strconv.Atoi(readLine(reader)) | ||
stocks := make([][]int, n) | ||
means := make([]float64, n) | ||
|
||
for i := 0; i < n; i++ { | ||
|
||
m, _ := strconv.Atoi(readLine(reader)) | ||
stocks[i] = make([]int, m) | ||
|
||
var sum int | ||
for j, p := range strings.Split(readLine(reader), " ") { | ||
stocks[i][j], _ = strconv.Atoi(p) | ||
sum += stocks[i][j] | ||
} | ||
|
||
means[i] = float64(sum) / float64(len(stocks[i])) | ||
} | ||
|
||
means = unique(means) | ||
|
||
runningTimes := make([]float64, len(means)) | ||
for idx, x := range means { | ||
for i := range stocks { | ||
for _, p := range stocks[i] { | ||
runningTimes[idx] += math.Abs(float64(p) - x) | ||
} | ||
} | ||
} | ||
|
||
sort.Float64s(runningTimes) | ||
|
||
fmt.Fprintf(writer, "%.12f", runningTimes[0]) | ||
} | ||
|
||
func unique(input []float64) []float64 { | ||
u := make([]float64, 0, len(input)) | ||
m := make(map[float64]bool) | ||
|
||
for _, val := range input { | ||
if _, ok := m[val]; !ok { | ||
m[val] = true | ||
u = append(u, val) | ||
} | ||
} | ||
|
||
return u | ||
} | ||
|
||
func main() { | ||
|
||
reader := bufio.NewReaderSize(os.Stdin, 1024*1024) | ||
|
||
stdout, err := os.Create(os.Getenv("OUTPUT_PATH")) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer stdout.Close() | ||
|
||
writer := bufio.NewWriterSize(stdout, 1024*1024) | ||
|
||
solve(reader, writer) | ||
|
||
writer.Flush() | ||
} | ||
|
||
func readLine(reader io.Reader) string { | ||
r := bufio.NewReader(reader) | ||
str, _, err := r.ReadLine() | ||
if err == io.EOF { | ||
return "" | ||
} | ||
|
||
return strings.TrimRight(string(str), "\r\n") | ||
} |
24 changes: 24 additions & 0 deletions
24
contests/moodys-analytics-2018/3-mean-normalization/main_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func Test_solve(t *testing.T) { | ||
|
||
// your test-case goes here (split by \n) | ||
in := "2\n3\n1 3 4\n2\n11 10" | ||
out := "19.000000000000" | ||
|
||
input := bufio.NewReader(strings.NewReader(in)) | ||
output := new(bytes.Buffer) | ||
|
||
solve(input, output) | ||
|
||
if out != strings.Trim(output.String(), "\r\n") { | ||
t.Fatal("\nexpected:\n", out, "\n\ngot:\n", output) | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
contests/moodys-analytics-2018/4-short-trade-transaction/main.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io" | ||
"math" | ||
"os" | ||
"strconv" | ||
"strings" | ||
"sync" | ||
) | ||
|
||
func solve(reader io.Reader, writer io.Writer) { | ||
|
||
q, _ := strconv.Atoi(readLine(reader)) | ||
|
||
for i := 0; i < q; i++ { | ||
|
||
am := strings.Split(readLine(reader), " ") | ||
a, _ := strconv.Atoi(am[0]) | ||
m, _ := strconv.Atoi(am[1]) | ||
|
||
var result int | ||
var wg sync.WaitGroup | ||
wg.Add(1) | ||
visit(&wg, m, a, math.MaxInt64, &result) | ||
wg.Wait() | ||
fmt.Fprintln(writer, result) | ||
} | ||
} | ||
|
||
func visit(wg *sync.WaitGroup, participant, shares, took int, result *int) { | ||
|
||
defer wg.Done() | ||
|
||
if participant < 0 { | ||
return | ||
} | ||
|
||
if shares == 0 { | ||
*result++ | ||
return | ||
} | ||
|
||
x := int(math.Min(float64(shares), float64(took-1))) | ||
|
||
for i := 1; i <= x; i++ { | ||
wg.Add(1) | ||
go visit(wg, participant-1, shares-i, i, result) | ||
} | ||
} | ||
|
||
func main() { | ||
|
||
reader := bufio.NewReaderSize(os.Stdin, 1024*1024) | ||
|
||
stdout, err := os.Create(os.Getenv("OUTPUT_PATH")) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer stdout.Close() | ||
|
||
writer := bufio.NewWriterSize(stdout, 1024*1024) | ||
|
||
solve(reader, writer) | ||
|
||
writer.Flush() | ||
} | ||
|
||
func readLine(reader io.Reader) string { | ||
r := bufio.NewReader(reader) | ||
str, _, err := r.ReadLine() | ||
if err == io.EOF { | ||
return "" | ||
} | ||
|
||
return strings.TrimRight(string(str), "\r\n") | ||
} |
Oops, something went wrong.