Python Articles - Page 905 of 1048

Python Program for Selection Sort

Pavitra
Updated on 11-Sep-2019 13:18:05

2K+ Views

In this article, we will learn about the Selection sort and its implementation in Python 3.x. Or earlier.In the 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 sortedThe 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 algorithm ... Read More

Python program for removing nth character from a string

Pavitra
Updated on 11-Sep-2019 13:13:35

275 Views

In this article, we will learn about the solution to the problem statement given below −Problem statement − We are given a string, we have to remove the ith indexed character from the given string and display it.In any string in Python, indexing always starts from 0. Suppose we have a string “tutorialspoint” then its indexing will be done as shown below −T u t o r i a l s p o i n t 0 1 2 3 4 5 6 7 8 9 10 11 12 13Now let’s see the Python script for ... Read More

Python Program for Product of unique prime factors of a number

Pavitra
Updated on 11-Sep-2019 13:10:13

1K+ Views

In this article, we will learn about the solution to the problem statement given below −Problem statement − Given a number n, we need to find the product of all of its unique prime factors available and return it.For example, Input: num = 11 Output: Product is 11 Explanation: Here, the input number is 11 having only 1 prime factor and it is 11. And hence their product is 11.Approach 1Using a for loop from i = 2 to n+1 check whether i is a factor of n & then check if i is the prime number itself, if yes ... Read More

Python Program for Print Number series without using any loop

Pavitra
Updated on 11-Sep-2019 13:04:11

736 Views

In this article, we will learn about the solution to the problem statement given below −Problem statement − Given Two number N and K, our problem is to subtract a number K from N until number(N) is greater than zero(0), once the N becomes negative or zero then we start adding K to it until that number become the original number(N).For example, N = 10 K = 4 Output will be: 10 6 2 -2 2 6 10Algorithm1. we call the function again and again until N is greater than zero (in every function    call we subtract K ... Read More

Python Program for n-th Fibonacci number

Pavitra
Updated on 11-Sep-2019 12:15:41

1K+ Views

In this article, we will compute the nth Fibonacci number.A Fibonacci number is defined by the recurrence relation given below −Fn = Fn-1 + Fn-2With F0= 0 and F1 = 1.First, few Fibonacci numbers are0,1,1,2,3,5,8,13,..................We can compute the Fibonacci numbers using the method of recursion and dynamic programming.Now let’s see the implementation in the form of Python scriptApproach 1: Recursion MethodExample Live Demo#recursive approach def Fibonacci(n):    if n

Python Program for nth Catalan Number

Pavitra
Updated on 11-Sep-2019 12:10:47

546 Views

In this article, we will learn about calculating the nth Catalan number.Catalan numbers are a sequence of natural numbers that are defined by the recursive formula −$$C_{0}= 1\:and\:C_{n+1}=\displaystyle\sum\limits_{i=0}^n C_{i}C_{n-i} for \:n\geq0;$$The first few Catalan numbers for n = 0, 1, 2, 3, … are 1, 1, 2, 5, 14, 42, 132,429,...................Catalan numbers can be obtained both by recursion and dynamic programming. So let’s see their implementation.Approach 1: Recursion MethodExample Live Demo# A recursive solution def catalan(n):    #negative value    if n

Python Program for How to check if a given number is a Fibonacci number?

Pavitra
Updated on 11-Sep-2019 11:56:56

596 Views

In this article, we will learn about the solution to the problem statement given below −Problem statementGiven a number n, check whether n is a Fibonacci number or notWe all are aware that the nth Fibonacci number is the sum of the previous two Fibonacci numbers. But they also offer an interesting relation other than the recurrence relation.A number is Fibonacci in nature if and only if (5*n2 + 4) or (5*n2 – 4) is a perfect square.We will use this property to check whether a number is Fibonacci or not.Now let’s see the implementation of the Python script −Example Live ... Read More

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

Pavitra
Updated on 11-Sep-2019 11:53:55

1K+ Views

In this article, we will learn about the solution to the problem statement given below −Problem statement − We 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, ... Read More

Python Program for the focal length of a spherical mirror

Pavitra
Updated on 11-Sep-2019 11:51:14

432 Views

In this article, we will learn about the solution to the problem statement given below −Problem statementWe will be given the radius of curvature of the spherical mirror and we have to find the focal length of the same.The focal length is the distance between the centre of curvature of the mirror to the principal foci. In order to determine the focal length of a spherical mirror firstly, we should know the radius of curvature of that mirror. The distance from the vertex of the mirror to the centre of curvature is called the radius of curvature.Mathematically −For concave mirror: ... Read More

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

Pavitra
Updated on 11-Sep-2019 11:48:11

1K+ 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

Advertisements