Posts tagged with: java

## HackerRank.com 30 Days of Code – Day 3: Intro to Conditional Statements

Here is my solution to HackerRank.com’s 30 days of code challenge, day 3. This is a basic introduction to conditional statements which I found a bit easy. My main take away from this was ensuring the quality of the input by making sure that the the input values were within the correct range and wrapping that conditional statement around the other application logic statements. It doesn’t make sense to continue on processing the input if it is not within the acceptable bounds.

```import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
scan.close();
String ans="";

//make sure n is within the constraints (between 1 and 100 inclusive)
if( n < 1 || n > 100){
ans="";
}
else
{
// if 'n' is NOT evenly divisible by 2 (i.e.: n is odd)
if(n%2==1){
ans = "Weird";
}
else //cover all of the even cases
{
//if n is between 2 and 5 inclusive
if(n >= 2 && n <= 5){ ans = "Not Weird"; } //if n is between 6 and 20 inclusive if(n >= 6 && n <= 20){ ans = "Weird"; } //if n is greater than 20 if(n > 20){
ans = "Not Weird";
}

/*
If  is even and in the inclusive range of 2 to 5, print Not Weird
If  is even and in the inclusive range of 6 to 20, print Weird
If  is even and greater than 20, print Not Weird
*/
}
}

System.out.println(ans);
}
}
```

## HackerRank.com 30 Days of Code – Day 2: Operators

Day 2 of HackerRank’s 30 days of code was a fairly simple task designed to teach operators.

```import java.util.*;
import java.math.*;

public class Arithmetic {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double mealCost = scan.nextDouble(); // original meal price
int tipPercent = scan.nextInt(); // tip percentage
int taxPercent = scan.nextInt(); // tax percentage
double totalCostOfMeal = 0;
double tipCost = 0;
scan.close();

// Write your calculation code here.
totalCostOfMeal = (mealCost + (mealCost * (tipPercent/100))) * ((taxPercent/100) + 1);
//System.out.println("mealCost: " + mealCost);
//System.out.println("tipPercent: " + (((float)tipPercent/100) + 1));
//System.out.println("taxPercent: " + (((float)taxPercent/100) + 1));
tipCost = (((float)tipPercent/100) * mealCost);
//System.out.println("tipCost: " + tipCost);
totalCostOfMeal = (mealCost*(((float)taxPercent/100) + 1)) + tipCost;
//System.out.println("totalCostOfMeal: " + totalCostOfMeal);

// cast the result of the rounding operation to an int and save it as totalCost
int totalCost = (int) Math.round(totalCostOfMeal);

System.out.println("The total meal cost is " + totalCost + " dollars.");
}
}```

## HackerRank.com Challenge – Diagonal Difference

This HackerRank challenge took some thinking. I needed to be able to accept a variable size multidimensional array, add the diagonals, and then find the absolute value of their difference. I haven’t needed to work with multidimensional arrays much in the past, so it took some working through the algorithm to ensure I was incrementing my values in the correct spots.

These challenges are great mental floss for coding while I learn Java.

```import java.io.*;
import java.util.*;
import java.lang.Math;

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 in = new Scanner(System.in);

//get the value that defines the size of the input matrix
int n = in.nextInt();

int[][] numberArray = new int[n][n];

String input = in.nextLine();;

int currentDiagnalPosition1 = 0, currentDiagnalPosition2 = n-1;
int runningTotal1 = 0, runningTotal2 = 0;

for(int j = 0; j < n; j++)
{
input = in.nextLine();

for(int k = 0; k < n; k++)
{
String[] data = input.trim().split("\\s+");

numberArray[j][k] = Integer.parseInt(data[k]);

if(currentDiagnalPosition1 == k)
{
runningTotal1 = runningTotal1 + numberArray[j][k];
}
if(currentDiagnalPosition2 == k)
{
runningTotal2 = runningTotal2 + numberArray[j][k];
}

}
currentDiagnalPosition1 = currentDiagnalPosition1 + 1;
currentDiagnalPosition2 = currentDiagnalPosition2 - 1;
}

System.out.println(Math.abs(runningTotal1-runningTotal2));

}
}
```

## HackerRank.com Challenge – A Very Big Sum

In this lesson on HackerRank, I was challenged to demonstrate my knowledge of the long data type by needing to add multiple large numbers that would have caused an integer to overflow.

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

public class Solution {

static long aVeryBigSum(int n, long[] ar) {
//Setup space on the heap to store our running total
long total = 0;

//Iterate over each element in array ar adding the value to the running total in total
for(long number : ar)
{
total = total + number;
}

//return the sum contained in total
}

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
long[] ar = new long[n];
for(int ar_i = 0; ar_i < n; ar_i++){
ar[ar_i] = in.nextLong();
}
long result = aVeryBigSum(n, ar);
System.out.println(result);
}
}
```

## HackerRank.com Challenge – Compare the Triplets

I recently completed the HackerRank.com Challenge “Compare the Triplets”. Below is the answer I submitted.

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

public class Solution {

static int[] solve(int a0, int a1, int a2, int b0, int b1, int b2){
// Complete this function

int score_a = 0;
int score_b = 0;

//verify all numbers are in the appropriate range.
if(a0 >= 1 && a0 <= 100 &&
a1 >= 1 && a1 <= 100 &&
a2 >= 1 && a2 <= 100 &&
b0 >= 1 && b0 <= 100 &&
b1 >= 1 && b1 <= 100 &&
b2 >= 1 && b2 <= 100)
{
//System.out.println("All numbers in valid range");

//tabulate score for a0/b0
if(a0 > b0)
{
score_a++;
}
else if(a0 < b0)
{
score_b++;
}

//tabulate score for a1/b1
if(a1 > b1)
{
score_a++;
}
else if(a1 < b1)
{
score_b++;
}
//tabulate score for a2/b2
if(a2 > b2)
{
score_a++;
}
else if(a2 < b2)
{
score_b++;
}

int[] number = {score_a, score_b};
return number;
}
else
{
System.out.println("The input needs to be between 1 and 100. Please correct the input and try again.");
int[] number = {0, 0};
return number;
}

}

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a0 = in.nextInt();
int a1 = in.nextInt();
int a2 = in.nextInt();
int b0 = in.nextInt();
int b1 = in.nextInt();
int b2 = in.nextInt();
int[] result = solve(a0, a1, a2, b0, b1, b2);
for (int i = 0; i < result.length; i++) {
System.out.print(result[i] + (i != result.length - 1 ? " " : ""));
}
System.out.println("");

}
}
```