Programming Articles - Page 2182 of 3363

Java program to find Largest, Smallest, Second Largest, Second Smallest in an array

Ashin Vincent
Updated on 16-Apr-2025 15:53:47

4K+ Views

In this problem we are given an array of numbers, and we have to find the largest, smallest, second largest, and second smallest elements in an array in Java. Let’s understand the problem better with the help of an example: Input int arr[] = {55, 10, 8, 90, 43, 87, 95, 25, 50, 12}; Output Smallest element = 8 2nd Smallest element = 10 Largest element = 95 2nd Largest element = 90 To solve this problem, use the following approaches: Using Arrays.sort() Using nested for-loops Optimized one-pass approach Using Arrays.sort() In this ... Read More

Find average of a list in Java

Ashin Vincent
Updated on 16-Apr-2025 15:53:07

1K+ Views

Given a list of numbers, our task is to calculate the average of this list. The average of a list can be found by adding all the elements in the list and dividing the sum by the total number of elements. In this article we will learn different methods to implement this in Java. There are multiple ways in Java to find the average of a list. In this article we will explore two different approaches: Using Java Streams Using for loop Find Average of a List Using Java Streams This approach shows how to find ... Read More

How to use Supplier interface in lambda expression in Java?

raja
Updated on 13-Jul-2020 07:22:11

7K+ Views

A Supplier interface is a pre-defined interface that represents the supplier of results. It is instantiated using lambda expression or method reference or default constructor. The functional method of a Supplier interface is the get() method. This interface belongs to java.util.function package.Syntax@FunctionalInterface public interface SupplierIn the below program, we can use the Supplier interface in a lambda expression. The get() method only returns a value and doesn't accept any argument, so a lambda expression having an empty argument part.Exampleimport java.util.*; import java.util.function.*; public class SupplierTest {    public static void main(String args[]) {       Supplier supplier1 = () -> "TutorialsPoint";   // lambda expression       System.out.println(supplier1.get());       ... Read More

Python - Column summation of tuples

Pradeep Elance
Updated on 02-Jan-2020 10:26:34

313 Views

Python has extensive availability of various libraries and functions which make it so popular for data analysis. We may get a need to sum the values in a single column for a group of tuples for our analysis. So in this program we are adding all the values present at the same position or same column in a series of tuples.It can be achieved in the following ways.Using for loop and zipUsing the for loop we loop through each item and apply zip function to gather the values from each column. Then we apply the sum function and finally get ... Read More

Python - Change column names and row indexes in Pandas DataFrame

Pradeep Elance
Updated on 02-Jan-2020 10:23:35

2K+ Views

Pandas is a python library offering many features for data analysis which is not available in python standard library. One such feature is the use of Data Frames. They are rectangular grids representing columns and rows. While creating a Data frame, we decide on the names of the columns and refer them in subsequent data manipulation. But there may be a situation when we need to change the name of the columns after the data frame has been created. In this article, we will see how to achieve that.Using rename()This is the most preferred method as we can change both ... Read More

max() and min() in Python

Pradeep Elance
Updated on 02-Jan-2020 10:16:18

2K+ Views

Finding maximum and minimum values from a given list of values is a very common need in data processing programs. Python has these two functions which handle both numbers and strings. We will see both the scenarios in the below examples.Numeric ValuesWe take a list of numeric values which has integers and floats. The functions work appropriately to give both the max and the min values.Example Live Demox=[10, 15, 25.5, 3, 2, 9/5, 40, 70] print("Maximum number is :", max(x)) print("Minimum number is :", min(x))OutputRunning the above code gives us the following result:Maximum number is : 70 Minimum number is : ... Read More

Getter and Setter in Python

Pradeep Elance
Updated on 02-Jan-2020 10:13:52

21K+ Views

For the purpose of data encapsulation, most object oriented languages use getters and setters method. This is because we want to hide the attributes of a object class from other classes so that no accidental modification of the data happens by methods in other classes.As the name suggests, getters are the methods which help access the private attributes or get the value of the private attributes and setters are the methods which help change or set the value of private attributes.Accessing Private AttributeBelow we write code to create a class, initialize it and access it variables without creating any additional ... Read More

Fractal Trees in Python

Pradeep Elance
Updated on 02-Jan-2020 10:07:38

1K+ Views

Fractal patterns are all around us in nature. Like a small branch taken out of the leaf of a fern leaf resembles the leaf itself. Or a pebble often resembles the shape of a mountain! So this idea of a repetition of small pattern to generate a large pattern is known as a fractal tree. In python programming we can also generate fractal trees by using various modules available.Using pygame ModuleThis module provides us with the required functions to generate the fractal trees. Here we first defines the screen layout size and then define the deepness up to which the ... Read More

Decision tree implementation using Python

Pradeep Elance
Updated on 02-Jan-2020 10:01:51

2K+ Views

Decision tree is an algorithm which is mainly applied to data classification scenarios. It is a tree structure where each node represents the features and each edge represents the decision taken. Starting from the root node we go on evaluating the features for classification and take a decision to follow a specific edge. Whenever a new data point comes in , this same method is applied again and again and then the final conclusion is taken when all the required features are studied or applied to the classification scenario. So Decision tree algorithm is a supervised learning model used in ... Read More

assert keyword in Python

Pradeep Elance
Updated on 02-Jan-2020 09:51:34

397 Views

Every programming language has feature to handle the exception that is raised during program execution. In python the keyword assert is used to catch an error and prompt a user defined error message rather than a system generated error message. This makes it easy for the programmer to locate and fix the error when it occurs.With AssertIn the below example we use the assert key word to catch the division by zero error. The message is written as per the wish of the programmer.Example Live Demox = 4 y = 0 assert y != 0, "if you divide by 0 it ... Read More

Advertisements