Code works in Java but not Python?

BuggyLemon

This code seems to work in Java, but when I convert it to Python, it exceeds the maximum recursion depth and exits.. Not sure what the difference is. They look like they function identically to me.

Java version:

public String addCommas(String number)
{
  if(number.length < 4
  {
    return number;
  }
  return addCommas(number.subString(0, number.length - 3)) + "," + number.subString(number.length - 3, number.length);
}

Python version:

def addCommas(number):
    number = str(number)
    if len(number) < 4:
        return number
    else:
        return addCommas(number[:len(number) - 3] + ',' + number[len(number) - 3:])

Thanks in advance for any help!

Leon

The difference is in the last line.

 return addCommas(number.subString(0, number.length - 3)) + "," + number.subString(number.length - 3, number.length);

This calls addCommas on the first substring only (which reduces the length of the string parameter for the next call by 3) and then appends a comma and the last three digits to its result.

 return addCommas(number[:len(number) - 3] + ',' + number[len(number) - 3:])

This on the other hand first adds a comma and calls addCommas on the whole new string (which is even longer than the original, resulting in the infinite recursion loop).

 return addCommas(number[:len(number) - 3]) + ',' + number[len(number) - 3:]

This would work as it only calls addCommas on the first substring and adds the commas to the result of addCommas, the same way the Java code does it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Code works in Java but not Python?

From Dev

How this code works in python?

From Dev

Code works for java but doesn't works for Android

From Dev

Python code works in 2.7 but not in 3.5

From Dev

fibonacci works in python but fails in Java

From Dev

jsoup code works in Java but not in Android- Nullpointerexception

From Dev

How the code works after calling a method in java?

From Dev

Check if code works on newer python versions

From Dev

Understanding why this python code works randomly

From Dev

Trying to understand why this Python code works

From Dev

python code works on one file but fails on other

From Dev

Python code not working in CMD but works in IDLE?

From Dev

Check if code works on newer python versions

From Dev

Python code not working in CMD but works in IDLE?

From Dev

Is this Python code equivalent of Java code?

From Dev

Python to Java code conversion

From Dev

How exactly works the Java application exit code of the main() method?

From Java

How to create a Java / Maven project that works in Visual Studio Code?

From Dev

Java script code works only when I place it in the body section

From Dev

Java GUI Design view not showing completely but code works

From Dev

Java - Why code with just {} works and is parsed by virtual machine?

From Dev

Code works in Xamarin Android but doestn't work in Java (HttpPost JSON)

From Dev

How this Java code works? Remove duplicates from an unsorted linked list

From Dev

How to create a Java / Maven project that works in Visual Studio Code?

From Dev

How to create a Java / Maven project that works in Visual Studio Code?

From Dev

How to create a Java / Maven project that works in Visual Studio Code?

From Dev

Mac device access works in C, but equivalent code in Java/JNA not

From Dev

i want to know how Scanner code works in Java

From Dev

Can anyone explain me how this java code works?

Related Related

  1. 1

    Code works in Java but not Python?

  2. 2

    How this code works in python?

  3. 3

    Code works for java but doesn't works for Android

  4. 4

    Python code works in 2.7 but not in 3.5

  5. 5

    fibonacci works in python but fails in Java

  6. 6

    jsoup code works in Java but not in Android- Nullpointerexception

  7. 7

    How the code works after calling a method in java?

  8. 8

    Check if code works on newer python versions

  9. 9

    Understanding why this python code works randomly

  10. 10

    Trying to understand why this Python code works

  11. 11

    python code works on one file but fails on other

  12. 12

    Python code not working in CMD but works in IDLE?

  13. 13

    Check if code works on newer python versions

  14. 14

    Python code not working in CMD but works in IDLE?

  15. 15

    Is this Python code equivalent of Java code?

  16. 16

    Python to Java code conversion

  17. 17

    How exactly works the Java application exit code of the main() method?

  18. 18

    How to create a Java / Maven project that works in Visual Studio Code?

  19. 19

    Java script code works only when I place it in the body section

  20. 20

    Java GUI Design view not showing completely but code works

  21. 21

    Java - Why code with just {} works and is parsed by virtual machine?

  22. 22

    Code works in Xamarin Android but doestn't work in Java (HttpPost JSON)

  23. 23

    How this Java code works? Remove duplicates from an unsorted linked list

  24. 24

    How to create a Java / Maven project that works in Visual Studio Code?

  25. 25

    How to create a Java / Maven project that works in Visual Studio Code?

  26. 26

    How to create a Java / Maven project that works in Visual Studio Code?

  27. 27

    Mac device access works in C, but equivalent code in Java/JNA not

  28. 28

    i want to know how Scanner code works in Java

  29. 29

    Can anyone explain me how this java code works?

HotTag

Archive