• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Product sum Problem

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

This is the problem statement.

Write a function that takes in a special array and return its product sum. A special array is a non empty array that contains either integers or other special arrays. The product sum of a special array is the sum of its elements where special arrays inside it are summed themselves and then multiplied by their level of depth.

For example the product sum of [x,y] is x+y; the product sum of [x,[y,z]] is x+2y+2z

Sample input:[5,2,[7,-1],3,[6,[-13,8],4]]

Sample output:12//calculated as 5+2+2*(7-1)+3+2*(6+3*(-13+8)+4)
 
Suhaas Parekh
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And this is my solution which is obviously not working.

 
Marshal
Posts: 69411
276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I think you can call that statement a problem
I have never heard the term “special array” elsewhere.
I suggest you write down the problem in your own words, and then write down the algorithm you would use. Make sure everything is written very simply.
 
Marshal
Posts: 25594
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks to me like you have "depth * " in the wrong place.
 
Bartender
Posts: 3959
155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have this snippet in your testcase:

The first element (Arrays.asList) is NOT an instance of ArrayList and so your ProductSum will throw an Exception, for you are trying to do: depth * Arrays.asList;
You might as well work with the raw List-type, instead of (Array)List<Object>, saves some typing.
 
Piet Souris
Bartender
Posts: 3959
155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way: in your opening post you gave an example of text input for your special array. Do you have a parser to transform this into a SpecialArray?
 
Suhaas Parekh
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I modified the code to below but still cannot get the test case to pass. Someone please help me

 
Campbell Ritchie
Marshal
Posts: 69411
276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stop guessing and write down what the algorithm should be. I don't think your if‑else on line 55 is correct. I also don't think you can readily get it to work in one method.
 
Piet Souris
Bartender
Posts: 3959
155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I told you what was wrong. Unfortunately, your latest ProductSum is not an improvement.

In ProductSum you ask whether an element is an instanceof ArrayList. But in TestCase1 you do this:

so the first element in List 'test' is an 'Arrays.asList', which is NOT an instanceof ArrayList. Therefore, in your ProductSum that element ends up in the wromg branch.

So, you either make sure that only ArrayLists are allowed as List in your testcases, or you should test for something else.
 
Saloon Keeper
Posts: 7175
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet, I think that all we care about are Lists and whether they are ArrayLists or come from asList() doesn't matter. I cut and pasted this into Eclipse and it works fine.
 
Campbell Ritchie
Marshal
Posts: 69411
276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, actually, the specification said array, so Lists oughtn't to count.
 
Carey Brown
Saloon Keeper
Posts: 7175
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suhaas, as already mentioned, you have messed up the logic at line 55.

Some points to simplify your code:
  • 'sum' can be a local variable, it doesn't need to be a parameter.
  • You can use an enhanced for() loop: for( Object o : array ), this eliminates the need for get(i).
  • You can start depth at 0 outside of productSum() and then increment it as soon as you get inside productSum(), in this way the depth is '1' the first time through. Of course doing this eliminates the need for the '+1' in the recursive call.
  •  
    Carey Brown
    Saloon Keeper
    Posts: 7175
    65
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:Well, actually, the specification said array, so Lists oughtn't to count.


    You're right. My bad.

    P.S. It also says "function", so...
     
    Piet Souris
    Bartender
    Posts: 3959
    155
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Carey Brown wrote:Piet, I think that all we care about are Lists and whether they are ArrayLists or come from asList() doesn't matter. I cut and pasted this into Eclipse and it works fine.


    You are right.

    @OP
    I apologize. I misread your testcase1, and I tested with:

    And what I said about testing for something else, I meant simply:

    then you can use all sorts of Lists.

    Now, if you gents will excuse me, I go looking for my donkey ears hat.
     
    Carey Brown
    Saloon Keeper
    Posts: 7175
    65
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
     
    It wasn't my idea to go to some crazy nightclub in the middle of nowhere. I just wanted to stay home and cuddle with this tiny ad:
    Devious Experiments for a Truly Passive Greenhouse!
    https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
      Bookmark Topic Watch Topic
    • New Topic