Skip to content

Add solution for Challenge 22 by WHFF521#1896

Open
WHFF521 wants to merge 1 commit into
RezaSi:mainfrom
WHFF521:challenge-22-WHFF521
Open

Add solution for Challenge 22 by WHFF521#1896
WHFF521 wants to merge 1 commit into
RezaSi:mainfrom
WHFF521:challenge-22-WHFF521

Conversation

@WHFF521

@WHFF521 WHFF521 commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Challenge 22 Solution

Submitted by: @WHFF521
Challenge: Challenge 22

Description

This PR contains my solution for Challenge 22.

Changes

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

Testing

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

Thank you for reviewing my submission! 🚀

@coderabbitai

coderabbitai Bot commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

Adds greedy MinCoins and CoinCombination functions for U.S. coin denominations, plus a main program that prints results for fixed test amounts.

Changes

Coin-change implementation

Layer / File(s) Summary
Coin-change calculation logic
challenge-22/submissions/WHFF521/solution-template.go
MinCoins calculates greedy coin counts and returns -1 for unformable amounts; CoinCombination returns the corresponding denomination-count map or an empty map.
Command-line demonstration
challenge-22/submissions/WHFF521/solution-template.go
main applies both functions to fixed U.S. denominations and test amounts, then prints the results.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

Poem

A bunny counts coins in a neat little row,
From quarters and dimes to pennies below.
Greedy paws choose the largest with care,
Then print every coin in a tidy affair.
Hop, hop—change solved! 🐇

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title matches the main change: adding WHFF521's Challenge 22 solution.
Description check ✅ Passed The description is directly related to the submitted Challenge 22 solution and its file change.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 golangci-lint (2.12.2)

level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain main module or its selected dependencies"


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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick comments (2)
challenge-22/submissions/WHFF521/solution-template.go (2)

32-47: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Format the code to adhere to Go standards.

The code logic is correct, but it lacks idiomatic Go spacing around operators and loop constructs. Consider applying gofmt to improve readability, and you can also safely remove the intermediate length variable and the TODO comment.

♻️ Proposed formatting
-	// TODO: Implement this function
-	sum := amount
-	var result int
-	length := len(denominations)
-	for i:=length-1;i>=0;i--{
-	    result +=sum/denominations[i]
-	    sum =sum%denominations[i]
-	    if sum==0{
-	        break
-	    }
-	}
-	if sum!=0 {
-	    return -1
-	}
-	return result
+	sum := amount
+	var result int
+	for i := len(denominations) - 1; i >= 0; i-- {
+		result += sum / denominations[i]
+		sum %= denominations[i]
+		if sum == 0 {
+			break
+		}
+	}
+	if sum != 0 {
+		return -1
+	}
+	return result

54-73: 🚀 Performance & Scalability | 🔵 Trivial | ⚡ Quick win

Avoid redundant calculation and format the code.

Currently, this function calls MinCoins to verify if the amount can be made, which duplicates the calculation. You can eliminate the overhead by simply returning an empty map if sum != 0 at the end of the local greedy pass.

Additionally, this snippet could benefit from gofmt spacing.

♻️ Proposed refactor
-	// TODO: Implement this function
-	sum := amount
-	m := make(map[int]int)
-	if MinCoins(amount,denominations) ==-1{
-	    return m
-	}
-	
-	length := len(denominations)
-	for i:=length-1;i>=0;i--{
-	    num := sum/denominations[i]
-	    if num == 0 {
-	        continue
-	    }
-	    m[denominations[i]] = num
-	    sum = sum%denominations[i]
-	    if sum==0{
-	        break
-	    }
-	}
-	return m
+	sum := amount
+	m := make(map[int]int)
+
+	for i := len(denominations) - 1; i >= 0; i-- {
+		num := sum / denominations[i]
+		if num == 0 {
+			continue
+		}
+		m[denominations[i]] = num
+		sum %= denominations[i]
+		if sum == 0 {
+			break
+		}
+	}
+
+	if sum != 0 {
+		return make(map[int]int)
+	}
+
+	return m

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 19de8ca0-9bc7-40b6-9cbd-3edb95a785b2

📥 Commits

Reviewing files that changed from the base of the PR and between d089e35 and 9368dcd.

📒 Files selected for processing (1)
  • challenge-22/submissions/WHFF521/solution-template.go

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.

1 participant