Printing a number odd sequence on a list

Asked

Viewed 2,298 times

3

How do I make my code display the sequence of odd numbers in reverse mode of a number typed by the user:

My code:

number = int (input())

if(number >= 2):
    listseq = list(range(0, number, 1))
    inverselist = listseq[::-1]
    print(inverselist)
  • 5

    Alex, I strongly suggest you read the documentation and seek to understand what the function range makes and what are its parameters. Your solution is practically ready, just need to set the proper values.

  • Brother I think the most appropriate was the filter() I’ll try to rewrite my code

  • 1

    No, just the range does what you want, use the filter will be unnecessary. But try to use it too.

  • and thus only use the range?

  • 2

    You used the range in the code you posted. You didn’t get the result you wanted. Study the function, analyze the values you passed as parameter along with the result you got and see what you did wrong. You literally only need to change 2 values in your code to work. Understanding exactly what you did will know where to fix.

4 answers

4


The signature of range is:

range(start, stop[, step])

If you want to list all odd numbers in the range [0, n], you just have to analyze that the first odd number is 1 and always the next one will be two positions away; in other words, pick the numbers every two starting at 1:

impares = range(start=1, stop=n, step=2)

Therefore, to display them in reverse, simply:

n = int(input('Valor de n: '))

impares = list(range(1, n, 2))

print(impares[::-1])

Thus:

>>> Valor de n: 10
[9, 7, 5, 3, 1]

Inclusive, the value of step can be negative and you generate the sequence already in reverse order. Just remember that the value of start belongs to the range generated by range, then you’ll need to decide if n should appear in the result or not if it is odd.

n = int(input('Valor de n: '))

# Se n é par, inicia em n-1, se ímpar inicia em n-2
n = n-1 if n % 2 == 0 else n-2

print( list(range(start=n, stop=0, step=-2)) )

Thus:

>>> Valor de n: 10
[9, 7, 5, 3, 1]
  • Thanks @Andersoncarloswoss just changed in my structure of range for list(range(1, number, 2)) that solved the problem.

  • The solution is sometimes simple, the more I curl up beautiful.

  • In programming the solution will always be simple. If it becomes complex it is a sign that you are doing something wrong.

2

impares = []
number = int (input())

for numero in range(0,number):
    if(numero % 2 != 0):
        impares.append(numero)

print(impares[::-1])

It is not necessary to use list(), follow this link to understand the use

1

I believe the most efficient way would be to use comprehensilist on (which is a concise way to create and manipulate lists, i.e., it applies the expression on each item in the list) in this way:

number = int (input())
if(number >= 2):
    l = [x for x in range(number) if x%2 != 0]
    print(l[::-1])

This way we have an algorithm stateless at the level of algorithmic complexity is more efficient.

0

To resolve this issue you can use List Comprehension. This way, we can implement a code with only 1 line of code.

In this case the code would be:

print([c for c in range(1, int(input('Digite um número: ')) + 1, 2)][::-1])

When we executed this code we received the following message: Digite um número: . Right now we must enter an integer number and press enter.

At this time the for will travel the range(1, int(input('Type a number: ')) + 1, 2), assemble a list containing only odd numbers and then display such numbers in reverse order.

Browser other questions tagged

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