diff options
| -rw-r--r-- | challenge-102/james-smith/perl/ch-1.pl | 16 | ||||
| -rw-r--r-- | challenge-102/james-smith/perl/ch-2.pl | 53 |
2 files changed, 69 insertions, 0 deletions
diff --git a/challenge-102/james-smith/perl/ch-1.pl b/challenge-102/james-smith/perl/ch-1.pl new file mode 100644 index 0000000000..930d91cc65 --- /dev/null +++ b/challenge-102/james-smith/perl/ch-1.pl @@ -0,0 +1,16 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; + +is( my_function(), 1 ); + +done_testing(); + +sub my_function { + return 1; +} + diff --git a/challenge-102/james-smith/perl/ch-2.pl b/challenge-102/james-smith/perl/ch-2.pl new file mode 100644 index 0000000000..015e40dc98 --- /dev/null +++ b/challenge-102/james-smith/perl/ch-2.pl @@ -0,0 +1,53 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; + +is( hash_count_string(1), '#' ); +is( hash_count_string(2), '2#' ); +is( hash_count_string(3), '#3#' ); +is( hash_count_string(4), '2#4#' ); +is( hash_count_string(5), '#3#5#' ); +is( hash_count_string(6), '2#4#6#' ); +is( hash_count_string(7), '#3#5#7#' ); +is( hash_count_string(8), '2#4#6#8#' ); +is( hash_count_string(9), '#3#5#7#9#' ); +is( hash_count_string(10), '#3#5#7#10#' ); +is( hash_count_string(11), '2#4#6#8#11#' ); +is( hash_count_string(12), '#3#5#7#9#12#' ); +is( hash_count_string(13), '#3#5#7#10#13#' ); +is( hash_count_string(14), '2#4#6#8#11#14#' ); +is( hash_count_string(15), '#3#5#7#9#12#15#' ); +is( hash_count_string(16), '#3#5#7#10#13#16#' ); +is( hash_count_string(17), '2#4#6#8#11#14#17#' ); +is( hash_count_string(18), '#3#5#7#9#12#15#18#' ); +is( hash_count_string(19), '#3#5#7#10#13#16#19#' ); +is( hash_count_string(20), '2#4#6#8#11#14#17#20#' ); +is( hash_count_string(99), '#3#5#7#9#12#15#18#21#24#27#30#33#36#39#42#45#48#51#54#57#60#63#66#69#72#75#78#81#84#87#90#93#96#99#' ); +is( hash_count_string(100), '#3#5#7#9#12#15#18#21#24#27#30#33#36#39#42#45#48#51#54#57#60#63#66#69#72#75#78#81#84#87#90#93#96#100#' ); +is( hash_count_string(100), '#3#5#7#9#12#15#18#21#24#27#30#33#36#39#42#45#48#51#54#57#60#63#66#69#72#75#78#81#84#87#90#93#96#100#' ); +is( hash_count_string(999), '#3#5#7#9#12#15#18#21#24#27#30#33#36#39#42#45#48#51#54#57#60#63#66#69#72#75#78#81#84#87#90#93#96#99#103#107#111#115#119#123#127#131#135#139#143#147#151#155#159#163#167#171#175#179#183#187#191#195#199#203#207#211#215#219#223#227#231#235#239#243#247#251#255#259#263#267#271#275#279#283#287#291#295#299#303#307#311#315#319#323#327#331#335#339#343#347#351#355#359#363#367#371#375#379#383#387#391#395#399#403#407#411#415#419#423#427#431#435#439#443#447#451#455#459#463#467#471#475#479#483#487#491#495#499#503#507#511#515#519#523#527#531#535#539#543#547#551#555#559#563#567#571#575#579#583#587#591#595#599#603#607#611#615#619#623#627#631#635#639#643#647#651#655#659#663#667#671#675#679#683#687#691#695#699#703#707#711#715#719#723#727#731#735#739#743#747#751#755#759#763#767#771#775#779#783#787#791#795#799#803#807#811#815#819#823#827#831#835#839#843#847#851#855#859#863#867#871#875#879#883#887#891#895#899#903#907#911#915#919#923#927#931#935#939#943#947#951#955#959#963#967#971#975#979#983#987#991#995#999#' ); +is( hash_count_string(1000), '#3#5#7#9#12#15#18#21#24#27#30#33#36#39#42#45#48#51#54#57#60#63#66#69#72#75#78#81#84#87#90#93#96#99#103#107#111#115#119#123#127#131#135#139#143#147#151#155#159#163#167#171#175#179#183#187#191#195#199#203#207#211#215#219#223#227#231#235#239#243#247#251#255#259#263#267#271#275#279#283#287#291#295#299#303#307#311#315#319#323#327#331#335#339#343#347#351#355#359#363#367#371#375#379#383#387#391#395#399#403#407#411#415#419#423#427#431#435#439#443#447#451#455#459#463#467#471#475#479#483#487#491#495#499#503#507#511#515#519#523#527#531#535#539#543#547#551#555#559#563#567#571#575#579#583#587#591#595#599#603#607#611#615#619#623#627#631#635#639#643#647#651#655#659#663#667#671#675#679#683#687#691#695#699#703#707#711#715#719#723#727#731#735#739#743#747#751#755#759#763#767#771#775#779#783#787#791#795#799#803#807#811#815#819#823#827#831#835#839#843#847#851#855#859#863#867#871#875#879#883#887#891#895#899#903#907#911#915#919#923#927#931#935#939#943#947#951#955#959#963#967#971#975#979#983#987#991#995#1000#' ); +is( hash_count_string(1001), '#3#5#7#9#12#15#18#21#24#27#30#33#36#39#42#45#48#51#54#57#60#63#66#69#72#75#78#81#84#87#90#93#96#100#104#108#112#116#120#124#128#132#136#140#144#148#152#156#160#164#168#172#176#180#184#188#192#196#200#204#208#212#216#220#224#228#232#236#240#244#248#252#256#260#264#268#272#276#280#284#288#292#296#300#304#308#312#316#320#324#328#332#336#340#344#348#352#356#360#364#368#372#376#380#384#388#392#396#400#404#408#412#416#420#424#428#432#436#440#444#448#452#456#460#464#468#472#476#480#484#488#492#496#500#504#508#512#516#520#524#528#532#536#540#544#548#552#556#560#564#568#572#576#580#584#588#592#596#600#604#608#612#616#620#624#628#632#636#640#644#648#652#656#660#664#668#672#676#680#684#688#692#696#700#704#708#712#716#720#724#728#732#736#740#744#748#752#756#760#764#768#772#776#780#784#788#792#796#800#804#808#812#816#820#824#828#832#836#840#844#848#852#856#860#864#868#872#876#880#884#888#892#896#900#904#908#912#916#920#924#928#932#936#940#944#948#952#956#960#964#968#972#976#980#984#988#992#996#1001#' ); + +done_testing(); + +## 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 length 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 "#" + +sub hash_count_string { + my ( $s, $n ) = ( '', @_ ); + do { + return $s if $n == length $s; + return "#$s" if $n-1 == length $s; + } while $s = ($n-length $s) ."#$s"; +} + |
