-
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
Java-Nerdery-ChallengeV2 - Extra-challenge - Luis Moroco #4
Open
luismoroco
wants to merge
16
commits into
ravnhq:master
Choose a base branch
from
luismoroco:extra-challenge
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
16 commits
Select commit
Hold shift + click to select a range
74ea96f
feat: complete readableTime function
luismoroco f7530a9
feat: complete circularArray function
luismoroco a054beb
feat: complete ownPower function
luismoroco ae739e0
feat: complete digitSum function
luismoroco 7eaf057
feat: complete decrypt function
luismoroco 5c32161
feat: complete encrypt function
luismoroco b3287b7
feat: complete calculateWinningHand function
luismoroco 185f59d
feat: complete calculateCost function
luismoroco 9a97f8e
feat: complete extra challenge weatherStations function
luismoroco 4598a14
refactor: remove enums. Remove unnecessary abstract classes. Remove u…
luismoroco 0af948a
feat: add ZonedDateTime for get day of the week in extra challenge
luismoroco 13b926f
chore: add data file to gitignore
luismoroco bd541fa
refactor: improve Variable.properties initialization using EXTREMES v…
luismoroco 1b08c4e
refactor: use Objects.isnull instead of direct comparation
luismoroco 085d8a3
feat: add Variable.getAverage function for avoid undetermined divisio…
luismoroco 577a65a
refactor: remove AggregatedMeteorologyReport class. Move processing l…
luismoroco 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,8 @@ build/ | |
.vscode/ | ||
|
||
### Mac OS ### | ||
.DS_Store | ||
.DS_Store | ||
|
||
### Files | ||
|
||
*.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
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,4 +1,6 @@ | ||
/* (C)2024 */ | ||
import java.math.BigInteger; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
|
@@ -21,7 +23,27 @@ public class Challenges { | |
|
||
public String readableTime(Integer seconds) { | ||
// YOUR CODE HERE... | ||
return ""; | ||
// Given that Integer in signed, we must verify if that is positive | ||
if (seconds < 0) { | ||
return ""; | ||
} | ||
|
||
final int SECOND = 1; | ||
final int MINUTE = 60 * SECOND; | ||
final int HOUR = 60 * MINUTE; | ||
|
||
List<String> formattedTime = new ArrayList<>(3); | ||
for (int unit : List.of(HOUR, MINUTE, SECOND)) { | ||
String value = String.format("%0" + 2 + "d", seconds / unit); | ||
formattedTime.add(value); | ||
|
||
seconds = seconds % unit; | ||
|
||
// The integer division is used to obtain the value in each unit and the | ||
// modulus is used to obtain the remainders after each iteration. | ||
} | ||
|
||
return String.join(":", formattedTime); | ||
} | ||
; | ||
|
||
|
@@ -45,7 +67,31 @@ public String readableTime(Integer seconds) { | |
public String[] circularArray(int index) { | ||
String[] COUNTRY_NAMES = {"Germany", "Norway", "Island", "Japan", "Israel"}; | ||
// YOUR CODE HERE... | ||
return COUNTRY_NAMES; | ||
// Given that Integer in signed, we must verify if that is positive | ||
if (index < 0) { | ||
return COUNTRY_NAMES; | ||
} | ||
|
||
// Use the module to avoid out-of-range indexes | ||
if (index > COUNTRY_NAMES.length) { | ||
index %= COUNTRY_NAMES.length; | ||
} | ||
|
||
String[] shiftedCountryNames = new String[COUNTRY_NAMES.length]; | ||
for (int idx = 0; idx < COUNTRY_NAMES.length; ++idx) { | ||
shiftedCountryNames[idx] = COUNTRY_NAMES[(index + idx) % COUNTRY_NAMES.length]; | ||
} | ||
|
||
/* | ||
- - 'n' is COUNTRY_NAMES' length | ||
Given an array A of n items where [0][1][2]...[n-1], and other one B of same size | ||
We use the modulus for copy them as follows: | ||
- Given i, where i in [0, 1, 2, ... n - 1] | ||
- We use it for populate B: | ||
- for instance: B[i] = A[(i + index) % n] // in order to iterate over all array indexes | ||
*/ | ||
|
||
return shiftedCountryNames; | ||
} | ||
; | ||
|
||
|
@@ -71,7 +117,25 @@ public String[] circularArray(int index) { | |
|
||
public String ownPower(int number, int lastDigits) { | ||
// YOUR CODE HERE... | ||
return ""; | ||
BigInteger sum = BigInteger.ZERO; | ||
for (int i = 1; i <= number; ++i) { | ||
// We get i^i | ||
BigInteger ownPower = BigInteger.ONE; | ||
for (int j = 1; j <= i; ++j) { | ||
ownPower = ownPower.multiply(BigInteger.valueOf(i)); | ||
} | ||
|
||
Comment on lines
+123
to
+127
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. Check |
||
// We add it to the accumulator | ||
sum = sum.add(ownPower); | ||
} | ||
|
||
String sumDigits = sum.toString(); | ||
// Verify if is greater than lastDigits in order to get a substring | ||
if (sumDigits.length() >= lastDigits) { | ||
return sumDigits.substring(sumDigits.length() - lastDigits); | ||
} | ||
|
||
return sumDigits; | ||
} | ||
; | ||
|
||
|
@@ -94,7 +158,21 @@ A factorial (x!) means x! * (x - 1)... * 3 * 2 * 1. | |
|
||
public Integer digitSum(int n) { | ||
// YOUR CODE HERE... | ||
return 1; | ||
// Declare a accumulator variable | ||
BigInteger factorial = BigInteger.ONE; | ||
for (int number = 1; number <= n; ++number) { | ||
// perform factorial | ||
factorial = factorial.multiply(BigInteger.valueOf(number)); | ||
} | ||
|
||
int sum = 0; | ||
String factorialDigits = factorial.toString(); | ||
// Parse chars to Integers and sum them | ||
for (int index = 0; index < factorialDigits.length(); ++index) { | ||
sum += Character.getNumericValue(factorialDigits.charAt(index)); | ||
} | ||
|
||
return sum; | ||
} | ||
|
||
/** | ||
|
@@ -108,7 +186,25 @@ public Integer digitSum(int n) { | |
*/ | ||
public String decrypt(List<Integer> ascivalues) { | ||
// YOUR CODE HERE... | ||
return ""; | ||
// If the list is empty, return an empty string | ||
if (ascivalues.size() == 0) { | ||
return ""; | ||
} | ||
|
||
// Create a local list in order to avoid modifications (references) | ||
List<Integer> cypheredText = new ArrayList<>(ascivalues); | ||
|
||
StringBuilder plainText = new StringBuilder(); | ||
// Because the first value is always the same, the following is added directly | ||
plainText.append((char) cypheredText.get(0).intValue()); | ||
for (int index = 1; index < cypheredText.size(); ++index) { | ||
// Set the cypheredText[index] for accumulate values | ||
cypheredText.set(index, cypheredText.get(index) + cypheredText.get(index - 1)); | ||
// Cast the values and add them to out string builder | ||
plainText.append((char) cypheredText.get(index).intValue()); | ||
} | ||
|
||
return plainText.toString(); | ||
} | ||
|
||
/** | ||
|
@@ -122,6 +218,21 @@ public String decrypt(List<Integer> ascivalues) { | |
*/ | ||
public List<Integer> encrypt(String text) { | ||
// YOUR CODE HERE... | ||
return Collections.emptyList(); | ||
// If the text is empty, return an empty list | ||
if (text.length() == 0) { | ||
return Collections.emptyList(); | ||
} | ||
|
||
// Declare a list for store the cyphered text | ||
List<Integer> cypheredText = new ArrayList<>(text.length()); | ||
|
||
// Because the first value is always the same, the following is added directly | ||
cypheredText.add((int) text.charAt(0)); | ||
// Iterate over the text and store the differences of text[index] - text[index - 1] | ||
for (int index = 1; index < text.length(); ++index) { | ||
cypheredText.add(text.charAt(index) - text.charAt(index - 1)); | ||
} | ||
|
||
return cypheredText; | ||
} | ||
} |
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,38 @@ | ||
import com.fasterxml.jackson.core.JsonFactory; | ||
import com.fasterxml.jackson.core.JsonToken; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import meteorology.model.Weather; | ||
import meteorology.MeteorologyReport; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
public class ExtraChallenges { | ||
final static String FILE_PATH = "weather.json"; | ||
|
||
public static void main(String[] args) { | ||
var FILE = new File(FILE_PATH); | ||
var factory = new JsonFactory(); | ||
var mapper = new ObjectMapper(factory); | ||
|
||
var meteorologyReport = new MeteorologyReport(); | ||
|
||
try { | ||
var parser = factory.createParser(FILE); | ||
if (parser.nextToken() == JsonToken.START_ARRAY) { | ||
while (parser.nextToken() != JsonToken.END_ARRAY) { | ||
var weather = mapper.readValue(parser, Weather.class); | ||
if (!weather.verifyFields()) { | ||
continue; | ||
} | ||
|
||
meteorologyReport.process(weather); | ||
} | ||
} | ||
|
||
meteorologyReport.show(); | ||
} catch (IOException | IllegalAccessException | NoSuchFieldException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
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,38 @@ | ||
package meteorology; | ||
|
||
import meteorology.model.Report; | ||
import meteorology.model.Weather; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
public class MeteorologyReport { | ||
protected Map<String, Report> dailyReports = new HashMap<>(); | ||
protected Report overview = new Report(); | ||
|
||
public void process(Weather weather) throws NoSuchFieldException, IllegalAccessException { | ||
this.overview.process(weather); | ||
|
||
var report = this.dailyReports.get(weather.getDate()); | ||
if (Objects.isNull(report)) { | ||
report = new Report(); | ||
report.process(weather); | ||
|
||
this.dailyReports.put(weather.getDate(), report); | ||
return; | ||
} | ||
|
||
report.process(weather); | ||
} | ||
|
||
public void show() { | ||
for (var report : this.dailyReports.entrySet()) { | ||
System.out.println(report.getKey()); | ||
report.getValue().show(); | ||
} | ||
|
||
System.out.println("OVERVIEW"); | ||
this.overview.show(); | ||
} | ||
} |
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,9 @@ | ||
package meteorology.model; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
|
||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public class Location { | ||
public Double lon; | ||
public Double 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.
Nice