-
-
Notifications
You must be signed in to change notification settings - Fork 690
Add solution for Challenge 1 #646
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 all commits
ddc5964
827008c
014571d
0b356cc
1d016e6
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,25 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| ) | ||
|
|
||
| func main() { | ||
| var a, b int | ||
| // Read two integers from standard input | ||
| _, err := fmt.Scanf("%d, %d", &a, &b) | ||
| if err != nil { | ||
| fmt.Println("Error reading input:", err) | ||
| return | ||
| } | ||
|
|
||
| // Call the Sum function and print the result | ||
| result := Sum(a, b) | ||
| fmt.Println(result) | ||
| } | ||
|
|
||
| // Sum returns the sum of a and b. | ||
| func Sum(a int, b int) int { | ||
| // TODO: Implement the function | ||
| return a+b | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "math" | ||
| ) | ||
|
|
||
| func main() { | ||
| // Example usage | ||
| celsius := 25.0 | ||
| fahrenheit := CelsiusToFahrenheit(celsius) | ||
| fmt.Printf("%.2f°C is equal to %.2f°F\n", celsius, fahrenheit) | ||
|
|
||
| fahrenheit = 68.0 | ||
| celsius = FahrenheitToCelsius(fahrenheit) | ||
| fmt.Printf("%.2f°F is equal to %.2f°C\n", fahrenheit, celsius) | ||
| } | ||
|
|
||
| // CelsiusToFahrenheit converts a temperature from Celsius to Fahrenheit | ||
| // Formula: F = C × 9/5 + 32 | ||
| func CelsiusToFahrenheit(celsius float64) float64 { | ||
| c := celsius | ||
| return Round(c*1.8+32, 2) | ||
| } | ||
|
|
||
| // FahrenheitToCelsius converts a temperature from Fahrenheit to Celsius | ||
| // Formula: C = (F - 32) × 5/9 | ||
| func FahrenheitToCelsius(fahrenheit float64) float64 { | ||
| f := (fahrenheit - 32) * 5 / 9 | ||
| return Round(f, 2) | ||
| } | ||
|
|
||
| // Round rounds a float64 value to the specified number of decimal places | ||
| func Round(value float64, decimals int) float64 { | ||
| precision := math.Pow10(decimals) | ||
| return math.Round(value*precision) / precision | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "fmt" | ||
| "os" | ||
| ) | ||
|
|
||
| func main() { | ||
| // Read input from standard input | ||
| scanner := bufio.NewScanner(os.Stdin) | ||
| if scanner.Scan() { | ||
| input := scanner.Text() | ||
|
|
||
| // Call the ReverseString function | ||
| output := ReverseString(input) | ||
|
|
||
| // Print the result | ||
| fmt.Println(output) | ||
| } | ||
| } | ||
|
|
||
| // ReverseString returns the reversed string of s. | ||
| func ReverseString(s string) (result string) { | ||
| for _, c := range s{ | ||
| result = string(c) + result | ||
| } | ||
| return result | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| ) | ||
|
|
||
| func main() { | ||
| // Standard U.S. coin denominations in cents | ||
| denominations := []int{1, 5, 10, 25, 50} | ||
|
|
||
| // Test amounts | ||
| amounts := []int{87, 42, 99, 33, 7} | ||
|
|
||
| for _, amount := range amounts { | ||
| // Find minimum number of coins | ||
| minCoins := MinCoins(amount, denominations) | ||
|
|
||
| // Find coin combination | ||
| coinCombo := CoinCombination(amount, denominations) | ||
|
|
||
| // Print results | ||
| fmt.Printf("Amount: %d cents\n", amount) | ||
| fmt.Printf("Minimum coins needed: %d\n", minCoins) | ||
| fmt.Printf("Coin combination: %v\n", coinCombo) | ||
| fmt.Println("---------------------------") | ||
| } | ||
| } | ||
|
|
||
| // MinCoins returns the minimum number of coins needed to make the given amount. | ||
| // If the amount cannot be made with the given denominations, return -1. | ||
| func MinCoins(amount int, denominations []int) int { | ||
| var l int | ||
| for i := len(denominations); i > 0; i-- { | ||
| for amount-denominations[i-1] >= 0 { | ||
| amount = amount - denominations[i-1] | ||
| l++ | ||
| } | ||
| } | ||
| if amount < 0 { | ||
| return -1 | ||
| } else if amount > 0 { | ||
| return -1 | ||
| } | ||
| return l | ||
| } | ||
|
|
||
| // CoinCombination returns a map with the specific combination of coins that gives | ||
| // the minimum number. The keys are coin denominations and values are the number of | ||
| // coins used for each denomination. | ||
| // If the amount cannot be made with the given denominations, return an empty map. | ||
| func CoinCombination(amount int, denominations []int) map[int]int { | ||
| coinCombination := make(map[int]int) | ||
| for i := len(denominations); i > 0; i-- { | ||
| for amount-denominations[i-1] >= 0 { | ||
| amount = amount - denominations[i-1] | ||
| coinCombination[denominations[i-1]]++ | ||
| } | ||
| } | ||
| if amount < 0 { | ||
| combination := make(map[int]int) | ||
| return combination | ||
| } | ||
| return coinCombination | ||
| } | ||
|
Comment on lines
+51
to
+64
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same greedy algorithm limitation applies. This function has the same greedy algorithm issue as Consider extracting the shared greedy logic into a helper function, or having func MinCoins(amount int, denominations []int) int {
combo := CoinCombination(amount, denominations)
total := 0
for _, count := range combo {
total += count
}
if len(combo) == 0 && amount > 0 {
return -1
}
return total
} |
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -15,23 +15,38 @@ type Manager struct { | |||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // AddEmployee adds a new employee to the manager's list. | ||||||||||||||||||||||||||||||||||||||||||||||
| func (m *Manager) AddEmployee(e Employee) { | ||||||||||||||||||||||||||||||||||||||||||||||
| // TODO: Implement this method | ||||||||||||||||||||||||||||||||||||||||||||||
| m.Employees = append(m.Employees, e) | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // RemoveEmployee removes an employee by ID from the manager's list. | ||||||||||||||||||||||||||||||||||||||||||||||
| func (m *Manager) RemoveEmployee(id int) { | ||||||||||||||||||||||||||||||||||||||||||||||
| // TODO: Implement this method | ||||||||||||||||||||||||||||||||||||||||||||||
| var removeValue []Employee | ||||||||||||||||||||||||||||||||||||||||||||||
| for i := 0; i < len(m.Employees); i++ { | ||||||||||||||||||||||||||||||||||||||||||||||
| if m.Employees[i].ID == id { | ||||||||||||||||||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| removeValue = append(removeValue, m.Employees[i]) | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| m.Employees = removeValue | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
22
to
32
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix the slice assignment inside the loop. Line 30 assigns Apply this diff to move the assignment outside the loop and simplify the logic: func (m *Manager) RemoveEmployee(id int) {
var removeValue []Employee
for i := 0; i < len(m.Employees); i++ {
if m.Employees[i].ID == id {
continue
- } else {
- removeValue = append(removeValue, m.Employees[i])
}
- m.Employees = removeValue
+ removeValue = append(removeValue, m.Employees[i])
}
+ m.Employees = removeValue
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // GetAverageSalary calculates the average salary of all employees. | ||||||||||||||||||||||||||||||||||||||||||||||
| func (m *Manager) GetAverageSalary() float64 { | ||||||||||||||||||||||||||||||||||||||||||||||
| // TODO: Implement this method | ||||||||||||||||||||||||||||||||||||||||||||||
| return 0 | ||||||||||||||||||||||||||||||||||||||||||||||
| var aveSal float64 | ||||||||||||||||||||||||||||||||||||||||||||||
| for i := 0; i < len(m.Employees); i++ { | ||||||||||||||||||||||||||||||||||||||||||||||
| aveSal += m.Employees[i].Salary | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| return aveSal / float64(len(m.Employees)) | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
35
to
41
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add division by zero check. If there are no employees, line 40 will panic due to division by zero. Apply this diff to add a guard: func (m *Manager) GetAverageSalary() float64 {
+ if len(m.Employees) == 0 {
+ return 0
+ }
var aveSal float64
for i := 0; i < len(m.Employees); i++ {
aveSal += m.Employees[i].Salary
}
return aveSal / float64(len(m.Employees))
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // FindEmployeeByID finds and returns an employee by their ID. | ||||||||||||||||||||||||||||||||||||||||||||||
| func (m *Manager) FindEmployeeByID(id int) *Employee { | ||||||||||||||||||||||||||||||||||||||||||||||
| // TODO: Implement this method | ||||||||||||||||||||||||||||||||||||||||||||||
| for i := 0; i < len(m.Employees); i++ { | ||||||||||||||||||||||||||||||||||||||||||||||
| if m.Employees[i].ID == id { | ||||||||||||||||||||||||||||||||||||||||||||||
| return &m.Employees[i] | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
44
to
51
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid returning pointers to slice elements. Line 47 returns Apply this diff to return a copy: -func (m *Manager) FindEmployeeByID(id int) *Employee {
+func (m *Manager) FindEmployeeByID(id int) (Employee, bool) {
for i := 0; i < len(m.Employees); i++ {
if m.Employees[i].ID == id {
- return &m.Employees[i]
+ return m.Employees[i], true
}
}
- return nil
+ return Employee{}, false
}Then update the caller in main: - employee := manager.FindEmployeeByID(2)
+ employee, found := manager.FindEmployeeByID(2)
fmt.Printf("Average Salary: %f\n", averageSalary)
- if employee != nil {
- fmt.Printf("Employee found: %+v\n", *employee)
+ if found {
+ fmt.Printf("Employee found: %+v\n", employee)
}
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package main | ||
|
|
||
| import "fmt" | ||
|
|
||
| type Employee struct { | ||
| ID int | ||
| Name string | ||
| Age int | ||
| Salary float64 | ||
| } | ||
|
|
||
| type Manager struct { | ||
| Employees []Employee | ||
| } | ||
|
|
||
| // AddEmployee adds a new employee to the manager's list. | ||
| func (m *Manager) AddEmployee(e Employee) { | ||
| m.Employees = append(m.Employees, e) | ||
| } | ||
|
|
||
| // RemoveEmployee removes an employee by ID from the manager's list. | ||
| func (m *Manager) RemoveEmployee(id int) { | ||
| var removeValue []Employee | ||
| for i := 0; i < len(m.Employees); i++ { | ||
| if m.Employees[i].ID == id { | ||
| continue | ||
| } else { | ||
| removeValue = append(removeValue, m.Employees[i]) | ||
| } | ||
| m.Employees = removeValue | ||
| } | ||
| } | ||
|
Comment on lines
+22
to
+32
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Critical bug: assignment inside loop causes incorrect removal. Line 30 assigns Apply this diff to move the assignment outside the loop: func (m *Manager) RemoveEmployee(id int) {
var removeValue []Employee
for i := 0; i < len(m.Employees); i++ {
if m.Employees[i].ID == id {
continue
- } else {
- removeValue = append(removeValue, m.Employees[i])
}
- m.Employees = removeValue
+ removeValue = append(removeValue, m.Employees[i])
}
+ m.Employees = removeValue
}🤖 Prompt for AI Agents |
||
|
|
||
| // GetAverageSalary calculates the average salary of all employees. | ||
| func (m *Manager) GetAverageSalary() float64 { | ||
| if len(m.Employees) == 0 { | ||
| return 0 | ||
| } | ||
| var aveSal float64 | ||
| for i := 0; i < len(m.Employees); i++ { | ||
| aveSal += m.Employees[i].Salary | ||
| } | ||
| return aveSal / float64(len(m.Employees)) | ||
| } | ||
|
|
||
| // FindEmployeeByID finds and returns an employee by their ID. | ||
| func (m *Manager) FindEmployeeByID(id int) *Employee { | ||
| for i := 0; i < len(m.Employees); i++ { | ||
| if m.Employees[i].ID == id { | ||
| return &m.Employees[i] | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func main() { | ||
| manager := Manager{} | ||
| manager.AddEmployee(Employee{ID: 1, Name: "Alice", Age: 30, Salary: 70000}) | ||
| manager.AddEmployee(Employee{ID: 2, Name: "Bob", Age: 25, Salary: 65000}) | ||
| manager.RemoveEmployee(1) | ||
| averageSalary := manager.GetAverageSalary() | ||
| employee := manager.FindEmployeeByID(2) | ||
|
|
||
| fmt.Printf("Average Salary: %f\n", averageSalary) | ||
| if employee != nil { | ||
| fmt.Printf("Employee found: %+v\n", *employee) | ||
| } | ||
| } | ||
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.
Remove the TODO comment or the implementation.
The TODO comment indicates the function needs to be implemented, but the implementation already exists. This creates confusion about whether the function is complete.
Additionally, consider using the more idiomatic Go function signature style:
🤖 Prompt for AI Agents