I am trying to create a program that gets the product of all n pairs in a number reading from left to right
For example, in the number 2345678:
I have completed most of the solution to the problem, but I cannot create an operation that can adapt for any value of n
num = 2345678
num = str(num)
n = 2
start_pos = 0
for i in range(start_pos,len(num)):
try:
x += 1
t = int(num[i]) * int(num[i+1]) # hardcoded for n = 2
print(t)
start_pos += 1
except IndexError:
break
n = 2
: t = int(num[i]) * int(num[i+1])
n = 3
: t = int(num[i]) * int(num[i+1]) * int(num[i+2])
n = 4
: t = int(num[i]) * int(num[i+1]) * int(num[i+2]) * int(num[i+3])
n = 5
: t = int(num[i]) * int(num[i+1]) * int(num[i+2]) * int(num[i+3]) * int(num[i+4])
How could I create an operation that can adapt for any value of n?
Truncation of Character Values Therefore, SAS creates Airport with a length of three bytes and uses only the first three characters in the New York observation. LENGTH variable-list $ number-of-bytes; variable-list. specifies the variable or variables to which you are assigning the length number-of-bytes.
For numeric variables, you can change the length of the variable by using a subsequent LENGTH statement. When SAS assigns a value to a character variable, it pads the value with blanks or truncates the value on the right side, if necessary, to make it match the length of the target variable.
If the reason to reduce character variable lengths is to save disk space, then you can do this by re-writing your SAS datasets with the COMPRESS = YES or BINARY SAS option.
Iterative calculations are repeated calculations until a specific numeric condition is met. Iterative calculations help Excel find the solution to formulas by performing the same calculation repeatedly using previous results. By analyzing the previous results, Excel can find the likelihood of possible solutions.
If you want an iterative solution without using any pythonic operator, you can simply add an inner for
to iterate from i
to i+n
, and then accumulate the product of numbers in a variable t
. Like this:
num = 2345678
num = str(num)
n = 3
start_pos = 0
x = 0
for i in range(start_pos,len(num)):
try:
t = 1
for j in range(i, i+n):
t = t * int(num[j])
print(t)
except IndexError:
break
Output:
24
60
120
210
336
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