I'm working on modernizing Rosetta Code's infrastructure. Starting with communications. Please accept this time-limited open invite to RC's Slack.. --Michael Mol (talk) 20:59, 30 May 2020 (UTC)

Talk:Next highest int from digits

From Rosetta Code

Task development[edit]

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:

    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}"
 

My original closest_more_than() function was quite long, but explicit:

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
 

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[edit]

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

Ambiguities in task statement[edit]

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)

Thanks Hout. I've added your phrasing to. Hopefully between them people will be able to more easily comprehend. Ta. --Paddy3118 (talk) 17:13, 21 February 2020 (UTC)

Using Permutations[edit]

Don't sort the permutations and then search for the specified condition. Map the permutations subtracting the original value. Reduce the Mapping to the smallest result greater than 0. If None return 0 else return the original value plus the final result.--Nigel Galloway (talk) 14:46, 22 February 2020 (UTC)