Initialize a Boolean Array in Java

Vivek Verma
Updated on 05-May-2025 13:46:37

25K+ Views

Initializing a Boolean Array in Java refers to the process of assigning values (allocating memory) to the Boolean array for the first time. We can initialize an array or any variable - Either at the time of creation. Or, later in the program, using the assignment operator when we are just defining it initially. A Boolean array can be used to store only Boolean values (i.e., either true or false), and the "default value" of each element in a Boolean array is false.  In some cases, we may need to initialize all ... Read More

0-1 Knapsack Problem Using Branch and Bound in C/C++

Ravi Ranjan
Updated on 05-May-2025 12:29:17

3K+ Views

In the 0-1 knapsack problem, a set of items is given, each with a weight and a value. We need to determine the number of each item to include in a collection so that the total weight is less than or equal to the given limit and the total value is as large as possible. What is Branch and Bound Algorithm?The branch and bound algorithm breaks the given problem into multiple sub-problems and then uses a bounding function. It eliminates only those solutions that cannot provide optimal solutions. In this article, we will discuss how to solve the 0-1 knapsack problem ... Read More

Generate Random Numbers Using Multiply with Carry Method in C++

Ravi Ranjan
Updated on 05-May-2025 12:28:21

391 Views

The multiply-with-carry method is a variant of the add-with-carry generator introduced by Marsaglia and Zaman (1991). The main advantages of this method are that it invokes simple computer integer arithmetic and leads to a very fast generation of sequences of random numbers with immense periods, ranging from around 260 to 22000000. In this article, our task is to generate random numbers using the multiply-with-carry method. Here is the formula of multiply-with-carry method: Multiply With Carry (MWC) Formula The multiply-with-carry formula is as follows: Xn = (a * Xn-1 + Cn-1) mod 232 Cn = (a * Xn-1 + Cn-1) ... Read More

Key Differences Between Python 2.7.x and Python 3.x

Sumana Challa
Updated on 02-May-2025 21:40:20

457 Views

Python 3.0 was released in December 2008. It was designed to rectify certain flaws in earlier versions. Python 3.0 doesn’t provide backward compatibility. That means a Python program written using version 2.x syntax doesn’t execute under the Python 3.x interpreter. Version 2.7 is the final major release in the Python 2.x series. The guiding principle of Python 3 was: "reduce feature duplication by removing old ways of doing things". Although there are quite a few differences in usage of these two versions, the most obvious ones are mentioned below - Print Statement Vs Function print is a keyword in Python ... Read More

Remove Special Characters, Punctuation and Spaces from a String in Python

Yaswanth Varma
Updated on 02-May-2025 20:34:26

7K+ Views

It is necessary to clean up the strings by removing the unwanted elements such as special characters, punctuation, and spaces. These unwanted characters can be involved in the tasks and cause unexpected results. In this article, we are going to learn more about removing all special characters, punctuation and spaces from a string. Using Python re Module Through the re module, Python provides support for regular expressions. This module contains methods and classes that we can search, match, and modify a string (text content) using regular expressions.ThePython re.sub() method accepts a pattern, a replacement string, and a string as ... Read More

Python Program to Count Nodes in a Binary Tree

AYUSH MISHRA
Updated on 02-May-2025 20:17:24

3K+ Views

In this article, we are going to learn how we can count the total number of nodes in a binary tree in Python, using different approaches. A binary tree is a data structure in which each node can have at most two children. The two children node of a binary tree are called the left child and the right child. We have to calculate the total number of nodes present in the binary tree. Scenario 1 Input: Binary Tree = 1 2 3 4 5 Output: Total number of nodes: 5 Above-given binary tree has five nodes: 1, 2, ... Read More

Convert Single Character to Integer Value in Python

Sakshi Rayu
Updated on 02-May-2025 19:46:40

926 Views

In Python, the ord() function converts a given character to the corresponding ASCII (American Standard Code for Information Interchange) integer. The ord() function raises a TypeError if you pass a string value as a parameter. Converting a single alphabet to its integer In the following example, we have converted a character 'A' into its Unicode using the ord() function - my_str='A' result=ord(my_str) print("Unicode of 'A'-", result) Following is an output of the above code - Unicode of 'A'- 65 Printing Integer values of all the alphabets We can use the ord() function to print all the Unicode characters ... Read More

Open File in Same Directory as Python Script

Niharikaa Aitam
Updated on 02-May-2025 19:38:07

36K+ Views

In Python, it is a common scenario that the files we want to work with should be placed in the same folder as the script itself. Simply we with the reference of a filename like "data.txt" may not always work as expected, especially when the script is executed from a different working directory. The best way is to place the scripts in the actual location using the __file__ variable along with the os or pathlib modules. In this article, we are going to see the different approaches to open a file in the same directory as a Python script. Using ... Read More

Align JRadioButtons Horizontally in Java

Alshifa Hasnain
Updated on 02-May-2025 19:26:39

861 Views

In this article, we will learn to align the JRadioButtons horizontally in Java. While designing graphical user interfaces (GUIs) in Java using Swing, you might frequently have to position radio buttons horizontally instead of the default vertical layout. What is a JRadioButton? A JRadioButton is a subclass of JToggleButton and is a two-state button that can be either selected or deselected. Unlike checkboxes, radio buttons are associated with a group, and only one radio button in a group can be selected. This can be implemented using the ButtonGroup class. Syntax The following is the syntax for JButton initialization: JRadioButton ... Read More

Sort Items of a JComboBox in Java

Alshifa Hasnain
Updated on 02-May-2025 19:26:28

1K+ Views

In this article, we will learn to sort the items of a JComboBox in Java. A JComboBox is a basic Swing component, and one of its common requirements is to show the items in sorted order. What is a JComboBox? A JComboBox is a subclass of the JComponent class, and it is a combination of a text field and a drop-down list from which the user can choose a value. A JComboBox can generate the ActionListener, ChangeListener, and ItemListener interfaces when the user actions with a combo box. Syntax The following is the syntax for JComboBox initialization: JComboBox comboBox ... Read More

Advertisements