diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-12-15 08:57:16 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-12-15 08:57:16 +0000 |
| commit | d74bd98354ed7ef23034cf8e740a401ade91c99f (patch) | |
| tree | fc250655ee26643460876344e7b58b3a5fa2d4da | |
| parent | a2d187baf48c66c8c7344c0410ef47db4c31b144 (diff) | |
| parent | 1c1dcf178ec6acb70f1e887341a99c3b40970ff1 (diff) | |
| download | perlweeklychallenge-club-d74bd98354ed7ef23034cf8e740a401ade91c99f.tar.gz perlweeklychallenge-club-d74bd98354ed7ef23034cf8e740a401ade91c99f.tar.bz2 perlweeklychallenge-club-d74bd98354ed7ef23034cf8e740a401ade91c99f.zip | |
Merge pull request #7258 from deadmarshal/TWC194
TWC194
24 files changed, 585 insertions, 0 deletions
diff --git a/challenge-194/deadmarshal/c/ch-1.c b/challenge-194/deadmarshal/c/ch-1.c new file mode 100644 index 0000000000..b8508e237f --- /dev/null +++ b/challenge-194/deadmarshal/c/ch-1.c @@ -0,0 +1,21 @@ +#include<stdio.h> +#include<stdlib.h> + +short digital_clock(const char *str) +{ + return str[0] == '?' ? (str[1] - '0' < 4 ? 2 : 1) + : str[1] == '?' ? (str[0] - '0' < 2 ? 9 : 3) + : str[3] == '?' ? 5 : 9; +} + +int main(void) +{ + printf("%d\n", digital_clock("?5:00")); + printf("%d\n", digital_clock("?3:00")); + printf("%d\n", digital_clock("1?:00")); + printf("%d\n", digital_clock("2?:00")); + printf("%d\n", digital_clock("12:?5")); + printf("%d\n", digital_clock("12:5?")); + return 0; +} + diff --git a/challenge-194/deadmarshal/c/ch-2.c b/challenge-194/deadmarshal/c/ch-2.c new file mode 100644 index 0000000000..508c83e208 --- /dev/null +++ b/challenge-194/deadmarshal/c/ch-2.c @@ -0,0 +1,34 @@ +#include <stdio.h> +#include <stdlib.h> + +int compare(const void *a, const void *b) +{ + int i1 = *(int*)a; + int i2 = *(int*)b; + return i2 - i1; +} + +int frequency_equalizer(const char *str) +{ + int freq[26] = {0}, *arr = NULL, res = 0, j = 0; + size_t count = 0; + for(int i = 0; str[i] != '\0'; ++i) freq[str[i] - 'a']++; + for(int i = 0; i < 26; ++i) if(freq[i] != 0) count++; + arr = malloc(count * sizeof(int)); + for(int i = 0; i < 26; ++i) if(freq[i] != 0) arr[j++] = freq[i]; + qsort(arr, count, sizeof(int),compare); + res = (arr[0] == arr[1]+1) && (arr[count-1] == arr[1]) ? 1 : 0; + if(arr){ + free(arr); + arr = NULL; + } + return res; +} + +int main(void) +{ + printf("%d\n", frequency_equalizer("abbc")); + printf("%d\n", frequency_equalizer("xyzyyxz")); + printf("%d\n", frequency_equalizer("xzxz")); + return 0; +} diff --git a/challenge-194/deadmarshal/cpp/ch-1.cpp b/challenge-194/deadmarshal/cpp/ch-1.cpp new file mode 100644 index 0000000000..c8e953dcbc --- /dev/null +++ b/challenge-194/deadmarshal/cpp/ch-1.cpp @@ -0,0 +1,28 @@ +#include<iostream> +#include<string> +#include<regex> + +unsigned short digital_clock(const std::string &str) +{ + std::regex re{"(.)(.):(.)(.)"}; + std::smatch m; + if (std::regex_match(str, m, re)) + { + return m[1] == '?' ? (std::stoi(m.str(2)) < 4 ? 2 : 1) + : m[2] == '?' ? (std::stoi(m.str(1)) < 2 ? 9 : 3) + : m[3] == '?' ? 5 : 9; + } + return 0; +} + +int main() +{ + std::cout << digital_clock("?5:00") << '\n'; + std::cout << digital_clock("?3:00") << '\n'; + std::cout << digital_clock("1?:00") << '\n'; + std::cout << digital_clock("2?:00") << '\n'; + std::cout << digital_clock("12:?5") << '\n'; + std::cout << digital_clock("12:5?") << '\n'; + return 0; +} + diff --git a/challenge-194/deadmarshal/cpp/ch-2.cpp b/challenge-194/deadmarshal/cpp/ch-2.cpp new file mode 100644 index 0000000000..8eaf52a1d8 --- /dev/null +++ b/challenge-194/deadmarshal/cpp/ch-2.cpp @@ -0,0 +1,24 @@ +#include<iostream> +#include<vector> +#include<string> +#include<unordered_map> +#include<algorithm> + +bool frequency_equalizer(const std::string &str) +{ + std::unordered_map<char,unsigned> m{}; + std::vector<unsigned> vals{}; + for(const auto &c : str) m[c]++; + for(const auto& kv : m) vals.push_back(kv.second); + std::sort(vals.begin(),vals.end(),std::greater<>()); + if((vals[0] == vals[1]+1) && (vals.back() == vals[1])) return true; + return false; +} + +int main() +{ + std::cout << std::boolalpha << frequency_equalizer("abbc")<< '\n'; + std::cout << std::boolalpha << frequency_equalizer("xyzyyxz")<<'\n'; + std::cout << std::boolalpha << frequency_equalizer("xzxz")<< '\n'; + return 0; +} diff --git a/challenge-194/deadmarshal/d/ch1.d b/challenge-194/deadmarshal/d/ch1.d new file mode 100644 index 0000000000..62ab5984e1 --- /dev/null +++ b/challenge-194/deadmarshal/d/ch1.d @@ -0,0 +1,19 @@ +import std.stdio:writeln; + +short digital_clock(string str) +{ + return str[0] == '?' ? (str[1] - '0' < 4 ? 2 : 1) + : str[1] == '?' ? (str[0] - '0' < 2 ? 9 : 3) + : str[3] == '?' ? 5 : 9; +} + +void main() +{ + writeln(digital_clock("?5:00")); + writeln(digital_clock("?3:00")); + writeln(digital_clock("1?:00")); + writeln(digital_clock("2?:00")); + writeln(digital_clock("12:?5")); + writeln(digital_clock("12:5?")); +} + diff --git a/challenge-194/deadmarshal/d/ch2.d b/challenge-194/deadmarshal/d/ch2.d new file mode 100644 index 0000000000..6e0cc3a6ff --- /dev/null +++ b/challenge-194/deadmarshal/d/ch2.d @@ -0,0 +1,21 @@ +import std.stdio:writeln; +import std.algorithm:sort; + +bool frequency_equalizer(string str) +{ + uint[char] hash; + int[] vals; + foreach(c;str) hash[c]++; + foreach(k,v;hash) vals ~= v; + vals.sort!("a > b"); + if((vals[0] == vals[1]+1) && (vals[$-1] == vals[1])) return true; + return false; +} + +void main() +{ + writeln(frequency_equalizer("abbc")); + writeln(frequency_equalizer("xyzyyxz")); + writeln(frequency_equalizer("xzxz")); +} + diff --git a/challenge-194/deadmarshal/lua/ch-1.lua b/challenge-194/deadmarshal/lua/ch-1.lua new file mode 100644 index 0000000000..67320493e3 --- /dev/null +++ b/challenge-194/deadmarshal/lua/ch-1.lua @@ -0,0 +1,19 @@ +local function digital_clock(str) + local one,two,three,four = str:match('(.)(.):(.)(.)') + if one == '?' then + if tonumber(two) < 4 then return 2 else return 1 end + elseif two == '?' then + if tonumber(one) < 2 then return 9 else return 3 end + elseif three == '?' then + return 5 + else return 9 + end +end + +print(digital_clock('?5:00')) +print(digital_clock('?3:00')) +print(digital_clock('1?:00')) +print(digital_clock('2?:00')) +print(digital_clock('12:?5')) +print(digital_clock('12:5?')) + diff --git a/challenge-194/deadmarshal/lua/ch-2.lua b/challenge-194/deadmarshal/lua/ch-2.lua new file mode 100644 index 0000000000..2f4d85d1f4 --- /dev/null +++ b/challenge-194/deadmarshal/lua/ch-2.lua @@ -0,0 +1,19 @@ +local function frequency_equalizer(str) + local t,vals = {},{} + setmetatable(t,{__index = function(t,k) return 0 end}) + for i=1, #str do + local c = str:sub(i,i) + t[c] = t[c] + 1 + end + for k,v in pairs(t) do vals[#vals+1] = v end + table.sort(vals,function(a,b) return a > b end) + if vals[1] == vals[2]+1 and vals[#vals] == vals[2] then + return true + end + return false +end + +print(frequency_equalizer('abbc')) +print(frequency_equalizer('xyzyyxz')) +print(frequency_equalizer('xzxz')) + diff --git a/challenge-194/deadmarshal/modula-3/ch1/src/Ch1.m3 b/challenge-194/deadmarshal/modula-3/ch1/src/Ch1.m3 new file mode 100644 index 0000000000..d4b0cf9241 --- /dev/null +++ b/challenge-194/deadmarshal/modula-3/ch1/src/Ch1.m3 @@ -0,0 +1,28 @@ +MODULE Ch1 EXPORTS Main; + +IMPORT IO; +FROM Scan IMPORT Int; +FROM Text IMPORT GetChar,FromChar; + +PROCEDURE DigitalClock(T:TEXT):CARDINAL = + BEGIN + IF GetChar(T,0) = '?' THEN + IF Int(FromChar(GetChar(T,1))) < 4 THEN RETURN 2 ELSE RETURN 1 END; + ELSIF GetChar(T,1) = '?' THEN + IF Int(FromChar(GetChar(T,0))) < 2 THEN RETURN 9 ELSE RETURN 3 END; + ELSIF GetChar(T,3) = '?' THEN + RETURN 5 + ELSE + RETURN 9 + END; + END DigitalClock; + +BEGIN + IO.PutInt(DigitalClock("?5:00")); IO.Put("\n"); + IO.PutInt(DigitalClock("?3:00")); IO.Put("\n"); + IO.PutInt(DigitalClock("1?:00")); IO.Put("\n"); + IO.PutInt(DigitalClock("2?:00")); IO.Put("\n"); + IO.PutInt(DigitalClock("12:?5")); IO.Put("\n"); + IO.PutInt(DigitalClock("12:5?")); IO.Put("\n"); +END Ch1. + diff --git a/challenge-194/deadmarshal/modula-3/ch1/src/m3makefile b/challenge-194/deadmarshal/modula-3/ch1/src/m3makefile new file mode 100644 index 0000000000..8a6bc1e4f8 --- /dev/null +++ b/challenge-194/deadmarshal/modula-3/ch1/src/m3makefile @@ -0,0 +1,3 @@ +import("libm3") +implementation("Ch1") +program("ch1") diff --git a/challenge-194/deadmarshal/modula-3/ch2/src/Ch2.m3 b/challenge-194/deadmarshal/modula-3/ch2/src/Ch2.m3 new file mode 100644 index 0000000000..8ee96d52a0 --- /dev/null +++ b/challenge-194/deadmarshal/modula-3/ch2/src/Ch2.m3 @@ -0,0 +1,61 @@ +MODULE Ch2 EXPORTS Main; + +IMPORT SIO,Text; + +PROCEDURE QuickSort(VAR A:ARRAY OF INTEGER;Left,Right:INTEGER) = + VAR + I,J:INTEGER; + Pivot,Temp:INTEGER; + BEGIN + I := Left; + J := Right; + Pivot := A[(Left + Right) DIV 2]; + REPEAT + WHILE Pivot < A[I] DO INC(I) END; + WHILE Pivot > A[J] DO DEC(J) END; + IF I <= J THEN + Temp := A[I]; + A[I] := A[J]; + A[J] := Temp; + INC(I); + DEC(J); + END; + UNTIL I > J; + IF Left < J THEN QuickSort(A, Left, J) END; + IF I < Right THEN QuickSort(A, I, Right) END; + END QuickSort; + +PROCEDURE FrequencyEqualizer(Str:TEXT):BOOLEAN = + VAR + Freq:ARRAY[0..25] OF INTEGER; + Arr:REF ARRAY OF INTEGER; + J,Count:INTEGER; + BEGIN + J := 0; + Count := 0; + FOR I := FIRST(Freq) TO LAST(Freq) DO Freq[I] := 0 END; + FOR I := 0 TO Text.Length(Str)-1 DO + INC(Freq[ORD(Text.GetChar(Str,I)) - ORD('a')]) + END; + FOR I := FIRST(Freq) TO LAST(Freq) DO IF Freq[I] # 0 THEN INC(Count) END END; + Arr := NEW(REF ARRAY OF INTEGER,Count); + <*ASSERT Arr # NIL *> + FOR I := FIRST(Freq) TO LAST(Freq) DO + IF Freq[I] # 0 THEN + Arr[J] := Freq[I]; + INC(J); + END + END; + QuickSort(Arr^,FIRST(Arr^),LAST(Arr^)); + IF(Arr[0] = Arr[1]+1) AND (Arr[LAST(Arr^)] = Arr[1]) THEN + RETURN TRUE + END; + RETURN FALSE; + END FrequencyEqualizer; + +BEGIN + SIO.PutBool(FrequencyEqualizer("abbc")); SIO.Nl(); + SIO.PutBool(FrequencyEqualizer("xyzyyxz")); SIO.Nl(); + SIO.PutBool(FrequencyEqualizer("xzxz")); SIO.Nl(); +END Ch2. + diff --git a/challenge-194/deadmarshal/modula-3/ch2/src/m3makefile b/challenge-194/deadmarshal/modula-3/ch2/src/m3makefile new file mode 100644 index 0000000000..798c627ef3 --- /dev/null +++ b/challenge-194/deadmarshal/modula-3/ch2/src/m3makefile @@ -0,0 +1,5 @@ +import("libm3") +import("libsio") +implementation("Ch2") +program("ch2") + diff --git a/challenge-194/deadmarshal/nim/ch1.nim b/challenge-194/deadmarshal/nim/ch1.nim new file mode 100644 index 0000000000..6dbd35683e --- /dev/null +++ b/challenge-194/deadmarshal/nim/ch1.nim @@ -0,0 +1,17 @@ +import std/[sequtils,strutils] + +proc digital_clock(str:string):int = + var s = toSeq(str) + if s[0] == '?': + if parseInt($s[1]) < 4: return 2 else: return 1 + elif s[1] == '?': + if parseInt($s[0]) < 2: return 9 else: return 3 + elif s[3] == '?': return 5 else: return 9 + +echo digital_clock("?5:00") +echo digital_clock("?3:00") +echo digital_clock("1?:00") +echo digital_clock("2?:00") +echo digital_clock("12:?5") +echo digital_clock("12:5?") + diff --git a/challenge-194/deadmarshal/nim/ch2.nim b/challenge-194/deadmarshal/nim/ch2.nim new file mode 100644 index 0000000000..d1da415e84 --- /dev/null +++ b/challenge-194/deadmarshal/nim/ch2.nim @@ -0,0 +1,14 @@ +import std/[tables,sequtils,algorithm] + +proc frequencyEqualizer(str:string):bool = + var + s = toSeq(toCountTable(str).values) + sort(s,Descending) + if s[0] == s[1]+1 and s[^1] == s[1]: + return true + return false + +echo frequencyEqualizer("abbc") +echo frequencyEqualizer("xyzyyxz") +echo frequencyEqualizer("xzxz") + diff --git a/challenge-194/deadmarshal/oberon/Ch1.Mod b/challenge-194/deadmarshal/oberon/Ch1.Mod new file mode 100644 index 0000000000..6f967d144b --- /dev/null +++ b/challenge-194/deadmarshal/oberon/Ch1.Mod @@ -0,0 +1,26 @@ +MODULE Ch1; + + IMPORT Out; + + PROCEDURE DigitalClock(Str:ARRAY OF CHAR):INTEGER; + BEGIN + IF Str[0] = '?' THEN + IF ORD(Str[1]) - ORD('0') < 4 THEN RETURN 2 ELSE RETURN 1 END; + ELSIF Str[1] = '?' THEN + IF ORD(Str[0]) - ORD('0') < 2 THEN RETURN 9 ELSE RETURN 3 END; + ELSIF Str[3] = '?' THEN + RETURN 5 + ELSE + RETURN 9; + END; + END DigitalClock; + +BEGIN + Out.Int(DigitalClock("?5:00"),0); Out.Ln; + Out.Int(DigitalClock("?3:00"),0); Out.Ln; + Out.Int(DigitalClock("1?:00"),0); Out.Ln; + Out.Int(DigitalClock("2?:00"),0); Out.Ln; + Out.Int(DigitalClock("12:?5"),0); Out.Ln; + Out.Int(DigitalClock("12:5?"),0); Out.Ln; +END Ch1. + diff --git a/challenge-194/deadmarshal/oberon/Ch2.Mod b/challenge-194/deadmarshal/oberon/Ch2.Mod new file mode 100644 index 0000000000..ba78275c93 --- /dev/null +++ b/challenge-194/deadmarshal/oberon/Ch2.Mod @@ -0,0 +1,61 @@ +MODULE Ch2; + + IMPORT Out; + + PROCEDURE QuickSort(VAR A:ARRAY OF INTEGER;Left,Right:LONGINT); + VAR + I,J:LONGINT; + Pivot,Temp:INTEGER; + BEGIN + I := Left; + J := Right; + Pivot := A[(Left + Right) DIV 2]; + REPEAT + WHILE Pivot < A[I] DO INC(I) END; + WHILE Pivot > A[J] DO DEC(J) END; + IF I <= J THEN + Temp := A[I]; + A[I] := A[J]; + A[J] := Temp; + INC(I); + DEC(J); + END; + UNTIL I > J; + IF Left < J THEN QuickSort(A, Left, J) END; + IF I < Right THEN QuickSort(A, I, Right) END; + END QuickSort; + + PROCEDURE FrequencyEqualizer(Str:ARRAY OF CHAR):LONGINT; + VAR + Freq:ARRAY 26 OF INTEGER; + Arr:POINTER TO ARRAY OF INTEGER; + Res,I,J,Count:LONGINT; + BEGIN + J := 0; + Count := 0; + FOR I := 0 TO LEN(Freq)-1 DO Freq[I] := 0 END; + FOR I := 0 TO LEN(Str)-1 DO INC(Freq[ORD(Str[I]) - ORD('a')]) END; + FOR I := 0 TO LEN(Freq)-1 DO IF Freq[I] # 0 THEN INC(Count) END END; + NEW(Arr,Count); + ASSERT(Arr # NIL); + FOR I := 0 TO LEN(Freq)-1 DO + IF Freq[I] # 0 THEN + Arr[J] := Freq[I]; + INC(J); + END + END; + QuickSort(Arr^,0,LEN(Arr^)-1); + IF(Arr[0] = Arr[1]+1) & (Arr[LEN(Arr^)-1] = Arr[1]) THEN + Res := 1 + ELSE + Res := 0 + END; + RETURN Res; + END FrequencyEqualizer; + +BEGIN + Out.Int(FrequencyEqualizer('abbc'),0); Out.Ln; + Out.Int(FrequencyEqualizer('xyzyyxz'),0); Out.Ln; + Out.Int(FrequencyEqualizer('xzxz'),0); Out.Ln; +END Ch2. + diff --git a/challenge-194/deadmarshal/pascal/ch1.pas b/challenge-194/deadmarshal/pascal/ch1.pas new file mode 100644 index 0000000000..9d6a4e2b53 --- /dev/null +++ b/challenge-194/deadmarshal/pascal/ch1.pas @@ -0,0 +1,27 @@ +program Ch1; + +{$mode objfpc} + +uses + SysUtils; + +function DigitalClock(constref Str:AnsiString):ShortInt; +begin + if(Str[1] = '?') then + if(StrToInt(Str[2]) < 4) then Exit(2) else Exit(1) + else if(Str[2] = '?') then + if(StrToInt(Str[1]) < 2) then Exit(9) else Exit(3) + else if(Str[4] = '?') then + Exit(5) + else + Exit(9); +end; + +begin + WriteLn(DigitalClock('?5:00')); + WriteLn(DigitalClock('?3:00')); + WriteLn(DigitalClock('1?:00')); + WriteLn(DigitalClock('2?:00')); + WriteLn(DigitalClock('12:?5')); + WriteLn(DigitalClock('12:5?')); +end. diff --git a/challenge-194/deadmarshal/pascal/ch2.pas b/challenge-194/deadmarshal/pascal/ch2.pas new file mode 100644 index 0000000000..a4cd2dc337 --- /dev/null +++ b/challenge-194/deadmarshal/pascal/ch2.pas @@ -0,0 +1,57 @@ +program Ch2; + +{$mode objfpc} + +uses + SysUtils,Types; + +procedure QuickSort(var Arr:array of Integer;Left,Right:Integer); +var + I,J,Pivot,Temp:Integer; +begin + I := Left; + J := Right; + Pivot := Arr[(Left + Right) div 2]; + repeat + while Pivot < Arr[I] do Inc(I); + while Pivot > Arr[J] do Dec(J); + if I <= J then + begin + Temp := Arr[I]; + Arr[I] := Arr[J]; + Arr[J] := Temp; + Inc(I); + Dec(J); + end; + until I > J; + if Left < J then QuickSort(Arr,Left,J); + if I < Right then QuickSort(Arr,I,Right); +end; + +function FrequencyEqualizer(constref Str:AnsiString):Boolean; +var + Freq:array[0..25] of Integer; + Arr:TIntegerDynArray; + I,J,Count:Integer; +begin + J := 0; + Count := 0; + FillDWord(Freq,Length(Freq),0); + for I := Low(Str) to High(Str) do Inc(Freq[Ord(Str[I]) - Ord('a')]); + for I := Low(Freq) to High(Freq) do if Freq[I] <> 0 then Inc(Count); + SetLength(Arr,Count); + Assert(Assigned(Arr)); + for I := Low(Freq) to High(Freq) do + if Freq[I] <> 0 then begin Arr[J] := Freq[I]; Inc(J); end; + QuickSort(Arr,Low(Arr),High(Arr)); + if((Arr[0] = Arr[1]+1) and (Arr[High(Arr)] = Arr[1])) then + Result := True + else Result := False; +end; + +begin + WriteLn(FrequencyEqualizer('abbc')); + WriteLn(FrequencyEqualizer('xyzyyxz')); + WriteLn(FrequencyEqualizer('xzxz')); +end. + diff --git a/challenge-194/deadmarshal/perl/ch-1.pl b/challenge-194/deadmarshal/perl/ch-1.pl new file mode 100644 index 0000000000..5ca34e98bf --- /dev/null +++ b/challenge-194/deadmarshal/perl/ch-1.pl @@ -0,0 +1,20 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +sub digital_clock{ + my ($str) = @_; + if($str =~ /(.)(.):(.)(.)/){ + $1 eq '?' ? ($2 < 4 ? 2 : 1) + : $2 eq '?' ? ($1 < 2 ? 9 : 3) + : $3 eq '?' ? 5 : 9; + } +} + +print digital_clock('?5:00'), "\n"; +print digital_clock('?3:00'), "\n"; +print digital_clock('1?:00'), "\n"; +print digital_clock('2?:00'), "\n"; +print digital_clock('12:?5'), "\n"; +print digital_clock('12:5?'), "\n"; + diff --git a/challenge-194/deadmarshal/perl/ch-2.pl b/challenge-194/deadmarshal/perl/ch-2.pl new file mode 100644 index 0000000000..e0df7e199b --- /dev/null +++ b/challenge-194/deadmarshal/perl/ch-2.pl @@ -0,0 +1,16 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +sub frequency_equalizer{ + my ($str) = @_; + my %hash; + $hash{$_}++ foreach split '', $str; + my @sorted = sort {$b <=> $a} values %hash; + $sorted[0] == $sorted[1]+1 && $sorted[-1] == $sorted[1] ? 1 : 0; +} + +print frequency_equalizer('abbc'), "\n"; +print frequency_equalizer('xyzyyxz'), "\n"; +print frequency_equalizer('xzxz'), "\n"; + diff --git a/challenge-194/deadmarshal/python/ch1.py b/challenge-194/deadmarshal/python/ch1.py new file mode 100644 index 0000000000..5dfff434b0 --- /dev/null +++ b/challenge-194/deadmarshal/python/ch1.py @@ -0,0 +1,24 @@ +def digital_clock(s): + arr = list(s) + if arr[0] == '?': + if int(arr[1]) < 4: + return 2 + else: + return 1 + elif arr[1] == '?': + if int(arr[0]) < 2: + return 9 + else: + return 3 + elif arr[3] == '?': + return 5 + else: + return 9 + +print(digital_clock('?5:00')) +print(digital_clock('?3:00')) +print(digital_clock('1?:00')) +print(digital_clock('2?:00')) +print(digital_clock('12:?5')) +print(digital_clock('12:5?')) + diff --git a/challenge-194/deadmarshal/python/ch2.py b/challenge-194/deadmarshal/python/ch2.py new file mode 100644 index 0000000000..5fe176c5cb --- /dev/null +++ b/challenge-194/deadmarshal/python/ch2.py @@ -0,0 +1,11 @@ +from collections import Counter + +def frequency_equalizer(s): + arr = sorted(list(Counter(s).values()),reverse=True) + if arr[0] == arr[1]+1 and arr[-1] == arr[1]: return True + return False + +print(frequency_equalizer("abbc")) +print(frequency_equalizer("xyzyyxz")) +print(frequency_equalizer("xzxz")) + diff --git a/challenge-194/deadmarshal/raku/ch-1.raku b/challenge-194/deadmarshal/raku/ch-1.raku new file mode 100644 index 0000000000..8e91ad3603 --- /dev/null +++ b/challenge-194/deadmarshal/raku/ch-1.raku @@ -0,0 +1,18 @@ +sub digital-clock(Str $str) +{ + if ($str ~~ /(.)(.):(.)(.)/) + { + $1 eq '?' ?? ($2 < 4 ?? 2 !! 1) + !! $2 eq '?' ?? ($1 < 2 ?? 9 !! 3) + !! $3 eq '?' ?? 5 + !! 9; + } +} + +say digital-clock('?5:00'); +say digital-clock('?3:00'); +say digital-clock('1?:00'); +say digital-clock('2?:00'); +say digital-clock('12:?5'); +say digital-clock('12:5?'); + diff --git a/challenge-194/deadmarshal/raku/ch-2.raku b/challenge-194/deadmarshal/raku/ch-2.raku new file mode 100644 index 0000000000..f6894ae28a --- /dev/null +++ b/challenge-194/deadmarshal/raku/ch-2.raku @@ -0,0 +1,12 @@ +sub frequency-equalizer($str) +{ + my %hash; + %hash{$_}++ for $str.comb; + my @vals = %hash.values.sort: {$^b <=> $^a}; + @vals[0] == @vals[1]+1 && @vals[*-1] == @vals[1] ?? True !! False; +} + +say frequency-equalizer('abbc'); +say frequency-equalizer('xyzyyxz'); +say frequency-equalizer('xzxz'); + |
