/**********************************/
/* How to execute */
/**********************************/
STEP 1 : Open terminal
STEP 2 : GOTO $ cd /opt/lamp/htdoc/assignmentByRavi/
STEP 3 : Execute the file
Scenario 1: $ php main.php data.csv 66
Scenario 2: $ php main.php data.csv 86 rectangle
Scenario 3: $ php main.php data.csv 90 circle
You will find the result with appropriate messages.
NOTE: If you provided invalid Id with the Type it gives the error message and if the result not found it also gives some valid message.
main.php
/* How to execute */
/**********************************/
STEP 1 : Open terminal
STEP 2 : GOTO $ cd /opt/lamp/htdoc/assignmentByRavi/
STEP 3 : Execute the file
Scenario 1: $ php main.php data.csv 66
Scenario 2: $ php main.php data.csv 86 rectangle
Scenario 3: $ php main.php data.csv 90 circle
You will find the result with appropriate messages.
NOTE: If you provided invalid Id with the Type it gives the error message and if the result not found it also gives some valid message.
main.php
error_reporting(0); $type = ''; if(findId($argv[1],$argv[2],$argv[3])){ if(isset($argv[3])){ $getFileData = readMyFile($argv[1],$argv[3]); }else{ $getFileData = readMyFile($argv[1]); } if(count($getFileData) > 0){ $myShape = findShapeIndex($argv[2],$getFileData); echo 'Index of ID ('.$argv[2].') is ['.$myShape[0].'] Before Sorting-- '; $sortByArea = sortByArea($myShape['type'],$getFileData); print_r($sortByArea); $findInstance = findShapeIndex($argv[2], $sortByArea); echo 'Index of ID ('.$argv[2].') is ['.$findInstance[0].'] After Sorting-- '; }else{ echo 'Invalid type'; exit;die; } }else{ echo 'Invalid ID';exit;die; } function readMyFile($fileName,$type=''){ $returnAry = array(); $file = fopen($fileName,"r"); $i = 0; $flag = 1; while($line1 = fgetcsv($file)){ if($i > 0){ if($type != ''){ if($type === $line1[1]){ $returnAry[] = array( 'area'=>getAreaOfCircle($line1[2]), 'ID'=>$line1[0], 'type'=>$line1[1], 'radius'=>$line1[2] ); $flag = 0; }else if($type === $line1[1]){ $returnAry[] = array( 'area'=>getAreaOfRectangle($line1[3],$line1[2]), 'ID'=>$line1[0], 'type'=>$line1[1], 'width'=>$line1[2], 'height'=>$line1[3] ); $flag = 0; } }else{ if($line1[3]==''){ $returnAry[] = array( 'area'=>getAreaOfCircle($line1[2]), 'ID'=>$line1[0], 'type'=>$line1[1], 'radius'=>$line1[2] ); $flag = 0; }else{ $returnAry[] = array( 'area'=>getAreaOfRectangle($line1[3],$line1[2]), 'ID'=>$line1[0], 'type'=>$line1[1], 'width'=>$line1[2], 'height'=>$line1[3] ); $flag = 0; } } } $i++; } fclose($file); if($flag > 0){ $returnAry = array(); return $returnAry; }else{ return $returnAry; } } function findId($filename,$ID,$type){ $f = fopen($filename, "r"); $result = false; while ($row = fgetcsv($f)) { if(isset($type)){ if ($row[0] == $ID && $type == $row[1]) { $result = $row[0]; break; } }else{ if ($row[0] == $ID) { $result = $row[0]; break; } } } fclose($f); return $result; } function getAreaOfCircle($radius){ $pi = 3.14; $areaOfCircle = ($radius*$radius) * $pi; return $areaOfCircle; } function getAreaOfRectangle($height, $width){ $areaOfRectangle = $height * $width; return $areaOfRectangle; } function sortByArea($type,&$shapes){ $shapes_sort = count($shapes); for($i = 0; $i < $shapes_sort; $i ++) { for($j = 0; $j < $shapes_sort; $j ++) { if($type === $shapes[$j]['type']){ if ($shapes[$i]['area'] < $shapes[$j]['area']) { $tem = $shapes[$i]; $shapes[$i] = $shapes[$j]; $shapes[$j] = $tem; } }else{ if ($shapes[$i]['area'] < $shapes[$j]['area']) { $tem = $shapes[$i]; $shapes[$i] = $shapes[$j]; $shapes[$j] = $tem; } } } } return $shapes; } function findShapeIndex($instance, $shapes){ $myShape = array(); for($i=0;$i< count($shapes); $i++){ if($shapes[$i]['ID'] == $instance){ $myShape = $shapes[$i]; array_push($myShape,$i); } } return $myShape; } exit;data.csv
| ID | type | radius | width/height |
| 2 | circle | 5 | |
| 33 | rectangle | 23 | 90 |
| 5 | rectangle | 32 | 44 |
| 23 | circle | 8 | |
| 29 | rectangle | 22 | 34 |
| 66 | circle | 2 | |
| 35 | rectangle | 54 | 63 |
| 87 | circle | 111 | |
| 86 | rectangle | 44 | 98 |
| 90 | circle | 234 | |
| 867 | circle | 66 | |
| 5309 | rectangle | 22 | 12 |
| 16 | circle | 9 | |
| 17 | rectangle | 88 | 23 |
| 56 | rectangle | 78 | 46 |
| 52 | circle | 18 | |
| 421 | rectangle | 6 | 37 |
| 99 | circle | 17 | |
| 90210 | circle | 45 | |
| 42 | rectangle | 100 | 54 |
No comments:
Post a Comment