Home > Mobile >  Receive a number, wrap it and get the corresponding value from keys in a dictionary
Receive a number, wrap it and get the corresponding value from keys in a dictionary

Time:03-10

I made a code to receive pieces of number, manipulate them, wrap it in parts of 3 and return letters based on the keys from a dictionary. However when I try to get the values, my list gets blank.

I think I have a problem with the fact that I am comparing int and string, but I changed the type of the keys and it didn't work anyway.

from textwrap import wrap

def solved(blocks, d, n):
    symbols = {111: '0', 112: '1', 113: '2', 114: '3', 115: '4',
116: '5', 117: '6', 118: '7', 119: '8', 121: '9', 122: '=', 123: ' ',
124: '-', 125: '/', 126: '*', 127: 'a', 128: 'b', 129: 'c', 131: 'd',
132: 'e', 133: 'f', 134: 'g', 135: 'h', 136: 'i', 137: 'j', 138: 'k',
139: 'l', 141: 'm', 142: 'n', 143: 'o', 144: 'p', 145: 'q', 146: 'r',
147: 's', 148: 't', 149: 'u', 151: 'v', 152: 'w', 153: 'x', 154: 'y',
155: 'z', 156: 'á', 157: 'à', 158: 'â', 159: 'ã', 161: 'é', 162: 'ê',
163: 'í', 164: 'ó', 165: 'ô', 166: 'õ', 167: 'ú', 168: 'ç', 169: 'A',
171: 'B', 172: 'C', 173: 'D', 174: 'E', 175: 'F', 176: 'G', 177: 'H',
178: 'I', 179: 'J', 181: 'K', 182: 'L', 183: 'M', 184: 'N', 185: 'O',
186: 'P', 187: 'Q', 188: 'R', 189: 'S', 191: 'T', 192: 'U', 193: 'V',
194: 'W', 195: 'X', 196: 'Y', 197: 'Z', 198: 'Á', 199: 'À', 211: 'Â',
212: 'Ã', 213: 'É', 214: 'Ê', 215: 'Í', 216: 'Ó', 217: 'Ô', 218: 'Õ',
219: 'Ú', 221: 'Ç', 222: ',', 223: '.', 224: '!', 225: '?', 226: ';',
227: ':', 228: '_', 229: '(', 231: ')', 232: '"', 233: '#', 234: '$',
235: '%', 236: '@', 237: ' ', 238: '\n'}
    numbers = []
    for block in blocks:
        number = pow(int(block), d, n)
        numbers.append(number)
    j = ""
    for frag in numbers:
        x = str(frag)
        j  = x
    s = wrap(j, 3)
    q = str(s)
    result = []
    for element in q:
        for key in symbols:
            if key == element:
                result.append(symbols.get(key))
    return q, result

message = [3323713707, 1319300010, 1144229290, 1660290264, 2194193588,
734058623, 2217413390, 3225084774, 781294944, 2728321168,
2631341137, 1922680560, 651540169, 3030894670, 41880099,
300780045, 559287950, 1767066193, 2787757960, 520879703,
1993872416, 1386565567, 2460441503, 2766908703]

CodePudding user response:

The debugger is your best aid. Here's the correct code:

from textwrap import wrap

def solved(blocks, d, n):
    symbols = {111: '0', 112: '1', 113: '2', 114: '3', 115: '4',
116: '5', 117: '6', 118: '7', 119: '8', 121: '9', 122: '=', 123: ' ',
124: '-', 125: '/', 126: '*', 127: 'a', 128: 'b', 129: 'c', 131: 'd',
132: 'e', 133: 'f', 134: 'g', 135: 'h', 136: 'i', 137: 'j', 138: 'k',
139: 'l', 141: 'm', 142: 'n', 143: 'o', 144: 'p', 145: 'q', 146: 'r',
147: 's', 148: 't', 149: 'u', 151: 'v', 152: 'w', 153: 'x', 154: 'y',
155: 'z', 156: 'á', 157: 'à', 158: 'â', 159: 'ã', 161: 'é', 162: 'ê',
163: 'í', 164: 'ó', 165: 'ô', 166: 'õ', 167: 'ú', 168: 'ç', 169: 'A',
171: 'B', 172: 'C', 173: 'D', 174: 'E', 175: 'F', 176: 'G', 177: 'H',
178: 'I', 179: 'J', 181: 'K', 182: 'L', 183: 'M', 184: 'N', 185: 'O',
186: 'P', 187: 'Q', 188: 'R', 189: 'S', 191: 'T', 192: 'U', 193: 'V',
194: 'W', 195: 'X', 196: 'Y', 197: 'Z', 198: 'Á', 199: 'À', 211: 'Â',
212: 'Ã', 213: 'É', 214: 'Ê', 215: 'Í', 216: 'Ó', 217: 'Ô', 218: 'Õ',
219: 'Ú', 221: 'Ç', 222: ',', 223: '.', 224: '!', 225: '?', 226: ';',
227: ':', 228: '_', 229: '(', 231: ')', 232: '"', 233: '#', 234: '$',
235: '%', 236: '@', 237: ' ', 238: '\n'}
    numbers = []
    for block in blocks:
        number = pow(int(block), d, n)
        numbers.append(number)
    j = ""
    for frag in numbers:
        x = str(frag)
        j  = x
    s = wrap(j, 3)
    q = s            # Not converting the s into str.
    result = []
    for element in q:
        for key in symbols:
            if str(key) == element:                # Comparing with str(key)
                result.append(symbols.get(key))
    return q, result

# message = [3323713707, 1319300010, 1144229290, 1660290264, 2194193588,
# 734058623, 2217413390, 3225084774, 781294944, 2728321168,
# 2631341137, 1922680560, 651540169, 3030894670, 41880099,
# 300780045, 559287950, 1767066193, 2787757960, 520879703,
# 1993872416, 1386565567, 2460441503, 2766908703]

##################################
Everything below: Rudimentary Test
##################################
message = [116111113]

if __name__ == '__main__':
    q, result = solved(message,1, 255)
    print(q, result)
  • Related