Found 34494 Articles for Programming

Python program to remove all duplicates word from a given sentence.

Ankith Reddy
Updated on 23-Jun-2020 16:04:23

475 Views

Given a sentence. Remove all duplicates words from a given sentence.ExampleInput: I am a peaceful soul and blissful soul. Output: I am a peaceful soul and blissful.AlgorithmStep 1: Split input sentence separated by space into words. Step 2: So to get all those strings together first we will join each string in a given list of strings. Step 3: now create a dictionary using the counter method which will have strings as key and their Frequencies as value. Step 4: Join each words are unique to form single string.Example Codefrom collections import Counter def remov_duplicates(st):    st = st.split(" ") ... Read More

Plotting graph using seaborn in python.

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

490 Views

Plotly's Python graphing library makes interactive, publication-quality graphs online. this graph is mainly used when we want to make line plots, scatter plots, area charts, bar charts, error bars, box plots, histograms, heatmaps, subplots, multiple-axes, polar charts, and bubble charts. Seaborn is a library for making statistical graphics in Python. It is built on top of matplotlib and it is integrated with pandas data structures. 1. We import seaborn, which is the only library necessary for this simple example. import seaborn as sns 2. We apply the default default seaborn theme, scaling, and color palette. sns.set() ... Read More

Python program to Display Hostname and IP address?

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

1K+ Views

Python provides gethostname(), gethostbyname() two function. gethostname() retrives the standard host name for the local machine. gethostbyname() retrives host information corresponding to a host name from a host database. Socket. gethostname() Socket. gethostbyname() Algorithm Step 1: use module socket. Step 2: use gethostname() retrives the standard host name for the local machine. Step 3: use gethostbyname() retrives host information corresponding to a host name from a host database. Example Code # Display hostname andIP address import socket def host_IP(): try: hname = socket.gethostname() ... Read More

Python program to reverse bits of a positive integer number?

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

2K+ Views

First convert number into binary using bin() function. Then skip the first two character of binary representation because bin() appends 0b as a prefix in a binary representation of the number and reverse the remaining part. From also character and reverse it till second last character from left. Convert a reversed binary string into integer. Algorithm integernumber(n, bit_size) /* n is the number and bit_size is the bitsize */ Step 1: first convert number into binary . Step 2: skip the first two characters of binary representation string and reverse. Step 3: remaining string and then append 0’s ... Read More

Python program to check if binary representation is palindrome?

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

630 Views

Here we use different python inbuilt function. First we use bin() for converting number into it’s binary for, then reverse the binary form of string and compare with originals, if match then palindrome otherwise not. Example Input: 5 Output: palindrome Explanation Binary representation of 5 is 101 Reverse it and result is 101, then compare and its match with originals. So its palindrome Algorithm Palindromenumber(n) /* n is the number */ Step 1: input n Step 2: convert n into binary form. Step 3: skip the first two characters of a string. Step 4: them reverse the ... Read More

Python program to find the length of the largest consecutive 1's in Binary Representation of a given string.

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

398 Views

Given the number, find length of the longest consecutive 1's in its binary representation. Example Input: n = 15 Output: 4 The binary representation of 14 is 1111. Algorithm Step 1: input the number. Step 2: use one counter variable c=0. Step 3: Count the number of iterations to reach i = 0. Step 4: This operation reduces length of every sequence of 1s by one. Example code # Python program to find # length of the longest # consecutive 1s in # binary representation of a number. def maxlength(n): # ... Read More

Python program to print the initials of a name with last name in full?

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

4K+ Views

Here we use different python inbuilt function. First we use split().split the words into a list. Then traverse till the second last word and upper() function is used for print first character in capital and then add the last word which is title of a name and here we use title(), title function converts the first alphabet to capital. Example Input Pradip Chandra Sarkar Output P.C Sarkar Algorithm fullname(str1) /* str1 is a string */ Step 1: first we split the string into a list. Step 2: newspace is initialized by a space(“”) Step 3: then traverse ... Read More

Python program to split the even and odd elements into two different lists.

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

21K+ Views

In this program we create a user input list and the elements are mixture of odd and even elements. Our task is to split these list into two list. One contains odd number of element and another is even number of elements. Example Input: [1, 2, 3, 4, 5, 9, 8, 6] Output Even lists: [2, 4, 8, 6] Odd lists: [1, 3, 5, 9] Algorithm Step 1 : create a user input list. Step 2 : take two empty list one for odd and another for even. Step 3 : then traverse each element in the ... Read More

SequenceMatcher in Python for Longest Common Substring.

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

813 Views

Given two strings, our task is to print the longest common sub-string. We will solve problem in python using SequenceMatcher.find_longest_match () method. Class difflib.SequenceMatcher is a flexible class for comparing pairs of sequences of any type, so long as the sequence elements are hashable. find_longest_match(a, x, b, y) Find longest matching block in a[a:x] and b[b:y]. Examples Input: str1 = "pythonprogramming", str2 = "pro" Output: pro Algorithm Step 1: Enter two string. Step 2: initialize SequenceMatcher object with the input string. Step 3: find the match ... Read More

Python program to find Maximum and minimum element’s position in a list?

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

15K+ Views

In python is very easy to find out maximum, minimum element and their position also. Python provides different inbuilt function. min() is used for find out minimum value in an array, max() is used for find out maximum value in an array. index() is used for finding the index of the element. Algorithm maxminposition(A, n) /* A is a user input list and n is the size of the list.*/ Step 1: use inbuilt function for finding the position of minimum element. A.index(min(A)) Step 2: use inbuilt ... Read More

Advertisements