Found 10784 Articles for Python

Python program to check if binary representation of two numbers are anagram.

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

566 Views

Given two numbers. Our task is to check whether they are anagrams of each other or not in binary representation. We can solve this problem quickly in python using Counter (iterable) method and Dictionary Comparison. Examples Input: a = 8, b = 16 Output : Yes Binary representations of both numbers have same 0s and 1s. Algorithm Step 1 : Given two numbers. Step 2 : Convert both number into its binary using bin() function and remove first two characters because of bin(). Step 3 : Since binary representation of both numbers could differ in length ... Read More

Print m multiplies of n without using any loop in Python.

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

197 Views

Given a number n, print m multiplies of n without using any loop. Here we use recursive function. Examples Input: n = 15 Output: 15 10 5 0 5 10 15 Algorithm Step 1: Given n. Step 2: If we are moving back toward the n and we have reached there, then we are done. Step 3: If we are moving toward 0 or negative. Step 4: If m is greater, then 5, recursive function with true flag else recursive function is false. Step 5: If m is not greater than 5 then flag is false. ... Read More

Python program to find missing and additional values in two lists?

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

1K+ Views

In set theory, the complement of a set A refers to elements not in A. the relative complement of A with respect to a set B, also termed the difference of Sets A and B. We just apply this principle here. Python has difference function. Algorithm Step 1 : first we create two user input list. A & B Step 2 : Insert A and B to a set. Step 3 : for finding the missing values of first list we apply difference function, difference of B from A. Step 4 : for finding the Additional ... Read More

Python program to check a sentence is a pangrams or not.

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

314 Views

Given a sentence. Our task is to check whether this sentence is pan grams or not. The logic of Pan grams checking is that words or sentences containing every letter of the alphabet at least once. To solve this problem we use set () method and list comprehension technique. Example Input: string = 'abc def ghi jkl mno pqr stu vwx yz' Output: Yes // contains all the characters from ‘a’ to ‘z’ Input: str='python program' Output: No // Does not contains all the characters from ‘a’ to 'z' Algorithm Step 1: create a string. Step 2: ... Read More

Python program to extract ‘k’ bits from a given position?

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

2K+ Views

This function is use to extract k bits from pos position and returns the extracted value. Here we use python slicing technique. Example Input:: number=170 K=5 Pos=2 Output=21 Algorithm Extractionbit(no, k, pos) /*user input number is stored in variable no, extracted bit is stored in variable k and the position of bit is pos. */ Step 1 : first convert the number into its binary form using bin(). Step 2 : remove the first two character. Step 3 : then extracting k bits from starting ... Read More

Python program to reverse each word in a sentence?

George John
Updated on 30-Jul-2019 22:30:23

3K+ Views

Here we use python built in function. First we split the sentence into a list of word. Then reverse each word and creating a new list ,here we use python list comprehension technique and last joining the new list of words and create an new sentence. Example Input :: PYTHON PROGRAM Output :: NOHTYP MARGORP Algorithm Step 1 : input a sentence. And store this in a variable s. Step 2 : Then splitting the sentence into a list of words. w=s.split(“”) Step 3 : Reversing each word and creating a new list of ... Read More

Python program to list the difference between two lists.

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

1K+ Views

In this problem given two lists. Our tasks is to display difference between two lists. Python provides set() method. We use this method here. A set is an unordered collection with no duplicate elements. Set objects also support mathematical operations like union, intersection, difference, and symmetric difference. Example Input::A = [10, 15, 20, 25, 30, 35, 40] B = [25, 40, 35] Output: [10, 20, 30, 15] Explanation difference list = A - B Algorithm Step 1: Input of two arrays. Step 2: convert the lists into sets explicitly. Step 3: simply reduce one ... Read More

Python program to print all distinct elements of a given integer array.

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

1K+ Views

Given an integer array. The elements of the array may be duplicate.Our task is to display the distinct values. Example Input::A=[1, 2, 3, 4, 2, 3, 5, 6] Output [1, 2, 3, 4, 5, 6] Algorithm Step 1: input Array element. Step 2: Then pick all the elements one by one. Step 3: then check if the picked element is already displayed or not. Step 4: use one flag variable which initialized by 0.if the element is displayed earlier flag variable is 1 and if the element is not displayed earlier flag variable is 0. ... Read More

Python program to print a checkboard pattern of n*n using numpy.

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

458 Views

Given the value of n, our task is to display the check board pattern for a n x n matrix. Different types of functions to create arrays with initial value are available in numpy . NumPy is the fundamental package for scientific computing in Python. Algorithm Step 1: input order of the matrix. Step 2: create n*n matrix using zeros((n, n), dtype=int). Step 3: fill with 1 the alternate rows and columns using a slicing technique. Step 4: print the matrix. Example Code import numpy as np def checkboardpattern(n): print("Checkerboard pattern:") ... Read More

Python program to cyclically rotate an array by one

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

1K+ Views

Given a user input array. Our task is to rotate cyclically means clockwise rotate the value. Example Input: A=[1, 2, 3, 4, 5] Output=[5, 1, 2, 3, 4] Algorithm Step 1: input array element. Step 2: Store the last element in a variable say x. Step 3: Shift all elements one position ahead. Step 4: Replace first element of array with x. Example Code # Python program to cyclically rotate #an array by one # Method for rotation def rotate(A, n): x = A[n - 1] for i in ... Read More

Advertisements