Skip to content
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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open

first #1207

Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/main/java/practice/CandidateValidator.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
package practice;

public class CandidateValidator {
//write your code here
import java.util.function.Predicate;
import model.Candidate;

public class CandidateValidator implements Predicate<Candidate> {
private static final String nationality = "Ukrainian";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's check naming convention for constants

private static final int partOne = 0;
private static final int partTwo = 1;
private static final int toAge = 35;
private static final int durationInUkr = 10;

@Override
public boolean test(Candidate candidate) {
String[] splitted = candidate.getPeriodsInUkr().split("-");

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 prevent NumberFormatException if the input string is not a valid integer. You can use a try-catch block to handle this exception.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that candidate.getPeriodsInUkr() returns a string in the expected format (e.g., 'YYYY-YYYY'). If the format is not guaranteed, consider adding validation or error handling to avoid runtime exceptions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the separator also should be constant

int inted = Integer.parseInt(splitted[partTwo]) - Integer.parseInt(splitted[partOne]);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is it inted?

if (candidate.isAllowedToVote() && candidate.getAge() >= toAge
&& candidate.getNationality().equals(nationality)
&& inted >= durationInUkr) {
return true;
}
return false;
}
}
47 changes: 40 additions & 7 deletions src/main/java/practice/StreamPractice.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package practice;

import java.util.Collections;
import java.util.Arrays;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.stream.IntStream;
import model.Candidate;
import model.Person;

Expand All @@ -14,7 +16,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))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's combine map and flatMap

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.map(s -> s.split(","))
.flatMap(strings -> Arrays.stream(strings))
.flatMap(input -> Arrays.stream(input.split(",")))

.map(s -> Integer.parseInt(s))

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 prevent potential NumberFormatException if the input strings are not valid integers.

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 prevent NumberFormatException if the input string is not a valid integer. You can use a try-catch block to handle this exception.

.filter(s -> s % 2 == 0)
.min((s,b) -> s - b)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.min((s,b) -> s - b)
.min(Integer::compareTo)

.orElseThrow(() ->
new RuntimeException("Can't get min value from list: "
+ "< Here is our input 'numbers' >"));
Comment on lines +26 to +27

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message in the RuntimeException should include the actual input list numbers to provide more context when the exception is thrown.

}

/**
Expand All @@ -23,7 +33,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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable sum is declared but not used in the method. It can be removed to clean up the code.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable sum is declared but not used in the getOddNumsAverage method. Consider removing it to clean up the code.

Copy link
Contributor

Choose a reason for hiding this comment

The 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"));
}

/**
Expand All @@ -35,7 +50,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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename s

check other methods

&& s.getSex() == Person.Sex.MAN)
.toList();
}

/**
Expand All @@ -50,7 +68,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))
.toList();
}

/**
Expand All @@ -59,7 +82,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())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

combine map and flatMap

.map(s -> s.getName())
.toList();
}

/**
Expand All @@ -75,6 +104,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())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename s

.sorted()
.toList();
}
}
Loading