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?
not sure if this answer the question, provided we can fill in any syntax to the ? as long it produce the result.
10
4
-3))+(([5]
# nums = list(range( ? , ? , ? ))
nums = list(range( 10 , 4 , -3))+(([5] ))
print(nums)
# nums = [10,7,5]
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With