-
Notifications
You must be signed in to change notification settings - Fork 0
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
Unit conversions #3
Open
colbs255
wants to merge
3
commits into
main
Choose a base branch
from
unit-conversions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
= Unit Conversions | ||
|
||
== Question | ||
|
||
Given a series of unit conversions of this form: | ||
|
||
---- | ||
1 ft = 12 in | ||
1 yd = 3 ft | ||
1 cup = 8 oz | ||
1 pint = 2 cups | ||
---- | ||
|
||
Create a system that can convert qty of one unit to another unit. For example: | ||
|
||
`2 yd = ___ in` would return 72 | ||
|
||
== Explanation | ||
|
||
Let's go through the example in the problem. | ||
We can convert `2 yd` to `ft` by multiplying the quantity 2 by 3 to get `6 ft`. | ||
Similarly, we can convert `6 ft` to `in` by multiplying quantity 6 by 12 to get `72 in`. | ||
If we know the intermediate units to convert our quantity to, getting our answer just requires some multiplication. | ||
So how do we determine the intermediate units? | ||
We have to search for a path between our start unit and our end unit. | ||
Graph searching algorithms are good options for tasks like this. | ||
Let each unit be a node in the graph and an edge between node `u` and node `v` means `u` can be converted to `v`. | ||
The edges also have our scalar values for conversions. | ||
Here's a graph for our example problem: | ||
|
||
TODO | ||
|
||
Now what should our graph search algorithm be? | ||
Both DFS and BFS can work here, but we should discuss the trade-offs. | ||
BFS is guaranteed to find the shortest path (i.e., fewest node conversions), | ||
Fewer conversions means less floating point multiplication, so let's use that. | ||
So our solution is now: | ||
|
||
* Convert our units to a conversion graph | ||
** Our graph is being directed, each edge can be reversed by dividing by the scalar rather than multiplying. | ||
* Use BFS to find a path between the starting unit and the ending unit | ||
** If no path exists, we cannot convert between these units and should notify the client somehow (throw an exception) | ||
* When constructing the path, keep track of the scalars so we can multiply the initial quantity for our answer. | ||
|
||
|
||
== Solution | ||
|
||
.Conversions.java | ||
[source,java] | ||
---- | ||
include::src/Conversions.java[] | ||
---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import java.util.*; | ||
|
||
class Conversions { | ||
|
||
private final Map<String, List<Edge>> unitToConversions; | ||
|
||
public Conversions() { | ||
this.unitToConversions = new HashMap<>(); | ||
} | ||
|
||
public void addConversion(String from, String to, double scalar) { | ||
unitToConversions.computeIfAbsent(from, k -> new ArrayList<>()).add(new Edge(to, scalar)); | ||
unitToConversions.computeIfAbsent(to, k -> new ArrayList<>()).add(new Edge(from, 1 / scalar)); | ||
} | ||
|
||
public double convert(ConversionQuery query) { | ||
var q = new ArrayDeque<Edge>(); | ||
var visited = new HashSet<String>(); | ||
q.add(new Edge(query.from, query.qty)); | ||
|
||
while (!q.isEmpty()) { | ||
var next = q.poll(); | ||
if (next.unit.equals(query.to)) { | ||
return next.scalar; | ||
} | ||
if (visited.contains(next.unit)) { | ||
continue; | ||
} | ||
for (var edge: unitToConversions.getOrDefault(next.unit, Collections.emptyList())) { | ||
q.offer(new Edge(edge.unit, next.scalar * edge.scalar)); | ||
} | ||
visited.add(next.unit); | ||
} | ||
|
||
throw new IllegalArgumentException("Impossible to convert: " + query); | ||
} | ||
|
||
private record ConversionQuery(String from, String to, double qty) { | ||
} | ||
|
||
private record Edge(String unit, double scalar) { | ||
} | ||
|
||
public static void main(String[] args) { | ||
var conversions = new Conversions(); | ||
|
||
conversions.addConversion("ft", "in", 12.0); | ||
conversions.addConversion("yd", "ft", 3.0); | ||
conversions.addConversion("cup", "oz", 8.0); | ||
conversions.addConversion("pint", "cup", 12.0); | ||
|
||
System.out.println("72.0 == " + conversions.convert(new ConversionQuery("yd", "in", 2))); | ||
System.out.println("2.0 == " + conversions.convert(new ConversionQuery("in", "yd", 72))); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.