From 04bbac85592156fbcf1ab5b6fb230dd97e29b996 Mon Sep 17 00:00:00 2001 From: xuu Date: Sat, 26 Oct 2024 12:03:06 -0600 Subject: [PATCH] chore: add hackerrank solutions --- aoc2023/day21/example.txt | 11 ++ aoc2023/day21/input.txt | 131 ++++++++++++++++++ aoc2023/day21/main.go | 30 ++++ aoc2023/day21/main_test.go | 41 ++++++ .../main.go | 84 +++++++++++ hacker-rank/counting-sort/main.go | 80 +++++++++++ hacker-rank/diagonal-difference/main.go | 84 +++++++++++ hacker-rank/flipping-bits/main.go | 64 +++++++++ {hacker01 => hacker-rank}/hacker.go | 0 hacker-rank/lonely-integer/main.go | 77 ++++++++++ hacker-rank/mini-max-sum/main.go | 64 +++++++++ hacker-rank/no-prefix-set/main.go | 94 +++++++++++++ hacker-rank/pangrams/main.go | 69 +++++++++ hacker-rank/plus-minus/main.go | 69 +++++++++ hacker-rank/sparse-arrays/main.go | 92 ++++++++++++ hacker-rank/time-conversion/main.go | 60 ++++++++ .../tree-height-of-a-binary-tree/main.go | 67 +++++++++ hacker-rank/tree-preorder-traversal/main.go | 52 +++++++ 18 files changed, 1169 insertions(+) create mode 100644 aoc2023/day21/example.txt create mode 100644 aoc2023/day21/input.txt create mode 100644 aoc2023/day21/main.go create mode 100644 aoc2023/day21/main_test.go create mode 100644 hacker-rank/binary-search-tree-lowest-common-ancestor/main.go create mode 100644 hacker-rank/counting-sort/main.go create mode 100644 hacker-rank/diagonal-difference/main.go create mode 100644 hacker-rank/flipping-bits/main.go rename {hacker01 => hacker-rank}/hacker.go (100%) create mode 100644 hacker-rank/lonely-integer/main.go create mode 100644 hacker-rank/mini-max-sum/main.go create mode 100644 hacker-rank/no-prefix-set/main.go create mode 100644 hacker-rank/pangrams/main.go create mode 100644 hacker-rank/plus-minus/main.go create mode 100644 hacker-rank/sparse-arrays/main.go create mode 100644 hacker-rank/time-conversion/main.go create mode 100644 hacker-rank/tree-height-of-a-binary-tree/main.go create mode 100644 hacker-rank/tree-preorder-traversal/main.go diff --git a/aoc2023/day21/example.txt b/aoc2023/day21/example.txt new file mode 100644 index 0000000..c66328c --- /dev/null +++ b/aoc2023/day21/example.txt @@ -0,0 +1,11 @@ +........... +.....###.#. +.###.##..#. +..#.#...#.. +....#.#.... +.##..S####. +.##..#...#. +.......##.. +.##.#.####. +.##..##.##. +........... \ No newline at end of file diff --git a/aoc2023/day21/input.txt b/aoc2023/day21/input.txt new file mode 100644 index 0000000..473d2c0 --- /dev/null +++ b/aoc2023/day21/input.txt @@ -0,0 +1,131 @@ +................................................................................................................................... +.#.......#..#...#.......#......#....#..#...#...#....#.#..........................................#...............#..........#...... +.............##..................#......#.#....#.......................##..#........................#......#..........#............ +......#..........................#....#.....#.............#................##....#.#.............#.........#.##...........#........ +.#................#............................#...........................#..##.....#..................#..##..#.#.........##...... +.....#.............#.....##.............##..........#...........#.............#.....#...........#.#.......#....#......#..........#. +........#....................#....#.#.#......##........#...........#...........#..........#...#..........#......................#.. +.........#.#...#.......#........#........##...#...#..##........................................##...#.#........................#... +......#.....#..#..#.#..#..............#...#..#.....#...........#.....#........#.#...........#...........#.#...............#.....#.. +...........#.......####..#.#..............##................#.#.#.....................##.....................#...#..............#.. +......###...#......#................##...#...#....#...........#...#..............#.........#..#..................................#. +...............#..##..........#...........................##......#.##.........................#..#.#...........#.#...........#.... +...##........#...#...#...##...##...........#..#...................#...................#.#..#.........#.......#..#...........##...#. +................#.....#..##......#.....#.....##............#..#..........##...........................#.#.......##.....#.....#..... +.#..#.......#.#...#..##.....#............................#......#...#....#................#.#..#.....#.......##................#... +...............#.....#........................#..........#.....#..............................................#..................#. +.....#......#..........................................#....................................#..#.......#.#..#......#.#....#..##.... +............##............#.......###.......................#..#....#......................................#..#.........#.......... +..#...............#.....#...#...#......................#...#.#..............................#.#..#.#.....#.#.#..#....#.....#...##.. +.#.............................##.................#.......#.......##....#....................#.....#.#........#.....#........#..... +.#....##...................##.....#.#..............#.#....#....#..#....#......#..........#.....##............#....#.............##. +.........#....#.....#..#.........#....#.#..........#...#...#.......#....#..##.......................#.#..#........#..#....#..#.#... +..#..................#.......#..#.#..............##.......#........#..............#.........#..#....#......#..#..........#......... +........#.....#......#.....#......#...#.......#....#.......#.#..#................#....................##.............#.......#..... +..#.................#..............#...........................................#.....#..................#...#.#..................#. +.......#......#.....#...........................#.....#..................#.....................#.....#.#......#........#.#.......#. +..#.#.#...................#...#..#................#....#.......#......................#...............#.........##...#.#.....###... +.....#...#....##....#........####...........#.................#...#...........................................#..............##.... +..##.#..#.#..........#.#.#..#................................#..#.............#..#......#...........................#.##........... +..............#.............................##........#..##........................#.............................#........#........ +........#.........#........#..#........#..#........#...#.#........#...........#.........................#.#..........#...#......#.. +.........##.....#........#.....................#..#.............#.....#........#....................#....#.#.#......#.....#........ +......................#...................####..#.........#....#.........#...........#..................#..#.................#..... +..#.........#...#.....##................##.##....#.....#....#..............................#..#.............#..#................... +..........##......#..#..............#.#....#......#................#.....#....#..............#..................#.................. +.#.#..####...#....................#.#.#.....#...#...................#....#.#.....#.#.......#.#...........#......#.#................ +.......#.........#..#.#..............###.......#....#.........................#.......#.###...#............#....#.#..#....###.#.... +........#....#...#......#.................#..........................#.........................#...........##.............#.#...... +.#..............#.....................#......#.............##............#..#..........#...................#...#....#.............. +......#..#..#.#...............#........###.......#.#...........#...........#.#.........#..#.....................#......#..#.#...... +.....#...............#.........#....#..........#.......#.......#......#...#..##.....#.....#........#.#.......#.......#..#...#...... +..#............#.....................#...#.......#....###...........#.....#.#.................#................#.........#..##.#... +..............................#.....#..................................#.....##...............#.......#.........#.....#....##...... +..#...##...#...#..............#.................##..#......#.................#....#......#...#...#......#.......................... +.........#....#..............#........#..#...............#....#...#.....#..#......#.........#.......#.#..#........#...........#.... +........#..#....................#.........#...##.#.#..............#......#.#..........##...##....#......................#.......... +.##..#....#....#.......##......#............#..#......#.....##..................#####..#...#......#.#...#..........#.#.....#..#.... +..#...........#....................#.......#......#.........#...............#......#........##........#.................#.....#.#.. +..#....#.#...............#.#.........................#.............####..............................##................###......... +.....#.......................................#....#...##...#.......#.......#.....#.##..##.................#.....................##. +.........#...........#..#..##.#..........#..#.........#..#.....#...................................##....#...............#..#.#.... +.....#..#...........##............#........#..................#............#..........#..........#..#...........#.................. +............................#.........#...............#..................#.#....#.#..#........#.....#...........#...........#...... +................#...##.#....#..##..........#.#........##.#..#..#.....................#....#.................#.#...............#.... +.#.....#..............#.#..........#..#........#...............#..#.#..##..#.#.#.........................#.................#....... +....#..............#................#.....#..#.......#..#.#.........#..........#..........#.....#..........##.....#............#... +..............##...........#.......#......#..................#.......#.....#.#...#...............#.#........#......#............#.. +................#.....#....#........#....#.......#.#.##.....#...#..#.##...#................#.#.....#....#.............#............ +...........#.#.##..####.....#....#.#...#.#...........#...#.......................#.#....#.....#..#................###.............. +...................#....#..........#.#..##.....#..#..#......###............#..#...........#....#......#............................ +...........................................#.......#..............#.##.......##.......#...#.....#.#..#.....#..###.................. +............###........#.......#.#....#.....#....#..#........#.........##....#.....#..........#......#............#..#.#........... +............#..........#................##.#..............#.....#................#....................................#..#......... +...........#.....#.....................#...#.....#.......#..........#....#.....###.....##.#.##.#.....#......#......#.......#....... +.....................#..#.....#...##.....#..#...........#....#....##............##.................##............##................ +.................................................................S................................................................. +........#.#........#..........#.....#......#........#.#............#.#.........##...#............#...#.....#...........#.....#..... +..................##.......#......#..#..................#..##......#.........#...#.........#............#....#...........##........ +..................#...........#.##.......#..............#....................#.#....#...##...#.....#.......#..#.....##............. +...........#....#....##...#................................#.........#.................#....#.......................#...#.......... +.............#.#.#.....#.#...#.......#.#...........#....#.#.........#........##......#.......#.......##...#..........#.##.......... +...........##.#....#....#..#.....#...............#......................#...........#.....#...........#.#.......#.................. +.#.......................#.#...##.#....#.#...........#.#....##.##......#........................##....#.#....#..................... +...#..................#.#...........##....###.................#....................##......#.....#............#.##....#.......##... +.#..................####...#.#..#.##.#...####...#......#....................#..........................#....#...................... +.....#..........#.....#.#...............#.........##...#......#.#.....#...........#.....#............##......#.....#............... +......#........#....#..#............................###.#...##........#........#.................#..#........#...............##.... +......................#.........................##....#...#..............#..#............#.#....#......##.#......#.........##...... +..................#....#...#.#.......#..............#..#..#...#.......#...#.....#.....#...............#.#.#...............#........ +...#...##......................##..##...........#......#.............#............#...........##.......#....#...................... +.............................##...........##...#...#.....#....#.......#.#..#.....##...............#..#.#......#..............##.... +...#..#.#...#.......#..................#............#....#.#.##.#.........#......#........#..#..........#....##...........#....#... +.....#.............................#......#.#.....#.##.#.................#.........#..#..#.#.#........................#.#.......#.. +....#........#........#....#.#.#.....#.##............###.#..........#...##......#........##...#.........#.............#........#... +..#..............................#.............................#......#...................#...#......#.............#............... +.......#...#...............#.................##....................#....##.......#....##....##...#..................#.............. +.........................#...#..#.....##.............#...##.......#.....#...#..............#.....#.#.#..#........................#. +......#.......##.#.............#....................#...#...........#...#..##....#.....#............#...........#........#......... +...#....#...#......#........................#.....#.........#......#........#.....#.#...........#.#..............#......#....#..... +........#..........#...................#...........##.#..#...#................#....##.............#................#....#.......... +........#.......#....#.............#..........#...................#.#....#.#........#..#..............................#..........#. +............#........#....................#.#.........#......#.............#................#...#.............#....#............... +..#..............................#..#....##.....#......................##...#..#.........#......#......................#...#...#.#. +.##....#.............#...........##..................#...#..............#.......#................................#.........#....... +.....#......#.#.....##...........#.....#.......#..........##..#.............#.#.........#.#.#.............##........#......#..#.... +.......#.......#.#.#..#......................#....#............##....#...........#.........................#......#................ +.........##.........#..............#.#.#....#...#.......#.................................#..##..................##.###.##...#..... +......................#...#.#.......#..#.#..........#........#.......#.................#..................................#........ +....##........#.........#.........................#................#......#...#.....#..................................#..#.#...... +...##..............#..................#...#......#........###..#.......#..........#..............................#......#.......... +.#.#.#....#..........#......#.#.........#.....#...............................#.....#.#............#....#........#.............#... +........#.........#.........#......................#..#......##.....#..#..........................#......##....#......##.##.....#.. +.##...#.......#.#....#..#.#...................#.........#....#...........#.....#.........#............##......#............#....... +.##..#.........#..........#.#.................................#.#.#...............................#............#......##...#.#..... +......#........#..#...#............#..............#............#......##.....#.................................#......#.#.....#.... +..........#........#...........#....................................#.....#...............................#............##....#.##.. +........#.............#..#......##..................#.............#..#...#.#.....#...#...............#........................#.... +....#...............#..........#....#...........................#....##.##...................#.#.......#...#.##....#........##..#.. +......#.....#..#..........###...#.................####.###..........#...........#..........#......#............................#... +.#...#......#........#..#............##...............#.##......#...#.....#....................#.........#............#...#........ +................##...................#.##..............#...#..#.......#.......###........#..#..#........#...##..................... +..............#....#..#..#.............##..............#................................#.....#..#....###.##..............#........ +..#..............#........................................#..........#.....#..................#............#..#.................... +.........#...#.....#...#...##...........#.###............##...#........#......................##......#....#.......#............... +....#....#.....#.......#......................................#.#.#....#..#..#..........#...................#........#...#......... +..#..#..................#..............#...#..#.........#......#....#.#................##..........#..#................#........... +...#...................#...........#....#...#.............##...#.......#.#..........#..#...............#....#....#.............#... +.......................##.#.#..##........#....#.........#.#.#.....##..............#.......#.......##.#..........#.#................ +.....#..#..................#..##....#.......#.............#...#.....#.............#.............#.#...............#.....##......##. +...#.#.....#..........#.#............#..#..........................##.............#....#.......#.#...#...#....................#.#.. +............##.#............#....#....#....#..##...#...............................#...#...#.........#.......................##.##. +....#.#...............#...#.......#............#..#...........................#..............#..........#....#...#...#..#...#....#. +.......#..#..#.#..#......#..........................#...............#.........#...........#................#..#...#................ +...#..#...#.....#.#.......#....................................................#........#..........#.#...#......#..........#....... +..#...#.......#..#..............#.#...#.....#...#....#............#..........##...#.........##.............#....................... +...#.............#...#....#...#.....#.#.#..............#..........#........#........##...........#..#.#......................#..... +.#.......#.................#...............#..........#.....................#.....#............#......##....#.....#...#....###..... +...##...........#..........#.#........#............#..#.#........................#.....##............#...##.#.......#.##....#...... +...........#........#...........#.......#......#...#.....#............................#........#..#................#...#....##..... +...##.##.........#.####..#............#..#.....#.......................#......#..........#.#..#.#....##........#....#.....#..#..##. +................................................................................................................................... diff --git a/aoc2023/day21/main.go b/aoc2023/day21/main.go new file mode 100644 index 0000000..c0596d8 --- /dev/null +++ b/aoc2023/day21/main.go @@ -0,0 +1,30 @@ +package main + +import ( + "bufio" + _ "embed" + "fmt" + + aoc "go.sour.is/advent-of-code" +) + +// var log = aoc.Log + +func main() { aoc.MustResult(aoc.Runner(run)) } + +type result struct { + valuePT1 int + valuePT2 int +} + +func (r result) String() string { return fmt.Sprintf("%#v", r) } + +func run(scan *bufio.Scanner) (*result, error) { + + for scan.Scan() { + _ = scan.Text() + + } + + return &result{}, nil +} diff --git a/aoc2023/day21/main_test.go b/aoc2023/day21/main_test.go new file mode 100644 index 0000000..b55d509 --- /dev/null +++ b/aoc2023/day21/main_test.go @@ -0,0 +1,41 @@ +package main + +import ( + "bufio" + "bytes" + "testing" + + _ "embed" + + "github.com/matryer/is" +) + +//go:embed example.txt +var example []byte + +//go:embed input.txt +var input []byte + +func TestExample(t *testing.T) { + is := is.New(t) + scan := bufio.NewScanner(bytes.NewReader(example)) + + result, err := run(scan) + is.NoErr(err) + + t.Log(result) + is.Equal(result.valuePT1, 0) + is.Equal(result.valuePT2, 0) +} + +func TestSolution(t *testing.T) { + is := is.New(t) + scan := bufio.NewScanner(bytes.NewReader(input)) + + result, err := run(scan) + is.NoErr(err) + + t.Log(result) + is.Equal(result.valuePT1, 0) + is.Equal(result.valuePT2, 0) +} diff --git a/hacker-rank/binary-search-tree-lowest-common-ancestor/main.go b/hacker-rank/binary-search-tree-lowest-common-ancestor/main.go new file mode 100644 index 0000000..e7b73d7 --- /dev/null +++ b/hacker-rank/binary-search-tree-lowest-common-ancestor/main.go @@ -0,0 +1,84 @@ +package main +import ( + "fmt" + "os" + "bufio" + "strings" + "strconv" +) +func main() { + //Enter your code here. Read input from STDIN. Print output to STDOUT + scanner := bufio.NewScanner(os.Stdin) + + var nodes int + var tree *node + var target [2]int + + for scanner.Scan() { + text := scanner.Text() + if nodes == 0 { + nodes, _ = strconv.Atoi(text) + continue + } + + if tree == nil { + for _, s := range strings.Fields(text) { + v, _ := strconv.Atoi(s) + tree = insertNode(tree, &node{value:v}) + } + continue + } + + for i, s := range strings.Fields(text) { + v, _ := strconv.Atoi(s) + target[i] = v + } + } + if target[0]>target[1] { + target[0], target[1] = target[1], target[0] + } + fmt.Fprintln(os.Stderr, tree) + if n := lsa(tree, target); n != nil { + fmt.Println(n.value) + return + } + fmt.Println("nil") +} + +type node struct{ + value int + left *node + right *node +} +func insertNode(root, n *node) *node { + if root == nil { + return n + } + if root.value > n.value { + root.left = insertNode(root.left, n) + return root + } + root.right = insertNode(root.right, n) + return root +} + +func (n *node) String() string { + if n == nil { return "nil" } + return fmt.Sprintf("%v (%v %v)", n.value, n.left, n.right) +} + +func lsa(tree *node, target [2]int) *node { + if tree == nil { + return nil + } + + if target[0]>tree.value { + return lsa(tree.right, target) + } + + if target[1] v { + min = v + } + if max < v { + max = v + } + sum += int(v) + } + fmt.Print(sum-int(max), sum-int(min)) +} + +func main() { + reader := bufio.NewReaderSize(os.Stdin, 16 * 1024 * 1024) + + arrTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ") + + var arr []int32 + + for i := 0; i < 5; i++ { + arrItemTemp, err := strconv.ParseInt(arrTemp[i], 10, 64) + checkError(err) + arrItem := int32(arrItemTemp) + arr = append(arr, arrItem) + } + + miniMaxSum(arr) +} + +func readLine(reader *bufio.Reader) string { + str, _, err := reader.ReadLine() + if err == io.EOF { + return "" + } + + return strings.TrimRight(string(str), "\r\n") +} + +func checkError(err error) { + if err != nil { + panic(err) + } +} diff --git a/hacker-rank/no-prefix-set/main.go b/hacker-rank/no-prefix-set/main.go new file mode 100644 index 0000000..7ce851c --- /dev/null +++ b/hacker-rank/no-prefix-set/main.go @@ -0,0 +1,94 @@ +package main + +import ( + "bufio" + "fmt" + "io" + "os" + "strconv" + "strings" +) + +/* + * Complete the 'noPrefix' function below. + * + * The function accepts STRING_ARRAY words as parameter. + */ + +type trie struct { + wordEnd bool + child [10]*trie +} + +func (m *trie) add(word string) { + curr := m + for _, r := range word { + index := r - 'a' + if curr.child[index] == nil { + curr.child[index] = &trie{} + } + curr = curr.child[index] + } + curr.wordEnd = true +} + +func (m *trie) find(word string) bool { + curr := m + for _, r := range word { + index := r - 'a' + if curr.child[index] != nil { + curr = curr.child[index] + + if curr.wordEnd { + return true + } + } else { + return false + } + } + return true +} + +func noPrefix(words []string) { + tree := &trie{} + + for _, word := range words { + if tree.find(word) { + fmt.Print("BAD SET\n", word) + return + } + tree.add(word) + } + fmt.Println("GOOD SET") +} +func main() { + reader := bufio.NewReaderSize(os.Stdin, 16*1024*1024) + + nTemp, err := strconv.ParseInt(strings.TrimSpace(readLine(reader)), 10, 64) + checkError(err) + n := int32(nTemp) + + var words []string + + for i := 0; i < int(n); i++ { + wordsItem := readLine(reader) + words = append(words, wordsItem) + } + + noPrefix(words) +} + +func readLine(reader *bufio.Reader) string { + str, _, err := reader.ReadLine() + if err == io.EOF { + return "" + } + + return strings.TrimRight(string(str), "\r\n") +} + +func checkError(err error) { + if err != nil { + panic(err) + } +} diff --git a/hacker-rank/pangrams/main.go b/hacker-rank/pangrams/main.go new file mode 100644 index 0000000..016e378 --- /dev/null +++ b/hacker-rank/pangrams/main.go @@ -0,0 +1,69 @@ +package main + +import ( + "bufio" + "fmt" + "io" + "os" + "strings" +) + +/* + * Complete the 'pangrams' function below. + * + * The function is expected to return a STRING. + * The function accepts STRING s as parameter. + */ + +func pangrams(s string) string { + // Write your code here + s = strings.ToLower(s) + + m := make(map[rune]struct{}, 26) + for _, a := range s { + if a >= 'a' && a <= 'z' { + fmt.Println(a) + m[a] = struct{}{} + } + } + fmt.Println(len(m)) + if len(m) == 26 { + return "pangram" + } else { + return "not pangram" + } +} + +func main() { + reader := bufio.NewReaderSize(os.Stdin, 16 * 1024 * 1024) + + stdout, err := os.Create(os.Getenv("OUTPUT_PATH")) + checkError(err) + + defer stdout.Close() + + writer := bufio.NewWriterSize(stdout, 16 * 1024 * 1024) + + s := readLine(reader) + + result := pangrams(s) + + fmt.Fprintf(writer, "%s\n", result) + + writer.Flush() +} + +func readLine(reader *bufio.Reader) string { + str, _, err := reader.ReadLine() + if err == io.EOF { + return "" + } + + return strings.TrimRight(string(str), "\r\n") +} + +func checkError(err error) { + if err != nil { + panic(err) + } +} diff --git a/hacker-rank/plus-minus/main.go b/hacker-rank/plus-minus/main.go new file mode 100644 index 0000000..2e12ef8 --- /dev/null +++ b/hacker-rank/plus-minus/main.go @@ -0,0 +1,69 @@ +package main + +import ( + "bufio" + "fmt" + "io" + "os" + "strconv" + "strings" +) + +/* + * Complete the 'plusMinus' function below. + * + * The function accepts INTEGER_ARRAY arr as parameter. + */ + +func plusMinus(arr []int32) { + n := float64(len(arr)) + var pos, neg, zero float64 + for _, v := range arr { + switch { + case v == 0: + zero++ + case v > 0: + pos++ + case v < 0: + neg++ + } + } + + fmt.Printf("%0.6f\n%0.6f\n%0.6f\n", pos/n, neg/n, zero/n) +} + +func main() { + reader := bufio.NewReaderSize(os.Stdin, 16 * 1024 * 1024) + + nTemp, err := strconv.ParseInt(strings.TrimSpace(readLine(reader)), 10, 64) + checkError(err) + n := int32(nTemp) + + arrTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ") + + var arr []int32 + + for i := 0; i < int(n); i++ { + arrItemTemp, err := strconv.ParseInt(arrTemp[i], 10, 64) + checkError(err) + arrItem := int32(arrItemTemp) + arr = append(arr, arrItem) + } + + plusMinus(arr) +} + +func readLine(reader *bufio.Reader) string { + str, _, err := reader.ReadLine() + if err == io.EOF { + return "" + } + + return strings.TrimRight(string(str), "\r\n") +} + +func checkError(err error) { + if err != nil { + panic(err) + } +} diff --git a/hacker-rank/sparse-arrays/main.go b/hacker-rank/sparse-arrays/main.go new file mode 100644 index 0000000..9f1b442 --- /dev/null +++ b/hacker-rank/sparse-arrays/main.go @@ -0,0 +1,92 @@ +package main + +import ( + "bufio" + "fmt" + "io" + "os" + "strconv" + stringz "strings" +) + +/* + * Complete the 'matchingStrings' function below. + * + * The function is expected to return an INTEGER_ARRAY. + * The function accepts following parameters: + * 1. STRING_ARRAY strings + * 2. STRING_ARRAY queries + */ + +func matchingStrings(strings []string, queries []string) []int32 { + // Write your code here + res := make([]int32, len(queries)) + for _, s := range strings { + for i, q := range queries { + if s == q { + res[i]++ + } + } + } + return res +} + +func main() { + reader := bufio.NewReaderSize(os.Stdin, 16 * 1024 * 1024) + + stdout, err := os.Create(os.Getenv("OUTPUT_PATH")) + checkError(err) + + defer stdout.Close() + + writer := bufio.NewWriterSize(stdout, 16 * 1024 * 1024) + + stringsCount, err := strconv.ParseInt(stringz.TrimSpace(readLine(reader)), 10, 64) + checkError(err) + + var strings []string + + for i := 0; i < int(stringsCount); i++ { + stringsItem := readLine(reader) + strings = append(strings, stringsItem) + } + + queriesCount, err := strconv.ParseInt(stringz.TrimSpace(readLine(reader)), 10, 64) + checkError(err) + + var queries []string + + for i := 0; i < int(queriesCount); i++ { + queriesItem := readLine(reader) + queries = append(queries, queriesItem) + } + + res := matchingStrings(strings, queries) + + for i, resItem := range res { + fmt.Fprintf(writer, "%d", resItem) + + if i != len(res) - 1 { + fmt.Fprintf(writer, "\n") + } + } + + fmt.Fprintf(writer, "\n") + + writer.Flush() +} + +func readLine(reader *bufio.Reader) string { + str, _, err := reader.ReadLine() + if err == io.EOF { + return "" + } + + return stringz.TrimRight(string(str), "\r\n") +} + +func checkError(err error) { + if err != nil { + panic(err) + } +} diff --git a/hacker-rank/time-conversion/main.go b/hacker-rank/time-conversion/main.go new file mode 100644 index 0000000..bdf799f --- /dev/null +++ b/hacker-rank/time-conversion/main.go @@ -0,0 +1,60 @@ +package main + +import ( + "bufio" + "fmt" + "io" + "os" + "strings" + "time" +) + +/* + * Complete the 'timeConversion' function below. + * + * The function is expected to return a STRING. + * The function accepts STRING s as parameter. + */ + +func timeConversion(s string) string { + dt, err := time.Parse("03:04:05PM", s) + if err != nil { + println(err.Error()) + return "" + } + return dt.Format("15:04:05") +} + +func main() { + reader := bufio.NewReaderSize(os.Stdin, 16 * 1024 * 1024) + + stdout, err := os.Create(os.Getenv("OUTPUT_PATH")) + checkError(err) + + defer stdout.Close() + + writer := bufio.NewWriterSize(stdout, 16 * 1024 * 1024) + + s := readLine(reader) + + result := timeConversion(s) + + fmt.Fprintf(writer, "%s\n", result) + + writer.Flush() +} + +func readLine(reader *bufio.Reader) string { + str, _, err := reader.ReadLine() + if err == io.EOF { + return "" + } + + return strings.TrimRight(string(str), "\r\n") +} + +func checkError(err error) { + if err != nil { + panic(err) + } +} diff --git a/hacker-rank/tree-height-of-a-binary-tree/main.go b/hacker-rank/tree-height-of-a-binary-tree/main.go new file mode 100644 index 0000000..72e3e25 --- /dev/null +++ b/hacker-rank/tree-height-of-a-binary-tree/main.go @@ -0,0 +1,67 @@ +package main +import ( + "fmt" + "bufio" + "strconv" + "strings" + "os" +) + +func main() { + var n int + var tree *node + scanner := bufio.NewScanner(os.Stdin) + for scanner.Scan() { + text := scanner.Text() + if n == 0 { + n, _ = strconv.Atoi(text) + continue + } + for _, s := range strings.Fields(text) { + if v, err := strconv.Atoi(s); err == nil { + tree = insert(tree, &node{value:v}) + } + } + } + fmt.Println(height(tree, 0)) +} + +type node struct{ + value int + left *node + right *node +} + +func insert(root, n *node) *node { + if root == nil { + return n + } + if n.value < root.value { + root.left = insert(root.left, n) + return root + } + root.right = insert(root.right, n) + return root +} + +func height(root *node, currentHeight int) int { + if root == nil { + return -1 + } + + maxHeight := currentHeight + + if root.left != nil { + if nh := height(root.left, currentHeight+1); nh > maxHeight { + maxHeight = nh + } + } + + if root.right != nil { + if nh := height(root.right, currentHeight+1); nh > maxHeight { + maxHeight = nh + } + } + + return maxHeight +} diff --git a/hacker-rank/tree-preorder-traversal/main.go b/hacker-rank/tree-preorder-traversal/main.go new file mode 100644 index 0000000..03bb24a --- /dev/null +++ b/hacker-rank/tree-preorder-traversal/main.go @@ -0,0 +1,52 @@ +package main +import "fmt" +import "os" +import "bufio" +import "strconv" +import "strings" + +func main() { + var length int + var tree *node + + scanner := bufio.NewScanner(os.Stdin) + for scanner.Scan() { + if length == 0 { + length, _ = strconv.Atoi(scanner.Text()) + continue + } + + for i, txt := range strings.Fields(scanner.Text()) { + if i >= length { break } + if v, err := strconv.Atoi(txt); err == nil { + tree = insert(tree, &node{value: v}) + } + } + } + + fmt.Println(tree) +} + +type node struct { + value int + left *node + right *node +} + +func insert(root, n *node) *node { + if root == nil { + return n + } + if root.value > n.value { + root.left = insert(root.left, n) + return root + } + root.right = insert(root.right, n) + return root +} + +func (n *node) String() string { + if n == nil { return "" } + return fmt.Sprintf("%v %v%v", n.value, n.left.String(), n.right.String()) + +}