diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2023-03-06 19:02:28 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2023-03-06 19:02:28 +0000 |
| commit | 5da3c4efe8be89a6298831a6ae9a28dbacd5df3b (patch) | |
| tree | 7b113924ad8db40210dc4a1c2eaf4ba29537eb99 | |
| parent | d75c83429332efc88f29eee14f988b199f2fa10c (diff) | |
| download | perlweeklychallenge-club-5da3c4efe8be89a6298831a6ae9a28dbacd5df3b.tar.gz perlweeklychallenge-club-5da3c4efe8be89a6298831a6ae9a28dbacd5df3b.tar.bz2 perlweeklychallenge-club-5da3c4efe8be89a6298831a6ae9a28dbacd5df3b.zip | |
Add Perl, C, C++ and Forth solutions
| -rw-r--r-- | challenge-207/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-207/paulo-custodio/basic/ch-1.bas | 59 | ||||
| -rw-r--r-- | challenge-207/paulo-custodio/basic/ch-2.bas | 70 | ||||
| -rw-r--r-- | challenge-207/paulo-custodio/c/ch-1.c | 67 | ||||
| -rw-r--r-- | challenge-207/paulo-custodio/c/ch-2.c | 61 | ||||
| -rw-r--r-- | challenge-207/paulo-custodio/cpp/ch-1.cpp | 59 | ||||
| -rw-r--r-- | challenge-207/paulo-custodio/cpp/ch-2.cpp | 54 | ||||
| -rw-r--r-- | challenge-207/paulo-custodio/forth/ch-1.fs | 85 | ||||
| -rw-r--r-- | challenge-207/paulo-custodio/forth/ch-2.fs | 78 | ||||
| -rw-r--r-- | challenge-207/paulo-custodio/perl/ch-1.pl | 45 | ||||
| -rw-r--r-- | challenge-207/paulo-custodio/perl/ch-2.pl | 47 | ||||
| -rw-r--r-- | challenge-207/paulo-custodio/t/test-1.yaml | 5 | ||||
| -rw-r--r-- | challenge-207/paulo-custodio/t/test-2.yaml | 15 |
13 files changed, 647 insertions, 0 deletions
diff --git a/challenge-207/paulo-custodio/Makefile b/challenge-207/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-207/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-207/paulo-custodio/basic/ch-1.bas b/challenge-207/paulo-custodio/basic/ch-1.bas new file mode 100644 index 0000000000..d5f8593713 --- /dev/null +++ b/challenge-207/paulo-custodio/basic/ch-1.bas @@ -0,0 +1,59 @@ +' Challenge 207 +' +' Task 1: Keyboard Word +' Submitted by: Mohammad S Anwar +' +' You are given an array of words. +' +' Write a script to print all the words in the given array that can be types +' using alphabet on only one row of the keyboard. +' +' Let us assume the keys are arranged as below: +' +' Row 1: qwertyuiop +' Row 2: asdfghjkl +' Row 3: zxcvbnm +' +' Example 1 +' +' Input: @words = ("Hello","Alaska","Dad","Peace") +' Output: ("Alaska","Dad") +' +' Example 2 +' +' Input: @array = ("OMG","Bye") +' Output: () + +function remove_char(byval s as string, c as string) as string + dim p as integer + + p=instr(s,c) + do while p>0 + s=left(s,p-1)+mid(s,p+1) + p=instr(s,c) + loop + remove_char=s +end function + +function can_type_word(k as string, byval s as string) as boolean + dim i as integer + for i=1 to len(k) + s=remove_char(s,mid(k,i,1)) + next + can_type_word=s="" +end function + +function can_type(s as string) as boolean + s=lcase(s) + can_type=can_type_word("qwertyuiop",s) or _ + can_type_word("asdfghjkl",s) or _ + can_type_word("zxcvbnm",s) +end function + +dim i as integer +i=1 +do while command(i)<>"" + if can_type(command(i)) then print command(i);" "; + i=i+1 +loop +print diff --git a/challenge-207/paulo-custodio/basic/ch-2.bas b/challenge-207/paulo-custodio/basic/ch-2.bas new file mode 100644 index 0000000000..2e2dfae944 --- /dev/null +++ b/challenge-207/paulo-custodio/basic/ch-2.bas @@ -0,0 +1,70 @@ +' Challenge 207 +' +' Task 2: H-Index +' Submitted by: Mohammad S Anwar +' +' You are given an array of integers containing citations a researcher has +' received for each paper. +' +' Write a script to compute the researcher’s H-Index. For more information +' please checkout the wikipedia page. +' +' The H-Index is the largest number h such that h articles have at least h +' citations each. For example, if an author has five publications, +' with 9, 7, 6, 2, and 1 citations (ordered from greatest to least), +' then the author’s h-index is 3, because the author has three publications +' with 3 or more citations. However, the author does not have four +' publications with 4 or more citations. +' +' +' Example 1 +' +' Input: @citations = (10,8,5,4,3) +' Output: 4 +' +' Because the 4th publication has 4 citations and the 5th has only 3. +' +' Example 2 +' +' Input: @citations = (25,8,5,3,3) +' Output: 3 +' +' The H-Index is 3 because the fourth paper has only 3 citations. + +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 + +sub rsort(mins() as integer) + dim i as integer, j as integer, n as integer + + for i=lbound(mins) to ubound(mins)-1 + for j=i+1 to ubound(mins) + if mins(i)<mins(j) then + n=mins(i): mins(i)=mins(j): mins(j)=n + end if + next + next +end sub + +function h_index(nums() as integer) as integer + dim i as integer + for i=0 to ubound(nums) + if nums(i)<i+1 then + h_index=i+1-1 + exit function + end if + next + h_index=-1 +end function + +dim nums() as integer +collect_args nums() +rsort nums() +print h_index(nums()) diff --git a/challenge-207/paulo-custodio/c/ch-1.c b/challenge-207/paulo-custodio/c/ch-1.c new file mode 100644 index 0000000000..40f5e2ccbd --- /dev/null +++ b/challenge-207/paulo-custodio/c/ch-1.c @@ -0,0 +1,67 @@ +/* +Challenge 207 + +Task 1: Keyboard Word +Submitted by: Mohammad S Anwar + +You are given an array of words. + +Write a script to print all the words in the given array that can be types +using alphabet on only one row of the keyboard. + +Let us assume the keys are arranged as below: + +Row 1: qwertyuiop +Row 2: asdfghjkl +Row 3: zxcvbnm + +Example 1 + +Input: @words = ("Hello","Alaska","Dad","Peace") +Output: ("Alaska","Dad") + +Example 2 + +Input: @array = ("OMG","Bye") +Output: () +*/ + +#define MAX_WORD 256 + +#include <assert.h> +#include <ctype.h> +#include <stdio.h> +#include <string.h> +#include <stdbool.h> + +void remove_char(char* word, int ch) { + char* p; + while ((p = strchr(word, ch)) != NULL) { + memmove(p, p+1, strlen(p+1)+1); // copy also null char + } +} + +bool can_type_word(const char* keys, const char* word_) { + char word[MAX_WORD]; + assert(strlen(word_) < MAX_WORD); + strcpy(word, word_); + for (char* p = word; *p != '\0'; p++) + *p = tolower(*p); + for (const char* p = keys; *p != '\0'; p++) + remove_char(word, tolower(*p)); + return word[0] == '\0'; +} + +bool can_type(const char* word) { + return can_type_word("qwertyuiop", word) || + can_type_word("asdfghjkl", word) || + can_type_word("zxcvbnm", word); +} + +int main(int argc, char* argv[]) { + argv++; argc--; + for (int i = 0; i < argc; i++) + if (can_type(argv[i])) + printf("%s ", argv[i]); + printf("\n"); +} diff --git a/challenge-207/paulo-custodio/c/ch-2.c b/challenge-207/paulo-custodio/c/ch-2.c new file mode 100644 index 0000000000..c051a6100f --- /dev/null +++ b/challenge-207/paulo-custodio/c/ch-2.c @@ -0,0 +1,61 @@ +/* +Challenge 207 + +Task 2: H-Index +Submitted by: Mohammad S Anwar + +You are given an array of integers containing citations a researcher has +received for each paper. + +Write a script to compute the researcher’s H-Index. For more information +please checkout the wikipedia page. + + The H-Index is the largest number h such that h articles have at least h + citations each. For example, if an author has five publications, + with 9, 7, 6, 2, and 1 citations (ordered from greatest to least), + then the author’s h-index is 3, because the author has three publications + with 3 or more citations. However, the author does not have four + publications with 4 or more citations. + + +Example 1 + +Input: @citations = (10,8,5,4,3) +Output: 4 + +Because the 4th publication has 4 citations and the 5th has only 3. + +Example 2 + +Input: @citations = (25,8,5,3,3) +Output: 3 + +The H-Index is 3 because the fourth paper has only 3 citations. +*/ + +#define MAX_ARTICLES 256 + +int articles[MAX_ARTICLES]; + +#include <assert.h> +#include <stdio.h> +#include <stdlib.h> + +int compare(const void* a, const void* b) { + return *(int*)b - *(int*)a; +} + +int h_index(int articles[], int num_articles) { + qsort(articles, num_articles, sizeof(int), compare); + for (int i = 0; i < num_articles; i++) + if (articles[i] < i+1) + return i+1-1; + return -1; +} + +int main(int argc, char* argv[]) { + argv++; argc--; + for (int i = 0; i < argc; i++) + articles[i] = atoi(argv[i]); + printf("%d\n", h_index(articles, argc)); +} diff --git a/challenge-207/paulo-custodio/cpp/ch-1.cpp b/challenge-207/paulo-custodio/cpp/ch-1.cpp new file mode 100644 index 0000000000..f8c8738239 --- /dev/null +++ b/challenge-207/paulo-custodio/cpp/ch-1.cpp @@ -0,0 +1,59 @@ +/* +Challenge 207 + +Task 1: Keyboard Word +Submitted by: Mohammad S Anwar + +You are given an array of words. + +Write a script to print all the words in the given array that can be types +using alphabet on only one row of the keyboard. + +Let us assume the keys are arranged as below: + +Row 1: qwertyuiop +Row 2: asdfghjkl +Row 3: zxcvbnm + +Example 1 + +Input: @words = ("Hello","Alaska","Dad","Peace") +Output: ("Alaska","Dad") + +Example 2 + +Input: @array = ("OMG","Bye") +Output: () +*/ + +#include <algorithm> +#include <cctype> +#include <iostream> +#include <string> + +void remove_char(std::string& word, int ch) { + word.erase(std::remove(word.begin(), word.end(), tolower(ch)), word.end()); +} + +bool can_type_word(const std::string& keys, const std::string& word_) { + std::string word; + for (auto& ch : word_) + word.push_back(tolower(ch)); + for (auto& ch : keys) + remove_char(word, tolower(ch)); + return word.size() == 0; +} + +bool can_type(const std::string& word) { + return can_type_word("qwertyuiop", word) || + can_type_word("asdfghjkl", word) || + can_type_word("zxcvbnm", word); +} + +int main(int argc, char* argv[]) { + argv++; argc--; + for (int i = 0; i < argc; i++) + if (can_type(argv[i])) + std::cout << argv[i]; + std::cout << std::endl; +} diff --git a/challenge-207/paulo-custodio/cpp/ch-2.cpp b/challenge-207/paulo-custodio/cpp/ch-2.cpp new file mode 100644 index 0000000000..79ebd8ea8e --- /dev/null +++ b/challenge-207/paulo-custodio/cpp/ch-2.cpp @@ -0,0 +1,54 @@ +/* +Challenge 207 + +Task 2: H-Index +Submitted by: Mohammad S Anwar + +You are given an array of integers containing citations a researcher has +received for each paper. + +Write a script to compute the researcher’s H-Index. For more information +please checkout the wikipedia page. + + The H-Index is the largest number h such that h articles have at least h + citations each. For example, if an author has five publications, + with 9, 7, 6, 2, and 1 citations (ordered from greatest to least), + then the author’s h-index is 3, because the author has three publications + with 3 or more citations. However, the author does not have four + publications with 4 or more citations. + + +Example 1 + +Input: @citations = (10,8,5,4,3) +Output: 4 + +Because the 4th publication has 4 citations and the 5th has only 3. + +Example 2 + +Input: @citations = (25,8,5,3,3) +Output: 3 + +The H-Index is 3 because the fourth paper has only 3 citations. +*/ + +#include <algorithm> +#include <iostream> +#include <vector> + +int h_index(std::vector<int>& articles) { + std::sort(articles.rbegin(), articles.rend()); + for (size_t i = 0; i < articles.size(); i++) + if (articles[i] < i+1) + return i+1-1; + return -1; +} + +int main(int argc, char* argv[]) { + argv++; argc--; + std::vector<int> articles; + for (int i = 0; i < argc; i++) + articles.push_back(atoi(argv[i])); + std::cout << h_index(articles) << std::endl; +} diff --git a/challenge-207/paulo-custodio/forth/ch-1.fs b/challenge-207/paulo-custodio/forth/ch-1.fs new file mode 100644 index 0000000000..095c3b20a2 --- /dev/null +++ b/challenge-207/paulo-custodio/forth/ch-1.fs @@ -0,0 +1,85 @@ +#! /usr/bin/env gforth + +\ Challenge 207 +\ +\ Task 1: Keyboard Word +\ Submitted by: Mohammad S Anwar +\ +\ You are given an array of words. +\ +\ Write a script to print all the words in the given array that can be types +\ using alphabet on only one row of the keyboard. +\ +\ Let us assume the keys are arranged as below: +\ +\ Row 1: qwertyuiop +\ Row 2: asdfghjkl +\ Row 3: zxcvbnm +\ +\ Example 1 +\ +\ Input: @words = ("Hello","Alaska","Dad","Peace") +\ Output: ("Alaska","Dad") +\ +\ Example 2 +\ +\ Input: @array = ("OMG","Bye") +\ Output: () + + +\ copy text to counted string, convert to upper case +: copy_text { addr-from len addr-to -- } + len 0 ?DO + addr-from I + C@ TOUPPER + addr-to 1+ I + C! + LOOP + len addr-to C! +; + +\ remove char from text +CREATE remove_char_ch 1 ALLOT +: remove_char { addr-cstr ch -- } + ch TOUPPER remove_char_ch C! + BEGIN + addr-cstr COUNT remove_char_ch 1 SEARCH \ while char is found + WHILE + { rest-addr rest-len } + rest-addr 1+ rest-addr rest-len CMOVE + addr-cstr C@ 1- addr-cstr C! + REPEAT + 2DROP +; + +\ check if word can be typed with chars +CREATE check_text 256 ALLOT +: can_type_word { addr-keys len-keys addr-text len-text -- } + addr-text len-text check_text copy_text \ make a copy + len-keys 0 ?DO + addr-keys I + C@ ( key ) + check_text SWAP remove_char + LOOP + check_text C@ 0= \ true if empty +; + +\ check if word can by typed with one row of the keyboard +: can_type { addr-text len-text -- } + S" qwertyuiop" addr-text len-text can_type_word + S" asdfghjkl" addr-text len-text can_type_word OR + S" zxcvbnm" addr-text len-text can_type_word OR +; + +\ check all command line words +: task ( -- ) + BEGIN NEXT-ARG DUP WHILE + 2DUP can_type IF + TYPE SPACE + ELSE + 2DROP + ENDIF + REPEAT + 2DROP + CR +; + +task +BYE diff --git a/challenge-207/paulo-custodio/forth/ch-2.fs b/challenge-207/paulo-custodio/forth/ch-2.fs new file mode 100644 index 0000000000..51ac744491 --- /dev/null +++ b/challenge-207/paulo-custodio/forth/ch-2.fs @@ -0,0 +1,78 @@ +\ Challenge 207 +\ +\ Task 2: H-Index +\ Submitted by: Mohammad S Anwar +\ +\ You are given an array of integers containing citations a researcher has +\ received for each paper. +\ +\ Write a script to compute the researcher’s H-Index. For more information +\ please checkout the wikipedia page. +\ +\ The H-Index is the largest number h such that h articles have at least h +\ citations each. For example, if an author has five publications, +\ with 9, 7, 6, 2, and 1 citations (ordered from greatest to least), +\ then the author’s h-index is 3, because the author has three publications +\ with 3 or more citations. However, the author does not have four +\ publications with 4 or more citations. +\ +\ +\ Example 1 +\ +\ Input: @citations = (10,8,5,4,3) +\ Output: 4 +\ +\ Because the 4th publication has 4 citations and the 5th has only 3. +\ +\ Example 2 +\ +\ Input: @citations = (25,8,5,3,3) +\ Output: 3 +\ +\ The H-Index is 3 because the fourth paper has only 3 citations. + +CREATE citations 256 CELLS ALLOT +0 VALUE num_citations + +: citation[] ( i -- addr ) + CELLS citations + +; + +: collect_args ( -- ) + BEGIN NEXT-ARG DUP WHILE + 0 0 2SWAP >NUMBER 2DROP DROP + num_citations citation[] ! + num_citations 1+ TO num_citations + REPEAT + 2DROP +; + +\ reverse sort array of integers +: rsort ( -- ) + num_citations 0> IF + num_citations 1- 0 ?DO + num_citations I 1+ ?DO + J citation[] @ + I citation[] @ + < IF + J citation[] @ I citation[] @ J citation[] ! I citation[] ! + THEN + LOOP + LOOP + THEN +; + +\ H-index +: h_index ( -- n ) + -1 + num_citations 0 ?DO + I citation[] @ I 1+ < IF + DROP I LEAVE + THEN + LOOP +; + +collect_args +rsort +h_index . CR +BYE diff --git a/challenge-207/paulo-custodio/perl/ch-1.pl b/challenge-207/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..30b23a1f46 --- /dev/null +++ b/challenge-207/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,45 @@ +#!/usr/bin/perl + +# Challenge 207 +# +# Task 1: Keyboard Word +# Submitted by: Mohammad S Anwar +# +# You are given an array of words. +# +# Write a script to print all the words in the given array that can be types +# using alphabet on only one row of the keyboard. +# +# Let us assume the keys are arranged as below: +# +# Row 1: qwertyuiop +# Row 2: asdfghjkl +# Row 3: zxcvbnm +# +# Example 1 +# +# Input: @words = ("Hello","Alaska","Dad","Peace") +# Output: ("Alaska","Dad") +# +# Example 2 +# +# Input: @array = ("OMG","Bye") +# Output: () + +use Modern::Perl; +use List::Util qw( any ); + +my @keys = qw( qwertyuiop asdfghjkl zxcvbnm ); + +sub can_type_word { + my($word) = @_; + $word =~ s/\W//g; + return any {($word =~ s/[$_]//gir) eq ""} @keys; +} + +sub can_type { + my(@words) = @_; + return grep {can_type_word($_)} @words; +} + +say join(" ", can_type(@ARGV)); diff --git a/challenge-207/paulo-custodio/perl/ch-2.pl b/challenge-207/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..b02cda55fe --- /dev/null +++ b/challenge-207/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,47 @@ +#!/usr/bin/perl + +# Challenge 207 +# +# Task 2: H-Index +# Submitted by: Mohammad S Anwar +# +# You are given an array of integers containing citations a researcher has +# received for each paper. +# +# Write a script to compute the researcher’s H-Index. For more information +# please checkout the wikipedia page. +# +# The H-Index is the largest number h such that h articles have at least h +# citations each. For example, if an author has five publications, +# with 9, 7, 6, 2, and 1 citations (ordered from greatest to least), +# then the author’s h-index is 3, because the author has three publications +# with 3 or more citations. However, the author does not have four +# publications with 4 or more citations. +# +# +# Example 1 +# +# Input: @citations = (10,8,5,4,3) +# Output: 4 +# +# Because the 4th publication has 4 citations and the 5th has only 3. +# +# Example 2 +# +# Input: @citations = (25,8,5,3,3) +# Output: 3 +# +# The H-Index is 3 because the fourth paper has only 3 citations. + +use Modern::Perl; + +sub h_index { + my(@citations) = @_; + @citations = sort {$b<=>$a} @citations; # sort descending + for my $h (1 .. @citations) { + return $h-1 if $citations[$h-1] < $h; + } + die; +} + +say h_index(@ARGV); diff --git a/challenge-207/paulo-custodio/t/test-1.yaml b/challenge-207/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..816bf79e37 --- /dev/null +++ b/challenge-207/paulo-custodio/t/test-1.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: Hello Alaska Dad Peace + input: + output: Alaska Dad diff --git a/challenge-207/paulo-custodio/t/test-2.yaml b/challenge-207/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..6c231c3d62 --- /dev/null +++ b/challenge-207/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 10 8 5 4 3 + input: + output: 4 +- setup: + cleanup: + args: 25 8 5 3 3 + input: + output: 3 +- setup: + cleanup: + args: 3 4 5 8 10 + input: + output: 4 |
