Commit c5ef2375 authored by Aleksandar Stanchev's avatar Aleksandar Stanchev
Browse files

Done

parent fdf65ae4
Showing with 89 additions and 0 deletions
+89 -0
Task1.java 0 → 100644
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
public class Task1 {
public static void main(String[] args) {
//=============input reading starts here==============
List<Integer> inputList = new ArrayList<>();
Integer divider = Integer.parseInt(args[args.length - 1]);
for (int i = 0; i < args.length - 1; i++) {
inputList.add(Integer.parseInt(args[i]));
}
//==============input reading ends here================
List<Integer> output = task1(inputList, divider); ///////task1 method is executed here
System.out.println(output);
}
public static List<Integer> task1(List<Integer> inputList, Integer divider) {
/* this also works
//reverse the list
Collections.reverse(inputList);
// then filter the numbers whose remainder is 0
List<Integer> listToReturn = inputList.stream()
.filter( (n) -> n % divider == 0 )
.collect(Collectors.toList());
*/
List<Integer> listToReturn = new LinkedList<>();
inputList.stream()
.collect(Collectors.toCollection(LinkedList::new)) //transform the collection to a Linked List
.descendingIterator() // iterate through the elements in reversed order
.forEachRemaining( // for each integer in the collection
integer -> { // if the remainder is 0, add it to a new list
if (integer % divider == 0) listToReturn.add(integer);
}
);
return listToReturn;
}
}
\ No newline at end of file
Task2.java 0 → 100644
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Task2 {
public static void main(String[] args) {
//=============input reading starts here==============
Integer lengthLimit = Integer.parseInt(args[args.length-1]);
List<String> inputList = new ArrayList<>(Arrays.asList(args).subList(0, args.length - 1));
//==============input reading ends here================
List<String> output = task2(inputList,lengthLimit);
System.out.println(output); ///////task2 method is executed here
}
public static List<String> task2 (List<String> inputList, Integer lengthLimit ) {
List<String> listToReturn = inputList.stream()
.filter( // filter out all string with length larger than the requested limit
(stringArg) -> stringArg.length()<=lengthLimit
).toList();
return listToReturn;
}
}
Task3.java 0 → 100644
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.IntStream;
public class Task3 {
public static void main(String[] args) {
String output = task3(Integer.parseInt(args[0]));
System.out.println(output); ///////task3 method is executed here
}
private static String task3(int number) {
AtomicBoolean isPrime = new AtomicBoolean(true);
//just in case someone is trying to check numbers less than or equal to one
if ( number <= 1 ) {
isPrime.set(false);
} else {
IntStream.range(2, number/2) // for all numbers between 2 and number/2
.forEach((divider) -> { // if the remainder is 0, then our number is not prime
if (number % divider == 0) isPrime.set(false);
});
}
return "Is " + number + " a prime number? - " + isPrime.get();
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment