Last active
December 11, 2015 05:29
-
-
Save jehoshua02/4552735 to your computer and use it in GitHub Desktop.
Counting in ruby and 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 | |
| function countBy($x = 1, $to = 100) | |
| { | |
| return range(0, $to, $x); | |
| } | |
| function countOccuranceOf($value, $array) | |
| { | |
| $counts = array_count_values($array); | |
| return (array_key_exists($value, $counts)) ? $counts[$value] : 0; | |
| } |
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
| def count_by(x = 1, to = 100) | |
| (0..to).step(x) | |
| end | |
| def count_occurance_of(value, array) | |
| array.count(value) | |
| end |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Okay, so I guess PHP has some functions to do this. I was originally thinking I'd need loops. But the PHP manual shows some functions I must have missed the first time I read through it (from cover to cover). Still, I find the ruby syntax to be superior, and -- still -- more compact.