How to reduce Python code 3?

Asked

Viewed 201 times

-3

a simple algorithm to read an X integer and print the next 6 odd ones (including the odd X)

X = int(input())
if X % 2 == 1:
    print(X)
    X+=2
    for i in range(5):
        if X % 2 == 1:
            print(X)
            X+=2
else: 
    X+=1
    for i in range(6):
        if X % 2 == 1:
            print(X)
            X+=2

How can I reduce code (Python 3)?

  • This problem is set right? Because I can’t imagine why it should be so complicated.

  • If you’ve already guaranteed that x is odd (adding 1 when it’s even, for example), and is increasing by 2 by 2, there’s no point testing again inside the for

  • 1

    Not questioning the content of the answer accepted (I myself upvoted the answer) and not questioning choice, but I saw that you marked how you accept all the answers by clicking on the " " of all of them. See in how and why to accept an answer, the operation of the response acceptance tool. In summary you can vote on all answers but must accept only one.

4 answers

6

I will not worry about failure if the person does not enter a valid number.

You have to change the value of x to the next unit if it is not odd, otherwise it should not change, then the increment should be conditional.

The range() is very powerful and you can set the sequence right there. You can put what should be the beginning number of the sequence, already properly guaranteed that it is an odd, then what is the number that should stop, so as you want 6 and only should pick the odd is that same number that already has more 12, and should indicate that you should jump from 2 to 2, as can be seen in the function documentation linked.

Nor can you forget that if you want to print the values of the sequence you have to take the loop variable and no longer the x which was used to give parameters for the sequence to be generated, it is not the individual item of each step that will vary, so we took the i.

You can make it easier, but I don’t think this is the time to learn these things.

x = int(input())
if x % 2 == 0:
    x += 1
for i in range(x, x + 12, 2):
    print(i)

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

4


If ficar pequeno is premise

X = int(input("Digite um número inteiro: "))
print([o+X for o in list(range(12)) if ((o+X) % 2) == 1])

Update: I used list comprehension. If you want to know more about list comprehension see here in the Python documentation - item 5.1.3

However in the @Maniero response the loop is smaller, consuming less time for processing...

Update 2

Small and efficient:

X = int(input('Digite um número inteiro: '))
print([2 * o + 1 for o in range((X - X % 2) // 2, ((X - X % 2) // 2) + 6)])

I hope it helps

3

As already mentioned in other answers this code does not bother to affirm the validity or not of the user input.

n = int(input('Digite um número inteiro: '))    
g = (n - n % 2 ) // 2                         #Calcula a geratriz de n usando sua paridade.
print([2 * i + 1 for i in range(g, g + 6)])   #Computa os seis próximos ímpares usando a geratriz.

Test this example on Repl.it

Some concepts to help understand the algorithm:

Parity

Parity is a notion applied to the set of integers where any integer is said to be even if being divided by the number two results in an integer, otherwise that number is said to be odd. Being able to affirm:

  • for a pair the rest of your entire division by 2 will always be 0.
  • for an odd the rest of your entire division by 2 will always be 1.

Pairs and Odd Sets

The set of even numbers is the set of integers divisible by two, i.e., the division by two is closed in the set of integers. This set is described by the function:

nₚ = 2 * n | nZ
the even number is equal twice the number n such that n belongs to the integers

In turn, the set of odd numbers is the set of integers not divisible by two, i.e., the division by two is opened in the set of integers. This set is described by the function:

nₗ = 2 * n + 1 | nZ
the odd number is equal to twice n plus 1 such that n belongs to the integers

Geratrizes

Geratriz is what generates or gives rise to parity we can say that:

  • The even number generator, the number n that generated nₚ

gₚ = nₚ / 2
even geratriz equals even number divided by 2

  • The odd number generator, the number n that generated nₗ:

gₗ = ( nₗ - 1 ) / 2
the odd geratriz is equal to the odd number minus one divided by 2

If we apply the notion of parity and terms % as the remaining operator of the entire division, we can generalize the generator calculation as follows:

g = ( n - n % 2 ) / 2 | nZ
the parity geratriz of an integer is equal to the division by two of n minus the remainder of its division by 2, such that n belongs to the integers

2

An alternative with Join:

n = int(input())
if n % 2 == 0:
    n +=1
    
' '.join(str(v) for v in range(n, n + 12, 2))

or

n = int(input())
' '.join(str(v) for v in range(n, n + 12) if v % 2 == 1)

Browser other questions tagged

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