Python Articles - Page 902 of 1048

Python program to check if a given string is number Palindrome

Pavitra
Updated on 26-Sep-2019 12:22:39

7K+ Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven a string input, we need to create a python function to check whether it is a palindrome or not.A string is referred to be palindrome if the reverse of the string is identical with string.We can do this by two methods −Reversal by slicingComparison via negative indexingHere we will be learning reversal pf string bu the help of slicing.To reverse a string by th method of slicing specify the following statement −Str[ : : -1 ]Where the start and end parameters are ... Read More

Python Program to check if the given array is Monotonic

Pavitra
Updated on 26-Sep-2019 12:17:17

1K+ Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven an array input Arr containing n integers. We need to check whether the input array is Monotonic in nature or not.An array is said to be monotonic in nature if it is either continuously increasing or continuously decreasing.Mathematically,An array A is continuously increasing if for all i

Python program to check if a string contains all unique characters

Pavitra
Updated on 26-Sep-2019 12:14:14

723 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven a sring input, we need to find whether a string contains all unique characters or not.ApproachWe will create an array of boolean values, where the variable flag at the index i indicates that whether character i in the alphabet is contained in the string or not.The second time we encounter this character we can immediately return false as string characters is no longer unique.We can also return false if the string length exceeds the value of number of unique characters presnt in ... Read More

Python Program to Check Armstrong Number

Pavitra
Updated on 26-Sep-2019 12:11:58

2K+ Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven an integer n , we need to check that the given integer is an armstrong number.A positive integer is called an Armstrong number of order n ifabcd... = a^n + b^n + c^n + d^n + …Here we will be discussing the brute-force approach for an armstrong number of 3 digits and hence of order three.To check the armstrong number of order n we need to replace 3 by the corresponding order value in line number 7.Now let’s see the implementation −Example Live ... Read More

Python Program for Sum of squares of first n natural numbers

Pavitra
Updated on 26-Sep-2019 12:04:49

902 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven a positive integer N as input . We need to compute the value of 12 + 22 + 32 + ….. + N2.Problem statement:This can be solved by two methodsMultiplication addition arithmeticUsing mathematical formulaApproach 1: Multiplication & Addition ArithmeticHere we run a loop from 1 to n and for each i, 1

Python Program for Smallest K digit number divisible by X

Pavitra
Updated on 26-Sep-2019 12:00:10

458 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementIntegers n and d are given. We need to find the smallest n-digit number divisible by d.Approach1. FirstNow let's we compute MIN : smallest n-digit number (1000...n-times)2. Now, If MIN % X is 0, ans = MIN3. else, ans = (MIN + X) - ((MIN + X) % X))This is because there will be a number in range [MIN...MIN+X] which is divisible by d.Now let’s see the implementation −Example Live Demodef answer(n, d):    # Computing MAX    Min = pow(10, d-1)    if(Min%n ... Read More

Simple interest in Python Program

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

292 Views

In this article, we will learn about the calculation of simple interest in Python 3.x. Or earlier.Simple interestis calculated by multiplying the daily interest rate by the principal amount by the number of days that elapse between the payments.Mathematically, Simple Interest = (P x T x R)/100Where, P is the principal amountT is the time andR is the rateFor Example, If P = 1000, R = 1, T = 2Then SI=20.0Now let’s see how we can implement a simple interest calculator in Python.Example Live DemoP = 1000 R = 1 T = 2 # simple interest SI = (P * R ... Read More

Selection Sort in Python Program

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

389 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

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

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

Advertisements