Skip to content

Commit 3ee897d

Browse files
committed
added some trivia algos
1 parent 7b64e34 commit 3ee897d

File tree

7 files changed

+167
-4
lines changed

7 files changed

+167
-4
lines changed

Challenges/check_anagrams.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/**
4+
* The is_anagram() function should determine whether two words are anagrams.
5+
*/
6+
7+
function is_anagram($wrd_1, $wrd_2){
8+
9+
if(!ctype_alpha($wrd_1) || !ctype_alpha($wrd_2)){
10+
return null;
11+
}
12+
13+
if(count_chars($wrd_1,1) == count_chars($wrd_2,1)){
14+
return "This two strings are anagram";
15+
}
16+
17+
return "This two strings are not anagram";
18+
19+
}
20+
21+
print_r(is_anagram('anagram','nagaram')."\n");
22+
print_r(is_anagram('cat','rat')."\n");

Challenges/check_palindrome.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/**
4+
* Check if a string is a Palindrome
5+
*/
6+
function check_palindrome(string $str): string{
7+
8+
try{
9+
10+
if(strrev($str) == $str){
11+
12+
return "Is Palindrome";
13+
14+
}else{
15+
16+
return "Not Palindrome";
17+
18+
}
19+
20+
}catch(\Exception $e){
21+
22+
exit($e->getMessage());
23+
echo "\n\n";
24+
25+
}
26+
27+
}
28+
29+
echo check_palindrome("DAD");
30+
echo "\n\n";
31+
echo check_palindrome("MALAYALAM");
32+
echo "\n\n";
33+
echo check_palindrome("MALAYAAM");
34+
echo "\n\n";

Sorting/trivia/group_by_value.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/**
4+
* group array
5+
*/
6+
$arr = array(
7+
"chimpanzee" => 20,
8+
"meeting" => 50,
9+
"dynasty" => 20,
10+
"chocolate" => 20,
11+
"bananas" => 50,
12+
"fantasy" => 30,
13+
"football" => 20
14+
);
15+
16+
17+
function group_by_value(array &$arr): array{
18+
19+
$results = [];
20+
21+
foreach($arr as $key => $val){
22+
23+
$results[$val][] = $key;
24+
25+
}
26+
27+
return $results;
28+
29+
}
30+
31+
print_r(group_by_value($arr));

Sorting/trivia/oddEven.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/**
4+
* Separate odd and even number
5+
*/
6+
7+
// input array
8+
$input = array(4, 3, 6, 5, 8, 7, 2);
9+
10+
$even = array_filter($input,function($val){
11+
12+
return ($val % 2 == 0)? 1 : -1;
13+
14+
});
15+
16+
$odd = array_filter($input,function($val){
17+
18+
return ($val % 2 != 0)? 1 : -1;
19+
20+
});
21+
22+
print_r($even);

Sorting/trivia/simple_uasort.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/**
4+
* Simple uasort example
5+
*/
6+
7+
function simpple_sort(array &$usort): array{
8+
9+
try{
10+
11+
if(empty($usort) || count($usort) == 0){
12+
13+
throw new \Exception("Invalid array, please check array");
14+
15+
}
16+
17+
uasort($usort,function($val_1, $val_2){
18+
19+
if($val_1 > $val_2){
20+
21+
return 1;
22+
23+
}else if($val_1 < $val_2){
24+
25+
return -1;
26+
27+
}
28+
29+
return 0;
30+
31+
});
32+
33+
return $usort;
34+
35+
}catch(\Exception $e){
36+
37+
exit($e->getMessage());
38+
echo "\n\n";
39+
40+
}
41+
42+
}
43+
44+
$arr = array("a" => 4,"b" => 2,"c" => 8,"d" => 6);
45+
46+
$sorted = simpple_sort($arr);
47+
48+
foreach($sorted as $ky => $vl){
49+
50+
echo "key = " . $ky . " : " . " value = " . $vl ."\n";
51+
52+
}
53+
54+
echo "\n\n";

Sorting/trivia/sort_using_index.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function index_sort(array &$sort, int $idx): array{
1818

1919
if(!ctype_alpha($val_1) || !ctype_alpha($val_2)){
2020

21-
return null;
21+
return 0;
2222

2323
}
2424

@@ -32,7 +32,7 @@ function index_sort(array &$sort, int $idx): array{
3232

3333
}
3434

35-
return null;
35+
return 0;
3636

3737
});
3838

Sorting/trivia/sort_using_last_char.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function sort_last_char(array &$usort): array{
1818

1919
if(!ctype_alpha($val_1) || !ctype_alpha($val_2)){
2020

21-
return null;
21+
return 0;
2222

2323
}
2424

@@ -32,7 +32,7 @@ function sort_last_char(array &$usort): array{
3232

3333
}
3434

35-
return null;
35+
return 0;
3636

3737
});
3838

0 commit comments

Comments
 (0)