[ Prev ]
2020-09-23

-- Sep 23 In-Class Exercise Thread
<?php function traverse($tree, $value) {
    $counter = 0;
    if(!is_array($tree))
    {
        if($tree == $value)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
    foreach($tree as $var)
    {
        if($var == $value)
        {
            $counter++;
        }
        else if(is_array($var))
        {
            $counter = $counter + traverse($var, $value);
        }
    }
    return $counter;
} $a = [3,4,[8,8,9], "hello", ["class", "hello", 8], 15]; echo traverse($a, "hello")."\n"; //output should be 2 echo traverse($a, 8)."\n"; //output should be 3
<?php function traverse($tree, $value) { $counter = 0; if(!is_array($tree)) { if($tree == $value) { return 1; } else { return 0; } } foreach($tree as $var) { if($var == $value) { $counter++; } else if(is_array($var)) { $counter = $counter + traverse($var, $value); } } return $counter; } $a = [3,4,[8,8,9], "hello", ["class", "hello", 8], 15]; echo traverse($a, "hello")."\n"; //output should be 2 echo traverse($a, 8)."\n"; //output should be 3

-- Sep 23 In-Class Exercise Thread
Resource Description for IC4.png
((resource:IC4.png|Resource Description for IC4.png))

-- Sep 23 In-Class Exercise Thread
<?php function traverse($tree, $value) {
	$number = 0;
	if(!is_array($tree)) {
    	if($tree == $value) {
        	return 1;
        } else {
        	return 0;
        }
    } else {
			foreach ($tree as $element) {
				if($element == $value) {
					$number++;
				} else if (is_array($element)) {
          $number += traverse($element, $value);
			}
		}
		return $number;
		}
}
	$tree = ["hi", 1, 2, [3, 3], "hi", ["hi", 9, ["hi", 10, 0]]];
	$value = "hi";
	echo (traverse($tree, $value));
	
	$tree1 = [5, 9 ,10 , ["hi", "hi"], 10, "hi"];
	echo (traverse($tree1, $value));
	?>
<?php function traverse($tree, $value) { $number = 0; if(!is_array($tree)) { if($tree == $value) { return 1; } else { return 0; } } else { foreach ($tree as $element) { if($element == $value) { $number++; } else if (is_array($element)) { $number += traverse($element, $value); } } return $number; } } $tree = ["hi", 1, 2, [3, 3], "hi", ["hi", 9, ["hi", 10, 0]]]; $value = "hi"; echo (traverse($tree, $value)); $tree1 = [5, 9 ,10 , ["hi", "hi"], 10, "hi"]; echo (traverse($tree1, $value)); ?>

-- Sep 23 In-Class Exercise Thread
Resource Description for Screen Shot 2020-09-24 at 12.05.39 PM.png Resource Description for Screen Shot 2020-09-24 at 12.06.20 PM.png
((resource:Screen Shot 2020-09-24 at 12.05.39 PM.png|Resource Description for Screen Shot 2020-09-24 at 12.05.39 PM.png)) ((resource:Screen Shot 2020-09-24 at 12.06.20 PM.png|Resource Description for Screen Shot 2020-09-24 at 12.06.20 PM.png))

-- Sep 23 In-Class Exercise Thread
Last login: Fri Sep 25 04:41:34 on ttys000
The default interactive shell is now zsh. To update your account to use zsh, please run `chsh -s /bin/zsh`. For more details, please visit https://support.apple.com/kb/HT208050. Shinhyungs-MacBook-Pro:~ shinlee$ pico prog1.php
  GNU nano 2.0.6              File: prog1.php                                   
<?php
        function traverse($tree, $value) {
                $count = 0;
                foreach($tree as $elt) {
                        $count+=substr_count($elt, $value);
                }
                return $count;
        }
        $tree = ["Irisss", "ris", "is", "s"];
        $value = "s";
        $number = traverse($tree, $value);
        echo $number . "\n"; //expected output: 6
        $array2 = ["JavaJava", "JavaScript", "Java COFFEE", "Java Indonesia"];
        $target2 = "Java";
        $occurrence2 = traverse($array2, $target2);
        echo $occurrence2 . "\n"; //expected output: 5
        $array3 = ["Hello WoRld", "Turtle", "Jess Mar", "RRR"];
        $target3 = "R";
        $occurrence3 = traverse($array3, $target3);
        echo $occurrence3 . "\n"; //expected output: 4
Resource Description for Screen Shot 2020-09-25 at 4.41.57 AM.png
(Edited: 2020-09-24)
Last login: Fri Sep 25 04:41:34 on ttys000 The default interactive shell is now zsh. To update your account to use zsh, please run @BT@chsh -s /bin/zsh@BT@. For more details, please visit https://support.apple.com/kb/HT208050. Shinhyungs-MacBook-Pro:~ shinlee$ pico prog1.php GNU nano 2.0.6 File: prog1.php <?php function traverse($tree, $value) { $count = 0; foreach($tree as $elt) { $count+=substr_count($elt, $value); } return $count; } $tree = ["Irisss", "ris", "is", "s"]; $value = "s"; $number = traverse($tree, $value); echo $number . "\n"; //expected output: 6 $array2 = ["JavaJava", "JavaScript", "Java COFFEE", "Java Indonesia"]; $target2 = "Java"; $occurrence2 = traverse($array2, $target2); echo $occurrence2 . "\n"; //expected output: 5 $array3 = ["Hello WoRld", "Turtle", "Jess Mar", "RRR"]; $target3 = "R"; $occurrence3 = traverse($array3, $target3); echo $occurrence3 . "\n"; //expected output: 4 ((resource:Screen Shot 2020-09-25 at 4.41.57 AM.png|Resource Description for Screen Shot 2020-09-25 at 4.41.57 AM.png))
2020-09-27

-- Sep 23 In-Class Exercise Thread
<?php 
function traverse($tree, $value){
    $count = 0;
    foreach($tree as $sub){
        if ($sub == $value) $count++;
        else if (is_array($sub)) $count += traverse($sub, $value);
    }
    return $count;
} 
 
$test1 = [1, 1, [1, 1, 1, 5, 4, 3], 3, 4, [1, 3, 5, 1]];
$test2 = [5, 2, 1, 4];
$test3 = [0, 2, 3, 4];
$test4 = [0, 5, [1, 3, [1, 3, [4, 1]]], 1]; 
 
echo traverse($test1, 1) .", should = 7<br />";
echo traverse($test2, 1) .", should = 1<br />";
echo traverse($test3, 1) .", should = 0<br />";
echo traverse($test4, 1) .", should = 4<br />";
<pre> <?php function traverse($tree, $value){ $count = 0; foreach($tree as $sub){ if ($sub == $value) $count++; else if (is_array($sub)) $count += traverse($sub, $value); } return $count; } $test1 = [1, 1, [1, 1, 1, 5, 4, 3], 3, 4, [1, 3, 5, 1]]; $test2 = [5, 2, 1, 4]; $test3 = [0, 2, 3, 4]; $test4 = [0, 5, [1, 3, [1, 3, [4, 1]]], 1]; echo traverse($test1, 1) .", should = 7<br />"; echo traverse($test2, 1) .", should = 1<br />"; echo traverse($test3, 1) .", should = 0<br />"; echo traverse($test4, 1) .", should = 4<br />"; </pre>

-- Sep 23 In-Class Exercise Thread
Resource Description for OnZBWaS.png
(Edited: 2020-09-27)
((resource:OnZBWaS.png|Resource Description for OnZBWaS.png))
2020-09-28

-- Sep 23 In-Class Exercise Thread
Resource Description for cs174_inclass.png
((resource:cs174_inclass.png|Resource Description for cs174_inclass.png))

-- Sep 23 In-Class Exercise Thread
(Edited: 2020-09-28)
<nowiki><?php $a = ["hi", "Apple", ["hi","apple"]]; $b = ["hi", "Apple", ["hi","apple"]]; $c = ["hi", "Apple", ["hi","Apple","Banana"]]; $d = ["hi", "Apple", ["hi","apple"],"hi"]; function traverse($arr,$val) { $num = 0; foreach($arr as $var){ if($val == $var){ $num ++; } foreach($var as $ele){ if($val == $ele){ $num ++; } } } echo $num; echo "<br>"; } traverse($a,"hi"); traverse($b,"apple"); traverse($c,"Apple"); traverse($d,"hi"); </nowiki>
2020-10-04

-- 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; 
} $test1 = [1, 1, [1, 1, 1, 5, 4, 3], 3, 4, [1, 3, 5, 1]]; $test2 = [5, 2, 1, 4]; $test3 = [0, 2, 3, 4]; $test4 = [0, 5, [1, 3, [1, 3, [4, 1]]], 1];
<?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; } $test1 = [1, 1, [1, 1, 1, 5, 4, 3], 3, 4, [1, 3, 5, 1]]; $test2 = [5, 2, 1, 4]; $test3 = [0, 2, 3, 4]; $test4 = [0, 5, [1, 3, [1, 3, [4, 1]]], 1];
X