-
-
Notifications
You must be signed in to change notification settings - Fork 685
Add solution for Challenge 30 by t4e1 #367
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
t4e1
wants to merge
63
commits into
RezaSi:main
Choose a base branch
from
t4e1:t4e1/remove-conflict
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.
+163
−0
Open
Changes from all commits
Commits
Show all changes
63 commits
Select commit
Hold shift + click to select a range
c0d1d68
Add solution for Challenge 1 by t4e1
t4e1 547250f
Add solution for Challenge 2 by t4e1
t4e1 c772c5f
Add solution for Challenge 3 by t4e1
t4e1 766eac1
Add solution for Challenge 6 by t4e1
t4e1 d9cc9c4
Add solution for Challenge 18 by t4e1
t4e1 dcf9e49
Add solution for Challenge 21 by t4e1
t4e1 9f7ece7
Add solution for Challenge 22 by t4e1
t4e1 c062ea1
Add solution for Challenge 4 by t4e1
t4e1 2fb4b31
Add solution for Challenge 10 by t4e1
t4e1 c3e3451
Add solution for Challenge 5 by t4e1
1f9d49d
Add solution for Challenge 7 by t4e1
61f7228
Add solution for Challenge 13 by t4e1
af3f014
Add solution for Challenge 17 by t4e1
t4e1 45470a5
Merge branch 'main' into main
RezaSi 9edeeed
Add solution for Challenge 19 by t4e1
t4e1 74623c7
Merge branch 'main' of https://github.com/t4e1/go-interview-practice
t4e1 2bc177f
Add solution for Challenge 27 by t4e1
t4e1 2704077
Merge branch 'main' into main
RezaSi 99d07a0
📊 Update scoreboards for: challenge-5 challenge-6
actions-user b454177
🏆 Auto-update: Profile badges regenerated
actions-user 444f562
📊 Update scoreboards for: challenge-27
actions-user 761578a
Auto-update main scoreboard
actions-user 573299a
🏆 Auto-update: Profile badges regenerated
actions-user 541ce4e
Add solution for Challenges by PolinaSvet (#342)
PolinaSvet 116f671
📊 Update scoreboards for: challenge-24 challenge-25 challenge-26 chal…
actions-user 49258f0
Auto-update main scoreboard
actions-user faa7fc2
🏆 Auto-update: Profile badges regenerated
actions-user 932c2a6
Add solution for Challenges by Gandook (#343)
Gandook e3ce203
📊 Update scoreboards for: challenge-26 challenge-5
actions-user b224b8b
Auto-update main scoreboard
actions-user 4ca9b75
🏆 Auto-update: Profile badges regenerated
actions-user b55f9e7
Add solution for Challenge 17 by longbui98 (#339)
longbui98 a909ee4
📊 Update scoreboards for: challenge-17
actions-user d8b2120
🏆 Auto-update: Profile badges regenerated
actions-user 955dbab
Add solution for Challenge 18 by longbui98 (#340)
longbui98 e3e5d24
📊 Update scoreboards for: challenge-18
actions-user f83ba94
🏆 Auto-update: Profile badges regenerated
actions-user 6c793c1
Add solution for Challenge 19 by longbui98 (#341)
longbui98 0e25884
📊 Update scoreboards for: challenge-19
actions-user fc3233a
🏆 Auto-update: Profile badges regenerated
actions-user 4aef9af
Add solution for Challenge 1 by jvllmr (#357)
jvllmr d469052
📊 Update scoreboards for: challenge-1
actions-user a8bb00c
Auto-update main scoreboard
actions-user 824d84d
Add solution for Challenges by wxai2324 and add single-array comparis…
wxai2324 f121aae
📊 Update scoreboards for: challenge-1 challenge-18 challenge-2 challe…
actions-user e15e9d4
Auto-update main scoreboard
actions-user 53f10f1
🏆 Auto-update: Profile badges regenerated
actions-user e634465
Update README.md
RezaSi 04f0a08
Add solution for Challenge 1 by malakagl (#358)
malakagl 96f2eb5
📊 Update scoreboards for: challenge-1
actions-user db6ac3c
Auto-update main scoreboard
actions-user 32e3dd9
Add solution for Challenges by Kesha005 (#345)
Kesha005 8fa331b
📊 Update scoreboards for: challenge-1 challenge-18
actions-user ea66788
Add solution for Challenge 21 by longbui98 (#346)
longbui98 20130cb
🏆 Auto-update: Profile badges regenerated
actions-user dbde3f0
📊 Update scoreboards for: challenge-21
actions-user f8cd427
🏆 Auto-update: Profile badges regenerated
actions-user 0968c1c
Add solution for Challenge 13 by Cpoing (#347)
Cpoing cdc5700
📊 Update scoreboards for: challenge-13
actions-user 8d18b35
🏆 Auto-update: Profile badges regenerated
actions-user 563d942
Add solution for Challenge 30 by t4e1
t4e1 e859507
Merge branch 'main' into t4e1/remove-conflict
t4e1 a06f8a6
Merge branch 'main' into t4e1/remove-conflict
RezaSi 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,161 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "time" | ||
| ) | ||
|
|
||
| // ContextManager defines a simplified interface for basic context operations | ||
| type ContextManager interface { | ||
| // Create a cancellable context from a parent context | ||
| CreateCancellableContext(parent context.Context) (context.Context, context.CancelFunc) | ||
|
|
||
| // Create a context with timeout | ||
| CreateTimeoutContext(parent context.Context, timeout time.Duration) (context.Context, context.CancelFunc) | ||
|
|
||
| // Add a value to context | ||
| AddValue(parent context.Context, key, value interface{}) context.Context | ||
|
|
||
| // Get a value from context | ||
| GetValue(ctx context.Context, key interface{}) (interface{}, bool) | ||
|
|
||
| // Execute a task with context cancellation support | ||
| ExecuteWithContext(ctx context.Context, task func() error) error | ||
|
|
||
| // Wait for context cancellation or completion | ||
| WaitForCompletion(ctx context.Context, duration time.Duration) error | ||
| } | ||
|
|
||
| // Simple context manager implementation | ||
| type simpleContextManager struct{} | ||
|
|
||
| // NewContextManager creates a new context manager | ||
| func NewContextManager() ContextManager { | ||
| return &simpleContextManager{} | ||
| } | ||
|
|
||
| // CreateCancellableContext creates a cancellable context | ||
| func (cm *simpleContextManager) CreateCancellableContext(parent context.Context) (context.Context, context.CancelFunc) { | ||
| // TODO: Implement cancellable context creation | ||
| // Hint: Use context.WithCancel(parent) | ||
| ctx, cancel := context.WithCancel(parent) | ||
| return ctx, cancel | ||
| } | ||
|
|
||
| // CreateTimeoutContext creates a context with timeout | ||
| func (cm *simpleContextManager) CreateTimeoutContext(parent context.Context, timeout time.Duration) (context.Context, context.CancelFunc) { | ||
| // TODO: Implement timeout context creation | ||
| // Hint: Use context.WithTimeout(parent, timeout) | ||
| ctx, cancel := context.WithTimeout(parent, timeout) | ||
| return ctx, cancel | ||
| } | ||
|
|
||
| // AddValue adds a key-value pair to the context | ||
| func (cm *simpleContextManager) AddValue(parent context.Context, key, value interface{}) context.Context { | ||
| // TODO: Implement value context creation | ||
| // Hint: Use context.WithValue(parent, key, value) | ||
| ctx := context.WithValue(parent, key, value) | ||
| return ctx | ||
| } | ||
|
|
||
| // GetValue retrieves a value from the context | ||
| func (cm *simpleContextManager) GetValue(ctx context.Context, key interface{}) (interface{}, bool) { | ||
| // TODO: Implement value retrieval from context | ||
| // Hint: Use ctx.Value(key) and check if it's nil | ||
| // Return the value and a boolean indicating if it was found | ||
| if val := ctx.Value(key); val != nil { | ||
| return val, true | ||
| } | ||
| return nil, false | ||
| } | ||
|
|
||
| // ExecuteWithContext executes a task that can be cancelled via context | ||
| func (cm *simpleContextManager) ExecuteWithContext(ctx context.Context, task func() error) error { | ||
| // TODO: Implement task execution with context cancellation | ||
| // Hint: Run the task in a goroutine and use select with ctx.Done() | ||
| // Return context error if cancelled, task error if task fails | ||
| errCh := make(chan error, 1) | ||
|
|
||
| go func() { | ||
| errCh <- task() | ||
| }() | ||
|
|
||
| select { | ||
| case err := <-errCh: | ||
| return err | ||
| case <-ctx.Done(): | ||
| return ctx.Err() | ||
| } | ||
|
|
||
| } | ||
|
|
||
| // WaitForCompletion waits for a duration or until context is cancelled | ||
| func (cm *simpleContextManager) WaitForCompletion(ctx context.Context, duration time.Duration) error { | ||
| // TODO: Implement waiting with context awareness | ||
| // Hint: Use select with ctx.Done() and time.After(duration) | ||
| // Return context error if cancelled, nil if duration completes | ||
| select { | ||
| case <-ctx.Done(): | ||
| return ctx.Err() | ||
| case <-time.After(duration): | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| // Helper function - simulate work that can be cancelled | ||
| func SimulateWork(ctx context.Context, workDuration time.Duration, description string) error { | ||
| // TODO: Implement cancellable work simulation | ||
| // Hint: Use select with ctx.Done() and time.After(workDuration) | ||
| // Print progress messages and respect cancellation | ||
| select { | ||
| case <-ctx.Done(): | ||
| return ctx.Err() | ||
| case <-time.After(workDuration): | ||
| fmt.Println(description) | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| // Helper function - process multiple items with context | ||
| func ProcessItems(ctx context.Context, items []string) ([]string, error) { | ||
| // TODO: Implement batch processing with context awareness | ||
| // Process each item but check for cancellation between items | ||
| // Return partial results if cancelled | ||
| result := make([]string, 0, len(items)) | ||
|
|
||
| for _, v := range items { | ||
| select { | ||
| case <-ctx.Done(): | ||
| return result, ctx.Err() | ||
| default: | ||
| } | ||
|
|
||
| time.Sleep(50 * time.Millisecond) | ||
|
|
||
| processedItem := fmt.Sprint("processed_", v) | ||
| result = append(result, processedItem) | ||
| } | ||
|
|
||
| return result, nil | ||
| } | ||
|
|
||
| // Example usage | ||
| func main() { | ||
| fmt.Println("Context Management Challenge") | ||
| fmt.Println("Implement the context manager methods!") | ||
|
|
||
| // Example of how the context manager should work: | ||
| cm := NewContextManager() | ||
|
|
||
| // Create a cancellable context | ||
| ctx, cancel := cm.CreateCancellableContext(context.Background()) | ||
| defer cancel() | ||
|
|
||
| // Add some values | ||
| ctx = cm.AddValue(ctx, "user", "alice") | ||
| ctx = cm.AddValue(ctx, "requestID", "12345") | ||
|
|
||
| // Use the context | ||
| fmt.Println("Context created with values!") | ||
| } | ||
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.
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.
Make tasks cancellable; pass ctx into the task to prevent goroutine leaks
Current design can’t stop a long-running task; the goroutine keeps executing after ctx is done. Pass ctx to the task and propagate cancellation.
Also applies to: 74-91
🤖 Prompt for AI Agents