0

I need to calculate every 3 digits of my decimal input. I have a code like this:

decimal = 136462380542525933949347185849942359177

#Encryption
e = 79
n = 3337

def mod(x,y):
        if (x<y):
            return x
        else:
            c = x%y
            return c
    
def enkripsi(m):
    decimalList = [int(i) for i in str(m)]
    print("Decimal List: ", decimalList)    
    cipher = []
    for i in decimalList:
        cipherElement = mod(i**e, n)
        cipher.append(cipherElement)
    return (cipher)

c = enkripsi(decimal)
print("Decimal Encyption: ", c)

The output are:

Decimal List: [1, 3, 6, 4, 6, 2, 3, 8, 0, 5, 4, 2, 5, 2, 5, 9, 3, 3, 9, 4, 9, 3, 4, 7, 1, 8, 5, 8, 4, 9, 9, 4, 2, 3, 5, 9, 1, 7, 7]

Decimal Encryption: [1, 158, 2086, 2497, 2086, 3139, 158, 2807, 0, 270, 2497, 3139, 270, 3139, 270, 1605, 158, 158, 1605, 2497, 1605, 158, 2497, 1254, 1, 2807, 270, 2807, 2497, 1605, 1605, 2497, 3139, 158, 270, 1605, 1, 1254, 1254]

How can i get output Decimal List: [ 136, 462, 380, ...] so that Decimal Encryption: [ 2174, 2504, 3249, ...] ?

brianK
  • 25
  • 7
  • Not clear why you need the mod function over the built-in `%` since it's just basically returning x % y. – DarrylG Apr 14 '21 at 07:45

3 Answers3

2

you're almost there:

def decimal2list(num, length=3):
   string = str(num)
   return [int(string[i:i+length]) for i in range(0, len(string), length)]
aSaffary
  • 793
  • 9
  • 22
1

You can use a slightly more complex list comprehensions:

m_str = str(m)
decimalList = [int(m_str[i:i+3]) for i in range(0,len(m_str),3)]

Alternative solution for the madman, (just for fun, don't actually do this):

import itertools
decimalList = [''.join(i) for i in ((a or '', b or '', c or '') for a,b,c in itertools.zip_longest(m_str[::3], m_str[1::3], m_str[2::3]))]
mousetail
  • 7,009
  • 4
  • 25
  • 45
0

Try implementing with this approach:

def convert(list):

    s = [str(i) for i in list]

    res = int("".join(s))

    return(res)
Shrish Sharma
  • 90
  • 1
  • 1
  • 9