-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
first #1207
base: main
Are you sure you want to change the base?
first #1207
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,18 @@ | ||
package practice; | ||
|
||
public class CandidateValidator { | ||
//write your code here | ||
import java.util.function.Predicate; | ||
import model.Candidate; | ||
|
||
public class CandidateValidator implements Predicate<Candidate> { | ||
@Override | ||
public boolean test(Candidate candidate) { | ||
String[] splitted = candidate.getPeriodsInUkr().split("-"); | ||
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. Ensure that 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. the separator also should be constant |
||
int inted = Integer.parseInt(splitted[1]) - Integer.parseInt(splitted[0]); | ||
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. Consider adding error handling for 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. indexes to constants too |
||
if (candidate.isAllowedToVote() && candidate.getAge() >= 35 | ||
&& candidate.getNationality().equals("Ukrainian") | ||
&& inted >= 10) { | ||
return true; | ||
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. extract to constants |
||
} | ||
return false; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -1,7 +1,10 @@ | ||||||||
package practice; | ||||||||
|
||||||||
import java.util.Collections; | ||||||||
import java.util.Arrays; | ||||||||
import java.util.List; | ||||||||
import java.util.NoSuchElementException; | ||||||||
import java.util.stream.Collectors; | ||||||||
import java.util.stream.IntStream; | ||||||||
import model.Candidate; | ||||||||
import model.Person; | ||||||||
|
||||||||
|
@@ -14,7 +17,15 @@ public class StreamPractice { | |||||||
* "Can't get min value from list: < Here is our input 'numbers' >" | ||||||||
*/ | ||||||||
public int findMinEvenNumber(List<String> numbers) { | ||||||||
return 0; | ||||||||
return numbers.stream() | ||||||||
.map(s -> s.split(",")) | ||||||||
.flatMap(strings -> Arrays.stream(strings)) | ||||||||
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. let's combine map and flatMap 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.
Suggested change
|
||||||||
.map(s -> Integer.parseInt(s)) | ||||||||
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. Consider adding error handling for 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. Consider adding error handling for |
||||||||
.filter(s -> s % 2 == 0) | ||||||||
.min((s,b) -> s - b) | ||||||||
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.
Suggested change
|
||||||||
.orElseThrow(() -> | ||||||||
new RuntimeException("Can't get min value from list: " | ||||||||
+ "< Here is our input 'numbers' >")); | ||||||||
Comment on lines
+26
to
+27
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. The error message in the |
||||||||
} | ||||||||
|
||||||||
/** | ||||||||
|
@@ -23,7 +34,12 @@ public int findMinEvenNumber(List<String> numbers) { | |||||||
* But before that subtract 1 from each element on an odd position (having the odd index). | ||||||||
*/ | ||||||||
public Double getOddNumsAverage(List<Integer> numbers) { | ||||||||
return 0D; | ||||||||
Double sum = (double) 0; | ||||||||
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. The variable 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. The variable 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. remove unused variable |
||||||||
return IntStream.range(0, numbers.size()) | ||||||||
.map(i -> (i % 2 != 0) ? numbers.get(i) - 1 : numbers.get(i)) | ||||||||
.filter(n -> n % 2 != 0) | ||||||||
.average() | ||||||||
.orElseThrow(() -> new NoSuchElementException("No odd numbers found")); | ||||||||
} | ||||||||
|
||||||||
/** | ||||||||
|
@@ -35,7 +51,10 @@ public Double getOddNumsAverage(List<Integer> numbers) { | |||||||
* Example: select men who can be recruited to army (from 18 to 27 years old inclusively). | ||||||||
*/ | ||||||||
public List<Person> selectMenByAge(List<Person> peopleList, int fromAge, int toAge) { | ||||||||
return Collections.emptyList(); | ||||||||
return peopleList.stream() | ||||||||
.filter(s -> s.getAge() >= fromAge && s.getAge() <= toAge | ||||||||
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. rename s check other methods |
||||||||
&& s.getSex() == Person.Sex.MAN) | ||||||||
.collect(Collectors.toList()); | ||||||||
} | ||||||||
|
||||||||
/** | ||||||||
|
@@ -50,7 +69,12 @@ public List<Person> selectMenByAge(List<Person> peopleList, int fromAge, int toA | |||||||
*/ | ||||||||
public List<Person> getWorkablePeople(int fromAge, int femaleToAge, | ||||||||
int maleToAge, List<Person> peopleList) { | ||||||||
return Collections.emptyList(); | ||||||||
return peopleList.stream() | ||||||||
.filter(s -> (s.getSex() == Person.Sex.WOMAN && s.getAge() >= fromAge | ||||||||
&& s.getAge() <= femaleToAge) | ||||||||
|| (s.getSex() == Person.Sex.MAN && s.getAge() >= fromAge | ||||||||
&& s.getAge() <= maleToAge)) | ||||||||
.collect(Collectors.toList()); | ||||||||
} | ||||||||
|
||||||||
/** | ||||||||
|
@@ -59,7 +83,13 @@ public List<Person> getWorkablePeople(int fromAge, int femaleToAge, | |||||||
* return the names of all cats whose owners are women from `femaleAge` years old inclusively. | ||||||||
*/ | ||||||||
public List<String> getCatsNames(List<Person> peopleList, int femaleAge) { | ||||||||
return Collections.emptyList(); | ||||||||
return peopleList.stream() | ||||||||
.filter(s -> s.getAge() >= femaleAge && !s.getCats().isEmpty() | ||||||||
&& s.getSex() == Person.Sex.WOMAN) | ||||||||
.map(s -> s.getCats()) | ||||||||
.flatMap(s -> s.stream()) | ||||||||
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. combine map and flatMap |
||||||||
.map(s -> s.getName()) | ||||||||
.collect(Collectors.toList()); | ||||||||
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. not a mistake,but use method references where possible, use .toList() |
||||||||
} | ||||||||
|
||||||||
/** | ||||||||
|
@@ -75,6 +105,10 @@ public List<String> getCatsNames(List<Person> peopleList, int femaleAge) { | |||||||
* parametrized with Candidate in CandidateValidator. | ||||||||
*/ | ||||||||
public List<String> validateCandidates(List<Candidate> candidates) { | ||||||||
return Collections.emptyList(); | ||||||||
return candidates.stream() | ||||||||
.filter(new CandidateValidator()) | ||||||||
.map(s -> s.getName()) | ||||||||
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. rename s |
||||||||
.sorted() | ||||||||
.collect(Collectors.toList()); | ||||||||
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. .toList() |
||||||||
} | ||||||||
} |
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.
Consider adding error handling for
Integer.parseInt
to preventNumberFormatException
if the input string is not a valid integer. You can use a try-catch block to handle this exception.