Posts

Showing posts from June, 2019

Add and find the rank in array using php

Hi  Recently i went to interview i was asked to add a mark and find the rank of it using php. 1) First i created a function called  rankChecker and passing  two parameter like collection of marks in array and mark that will be added and finding the rank of it. 2) Then i created dumpy variable $s ,$ab as array. 3)After that i added the mark in the array mark list and sorted into descending order and looped in for loop. 4) passing the value of first array into dumpy array $ab as key and passing the $i value  incremented by one as $i starts with zero. 5) And returning the current $ab array and passing $mark as key value in it. function rankChecker($arr,$mark){ $arr[] = $mark; rsort($arr); $ab = array(); //echo count($arr);    for($i=0;$i<count($arr);$i++){          $ab[$arr[$i]] = $i+1;       } return $ab[$mark]; } $arr=[39,37,34,56,67,23,37]; $mark=10; print_r(rankChecker($arr,$ mark));

print triangle of star using php

1) first loop considering $i as row and $j as column ,where total number of row is 4 and column is 7. 2) second is print * as passing the condition as following. CODE: for($i=1;$i<=4;$i++){     for($j=1;$j<=7;$j++){             if($i==1 && $j==4)             {                 echo "*";             } else if(($j==3 || $j==5)&& $i==2 )             {                 echo "*";             }else if(($j==2||$j==6)&&$i==3)             {                 echo "*";             } else if(($j==1||$j==7)&&$i==4){                 echo "*";             }             else{                 echo " ";             }     }     echo "\n";     }