Programming Articles - Page 2171 of 3366

How to generate prime numbers using lambda expression in Java?

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

2K+ Views

A prime number is a number that is greater than 1 and divided by 1 or itself only. It other words, it can't be divided by other numbers than itself or 1. The generation of prime numbers is 2, 3, 5, 7, 11, 13, 17 and etc.In the below example, we can generate the prime numbers with the help of Stream API and lambda expressions.Exampleimport java.util.*; import java.util.stream.*; public class PrimeNumberLambdaTest {    public static void main(String[] args) {       List generate = PrimeNumberLambdaTest.generate(10);       System.out.println(generate);    }    public static List generate(int series) {       Set set = new TreeSet(); ... Read More

Count and Say in Python

Arnab Chakraborty
Updated on 28-Apr-2020 16:00:49

1K+ Views

Here we will see the Count and Say sequence. This is a sequence whose few terms are like below −111211211111221The string will be read like1 (One)11 (One 1) So read the previous 1, and say “One 1”21 (Two 1) So read the previous 11, and say “Two 1”1211 (One 2 one 1) So read the previous 21, and say “One 2 one 1”111221 (One 1 one 2 two 1) So read the previous 1211, and say “One 1 one 2 two 1”Suppose we have a number n, 1

Implement strStr() in Python

Arnab Chakraborty
Updated on 28-Apr-2020 16:00:13

2K+ Views

Suppose we have two strings str and sub_str. We have to find the first occurrence of sub_str in the str. So if the string str is “helloworld”, and substring is “lo”, then the result will be 3.This can be done using the strstr() function in C. We have to design another function that is similar to the strstr() in C.To solve this, follow these steps −i := 0, j := 0, m := length of sub_str and n := length of strif m = 0, then return 0while i < n and n – i + 1 = m, doif ... Read More

Merge Two Sorted Lists in Python

Arnab Chakraborty
Updated on 28-Apr-2020 15:58:23

1K+ Views

Suppose we have two sorted lists A and B. We have to merge them and form only one sorted list C. The size of lists may different.For an example, suppose A = [1,2,4,7] and B = [1,3,4,5,6,8], then merged list C will be [1,1,2,3,4,4,5,6,7,8]We will solve this using recursion. So the function will work like below −Suppose the lists A and B of function merge()if A is empty, then return B, if B is empty, then return Aif value in A

Valid Parentheses in C++

Arnab Chakraborty
Updated on 28-Apr-2020 08:40:23

6K+ Views

Suppose we have an expression. The expression has some parentheses; we have to check the parentheses are balanced or not. The order of the parentheses are (), {} and []. Suppose there are two strings. “()[(){()}]” this is valid, but “{[}]” is invalid.The task is simple; we will use the stack to do this. We should follow these steps to get the solution −Traverse through the expression until it has exhaustedif the current character is opening bracket like (, { or [, then push into stackif the current character is closing bracket like ), } or ], then pop from ... Read More

Longest Common Prefix in Python

Arnab Chakraborty
Updated on 28-Apr-2020 15:50:24

7K+ Views

Suppose we have a set of strings in an array. We have to find the Longest Common Prefix amongst the string in the array. Here we will assume that all strings are lower case strings. And if there is no common prefix, then return “”.So if the array of a string is like ["school", "schedule", "Scotland"], then the Longest Common Prefix is “sc” as this is present in all of these string.To solve this, we will take the first string as curr, now take each string from the array and read them character by character, and check the characters between ... Read More

Roman to Integer in Python

Arnab Chakraborty
Updated on 14-Sep-2023 01:09:07

37K+ Views

Suppose we have Roman literals; we have to convert them into an integer. As we know the Roman numerals represent in some different symbols as below −NumeralValueI1V5X10L50C100D500M1000If we see the roman numbers closely, it is like suppose the numeral is 'II', so this is 2, there are two 'I's are added together. For XII, it is 12, so this is actually X + II = 10 + 2 = 12. The roman numerals of 4 are not IIII, it is IV. This is a little tricky.I can be used before V(5) and X(10) to make it 4 and 9 respectivelyX ... Read More

Two Sum in Python

Arnab Chakraborty
Updated on 28-Apr-2020 08:04:05

19K+ Views

Suppose we have an array of integers. We have to return the indices of two integers, such that if we add them up, we will reach to a specific target that is also given. Here we will take one assumption, that is always there will be one unique solution in the array, so no two set of indices for same target will be there.For an example, suppose the array is like A = [2, 8, 12, 15], and the target sum is 20. Then it will return indices 1 and 2, as A[1] + A[2] = 20.To solve this, we ... Read More

How to implement the Fibonacci series using lambda expression in Java?

raja
Updated on 13-Jul-2020 09:24:29

3K+ Views

The Fibonacci is a sequence of numbers in which every number after the first two numbers is the sum of the two preceding numbers like 0, 1, 1, 2, 3, 5, 8, 13, 21 and so on. The sequence of Fibonacci numbers defined by using "F(n)=F(n-1)+F(n-2)".In the below example, we can implement the Fibonacci series with the help of Stream API and lambda expression. The Stream.iterate() method returns an infinite sequential ordered stream produced by iterative application of a function to an initial element seed, producing a stream consisting of seed, f(seed), f(f(seed)), etc.Exampleimport java.util.List; import java.util.stream.*; public class FibonacciTest {    public static void ... Read More

How to implement IntFunction with lambda expression in Java?

raja
Updated on 13-Jul-2020 09:25:13

483 Views

IntFunction interface is a functional interface in Java 8 and defined in java.util.function package. This interface accepts an int-valued parameter as input and returns a value of type R. IntFunction interface can be used as an assignment target for a lambda expression or method reference and holds only one abstract method, apply().Syntax@FunctionalInterface public interface IntFunction {    R apply(int value); }Exampleimport java.util.HashMap; import java.util.Map; import java.util.function.IntFunction; public class IntFunctionTest {    public static void main(String[] args) {       IntFunction getMonthName = monthNo -> {  // lambda expression          Map months = new HashMap();          months.put(1, "January");       ... Read More

Advertisements