- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to partitioning into minimum number of Deci- Binary numbers in Python
Suppose we have a number n in string format. We have to find minimum deci-binary numbers are required, so that whose sum is equal to n. A deci-binary number is a decimal number whose digits are either 0 or 1.
So, if the input is like n = "132", then the output will be 3 because 132 is sum of three decibinary number (10 + 11 + 111).
To solve this, we will follow these steps −
- result := 1
- for each i in n, do
- if i is not in {0,1}, then
- result := maximum of result and i
- if i is not in {0,1}, then
- return result
Example
Let us see the following implementation to get better understanding −
def solve(n): result = 1 for i in n: if i not in {0,1}: result = max(result, int(i)) return result n = "132" print(solve(n))
Input
132
Output
3
Advertisements