Skip to content

Instantly share code, notes, and snippets.

@NazemMahmud
Last active December 21, 2022 17:18
Show Gist options
  • Select an option

  • Save NazemMahmud/aeea9cdc4bda71ead070c6de7a00d4ea to your computer and use it in GitHub Desktop.

Select an option

Save NazemMahmud/aeea9cdc4bda71ead070c6de7a00d4ea to your computer and use it in GitHub Desktop.
Airwrk interview tasks
<?php
// Problem 1
function findPairs($nums) {
$count = 0;
$len = count($nums);
if ($len <= 1) { return 0; }
for ($i=0; $i < $len; $i++) {
for ($j=$i+1; $j < $len; $j++) {
if ($nums[$i] == $nums[$j] ) {
$count++;
}
}
}
return $count;
}
// Problem 2
function findSingleNumber($nums) {
sort($nums);
$len = count($nums);
if ($len == 1) { return $nums[0]; }
for ($i=1; $i < $len-2; $i++) {
if ($nums[$i] == $nums[$i-1] || $nums[$i] == $nums[$i+1]) {
continue;
}
else {
return $nums[$i];
}
}
return $nums[$len -1] != $nums[$len -2] ? $nums[$len -1] : $nums[0];
}
// Problem 3
function findWords($line, $num) {
$words = implode(' ', array_slice(explode(' ', $line), 0, $num));
return $words;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment