Scripting Languages CSCI-GA.3033.003, Summer 2012 hw05


Reading Assignments

Concept Questions

hw05_1 Call-backs

Consider the following PHP code.
<?php
function f($arr, $cmp) {
  $result = null;
  foreach ($arr as $val)
    if ($result == null || $cmp($val, $result) > 0)
      $result = $val;
  return $result;
}
function comparator($x, $y) {
  if ($x < $y) return -1;
  if ($x == $y) return 0;
  return 1;
}
$a = array(3,1,5,2);
$m = f($a, "comparator");
print "\$m is $m<br/>\n";
?>
1a.
(3 points) What does it print? Before you run it, try to answer the question by just reading the code.
1b.
(4 points) Explain briefly how this code uses call-backs.
1c.
(3 points) How do you get the higher-order function f to find the minimum value of the array, without changing the code of f itself?

hw05_2 Properties

Consider the following PHP code.
<?php
class Store {
  var $val = "";
  function __get($propName) {
    if ($propName == 'text')
      return htmlentities($this->val);
    if ($propName == 'rawText')
      return $this->val;
  }
  function __set($propName, $value) {
    if ($propName == 'text' || $propName == 'rawText')
      $this->val = $value;
  }
}
$s = new Store();
$s->text = "A <b>bold</b> string."; ?>
The text is "<?php print $s->text; ?>".<br/>
The raw text is "<?php print $s->rawText; ?>".<br/>
2a.
(3 points) What does it print? As before, try to run the code in your head before you run it on a computer.
2b.
(3 points) What is a possible reason why someone might implement a Store like this?
2c.
(4 points) In VBA terminology, is this code using simple properties or indexed properties?

Programming exercises

hw05_3 Installing script on server (PHP)

3a.
(4 points)

Follow the instructions from the lecture slides to create an HTML page that can be viewed from any web browser using the following URL:

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

For example, if your_cims_id is hirzel, then the URL would be:

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

3b.
(3 points)

Use a .htaccess file to create a password protected HTML page that can be viewed from any web browser using the following URL:

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

For example, with your_cims_id as hirzel, you get the following URL, which requires you to enter the user name student and the password Psh_P_8:

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

Please create a user name grader. Please indicate the URL and the password for grader in your submission, so that Aditya Bhatia can access your web page.

3c.
(3 points)

In the same directory that you secured using a .htaccess file, create a PHP script hw05-3c.php with the following code:

<html><body>
<?php
  if(!empty($_GET['who'])) { echo "Hi, {$_GET['who']}."; }
?>
<form action="<?php echo $_SERVER[PHP_SELF]; ?>" method="get">
  Who shall be greeted: <input type="text" name="who" />
</form>
</body></html>

(See also http://www.cs.nyu.edu/~hirzel/php/hw05-3c.phps). Test it and make sure it works. For example, for your_cims_id hirzel, user name student, and password P_8sh_P, this URL runs the script for you:

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

hw05_4 Priority queue class

(10 points) As you are learning new languages, you need to practice writing code. But since the focus is on the language, not on the code itself, you can just re-implement something you are already familiar with. That is why this homework question again asks you to write a priority queue class, this time in PHP. As usual, your class should have methods insert(key) and extractMax(). Here is the driver:
<?php
include 'PriorityQueue.inc';
$q = new PriorityQueue();
$q->insert(3.1);
$q->insert(4.2);
$q->insert(0.5);
$q->insert(1.9);
print $q->extractMax() . "<br/>\n";
print $q->extractMax() . "<br/>\n";
print $q->extractMax() . "<br/>\n";
print $q->extractMax() . "<br/>\n"; ?>
This should print 4.2, 3.1, 1.9, and 0.5, in other words, it should output the previously-inserted numbers in descending order by value. To solve this question, you need to put your code in a file PriorityQueue.inc, so that the driver can pick it up via the include statement. You can put the driver in a file priorityQueueDriver.php.
http://cs.nyu.edu/courses/summer12/CSCI-GA.3033-003/hw05.html