MCPcopy Index your code
hub / github.com/OmkarPathak/pygorithm / __create_group_list

Function __create_group_list

pygorithm/binary/base2.py:105–183  ·  view source on GitHub ↗

Helper method for conversion Takes binary number and converts to a list of grouped binary values Process: 1. convert number to a list of chars 2. reverse the list 3. iterate through the list creating groups of 4 removing each value as its iterate

(n)

Source from the content-addressed store, hash-verified

103
104
105def __create_group_list(n):
106 """
107 Helper method for conversion
108
109 Takes binary number and converts to a list of
110 grouped binary values
111
112 Process:
113 1. convert number to a list of chars
114 2. reverse the list
115 3. iterate through the list creating groups of 4
116 removing each value as its iterated
117 4. if the remaining values can not make a group,
118 add 0's to the end to get to 4
119 5. join each list together
120 6. reverse each value in the final list to put
121 them back in order
122
123 Example:
124
125 n = 11101100101001001100101001
126 reverse = 10010100110010010100110111
127
128 Start grouping values:
129
130 [['1', '0', '0', '1'],
131 ['0', '1', '0', '0'],
132 ['1', '1', '0', '0'],
133 ['1', '0', '0', '1'],
134 ['0', '1', '0', '0'],
135 ['1', '1', '0', '1']]
136
137 remaining value = 11
138 add missing 0's -> 1100
139
140 join each list:
141
142 ['1', '0', '0', '1'] = 1001
143 ['0', '1', '0', '0'] = 0100
144 ['1', '1', '0', '0'] = 1100
145 ['1', '0', '0', '1'] = 1001
146 ['0', '1', '0', '0'] = 0100
147 ['1', '1', '0', '1'] = 1101
148
149 ['1100', '1101', '0100', '1001', '1100', '0100', '1001']
150
151 reverse final list
152 result = ['0011', '1011', '0010', '1001', '0011', '0010', '1001']
153
154 :param n: binary number
155 :return: list
156 """
157 reversed_number_list = list(str(n)[::-1])
158 grouped_list = []
159
160 while len(reversed_number_list) != 0:
161 # go until list is empty
162 if len(reversed_number_list) < 4:

Callers 1

to_base16Function · 0.85

Calls 1

popMethod · 0.45

Tested by

no test coverage detected