Pavitra

Pavitra

123 Articles Published

Articles by Pavitra

Page 10 of 13

Iterate over characters of a string in Python

Pavitra
Pavitra
Updated on 01-Jul-2020 2K+ Views

In this article, we will learn about iterating/ traversing over characters of a string in Python 3.x. Or earlier.The string is a collection of characters which may contain spaces, alphabets or integers. They can be accessed using indexes or via references . Some commonly implemented methods are shown below.Method 1 − The direct itertor without indexingExamplestring_inp = "tutorialspoint" # Iterate over the string for value in string_inp:    print(value, end='')Method 2 − The most common way using index based accessExamplestring_inp = "tutorialspoint" # Iterate over the string for value in range(0, len(string_inp)):    print(string_inp[value], end='')Method 3 − The ...

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 294 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 314 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 790 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

Python program to find the most occurring character and its count

Pavitra
Pavitra
Updated on 26-Sep-2019 536 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven an input string we need to find the most occurring character and its count.ApproachCreate a dictionary using Counter method having strings as keys and their frequencies as values.Find the maximum occurrence of a character i.e. value and get the index of it.Now let’s see the implementation below −Examplefrom collections import Counter    def find(input_):    # dictionary    wc = Counter(input_)    # Finding maximum occurrence    s = max(wc.values())    i = wc.values().index(s)    print (wc.items()[i]) # Driver program if __name__ ...

Read More

Python Program to find the sum of array

Pavitra
Pavitra
Updated on 26-Sep-2019 1K+ Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven an array as an input, we need to compute the sum of the given array.Here we may follow the brute-force approach i.e. traversing over a list and adding each element to an empty sum variable. Finally, we display the value of the sum.We can also perform an alternate approach using built-in sum function as discussed below.Example# main arr = [1, 2, 3, 4, 5] ans = sum(arr, n) print ('Sum of the array is ', ans)Output15 All the variables & functions are ...

Read More

Insertion Sort in Python Program

Pavitra
Pavitra
Updated on 26-Sep-2019 1K+ Views

In this article, we will learn about the implementation of Insertion sort in Python 3.x. Or earlier.AlgorithmIterate over the input elements by growing the sorted array at each iteration.Compare the current element with the largest value available in the sorted array.If the current element is greater, then it leaves the element in its place and moves on to the next element else it finds its correct position in the sorted array and moves it to that position in the array.This is achieved by shifting all the elements towards the right, which are larger than the current element, in the sorted ...

Read More
Showing 91–100 of 123 articles
« Prev 1 8 9 10 11 12 13 Next »
Advertisements