-3
I wanted to leave this little piece of code in one line ...
i = int(input())
for j in range(1,i+1):
if(i%j==0):
print(j)
it is possible ?
-3
I wanted to leave this little piece of code in one line ...
i = int(input())
for j in range(1,i+1):
if(i%j==0):
print(j)
it is possible ?
6
What you can do is reduce and simplify your code with comprehensilist on:
i = int(input('Calcular divisores de: '))
divisores = [j for j in range(1, i+1) if i % j == 0]
print('Divisores:', divisores)
Less than this will harm the readability and semantics of your code: do not mix the logic of reading the value of the input with the calculation of dividers.
But given his commenting:
wanted to see if I could shorten the execution time of the code ...
I make my own words of the Isac: number of lines has no direct relation to code execution time.
About runtime, I recommend that you open another question detailing exactly what you need to analyze better. It seems that the question you asked here does not reflect exactly what you want to do.
Only missing the "in 1 line" in honor of the codegolf :D
good, helped me a lot ... (=
2
To do this, we will use functions Amble, but you can rest assured it’s something simple.
We can do it separately:
The variable i
we’ll leave for the end.
First, let’s discuss the if
and the print
(lambda x,j: print(j) if x%j == 0 else None)
Then we’ll figure out the cycle for
, where "Func" is written, imagine it is any function
[ Func(xa,ja) for xa in range(1, ja+1)]
Now we need to put the function lambda that we create within the cycle for
compacted. Then only replace "Func" with lambda
[ (lambda x,j: print(j) if x%j == 0 else None)(ja, xa) for xa in range(1, ja+1)]
But we still have to pass our entry argument, i
, the input. Let’s build a new function lambda for such
(lambda ja: [ (lambda x,j: print(j) if x%j == 0 else None)(ja,xa) for xa in range(1, ja+1)])
Now we just need to add one input to her
(lambda ja: [ (lambda x,j: print(j) if x%j == 0 else None)(ja, xa) for xa in range(1, ja+1)])(int(input('Digite uma entrada : ')))
And there, you have it all in one line, if you want to learn more, you can look for "using lambda in python" or "functional programming in python".
Some sources that may help you in your studies:
caraaaa .... very good (=
@Cleefsouza , if any of the answer was the one you are looking for, mark as accepted, to know that it has already been resolved. If not, edit the question with what you need. To help. Flw :)
1
You can use the "one line converter", converts any python code into a single line automatically:
https://onelinepy.herokuapp.com/
Your code stays like this:
(lambda __y, __print, __g: [(lambda __after, __items, __sentinel: __y(lambda __this: lambda: (lambda __i: [(lambda __after: (__print(j), __after())[1] if ((i % j) == 0) else __after())(lambda: __this()) for __g['j'] in [(__i)]][0] if __i is not __sentinel else __after())(next(__items, __sentinel)))())(lambda: None, iter(range(1, (i + 1))), []) for __g['i'] in [(int(input()))]][0])((lambda f: (lambda x: x(x))(lambda y: f(lambda: y(y)()))), __import__('__builtin__', level=0).__dict__['print'], globals())
The interesting thing is that this serves to prove that a line is only silly in python, the language favors legible codes and not number of lines.
Browser other questions tagged python python-3.x
You are not signed in. Login or sign up in order to post.
Have you ever heard of list comprehension?
– Wallace Maxters
An important question: why does it need to be in a line, and the reading of an input value and the generation of the divisor list are two completely different things?
– Woss
@Andersoncarloswoss wanted to see if he could shorten the execution time of the code ...
– Cleef Souza
@Wallacemaxters I’ll look over ...
– Cleef Souza
The number of lines is not at all related to code execution time
– Isac
Write the code in C and reduce the execution time. If the problem is time, and I doubt it is, then for such a simple code I only see this solution. I could speak Assembly, but I doubt anyone can do it faster than in C, and it’s much more at risk.
– Maniero
In whatever programming language you work
sempre
will exist in themínimo
"1"input
and "1"output
. Therefore, this code can be reduced in themáximo
in"2"
lines, what you will achieve using List Comprehesion.– Solkarped