Next: , Up: (dir)

Scripting Languages G22.3033-002 Summer 2008


Previous: Top, Up: Top

Scripting Languages G22.3033-002 Summer 2008 Example solutions for hw05

Assigned Th 6/19/2008, due Fr 6/27 at 9pm. 50 points.

http://www.cs.nyu.edu/courses/summer08/G22.3033-002/


Next: , Up: solutions-hw05

Instructions for example solutions

These are example solutions. Please keep in mind that often, there is not just one correct solution to a question. If you come up with different answers, then it may be that both your answers and these answers here are correct. Of course, these answers here may also contain mistakes. If you spot a mistake, please let me know so I can correct it.


Previous: Instructions, Up: solutions-hw05

Example solutions for Homework 5


Next: , Up: Solutions-hw05

solutions-hw05-1 Properties

  1.           |2.00, 3.00| = 3.61
              |2.77, 4.16| = 5.00
         
  2. One could convincingly argue either way. Properties are easier to use in PHP than in VBA, because in PHP, they are just special methods, whereas in VBA, they require a whole separate syntax. Properties are harder to use in PHP than in VBA, because in PHP, you have to look at the nested if-statement in the special method, whereas in VBA, the property is an official part of the top-level signature of a class.
  3. Again, one could convincingly argue either way. Properties are more expressive in PHP than in VBA, because in PHP, they can have arbitrary names at runtime, whereas in in VBA, the set of names must be fixed at compile time. Properties are less expressive in PHP than in VBA, because in PHP, they always have a field-like look-and-feel, whereas in VBA, they can also have an array-like look-and-feel.


Next: , Previous: solutions-hw05-1, Up: Solutions-hw05

solutions-hw05-2 Call-backs

  1.           0 => (2, 3)<br/>
              1 => (0, 4)<br/>
              2 => (-3, 4)<br/>
         
  2. The code passes the string "cmp_vector" as a parameter to library function usort(). That way, the library function makes a call-back to function cmp_vector() whenever it needs to compare two vectors for sorting.
  3. Either change the body of function "cmp_vector", or write a new function, and pass a different string for the call-back. Here is an example for the second solution:
              function cmp_vector_by_x($a, $b) {
                $aa = $a->x; $bb = $b->x;
                return $aa < $bb ? -1 : $aa == $bb ? 0 : 1;
              }
              usort($a, "cmp_vector_by_x");
         


Next: , Previous: solutions-hw05-2, Up: Solutions-hw05

solutions-hw05-3 Installing script on server (PHP)

  1. http://www.cs.nyu.edu/~hirzel/hw05-3a.html

    No user name or password required.

  2. http://www.cs.nyu.edu/~hirzel/php/hw05-3b.html

    User name grader, password hype3.

  3. http://www.cs.nyu.edu/~hirzel/php/hw05-3c.php

    User name grader, password hype3.


Previous: solutions-hw05-3, Up: Solutions-hw05

solutions-hw05-4 Cooking conversions (PHP)

     <html>
     <head>
       <?php
         function convert($qty, $unit, $ing) {
           $cup2g = array('flour' => '110', 'sugar' => 225, 'butter' => 225);
           $volume = array('cup' => 1, 'tbsp' => 16, 'tsp' => 48, 'ml' => 236);
           $weight = array('lb' => 1, 'oz' => 16, 'g' => 453);
           if (array_key_exists($ing, $cup2g) && array_key_exists($unit, $volume)) {
             $qty = 1.0 * $qty * $cup2g[$ing] / $volume[$unit];
             $unit = 'g';
           } elseif (array_key_exists($unit, $volume)) {
             $qty = 1.0 * $qty * $volume[ml] / $volume[$unit];
             $unit = 'ml';
           } elseif (array_key_exists($unit, $weight)) {
             $qty = 1.0 * $qty * $weight[g] / $weight[$unit];
             $unit = 'g';
           }
           return ((int)($qty + .5)) . " $unit $ing";
         }
       ?>
     </head>
     <body>
       <?php if(!empty($_GET['qty'])) { ?>
       Converted result:
       <?php    echo convert($_GET['qty'], $_GET['unit'], $_GET['ing']); ?>
       <br/>
       <?php } ?>
       <form action="<?php echo $_SERVER[PHP_SELF]; ?>" method=get>
         <table border=1>
           <tr><td>Quantity
             <td><input type="text" name="qty"  value=<?php echo $_GET[qty] ?> />
           <tr><td>Unit
             <td><input type="text" name="unit" value=<?php echo $_GET[unit] ?> />
           <tr><td>Ingredient
             <td><input type="text" name="ing"  value=<?php echo $_GET[ing] ?> />
         </table>
         <input type="submit" value="Submit" />
       </form>
     </body>
     </html>