diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-12-10 08:09:13 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-12-10 08:09:13 +0000 |
| commit | b6e84aef41ce5aa5bbe8037fa8dc92f9ede5c211 (patch) | |
| tree | 0652113985f2cf856b656dfcb088ffaf44c01bd3 | |
| parent | 544483fa1e80e35c153787860350c981d96e91d0 (diff) | |
| parent | 8823eed6e8af52286a712ed97c02088ce3d53a65 (diff) | |
| download | perlweeklychallenge-club-b6e84aef41ce5aa5bbe8037fa8dc92f9ede5c211.tar.gz perlweeklychallenge-club-b6e84aef41ce5aa5bbe8037fa8dc92f9ede5c211.tar.bz2 perlweeklychallenge-club-b6e84aef41ce5aa5bbe8037fa8dc92f9ede5c211.zip | |
Merge pull request #7226 from steve-g-lynn/branch-for-challenge-194
pwc 194
| -rw-r--r-- | challenge-194/steve-g-lynn/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-194/steve-g-lynn/julia/ch-1.jl | 81 | ||||
| -rwxr-xr-x | challenge-194/steve-g-lynn/julia/ch-2.jl | 32 | ||||
| -rwxr-xr-x | challenge-194/steve-g-lynn/perl/ch-1.pl | 64 | ||||
| -rwxr-xr-x | challenge-194/steve-g-lynn/perl/ch-2.pl | 35 | ||||
| -rwxr-xr-x | challenge-194/steve-g-lynn/raku/ch-1.p6 | 66 | ||||
| -rwxr-xr-x | challenge-194/steve-g-lynn/raku/ch-2.p6 | 30 |
7 files changed, 309 insertions, 0 deletions
diff --git a/challenge-194/steve-g-lynn/blog.txt b/challenge-194/steve-g-lynn/blog.txt new file mode 100644 index 0000000000..c1aad4013f --- /dev/null +++ b/challenge-194/steve-g-lynn/blog.txt @@ -0,0 +1 @@ +https://thiujiac.blogspot.com/2022/12/pwc-194.html diff --git a/challenge-194/steve-g-lynn/julia/ch-1.jl b/challenge-194/steve-g-lynn/julia/ch-1.jl new file mode 100755 index 0000000000..2bda71b758 --- /dev/null +++ b/challenge-194/steve-g-lynn/julia/ch-1.jl @@ -0,0 +1,81 @@ +#!/usr/bin/env julia + +function digital_clock( mytime::String ) ::Int64 + + r_time = r"^[0-2\?][0-9\?]\:[0-5\?][0-9\?]$" + r_qn = r"\?" + + if (match(r_time, mytime)==nothing) + return -1 + end + + mytest=SubString(mytime,1,2) + if !occursin(r_qn, mytest) + if parse(Int64, mytest ) >= 24 + return -1 + end + end + + mytest=SubString(mytime,4,5) + if !occursin(r_qn, mytest) + if parse(Int64, mytest ) > 59 + return -1 + end + end + + d_i = Dict() + + for i in mytime + d_i[i]=0 + end + + for i in mytime + d_i[i] += 1 + end + + if haskey(d_i,'?') + if d_i['?'] != 1 + return -1 + end + end + + qn_indx = findfirst('?',mytime) + + if qn_indx == 1 + if parse(Int64,mytime[2]) > 3 + return 1 + else + return 2 + end + end + + + if qn_indx == 2 + mytest=parse(Int64,mytime[1]) + if mytest <= 1 + return 9 + elseif mytest == 2 + return 3 + else + return -999 #-- bug + end + end + + if qn_indx == 4 + return 5 + end + + if qn_indx == 5 + return 9 + end + + return -999 #-- bug + +end + +println( digital_clock("?5:00") ) #1 +println( digital_clock("?3:00") ) #2 +println( digital_clock("1?:00") ) #9 +println( digital_clock("2?:00") ) #3 +println( digital_clock("12:?5") ) #5 +println( digital_clock("12:5?") ) #9 diff --git a/challenge-194/steve-g-lynn/julia/ch-2.jl b/challenge-194/steve-g-lynn/julia/ch-2.jl new file mode 100755 index 0000000000..41f484c007 --- /dev/null +++ b/challenge-194/steve-g-lynn/julia/ch-2.jl @@ -0,0 +1,32 @@ +#!/usr/bin/env julia + +function frequency_equalizer(s::String ) ::Bool + + d_s = Dict() + + for i in s + d_s[i]=0 + end + + for i in s + d_s[i] += 1 + end + + + v_s=Vector( sort!(unique(values(d_s))) ) + + if ( (length(v_s) <= 2) && + ( (v_s[1] == 1) || + ( length(v_s) > 1 && (abs(v_s[2] - v_s[1]) == 1)) ) ) + return true + end + + return false +end + +println( frequency_equalizer("abbc") ) #true +println( frequency_equalizer("xyzyyxz") ) #true +println( frequency_equalizer("xzxz") ) #false +println( frequency_equalizer("abcde") ) #true +println( frequency_equalizer("abbbccc") ) #true + diff --git a/challenge-194/steve-g-lynn/perl/ch-1.pl b/challenge-194/steve-g-lynn/perl/ch-1.pl new file mode 100755 index 0000000000..a7ce0d8a3c --- /dev/null +++ b/challenge-194/steve-g-lynn/perl/ch-1.pl @@ -0,0 +1,64 @@ +#!/usr/bin/env perl + +print &digital_clock('?5:00'),"\n"; #1 +print &digital_clock('?3:00'),"\n"; #2 +print &digital_clock('1?:00'),"\n"; #9 +print &digital_clock('2?:00'),"\n"; #3 +print &digital_clock('12:?5'),"\n"; #5 +print &digital_clock('12:5?'),"\n"; #9 + +sub digital_clock { + my ($time)=@_; + + $time =~ /^[0-2?][0-9?]:[0-5?][0-9?]$/ || (return "Invalid input"); + (substr($time,0,2) >= 24) && (return "Invalid input"); + (substr($time,3,2) > 59) && (return "Invalid input"); + + my @time=split(//,$time); + my (%i, %time_indx, $ctr); + + $ctr=0; + + for my $i (@time){ + $i{$i}++; + $time_indx{$i}=$ctr++; + } + + (return "Invalid input") unless ($i{'?'}==1); + + if ($time_indx{'?'}==0) { + if ($time[1] > 3) { + return 1; + } + else { + return 2; + } + } + + if ($time_indx{'?'}==1) { + if ($time[0] <= 1) { + return 9; + } + elsif ($time[0] == 2) { + return 3; + } + else { #-- shouldn't get here + return "Something wrong"; + } + } + + if ($time_indx{'?'}==2) { #-- shouldn't get here + return "Something wrong"; + } + + if ($time_indx{'?'}==3) { + return 5; + } + + if ($time_indx{'?'}==4) { + return 9; + } + +} + + diff --git a/challenge-194/steve-g-lynn/perl/ch-2.pl b/challenge-194/steve-g-lynn/perl/ch-2.pl new file mode 100755 index 0000000000..65f6404b6f --- /dev/null +++ b/challenge-194/steve-g-lynn/perl/ch-2.pl @@ -0,0 +1,35 @@ +#!/usr/bin/env perl + +use strict; +use warnings; + +print frequency_equalizer('abbc'),"\n"; #1 +print frequency_equalizer('xyzyyxz'),"\n"; #1 +print frequency_equalizer('xzxz'),"\n"; #0 +print frequency_equalizer('abcde'),"\n"; #1 +print frequency_equalizer('abbbccc'),"\n"; #1 + +sub frequency_equalizer { + my ($s) = @_; + + my @s = split (//, $s); + + my (%s, %uniq); + + for my $i (@s) { + $s{$i}++; + } + + for my $i (values %s) { + $uniq{$i} = $i; + } + + @s = sort keys %uniq; + + ( (scalar(@s) <= 2) && ( ($s[0]==1) || + ( exists($s[1]) && (abs($s[1] - $s[0])==1) ) ) ) && + (return 1); + + return 0; +} + diff --git a/challenge-194/steve-g-lynn/raku/ch-1.p6 b/challenge-194/steve-g-lynn/raku/ch-1.p6 new file mode 100755 index 0000000000..6e0d9f0e84 --- /dev/null +++ b/challenge-194/steve-g-lynn/raku/ch-1.p6 @@ -0,0 +1,66 @@ +#!/usr/bin/env perl6 + +say &digital-clock('?5:00'); #1 +say &digital-clock('?3:00'); #2 +say &digital-clock('1?:00'); #9 +say &digital-clock('2?:00'); #3 +say &digital-clock('12:?5'); #5 +say &digital-clock('12:5?'); #9 + +sub digital-clock (Str $time) { + + $time ~~ /^ <[0 .. 2 ?]> <[0 .. 9 ?]> \: <[0 .. 5 ?]> <[0 .. 9 ?]>$/ || (return "Invalid input"); + + ($time ~~ /\?/) || (return "Invalid input"); + + ( (substr($time,0,2) ~~ /^ <[0 .. 2]> <[0 .. 9]> $/) && (substr($time,0,2) > 23) ) && (return "Invalid input"); + + ( (substr($time,3,2) ~~ /^ <[0 .. 5]> <[0 .. 9]> $/) && (substr($time,3,2) > 59) ) && (return "Invalid input"); + + my @time=$time.comb; + my (%i, %time_indx, $ctr); + + $ctr=0; + + for (@time) -> $i { + %i{$i}++; + %time_indx{$i}=$ctr++; + } + + (return "Invalid input") unless (%i{'?'}==1); + + if (%time_indx{'?'}==0) { + if (@time[1] > 3) { + return 1; + } + else { + return 2; + } + } + + if (%time_indx{'?'}==1) { + if (@time[0] <= 1) { + return 9; + } + elsif (@time[0] == 2) { + return 3; + } + else { #-- shouldn't get here + return "Something wrong"; + } + } + + if (%time_indx{'?'}==2) { #-- shouldn't get here + return "Something wrong"; + } + + if (%time_indx{'?'}==3) { + return 5; + } + + if (%time_indx{'?'}==4) { + return 9; + } +} + + diff --git a/challenge-194/steve-g-lynn/raku/ch-2.p6 b/challenge-194/steve-g-lynn/raku/ch-2.p6 new file mode 100755 index 0000000000..16448dca44 --- /dev/null +++ b/challenge-194/steve-g-lynn/raku/ch-2.p6 @@ -0,0 +1,30 @@ +#!/usr/bin/env perl6 + +say frequency-equalizer('abbc'); #1 +say frequency-equalizer('xyzyyxz'); #1 +say frequency-equalizer('xzxz'); #0 +say frequency-equalizer('abcde'); #1 +say frequency-equalizer('abbbccc'); #1 + +sub frequency-equalizer( Str $s ) { + + my @s = $s.comb; + + my (%s, %uniq); + + for (@s) -> $i { + %s{$i}++; + } + + for (%s.values) -> $i { + %uniq{$i} = $i; + } + + @s = %uniq.keys.sort; + + ( ( @s.elems <= 2 ) && ( @s.min==1 || (@s.min==(@s.max-1))) ) && + (return 1); + + return 0; +} + |
