Undesired new lines added to text document when writing to it - php

I was just trying to do a simple sorting algorithm on a matrix that I read from a matrix.txt file and append the sorted matrix back to the file.
The problem is that undesired new lines are written to the text file. I also tried in parallel to echo the same things I am writing in the text file, but the echo prints everything okay.
// .. reading the file and sorting the matrix ..
// Write the sorted matrix back to the text file
$handle = #fopen("matrix.txt", "a");
if ($handle) {
fwrite($handle, PHP_EOL . PHP_EOL . "Sorted matrix:" . PHP_EOL);
for ($i = 0; $i < $n; $i++) {
for ($j = 0; $j < $m; $j++) {
echo $matrix[$i][$j] . " ";
fwrite($handle, $matrix[$i][$j] . " ");
}
fwrite($handle, PHP_EOL);
echo "<br>";
}
fclose($handle);
}
matrix.txt file contents:
1 2 5 2 5 8 12 323 1 4
8 32 2 1 3 82 2 8 4 2
1 2 5 2 5 8 12 323 1 4
8 32 2 1 3 82 2 8 4 2
In the web browser it echoes the matrix nicely sorted, each row by itself; however, in the text file, the following is appended:
Matrix sorted using selection sort:
1 1 2 2 4
5 5 8 12 323
1 2 2 2
3 4 8 8 32 82
1 1 2 2 4
5 5 8 12 323
1 2 2 2 3 4 8 8 32 82
Any clues what could cause this? Thanks in advance !

The problem isn't in the code you posted; it's in the input matrix you provided. Notice that every extra newline corresponds to the item which used to be at the end of the row, except for the last row. That's because the final newline from each row is being included when you read the line, and explode (which I imagine you're using) doesn't know to remove it. You could simply trim the lines before exploding to fix this, or specifically remove \r and \n characters.

Related

I want to print numbers pattern program but it not give me wrong out put

I want to get my output like this
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
I am trying like this:
<?php
for($a=1; $a<=16; $a++)
{
for($b=$a; $b>=1; $b--)
{
echo "$b";
}
echo "<br>";
}
?>
The above code gives me the wrong output.
Let's debug.
You are starting from 1 in your outer loop and in your inner loop, you are going from $a till 1 times.
This doesn't comply with your requirements because we have to print an increasing sequence in each row.
You can also notice that every number in a row differs by 4.
So, logic would be like below:
Pseudocode:
rows = 4
starting_number = 1
loop from 1 to rows
number = starting_number
loop from 1 to 4 // since each row has 4 numbers
print number
number += 4
print new_line
starting_number++
Demo: https://3v4l.org/9YjIP

7001 write 8? round(7001/1000)

I have to write numbers in groups of 1000.
so, if I have 7000, I want to write:
1 2 3 4 5 6 7
but If I have 7001 or more, I want to write:
1 2 3 4 5 6 7 8
php
$num = 22501;
$num = round($num/1000);
for ($i=1; $i<=$num; $i++){
echo $i;
}
if my number is 22501 I will write 1 to 23. but if it is 22001 it will write 1 to 22.
I want to write 1 to 23. how can I do this?

Backtracking algorithm to generate a sudoku grid does not complete

