Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python range with Uneven gap [closed]

Tags:

Today I had a python exam where following question was asked:

Given the following code extract, Complete the code so the output is: 10 7 5.

nums = list (range (?,?,?))

print(nums)

How is it possible to get such output in python using range function?

like image 701
Anish Sapkota Avatar asked Jun 23 '20 01:06

Anish Sapkota


2 Answers

not sure if this answer the question, provided we can fill in any syntax to the ? as long it produce the result.

  • 1st ? = 10
  • 2nd ? = 4
  • 3rd ? = -3))+(([5]
# nums = list(range(   ?  ,  ?  ,      ?       ))
nums   = list(range(  10  ,  4  ,  -3))+(([5]  ))
print(nums)
# nums = [10,7,5]
like image 68
Skycc Avatar answered Oct 13 '22 02:10

Skycc


There is no sane way to get the result required. The meat of the problem is that the built in range is strict about its inputs and its definition. It only accepts integers. The only way to get the required answer is to override one of the built ins. But you could override any of them.

range = lambda x, y, z: [10, 7, 5]
list = lambda x: [10, 7, 5]
print = lambda x: sys.stdout.write([10, 7, 5])

On a scale of C# minor what's your favorite color? Mine's triangle.

like image 40
kojiro Avatar answered Oct 13 '22 00:10

kojiro