lectures

2014-05-13 Extra Assignment

posted Jun 13, 2014, 9:27 AM by Samuel Konstantinovich   [ updated Jun 13, 2014, 10:36 AM ]

Hand in a paper copy on Monday. June 16th.

Goal: Present the ideas you learned in class about these topics:
  1. Graphs + Their representation 
  2. Depth first searches using a frontier (fringe), how the frontier is implemented. 
  3. Path recovery 
  4. How changing the frontier can yield different types of searches : Breadth furst, Best first, and A* searches.

I want to see that you understand the concepts, not just regurgitate a wiki. The target audience is a stuy AP student who had all prerequisite knowledge.

I want to see that you put in effort and took notes. If you write it by hand on a napkin, don't expect much. You should try to impress me. Handwritten is allowed, but it must be amazingly neat. You may draw diagrams by hand if you type the rest. 

Sources:
You should use your notes.
You MAY use outside sources, but you should make citations. 

2014-05-20 Final Project Notes

posted May 21, 2014, 4:58 AM by Samuel Konstantinovich   [ updated May 21, 2014, 4:58 AM ]

final project:


Your final Project should use github. Specifications at the end.


Any of my students from last semester don't know github. Now you get to learn it. (Yay!)

Github interactive tutorial: https://try.github.io/

 One person per group make a repository.

 Name: PerX-last1-last2-nameofproject

 Description: Brief description

 Initialize with a readme

 Make it Public

 Add your teammates:

     Settings for repository: Collaborate, Collaborators, Add your teammate(s) 

 Please use meaningful comments for your commits. ( http://xkcd.com/1296/ )
 If you want to try something that might break your code look into forking your project.


2014-05-16 Homework!!

posted May 16, 2014, 10:50 AM by Samuel Konstantinovich   [ updated May 16, 2014, 12:17 PM ]

Deadline Sunday the 18th of May at 1159pm

UPDATED - don't do it 30 times, do it size times!!!

You are writing a Sorry class which is a driver to test your RunningMedian

command line usage:  java Sorry size seed1 seed2 seed3... (at least 2 integer argunments on the command line)
java Sorry 30 99 45  //size is 30, seeds are 99 and 45

The first integer (size) tells you how many iterations per test. 

For each subsequent integer you will create a java Random  ( http://docs.oracle.com/javase/7/docs/api/java/util/Random.html )
and use that random in 2 tests.

so:
for each seed in arguments {
   Random R=new Random(seed);
   test1(R,size);
   test2(R,size);
}

//////////////////////////////////


void test1(Random r, int size){
    //create a runningMedian
    //repeat size times:
    //create a random value, and add it to your running median.
    //print what you added and the median after each add.
}


void test2(Random r,int size){
    //create a runningMedian
    //repeat size times:
    //create a random value, and add it to your running median.
    //print what you added, and the median after each add.
    //there is a 20% chance that the median is removed. (print that you are doing this)
}



Output test1:
added: 2, median is: 2.0
added: 6, median is: 4.0
added: 100, median is: 6.0
....

Optional1: You can improve it by making it display the values added so far (make a sorted array that you calculate as you go)
added: 2, median is: 2. [2 ]
added: 6, median is: 4. [2, 6]
added: 100, median is: 6.  [2, 6, 100]
....


Optional2: You can improve it by making it display the values added so far (make a sorted array that you calculate as you go) BUT! Only display the middle 9 elements(if an odd number of elements are present) OR the middle 8 elements (if an even number of elements are present)
added: 0, median is: 5.0 [0, 1, 2, 3, 4, 5, 6, 7, 8]
added: 9, median is: 4.5 [... 1, 2, 3, 4, 5, 6, 7, 8...]
added: 10, median is: 5.0 [... 1, 2, 3, 4, 5, 6, 7, 8, 9...]
added: 100, median is: 5.5 [... 2, 3, 4, 5, 6, 7, 8, 9...]
....



Output test2:
added: 5, median is: 5.0  [5]
added: 4, median is: 4.5 [4, 5]
added: 100, median is: 5.0 [4,5,100]
removed median 5.0. [4,100] new median is: 52.0  //it removes only 1
added: 10 median is: 10.0 [4,10,100]
added: 100 median is: 55.0 [4,10,100,100]
removed median 55.0. [4, 100] new median is: 52.0  //notice it removes both




2014-05-12 + HW

posted May 12, 2014, 9:40 AM by Samuel Konstantinovich   [ updated May 15, 2014, 4:54 AM ]

UPDATED
You are writing a RunningMedian class which is a container that has optimized methods to add/remove elements, and calculate the median. In addition to a default constructor that makes an empty container, It supports 3 methods:

void add(int value) 
    add an integer to the collection, should not be worse than O(logn) , duplicates are allowed.

double remove()  
    remove the median (or both medians) from the collection and return its value (or the average of two values), should not be worse than O(logn). Throws an IllegalStateException() when there are no values in the container.

double median()
    return the median of the collection. Should not be worse than O(1). Throws an IllegalStateException() when there are no values in the container.

2014-04-09 HW (vacation)

posted Apr 9, 2014, 2:26 PM by Samuel Konstantinovich   [ updated Apr 9, 2014, 2:27 PM ]

Vacation Homework: (Due April 25th)
1. Gridworld (You need to finish learning gridworld in order to complete practice exam1)
     You should have read through chapters 1-3, now complete reading chapter 4. Then you can work on the practice exam!!!

2. Complete the two exams:

APCS practice exam 1:  (full exam)

APCS practice  exam 2: (part 1 practice only)

2014-04-04 + HW

posted Apr 5, 2014, 7:31 PM by Samuel Konstantinovich   [ updated Apr 5, 2014, 7:31 PM ]

Homework:
Implement MyArrayStack<T>
-The interface should be the same as a regular stack.
-You must use an array of <T> instead of Node<T> to store your data.
-You must resize the array whenever appropriate.

Code from class attached to post. File names are wrong, but include the period.



2014-03-28 HW

posted Mar 28, 2014, 8:32 AM by Samuel Konstantinovich   [ updated Mar 28, 2014, 8:34 AM ]

Reminder: I posted test cases for 1-5 for your old hw server assignments. You were supposed to use the test cases for the 6th assignment on your own.

Today you are fixing broken code!

Double Linked List idea:

Each element points to the next and to the previous. 
So if you had a list:
A - B - C
A's previous is null.
A's next is B.
B's previous is A.
B's next is C.
C's previous is B.
C's next is null.

There is no head/tail. The only instance variable is current. 

The current node can point to ANY element in the list. It is not always the front or back.
-You can move the current to the next and previous. This is required to access other values.
-Insert will insert AFTER the current node. 

2014-03-26

posted Mar 26, 2014, 8:45 AM by Samuel Konstantinovich   [ updated Mar 28, 2014, 8:27 AM ]

Amnesty program:

All of the homework server assignments have major issues with multiple people's submissions.

In order of WORST to least horrible errors:
1. Epic bad: SYNTAX errors. Your submitted file doesn't even compile.
2. Bad: Output has hundreds or thousands of lines of text. 
3. Pretty bad: Wrong class name, method names, or wrong parameters
4. Bad enough to mention: You submitted a java file OR your zip had a folder in it.
5. Bad but I am not mad: You submitted a file that wasn't zip or rar. (like .gz)  This is awesome, but my script didn't account for this. It only handles zip/rar files. 

Tests I used:

A1 Recursion
public class Driver{
    public static void main(String[]args){
/*create an instance of what you are testing*/
recursion a = new recursion();
int score = 0;
/*run calculations*/
int[] answers={0,1,1,2,3,5,8,13,21,34,55,89};
for(int i=0;i<answers.length;i++){
   if(answers[i] == a.fibItr(i)){
score++;
   }else
System.out.println("failed fibItr("+i+")");
}
/*score the calculations*/
/*print out your test results*/
int[]answers2={0,1,2,8,3,6,9,17,4,20,7,15,10};
int[]answers3={0,1,2,3,3,3,6, 7,7, 9,9, 9, 9};
int score2=0;
int score3=0;

for(int i=1;i<answers2.length;i++){
   if(answers2[i] == a.hailLen(i)){
score2++;
   }else
System.out.println("failed hailLen("+i+")");
}
System.out.println();
for(int i=1;i<answers3.length;i++){
   if(answers3[i] == a.longestHail(i)){
score3++;
   }else
System.out.println("failed longestHail("+i+")");
}
score /= 6;
score2 /= 4;
score3 /= 4;

long start=System.currentTimeMillis();
int result = a.fibItr(40);
long end=System.currentTimeMillis();

if(result==102334155 && (end-start) < 200){
   score++;
}else{
   System.out.println("Either fibIter(40) was not 102334155, or it took too long. Result:"+result+" time="+(end-start)/1000.0+"seconds");
}

System.out.println("max fib score 3,     student score:"+score);  
System.out.println("hailLen score 3,     student score:"+score2);  
System.out.println("longestHail score 3, student score:"+score3);  
System.out.println("SCORE,"+a.lastName()+","+a.firstName()+","+(score+score2+score3));


    }
}

A2 Knights Tour:
public class Driver{
    public static void main(String[]args){
Knight k=new Knight(Integer.parseInt(args[0]));
k.solve();
System.out.println(k);
    }
}


A3 merge sort ArrayList<String>

import java.util.*;
public class Driver{
    public static void main(String[]args){
System.out.println(Sorts.name());
ArrayList<String>A1= new ArrayList<String>();
ArrayList<String>A2= new ArrayList<String>();
ArrayList<String>B1= new ArrayList<String>();
ArrayList<String>B2= new ArrayList<String>();
for(int i=0;i<10000;i++){
   String value=""+(10000-i);
   String value2=""+(i);
   A1.add(value);
   A2.add(value);
   B1.add(value);
   B2.add(value);
   B1.add(value2);
   B2.add(value2);
}

Collections.sort(A2);
Collections.sort(B2);

Sorts.msort(A1);
Sorts.msort(B1);

String ans=Sorts.name();
if(A1.equals(A2))
  ans+="Sorted A!";
if(B1.equals(B2))
  ans+="Sorted B!";
System.out.println(ans);   
    }
}

A4 optimized mergesort:

public class Driver{
    public static void main(String[]args){
int[]aryA = new int[5000000];
int[]aryB = new int[aryA.length];

for(int i = 0;i<aryA.length;i++){
   aryA[i]=(int)(Math.random()*400000000-200000000);
   aryB[i]=aryA[i];
}
long start,end,duration,start2,end2;

start=System.currentTimeMillis();
Sorts.msortOpt(aryA);
end=System.currentTimeMillis();

start2=System.currentTimeMillis();
Arrays.sort(aryB);
end2=System.currentTimeMillis();

if(equals(aryA,aryB)){
   System.out.println(Sorts.name()+" Time to mergesort \t"+aryA.length+" integers: "+(end-start)/1000.0+"seconds. builtin took:"+(end2-start2)/1000.0);
}else{
   System.out.println("Not Sorted!");
}

    }

    public static boolean isSorted(int[]L){
if(L==null || L.length<2){
   return true;
}
for(int i=0; i<L.length-1;i++)
   if(L[i]>L[i+1]){
return false;
   }
return true;
    }
    public static boolean equals(int[]L1,int[]L2){
if(L1.length == L2.length){
   for(int i=0; i<L1.length-1;i++)
if(L1[i]!=L2[i]){
   return false;
}
   return true;
}else{
   return false;
}


    }
}


A5: Quicksort:


public class Driver{
    public static void main(String[]args){
int[]aryA = new int[100000];
int[]aryB = new int[aryA.length];

for(int i = 0;i<aryA.length;i++){
   aryA[i]=(int)(Math.random()*400000000-200000000);
   aryB[i]=aryA[i];
}
long start,end,duration,start2,end2;
String name = Sorts.name();
start=System.currentTimeMillis();
Sorts.quicksort(aryA);
end=System.currentTimeMillis();

start2=System.currentTimeMillis();
Arrays.sort(aryB);
end2=System.currentTimeMillis();

if(equals(aryA,aryB)){
   System.out.println(name+" time to Quicksort  "+aryA.length+" integers: "+(end-start)/1000.0+"seconds. builtin took:"+(end2-start2)/1000.0);
}else{
   System.out.println("Not Sorted!");
}


    }

    public static boolean isSorted(int[]L){
if(L==null || L.length<2){
   return true;
}
for(int i=0; i<L.length-1;i++)
   if(L[i]>L[i+1]){
return false;
   }
return true;
    }
    public static boolean equals(int[]L1,int[]L2){
if(L1.length == L2.length){
   for(int i=0; i<L1.length-1;i++)
if(L1[i]!=L2[i]){
   return false;
}
   return true;
}else{
   return false;
}


    }
}

2014-03-24

posted Mar 24, 2014, 8:37 AM by Samuel Konstantinovich   [ updated Mar 24, 2014, 9:11 AM ]

Lab: Modify your linked list

tail :  a new instance variable pointing to the last element.  (you need to update this in your constructor, add, and remove methods)
add( T  data)    will add a new node at the END of the list.
For consistency, length() should also work if you say size() 


2014-03-21

posted Mar 21, 2014, 5:36 AM by Samuel Konstantinovich   [ updated Mar 22, 2014, 10:07 AM ]

Goal: Exceptions and Generics.
Summary:
Checked vs. Unchecked exceptions.

Unchecked exceptions (runtime exceptions like indexOutOfBounds, DivideByZero, and NullPointerExceptions)
are allowed to be passed out of your code all the way to the JVM. These are serious problems that are agreed upon to be things that should really stop the program. You can still catch them, but it isn't required.

Checked Exceptions (the regular Excepiton and many others) must be caught in your method, OR the method must be declared to throw it. 

You should throw IndexOutOfBoundsException() in your linked list code. This is an UNCHECKED exception that
a) accurately describes the problem you encountered
b) does not require you to declare that your methods throw it, nor require you to catch it. 

Here is reference on exceptions.
http://docs.oracle.com/javase/tutorial/essential/exceptions/throwing.html

LAB+HOMEWORK this weekend:
Make your MyLinkedList class accept generic types:  MyLinkedList<T>

You are submitting a zip with two files:
MyLinkedList.java
Node.java

I will test it on Integer() after you submit it. 
SUBMIT IT BY 1159PM SUNDAY. TEST IT FIRST! Post how you tested it on the mailing list if you aren't sure if it is sufficient.


1-10 of 23