I wrote a sudoku generator that creates numbers cell by cell and checks immediately after a cell has been created if it is valid (horizontally, vertically and in a 3x3 block).
Now my problem is that the algorithm always gets stuck at some point, as it won't find a valid number for the current cell. Sometimes closer to the end, sometimes already after writing 30 cells.
This is my function to check the cell, which should change the number depending on its validity:
private function checkCell($index)
{
while ($this->isValid($index) === false) {
$this->cell[$index]->setValue(rand(1, 9));
$this->counter++;
echo 'counter: ' . $this->counter;
echo PHP_EOL;
if ($this->counter > 1000) {
$this->display();
die();
}
}
}
isValid() checks if the cell is valid horizontally, vertically and in a block (this is currently not working, it just returns true).
The counter is for debugging purposes so I can see when it gets stuck.
Here is the function generating my cells:
private function fillCell($index)
{
$rand = rand(1, 9);
$this->cell[$index]->setValue($rand);
$this->checkCell($index);
}
What should be changed so the algorithm doesn't get stuck all the time?
The issue might be that the algorithm is a little too random. You end up creating a grid that is invalid and can not be completed further.
I would propose starting from a known valid grid and shuffle the cells randomly. If a cell can't be moved, we can simply skip it.
A fair warning to the reader, the following will contain pseudo code rather than working code.
A perfectly valid starting grid:
1 2 3 | 4 5 6 | 7 8 9
7 8 9 | 1 2 3 | 4 5 6
4 5 6 | 7 8 9 | 1 2 3
------|-------|------
9 1 2 | 3 4 5 | 6 7 8
6 7 8 | 9 1 2 | 3 4 5
3 4 5 | 6 7 8 | 9 1 2
------|-------|------
8 9 1 | 2 3 4 | 5 6 7
5 6 7 | 8 9 1 | 2 3 4
2 3 4 | 5 6 7 | 8 9 1
We can store this in a single dimension array, as you already appear to do.
We follow a simple logic:
We create a single dimension array containing the cells
$cells = array(
1,2,3,4,5,6,7,8,9,7,8,9,1,2,3,4,5,6,4,5,6,7,8,9,1,2,3,
9,1,2,3,4,5,6,7,8,6,7,8,9,1,2,3,4,5,3,4,5,6,7,8,9,1,2,
8,9,1,2,3,4,5,6,7,5,6,7,8,9,1,2,3,4,2,3,4,5,6,7,8,9,1,
);
We create another array containing numbers from 0 to 80 in a random order, which are the indexes of $cells
$indexes = range(0, 80);
shuffle($indexes);
We iterate over $indexes and use the value to select a random $cell in $cells
foreach($indexes as $index) {
$cell = $cells[$index];
}
For each $cell, we iterate over $cells. In each iteration, we create a temporary grid where we switch the value of the current cell with the value of the target cell. If the temporary grid is valid, we save the target index in an array of candidates
// pseudo code because that's a lot of work
$candidates = getCandidates($cell, $cells);
We randomly choose one of the candidates and switch the cells. If no candidate is available, we simply ignore this step
candidatesCount = count(candidates);
if(candidatesCount > 0) {
$candidate = $candidates[range(0, candidatesCount -1)];
// switch values
$cells[$index] = $cells[$candidate];
$cells[candidate] = $cell;
}
Repeat until $cells is processed
There are likely more efficient ways to proceed, but this logic can not get stuck.
Note that there is a low probability that the shuffle will undo itself and produce the original grid. But it's still a valid grid.
You never want to make a backtracking algorithm that uses random numbers. It can end up running infinitely.
What you want to do is:
Find the first empty cell
Try all possible values from 1 to 9 in this cell. When all values are tried, go back.
For every value you try in the cell (at step 2), recursively call the backtracking algorithm. (go back to step 1)
If the function is called and there are no empty cells, evaluate the board. If everything is ok, you found the solution! If it's not ok, go back.
The evaluation means, check that you have all numbers from 1 to 9 exactly once on every line, every column, and every 3x3 square.
Example of how it might look like:
function back($pos) {
if ($pos >= 9*9) {
if (evaluate()) {
// we found a solution
// do soemthing with it
} else {
return;
}
}
$x = pos / 9;
$y = pos % 9;
if ($m[x][y] != 0) {
// we already have a value assigned for this position
back($pos+1);
return;
}
for ($v = 1; $v <= 9; $v++) {
$m[x][y] = $v;
back($pos+1);
}
$m[x][y] = 0; // clean up tested value before going back
}
back(1)
The above algorithm can be optimized by evaluating lines/columns at every step, instead of just once at the end. If the algorithm tries to place number x, but x is already found on the line/column, then we can just move on to try x+1 since we know x will create an invalid solution.

Multiple comparison and edit file

Here is my $file1 structure (there are thousands of like that groups):
Group id_7653
{
type register
sub_name 155
1 3123 1 12
2 3124 1 8
3 3125 1 4
4 3126 1 12
5 3127 1 8
6 3128 1 4
.....
}
Group id_8731
{
type register
sub_name 155
1 4331 1 12
2 4332 1 8
3 4333 1 4
4 4334 1 12
5 4335 1 8
6 4336 1 4
.....
}
And here is my $file2 structure (again, there are thousands of defined values)
.....
3123 Spada+1
3124 Spada+2
3125 Spada+3
3126 Spada+4
3127 Spada+5
3128 Spada+6
3129 Spada+7
3130 Spada+8
.....
And here is my Worker script that makes, compares $file1 and $file2.
<?php
//read the first file in as a string
$file1 = file_get_contents("dataparser\names1.txt");
//read the second file in as an array
$file2 = file("dataparser\names2.txt");
//index from file2 that we are going to build
$file2Index = array();
foreach($file2 as $line){
//split the line
$line = explode("\t", $line, 2);
//validate the line, should be only 2 values after explode and first should be a number
if(count($line) == 2 && is_numeric($line[0])){
//add to index
$file2Index[$line[0]] = $line[1];
}
}
//now get all the values from file1 that we want (second column)
preg_match_all('/^\s*\d+\s*(\d+)\s*\d+\s*\d+\s*$/m', $file1, $matches);
$file1Values = array_unique($matches[1]);
//loop over the matches from column 2
foreach($file1Values as $value){
//check if the key doesn't exist
if(!isset($file2Index[$value])){
//echo error message
echo "Value {$value} does not exist in file2<br>";
}
}
?>
What makes that script:
Compares $file1 and $file2 and shows me which values are not defined in $file2
So far, everything works okay.
I want to extend that my script a little bit, so I want to replace that {$value} with my $file2 structure.
This time, I don't want to check that value, I want replace it directly from $file1 value. (Spada etc...)
Which paths I should follow...? Can I get some examples please...

proper arrangement in foreach

$random = rand(4, 23);
$range = range(1, $random );
HI.. guys
I have a random range value here in foreach function i want to display with below
rules.. my aim is to display like a square box
if i get range 1 to 3 it has to display table like this
1 2
3
if range from 1 to 6
1 2 3
4 5 6
if range from 1 to 19
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19
get the ceil of the square root of the number of records, and then anytime you're at an index with a mod of that value that is equal to 0, start a new line. Since you already have $random something like:
$dim = ceil(sqrt($random));
foreach ($range as $index => $number) {
print $number;
if (!(($index + 1) % $dim)) {
print "\n";
}
else {
print " ";
}
}
May need some adjustment (I'm not in PHP mode atm) and also doesn't factor in the padding but that should be straightforward.

Categories