Can this code be shortened?

Can the following snippets be reduced? The total lines of each snippet is 136!

[code]

while n <= x:
if char == 'A':
newstr = newstr + 'D'
elif char == 'B':
newstr = newstr + 'E'
elif char == 'C':
newstr = newstr + 'F'
...
elif char == '!':
newstr = newstr + 'A'
elif char == '-':
newstr = newstr + 'B'
elif char == ' ':
newstr = newstr + 'C'
str = newstr
[/code]
[code]
while n <= x:
if char == 'A':
newstr = newstr + '!'
elif char == 'B':
newstr = newstr + '-'
elif char == 'C':
newstr = ' '
...
elif char == '!':
newstr = newstr + ','
elif char == '-':
newstr = newstr + '.'
elif char == ' ':
newstr = newstr + '?'
str = newstr
[/code]

Any ideas?

Thanks in advance,
Nathan
Swat spam before it fills your inbox!

Comments

  • : Can the following snippets be reduced? The total lines of each snippet is 136!
    :
    : [code]
    :
    : while n <= x:
    : if char == 'A':
    : newstr = newstr + 'D'
    : elif char == 'B':
    : newstr = newstr + 'E'
    : elif char == 'C':
    : newstr = newstr + 'F'
    : ...
    : elif char == '!':
    : newstr = newstr + 'A'
    : elif char == '-':
    : newstr = newstr + 'B'
    : elif char == ' ':
    : newstr = newstr + 'C'
    : str = newstr
    : [/code]
    : [code]
    : while n <= x:
    : if char == 'A':
    : newstr = newstr + '!'
    : elif char == 'B':
    : newstr = newstr + '-'
    : elif char == 'C':
    : newstr = ' '
    : ...
    : elif char == '!':
    : newstr = newstr + ','
    : elif char == '-':
    : newstr = newstr + '.'
    : elif char == ' ':
    : newstr = newstr + '?'
    : str = newstr
    : [/code]
    :
    : Any ideas?
    :
    : Thanks in advance,
    : Nathan
    : Swat spam before it fills your inbox!
    :

    What are you trying to do? May be its possible to generalize things... post the whole code, if you can...or you yourself look for patterns you can exploit

    like for the first part
    [code]
    if char == 'A':
    newstr = newstr + 'D'
    elif char == 'B':
    newstr = newstr + 'E'
    elif char == 'C':
    newstr = newstr + 'F'
    [/code]
    you can go (pseduocode)

    [code]
    if char is in range 'A' to 'D'
    newstr = newstr + str(int(char)+3)
    [/code]
    ...thats how I would do it in C, or something like that, it runs atop the fact that characters are numbers, but I'm not sure you can do such stuff in python efficiently...

    if that didn't explain much (I doubt it did), just post mroe of your code, and we can help :)

    {2}rIng
  • : Can the following snippets be reduced? The total lines of each snippet is 136!
    :
    : [code]
    :
    : while n <= x:
    : if char == 'A':
    : newstr = newstr + 'D'
    : elif char == 'B':
    : newstr = newstr + 'E'
    : elif char == 'C':
    : newstr = newstr + 'F'
    : ...
    : elif char == '!':
    : newstr = newstr + 'A'
    : elif char == '-':
    : newstr = newstr + 'B'
    : elif char == ' ':
    : newstr = newstr + 'C'
    : str = newstr
    : [/code]
    : [code]
    : while n <= x:
    : if char == 'A':
    : newstr = newstr + '!'
    : elif char == 'B':
    : newstr = newstr + '-'
    : elif char == 'C':
    : newstr = ' '
    : ...
    : elif char == '!':
    : newstr = newstr + ','
    : elif char == '-':
    : newstr = newstr + '.'
    : elif char == ' ':
    : newstr = newstr + '?'
    : str = newstr
    : [/code]

    It looks to me like you're just doing a simple cipher-like translation. Python's string module can do this for you very easily:

    [code]
    import string
    table = string.maketrans('ABC!- ', 'DEFABC')
    print string.translate('!- ABC', table)
    [/code]


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

    [code]
    $ select * from users where clue > 0
    no rows returned
    [/code]

  • : : Can the following snippets be reduced? The total lines of each snippet is 136!
    : :
    : : [code]
    : :
    : : while n <= x:
    : : if char == 'A':
    : : newstr = newstr + 'D'
    : : elif char == 'B':
    : : newstr = newstr + 'E'
    : : elif char == 'C':
    : : newstr = newstr + 'F'
    : : ...
    : : elif char == '!':
    : : newstr = newstr + 'A'
    : : elif char == '-':
    : : newstr = newstr + 'B'
    : : elif char == ' ':
    : : newstr = newstr + 'C'
    : : str = newstr
    : : [/code]
    : : [code]
    : : while n <= x:
    : : if char == 'A':
    : : newstr = newstr + '!'
    : : elif char == 'B':
    : : newstr = newstr + '-'
    : : elif char == 'C':
    : : newstr = ' '
    : : ...
    : : elif char == '!':
    : : newstr = newstr + ','
    : : elif char == '-':
    : : newstr = newstr + '.'
    : : elif char == ' ':
    : : newstr = newstr + '?'
    : : str = newstr
    : : [/code]
    : :
    : : Any ideas?
    : :
    : : Thanks in advance,
    : : Nathan
    : : Swat spam before it fills your inbox!
    : :
    :
    : What are you trying to do? May be its possible to generalize things... post the whole code, if you can...or you yourself look for patterns you can exploit
    :
    : like for the first part
    : [code]
    : if char == 'A':
    : newstr = newstr + 'D'
    : elif char == 'B':
    : newstr = newstr + 'E'
    : elif char == 'C':
    : newstr = newstr + 'F'
    : [/code]
    : you can go (pseduocode)
    :
    : [code]
    : if char is in range 'A' to 'D'
    : newstr = newstr + str(int(char)+3)
    : [/code]
    : ...thats how I would do it in C, or something like that, it runs atop the fact that characters are numbers, but I'm not sure you can do such stuff in python efficiently...
    :
    : if that didn't explain much (I doubt it did), just post mroe of your code, and we can help :)
    :
    : {2}rIng
    :

    I'm trying to encrypt/decrypt messages. I switched to PyCrypto and ezPyCrypto and got the following error:

    >>> from ezPyCrypto import key
    >>> str = '4 big dogs.'
    >>> key.encString(str)
    Traceback (most recent call last):
    File "", line 1, in ?
    key.encString(str)
    TypeError: unbound method encString() must be called with key instance as first argument (got str instance instead)
    >>>

    How can I fix the error
    Swat spam before it fills your inbox!

  • : I'm trying to encrypt/decrypt messages. I switched to PyCrypto and ezPyCrypto and got the following error:
    :
    : >>> from ezPyCrypto import key
    : >>> str = '4 big dogs.'
    : >>> key.encString(str)
    : Traceback (most recent call last):
    : File "", line 1, in ?
    : key.encString(str)
    : TypeError: unbound method encString() must be called with key instance as first argument (got str instance instead)
    : >>>
    :
    : How can I fix the error

    The problem is you're passing a string and it wants a key instead. The fact that it's calling it an "unbound" method tells me you're probably calling a class method without instantiating an object of that class. I've never used these libraries so I can only guess at the solution. Try something like this to see if it helps:

    from exPyCrypto import key
    k = key()
    e = k.encString('4 big dogs.')


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

    [code]
    $ select * from users where clue > 0
    no rows returned
    [/code]

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories