Hiking around HackerRank — 04

07/31/2020

Note:

  1. All problems are published by HackerRank, credit to HackerRank
  2. The solutions are coded by myself, and open source just for study.

Java BitSet

Referred to this doc on GeeksforGeeks, we can know that BitSet in Java would create an array represented by boolean values[1].

By the way, if we want to print the number of “1” in a BitSet, we can use the “.cardinality()” method.

Then, let’s do it!

That’s what my code looked like:

import java.io.*;
import java.util.*;

public class Solution {

public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner input = new Scanner(System.in);
int size = input.nextInt();
int op = input.nextInt();

BitSet bs1 = new BitSet(a);
BitSet bs2 = new BitSet(b);

while (op -- > 0){
String s = input.nextLine();
int x = input.nextInt();
int y = input.nextInt();

if (s.equals("AND")) {
if (x == 1) {
bs1.and(bs2);
}
else {
bs2.and(bs1);
}
}
else if ( s.equals("OR")) {
if (x == 1) {
bs1.or(bs2);
}
else {
bs2.or(bs1);
}
}
else if ( s.equals("XOR")) {
if (x == 1) {
bs1.xor(bs2);
}
else {
bs2.xor(bs1);
}
}
else if (s.equals("FLIP")) {
if (x == 1){
bs1.flip(y);
}
else {
bs2.flip(y);
}
}
else {
if (x == 1){
bs1.set(y);
}
else {
bs2.set(y);
}
}
}

System.out.println(bs1.cardinality() + " " + bs2.cardinality());

}
}

Well, it is not elegant, and I felt something was wrong. But I still want to give it a try.

As expected, it could run, but we encountered a run-time error.

I was completely lost, what should I do then?

As usual, I looked for help on the HackerRank forum. Fortunately, my idea was almost correct, but it is more elegant and efficient to use “switch.” Another try!

import java.io.*;
import java.util.*;

public class Solution {

public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner input = new Scanner(System.in);
int size = input.nextInt();
int op = input.nextInt();

BitSet bs1 = new BitSet(size);
BitSet bs2 = new BitSet(size);
import java.io.*;
import java.util.*;

public class Solution {

public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner input = new Scanner(System.in);
int size = input.nextInt();
int op = input.nextInt();

BitSet bs1 = new BitSet(size);
BitSet bs2 = new BitSet(size);

while (op -- > 0) {
String s = input.nextLine();
int i = input.nextInt();
int j = input.nextInt();

switch (s) {
case("AND"):
if (i == 1) {
bs1.and(bs2);
} else {
bs2.and(bs1);
}
case("OR"):
if (i == 1) {
bs1.or(bs2);
}
else {
bs2.or(bs1);
}
case("XOR"):
if (i == 1) {
bs1.xor(bs2);
}
else{
bs2.xor(bs1);
}
case("FLIP"):
if (i == 1) {
bs1.flip(j);
}
else {
bs2.flip(j);
}
case("SET"):
if (i == 1){
bs1.set(j);
}
else {
bs2.set(j);
}
}
}

System.out.println(bs1.cardinality() + " " + bs2.cardinality());

}
}
while (op -- > 0) {
String s = input.nextLine();
int i = input.nextInt();
int j = input.nextInt();

switch (s) {
case("AND"):
if (i == 1) {
bs1.and(bs2);
} else {
bs2.and(bs1);
}
case("OR"):
if (i == 1) {
bs1.or(bs2);
}
else {
bs2.or(bs1);
}
case("XOR"):
if (i == 1) {
bs1.xor(bs2);
}
else{
bs2.xor(bs1);
}
case("FLIP"):
if (i == 1) {
bs1.flip(j);
}
else {
bs2.flip(j);
}
case("SET"):
if (i == 1){
bs1.set(j);
}
else {
bs2.set(j);
}
}
}

System.out.println(bs1.cardinality() + " " + bs2.cardinality());

}
}

Unfortunately, the same problem generated, too. What’s wrong with it?

Well, it seems if we want to add a conditional statement in the “Switch” statement, we need to do something else. That’s what I got:

import java.io.*;
import java.util.*;

public class Solution {

public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner input = new Scanner(System.in);
int size = input.nextInt();
int op = input.nextInt();

BitSet bs1 = new BitSet(size);
BitSet bs2 = new BitSet(size);

while (op -- > 0) {
String s = input.next();
int i = input.nextInt();
int j = input.nextInt();

switch (s) {
case("AND"):
if (i == 1) {
bs1.and(bs2);
} else {
bs2.and(bs1);
}
break;
case("OR"):
if (i == 1) {
bs1.or(bs2);
}
else {
bs2.or(bs1);
}
break;
case("XOR"):
if (i == 1) {
bs1.xor(bs2);
}
else{
bs2.xor(bs1);
}
break;
case("FLIP"):
if (i == 1) {
bs1.flip(j);
}
else {
bs2.flip(j);
}
break;
case("SET"):
if (i == 1){
bs1.set(j);
}
else {
bs2.set(j);
}
break;

}
System.out.println(bs1.cardinality() + " " + bs2.cardinality());
}
input.close();

}
}

Awesome! It compiled this time!

However, when I check other programmer’s submissions, I noticed an interesting one. We have almost the same idea, but he added “default: break” after the “case(“SET”).” I’m not supposed to publish his work in this blog, but you can possibly find it!

Java Inheritance I

To be honest, this exercise gave me a lot of confidence. And here’s what I’ve done:

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

class Animal{
void walk()
{
System.out.println("I am walking");
}
}
class Bird extends Animal
{
void fly()
{
System.out.println("I am flying");
}

void sing() {
System.out.println("I am singing");
}
}

public class Solution{

public static void main(String args[]){

Bird bird = new Bird();
bird.walk();
bird.fly();
bird.sing();

}
}

Done!

Java Inheritance II

Frankly speaking, I was kind of confused when I was reading the statements. However, things became clear when it came to code:

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

//Write your code here
class Arithmetic {
public int calc(int a, int b) {
int sum = a + b;
return sum;
}
}

class Adder extends Arithmetic {
public int add(int x, int y) {
return calc(x, y);
}
}
public class Solution{
public static void main(String []args){
// Create a new Adder object
Adder a = new Adder();

// Print the name of the superclass on a new line
System.out.println("My superclass is: " + a.getClass().getSuperclass().getName());

// Print the result of 3 calls to Adder's `add(int,int)` method as 3 space-separated integers:
System.out.print(a.add(10,32) + " " + a.add(10,3) + " " + a.add(10,10) + "\n");
}
}

See, we are just creating a class, extend it, and call it. Nothing special, right? But pretty useful!

Conclusion:

OOP is an inevitable topic in Java. Hope we can dig into it further. Admittedly, I’ve still trying to pick up my skills, and it is not easy. Anyway, let’s enjoy coding together!

My repo:

References:

[1] Java BitSet, GeeksforGeeks, retrieved from https://www.geeksforgeeks.org/bitset-class-java-set-1/

Undergraduate student / Research assistant/ Always curious / Opinions are mine