Skip to content

Shyam goodies test submission #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
55 changes: 55 additions & 0 deletions 2020-Freshers/goodie-dilemma/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="target/generated-sources/annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
23 changes: 23 additions & 0 deletions 2020-Freshers/goodie-dilemma/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>goddies</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eclipse.preferences.version=1
org.eclipse.jdt.apt.aptEnabled=false
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
org.eclipse.jdt.core.compiler.compliance=1.5
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
org.eclipse.jdt.core.compiler.processAnnotations=disabled
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.5
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
14 changes: 14 additions & 0 deletions 2020-Freshers/goodie-dilemma/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Number of employees: 6

Goodies and Prices:

Fitbit Plus: 7980
IPods: 22349
MI Band: 999
Cult Pass: 2799
Macbook Pro: 229900
Digital Camera: 11101
Alexa: 9999
Sandwich Toaster: 2195
Microwave Oven: 9800
Scale: 4999
10 changes: 10 additions & 0 deletions 2020-Freshers/goodie-dilemma/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
The goodies selected for distribution are:

Sandwich Toaster: 2195
Cult Pass: 2799
Scale: 4999
Fitbit Plus: 7980
Microwave Oven: 9800
Alexa: 9999

And the difference between the chosen goodie with highest price and the lowest price is 7804
6 changes: 6 additions & 0 deletions 2020-Freshers/goodie-dilemma/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.goddies</groupId>
<artifactId>goddies</artifactId>
<version>0.0.1-SNAPSHOT</version>
</project>
34 changes: 34 additions & 0 deletions 2020-Freshers/goodie-dilemma/src/main/java/Goodie.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

public class Goodie {

String name;
long price;

public Goodie(String name, long price) {
super();
this.name = name;
this.price = price;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public long getPrice() {
return price;
}

public void setPrice(long price) {
this.price = price;
}

@Override
public String toString() {
return this.getName() + ": " + this.getPrice();
}

}
202 changes: 202 additions & 0 deletions 2020-Freshers/goodie-dilemma/src/main/java/GoodiesApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class GoodiesApp {

static int noOfEmployee = 0;

public static void main(String[] args) {
List<Goodie> goodies = readGoodiesFromFile();
selectGoodies(goodies, noOfEmployee);
System.out.println("Distribution done, please look into file for the result.");
}

/**
*
* @param goodies: list of goodies sorted in ASC order
* @param M: No of employee among which this goodies needs to be
* distributed
*/
static public void selectGoodies(List<Goodie> goodies, int M) {
int windowSize = M;

// sorting the goodies list in ASC order by price
// intention behind doing this to get the min & max of each window
mergeSort(goodies, 0, goodies.size() - 1);

int start = 0;
// defining window size as number employee
int end = windowSize - 1;
long min = Long.MAX_VALUE;
int j = 0;

// using sliding window concept to iterate on each window
// find the difference b/w max & min of each window
// comparing with prev min

// i is the start of window
for (int i = 0; i <= goodies.size() - windowSize; i++) {

// calculating end point of a window
j = i + windowSize - 1;
// calculating diff of min & max
long diff = goodies.get(j).getPrice() - goodies.get(i).getPrice();

// comparing with prev min
if (diff < min) {
start = i;
end = j;
min = diff;
}
}

// Writing to file
writeToFile(goodies, start, end);
}

/**
*
* @param goodies: list of goodies sorted in ASC order
* @param start: start index of the window
* @param end: end index of the window
*/
static void writeToFile(List<Goodie> goodies, int start, int end) {
File file = new File("output.txt");
FileWriter fr = null;
BufferedWriter br = null;
try {
fr = new FileWriter(file);
br = new BufferedWriter(fr);
br.write("The goodies selected for distribution are:\n");
br.write("\n");
for (int i = start; i <= end; i++) {
br.write(goodies.get(i).toString());
br.write("\n");
}
br.write("\n");
br.write("And the difference between the chosen goodie with highest price and the lowest price is "
+ (goodies.get(end).getPrice() - goodies.get(start).getPrice()));
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

/**
* utility method to read goodies from input file and create Goodie object and
* collect in a list
*
* @return
*/
static List<Goodie> readGoodiesFromFile() {
String fileName = "input.txt";
List<Goodie> goodies = new ArrayList<Goodie>();
BufferedReader buffer = null;
int M = 0;
try {
String line = "";
buffer = new BufferedReader(new FileReader(fileName));
int linecount = 1;

while ((line = buffer.readLine()) != null) {
if (linecount == 1) {
M = Integer.parseInt(line.split(":")[1].trim());
} else if (linecount >= 5) {
String goodieInfo[] = line.split(":");
String goodieName = goodieInfo[0];
long price = Long.parseLong(goodieInfo[1].trim());
Goodie goodie = new Goodie(goodieName, price);
goodies.add(goodie);
}
linecount++;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (buffer != null) {
try {
buffer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

noOfEmployee = M;
return goodies;
}

/**
* Utility to sort goodies
*
* @param goodies
* @param left
* @param right
*/
public static void mergeSort(List<Goodie> goodies, int left, int right) {
int mid = (left + right) / 2;
if (left < right) {
mergeSort(goodies, left, mid);
mergeSort(goodies, mid + 1, right);
mergeArray(goodies, left, right, mid);

}
}

/**
*
* Program to sort the goodies in ascending order on price attribute
*
* @param goodies
* @param left
* @param right
* @param mid
*/
public static void mergeArray(List<Goodie> goodies, int left, int right, int mid) {
int size1 = mid - left + 1;
int size2 = right - mid;

List<Goodie> g1 = new ArrayList<Goodie>();
List<Goodie> g2 = new ArrayList<Goodie>();

for (int i = 0; i < size1; i++) {
g1.add(goodies.get(left + i));
}
for (int i = 0; i < size2; i++) {
g2.add(goodies.get(mid + 1 + i));
}
int l = 0, r = 0;
int k = left;
while (l < size1 && r < size2) {
if (g1.get(l).getPrice() <= g2.get(r).getPrice()) {
goodies.set(k++, g1.get(l));
l++;
} else {
goodies.set(k++, g2.get(r));
r++;
}
}
while (l < size1) {
goodies.set(k++, g1.get(l));
l++;
}
while (r < size2) {
goodies.set(k++, g2.get(r));
r++;
}

}

}
14 changes: 14 additions & 0 deletions 2020-Freshers/goodie-dilemma/src/main/resources/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Number of employees: 2

Goodies and Prices:

Fitbit Plus: 7980
IPods: 22349
MI Band: 999
Cult Pass: 2799
Macbook Pro: 229900
Digital Camera: 11101
Alexa: 9999
Sandwich Toaster: 2195
Microwave Oven: 9800
Scale: 4999
Binary file added recording1_compress.mp4
Binary file not shown.