Question

In: Computer Science

Using a combination of HTML/PHP, implement a web page where the users can upload a text...

Using a combination of HTML/PHP, implement a web page where the users can upload a text file, exclusively in .txt format, which contains a string of 400 numbers, such as:

71636269561882670428
85861560789112949495
65727333001053367881
52584907711670556013
53697817977846174064
83972241375657056057
82166370484403199890
96983520312774506326
12540698747158523863
66896648950445244523
05886116467109405077
16427171479924442928
17866458359124566529
24219022671055626321
07198403850962455444
84580156166097919133
62229893423380308135
73167176531330624919
30358907296290491560
70172427121883998797

Your code should contain a PHP function that, accepting the string of 400 numbers in input, is able to find the greatest product of four adjacent numbers in all the four possible directions (up, down, left, right, or diagonally).

NOTE:

  • You will need to arrange the numbers in a grid 20x20.
  • If the file doesn't contain 400 numbers or contains some characters in between, it should be discarded and the application should inform the user that the file is not format correctly.
  • You can ignore new lines and white spaces.
  • Add a tester function to check the behavior of your PHP function.
  • tester function should cover all possible checks:

    1) correct file format

    2) proper file contents

    3) size of the file

    4)For any file, with known answer check if the function returns the same answer (true test case). Because with null string and all the application itself wont work. Until we upload any file. You can add another possibilities here, will help in enhancing test cases.

  • Follow the guidelines showed to avoid losing points.

Correct solution will be highly appreciated

Solutions

Expert Solution

Working code implemented in PHP and appropriate comments provided for better understanding.

index.php Source Code:

<?php
echo <<<_END
<html><head><title>PHP Form Upload</title></head><body>
<form method='post' action='index.php' enctype='multipart/form-data' >
Select File: <input type='file' name='uploadfileinput' size='10' >
<input type='submit' value='Upload'>
</form>
_END;

if($_FILES){
$inputtype = $_FILES['uploadfileinput']['type'];

if($inputtype == "text/plain"){

// Accessing the Name of the file uploaded.
$name = $_FILES['uploadfileinput']['name'];

// Opening the file and reading the lines of numbers.
$fh = fopen($name, 'r') or die("File Does not exist");

// Assigning all the values to an array.
$charArray = [];
while(!feof($fh)){
$line = fgets($fh);
$tempCharArray = str_split($line);
array_push($charArray, $tempCharArray);
}
  
fclose($fh);

// Validate for the right input and Give out the ANSWER.
if(validateInputData($charArray) != true){
echo "Error: File is Not Formatted properly.";
}else{
// Giving out the Answer if everything is Correct.
echo "File Name: " . $name . " <br> ";

echo "The max product is: <h1>";
echo calculateProduct($charArray);
echo "</h1>";
}

tester($charArray);

}else{
echo "Illegal File Type: Please Upload a text file with extension .txt only";
echo "<br>";
}
}

// Ignored the top and left cases because it will be the same as right and bottom.
function calculateProduct($charArray){
$product = 0;
for($row = 0; $row < sizeof($charArray); $row++){
for($col = 0; $col < sizeof($charArray[$row]); $col++){
if($charArray[$row][$col] !== "\n"){
//Right
$tempProductRight = $charArray[$row][$col] * $charArray[$row][$col + 1] * $charArray[$row][$col + 2] * $charArray[$row][$col + 3];
//Bottom
$tempProductDown = $charArray[$row][$col] * $charArray[$row + 1][$col] * $charArray[$row + 2][$col] * $charArray[$row + 3][$col];
//Diagnol
$tempProductCross = $charArray[$row][$col] * $charArray[$row + 1][$col + 1] * $charArray[$row + 2][$col +2] * $charArray[$row + 3][$col + 3];

// Getting the max from Right, Bottom and Diagnol.
$tempMax = max($tempProductRight, $tempProductDown, $tempProductCross);

if($tempMax > $product){
$product = $tempMax;
}
}
}
}
return $product;
}

//Method to find if the length is 400 and has the right datatypes in the string.
function validateInputData($charArray){
$counter = 0;

for($row = 0; $row < sizeof($charArray); $row++){
for($col = 0; $col < sizeof($charArray[$row]); $col++){
if($charArray[$row][$col] !== "\n"){
$val = $charArray[$row][$col];
if(is_numeric($val) != true){
return false;
}
$counter++;
}
}
}

if($counter != 400){
return false;
}

return true;
}

