Programming Articles - Page 2095 of 3366

Count frequencies of all elements in array in Python

Hafeezul Kareem
Updated on 12-Feb-2020 11:16:43

20K+ Views

In this tutorial, we are going to write a program that finds the frequency of all the elements in an array. We can find it in different ways let's explore two of them.Using dictInitialize the array.Initialize an empty dict.Iterate over the list.If the element is not in dict, then set the value to 1.Else increment the value by 1.Print the element and frequencies by iterating over the dict.ExampleLet's see the code.# intializing the list arr = [1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3] # initializing dict to store frequency of each element elements_count = {} ... Read More

Count all prefixes in given string with greatest frequency using Python

Hafeezul Kareem
Updated on 12-Feb-2020 11:10:44

636 Views

In this tutorial, we are going to write a program that counts and prints the words with a higher frequency of an alphabet than the second one.Take a string and two alphabets. The prefixes with a higher frequency of the first alphabet will be printed. And display the count at the end of the output.Let's see some examples.Inputstring:- apple alphabets:- p, eOutputap app appl apple 4Inputstring:- apple alphabets:- e, pOutput0Let's see the steps to write the code.Define a function and write the code in it.Initialize count to 0 and an empty string.Iterate over the string.Get the prefix using the string ... Read More

What are the new features added to Stream API in Java 9?

raja
Updated on 21-Feb-2020 12:20:30

194 Views

In Java 9, Oracle Corporation has added four useful new methods to Stream API. Those methods are iterate(), ofNullable(), takeWhile() and dropWhile().iterate()The iterate() can be used as stream version replacement of traditional for-loops. This method has improved by adding another parameter, the Predicate interface that allows us to stop these endless numbers based on conditions defined with the Predicate interface.Exampleimport java.util.stream.Stream; public class StreamIterateMethodTest {    public static void main(String[] args) {       Stream.iterate(1, i -> i < 5, i -> i + 1).forEach(System.out::println); // iterate()    } }Output1 2 3 4ofNullable()The ofNullable() method returns the stream object of an element if it ... Read More

What are the new methods added to an Optional class in Java 9?

raja
Updated on 21-Feb-2020 12:21:47

286 Views

An Optional class provides a container that may or may not contain a non-null value. This Optional class introduced in Java 8 to reduce the number of places in the code where a NullPointerException can be generated. Java 9 added three new methods to Optional class: or(), ifPresentOrElse() and stream() that help us to deal with default values.Optional.or()The or() method introduced in Java 9 and the parameter of this method is a functional interface Supplier. This method always gives us an Optional object that is not empty. If the Optional object is not empty, it returns the Optional object itself. Otherwise, it returns an Optional ... Read More

What are the rules for private methods in an interface in Java 9?

raja
Updated on 21-Feb-2020 12:43:46

1K+ Views

Java 9 added a new feature of private methods to an interface. The private methods can be defined using a private modifier. We can add both private and private static methods in an interface from Java 9 onwards.Rules for private methods in an interface:A private method has a body in an interface means that we can’t be declared as a normal abstract method as usually do in an interface. If we are trying to declare a private method without a body then it can throw an error says that "This method requires a body instead of a semicolon".We can't be used both private and abstract modifiers together in an interface.If ... Read More

What are the main features and enhancements introduced in Java 9?

raja
Updated on 11-Feb-2020 08:00:28

267 Views

Oracle has released Java 9 version with a rich set of new features and brings a lot of new enhancements.Below are a few important features and enhancements introduced in Java 9.Factory Methods for Collections: Factory methods are special kinds of static methods that can be used to create unmodifiable instances of collections, which means we can use these methods to create a list, set, and map.Java Platform Module System (JPMS): A Java Module is a mechanism to bundle java applications and java packages into a Java module. It specifies which of the java packages that contain visible to other java modules by using ... Read More

Count all possible position that can be reached by Modified Knight in C++

Ayush Gupta
Updated on 10-Feb-2020 12:13:17

128 Views

In this tutorial, we will be discussing a program to find the number of possible positions that can be reached by Modified Knight.For this we will be provided with a 8*8 chessboard. Our task is to find the number of positions Modified Knight can capture with the given number of steps.Example#include using namespace std; //finding the positions void findSteps(int current_row, int current_column, int curr, int board_size, int steps, int* visited){    //bound checking    if (current_row >= board_size || current_row < 0       || current_column >= board_size || current_column < 0       || curr > ... Read More

Count all possible paths from top left to bottom right of a mXn matrix in C++

Ayush Gupta
Updated on 10-Feb-2020 12:08:04

247 Views

In this tutorial, we will be discussing a program to find the number of possible paths from top left to bottom right of a mXn matrix.For this we will be provided with a mXn matrix. Our task is to find all the possible paths from top left to bottom right of the given matrix.Example#include using namespace std; //returning count of possible paths int count_paths(int m, int n){    if (m == 1 || n == 1)       return 1;    return count_paths(m - 1, n) + count_paths(m, n - 1); } int main(){    cout

Count all possible paths between two vertices in C++

Ayush Gupta
Updated on 10-Feb-2020 12:06:13

423 Views

In this tutorial, we will be discussing a program to find the number of paths between two vertices.For this we will be provided with a directed graph. Our task is to find the number of paths possible between two given vertices.Example#include using namespace std; //constructing a directed graph class Graph{    int V;    list *adj;    void countPathsUtil(int, int, bool [], int &);    public:       //constructor       Graph(int V);       void addEdge(int u, int v);       int countPaths(int s, int d); }; Graph::Graph(int V){    this->V = V;    adj ... Read More

Count all possible N digit numbers that satisfy the given condition in C++

Ayush Gupta
Updated on 10-Feb-2020 12:02:03

206 Views

In this tutorial, we will be discussing a program to find the number of possible N digit numbers that satisfy the given condition.For this we will be provided with an integer. Our task is to check which one of number having N digits followNumber + Reverse(Number) = 10N -1Example#include using namespace std; //returning the count of numbers string count_num(int N){    if (N % 2 == 1)       return 0;    string result = "9";    for (int i = 1; i

Advertisements