Found 33676 Articles for Programming

Java program to remove all duplicates words from a given sentence

AmitDiwan
Updated on 23-Oct-2024 17:29:44

991 Views

In this article, we will learn to remove duplicate words from a given sentence in Java. The first method uses Java Streams to remove duplicates and join the words back into a sentence using the distinct() and Collectors.joining() methods. The second method uses a Set to automatically filter out duplicate words and a StringJoiner to combine the words back into a sentence. Both methods will give a sentence with only unique words. Different approaches Following are the different approaches to remove all duplicate words from a given sentence − Using Java Streams ... Read More

Java program to sort words of sentence in ascending order

AmitDiwan
Updated on 07-Jul-2020 09:41:56

5K+ Views

To sort words of sentence in ascending order, the Java code is as follows −Example Live Demoimport java.util.*; public class Demo{    static void sort_elements(String []my_str, int n){       for (int i=1 ;i= 0 && temp.length() < my_str[j].length()){             my_str[j+1] = my_str[j];             j--;          }          my_str[j+1] = temp;       }    }    public static void main(String args[]){       String []my_arr = {"This", "is", "a", "sample"};       int len = my_arr.length;       sort_elements(my_arr,len);       System.out.print("The sorted array is : ");       for (int i=0; i

Python Grouped Flattening of list

Hafeezul Kareem
Updated on 07-Jul-2020 09:04:25

180 Views

In this tutorial, we are going to write a program that flattens a list that contains sub-lists. Given number flatten the sublists until the given number index as parts. Let's see an example to understand it clearly.Inputlists = [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]] number = 2Output[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10]]Let's see the steps to solve the problem.Initialize the list and number.Initialize an empty list.Iterate over the list with range(0, len(lists), number.Get the sublists using slicing lists[i:number].Iterate over the sublists and append the resultant list to the result list.Print the result.Example# ... Read More

Python Group by matching second tuple value in list of tuples

Hafeezul Kareem
Updated on 07-Jul-2020 09:03:26

482 Views

In this tutorial, we are going to write a program that groups all the tuples from a list that have same element as a second element. Let's see an example to understand it clearly.Input[('Python', 'tutorialspoints'), ('Management', 'other'), ('Django', 'tutorialspoints'), ('React', 'tutorialspoints'), ('Social', 'other'), ('Business', 'other')]Output{'tutorialspoint': [('Python', 'tutorialspoints'), ('Django', 'tutorialspoints'), ('React', 'tutorialspoints')], 'other’: [('Management', 'other'), ('Social', 'other'), ('Business', 'other')]}We have to group the tuples from the list. Let's see the steps to solve the problem.Initiate a list with required tuples.Create an empty dictionary.Iterate through the list of tuples.Check if the second element of the tuple is already present in the dictionary ... Read More

Python Grayscaling of Images using OpenCV

SaiKrishna Tavva
Updated on 13-Nov-2024 14:29:19

4K+ Views

Grayscaling an image includes converting a color image into a grayscale image. By using OpenCV library in various methods we can convert the color image with (multiple color channels) into a grayscale image (with a single channel). Some of the common methods for greyscaling an image using OpenCV are as follows. cv2.cvtColor(): This function is used to convert an image from one space color to another. Pixel ... Read More

Python Getting started with psycopg2-PostgreSQL

Hafeezul Kareem
Updated on 07-Jul-2020 08:57:48

924 Views

In this tutorial, we are going to learn how to use PostgreSQL with Python. You have to install certain thing before going into the tutorial. Let's install them.Install the PostgreSQL with the guide..Install the Python module psycopg2 for PostgreSQL connection and working. Run the command to install it.pip install psycopg2Now, open the pgAdmin. And create a sample database. Next, follow the below steps to get started with database operations.Import the psycopg2 module.Store the database name, username, and password in separate variables.Make a connection to the database using psycopg2.connect(database=name, user=name, password=password) method.Instantiate a cursor object to execute SQL commands.Create queries and ... Read More

Python How to copy a nested list

Hafeezul Kareem
Updated on 07-Jul-2020 08:53:49

483 Views

In this tutorial, we are going to see different ways to copy a nested list in Python. Let's see one by one.First, we will copy the nested list using loops. And it's the most common way.Example Live Demo# initializing a list nested_list = [[1, 2], [3, 4], [5, 6, 7]] # empty list copy = [] for sub_list in nested_list:    # temporary list    temp = []    # iterating over the sub_list    for element in sub_list:       # appending the element to temp list       temp.append(element)    # appending the temp list to copy ... Read More

Python Indices of numbers greater than K

Hafeezul Kareem
Updated on 07-Jul-2020 08:52:46

562 Views

In this tutorial, we are going to find the indices of the numbers that are greater than the given number K. Let's see the different ways to find them.A most common way to solve the problem is using the loops. Let's see the steps to solve the problem.Initialize the list and K.Iterate over the list using its length.If you find any number greater than K, then print the current index.Example Live Demo# initializing the list and K numbers = [3, 4, 5, 23, 12, 10, 16] K = 10 # iterating over thAe list for i in range(len(numbers)):    # checking ... Read More

Python Indexing a sublist

Hafeezul Kareem
Updated on 07-Jul-2020 08:51:13

2K+ Views

In this tutorial, we are going to write a program that finds the index of a sublist element from the list. Let's see an example to understand it clearly.Inputnested_list = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]OutputIndex of 7:- 2 Index of 5:- 1 Index of 3:- 0Let's see the simple and most common way to solve the given problem. Follow the given steps solve it.Initialize the list.Iterate over the list using the index.Iterate over the sub list and check the element that you want to find the index.If we find the element then print and break itExample Live ... Read More

Python Index specific cyclic iteration in list

Hafeezul Kareem
Updated on 07-Jul-2020 08:49:32

1K+ Views

In this tutorial, we are going to write a program that cyclically iterates a list from the given Let's see the steps to solve the problemInitialize the list and index.Find the length of the list using len.Iterate over the list using the length.Find the index of the element using index % length.Print the element.Increment the index.It's a simple loop iteration. You can write it without any trouble. Let's see the code.Example Live Demo# initializing the list and index alphabets = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'] start_index = 5 # finding the length length = len(alphabets) # iterating over ... Read More

Advertisements