Found 35164 Articles for Programming

Python program to reverse bits of a positive integer number?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:23

2K+ Views

First convert number into binary using bin() function. Then skip the first two character of binary representation because bin() appends 0b as a prefix in a binary representation of the number and reverse the remaining part. From also character and reverse it till second last character from left. Convert a reversed binary string into integer. Algorithm integernumber(n, bit_size) /* n is the number and bit_size is the bitsize */ Step 1: first convert number into binary . Step 2: skip the first two characters of binary representation string and reverse. Step 3: remaining string and then append 0’s ... Read More

Python program to check if binary representation is palindrome?

Samual Sam
Updated on 30-Jul-2019 22:30:23

591 Views

Here we use different python inbuilt function. First we use bin() for converting number into it’s binary for, then reverse the binary form of string and compare with originals, if match then palindrome otherwise not. Example Input: 5 Output: palindrome Explanation Binary representation of 5 is 101 Reverse it and result is 101, then compare and its match with originals. So its palindrome Algorithm Palindromenumber(n) /* n is the number */ Step 1: input n Step 2: convert n into binary form. Step 3: skip the first two characters of a string. Step 4: them reverse the ... Read More

Python program to find the length of the largest consecutive 1's in Binary Representation of a given string.

Arjun Thakur
Updated on 30-Jul-2019 22:30:23

386 Views

Given the number, find length of the longest consecutive 1's in its binary representation. Example Input: n = 15 Output: 4 The binary representation of 14 is 1111. Algorithm Step 1: input the number. Step 2: use one counter variable c=0. Step 3: Count the number of iterations to reach i = 0. Step 4: This operation reduces length of every sequence of 1s by one. Example code # Python program to find # length of the longest # consecutive 1s in # binary representation of a number. def maxlength(n): # ... Read More

Python program to print the initials of a name with last name in full?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:23

4K+ Views

Here we use different python inbuilt function. First we use split().split the words into a list. Then traverse till the second last word and upper() function is used for print first character in capital and then add the last word which is title of a name and here we use title(), title function converts the first alphabet to capital. Example Input Pradip Chandra Sarkar Output P.C Sarkar Algorithm fullname(str1) /* str1 is a string */ Step 1: first we split the string into a list. Step 2: newspace is initialized by a space(“”) Step 3: then traverse ... Read More

Python program to split the even and odd elements into two different lists.

Chandu yadav
Updated on 30-Jul-2019 22:30:23

20K+ Views

In this program we create a user input list and the elements are mixture of odd and even elements. Our task is to split these list into two list. One contains odd number of element and another is even number of elements. Example Input: [1, 2, 3, 4, 5, 9, 8, 6] Output Even lists: [2, 4, 8, 6] Odd lists: [1, 3, 5, 9] Algorithm Step 1 : create a user input list. Step 2 : take two empty list one for odd and another for even. Step 3 : then traverse each element in the ... Read More

SequenceMatcher in Python for Longest Common Substring.

Samual Sam
Updated on 30-Jul-2019 22:30:23

734 Views

Given two strings, our task is to print the longest common sub-string. We will solve problem in python using SequenceMatcher.find_longest_match () method. Class difflib.SequenceMatcher is a flexible class for comparing pairs of sequences of any type, so long as the sequence elements are hashable. find_longest_match(a, x, b, y) Find longest matching block in a[a:x] and b[b:y]. Examples Input: str1 = "pythonprogramming", str2 = "pro" Output: pro Algorithm Step 1: Enter two string. Step 2: initialize SequenceMatcher object with the input string. Step 3: find the match ... Read More

Python program to find Maximum and minimum element’s position in a list?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:23

14K+ Views

In python is very easy to find out maximum, minimum element and their position also. Python provides different inbuilt function. min() is used for find out minimum value in an array, max() is used for find out maximum value in an array. index() is used for finding the index of the element. Algorithm maxminposition(A, n) /* A is a user input list and n is the size of the list.*/ Step 1: use inbuilt function for finding the position of minimum element. A.index(min(A)) Step 2: use inbuilt ... Read More

Difference between == and is operator in python.

George John
Updated on 30-Jul-2019 22:30:23

714 Views

is and equals(==) operators are mostly same but they are not same. is operator defines if both the variables point to the same object whereas the == sign checks if the values for the two variables are the same. Example Code # Python program to # illustrate the # difference between # == and is operator # [] is an empty list list1 = [] list2 = [] list3=list1 if (list1 == list2): print("True") else: print("False") if (list1 is list2): print("True") else: print("False") if (list1 is list3): print("True") else: print("False") Output True False True

Zip function in Python to change to a new character set.

Samual Sam
Updated on 30-Jul-2019 22:30:23

62 Views

Given a 26 letter character set, here we are using a new character set. And another character set just like alphabet set (a, b, c........z), then our task is to make a relation between new character set and that alphabet set. Example New character set: qwertyuiopasdfghjklzxcvbnm Input: "wwmm" Output: bbzy Algorithm Step 1: Given a new character set and input the string to make a relation. Step 2: and the original character set also given below. Step 3: Create a dictionary, we use here map technique, we map the English character set and new given character set, ... Read More

Python program to check if binary representation of two numbers are anagram.

Ankith Reddy
Updated on 30-Jul-2019 22:30:23

543 Views

Given two numbers. Our task is to check whether they are anagrams of each other or not in binary representation. We can solve this problem quickly in python using Counter (iterable) method and Dictionary Comparison. Examples Input: a = 8, b = 16 Output : Yes Binary representations of both numbers have same 0s and 1s. Algorithm Step 1 : Given two numbers. Step 2 : Convert both number into its binary using bin() function and remove first two characters because of bin(). Step 3 : Since binary representation of both numbers could differ in length ... Read More

Advertisements