Skip to content

Commit f8a5eca

Browse files
authored
Merge pull request #327 from sir-gon/feature/two_strings
[Hacker Rank] Interview Preparation Kit: Dictionaries and Hashmaps: T…
2 parents 37dba4b + 7bf49d4 commit f8a5eca

3 files changed

Lines changed: 95 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/two_strings.md]]
3+
*/
4+
5+
package hackerrank
6+
7+
const __TWO_STRINGS_YES__ = "Yes"
8+
const __TWO_STRINGS_NO__ = "No"
9+
10+
func twoStringsCompute(s1 string, s2 string) bool {
11+
for _, letter := range s1 {
12+
for _, n := range s2 {
13+
if letter == n {
14+
return true
15+
}
16+
}
17+
}
18+
19+
return false
20+
}
21+
22+
func twoStrings(s1 string, s2 string) string {
23+
if twoStringsCompute(s1, s2) {
24+
return __TWO_STRINGS_YES__
25+
}
26+
27+
return __TWO_STRINGS_NO__
28+
}
29+
30+
func TwoStrings(s1 string, s2 string) string {
31+
return twoStrings(s1, s2)
32+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[
2+
{
3+
"title": "Example 1",
4+
"s1": "and",
5+
"s2": "art",
6+
"expected": "Yes"
7+
},
8+
{
9+
"title": "Example 2",
10+
"s1": "be",
11+
"s2": "cat",
12+
"expected": "No"
13+
},
14+
{
15+
"title": "Sample Test Case 0",
16+
"s1": "hello",
17+
"s2": "world",
18+
"expected": "Yes"
19+
}
20+
]
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package hackerrank
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
"gon.cl/algorithms/utils"
10+
)
11+
12+
type TwoStringsTestCase struct {
13+
S1 string `json:"s1"`
14+
S2 string `json:"s2"`
15+
Expected string `json:"expected"`
16+
}
17+
18+
var TwoStringsTestCases []TwoStringsTestCase
19+
20+
// You can use testing.T, if you want to test the code without benchmarking
21+
func TwoStringsSetupSuite(t testing.TB) {
22+
wd, _ := os.Getwd()
23+
filepath := wd + "/two_strings.testcases.json"
24+
t.Log("Setup test cases from JSON: ", filepath)
25+
26+
var _, err = utils.LoadJSON(filepath, &TwoStringsTestCases)
27+
if err != nil {
28+
t.Log(err)
29+
}
30+
}
31+
32+
func TestTwoStrings(t *testing.T) {
33+
34+
TwoStringsSetupSuite(t)
35+
36+
for _, tt := range TwoStringsTestCases {
37+
testname := fmt.Sprintf("TwoStrings(%v, %v) => %v \n", tt.S1, tt.S2, tt.Expected)
38+
t.Run(testname, func(t *testing.T) {
39+
ans := TwoStrings(tt.S1, tt.S2)
40+
assert.Equal(t, tt.Expected, ans)
41+
})
42+
}
43+
}

0 commit comments

Comments
 (0)