Question: Compare which code runs fasterdef binary _ to _ decimal _ v 1 ( binary _ str ) : k = len ( binary _

Compare which code runs fasterdef binary_to_decimal_v1(binary_str):
k = len(binary_str)
decimal =0 # initialization
for i in range(k): # iteration
decimal +=2**i *(1 if binary_str[(k -1)- i]=="1" else 0)
return decimaldef binary_to_decimal_v2(binary_str):
decimal =0 # initialization
for bit in binary_str:
decimal = decimal *2+(1 if bit =="1" else 0) # iteration
return decimal

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Finance Questions!