יום שני, 12 בדצמבר 2011

Hash Tables, Maps, and Sets

8_1
No

8_2
Arraylists. Maybe queues, but that's pushing it.

8_3
you could have a temp which checks all the objects in a queue, and then adds them into another queue, if the object is the same as what you are looking for, you add it twice, otherwise you continue dumping everything into the other queue, which then  becomes your base queue for next time the method is called.

8_4
I believe they are called hash tables because they split the data into parts. I honestly still don't completely understand the process.

8_5


יום שני, 28 בנובמבר 2011

Queues & Heaps


QUEUES & HEAPS
Queues

7_1
Yes it works in the FIFO behavior.
7_2
Using implementation of LinkedLists is ideal because they are very flexible at adding new objects at the front and the back, in retrospect I am not sure why you couldn't also use ArrayLists. I am not sure how to calculate the BigO here.
7_3
The add() method adds to the beginning whilst the addLast() adds to the end. I believe addLast() was used because in the case of Queues, adding means adding to the end.
7_4
I had to look at the API.
import java.util.ArrayList;
public class ArrayListQueue implements Queue 
{ 
private ArrayList list;

public void ListQueue() 
{ 
    list = new ArrayList(); 
}//constructor

public void enqueue(Object x) 
{ 
    list.add(x); 
}//enqueue

public Object dequeue()
{ 
    return list.remove(0); 
}//dequeue

public Object peekFront()
{ 
    return list.get(0); 
}//peekFront

public boolean isEmpty() 
{ 
    return list.size() == 0; 
}//isEmpty

}//class ListQueue

7_5
Ease - Almost the same ease, almost all the methods were still one liners.
Efficiency - Not a change at all, ArrayLists still get the job done and the ease of writing its code is not much harder.

Heaps

Started.

Binary Trees 2

6_6

יום ראשון, 20 בנובמבר 2011

Binary Trees 1


6_1
There are four leaves, 3 nodes have two children, and it would take 2 iterations to find out that 12 is not in it, i believe

6_2
Yes it worked.

6_3
I don't know why it took me so long to figure out where to put the code
public void add(Object newValue)
{
    TreeNode newNode = new TreeNode(newValue,null,null);

    if(root==null)//bst is empty, make root reference node referenced by newNode
    {
        root=newNode;
    }//if

    else//search for place to insert node referenced by newNode
    {
        TreeNode T=root;//T is a traversing TreeNode reference (pointer)
        TreeNode follow=null;//follows one node behind T

        while(T!=null)
        {
            follow=T;
            if (((Comparable)newNode.getValue()).compareTo(T.getValue())==0){return;}
            if (((Comparable)newNode.getValue()).compareTo(T.getValue())>0)
            {//the value in node referenced by newNode >= 
              //value in node referenced by T (go right)
                T=T.getRight();
            }//if
            else
            {//go left
                T=T.getLeft();
            }//else
        }//while

        //follow will now reference (point to) the node where insertion to be made 
        if (((Comparable)newNode.getValue()).compareTo(follow.getValue())>=0)
        {//the value in node referenced by newNode >=
          //value in node referenced by follow (link right)
            follow.setRight(newNode);
        }//if
        else
        {//link left
            follow.setLeft(newNode);
        }//else

    }//else
}//add

}//class BST

6_4
I am not sure why

6_5
public void find(Object target){
if(root==null){return;}
TreeNode T=root;//T is a traversing TreeNode reference (pointer)
    TreeNode follow=null;//follows one node behind T
    while(T!=null); {
    follow = T;
    if (((Comparable)newNode.getValue()).compareTo(T.getValue())==0) {return T;}
    if (((Comparable)newNode.getValue()).compareTo(T.getValue())>0) {T=T.getRight();}
    else {T=T.getLeft();}
    }
return null;
}

יום ראשון, 23 באוקטובר 2011

Long Overdue Stacks

4_1 done
4_2 done
4_3 done
4_4 yes, resolved
4_5 wasn't that hard, done

The recursive sum method helped visualize things, but a little redundant to me.
To create infinite recursion with a positive number, I would have to delete the base case or alter it.

