-
Notifications
You must be signed in to change notification settings - Fork 5
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
Challenges completed #5
Open
Andre6-dev
wants to merge
17
commits into
ravnhq:master
Choose a base branch
from
Andre6-dev:develop
base: master
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 all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
90a3361
Solving the first and second challenge.
Andre6-dev 9fe8f8f
Implementing ownPower and digitSum
Andre6-dev 7d5f33a
Fixing the method of ownPower
Andre6-dev c1f587f
Encrypt and Decrypt challenges done
Andre6-dev bbf6785
Solving the challenges from ChallengeStream
Andre6-dev 338d7e2
Fixing the methods for calculateCost
Andre6-dev 15b9454
Fixing the tests for the winning hand challenge
Andre6-dev 8a8f402
Fixing the winningHand challenge with the latests tests
Andre6-dev 6a08914
Adding the extra challenge
Andre6-dev 4ebfca2
Adding the solution using ConcurrentHashMap and process individually
Andre6-dev be1d084
WeatherAnalyzerOption2 is my solution for extra challenge
Andre6-dev dcda702
Remove weather.json from tracking and add to .gitignore
Andre6-dev 306b242
Adding weather.json to gitignore
Andre6-dev da4d0ea
Simplifying the method for calculating the cost per type
Andre6-dev afcc67e
Fixing the observations of extraChallenge
Andre6-dev f9c75a0
Deleting comments
Andre6-dev 86916e7
Fixing the lasts observations made in my PR
Andre6-dev 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 |
---|---|---|
|
@@ -36,4 +36,7 @@ build/ | |
.vscode/ | ||
|
||
### Mac OS ### | ||
.DS_Store | ||
.DS_Store | ||
|
||
# Json file | ||
src/main/resources/weather.json |
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 |
---|---|---|
@@ -1,28 +1,24 @@ | ||
/* (C)2024 */ | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import mocks.CallCostObject; | ||
import mocks.CallSummary; | ||
import mocks.CardWinner; | ||
import mocks.TotalSummary; | ||
|
||
public class ChallengeStream { | ||
|
||
/** | ||
* One stack containing five numbered cards from 0-9 are given to both players. Calculate which hand has winning number. | ||
* The winning number is calculated by which hard produces the highest two-digit number. | ||
* | ||
* calculateWinningHand([2, 5, 2, 6, 9], [3, 7, 3, 1, 2]) ➞ true | ||
* P1 can make the number 96 | ||
* P2 can make the number 73 | ||
* P1 win the round since 96 > 73 | ||
* | ||
* The function must return which player hand is the winner and the two-digit number produced. The solution must contain streams. | ||
* | ||
* @param player1 hand, player2 hand | ||
*/ | ||
public CardWinner calculateWinningHand(List<Integer> player1, List<Integer> player2) { | ||
// YOUR CODE HERE... | ||
return new CardWinner(); | ||
} | ||
private static final double INTERNATIONAL_FIRST_3_MIN = 7.56; | ||
private static final double INTERNATIONAL_ADDITIONAL_MIN = 3.03; | ||
private static final double NATIONAL_FIRST_3_MIN = 1.20; | ||
private static final double NATIONAL_ADDITIONAL_MIN = 0.48; | ||
private static final double LOCAL_PER_MIN = 0.2; | ||
private static final int BASE_MINUTES = 3; | ||
|
||
private static final String INTERNATIONAL = "International"; | ||
private static final String NATIONAL = "National"; | ||
private static final String LOCAL = "Local"; | ||
|
||
/** | ||
* Design a solution to calculate what to pay for a set of phone calls. The function must receive an | ||
|
@@ -43,7 +39,91 @@ public CardWinner calculateWinningHand(List<Integer> player1, List<Integer> play | |
* @returns {CallsResponse} - Processed information | ||
*/ | ||
public TotalSummary calculateCost(List<CallCostObject> costObjectList) { | ||
// YOUR CODE HERE... | ||
return new TotalSummary(); | ||
List<CallSummary> callSummaries = costObjectList.stream() | ||
.map(call -> new CallSummary(call, calculateCallCost(call))) | ||
.toList(); | ||
|
||
// get the total cost of all calls | ||
double totalCost = callSummaries.stream() | ||
.mapToDouble(CallSummary::getTotalCost) | ||
.sum(); | ||
|
||
// Validate the type of the call to get the size if it is not National, International or Local dont count it | ||
long totalCalls = callSummaries.stream() | ||
.filter(call -> call.getCallCostObject().getType().equals("National") || | ||
call.getCallCostObject().getType().equals("International") || | ||
call.getCallCostObject().getType().equals("Local")) | ||
.count(); | ||
|
||
return new TotalSummary( | ||
callSummaries, | ||
(int) totalCalls, | ||
totalCost); | ||
} | ||
|
||
private Double calculateCallCost(CallCostObject call) { | ||
double first3MinRate; | ||
double additionalMinRate; | ||
int duration = call.getDuration(); | ||
|
||
switch (call.getType()) { | ||
case INTERNATIONAL -> { | ||
first3MinRate = INTERNATIONAL_FIRST_3_MIN; | ||
additionalMinRate = INTERNATIONAL_ADDITIONAL_MIN; | ||
} | ||
case NATIONAL -> { | ||
first3MinRate = NATIONAL_FIRST_3_MIN; | ||
additionalMinRate = NATIONAL_ADDITIONAL_MIN; | ||
} | ||
case LOCAL -> { | ||
return duration * LOCAL_PER_MIN; | ||
} | ||
default -> { | ||
return 0.0; | ||
} | ||
} | ||
|
||
if (duration <= BASE_MINUTES) { | ||
return first3MinRate * duration; | ||
} else { | ||
return (additionalMinRate * (duration - BASE_MINUTES)) + (BASE_MINUTES * first3MinRate); | ||
} | ||
} | ||
|
||
/** | ||
* One stack containing five numbered cards from 0-9 are given to both players. Calculate which hand has winning number. | ||
* The winning number is calculated by which hard produces the highest two-digit number. | ||
* | ||
* calculateWinningHand([2, 5, 2, 6, 9], [3, 7, 3, 1, 2]) ➞ true | ||
* P1 can make the number 96 | ||
* P2 can make the number 73 | ||
* P1 win the round since 96 > 73 | ||
* | ||
* The function must return which player hand is the winner and the two-digit number produced. The solution must contain streams. | ||
* | ||
* @param player1 hand, player2 hand | ||
*/ | ||
public CardWinner calculateWinningHand(List<Integer> player1, List<Integer> player2) { | ||
|
||
int player1Max = player1.stream() | ||
// create a stream of all possible two-digit numbers | ||
.flatMap(d1 -> player1.stream() | ||
.filter(d2 -> !d2.equals(d1)) // ensure that the two digits are different | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Digits don't need to be different, a player could win with 11,22,...,99 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What Sebastian said, please send a fix |
||
.map(d2 -> d1 * 10 + d2)) // combine the two digits to form a two-digit number | ||
.max(Integer::compareTo)// get the highest two-digit number | ||
.orElse(0); | ||
|
||
int player2Max = player2.stream() | ||
.flatMap(d1 -> player2.stream() | ||
.filter(d2 -> !d2.equals(d1)) | ||
.map(d2 -> d1 * 10 + d2)) | ||
.max(Integer::compareTo) | ||
.orElse(0); | ||
|
||
// return the winner and the winning number and if bot players have the same number return TIE | ||
return new CardWinner(player1Max > player2Max | ||
? "P1" : player1Max == player2Max | ||
? "TIE" : "P2", | ||
Math.max(player1Max, player2Max)); | ||
} | ||
} |
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,30 @@ | ||
package extraChallenge; | ||
|
||
public class Location { | ||
|
||
private double lon; | ||
private double lat; | ||
|
||
public Location() {} | ||
|
||
public Location(double lon, double lat) { | ||
this.lon = lon; | ||
this.lat = lat; | ||
} | ||
|
||
public double getLon() { | ||
return lon; | ||
} | ||
|
||
public void setLon(double lon) { | ||
this.lon = lon; | ||
} | ||
|
||
public double getLat() { | ||
return lat; | ||
} | ||
|
||
public void setLat(double lat) { | ||
this.lat = lat; | ||
} | ||
} |
Oops, something went wrong.
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.
nit: Code smell here, part of the switch doesn't return and part of it returns, this divergent behavior and could be confusing when reading the code