8: Is There a Better Method?


#1

https://www.codecademy.com/courses/learn-python/lessons/introduction-to-bitwise-operators/exercises/a-bit-of-this-or-that?action=lesson_resume

Hello, I’m trying to do the challenge on the instructions that asks me to make a script without the “|” operator. I have done just that with this line of code. I’m wondering if there is a far more efficient way in doing this, as my line of code is massive. What I am doing is iterating through each digit on the binary number through OR logic on a zip function. In order run the binary numbers through the zip function, I must convert them into lists, run them through the function, and then convert them back to binary.

Here is the code in question:

def binaryORzip(a,b):

# Converting binary to lists for the zip function:
  astring = str(bin(a))[2: ]
  alist = [int(digit) for digit in astring]
  bstring = str(bin(b))[2: ]
  blist = [0] +[int(digit) for digit in bstring]

# Zipping the lists through OR logic:
  ablist =[]
  for digit1,digit2 in zip(alist,blist):
    if digit1 or digit2 == 1:
      ablist.append(1)
    else:
      ablist.append(0)
  print ablist
 
# Converting the result of the zip back to binary:
  abstring = [str(digit) for digit in ablist]
  abstring2 = ''.join(abstring)
  abnum = int(abstring2,2)
  result = bin(abnum)
  print result
  return result
  
print binaryORzip(14,5)

The code works, but I am wondering if anyone found a better method on doing this challenge.


#2

Update: I cut the lines of code a little bit more so the function looks like this. Though it is still pretty ugly work:

def binaryorzip(a,b):
  # Converting binary to lists for the zip function:
  alist = [int(digit) for digit in str(bin(a))[2: ]]
  blist = [0] +[int(digit) for digit in str(bin(b))[2: ]]
  
  # Zipping the lists through OR logic: 
  ablist =[]
  for digit1,digit2 in zip(alist,blist):
    if digit1 or digit2 == 1:
      ablist.append(1)
    else:
      ablist.append(0)
      
  # Converting the result of the zip back to binary: 
  abnum = int(''.join([str(digit) for digit in ablist]),2)
  print abnum
  result = bin(abnum)
  print result
  return result
  
binaryorzip(14,5)

#3

Sometimes ‘ugly’ is as good as it gets. Having something that works is always a good outcome. At least it’s a starting point.

My experiment is not any prettier…

A = []
B = []
a = 0b1110
b = 0b101

if b > a: a, b = b, a
  
while a > 0:
    A.append(0b1 & a)
    a = a >> 1
  
A = A[::-1]

while b > 0:
    B.append(0b1 & b)
    b = b >> 1

while len(B) < len(A):
    B.append(0)
  
B = B[::-1]

print A, B

# -> [1, 1, 1, 0] [0, 1, 0, 1]
C = zip(A, B)
print C

# -> [(1, 0), (1, 1), (1, 0), (0, 1)]

R = map(lambda x: str(x[0] + x[1]), C)
print R

# -> ['1', '2', '1', '1']

Y = map(lambda x: '1' if x else '0', R)
print Y

# -> ['1', '1', '1', '1']

Z = bin(int(''.join(Y), 2))
print Z

# -> 0b1111

There are a lot of intermediate steps, which are self-explanatory, but if you have any questions, or come up with ways to incorporate both examples into a finished function… Dont’ be shy.


Functional model

>>> def bitwise_or(a, b):
	A = []
	B = []
	if b > a: a, b = b, a
	while a > 0:
		A.append(0b1 & a)
		a = a >> 1
	while b > 0:
		B.append(0b1 & b)
		b = b >> 1
	while len(B) < len(A):
		B.append(0)
	C = zip(A[::-1], B[::-1])
	R = map(lambda x: str(x[0] + x[1]), C)
	Y = map(lambda x: '1' if x else '0', R)
	return bin(int(''.join(Y), 2))

>>> bitwise_or(14, 5)
'0b1111'
>>> 

Further reduced…

>>> def bitwise_or(a, b):
	def bin_list(x): return list(bin(x))[2:]
	if b > a: a, b = b, a
	A = bin_list(a)
	B = bin_list(b)
	while len(B) < len(A): B.insert(0, 0)
	R = map(lambda x: '1' if int(x[0]) + int(x[1]) else '0', zip(A, B))
	return bin(int(''.join(R), 2))

>>> bitwise_or(14, 5)
'0b1111'
>>> 

#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.