4_6
This took a while because of my syntax of ArrayStackList, but i had the logic down pretty quickly

public class StringReverseDemo extends ArrayListStack
{
    //uses stacks (class ArrayStack) to reverse word. 
    public static String getReverse(String word)
    {
    ArrayListStack stack1=new ArrayListStack();
    String result = "";
    for (int i=0;i<word.length();i++){
    stack1.push(word.substring(i, i+1));
    }
    while (!stack1.isEmpty()){
    result+=stack1.pop();
    }
    return result;
    }//getReverse

    //uses recursion to reverse word
    public static String getReverseRecursive(String word)
    {
    if (word.length()==1){return word;}
    else
    return word.substring(word.length()-1,word.length())+getReverseRecursive(word.substring(0,word.length()-1));
    }//getReverseRecursive

}//class String

4_7
I believe recursion simplifies code dramatically, at least for me, as long as you understand it. The iterative approach is easier to think about but is more prone to errors.

4_8
 public static boolean isPalindrome(String word){
    return word==getReverseRecursive(word);
    }

4_9
Ha!... I'm not sure.

TOWERS OF HANOI

I am really really frustrated because it won't let me run the TowersOfHanoi application. I can still do the other implementation and alterations but I can't check that it completley works.....


4_10
            2n^(n-1)

4_11

I didn't really understand this so I got help from classmates.

public void play()
{
    setUpPegs();

    ArrayStack blue=new ArrayStack(maxPegs);
    ArrayStack green=new ArrayStack(maxPegs);
    ArrayStack yellow=new ArrayStack(maxPegs);

    int difficulty; 
    SimpleInput inputDifficulty = new SimpleInput();
    difficulty=inputDifficulty.getInt("How many disks would you like?");

    for (int i=0;i<difficulty;i++)
    {
        disk temp=new disk(20,100-(i*10),0+(i*5),180-(i*20),"red",true);
        blue.push(temp);
        temp.makeVisible();
    }//for

    reDraw(blue,green,yellow);


    while(!(blue.isEmpty() && green.isEmpty()))
    {
        char from, to;
        SimpleInput moveCharacter = new SimpleInput();
        from = moveCharacter.getChar("from: b=blue, g=green, y=yellow");
        to = moveCharacter.getChar("to: b=blue, g=green, y=yellow");

        disk carry=null;
        
        boolean valid = false;
        while (!valid)
        {
        switch (from) 
        {
            case 'b': carry=(disk)blue.pop(); valid = true; break;
            case 'g': carry=(disk)green.pop(); valid = true; break;
            case 'y': carry=(disk)yellow.pop(); valid = true; break;
            default : System.out.println("error, try again");
        }//switch
        }

        boolean nextValid = false;
        while (!nextValid)
        {
        switch (to)
        {
        case 'b': if (blue.isEmpty() || carry.getWidth()<((disk)blue.peekTop()).getWidth())
          {
          carry.changePosition(carry.getXPosition()%100,180-(stackHeight(blue)*20));
          blue.push(carry); nextValid = true; break;
          }
          else
          {
        System.out.println("Cannot put bigger disk on smaller disk");
          }
        case 'g': if (green.isEmpty() || carry.getWidth()<((disk)green.peekTop()).getWidth())
    {
          carry.changePosition((carry.getXPosition()%100)+100,180-(stackHeight(green)*20));
                        green.push(carry); nextValid = true; break;
    }
          else
          {
        System.out.println("Cannot put bigger disk on smaller disk");
          }
            case 'y': if (yellow.isEmpty() || carry.getWidth()<((disk)yellow.peekTop()).getWidth())
              {
            carry.changePosition((carry.getXPosition()%100)+200,180-(stackHeight(yellow)*20));
                        yellow.push(carry); nextValid = true; break;
              }
              else
              {
            System.out.println("Cannot put bigger disk on smaller disk");
              }
            default : System.out.println("error, try again"); 
        }//switch
        }

        reDraw(blue,green,yellow); 
    }//while(main loop)

    System.out.println("Well Done");

}//play

