7

I do like the way I can treat lists in Python. It does any recursion solution to look easy and clean. For instance the typical problem of getting all the permutations of elements in a list, in Python looks like:

def permutation_recursion(numbers,sol):
    if not numbers:
        print "this is a permutation", sol
    for i in range(len(numbers)):
        permutation_recursion(numbers[:i] + numbers[i+1:], sol + [numbers[i]])

def get_permutations(numbers):
    permutation_recursion(numbers,list())

if __name__ == "__main__":
    get_permutations([1,2,3])

I do like the way I can simple get new instances of modified lists by doing things like numbers[:i] + numbers[i+1:] or sol + [numbers[i]]

If I try to code exactly the same in Java, it looks like:

import java.util.ArrayList;
import java.util.Arrays;

class rec {
    static void permutation_recursion(ArrayList<Integer> numbers, ArrayList<Integer> sol) {
       if (numbers.size() == 0)
            System.out.println("permutation="+Arrays.toString(sol.toArray()));
       for(int i=0;i<numbers.size();i++) {
             int n = numbers.get(i);

             ArrayList<Integer> remaining = new ArrayList<Integer>(numbers);
             remaining.remove(i);

             ArrayList<Integer> sol_rec = new ArrayList<Integer>(sol);
             sol_rec.add(n);

             permutation_recursion(remaining,sol_rec);
       }
    }
    static void get_permutation(ArrayList<Integer> numbers) {
        permutation_recursion(numbers,new ArrayList<Integer>());
    }
    public static void main(String args[]) {
        Integer[] numbers = {1,2,3};
        get_permutation(new ArrayList<Integer>(Arrays.asList(numbers)));
    }
}

To create the same recursion I need to do :

ArrayList<Integer> remaining = new ArrayList<Integer>(numbers);
remaining.remove(i);

ArrayList<Integer> sol_rec = new ArrayList<Integer>(sol);
sol_rec.add(n);

Which is quite ugly and it gets worse for more complex solutions. Like in this example

So my question is ... are there any buil-in operators or helper functions in the Java API that would make this solution more "Pythonic" ?

Community
  • 1
  • 1
Manuel Salvadores
  • 16,287
  • 5
  • 37
  • 56

5 Answers5

8

No.

But this is why Martin Odersky created Scala. He's even said that one of his goals for Scala is that it be the Python of the Java world. Scala compiles to Java bytecode and easily interops with Java compiled classes.

If that's not an option, you could take a look at the Commons Collection Library.

sblundy
  • 60,628
  • 22
  • 121
  • 123
2

You can use the clone() function on Lists to get a shallow copy of them. That way you won’t have to instantiate a new Object yourself but can just use the copy.

ArrayList<Integer> remaining = remaining.clone().remove(i);

Other than that, no, java does not have such operators for Lists.

Kissaki
  • 8,810
  • 5
  • 40
  • 42
1

Apache Commons solves a lot of these kinds of issues. Have a look at ArrayUtils to do slicing. Java doesn't have a lot of syntactic sugar like scripting languages do for various reasons.

jiggy
  • 3,828
  • 1
  • 25
  • 40
1

Different languages require different styles. Trying to accomplish mylist[:i] + mylist[i+1:] in java is like using a hammer with a screw. Yes, you can do it, but it's not very tidy. I believe the equivalent might be something like ArrayList temp = new ArrayList(list); temp.remove(index);

I believe the following accomplishes the same task, but does so in a slightly different fashion, but doesn't suffer readability issues. Instead of creating a new list, it modifies the list, passes it on, and returns the list to its previous state when the recursive call returns.

import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;

public class Permutation {

   public static void main(String[] args) {

      List<List<Integer>> result = permutations(
                                       Arrays.asList( 
                                          new Integer[] {1,2,3}));

      for (List<Integer> permutation : result) {
         System.out.println(permutation);  
      }
   }


   public static <T> List<List<T>> permutations(List<T> input) {
      List<List<T>> out = new ArrayList<List<T>>();
      permutationsSlave(input, new ArrayList<T>(), out);
      return out;
   }

   public static <T> void permutationsSlave(List<T> input, 
            ArrayList<T> permutation, List<List<T>> result) {

      if (input.size() == chosen.size()) {
         result.add(new ArrayList<T>(permutation));
         return;
      }

      for (T obj : input) {
         if (!permutation.contains(obj)) {
            permutation.add(obj);
            permutationsSlave(input, permutation, result);
            permutation.remove(permutation.size()-1);
         }
      } 

   }
}

The python way may look easy and cleaner, but ability to look clean often hides the fact that the solution is quite inefficient (for each level of recursion it creates 5 new lists).

But then my own solution isn't terribly efficient either -- instead of creating multiple new objects it performs redundant comparisons (though some of this could be alleviated by use of accumulators).

Dunes
  • 37,291
  • 7
  • 81
  • 97
0

Hi 1 you can use stack, which will be more handy.

2 the for loop can be written like this : for(Number n:numbers)

benzen
  • 6,204
  • 4
  • 25
  • 37