aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-194/paulo-custodio/Makefile2
-rw-r--r--challenge-194/paulo-custodio/basic/ch-1.bas55
-rw-r--r--challenge-194/paulo-custodio/basic/ch-2.bas55
-rw-r--r--challenge-194/paulo-custodio/c/ch-1.c53
-rw-r--r--challenge-194/paulo-custodio/c/ch-2.c61
-rw-r--r--challenge-194/paulo-custodio/forth/ch-1.fs57
-rw-r--r--challenge-194/paulo-custodio/forth/ch-2.fs72
-rw-r--r--challenge-194/paulo-custodio/perl/ch-1.pl48
-rw-r--r--challenge-194/paulo-custodio/perl/ch-2.pl39
-rw-r--r--challenge-194/paulo-custodio/t/test-1.yaml30
-rw-r--r--challenge-194/paulo-custodio/t/test-2.yaml15
11 files changed, 487 insertions, 0 deletions
diff --git a/challenge-194/paulo-custodio/Makefile b/challenge-194/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-194/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-194/paulo-custodio/basic/ch-1.bas b/challenge-194/paulo-custodio/basic/ch-1.bas
new file mode 100644
index 0000000000..bb717ba56a
--- /dev/null
+++ b/challenge-194/paulo-custodio/basic/ch-1.bas
@@ -0,0 +1,55 @@
+' Challenge 194
+'
+' Task 1: Digital Clock
+' Submitted by: Mohammad S Anwar
+' You are given time in the format hh:mm with one missing digit.
+'
+' Write a script to find the highest digit between 0-9 that makes it valid time.
+'
+' Example 1
+' Input: $time = '?5:00'
+' Output: 1
+'
+' Since 05:00 and 15:00 are valid time and no other digits can fit in the missing place.
+' Example 2
+' Input: $time = '?3:00'
+' Output: 2
+' Example 3
+' Input: $time = '1?:00'
+' Output: 9
+' Example 4
+' Input: $time = '2?:00'
+' Output: 3
+' Example 5
+' Input: $time = '12:?5'
+' Output: 5
+' Example 6
+' Input: $time = '12:5?'
+' Output: 9
+
+sub check_digit(s as string)
+ if len(s)=1 and (s="?" or (s>="0" and s<="9")) then
+ exit sub
+ else
+ print "invalid digit:";s
+ end
+ end if
+end sub
+
+function missing_digit(clock as string) as integer
+ dim h1 as string, h2 as string, m1 as string, m2 as string, col as string
+ h1=mid(clock,1,1): check_digit h1
+ h2=mid(clock,2,1): check_digit h2
+ col=mid(clock,3,1): if col<>":" then print "invalid clock:";clock: end
+ m1=mid(clock,4,1): check_digit m1
+ m2=mid(clock,5,1): check_digit m2
+ if h1="?" and h2<="3" then missing_digit=2: exit function
+ if h1="?" then missing_digit=1: exit function
+ if h1<="1" and h2="?" then missing_digit=9: exit function
+ if h2="?" then missing_digit=3: exit function
+ if m1="?" then missing_digit=5: exit function
+ if m2="?" then missing_digit=9: exit function
+ print "invalid clock:";clock: end
+end function
+
+print missing_digit(command(1))
diff --git a/challenge-194/paulo-custodio/basic/ch-2.bas b/challenge-194/paulo-custodio/basic/ch-2.bas
new file mode 100644
index 0000000000..5a08402bbb
--- /dev/null
+++ b/challenge-194/paulo-custodio/basic/ch-2.bas
@@ -0,0 +1,55 @@
+' Challenge 194
+'
+' Task 2: Frequency Equalizer
+' Submitted by: Mohammad S Anwar
+' You are given a string made of alphabetic characters only, a-z.
+'
+' Write a script to determine whether removing only one character can make the
+' frequency of the remaining characters the same.
+'
+' Example 1:
+' Input: $s = 'abbc'
+' Output: 1 since removing one alphabet 'b' will give us 'abc' where each
+' alphabet frequency is the same.
+' Example 2:
+' Input: $s = 'xyzyyxz'
+' Output: 1 since removing 'y' will give us 'xzyyxz'.
+' Example 3:
+' Input: $s = 'xzxz'
+' Output: 0 since removing any one alphabet would not give us string with same
+' frequency alphabet.
+
+function freq_equalizer(s as string) as integer
+ dim freq(25) as integer
+ dim i as integer, l as integer, min as integer, max as integer, count as integer
+ if s="" then freq_equalizer=0: exit function
+ for i=1 to len(s)
+ if mid(s,i,1)>="a" and mid(s,i,1)<="z" then
+ l=asc(mid(s,i,1))-asc("a")
+ freq(l)=freq(l)+1
+ end if
+ next
+ min=0
+ for i=0 to ubound(freq)
+ if freq(i)<>0 then
+ if min=0 then
+ min=freq(i)
+ max=freq(i)
+ end if
+ if min>freq(i) then min=freq(i)
+ if max<freq(i) then max=freq(i)
+ end if
+ next
+ if min+1<>max then freq_equalizer=0: exit function
+ count=0
+ for i=0 to ubound(freq)
+ if freq(i)=max then count=count+1
+ next
+ if count=1 then
+ freq_equalizer=1
+ else
+ freq_equalizer=0
+ end if
+end function
+
+print freq_equalizer(command(1))
diff --git a/challenge-194/paulo-custodio/c/ch-1.c b/challenge-194/paulo-custodio/c/ch-1.c
new file mode 100644
index 0000000000..4188e8d522
--- /dev/null
+++ b/challenge-194/paulo-custodio/c/ch-1.c
@@ -0,0 +1,53 @@
+/*
+Challenge 194
+
+Task 1: Digital Clock
+Submitted by: Mohammad S Anwar
+You are given time in the format hh:mm with one missing digit.
+
+Write a script to find the highest digit between 0-9 that makes it valid time.
+
+Example 1
+Input: $time = '?5:00'
+Output: 1
+
+Since 05:00 and 15:00 are valid time and no other digits can fit in the missing place.
+Example 2
+Input: $time = '?3:00'
+Output: 2
+Example 3
+Input: $time = '1?:00'
+Output: 9
+Example 4
+Input: $time = '2?:00'
+Output: 3
+Example 5
+Input: $time = '12:?5'
+Output: 5
+Example 6
+Input: $time = '12:5?'
+Output: 9
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+
+int missing_digit(const char* clock) {
+ if (clock[0]=='?' && clock[1]<='3') return 2;
+ if (clock[0]=='?') return 1;
+ if (clock[0]<='1' && clock[1]=='?') return 9;
+ if (clock[1]=='?') return 3;
+ if (clock[3]=='?') return 5;
+ if (clock[4]=='?') return 9;
+ return -1;
+}
+
+int main(int argc, char* argv[]) {
+ argv++; argc--;
+ if (argc != 1) {
+ fputs("usage: ch-1 hh:mm\n", stderr);
+ return EXIT_FAILURE;
+ }
+
+ printf("%d\n", missing_digit(argv[0]));
+}
diff --git a/challenge-194/paulo-custodio/c/ch-2.c b/challenge-194/paulo-custodio/c/ch-2.c
new file mode 100644
index 0000000000..ee7cb04c5b
--- /dev/null
+++ b/challenge-194/paulo-custodio/c/ch-2.c
@@ -0,0 +1,61 @@
+/*
+Challenge 194
+
+Task 2: Frequency Equalizer
+Submitted by: Mohammad S Anwar
+You are given a string made of alphabetic characters only, a-z.
+
+Write a script to determine whether removing only one character can make the
+frequency of the remaining characters the same.
+
+Example 1:
+Input: $s = 'abbc'
+Output: 1 since removing one alphabet 'b' will give us 'abc' where each
+alphabet frequency is the same.
+Example 2:
+Input: $s = 'xyzyyxz'
+Output: 1 since removing 'y' will give us 'xzyyxz'.
+Example 3:
+Input: $s = 'xzxz'
+Output: 0 since removing any one alphabet would not give us string with same
+frequency alphabet.
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <ctype.h>
+
+#define LETTERS ('z'-'a'+1)
+
+int freq_equalizer(const char* s) {
+ int freq[LETTERS]={0};
+ if (*s=='\0') return 0;
+ for (const char* p=s; *p; p++) {
+ if (isalpha(*p))
+ freq[tolower(*p)-'a']++;
+ }
+ int min=0, max=0;
+ for (int i=0; i<LETTERS; i++) {
+ if (freq[i]) {
+ if (min==0) min=max=freq[i];
+ if (min>freq[i]) min=freq[i];
+ if (max<freq[i]) max=freq[i];
+ }
+ }
+ if (min+1!=max) return 0;
+ int count=0;
+ for (int i=0; i<LETTERS; i++) {
+ if (freq[i]==max) count++;
+ }
+ return count==1 ? 1 : 0;
+}
+
+int main(int argc, char* argv[]) {
+ argv++; argc--;
+ if (argc != 1) {
+ fputs("usage: ch-1 string\n", stderr);
+ return EXIT_FAILURE;
+ }
+
+ printf("%d\n", freq_equalizer(argv[0]));
+}
diff --git a/challenge-194/paulo-custodio/forth/ch-1.fs b/challenge-194/paulo-custodio/forth/ch-1.fs
new file mode 100644
index 0000000000..30efdf919c
--- /dev/null
+++ b/challenge-194/paulo-custodio/forth/ch-1.fs
@@ -0,0 +1,57 @@
+#! /usr/bin/env gforth
+
+\ Challenge 194
+\
+\ Task 1: Digital Clock
+\ Submitted by: Mohammad S Anwar
+\ You are given time in the format hh:mm with one missing digit.
+\
+\ Write a script to find the highest digit between 0-9 that makes it valid time.
+\
+\ Example 1
+\ Input: $time = '?5:00'
+\ Output: 1
+\
+\ Since 05:00 and 15:00 are valid time and no other digits can fit in the missing place.
+\ Example 2
+\ Input: $time = '?3:00'
+\ Output: 2
+\ Example 3
+\ Input: $time = '1?:00'
+\ Output: 9
+\ Example 4
+\ Input: $time = '2?:00'
+\ Output: 3
+\ Example 5
+\ Input: $time = '12:?5'
+\ Output: 5
+\ Example 6
+\ Input: $time = '12:5?'
+\ Output: 9
+
+0 VALUE clock
+: h1 ( -- d ) clock C@ ;
+: h2 ( -- d ) clock 1+ C@ ;
+: m1 ( -- d ) clock 3 + C@ ;
+: m2 ( -- d ) clock 4 + C@ ;
+: is_quest ( d -- f ) '?' = ;
+: is_01 ( d -- f ) DUP '0' >= SWAP '1' <= AND ;
+: is_03 ( d -- f ) DUP '0' >= SWAP '3' <= AND ;
+: is_05 ( d -- f ) DUP '0' >= SWAP '5' <= AND ;
+: is_2 ( d - f ) '2' = ;
+
+: missing_digit ( addr len -- d )
+ 5 <> IF DROP -1 ELSE
+ TO clock
+ h1 is_quest h2 is_03 AND IF 2 ELSE
+ h1 is_quest IF 1 ELSE
+ h1 is_01 h2 is_quest AND IF 9 ELSE
+ h1 is_2 h2 is_quest AND IF 3 ELSE
+ m1 is_quest IF 5 ELSE
+ m1 is_05 m2 is_quest AND IF 9 ELSE -1
+ THEN THEN THEN THEN THEN THEN
+ THEN
+;
+
+NEXT-ARG missing_digit . CR
+BYE
diff --git a/challenge-194/paulo-custodio/forth/ch-2.fs b/challenge-194/paulo-custodio/forth/ch-2.fs
new file mode 100644
index 0000000000..a2c89c2663
--- /dev/null
+++ b/challenge-194/paulo-custodio/forth/ch-2.fs
@@ -0,0 +1,72 @@
+#! /usr/bin/env gforth
+
+\ Challenge 194
+\
+\ Task 2: Frequency Equalizer
+\ Submitted by: Mohammad S Anwar
+\ You are given a string made of alphabetic characters only, a-z.
+\
+\ Write a script to determine whether removing only one character can make the
+\ frequency of the remaining characters the same.
+\
+\ Example 1:
+\ Input: $s = 'abbc'
+\ Output: 1 since removing one alphabet 'b' will give us 'abc' where each
+\ alphabet frequency is the same.
+\ Example 2:
+\ Input: $s = 'xyzyyxz'
+\ Output: 1 since removing 'y' will give us 'xzyyxz'.
+\ Example 3:
+\ Input: $s = 'xzxz'
+\ Output: 0 since removing any one alphabet would not give us string with same
+\ frequency alphabet.
+
+'z' 'a' - 1+ CONSTANT letters
+CREATE freq letters CELLS ALLOT
+
+: freq[] ( idx -- addr ) CELLS freq + ;
+
+: clear_freq ( -- ) freq letters CELLS ERASE ;
+
+: is_letter ( c -- f ) DUP 'a' >= SWAP 'z' <= AND ;
+
+: calc_freq ( addr len -- )
+ clear_freq
+ BOUNDS ?DO
+ I C@ DUP is_letter IF
+ 'a' - freq[] 1 SWAP +!
+ ELSE
+ DROP
+ THEN
+ LOOP
+;
+
+: min_freq ( -- n )
+ 100000
+ letters 0 DO
+ i freq[] @ ?DUP IF MIN THEN
+ LOOP
+;
+
+: max_freq ( -- n )
+ 0
+ letters 0 DO
+ i freq[] @ ?DUP IF MAX THEN
+ LOOP
+;
+
+: count_freq { n -- n }
+ 0
+ letters 0 DO
+ i freq[] @ n = IF 1+ THEN
+ LOOP
+;
+
+: freq_equalizer ( addr len -- n )
+ calc_freq
+ min_freq max_freq 2DUP 1- <> IF 2DROP 0 ELSE
+ NIP count_freq 1 <> IF 0 ELSE 1 THEN THEN
+;
+
+NEXT-ARG freq_equalizer . CR
+BYE
diff --git a/challenge-194/paulo-custodio/perl/ch-1.pl b/challenge-194/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..b693f32b06
--- /dev/null
+++ b/challenge-194/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,48 @@
+#!/usr/bin/perl
+
+# Challenge 194
+#
+# Task 1: Digital Clock
+# Submitted by: Mohammad S Anwar
+# You are given time in the format hh:mm with one missing digit.
+#
+# Write a script to find the highest digit between 0-9 that makes it valid time.
+#
+# Example 1
+# Input: $time = '?5:00'
+# Output: 1
+#
+# Since 05:00 and 15:00 are valid time and no other digits can fit in the missing place.
+# Example 2
+# Input: $time = '?3:00'
+# Output: 2
+# Example 3
+# Input: $time = '1?:00'
+# Output: 9
+# Example 4
+# Input: $time = '2?:00'
+# Output: 3
+# Example 5
+# Input: $time = '12:?5'
+# Output: 5
+# Example 6
+# Input: $time = '12:5?'
+# Output: 9
+
+use Modern::Perl;
+
+sub missing_digit {
+ my($clock)=@_;
+ $clock =~ /^[\d?]{2}:[\d?]{2}$/ or die "invalid format: $clock";
+ if ($clock =~ /^\?[0-3]/) { return 2; }
+ elsif ($clock =~ /^\?[4-9]/) { return 1; }
+ elsif ($clock =~ /^[0-1]\?/) { return 9; }
+ elsif ($clock =~ /^2\?/) { return 3; }
+ elsif ($clock =~ /^[3-9]/) { die "invalid format: $clock"; }
+ elsif ($clock =~ /^\d{2}:\?[0-9]/) { return 5; }
+ elsif ($clock =~ /^\d{2}:[0-5]\?/) { return 9; }
+ elsif ($clock =~ /^\d{2}:[6-9]/) { die "invalid format: $clock"; }
+ else { die "invalid format: $clock"; }
+}
+
+say missing_digit($ARGV[0]);
diff --git a/challenge-194/paulo-custodio/perl/ch-2.pl b/challenge-194/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..3effef7847
--- /dev/null
+++ b/challenge-194/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,39 @@
+#!/usr/bin/perl
+
+# Challenge 194
+#
+# Task 2: Frequency Equalizer
+# Submitted by: Mohammad S Anwar
+# You are given a string made of alphabetic characters only, a-z.
+#
+# Write a script to determine whether removing only one character can make the
+# frequency of the remaining characters the same.
+#
+# Example 1:
+# Input: $s = 'abbc'
+# Output: 1 since removing one alphabet 'b' will give us 'abc' where each
+# alphabet frequency is the same.
+# Example 2:
+# Input: $s = 'xyzyyxz'
+# Output: 1 since removing 'y' will give us 'xzyyxz'.
+# Example 3:
+# Input: $s = 'xzxz'
+# Output: 0 since removing any one alphabet would not give us string with same
+# frequency alphabet.
+
+use Modern::Perl;
+use List::Util qw( min max );
+
+sub freq_equalizer {
+ my($text)=@_;
+ my %freq; $freq{$_}++ for split //, $text;
+ my @freq=sort values %freq;
+ return 0 if @freq<1;
+ my $min=min(@freq);
+ my $max=max(@freq);
+ return 0 unless $min+1==$max;
+ return 1 if 1==scalar grep {$_==$max} @freq;
+ return 0;
+}
+
+say freq_equalizer($ARGV[0]);
diff --git a/challenge-194/paulo-custodio/t/test-1.yaml b/challenge-194/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..9f048bad99
--- /dev/null
+++ b/challenge-194/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,30 @@
+- setup:
+ cleanup:
+ args: ?5:00
+ input:
+ output: 1
+- setup:
+ cleanup:
+ args: ?3:00
+ input:
+ output: 2
+- setup:
+ cleanup:
+ args: 1?:00
+ input:
+ output: 9
+- setup:
+ cleanup:
+ args: 2?:00
+ input:
+ output: 3
+- setup:
+ cleanup:
+ args: 12:?5
+ input:
+ output: 5
+- setup:
+ cleanup:
+ args: 12:5?
+ input:
+ output: 9
diff --git a/challenge-194/paulo-custodio/t/test-2.yaml b/challenge-194/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..64394917a7
--- /dev/null
+++ b/challenge-194/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,15 @@
+- setup:
+ cleanup:
+ args: abbc
+ input:
+ output: 1
+- setup:
+ cleanup:
+ args: xyzyyxz
+ input:
+ output: 1
+- setup:
+ cleanup:
+ args: xzxz
+ input:
+ output: 0