Skip to content

Instantly share code, notes, and snippets.

@sukhchander
Created July 30, 2018 13:54
Show Gist options
  • Save sukhchander/9f36509b1f873277ab3d1d5c7564d595 to your computer and use it in GitHub Desktop.
Save sukhchander/9f36509b1f873277ab3d1d5c7564d595 to your computer and use it in GitHub Desktop.
NiceBag.java
/*
The package has a weight limitation.
Your goal is to determine which things to put into the package so that the total weight is less than or equal to the package limit and the total cost is as large as possible.
You would prefer to send a package which has less weight if there is more than one package with the same price.
This is a variation of the Knapsack problem.
Input:
Your program should read lines from standard input. Each line contains the weight that a package can take (before the colon) and the list of things you need to pick from. Each thing is enclosed in parentheses where the 1st number is a thing's index number, the 2nd is its weight and the 3rd is its cost.
Max weight any package can take is <= 100.
There might be up to 15 things you need to choose from.
Max weight and max cost of any thing is <= 100.
Output:
For each set of things produce a list of things (their index numbers separated by comma) that you put into the package. If none of the items will fit in the package, print a hyphen (-).
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.io.FileReader;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
import java.util.Comparator;
import java.util.stream.Collectors;
class Item {
int index;
float weight;
float cost;
public int getIndex() {
return this.index;
}
public float getWeight() {
return this.weight;
}
public float getCost() {
return this.cost;
}
public void setIndex(int index) {
this.index = index;
}
public void setWeight(float weight) {
this.weight = weight;
}
public void setCost(float cost) {
this.cost = cost;
}
}
public class Main {
public static Comparator<Item> customComparator = new Comparator<Item>() {
public int compare(Item o1, Item o2) {
int result = Float.compare(o2.getCost(), o1.getCost());
return result == 0 ? Float.compare(o1.getWeight(), o2.getWeight()) : result;
}
};
public static List<Integer> fillBag(Float bag_weight, List<Item> items) {
List<Integer> selected = new ArrayList<>();
Collections.sort(items, customComparator);
for (Item item : items) {
if (item.getWeight() <= bag_weight) {
bag_weight -= item.getWeight();
selected.add(item.getIndex());
}
}
return selected;
}
static final int MAX_BAG_WEIGHT = 100;
public static void main(String[] args) throws IOException {
try {
Float bag_weight;
List<Item> items;
InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
BufferedReader in = new BufferedReader(reader);
String readLine;
while ((readLine = in.readLine()) != null) {
items = new ArrayList<>();
String[] testCase = readLine.split(" : ");
bag_weight = Float.parseFloat(testCase[0]);
if (bag_weight <= MAX_BAG_WEIGHT) {
for (String item : testCase[1].split(" ")) {
String[] listItems = (item.replaceAll("[^0-9/.,]+", "")).split(",");
int index = Integer.parseInt(listItems[0]);
Float mass = Float.parseFloat(listItems[1]);
Float price = Float.parseFloat(listItems[2]);
if (mass <= 100.0 && price <= 100.0) {
Item currItem = new Item();
currItem.setCost(price);
currItem.setIndex(index);
currItem.setWeight(mass);
items.add(currItem);
} else {
System.out.println("Error: Case Item exceeds value of 100");
items.clear();
break;
}
}
if (items.size() > 0) {
List<Integer> sizes = fillBag(bag_weight, items);
String result = sizes.stream().map(n -> n.toString()).collect(Collectors.joining(","));
System.out.println(result);
}
} else {
System.out.println("Error: Case exceeds maximum bag capacity of 100");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment