MCPcopy Index your code
hub / github.com/Rohit91singh9/Coding-DP-DSA / solution

Function solution

String_containing_binary.py:3–22  ·  view source on GitHub ↗
(s)

Source from the content-addressed store, hash-verified

1""" 1st Approach """
2
3def solution(s):
4 # Converting binary string to decimal
5 decimal_number = int(s, 2)
6
7 # Defining variable to count no of operations required
8 operations_count = 0
9
10 # loopping until decimal number becomes 0
11 while decimal_number > 0:
12 # if Even, divide the number by 2
13 if decimal_number % 2 == 0:
14 decimal_number /= 2
15 # if Odd, subtract 1 from it
16 else:
17 decimal_number -= 1
18 # incrementing operations count by 1
19 operations_count += 1
20 return operations_count
21
22 # validating solution function
23print("solution('011100'):", solution('011100'))
24print("solution('111'):", solution('111'))
25print("solution('1111010101111'):", solution('1111010101111'))

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected