Thursday, July 4, 2013

Maximum number of Collinear Points | Java

I came across this question to find the maximum number of Collinear Points.
I have used a Binary Search Tree(BST) structure to keep track of all the gradients between the different points. Another approach which could have been simpler would have been to use a HashMap. However, considering that there would be a limited number of gradients, unless its a curve... a HashMap would have resulted in occupying far much space compared to a BST.
Here's my attempt at it.

Wednesday, July 3, 2013

Checking for Palindromes in Java

In our first year at KCL, I remember a lab assignment to write something which would check for palindromes. So when recently I came through this question apparently posed during a Facebook interview to check for palindromes whilst ignoring spaces, I felt this shouldn't be mind bogging.

It certainly isn't. In my code below, I have created an internal private class Cursor, which as its name suggests will act as a cursor on the String. The cursor can move one character at a time, either backwards or forwards.  Two cursor objects will be initialised, head and tail. The head will move from the first character whereas the tail will move from the last. This will be so until they reach opposite ends and the head is at a position lower than the tail.

To avoid spaces, I loop the head cursor, until it finds a valid character. I follow this by looping the tail cursor until it finds a valid character as well. And then finally compare them, if they dont match then the test is terminated with the method returning false.

Sunday, July 8, 2012

Overriding properties in CSS

While working on the design of my site using Twitter's bootstrap, I had a hard time resizing the height of the search box, which due to a strange reason was looking much smaller than it should.
The first thing I tried was making my own CSS file and make a class style.css . However even after adding that class to my search box div, there was no change. This is because the class contained within the bootstrap.css is more specific. To enforce a change however all that needs to be done is add !important following the property in css. As an example :-
.search
{ height: 28px !important; }

Tuesday, July 3, 2012

multi-dimensional array in php

Similar to Java, php also allows us to have multi-dimensional arrays. As an example of this
$food = arrays('healthy' => arrays('salad', 'chicken') , 'unhealthy' => arrays('ice cream', 'pizza'));


Now to access salad element, the following needs to be done.

$food[healthy][0];

associative arrays in php

Associative arrays are similar to arrays discussed previously, however they have a key attached to a value.

As an example

$food = ('pizza' => 300 , 'pasta' => 100, 'ice cream' =>100 );


to get the value stored at key 'pizza' :-


$food['pizza'];

arrays in php

An array can be simply defined as a collection of data stored within one variable. To do this in php initialize the variable using the dollar sign, and then in the right hand side add the values within rounded brakets each data entry separated with a inverted comma. Bear in mind that the types of the data should not be different for each value within that array.

An example of an array :-
$food = ('apple', 'chicken' ,' sandwich');

Thursday, June 28, 2012

php functions on strings

The following are some functions which can be used on strings in php.
  1. str_word_count :- str_word_count has different results depending on parameters.
    • str_word_count($string_variable) returns the word count
    • str_word_count($string_variable, (0,1 or 2)) where
      • 0 returns the word count(the default option)
      • 1 returns an array
      • 2 returns an associative array
    • str_word_count($string_variable, (0,1,2), ' (something to be included as a word for ex '.!&' ').
      • different arguments included next to each other.
  2. str_shuffle($string_variable) :- shuffles string_variable
  3. substr($string_variable,  starting position, ending position) :- returns the substring
  4. strlen($string_variable) :- returns the length of the string_variable
  5. strtolower($string_variable) :- returns $string_variable with all characters in lower case
  6. strttoupper($string_variable) :- returns $string_variable with all characters in upper case