4_12
I have NO idea how to implement the algorithm i found online (which I believe is correct) into a solve method. 

4_13
Everything worked perfectly, for (a/0) I just got an error, as expected.

4_14
Worked fine, it returned false.

4_15
I honestly don't know how to start this. I looked at my classmate's work for this and it's not that I don't understand the syntax.. this time im having trouble with the logic.

4_16
You would need to change every int in your code into into double, besides ofcourse for the int i =... in loops.

4_17
Easy.
switch (operator)
            {
                case '+': result=operand1+operand2; break;
                case '*': result=operand1*operand2; break;
                case '/': {if (operand1==0) {System.out.println("can't divide by 0");return 0;} result=operand2/operand1; break;}
                case '-': result=operand2-operand1; break;
            }//switch

4_18
I have a few ideas of how to approach this, however I am really running out of time. This is something for me to come back to hopefully sometime this week.

Finally. I caught up.



יום ראשון, 2 באוקטובר 2011

The CatchUp- Part 1

I went back to my previous post and attempted the last exercise and noticed how simple it was. I therefore managed to successfully modify LinkedListTest_Application to incorporate the new methods I made before.
-------------
Here's my recursiveFind method. Wasn't a problem at all.


public ListNode recursiveFind(ListNode list, Object goal){
 if (goal.toString().compareTo(list.getData().toString()) == 0){return list;}
 if (list.getData().toString().compareTo(goal.toString()) == 1){return null;}
 return recursiveFind(list.getNext(), goal);
}

Here is my recursive Length method.  Not a problem either.

public int recursiveListSize(ListNode list){
int count = 0;
if (list.getData()==null) 
{return count;}
{count++;}{return recursiveListSize(list.getNext());}
}

for 3_8 I couldn't think of a good way to approach this. The only thing i thought of doing was doing a count, putting the data on an array and then printing that backwards, but I felt like that was somewhat, cheap.

------------
CircularListNodes wasn't tough, it was pretty straight forward. I have it coded on my Eclipse workspace.
3_11 Having an end node complicates it for me and I get confused however it saves you a bunch of coding lines which you can ignore, but I would prefer writing those lines rather than to get confused.

------------
All I want to say at this stage is that a DoubleList is something beautiful and I wish I knew about it earlier.


3_12-3_14 I did, but I wish there were more specifications so I could know if my  method is right or wrong.


------------
LINKED LIST APPLICATION.


Mr Daly - I would just like to say that I spent about 10 minutes trying to fix your spelling mistakes in your code which gave me errors. It drove me mad.


I am now on 3_16 and can't keep my eyes open, I will continue my catchup mission tomorrow.




יום ראשון, 11 בספטמבר 2011

Linked Lists Student Tasks 1-6

Student task number one disappointingly confused me a lot.
I managed to get task number two; I know it is not a huge accomplishment because it is quite basic, but it's a start.

public ListNode findInOrderedList(Object goal)
{
  if(listEmpty()){return null;}
  ListNode temp = list;
  while (!(temp.getData().toString().compareTo(goal.toString()) == 1)) // condition that its still in the part that is smaller or equal to it
    {
        if (goal.toString().compareTo(temp.getData().toString()) == 0)
        return temp;
        else
        temp=temp.getNext();//move on to node 2
       }
  return null;
}

Task three took a while, but Amit helped me with it, and I finally understood it. This helped me a lot with the syntax of linked lists and their logic.


public void deleteFromList(Object goal)
 {
if (findInOrderedList(goal) == null) {return;}
else {
ListNode temp = list;
if (goal.toString().compareTo(temp.getNext().getData().toString()) == 0)
temp.setNext((temp.getNext()).getNext());
else
temp = temp.getNext();}
 }

I am not sure why, but the link for task 4 was blocked for me, I am not sure what I was supposed to do, but it didn't seem like such a necessity.

Task 5&6: I honestly was not very sure what was going on. I know realize I need help.

The following week's work is about halfway done, I will post it when I'm done, hopefully in the next day or two. I know I am behind, and it's a little bit frustrating, but I'm catching up.