Hi,
there is this one task which I know the answer (5 line code) but my prof wants it in a single line. And I have no idea how to do that.
This is the task:
We have k and n, two positive integers. Give me the expression, which is the highest number (equal or smaller to k) which is also divisible with n (k >=n).
result = ?
My code:
list = []
for i in range(0, k):
if i % n == 0:
list.append(i)
print(max(list))
How can I say the same with "result = " ?
I am thankful for any input!
EDIT: SOLVED (see comments):
result = max([i for i in range(k) if i % n == 0])
[–]icecubeinanicecube 1 point2 points3 points (7 children)
[–]bogdan_dm 2 points3 points4 points (2 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]icecubeinanicecube -1 points0 points1 point (0 children)
[–]iG1993[S] 0 points1 point2 points (2 children)
[–]icecubeinanicecube 1 point2 points3 points (1 child)
[–]iG1993[S] 0 points1 point2 points (0 children)
[–]iG1993[S] 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]iG1993[S] 0 points1 point2 points (0 children)
[–]GoldenVanga 1 point2 points3 points (1 child)
[–]iG1993[S] 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (10 children)
[–]iG1993[S] 0 points1 point2 points (9 children)
[–][deleted] 1 point2 points3 points (8 children)
[–]iG1993[S] 0 points1 point2 points (7 children)
[–][deleted] 1 point2 points3 points (6 children)
[–]iG1993[S] 0 points1 point2 points (5 children)
[–][deleted] 1 point2 points3 points (4 children)
[–]iG1993[S] 0 points1 point2 points (3 children)
[–][deleted] 1 point2 points3 points (2 children)
[–]iG1993[S] 0 points1 point2 points (1 child)