Found 34494 Articles for Programming

String slicing in Python to rotate a string

karthikeya Boyini
Updated on 23-Jun-2020 16:09:07

4K+ Views

A string is given, our task is to slicing the string into two way. One is clockwise and another anticlockwise.1. Left (Or anticlockwise) rotate the given string by d elements (where d pythonprogram Left Rotation: thonprogrampy Right Rotation: ampythonprogr

Python program to move spaces to front of string in single traversal

Samual Sam
Updated on 23-Jun-2020 16:09:23

355 Views

Given a string that has set of words and spaces, our task is to move all spaces to front of string, by traversing the string only once. We will solve this problem quickly in Python using List Comprehension.ExampleInput: string = "python program" Output: string= “ pythonprogram"AlgorithmStep1: input a string with word and space. Step2: Traverse the input string and using list comprehension create a string without any space. Step 3: Then calculate a number of spaces. Step 4: Next create a final string with spaces. Step 5: Then concatenate string having no spaces. Step 6: Display string.Example Code# Function to ... Read More

Python code to print common characters of two Strings in alphabetical order

karthikeya Boyini
Updated on 23-Jun-2020 16:09:56

832 Views

Two user input strings are given, our task is to print all the common characters in alphabetical order.ExampleInput: string1: python string2: program Output: opExplanationThe letters that are common between the two strings are o (1 times), p (1 time)AlgorithmStep 1: first we take two input string. Step 2: next we will do to convert these two strings into counter dictionary. Step 3: Now find common elements between two strings using intersection ( ) property. Step 4: Resultant will also be a counter dictionary having common elements as keys and their common frequencies as value. Step 5: Use elements () method ... Read More

Python Program to Cyclic Redundancy Check

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

6K+ Views

For detecting errors in digital data CRC is used, this is a good technique in detecting the transmission errors. In this technique mainly binary division is applied. In these technique, cyclic redundancy check bits are present which is a sequence of redundant bits, these bits are appended to the end of data unit so that the resulting data unit becomes exactly divisible by a second which is predetermined binary number. At the destination side, the incoming data is divided by the same number, if there is no remainder then assumed that data is correct and it’s ready to accept. A ... Read More

Python program to create a sorted merged list of two unsorted list

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

431 Views

Here two user input list is given, the elements of two lists are unsorted. Our task is to merged these two unsorted array and after that sort the list. Example Input: A [] = {100, 50, 150} B [] = {200, 30, 20} Output: Merge List:{20, 30, 50, 100, 150, 200} Algorithm Step 1: first we create two user input list. Step 2: Final merge list size is (size of the first list + the size of the second list). Step 3: then sort two list using sort() method. Step ... Read More

Python program to find Largest, Smallest, Second Largest, and Second Smallest in a List?

karthikeya Boyini
Updated on 23-Jun-2020 16:11:30

1K+ Views

Array is given, we have to find out maximum, minimum, secondlargest, second smallest number.AlgorithmStep 1: input list element Step 2: we take a number and compare it with all other number present in the list. Step 3: get maximum, minimum, secondlargest, second smallest number.Example code# To find largest, smallest, second largest and second smallest in a List    def maxmin(A):       maxi = A[0]       secondsmax = A[0]       mini = A[0]       secondmini = A[0]       for item in A:    if item > maxi:       maxi ... Read More

Find average of a list in python?

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

728 Views

Python provides sum function for calculating n number of element. Here we use this function and then calculate average. Algorithm Step 1: input “size of the list” Step 2: input “Element” Step 3: using sum function calculate summation of all numbers. Step 4: calculate average. Example code # Average of a list A=list() n=int(input("Enter the size of the List ::")) print("Enter the number ::") for i in range(int(n)): k=int(input("")) A.append(int(k)) sm=sum(A) avg=sm/n print("SUM = ",sm) print("AVERAGE = ",avg) Output Enter the size of the List ::5 Enter the number:: 10 20 30 40 50 SUM = 150 AVERAGE = 30.0

Print powers using Anonymous Function in Python?

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

223 Views

Here we used anonymous (lambda) function inside the map() built-in function. In Python, anonymous function is defined without name, its defined using lambda keyword. Algorithm Step 1: input n Step 2: input p Step 3: use anonymous function. Step 4: display result. Example Code # To display the powers of any number using anonymous function n = int(input("Enter how many terms want to display??")) p = int(input("Enter the number want to calculate power ::")) # use anonymous function cal = list(map(lambda i: p ** i, range(n))) # display the result print("The total terms is ::>", ... Read More

Second most repeated word in a sequence in Python?

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

644 Views

The string is given, our task is to find out the second repeated word. Here we Counter(iterator) for creating dictionary which contains word as key and its frequency as value. Algorithm Step 1: Create user define list. Step 2: Then convert list into a dictionary. Step 2: Next get the values and sort them in descending order. Step 3: Then the second element is the second largest value. Step 4: Next again traverse whole dictionary and display key whose value is equal to second largest element. Example code # To print Second most repeated word in a ... Read More

Python program to find Intersection of two lists?

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

420 Views

Intersection operation means, we have to take all the common elements from List1 and List 2 and all the elements store in another third list. List1::[1, 2, 3] List2::[2, 3, 6] List3::[2, 3] Algorithm Step 1: input lists. Step 2: first traverse all the elements in the first list and check with the elements in the second list. Step 3: if the elements are matched then store in third list. Example code #Intersection of two lists def intertwolist(A, B): C = [i for i in A if i in B] ... Read More

Advertisements