Last active
February 3, 2024 01:02
-
-
Save queky18/78a91f170222f0937e8d80a87668a8e0 to your computer and use it in GitHub Desktop.
Hackerrank - 30 days of code in PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$_fp = fopen("php://stdin", "r"); | |
$inputString = fgets($_fp); // get a line of input from stdin and save it to our variable | |
// Your first line of output goes here | |
print("Hello, World.\n"); | |
// Write the second line of output | |
print($inputString); | |
fclose($_fp); | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$i2 = fgets($handle); | |
$d2 = fgets ($handle); | |
$s2 = fgets ($handle); | |
printf("%d\n", $i + $i2); | |
printf("%.1f\n", $d + $d2); | |
printf($s.$s2."\n"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$_fp = fopen("php://stdin", "r"); | |
/* Enter your code here. Read input from STDIN. Print output to STDOUT */ | |
$mealCost = fgets($_fp); | |
$tipPercent = fgets($_fp) / 100 * $mealCost; | |
$taxPercent = fgets($_fp) / 100 * $mealCost; | |
$totalCost = round($mealCost + $tipPercent + $taxPercent); | |
echo "The total meal cost is ". $totalCost. " dollars."; | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$handle = fopen ("php://stdin","r"); | |
fscanf($handle,"%d",$N); | |
if(($N % 2 !==0) || (($N % 2 == 0) && ($N < 21) && (5 < $N))) { | |
print "Weird"; | |
} elseif ((($N % 2 == 0) && (1<$N) && ($N < 6)) || (($N > 20)&& ($N % 2 ==0))) { | |
print "Not Weird"; | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Person{ | |
public $age; | |
public function __construct($initialAge){ | |
// Add some more code to run some checks on initialAge | |
if($initialAge < 0){ | |
$this -> age = 0; | |
print("Age is not valid, setting age to 0.\n"); | |
} else { | |
$this-> age = $initialAge; | |
} | |
} | |
public function amIOld(){ | |
// Do some computations in here and print out the correct statement to the console | |
if($this->age < 13){ | |
echo "You are young.\n"; | |
}elseif($this->age < 18){ | |
echo "You are a teenager.\n"; | |
} else { | |
echo "You are old.\n"; | |
} | |
} | |
public function yearPasses(){ | |
// Increment the age of the person in here | |
$this->age++; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$handle = fopen ("php://stdin","r"); | |
fscanf($handle,"%d",$N); | |
fgets($handle); | |
for($i = 1; $i < 11; $i++){ | |
$result = $N * $i; | |
echo "$N x $i = " . $result . "\n"; | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$handle = fopen ("php://stdin","r"); | |
fscanf($handle,"%d",$n); | |
$arr_temp = fgets($handle); | |
$arr = explode(" ",$arr_temp); | |
array_walk($arr,'intval'); | |
$array = array_reverse($arr, true); | |
foreach($array as $key=>$value){ | |
print_r($value." "); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment