Programming Articles - Page 1002 of 3363

Program to generate Pascal's triangle in Python

Arnab Chakraborty
Updated on 12-Oct-2021 13:28:50

15K+ Views

Suppose we have a number n. We have to generate Pascal's triangle up to n lines. The Pascal's triangle will be look like this −The property of Pascal's triangle is the sum of each adjacent two numbers of previous row is the value of the number which is placed just below on the second row. For example, the first 10 at row 6 is sum of 4 and 6 at row 5 and second 10 is sum of two numbers 6 and 4 at row 5.So, if the input is like n = 5, then the output will be ... Read More

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

345 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

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

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

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

Python program to count distinct words and count frequency of them

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

895 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

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

Advertisements