Unary coding in Python Last Updated : 13 Mar, 2023 Comments Improve Suggest changes Like Article Like Report Unary code also sometimes called thermometer code is a type of entropy encoding. It is a lossless data compression technique with application in Golomb codes. Unary representation of a natural number n is n ones followed by a zero. For example, unary code for 6 will be 6 ones followed by a zero which is 1111110 Example: Input: 5 Output: 111110 result has 5 ones followed by a 0 Input: 1 Output: 10 result has 1 one followed by a 0 Approach: For encoding: take an empty list then append N times 1 and 0 in the last then convert it into string and print the result.For decoding: Take a given string and count number of 1's into it and then print the result. Example 1: Unary representation of a natural number. Python3 # Unary code encoding N = 8 A = [] for i in range(N): A.append(1) A.append(0) B = [str(k) for k in A] C = "".join(B) print("Unary code for", N, 'is', C) Output: Unary code for 8 is 111111110 Time Complexity: O(n)Auxiliary Space: O(n) Example 2: Decimal representation of an unary code. Python3 # Unary code decoding code = "111111110" count = 0 for i in code: if i == "1": count += 1 print("decoded number is :", count) Output: decoded number is : 8 Time Complexity: O(n)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Unary coding in Python A AmiMunshi Follow Improve Article Tags : Technical Scripter Python python-utility Practice Tags : python Similar Reads Code Golfing in Python Code Golf in Python refers to attempting to solve a problem using the least amount of characters possible. Like in Golf, the low score wins, the fewest amount of characters "wins". Python is a fantastic language for code golfing due to backward compatibility, quirks, it being a high-level language, 8 min read PEP 8 : Coding Style guide in Python Indeed coding and applying logic is the foundation of any programming language but there's also another factor that every coder must keep in mind while coding and that is the coding style. Keeping this in mind, Python maintains a strict way of order and format of scripting.Following this sometimes m 5 min read Python Naming Conventions Python, known for its simplicity and readability, places a strong emphasis on writing clean and maintainable code. One of the key aspects contributing to this readability is adhering to Python Naming Conventions. In this article, we'll delve into the specifics of Python Naming Conventions, covering 4 min read Python input() Function Python input() function is used to take user input. By default, it returns the user input in form of a string.input() Function Syntax: input(prompt)prompt [optional]: any string value to display as input messageEx: input("What is your name? ")Returns: Return a string value as input by the user.By de 4 min read Unpacking arguments in Python If you have used Python even for a few days now, you probably know about unpacking tuples. Well for starter, you can unpack tuples or lists to separate variables but that not it. There is a lot more to unpack in Python. Unpacking without storing the values: You might encounter a situation where you 3 min read Like