Talk:Next highest int from digits: Difference between revisions

From Rosetta Code
Content added Content deleted
Line 43: Line 43:
''Given'' ? What does that mean here ? [[User:Hout|Hout]] ([[User talk:Hout|talk]]) 16:45, 21 February 2020 (UTC)
''Given'' ? What does that mean here ? [[User:Hout|Hout]] ([[User talk:Hout|talk]]) 16:45, 21 February 2020 (UTC)


I think you may be asking us to find the smallest integer larger than the (positive or zero) integer N which can be obtained by reordering the (decimal base) digits of N ? [[User:Hout|Hout]] ([[User talk:Hout|talk]]) 16:54, 21 February 2020 (UTC)
I think you may be asking us to find the smallest integer larger than the (positive or zero) integer N which can be obtained by reordering the (base 10) digits of N ? [[User:Hout|Hout]] ([[User talk:Hout|talk]]) 16:54, 21 February 2020 (UTC)

Revision as of 16:57, 21 February 2020

Task development

I got the bare question from an SO question My original attempt at Algorithm1 failed as it relied of Pythons permutation generator generationg in sorted order from initially sorted digits - this only worked if there were no duplicated digits.

After fixing and manual testing I started on the second algorithm and used the following kind of loop to find errors: <lang python> for i in range(1_000):

       x = randint(10_000_000, 99_999_999)
       assert nexthigh1(x) == nexthigh(x), f"{x:>10} -> {nexthigh1(x):>10}, {nexthigh(x):>10}"

</lang>

My original closest_more_than() function was quite long, but explicit: <lang python>def closest_more_than2(n, lst):

   "(index of), closest int to digit n from lst that is > n"
   i, dif = None, None
   n = int(n)
   for j, val in enumerate(int(x) for x in lst):
       if val > n:
           inew, difnew = j, val - n
           if difnew == 1:
               return inew
           elif dif is None or dif > difnew:
               i, dif = inew, difnew
   return i

</lang> I had fun updating it, but needed the random test framework to flush out errors.
--Paddy3118 (talk) 11:12, 21 February 2020 (UTC)

Next_permutation

Someone on Reddit r/coding posted a cryptic `std::next_permutation()`. Some searching shows that if it produces the next lexicographic permutation thn it is doing all the heavy lifting! Try this explanation. --Paddy3118 (talk) 11:52, 21 February 2020 (UTC)


Ambiguities in task statement

Perhaps the current phrasing imposes a bit more effort on the reader than it really needs to:

the task is to generate the next largest integer using only the given digits.

So this is in the sense in which Lyon is the next largest town in France after Paris ? We are moving down ?

Using only the the given digits

This is as in "Using only a straight edge and a pair of compasses" ? Given ? What does that mean here ? Hout (talk) 16:45, 21 February 2020 (UTC)

I think you may be asking us to find the smallest integer larger than the (positive or zero) integer N which can be obtained by reordering the (base 10) digits of N ? Hout (talk) 16:54, 21 February 2020 (UTC)