Found 10476 Articles for Python

Python program to print palindrome triangle with n lines

Arnab Chakraborty
Updated on 12-Oct-2021 08:16:08

4K+ Views

Suppose we have a number n. We have to find a triangle with n rows and each row contains palindrome.So, if the input is like n = 5, then the output will be1 121 12321 1234321 123454321To solve this, we will follow these steps −for i in range 1 to n, dodisplay ((integer part of (10^i) - 1)/9)^2go to next lineExampleLet us see the following implementation to get better understanding −def solve(n): for i in range(1,n+1): print((((10**i) - 1)//9)**2) n = 8 solve(n)Input8 Output 1 121 12321 1234321 123454321 12345654321 1234567654321 123456787654321

Program to find nth Fibonacci term in Python

Arnab Chakraborty
Updated on 12-Oct-2021 08:14:32

6K+ Views

Suppose we have a number n. We have to find the nth Fibonacci term by defining a recursive function.So, if the input is like n = 8, then the output will be 13 as first few Fibonacci terms are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34...To solve this, we will follow these steps −Define a function solve() . This will take nif n

Python program to check whether we can pile up cubes or not

Arnab Chakraborty
Updated on 12-Oct-2021 08:14:01

310 Views

Suppose we have an array nums containing size of n different cubes, they are placed horizontally. We have to make a pile of cubes vertically. The new cube should follow −if ith cube is on top of jth cube, then side length of jth one must be greater or equal to side length of ith one.When we are making the vertical pile, we can only take cubes from left side or right side but not from the middle. We have to check whether we can pile them up or not.So, if the input is like nums = [1, 2, 3, ... Read More

Program to compute gcd of two numbers recursively in Python

Arnab Chakraborty
Updated on 12-Oct-2021 08:11:17

8K+ Views

Suppose we have two numbers a and b. We have to find the GCD of these two numbers in recursive way. To get the GCD we shall use the Euclidean algorithm.So, if the input is like a = 25 b = 45, then the output will be 5To solve this, we will follow these steps −Define a function gcd() . This will take a, bif a is same as b, thenreturn aotherwise when a < b, thenreturn gcd(b, a)otherwise, return gcd(b, a - b)ExampleLet us see the following implementation to get better understanding −def gcd(a, b):    if a == ... Read More

Python program to find top three mostly occurred letters from company name

Arnab Chakraborty
Updated on 12-Oct-2021 08:09:00

607 Views

Suppose we have a company name as string. We have to find the most common three characters from the company name and show them by following these rules −Pick most frequent three lettersSort them in descending orderIf the frequencies of some characters are same then take by their alphabetical orderSo, if the input is like s = "TUTORIALSPOINT", then the output will be [[3, 'T'], [2, 'I'], [2, 'O']]To solve this, we will follow these steps −x := a map containing letters and frequencies of letters in sres := a new listfor each i in x, doinsert pair (x[i], i) ... Read More

Program to find area of a polygon in Python

Arnab Chakraborty
Updated on 12-Oct-2021 08:07:29

3K+ Views

Suppose we have a list of ordered points represents a simple polygon endpoint on a 2D plane. We have to find the area of this polygon.So, if the input is like points = [(0, 0), (0, 5), (3, 5), (3, 0)], then the output will be 15.To solve this, we will follow these steps −Define a function getInfo() . This will take x1, y1, x2, y2return x1*y2 - y1*x2From the main method, do the followingN := size of points(firstx, firsty) := points[0](prevx, prevy) := (firstx, firsty)res := 0for i in range 1 to N-1, do(nextx, nexty) := points[i]res := res ... Read More

Python program to count pairs for consecutive elements

Arnab Chakraborty
Updated on 12-Oct-2021 08:05:34

570 Views

Suppose we have a numeric string s contains few digits. The digits may occur multiple times. We have to return some pairs (digit, count) represents which digit has occurred consecutively how many times in s. To solve this problem we can use the groupby() function that comes under itertools library. This will return one iterator object inside that each item will be at first place and another groupby objects at the second place. We have to count number of groupby objects for each pair.So, if the input is like s = "11522226551", then the output will be [(1, 2), (5, ... Read More

Python program to count distinct words and count frequency of them

Arnab Chakraborty
Updated on 12-Oct-2021 08:02:11

877 Views

Suppose we have a list of words. These words may occur multiple times. We have to show the frequencies of these words and count how many distinct words are there.So, if the input is like words = ["Book", "Sound", "Language", "Computer", "Book", "Language"], then the output will be (4, '2 1 2 1') because there are four distinct words, the first and third words have occurred twice.To solve this, we will follow these steps −d:= an OrderedDict to store items based on insert orderfor each w in words, doif w is in d, thend[w] := d[w] + 1otherwise, d[w] := ... Read More

Program to find perimeter of a polygon in Python

Arnab Chakraborty
Updated on 12-Oct-2021 08:02:08

2K+ Views

Suppose we have a list of ordered points represents a simple polygon endpoint on a 2D plane. We have to find the perimeter of this polygon.So, if the input is like points = [(0, 0), (0, 5), (3, 5), (3, 0)], then the output will be 16 becausetwo sides are of length 3 and two sides are of length 5, so 2*5 + 2*3 = 16.To solve this, we will follow these steps −Define a function getInfo() . This will take x1, y1, x2, y2return square root of ((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)) which is Euclidean distancebetween (x1, y1) and (x2, y2)From the main ... Read More

Program to check whether domain and range are forming function or not in Python

Arnab Chakraborty
Updated on 12-Oct-2021 07:56:35

497 Views

Suppose we have a list of data say x, represents a domain and a list of data y (size of y is same as size of x), represents a range. We have to check whether x -> y is a function or not. Here we are considering all elements in x and y are positive.So, if the input is like x = [1, 3, 2, 6, 5] y = [1, 9, 4, 36, 25], then the output will be True, because for each x, the corresponding y is its square value here, so this is a function.To solve this, we ... Read More

Advertisements