Permalink
Browse files

developer test

  • Loading branch information...
1 parent 9b3da8b commit 7c9a3dd2a46dfbc9fe80cb693703cb8029386e5d lcaraccioli committed Apr 8, 2012
Showing with 6,042 additions and 0 deletions.
  1. BIN PHP_developer_test/PHP_developer_test/PHP Developer Test.pdf
  2. +40 −0 PHP_developer_test/PHP_developer_test/task_1/index.php
  3. +171 −0 PHP_developer_test/PHP_developer_test/task_3/index.php
  4. +7 −0 PHP_developer_test/PHP_developer_test/task_4/4a.sql
  5. +27 −0 PHP_developer_test/PHP_developer_test/task_4/4b.sql
  6. BIN PHP_developer_test/PHP_developer_test/task_5_feature_test/images/add_to_cart.png
  7. BIN PHP_developer_test/PHP_developer_test/task_5_feature_test/images/checkout.png
  8. BIN PHP_developer_test/PHP_developer_test/task_5_feature_test/images/mousepad.png
  9. BIN PHP_developer_test/PHP_developer_test/task_5_feature_test/images/mug.png
  10. BIN PHP_developer_test/PHP_developer_test/task_5_feature_test/images/polo.png
  11. BIN PHP_developer_test/PHP_developer_test/task_5_feature_test/images/tshirt.png
  12. +13 −0 PHP_developer_test/PHP_developer_test/task_5_feature_test/index.php
  13. BIN PHP_developer_test/PHP_developer_test/task_5_feature_test/mockup.png
  14. +5,491 −0 PHP_developer_test/PHP_developer_test/task_5_feature_test/mootools.js
  15. +19 −0 PHP_developer_test/PHP_developer_test/task_5_feature_test/processAHR.php
  16. +1 −0 PHP_developer_test/PHP_developer_test/task_5_feature_test/store.css
  17. +2 −0 PHP_developer_test/PHP_developer_test/task_5_feature_test/store.js
  18. +52 −0 PHP_developer_test/PHP_developer_test/task_6/SudokuRenderer.php
  19. +89 −0 PHP_developer_test/PHP_developer_test/task_6/SudokuVerifier.php
  20. +12 −0 PHP_developer_test/PHP_developer_test/task_6/exception-suduko-board.php
  21. +94 −0 PHP_developer_test/PHP_developer_test/task_6/index.php
  22. +12 −0 PHP_developer_test/PHP_developer_test/task_6/invalid-suduko-board.php
  23. +12 −0 PHP_developer_test/PHP_developer_test/task_6/valid-suduko-board.php
Binary file not shown.
@@ -0,0 +1,40 @@
+<?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
+*/
+$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();
+
+//generate all subsets
+for ($j=0; $j < count($set); $j++){
+ for ($i=$j; $i < count($set); $i++){
+ $subset = implode('',array_slice($set, $j, $i-$j+1));
+ $subsets[] = $subset;
+ }
+}
+
+//count subsets
+$subsetCounts = array();
+foreach($subsets as $subset){
+ //to avoid notices... alternatively change error reporting to ignore notices depending on conventions
+ $subsetCounts[$subset] = isset($subsetCounts[$subset])?$subsetCounts[$subset] + 1:1;
+}
+
+//determine the largest subset that is repeated
+$largestRepeatingSubset = '';
+foreach($subsetCounts as $subset =>$count){
+ if ($count > 1){
+ if (strlen($subset) > strlen($largestRepeatingSubset)){
+ $largestRepeatingSubset = $subset;
+ }
+ }
+}
+echo('<pre>');
+var_dump($largestRepeatingSubset);
+echo('</pre>');
@@ -0,0 +1,171 @@
+<?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>... */
+class LC_Tool_Html{
+ /**
+ * @var string $_html the raw html being processed
+ */
+ protected $_html = null;
+
+ /**
+ * @var array $_tokens the array of tokens representing the raw html
+ */
+ protected $_tokens = null;
+
+ /**
+ * Used to configure the object with an html formatted string which will be used for further processing
+ */
+ public function setHtml($html){
+ $this->_html = $html;
+ $this->_tokens = null;
+ return $this;
+ }
+
+ /**
+ * Used to get the html that was previously set by a call to {@link setHtml()}
+ */
+ public function getHtml(){
+ if (is_null($this->_html)){
+ throw new Exception ("You must first specify an html string to work with using ".__CLASS__."::setHtml'");
+ }
+ return $this->_html;
+ }
+
+ /**
+ * Used to get the tokenized version of the raw html currently being processed.
+ */
+ public function getTokens(){
+ if (is_null($this->_tokens)){
+ $this->_tokens = $this->_tokenize();
+ }
+ return $this->_tokens;
+ }
+
+ /**
+ * Protected method to do the actual tokenization of the html
+ */
+ 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
+ */
+ public function isOpenTag($token){
+ return preg_match('/(<\s*[^\/][^>]*>)/', $token);
+ }
+
+ /**
+ * Used to check if a particular string is an html closing tag
+ */
+ public function isCloseTag($token){
+ return preg_match('/(<\s*\/[^>]*>)/', $token);
+ }
+
+ /**
+ * Used to check if a particular string is an html tag. This is useful for detecting html self closing tags which will return true on this method, but false on {@link isOpenTag()} and {@link isCloseTag()}
+ */
+ public function isTag($token){
+ return preg_match('/(<[^>]*>)/', $token);
+ }
+
+ /**
+ * Truncates a string after the closest whole word to the truncation length, but also safely retains HTML code. By default it will append ellipses to the the string
+ *
+ * Conceptually this method uses a stack to keep up with open tags pushing and popping to that stack as open and close tags are encountered. This is a nieve
+ * implementation that expects valid html to work on. Therefore if the original html has miss matched tags then this method will reflect those errors in the original
+ * version of the html.
+ *
+ * @param int $truncateLength the length that the textual representation of the html should be truncated.
+ * @param string $moreIndicator a string to append to the textual representation of the truncated html.
+ * @return string html representing the truncated textual version of the original html
+ */
+ public function truncate($truncateLength = 12, $moreIndicator = '...'){
+ $tokens = $this->getTokens();
+ $openTagStack = array();
+ $truncatedHtml = '';
+ $textualLength = 0;
+ foreach($tokens as $token){
+ //if this token is an opening tag then push it to the open tag stack and append it to the truncated html
+ if ($this->isOpenTag($token)){
+ array_unshift($openTagStack, $token);
+ $truncatedHtml .= $token;
+ }
+ //if this token is a closing tag then pop from the open tag stack and append it to the truncated html
+ else if ($this->isCloseTag($token)){
+ $openingTag = array_shift($openTagStack);
+ $truncatedHtml .= $token;
+ }
+ //if this token is a tag, but not opening or closing we assume it's self closing tag then append this token to the truncatedHtml and continue
+ else if ($this->isTag($token)){
+ $truncatedHtml .= $token;
+ }
+ //if this token is a text node then first divide it into individual words. Append each of the words one at a time until the truncate length is passed or all of the words for this token have been added.
+ else {
+ foreach(preg_split('/([\s]+)/', $token, null, PREG_SPLIT_DELIM_CAPTURE) as $word){
+ $truncatedHtml .= $word;
+ $textualLength += strlen($word);
+ if ($textualLength > $truncateLength){
+ break;
+ }
+ }
+ if ($textualLength > $truncateLength){
+ break;
+ }
+ }
+ }
+ $truncatedHtml .= $moreIndicator;
+ foreach ($openTagStack as $token){
+ preg_match('/<\s*([^\s>]*)/', $token, $matches);
+ $tagName = $matches[1];
+ $truncatedHtml .= "</$tagName>";
+ }
+
+ return $truncatedHtml;
+ }
+
+}
+
+/*
+* A set of test cases for manually inspecting the output of the LC_Tool_Html::truncate method
+*/
+$testCases = array(
+ array('length'=>1000000,'html'=>'<div >Go <a class="link" href="#store">to <span>our store</span></a> or take our <a href="../PHP Developer Test.pdf"> php developer test </a> <img src="" /> <span> <strong>thanks for visiting</strong></ span></div>')
+ ,array('length'=>29,'html'=>'<div >Go <a class="link" href="#store">to <span>our store</span></a> or take our <a href="../PHP Developer Test.pdf"> php developer test </a> <img src="" /> <span> <strong>thanks for visiting</strong></ span></div>')
+ ,array('length'=>28,'html'=>'<div >Go <a class="link" href="#store">to <span>our store</span></a> or take our <a href="../PHP Developer Test.pdf"> php developer test </a> <img src="" /> <span> <strong>thanks for visiting</strong></ span></div>')
+ ,array('length'=>23,'html'=>'<div >Go <a class="link" href="#store">to <span>our store</span></a> or take our <a href="../PHP Developer Test.pdf"> php developer test </a> <img src="" /> <span> <strong>thanks for visiting</strong></ span></div>')
+ ,array('length'=>22,'html'=>'<div >Go <a class="link" href="#store">to <span>our store</span></a> or take our <a href="../PHP Developer Test.pdf"> php developer test </a> <img src="" /> <span> <strong>thanks for visiting</strong></ span></div>')
+ ,array('length'=>21,'html'=>'<div >Go <a class="link" href="#store">to <span>our store</span></a> or take our <a href="../PHP Developer Test.pdf"> php developer test </a> <img src="" /> <span> <strong>thanks for visiting</strong></ span></div>')
+ ,array('length'=>1,'html'=>'<div >Go <a class="link" href="#store">to <span>our store</span></a> or take our <a href="../PHP Developer Test.pdf"> php developer test </a> <img src="" /> <span> <strong>thanks for visiting</strong></ span></div>')
+ ,array('length'=>0,'html'=>'<div >Go <a class="link" href="#store">to <span>our store</span></a> or take our <a href="../PHP Developer Test.pdf"> php developer test </a> <img src="" /> <span> <strong>thanks for visiting</strong></ span></div>')
+ ,array('length'=>-1,'html'=>'<div >Go <a class="link" href="#store">to <span>our store</span></a> or take our <a href="../PHP Developer Test.pdf"> php developer test </a> <img src="" /> <span> <strong>thanks for visiting</strong></ span></div>')
+ ,array('length'=>100000, 'html'=>'This is my <span class="bold">bold</span> string!' )
+ ,array('length'=>15, 'html'=>'This is my <span class="bold">bold</span> string!' )
+ ,array('length'=>14, 'html'=>'This is my <span class="bold">bold</span> string!' )
+ ,array('length'=>9, 'html'=>'This is my <span class="bold">bold</span> string!' )
+ ,array('length'=>1, 'html'=>'This is my <span class="bold">bold</span> string!' )
+ ,array('length'=>0, 'html'=>'This is my <span class="bold">bold</span> string!' )
+ ,array('length'=>-1, 'html'=>'This is my <span class="bold">bold</span> string!' )
+);
+
+foreach ($testCases as $testCase){
+ $htmlTool = new LC_Tool_Html();
+ $htmlTool->setHtml($testCase['html']);
+ echo "<fieldset>";
+ echo("<h3>Truncation Length ({$testCase['length']})</h3>");
+ echo("<h3>Raw Html</h3>");
+ echo('<pre>');
+ echo htmlentities($testCase['html']);
+ echo('</pre>');
+ echo("<h3>Truncated Html</h3>");
+ echo('<pre>');
+ echo htmlentities($htmlTool->truncate($testCase['length']));
+ echo('</pre>');
+ echo("<h3>Result</h3>");
+ echo ($htmlTool->truncate($testCase['length']));
+ echo "</fieldset>";
+
+}
@@ -0,0 +1,7 @@
+/*assuming that the 4a task description of "userid" actually referes to the jobs.user_id field*/
+
+SELECT categories.category_name, count(*)
+FROM `jobs`
+LEFT JOIN categories on categories.id = jobs.cat_id
+WHERE jobs.user_id = 94
+GROUP BY categories.id
@@ -0,0 +1,27 @@
+/* assuming that only one category is desired (based on "the category") being used even if multiple categories, with the most number of jobs, share the same job count */
+SELECT category_name FROM (
+ SELECT cat_id, count(*) job_count
+ FROM `jobs`
+ GROUP BY cat_id
+ ORDER BY job_count desc
+ LIMIT 1
+) job_counts
+LEFT JOIN categories on categories.id = job_counts.cat_id
+
+
+/* assuming that all categories that have the most number of jobs should be returned in the result set */
+SELECT counted_categories.category_name FROM (
+ SELECT categories.category_name, count(*) job_count, max_job_count.job_max max_job_count
+ FROM `jobs`
+ LEFT JOIN categories on categories.id = jobs.cat_id
+ LEFT JOIN (
+ SELECT max(job_count) job_max FROM (
+ SELECT count(*) job_count
+ FROM `jobs`
+ GROUP BY cat_id
+ ) counted_job_categories
+ ) max_job_count
+ ON 1=1
+ GROUP BY jobs.cat_id
+) counted_categories
+WHERE counted_categories.job_count = counted_categories.max_job_count
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,13 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
+<html>
+<head>
+ <link rel="stylesheet" href="store.css" type="text/css">
+ <script language="JavaScript" type="text/javascript" src="mootools.js"></script>
+ <script language="JavaScript" type="text/javascript" src="store.js"></script>
+</head>
+<body>
+
+<!-- html code here -->
+
+</body>
+</html>
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.

0 comments on commit 7c9a3dd

Please sign in to comment.