Stumped by recursive interview prep question via /r/learnprogramming


Stumped by recursive interview prep question

Hi everyone,

I've been prepping for job interviews and came across a question that really stumped me. It's been a while since I've done anything functional programming related so I'm a bit rusty when it comes to writing recursive algorithms.

Here's the problem:

Print the sums of all possible subsets of the numbers, ranging from 0 for containing no numbers to the sum of all the numbers for the whole set. Do this recursively: print all possible sums of the subsets that exclude the first element, then print all possible sums of the subsets that include the first element. Thus the input: 1 2 3 would produce the output: 0 3 2 5 1 4 3 6 

I've attempted this a few times, but can't seem to figure out what to pass in-between each recursive call or what to set for the base case since the recursion is not approaching zero. I'm only posting here because I'm entirely stumped by this problem.

Any hints or help is appreciated!

Submitted July 13, 2017 at 11:21PM by Dave_the_Pigeon
via reddit http://ift.tt/2ukXjjf

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s