Browse files

update to add helpful comments

  • Loading branch information...
1 parent d2d4dc6 commit bb0d0b4cd15c830f76d995b3a3291a73c159063a lcaraccioli committed Apr 10, 2012
View
7 PHP_developer_test/PHP_developer_test/task_1/index.php
@@ -1,12 +1,11 @@
<?php
-/*
-* Start: 10:30pm
-* End: 11:50pm
+/**
+*
* Problem: Largest repeated subset. Find the longest repeated subset of array elements in given array. For
* example, for array('b','r','o','w','n','f','o','x','h','u','n','t','e','r','n','f','o','x','r','y','h','u','n') the longest repeated
* subset will be array('n','f','o','x').
*
-* Solution: Exaughstive exploration of subset space... generate all subsets, count them, determine the longest set that is repeated
+* Solution: Exhaustive exploration of subset space... generate all subsets, count them, determine the longest set that is repeated
*/
$set = array('b','r','o','w','n','f','o','x','h','u','n','t','e','r','n','f','o','x','r','y','h','u','n');
$subsets = array();
View
23 PHP_developer_test/PHP_developer_test/task_3/index.php
@@ -1,9 +1,11 @@
<?php
-/* Truncation with HTML. Write a function that truncates a string after the closest whole word, but also
-safely retains HTML code. The string should be appended with ellipsis (6) at the end of the truncation.
-For example, “This is my <span class=”bold”>bold</span> string!” with a truncation length of 12 should
-return “This is my <span class=”bold”>bold</span>... */
+/**
+* A class used to operate on html
+*
+* @author Lance Caraccioli
+* @copyright 2012
+*/
class LC_Tool_Html{
/**
* @var string $_html the raw html being processed
@@ -26,6 +28,9 @@ public function setHtml($html){
/**
* Used to get the html that was previously set by a call to {@link setHtml()}
+ *
+ * @return string html that is currently being operated on
+ * @throws Exception you must first specify html to be operated on using {@link setHtml}
*/
public function getHtml(){
if (is_null($this->_html)){
@@ -36,6 +41,8 @@ public function getHtml(){
/**
* Used to get the tokenized version of the raw html currently being processed.
+ *
+ * @return array tokens of html split on html tags.
*/
public function getTokens(){
if (is_null($this->_tokens)){
@@ -45,21 +52,27 @@ public function getTokens(){
}
/**
- * Protected method to do the actual tokenization of the html
+ * Method to do the actual tokenization of the html
+ *
+ * @return array tokens of html split on html tags.
*/
protected function _tokenize(){
return preg_split('/(<[^>]*>)/', $this->getHtml(), null, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
}
/**
* Used to check if a particular string is an html opening tag
+ *
+ * @param $token string text that may or may not be a closing text
*/
public function isOpenTag($token){
return preg_match('/(<\s*[^\/][^>]*>)/', $token);
}
/**
* Used to check if a particular string is an html closing tag
+ *
+ * @param $token string text that may or may not be a closing text
*/
public function isCloseTag($token){
return preg_match('/(<\s*\/[^>]*>)/', $token);
View
72 PHP_developer_test/PHP_developer_test/task_6/SudokuVerifier.php
@@ -1,10 +1,18 @@
<?php
+/**
+* Verfies that a particular sudoku model contains a correct solution to a sudoku puzzle
+*
+* @author Lance Caraccioli
+* @copyright 2012
+*/
class SudokuVerifier{
protected $_model = null;
- /*
+ /**
* This sets the model which sould be a 9X9 2d array. First we verify that it is in fact 9X9 then we massage the arrays to insure they are numerically indexed and that theire indexes are sequential
- * Doing this allows us to illiminate messy checks later in the methods that retrieve the rows, columns, and cells
+ * Doing this allows us to illiminate messy checks later in the methods that retrieve the rows, columns, and squares
+ *
+ * @param $model array 9X9 array representing a sudoku board
*/
public function setModel($model){
if (!is_array($model)){
@@ -29,43 +37,78 @@ public function setModel($model){
return $this;
}
+ /**
+ * Get the model previously set with {@link setModel()}. If no model was previously set then throws an exception indicating that a model must first be set before trying to retrieve it.
+ *
+ * @throws Exception
+ * @return array the sudoku board model
+ *
+ */
public function getModel(){
if (!$this->_model){
throw new Exception('You must first specify a 9X9 2d array that represents a board configuration of a sudoku board by passing that array to setModel');
}
return $this->_model;
}
+ /**
+ * Verify a sudoku model contains a valid solution to the sudoku puzzle.
+ *
+ * @return bool true if the model is valid false otherwise
+ */
public function verify(){
- //rows, columns, and cells can all be indexed 0-8 because there are 9 of each...
+ //rows, columns, and squares can all be indexed 0-8 because there are 9 of each...
for($i=0;$i<9; $i++){
if(!$this->verifyList($this->getRow($i))){
return false;
}
if (!$this->verifyList($this->getColumn($i))){
return false;
}
- if(!$this->verifyList($this->getCell($i))){
+ if(!$this->verifyList($this->getSquare($i))){
return false;
}
}
return true;
}
- public function verifyList($list){
+ /**
+ * Verify that a list of values contains the number 1 thru 9
+ *
+ * This method is nieve and used carelessly by extending classes will return true on lists that contain all the correct values and additional values as well.
+ * This argues that the method should be at least protected and that any extensions must be aware of it's nature.
+ *
+ * @TODO add sanity checks; to check if $list is an array, to check if $list is the correct size
+ * @param $list array is intended to be a list of 9 elements each representing the value of a single cell. These cells squares theoretically came from a row, column, or square
+ */
+ protected function verifyList($list){
$checklist = array_fill_keys(array(1,2,3,4,5,6,7,8,9), false);
foreach($list as $key=>$value){
$checklist[$value]=true;
}
return !in_array(false, $checklist);
}
+ /**
+ * Method used to retrieve a row specified by it's index
+ *
+ * @TODO based on the Elance request a single class should be build to verify a suduko board. Because of this I make the model a simple php array, however refactoring would necessarily move this method into a new class which represents the sudoku board's model as a decoupled entity.
+ * @param $index the index of the row which we are trying to retrieve. From the top to the bottom of the sudoku board is indexed 0-8
+ * @return $row array 9 elements representing the values stored in the row specified by $index
+ */
public function getRow($index){
$model = $this->getModel();
$row = $model[$index];
return $row;
}
-
+
+ /**
+ * Method used to retrieve a column specified by it's index
+ *
+ * @TODO based on the Elance request a single class should be build to verify a suduko board. Because of this I make the model a simple php array, however refactoring would necessarily move this method into a new class which represents the sudoku board's model as a decoupled entity.
+ * @param $index the index of the column which we are trying to retrieve. From the left to the right of the sudoku board is indexed 0-8
+ * @return $column array 9 elements representing the values stored in the column specified by $index
+ */
public function getColumn($index){
$model = $this->getModel();
$column = array();
@@ -74,16 +117,23 @@ public function getColumn($index){
}
return $column;
}
-
- public function getCell($index){
+
+ /**
+ * Method used to retrieve a square (3X3 grouping of cells) specified by it's index
+ *
+ * @TODO based on the Elance request a single class should be build to verify a suduko board. Because of this I make the model a simple php array, however refactoring would necessarily move this method into a new class which represents the sudoku board's model as a decoupled entity.
+ * @param $index the index of the square which we are trying to retrieve. From the left to the right of the sudoku board is indexed 0-8
+ * @return $column array 9 elements representing the values stored in the column specified by $index
+ */
+ public function getSquare($index){
$model = $this->getModel();
$rowMultiplier = floor(($index)/3) ;
$columnOffset = (($index)%3)*3;
- $cell = array();
+ $square = array();
for($row=0;$row<3;$row++){
$segment = array_slice($model[(3*$rowMultiplier)+$row], $columnOffset, 3);
- $cell = array_merge($cell, $segment);
+ $square = array_merge($square, $segment);
}
- return $cell;
+ return $square;
}
}

0 comments on commit bb0d0b4

Please sign in to comment.