
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
1-bit and 2-bit Characters in Python
Suppose we have two special characters. The first character can be represented by one bit 0. And the second character can be represented by two bits like (10 or 11). So, if we have a string represented by several bits. We have to check whether the last character must be a one-bit character or not. The given string will always end with a zero.
So, if the input is like [1,0,0], then the output will be True, as the only way to decode it is twobit character (10) and one-bit character (0). So, the last character is one-bit character.
To solve this, we will follow these steps −
- while size of bits > 1, do
- current := first element of bits, then delete first element from bits
- if current is same as 1, then
- delete first element from bits
- if size of bits is same as 0, then
- return False
- return true when bits[0] is same as 0, otherwise false
Let us see the following implementation to get better understanding −
Example
class Solution: def isOneBitCharacter(self, bits): while len(bits) > 1: current = bits.pop(0) if current == 1: bits.pop(0) if len(bits) == 0: return False return bits[0] == 0 ob = Solution() print(ob.isOneBitCharacter([1,0,0]))
Input
[1,0,0]
Output
True
- Related Articles
- Difference Between 8 Bit and 16 Bit Microcontroller
- Difference Between 8-Bit and 16-Bit Music
- Difference between 32-bit and 64-bit Operating Systems
- Compile 32-bit program on 64-bit gcc in C and C++
- Program to find number of bit 1 in the given number in Python
- Map a 10-bit number to 8-bit in Arduino
- Linux Kernel Versions 32-Bit vs 64-Bit
- How to display the bit(1) fields in MySQL?
- How to compile 32-bit program on 64- bit gcc in C and C++
- Differentiate between 32-Bit vs. 64-Bit Operating System.
- Bit Fields in C
- 8085 program to find 1's and 2's complement of 8-bit number
- 8085 program to find 1's and 2's complement of 16-bit number
- Efficient and reliable data transmission: A comprehensive guide to Bit Stuffing and Bit Destuffing implementation
- Find Duplicates of array using bit array in Python

Advertisements