Skip to content

Conversation

@Johrespi
Copy link
Contributor

Challenge 22 Solution

Submitted by: @Johrespi
Challenge: Challenge 22

Description

This PR contains my solution for Challenge 22.

Changes

  • Added solution file to challenge-22/submissions/Johrespi/solution-template.go

Testing

  • Solution passes all test cases
  • Code follows Go best practices

Thank you for reviewing my submission! 🚀

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 31, 2025

Walkthrough

Adds two exported Go functions implementing a greedy coin-change algorithm: MinCoins computes the minimum number of coins needed for a given amount, and CoinCombination returns a map of denominations to coin counts. Includes a main function demonstrating usage.

Changes

Cohort / File(s) Summary
Greedy Coin Change Implementation
challenge-22/submissions/Johrespi/solution-template.go
Added MinCoins(amount int, denominations []int) int to calculate minimum coins using greedy algorithm; added CoinCombination(amount int, denominations []int) map[int]int to return denomination-to-count mapping; added main function with sample usage and output printing

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Verify greedy algorithm correctness and edge case handling (e.g., when exact amount cannot be formed)
  • Confirm return values and error conditions align with function signatures
  • Check consistency between the two similar implementations

Possibly related PRs

Pre-merge checks

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title Check ✅ Passed The title "Add solution for Challenge 22 by Johrespi" directly and clearly corresponds to the main change in the pull request, which adds a solution file containing two exported Go functions for coin change computation. The title is concise, specific (includes challenge number and author), and provides sufficient context for someone scanning the history to understand that this PR adds a Challenge 22 solution. It avoids vague language and accurately represents the primary purpose of the changeset.
Description Check ✅ Passed The pull request description is clearly related to the changeset and provides relevant context about the submission. It identifies the challenge number, the author, describes the specific file being added, and notes testing status. The description accurately reflects the changes present in the PR (the addition of the solution file at challenge-22/submissions/Johrespi/solution-template.go) and is directly tied to the actual modifications made.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d4ef9f0 and cc181c3.

📒 Files selected for processing (1)
  • challenge-22/submissions/Johrespi/solution-template.go (1 hunks)
🔇 Additional comments (2)
challenge-22/submissions/Johrespi/solution-template.go (2)

7-27: LGTM!

The main function provides clear demonstration of both functions with reasonable test cases.


31-52: Remove the algorithmic critique and focus only on valid code quality issues.

This challenge explicitly requires a greedy approach for U.S. coin denominations [1, 5, 10, 25, 50], where it produces optimal results. The challenge documentation already acknowledges that greedy doesn't work for arbitrary denominations but explicitly states: "Your solution should implement the greedy approach, which always chooses the largest coin possible."

The following issues need addressing:

  1. Remove TODO comment on line 32
  2. Fix formatting on line 34: change sum :=0 to sum := 0

The algorithm implementation itself aligns with the challenge requirements. Dynamic programming is not needed for this specific challenge.

Likely an incorrect or invalid review comment.

Comment on lines +58 to +79
func CoinCombination(amount int, denominations []int) map[int]int {
// TODO: Implement this function
c := make(map[int]int)
sum := 0

for i:= len(denominations) - 1; i >= 0; i--{
for{
if sum + denominations[i] <= amount{
sum += denominations[i]
c[denominations[i]]++
} else{
break
}
}
}

if sum != amount {
return map[int]int{}
}

return c
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion | 🟠 Major

Significant code duplication with MinCoins.

This function duplicates the core greedy logic from MinCoins (lines 36-45 vs lines 63-71). The same greedy algorithm issues and input validation concerns apply here.

Consider refactoring to eliminate duplication:

func CoinCombination(amount int, denominations []int) map[int]int {
	combination := make(map[int]int)
	remaining := amount
	
	// Iterate from largest to smallest denomination
	for i := len(denominations) - 1; i >= 0; i-- {
		coin := denominations[i]
		if coin <= remaining {
			count := remaining / coin
			if count > 0 {
				combination[coin] = count
				remaining -= count * coin
			}
		}
	}
	
	if remaining != 0 {
		return map[int]int{}
	}
	
	return combination
}

This refactored version:

  • Uses division instead of nested loops for better efficiency
  • Eliminates the duplicate loop logic
  • More clearly expresses intent
  • Removes the TODO comment (line 59)

Note: The greedy algorithm limitation mentioned in the MinCoins review also applies here.

🤖 Prompt for AI Agents
In challenge-22/submissions/Johrespi/solution-template.go around lines 58 to 79,
CoinCombination duplicates the greedy loop logic from MinCoins and uses
inefficient nested loops; replace the nested loop with a single-pass
division-based greedy approach: iterate denominations from largest to smallest,
compute count := remaining / coin, set combination[coin] = count if >0, subtract
count*coin from remaining, and after the loop return an empty map if remaining
!= 0; remove the TODO comment and consider extracting shared greedy logic into a
helper to eliminate duplication with MinCoins.

@RezaSi RezaSi merged commit aba2924 into RezaSi:main Oct 31, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants