Reputation: 43
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?
Upvotes: 0
Views: 119
Reputation: 31692
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]
Upvotes: 0
Reputation: 334
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
Upvotes: 1
Reputation: 1
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
Upvotes: 0
Reputation: 42796
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]
Upvotes: 0