tags:

views:

153

answers:

8

Here's what I've got so far:

def encodeFive(zip):

    zero =  "||:::"

    one =   ":::||"

    two =   "::|:|"

    three = "::||:"

    four =  ":|::|"

    five =  ":|:|:"

    six =   ":||::"

    seven = "|:::|"

    eight = "|::|:"

    nine =  "|:|::"

    codeList = [zero,one,two,three,four,five,six,seven,eight,nine]

    allCodes = zero+one+two+three+four+five+six+seven+eight+nine

    code = ""
    digits = str(zip)
    for i in digits:

        code = code + i    

    return code

With this I'll get the original zip code in a string, but none of the numbers are encoded into the barcode. I've figured out how to encode one number, but it wont work the same way with five numbers.

A: 

I don't know what language you are usingm so I made an example in C#:

int zip = 72353;

string[] codeList = {
  "||:::", ":::||", "::|:|", "::||:", ":|::|",
  ":|:|:", ":||::", "|:::|", "|::|:", "|:|::"
};
string code = String.Empty;
while (zip > 0) {
  code = codeList[zip % 10] + code;
  zip /= 10;
}
return code;

Note: Instead of converting the zip code to a string, and the convert each character back to a number, I calculated the digits numerically.

Just for fun, here's a one-liner:

return String.Concat(zip.ToString().Select(c => "||::::::||::|:|::||::|::|:|:|::||::|:::||::|:|:|::".Substring(((c-'0') % 10) * 5, 5)).ToArray());
Guffa
@ΤΖΩΤΖΙΟΥ: The tag wasn't there when I answered the question.
Guffa
Ah, ok. I didn't check the revision history to see what was corrected, but in any case, I deleted my comment.
ΤΖΩΤΖΙΟΥ
+1  A: 

You're just adding i (the character in digits) to the string where I think you want to be adding codeList[int(i)].

The code would probably be much simpler by just using a dict for lookups.

Dan Head
that should be `codeList[int(i)]`, as `i` is a string in this current example.
Autoplectic
You're right! I didn't think of using int(i). Thanks so much!
Maggie
+4  A: 
codeList = ["||:::", ":::||", "::|:|", "::||:", ":|::|",
    ":|:|:", ":||::", "|:::|", "|::|:", "|:|::" ]
barcode = "".join(codeList[int(digit)] for digit in str(zipcode))
Mike DeSimone
An additional note: zip is a built in python method, it's preferable to use another variable name.
KillianDS
Fixed. I use `zip()` so rarely (list comprehensions or generators usually fit better), I forget it exists sometimes.
Mike DeSimone
A: 

This is made in python.

number = ["||:::",
    ":::||",
    "::|:|",
    "::||:",
    ":|::|",
    ":|:|:",
    ":||::",
    "|:::|",
    "|::|:",
    "|:|::"
    ]
def encode(num):
    return ''.join(map(lambda x: number[int(x)], str(num)))

print encode(32345)
noinflection
way to make python code look less readable than Perl ;)
Assaf Lavie
While your code is correct, a dictionary (with "0", "1"… as keys) seems more preferable.
ΤΖΩΤΖΙΟΥ
@ΤΖΩΤΖΙΟΥ at the beggining i did a dict, but in order to make the example clearer i decided to use an "array".
noinflection
+2  A: 

Perhaps use a dictionary:

barcode = {'0':"||:::",
           '1':":::||",
           '2':"::|:|",
           '3':"::||:",
           '4':":|::|",
           '5':":|:|:",
           '6':":||::",
           '7':"|:::|",
           '8':"|::|:",
           '9':"|:|::",
           }

def encodeFive(zipcode):
    return ''.join(barcode[n] for n in str(zipcode))

print(encodeFive(72353))
# |:::|::|:|::||::|:|:::||:

PS. It is better not to name a variable zip, since doing so overrides the builtin function zip. And similarly, it is better to avoid naming a variable code, since code is a module in the standard library.

unutbu
A: 

Thanks everyone who replied, I really appreciate the help! I used int() and it worked. =)

Maggie
@Maggie: Great, but this isn't really an answer to your question. Better delete it and accept the "best" (in your opinion) answer by clicking the checkmark next to it. You can also upvote good answers (I've just upvoted your question so you now have enough reputation to do so). This will make the community even happier than a written thank-you :)
Tim Pietzcker
+1  A: 

I find it easier to use split() to create lists of strings:

codes = "||::: :::|| ::|:| ::||: :|::| :|:|: :||:: |:::| |::|: |:|::".split()

def zipencode(numstr): 
    return ''.join(codes[int(x)] for x in str(numstr))

print zipencode("32345") 
Paul McGuire
A: 

It appears you're trying to generate a "postnet" barcode. Note that the five-digit ZIP postnet barcodes were obsoleted by ZIP+4 postnet barcodes, which were obsoleted by ZIP+4+2 delivery point postnet barcodes, all of which are supposed to include a checksum digit and leading and ending framing bars. In any case, all of those forms are being obsoleted by the new "intelligent mail" 4-state barcodes, which require a lot of computational code to generate and no longer rely on straight digit-to-bars mappings. Search USPS.COM for more details.

joe snyder