Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package _15_Lists;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- public class _05_ListManipulationAdvanced {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List<Integer> list = Arrays.stream(scanner.nextLine().split(" "))
- .map(Integer::parseInt).collect(Collectors.toList());
- while (true) {
- String input = scanner.nextLine();
- if (input.equals("end")) {
- break;
- }
- String[] tokens = input.split(" ");
- switch (tokens[0]) {
- case "Contains":
- int num = Integer.parseInt(tokens[1]);
- if (list.contains(num)) {
- System.out.println("Yes");
- } else {
- System.out.println("No such number");
- }
- break;
- case "Print":
- if (tokens[1].equals("even")) {
- for (int i = 0; i < list.size(); i++) {
- Integer numEven = (list.get(i));
- if (numEven % 2 == 0) {
- System.out.print(numEven + " ");
- }
- }
- System.out.println();
- break;
- } else if (tokens[1].equals("odd")) {
- for (int i = 0; i < list.size(); i++) {
- Integer numOdd = (list.get(i));
- if (numOdd % 2 == 1) {
- System.out.print(numOdd + " ");
- }
- }
- System.out.println();
- break;
- }
- case "Get":
- if (tokens[1].equals("sum")) {
- int sum = 0;
- for (int i = 0; i < list.size(); i++) {
- Integer number = list.get(i);
- sum += number;
- }
- System.out.println(sum);
- }
- break;
- case "Filter":
- for (int i = 0; i < list.size(); i++) {
- Integer numFilter = list.get(i);
- String condition = tokens[1];
- int marker = Integer.parseInt(tokens[2]);
- if (condition.equals(">")) {
- if (numFilter > marker) {
- System.out.print(numFilter + " ");
- }
- }
- if (condition.equals("<")) {
- if (numFilter < marker) {
- System.out.print(numFilter + " ");
- }
- }
- if (condition.equals(">=")) {
- if (numFilter >= marker) {
- System.out.print(numFilter + " ");
- }
- }
- if (condition.equals("<=")) {
- if (numFilter <= marker) {
- System.out.print(numFilter + " ");
- }
- }
- }
- System.out.println();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement