From 662bb9eca6360a0b973474b155dc4c1f256fff22 Mon Sep 17 00:00:00 2001 From: ealvar3z <55966724+ealvar3z@users.noreply.github.com> Date: Tue, 10 Jan 2023 00:54:56 -0800 Subject: Week 199 Go & Python solutions --- challenge-199/ealvar3z/go/ch-1.go | 30 ++++++++++++++++++++++++++++++ challenge-199/ealvar3z/python/ch-2.py | 21 +++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 challenge-199/ealvar3z/go/ch-1.go create mode 100644 challenge-199/ealvar3z/python/ch-2.py diff --git a/challenge-199/ealvar3z/go/ch-1.go b/challenge-199/ealvar3z/go/ch-1.go new file mode 100644 index 0000000000..b4de997100 --- /dev/null +++ b/challenge-199/ealvar3z/go/ch-1.go @@ -0,0 +1,30 @@ +package main + +import "fmt" + +func main() { + tests := [][]int{ + {1, 2, 3, 1, 1, 3}, + {1, 2, 3}, + {1, 1, 1, 1}, + } + for i := range tests { + fmt.Println("Output: ", solve(tests[i])) + } +} + +// Given a list of ints: +// we're asked to find the total count of "Good Pairs" +// A pair(i,j) is good iff list[i] == list[j] && i < j + +// we'll keep a counter of the times we have the value +// in our counter map by looping over each value in the array +func solve(list []int) int { + total := 0 + counter := make(map[int]int) + for _, j := range list { + total += counter[j] + counter[j]++ + } + return total +} diff --git a/challenge-199/ealvar3z/python/ch-2.py b/challenge-199/ealvar3z/python/ch-2.py new file mode 100644 index 0000000000..1ed0a2c97e --- /dev/null +++ b/challenge-199/ealvar3z/python/ch-2.py @@ -0,0 +1,21 @@ +from itertools import combinations + + +def solve(arr, a, b, c): + total = 0 + for i,j,k in combinations(arr,3): + if ((abs(i-j) <= a) and + (abs(j-k) <= b) and + (abs(i-k) <= c)): + total += 1 + return total + + +if __name__ == '__main__': + test1 = [3, 0, 1, 1, 9, 7] + test2 = [1, 1, 2, 2, 3] + a, b, c = [7, 2, 3] + d, e, f = [0, 0, 1] + + print("Output: ", solve(test1, a, b, c)) + print("Output: ", solve(test2, d, e, f)) -- cgit