Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8592623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:47:42+00:00 2026-06-11T23:47:42+00:00

A friend is doing an online Scala course and shared this. # Write a

  • 0

A friend is doing an online Scala course and shared this.

# Write a recursive function that counts how many different ways you can make
# change for an amount, given a list of coin denominations. For example, there
# are 3 ways to give change for 4 if you have coins with denomiation 1 and 2:
# 1+1+1+1, 1+1+2, 2+2.

If you are attending and still working on a solution, don’t read this!

(disclaimer: even if my Python solution may be wrong, I don’t want to influence your thinking if you are on the course, one way or the other! I guess it is the thinking that goes into it that yields learning, not just the “solving”…)


That aside…

I thought I’d have a go at it in Python as I don’t have the Scala chops for it (I am not on the course myself, just interested in learning Python and Java and welcome “drills” to practice on).

Here’s my solution, which I’d like to port to Java using as compact a notation as possible:


def show_change(money, coins, total, combo):
  if total == money:
    print combo, '=', money
    return 1
  if total > money or len(coins) == 0:
    return 0
  c = coins[0]
  return (show_change(money, coins, total + c, combo + [c]) +
    show_change(money, coins[1:], total, combo))

def make_change(money, coins):
  if money == 0 or len(coins) == 0:
    return 0
  return show_change(money, list(set(coins)), 0, [])

def main():
  print make_change(4, [2, 1])

if __name__ == '__main__':
  main()

Question

  • How compact can I make the above in Java, allowing the use of libraries external to the JDK if they help?

I tried doing the porting myself but it was getting very verbose and I thought the usual “there must be a better way of doing this”!

Here my attempt:


import java.util.ArrayList;
import java.util.List;
import com.google.common.collect.Lists;
import com.google.common.primitives.Ints;
public class MakeChange {
  static int makeChange(int money, int[] coins) {
    if (money == 0 || coins.length == 0) {
      return 0;
    }
    return showChange(money, Ints.asList(coins), 0, new ArrayList<Integer>());
  }
  static int showChange(int money, List<Integer> coins, int total,
      List<Integer> combo) {
    if (total == money) {
      System.out.printf("%s = %d%n", combo, total);
      return 1;
    }
    if (total > money || coins.isEmpty()) {
      return 0;
    }
    int c = coins.get(0);
    List<Integer> comboWithC = Lists.newArrayList(combo);
    comboWithC.add(c);
    return (showChange(money, coins, total + c, comboWithC) + showChange(money,
        coins.subList(1, coins.size()), total, combo));
  }
  public static void main(String[] args) {
    System.out.println(makeChange(4, new int[] { 1, 2 }));
  }
}

Specifically, what I dislike a lot is having to do the stuff below just to pass a copy of the list with an element appended to it:

    List<Integer> comboWithC = Lists.newArrayList(combo);
    comboWithC.add(c);

Please show me how compact and readable Java can be. I am still a beginner in both languages…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T23:47:43+00:00Added an answer on June 11, 2026 at 11:47 pm

    Really, almost everything you’re doing here is directly convertible to Java, without much extra verbosity.

    For example:

    def make_change(money, coins):
      if money == 0 or len(coins) == 0: return 0
      return calculate_change(money, list(set(coins)), 0)
    

    The obvious Java equivalent is:

    public static int make_change(int money, int coins[]) {
      if (money == 0 || coins.length == 0) return 0;
      return calculate_change(money, coins, 0);
    }
    

    A few extra words here and there, an extra line because of the closing brace, and of course the explicit types… but beyond that, there’s no big change.

    Of course a more Python and Javariffic (what is the equivalent word?) version would be:

    def make_change(money, coins):
      if money == 0 or len(coins) == 0: 
        return 0
      return calculate_change(money, list(set(coins)), 0)
    

    The obvious Java equivalent is:

    public static int make_change(int money, int coins[]) {
      if (money == 0 || coins.length == 0) {
        return 0;
      }
      return calculate_change(money, coins, 0);
    }
    

    So, Java gets one extra closing brace plus a few chars of whitespace; still not a big deal.

    Putting the whole thing inside a class, and turning main into a method, adds about 3 more lines. Initializing an explicit array variable instead of using [2, 1] as a literal is 1 more. And System.out.println is a few characters longer than print, and length is 3 characters longer than len, and each comment takes two characters // instead of one #. But I doubt any of that is what you’re worried about.

    Ultimately, there’s a total of one line that’s tricky:

    return (calculate_change(money, coins, total + c, combo + [c]) +
        calculate_change(money, coins[1:], total, combo))
    

    A Java int coins[] doesn’t have any way to say “give me a new array with the tail of the current one”. The easiest solution is to pass an extra start parameter, so:

    public static int calculate_change(int money, int coins[], int start, int total) {
        if (total == money) {
            return 1;
        }
        if (total > money || coins.length == start) {
            return 0;
        }
        return calculate_change(money, coins, 0, total + coins[start]) +
            calculate_change(money, coins, start + 1 total);
    }
    

    In fact, nearly everything can be trivially converted to C; you just need to pass yet another param for the length of the array, because you can’t calculate it at runtime as in Java or Python.

    The one line you’re complaining about is an interesting point that’s worth putting a bit more thought into. In Python, you’ve got (in effect):

    comboWithC = combo + [c]
    

    With Java’s List, this is:

    List<Integer> comboWithC = Lists.newArrayList(combo);
    comboWithC.add(c);
    

    This is more verbose. But that’s intentional. Java List<> is not meant to be used this way. For small lists, copying everything around is no big deal, but for big lists, it can be a huge performance penalty. Python’s list was designed around the assumption that most of the time, you’re dealing with small lists, and copying them around is perfectly fine, so it should be trivial to write. Java’s List was designed around the assumption that sometimes, you’re dealing with huge lists, and copying them around is a very bad idea, so your code should make it clear that you really want to do that.

    The ideal solution would be to either use an algorithm that didn’t need to copy lists around, or to find a data structure that was designed to be copied that way. For example, in Lisp or Haskell, the default list type is perfect for this kind of algorithm, and there are about 69105 recipes for “Lisp-style lists in Java” or “Java cons” that you should be able to find online. (Of course you could also just write your a trivial wrapper around List that added an “addToCopy” method like Python’s __add__, but that’s probably not the right answer; you want to write idiomatic Java, or why use Java instead of one of the many other JVM languages?)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing this site for a friend http://www.kidsmartnyc.com/ and there is an enormous
I have this question in my online QUIZ since i am doing my degree
I'm doing this facebook comments for a friend and I can't get any info
My friend gave me a database file: record.mdf . I copied that .mdf file
A friend show me this sample code to implement HTTP POST in C# and
My friend done this below coding for custom control <a href=javascript:__doPostBack('id','msg');>click</a> now i want
a friend and I develop a web application for conferences management that uses hibernate
I am not a ColdFusion coder. Doing a favor for a friend who ported
I have a friend that wants to incrementally move data from one db to
I was helping a friend to write some Java code, who doesn't know a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.