napredno
unknown
java
3 years ago
6.1 kB
14
Indexable
import java.util.Scanner;
import java.util.*;
import java.util.stream.Collectors;
class IntegerList {
private List<Integer> integerList;
public IntegerList() {
this.integerList = new LinkedList<Integer>();
}
public IntegerList(Integer ... numbers) {
this.integerList = Arrays.asList(numbers);
}
public void add(int el, int idx) {
try {
this.integerList.add(idx, el);
}
catch (IndexOutOfBoundsException e) {
while(integerList.size() != idx)
integerList.add(integerList.size(), 0);
integerList.add(idx, el);
}
}
public int remove(int idx) {
if(idx < 0 || idx >= integerList.size())
throw new ArrayIndexOutOfBoundsException();
return integerList.remove(idx);
}
public void set(int el, int idx) {
if(idx < 0 || idx >= integerList.size())
throw new ArrayIndexOutOfBoundsException();
integerList.set(idx, el);
}
public int get(int idx) {
if(idx < 0 || idx >= integerList.size())
throw new ArrayIndexOutOfBoundsException();
return integerList.get(idx);
}
public int size() {
return integerList.size();
}
public int count(int el) {
return (int) integerList.stream().filter(i -> i == el).count();
}
public void removeDuplicates() {
Collections.reverse(integerList);
integerList = integerList.stream().distinct().collect(Collectors.toList());
Collections.reverse(integerList);
}
public int sumFirst(int k) {
if(k < 0)
throw new ArrayIndexOutOfBoundsException();
return integerList.stream().limit(Math.min(k, integerList.size())).mapToInt(i -> i.intValue()).sum();
}
public int sumLast(int k) {
if(k < 0)
throw new ArrayIndexOutOfBoundsException();
Collections.reverse(integerList);
int sum = integerList.stream().limit(Math.min(k, integerList.size())).mapToInt(i -> i.intValue()).sum();
Collections.reverse(integerList);
return sum;
}
public void shiftRight(int idx, int count) {
if(idx < 0 || idx >= size())
throw new ArrayIndexOutOfBoundsException();
int newIndexOfElement = (idx + count) % integerList.size();
int elementHolder = remove(idx);
integerList.add(newIndexOfElement, elementHolder);
}
public void shiftLeft(int idx, int count) {
if(idx < 0 || idx >= size())
throw new ArrayIndexOutOfBoundsException();
int newIndexOfElement = (idx - count) % integerList.size();
if (newIndexOfElement < 0) {
newIndexOfElement = integerList.size() + newIndexOfElement;
}
int elementHolder = remove(idx);
integerList.add(newIndexOfElement, elementHolder);
}
@Override
public String toString() {
Iterator<Integer> iterator = integerList.listIterator();
StringBuilder sb = new StringBuilder();
while(iterator.hasNext()) {
sb.append(iterator.next() + " ");
}
return sb.toString();
}
public IntegerList addValue(int value) {
Integer[] array = integerList.toArray(new Integer[0]);
for(int i = 0; i < array.length; i++) {
array[i] += value;
}
IntegerList newList = new IntegerList(array);
return newList;
}
}
public class IntegerListTest {
public static void main(String[] args) {
Scanner jin = new Scanner(System.in);
int k = jin.nextInt();
if ( k == 0 ) { //test standard methods
int subtest = jin.nextInt();
if ( subtest == 0 ) {
IntegerList list = new IntegerList();
while ( true ) {
int num = jin.nextInt();
if ( num == 0 ) {
list.add(jin.nextInt(), jin.nextInt());
}
if ( num == 1 ) {
list.remove(jin.nextInt());
}
if ( num == 2 ) {
print(list);
}
if ( num == 3 ) {
break;
}
}
}
if ( subtest == 1 ) {
int n = jin.nextInt();
Integer a[] = new Integer[n];
for ( int i = 0 ; i < n ; ++i ) {
a[i] = jin.nextInt();
}
IntegerList list = new IntegerList(a);
print(list);
}
}
if ( k == 1 ) { //test count,remove duplicates, addValue
int n = jin.nextInt();
Integer a[] = new Integer[n];
for ( int i = 0 ; i < n ; ++i ) {
a[i] = jin.nextInt();
}
IntegerList list = new IntegerList(a);
while ( true ) {
int num = jin.nextInt();
if ( num == 0 ) { //count
System.out.println(list.count(jin.nextInt()));
}
if ( num == 1 ) {
list.removeDuplicates();
}
if ( num == 2 ) {
print(list.addValue(jin.nextInt()));
}
if ( num == 3 ) {
list.add(jin.nextInt(), jin.nextInt());
}
if ( num == 4 ) {
print(list);
}
if ( num == 5 ) {
break;
}
}
}
if ( k == 2 ) { //test shiftRight, shiftLeft, sumFirst , sumLast
int n = jin.nextInt();
Integer a[] = new Integer[n];
for ( int i = 0 ; i < n ; ++i ) {
a[i] = jin.nextInt();
}
IntegerList list = new IntegerList(a);
while ( true ) {
int num = jin.nextInt();
if ( num == 0 ) { //count
list.shiftLeft(jin.nextInt(), jin.nextInt());
}
if ( num == 1 ) {
list.shiftRight(jin.nextInt(), jin.nextInt());
}
if ( num == 2 ) {
System.out.println(list.sumFirst(jin.nextInt()));
}
if ( num == 3 ) {
System.out.println(list.sumLast(jin.nextInt()));
}
if ( num == 4 ) {
print(list);
}
if ( num == 5 ) {
break;
}
}
}
}
public static void print(IntegerList il) {
if ( il.size() == 0 ) System.out.print("EMPTY");
for ( int i = 0 ; i < il.size() ; ++i ) {
if ( i > 0 ) System.out.print(" ");
System.out.print(il.get(i));
}
System.out.println();
}
}Editor is loading...