feb 2015 problem 1 / 3 / 4

This commit is contained in:
Xevion
2020-02-21 05:45:14 -06:00
parent bc11562f49
commit ba32bcbd62
6 changed files with 178 additions and 0 deletions

View File

@@ -0,0 +1,20 @@
# Fate
Run my solution on **[repl.it](https://repl.it/@Xevion/A-Computer-Science-February-2015-Fate)!**
This one was interesting in all honesty. It's one of the few problems that stumped me, but I eventually got past. The rest simply stumped me and wouldn't budge.
I ended up implementing a much more robust class using ArrayLists, HashMaps, and Collections to support me.
My implementation essentially revolved around create a `Hand` object, using a `.add(String card)` method to add cards, which checked if it was above the specified card limit, and then consequently chose
one to remove. I used a `HashMap<String, Integer>` to count and then decide upon which card to remove.
I first used the cards as keys and counted up the value by 1 each time I iterated over it (once each in ArrayList). I then iterated over the `HashMap` using the `.keySet()` method (which returns a `Set` object you can iterate over easily) and simply found the card with the least count and the lowest card value.
Card Value was implemented using `String.compareTo`, which you'll remember is simply `this - that`, i.e. a capital letter `A` has a value of `65`, `B` has `66` and so on. As such, if `this - that` (e.g. 65 - 66) returned a negative value when comparing the new possible letter versus the current selected 'minimum card', I swapped as usual. This was all done in a single if statement.
After that, I simply used `ArrayList.remove()` to remove the String, which by the way, has `String.equals`, allowing one to remove without requiring a reference to the variable you pick.
Once completed, I began printing, which started with sorting the `ArrayList` using `Collections.sort`, which sorted it in "Alphabetical" format as the problem specified. Then, I printed to `sysout` in the correct format using `String.join(" ", array)` for simplicity.
A interesting problem, but it did stump me when I forgot to add necessary code - although I forget exactly *what* I forgot to add that made it difficult to debug...

View File

@@ -0,0 +1,68 @@
import static java.lang.System.out;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Collections;
import java.io.File;
import java.io.IOException;
class Hand {
private ArrayList<String> hand;
Hand() {hand = new ArrayList<String>();}
Hand(String[] initial) {
this();
for(String card : initial)
hand.add(card);
}
public void print() {
Collections.sort(this.hand);
out.println(String.join(" ", this.hand));
}
public void add(String card) {
this.hand.add(card);
// Begin process to remove a card from hand if exceeding 5 card limit
if(this.hand.size() > 5) {
HashMap<String, Integer> map = new HashMap<String, Integer>();
// Count each card into map
for(String next : this.hand)
if(map.get(next) != null)
map.put(next, map.get(next) + 1);
else
map.put(next, 1);
// Search for what card to remove
int min = 0;
String mintype = "";
for(String type : map.keySet()) {
int cur = map.get(type);
// if found is less, or found is equal, but foundtype is of less value than curmintype
if(cur < min || (cur == min && !mintype.equals(type) && type.compareTo(mintype) < 0) || mintype.equals("")) {
min = cur;
mintype = type;
}
}
// Remove card from map
hand.remove(mintype);
}
}
}
class Main {
public static void main(String[] args) throws IOException {
Scanner s = new Scanner(new File("fate.dat"));
while(s.hasNextLine()) {
String input = s.nextLine();
Scanner r = new Scanner(input);
Hand hand = new Hand();
while(r.hasNext())
hand.add(r.next());
hand.print();
}
}
}