4 Replies - 233 Views - Last Post: 19 July 2019 - 06:20 AM Rate Topic: -----

#1 lychee123   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 19-July 19

Function to return the minimal next N-digit number with the same sum

Posted 19 July 2019 - 04:34 AM

Hello I am trying to understand this coding challenge

 write a function in Python which receives a natural decimal N-digit number
(0<N<15) as an input and returns the minimal next N-digit number with the same sum
of digits or -1 if there isn't one.
Examples:
Input: Output:
123 132
0200 1001
09999999999999 18999999999999
90 -1
9999 -1



I am unsure what exactly the question is asking, can someone clarify and shed light/ I don't understand the part about next minimal

N digit number.

Is This A Good Question/Topic? 1
  • +

Replies To: Function to return the minimal next N-digit number with the same sum

#2 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6813
  • View blog
  • Posts: 28,189
  • Joined: 12-December 12

Re: Function to return the minimal next N-digit number with the same sum

Posted 19 July 2019 - 04:53 AM

Take the example 123. The sum of these digits is 6. Starting from the next number, 124, find the next number that also has digits which sum to 6.

124 is not correct, because their sum is 7. Progressing in this way you should discover the first available number is 132.
Was This Post Helpful? 0
  • +
  • -

#3 lychee123   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 19-July 19

Re: Function to return the minimal next N-digit number with the same sum

Posted 19 July 2019 - 05:23 AM

View Postandrewsw, on 19 July 2019 - 04:53 AM, said:

Take the example 123. The sum of these digits is 6. Starting from the next number, 124, find the next number that also has digits which sum to 6.

124 is not correct, because their sum is 7. Progressing in this way you should discover the first available number is 132.



Hi Thanks for the reply.

I did understand the part about sum but if I follow what your saying, that means

I wont ever get to 132

because

124 = 7
133 = 7
223 = 7


I think this would be impossible to code, cause it does not specify if you only change just one of the numbers, and keep the rest the same.
Was This Post Helpful? 0
  • +
  • -

#4 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2918
  • View blog
  • Posts: 11,335
  • Joined: 03-December 12

Re: Function to return the minimal next N-digit number with the same sum

Posted 19 July 2019 - 05:41 AM

View Postlychee123, on 19 July 2019 - 07:23 AM, said:

I wont ever get to 132

because

124 = 7
133 = 7
223 = 7


I think this would be impossible to code



Quote

or -1 if there isn't one.

Was This Post Helpful? 0
  • +
  • -

#5 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6813
  • View blog
  • Posts: 28,189
  • Joined: 12-December 12

Re: Function to return the minimal next N-digit number with the same sum

Posted 19 July 2019 - 06:20 AM

View Postlychee123, on 19 July 2019 - 12:23 PM, said:

Hi Thanks for the reply.

I did understand the part about sum but if I follow what your saying, that means

I wont ever get to 132

because

124 = 7
133 = 7
223 = 7


I think this would be impossible to code, cause it does not specify if you only change just one of the numbers, and keep the rest the same.

I don't follow this.

You would check all three digit numbers after 123, so 124,125,126,etc. You will reach 132 and realise that it sums to 6, so stop. If you reach 999 and haven't found a solution then you would also stop.

Quote

I think this would be impossible to code

Does that seem likely...? The more likely case is that you haven't yet fully understood the question.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1