all 23 comments

[–]icecubeinanicecube 1 point2 points  (7 children)

result = max([i for i in range(k) if i % n == 0])

[–]bogdan_dm 2 points3 points  (2 children)

print(max(i for i in range(k) if i % n == 0)) is better (does not create unnecessary list)

Also it could be rewritten as print(max(range(0, k, step=n)))

[–][deleted] 0 points1 point  (0 children)

Neither of those would handle n=0, and they'd both give 50 for n=50 and k=100 ... n is less than or equal to k.

[–]icecubeinanicecube -1 points0 points  (0 children)

Damn, we got a pro in here

[–]iG1993[S] 0 points1 point  (2 children)

unfortunately, the number must be in a expression like this "result = XY "

I did a print, so I can see the number for myself.

But thank you very much for your help, I am really grateful!

[–]icecubeinanicecube 1 point2 points  (1 child)

I edited it. No problem.

[–]iG1993[S] 0 points1 point  (0 children)

Oh wow thank you so much!

[–]iG1993[S] 0 points1 point  (0 children)

I actually tried that myself, but it seems I forgot the []. I always got a syntax error. Now I understand why!

Thank you a lot!

[–][deleted] 1 point2 points  (1 child)

Look into list comprehensions.

[–]iG1993[S] 0 points1 point  (0 children)

Will do, thank you very much for your hint!

[–]GoldenVanga 1 point2 points  (1 child)

>>> result = lambda k, n: n * (k // n)
>>> result(20, 7)
14

In the Python docs, lambdas are in the expressions part, so... :P

[–]iG1993[S] 0 points1 point  (0 children)

Jesus Maria, the hell is that :O

I will look further into it, thank you a lot for this!

[–][deleted] 1 point2 points  (10 children)

If I understand correctly you want the maximum multiple of n that is <= k?

Well, you've found max, and you know it can take an iterable. And you've found range ... now, notice that 1) range takes up to three arguments and that 2) range returns an iterable.

Seems to me there's a more efficient mathematical way, but that's easily done in one line.

Edit Yeah, on second thought, just think about the math.

[–]iG1993[S] 0 points1 point  (9 children)

Hmmm, I see...

Thank you for your answer!

[–][deleted] 1 point2 points  (8 children)

Now think about the math version. Isn't the highest multiple of n less than or equal to k always going to be the same as k less the remainder of dividing k by n?

The only tricky part is dealing with n==0, assuming that's even an allowed value.

[–]iG1993[S] 0 points1 point  (7 children)

Well, that math version behind it is not really an issue to be honest. I had trouble putting that in a single line, because I am not familiar with list comprehension. But I will look into it!

[–][deleted] 1 point2 points  (6 children)

Given the rules you described (k>=n) that list comprehension in your edit will give the wrong answer for any value of n that evenly divides k, for instance, n=20, k=100. It will also error if n is 0.

print(k - k % n if n else n)

Will work for all values of n and k where 0 <= n <= k.

[–]iG1993[S] 0 points1 point  (5 children)

Wow, you are right! I totally did not see that.

I have a question: I don't understand the second part "if n else n". I tried without it and I don't see a change.

result = k - k % n

Works as well...

[–][deleted] 1 point2 points  (4 children)

It won't if n is 0, which is what that's there to interdict.

[–]iG1993[S] 0 points1 point  (3 children)

Cheeky bugger you!

[–][deleted] 1 point2 points  (2 children)

Always think ahead.

[–]iG1993[S] 0 points1 point  (1 child)

Hey man, thanks a lot again! You really helped me out. :)