-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicProgramming.java
154 lines (120 loc) · 3.96 KB
/
BasicProgramming.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
public class BasicProgramming {
public static void main(String[] args) throws IOException{
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
int inputOperation = Integer.parseInt(bufferedReader.readLine().split(" ")[1]);
int[] data = Arrays.stream(bufferedReader.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
bufferedReader.close();
Operation operation = new GetSeven();
switch (inputOperation) {
case 2:
operation = new CompareFirstTwoElements();
break;
case 3:
operation = new CalculateMedianOfFirstThreeElements();
break;
case 4:
operation = new SumElements();
break;
case 5:
operation = new SumEvenElements();
break;
case 6:
operation = new ConvertElementsToString();
break;
case 7:
operation = new WalkThroughIndices();
break;
}
System.out.println(operation.execute(data));
}
}
interface Operation {
abstract public String execute(int[] data);
}
class GetSeven implements Operation {
public GetSeven() {}
@Override
public String execute(int[] data) {
return "7";
}
}
class CompareFirstTwoElements implements Operation {
public CompareFirstTwoElements() {}
@Override
public String execute(int[] data) {
if (data[0] > data[1]) {
return "Bigger";
}
if (data[0] == data[1]) {
return "Equal";
}
return "Smaller";
}
}
class CalculateMedianOfFirstThreeElements implements Operation {
public CalculateMedianOfFirstThreeElements() {}
@Override
public String execute(int[] data) {
return String.valueOf(Arrays.stream(data)
.limit(3)
.sorted()
.skip(1)
.limit(1)
.toArray()[0]);
}
}
class SumElements implements Operation {
public SumElements() {}
@Override
public String execute(int[] data) {
return String.valueOf(Arrays.stream(data)
.mapToLong(e -> e)
.reduce(0, Long::sum));
}
}
class SumEvenElements implements Operation {
public SumEvenElements() {}
@Override
public String execute(int[] data) {
return String.valueOf(Arrays.stream(data)
.filter(e -> e % 2 == 0)
.mapToLong(e -> e)
.reduce(0, Long::sum));
}
}
class ConvertElementsToString implements Operation {
public ConvertElementsToString() {}
@Override
public String execute(int[] data) {
return Arrays.stream(data).map(e -> (e % 26) + 97).mapToObj(e -> String.valueOf((char)e)).collect(Collectors.joining());
}
}
class WalkThroughIndices implements Operation {
public WalkThroughIndices() {}
@Override
public String execute(int[] data) {
int arrayLength = data.length;
Set<Integer> visitedIndices = new HashSet<Integer>();
int currentIndex = data[0];
while (true) {
if (currentIndex >= arrayLength) {
return "Out";
}
if (currentIndex == arrayLength - 1) {
return "Done";
}
if (visitedIndices.contains(currentIndex)) {
return "Cyclic";
}
visitedIndices.add(currentIndex);
currentIndex = data[currentIndex];
}
}
}