Skip to content

Instantly share code, notes, and snippets.

@Devac
Last active August 29, 2015 14:27
Show Gist options
  • Save Devac/c312bd40aa9631ad0a6a to your computer and use it in GitHub Desktop.
Save Devac/c312bd40aa9631ad0a6a to your computer and use it in GitHub Desktop.
Improved style, but not really the efficiency.
# -*- coding: utf-8 -*-
"""
Created on Wed Aug 19 20:39:55 2015
@author: Dave Cöen
Please, do some follow-up reading on PEP-8.
"""
import re
import sys
# Devac:
# Constants (and they are constant in your program) should be writen in
# UPPERCASE
# Generally only global variables are outside functions, in Python you are
# still supposed to make a main function.
N = 4
BIT_SEQ_LENGTH = 2 ** N
def bintodec_conv(bin_num): # string
"""
Your implementation of this function can be simplified ;)
"""
return int(bin_num, 2) #int
def dectobin_conv(dec_num): # int
"""
Same problem as with first converter.
"""
return bin(dec_num)[2:] #string
def find_smallest_binary(list_bin): # string
"""
Think about string of binary numbers and a binary number.
Why would their order differ?
"""
list_bin.sort()
return str(list_bin[0])
def find_special_number(bin_in):
"""
Find the special number := find indices of where 1 is, find differences
between indices, then take each element in list to the power of the next
one (last one to the power of first) and sum together.
"""
# iter is a function name.
iteran = [m.start() for m in re.finditer('1', bin_in)]
iter_dif = [y - x for x, y in zip(iteran[:-1], iteran[1:])]
iter_dif.append(BIT_SEQ_LENGTH - (iteran[len(iteran) - 1] - iteran[0]))
stopgap_var = 0
len_iter_dif = len(iter_dif) # Why would you want to compute it each time?
for num_j in range(0, len_iter_dif):
if num_j != len_iter_dif - 1:
stopgap_var += iter_dif[num_j] ** iter_dif[num_j + 1]
else:
stopgap_var += iter_dif[num_j] ** iter_dif[0]
return stopgap_var
def create_hashed_sequence():
"""
Isolated helper function that will create binary representation to use in
other parts of the code.
"""
bit_variants = [dectobin_conv(i) for i in range(0, 2 ** BIT_SEQ_LENGTH)]
hash_seq = {}
for bits in bit_variants:
# add zeros infront of possible bit combination
for i in range(0, BIT_SEQ_LENGTH - len(bits)):
bits = '0' + bits
# create separate bit combination with circular property so that when
# cycling through a bit combination the last sequences aren't shortened
long_bits = bits
for j in range(0, N - 1):
long_bits = long_bits + bits[j]
# creation of hash table with bit combination and corresponding bit
# sequences
for i in range(0, BIT_SEQ_LENGTH):
if i == 0:
hash_seq[bits] = [long_bits[i:i + N]]
else:
hash_seq[bits].append(long_bits[i:i + N])
return hash_seq
def main():
"""
This is where we can use our previously defined functions in a way that is
pretty much a common convention.
"""
hash_seq = create_hashed_sequence()
# Devac:
# A word about convention in naming: use only lowercase letters to name
# your variables.
unique_bit_variant = []
# Check for duplicates, create list with unique bit variants.
# Devac:
# Again, there are data structures that don't allow duplicates.
for key, val in hash_seq.items():
duplicate = False
for i in range(0, len(val)):
for j in range(i + 1, len(val)):
if i != j and val[i] == val[j]:
duplicate = True
if not duplicate:
unique_bit_variant.append(key)
if unique_bit_variant is None:
print('No unique bit Variants!')
sys.exit()
# Get rid of rotated bit variants.
check = []
# Devac:
# If you want to store unique variants use set() instead of array!
unique_variant = []
# Go through all bit combinations with no duplicates in their sequences
# (unique)
for i in range(0, len(unique_bit_variant)):
# Calculate special number via non linear operation, this makes order
# matter but rotational symmetry still exists
# (123 = 312 but 123 != 132)
stopgap = find_special_number(unique_bit_variant[i])
# Make a list out of the different special numbers found, list length
# implies the number of unique bit combinations while disregarding
# rotationally symmetric ones
if stopgap not in check:
unique_variant.append([])
check.append(stopgap)
# Go through all unique bit combinations, and assign those with the same
# special number to a list.
# 2 dimensional list will be created.
# Devac: Or you could use Dictionary Data Structure.
for k in range(0, len(unique_bit_variant)):
for j in range(0, len(check)):
if check[j] == find_special_number(unique_bit_variant[k]):
unique_variant[j].append(unique_bit_variant[k])
final_answer = []
# Go through the inner lists of the 2 dimensional list unique_Variant,
# and find the smallest binary for each one
for variant in unique_variant:
final_answer.append(find_smallest_binary(variant))
# Prints the smallest unique bit combination for each special number
print(final_answer)
# Devac:
# DON'T use function names as variable names! sum is a function in standard
# Python implementation.
print(sum([int(i, 2) for i in final_answer]))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment