Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Homework 4 - PHP Introduction

Due: Thursday, March 14, 2024 at 11:59pm

This assignment must be completed individually.

Purpose:

  • Gain experience writing in PHP
  • Get ready to work on Sprint 3

Overview

In this assignment, we will be exploring the PHP language through solving a variety of problems. You are strongly encouraged to review the PHP documentation and utilize the language features that PHP provides. For example, you may want to review:

homework4.php

To begin, you should create a new PHP file, entitled homework4.php. You should copy and paste the template below into the file, adding your functions as you solve each problem. You should not include any code in this file outside of your implemented functions. Each function must return its result; do not print out your results in the function.

<?php
    /**
     * Homework 4 - PHP Introduction
     *
     * Computing ID:
     * Resources used: [list any resources used to complete this assignment]
     */
     
    // Your functions here

    // No closing php tag needed since there is only PHP in this file

Submit your completed homework4.php file to Gradescope.

hw4_testing.php

You should also create a separate PHP file to test your Homework 4 functions. Call your functions with various inputs, including edge cases, and ensure that they behave as expected.

A template file is provided below, where we have “included” the homework4.php file we are writing.

<?php
    include("homework4.php");

    // Hint: include error printing!
?><!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">  
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="author" content="your name">
  <meta name="description" content="include some description about your page">  
    
  <title>Homework 4 Test File</title>
</head>
<body>
<h1>Homework 4 Test File</h1>

<h2>Problem 1</h2>
<?php
    echo "Write tests for Problem 1 here\n";
    $test1 = [ [ "score" => 55, "max_points" => 100 ], [ "score" => 55, "max_points" => 100 ] ];
    echo calculateAverage($test1, false); // should be 55
    //...
?>

<p>...</p>
</body>
</html>

Problem 1: Compute the Grade

Write a PHP function, calculateGrade() that accepts an array of project scores and an option specifying whether the score with the lowest percentage points will be dropped. Each project score in the array is an associative array with keys “score” and “max_points”; for example:

[ "score" => 55, "max_points" => 100 ]

The drop option will be given as a boolean value where true means drop the lowest project score and false means do not drop any scores. The lowest score is determined based on percentage of points awarded (for example, a 55/100 is 55% while a 7/10 is 70%, so the 55/100 is dropped). If there are multiple lowest project scores (i.e., same percentages), you decide which one to drop. The function must return the overall average of the project scores (100 * total scored points / total available points) rounded to three decimal places.

function calculateGrade($scores, $drop) { /*...*/ }

Problem 2: Gridding

Write a PHP function which takes in the width and height of a grid (in number of tiles):

function gridCubbies($width, $height) { ... }

Each tile in the grid is numbered, starting at the bottom-left and going bottom-to-top, left to right. For example, if the user calls our function with width = 3 and height = 4, the grid would be:

 4  8  12
 3  7  11
 2  6  10
 1  5   9

The function gridCubbies should return a comma-separated string that represents all unique tiles that are in the corner cubbies, in numerical order. A cubbie is defined as the 2x2 box at that corner. For example, the top-left cubbie of the example above is:

 4  8
 3  7

In this case, our function should return:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12

Since the values should be unique, 5, 6, 7, and 8 are not repeated in the output. That is, the cubbies overlap in this example.

If the grid doesn’t have a width or height of at least two, the function should return the ordered tiles of all the edges. If there are no tiles to be printed, return an empty string.

Note: The grids may get large! Too large for us to create a 2-dimensional array! So, you should avoid creating an array to store the grid; however, you may want to store the tiles you plan to return in your output in an array, using a PHP method to sort, uniquify, and combine the array into the appropriate return string. (Remember, the output string will have at most 16 tiles.)

Problem 3: Combining Address Books

Write a PHP function named combineAddressBooks() that accepts two or more associative arrays containing address books and returns a merged address book. There should not be duplicate entries in the merged book.

For example, if the incoming address books are:

$friend_book1 = ['Happy' => '111-111-1111', 'Sneezy' => '222-222-2222',
                'Doc' => '333-333-3333', 'Grumpy' => '444-444-4444', 'Bashful' => '555-555-5555',
                'Sleepy' => 'sleepy@uva.edu'];

$friend_book2 = ['Sneezy' => 'sneezy@uva.edu', 'Doc' => 'doc@uva.edu', 
                'Happy' => 'happy@uva.edu', 'Bashful' => 'bashful@uva.edu', 
                'Sleepy' => 'sleepy@uva.edu'];

Calling the function on these books:

combineAddressBooks($friend_book1, $friend_book2);

will return a merged friend book with the people as the keys.

[
    "Happy" => [ "111-111-1111", "happy@uva.edu" ]
    "Sneezy" => [ "222-222-2222", "sneezy@uva.edu" ],
    "Doc" => [ "333-333-3333", "doc@uva.edu" ],
    "Grumpy" => [ "444-444-4444" ],
    "Bashful" => [ "555-555-5555", "bashful@uva.edu" ],
    "Sleepy" => [ "sleepy@uva.edu" ] 
]

Problem 4: Acronym Summary

Write a PHP function named acronymSummary() that calculates and returns an “acronym summary” of a given string. The “acronym summary” is calculated by finding how many times each of the possible words in the first string parameter are acronyms used in the second string parameter. (A word is used as an acronym in the search string if there are consecutive words that start with the letters of that word.) Note: the acronym summary is case insensitive.

Example: If given the following input:

$acronyms = "rofl lol afk"
$searchString = "Rabbits on freezing lakes only like really old fleece leggings."
$acrosum = acronymSummary($acronyms, $searchString);

The acronym summary returned would be the following associative array:

[
    "rofl" => 2, 
    "lol" => 1,
    "afk" => 0
]

That is, rofl appears in “Rabbits on freezing lakes” and “really old fleece leggings”; lol appears in “lakes only like”; and afk does not appear at all.

The acronym summary should match the case of the $acronyms string passed in, but should search through the search string in a case-insensitive manner. If either parameter string is empty or the wrong type, the function should return an empty array.

Submission

Submit your homework4.php PHP file to Gradescope in the “Homework 4” submission. Remember that this is an individual assignment.

Note: You will be allowed to submit a maximum of 15 times to Gradescope. Please utilize your local development environment or the cs4640 server to test your PHP code and verify that the output matches what is expected.

Grading Rubric

This homework is worth 50 points, and will be autograded by Gradescope using the following rubric:

  • 10 points - Problem 1
  • 15 points - Problem 2
  • 10 points - Problem 3
  • 15 points - Problem 4

Academic Integrity

For this assignment, you may not share your solutions publicly.

Use of Generative AI

For Homework 4, you are not allowed to use generative AI tools to help you solve this problem.