package labT1;
import java.util.Scanner;

public class TestA0 {

	public static void main(String[] args) {
		int score=30;

		score-=checkSize(8);
		score-=checkSize(20);
		score-=checkSize(60);

		System.out.println("Total score: " + score);
	}

	public static int checkSize(int sz) {
		T1A0 obj = new T1A0(sz);
		double[] data=new double[sz];
		int deduction=0;
		if (scanArray(obj.toString(),sz,data)) {
			for(int i=1;i<sz-2;i++) {
				if (!deq(Math.abs(data[i]-data[i+1]),2)) {
					System.out.println("Difference between " + i + " and " + (i+1) + " was " + (data[i]-data[i+1]) + " but should be 2.");
					deduction+=5;
					break;
				}
			}
			double sum=0.0;
			for(double d : data) { sum+=d; }
			if (!deq(sum,sz)) {
				System.out.println("Sum was not " + sz + " : " + sum);
				deduction+=5;
			}
			if (!deq(obj.average(),1.0)) {
				System.out.println("Average for size " + sz + " was not 1: " + obj.average());
				deduction+=5;
			}
		} else {
			deduction=10;
		}
		System.out.println("Checked size " + sz + ", deduction=" + deduction);
		return deduction;
	}


	public static boolean scanArray(String str,int sz, double[] data) {
		String orig=new String(str);
		str=str.trim();
		if (!str.startsWith("[")) {
			System.out.println("Bad opening delimiter in array: " + orig);
			return false;
		}
		str=str.replaceFirst("\\[","");
		if (!str.endsWith("]")) {
			System.out.println("Failed to find closing delimiter in: " +orig);
			return false;
		}
		str=str.replaceFirst("\\]","");
		str=str.trim();
		Scanner scan = new Scanner(str);
		scan.useDelimiter("\\s*,\\s*");
		for(int i=0;i<sz; i++) {
			if (!scan.hasNextDouble()) {
				System.out.println("Failed to find the " + i + " th double value in array: " + orig);
				System.out.println("Found: |" + scan.next() + "|");
				scan.close();
				return false;
			}
			data[i]=scan.nextDouble();
		}
		scan.close();
		return true;
	}
	static boolean deq(double a, double b) {
		return (Math.abs(a-b) <= 1e-6);
	}
}