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

Using the combination of HTML and PHP, implement a web page where the users can upload...
Using the combination of HTML and PHP, implement a web page where the users can upload a text file, exclusively in .txt format, which contains a string of 1000 numbers, such as: 71636269561882670428252483600823257530420752963450 85861560789112949495459501737958331952853208805511 65727333001053367881220235421809751254540594752243 52584907711670556013604839586446706324415722155397 53697817977846174064955149290862569321978468622482 83972241375657056057490261407972968652414535100474 82166370484403199890008895243450658541227588666881 96983520312774506326239578318016984801869478851843 12540698747158523863050715693290963295227443043557 66896648950445244523161731856403098711121722383113 05886116467109405077541002256983155200055935729725 16427171479924442928230863465674813919123162824586 17866458359124566529476545682848912883142607690042 24219022671055626321111109370544217506941658960408 07198403850962455444362981230987879927244284909188 84580156166097919133875499200524063689912560717606 62229893423380308135336276614282806444486645238749 73167176531330624919225119674426574742355349194934 30358907296290491560440772390713810515859307960866 70172427121883998797908792274921901699720888093776 Your code should contain a PHP function that, accepting the string of 1000 numbers in input, is able to: 1) Find the 5 adjacent numbers that multiplied together...
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...
please write Django web app with simple html page, that you can upload the image and...
please write Django web app with simple html page, that you can upload the image and display image and display path of the image on the PC.
Develop a personal web page for yourself using HTML, CSS, and Javascript Use the following HTML...
Develop a personal web page for yourself using HTML, CSS, and Javascript Use the following HTML tags to design your webpage: <h1>...</h1>,<h3>...</h3>, <h6>...</h6>, <p>...</p>, <b>...</b>, <i>...</i>, <a>...</a>, <img...>, <table>... </table>, <div>...</div>, <form>...</form>, <input type="text">, and <input type= "submit"> Use an external css to change the default style of your webpage. You must use at least one element selector, one id selector, and one class selector Using text input and submit button, allow the user to change the background color of...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT