On Jun 28, 2011, at 11:37 PM, David Sletten wrote:

> 
> On Jun 28, 2011, at 8:20 PM, Bhinderwala, Shoeb wrote:
>> The inputs are two arrays of type double of the same length – dailyValues 
>> and totalValues. The output is the array contrib of the same length.
>> 
>> 
>>         int n = dailyValues.length;
>> 
>>         for (int i = 0; i < n; i++)
>> 
>>         {
>> 
>>             sum = 1.0;
>> 
>>             for (int j = i + 1; j < n; j++)
>> 
>>             {
>> 
>>                 sum *= (1.0 + (totalValues[j] / 100.0));
>> 
>>             }
>> 
>>             contrib[i] = sum * dailyValues[i];
>> 
>>         }
>> 
> 
> 1. Are you sure that this does what you want it to?
> 2. What does it do?
> 
> There are two obvious red flags with the code:
> -You have a variable called 'sum' which is really a product.
> -You never use index 0 of totalValues
> 

Assuming that your code is correct, the following reproduces your results:
(defn compute-contrib [daily-values total-values]
  (loop [contrib []
         daily-values daily-values
         total-values total-values]
    (if (empty? daily-values)
      contrib
      (recur (conj contrib (* (first daily-values) (reduce (fn [sum 
total-value] (* sum (+ (/ total-value 100.0) 1.0)))
                                                           1.0
                                                           (rest 
total-values))))
             (rest daily-values)
             (rest total-values)))) )

In the outer loop you are working with each element of dailyValues and 
consecutively smaller subsequences of totalValues. In the inner loop you repeat 
a calculation using each of the remaining elements of totalValues starting with 
a seed value of 1.0.

The Clojure implementation uses 'loop' to traverse each of the input sequences. 
On each iteration we use 'reduce' to do the work of the inner loop (notice that 
'reduce' is working with (rest total-values) rather than simply total-values 
itself, so we discard the first element each time just as your inner loop 
does), multiply that by the current element of interest in daily-values and 
then accumulate the result by conjoining it with our result 'contrib'.

The mental shift that you need to make involves forgetting about the index 
variables i and j and thinking instead about what the loops are accomplishing. 
If an inner loop is collapsing a sequence to a single result as above, then 
'reduce' is what you want to use. Other inner loops might require 'map' or 
'filter' depending on the computation.

Have all good days,
David Sletten




-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to