-
Notifications
You must be signed in to change notification settings - Fork 23
[Concept] String Functions - add exercise templates #158
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
base: main
Are you sure you want to change the base?
Changes from 5 commits
bc83ffc
9122c95
351126b
f835a55
f501ede
5552405
89be674
cfb0f7d
dcf2014
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
| } | ||
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package string_functions | ||
|
|
||
| import "strings" | ||
|
|
||
| // Contains checks if substr is within s | ||
| func Contains(s, substr string) bool { | ||
| return strings.Contains(s, substr) | ||
| } | ||
|
|
||
| // HasPrefix tests whether the string s begins with prefix | ||
| func HasPrefix(s, prefix string) bool { | ||
| return strings.HasPrefix(s, prefix) | ||
| } | ||
|
|
||
| // HasSuffix tests whether the string s ends with suffix | ||
| func HasSuffix(s, suffix string) bool { | ||
| return strings.HasSuffix(s, suffix) | ||
| } | ||
|
|
||
| // Index returns the index of the first instance of substr in s, or -1 if substr is not present in s | ||
| func Index(s, substr string) int { | ||
| return strings.Index(s, substr) | ||
| } | ||
|
|
||
| // ToUpper returns s with all Unicode letters mapped to their upper case | ||
| func ToUpper(s string) string { | ||
| return strings.ToUpper(s) | ||
| } | ||
|
|
||
| // ToLower returns s with all Unicode letters mapped to their lower case | ||
| func ToLower(s string) string { | ||
| return strings.ToLower(s) | ||
| } | ||
|
|
||
| // TrimSpace returns s with all leading and trailing white space removed | ||
| func TrimSpace(s string) string { | ||
| return strings.TrimSpace(s) | ||
| } |
| 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 | ||
| } |
| 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) | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion | 🟠 Major
🧩 Analysis chain
Verify exercise numbering sequence.
The new exercises 41_regex and 42_string_functions are listed before 36_json in the Advanced section. This breaks the numerical sequence. Consider either:
However, if exercises 38-40 exist elsewhere and this ordering is intentional, this is acceptable.
Run this script to check if exercises 38, 39, and 40 exist in the catalog:
🏁 Script executed:
Length of output: 82
🏁 Script executed:
Length of output: 0
🏁 Script executed:
Length of output: 622
Reorder Advanced exercises in docs/exercises.md
<li>36_json – JSON Processing</li>and<li>37_xml – XML Processing</li>above the new entries (<li>41_regex – Regular Expressions</li>,<li>42_string_functions – String Functions</li>).Advanced (21-42)if you intend to include 41 and 42 here.🤖 Prompt for AI Agents