//Tester function is being called in the main area after echoing the answer.
function tester($charArray){
echo "<br> <br> Tester Function: <br>";

$inputWithDifferentDatatypeInBetween = '12345d7567m';

$inputWithLengthLessThanFourHundred = '71636269561882677';
  
if(validateInputData($charArray) != true){
echo "Error: Data Invalid";
echo "<br>";
}else{
// Giving out the Answer if everything is Correct.
echo "Test 1: For Correct Data: ";
echo calculateProduct($charArray);
echo "<br>";
}

if(validateInputData($inputWithDifferentDatatypeInBetween) != true){
echo "Test 2: For Incorrect Data with Datatypes other than numeric in between: ";
echo "Error: Data Invalid";
echo "<br>";
}else{
echo calculateProduct($charArray);
}

if(validateInputData($inputWithLengthLessThanFourHundred) != true){
echo "Test 3: For length not equal to 400: ";
echo "Error: Data Invalid";
echo "<br>";
}else{
echo calculateProduct($charArray);
}


}

echo "</body></html>


Related Solutions

Create a web page using PHP that allows the users to create an account (a username...
Create a web page using PHP that allows the users to create an account (a username and a password). There should be a login page that allows users to create an account by entering their preferred username and password. The same page should also let users login if they already have an account. If a user is logged in successfully , they should be able to enter a comment and also read the comments entered by others previously.
Create a web page using PHP and HTML that contains a list of movie names available...
Create a web page using PHP and HTML that contains a list of movie names available for rent (at least 10 movies), so that each time you visit the home page, you see a different order of movies in the list. The movie names are required. 2) The rental price for each movie ranges between $1.99 to $7.99. • Create an associative array with a code for the movie as the key (e.g., movie1, movie2, etc.) and the associated rental...
write php code for buying and selling web using sql, javascript ,html ,css style
write php code for buying and selling web using sql, javascript ,html ,css style
Create a Web Page Using HTML, CSS, JS, jQuery • Create a web profile, including any...
Create a Web Page Using HTML, CSS, JS, jQuery • Create a web profile, including any of the following: • Your hobbies, likes/dislikes, career aspirations, dream job, favorite animals/ pets, favorite superhero, etc. • You do not have to share personal information that you are uncomfortable with sharing. Follow these guidelines when creating your page: • Include at least one heading (h1, h2, etc.) in your web page. • Provide a quote from a book, movie, etc. • Include at...
1. An HTML document that’s generated by a web application is a ________________________ web page. 2....
1. An HTML document that’s generated by a web application is a ________________________ web page. 2. An easy way to log the progress of an application in Chrome’s Console panel is to insert __________________ methods at critical points in the code. 3. The childNodes property of a DOM object returns a/an ______________________ of the child nodes for that object. 4. The ___________________ method of an array can be used to concatenate the elements of the array into a single string.​...
Make a modest or simple Web page using Python flask. The basic components of HTML should...
Make a modest or simple Web page using Python flask. The basic components of HTML should be included. The Web page should have at least 3 Headings(<h1>), paragraph (<p>), comments (<!-- -->), ordered list, unordered list, three links to website, and should display time & date. Example: <html>     <head>         <title>Page Title</title>     </head> <body>     ..new page content.. </body> </html>
Week 4 Assignment HTML – Creating a Simple Web Page “HTML or HyperText Markup Language as...
Week 4 Assignment HTML – Creating a Simple Web Page “HTML or HyperText Markup Language as it is formally known is the main markup language for creating web pages and other information that can be displayed in a webbrowser”. It was created by Tim Berners-Lee in 1989 as a user friendly way of sharing information on the Internet. http://en.wikipedia.org/wiki/HTML Assignment Instructions - Create a simple HTML Web Page that includes hyperlinks to three of your favorite websites. Use the online...
I already have a database in phpMyAdmin and need to create an html/php "SEARCH" page to...
I already have a database in phpMyAdmin and need to create an html/php "SEARCH" page to retrieve the information from the database on to my localhost. Please post a php/html code that will create a search page and return the data from my phpmyadmin database. The 3 items I have on the database are first_name, last_name and birth_day.
use CSS, java and HTML to make a charity admin page. It's a web page after...
use CSS, java and HTML to make a charity admin page. It's a web page after you log in to your account. I can have personal information, the amount of money donated, the children who have received charity, and some blogs. In fact, all the things are what I want. But you are free to do whatever You like, even if you don't say what I say. I just need a charity Admin page for charity User.Don't ask me for...
use CSS, java and HTML to make a charity admin page. It's a web page after...
use CSS, java and HTML to make a charity admin page. It's a web page after you log in to your account. I can have personal information, the amount of money donated, the children who have received charity, and some blogs. In fact, all the things are what I want. But you are free to do whatever You like, even if you don't say what I say. I just need a charity Admin page for charity User.Don't ask me for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT