# ‘Library Fine’ problem in Hackerrank – solution in Scala

Problem Statement:

The Head Librarian at a library wants you to make a program that calculates the fine for returning the book after the return date. You are given the actual and the expected return dates. Calculate the fine as follows:

If the book is returned on or before the expected return date, no fine will be charged, in other words fine is 0.

If the book is returned in the same month as the expected return date, Fine = 15 Hackos × Number of late days

If the book is not returned in the same month but in the same year as the expected return date, Fine = 500 Hackos × Number of late months

If the book is not returned in the same year, the fine is fixed at 10000 Hackos.

Solution in Scala

``````
import java.util.{Calendar, Scanner}

object Solution {

def main(args: Array[String]) {
val scanner: Scanner = new Scanner(System.in)

//Actual Returned Date
val aMonth: Int = scanner.nextInt
val aYear: Int = scanner.nextInt

//Due Date
val dDate: Int = scanner.nextInt
val dMonth: Int = scanner.nextInt
val dYear: Int = scanner.nextInt

val isValidData: Boolean = ((aDate >= 1 && aDate = 1 && dDate = 1 && aMonth = 1 && dMonth = 1 && aYear = 1 && dYear <= 3000))

var fineAmount: Int = 0
if (isValidData) {
val actualCalendar: Calendar = Calendar.getInstance()

val dCalendar: Calendar = Calendar.getInstance()
dCalendar.set(dYear, dMonth, dDate)

if ((actualCalendar.getTime == dCalendar.getTime) || actualCalendar.getTime.before(dCalendar.getTime)) {
fineAmount = 0
}
else if (actualCalendar.getTime.after(dCalendar.getTime) && aYear == dYear) {
fineAmount = if ((aMonth == dMonth)) 15 * (aDate - dDate) else 500 * (aMonth - dMonth)
}
else {
fineAmount = 10000
}
}
println(fineAmount)
}
}
```
```

# Mock System class with Mockito + PowerMock

In this post, I am going to show how to mock the System.getenv and System.getProperty methods.

I have used Mockito and PowerMock to do this. Make sure to include the below dependencies in your pom file.

``````
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.6.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.6.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito-common</artifactId>
<version>1.6.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-core</artifactId>
<version>1.6.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock.tests</groupId>
<artifactId>powermock-tests-utils</artifactId>
<version>1.6.4</version>
<scope>test</scope>
</dependency>
```
```

I have created an another util class which has the getEnv and getProperty methods. So instead of mocking the System class methods explicitly, I have mocked the MockUtils static methods.

MockUtils.java

``````
package com;

import org.apache.commons.lang3.StringUtils;

public class MockUtils {

public static String getEnv(String name) {
return StringUtils.defaultIfBlank(System.getenv(name), "");
}

public static String getProperty(String name) {
return StringUtils.defaultIfBlank(System.getProperty(name), "");
}
}
```
```

Here is my test class.

MockTest.java

``````

package com;

import com.MockUtils;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.modules.junit4.PowerMockRunner;

import static junit.framework.Assert.assertEquals;

