diff --git a/challenge-5/submissions/korranat9/solution-template.go b/challenge-5/submissions/korranat9/solution-template.go new file mode 100644 index 00000000..0e2e2aa0 --- /dev/null +++ b/challenge-5/submissions/korranat9/solution-template.go @@ -0,0 +1,58 @@ +package main + +import ( + "fmt" + "net/http" +) + +const validToken = "secret" + +// AuthMiddleware checks the "X-Auth-Token" header. +// If it's "secret", call the next handler. +// Otherwise, respond with 401 Unauthorized. +func AuthMiddleware(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // TODO: Implement the logic: + // 1) Grab the "X-Auth-Token" header + token := r.Header.Get("X-Auth-Token") + + // 2) Compare against validToken + if token != validToken{ + http.Error(w, "", 401) + return + } + // 3) If mismatch or missing, respond with 401 + // 4) Otherwise pass to next handler + next.ServeHTTP(w, r) + }) +} + +// helloHandler returns "Hello!" on GET /hello +func helloHandler(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, "Hello!") +} + +// secureHandler returns "You are authorized!" on GET /secure +func secureHandler(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, "You are authorized!") +} + +// SetupServer configures the HTTP routes with the authentication middleware. +func SetupServer() http.Handler { + mux := http.NewServeMux() + + // Public route: /hello (no auth required) + mux.HandleFunc("/hello", helloHandler) + + // Secure route: /secure + // Wrap with AuthMiddleware + secureRoute := http.HandlerFunc(secureHandler) + mux.Handle("/secure", AuthMiddleware(secureRoute)) + + return mux +} + +func main() { + // Optional: you can run a real server for local testing + // http.ListenAndServe(":8080", SetupServer()) +} diff --git a/challenge-6/submissions/korranat9/solution-template.go b/challenge-6/submissions/korranat9/solution-template.go new file mode 100644 index 00000000..8c920d6e --- /dev/null +++ b/challenge-6/submissions/korranat9/solution-template.go @@ -0,0 +1,44 @@ +// Package challenge6 contains the solution for Challenge 6. +package challenge6 + +import ( + // Add any necessary imports here + "strings" + "regexp" +) + +// CountWordFrequency takes a string containing multiple words and returns +// a map where each key is a word and the value is the number of times that +// word appears in the string. The comparison is case-insensitive. +// +// Words are defined as sequences of letters and digits. +// All words are converted to lowercase before counting. +// All punctuation, spaces, and other non-alphanumeric characters are ignored. +// +// For example: +// Input: "The quick brown fox jumps over the lazy dog." +// Output: map[string]int{"the": 2, "quick": 1, "brown": 1, "fox": 1, "jumps": 1, "over": 1, "lazy": 1, "dog": 1} +func CountWordFrequency(text string) map[string]int { + // Your implementation here + re := regexp.MustCompile(`[^a-zA-Z0-9 ]+`) + text = strings.ReplaceAll(text, "\n", " ") + text = strings.ReplaceAll(text, "\t", " ") + text = strings.ReplaceAll(text, "'", "") + cleaned := re.ReplaceAllString(text, " ") + wordFrequency := make(map[string]int) + words := strings.Split(cleaned, " ") + for _, word := range words{ + lowercase := strings.ToLower(word) + if len(lowercase) > 0{ + _, exists := wordFrequency[lowercase] + if exists { + wordFrequency[lowercase] += 1 + }else{ + wordFrequency[lowercase] = 1 + } + } + + } + + return wordFrequency +} \ No newline at end of file diff --git a/challenge-7/submissions/korranat9/solution-template.go b/challenge-7/submissions/korranat9/solution-template.go new file mode 100644 index 00000000..e7328d08 --- /dev/null +++ b/challenge-7/submissions/korranat9/solution-template.go @@ -0,0 +1,164 @@ +// Package challenge7 contains the solution for Challenge 7: Bank Account with Error Handling. +package challenge7 + +import ( + "sync" + "fmt" + // Add any other necessary imports +) + +// BankAccount represents a bank account with balance management and minimum balance requirements. +type BankAccount struct { + ID string + Owner string + Balance float64 + MinBalance float64 + mu sync.Mutex // For thread safety +} + +// Constants for account operations +const ( + MaxTransactionAmount = 10000.0 // Example limit for deposits/withdrawals +) + +// Custom error types + +// AccountError is a general error type for bank account operations. +type AccountError struct { + // Implement this error type + Message string +} + +func (e *AccountError) Error() string { + // Implement error message + return fmt.Sprintf("Account error: %s", e.Message) +} + +// InsufficientFundsError occurs when a withdrawal or transfer would bring the balance below minimum. +type InsufficientFundsError struct { + // Implement this error type + Required float64 + Available float64 +} + +func (e *InsufficientFundsError) Error() string { + // Implement error message + return fmt.Sprintf("Insufficient funds: need at least %.2f, but only %.2f is available", e.Required, e.Available) + +} + +// NegativeAmountError occurs when an amount for deposit, withdrawal, or transfer is negative. +type NegativeAmountError struct { + // Implement this error type + Amount float64 +} + +func (e *NegativeAmountError) Error() string { + // Implement error message + return fmt.Sprintf("Negative amount error: %.2f is not a valid amount", e.Amount) +} + +// ExceedsLimitError occurs when a deposit or withdrawal amount exceeds the defined limit. +type ExceedsLimitError struct { + // Implement this error type + Amount float64 + Limit float64 +} + +func (e *ExceedsLimitError) Error() string { + // Implement error message + return fmt.Sprintf("Amount %.2f exceeds transaction limit of %.2f", e.Amount, e.Limit) +} + +// NewBankAccount creates a new bank account with the given parameters. +// It returns an error if any of the parameters are invalid. +func NewBankAccount(id, owner string, initialBalance, minBalance float64) (*BankAccount, error) { + // Implement account creation with validation + if id == "" || owner == ""{ + return nil, &AccountError{"ID and Owner must not be empty"} + } + + + if initialBalance < 0 { + return nil, &NegativeAmountError{Amount: initialBalance} + } + if minBalance < 0 { + return nil, &NegativeAmountError{Amount: minBalance} + } + if initialBalance < minBalance { + return nil, &InsufficientFundsError{Required: minBalance,Available: initialBalance} + } + + return &BankAccount{ + ID: id, + Owner: owner, + Balance: initialBalance, + MinBalance: minBalance, + }, nil +} + +// Deposit adds the specified amount to the account balance. +// It returns an error if the amount is invalid or exceeds the transaction limit. +func (a *BankAccount) Deposit(amount float64) error { + // Implement deposit functionality with proper error handling + if amount < 0 { + return &NegativeAmountError{Amount: amount} + } + if amount > MaxTransactionAmount { + return &ExceedsLimitError{Amount: amount, Limit: MaxTransactionAmount} + } + a.mu.Lock() + defer a.mu.Unlock() + a.Balance += amount + return nil +} + +// Withdraw removes the specified amount from the account balance. +// It returns an error if the amount is invalid, exceeds the transaction limit, +// or would bring the balance below the minimum required balance. +func (a *BankAccount) Withdraw(amount float64) error { + // Implement withdrawal functionality with proper error handling + if amount < 0 { + return &NegativeAmountError{Amount: amount} + } + if amount > MaxTransactionAmount { + return &ExceedsLimitError{Amount: amount, Limit: MaxTransactionAmount} + } + + a.mu.Lock() + defer a.mu.Unlock() + + if a.Balance-amount < a.MinBalance { + return &InsufficientFundsError{ + Required: a.MinBalance, + Available: a.Balance - amount, + } + } + + a.Balance -= amount + return nil +} + +// Transfer moves the specified amount from this account to the target account. +// It returns an error if the amount is invalid, exceeds the transaction limit, +// or would bring the balance below the minimum required balance. +func (a *BankAccount) Transfer(amount float64, target *BankAccount) error { + // Implement transfer functionality with proper error handling + if a == target { + return &AccountError{"Cannot transfer to the same account"} + } + + // Try to withdraw first + if err := a.Withdraw(amount); err != nil { + return err + } + + // If withdraw succeeded, deposit to target + if err := target.Deposit(amount); err != nil { + // Rollback the withdrawal if deposit failed + a.Deposit(amount) + return err + } + + return nil +} \ No newline at end of file