How to compress python code 3?

Asked

Viewed 639 times

-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 ?

  • Have you ever heard of list comprehension?

  • 3

    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?

  • @Andersoncarloswoss wanted to see if he could shorten the execution time of the code ...

  • @Wallacemaxters I’ll look over ...

  • 4

    The number of lines is not at all related to code execution time

  • 2

    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.

  • In whatever programming language you work sempre will exist in the mínimo "1" input and "1" output. Therefore, this code can be reduced in the máximo in "2" lines, what you will achieve using List Comprehesion.

Show 2 more comments

3 answers

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.

  • 3

    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.

  • 2

    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.

  1. First, let’s discuss the if and the print

    (lambda x,j: print(j) if x%j == 0 else None)
    
  2. 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)]
    
  3. 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)]
    
  4. 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)])
    
  5. 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

You are not signed in. Login or sign up in order to post.