2020-09-22

Sep 23 In-Class Exercise Thread.

Post your solutions here for the Sep 23 In-class Exercise.
Best, Chris
Post your solutions here for the Sep 23 In-class Exercise. Best, Chris
2020-09-23

-- Sep 23 In-Class Exercise Thread
<?php function traverse($tree, $value) { $counter = 0;
   foreach($tree as $var){
   
     if($var == $value){
     $counter++; 
      }
   }
   echo "There was ". ($counter) ." value(s) in the array that matched " . ($value) . "\n"; 
   return $counter;
  }
$a = ["hello", $w => [2,3], 2,3,3, 2.1003, "what"]; $b = 3; $c = "hello"; $d = 2.1003; $e = "Shouldnt be a match for this"; $f = [2,3]; $g = [2,"no match for this one, either"]; traverse($a,$b); traverse($a,$c); traverse($a,$d); traverse($a,$e); traverse($a,$f); traverse($a,$g);
(Edited: 2020-09-23)
<?php function traverse($tree, $value) { $counter = 0; foreach($tree as $var){ if($var == $value){ $counter++; } } echo "There was ". ($counter) ." value(s) in the array that matched " . ($value) . "\n"; return $counter; } $a = ["hello", $w => [2,3], 2,3,3, 2.1003, "what"]; $b = 3; $c = "hello"; $d = 2.1003; $e = "Shouldnt be a match for this"; $f = [2,3]; $g = [2,"no match for this one, either"]; traverse($a,$b); traverse($a,$c); traverse($a,$d); traverse($a,$e); traverse($a,$f); traverse($a,$g);

