Skip to content

Instantly share code, notes, and snippets.

@fbwright
Last active August 29, 2015 14:14
Show Gist options
  • Save fbwright/7dca1364f0976e39a575 to your computer and use it in GitHub Desktop.
Save fbwright/7dca1364f0976e39a575 to your computer and use it in GitHub Desktop.
/r/dailyprogrammer Challenge #199 - Bank Number Banners
## ==========================================
## Challenge #199 - Bank Number Banners Pt. 1
## ==========================================
## http://www.reddit.com/r/dailyprogrammer/comments/2tr6yn/2015126_challenge_199_bank_number_banners_pt_1/
##
## Description
## -----------
## You work for a bank, which has recently purchased an ingenious machine to
## assist in reading letters and faxes sent in by branch offices. The machine
## scans the paper documents, and produces a file with a number of entries
## which each look like this:
##
## ::
## _ _ _ _ _ _ _ _
## | _| _||_||_ |_ ||_||_|| |
## ||_ _| | _||_| ||_| _||_|
##
## Each entry is 4 lines long, and each line has 27 characters. The first 3
## lines of each entry contain an account number written using pipes and
## underscores, and the fourth line is blank. Each account number should have
## 9 digits, all of which should be in the range 0-9.
##
## Right now you're working in the print shop and you have to take account
## numbers and produce those paper documents.
##
## Input
## -----
## You'll be given a series of numbers and you have to parse them into the
## previously mentioned banner format. This input...
##
## ::
## 000000000
## 111111111
## 490067715
##
import parseopt2
const
font: array[0..9, int] =
[111, 9, 94, 91, 57, 115, 119, 73, 127, 123]
proc getBit(n: int, b: int): bool =
((n and (1 shl b)) shr b) == 1
proc toBanner(text): string =
if text.len != 9:
raise newException(ValueError, "The string must be exactly 9 characters long.")
var top, middle, bottom: string = ""
for character in text:
if not (character in {'0'..'9'}):
raise newException(ValueError, "The string can contain only digits (0-9)")
let value = ord(character) - ord('0')
top &= (if font[value].getBit(6): " _ " else: " ")
for i in countdown(5, 3):
if font[value].getBit(i):
middle &= (if i mod 2 == 0: "_" else: "|")
else:
middle &= " "
for i in countdown(2, 0):
if font[value].getBit(i):
bottom &= (if i mod 2 == 0: "|" else: "_")
else:
bottom &= " "
result = top & "\n" & middle & "\n" & bottom
proc writeHelp() =
echo("Usage: bankbanners FILENAME [-h|--help]")
when isMainModule:
var
numbers: File
line: string = ""
filename: string = ""
for kind, key, val in getopt():
case kind
of cmdArgument: filename = key
of cmdLongOption, cmdShortOption:
case key
of "help", "h": writeHelp()
of cmdEnd: assert(false)
if filename == "":
writeHelp()
else:
if numbers.open(filename):
while numbers.readLine(line):
echo(toBanner(line))
font = [111, 9, 94, 91, 57, 115, 119, 73, 127, 123]
def bannerToNumber(banner):
number = ""
for i in range(9):
start, end = i*3, (i+1)*3
digit = banner[0][start:end].strip() + \
banner[1][start:end] + \
banner[2][start:end]
#value = sum(list(map(lambda i, n: 2**i if n != ' ' else 0, range(7), reversed(digit))))
digit = ''.join("1" if c != ' ' else "0" for c in digit)
value = int(digit, base=2) #as suggested by /u/adrian17
if value in font:
number += "%s" % font.index(value)
else:
raise ValueError("Not a valid digit")
return number
if __name__ == "__main__":
filename = "banners.txt"
line_count = 0
banner = []
with open(filename) as file:
for line in file:
banner.append(line)
line_count += 1
if line_count == 3:
print(bannerToNumber(banner))
banner = []
line_count = 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment