Found 10805 Articles for Python

Selection Sort in Python Program

Pavitra
Updated on 26-Sep-2019 11:49:07

236 Views

In this article, we will learn about the Selection sort and its implementation in Python 3.x. Or earlier.In selection sort algorithm, an array is sorted by recursively finding the minimum element from the unsorted part and inserting it at the beginning. Two subarrays are formed during the execution of Selection sort on a given array.The subarray , which is already sorted.The subarray , which is unsorted.During every iteration of selection sort, the minimum element from the unsorted subarray is popped and inserted into the sorted subarray.Let’s see the visual representation of the algorithm −Now let’s see the implementation of the ... Read More

Linear Search in Python Program

Pavitra
Updated on 26-Sep-2019 08:11:37

13K+ Views

In this article, we will learn about the Linear Search and its implementation in Python 3.x. Or earlier.AlgorithmStart from the leftmost element of given arr[] and one by one compare element x with each element of arr[]If x matches with any of the element, return the index value.If x doesn’t match with any of elements in arr[] , return -1 or element not found.Now let’s see the visual representation of the given approach −Example Live Demodef linearsearch(arr, x):    for i in range(len(arr)):       if arr[i] == x:          return i    return -1 arr = ... Read More

Insertion Sort in Python Program

Pavitra
Updated on 26-Sep-2019 08:05:33

889 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

GCD of more than two (or array) numbers in Python Program

Pavitra
Updated on 26-Sep-2019 07:59:28

119 Views

In this article, we will learn about the solution to the problem statement given below −Problem statementWe will be given an array of number and we need to find the greatest common divisor.If we need to find gcd of more than two numbers, gcd is equal to the product of the prime factors common to all the numbers provided as arguments. It can also be calculated by repeatedly taking the GCDs of pairs of numbers of arguments.Here we will be implementing the latter approachSo now, let’s see the implementationExample Live Demodef findgcd(x, y):    while(y):       x, y = ... Read More

Finding the vertex, focus and directrix of a parabola in Python Program

Pavitra
Updated on 26-Sep-2019 07:46:33

107 Views

In this article, we will learn about the solution to the problem statement given below −Problem statementThe standard form of a parabola equation is y=ax^2+bx+c. Input the values of a, b and c, our task is to find the coordinates of the vertex, focus and the equation of the directrix.The vertex of a parabola is the coordinate from which it takes the sharpest turn whereas y=a is the straight-line used to generate the curve.A directrix a fixed line used in describing a curve or surface.Now let’s see the implementation −Example Live Demodef findparabola(a, b, c):    print ("Vertex: (" , (-b ... Read More

Find the perimeter of a cylinder in Python Program

Pavitra
Updated on 26-Sep-2019 07:43:38

268 Views

In this article, we will learn about the solution to the problem statement given below −Problem statementInput diameter and height, find the perimeter of a cylinder.Perimeter is nothing but the side view of a cylinder i.e. a rectangle.Therefore Perimeter= 2 * ( h + d )here d is the diameter of the cylinderh is the height of the cylinderNow let’s see the implementationExample Live Demo# Function to calculate the perimeter of a cylinder def perimeter( diameter, height ) :    return 2 * ( diameter + height ) # main diameter = 5 ; height = 10 ; print ("Perimeter = ... Read More

Python Program for Find sum of Series with the n-th term as n^2 – (n-1)^2

Pavitra
Updated on 26-Sep-2019 07:34:20

227 Views

In this article, we will learn about the solution to the problem statement given below:Problem statementWe are given an integer input n and we need to sum of all n terms where the n-th term in a series as expressed below −Tn = n2 - (n-1)2We have direct formulas for computing the sum which includes the squared muktiolicaion of n which involves more time complexity . To reduce that we usr modular multiplication approach hereNow let's see the implementation −Example Live Demo# Python program to find sum of given # series. mod = 1000000007 def findSum(n):    return ((n % mod) ... Read More

Python Program for Find sum of odd factors of a number

Pavitra
Updated on 26-Sep-2019 07:32:00

483 Views

In this article, we will learn about the solution to the problem statement given below −Problem statementGiven a number input n, the task is to Find the sum of odd factors of a number.Here we first need to eliminate all the even factors.To remove all even factors, we repeatedly divide n till it is divisible by 2. After this step, we only get the odd factors of the number.Below is the implementation −Example Live Demoimport math def sumofoddFactors( n ):    #prime factors    res = 1    # ignore even factors    while n % 2 == 0:     ... Read More

Python Program for Find reminder of array multiplication divided by n

Pavitra
Updated on 26-Sep-2019 07:25:13

507 Views

In this article, we will learn about the solution to the problem statement given below −Problem statementGiven multiple numbers and a number input n, we need to print the remainder after multiplying all the number divisible by n.ApproachFirst, compute the remainder like arr[i] % n. Then multiply this remainder with the current result.After multiplication, again take the same remainder to avoid overflow. This is in accordance with distributive properties of modular arithmetic.( a * b) % c = ( ( a % c ) * ( b % c ) ) % cExample Live Demodef findremainder(arr, lens, n):    mul = ... Read More

Python Program for Find minimum sum of factors of number

Pavitra
Updated on 26-Sep-2019 07:18:50

496 Views

In this article, we will learn about the solution to the problem statement given below −Problem statementGiven a number input , find the minimum sum of factors of the given number.Here we will compute all the factors and their corresponding sum and then find the minimum among them.So to find the minimum sum of the product of number, we find the sum of prime factors of the product.Here is the iterative implementation for the problem −Example Live Demo#iterative approach def findMinSum(num):    sum_ = 0    # Find factors of number and add to the sum    i = 2    while(i * i

Advertisements