Print Palindrome Triangle with N Lines in Python

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

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

Check Whether We Can Pile Up Cubes in Python

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

346 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

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

Find Top Three Most Occurred Letters from Company Name in Python

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

634 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

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

Count Pairs for Consecutive Elements in Python

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

605 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

Count Distinct Words and Their Frequency in Python

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

896 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

Find Perimeter of a Polygon in Python

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

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

Find Happiness by Checking Participation of Elements into Sets in Python

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

365 Views

Suppose we have an array nums with n different integers. We also have two disjoint sets A and B. We have one happiness parameter which is set to 0 initially. We go through each integer i in nums. If i is in A then add happiness by 1 and if i is in B decrease it by 1. We have to finally find the final happiness value.So, if the input is like nums = [1, 2, 5, 8, 6, 3] A = {5, 8, 9, 7, 3} B = {2, 4, 12, 15}, then the output will be 2 because ... Read More

Advertisements