Skip to content

Commit 1df51c9

Browse files
authored
Merge pull request #57 from codecrafters-io/andy/fix-2
Add randomWordsNoSubstrings
2 parents 01c9cef + 70a335e commit 1df51c9

File tree

5 files changed

+22
-6
lines changed

5 files changed

+22
-6
lines changed

internal/stage_end_of_string_anchor.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@ import (
44
"fmt"
55

66
"github.com/codecrafters-io/grep-tester/internal/test_cases"
7-
"github.com/codecrafters-io/tester-utils/random"
87
"github.com/codecrafters-io/tester-utils/test_case_harness"
98
)
109

1110
func testEndOfStringAnchor(stageHarness *test_case_harness.TestCaseHarness) error {
1211
RelocateSystemGrep(stageHarness)
1312

14-
words := random.RandomWords(3)
13+
words := randomWordsWithoutSubstrings(3)
1514

1615
testCaseCollection := test_cases.StdinTestCaseCollection{
1716
{

internal/stage_match_alphanumeric.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
func testMatchAlphanumeric(stageHarness *test_case_harness.TestCaseHarness) error {
1414
RelocateSystemGrep(stageHarness)
1515

16-
words := random.RandomWords(2)
16+
words := randomWordsWithoutSubstrings(2)
1717
specialCharacters := []string{"+", "-", "÷", "×", "=", "#", "%"}
1818

1919
nonWord1 := strings.Join(random.RandomElementsFromArray(specialCharacters, 3), "")

internal/stage_positive_character_groups.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
func testPositiveCharacterGroups(stageHarness *test_case_harness.TestCaseHarness) error {
1313
RelocateSystemGrep(stageHarness)
1414

15-
words := random.RandomWords(3)
15+
words := randomWordsWithoutSubstrings(3)
1616
letterInsideWord0 := random.RandomElementFromArray(strings.Split(words[0], ""))
1717
lettersOutsideWord0 := pickLettersOutsideWord(words[0], 2)
1818
lettersOutsideWord1 := pickLettersOutsideWord(words[1], len(words[1]))

internal/stage_start_of_string_anchor.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@ import (
44
"fmt"
55

66
"github.com/codecrafters-io/grep-tester/internal/test_cases"
7-
"github.com/codecrafters-io/tester-utils/random"
87
"github.com/codecrafters-io/tester-utils/test_case_harness"
98
)
109

1110
func testStartOfStringAnchor(stageHarness *test_case_harness.TestCaseHarness) error {
1211
RelocateSystemGrep(stageHarness)
1312

14-
words := random.RandomWords(2)
13+
words := randomWordsWithoutSubstrings(2)
1514

1615
testCaseCollection := test_cases.StdinTestCaseCollection{
1716
{

internal/utils.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package internal
22

33
import (
44
"strconv"
5+
"strings"
56

67
"github.com/codecrafters-io/tester-utils/random"
78
)
@@ -14,3 +15,20 @@ var VEGETABLES = []string{"carrot", "onion", "potato", "tomato", "broccoli", "ca
1415
func randomFilePrefix() string {
1516
return strconv.Itoa(random.RandomInt(1000, 10000))
1617
}
18+
19+
func randomWordsWithoutSubstrings(n int) []string {
20+
loop:
21+
for {
22+
words := random.RandomWords(n)
23+
24+
for i := 0; i < len(words); i++ {
25+
for j := i + 1; j < len(words); j++ {
26+
if strings.Contains(words[j], words[i]) || strings.Contains(words[i], words[j]) {
27+
continue loop
28+
}
29+
}
30+
}
31+
32+
return words
33+
}
34+
}

0 commit comments

Comments
 (0)