Pradeep Elance

Pradeep Elance

302 Articles Published

Articles by Pradeep Elance

Page 15 of 31

Maximum length of consecutive 1’s in a binary string in Python using Map function

Pradeep Elance
Pradeep Elance
Updated on 11-Mar-2026 498 Views

Sometimes when dealing with the binary representation of numbers we may be needed to find out how many continuous 1’s are present in the number. This article shows two ways how we can find out that.Using Split and MapThe split function in python can be used to split the given string into multiple strings. We split it by zeros and the map function is used to find the maximum length among the splits generated.Exampledata = '11110000111110000011111010101010101011111111' def Max_len_cons_1(data): print ("Maximum Number of consecutive one's: ", max(map(len, data.split('0'))) ) Max_len_cons_1(data)OutputRunning the above code gives us the following result −Maximum Number of ...

Read More

Prime or not in Python

Pradeep Elance
Pradeep Elance
Updated on 11-Mar-2026 808 Views

Prime numbers play a central role in many e it applications like cryptography. So it is a necessity to check for prime numbers using Python programs in various applications. A prime number is a number which doesn't have any factors other than one and itself. Below will see programs that can find out if a given number is prime or not.ApproachWe take the following approach to decide whether a number is prime or not.Check at the beginning is positive or not. As only positive numbers can be prime numbers.We divide the number with all the numbers in the range of ...

Read More

Print first n distinct permutations of string using itertools in Python

Pradeep Elance
Pradeep Elance
Updated on 11-Mar-2026 438 Views

Permutation of a number of objects is the representation of how the updates can be present in different sequences. But sometimes we may have two objects in a series of given objects which are identical. In that case two sequences will become equal. In this article will see how to represent only the unique sequences from a given list of objects.The module itertools have a method called permutations which help us achieve this. To get the unique permutations we take help of the set method which stores only the distinct elements. But before that we get the elements in the ...

Read More

Print number with commas as 1000 separators in Python

Pradeep Elance
Pradeep Elance
Updated on 11-Mar-2026 555 Views

Many times the numbers with three or more digits need to be represented suitably using comma. This is a requirement mainly in the accounting industry as well as in the finance domain. In this article we'll see how Python program can be used to insert a comma at a suitable place. We are aiming to insert comma as a thousand separator.Format FunctionThe format function in python can be used with below settings to achieve this requirement.(f"{num:, d}") : is the format specifier D is the thousand separatorExample - Integersprint(f'{1445:, d}') print(f'{140045:, d}')OutputRunning the above code gives us the following result ...

Read More

Programs for printing pyramid patterns in Python

Pradeep Elance
Pradeep Elance
Updated on 11-Mar-2026 2K+ Views

Taking advantage of the for loop and range function in python, we can draw a variety of for pyramid structures. The key to the approach is designing the appropriate for loop which will leave both vertical and horizontal space for the position of the symbol we choose for drawing the pyramid structure.Pattern -1We draw a right angle based pattern.Exampledef pyramid(p):    for m in range(0, p):       for n in range(0, m+1):          print("* ", end="")       print("\r") p = 10 pyramid(p)OutputRunning the above code gives us the following result −* * * ...

Read More

Accessing all elements at given Python list of indexes

Pradeep Elance
Pradeep Elance
Updated on 11-Mar-2026 391 Views

We can access the individual elements of a list using the [] brackets and the index number. But when we need to access some of the indices then we cannot apply this method. We need the below approaches to tackle this.Using two listsIn this method, along with the original list, we take the indices as another list. Then we use a for loop to iterate through the indices and supply those values to the main list for retrieving the values.Examplegiven_list = ["Mon", "Tue", "Wed", "Thu", "Fri"] index_list = [1, 3, 4] # printing the lists print("Given list : " ...

Read More

Possible Words using given characters in Python

Pradeep Elance
Pradeep Elance
Updated on 11-Mar-2026 916 Views

In this article we are going to see a python program that will give output of possible words from a given set of characters. Here we are taking a list as an input which will contain the set of reference words and another list containing the characters from which the words are made up of.In the below program, we define two functions. One to take the letters from the second list and make up words. Another function to match the words formed with the words present in the given list of words.Exampledef Possible_Words(character):    x = {}    for n ...

Read More

Python - Check if dictionary is empty

Pradeep Elance
Pradeep Elance
Updated on 11-Mar-2026 6K+ Views

During analysis of data sets we may come across situations where we have to deal with empty dictionaries. In tis article we will see how to check if a dictionary is empty or not.Using ifThe if condition evaluates to true if the dictionary has elements. Otherwise it evaluates to false. So in the below program we will just check the emptiness of a dictionary using only the if condition.Exampledict1 = {1:"Mon", 2:"Tue", 3:"Wed"} dict2 = {} # Given dictionaries print("The original dictionary : " ,(dict1)) print("The original dictionary : " ,(dict2)) # Check if dictionary is empty if dict1:   ...

Read More

Python - Check if frequencies of all characters of a string are different

Pradeep Elance
Pradeep Elance
Updated on 11-Mar-2026 258 Views

In this article we will see how to find the frequency of each character in a given string. Then see if two or more characters have the same frequency in the given string or not. We will accomplish this in two steps. In the first program we will just find out the frequency of each character.Frequency of each characterHere we find the frequency of each character in the given input screen. We declare a empty dictionary and then add each character as a string. We also assign keys to each of the character to create the key-value pair needed by ...

Read More

Python - Clearing list as dictionary value

Pradeep Elance
Pradeep Elance
Updated on 11-Mar-2026 204 Views

In this article we consider a dictionary where the values are presented as lists. Then we consider clearing those values from the lists. We have two approaches here. One is to use the clear methods and another is to designate empty values to each key using list comprehension.Examplex1 = {"Apple" : [4, 6, 9, 2], "Grape" : [7, 8, 2, 1], "Orange" : [3, 6, 2, 4]} x2 = {"mango" : [4, 6, 9, 2], "pineapple" : [7, 8, 2, 1], "cherry" : [3, 6, 2, 4]} print("The given input is : " + str(x1)) # using loop + clear() ...

Read More
Showing 141–150 of 302 articles
« Prev 1 13 14 15 16 17 31 Next »
Advertisements