Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change length of operation depending on a value

Tags:

Context

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:

  • The product of all 2 pairs would be 2*3 = 6, 3*4=12, 4*5=20, 5*6=30 etc...
  • The product of all 3 pairs would be 2*3*4=24,3*4*5=60, 4*5*6=120 etc...

I have completed most of the solution to the problem, but I cannot create an operation that can adapt for any value of n

Code

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

Operation for different n values

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?

like image 246
Newbie101 Avatar asked Feb 21 '19 13:02

Newbie101


People also ask

How do you assign a length in SAS?

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.

How do I change the length of a numeric variable in SAS?

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.

How do I reduce the length of a variable in SAS?

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.

What is iterative calculation in Excel?

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.


1 Answers

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
like image 78
Eduardo Soares Avatar answered Oct 11 '22 17:10

Eduardo Soares