Found 10784 Articles for Python

Python Program for Basic Euclidean algorithms

Pavitra
Updated on 20-Dec-2019 05:25:22

421 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement− Given two numbers we need to calculate gcd of those two numbers and display them.GCD Greatest Common Divisor of two numbers is the largest number that can divide both of them. Here we follow the euclidean approach to compute the gcd i.e. to repeatedly divide the numbers and stop when the remainder becomes zero.Now let’s observe the solution in the implementation below −Example Live Demo# euclid algorithm for calculation of greatest common divisor def gcd(a, b):    if a == 0 :       ... Read More

Python Program for array rotation

Pavitra
Updated on 20-Dec-2019 05:23:17

115 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − Given a text and a pattern, we need to print all occurrences of pattern and its permutations (or anagrams) in text.Now let’s observe the solution in the implementation below −Example Live Demo# maximum value MAX = 300 # compare def compare(arr1, arr2):    for i in range(MAX):       if arr1[i] != arr2[i]:          return False    return True # search def search(pat, txt):    M = len(pat)    N = len(txt)    # countP pattern account    # countTW ... Read More

Python Program for Activity Selection Problem

Pavitra
Updated on 20-Dec-2019 05:14:29

804 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement− We are given n activities with their respective starting and finish times. We need to select the maximum number of activities that can be performed by a single person, provided he works on one activity at a time.Variable notationsN - Total number of activitiesS - An array that contains start time of all activitiesF - An array that contains finish time of all activitiesNow let’s observe the solution in the implementation below −# Greedy approachExample Live Demo# maximum number of activities that can be performed ... Read More

Python Program for 0-1 Knapsack Problem

Pavitra
Updated on 20-Dec-2019 05:11:38

4K+ Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given weights and values of n items, we need to put these items in a bag of capacity W up to the maximum capacity w. We need to carry a maximum number of items and return its value.Now let’s observe the solution in the implementation below −# Brute-force approachExample Live Demo#Returns the maximum value that can be stored by the bag def knapSack(W, wt, val, n):    # initial conditions    if n == 0 or W == 0 :     ... Read More

To print all elements in sorted order from row and column wise sorted matrix in Python

Pradeep Elance
Updated on 19-Dec-2019 12:44:33

174 Views

Sometimes we need all the elements of a matrix in a sorted order. But as a matrix is in form of rows and columns, we do not apply the usual sorting algorithms to get the result. Rather we use the below user defined functions to get the elements sorted.Example Live Demodef heapq(a, k, i):    greater = i    l = 2 * i + 1    r = 2 * i + 2    if l < k and a[i] < a[l]:       greater = l    if r < k and a[greater] < a[r]:       ... Read More

Python - Check if all elements in a List are same

Pradeep Elance
Updated on 27-Aug-2023 03:49:01

25K+ Views

Sometimes we come across the need to check if we have one single value repeated in a list as list elements. We can check for such scenario using the below python programs. There are different approaches.Using for LoopIn this method we grab the first element from the list and use a traditional for loop to keep comparing each element with the first element. If the value does not match for any element then we come out of the loop and the result is false.Example Live DemoList = ['Mon', 'Mon', 'Mon', 'Mon'] result = True # Get the first element first_element = ... Read More

Generate two output strings depending upon occurrence of character in input string in Python

Pradeep Elance
Updated on 19-Dec-2019 12:32:58

70 Views

In this program , we take a string and count the characters in it with certain condition. The first condition is to capture all those characters which occur only once and the second condition is to capture all the characters which occur more than once. Then we list them out.Below is the logical steps we are going to follow to get this result.Counter converts the strings into Dictionary which is having keys and value.Then separate list of characters occurring once and occurring more than once using the join()In the below program we take the input string andExample Live Demofrom collections import ... Read More

Find the first repeated word in a string in Python using Dictionary

Pradeep Elance
Updated on 19-Dec-2019 12:30:11

1K+ Views

In a given sentence there may be a word which get repeated before the sentence ends. In this python program, we are going to catch such word which is repeated in sentence. Below is the logical steps we are going to follow to get this result.Splits the given string into words separated by space.Then we convert these words into Dictionary using collectionsTraverse this list of words and check which first word has the frequency > 1Program - Find the Repeated WordIn the below program we use the counter method from the collections package to keep a count of the words.Example Live ... Read More

Exploring Correlation in Python

Pradeep Elance
Updated on 19-Dec-2019 12:26:02

420 Views

Correlation is a statistical term to measure the relationship between two variables. If the relationship is string, means the change in one variable reflects a change in another variable in a predictable pattern then we say that the variables are correlated. Further the variation in first variable may cause a positive or negative variation in the second variable. Accordingly, they are said to be positively or negatively correlated. Ideally the value of the correlation coefficient varies between -1 to +1.If the value is +1 or close to it then we say the variables are positively correlated. And they vary in ... Read More

Different messages in Tkinter - Python

Pradeep Elance
Updated on 19-Dec-2019 12:22:14

280 Views

Tkinter is the GUI module of python. It uses various message display options which are in response to the user actions or change in state of a running program. The message box class is used to display variety of messages like confirmation message, error message, warning message etc.Example-1The below example shows display of a message with the background colour, font size and colour etc customizable.import tkinter as tk main = tk.Tk() key = "the key to success is to focus on goals and not on obstacles" message = tk.Message(main, text = key) message.config(bg='white', font=('times', 32, 'italic')) message.pack() ... Read More

Advertisements