Wednesday 5 December 2012

Last Post. Maybe.

Seeing as today is the last day to post something, I shall try and add one more thing to this slog. A few days ago my friend told me that there were things we could look at on the SLOG handout that we might be interested in. The problem solving wiki that Danny provided was fairly interesting so i shall try and solve one of those myself. Seeing how nobody has posted a solution to this question, i shall try.
Question:
Take a list of integers. Can you find a longest non-decreasing subsequence of the list?
If the list were sorted, the problem would be easy enough. However the list is more likely to be something like this:
37 93 0 23 79 65 49 81 67 8 32 29 96 76 15 9 51 14 29 69
Certainly 37, 93, 96 is a non-decreasing, but is it the longest? Whoops, 0, 23, 79 81 is longer...

 Trying to solve #1:
After thinking for a bit, i have decided to try to solve this using Java:

public static int[] longestSubSeq(int[] List){
        ArrayList<ArrayList> subSeq = new ArrayList();
        ArrayList<Integer> temp = new ArrayList();
        //Goes through each element of the list
        for (int start = 0; start < List.length; start++){
            ArrayList<Integer> toCheck = new ArrayList();
            //Copies all elements after the current index
            for (int begin = start + 1; begin < List.length - start; begin ++){
                toCheck.add(List[begin]);
            }
            int currentBiggest = List[start];
            //Finds all the subsequences possible that is non-decreasing
            //and adds it to subSeq
            while (toCheck.size() != 0){
                temp.add(List[start]);
                for (int number : toCheck){
                    if (number >= currentBiggest){
                        temp.add(number);
                        currentBiggest = number;
                    }
                }
                toCheck.remove(0);
                subSeq.add(new ArrayList(temp));
                temp.removeAll(temp);
                currentBiggest = List[start];
            }
        }

        int intLongest = 0;
        ArrayList<Integer> lstLongest = new ArrayList();
        System.out.println(subSeq.size());
        for (ArrayList<Integer>subSequence : subSeq){
            if (subSequence.size() > intLongest){
                intLongest = subSequence.size();
                lstLongest = new ArrayList(subSequence);
            }
        }
        int size = lstLongest.size();
        int[] list = new int [size];
        int count = 0;
        for (int number : lstLongest){
            list[count] = number;
            count ++;
        }
        return list;
    }

I then thought about and realized that the code would have missed some combinations. It worked with the current list and gave me a list of length 5, but it might not have worked with another. So i went on to trying a new method.


Trying to Solve #2:
I wanted to create and array of lists with all the possible combinations of the given list. So if i had {1,3,2}
     I would result in {{1}, {3}, {2}, {1,3}, {1,2}, {3,2}, {1,3,2}}
Then i would check if each and every one of the list is non decreasing. If it is, pop it out from the list.
     The resulting array of lists will become {{1}, {3}, {2}, {1,3}, {1,2}}
Then we will  find the list with the longest length inside the array and that would be the longest non decreasing list.
     The longest list will be either {1,3} or {1,2} depending on the code.
Now that there's 30 minutes left, and it will take too long to code, so i shall leave the solution as that. This solution might not be the most efficient one, but it should work.

Monday 3 December 2012

Well here is the question that i have mentioned before
Devise a finite state machine that accepts the language of binary strings that have an even number of 1s and an odd length.Coming up with this machine during the quiz was quite difficult but after doing more questions on creating machines, this has become a lot easier all of a sudden. Well here is the machine i have created and i am pretty sure it is right:


Wednesday 21 November 2012

Quiz this week

The quiz that was given in tutorial was a little hard in my opinion. We did not have enough time to come up with a machine and an explanation in merely 10 minutes. I don't remember what the question was but once i get it back i shall be trying to solve it on this SLOG. Hopefully.

Sunday 4 November 2012

Procrastinating A2

Every single time an assignment comes out on a single sheet of paper, it turns out to be a 4 page assignment. As usual I started the assignment on the last day and spent the final hours finishing the assignment. Amazingly every single time i am able to complete it on time even with my bad procrastination habits. Trying to type up the assignments probably take up the most time and effort. I would really like to just scan pieces of papers and hand it in as a pdf. Should really start on the assignments earlier next time around, but... we shall see what happens.

Sunday 7 October 2012

Latex Latex Latex

        Ever since assignment 1 was posted, all i have been hearing are "Are you going to use LaTex?" I started to get curious after many mentions of the software LaTex. Is it really that much better than other software at writing assignments? Does LaTex actually produce math symbols faster? I have been using Microsoft Word for the CSC165 assignments and have been quite satisfied with it. The auto-correct function allows me to write math symbols at a very efficient pace. For example, if I wanted the forall symbol i would simply need to type /fa(the way I configured it). Do i really want to invest time into learning LaTex?

         Since I didn't know much about LaTeX, I decided to consult my good buddy google. A few articles later I realized that learning LaTeX would probably take a decent amount of time. The difference of LaTeX and word was not too great in terms of doing a simple assignment. With the assignment almost due, using LaTeX to do Assignment 1 would probably not be a good idea. LaTeX will have to wait a little while longer.