Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
Last active June 24, 2020 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lbvf50mobile/b363e9a160ab2c1c451dadd574ea49bb to your computer and use it in GitHub Desktop.
Save lbvf50mobile/b363e9a160ab2c1c451dadd574ea49bb to your computer and use it in GitHub Desktop.
Just PHP FUN 032.
<?php
# https://www.codewars.com/kata/55d17ddd6d7868493e000074 Linked Lists - Append.
function append($list_a, $list_b) {
$list_a = cln($list_a);
$list_b = cln($list_b);
echo "Start---------------\n";
echo "A: \n";
prnt($list_a);
echo "B: \n";
prnt($list_b);
if(is_null($list_a) && is_null($list_b)) return NULL;
if(is_null($list_a)) return $list_b;
$x = $list_a;
while( !is_null($x->next)){
$x->mark = true;
$x = $x->next;
}
$x->mark = true;
echo "Now all A must be marked\n";
prnt($list_a);
$x->next = $list_b;
echo "Ans: \n";
prnt($list_a);
return $list_a;
}
function cln($l){
if(!$l) return null;
$ans = new Node($l->data);
$tmp = $ans; $l = $l->next;
while($l){
$tmp->next = new Node($l->data);
$tmp = $tmp->next;
$l = $l->next;
}
return $ans;
}
function prnt($l){
while($l){ echo $l->data;
if(isset($l->mark)) echo ".";
echo "->";
$l = $l->next; }
echo "null \n";
}
<?php
# https://www.codewars.com/kata/58ddffda929dfc2cae0000a5 Send in the Clones.
function clonewars(int $n): array {
if(0 == $n) return [1,0];
$sum = 0;
for($i = 0; $i < $n; $i += 1) $sum += (2**$i)*($n - $i);
return [(int)2**($n-1),(int)$sum];
}
<?php
# https://www.codewars.com/kata/554b4ac871d6813a03000035 Highest and Lowest.
function highAndLow($n)
{
$max = max(explode(" ",$n));
$min = min(explode(" ",$n));
return "$max $min";
}

Just PHP FUN 032.

Started at 19:27 at 24.06.2020 Wednesday. Finished at 0:29 at 25.06.2020 Thursday. (5hs 3minues)

Linked Lists - Append Error in the random tests.

Random tests in PHP create DEAD LOOPS. Because same nodes uesed in a different tests and point on each other. For example 70->null and 8->19->null. Get answer 70 -> 8 -> 19 -> null. Then take 8 -> 19 the middle of ALREADY EXISTED LIST and ask to prepend it to the head 70. Or append head to a tail. Eventually DEAD LOOP appears: 70->8->19->70....

Start---------------
A: 
70->null 
B: 
8->19->null 
Now all A must be marked
70.->null 
Ans: 
70.->8->19->null 
Start---------------
A: 
8->19->null 
B: 
70.->8->19->null 
Now all A must be marked
8.->19.->null 
Ans: 
8.->19.->70.->8.->19.->70.->8.->19.->70.->8.->19.->70.->

PHP codee for testing.

<?php
# https://www.codewars.com/kata/55d17ddd6d7868493e000074 Linked Lists - Append.
function append($list_a, $list_b) {
  echo "Start---------------\n";
  echo "A: \n";
  prnt($list_a);
  echo "B: \n";
  prnt($list_b);
  
  if(is_null($list_a) && is_null($list_b)) return NULL;
  if(is_null($list_a)) return $list_b;
  $x = $list_a;
  while( !is_null($x->next)){ 
    $x->mark = true;
    $x = $x->next;
  }
  $x->mark = true;
  
  echo "Now all A must be marked\n";
  prnt($list_a);
  $x->next = $list_b;
  echo "Ans: \n";
  prnt($list_a);
  return $list_a;
}
function prnt($l){
  while($l){ echo $l->data;
  if(isset($l->mark)) echo ".";
  echo "->";
  
  $l = $l->next; }
  echo "null \n";
}
<?php
# https://www.codewars.com/kata/555eded1ad94b00403000071 Sum of the first nth term of Series.
function series_sum($n) {
if(0 == $n) return "0.00";
$x = 4;
$sum = 1.00;
for($i = 2; $i <= $n; $i++){
$sum += 1/$x;
$x = $x + 3;
$sum = round($sum,5);
}
return sprintf("%.2f", $sum);
}
<?php
# https://www.codewars.com/kata/55efecb8680f47654c000095 Hero's root.
function intRac($n, $guess) {
$ans = [];
while (true){
$tmp = $guess;
array_push($ans,$tmp);
$guess = floor(($guess + $n/$guess)/2);
if(0 == abs($tmp - $guess)) break;
}
echo implode(",",$ans)." last $guess \n";
return count($ans);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment