0

So I'm trying to work to create a program which can take two inputs such as

encrypt('12345','12')

and it will return

'33557'

where the code ('12345') and been incremented by the key ('12'), working from right to left.

I have already created one which will work for when the code and key are both 8 long, but I cannot work out how to do this should the code be allowed to be any size, possibly with nested for statments?

Here is the one i did early so you can see better what i am trying to do

def code_block(char,charBlock):
    if len(char) == 8 and len(charBlock) == 8:    #Check to make sure both are 8 didgets.
        c = char
        cB = charBlock
        line = ""
        for i in range(0,8):
            getDidget = code_char2(front(c),front(cB))
            c = last(c)
            cB = str(last(cB))
            line =line + getDidget
        print(line)
    else:
        print("Make sure two inputs are 8 didgets long")

def front(word):
    return word[:+1]

def last(word):
    return word[+1:]
Jack
  • 2,891
  • 11
  • 48
  • 65

1 Answers1

1

Some code tested on Python 3.2:

from decimal import Decimal
import itertools

def encrypt(numbers_as_text, code):
    key = itertools.cycle(code[::-1])

    num = Decimal(numbers_as_text)

    power = 1
    for _ in numbers_as_text:
        num += power * int(next(key))
        power *= Decimal(10)

    return num



if __name__ == "__main__":
    print(encrypt('12345','12'))

Some explanation:

  • code[::-1] is a cool way to reverse a string. Stolen from here
  • itertools.cycle endlessly repeats your key. So the variable key now contains a generator which yields 2, 1, 2, 1, 2, 1, etc
  • Decimal is a datatype which can handle arbitrary precision numbers. Actually Python 3's integer numbers would be sufficient because they can handle integer numbers with arbitrary number of digits. Calling the type name as a function Decimal(), calls the constructor of the type and as such creates a new object of that type. The Decimal() constructor can handle one argument which is then converted into a Decimal object. In the example, the numbers_as_text string and the integer 10 are both converted into the type Decimal with its constructor.
  • power is a variable that starts with 1 is multiplied by 10 for every digit that we have worked on (counting from the right). It's basically a pointer to where we need to modify num in the current loop iteration
  • The for loop header ensures we're doing one iteration for each digit in the given input text. We could also use something like for index in range(len(numbers_as_text)) but that's unnecessarily complex

Of course if you want to encode text, this approach does not work. But since that wasn't in your question's spec, this is a function focused on dealing with integers.

Community
  • 1
  • 1
cfi
  • 10,915
  • 8
  • 57
  • 103
  • 1
    @jack: If you consider an answer useful, upvote it. If an answer solves your problem, accept it (click the check mark next to it). You should also do this for your other open questions. If you never do this, people will stop looking at your questions. It's also always good to provide feedback :-) – cfi Mar 08 '13 at 14:39
  • I would do but i do not yet have +15 rep, otherwise i would definitely, although i may have a few more questions. – Jack Mar 08 '13 at 14:46
  • 1
    @jack: My fault about the upvoting, accepting answer should work though and will give you +2 (iirc) reputation as well. Of course, only accept if it reasonably works for you - not for the sake of the +2 rep ;-) – cfi Mar 08 '13 at 14:48
  • Will do thank you so much for taking your time to help us newbies out! – Jack Mar 08 '13 at 15:05
  • So i worked out why this is not working correctly 100% its due to the fact that if a 9 is to be encrypted with 1, it goes to '1' where it should go to '0' although i am not sure how i can change this in the code. any idea Which part i would need to change? Or should i just use a if statement to catch it before? – Jack Mar 09 '13 at 00:37
  • 1
    Why? It works for me! `print(encrypt('19349','12'))` at the end prints out `40561`. Which Python version are you using? What exactly are you doing and what's the output? – cfi Mar 10 '13 at 14:35
  • "Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600 32 bit (Intel)] on win32" - Let me try again, although i will say i have got it to work but i didn't use your code, although i did use parts of it like the intertools cycle and reverse string which was great. – Jack Mar 10 '13 at 15:01
  • Also out of interest whats the decimal() doing, in my testing I could not work it out. – Jack Mar 10 '13 at 16:55
  • 1
    @jack: Added some text to the bullet about the `Decimal()` constructor. – cfi Mar 10 '13 at 19:33