יום שני, 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.


יום ראשון, 28 באוגוסט 2011

Week 2 (Insertion and Selection Sort, Recursive Iterative, MergeSort QuickSort)

This week's student tasks were surprisingly not as difficult as I expected. I managed to complete the first student tasks in about 10 minutes which was surprising to me. The rest was more or less a review again, but it got me very confident in using Eclipse. The recursive and iterative methods were not a problem. When I arrived to the mergesort activity I was pretty lost. I know how it works yet I can't seem to find the right way to program it, hopefully we could brush over this in class. I am progressing with python, I have done plenty to give me confidence for the in class quizzes. Overall I am doing well, I might just need a little bit of help dealing with the mergesort and quicksorts.

You have probably heard of the group called Folding@Home, created in Stanford. http://folding.stanford.edu/
I heard about this when I was in London and saw a cool 3D visual representation of protein folding on my friends computer. What it does is it runs through a large amount of algorithms and records the data of the outcomes. When  proteins don't fold correctly they can cause different diseases or syndromes.

יום ראשון, 21 באוגוסט 2011

Week 1 (Searches, Big O, Visual Rep)

I have completed my first few coding problems using python. They were not difficult at all, the logic is the same and I feel like I am going to grasp the syntax quickly. I went through all the assignments of this week today and noticed that it was more or else of a review of what I already knew, however it really helped me get familiar with Eclipse. The student assignemtn at the end was tough for me I wasn't sure where to start, hopefully we could quickly talk about it in class, otherwise everything else was pretty smooth and self explanatory.

יום שני, 21 במרץ 2011

Gridworld Handout

After trying to comprehend the packet I felt a little unconfindent with myself. I am really struggling with Gridworld; the free response I don't know where to begin and the multiple choices have me confused. I can get the very very basic questions but am having trouble with the rest. This is probably the thing I will be most focused on comprehending until the exam.

יום ראשון, 6 במרץ 2011

Sunday 06/03/11 Program Design and Analysis

I read the chapter and noticed that it was half review half new material for me. I do not have the terms down completely but will go over them some more in the future. The multiple choice was not tough and I understood what I got wrong, it was mostly knowing the terms. I caught up with a little Grid world today as well and will try to finish it as soon as I can. I still have not started my project however.

יום שני, 28 בפברואר 2011

Monday 28/02/11 Some Standard Classes

After reading the assigned pages a lot of syntactical problems were cleared up. It was all review but it helped me noticeably. The questions were not difficult and I got most of them right however the ones I got wrong I am not sure where my mistakes are and I am hoping to clear these up in class tomorrow.

The Coding bat problems were not easy. I have the logic down for the three but for the two of them I am having unsolvable syntactical problems which I also hope to clear up in class tomorrow.

The implementation of caterpillar is something that I am not sure even where to start.

The GridWorld project assigned is an interesting one. I can't say I fully understand it at this stage but I am sure I will as time goes by.

יום שני, 14 בפברואר 2011

Gridworld: Part 3: Classes & Interfaces: Set 6: Page 25-26



1
Which statement(s) in the canMove() method ensures that a bug does not try to move out of its grid?  

2
Which statement(s) in the canMove()method determines that a bug will not walk into a rock?  

3
Which methods of the Grid  interface are invoked by the canMove()  method and why?  

4
Which method of the Location  class is invoked by the canMove() method and why?  

5
Which methods inherited from the Actor class are invoked in the canMove()method?  

6
What happens in the move() method when the location immediately in front of the bug is out of the grid?  

7
Is the variable loc needed in the move()method, or could it be avoided by callinggetLocation() multiple times?  

8
Why do you think the flowers that are dropped by a bug have the same color as the bug?  

9
When a bug removes itself from the grid, will it place a flower into its previous location?  

10
Which statement(s) in the  move() method places the flower into the grid at the bug’s previous location?  

11
If a bug needs to turn 180 degrees, how many times should it call the turn()method?  

Gridworld: Part 3: Classes & Interfaces: Set 5: Page 23



1
What methods are implemented inCritter?  

2
What are the five basic actions common to all critters when they act?  

3
Should subclasses of Critter override thegetActors()   method?

Explain

4
Describe three ways that a critter could process actors.  

5
What three methods must be invoked to make a critter move?

Explain each of  these methods.  

6
Why is there no Critter constructor?