[Challenge] Number Permutation

Basic and intermediate levels.

The program takes two arguments:
z - the input number
max_length - the maximum number of unique elements which sum should give z value

I decided to combine both basic and intermediate solutions in one function to make it easier to read.

3 Likes

I’m sorry it was an input error, now it’s working propertly :grinning:

1 Like

There are no issues regarding your code. I tried typing a number on the console, and got the correct output. Good job. Happy Coding! :smiley:

1 Like

Awesome code! Keep it up!:grinning:

2 Likes

Solution to the intermediate level…but I need help in limiting the number of digits to 5…all suggestions are welcome.

2 Likes

I hope my comments are good enough to explain the code.
The only major difference between the two is the range of the for loop inside the recursion function.

Changing the function to return only the number of permutations/combinations instead of a list of all possible permutations/combinations would be very simple.

Code

C++ version of the same code: link

3 Likes

Hey all, for your answer to be considered the winner we need to see two solutions, 1 for the basic version and 1 for the intermediate version, both refined to be as efficient as possible. Preferably put both solutions in one repl.it file called makeNumberBasic and makeNumber .

Also, a combination of just one digit is valid. I.e. the ways to make 2 are : 1+1 & 2 Or for the intermediate difficulty just 2.

And some notes so far:
Firstly, every one of these solutions is a very nice implementation! I’m just posting ways it could be made even better…


@gargol12 your code needs to:[quote=“danieloduffy, post:1, topic:85775”]
limit NumberMaker(z) to the use of five digits
[/quote]

you only use three.


@smreia there is quite a lot of redundant code in your solution, For example when you check n1 != n2 this is already checked in the if block above it, perhaps have another look through to make it more efficient.


@fredswed your code includes 0:


@rd0122 if you’re looking for efficiency, there’s a few places you can neaten up the code. E.g.

nums = [1, 2, 3, 4, 5, 6, 7, 8, 9]
av_nums = [x for x in nums if x < z]

can be turned into:
av_nums = [x for x in range (1,10) if x < z]

And you do a couple of slow calculations multiple times instead of using some temp variables. On the other hand you also use temp variables where they’re not needed. If you look over your code there’s one place that you could add an extra line rather than having 6 lines at the bottom of your code.

Your code will be a really really great solution if you make some of these changes :slight_smile:


@shrinkhla same as a few above your code allows for 0. Also please use repl.it , if you wish your code to be considered when looking for the winner :slight_smile:


@lmreia it seems like your recursion checks too many different inputs. If you can cut this down this would be a very good contender for best solution. E.g. for 3 your program runs the inside of that for loop 78 times. Whereas you only really need to check:

1 - small
1 + 1 - small
1 + 2 - correct break
2 - small
3 - correct break

So in an optimum world it would only run the middle of the for loop 5 times. Also, could you edit your code so it assumes a max of 5 possible digits, as stated in the challenge description, or so it has a max of x digits.


Hope this helps everyone, don’t forget the two key points at the start!!


8 Likes

Thanks a lot for the help and guidance, really appreciate it :relaxed:
I’ve made changes in my code an linked them to repl.it .

2 Likes

Okej. So I am working on maximize the value of terms to nine and also getting rid of zeros so 2+4+0+0+0 will be just 2+4.

2 Likes

Thanks for the advice. I have made some changes in my code.

2 Likes

Thank you for the advice, I’ll edit my code so it fits your considerations.

Update: The new version of the code it’s up! :smiley:

3 Likes

Thanks for great feedback! I will try to work on this :slight_smile:

Update:
I’ve edited my code!

2 Likes

Thanks for the useful observations.
I tried to optmize the code as you said and also created a basic version the challenge.

2 Likes

Prints the total number + list of all possible permutations - combinations in the same function both basic & intermediate solution.

2 Likes

Python 3, basic only. I think this works… My Function allows a second optional parameter to specify how many digits you want to permutate. This qualifies as a pretty brute force algorithm, I think, but I had fun sort of “animating” the results and I thought it was kind of interesting to be able to study them.

1 Like

The output says “undefined”

1 Like

You have to call the function eg. makeNumber(6) to print the results but if this is inconvenient i 'll include an example in the code.

1 Like

I’d like a clarification, please:
When you say “You may limit makeNumberBasic(z) to the use of five digits,” do you mean the use of five unique digits (i.e. 1+2+3+4+5 but not 1+2+3+4+5+6) or the use of five digits at all (i.e. the same digit times five but not the same digit times six)?

1 Like

At most 5 digits. So you can make the number x using 1,2,3,4 or 5 digit combinations.

1 Like

Here’s my attempt at this challenge. I managed to code both the basic and intermediate diffculties, hope they are correct :slight_smile:

1 Like