In [4]:
import math

def workdiodes(freq, recv=False):
"""
Formulas for calculating the frequency:

Fc = freq       For transmitting frequency.
Fc = freq       For receiving fequency.
N = Fc/0.4      Disregarding any fractional part.
A = (Fc - 0.4 * N)/0.01  Rounding up any fractional part.
K = 0           If the frequency ends in 5 Khz
K = 1           If the frequency ends in 0 Khz

Example:
Calculating for 155.995 Mhz.

N = 155.995/0.4 = 389.9875

Therefore, N = 389

A = (155.995 - 0.4 * 389)/0.1 = .395/0.01 = 39.5

Therefore, A = 40

The freqency ends in 5 kHz. Therefore K = 0

Finally, convert the N and A to binary. N = 110000101, A = 101000

In the pinout below, pin 1 is always "1", and 18
is always 0.

Pin Number:  1  2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18
Designation     N8  N7  N6  N5  N4  N3  N2  N1  N0   A5   A4   A3   A2   A1   A0    K   Common
Decimal Value   256 128 64  32  16  8   4   2   1    32   16   8    4    2    1
Binary Code  1  1   1   0   0   0   0   1   0   1    1    0    1    0    0    0     0   0

This is a common cathode array, so the diode's cathode (black band side) are all joined together
and connected to pin 18. When connecting diodes, you leave binary "1" disconnected (or cut), and
only connect "0".
"""
if recv:
Fc = freq - 21.4
else:
Fc = freq

# Pad the frequency for display
freq_fmt = "{:.3f}" # 3 decimals
freq = freq_fmt.format(freq)

# if freq ends in 5, K=0, else K =1

if freq[6:] == "0":
K = 1
elif freq[6:] == "5":
K = 0
else:
return "{0}: ERROR".format(freq)

N = int(Fc/0.4) # Disrearding any fractional part
A = int(math.ceil((Fc - 0.4 * N)/0.01)) # Rounding UP any fractional part

# N binary will always be 9 characters, A binary will be 6
# http://stackoverflow.com/a/21732313/895866
get_bin = lambda x, n: x >= 0 and str(bin(x))[2:].zfill(n) or "-" + str(bin(x))[3:].zfill(n)

Nb = get_bin(N, 9)
Ab = get_bin(A, 6)

return "{0}: {1}{2}{3}".format(freq, Nb, Ab, K)

def pinline(line):

# Binary Code  1  1   1   0   0   0   0   1   0   1    1    0    1    0    0    0     0   0
if len(line) != 25:
return line

outline = []
for x in range(0, len(line)):
if x < 8:
outline.append(line[x])
elif x == 8:
outline.append('     1  ')
else:
outline.append('{0}   '.format(line[x]))

# Now add the common 0
outline.append('0')
return ''.join(outline)
#     return """{0[0:9]}     1  {0[0]}   {0[1]}   {0[2]}   {0[3]}   {0[4]}   {0[5]}   {0[6]}   {0[7]}
#            {0[8]}   {0[9]}   {0[10]}   {0[11]}   {0[12]}   {0[13]}   {0[14]}   {0[15]}   {0[16]}
#            {0[17]}""".format(line)

print("""
Pin Number:  1  2   3   4   5   6   7   8   9   10  11  12  13  14  15  16  17  18
Designation  -  N8  N7  N6  N5  N4  N3  N2  N1  N0  A5  A4  A3  A2  A1  A0  K   Common
Value        -  256 128 64  32  16  8   4   2   1   32  16  8   4   2   1   -   - """)

print(pinline(workdiodes(155.995)))
print(pinline(workdiodes(150.010)))
print(pinline(workdiodes(150.025)))
print(pinline(workdiodes(155.995)))
print(pinline(workdiodes(146.520)))
print(pinline(workdiodes(145.050)))
print(pinline(workdiodes(145.050, True)))
print(pinline(workdiodes(144.390)))
print(pinline(workdiodes(144.390, True)))
print(pinline(workdiodes(144.392)))
print(pinline(workdiodes(145.047)))
print(pinline(workdiodes(145.049)))

Pin Number:  1  2   3   4   5   6   7   8   9   10  11  12  13  14  15  16  17  18
Designation  -  N8  N7  N6  N5  N4  N3  N2  N1  N0  A5  A4  A3  A2  A1  A0  K   Common
Value        -  256 128 64  32  16  8   4   2   1   32  16  8   4   2   1   -   -
155.995:     1  1   1   0   0   0   0   1   0   1   1   0   1   0   0   0   0   0
150.010:     1  1   0   1   1   1   0   1   1   1   0   0   0   0   0   1   1   0
150.025:     1  1   0   1   1   1   0   1   1   1   0   0   0   0   1   1   0   0
155.995:     1  1   1   0   0   0   0   1   0   1   1   0   1   0   0   0   0   0
146.520:     1  1   0   1   1   0   1   1   1   0   0   0   1   1   0   1   1   0
145.050:     1  1   0   1   1   0   1   0   1   0   0   1   1   0   0   1   1   0
145.050:     1  1   0   0   1   1   0   1   0   1   0   0   0   1   0   1   1   0
144.390:     1  1   0   1   1   0   1   0   0   0   1   0   0   1   1   1   1   0
144.390:     1  1   0   0   1   1   0   0   1   1   0   1   0   0   1   1   1   0
144.392: ERROR
145.047: ERROR
145.049: ERROR