go: allow testing

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-06-16 11:44:08 +02:00
parent e2b6b15847
commit d2ef757754
Signed by: mfocko
GPG key ID: 7C47D46246790496
33 changed files with 81 additions and 64 deletions

View file

@ -1,4 +1,4 @@
package append_characters_to_string_to_make_subsequence package main
func appendCharacters(s string, t string) int { func appendCharacters(s string, t string) int {
si, ti := 0, 0 si, ti := 0, 0

View file

@ -1,4 +1,4 @@
package continuous_subarray_sum package main
func checkSubarraySum(nums []int, k int) bool { func checkSubarraySum(nums []int, k int) bool {
first_index_of := make(map[int]int) first_index_of := make(map[int]int)

View file

@ -1,4 +1,4 @@
package count_triplets_that_can_form_two_arrays_of_equal_xor package main
func countTriplets(arr []int) int { func countTriplets(arr []int) int {
precomputePrefix := func() []int { precomputePrefix := func() []int {

View file

@ -1,4 +1,4 @@
package diameter_of_binary_tree package main
/** /**
* Definition for a binary tree node. * Definition for a binary tree node.

View file

@ -1,4 +1,4 @@
package find_common_characters package main
func commonChars(words []string) []string { func commonChars(words []string) []string {
A := int('a') A := int('a')

View file

@ -1,4 +1,4 @@
package get_equal_substrings_within_budget package main
func equalSubstring(s string, t string, maxCost int) int { func equalSubstring(s string, t string, maxCost int) int {
abs := func(x int) int { abs := func(x int) int {

11
go/go.mod Normal file
View file

@ -0,0 +1,11 @@
module gitlab.com/mfocko/LeetCode
go 1.22.3
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emirpasic/gods/v2 v2.0.0-alpha // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.9.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

11
go/go.sum Normal file
View file

@ -0,0 +1,11 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/emirpasic/gods/v2 v2.0.0-alpha h1:dwFlh8pBg1VMOXWGipNMRt8v96dKAIvBehtCt6OtunU=
github.com/emirpasic/gods/v2 v2.0.0-alpha/go.mod h1:W0y4M2dtBB9U5z3YlghmpuUhiaZT2h6yoeE+C1sCp6A=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View file

@ -1,4 +1,4 @@
package height_checker package main
func heightChecker(heights []int) int { func heightChecker(heights []int) int {
// count the heights // count the heights

View file

@ -1,4 +1,4 @@
package implement_queue_using_stacks package main
import ( import (
"slices" "slices"
@ -9,13 +9,18 @@ type MyQueue struct {
asStack []int asStack []int
} }
func Constructor() MyQueue { func NewQueue() MyQueue {
return MyQueue{ return MyQueue{
asQueue: make([]int, 0, 16), asQueue: make([]int, 0, 16),
asStack: make([]int, 0, 16), asStack: make([]int, 0, 16),
} }
} }
// [XXX] Uncomment for LeetCode
// func Constructor() MyQueue {
// return NewQueue()
// }
func (this *MyQueue) Push(x int) { func (this *MyQueue) Push(x int) {
this.asStack = append(this.asStack, x) this.asStack = append(this.asStack, x)
} }

View file

@ -1,19 +1,19 @@
package implement_queue_using_stacks package main
import ( import (
"testing" "testing"
) )
func TestEmpty(t *testing.T) { func Test_ImplementQueueUsingStacks_Empty(t *testing.T) {
obj := Constructor() obj := NewQueue()
if !obj.Empty() { if !obj.Empty() {
t.Error("Queue should be empty") t.Error("Queue should be empty")
} }
} }
func TestBasic(t *testing.T) { func Test_ImplementQueueUsingStacks_Basic(t *testing.T) {
q := Constructor() q := NewQueue()
q.Push(1) q.Push(1)
q.Push(2) q.Push(2)

View file

@ -1,4 +1,4 @@
package implement_trie_prefix_tree package main
type TrieNode struct { type TrieNode struct {
has bool has bool
@ -9,10 +9,15 @@ type Trie struct {
root TrieNode root TrieNode
} }
func Constructor() Trie { func NewTrie() Trie {
return Trie{} return Trie{}
} }
// [XXX] Uncomment for LeetCode
// func Constructor() Trie {
// return NewTrie()
// }
func (this *Trie) Insert(word string) { func (this *Trie) Insert(word string) {
node := &this.root node := &this.root

View file

@ -1,4 +1,4 @@
package ipo package main
import ( import (
"cmp" "cmp"

6
go/listnode.go Normal file
View file

@ -0,0 +1,6 @@
package main
type ListNode struct {
Val int
Next *ListNode
}

View file

@ -1,4 +1,4 @@
package longest_palindrome package main
func longestPalindrome(s string) int { func longestPalindrome(s string) int {
getFreqs := func() map[rune]int { getFreqs := func() map[rune]int {

View file

@ -1,4 +1,4 @@
package middle_of_the_linked_list package main
/** /**
* Definition for singly-linked list. * Definition for singly-linked list.

View file

@ -1,4 +1,4 @@
package minimum_increment_to_make_array_unique package main
import "slices" import "slices"

View file

@ -1,4 +1,4 @@
package minimum_number_of_moves_to_seat_everyone package main
import ( import (
"slices" "slices"

View file

@ -1,4 +1,4 @@
package number_of_steps_to_reduce_a_number_in_binary_representation_to_one package main
func numSteps(s string) int { func numSteps(s string) int {
steps := 0 steps := 0

View file

@ -1,4 +1,4 @@
package palindrome_partitioning package main
import ( import (
"slices" "slices"

View file

@ -1,4 +1,4 @@
package palindromic_substrings package main
func checkSubstring(s string, i, j int) int { func checkSubstring(s string, i, j int) int {
if i < 0 || j >= len(s) { if i < 0 || j >= len(s) {

View file

@ -1,4 +1,4 @@
package relative_sort_array package main
import ( import (
"cmp" "cmp"

View file

@ -1,4 +1,4 @@
package remove_nth_node_from_end_of_list package main
/** /**
* Definition for singly-linked list. * Definition for singly-linked list.

View file

@ -1,4 +1,4 @@
package same_tree package main
/** /**
* Definition for a binary tree node. * Definition for a binary tree node.

View file

@ -1,4 +1,4 @@
package score_of_a_string package main
func scoreOfString(s string) int { func scoreOfString(s string) int {
abs := func(x int) int { abs := func(x int) int {

View file

@ -1,32 +1,4 @@
package search_suggestions_system package main
type TrieNode struct {
has bool
successors [26]*TrieNode
}
type Trie struct {
root TrieNode
}
func Constructor() Trie {
return Trie{}
}
func (this *Trie) Insert(word string) {
node := &this.root
for _, c := range word {
idx := int(c) - int('a')
if node.successors[idx] == nil {
node.successors[idx] = new(TrieNode)
}
node = node.successors[idx]
}
node.has = true
}
func (this *Trie) Suggest(word string) [][]string { func (this *Trie) Suggest(word string) [][]string {
firstThree := func(node *TrieNode, prefix string) []string { firstThree := func(node *TrieNode, prefix string) []string {
@ -71,7 +43,7 @@ func (this *Trie) Suggest(word string) [][]string {
} }
func suggestedProducts(products []string, searchWord string) [][]string { func suggestedProducts(products []string, searchWord string) [][]string {
trie := Constructor() trie := NewTrie()
for _, product := range products { for _, product := range products {
trie.Insert(product) trie.Insert(product)
} }

View file

@ -1,4 +1,4 @@
package single_number_iii package main
func singleNumber(nums []int) []int { func singleNumber(nums []int) []int {
reduce := func() int { reduce := func() int {

View file

@ -1,4 +1,4 @@
package sort_colors package main
func sortColors(nums []int) { func sortColors(nums []int) {
l, r := 0, len(nums)-1 l, r := 0, len(nums)-1

View file

@ -1,4 +1,4 @@
package special_array_with_x_elements_greater_than_or_equal_x package main
import ( import (
"slices" "slices"

View file

@ -1,4 +1,4 @@
package subarray_sums_divisible_by_k package main
func subarraysDivByK(nums []int, k int) int { func subarraysDivByK(nums []int, k int) int {
counters := make([]int, k) counters := make([]int, k)

View file

@ -1,4 +1,4 @@
package subsets package main
func subsets(nums []int) [][]int { func subsets(nums []int) [][]int {
var sets [][]int var sets [][]int

View file

@ -1,4 +1,4 @@
package the_number_of_beautiful_subsets package main
import ( import (
"slices" "slices"

7
go/treenode.go Normal file
View file

@ -0,0 +1,7 @@
package main
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}