(s)
| 1 | """ 1st Approach """ |
| 2 | |
| 3 | def 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 |
| 23 | print("solution('011100'):", solution('011100')) |
| 24 | print("solution('111'):", solution('111')) |
| 25 | print("solution('1111010101111'):", solution('1111010101111')) |
no outgoing calls
no test coverage detected