Found 33676 Articles for Programming

Return the mask of a masked array or full boolean array of False in Numpy

AmitDiwan
Updated on 21-Feb-2022 09:57:15

402 Views

To return the mask of a masked array, or full boolean array of False, use the ma.getmaskarray() method in Python Numpy. Returns the mask of arr as an ndarray if arr is a MaskedArray and the mask is not nomask, else return a full boolean array of False of the same shape as arr.A masked array is the combination of a standard numpy.ndarray and a mask. A mask is either nomask, indicating that no value of the associated array is invalid, or an array of booleans that determines for each element of the associated array whether the value is valid ... Read More

Java Program to Display All Prime Numbers from 1 to N

Manisha Chand
Updated on 05-Jun-2025 09:55:00

37K+ Views

In this article, we will understand how to display all the prime numbers from 1 to N in Java. All possible positive numbers from 1 to infinity are called natural numbers. A number is a prime number if its only factors are 1 and itself and cannot be divided by any other number. 11 is a prime number. Its factors are 1 and 11 itself. Some examples of prime numbers are 2, 3, 5, 7, 11, 13, and so on. 2 is the only even prime number. All other prime numbers are odd numbers. Below is a demonstration of the ... Read More

Java Program to Add Two Complex numbers

Shriansh Kumar
Updated on 27-May-2025 10:22:40

2K+ Views

Complex numbers are expressed in the form of p + iq, where p and q indicate real numbers and i represents imaginary numbers. For instance, 8.2 + 5.6i is a complex number, where 8.2 is the real part and 5.6i is the imaginary part. As you can see, the real number part contains an integer value (mathematical, not Java integer), and the imaginary part has an additional symbol i. In this article, we will understand how to add two complex numbers in Java. Adding Complex Numbers Since complex numbers are divided into two parts (real and imaginary), the real numbers ... Read More

How to compare slices, structs and maps in Golang?

Mukul Latiyan
Updated on 21-Feb-2022 08:09:20

1K+ Views

The reflect package in Go provides a very important function called DeepEqual() which can be used to compare composite types. The DeepEqual() function is used when we want to check if two data types are "deeply equal".Comparing slicesExample 1Consider the code shown belowpackage main import (    "fmt"    "reflect" ) func main() {    sl := []int{1, 2, 3}    sl1 := []int{1, 2, 3}    fmt.Println(reflect.DeepEqual(sl, sl1)) }OutputIf we run the command go run main.go on the above code, then we will get the following output in the terminal.trueComparing mapsExample 2Consider the code shown below.package main import ( ... Read More

How to check if a string contains a substring in Golang?

Mukul Latiyan
Updated on 21-Feb-2022 08:16:10

869 Views

We know that substrings are a contiguous sequence of characters in a string, and in order to check whether a string contains a substring, we have two options available.The first approach is to use a built-in function called Contains() and the second approach is to make use a self-written logic for the same.The syntax for the Contains() function of the strings package is shown below.func Contains(s, substr string) boolIn the above syntax, there are two parameters inside the function Contains(). The first parameter is the string in which we are trying to find the pattern, and the second is the ... Read More

How to check if a key exists in a map in Golang?

Mukul Latiyan
Updated on 21-Feb-2022 07:32:37

902 Views

We know that maps in Go contain key-value pairs. There are often instances where we would want to know that a certain key exists in a map or not, in such cases, we have two options available.The first approach is very naive, as in this approach, we basically iterate over the map with the help of the range clause and then compare each key to the key that we want to check if is available or not.And the second approach is a bit better, as we make use of the if statement with a bit of syntactic sugar.Let's first check ... Read More

How to search and replace texts in a string in Golang?

Mukul Latiyan
Updated on 21-Feb-2022 07:28:20

1K+ Views

We often want to replace certain strings or all the strings that match a pattern with some other string. In order to do that in Golang, we can either use the native functions that the strings package of Go's standard library provides us with or we can write the logic for the same on our own.In this article, we will see different examples, where we will use the two most used functions of the strings package. These functions are −strings.Replace()strings.ReplaceAll()Let's first consider the signature of these functions to know a little more about them.Syntax of strings.Replace()func Replace(s, old, new string, ... Read More

How to read CSV files in Golang?

Mukul Latiyan
Updated on 21-Feb-2022 07:25:43

2K+ Views

To read a CSV file in Go, the first thing that we need to make use of is the encoding/csv package that the Go standard library provides us with. The encoding/csv package contains different functions and methods that can be used when we want to read data from a CSV file.In this article, we will use the NewReader() function that the package provides, which takes one argument and that is basically the file that we want to open and then invoke the ReadAll() method on the file as well.Besides the encoding/csv package, we will also use the os package, which ... Read More

How to check if a slice contains an element in Golang?

Mukul Latiyan
Updated on 21-Feb-2022 07:21:46

4K+ Views

Many languages do provide a method similar to indexOf() where one can find the existence of a particular element in an array-like data structure. However, in Golang, there's no such method and we can simply implement it with the help of a for-range loop.Let's suppose we have a slice of strings, and we want to find out whether a particular string exists in the slice or not.Example 1Consider the code shown below.package main import (    "fmt" ) func Contains(sl []string, name string) bool {    for _, value := range sl {       if value == name ... Read More

Number Parsing in Golang

Mukul Latiyan
Updated on 21-Feb-2022 07:14:30

791 Views

Number parsing in Go is about converting the numbers that are present in string form to number form. By number form, we mean that these numbers can either be converted into integers, floats, etc.The most widely used package for number parsing is the "strconv" package that Go library provides us with. There are many cases that are present in the number parsing in Go, we will talk about all of these one by one in this article.The most basic approach is when we have a base 10 number that is actually present in a string form and we want to ... Read More

Advertisements