-
Notifications
You must be signed in to change notification settings - Fork 23
[Concept] Regular Expressions-add exercise templates #141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
aryasoni98
wants to merge
8
commits into
zhravan:main
Choose a base branch
from
aryasoni98:issue-80
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bc83ffc
[Concept] Regular Expressions-add exercise templates
aryasoni98 29949a0
Merge main into issue-80: Resolved conflicts, moved regex exercise to…
aryasoni98 1d28a4b
docs: Update exercises.md to include new exercises 28-35
aryasoni98 a43b76e
[Concept] Regular Expressions-add exercise templates
aryasoni98 a22daf7
Merge main into issue-80: resolve catalog.yaml conflict by renumberin…
aryasoni98 4fd0f3a
Merge branch 'main' into issue-80
aryasoni98 160d672
Merge branch 'main' into issue-80
aryasoni98 7b562bd
Merge branch 'main' into issue-80
aryasoni98 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package regex | ||
|
|
||
| import "regexp" | ||
|
|
||
| // IsValidEmail returns true if the email address is valid. | ||
| func IsValidEmail(email string) bool { | ||
| pattern := `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$` | ||
| re := regexp.MustCompile(pattern) | ||
| return re.MatchString(email) | ||
| } | ||
|
|
||
| // ExtractNumbers returns all numbers found in the input string. | ||
| func ExtractNumbers(text string) []string { | ||
| pattern := `\d+\.?\d*` | ||
| re := regexp.MustCompile(pattern) | ||
| matches := re.FindAllString(text, -1) | ||
| if matches == nil { | ||
| return []string{} | ||
| } | ||
| return matches | ||
| } | ||
|
|
||
| // ReplaceVowels replaces all vowels with asterisks. | ||
| func ReplaceVowels(text string) string { | ||
| pattern := `[aeiouAEIOU]` | ||
| re := regexp.MustCompile(pattern) | ||
| return re.ReplaceAllString(text, "*") | ||
| } | ||
|
|
||
| // IsPhoneNumber returns true if the phone number matches format (XXX) XXX-XXXX. | ||
| func IsPhoneNumber(phone string) bool { | ||
| pattern := `^\(\d{3}\) \d{3}-\d{4}$` | ||
| re := regexp.MustCompile(pattern) | ||
| return re.MatchString(phone) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| package regex | ||
|
|
||
| import "testing" | ||
|
|
||
| func TestIsValidEmail(t *testing.T) { | ||
| tests := []struct { | ||
| email string | ||
| expected bool | ||
| }{ | ||
| {"[email protected]", true}, | ||
| {"[email protected]", true}, | ||
| {"invalid-email", false}, | ||
| {"@domain.com", false}, | ||
| {"user@", false}, | ||
| {"user@domain", false}, | ||
| {"", false}, | ||
| } | ||
|
|
||
| for _, test := range tests { | ||
| result := IsValidEmail(test.email) | ||
| if result != test.expected { | ||
| t.Errorf("IsValidEmail(%q) = %v, want %v", test.email, result, test.expected) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestExtractNumbers(t *testing.T) { | ||
| tests := []struct { | ||
| text string | ||
| expected []string | ||
| }{ | ||
| {"I have 123 apples and 45.67 oranges", []string{"123", "45.67"}}, | ||
| {"The price is $99.99", []string{"99.99"}}, | ||
| {"No numbers here", []string{}}, | ||
| {"123", []string{"123"}}, | ||
| {"12.34.56", []string{"12.34", "56"}}, | ||
| {"", []string{}}, | ||
| } | ||
|
|
||
| for _, test := range tests { | ||
| result := ExtractNumbers(test.text) | ||
| if len(result) != len(test.expected) { | ||
| t.Errorf("ExtractNumbers(%q) length = %d, want %d", test.text, len(result), len(test.expected)) | ||
| continue | ||
| } | ||
| for i, num := range result { | ||
| if num != test.expected[i] { | ||
| t.Errorf("ExtractNumbers(%q)[%d] = %q, want %q", test.text, i, num, test.expected[i]) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestReplaceVowels(t *testing.T) { | ||
| tests := []struct { | ||
| text string | ||
| expected string | ||
| }{ | ||
| {"hello", "h*ll*"}, | ||
| {"HELLO", "H*LL*"}, | ||
| {"Hello World", "H*ll* W*rld"}, | ||
| {"bcdfg", "bcdfg"}, | ||
| {"", ""}, | ||
| {"aeiou", "*****"}, | ||
| {"AEIOU", "*****"}, | ||
| } | ||
|
|
||
| for _, test := range tests { | ||
| result := ReplaceVowels(test.text) | ||
| if result != test.expected { | ||
| t.Errorf("ReplaceVowels(%q) = %q, want %q", test.text, result, test.expected) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestIsPhoneNumber(t *testing.T) { | ||
| tests := []struct { | ||
| phone string | ||
| expected bool | ||
| }{ | ||
| {"(123) 456-7890", true}, | ||
| {"(555) 123-4567", true}, | ||
| {"(000) 000-0000", true}, | ||
| {"123-456-7890", false}, | ||
| {"(123)456-7890", false}, | ||
| {"(123) 4567890", false}, | ||
| {"123 456 7890", false}, | ||
| {"(123) 456-789", false}, | ||
| {"(12) 456-7890", false}, | ||
| {"", false}, | ||
| } | ||
|
|
||
| for _, test := range tests { | ||
| result := IsPhoneNumber(test.phone) | ||
| if result != test.expected { | ||
| t.Errorf("IsPhoneNumber(%q) = %v, want %v", test.phone, result, test.expected) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package regex | ||
|
|
||
| // Task: | ||
| // Implement regular expression functions using Go's regexp package. | ||
| // | ||
| // 1. Implement IsValidEmail to validate email addresses using regex. | ||
| // 2. Implement ExtractNumbers to extract all numbers from a string. | ||
| // 3. Implement ReplaceVowels to replace all vowels with asterisks. | ||
| // 4. Implement IsPhoneNumber to validate phone numbers in format (XXX) XXX-XXXX. | ||
|
|
||
| // IsValidEmail should return true if the email address is valid. | ||
| // A valid email should have the format: [email protected] | ||
| func IsValidEmail(email string) bool { | ||
| // TODO: implement using regexp.MustCompile and MatchString | ||
| return false | ||
| } | ||
|
|
||
| // ExtractNumbers should return all numbers found in the input string. | ||
| // Numbers can be integers or decimals (e.g., "123", "45.67"). | ||
| func ExtractNumbers(text string) []string { | ||
| // TODO: implement using regexp.FindAllString | ||
| return nil | ||
| } | ||
|
|
||
| // ReplaceVowels should replace all vowels (a, e, i, o, u) with asterisks. | ||
| // Case-insensitive replacement. | ||
| func ReplaceVowels(text string) string { | ||
| // TODO: implement using regexp.MustCompile and ReplaceAllString | ||
| return "" | ||
| } | ||
|
|
||
| // IsPhoneNumber should return true if the phone number matches format (XXX) XXX-XXXX. | ||
| // Example: (123) 456-7890 | ||
| func IsPhoneNumber(phone string) bool { | ||
| // TODO: implement using regexp.MustCompile and MatchString | ||
| return false | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| package regex | ||
|
|
||
| import "testing" | ||
|
|
||
| func TestIsValidEmail(t *testing.T) { | ||
| tests := []struct { | ||
| email string | ||
| expected bool | ||
| }{ | ||
| {"[email protected]", true}, | ||
| {"[email protected]", true}, | ||
| {"invalid-email", false}, | ||
| {"@domain.com", false}, | ||
| {"user@", false}, | ||
| {"user@domain", false}, | ||
| {"", false}, | ||
| } | ||
|
|
||
| for _, test := range tests { | ||
| result := IsValidEmail(test.email) | ||
| if result != test.expected { | ||
| t.Errorf("IsValidEmail(%q) = %v, want %v", test.email, result, test.expected) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestExtractNumbers(t *testing.T) { | ||
| tests := []struct { | ||
| text string | ||
| expected []string | ||
| }{ | ||
| {"I have 123 apples and 45.67 oranges", []string{"123", "45.67"}}, | ||
| {"The price is $99.99", []string{"99.99"}}, | ||
| {"No numbers here", []string{}}, | ||
| {"123", []string{"123"}}, | ||
| {"12.34.56", []string{"12.34", "56"}}, | ||
| {"", []string{}}, | ||
| } | ||
|
|
||
| for _, test := range tests { | ||
| result := ExtractNumbers(test.text) | ||
| if len(result) != len(test.expected) { | ||
| t.Errorf("ExtractNumbers(%q) length = %d, want %d", test.text, len(result), len(test.expected)) | ||
| continue | ||
| } | ||
| for i, num := range result { | ||
| if num != test.expected[i] { | ||
| t.Errorf("ExtractNumbers(%q)[%d] = %q, want %q", test.text, i, num, test.expected[i]) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestReplaceVowels(t *testing.T) { | ||
| tests := []struct { | ||
| text string | ||
| expected string | ||
| }{ | ||
| {"hello", "h*ll*"}, | ||
| {"HELLO", "H*LL*"}, | ||
| {"Hello World", "H*ll* W*rld"}, | ||
| {"bcdfg", "bcdfg"}, | ||
| {"", ""}, | ||
| {"aeiou", "*****"}, | ||
| {"AEIOU", "*****"}, | ||
| } | ||
|
|
||
| for _, test := range tests { | ||
| result := ReplaceVowels(test.text) | ||
| if result != test.expected { | ||
| t.Errorf("ReplaceVowels(%q) = %q, want %q", test.text, result, test.expected) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestIsPhoneNumber(t *testing.T) { | ||
| tests := []struct { | ||
| phone string | ||
| expected bool | ||
| }{ | ||
| {"(123) 456-7890", true}, | ||
| {"(555) 123-4567", true}, | ||
| {"(000) 000-0000", true}, | ||
| {"123-456-7890", false}, | ||
| {"(123)456-7890", false}, | ||
| {"(123) 4567890", false}, | ||
| {"123 456 7890", false}, | ||
| {"(123) 456-789", false}, | ||
| {"(12) 456-7890", false}, | ||
| {"", false}, | ||
| } | ||
|
|
||
| for _, test := range tests { | ||
| result := IsPhoneNumber(test.phone) | ||
| if result != test.expected { | ||
| t.Errorf("IsPhoneNumber(%q) = %v, want %v", test.phone, result, test.expected) | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.