views:

71

answers:

1

Hi all, I have never used ruby but need to translate this code to java.

Can anyone help me.

This is the code in Ruby.

DEFAULT_PRIOR = [2, 2, 2, 2, 2]
## input is a five-element array of integers
## output is a score between 1.0 and 5.0
def score votes, prior=DEFAULT_PRIOR
posterior = votes.zip(prior).map { |a, b| a + b }
sum = posterior.inject { |a, b| a + b }
posterior.
map.with_index { |v, i| (i + 1) * v }.
inject { |a, b| a + b }.
to_f / sum
end

I obtained it from here so maybe some clues can be found there. its about calculating averages

How to rank products based on user input

This was the solution. In case anyone needs it

    static final int[] DEFAULT_PRIOR = {2, 2, 2, 2, 2};

static float  score(int[] votes) {
    return score(votes, DEFAULT_PRIOR);
}

private static float score(int[] votes, int[] prior) {

    int[] posterior = new int[votes.length];
    for (int i = 0; i < votes.length; i++) {
        posterior[i] = votes[i] + prior[i];
    }
    int sum = 0;
    for (int i = 0; i < posterior.length; i++) {
        sum = sum + posterior[i];
    }

    float sumPlusOne = 0;
    for (int i = 0; i < posterior.length; i++) {
        sumPlusOne = sumPlusOne + (posterior[i] * (i + 1));
    }
    return sumPlusOne / sum;
}
+3  A: 

The code does the following things:

It set a constant named DEFAULT_PRIOR (java equivalent would be a static final variable) to an array containing the number 2 five times. In java:

static final int[] DEFAULT_PRIOR = {2,2,2,2,2};

It defines a two-argument method named score where the second argument defaults to DEFAULT_PRIOR. In java this can be achieved with overloading:

float score(int[] votes) {
    return score(votes, DEFAULT_PRIOR);
}

Inside the score method, it does the following:

posterior = votes.zip(prior).map { |a, b| a + b }

This creates an array posterior, where each element is the sum of the corresponding element in votes and the corresponding element in prior (i.e. posterior[i] = votes[i] + prior[i]).

sum = posterior.inject { |a, b| a + b }

This sets sum to be the sum of all elements in posterior.

posterior.
map.with_index { |v, i| (i + 1) * v }.
inject { |a, b| a + b }.
to_f / sum

This bit multiplies each element in posterior with its index plus one and then sums that. The result is converted to a float and then divided by sum.

In java you would iterate over posterior with for-loop (not foreach) and in each iteration add (i + 1) * posterior[i] (where i is the index of the for-loop) to a variable tmp, which you set to 0 before the loop. Then you would cast tmp to float and divide it by sum.

sepp2k
thanks for that. Can u show how the last bit looks like in java? I'm lost trying to write that part. Thank you for the hep anyway. very much appreciated
Farouk Alhassan
@Farouk: Added one paragraph at the end.
sepp2k
I think it may be misleading that you say its static.
Woot4Moo
@Woot4Moo: Why? Ruby constants *are* static both in the sense that they're per-class, not per-instance, and in the sense that they're statically looked-up.
sepp2k
umm thats not at all what static means. In Java static means that the object is created at compile time instead of run time. Also that you don't need to instantiate the class to utilize it
Woot4Moo
@Woot4Moo: Ehrm, no, it doesn't mean that it's created at compile time.
sepp2k
http://mindprod.com/jgloss/static.html
Woot4Moo
@Woot4Moo: http://pastie.org/1242937 As you can see, that code compiles and prints "Foo constructor called" (at run-time). So obviously the object is created at run-time.
sepp2k
@Woot4Moo: Where on that page does it say the objects are created at compile-time?
sepp2k
`The word static is used in a second context, the opposite of dynamic or runtime type. This refers to the compile-time declared type of a variable, compared with the run time actual type it points to. e.g. a Dog variable may point to a Dalmatian object, but not vice versa. The static type (the type of the reference) is Dog and the dynamic type (the type of the object) is Dalmatian. You will often hear Java referred to a language with static type checking. The types of all references are checked for consistency at compile time. ` The second definition
Woot4Moo
Heyy guys, thanks for the help. almost made it but need one last help. What is the variable v for in (i + 1) * v
Farouk Alhassan
@Woot4Moo: The definition you just quoted has a) nothing to do with the `static` keyword and b) nothing to do with time of evaluation (note that it's talking about types, not values).
sepp2k
@Farouk: That `v` should have been `posterior[i]`.
sepp2k
@sepp2k until someone can provide conclusive evidence that it is not my definition that is what I will continue to say.
Woot4Moo
THANK YOU Very MUCH...It works............. if you are intereted in the code, i can post it somewhere
Farouk Alhassan
@Woot4Moo: My pastie wasn't conclusive enough for you?
sepp2k
Based on your suggestions, this is the output. IS it correct?run:average of [2, 2, 2, 2, 2]=3.0average of [5, 0, 0, 0, 0]=2.3333333average of [0, 0, 0, 0, 100]=4.818182
Farouk Alhassan
@Farouk: Yes, that's the same results as you get from the ruby code.
sepp2k
@posted the solution to the original question. Thanks guys. Really grateful
Farouk Alhassan
not sure I should be replying but I feel the urge to make it clear. @Woot4Moo in the article whose link you provided it clearly says "They are allocated when the class is loaded." hope this is clear enough. :)
João Portela
Fair enough. I'll have to inform my old cs professor. Thanks
Woot4Moo