Skip to content
Merged
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
846a35f
Add solution for Challenge 1 by ashwinipatankar
ashwinipatankar Jul 7, 2025
8c5c5f2
Add solution for Challenge 2 by ashwinipatankar
ashwinipatankar Jul 7, 2025
6f785f3
Add solution for Challenge 3 by ashwinipatankar
ashwinipatankar Jul 7, 2025
6ce142c
Add solution for Challenge 6 by ashwinipatankar
ashwinipatankar Jul 7, 2025
4ddba3c
Merge branch 'RezaSi:main' into main
ashwinipatankar Jul 7, 2025
27d03b9
Add solution for Challenge 18 by ashwinipatankar
ashwinipatankar Jul 7, 2025
b59f8d7
Add solution for Challenge 21 by ashwinipatankar
ashwinipatankar Jul 9, 2025
1274742
Merge branch 'RezaSi:main' into main
ashwinipatankar Jul 10, 2025
1650926
Add solution for Challenge 22 by ashwinipatankar
ashwinipatankar Jul 10, 2025
4d249e1
Merge branch 'RezaSi:main' into main
ashwinipatankar Jul 11, 2025
eda8f42
Add solution for Challenge 17 by ashwinipatankar
ashwinipatankar Jul 13, 2025
a6f9b25
Add solution for Challenge 19 by ashwinipatankar
ashwinipatankar Jul 13, 2025
64dabf7
Merge branch 'RezaSi:main' into main
ashwinipatankar Jul 13, 2025
3f519ba
Merge branch 'RezaSi:main' into main
ashwinipatankar Jul 16, 2025
b8bb274
Merge branch 'RezaSi:main' into main
ashwinipatankar Jul 17, 2025
d396ee4
Merge branch 'RezaSi:main' into main
ashwinipatankar Jul 19, 2025
77e0aa5
Add solution for Challenge 23 by ashwinipatankar
ashwinipatankar Jul 20, 2025
01f378e
Add solution for Challenge 7 by ashwinipatankar
ashwinipatankar Jul 20, 2025
b2d920f
Add solution for Challenge 10 by ashwinipatankar
ashwinipatankar Jul 20, 2025
c86ef87
Merge branch 'RezaSi:main' into main
ashwinipatankar Jul 21, 2025
0265b10
Add solution for Challenge 27 by ashwinipatankar
ashwinipatankar Jul 22, 2025
5905892
Merge branch 'RezaSi:main' into main
ashwinipatankar Jul 22, 2025
f47c3e4
Merge branch 'RezaSi:main' into main
ashwinipatankar Jul 22, 2025
c091af9
Add solution for Challenge 27 by ashwinipatankar
ashwinipatankar Jul 22, 2025
6f58292
Add solution for Challenge 13 by ashwinipatankar
ashwinipatankar Jul 22, 2025
e37bc41
Merge branch 'RezaSi:main' into main
ashwinipatankar Jul 23, 2025
18985a3
Merge branch 'RezaSi:main' into main
ashwinipatankar Jul 24, 2025
9a33e50
Add solution for Challenge 5 by ashwinipatankar
ashwinipatankar Jul 24, 2025
b691850
Merge branch 'RezaSi:main' into main
ashwinipatankar Jul 24, 2025
ff81574
Merge branch 'RezaSi:main' into main
ashwinipatankar Jul 24, 2025
9f11391
Merge branch 'RezaSi:main' into main
ashwinipatankar Jul 25, 2025
efd8816
Add solution for cobra challenge-1-basic-cli by ashwinipatankar
ashwinipatankar Jul 25, 2025
6c14637
Add solution for cobra challenge-2-flags-args by ashwinipatankar
ashwinipatankar Jul 26, 2025
3471f12
Merge branch 'RezaSi:main' into main
ashwinipatankar Jul 26, 2025
512c29e
Merge branch 'RezaSi:main' into main
ashwinipatankar Jul 28, 2025
a5397fc
Add solution for cobra challenge-3-subcommands-persistence by ashwini…
ashwinipatankar Jul 28, 2025
b743d7f
Merge branch 'RezaSi:main' into main
ashwinipatankar Jul 29, 2025
370e6e1
Merge branch 'RezaSi:main' into main
ashwinipatankar Jul 29, 2025
2feb5b4
Add solution for Challenge 30 by ashwinipatankar
ashwinipatankar Jul 29, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 163 additions & 0 deletions challenge-30/submissions/ashwinipatankar/solution-template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
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)
return context.WithCancel(parent)
}

// 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)
return context.WithTimeout(parent, timeout)
}

// 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)
return context.WithValue(parent, key, value)
}

// 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
val := ctx.Value(key)
if val == nil {
return nil, false
}

return val, true
}

// 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
done := make(chan error, 1)

go func() {
done <- task()
}()

select {
case err := <-done:
return err // Task completed first
case <-ctx.Done():
return ctx.Err() // Context cancelled/timeout first
}
}

// 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():
// work cancelled
return ctx.Err()
case <-time.After(duration):
// work completed
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():
// work cancelled
return ctx.Err()
case <-time.After(workDuration):
// work completed
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
var newItems []string
for i := range items {
select {
case <-ctx.Done():
return newItems, ctx.Err()
default:
// Process item
}

time.Sleep(100 * time.Millisecond)
newItems = append(newItems, fmt.Sprintf("processed_%v", items[i]))
}

return newItems, 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!")
}
Loading