diff options
| author | drbaggy <js5@sanger.ac.uk> | 2021-03-01 22:47:01 +0000 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2021-03-01 22:47:01 +0000 |
| commit | b8cb259c849a0ed4279987c37a960db4e09258b2 (patch) | |
| tree | 9a6da180399c09fcc1c017dfb69619121af131d3 | |
| parent | 2aad1303fff0536840278e0e7124fe099bdaff3e (diff) | |
| download | perlweeklychallenge-club-b8cb259c849a0ed4279987c37a960db4e09258b2.tar.gz perlweeklychallenge-club-b8cb259c849a0ed4279987c37a960db4e09258b2.tar.bz2 perlweeklychallenge-club-b8cb259c849a0ed4279987c37a960db4e09258b2.zip | |
php version of 2 - simple rewrite...
| -rw-r--r-- | challenge-102/james-smith/php/ch-2.php | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/challenge-102/james-smith/php/ch-2.php b/challenge-102/james-smith/php/ch-2.php new file mode 100644 index 0000000000..015733e992 --- /dev/null +++ b/challenge-102/james-smith/php/ch-2.php @@ -0,0 +1,22 @@ +<?php + +foreach( range(1,20) as $_ ) { + echo hash_count_string($_),"\n"; +} + +## Like many of these problems - we can work backwards... +## We know the string is going to be just the # +## or the end of the string is going to be nnnn# (where nnnn is the strlen of the string) +## we can repeat this backwards until we reach the start of the string. +## note for the start of the string there are two posibilities +## If you get to "2#" at the start then you have finished +## If you have a single gap just prefix by "#" + +function hash_count_string($n) { + $s = ''; + do { + if( $n == strlen($s) ) return $s; + if( $n-1 == strlen($s) ) return "#$s"; + } while( $s = ($n-strlen($s)) ."#$s" ); +} + |
