Difference between revisions of "CSC220 Homework 2 Solutions 2010"
(→Problem #3) |
(→Problem #3) |
||
Line 133: | Line 133: | ||
Typical input: | Typical input: | ||
− | Wolf, | + | Wolf, villain, aka Big Bad Wolf |
Little Red Riding Hood, hero, lost in the woods | Little Red Riding Hood, hero, lost in the woods | ||
Firt little pig, hero, builds a house out of straw | Firt little pig, hero, builds a house out of straw | ||
Line 150: | Line 150: | ||
<li>The hero is Third little pig, known as builds a house out of bricks <br> | <li>The hero is Third little pig, known as builds a house out of bricks <br> | ||
<li>The hero is Snow White, known as lost in the woods <br> | <li>The hero is Snow White, known as lost in the woods <br> | ||
− | </ul><h2>The | + | </ul><h2>The Villain: </h2> |
− | <ul><li>The | + | <ul><li>The villain is Wolf, known as aka Big Bad Wolf <br> |
</ul><h2>Supporting Characters: </h2> | </ul><h2>Supporting Characters: </h2> | ||
<ul><li>Prince charming is the the prince, rescues damesels in distress | <ul><li>Prince charming is the the prince, rescues damesels in distress |
Revision as of 08:10, 5 October 2010
Problem #1
<html>
<!--
Julia P. (edited by D. T.)
220a-aj
Sept 29, 2010
Homework #2
Problem #1
Computes and prints the first 10 rows of Pascal's Triangle.
-->
<head>
<title> Pascal\'s Triangle</title>
</head>
<body>
<?php
//--- compute the triangle and store it in a 2D array ---
//--- init as 1 row of 1 cell containing 1 ---
$pascal = array(array(1));
//--- loop and expand the array ---
for($i=1;$i<10;$i++){
//--- $i is the row index ---
$pascal[$i] = array();
$count = count($pascal[$i-1])+1;
$pascal[$i][0]=1;
//--- $j is the column index ---
for($j=1;$j<$count-1;$j++){
$pascal[$i][$j] = $pascal[$i-1][$j] + $pascal[$i-1][$j-1];
}
//--- add a 1 at the end of the row ---
$pascal[$i][$count-1]=1;
}
//--- display the table ---
print"<table border = \"1\">";
//--- double loop scans 2D array ----
foreach($pascal as $row){
print"<tr>";
foreach($row as $element){
print"<td>".$element."</td>";
}
print "</tr>\n";
}
print "</table>";
?>
</body>
</html>
Problem #2
<html>
<!--
hw2b.php
Julia P. (edited by D.T.)
220a-aj
Computes successive rows of Pascal's triangle until one is
found with a term larger than 1000. That row is printed
and the program stops.
-->
<head>
<title> Pascal\'s Triangle</title>
</head>
<body>
<?php
//--- double loop creates a 2D array ---
//--- $i controls row index
//--- $j controls column index
//--- $large becomes True as soon as term > 1000 found.
//--- $i-loop stops when $large is True
$pascal = array(array(1));
$large = False;
for($i=1; $large==False ;$i++){
$pascal[$i] = array();
$count = count($pascal[$i-1])+1;
$pascal[$i][0]=1;
for($j=1;$j<$count-1;$j++){
$pascal[$i][$j] = $pascal[$i-1][$j] + $pascal[$i-1][$j-1];
if($pascal[$i][$j]>=1000)
$large = true;
}
$pascal[$i][$count-1]=1;
}
//--- display Pascal array ---
$newcount = count($pascal);
print "<table border = \"1\">";
print "<tr>";
foreach($pascal[$newcount-1] as $element){
print"<td>".$element."</td>";
}
print "</tr>\n";
print "</table>";
?>
</body>
</html>
Problem #3
<html>
<!--
hw2c.php
Julia P. (Edited by D.T.)
Reads a file containing lines with 3 fields describing characters from comic books.
Parses the information from each line in person, type, extra information, and
displays the contents by listing first the hero or heroes, then the villain or villains,
then the supporting characters.
Note that the heroes or villains can appear anywhere in the input file.
Typical input:
Wolf, villain, aka Big Bad Wolf
Little Red Riding Hood, hero, lost in the woods
Firt little pig, hero, builds a house out of straw
Second little pig, hero, builds a house out of sticks
Third little pig, hero, builds a house out of bricks
Snow White, hero, lost in the woods
Prince charming, the prince, rescues damesels in distress
The king, the father of the prince or pricess, totally lost with the task of raising children
Typical output:
<h2>The Heroes: </h2>
<ul><li>The hero is Little Red Riding Hood, known as lost in the woods <br>
<li>The hero is Firt little pig, known as builds a house out of straw <br>
<li>The hero is Second little pig, known as builds a house out of sticks <br>
<li>The hero is Third little pig, known as builds a house out of bricks <br>
<li>The hero is Snow White, known as lost in the woods <br>
</ul><h2>The Villain: </h2>
<ul><li>The villain is Wolf, known as aka Big Bad Wolf <br>
</ul><h2>Supporting Characters: </h2>
<ul><li>Prince charming is the the prince, rescues damesels in distress
<li>The king is the the father of the prince or pricess, totally lost with the task of raising children
</ul>
-->
<body>
<?php
//--- read the lines from file play.txt and store in array $lines ---
$lines = file( 'play.txt' );
$elements = array();
for($i=0;$i<count($lines);$i++){
//--- split on ',' into 3 fields ---
$elements[$i] = explode(",", $lines[$i]);
for($j=0;$j<count($elements[$i]);$j++){
$elements[$i][$j] = trim($elements[$i][$j]);
}
}
//--- get ready to capture heroes and villains ---
$hero=array();
$villain=array();
for($i=0;$i<count($elements);$i++){
if($elements[$i][1]=="hero"){
$hero[] = $i;
}
//--- capture villains (even if misspelled!) ---
elseif($elements[$i][1]=="villain" || $elements[$i][1]=="vilain" ){
$villain[]=$i;
}
}
//--- display the arrays of heroes and villains, and supporting cast ---
//--- HEROES ---
print "<h2>The Hero";
if(count($hero)>=2){
print "es";
}
print ": </h2>\n<ul>";
for($i=0; $i<count($hero);$i++){
print "<li>The hero is ".$elements[$hero[$i]][0].", known as ".$elements[$hero[$i]][2]." <br>\n";
}
print "</ul>";
//--- VILLAINS ---
print "<h2>The Villain";
if(count($villain)>=2){
print "s";
}
print ": </h2>\n<ul>";
for($i=0; $i<count($vilain);$i++){
print "<li>The villain is ".$elements[$vilain[$i]][0].", known as ".$elements[$vilain[$i]][2]." <br>\n";
}
print "</ul>";
//--- SUPPORTING CHARACTERS ---
print "<h2>Supporting Characters: </h2>\n<ul>";
for ($i=0;$i<count($lines);$i++){
if ((!in_array($i, $hero))&&(!in_array($i, $villain))){
print "<li>".$elements[$i][0]." is the ".$elements[$i][1].", ".$elements[$i][2]."\n";
}
}
print "</ul>";
?>
</body>
</html>