Found 34494 Articles for Programming

Python program to find Union of two or more Lists?

Samual Sam
Updated on 30-Jul-2019 22:30:23

484 Views

Union operation means, we have to take all the elements from List1 and List 2 and all the elements store in another third list. List1::[1, 2, 3] List2::[4, 5, 6] List3::[1, 2, 3, 4, 5, 6] Algorithm Step 1: Input two lists. Step 2: for union operation we just use + operator. Example code # UNION OPERATION A=list() B=list() n=int(input("Enter the size of the List ::")) print("Enter the Element of first list::") for i in range(int(n)): k=int(input("")) A.append(k) print("Enter the Element of second list::") for i in range(int(n)): ... Read More

Python program to count unset bits in a range.

Ankith Reddy
Updated on 30-Jul-2019 22:30:23

151 Views

Given a positive a number and the range of the bits. Our task is to count unset bits in a range. Input : n = 50, starting address = 2, ending address = 5 Output : 2 There are '2' unset bits in the range 2 to 5. Algorithm Step 1 : convert n into its binary using bin(). Step 2 : remove first two characters. Step 3 : reverse string. Step 4 : count all unset bit '0' starting from index l-1 to r, where r is exclusive. Example Code # ... Read More

Program to check if a number is Positive, Negative, Odd, Even, Zero?

Samual Sam
Updated on 23-Jun-2020 16:07:21

5K+ Views

Number is given, we have to check that the number is even or odd and positive or negative.AlgorithmStep 1: input number Step 2: check number is greater than equal to 0 or not. If true then positive otherwise negative and if it 0 then number is 0. Step 3: if number is divisible by 2 then it’s even otherwise its odd.Example code# Python program check if a number is Positive, Negative, Odd, Even, Zero n=int(input("Enter Number ::>")) if n >= 0:    if n == 0:       print("The Number Is Zero")    else:       print("This Is ... Read More

Python program to print all sublists of a list.

Arjun Thakur
Updated on 30-Jul-2019 22:30:23

5K+ Views

Given a list, print all the sublists of a list. Examples − Input : list = [1, 2, 3] Output : [], [1], [1, 2], [1, 2, 3], [2], [2, 3], [3]] Algorithm Step 1 : given a list. Step 2 : take one sublist which is empty initially. Step 3 : use one for loop till length of the given list. Step 4 : Run a loop from i+1 to length of the list to get all the sub arrays from i to its right. Step 5 : Slice the sub array from i to ... Read More

Concatenated string with uncommon characters in Python?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:23

726 Views

Here two strings are given, first we have to remove all the common element from the first string and uncommon characters of the second string have to be concatenated with uncommon element of first string. Example Input >> first string::AABCD Second string:: MNAABP Output >> CDMNP Algorithm Uncommonstring(s1, s2) /* s1 and s2 are two string */ Step 1: Convert both string into set st1 and st2. Step 2: use the intersection of two sets and get common characters. Step 3: now separate out characters in each string which are not common in both string. Step 4: ... Read More

Python program to print all the common elements of two lists.

Chandu yadav
Updated on 30-Jul-2019 22:30:23

873 Views

Given two lists, print all the common element of two lists. Examples − Input : L1 = [5, 6, 7, 8, 9] L2 = [5, 13, 34, 22, 90] Output : {5} Explanation The common elements of both the list is 5. Algorithm Step1 : create two user input lists. Step2 : Convert the lists to sets and then print set1&set2. Step3 : set1 and set2 returns the common elements set, where set1 is the list1 and set2 is the list2. Example Code # Python ... Read More

Python program to check if both halves of the string have same set of characters.

karthikeya Boyini
Updated on 23-Jun-2020 16:01:38

183 Views

Given a string, our task is to check if both halves of the string have the same set of characters or not. To solve this problem we first split the string from the middle, so we get two halves, now we check each halves having the same set of characters or not. If the length of the string is not even then ignore the middle element and check for the rest.AlgorithmStep 1: Given a string. Step 2: Break the input string into two parts. Step 3: Then convert both parts into a dictionary using Counter(iterator) method and each dictionary contains ... Read More

Python Program to calculate n+nm+nmm.......+n(m times).

Samual Sam
Updated on 23-Jun-2020 16:03:14

398 Views

Here n is given value which is positive number m is the number of times till which the series run. Our task is to calculate this series.AlgorithmStep 1: Input n, m; Step 2: Converting the number to string. Step 3: Initializing result as number and string. Step 4: Adding remaining terms. Step 5: Concatenating the string making n, nn, nnn... Step 6: Before adding converting back to integer. Step 7: return sum.Example Code# Python program to sum the series def sumofseries(n, m):    str1 = str(n)    sum1 = n    sumofstr1 = str(n)    for i in range(1, m): ... Read More

Python program to display Astrological sign or Zodiac sign for a given data of birth.

George John
Updated on 23-Jun-2020 16:02:25

3K+ Views

Given date of birth, our task is to display astrological sign or Zodiac sign.ExamplesInput : Day = 13, Month = November Output : Scorpio.AlgorithmStep 1 : input date of birth. Step 2 : checks month and date within the valid range of a specified zodiac. Step 3 : display zodiac sign.Example Codedef zodiac_sign(day, month):    # checks month and date within the valid range    # of a specified zodiac    if month == 'december':       astro_sign = 'Sagittarius' if (day < 22) else 'capricorn'    elif month == 'january':       astro_sign = 'Capricorn' if (day ... Read More

Python program to create 3D list.

karthikeya Boyini
Updated on 23-Jun-2020 16:03:40

2K+ Views

3D list means 3D array. In this program we create 3D array with integer elements.ExampleInput: 3× 3 × 2 [[1, 1, 1], [2, 2, 2], [3, 3, 3]], [[4, 4, 4], [5, 5, 5], [6, 6, 6]]AlgorithmStep 1: given the order of 3D list. Step 2: using for loop we create list and print data.Example Code# Python program to created 3D list import pprint def print3D(i, j, k):    lst = [[ ['*' for cc1 in range(i)] for cc2 in range(j)] for r in range(k)]    return lst    # Driver Code    c1 = 3    c2 = 2 ... Read More

Advertisements