Commit cc1e4bb8 authored by sebastian.graf's avatar sebastian.graf

Deleted all tests from the last lab

parent ee4c77b6
/*
* Filter.java -- Converts decimal integers to the byte with the same value.
*
* You can use this program as a filter for the MiniJava examples in this
* package. Simply pipe the output of the MiniJava examples to this program
* and you'll get on standard output what you should have seen in the first
* place, would MiniJava's I/O capabilities have not been so severely limited.
*
* This filter doesn't buffer its output so you can see it immediately. This
* makes the whole thing slow, of course.
*
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
public final class Filter {
public static void main(final String[] args) {
try (final BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
if (args.length != 0) {
throw new IllegalArgumentException("Too many arguments");
}
while (true) {
final String line = br.readLine();
if (line == null) {
return;
}
final int value = Integer.parseInt(line);
if ((value < 0) || (value >= 256)) {
throw new RuntimeException("Value not in range [0, 256)");
}
System.out.write(value);
System.out.flush();
}
} catch (final Exception e) {
System.err.printf("filter: error: %s%n", e.getMessage());
System.exit(1);
}
}
}
class ArrayListTest {
public static void main(String[] args) {
List list = new List();
list.init();
int i = 0;
while (i < 34) {
list.add(i);
i = i + 1;
}
System.out.println(list.numberOfItems());
System.out.println(list.actualSize());
System.out.println(list.get(5));
i = 0;
while (i < 10) {
list.remove(5);
i = i + 1;
}
System.out.println(list.numberOfItems());
System.out.println(list.actualSize());
System.out.println(list.get(5));
}
}
class List {
public int size;
public int items;
public int threshold;
public int[] list;
public void init() {
size = 0;
items = 0;
threshold = 10;
size = threshold;
items = 0;
list = new int[size];
}
public void add(int item) {
if (items < size) {
list[items] = item;
items = items + 1;
} else {
int[] array = new int[size + threshold];
int i = 0;
while (i < size) {
array[i] = list[i];
i = i + 1;
}
list = array;
size = size + threshold;
add(item);
}
}
public int get(int nr) {
if (nr < items) {
return list[nr];
}
return -1;
}
public void remove(int nr) {
int i = nr;
while (i < items - 1) {
list[i] = list[i + 1];
i = i + 1;
}
items = items - 1;
if (items <= size - threshold) {
int[] array = new int[size - threshold];
i = 0;
while (i < items) {
array[i] = list[i];
i = i + 1;
}
list = array;
size = size - threshold;
}
}
public int numberOfItems() {
return items;
}
public int actualSize() {
return size;
}
}
class BalancedParens {
public int size;
public int[] parens;
public boolean check() {
return size % 2 == 0 && sameOpenAsClose() && isBalanced();
}
public boolean sameOpenAsClose() {
int open; int close;
int i = open = close = 0;
while (i < size) {
if (parens[i] == 1) {
open = open + 1;
} else {
close = close + 1;
}
i = i + 1;
}
return open == close && open == size / 2;
}
public boolean isBalanced() {
int i = 0;
while (i >= 0 && i < size)
i = recursiveCheck(i);
return i == size;
}
/**
* @return -1 if we run out of parens to check or the position where
* the paren at pos is closed + 1
*/
public int recursiveCheck(int pos) {
if (pos + 1 >= size)
return -1;
if (parens[pos] != 1)
return -1;
pos = pos + 1;
while (pos >= 0 && parens[pos] != 0)
pos = recursiveCheck(pos);
return pos + 1;
}
public static void main(String[] args) {
BalancedParens test = new BalancedParens();
int size = 20;
test.size = size;
test.parens = new int[size];
/* Create input data */
test.parens[ 0] = 1; /* ( */
test.parens[ 1] = 1; /* (( */
test.parens[ 2] = 0; /* (() */
test.parens[ 3] = 1; /* (()( */
test.parens[ 4] = 1; /* (()(( */
test.parens[ 5] = 0; /* (()(() */
test.parens[ 6] = 0; /* (()(()) */
test.parens[ 7] = 0; /* (()(())) */
test.parens[ 8] = 1; /* (()(()))( */
test.parens[ 9] = 0; /* (()(()))() */
test.parens[10] = 1; /* (()(()))()( */
test.parens[11] = 0; /* (()(()))()() */
test.parens[12] = 1; /* (()(()))()()( */
test.parens[13] = 1; /* (()(()))()()(( */
test.parens[14] = 1; /* (()(()))()()((( */
test.parens[15] = 1; /* (()(()))()()(((( */
test.parens[15] = 0; /* (()(()))()()(((() */
test.parens[15] = 0; /* (()(()))()()(((()) */
test.parens[16] = 0; /* (()(()))()()(((())) */
test.parens[17] = 1; /* (()(()))()()(((()))( */
test.parens[18] = 0; /* (()(()))()()(((()))() */
test.parens[19] = 0; /* (()(()))()()(((()))()) */
/* Print 1 */
if (test.check())
System.out.println(1);
else
System.out.println(0);
}
}
class HitboxTest {
public static void main(String[] args) {
Position p = new Position();
p.x = 100;
p.y = 50;
Hitbox b = new Hitbox();
b.x = 160;
b.y = 90;
b.w = 30;
b.h = 20;
while (!b.contains(p)) {
p.move(2, 1);
}
System.out.println(p.x);
System.out.println(p.y);
}
}
class Position {
public int x;
public int y;
public void move(int xstep, int ystep) {
x = x + xstep;
y = y + ystep;
}
}
class Hitbox {
public int x;
public int y;
public int w;
public int h;
public boolean contains(Position p) {
return p.x >= x && p.x <= x + w && p.y >= y && p.y <= y + h;
}
}
class ParseInt {
public static void main(String[] args) {
int[] places = new int[9];
int i = 0;
/* Generate input data:
[2, 4, 6, 1, 3, 5, 0, 2, 4] */
while (i < 9) {
places[i] = (9 * (i + 1)) % 7;
i = i + 1;
}
/* Do the actual parsing */
i = 0;
int res = 0;
int pow = 1;
while (i < 9) {
res = res + places[8 - i] * pow;
pow = pow * 10;
i = i + 1;
}
/* 246135024 */
System.out.println(res);
}
}
class Img {
public int[][] data;
public int size; /* Set by init */
public int prefix; /* Set by init */
public int blank; /* Set by init */
public void init(int blank) {
size = 9;
data = new int[size][];
prefix = 1;
this.blank = blank;
int i = 0;
while (i < size) {
/* This is not necessary in minijava, but in java it is: */
data[i] = new int[size];
prefix = prefix * 10;
i = i + 1;
}
clear();
}
public void drawVerticalLine(int color, int x, int y1, int y2) {
if (y1 > y2) {
int s = y1;
y1 = y2;
y2 = s;
}
int i = y1;
while (i <= y2) {
data[i][x] = color;
i = i + 1;
}
}
public void drawHorizontalLine(int color, int x1, int x2, int y) {
if (x1 > x2) {
int s = x1;
x1 = x2;
x2 = s;
}
int i = x1;
while (i <= x2) {
data[y][i] = color;
i = i + 1;
}
}
public void drawLine(int color, int x1, int y1, int x2, int y2) {
if (x1 == x2) {
drawVerticalLine(color, x1, y1, y2);
return;
}
if (y1 == y2) {
drawHorizontalLine(color, x1, x2, y1);
return;
}
if (x1 > x2) {
int s = x1;
x1 = x2;
x2 = s;
s = y1;
y1 = y2;
y2 = s;
}
int dx = x2 - x1 + 1;
int dy = y2 - y1;
int dir = 1;
if (dy < 0) {
dy = dy * -1;
dir = -1;
}
dy = dy + 1;
int x = x1;
int y = y1;
if (dx >= dy) {
int ppl = dx / dy;
int rest = dx % dy;
while (y != y2 + dir) {
int thisline = ppl;
if (rest > 0) {
rest = rest - 1;
thisline = thisline + 1;
}
drawHorizontalLine(color, x, x + thisline - 1, y);
x = x + thisline;
y = y + dir;
}
} else {
int ppr = dy / dx;
int rest = dy % dx;
while (y != y2 + dir) {
int thisrow = ppr;
if (rest > 0) {
rest = rest - 1;
thisrow = thisrow + 1;
}
drawVerticalLine(color, x, y, y + thisrow - 1);
x = x + 1;
y = y + dir * thisrow;
}
}
}
public void clear() {
int i = 0;
while (i < size) {
int j = 0;
while (j < size) {
data [i][j] = blank;
j = j + 1;
}
i = i + 1;
}
}
public int convertLine(int l) {
int i = size - 1;
int pow = 1;
int res = 0;
while (i >= 0) {
res = res + data[l][i] * pow;
pow = pow * 10;
i = i - 1;
}
return res + prefix;
}
public void print() {
int i = 0;
while (i < size) {
System.out.println(convertLine(i));
i = i + 1;
}
}
}
class Alphabet {
public int color;
public void H(Img img) {
img.drawLine(color, 0, 0, 0, img.size - 1);
img.drawLine(color, img.size - 1, 0, img.size - 1, img.size - 1);
img.drawLine(color, 0, img.size / 2, img.size - 1, img.size / 2);
}
public void E(Img img) {
img.drawLine(color, 0, 0, 0, img.size - 1);
img.drawLine(color, 0, 0, img.size - 1, 0);
img.drawLine(color, 0, img.size / 2, img.size - 1, img.size / 2);
img.drawLine(color, 0, img.size -1, img.size - 1, img.size -1);
}
public void L(Img img) {
img.drawLine(color, 0, 0, 0, img.size - 1);
img.drawLine(color, 0, img.size -1, img.size - 1, img.size -1);
}
public void O(Img img) {
img.drawLine(color, 0, img.size / 2, img.size / 2, 0);
img.drawLine(color, img.size / 2, 0, img.size - 1, img.size / 2);
img.drawLine(color, 0, img.size / 2, img.size / 2, img.size - 1);
img.drawLine(color, img.size / 2, img.size - 1, img.size - 1, img.size / 2);
}
public void W(Img img) {
img.drawLine(color, 0, 0, 0, img.size - 1);
img.drawLine(color, img.size - 1, 0, img.size - 1, img.size - 1);
img.drawLine(color, 0, img.size - 1, img.size / 2, img.size / 2);
img.drawLine(color, img.size / 2, img.size / 2, img.size - 1, img.size - 1);
}
public void R(Img img) {
img.drawLine(color, 0, 0, 0, img.size - 1);
img.drawLine(color, 0, 0, img.size - 1, 0);
img.drawLine(color, 0, img.size / 2, img.size - 1, img.size / 2);
img.drawLine(color, img.size - 1, 0, img.size - 1, img.size / 2);
img.drawLine(color, 0, img.size / 2, img.size - 1, img.size - 1);
}
public void D(Img img) {
img.drawLine(color, 0, 0, 0, img.size - 1);
img.drawLine(color, 0, 0, img.size - 1, img.size / 2);
img.drawLine(color, 0, img.size - 1, img.size - 1, img.size / 2);
}
public void print(Img img) {
img.print();
img.clear();
System.out.println(-1);
}
public void HelloWorld(Img img) {
H(img); print(img);
E(img); print(img);
L(img); print(img);
L(img); print(img);
O(img); print(img);
print(img);
W(img); print(img);
O(img); print(img);
R(img); print(img);
L(img); print(img);
D(img); img.print();
}
}