Click here to Skip to main content
14,579,188 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello! I'd like to ask how to write this line for j in [c for c in coinValueList if c <= cents]: from Python to Java
i'm a newbie and want to work it out in java as i don't understand how it works in python. Thanx in advance!
the whole code in Python:
def dpMakeChange(coinValueList,change,minCoins,coinsUsed):
  for cents in range(change+1):
     coinCount = cents
     newCoin = 1
     for j in [c for c in coinValueList if c <= cents]:
           if minCoins[cents-j] + 1 < coinCount:
              coinCount = minCoins[cents-j]+1
              newCoin = j
     minCoins[cents] = coinCount
     coinsUsed[cents] = newCoin
  return minCoins[change]

def printCoins(coinsUsed,change):
  coin = change
  while coin > 0:
     thisCoin = coinsUsed[coin]
     print(thisCoin)
     coin = coin - thisCoin

def main():
   amnt = 63
   clist = [1,5,10,21,25]
   coinsUsed = [0]*(amnt+1)
   coinCount = [0]*(amnt+1)

   print("Making change for",amnt,"requires")
   print(dpMakeChange(clist,amnt,coinCount,coinsUsed),"coins")
   print("They are:")
   printCoins(coinsUsed,amnt)
   print("The used list is as follows:")
   print(coinsUsed)

main()


What I have tried:

I've tried this
for (int j : coinValueList) {
              if (j <= 1) {

but apparently it's wrong(
Posted
Updated 26-Jun-20 22:47pm
Rate this:
Please Sign up or sign in to vote.

Solution 1

Quote:
i'm a newbie and want to work it out in java as i don't understand how it works in python.

There is a tool that might help you to understand the code, it is the debugger.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

27.3. pdb — The Python Debugger — Python 3.6.1 documentation[^]
Debugging in Python | Python Conquers The Universe[^]
pdb – Interactive Debugger - Python Module of the Week[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

You first need a new list to capture all the values of coinValueList. Once you have that then you can iterates over those values ...
List<Integer> newlist = new ArrayList<Integer>();
for (int c : coinValueList)
{
    if (c <= cents)
        newlist.add(c)
}
for (int j : newlist)
{
    // loop over the selected values
}


[edit]
If you know Java, then learning Python is not difficult. See The Python Tutorial — Python 3.7.8rc1 documentation[^].
[/edit]
   
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100