Google Doc - Workout Spreadsheet

Last week I recieved my lacrosse workout schedule, and I wanted to have access to the workouts without always having the workout sheets my coach gave me. So naturaly I went to Google Docs and created a spreadsheet with a sheet for each day so that I could print a specific days workout.

Being a nerd, I decided that I would keep track of my progress and to do so I created a Google SpreadSheets script that will look for a sheet named after the day of the week. Now the workouts were templates and I added them to a workout sheet so that I could show my actual completion of the workout and the weights used.






function dayOfWeek(date) {


  var weekday=new Array(7);


  weekday[0]="Sunday";


  weekday[1]="Monday";


  weekday[2]="Tuesday";


  weekday[3]="Wednesday";


  weekday[4]="Thursday";


  weekday[5]="Friday";


  weekday[6]="Saturday";


 


  return weekday[ date.getDay() ];


}


 


function newWorkout() {


  var today = new Date();


  //uncomment the following to test


  //today = new Date(2012,1,24);


 


  var day = dayOfWeek(today);


 


  var workoutSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Workouts");


  var exersizeSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName( day );


 


  //check that the spreadsheets have been loaded, exit if they have not


  if (workoutSheet == null) {


 


    Browser.msgBox("Could not load the 'Workouts' spreadsheet, make sure it is created");


    return false;


 


  } else if (exersizeSheet == null) {


 


    Browser.msgBox("Could not load the spreadsheet '" + day + "', make sure that it has been created");


    return false;


 


  }


 


  /*


   * add todays date, and the day of the week before adding the workout


   */


 


  //get the range so that the title can be added


  var todaysTitle = workoutSheet.getRange(


    workoutSheet.getLastRow()+2, //start row


    1, //start column


    1, //number of rows


    2 //number of columns


  );


 


  //add the date information


  todaysTitle.setValues(


    [ 


      [


        today, 


        day


      ]


    ]


  );


 


  /*


   * add todays workout


   */


 


  //get the workout to be done today


  var todaysWorkout = exersizeSheet.getRange(


    1, //start row


    1, //start column


    exersizeSheet.getLastRow(), //number of rows


    exersizeSheet.getLastColumn() //number of columns


  );


 


  todaysWorkout.copyValuesToRange(


    workoutSheet, //the sheet to be copied to


    1, //the start column


    exersizeSheet.getLastColumn(), //the number of columns


    workoutSheet.getLastRow() + 2, //the start row


    workoutSheet.getLastRow() + 2 + exersizeSheet.getLastRow() //the number of rows


  );


 


 


  //copy todays workout (format) to the workout sheet


  todaysWorkout.copyFormatToRange(


    workoutSheet, //the sheet to be copied to


    1, //the start column


    exersizeSheet.getLastColumn(), //the number of columns


    workoutSheet.getLastRow() + 2, //the start row


    workoutSheet.getLastRow() + 2 + exersizeSheet.getLastRow() //the number of rows


  );


 


}


 


 


function onOpen() {


  var ss = SpreadsheetApp.getActiveSpreadsheet();


  var menuEntries = [ 


    {name: "New Workout", functionName: "newWorkout"}


  ];


  ss.addMenu("Workout", menuEntries);


}
<div></div>



PHP Binary Search Tree - Part 2

This week the Binary Search Tree has been altered to find more than one object and also to allow the Binary Search Tree to be iterated through as if it was an array (so now it can be used in a foreach loop). This uses the built in PHP ArrayIterator, if you would like your own iterator that is included at the very bottom of the page and is named MyIterator.php

Today were goin to start with a little concrete class myBST that extends the binarySearchTree class and defines what is equal and what is, then a PHP_UNIT testing class to test the Binary Search Tree and finally the revised Binary Search Tree code. I have not altered the Binary Search Tree Node class, so I have not included it's code today.

 

myBST



<?php
/**
 * Description
 *
 * PHP Version 5
 *
 * @package PHPEssentials
 * @subpackage tree
 */

/**
 * A concrete BST to test with
 */
class myBST extends binarySearchTree
{
    protected function equalTo($prop1, $prop2)
    {
        return $prop1->me == $prop2->me;
    }
    
    protected function greaterThan($prop1, $prop2)
    {
        return $prop1->me > $prop2->me;
    }
}


 

Binary Search Tree Test w/ PHP_UNIT



<?php

require_once "binarySearchTreeNode.php";
require_once "binarySearchTree.php";
require_once "myBST.php";

/**
 * Class to Unit Test the myBST class (extends the binarySearchTree class)
 *
 * @package PHPEssentials
 * @subpackage UnitTests
 */
class bstTEST extends PHPUnit_Framework_TestCase {
    
    /**
     * Created a data set to be tested
     *
     * @return myBST
     */
    public function testInsertionSuccess()
    {
        $bst = new myBST();
        
        //expect the newly created object to be empty
        $this->assertEquals(0, $bst->length());

        //add item
        $item = new stdclass;
        $item->me = 5;
        $bst->insert($item);
        
        $this->assertEquals(1, $bst->length());
        
        //add greater item
        $item = new stdclass;
        $item->me = 6;
        $bst->insert($item);
        
        $this->assertEquals(2, $bst->length());
        
        //add equal item
        $item = new stdclass;
        $item->me = 6;
        $bst->insert($item);
        
        $this->assertEquals(3, $bst->length());
        
        //add lesser & equal
        $item = new stdclass;
        $item->me = 5;
        $bst->insert($item);
        
        $this->assertEquals(4, $bst->length());
        
        //add lesser
        $item = new stdclass;
        $item->me = 1;
        $bst->insert($item);
        
        $this->assertEquals(5, $bst->length());
        
        return $bst;
    }
    
    /**
     * Will attempt to find one or more objects
     *
     * @param myBST $tree the tree to be searched
     *
     * @depends testInsertionSuccess
     */
    public function testFindObjectSuccess($tree)
    {
        $item = new stdclass;
        $item->me = 6;
        
        $results = $tree->find($item);
        $this->assertEquals(2, count($results));
    }
    
    /**
     * Will attempt to find an object that has been added
     *
     * @param myBST $tree the tree to be searched
     *
     * @depends testInsertionSuccess
     */
    public function testFindOneObjectByPropertySuccess($tree)
    {
        $this->assertInstanceOf("stdclass", $tree->findOneByProperty("me", 1));
    }
    
    /**
     * Will attempt to find an object that has not been added
     *
     * @param myBST $tree
     *
     * @depends testInsertionSuccess
     */
    public function testFindOneObjectByPropertyFail($tree)
    {
        $this->assertFalse($tree->findOneByProperty("me", 7));
    }
    
}


Altered Binary Search Tree class



<?php
/**
 * @package PHPEssentials
 * @subpackage tree
 */
 
/**
 * A binary search tree. 
 *
 * A binary search tree that saves <= to the left and > to the right. The class
 * is abstract because it requires the usage of a greater than function that
 * will depend on the object
 *
 * @package PHPEssentials
 * @subpackage tree
 * @uses binarySearchTreeNode
 */
abstract class binarySearchTree implements IteratorAggregate
{
    /**
     * functions that return by reference report to the error log
     * if a value is passed that is not a variable, this avoids that
     */
    private $nullGuard = false;
    
    /**
     * The starting binarySearchTreeNode 
     */
    private $head = null;
    
    public function __construct()
    {
        $this->head = null;
        $this->parent = null;
        $this->child = null;
    }
    
    /**
     * A function created by the user, used to decide if a node has equal data
     */
    abstract protected function equalTo($prop1, $prop2);
    
    /**
     * A function created by the user, used to decide if a node goes right
     */
    abstract protected function greaterThan($prop1, $prop2);
    
    /**
     * inserts the given node
     *
     * @param binarySearchTreeNode $node
     *
     * @return bool
     */
    public function insert(&$item)
    {
        //make sure there is something to save
        if ( isset($item) ) {
            $node = new binarySearchTreeNode($item);
            
            //if the list is empty then add as head
            if (!isset($this->head)) {
                $this->head = $node;
                return true;
            }
            
            //call recursive function to add the node
            return $this->add($node, $this->head);
        }
        return false;
    }
    
    /**
     * recursive function to add new nodes
     *
     * @param binarySearchTreeNode $newNode The node to be added
     * @param binarySearchTreeNode $checkNode The current node being navigated
     *
     * @return bool
     */
    protected function add(&$newNode, &$checkNode)
    {
        
        //check that there are values to work with
        if (isset($newNode) && isset($checkNode)) {
            
            //if the new item is greater than, add it to the right
            if ( $this->greaterThan($newNode->getItem(), $checkNode->getItem()) ) {
                
                //if the right is empty, add on
                if ($checkNode->getRight() == NULL) {
                    $checkNode->setRight($newNode);
                    return true;
                }
                
                //if the right was not empty add to the right
                return $this->add($newNode, $checkNode->getRight());
            }
            
            //if the new item is <=, add it to the left
            
            //if the left is empty, add on
            if ($checkNode->getLeft() == NULL) {
                
                $checkNode->setLeft($newNode);
                return true;
            }
            
            //if the left was not empty add to the left
            return $this->add($newNode, $checkNode->getLeft());
            
        }
        
        //the program made it to far
        return false;
    }
    
    /**
     * Searches the BST for matching items
     *
     * @param mixed $item The needle
     *
     * @uses findAux()
     * @return array
     */
    public function &find($item)
    {
        //if there is a needle and a haystack
        if ( isset($item) && isset($this->head) ) {
            $result = array();
            $this->findAux($item, $this->head, $result);
            return $result;
        }
        
        //if there is no needle, or no haystack
        return $this->nullGuard;
    }
    
    protected function &findAux($item, &$current, &$results)
    {
        if (isset($item) && isset($current)) {
        
            $this->findAux($item, $current->getLeft(), $results);
            
            //if the item is in the current node
            if ( $this->equalTo($current->getItem(), $item) ) {
                $results[] = &$current->getItem();
            }
            
            $this->findAux($item, $current->getRight(), $results);
            
        }
        
        //could not find a match
        return $this->nullGuard;
    }
    
    
    /**
     * Searches the BST for a matching item
     *
     * @param mixed $item The needle
     *
     * @uses findOneAux()
     * @return mixed
     */
    public function &findOne($item)
    {
        //if there is a needle and a haystack
        if ( isset($item) && isset($this->head) ) {
            return $this->findOneAux($item, $this->head);
        }
        
        //if there is no needle, or no haystack
        return $this->nullGuard;
    }
    
    /**
     * Recursive Aux function that actually performs searches
     *
     * @param mixed $item The needle
     * @param binarySearchTreeNode The current, but ever changing haystack
     *
     * @param binarySearchTreeNode
     */
    protected function &findOneAux(&$item, &$current)
    {
        if (isset($item) && isset($current)) {
        
            //if the item is in the current node
            if ( $this->equalTo($current->getItem(), $item) ) {
                return $current->getItem();
            }
            
            //see if the path is to the right
            if ( $this->greaterThan($item, $current->getItem()) ) {
                return $this->findOneAux($item, $current->getRight());
            }
            
            return $this->findOneAux($item, $current->getLeft());
            
        }
        
        //could not find a match
        return $this->nullGuard;
    }
    
    
    /**
     * Search for a object by property
     *
     * @param string $property The object resource to search
     * @param string $needle The object resource to search for
     *
     * @return binarySearchTreeNode
     */
    public function &findOneByProperty($property, $needle)
    {
        //make surethere is a property, a needle, and a haystack
        if (isset($property) && isset($needle) && isset($this->head)) {
            return $this->findOneByPropertyAux($property, $needle, $this->head);
        }
        //if there is nothing to search through or for
        return $this->nullGuard;
    }
    
    
    /**
     * Recursive Aux function that actually performs searches by property
     *
     * @param string $property The property of the object to be searched for
     * @param mixed $needle The needle
     * @param binarySearchTreeNode The current, but ever changing haystack
     *
     * @param mixed
     */
    protected function &findOneByPropertyAux($property, $needle, &$current)
    {
        /* check that there is a object property to seach, a needle to search
           for and a haystack */
        if (isset($property) && isset($needle) && isset($current)) {
            //check that the object has the property and then checks with needle
            if ( 
                $current->getItem()->$property == $needle
                ) {
                //if this is the item being searched for
                return $current->getItem();
            }
            
            //if the needle would have been added to the right, follow
            if (
                $needle > $current->getItem()->$property
                ) {
                return $this->findOneByPropertyAux(
                    $property, 
                    $needle, 
                    $current->getRight()
                    );
            }
            
            //if the needle would have been added to the left, follow
            if (
                $needle <= $current->getItem()->$property
                ) {
                return $this->findOneByPropertyAux(
                    $property, 
                    $needle, 
                    $current->getLeft()
                    );
            }
            
            //if it makes it here (it shouldn't) then it has failed
            return $this->nullGuard;
        }
        //if there is nothing to search through or possibly for
        return $this->nullGuard;
    }
    
