From 7bf49d4d70ecba9fdb086c6c1d0f3216505ae2ff Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Tue, 22 Apr 2025 20:50:21 -0400 Subject: [PATCH] =?UTF-8?q?[Hacker=20Rank]=20Interview=20Preparation=20Kit?= =?UTF-8?q?:=20Dictionaries=20and=20Hashmaps:=20Two=20Strings.=20Solved=20?= =?UTF-8?q?=E2=9C=93.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dictionaries_and_hashmaps/two_strings.go | 32 ++++++++++++++ .../two_strings.testcases.json | 20 +++++++++ .../two_strings_test.go | 43 +++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 exercises/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/two_strings.go create mode 100644 exercises/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/two_strings.testcases.json create mode 100644 exercises/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/two_strings_test.go diff --git a/exercises/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/two_strings.go b/exercises/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/two_strings.go new file mode 100644 index 0000000..8369587 --- /dev/null +++ b/exercises/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/two_strings.go @@ -0,0 +1,32 @@ +/** + * @link Problem definition [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/two_strings.md]] + */ + +package hackerrank + +const __TWO_STRINGS_YES__ = "Yes" +const __TWO_STRINGS_NO__ = "No" + +func twoStringsCompute(s1 string, s2 string) bool { + for _, letter := range s1 { + for _, n := range s2 { + if letter == n { + return true + } + } + } + + return false +} + +func twoStrings(s1 string, s2 string) string { + if twoStringsCompute(s1, s2) { + return __TWO_STRINGS_YES__ + } + + return __TWO_STRINGS_NO__ +} + +func TwoStrings(s1 string, s2 string) string { + return twoStrings(s1, s2) +} diff --git a/exercises/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/two_strings.testcases.json b/exercises/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/two_strings.testcases.json new file mode 100644 index 0000000..73d1dcf --- /dev/null +++ b/exercises/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/two_strings.testcases.json @@ -0,0 +1,20 @@ +[ + { + "title": "Example 1", + "s1": "and", + "s2": "art", + "expected": "Yes" + }, + { + "title": "Example 2", + "s1": "be", + "s2": "cat", + "expected": "No" + }, + { + "title": "Sample Test Case 0", + "s1": "hello", + "s2": "world", + "expected": "Yes" + } +] diff --git a/exercises/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/two_strings_test.go b/exercises/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/two_strings_test.go new file mode 100644 index 0000000..3ccd501 --- /dev/null +++ b/exercises/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/two_strings_test.go @@ -0,0 +1,43 @@ +package hackerrank + +import ( + "fmt" + "os" + "testing" + + "github.com/stretchr/testify/assert" + "gon.cl/algorithms/utils" +) + +type TwoStringsTestCase struct { + S1 string `json:"s1"` + S2 string `json:"s2"` + Expected string `json:"expected"` +} + +var TwoStringsTestCases []TwoStringsTestCase + +// You can use testing.T, if you want to test the code without benchmarking +func TwoStringsSetupSuite(t testing.TB) { + wd, _ := os.Getwd() + filepath := wd + "/two_strings.testcases.json" + t.Log("Setup test cases from JSON: ", filepath) + + var _, err = utils.LoadJSON(filepath, &TwoStringsTestCases) + if err != nil { + t.Log(err) + } +} + +func TestTwoStrings(t *testing.T) { + + TwoStringsSetupSuite(t) + + for _, tt := range TwoStringsTestCases { + testname := fmt.Sprintf("TwoStrings(%v, %v) => %v \n", tt.S1, tt.S2, tt.Expected) + t.Run(testname, func(t *testing.T) { + ans := TwoStrings(tt.S1, tt.S2) + assert.Equal(t, tt.Expected, ans) + }) + } +}