Python Articles

Page 828 of 855

Python Program to print the diamond shape

Pradeep Elance
Pradeep Elance
Updated on 30-Dec-2019 476 Views

The looping features in python can be used to create many nicely formatted diagrams using various characters from the keyboard. One such shape is diamond shape which will involve multiple loops. This is because we have to print the character both vertically and horizontally. Also we have to take care of the shape gradually growing from top till middle and then gradually shrinking from middle till the bottom. For this reason, we will use two for loops each containing one more for loop inside it.Below is the code for creating the diamond shape.Exampledef Shape_of_Diamond(shape): a = 0 for m in ...

Read More

Contingency Table in Python

Pavitra
Pavitra
Updated on 30-Dec-2019 2K+ Views

A contingency table is a table showing the distribution of one variable in rows and another variable in columns. It is used to study the correlation between the two variables. It is a multiway table which describes a dataset in which each observation belongs to one category for each of several variables. Also It is basically a tally of counts between two or more categorical variables. Contingency tables are also called crosstabs or two-way tables, used in statistics to summarize the relationship between several categorical variables.The contingency coefficient is a coefficient of association which tells whether two variables or datasets ...

Read More

Python-program-to-convert-pos-to-sop

Pavitra
Pavitra
Updated on 24-Dec-2019 287 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given pos form we need to convert it into its equivalent sop formThe conversion can be done by first counting the number of alphabets in the pos form and then calculating all the max and the minterms.Now let’s observe the concept in the implementation below−Example# Python code to convert standard POS form # to standard SOP form # to calculate number of variables def count_no_alphabets(POS):    i = 0    no_var = 0    # total no. of alphabets before will be ...

Read More

Python Program for Subset Sum Problem

Pavitra
Pavitra
Updated on 20-Dec-2019 2K+ Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a set of non-negative integers in an array, and a value sum, we need to determine if there exists a subset of the given set with a sum equal to a given sum.Now let’s observe the solution in the implementation below −# Naive approachExampledef SubsetSum(set, n, sum) :    # Base Cases    if (sum == 0) :       return True    if (n == 0 and sum != 0) :       return False    # ...

Read More

Python Program for Sieve of Eratosthenes

Pavitra
Pavitra
Updated on 20-Dec-2019 2K+ Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a number n, we need to print all primes smaller than or equal to n. Constraint: n is a small number.Now let’s observe the solution in the implementation below −Exampledef SieveOfEratosthenes(n):    # array of type boolean with True values in it    prime = [True for i in range(n + 1)]    p = 2    while (p * p

Read More

Python Program for Odd-Even Sort / Brick Sort

Pavitra
Pavitra
Updated on 20-Dec-2019 306 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given an array, we need to sort it using brick sort.Here we have two phases: Odd and Even Phase. In the odd phase, bubble sort is performed on odd indexed elements and in the even phase, bubble sort is performed on even indexed elements.Now let’s observe the solution in the implementation below−Exampledef oddEvenSort(arr, n):    # flag    isSorted = 0    while isSorted == 0:       isSorted = 1       temp = 0       ...

Read More

Python Program for Merge Sort

Pavitra
Pavitra
Updated on 20-Dec-2019 783 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given an array, we need to sort it using the concept of merge sortHere we place the maximum element at the end. This is repeated until the array is sorted.Now let’s observe the solution in the implementation below −Example#merge function def merge(arr, l, m, r):    n1 = m - l + 1    n2 = r- m    # create arrays    L = [0] * (n1)    R = [0] * (n2)    # Copy data to arrays   ...

Read More

How to remove tabs and newlines using Python regular expression?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 19-Dec-2019 1K+ Views

The following code removes tabs and newlines from given stringExampleimport re print re.sub(r"\s+", " ", """I find Tutorialspoint helpful""")OutputThis gives outputI find Tutorialspoint helpful

Read More

How to compare two strings using regex in Python?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 19-Dec-2019 2K+ Views

We can compare given strings using the following codeExampleimport re s1 = 'Pink Forest' s2 = 'Pink Forrest' if bool(re.search(s1,s2))==True:    print 'Strings match' else:    print 'Strings do not match'OutputThis gives the outputStrings do not match

Read More

How to divide a string by line break or period with Python regular expressions?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 16-Dec-2019 423 Views

The following code splits given string by a period and a line break as followsExampleimport re s = """Hi. It's nice meeting you. My name is Jason.""" result = re.findall(r'[^\s\.][^\.]+', s) print resultOutputThis gives the following output['Hi', "It's nice meeting you", 'My name is Jason']

Read More
Showing 8271–8280 of 8,547 articles
« Prev 1 826 827 828 829 830 855 Next »
Advertisements