npm and npx

Both npm and npx are tools that come with Node.js, but they serve different purposes.

npm

Full Form: Node Package Manager Purpose: npm is used for managing packages in Node.js projects. It helps you install, update, and
npm Commands
  • remove packages, as well as manage project dependencies.:
  • npm install : Installs a package.
  • npm uninstall : Removes a package.
  • npm update: Updates all packages to the latest versions.
  • npm list: Lists installed packages and their versions.

npx

Full Form: Node Package Executor Purpose: npx is used to execute binaries from Node modules or packages without needing to install them globally. It's especially handy for running CLI tools that are part of your project or that you don't want to install globally.
npx Commands
  • npx : Runs a command from a package. For example, npx create-react-app my-app will run the create-react-app command without needing to install it globally.

Example Scenario

If you want to start a new React project, you could use:
This command runs create-react-app without having it installed globally.
npx create-react-app my-app
If you wanted to install create-react-app globally for repeated use, you would use:
Bellow code uses npm to install create-react-app globally
npm install -g create-react-app

Summary

To run Node.js packages without having to install them globally, npx is very helpful. We can use it to run command-line interface (CLI) tools, run scripts, and carry out other operations.

How to find hamming weight in Java

hamming weight for a number is the count of bits that are non zero. For instance for 1001 hamming weight is 2. For 100001111 hamming weight is 5. In this article we will see how to find hamming weight efficiently.

Using Simple divide and reminder

Here we are divide the number by 2 and until it becomes 0 and each step we check if the intermediate gives reminder 1 while dividing by 2.
public static int hammingWeight(int n) {

    int count = 0;

    while (n != 0) {
      if (n % 2 == 1) {
        count++;
      }
      n = n / 2;
    }

    return count;

  }

Using Bit marking

In this example we are using Bit masking. Since the input is an Integer and it contains 32 bits. We do a & (bit wise and) operation for each of its digits.
public static int hammingWeightII(int n) {

    int count = 0;
    int mask = 1;

    for (int i = 0; i <= 31; i++) {

      count += (n & mask) == 0 ? 0 : 1;

      //expand the mask
      mask = mask << 1;

    }

    return count;

  }

Full Example

package ic.binary;

public class NumberOf1s {

  public static void main(String[] args) {
    
    int n = 5;
    System.out.println("Number of 1 bits for :" + n + " -> " + NumberOf1s.hammingWeight(n));
    System.out.println("Number of 1 bits for :" + n + " -> " + NumberOf1s.hammingWeight(n));
    n = 8;
    System.out.println("Number of 1 bits for :" + n + " -> " + NumberOf1s.hammingWeight(n));
    System.out.println("Number of 1 bits for :" + n + " -> " + NumberOf1s.hammingWeight(n));

  }


  public static int hammingWeight(int n) {

    int count = 0;

    while (n != 0) {
      if (n % 2 == 1) {
        count++;
      }
      n = n / 2;
    }

    return count;

  }

  public static int hammingWeightII(int n) {

    int count = 0;
    int mask = 1;

    for (int i = 0; i <= 31; i++) {

      count += (n & mask) == 0 ? 0 : 1;

      mask = mask << 1;

    }

    return count;

  }

}
Output of above program
Number of 1 bits for :5 -> 2
Number of 1 bits for :5 -> 2
Number of 1 bits for :8 -> 1
Number of 1 bits for :8 -> 1
</
  

Java OptionalInt example

OptionalInt allows us to create an object which may or may not contain a int value. If a value is present, isPresent() will return true and getAsInt() will return the value. Additional methods that depend on the presence or absence of a contained value are provided, such as orElse().
Other classes similar to OptionalInt are OptionalFloat, OptionalDouble, Optional. These can help us eliminate exceptions that occur due to the absence of a value at runtime. Basically we need to first check if the Optional is carrying any value then only try to get value.
In this example we are returning an OptionalInt from Stream created from integer array and finally returning the sum using reduce method. If the Value is present then only we are trying to pring the value by calling result.getAsint()
package javaexp.blogspot.stream;

import java.util.Arrays;
import java.util.OptionalInt;

public class OptionalIntExample {

  public static void main(String[] args) {
    int iarray[] = {9, 10, 11, 12, 15, 15, 25};
    
    OptionalInt result = Arrays.stream(iarray).reduce((left, right) ->left );
    
    if (result.isPresent() ) {
      System.out.println("Sum of Array " + result.getAsInt());
    }
    
  }

}

Summary

OptionalInt and other respective Optional classes helping in protecting from Nullpointer exception when we try to get value (say integer value) from and Integer object which is null.

Bucket Sort in Java

In this article we will go though a simple implementation of Bucket sort in Java

What is Bucket sort

Bucket sort is a sorting algorithm that divides the inputs into several buckets. Once the buckets are populated with input data, then all these buckets are sorted individually using a different sorting mechanism. After individual buckets are sorted they are contaminated together and returned as final result.
Following is the Pseudocode for Bucket sort
function bucketSort(array, k) is 
  buckets ← new array of k empty lists
  Max ←the maximum key value in the array
  Min ←the minimum key value in the array 
  Divisor = Max-Min/k-1
  for i = 0 to length(array) 
  do insert array[i] into buckets[floor(array[i] - Min/ Divisor)] 
  for i = 0 to k 
  do Sort(buckets[i]) 
  return the concatenation of buckets[0] .....buckets[k]
Bucket sort implementation in java.
public class BucketSortExample {

  public static void main(String[] args) {
    int[] arr = {8, 4, 2, 10, 3, 1, 9, 6, 5, 7};
    int numBuckets = 5;
    bucketSort(arr, numBuckets);

    System.out.println("Sorted array:");
    for (int i = 0; i < arr.length; i++) {
      System.out.print(arr[i] + " ");

    }
  }

  public static void bucketSort(int[] arr, int numBuckets) {

    // create empty buckets total numBuckets number of buckets
    ArrayList<ArrayList<Integer>> buckets = new ArrayList<>(numBuckets);
    for (int i = 0; i < numBuckets; i++) {
      buckets.add(new ArrayList<Integer>());

    }

    distributeToBuckets(arr, numBuckets, buckets);

    // sort the elements in each bucket
    for (ArrayList<Integer> bucket : buckets) {
      Collections.sort(bucket);
    }

    // concatenate the elements in each bucket to obtain the sorted array
    int index = 0;
    for (ArrayList<Integer> bucket : buckets) {
      for (int item : bucket) {
        arr[index++] = item;

      }

    }

  }

  private static void distributeToBuckets(int[] arr, int numBuckets,
      ArrayList<ArrayList<Integer>> buckets) {
    // determine minimum and maximum values of input array
    int min = Arrays.stream(arr).min().getAsInt();
    int max = Arrays.stream(arr).max().getAsInt();
    float diff = max - min;
    float divisor = diff / (numBuckets - 1);

    // place elements into buckets based on value
    for (int i = 0; i < arr.length; i++) {
      int index = (int) ((arr[i] - min) / divisor);
      buckets.get(index).add(arr[i]);

    }
  }

}

Complexity

The best case for bucket sort happens when the data can be distributed evenly across the buckets. These leads to a situation when non of the buckets are overloaded. This uniform distribution minimizes the complexity of sorting each bucket, leading to an overall time complexity of O(n+k), where n represents the number of elements and k refers to the number of buckets Best Case Complexity : O(n+k)
Average Case Complexity : O(n+k)
Worst Case complexity: O(n²)
Space complexity : O(n+k)

Summary

Bucket sort is a unique sorting method where the inputs are distributed in individual buckets and then the buckets are sorted using other sorting mechanism.

IntSummaryStatistics

IntSummaryStatistics provides different statistical data like max, min, average. This class is desinged to work with Java streams but can work with with out streams also.

Example

Following class shows an example of IntSummaryStatistics used with stream. It is used to print interesting information like max, min, average, sum and count. And then the accept method is used to add a new integer to the previous data passed as the numStream.
public class IntSummaryStatisticsExample {

  public static void main(String[] args) {
   
    Stream<Integer> numStream = Stream.of(1, 2, 3, 4, 5);
    IntSummaryStatistics summary = numStream.mapToInt(p-> p).summaryStatistics();
    
    System.out.println("Max From the Data is " + summary.getMax());
    System.out.println("Min From the Data is " + summary.getMin());
    System.out.println("Average From the Data is " + summary.getAverage());
    System.out.println("Sum From the Data is " + summary.getSum());
    System.out.println("Count From the Data is " + summary.getCount());
    
    //Add a new number to the stream
    System.out.println("\n");
    summary.accept(10);
    
    System.out.println("Max From the Data is " + summary.getMax());
    System.out.println("Min From the Data is " + summary.getMin());
    System.out.println("Average From the Data is " + summary.getAverage());
    System.out.println("Sum From the Data is " + summary.getSum());
    System.out.println("Count From the Data is " + summary.getCount());
   
  }
}
IntSummaryStatistics Example Max From the Data is 5 Min From the Data is 1 Average From the Data is 3.0 Sum From the Data is 15 Count From the Data is 5 Adding number 10 to the data Max From the Data is 10 Min From the Data is 1 Average From the Data is 4.166666666666667 Sum From the Data is 25 Count From the Data is 6
IntSummaryStatistics Example
Max From the Data is 5
Min From the Data is 1
Average From the Data is 3.0
Sum From the Data is 15
Count From the Data is 5

Adding number 10 to the data
Max From the Data is 10
Min From the Data is 1
Average From the Data is 4.166666666666667
Sum From the Data is 25
Count From the Data is 6

Conclusion

IntSummaryStatistics defined in java.util package and available since java 8 provides a way to calculate statistical information. It works with and with out stream.

How to convert Java Stream to List

Convert Java Stream to List
Following code show how to convert the stream intStream to list using collect.
 // Converting Streams to Collection
 Stream<Integer> intStream = Stream.of(1, 2, 3, 4, 5);
 List<Integer> list = intStream.collect(Collectors.toList());
 System.out.println(list); // prints [1, 2, 3, 4, 5]

Arduino Humadity

January 31, 2024

#include 

#include 

dht DHT;

#define DHT11_PIN 7

void setup(){
  Serial.begin(9600);
}

void loop(){
  int chk = DHT.read11(DHT11_PIN);
  Serial.print("Temperature = ");
  Serial.println(DHT.temperature);
  Serial.print("Humidity = ");
  Serial.println(DHT.humidity);
  delay(1000);
}

Ardino Gas Sensors

January 31, 2024

 


//#include 

#define MQ2pin (0)
//https://robu.in/mq2-sensor-interfacing-with-arduino-gas-and-smoke-detection/
float sensorValue;  //variable to store sensor value

void setup()
{
  Serial.begin(9600); // sets the serial port to 9600
  Serial.println("Gas sensor warming up!");
  Serial.println("I can detect 300 - 10000ppm");

  delay(20000); // allow the MQ-2 to warm up
}

void loop()
{
  sensorValue = analogRead(MQ2pin); // read analog input pin 0
  
  Serial.print("Sensor Value: ");
  Serial.print(sensorValue);
  
  Serial.println("");
  delay(2000); // wait 2s for next reading
}

Extract content of .war file

A WAR file (Web Application Resource or Web application Archive) is a file used to distribute a collection of files and folders associated with web application. Sometimes we want to look inside the war file to know what files or folders are inside the war file. Jar -xvf jar with option -xvf will extract the content of the war file. Following command will extract the content of blogapp.war file into the current directory

jar -xvf blogapp.war

To extract the content of war file type the above command. For instance for a war file blogapp.war we can use the command jar -xvf.
A generic square placeholder image with rounded corners in a figure.
Content of the blogapp.war file

Conclusion

jar -xvf can be used to extract the content of jar file. This command will work in any platform with installed java.

HashMap computeIfPresent() method in Java with Examples

The computeIfPresent(Key, BiFunction) method of HashMap class is used to update value in a key-value pair in the map. If key does not exist then it does not do any thing.

Syntax

undefined
V computeIfPresent(K key,
            BiFunction<? super K, ? super V, ? extends V> remappingFunction) 

Example

Following is an example of computeIfAbsent invocation and its results.
ComputeIfAbsentExample
package corejava.map;

import java.util.HashMap;
import java.util.Map;

public class ComputeIfPresentExample {

	public static void main (String argv[]) {
		
		Map<String, Integer> map = new HashMap<String, Integer>();

		map.put("Sunglass", 105);
		map.put("Watch", 1501);
		
		
		newline("Original Map");
		map.forEach((a, b) -> {
			System.out.println(a + " -> " + b);
		});
		
		
		map.computeIfPresent("Watch", (k,v) -> {return v + v;});
		
		newline("After calling computeIfAbsent Map");
		map.forEach((a, b) -> {
			System.out.println(a + " -> " + b);
		});
	}
	
	static void newline(String s) {
		System.out.println("\n" + s);
	};
}
Output of above program.
Original Map
Watch -> 1501
Sunglass -> 105

After calling computeIfAbsent Map
Watch -> 3002
Sunglass -> 105

Summary

computeIfPresent helps in writing more compact code and also make code more readable.

HashMap computeIfAbsent() method in Java with Examples

The computeIfAbsent(Key, Function) method of HashMap class is used to enter a new key-value pair to map if the key does not exist. If key exists then it does not do any thing.

Syntax

undefined
public V computeIfAbsent(K key, Function<? super K, ? extends V> remappingFunction)

Example

Following is an example of computeIfAbsent invocation and its results.
ComputeIfAbsentExample
package corejava.map;

import java.util.HashMap;
import java.util.Map;

public class ComputeIfAbsentExample {

	public static void main (String argv[]) {
		
		Map<Integer, String> map = new HashMap<Integer, String>();

		map.put(1, "one");
		map.put(2, "two");
		map.put(3, "three");
		map.put(4, "four");
		map.put(5, "five");
		
		newline("Original Map");
		map.forEach((a, b) -> {
			System.out.println(a + " -> " + b);
		});
		
		map.computeIfAbsent(6, k-> { return "six";});
		map.computeIfAbsent(7, k -> "seven");
		
		newline("After calling computeIfAbsent Map");
		map.forEach((a, b) -> {
			System.out.println(a + " -> " + b);
		});
	}
	
	static void newline(String s) {
		System.out.println("\n" + s);
	};
}
Output of above program.
Original Map
1 -> one
2 -> two
3 -> three
4 -> four
5 -> five

After calling computeIfAbsent Map
1 -> one
2 -> two
3 -> three
4 -> four
5 -> five
6 -> six
7 -> seven

Summary

computeIfAbsent helps in writing more compact code and also make code more readable.