Dan
BAN USERIn Python just for fun...
class Marks(object):
def __init__(self, mark1, mark2, mark3):
self.mark1 = mark1
self.mark2 = mark2
self.mark3 = mark3
self.maxmark = max([mark1, mark2, mark3])
def getMaxMarks(self):
return self.maxmark
def isPass(self):
return (self.mark1 + self.mark2 + self.mark3) / 3 >= 40
class StudentList(object):
def __init__(self, all_marks):
self.all_marks = all_marks
def totalStudents(self):
return len(self.all_marks)
def passCount(self):
return sum([1 for mark in self.all_marks if mark.isPass()])
if __name__ == '__main__':
# simple test...prints 1
print repr(StudentList([Marks(35, 40, 45), Marks(15, 15, 9), Marks(30, 30, 30)]).passCount())
Set up a map of the symbol (just 9 characters in a row, left/right, top/bottom) to digit.
Read the input file, store the 3 keys you are looking for by appending to each key in a list (column / 3 tells you which one you are appending too).
I assumed no space in the input file between numbers, so it is a 9x9 file input file exactly.
./lednumbers.py led_image
860
#!/usr/bin/env python
import sys
SYMBOLS = [
"""\
_
| |
|_|\
""",
"""\
|
|
| """,
"""\
___
_|
|__""",
"""\
___
__|
__|""",
"""\
| |
|_|
|""",
"""\
___
|_
__|""",
"""\
__
|_
|_|""",
"""\
___
|
|""",
"""\
___
|_|
|_|""",
"""\
___
| |
|_|
__|"""]
def build_map():
"""Build a map of LED pattern to numeric value."""
symbol_map = {}
for i, symbol in enumerate(SYMBOLS):
line = symbol.replace('\n', '')
symbol_map[line] = i
return symbol_map
if __name__ == '__main__':
# open file with three digits
lines = [line.strip('\n') for line in open(sys.argv[1]).readlines()]
assert len(lines) == 3
symbols = ['', '', '']
row = 0
for line in lines:
assert len(line) == 9
for i in range(9):
symbols[i / 3] += line[i]
symbol_map = build_map()
for symbol in symbols:
sys.stdout.write(str(symbol_map[symbol]))
print ''
Maybe this could be made shorter, but it seems straightforward. O(n).
$ ./ternary.py 1
3
$ ./ternary.py 2
7
$ ./ternary.py 3
15
$ ./ternary.py 4
30
#!/usr/bin/env python
import sys
def num_ternary(previous_letter, b_used, length):
if length == 1:
# can place either a, b, or c
if previous_letter == 'a':
return 1 if b_used else 2
else:
return 2 if b_used else 3
if b_used:
if previous_letter == 'a':
# can only place 'c'
return num_ternary('c', True, length - 1)
else:
# can place 'a' or 'c'
return num_ternary('a', True, length - 1) + num_ternary('c', True, length - 1)
else:
if previous_letter == 'a':
# can place 'b' or 'c'
return num_ternary('b', True, length - 1) + num_ternary('c', False, length - 1)
else:
# 'a', 'b', or 'c'
return num_ternary('a', False, length - 1) + num_ternary('b', True, length - 1) + num_ternary('c', False, length - 1)
if __name__ == '__main__':
# assume one interger arg, length N
print num_ternary(None, False, int(sys.argv[1]))
Still returns 0 when I compiled and tested the update. I understand how you might do a quick-sort style solution, but only after unshifting the sorted array. See my Python example of making a single linear pass for what I think solves it in O(n). O(log N) might be possible but only after unshifting, I think, so I suspect O(n) is the most efficient.
- Dan August 04, 2014
Or, a thread acquired the lock and never released it, perhaps because Func() threw an exception. Adding a try/finally around Func with a release in the finally would resolve that. That's not a deadlock, but is a different plausible explanation.
- Dan October 09, 2014