diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2023-03-26 17:03:54 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2023-03-26 17:07:15 +0100 |
| commit | f95325491b24ee7c2476e4fe4bfeafcf39bd001e (patch) | |
| tree | 8af7a2cd45884a015dc9995690d73e5f91f8323c | |
| parent | 82f859ecba3ecddd256ed6169bc448be5322c10e (diff) | |
| download | perlweeklychallenge-club-f95325491b24ee7c2476e4fe4bfeafcf39bd001e.tar.gz perlweeklychallenge-club-f95325491b24ee7c2476e4fe4bfeafcf39bd001e.tar.bz2 perlweeklychallenge-club-f95325491b24ee7c2476e4fe4bfeafcf39bd001e.zip | |
Add Perl, C, C++, BASIC and Forth solutions
| -rw-r--r-- | challenge-195/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-195/paulo-custodio/basic/ch-1.bas | 42 | ||||
| -rw-r--r-- | challenge-195/paulo-custodio/basic/ch-2.bas | 52 | ||||
| -rw-r--r-- | challenge-195/paulo-custodio/c/ch-1.c | 53 | ||||
| -rw-r--r-- | challenge-195/paulo-custodio/c/ch-2.c | 70 | ||||
| -rw-r--r-- | challenge-195/paulo-custodio/cpp/ch-1.cpp | 51 | ||||
| -rw-r--r-- | challenge-195/paulo-custodio/cpp/ch-2.cpp | 58 | ||||
| -rw-r--r-- | challenge-195/paulo-custodio/forth/ch-1.fs | 54 | ||||
| -rw-r--r-- | challenge-195/paulo-custodio/forth/ch-2.fs | 77 | ||||
| -rw-r--r-- | challenge-195/paulo-custodio/perl/ch-1.pl | 43 | ||||
| -rw-r--r-- | challenge-195/paulo-custodio/perl/ch-2.pl | 46 | ||||
| -rw-r--r-- | challenge-195/paulo-custodio/t/test-1.yaml | 10 | ||||
| -rw-r--r-- | challenge-195/paulo-custodio/t/test-2.yaml | 15 |
13 files changed, 573 insertions, 0 deletions
diff --git a/challenge-195/paulo-custodio/Makefile b/challenge-195/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-195/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-195/paulo-custodio/basic/ch-1.bas b/challenge-195/paulo-custodio/basic/ch-1.bas new file mode 100644 index 0000000000..d3acc691d5 --- /dev/null +++ b/challenge-195/paulo-custodio/basic/ch-1.bas @@ -0,0 +1,42 @@ +' Challenge 195 +' +' Task 1: Special Integers +' Submitted by: Mohammad S Anwar +' You are given a positive integer, $n > 0. +' +' Write a script to print the count of all special integers between 1 and $n. +' +' An integer is special when all of its digits are unique. +' +' Example 1: +' Input: $n = 15 +' Output: 14 as except 11 all other integers between 1 and 15 are spcial. +' Example 2: +' Input: $n = 35 +' Output: 32 as except 11, 22, 33 all others are special. + +function is_special_int(n as integer) as boolean + dim digits(9) as integer, digit as integer + do while n>0 + digit=n mod 10 + n=n/10 + if digits(digit)>0 then + is_special_int=false + exit function + else + digits(digit)=1 + end if + loop + is_special_int=true +end function + +function count_special_ints(n as integer) as integer + dim i as integer, count as integer + count=0 + for i=1 to n + if is_special_int(i) then count=count+1 + next + count_special_ints=count +end function + +print count_special_ints(val(command(1))) diff --git a/challenge-195/paulo-custodio/basic/ch-2.bas b/challenge-195/paulo-custodio/basic/ch-2.bas new file mode 100644 index 0000000000..dd14196435 --- /dev/null +++ b/challenge-195/paulo-custodio/basic/ch-2.bas @@ -0,0 +1,52 @@ +' Challenge 195 +' +' Task 2: Most Frequent Even +' Submitted by: Mohammad S Anwar +' You are given a list of numbers, @list. +' +' Write a script to find most frequent even numbers in the list. In case you +' get more than one even numbers then return the smallest even integer. For all +' other case, return -1. +' +' Example 1 +' Input: @list = (1,1,2,6,2) +' Output: 2 as there are only 2 even numbers 2 and 6 and of those 2 appears +' the most. +' Example 2 +' Input: @list = (1,3,5,7) +' Output: -1 since no even numbers found in the list +' Example 3 +' Input: @list = (6,4,4,6,1) +' Output: 4 since there are only two even numbers 4 and 6. They both appears +' the equal number of times, so pick the smallest. + +sub collect_args(nums() as integer) + dim i as integer + i=0 + do while command(i+1)<>"" + redim preserve nums(i) as integer + nums(i)=val(command(i+1)) + i=i+1 + loop +end sub + +function most_freq_even(nums() as integer) as integer + dim hist() as integer, i as integer, min_even as integer, max_count as integer + for i=0 to ubound(nums) + if nums(i)>ubound(hist) then redim preserve hist(nums(i)) as integer + hist(nums(i))=hist(nums(i))+1 + next i + min_even=-1 + max_count=0 + for i=0 to ubound(hist) step 2 + if hist(i)>max_count then + max_count=hist(i) + min_even=i + end if + next + most_freq_even=min_even +end function + +dim nums() as integer +collect_args nums() +print most_freq_even(nums()) diff --git a/challenge-195/paulo-custodio/c/ch-1.c b/challenge-195/paulo-custodio/c/ch-1.c new file mode 100644 index 0000000000..a4d1c6fad7 --- /dev/null +++ b/challenge-195/paulo-custodio/c/ch-1.c @@ -0,0 +1,53 @@ +/* +Challenge 195 + +Task 1: Special Integers +Submitted by: Mohammad S Anwar +You are given a positive integer, $n > 0. + +Write a script to print the count of all special integers between 1 and $n. + +An integer is special when all of its digits are unique. + +Example 1: +Input: $n = 15 +Output: 14 as except 11 all other integers between 1 and 15 are spcial. +Example 2: +Input: $n = 35 +Output: 32 as except 11, 22, 33 all others are special. +*/ + +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> + +bool is_special_int(int n) { + bool digits[10]={0}; + while (n>0) { + int digit=n%10; + n/=10; + if (digits[digit]) + return false; + digits[digit]=true; + } + return true; +} + +int count_special_ints(int n) { + int count=0; + for (int i=1; i<=n; i++) + if (is_special_int(i)) + count++; + return count; +} + +int main(int argc, char* argv[]) { + argv++; argc--; + if (argc != 1) { + fputs("usage: ch-1 n\n", stderr); + return EXIT_FAILURE; + } + + int n=atoi(argv[0]); + printf("%d\n", count_special_ints(n)); +} diff --git a/challenge-195/paulo-custodio/c/ch-2.c b/challenge-195/paulo-custodio/c/ch-2.c new file mode 100644 index 0000000000..6fae4766c0 --- /dev/null +++ b/challenge-195/paulo-custodio/c/ch-2.c @@ -0,0 +1,70 @@ +/* +Challenge 195 + +Task 2: Most Frequent Even +Submitted by: Mohammad S Anwar +You are given a list of numbers, @list. + +Write a script to find most frequent even numbers in the list. In case you +get more than one even numbers then return the smallest even integer. For all +other case, return -1. + +Example 1 +Input: @list = (1,1,2,6,2) +Output: 2 as there are only 2 even numbers 2 and 6 and of those 2 appears +the most. +Example 2 +Input: @list = (1,3,5,7) +Output: -1 since no even numbers found in the list +Example 3 +Input: @list = (6,4,4,6,1) +Output: 4 since there are only two even numbers 4 and 6. They both appears +the equal number of times, so pick the smallest. +*/ + +#include <stdio.h> +#include <stdlib.h> + +void* check_mem(void* p) { + if (!p) { + fputs("Out of memory", stderr); + exit(EXIT_FAILURE); + } + return p; +} + +int most_freq_even(int* nums, int nums_size) { + int hist_size=0; + for (int i=0; i<nums_size; i++) + if (hist_size<nums[i]) + hist_size=nums[i]; + int* hist=check_mem(calloc(++hist_size, sizeof(int))); + for (int i=0; i<nums_size; i++) + hist[nums[i]]++; + int max_count=0; + int max_even=-1; + for (int i=0; i<hist_size; i+=2) { + if (hist[i]>max_count) { + max_count=hist[i]; + max_even=i; + } + } + free(hist); + return max_even; +} + +int main(int argc, char* argv[]) { + argv++; argc--; + if (argc == 0) { + fputs("usage: ch-2 nums...\n", stderr); + return EXIT_FAILURE; + } + + int* nums = check_mem(malloc(argc * sizeof(int))); + for (int i = 0; i < argc; i++) + nums[i] = atoi(argv[i]); + + printf("%d\n", most_freq_even(nums, argc)); + + free(nums); +} diff --git a/challenge-195/paulo-custodio/cpp/ch-1.cpp b/challenge-195/paulo-custodio/cpp/ch-1.cpp new file mode 100644 index 0000000000..8c5ebd76a7 --- /dev/null +++ b/challenge-195/paulo-custodio/cpp/ch-1.cpp @@ -0,0 +1,51 @@ +/* +Challenge 195 + +Task 1: Special Integers +Submitted by: Mohammad S Anwar +You are given a positive integer, $n > 0. + +Write a script to print the count of all special integers between 1 and $n. + +An integer is special when all of its digits are unique. + +Example 1: +Input: $n = 15 +Output: 14 as except 11 all other integers between 1 and 15 are spcial. +Example 2: +Input: $n = 35 +Output: 32 as except 11, 22, 33 all others are special. +*/ + +#include <iostream> + +bool is_special_int(int n) { + bool digits[10]={0}; + while (n>0) { + int digit=n%10; + n/=10; + if (digits[digit]) + return false; + digits[digit]=true; + } + return true; +} + +int count_special_ints(int n) { + int count=0; + for (int i=1; i<=n; i++) + if (is_special_int(i)) + count++; + return count; +} + +int main(int argc, char* argv[]) { + argv++; argc--; + if (argc != 1) { + std::cerr<<"usage: ch-1 n"<<std::endl; + return EXIT_FAILURE; + } + + int n=atoi(argv[0]); + std::cout<<count_special_ints(n)<<std::endl; +} diff --git a/challenge-195/paulo-custodio/cpp/ch-2.cpp b/challenge-195/paulo-custodio/cpp/ch-2.cpp new file mode 100644 index 0000000000..ad568c1573 --- /dev/null +++ b/challenge-195/paulo-custodio/cpp/ch-2.cpp @@ -0,0 +1,58 @@ +/* +Challenge 195 + +Task 2: Most Frequent Even +Submitted by: Mohammad S Anwar +You are given a list of numbers, @list. + +Write a script to find most frequent even numbers in the list. In case you +get more than one even numbers then return the smallest even integer. For all +other case, return -1. + +Example 1 +Input: @list = (1,1,2,6,2) +Output: 2 as there are only 2 even numbers 2 and 6 and of those 2 appears +the most. +Example 2 +Input: @list = (1,3,5,7) +Output: -1 since no even numbers found in the list +Example 3 +Input: @list = (6,4,4,6,1) +Output: 4 since there are only two even numbers 4 and 6. They both appears +the equal number of times, so pick the smallest. +*/ + +#include <iostream> +#include <vector> + +int most_freq_even(const std::vector<int>& nums) { + std::vector<int> hist; + for (size_t i=0; i<nums.size(); i++) { + if (nums[i]>=static_cast<int>(hist.size())) + hist.resize(nums[i]+1); + hist[nums[i]]++; + } + int max_count=0; + int max_even=-1; + for (size_t i=0; i<hist.size(); i+=2) { + if (hist[i]>max_count) { + max_count=hist[i]; + max_even=i; + } + } + return max_even; +} + +int main(int argc, char* argv[]) { + argv++; argc--; + if (argc == 0) { + std::cerr<<"usage: ch-2 nums..."<<std::endl; + return EXIT_FAILURE; + } + + std::vector<int> nums; + for (int i = 0; i < argc; i++) + nums.push_back(atoi(argv[i])); + + std::cout<<most_freq_even(nums)<<std::endl; +} diff --git a/challenge-195/paulo-custodio/forth/ch-1.fs b/challenge-195/paulo-custodio/forth/ch-1.fs new file mode 100644 index 0000000000..54118c7503 --- /dev/null +++ b/challenge-195/paulo-custodio/forth/ch-1.fs @@ -0,0 +1,54 @@ +#! /usr/bin/env gforth + +\ Challenge 195 +\ +\ Task 1: Special Integers +\ Submitted by: Mohammad S Anwar +\ You are given a positive integer, $n > 0. +\ +\ Write a script to print the count of all special integers between 1 and $n. +\ +\ An integer is special when all of its digits are unique. +\ +\ Example 1: +\ Input: $n = 15 +\ Output: 14 as except 11 all other integers between 1 and 15 are spcial. +\ Example 2: +\ Input: $n = 35 +\ Output: 32 as except 11, 22, 33 all others are special. + +CREATE digits 10 CELLS ALLOT + +: clear_digits ( -- ) digits 10 CELLS ERASE ; + +: count_digits ( n -- ) + BEGIN DUP 0> WHILE + DUP 10 MOD CELLS digits + 1 SWAP +! + 10 / + REPEAT + DROP +; + +: has_duplicate_digit ( -- f ) + FALSE + 10 0 DO + I CELLS digits + @ 1 > IF DROP TRUE THEN + LOOP +; + +: is_special_digit ( n -- f ) + clear_digits + count_digits + has_duplicate_digit 0= +; + +: count_special_ints ( n -- count ) + 0 SWAP + 1+ 1 ?DO + I is_special_digit IF 1+ THEN + LOOP +; + +NEXT-ARG 0 0 2SWAP >NUMBER 2DROP DROP +count_special_ints . CR +BYE diff --git a/challenge-195/paulo-custodio/forth/ch-2.fs b/challenge-195/paulo-custodio/forth/ch-2.fs new file mode 100644 index 0000000000..03d0eff8ad --- /dev/null +++ b/challenge-195/paulo-custodio/forth/ch-2.fs @@ -0,0 +1,77 @@ +#! /usr/bin/env gforth + +\ Challenge 195 +\ +\ Task 2: Most Frequent Even +\ Submitted by: Mohammad S Anwar +\ You are given a list of numbers, @list. +\ +\ Write a script to find most frequent even numbers in the list. In case you +\ get more than one even numbers then return the smallest even integer. For all +\ other case, return -1. +\ +\ Example 1 +\ Input: @list = (1,1,2,6,2) +\ Output: 2 as there are only 2 even numbers 2 and 6 and of those 2 appears +\ the most. +\ Example 2 +\ Input: @list = (1,3,5,7) +\ Output: -1 since no even numbers found in the list +\ Example 3 +\ Input: @list = (6,4,4,6,1) +\ Output: 4 since there are only two even numbers 4 and 6. They both appears +\ the equal number of times, so pick the smallest. + +CREATE nums 256 CELLS ALLOT +0 VALUE nums_size + +: nums[] ( idx -- addr ) + CELLS nums + +; + +: collect_args ( -- ) + BEGIN NEXT-ARG DUP WHILE + 0 0 2SWAP >NUMBER 2DROP DROP + nums_size nums[] ! + nums_size 1+ TO nums_size + REPEAT + 2DROP +; + +: max_nums ( -- n ) + 0 + nums_size 0 ?DO + I nums[] @ MAX + LOOP +; + +0 VALUE hist +0 VALUE hist_size + +: hist[] ( idx -- addr ) + CELLS hist + +; + +: make_hist ( -- ) + max_nums 1+ DUP TO hist_size + CELLS ALLOCATE THROW TO hist + hist hist_size CELLS ERASE + nums_size 0 ?DO + I nums[] @ hist[] 1 SWAP +! + LOOP +; + +: max_even ( -- ) + make_hist + -1 0 ( n max ) + hist_size 0 ?DO + I hist[] @ OVER > IF + 2DROP I I hist[] @ + THEN + 2 +LOOP + DROP +; + +collect_args +max_even . CR +BYE diff --git a/challenge-195/paulo-custodio/perl/ch-1.pl b/challenge-195/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..05727ce3f5 --- /dev/null +++ b/challenge-195/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,43 @@ +#!/usr/bin/perl + +# Challenge 195 +# +# Task 1: Special Integers +# Submitted by: Mohammad S Anwar +# You are given a positive integer, $n > 0. +# +# Write a script to print the count of all special integers between 1 and $n. +# +# An integer is special when all of its digits are unique. +# +# Example 1: +# Input: $n = 15 +# Output: 14 as except 11 all other integers between 1 and 15 are spcial. +# Example 2: +# Input: $n = 35 +# Output: 32 as except 11, 22, 33 all others are special. + +use Modern::Perl; + +sub is_special_int { + my($n)=@_; + my @digits; + for my $digit (split //, $n) { + return 0 if $digits[$digit]; + $digits[$digit]=1; + } + return 1; +} + +sub count_special_ints { + my($n)=@_; + my $count=0; + for my $i (1..$n) { + $count++ if is_special_int($i); + } + return $count; +} + +@ARGV==1 or die "usage: ch-1.pl n\n"; +my $n=shift; +say count_special_ints($n); diff --git a/challenge-195/paulo-custodio/perl/ch-2.pl b/challenge-195/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..59c6f2fa3b --- /dev/null +++ b/challenge-195/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,46 @@ +#!/usr/bin/perl + +# Challenge 195 +# +# Task 2: Most Frequent Even +# Submitted by: Mohammad S Anwar +# You are given a list of numbers, @list. +# +# Write a script to find most frequent even numbers in the list. In case you +# get more than one even numbers then return the smallest even integer. For all +# other case, return -1. +# +# Example 1 +# Input: @list = (1,1,2,6,2) +# Output: 2 as there are only 2 even numbers 2 and 6 and of those 2 appears +# the most. +# Example 2 +# Input: @list = (1,3,5,7) +# Output: -1 since no even numbers found in the list +# Example 3 +# Input: @list = (6,4,4,6,1) +# Output: 4 since there are only two even numbers 4 and 6. They both appears +# the equal number of times, so pick the smallest. + +use Modern::Perl; + +sub most_freq_even { + my(@n)=@_; + my %hist; + $hist{$_}++ for grep {$_%2==0} @n; + my $most_freq=0; + my $most_freq_num=-1; + while (my($num, $freq)=each %hist) { + if ($freq>$most_freq) { + ($most_freq_num, $most_freq) = ($num, $freq); + } + elsif ($freq==$most_freq && $num<$most_freq_num) { + ($most_freq_num, $most_freq) = ($num, $freq); + } + } + return $most_freq_num; +} + +@ARGV or die "usage: ch-2.pl nums...\n"; +my @n=@ARGV; +say most_freq_even(@n); diff --git a/challenge-195/paulo-custodio/t/test-1.yaml b/challenge-195/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..b264e350eb --- /dev/null +++ b/challenge-195/paulo-custodio/t/test-1.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: 15 + input: + output: 14 +- setup: + cleanup: + args: 35 + input: + output: 32 diff --git a/challenge-195/paulo-custodio/t/test-2.yaml b/challenge-195/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..f5e1a13445 --- /dev/null +++ b/challenge-195/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 1 1 2 6 2 + input: + output: 2 +- setup: + cleanup: + args: 1 3 5 7 + input: + output: -1 +- setup: + cleanup: + args: 6 4 4 6 1 + input: + output: 4 |
