|
3 | 3 | # Joe McFarland
|
4 | 4 | # import sys
|
5 | 5 | # import re
|
| 6 | +import copy |
6 | 7 |
|
7 |
| -filename = "data3.txt" |
| 8 | +filename = "data3_short.txt" |
8 | 9 |
|
9 | 10 | file = open(filename)
|
10 | 11 | filestr = file.read()
|
11 | 12 | a_list = filestr.split("\n")
|
12 | 13 | maxrows = len(a_list)
|
13 |
| -#print(a_list) |
| 14 | +print(a_list) |
14 | 15 |
|
15 | 16 | # def read_bit(x, pos):
|
16 | 17 | # mask = 1 << pos
|
17 | 18 | # return (x & mask) >> pos
|
18 | 19 |
|
19 |
| -def arraytostr(arr): |
20 |
| - mystr = "" |
21 |
| - for colval in (arr): |
22 |
| - if colval == 0: |
23 |
| - mystr += "0" |
24 |
| - elif colval == 1: |
25 |
| - mystr += "1" |
26 |
| - else: |
27 |
| - print(f"ERROR {arr}") |
28 |
| - exit() |
29 |
| - return mystr |
| 20 | +# def arraytostr(arr): |
| 21 | +# mystr = "" |
| 22 | +# for colval in (arr): |
| 23 | +# if colval == 0: |
| 24 | +# mystr += "0" |
| 25 | +# elif colval == 1: |
| 26 | +# mystr += "1" |
| 27 | +# else: |
| 28 | +# print(f"ERROR {arr}") |
| 29 | +# exit() |
| 30 | +# return mystr |
30 | 31 |
|
31 | 32 | maxcols = len(a_list[0])
|
32 |
| -gamma = [None]*maxcols |
33 |
| -epsilon = [None]*maxcols |
| 33 | + |
| 34 | +ogen = c02 = None |
| 35 | +current_list = copy.deepcopy(a_list) |
34 | 36 | for col in range(maxcols):
|
35 | 37 | zero_bits = one_bits = 0
|
36 |
| - for row in a_list: |
| 38 | + #col = 0 |
| 39 | + for row in current_list: |
37 | 40 | if row[col] == "0":
|
38 | 41 | zero_bits += 1
|
39 | 42 | elif row[col] == "1":
|
40 | 43 | one_bits += 1
|
41 |
| - else: |
42 |
| - print(f"ERROR {row}") |
43 |
| - exit() |
44 |
| - if zero_bits > one_bits: |
45 |
| - gbit = 0 |
46 |
| - ebit = 1 |
| 44 | + if one_bits >= zero_bits: |
| 45 | + most_common = 1 |
47 | 46 | else:
|
48 |
| - gbit = 1 |
49 |
| - ebit = 0 |
50 |
| - gamma[col] = gbit |
51 |
| - epsilon[col] = ebit |
| 47 | + most_common = 0 |
| 48 | + #col = 0 |
| 49 | + new_list = [] |
| 50 | + for row in current_list: |
| 51 | + if row[col] == str(most_common): |
| 52 | + new_list.append(row) |
| 53 | + print(f"new_list(col={col}):\n{new_list}") |
| 54 | + if len(new_list) == 1: |
| 55 | + print(f"found entry, {new_list}") |
| 56 | + #found_ogen = new_list[0] |
| 57 | + ogen = int(new_list[0],2) |
| 58 | + break |
| 59 | + current_list = copy.deepcopy(new_list) |
| 60 | +print(f"ogen = {ogen}") |
52 | 61 |
|
53 |
| -print(f"gamma={gamma}, epsilon={epsilon}") |
54 |
| -g1 = arraytostr(gamma) |
55 |
| -e1 = arraytostr(epsilon) |
56 |
| -gamma_dec = int(g1,2) |
57 |
| -epsilon_dec = int(e1,2) |
58 |
| -print(f"gamma_str={g1}, epsilon_str={e1}") |
59 |
| -print(f"gamma_dec={gamma_dec}, epsilon_dec={epsilon_dec}") |
60 |
| -print(f"mul = {gamma_dec*epsilon_dec}") |
| 62 | +# print(f"gamma={gamma}, epsilon={epsilon}") |
| 63 | +# g1 = arraytostr(gamma) |
| 64 | +# e1 = arraytostr(epsilon) |
| 65 | +# gamma_dec = int(g1,2) |
| 66 | +# epsilon_dec = int(e1,2) |
| 67 | +# print(f"gamma_str={g1}, epsilon_str={e1}") |
| 68 | +# print(f"gamma_dec={gamma_dec}, epsilon_dec={epsilon_dec}") |
| 69 | +# print(f"mul = {gamma_dec*epsilon_dec}") |
0 commit comments