10e notation used with variables?
Solution 1
10e+4
is a notation for 10 * 10^4
, not an operation. You have to use the power-operator:
low = 10 ** (N+1)
high = 10 ** (N+2)
Solution 2
Something like 10e3
is a float literal. You could create it as a string and then use float()
to convert it to a number (and int(float())
if you wanted to convert that number to an int):
>>> N = raw_input()
3
>>> float("10e"+N)
10000.0
>>> #compare:
>>> 10e3
10000.0
It is probably better to use the answer of @Daniel, but the above seems closer to what you were trying to do with int(10e+(N))
since you were explicitly asking about literals which depend on variables.
Related videos on Youtube
Nishit Mengar
Updated on July 09, 2022Comments
-
Nishit Mengar almost 2 years
I want to know how can I use the 10eX notation in python 2.7.9 with a variable. In terms of literals 10eX gives (10^X).00000(floating point number). I want to use some variable instead of literal, however, and it does not work. What syntactical change should I make if it is possible to do so or is there any other way to do so? Thanks in advance!
T = int(raw_input()) while T: N = int(raw_input()) LIS = map(int,raw_input().split()) num_lis, num = []*N, []*N low = int(10e+(N)) high = int(10e+(N+1)) temp, count = 0, 0 for i in xrange(low,high): num_lis = [1]*N temp = i while temp!=0: r = temp%10 num[high-1-i] = r temp=temp/10 for p in xrange[1,N]: for q in xrange(0,p): if num[q]<num[p]: if num_lis[p]<(num_lis[q]+1): num_lis[p]=num_lis[q]+1 if LIS[p]!=num_lis[p]: break else: count++ print count T-=1
On running the interpreter I get error for- 10e(N) : Invalid Syntax
-
Willem Van Onsem over 7 yearsMind that
10eX
is not equal to10^X
, but10*10^X
.
-
-
Jean-François Fabre over 7 yearsnot to mention that using
int(math.pow(10,N))
leads to floating point inaccuracy when N is high. -
Jean-François Fabre over 7 yearsdoing this with a big N leads to floating point inaccuracy.
int(10E+30) = 9999999999999999635896294965248
-
John Coleman over 7 yearsI don't really recommend this, but they asked what syntactical change they would have to make to be able to create literals of the requisite form with a variable exponent. Answer: construct it as a string.