@RunWith(PowerMockRunner.class)
@PrepareForTest(MockUtils.class)
@PowerMockIgnore("javax.management.*")
public class MockTest {

@BeforeClass
public static void setupClass() throws Exception {
PowerMockito.mockStatic(MockUtils.class);
PowerMockito.when(MockUtils.getEnv("APP_ENV")).thenReturn("DEV");
}

@Test
public void testMockUtils() {
assertEquals("DEV", MockUtils.getEnv("APP_ENV"));
}

```
```

Sometimes the static method might be used in the underlying classes and if those classes are not using the mocked values, then you can initialize the mocked values in the @Before like below,

``````
import static junit.framework.Assert.assertEquals;

@RunWith(PowerMockRunner.class)
@PrepareForTest(MockUtils.class)
@PowerMockIgnore("javax.management.*")
public class MockTest {

@Before
public void setupClass() throws Exception {
PowerMockito.mockStatic(MockUtils.class);
PowerMockito.when(MockUtils.getEnv("APP_ENV")).thenReturn("DEV");
}

@Test
public void testMockUtils() {
assertEquals("DEV", MockUtils.getEnv("APP_ENV"));
}

```
```

# Spark Scala Unit Testing

In this post, I am going to show an example for writing unit test cases for Spark Scala job and run it with Maven.

Assume that we have a set of XML files which has user information like first name, last name and etc. Assume that middle name and county name are optional fields but the XML file does contain empty nodes for these two fields. So now our job is to read those files and remove those empty nodes and output those updated content into a text file either in local env or hadoop env.

The sample XML content is given below,

```<persons>
<person>
<firstName>Bala</firstName>
<middleName/>
<lastName>Samy</lastName>
<countyName/>
</person>
<person>
<firstName>Bala1</firstName>
<middleName/>
<lastName>Samy1</lastName>
<countyName/>
</person>
</persons>
```

The Spark scala code for reading XML files and removing the empty nodes are given below.

``````
package com

import org.apache.spark.{SparkConf, SparkContext}

import scala.collection.Map

object EmptyTagReplacer {

def main(args: Array[String]) {

if (args.length < 2) {
println("Usage <inputDir> <outputDir>")
}
val conf = new SparkConf().setAppName("EmptyTagReplacer")
val sc = new SparkContext(conf)

val inFile = args(0)
val outFile = args(1)

val input: Map[String, String] = sc.wholeTextFiles(inFile).collectAsMap()
searchAndReplaceEmptyTags(sc, input, outFile)
sc.stop()
}

def searchAndReplaceEmptyTags(sc: SparkContext, inputXml: Map[String, String], outFile: String):
scala.collection.mutable.ListBuffer[String] = {

var outputXml = new scala.collection.mutable.ListBuffer[String]()
val htmlTags = List("<middleName/>", "<countyName/>")
inputXml.foreach { case (fileName, content) =>
var newContent = content
for (tag  <- htmlTags) {
val data = sc.parallelize(newContent)
data.saveAsTextFile(outFile + "/" + fileName)
}
outputXml += newContent
}
outputXml
}

def countTags(sc: SparkContext, xmlRecords: List[String]): List[Int] = {

var middleNameTagCounter = sc.accumulator(0)
var countyTagCounter = sc.accumulator(0)
val middleNameRegex = "<middleName/>".r
val countyRegEx = "<countyName/>".r
xmlRecords.foreach { content =>
middleNameTagCounter += middleNameRegex.findAllIn(content).length
countyTagCounter += countyRegEx.findAllIn(content).length
}
List(middleNameTagCounter.value, countyTagCounter.value)
}
}
```
```

Now the test case for testing the above spark job is given below,

``````

package com

import java.io.File

import com.holdenkarau.spark.testing.SharedSparkContext
import org.apache.commons.io.FileUtils
import org.scalatest.FunSuite
import collection.mutable.Map

//import scala.io.Source._

class EmptyTagReplacerTest extends FunSuite with SharedSparkContext {

test("Empty HTML tag replacer test") {

//Read the content and create a content Map.
//val content: String = scala.io.Source.fromFile("./src/test/resources/text-files/xml1").mkString
val content: String =  FileUtils.readFileToString(new File("./src/test/resources/text-files/xml1"), "UTF-8")

println("content"+content)
val contentMap = collection.mutable.Map[String, String]()
contentMap.+=("fileName" -> content)
//Call searchAndReplaceMethod to remove empty Nodes
val outputContent: scala.collection.mutable.ListBuffer[String] = EmptyTagReplacer.searchAndReplaceEmptyTags(sc, contentMap, "")
val counts: List[Int] = EmptyTagReplacer.countTags(sc, outputContent.toList)
println(counts)
val expected = List(0, 0)
assert(counts == expected)
}
}

```
```

You have to include the scala-maven-plugin and scalatest-maven-plugin in pom.xml to make this work.

Please refer my github repo to know more https://github.com/dkbalachandar/scala-spark-test

# Bucket Sorting In Java

This algorithm works by distributing the element into a buckets, then sorting the bucket contents, finally combining the all the buckets one by one

Best case and Average case Time complexity is O(n+m), where m is the largest number in the data set

Worst case time complexity is O(n^2)

Please refer the below GitHub repository

https://github.com/dkbalachandar/bucketsorting

# Algorithm Time Complexity

O(1) – Completes the operation/process in a single step. Best example is the number to search in an array is present in the first index itself
O(n) – Completes the operation/process in n steps. So the time gets increased linearly. Best example is the number to find in an array is present in the last index only
O(log n) – Completes the operation/process in n/2 steps. Best example, do a binary search in a sorted array.
O(n^2) – Completes the operation n^2 steps. Best example is sorting an array by bubble sort.
O(n log n) – Completes the operation n*n/2 steps. Best example is sorting an array by quick sort.

# Find middle element in a linkedlist

```   private void findMiddleElement() {

//Move by one node at a time
//Move by two nodes at a time
while (secondPointer!= null && secondPointer.getNext() != null &&
secondPointer.getNext().getNext() != null) {
firstPointer = firstPointer.getNext();
secondPointer = secondPointer.getNext().getNext();
}
System.out.println("Middle Element is: "+ firstPointer.getData());

}
```

# Convert Sorted Array to BST

``````
import org.apache.commons.lang3.builder.ToStringBuilder;

/**
* Convert a sorted array into a Binary Search Tree
*
*/
public class TreeUtils {

static class Tree {
int val;
Tree left;
Tree right;

Tree(int x) {
val = x;
}
@Override
public String toString() {
}
}

public static void main(String[] args) {
int num[] = {1, 2, 3, 4, 5, 6, 7};
Tree tree = sortedArrayToBST(num, 0, num.length - 1);
System.out.println(tree);
}

public static Tree sortedArrayToBST(int[] num, int start, int end) {
if (start > end)
return null;
int middle= (start + end) / 2;
Tree root = new Tree(num[middle]);
root.left = sortedArrayToBST(num, start, middle - 1);
root.right = sortedArrayToBST(num, middle + 1, end);
return root;
}
}
```
```