0

In my program the user enters some encrypted text, for my example the encrypted text is: ,@AK ,=PL /ADD x= {F;JQHL=< ghi p_

This is a very basic program before any one says its low level encryption .. i know!

Anyway it converts the encrypted text to ascii code so it looks like this:

print(convert)
[111, 37, 38, 48, 32, 111, 34, 53, 49, 32, 114, 38, 41, 41, 32, 93, 34, 32, 96, 43, 126, 47, 54, 45, 49, 34, 33, 32, 76, 77, 78, 32, 85, 68]

I need some help though, i need each number in that list to be subtracted from 27 then if the result is less than 33 add 94 then print the numbers as they were just with that sum completed how do i do this?

Anton Protopopov
  • 30,354
  • 12
  • 88
  • 93
ThePlacid
  • 43
  • 8
  • Can you simplify your requirement? Couldn't quite get it – Ahsanul Haque Dec 05 '15 at 20:41
  • What does this mean? *print the numbers as they were just with that sum completed* – Mike Müller Dec 05 '15 at 20:41
  • You can `map` a function to your list. Eg: `def cube(x): return x*x*x` map(cube, convert) – salomonvh Dec 05 '15 at 20:42
  • I think you can figure it out from here [1] [1]: http://stackoverflow.com/questions/4406389/if-else-in-a-list-comprehension – gamda Dec 05 '15 at 20:42
  • so for each character in "convert" needs to be subtracted from 27 then if the result is less the 33 it needs to add 94 then print the list as it is, just after the sum is done. So like the first character in the list is 111, the program needs to subtract 27 , the result isnt less than 33 so the if statement wont be needed – ThePlacid Dec 05 '15 at 20:43

4 Answers4

1

Use list comprehension, it's simpler and neat. You can do it like this but I suggest that you read something about it to understand what's going on.

convert = [111, 37, 38, 48, 32, 111, 34, 53, 49, 32, 114, 38, 41, 41, 32, 93, 34, 32, 96, 43, 126, 47, 54, 45, 49, 34, 33, 32, 76, 77, 78, 32, 85, 68]

convert = [x - 27 if x - 27 >= 33 else x - 27 + 94 for x in convert]
print(convert)

Result: [84, 104, 105, 115, 99, 84, 101, 120, 116, 99, 87, 105, 108, 108, 99, 66, 101, 99, 69, 110, 99, 114, 121, 112, 116, 101, 100, 99, 49, 50, 51, 99, 58, 41]

left the x - 27 + 94 for you to understand what I did.

Check out list comprehension here: list comprehension

Lidia Freitas
  • 334
  • 1
  • 2
  • 9
0
    list = [111, 37, 38, 48, 32, 111, 34, 53, 49, 32, 114, 38, 41, 41, 32, 93, 34, 32, 96, 43, 126, 47, 54, 45, 49, 34, 33, 32, 76, 77, 78, 32, 85, 68]
    for n in range(len(list)):
        list[n] -= 27
        if(list[n] < 33):
            list[n] += 94
    print(list)

this should do the work

0

Just map a function that performs the desired operation to the list:

94 -27 = 67 -> What you must sum in case the condition is true...

>>> lst = [111, 37, 38, 48, 32, 111, 34, 53, 49, 32, 114, 38, 41, 41, 32, 93, 34, 32, 96, 43, 126, 47, 54, 45, 49, 34, 33, 32, 76, 77, 78, 32, 85, 68]
>>>>print map(lambda x: x + 67 if x-27 < 33 else x, lst)

[205, 131, 132, 142, 126, 205, 128, 147, 143, 126, 208, 132, 135, 135, 126, 187, 128, 126, 190, 137, 220, 141, 148, 139, 143, 128, 127, 126, 170, 171, 172, 126, 179, 162]
Netwave
  • 40,134
  • 6
  • 50
  • 93
0

IIUC you could try that, where l is you list:

l = [111, 37, 38, 48, 32, 111, 34, 53, 49, 32, 114, 38, 41, 41, 32, 93, 34, 32, 96, 43, 126, 47, 54, 45, 49, 34, 33, 32, 76, 77, 78, 32, 85, 68]

l1 = [i + 67 if (i - 27) < 33 else i - 27 for i in l]

In [8]: l1
Out[8]: 
[84, 104, 105, 115, 99, 84, 101, 120, 116, 99, 87, 105, 108, 108, 99, 66, 101, 99, 69, 110, 99, 114, 121, 112, 116, 101, 100, 99, 49, 50, 51, 99, 58, 41]
Anton Protopopov
  • 30,354
  • 12
  • 88
  • 93