Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 30 additions & 46 deletions src/main/java/org/jugbd/marathon/day2/BiggestNumber.java
Original file line number Diff line number Diff line change
@@ -1,50 +1,34 @@
package org.jugbd.marathon.day2;

import java.util.Arrays;

public class BiggestNumber {
public static long findTheNextBiggerNumber(long n) {

int ar[] = new int[20]; // long is 64 bit, so max 19 digits can be possible
int length = 0, tmp;
boolean found = false;
while (n > 0) {

ar[length++] = (int) (n % 10);
n /= 10;
}

for (int i = 0, j = length - 1; i < length / 2; i++, j--) {
tmp = ar[i];
ar[i] = ar[j];
ar[j] = tmp;
}

// 52876

for (int i = length - 1; i >= 0; i--) {
for (int j = i - 1; j >= 0; j--) {
if (ar[j] < ar[i]) {

tmp = ar[i];
ar[i] = ar[j];
ar[j] = tmp;
Arrays.sort(ar, j + 1, length);
found = true;
break;
}
}

if (found) {
break;
}
}

n = 0;
for (int i = 0; i < length; i++) {
n = n * 10 + ar[i];
}

return found ? n : -1;
}
public static long findTheNextBiggerNumber(long n) {

char[] digits = String.valueOf(n).toCharArray();
if (digits.length == 1)
return -1;
for (int i = digits.length - 2; i >= 0; i--) {
int index = getImmediateMaxDigitAsIndex(digits, i + 1, digits.length, digits[i]);
if (index != -1) {
char ch = digits[index];
for (int j = index - 1; j >= i; j--) {
digits[j + 1] = digits[j];
}
digits[i] = ch;
return Long.parseLong(new String(digits));
}
}
return -1;
}

static int getImmediateMaxDigitAsIndex(char[] digits, int startInd, int endInd, char givenDigit) {
int index = -1;
char min = '9';
for (int i = startInd; i < endInd; i++) {
if (digits[i] > givenDigit && min > digits[i]) {
index = i;
min = digits[i];
}
}
return index;
}
}
58 changes: 27 additions & 31 deletions src/main/java/org/jugbd/marathon/day3/TrappingTheRainWater.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,31 @@
package org.jugbd.marathon.day3;


public class TrappingTheRainWater {
static int findWaterUnit(int[] arr) {

int unitOfWater = 0, i, j, k;
boolean checked[] = new boolean[arr.length];

for(i=0;i<arr.length;i++) {

checked[i] = false;
}

for(i=0;i<arr.length;i++) {

if(checked[i] == false) {

for(j=i+1;j<arr.length;j++) {

if(arr[i] <= arr[j] && checked[j] == false) {

for(k=i+1;k<j;k++) {

unitOfWater += (arr[i]-arr[k]);
checked[k] = true;
}
break;
}
}
}
}
return unitOfWater;
}
static int findWaterUnit(int[] arr) {
int unitOfWater = 0;
int length = arr.length - 1;
int i = 0;
while (i < length) {
if (arr[i] != 0) {
int j = i + 1;
int sum = 0;
boolean canHold = true;
while (arr[i] > arr[j]) {
sum += arr[i] - arr[j];
if (j == length) {
canHold = false;
break;
}
j++;
}
if (canHold) {
unitOfWater += sum;
i = j;
continue;
}
}
i++;
}
return unitOfWater;
}
}
69 changes: 44 additions & 25 deletions src/main/java/org/jugbd/marathon/day4/Directory.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,48 @@
package org.jugbd.marathon.day4;

import java.util.ArrayList;
import java.util.Date;

public class Directory {
private String name;
private Date createdDate;

public Directory(String root) {

}

public void add(File file) {

}

public int numberOfFiles() {

return 0;
}

public Object getName() {
return name;
}

public Date getCreatedDate() {
return createdDate;
}
import java.util.List;

public class Directory extends FileSystemComponent {
private String name;
private Date createdDate;
private List<FileSystemComponent> files = new ArrayList<>();

public Directory(String root) {
if (root == null || root.isEmpty()) {
throw new IllegalArgumentException(
"Directory name shouldn't be null or empty string");
}
this.name = root;
this.createdDate = new Date();
}

@Override
public void add(FileSystemComponent fileComponent) {
if (files.contains(fileComponent)) {
throw new IllegalArgumentException(fileComponent.getName()
+ " already exists in " + this.getName() + " directory");
} else if (fileComponent.hashCode() == this.hashCode()
&& fileComponent.getName().equals(this.getName())) {
throw new IllegalArgumentException(
"A parent directory can't hold itself as its child component");
}
files.add(fileComponent);
}

@Override
public int numberOfFiles() {
return files.size();
}

@Override
public String getName() {
return name;
}

@Override
public Date getCreatedDate() {
return createdDate;
}
}
24 changes: 14 additions & 10 deletions src/main/java/org/jugbd/marathon/day4/File.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package org.jugbd.marathon.day4;

public class File extends FileSystemComponent{
private String name;

public class File {
private String name;

public File(String name) {

}

public String getName() {
return name;
}
public File(String name) {
if (name == null || name.isEmpty()) {
throw new IllegalArgumentException(
"Directory name shouldn't be null or empty string");
}
this.name = name;
}

@Override
public String getName() {
return name;
}
}
22 changes: 22 additions & 0 deletions src/main/java/org/jugbd/marathon/day4/FileSystemComponent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.jugbd.marathon.day4;

import java.util.Date;

public abstract class FileSystemComponent {

public String getName() {
throw new UnsupportedOperationException();
}

public int numberOfFiles() {
throw new UnsupportedOperationException();
}

public void add(FileSystemComponent fileSystemComponent) {
throw new UnsupportedOperationException();
}

public Date getCreatedDate() {
throw new UnsupportedOperationException();
}
}
2 changes: 2 additions & 0 deletions src/test/java/org/jugbd/marathon/day2/BiggestNumberTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ public void basicTests() {
assertEquals(-1, BiggestNumber.findTheNextBiggerNumber(111));
assertEquals(-1, BiggestNumber.findTheNextBiggerNumber(531));
assertEquals(753772, BiggestNumber.findTheNextBiggerNumber(737752));
assertEquals(5499214, BiggestNumber.findTheNextBiggerNumber(5499142));
assertEquals(219, BiggestNumber.findTheNextBiggerNumber(192));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ public void basicTest() {
assertEquals(2, TrappingTheRainWater.findWaterUnit(new int[]{2, 0, 2}));
assertEquals(10, TrappingTheRainWater.findWaterUnit(new int[]{3, 0, 0, 2, 0, 4}));
assertEquals(6, TrappingTheRainWater.findWaterUnit(new int[]{0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1}));
assertEquals(0, TrappingTheRainWater.findWaterUnit(new int[]{0, 1, 0}));
assertEquals(4, TrappingTheRainWater.findWaterUnit(new int[]{3, 4, 0, 5, 2, 3, 0, 0}));
}
}
79 changes: 53 additions & 26 deletions src/test/java/org/jugbd/marathon/day4/FileSystemTest.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,62 @@
package org.jugbd.marathon.day4;


import org.junit.Test;

import java.util.Date;

import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertNotNull;

public class FileSystemTest {


@Test
public void basicTest() {
String rootDirectoryName = "root";
Directory root = new Directory(rootDirectoryName);
assertNotNull(root);
assertEquals(rootDirectoryName, root.getName());

String fileName = "profile.jpg";
File file = new File(fileName);
assertNotNull(file);

assertEquals(fileName, file.getName());
import java.util.Date;

root.add(file);
assertEquals(1, root.numberOfFiles());
import org.junit.Test;

Date createdDate = root.getCreatedDate();
assertNotNull(createdDate);
public class FileSystemTest {

//Add more test cases
}
@Test
public void basicTest() {
String rootDirectoryName = "root";
Directory root = new Directory(rootDirectoryName);
assertNotNull(root);
assertEquals(rootDirectoryName, root.getName());

String fileName = "profile.jpg";
File file = new File(fileName);
assertNotNull(file);

assertEquals(fileName, file.getName());

root.add(file);
assertEquals(1, root.numberOfFiles());

Date createdDate = root.getCreatedDate();
assertNotNull(createdDate);

FileSystemComponent dir2 = new Directory("Dir2");
root.add(dir2);
assertEquals(2, root.numberOfFiles());
}

@Test(expected = IllegalArgumentException.class)
public void testDuplicateFileExistance() {
FileSystemComponent root = new Directory("root");
File file = new File("profile.jpg");
root.add(file);
root.add(file);
assertEquals(1, root.numberOfFiles());
}

@Test(expected = IllegalArgumentException.class)
public void testEmptyOrNullDirectoryName(){
new Directory(null);
}

@Test(expected = IllegalArgumentException.class)
public void testEmptyOrNullFileName(){
new File("");
}

@Test(expected = IllegalArgumentException.class)
public void testParentAsAChild(){
FileSystemComponent root = new Directory("root");
root.add(root);
}


}