I have a PHP code. The code is supposed to print the output followed by a new line.
The code works fine but i have unneccesary new line at the end. There should be only one newline at the end, but my code prints several new lines. What could be the issue? Please help.
<?php
/* Read input from STDIN. Print your output to STDOUT*/
$fp = fopen("php://stdin", "r");
//Write code here
$loop = 0;
$n = 0; $arr = [];
while(!feof($fp)) {
$arr = []; $n = 0;
if($loop == 0) {
$total = fgets($fp);
}
else {
if($loop%2 == 1) {
$n = fgets($fp);
}
else {
$arr = fgets($fp);
}
}
if($loop > 0 && $loop%2 == 0) {
$arr = explode(" ", $arr);
$m = [];
for($i = 0; $i < 1<<10; $i++) {
$m[$i] = -1;
}
$n = count($arr);
$r = 0;
for($i = 0; $i < 1<<10; $i++) {
$r = max($r, fd_sum($i, $m, $arr, $n));
}
echo $r."\n";
}
$loop++;
}
fclose($fp);
?>
<?php
function fd_sum($i, $m, $arr, $n) {
if($i == 0) {
return $m[$i] = 0;
}
else if($m[$i] != -1) {
return $m[$i];
}
else {
$rr = 0;
for($j = 0; $j < $n; $j++) {
$num = (int)$arr[$j];
$b = save($num);
if(($i | $b) == $i) {
$z = $i^save($num);
$y = fd_sum($z, $m, $arr, $n);
$v = ($y + $num);
$rr = max($v, $rr);
}
}
return $m[$i] = $rr;
}
}
?>
<?php
function save($nm)
{
$x = 0;
for($i = 1; $nm/$i > 0; $i *= 10) {
$d = ($nm/$i) % 10;
$x = $x | (1 << $d);
}
return $x-1;
}
?>
My input is
3
4
3 5 7 2
5
121 3 333 23 4
7
32 42 52 62 72 82 92
My output is
17
458
92
-
-
-
-
The expected output is
17
458
92
-
Note : I have used '-' to indicate a new line
What am i doing wrong? Please help.
The PHP interpreter is reading the new lines after the closing tags and just spitting it right back out as output. Removing the extra opening/closing tags should remove the extra new lines.
Also, php closing tags are not necessary and i recommend omitting them.
<?php
/* Read input from STDIN. Print your output to STDOUT*/
$fp = fopen("php://stdin", "r");
//Write code here
$loop = 0;
$n = 0; $arr = [];
while(!feof($fp)) {
$arr = []; $n = 0;
if($loop == 0) {
$total = fgets($fp);
}
else {
if($loop%2 == 1) {
$n = fgets($fp);
}
else {
$arr = fgets($fp);
}
}
if($loop > 0 && $loop%2 == 0) {
$arr = explode(" ", $arr);
$m = [];
for($i = 0; $i < 1<<10; $i++) {
$m[$i] = -1;
}
$n = count($arr);
$r = 0;
for($i = 0; $i < 1<<10; $i++) {
$r = max($r, fd_sum($i, $m, $arr, $n));
}
echo $r."\n";
}
$loop++;
}
fclose($fp);
function fd_sum($i, $m, $arr, $n) {
if($i == 0) {
return $m[$i] = 0;
}
else if($m[$i] != -1) {
return $m[$i];
}
else {
$rr = 0;
for($j = 0; $j < $n; $j++) {
$num = (int)$arr[$j];
$b = save($num);
if(($i | $b) == $i) {
$z = $i^save($num);
$y = fd_sum($z, $m, $arr, $n);
$v = ($y + $num);
$rr = max($v, $rr);
}
}
return $m[$i] = $rr;
}
}
function save($nm)
{
$x = 0;
for($i = 1; $nm/$i > 0; $i *= 10) {
$d = ($nm/$i) % 10;
$x = $x | (1 << $d);
}
return $x-1;
}
Related
I would like to make a program which can calculate first 5000 primary numbers whos ends with 9 :
I tried this but it didn't work :
$div9 = [];
$x = 2;
while (count($div9) <= 5000) {
function findPrime($x)
{
for ($i = 2; $i < ($x / 2); $i++) {
$rest = $x % $i;
if ($rest == 0) {
break;
}
}
return $x;
}
$primeList[] = $x;
for ($j = 0; $j < count($primeList); $j++) {
$array = array_map('intval', str_split($primeList[$j]));
if (end($array[$j]) === 9) {
return $primeList[$j];
$div9[] = $primeList[$j];
}
}
$x++;
}
any hints please?
You should not define a function inside your while loop
This should help
function check_prime($num)
{
if ($num == 1)
return false;
for ($i = 2; $i <= $num/2; $i++)
{
if ($num % $i == 0)
return false;
}
return true;
}
$div9 = [];
$i = 0;
while(count($div9) < 5000) {
if($i%10 === 9 && check_prime($i)) {
$div9[] = $i;
}
$i++;
}
Another variation on the theme, the isPrime function was ported from Javascript cryptoJS library.
# Adapted from CryptoJS v3.1.2
function isPrime( $n=0 ){
$r=sqrt( $n );
for( $f=2; $f <= $r; $f++ ){
if( !( $n % $f ) )return false;
}
return true;
}
function isFactor($n,$f){
return $n % 10 == $f;
}
$limit=5000;
$primes=[];
$x=2;
$f=9;
while( count( $primes ) < $limit ){
if( isPrime( $x ) && isFactor( $x, $f ) )$primes[]=$x;
$x++;
}
printf('<pre>%s</pre>',print_r($primes,true));
Is it possible to add or concatenate something into a variable name in a PHP variable? For example:
for($g = 7; $g <= 10; $g++){
for($i = 0; $i <= 4; $i++){
$counter = $g - 7;
if($i != $counter){
continue;
} else {
$grade.[$g] = $grades[$i];
}
}
}
I want this to happen:
$grade7 = 0
$grade8 = 1
$grade9 = 2
$grade10 = 3
Concatenates the $g with grade and make this value a variable by adding a $ sign at the starting line...
The example given below:
for($g = 7; $g <= 10; $g++){
for($i = 0; $i <= 4; $i++){
$counter = $g - 7;
if($i != $counter){
continue;
} else {
${"grade".$g} = $grades[$i];
}
}
}
echo $grade7; // 0
echo $grade8; // 1
echo $grade9; // 2
echo $grade10;// 3
One solution would be to create the variables dynamically:
for ($g = 7; $g <= 10; $g++) {
for ($i = 0; $i <= 4; $i++) {
$counter = $g - 7;
if ($i == $counter) {
${'grade' . $g} = $grades[$i];
}
}
}
You should use ARRAY instead of your method. :)
Try to look at variable named like "$$".
EDIT: Maybe something like
var $array = array();
for($g=7; $g<=10; $g++)
{
for($i=0; $i<=4; $i++)
{
$counter = $g - 7;
if($i != $counter) continue;
else $array[$grade.[$g]] = $grades[$i];
}
}
$data = new Array();
for($g = 7; $g <= 10; $g++){
for($i = 0; $i <= 4; $i++){
$counter = $g - 7;
if($i != $counter){
continue;
} else {
$data[$grade.[$g]] = $grades[$i]);
}
}
}
Im trying to solve one challenge where you have to check all string substrings are they anagrams. The condition is basically For S=abba, anagramic pairs are: {S[1,1],S[4,4]}, {S[1,2],S[3,4]}, {S[2,2],S[3,3]} and {S[1,3],S[2,4]}
Problem is that I have string with 100 chars and execution time should be below 9 secs. My time is around 50 secs... Below is my code, I will appreciate any advice - if you give me only directions or pseudo code it is even better.
$time1 = microtime(true);
$string = 'abdcasdabvdvafsgfdsvafdsafewsrgsdcasfsdfgxccafdsgccafsdgsdcascdsfsdfsdgfadasdgsdfawdascsdsasdasgsdfs';
$arr = [];
$len = strlen($string);
for ($i = 0; $i < strlen($string); $i++) {
if ($i === 0) {
for ($j = 1; $j <= $len - 1; $j++) {
$push = substr($string, $i, $j);
array_push($arr, $push);
}
} else {
for ($j = 1; $j <= $len - $i; $j++) {
$push = substr($string, $i, $j);
array_push($arr, $push);
}
}
}
$br = 0;
$arrLength = count($arr);
foreach ($arr as $key => $val) {
if ($key === count($arr) - 1) {
break;
}
for ($k = $key + 1; $k < $arrLength; $k++) {
if (is_anagram($val, $arr[$k]) === true) {
$br++;
}
}
}
echo $br."</br>";
function is_anagram($a, $b)
{
$result = (count_chars($a, 1) == count_chars($b, 1));
return $result;
}
$time2 = microtime(true);
echo "Script execution time: ".($time2-$time1);
Edit:
Hi again, today I had some time so I tried to optimize but couldnt crack this... This is my new code but I think it got worse. Any advanced suggestions ?
<?php
$string = 'abdcasdabvdvafsgfdsvafdsafewsrgsdcasfsdfgxccafdsgccafsdgsdcascdsfsdfsdgfadasdgsdfawdascsdsasdasgsdfs';
$arr = [];
$len = strlen($string);
for ($i = 0; $i < strlen($string); $i++) {
if ($i === 0) {
for ($j = 1; $j <= $len - 1; $j++) {
$push = substr($string, $i, $j);
array_push($arr, $push);
}
} else {
for ($j = 1; $j <= $len - $i; $j++) {
$push = substr($string, $i, $j);
array_push($arr, $push);
}
}
}
$br = 0;
$arrlen = count ($arr);
foreach ($arr as $key => $val) {
if (($key === $arrlen - 1)) {
break;
}
for ($k = $key + 1; $k < $arrlen; $k++) {
$result = stringsCompare($val,$arr[$k]);
if ($result === true)
{
$br++;
}
}
echo $br."\n";
}
function stringsCompare($a,$b)
{
$lenOne = strlen($a);
$lenTwo = strlen ($b);
if ($lenOne !== $lenTwo)
{
return false;
}
else {
$fail = 0;
if ($lenOne === 1) {
if ($a === $b) {
return true;
}
else
{
return false;
}
}
else
{
for ($x = 0; $x < $lenOne; $x++)
{
$position = strpos($b,$a[$x]);
if($position === false)
{
$fail = 1;
break;
}
else
{
$b[$position] = 0;
$fail = 0;
}
}
if ($fail === 1)
{
return false;
}
else
{
return true;
}
}
}
}
?>
You should think of another rule that all anagrams of a certain string can meet. For example, something about the number of occurrences of each character.
already look around but cant find what i want for PHP.
just say i have a number : 1234 ( can be splitted first into array )
and i want to get how many number combination possible for 2 digits, 3 digits , and 4 digits
for example :
possible 4 digits will be :
1234,1243,1324,1342, and so on. ( i dont know how many more )
possible 2 digits will be :
12,13,14,21,23,24,31,32,34,41,42,43
the closest one i get is :
$p = permutate(array('1','2','3','4'));
$result = array();
foreach($p as $perm) {
$result[]=join("",$perm);
}
$result = array_unique($result);
print join("|", $result);
function permutate($elements, $perm = array(), &$permArray = array()){
if(empty($elements)){
array_push($permArray,$perm); return;
}
for($i=0;$i<=count($elements)-1;$i++){
array_push($perm,$elements[$i]);
$tmp = $elements; array_splice($tmp,$i,1);
permutate($tmp,$perm,$permArray);
array_pop($perm);
}
return $permArray;
}
but how can i edit this so i can display for 3 and 2 digits ?
Thanks
i got what i want
it's from #mudasobwa link. and i edit to what i want.
<?php
$in = array(1,2,3,4,5,6);
$te = power_perms($in);
// print_r($te);
$thou=0;
$hun =0;
$pu = 0;
for($i=0;$i<count($te);$i++)
{
$jm = count($te[$i]);
for($j=0;$j<$jm;$j++)
{
$hsl[$i] = $hsl[$i] . $te[$i][$j];
}
if($hsl[$i] >=100 && $hsl[$i] < 1000 )
{
$ratus[$hun] = intval($hsl[$i]);
$hun = $hun + 1;
}
if($hsl[$i] <100 && $hsl[$i] >=10)
{
$pul[$pu] = intval($hsl[$i]);
$pu = $pu + 1;
}
if($hsl[$i] >=1000 && $hsl[$i] < 10000)
{
$th[$thou] = intval($hsl[$i]);
$thou = $thou + 1;
}
}
$th=array_unique($th);
$pul = array_unique($pul);
$ratus = array_unique($ratus);
sort($ratus);
sort($pul);
sort($th);
print_r($th);
function power_perms($arr) {
$power_set = power_set($arr);
$result = array();
foreach($power_set as $set) {
$perms = perms($set);
$result = array_merge($result,$perms);
}
return $result;
}
function power_set($in,$minLength = 1) {
$count = count($in);
$members = pow(2,$count);
$return = array();
for ($i = 0; $i < $members; $i++) {
$b = sprintf("%0".$count."b",$i);
$out = array();
for ($j = 0; $j < $count; $j++) {
if ($b{$j} == '1') $out[] = $in[$j];
}
if (count($out) >= $minLength) {
$return[] = $out;
}
}
// usort($return,"cmp"); //can sort here by length
return $return;
}
function factorial($int){
if($int < 2) {
return 1;
}
for($f = 2; $int-1 > 1; $f *= $int--);
return $f;
}
function perm($arr, $nth = null) {
if ($nth === null) {
return perms($arr);
}
$result = array();
$length = count($arr);
while ($length--) {
$f = factorial($length);
$p = floor($nth / $f);
$result[] = $arr[$p];
array_delete_by_key($arr, $p);
$nth -= $p * $f;
}
$result = array_merge($result,$arr);
return $result;
}
function perms($arr) {
$p = array();
for ($i=0; $i < factorial(count($arr)); $i++) {
$p[] = perm($arr, $i);
}
return $p;
}
function array_delete_by_key(&$array, $delete_key, $use_old_keys = FALSE) {
unset($array[$delete_key]);
if(!$use_old_keys) {
$array = array_values($array);
}
return TRUE;
}
?>
here's my code so please tell me what i used in place of indexOf usind php because there is no indexOf function in php .
function generate($arrLength)
{
$arr = array();
$n = 0;
$start = 10;
$end = 20;
for($i=0; $i < $arrLength; $i++)
{
do{
$n = $start + round(rand()*($end - $start));
}while($arr.indexOf($n) !== -1);
$arr[$i] = $n;
}
return $arr;
}
$generatedArr = generate(4);
You can use array_search() in this case:
function generate($arrLength) {
$arr = array();
$n = 0;
$start = 10;
$end = 20;
for($i=0; $i < $arrLength; $i++) {
do {
$n = $start + round(rand()*($end - $start));
} while(array_search($n, $arr) !== false);
$arr[$i] = $n;
}
return $arr;
}
$generatedArr = generate(4);
echo '<pre>';
print_r($generatedArr);