-- Sep 23 In-Class Exercise Thread
<?php
function traverse($tree, $value) {
	$sum = 0;
	$total = 0;
	$start = 0;
	for ($i = 0; $i < count($tree); $i++) {
		$sum += $tree[$i];
		while ($sum >= $value) {
		  //  echo $sum;
		    if($sum == $value){
			    $total++;
			    break;
		    }
		    $sum -= $tree[$start];
		    $start++;
		}
	}
    return $total; 
}
// Driver Code $tree = array( 4, 2, 1, 3, 0); echo(traverse($tree, 3)); //result is 3 ?>
(Edited: 2020-09-23)
<?php function traverse($tree, $value) { $sum = 0; $total = 0; $start = 0; for ($i = 0; $i < count($tree); $i++) { $sum += $tree[$i]; while ($sum >= $value) { // echo $sum; if($sum == $value){ $total++; break; } $sum -= $tree[$start]; $start++; } } return $total; } // Driver Code $tree = array( 4, 2, 1, 3, 0); echo(traverse($tree, 3)); //result is 3 ?>

-- Sep 23 In-Class Exercise Thread
<?php
    function traverse($tree, $value){
        /*tree is an array; value is what we're looking for in tree*/
        $count = 0;
        foreach ($tree as $subTree){
            if (is_array($subTree))
            {
                $count += traverse($subTree, $value);
            }
            elseif ($subTree == $value) {
                ++$count;
            }
        }
        return $count;
    }
$arr = [1 , 2, 3]; echo traverse($arr, 3); $arr = [1 , 2, 3, 3]; echo traverse($arr, 3); $arr = [1 , 2, 3, [3, 3]]; echo traverse($arr, 3); $arr = [[3] , 2, 3, [3, 3]]; echo traverse($arr, 3);
<?php function traverse($tree, $value){ /*tree is an array; value is what we're looking for in tree*/ $count = 0; foreach ($tree as $subTree){ if (is_array($subTree)) { $count += traverse($subTree, $value); } elseif ($subTree == $value) { ++$count; } } return $count; } $arr = [1 , 2, 3]; echo traverse($arr, 3); $arr = [1 , 2, 3, 3]; echo traverse($arr, 3); $arr = [1 , 2, 3, [3, 3]]; echo traverse($arr, 3); $arr = [[3] , 2, 3, [3, 3]]; echo traverse($arr, 3);

-- Sep 23 In-Class Exercise Thread
<?php function traverse($tree, $value) {
	if(!is_array($tree)) {
		if($tree == $value){
			return 1;
		} else {
			return 0;
		}
	}
	$ret = 0;
	foreach ($tree as $var) {
		if($var == $value) {
			$ret++;
		}
		if(is_array($var)) {
			$ret += traverse($var, $value);
		}
	}
	return $ret;
}
$tree = [0 => 5, "hi" => [3, 4], 6 => 3, "two" => [6, 8, 3, 5, 3], 3 => 8, "foo" => 3]; $test = 3;
echo traverse($tree, 3);
<?php function traverse($tree, $value) { if(!is_array($tree)) { if($tree == $value){ return 1; } else { return 0; } } $ret = 0; foreach ($tree as $var) { if($var == $value) { $ret++; } if(is_array($var)) { $ret += traverse($var, $value); } } return $ret; } $tree = [0 => 5, "hi" => [3, 4], 6 => 3, "two" => [6, 8, 3, 5, 3], 3 => 8, "foo" => 3]; $test = 3; echo traverse($tree, 3);

-- Sep 23 In-Class Exercise Thread
<?php
function traverse($tree,$value){
    $count=0;
    if(!is_array($tree)){
        if($tree===$value){
            return 1;
        }
        else return 0;
    }
    foreach($tree as $i){
            $count+=traverse($i,$value);
    }
    return $count;
}
$a=[[1,2,[21,3],1],[1],3,[2]];
$a2=1;
$v=1;
print_r(traverse($a,$v));
print_r(traverse($a2,$v));
(Edited: 2020-09-23)
<?php function traverse($tree,$value){ $count=0; if(!is_array($tree)){ if($tree===$value){ return 1; } else return 0; } foreach($tree as $i){ $count+=traverse($i,$value); } return $count; } $a=[[1,2,[21,3],1],[1],3,[2]]; $a2=1; $v=1; print_r(traverse($a,$v)); print_r(traverse($a2,$v));

-- Sep 23 In-Class Exercise Thread
<?php
function countValues($tree, $value) {
	$count = 0;
	foreach($tree as $elt){
		
		if ($elt == $value) {
   			$count++;
		}
		if(is_array($elt)){
			foreach($elt as $b){
				if ($b == $value) {
   					$count++;
				}
			}
		}
	}
	echo "For the Array: ";
	print_r($tree);
	echo "Total Count of Values: " . $value . " is: " . $count . "\n\n";
}
$a = [1,2,3,4,5,6,[7,7,7],7,7,7, [1,2,2,5,5,"tree"]]; $c = [0,11,"e","e",["a","b","c","e","e","e","e"], "f","g"]; $b = ["a","b", "c","d","e","e", "e"];
countValues($a,7); countValues($b,"e"); countValues($c,"e");
<?php function countValues($tree, $value) { $count = 0; foreach($tree as $elt){ if ($elt == $value) { $count++; } if(is_array($elt)){ foreach($elt as $b){ if ($b == $value) { $count++; } } } } echo "For the Array: "; print_r($tree); echo "Total Count of Values: " . $value . " is: " . $count . "\n\n"; } $a = [1,2,3,4,5,6,[7,7,7],7,7,7, [1,2,2,5,5,"tree"]]; $c = [0,11,"e","e",["a","b","c","e","e","e","e"], "f","g"]; $b = ["a","b", "c","d","e","e", "e"]; countValues($a,7); countValues($b,"e"); countValues($c,"e");

-- Sep 23 In-Class Exercise Thread
<?php // test case 1 $array1 = [
  '0' => [
            '0' => 4,
            '1' => 5,
            '2' => "hi",
            '3' => "bye"
         ],
  '1' => 9,
  '2' => [
            '0' => 1,
            '1' => 2,
            '2' => 3,
            '3' => 4
         ],
  '3' => 11
]; // test case 2 $array2 = [
  '0' => "no",
  '2' => 42,
  '3' => 22,
  '4' => 54,
  '5' => 4,
  '6' => "bye"
]; // test case 3 $array3 = [0]; //test case 4 $array4 = 0; //test case 5 $array5 = 4; function traverse ($tree, $value) {
  $count = 0;
  if (!is_array($tree)) {
    if ($tree == $value) {
      return 1;
    } else {
      return 0;
    }
  }
  foreach ($tree as $node) {
    if(is_array($node)) {
        $count+=traverse($node, $value);
    } else {
      if ($node == $value) {
        $count++;
      }
    }
  }
  return $count;
} $searchParam = 4; echo "Array1 has ".traverse($array1, $searchParam)."\n"; //should be 2 echo "Array2 has ".traverse($array2, $searchParam)."\n"; //should be 1 echo "Array3 has ".traverse($array3, $searchParam)."\n"; //should be 0 echo "Array4 has ".traverse($array4, $searchParam)."\n"; //should be 0 echo "Array5 has ".traverse($array5, $searchParam)."\n"; //should be 1
<?php // test case 1 $array1 = [ '0' => [ '0' => 4, '1' => 5, '2' => "hi", '3' => "bye" ], '1' => 9, '2' => [ '0' => 1, '1' => 2, '2' => 3, '3' => 4 ], '3' => 11 ]; // test case 2 $array2 = [ '0' => "no", '2' => 42, '3' => 22, '4' => 54, '5' => 4, '6' => "bye" ]; // test case 3 $array3 = [0]; //test case 4 $array4 = 0; //test case 5 $array5 = 4; function traverse ($tree, $value) { $count = 0; if (!is_array($tree)) { if ($tree == $value) { return 1; } else { return 0; } } foreach ($tree as $node) { if(is_array($node)) { $count+=traverse($node, $value); } else { if ($node == $value) { $count++; } } } return $count; } $searchParam = 4; echo "Array1 has ".traverse($array1, $searchParam)."\n"; //should be 2 echo "Array2 has ".traverse($array2, $searchParam)."\n"; //should be 1 echo "Array3 has ".traverse($array3, $searchParam)."\n"; //should be 0 echo "Array4 has ".traverse($array4, $searchParam)."\n"; //should be 0 echo "Array5 has ".traverse($array5, $searchParam)."\n"; //should be 1

