Hello :) I am stuck in my mini-app developing. I have following text, copied from 3rd party web page:
Type A GZ 600 11.09.2021 12:00 OST 9
Type A GZ 601 11.09.2021 13:20 ADS 1
Type A GZ 602 11.09.2021 21:35 OCS 1
Type A GZ 603 11.09.2021 14:50 CSE 10
Type B GZ 600 11.09.2021 12:00 OST 5
Type B GZ 601 11.09.2021 13:20 ADS 3
Type B GZ 602 11.09.2021 21:35 OCS 6
Type B GZ 603 11.09.2021 14:50 CSE 12
I need to parse it to following format:
$s = 10, $ns = 11, $bs = 26, like:
echo "S:" . $s . " NS:" . $ns . " BS:" . $bs; // Output: S:10 NS:11 BS:26
where:
$fa = array("OCS", "CSE"); is array of codes
$ns is sum of Type A last column numbers, which 5 column 3-letter code is in the array,
$s is sum of Type A last column numbers, which 5 column 3-letter code is not in the array
$bs is just sum of Type B last column numbers
My code now is following:
if(!empty($_POST['indata'])){
$in_data = $_POST['indata']; // Get POST data
$fa = array("OCS", "CSE"); // Make array
$ns = 0; // Init ns value
$s = 0; // Init ss value
foreach(explode("/n",$in_data) as $line){ // Divide text to lines
$info[] = explode(" ", $line); // Divide line to values and put them to array
print_r($info); //Show input for test purposes
if(in_array($info[4], $fa)) { // Check, if 4th array value (code) is in array
$ns = $ns + $info[5]; // plus to $ns, if yes
} else {
$s = $s + $info[5]; // plus to $s, if no
}
unset($info); // clear array for next usage
}
}
But it seems not cutting line into array. It just shows me lines, not dividing to array. I am using Summernote text editor, it sends data as rows.
Because you're using $info[] = ... you get a 2 levels deep array instead of 1 level as your code is expecting. $info[] = ... basically means "Add the right hand side to $info as one element". So if the right hand side is a string and $info was empty before you'd get [0 => "my string"]. If the right hand side was an array you'd get [0 => [0 => "my", 1 => "array"]].
Do you see what I am getting at? Your code is adding one element to $info, never more than that. So to access anything in $info the first part needs to be $info[0]. But the code looks for the 4th and 5th elements, and they'll never be there. On the other hand, if you'd look for the 4th element inside the 1st one.. That is, $info[0] for the 1st element, and then the 4th inside it: $info[0][4], then you get what you're looking for.
if(!empty($_POST['indata'])){
$in_data = $_POST['indata']; // Get POST data
$fa = array("OCS", "CSE"); // Make array
$ns = 0; // Init ns value
$s = 0; // Init ss value
foreach(explode("\n",$in_data) as $line){ // Divide text to lines
$info[] = explode(" ", $line); // Divide line to values and put them to array
if(in_array($info[0][4], $fa)) { // Check, if 4th array value (code) is in array
$ns = $ns + (int) $info[0][5]; // plus to $ns, if yes
} else {
$s = $s + (int) $info[0][5]; // plus to $s, if no
}
unset($info);
}
}
var_dump($ns, $s); // int(29) int(18)
Version 2. Do away with one level in $info as mentioned earlier:
foreach(explode("\n",$in_data) as $line){
$info = explode(" ", $line);
if(in_array($info[4], $fa)) {
$ns = $ns + (int) $info[5];
} else {
$s = $s + (int) $info[5];
}
}
Alternative version, regexp:
foreach(explode("\n",$in_data) as $line){
$info = preg_split('/\s{4,}/', $line); // Split when 4 or more spaces
if(in_array($info[3], $fa)) {
$ns = $ns + (int) $info[4];
} else {
$s = $s + (int) $info[4];
}
}
That way you don't get any "junk columns" :).
Edit: I think it was PHP 7.1 that introduced some more "strictness" regarding adding values of different types, strings + numbers that is. A notice is issued, "A non well formed numeric value encountered". But if the string is cast/converted as a number before summing PHP will accept it. Casting can be done by adding (int) in front of the string value. (Provided it contains an integer value, of course, otherwise it needs to be cast differently)
After parsing the lines of text with regex, you only need to iterate the matches and conditionally sum the groups.
Code: (Demo)
$text = <<<TEXT
Type A GZ 600 11.09.2021 12:00 OST 9
Type A GZ 601 11.09.2021 13:20 ADS 1
Type A GZ 602 11.09.2021 21:35 OCS 1
Type A GZ 603 11.09.2021 14:50 CSE 10
Type B GZ 600 11.09.2021 12:00 OST 5
Type B GZ 601 11.09.2021 13:20 ADS 3
Type B GZ 602 11.09.2021 21:35 OCS 6
Type B GZ 603 11.09.2021 14:50 CSE 12
TEXT;
$fa = ["OCS", "CSE"];
$result = ['s' => 0, 'ns' => 0, 'bs' => 0];
preg_match_all(
'/^Type ([AB]).+([A-Z]{3})\h+(\d+)$/m',
$text,
$matches,
PREG_SET_ORDER
);
foreach ($matches as $m) {
if ($m[1] === 'B') {
$result['bs'] += $m[3];
} elseif (in_array($m[2], $fa)) {
$result['s'] += $m[3];
} else {
$result['ns'] += $m[3];
}
}
var_export($result);
Output:
array (
's' => 11,
'ns' => 10,
'bs' => 26,
)
Related
I truly thinking too long to find the logic/ algorithm to do this thing. Then I just use if else but I know this is bad because there will be too much statement.
I have number format group below to split the input number :
01 (Get 14 digits after this number format)
3101 (Get 6 digits after this number format)
3102 (Get 6 digits)
3202 (Get 6 digits)
13 (Get 6 digits)
15 (Get 6 digits)
11 (Get 6 digits)
21 (Get the rest)
Some of rules :
01 always in first sequence
21 always in last sequence
other number format except 01 and 21 can be in any sequence position.
The same prefix number format cannot be repeat
example, Input Number : 010069008517306731020020001319100421191004091395
The Result Should be :
01 : 00690085173067
3102 : 002000
13 : 191004
21 : 191004091395
Currently I only use IF ELSE statement to get the digits after.
This is my pieces of code using PHP. This code can only handle that example input above. There will be possibility of other sequence number format as per rules, but it will difficult if only use if else statement like this.
$first = substr($input, 0, 2);
if ($first == 01) {
$itemCode = substr($input, 2, 14); // get the 6 digits after 01
$second = substr($input, 16, 4);
if ($second == 3102) {
$quantity = substr($input, 20, 6); // get the 6 digits after 3102
$third = substr($input, 26, 2);
if ($third == 13) {
$packedDate = substr($input, 28, 6); // get the 6 digits after 13
$fourth = substr($input, 34, 2);
if ($fourth == 21) {
$serialNumber = substr($scanner, 36); // get the rest number after 21
}
}
}
}
Is there any good way to solve this thing?
If the prefixes won't repeat, you can use preg_match_all to match the prefixes with each of their trailing digits, using array_combine to create arrays of digits indexed by their prefixes:
$input = '010069008517306731020020001319100421191004091395';
if (preg_match_all('/(01)(\d{14})|(310[12]|3202|1[135])(\d{6})|(21)(\d+)/', $input, $matches)) {
$numbers = array_filter(array_combine($matches[1], $matches[2]) +
array_combine($matches[3], $matches[4]) +
array_combine($matches[5], $matches[6]));
print_r($numbers);
}
else {
echo "Invalid input!";
}
Output:
Array
(
[01] => 00690085173067
[3102] => 002000
[13] => 191004
[21] => 191004091395
)
Demo on 3v4l.org
I have text file having the following format
00151422 N8X 3V6 2013-11-11 00:19:00.000 IN patricksoong#hotmail.com E 200-2462 Howard Avenue Windsor ON CAN N8X3V6 M Dr. Patrick Soong
00331448 T6E 2R1 2010-03-01 00:00:00.000 IN atirlea#yahoo.com E 9743 88 Ave NW Edmonton AB CAN T6E2R1 Alina Tirlea Engstrom
00364578 K7N 1A3 2011-01-12 00:00:00.000 IN E 4463 Bath Rd Amherstview ON CAN K7N1A3 M Mr. Martin Kandler
The above positional text file contains 3 records and 20 fields in each record. Also I now the size for each column. How will i read records and fields with in a record using PHP?
Size of fields are
f1=8;f2=10;f3=10;f4=10;f5=255;f6=50;f7=255;f8=10;f9=10;f10=50;f11=50;f12=1;f13=20;f14=50;f15=50;f16=60;f17=10;f18=20;f19=20;f20=1;
Use a substr() inside some kind of loop. Untested, but should give you an idea:
$lengths = [8,10,10]; // define all the lengths
function fixed_width_data_to_array($data, $lengths) {
foreach($rows as $row) {
$position = 0; // start at the beginning of the row
foreach($lengths as $length) {
// add current field to array
$my_data[] = trim(substr($row, $position, $length));
// move the 'pointer' to the start of the next field
$position += $length;
}
// add current row to an array
$my_array[] = $my_data;
}
return $my_array;
}
$array = array(1, 2, 3, 4, 5, ..., N);
Also there is a number D = 10%. What is the fastest way to sort the array in such way that:
$sorted_array = {a[i]}
contains exactly the elements of $array in a mixed order, but also:
abs(a[i + 1] - a[i]) >= N * 10%
for any [i] and look randomized as much as possible.
For example,
// assume D = 25%
$array = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// so the difference between any neighbors is >= 4 = 10 * 25%.
$sorted_array = array(4, 8, 3, 7, 1, 5, 9, 2, 6, 10);
Of course if D is large, it is impossible to sort the array I want. I don't need the 100% perfect result, but I want the numbers to look "randomized" and most of them to be different at least for 10%.
I have a strange task but it has a practical area to use. I want to extract randomized lines from the image and they should be different as much as possible. Of course, the neighbor lines on the digital images (photos etc) look very similar.
Did I explain it properly?
I know it's not a good idea just to provide code, but I was intrigued by this question. Here's how I would do it:
$d = 0.3;
$random = array();
// Populate the original array
for ($n=1; $n <= 10; $n++) {
$arr[] = $n;
}
$count = count($arr);
// Loop through array
foreach (array_keys($arr) as $key) {
if (!isset($prev_key)) {
$prev_key = array_rand($arr);
}
$possibles = array(); // This stores the possible values
echo "Trying: $prev_key";
echo ":\n";
// Loop through the array again and populate $possibles with all possible
// values based on the previous values
foreach (array_keys($arr) as $n) {
if ($arr[$n] < $prev_key - $count * $d || $arr[$n] > $prev_key + $count * $d) {
$possibles[] = $n;
echo $arr[$n]." is valid\n";
}
else {
echo $arr[$n];
echo " outside range\n";
}
}
// If there is nothing outside that range, just return the remaining values
if (count($possibles) == 0) {
$possibles = array_keys($arr);
echo "Nothing within range so just returning whole array\n";
}
echo "\n";
// Choose random value from the possible values array
$rand_key = $possibles[array_rand($possibles)];
$random[] = $arr[$rand_key];
$prev_key = $arr[$rand_key];
// Unset this value from the original array since we can only use the
// values once
unset($arr[$rand_key]);
}
print_r($random);
This will produce output like this:
Trying: 8:
1 is valid
2 is valid
3 is valid
4 is valid
5 outside range
6 outside range
7 outside range
8 outside range
9 outside range
10 outside range
Trying: 2:
1 outside range
3 outside range
4 outside range
5 outside range
6 is valid
7 is valid
8 is valid
9 is valid
10 is valid
Trying: 9:
1 is valid
3 is valid
4 is valid
5 is valid
6 outside range
7 outside range
8 outside range
10 outside range
Trying: 5:
1 is valid
3 outside range
4 outside range
6 outside range
7 outside range
8 outside range
10 is valid
Trying: 10:
1 is valid
3 is valid
4 is valid
6 is valid
7 outside range
8 outside range
Trying: 4:
1 outside range
3 outside range
6 outside range
7 outside range
8 is valid
Trying: 8:
1 is valid
3 is valid
6 outside range
7 outside range
Trying: 3:
1 outside range
6 outside range
7 is valid
Trying: 7:
1 is valid
6 outside range
Trying: 1:
6 is valid
Array
(
[0] => 2
[1] => 9
[2] => 5
[3] => 10
[4] => 4
[5] => 8
[6] => 3
[7] => 7
[8] => 1
[9] => 6
)
The only drawback is that since it randomly gets rows, there is a chance that the values near the end may not be outside the defined range. By my tests, this happens to about 4% using the above $d = 0.25 and 1000 values. One way to get around this is just to insert these values back in at random places instead of appending them like I have done.
Also note, this method is not that efficient. It has to loop through the array count($arr) ^ 2 times. So for 1000 values, you're looking at 1,000,000 iterations. Fortunately the array gets progressively smaller.
I have a PHP web application which an option of generating invoices. The Problem is i need to print the invoices using the dot matrix printer in draft mode. I have tries, but it prints like a normal printer with bigger fonts. simply it prints in the format as same in the page.
I need to print like receipts. Do I need to format anything with the PHP?
Well, I think you need to print in raw mode.
I do not claim I have completely solved this issue but here are my tries.
Firstly, there should be a "generic / text only" driver on the system where the dot matrix printer is connected to, probably in port LPT1.
Secondly, you should send ESCP commands to the printer and not use the build in processes of php to print.
You could for example create an array of 135 * 66 characters, where 135 is the usual page width in characters and 66 the usual number of printing lines (on a 24 pin dot matrix printer with a 8-1/2" x 12" paper size). You should be careful to fill the array with characters and not with strings, lie this:
$GRAND_ARRAY = array();
$maxCharactersPerLine = 135;
$maxLines = 66;
//initialize the array with spaces and the last column of \r\n
for($i=0; $i<$maxLines; $i++){
$pos = $i*$maxCharactersPerLine + ($maxCharactersPerLine-1) + $i + 1;
$value = "\r\n";
$this->printChars($GRAND_ARRAY, $pos, $value);
}
//make an array that fits every one character of the page
//plus a column for the "next line" character
$totalCells = $maxCharactersPerLine * $maxLines + $maxLines - 1;
//what you want to print
$value = "name:"; //here is a string with 5 characters
//where you want to print it on the page
//$line = 2
//$column = 5
//consider that array starts at 0 and not 1
//$pos = ($column - 1) * $maxCharactersPerLine + $line + $column - 2;
$pos = 275; //position is 5th character in line 2
//do not just $GRAND_ARRAY[$pos] = $value because you should
//make sure that every array cell has only one character
$this->printChars($GRAND_ARRAY, $pos, $value, 1);
// also included a flag if you want to print only the $limit characters of $newstring
public function printChars(&$mainArray, $start, $newString, $limit=0){
$j = $start;
$charsArray = $this->mb_str_split($newString);
//$len = mb_strlen($newString, "UTF-8"); //can't remember why not this
$len = count($charsArray);
for($i=0; $i<$len; $i++){
if($limit > 0 && $limit < $i){
return;
}else{
$mainArray[$j] = $charsArray[$i];
$j = $j + 1;
}
}
}
//this I found here on s.o. i think.
public function mb_str_split( $string ) {
# Split at all position not after the start: ^
# and not before the end: $
return preg_split('/(?<!^)(?!$)/u', $string );
}
After creating the array I used qz-print plugin (https://code.google.com/p/jzebra/) that includes javascript functions and a java applet in order to send this array to the "generic / text only" dot matrix printer in the client.
The javascript function I use is:
//just before that, I turn the array into string str.
function printESCP2(str, type, quality, pitch, line_spacing, topOffset, commands) {
if (notReady()) { return; }
qz.appendHex("x1Bx40"); //ESC # for reset settings
var j = commands.length;
for(var k=0;k<j;k++){
console.log(':'+commands[k]);
qz.appendHex(commands[k]);
}
if(topOffset){
if(topOffset > 0){
alert("topOffset: "+topOffset);
for(var k=0; k<topOffset; k++){
qz.appendHex("x0D"); //CR for carriage return
qz.appendHex("x0A"); //LF for line feed
}
}
}
if(quality == <?php echo SettingsPrinter::FONT_QUALITY_LQ; ?>){
alert("font quality: LQ");
qz.appendHex("x1Bx78x31"); // ESC x 1 for LQ (31=1)
}else{
alert("font quality: draft");
qz.appendHex("x1Bx78x30"); // ESC x 0 for Draft (30=0)
}
if(line_spacing == <?php echo SettingsPrinter::PRINTER_LINE_SPACING_NARROW; ?>){
//alert("line spacing: narrow (1/8)");
qz.appendHex("x1Bx30"); //ESC 0 for line spacing minimum (1/8)
}else{
//alert("line spacing: normal (1/6)");
qz.appendHex("x1Bx32"); //ESC 2 for line spacing normal (1/6)
}
switch(pitch){
case '<?php echo SettingsPrinter::FONT_PITCH_10; ?>':
alert("pitch: 10");
qz.appendHex("x1Bx50"); //ESC P for pitch 10 cpi
break;
case '<?php echo SettingsPrinter::FONT_PITCH_12; ?>':
alert("pitch: 12");
qz.appendHex("x1Bx4D"); //ESC M for pitch 12 cpi
break;
case '<?php echo SettingsPrinter::FONT_PITCH_15; ?>':
alert("pitch: 15");
qz.appendHex("x1Bx67"); // ESC g for pitch 15 cpi
break;
case '<?php echo SettingsPrinter::FONT_PITCH_17; ?>':
alert("pitch: 17");
qz.appendHex("x1Bx50"); //ESC P for pitch 10 cpi
qz.appendHex("x0F"); // SI for condensed resulting in pitch 17 cpi
break;
case '<?php echo SettingsPrinter::FONT_PITCH_20; ?>':
alert("pitch: 20");
qz.appendHex("x1Bx4D"); //ESC M for pitch 12 cpi
qz.appendHex("x0F"); // SI for condensed resulting in pitch 20 cpi
break;
default:
alert("pitch unknown -- not set");
break;
}
qz.append(str);
qz.appendHex("x0D"); //CR for carriage return
qz.appendHex("x1Bx40"); //ESC # for reset settings
window["qzDoneAppending"] = function() {
// Tell the apple to print.
qz.print();
// Remove any reference to this function
window['qzDoneAppending'] = null;
};
}
One can actually create the array of characters-to-print as a two dimension array and ommit the last column of \r\n as I think this is not well accepted by all dot matrix printers. In that case one should do
//for each line of the array
qz.append($line); //line-1 turned into string, line-2 etc
qz.appendHex("x0D"); //CR for carriage return after each line
One problem that I came across is that if the user chose 17CPI then every character holds less space in the paper. This means that you cannot depend on one space characters to spread your data across the entire page in this case.
If anyone thinks they can contribute to this they are most welcome.
I am doing this programming challenge which can be found at www.interviewstreet.com (its the first challenge worth 30 points).
When I submitted the solution, I was returned a result which said that the answer was wrong because it only passed 1/11 test cases. However, I feel have tested various cases and do not understand what I am doing wrong. It would be helpful to know what those test cases could be so that I can test my program.
Here is the question (in between the grey lines below):
Quadrant Queries (30 points)
There are N points in the plane. The ith point has coordinates (xi, yi). Perform the following queries:
1) Reflect all points between point i and j both including along the X axis. This query is represented as "X i j"
2) Reflect all points between point i and j both including along the Y axis. This query is represented as "Y i j"
3) Count how many points between point i and j both including lie in each of the 4 quadrants. This query is represented as "C i j"
Input:
The first line contains N, the number of points. N lines follow.
The ith line contains xi and yi separated by a space.
The next line contains Q the number of queries. The next Q lines contain one query each, of one of the above forms.
All indices are 1 indexed.
Output:
Output one line for each query of the type "C i j". The corresponding line contains 4 integers; the number of points having indices in the range [i..j] in the 1st,2nd,3rd and 4th quadrants respectively.
Constraints:
1 <= N <= 100000
1 <= Q <= 100000
You may assume that no point lies on the X or the Y axis.
All (xi,yi) will fit in a 32-bit signed integer
In all queries, 1 <=i <=j <=N
Sample Input:
4
1 1
-1 1
-1 -1
1 -1
5
C 1 4
X 2 4
C 3 4
Y 1 2
C 1 3
Sample Output:
1 1 1 1
1 1 0 0
0 2 0 1
Explanation:
When a query says "X i j", it means that take all the points between indices i and j both including and reflect those points along the X axis. The i and j here have nothing to do with the co-ordinates of the points. They are the indices. i refers to point i and j refers to point j
'C 1 4' asks you to 'Consider the set of points having index in {1,2,3,4}. Amongst those points, how many of them lie in the 1st,2nd,3rd and 4th quads respectively?'
The answer to this is clearly 1 1 1 1.
Next we reflect the points between indices '2 4' along the X axis. So the new coordinates are :
1 1
-1 -1
-1 1
1 1
Now 'C 3 4' is 'Consider the set of points having index in {3,4}. Amongst those points, how many of them lie in the 1st,2nd,3rd and 4th quads respectively?' Point 3 lies in quadrant 2 and point 4 lies in quadrant 1.
So the answer is 1 1 0 0
I'm coding in PHP and the method for testing is with STDIN and STDOUT.
Any ideas on difficult test cases to test my code with? I don't understand why I am failing 10 / 11 test cases.
Also, here is my code if you're interested:
// The global variable that will be changed
$points = array();
/******** Functions ********/
// This function returns the number of points in each quadrant.
function C($beg, $end) {
// $quad_count is a local array and not global as this gets reset for every C operation
$quad_count = array("I" => 0, "II" => 0, "III" => 0, "IV" => 0);
for($i=$beg; $i<$end+1; $i++) {
$quad = checkquad($i);
$quad_count[$quad]++;
}
return $quad_count["I"]." ".$quad_count["II"]." ".$quad_count["III"]." ".$quad_count["IV"];
}
// Reflecting over the x-axis means taking the negative value of y for all given points
function X($beg, $end) {
global $points;
for($i=$beg; $i<$end+1; $i++) {
$points[$i]["y"] = -1*($points[$i]["y"]);
}
}
// Reflecting over the y-axis means taking the negative value of x for all given points
function Y($beg, $end) {
global $points;
for($i=$beg; $i<$end+1; $i++) {
$points[$i]["x"] = -1*($points[$i]["x"]);
}
}
// Determines which quadrant a given point is in
function checkquad($i) {
global $points;
$x = $points[$i]["x"];
$y = $points[$i]["y"];
if ($x > 0) {
if ($y > 0) {
return "I";
} else {
return "IV";
}
} else {
if ($y > 0) {
return "II";
} else {
return "III";
}
}
}
// First, retrieve the number of points that will be provided. Make sure to check constraints.
$no_points = intval(fgets(STDIN));
if ($no_points > 100000) {
fwrite(STDOUT, "The number of points cannot be greater than 100,000!\n");
exit;
}
// Remember the points are 1 indexed so begin key from 1. Store all provided points in array format.
for($i=1; $i<$no_points+1; $i++) {
global $points;
list($x, $y) = explode(" ",fgets(STDIN)); // Get the string returned from the command line and convert to an array
$points[$i]["x"] = intval($x);
$points[$i]["y"] = intval($y);
}
// Retrieve the number of operations that will be provied. Make sure to check constraints.
$no_operations = intval(fgets(STDIN));
if($no_operations > 100000) {
fwrite(STDOUT, "The number of operations cannot be greater than 100,000!\n");
exit;
}
// Retrieve the operations, determine the type and send to the appropriate functions. Make sure i <= j.
for($i=0; $i<$no_operations; $i++) {
$operation = explode(" ",fgets(STDIN));
$type = $operation[0];
if($operation[1] > $operation[2]) {
fwrite(STDOUT, "Point j must be further in the sequence than point i!\n");
exit;
}
switch ($type) {
case "C":
$output[$i] = C($operation[1], $operation[2]);
break;
case "X":
X($operation[1], $operation[2]);
break;
case "Y":
Y($operation[1], $operation[2]);
break;
default:
$output[$i] = "Sorry, but we do not recognize this operation. Please try again!";
}
}
// Print the output as a string
foreach($output as $line) {
fwrite(STDOUT, $line."\n");
}
UPDATE:
I finally found a test case for which my program fails. Now I am trying to determine why. This is a good lesson on testing with large numbers.
10
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
12
C 1 10
X 1 3
C 5 5
Y 2 10
C 10 10
C 1 10
X 1 3
C 5 5
Y 2 10
C 10 10
X 3 7
C 9 9
I am going to test this properly by initializing an error array and determining which operations are causing an issue.
I discovered a test case that failed and understood why. I am posting this answer here so it's clear to everyone.
I placed a constraint on the program so that j must be greater than i, otherwise an error should be returned. I noticed an error with the following test case:
10
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1
C 2 10
The error returned for the operation C. Essentially the program believed that "2" was greater than "10". The reason for this I discovered was the following:
When using fgets(), a string is returned. If you perform string operations such as explode() or substr() on that line, you are converting the numbers in that initial string into a string again. So this means that the 10 becomes "10" and then after string operations becomes "0".
One solution to this is to use the sscanf() function and basically tell the program to expect a number. Example: for "C 2 10" you could use:
$operation_string = fgets(STDIN);
list($type, $begpoint, $endpoint) = sscanf($operation_string, "%s %d %d");
I submitted the new solution using sscanf() and now have 3/11 test cases passed. It did not check any more test cases because the CPU time limit was exceeded. So, now I have to go back and optimize my algorithm.
Back to work! :)
To answer, "What are those test cases?" Try this "solution":
<?php
$postdata = http_build_query(
array(
'log' => file_get_contents('php://stdin')
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
file_get_contents('http://myserver/answer.php', false, $context);
?>
On your server:
<?php
$fp = fopen('/tmp/answers.log', 'a');
fputs($fp, $_POST['log']."\n");
fclose($fp);
?>
Edit:
I did that. And came up with this being your main problem (I think):
$operation = explode(" ",fgets(STDIN));
Change that to:
$operation = explode(" ",trim(fgets(STDIN)));
Because otherwise "9" > "41 " due to string comparison. You should make that fix in any place you read a line.
As far as I guess, this solution won't work. Even if you solve the Wrong Answer problem, the solution will time out.
I was able to figure out a way for returning the quadrants count in O(1) time.
But not able to make the reflections in lesser time. :(