    /**
     * Will return the number of items in the tree
     */
    public function length()
    {
        $i = 0;
        $this->lengthAux($this->head, $i);
        return $i;
    }
    
    private function lengthAux(&$node, &$count)
    {
        if (isset($node) && isset($count)) {
            //count left
            $this->lengthAux($node->getLeft(), $count);
            
            //count item
            $count++;
            
            //count right
            $this->lengthAux($node->getRight(), $count);
            
            return true;
        }
        
        return false;
    }
    
    /**
     * Prints items left to right
     */
    public function toArray()
    {
        $results = array();
        $this->toArrayAux($this->head, $results);
        return $results;
    }
    
    /**
     * Finds and returns results to print
     */
    private function toArrayAux(&$node, &$results)
    {
        if (isset($node) && isset($results)) {
            //add left
            $this->toArrayAux($node->getLeft(), $results);
            
            //add item
            $results[] = $node->getItem();
            
            //add right
            $this->toArrayAux($node->getRight(), $results);
            
            return true;
        }
        
        return false;
    }
    
    // Required definition of interface IteratorAggregate
    public function getIterator() {
        return new ArrayIterator($this->toArray());
    }
   
}


The custom iterator (replace "return new ArrayIterator" with "return new MyIterator" in the Binary Search Tree class



<?php
class MyIterator implements Iterator
{
    private $var = array();

    public function __construct($array)
    {
        if (is_array($array)) {
            $this->var = $array;
        }
    }

    public function rewind()
    {
        reset($this->var);
    }
  
    public function current()
    {
        $var = current($this->var);
        return $var;
    }
  
    public function key() 
    {
        $var = key($this->var);
        return $var;
    }
  
    public function next() 
    {
        $var = next($this->var);
        return $var;
    }
  
    public function valid()
    {
        $key = key($this->var);
        $var = ($key !== NULL && $key !== FALSE);
        return $var;
    }

}


My First jEdit Micro

Today I installed the jEdit plugin for Snipplr and it is very nice, allows me to post to Snipplr.com from the editor itself, allows me to search and insert my uploaded snippets into the current file. There was just one issue, everytime I inserted a code snippet (uploaded directly at Snipplr.com or from the jEdit plugin) into my file all greater than, less than, and double quote symblos were inserted as HTML safe characters &gt; &lt; &quote;

As you can imagine it was somewhat annoying because having a code snippet library will be very usefull, but if I have to replace all this text it will defeat the purpose of having the code so readily available. So I had jEdit record a macro that would Find and Replace &gt; with ">", &lt; with "<" and &quote with '"' and after stopping the recording and giving it a name, jEdit gave me the macro code required to perform the operations automatically.

Now I insert a Code Snippet from Snipplr.com and run my macro (just two clicks) and I am back on my way.

The Macro:



// This is a recorded macro. First, check over the
// commands to make sure this is what you intended. Then,
// save this buffer, and the macro should appear in the
// Macros menu.
SearchAndReplace.setSearchString("&quot;");
SearchAndReplace.setReplaceString("\"");
SearchAndReplace.setBeanShellReplace(false);
SearchAndReplace.setIgnoreCase(false);
SearchAndReplace.setRegexp(false);
SearchAndReplace.setSearchFileSet(new CurrentBufferSet());
SearchAndReplace.replaceAll(view);
SearchAndReplace.setSearchString("&lt;");
SearchAndReplace.setReplaceString("<");
SearchAndReplace.setBeanShellReplace(false);
SearchAndReplace.setIgnoreCase(false);
SearchAndReplace.setRegexp(false);
SearchAndReplace.setSearchFileSet(new CurrentBufferSet());
SearchAndReplace.replaceAll(view);
SearchAndReplace.setSearchString("&gt;");
SearchAndReplace.setReplaceString(">");
SearchAndReplace.setBeanShellReplace(false);
SearchAndReplace.setIgnoreCase(false);
SearchAndReplace.setRegexp(false);
SearchAndReplace.setSearchFileSet(new CurrentBufferSet());
SearchAndReplace.replaceAll(view);


 

Filename: Snipplr_Fix_-_Insert.bsh

Installed in: ~/.jedit/macros/

PHP Binary Search Tree - Part 1

Today I wrote a basic PHP Binary Search tree that will allow the insertion of mixed data types.

This abstract class follows the rules of a left to right Binary Search where the right node has content greater than the current node and the left node has content greater than or equal to that of the current node.

When implementing the class it is required that a greaterThan and an equalTo method are implemented for insertion and retrieval to know what is considered greater than and equal. If you wish to have the default PHP types stored in the BST then your methods should just perform the default PHP comparisons, but for custom object you are able to be precise with which property of the object is to be considered for comparison.

The current implementation will retrieve only one item when find() or findByProperty() is called so in Part 2 find() will change to findOne() and there will be a new method find() that will return all items that meet the criteria in an array. Also I will add Object Iteration so that the Binary Search Tree can be used in a foreach statement.

Binary Search Tree Node Class:



<?php
/**
 * A sub component of the binarySearchTree class
 *
 * The data type stored by the binarySearchTree
 *
 * @package PHPEssentials
 * @subpackage tree
 */
class binarySearchTreeNode
{
    /**
     * The object or basic type to be stored
     */
    private $item = NULL;
    
    /**
     * The node to be stored to the left
     */
    private $leftNode = NULL;
    
    /**
     * The node to be stored to the right
     */
    private $rightNode = NULL;
    
    /**
     * Sets the values for all variables when the object is initiated
     *
     * @param $item The item to be stored, passed by reference
     * @param $left The object to be stored at the left of this object
     * @param $right The object to be stored at the right of this object
     *
     * @return binarySearchTreeNode
     */
    public function __construct(&$item, &$left = NULL, &$right = NULL)
    {
        $this->setItem($item);
        $this->setLeft($left);
        $this->setRight($right);
    }
    
    /**
     * sets the object item to be the item passed
     *
     * @param $item The item to be stored, passed by reference
     *
     * @return bool
     */
    public function setItem(&$item)
    {
        if ( isset($item) ) {
            $this->item = $item;
            return true;
        }
        return false;
    }
    
    /**
     * sets the next left node
     *
     * @param binarySearchTreeNode $left
     *
     * @return bool
     */
    public function setLeft(&$left)
    {
        if ( isset($left) ) {
            $this->leftNode = $left;
            return true;
        }
        return false;
    }
    
    /**
     * sets the next right node
     *
     * @param binarySearchTreeNode $right
     *
     * @return bool
     */
    public function setRight(&$right)
    {
        if ( isset($right) ) {
            $this->rightNode = $right;
            return true;
        }
        return false;
    }
    
    public function &getItem()
    {
        return $this->item;
    }
    
    public function &getLeft()
    {
        return $this->leftNode;
    }
    
    public function &getRight()
    {
        return $this->rightNode;
    }
}


 

Binary Search Tree Class:



<?php
 
/**
 * A binary search tree. 
 *
 * A binary search tree that saves <= to the left and > to the right. The class
 * is abstract because it requires the usage of a greater than function that
 * will depend on the object
 *
 * @package PHPEssentials
 * @subpackage tree
 * @uses binarySearchTreeNode
 */
abstract class binarySearchTree
{
    /**
     * functions that return by reference report to the error log
     * if a value is passed that is not a variable, this avoids that
     */
    private $nullGuard = false;
    
    /**
     * The starting binarySearchTreeNode 
     */
    private $head = null;
    
    public function __construct()
    {
        $this->head = null;
    }
    
    /**
     * A function created by the user, used to decide if a node has equal data
     */
    abstract protected function equalTo($prop1, $prop2);
    
    /**
     * A function created by the user, used to decide if a node goes right
     */
    abstract protected function greaterThan($prop1, $prop2);
    
    /**
     * inserts the given node
     *
     * @param binarySearchTreeNode $node
     *
     * @return bool
     */
    public function insert(&$item)
    {
        //make sure there is something to save
        if ( isset($item) ) {
            $node = new binarySearchTreeNode($item);
            
            //if the list is empty then add as head
            if (!isset($this->head)) {
                $this->head = $node;
                return true;
            }
            
            //call recursive function to add the node
            return $this->add($node, $this->head);
        }
        return false;
    }
    
    /**
     * recursive function to add new nodes
     *
     * @param binarySearchTreeNode $newNode The node to be added
     * @param binarySearchTreeNode $checkNode The current node being navigated
     *
     * @return bool
     */
    protected function add(&$newNode, &$checkNode)
    {
        
        //check that there are values to work with
        if (isset($newNode) && isset($checkNode)) {
            
            //if the new item is greater than, add it to the right
            if ( $this->greaterThan($newNode->getItem(), $checkNode->getItem()) ) {
                
                //if the right is empty, add on
                if ($checkNode->getRight() == NULL) {
                    $checkNode->setRight($newNode);
                    return true;
                }
                
                //if the right was not empty add to the right
                return $this->add($newNode, $checkNode->getRight());
            }
            
            //if the new item is <=, add it to the left
            
            //if the left is empty, add on
            if ($checkNode->getLeft() == NULL) {
                
                $checkNode->setLeft($newNode);
                return true;
            }
            
            //if the left was not empty add to the left
            return $this->add($newNode, $checkNode->getLeft());
            
        }
        
        //the program made it to far
        return false;
    }
    
    
    /**
     * Searches the BST for the item
     *
     * @param mixed $item The needle
     *
     * @return mixed
     */
    public function &find($item)
    {
        //if there is a needle and a haystack
        if ( isset($item) && isset($this->head) ) {
            return $this->findAux($item, $this->head);
        }
        
        //if there is no needle, or no haystack
        return $this->nullGuard;
    }
    
    /**
     * Recursive Aux function that actually performs searches
     *
     * @param mixed $item The needle
     * @param binarySearchTreeNode The current, but ever changing haystack
     *
     * @param binarySearchTreeNode
     */
    protected function &findAux($item, &$current)
    {
        if (isset($item) && isset($current)) {
        
            //if the item is in the current node
            if ( $this->equalTo($current->getItem(), $item) ) {
                return $current->getItem();
            }
            
            //see if the path is to the right
            if ( $this->greaterThan($item, $current->getItem()) ) {
                return $this->findAux($item, $current->getRight());
            }
            
            return $this->findAux($item, $current->getLeft());
            
        }
        
        //could not find a match
        return $this->nullGuard;
    }
    
    
    /**
     * Search for a object by property
     *
     * @param string $property The object resource to search
     * @param string $needle The object resource to search for
     *
     * @return binarySearchTreeNode
     */
    public function &findByProperty($property, $needle)
    {
        //make surethere is a property, a needle, and a haystack
        if (isset($property) && isset($needle) && isset($this->head)) {
            return $this->findByPropertyAux($property, $needle, $this->head);
        }
        //if there is nothing to search through or for
        return $this->nullGuard;
    }
    
    
    /**
     * Recursive Aux function that actually performs searches by property
     *
     * @param string $property The property of the object to be searched for
     * @param mixed $needle The needle
     * @param binarySearchTreeNode The current, but ever changing haystack
     *
     * @param mixed
     */
    protected function &findByPropertyAux($property, $needle, &$current)
    {
        /* check that there is a object property to seach, a needle to search
           for and a haystack */
        if (isset($property) && isset($needle) && isset($current)) {
            //check that the object has the property and then checks with needle
            if ( 
                $current->getItem()->$property == $needle
                ) {
                //if this is the item being searched for
                return $current->getItem();
            }
            
            //if the needle would have been added to the right, follow
            if (
                $needle > $current->getItem()->$property
                ) {
                return $this->findByPropertyAux(
                    $property, 
                    $needle, 
                    $current->getRight()
                    );
            }
            
            //if the needle would have been added to the left, follow
            if (
                $needle <= $current->getItem()->$property
                ) {
                return $this->findByPropertyAux(
                    $property, 
                    $needle, 
                    $current->getLeft()
                    );
            }
            
            //if it makes it here (it shouldn't) then it has failed
            return $this->nullGuard;
        }
        //if there is nothing to search through or possibly for
        return $this->nullGuard;
    }
    
}


 


?>


Example Implementation 1:

This implementation is for when saving standard PHP Data Types like integers and strings



<?php


class BST extends binarySearchTree
{
    protected function equalTo($prop1, $prop2)
    {
        return $prop1 = $prop2;
    }
    
    protected function greaterThan($prop1, $prop2)
    {
        return $prop1 > $prop2;
    }
}


 


?>


Example Implementation 2:

This implementation will sort based on the objects property "me"



<?php


class myBST extends binarySearchTree
{
    protected function equalTo($prop1, $prop2)
    {
        return $prop1->me = $prop2->me;
    }
    
    protected function greaterThan($prop1, $prop2)
    {
        return $prop1->me > $prop2->me;
    }
}


?>


 

 

 

PHP Script for Ubuntu Background Rotation

Today I wrote a PHP script that will output the contents for an XML file that can be added to Ubuntu's Appearance -> Background Settings. The script finds all files withint a folder that do not contain PHP or XML (changed so that the extention cannot be .php or .xml) and it assumes that any other files in the folder are images. Then it creates the XML required by the Background Settings to rotate through images.

Once the images are together in a folder, make a file called makeRotationXML.php and copy the PHP code below into it. Now define APP_ROOT to have the path to your folder containing the background images. For instance define APP_ROOT as "/home/username/Pictures/Backgrounds".

Now open a terminal and navigate to the folder and run the command "php makeRotationXML.php > rotation.xml" this will create an xml file containing a list of images to be rotated through.

When you have the rotation.xml file, open Appearances from System -> Preferences and go to the Background Tab. Now you will see the default backgrounds provided by Ubuntu or backgrounds you have added. In the lower right click the "Add.." button and select rotation.xml file. By default only images and folders will be shown in the dialog to select a background file. In the lower right there is a drop down that has "imags" selected. click it and select "All Files" and then select the rotation.xml file and click open.

Now the collection of images has been added to the background list and you can select it as the new background.



<?php
/**
 * This file will print the XML required to rotate background images in Ubuntu
 */

/**
 * Defines where the script is installed (no trailing /)
 */
define("APP_ROOT", "/path/to/folder");
 
/**
 * The number of seconds the transition from image to image should take
 */
define("TRANSITION_TIME", 5.0);
 
/**
 * The number of seconds each image should show
 */
define("DURRATION", 45.0);

//make a directory handler for the application root
$dh = opendir(APP_ROOT);

//check all folder resources
while (($file = readdir($dh)) !== false) {
    //check that the resource is a file not a link and that it is not PHP or XML
    if ( is_file(APP_ROOT .'/'. $file) && !preg_match('(php|xml)', $file) ) {
        $files[] = $file;
    }
}

//test resources allowed
//print_r($files);

//clode the directory handler
closedir($dh);

?>

<background>
    <starttime>
    <year><?php echo date('Y'); ?></year>
    <month><?php echo date('m'); ?></month>
    <day><?php echo date('d'); ?></day>
    <hour>00</hour>
    <minute>00</minute>
    <second>00</second>
    </starttime>

<?php
for ($i = 0; $i < count($files); $i++) {
    
    $currentFile = $files[$i];
    
    if ( $i+1 < count($files) ) {
        $nextFile = $files[$i+1];
    } else {
        $nextFile = $files[0];
    }
    
    ?>
    
    <static>
    <duration><?php echo DURRATION; ?></duration>
    <file><?php echo APP_ROOT .'/'.$currentFile; ?></file>
    </static>
    
    <transition>
    <duration><?php echo TRANSITION_TIME; ?></duration>
    <from><?php echo APP_ROOT .'/'.$currentFile; ?></from>
    <to><?php echo APP_ROOT .'/'.$nextFile; ?></to>
    </transition>
    
    <?php
}

?>

</background>


Virus Protection

I work at a computer support facility where students bring in their computers to have hardware installed, OS upgrades, data backed up, and other similar tasks. Probably the most common work we do is Virus removal; Sometimes the virus goes nicely and sometimes the system requires an install. What surprised me was the number of customers who have virus protection, and still come in with Viruses.

Each customer then asks the all powerful question: "What is the best Anti-Virus software?".

There are two problems with this question. First it assumes that the support provider (whomever is in that day) has tried all the Anti-Virus systems when in reality the average person tries one or two and stick with the one that works for us - The "if its not broken don't fix it" mentality really takes effect with Virus Protection. So our recommendation will be the best of the two Anti-Viruses an individual tries. Secondly, Anti-Virus software can be considered a never ending chase where the Anti-Virus companies have a 50 jail ball tied around their ankle and are trying to catch a few thousand of the Viruses who are toying around on their new mountain bikes.

I do not think Virus Software is useless, I actually install it first thing on a new system. But I do not think of it as my first line of defense, its more of a response tool, helping to remove infections if they occur. They probably stop a lot more than I know of and thats great, but to me the first line is always the user.

The user decides what to install, and what sites to visit. The Virus Protection responds to a limited (its a lot, but still there are only so many it can handle) number of known Virus' and a number of known things Virus' will attempt to do. When a new virus comes out and you are lucky enough to receive a copy, your Anti-Virus may not be able to recognize it as a threat and there for the Virus will make it to your system often unhindered. My recommendation for "Best Anti-Virus": a knowledgable user, who doesn't install all the software (plugin, application, app) they ever see or think of and who checks the address of a link before clicking it.

My opinion of another opinion

Today was going well, made it to classes on time, the lectures had been keeping my attention, all things that make a day a little more pleasant. Then the guy siting next to me in my computer science class was arrogant enough to declare the end of JavaScript with the statement: "Javascript is dead, I just don't see it anymore". Now my day is going fantastic, nothing raises the spirits like a person blindly stating unfounded opinion as fact.

For a moment I considered pointing out a few faults in his opinion. That would have lead to a discussion taking us farther off topic than we already were (the lecture was about c++ searching algorithms and somehow we were talking about JavaScript) and remembering how much I dislike getting off topic in class, I decided to keep my thoughts to myself. Becoming a hypocrite was not appealing to me. 

What basis is there to say that JavaScript is dead? I tried seeing how he came to his conclusion, and all I was able to see was that JavaScript is being used less to create content, as in document.write() for every line of the webpage. That however is still not enough to conclusively state that JavaScript is dead. My second thought as to how his opinion was formulated is what if he doesn't understand the term AJAX, that would explain the belief. All over the web the term AJAX is being tossed around, possibly he believes that it is replacing Javascript. Continuing on I am going to assume that is not the case, as a coder you hear ajax and google it....instantly you are able to read that Ajax uses Javascript heavily.

Possibly it could be the coming of HTML5 (the golden child of the press ever scenes it was announced that the iPhone would not support flash), this thoughts has two issues generally glanced over: 1. HTML5 requires more JavaScript because of features like Canvas. 2: WC3 says that HTML5 standard will not be completed until 2012. Humoring the idea that with HTML5, JavaScript will stop being the mainstream DOM manipulator of the internet, there is still the issue of completing the standard and then magically getting the browsers to all follow the same standard. These issues makes HTML5 a long shot for removing JavaScript from our daily internet lives.

For the opposing mindset, the belief that JavaScript is still a language developers should continue to use, I would simply like to bring into evidence:

1. Google
2. Facebook
3. Wikipedia

These three companies arguably receive the most internet traffic in the United States, all use JavaScript. The most recognizable use of JavaScript would be Google Instant or Google Preview.

As long as we are making unfounded opinions based off personal experience, I would like to quote "Javascript is dead like English is dead"

Thanks for the good day though