-- Sep 23 In-Class Exercise Thread
<?php
//Tree is an array (with sub arrays) and Value is a type, //traverse through the array through each element and its subarray elements and count how many times //value is counted function traverse($tree, $value) {
    $count = 0;
    //base case for recursion
    //if $tree is not an array, we check if its equal to $value and reutrn 1
    //if not return 0
    if(!is_array($tree)) {
        if($tree == $value) {
            return 1;
        }
        else {
            return 0;
        }
    }
    
    //For each value in tree traverse
    foreach($tree as $someValue) {
        //If someValue is the value we are looking for, increase count
        if($someValue == $value) {
            $count++;
        }
        //if there is a subarray as an element, then recurse through the function
        else if(is_array($someValue)){
            $count += traverse($someValue, $value);
        }
    }
    return $count;
}
$a = [1, [7, 3, 3], [2.1, "string"], 3, 6.11, "string", "hello"]; $b = 3; $c = "string"; $d = 2.1; $e = 90;
echo traverse($a, $b) . "\n"; //return 3 echo traverse($a, $c) . "\n"; //return 2 echo traverse($a, $d) . "\n"; //return 1 echo traverse($a, $e) . "\n"; //return 0 ?>
<?php //Tree is an array (with sub arrays) and Value is a type, //traverse through the array through each element and its subarray elements and count how many times //value is counted function traverse($tree, $value) { $count = 0; //base case for recursion //if $tree is not an array, we check if its equal to $value and reutrn 1 //if not return 0 if(!is_array($tree)) { if($tree == $value) { return 1; } else { return 0; } } //For each value in tree traverse foreach($tree as $someValue) { //If someValue is the value we are looking for, increase count if($someValue == $value) { $count++; } //if there is a subarray as an element, then recurse through the function else if(is_array($someValue)){ $count += traverse($someValue, $value); } } return $count; } $a = [1, [7, 3, 3], [2.1, "string"], 3, 6.11, "string", "hello"]; $b = 3; $c = "string"; $d = 2.1; $e = 90; echo traverse($a, $b) . "\n"; //return 3 echo traverse($a, $c) . "\n"; //return 2 echo traverse($a, $d) . "\n"; //return 1 echo traverse($a, $e) . "\n"; //return 0 ?>

-- Sep 23 In-Class Exercise Thread
<pre> <?php
   function traverse ($tree, $value){
       if(!is_array($tree)){
           if($tree == $value){
               return 1;
           }
           else{
               return 0;
           }
       }
       $num_values = 0;
       foreach($tree as $element){
           if($element == $value){
               $num_values++;
           }
           else if (is_array($element)){
               $num_values += traverse($element, $value);
           }
       }
       return $num_values;
   }
    //Driver
    $tree = [1,2,3,3,2,1,2];
    echo (traverse($tree, 2))."\n";// Outputs 3
    $tree = [1,2,3,[2,3,5,[3,5,6,[3]]],2,3,[4,3],3,2,1];
    echo (traverse($tree, 3))."\n";// Outputs 7
    $tree = [1,2,"hello",[2,3,5,[3,"hello",6,[3,"hello"]]],2,3,["hello",3],3,"hello",1];
    echo (traverse($tree, "hello"))."\n";// Outputs 5
<pre>
(Edited: 2020-09-23)
<pre> <?php function traverse ($tree, $value){ if(!is_array($tree)){ if($tree == $value){ return 1; } else{ return 0; } } $num_values = 0; foreach($tree as $element){ if($element == $value){ $num_values++; } else if (is_array($element)){ $num_values += traverse($element, $value); } } return $num_values; } //Driver $tree = [1,2,3,3,2,1,2]; echo (traverse($tree, 2))."\n";// Outputs 3 $tree = [1,2,3,[2,3,5,[3,5,6,[3]]],2,3,[4,3],3,2,1]; echo (traverse($tree, 3))."\n";// Outputs 7 $tree = [1,2,"hello",[2,3,5,[3,"hello",6,[3,"hello"]]],2,3,["hello",3],3,"hello",1]; echo (traverse($tree, "hello"))."\n";// Outputs 5 <pre>
[ Next ]
X