diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-01-26 22:00:25 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-01-28 00:44:55 +0000 |
| commit | 439ca978bac8ffcfc6cd59bf18593302713b7fab (patch) | |
| tree | bd5a9f18810bd6856403b98580ea6e12e22c7dc3 /challenge-001 | |
| parent | 978ad5065e463bf52cd11ea13c1a2c1519a2c07b (diff) | |
| download | perlweeklychallenge-club-439ca978bac8ffcfc6cd59bf18593302713b7fab.tar.gz perlweeklychallenge-club-439ca978bac8ffcfc6cd59bf18593302713b7fab.tar.bz2 perlweeklychallenge-club-439ca978bac8ffcfc6cd59bf18593302713b7fab.zip | |
Challenge 001: fix Perl and Forth, add Basic, C, C++, Lua and Python solutions
Diffstat (limited to 'challenge-001')
| -rw-r--r-- | challenge-001/paulo-custodio/basic/ch-1.bas | 33 | ||||
| -rw-r--r-- | challenge-001/paulo-custodio/basic/ch-2.bas | 21 | ||||
| -rw-r--r-- | challenge-001/paulo-custodio/c/ch-1.c | 46 | ||||
| -rw-r--r-- | challenge-001/paulo-custodio/c/ch-2.c | 27 | ||||
| -rw-r--r-- | challenge-001/paulo-custodio/cpp/ch-1.cpp | 34 | ||||
| -rw-r--r-- | challenge-001/paulo-custodio/cpp/ch-2.cpp | 27 | ||||
| -rw-r--r-- | challenge-001/paulo-custodio/forth/ch-1.fs | 39 | ||||
| -rw-r--r-- | challenge-001/paulo-custodio/lua/ch-1.lua | 20 | ||||
| -rw-r--r-- | challenge-001/paulo-custodio/lua/ch-2.lua | 19 | ||||
| -rw-r--r-- | challenge-001/paulo-custodio/perl/ch-1.pl | 6 | ||||
| -rw-r--r-- | challenge-001/paulo-custodio/perl/ch-2.pl | 10 | ||||
| -rw-r--r-- | challenge-001/paulo-custodio/python/ch-1.py | 13 | ||||
| -rw-r--r-- | challenge-001/paulo-custodio/python/ch-2.py | 21 | ||||
| -rw-r--r-- | challenge-001/paulo-custodio/test.pl | 43 |
14 files changed, 342 insertions, 17 deletions
diff --git a/challenge-001/paulo-custodio/basic/ch-1.bas b/challenge-001/paulo-custodio/basic/ch-1.bas new file mode 100644 index 0000000000..95c2d80e6e --- /dev/null +++ b/challenge-001/paulo-custodio/basic/ch-1.bas @@ -0,0 +1,33 @@ +' Challenge 001 +' +' Challenge #1 +' Write a script to replace the character ‘e’ with ‘E’ in the string +' ‘Perl Weekly Challenge’. Also print the number of times the character ‘e’ +' is found in the string. + +function replace_e(byref text as string) as integer + dim i as integer, count as integer + for i=1 to len(text) + if mid(text,i,1)="e" then + text = left(text, i-1) & "E" & mid(text, i+1) + count = count+1 + end if + next i + replace_e = count +end function + +function join_args() as string + dim i as integer, sep as string, result as string + sep = "" + i = 1 + do while command(i)<>"" + result = result & sep & command(i) + sep = " " + i = i+1 + loop + join_args = result +end function + +dim text as string +text = join_args() +print trim(str(replace_e(text)));" ";text diff --git a/challenge-001/paulo-custodio/basic/ch-2.bas b/challenge-001/paulo-custodio/basic/ch-2.bas new file mode 100644 index 0000000000..703b87d01c --- /dev/null +++ b/challenge-001/paulo-custodio/basic/ch-2.bas @@ -0,0 +1,21 @@ +' Challenge 001 +' +' Challenge #2 +' Write a one-liner to solve the FizzBuzz problem and print the numbers 1 +' through 20. However, any number divisible by 3 should be replaced by the word +' ‘fizz’ and any divisible by 5 by the word ‘buzz’. Those numbers that are both +' divisible by 3 and 5 become ‘fizzbuzz’. + +dim i as integer + +for i=1 to val(command(1)) + if i mod 15=0 then + print "fizzbuzz" + elseif i mod 3=0 then + print "fizz" + elseif i mod 5=0 then + print "buzz" + else + print trim(str(i)) + end if +next i diff --git a/challenge-001/paulo-custodio/c/ch-1.c b/challenge-001/paulo-custodio/c/ch-1.c new file mode 100644 index 0000000000..046506ea56 --- /dev/null +++ b/challenge-001/paulo-custodio/c/ch-1.c @@ -0,0 +1,46 @@ +/* +Challenge 001 + +Challenge #1 +Write a script to replace the character ‘e’ with ‘E’ in the string +‘Perl Weekly Challenge’. Also print the number of times the character ‘e’ +is found in the string. +*/ + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <memory.h> + +void* check_mem(void* p) { + if (!p) { + fputs("out of memory", stderr); + exit(EXIT_FAILURE); + } +} + +int replace_e(char* text) { + int count = 0; + for (int i = 0; i < strlen(text); i++) { + if (text[i] == 'e') { + text[i] = 'E'; + count++; + } + } + return count; +} + +int main(int argc, char* argv[]) { + char* text = check_mem(strdup("")); + for (int i = 1; i < argc; i++) { + text = check_mem(realloc(text, strlen(text)+strlen(argv[i])+2)); + strcat(text, argv[i]); + strcat(text, " "); + } + if (*text) + text[strlen(text)-1] = '\0'; // remove last space + + int count = replace_e(text); + printf("%d %s\n", count, text); + free(text); +} diff --git a/challenge-001/paulo-custodio/c/ch-2.c b/challenge-001/paulo-custodio/c/ch-2.c new file mode 100644 index 0000000000..24b292b5f2 --- /dev/null +++ b/challenge-001/paulo-custodio/c/ch-2.c @@ -0,0 +1,27 @@ +/* +Challenge 001 + +Challenge #1 +Write a script to replace the character ‘e’ with ‘E’ in the string +‘Perl Weekly Challenge’. Also print the number of times the character ‘e’ +is found in the string. +*/ + +#include <stdio.h> +#include <stdlib.h> + +int main(int argc, char* argv[]) { + if (argc == 2) { + int n = atoi(argv[1]); + for (int i = 1; i <= n; i++) { + if (i%15==0) + printf("fizzbuzz\n"); + else if (i%3==0) + printf("fizz\n"); + else if (i%5==0) + printf("buzz\n"); + else + printf("%d\n", i); + } + } +} diff --git a/challenge-001/paulo-custodio/cpp/ch-1.cpp b/challenge-001/paulo-custodio/cpp/ch-1.cpp new file mode 100644 index 0000000000..cda2637b4f --- /dev/null +++ b/challenge-001/paulo-custodio/cpp/ch-1.cpp @@ -0,0 +1,34 @@ +/* +Challenge 001 + +Challenge #1 +Write a script to replace the character ‘e’ with ‘E’ in the string +‘Perl Weekly Challenge’. Also print the number of times the character ‘e’ +is found in the string. +*/ + +#include <iostream> +#include <string> + +int replace_e(std::string& text) { + int count = 0; + for (auto& c : text) { + if (c == 'e') { + c = 'E'; + count++; + } + } + return count; +} + +int main(int argc, char* argv[]) { + std::string text; + for (int i = 1; i < argc; i++) { + text += argv[i]; + text += " "; + } + if (!text.empty()) + text.pop_back(); // remove last space + + std::cout << replace_e(text) << " " << text << std::endl; +} diff --git a/challenge-001/paulo-custodio/cpp/ch-2.cpp b/challenge-001/paulo-custodio/cpp/ch-2.cpp new file mode 100644 index 0000000000..3efaf01d61 --- /dev/null +++ b/challenge-001/paulo-custodio/cpp/ch-2.cpp @@ -0,0 +1,27 @@ +/* +Challenge 001 + +Challenge #1 +Write a script to replace the character ‘e’ with ‘E’ in the string +‘Perl Weekly Challenge’. Also print the number of times the character ‘e’ +is found in the string. +*/ + +#include <iostream> +#include <cstdlib> + +int main(int argc, char* argv[]) { + if (argc == 2) { + int n = atoi(argv[1]); + for (int i = 1; i <= n; i++) { + if (i%15==0) + std::cout << "fizzbuzz" << std::endl; + else if (i%3==0) + std::cout << "fizz" << std::endl; + else if (i%5==0) + std::cout << "buzz" << std::endl; + else + std::cout << i << std::endl; + } + } +} diff --git a/challenge-001/paulo-custodio/forth/ch-1.fs b/challenge-001/paulo-custodio/forth/ch-1.fs index b48413dfdb..6fe473d7d9 100644 --- a/challenge-001/paulo-custodio/forth/ch-1.fs +++ b/challenge-001/paulo-custodio/forth/ch-1.fs @@ -3,13 +3,36 @@ \ Challenge 001 \ \ Challenge #1 -\ Write a script to replace the character ‘e’ with ‘E’ in the string ‘Perl Weekly Challenge’. Also print the number of times the character ‘e’ is found in the string. +\ Write a script to replace the character ‘e’ with ‘E’ in the string +\ ‘Perl Weekly Challenge’. Also print the number of times the character ‘e’ +\ is found in the string. -CREATE message ," Perl Weekly Challenge" +\ init PAD as empty string +: clear_pad ( -- ) + 0 PAD C! +; -: replace-e ( cstr -- n ) - 0 SWAP ( n cstr ) \ n = number of chars replaced ) - COUNT BOUNDS ?DO +\ append string to PAD +: append_PAD ( str len -- ) + PAD COUNT + ( src len dst ) + ROT ( len dst src ) + SWAP ( len src dst ) + 2 PICK CMOVE ( len ) \ copy text + PAD C@ + PAD C! \ save new length +; + +\ append all input args +: append_args ( -- ) + clear_pad + BEGIN NEXT-ARG DUP 0> WHILE + append_pad s" " append_pad + REPEAT 2DROP + PAD C@ DUP 0> IF 1- PAD C! ELSE DROP THEN \ cut last space +; + +: replace-e ( str len -- n ) + 0 ROT ROT ( n str len ) \ n = number of chars replaced ) + BOUNDS ?DO I C@ 'e' = IF \ is an 'e' 'E' i C! \ replace by 'E' 1+ \ count it @@ -17,6 +40,8 @@ CREATE message ," Perl Weekly Challenge" LOOP ; -message replace-e . -message COUNT TYPE CR +\ main +append_args +PAD COUNT replace-e . +PAD COUNT TYPE CR BYE diff --git a/challenge-001/paulo-custodio/lua/ch-1.lua b/challenge-001/paulo-custodio/lua/ch-1.lua new file mode 100644 index 0000000000..693a03bb83 --- /dev/null +++ b/challenge-001/paulo-custodio/lua/ch-1.lua @@ -0,0 +1,20 @@ +#!/usr/bin/env lua + +--[[ +Challenge 001 + +Challenge #1 +Write a script to replace the character ‘e’ with ‘E’ in the string +‘Perl Weekly Challenge’. Also print the number of times the character ‘e’ +is found in the string. +--]] + +count = 0 +words = {} +for i=1, #arg do + word, n = string.gsub(arg[i], "e", "E"); + table.insert(words, word) + count = count+n +end + +io.write(count, " ", table.concat(words, " "), "\n") diff --git a/challenge-001/paulo-custodio/lua/ch-2.lua b/challenge-001/paulo-custodio/lua/ch-2.lua new file mode 100644 index 0000000000..9df96e1fb1 --- /dev/null +++ b/challenge-001/paulo-custodio/lua/ch-2.lua @@ -0,0 +1,19 @@ +#!/usr/bin/env lua + +--[[ +Challenge 001 + +# Challenge #2 +# Write a one-liner to solve the FizzBuzz problem and print the numbers 1 +# through 20. However, any number divisible by 3 should be replaced by the word +# ‘fizz’ and any divisible by 5 by the word ‘buzz’. Those numbers that are both +# divisible by 3 and 5 become ‘fizzbuzz’. +--]] + +for i=1,arg[1] do + if i%15==0 then io.write("fizzbuzz\n") + elseif i% 3==0 then io.write("fizz\n") + elseif i% 5==0 then io.write("buzz\n") + else io.write(i, "\n") + end +end diff --git a/challenge-001/paulo-custodio/perl/ch-1.pl b/challenge-001/paulo-custodio/perl/ch-1.pl index edfc497225..f31aa40a83 100644 --- a/challenge-001/paulo-custodio/perl/ch-1.pl +++ b/challenge-001/paulo-custodio/perl/ch-1.pl @@ -3,11 +3,13 @@ # Challenge 001 # # Challenge #1 -# Write a script to replace the character ‘e’ with ‘E’ in the string ‘Perl Weekly Challenge’. Also print the number of times the character ‘e’ is found in the string. +# Write a script to replace the character ‘e’ with ‘E’ in the string +# ‘Perl Weekly Challenge’. Also print the number of times the character ‘e’ +# is found in the string. use strict; use warnings; use 5.030; -my $s = "Perl Weekly Challenge"; +my $s = @ARGV ? "@ARGV" : "Perl Weekly Challenge"; say(($s =~ tr/e/E/), " ", $s); diff --git a/challenge-001/paulo-custodio/perl/ch-2.pl b/challenge-001/paulo-custodio/perl/ch-2.pl index c64625432b..6c70c579b5 100644 --- a/challenge-001/paulo-custodio/perl/ch-2.pl +++ b/challenge-001/paulo-custodio/perl/ch-2.pl @@ -3,10 +3,16 @@ # Challenge 001 # # Challenge #2 -# Write a one-liner to solve the FizzBuzz problem and print the numbers 1 through 20. However, any number divisible by 3 should be replaced by the word ‘fizz’ and any divisible by 5 by the word ‘buzz’. Those numbers that are both divisible by 3 and 5 become ‘fizzbuzz’. +# Write a one-liner to solve the FizzBuzz problem and print the numbers 1 +# through 20. However, any number divisible by 3 should be replaced by the word +# ‘fizz’ and any divisible by 5 by the word ‘buzz’. Those numbers that are both +# divisible by 3 and 5 become ‘fizzbuzz’. use strict; use warnings; use 5.030; -for (1..20) { say(($_%15)==0 ? "fizzbuzz" : ($_%3)==0 ? "fizz" : ($_%5)==0 ? "buzz" : $_) } +my $n = shift || 20; +for (1 .. $n) { + say(($_%15)==0 ? "fizzbuzz" : ($_%3)==0 ? "fizz" : ($_%5)==0 ? "buzz" : $_); +} diff --git a/challenge-001/paulo-custodio/python/ch-1.py b/challenge-001/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..fe51b3e1a3 --- /dev/null +++ b/challenge-001/paulo-custodio/python/ch-1.py @@ -0,0 +1,13 @@ +#!/usr/bin/env python + +# Challenge 001 +# +# Challenge #1 +# Write a script to replace the character ‘e’ with ‘E’ in the string +# ‘Perl Weekly Challenge’. Also print the number of times the character ‘e’ +# is found in the string. + +import sys + +str = " ".join(sys.argv[1:]) +print(str.count('e'), str.replace('e', 'E')) diff --git a/challenge-001/paulo-custodio/python/ch-2.py b/challenge-001/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..394592f1dd --- /dev/null +++ b/challenge-001/paulo-custodio/python/ch-2.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python + +# Challenge 001 +# +# Challenge #1 +# Write a one-liner to solve the FizzBuzz problem and print the numbers 1 +# through 20. However, any number divisible by 3 should be replaced by the word +# ‘fizz’ and any divisible by 5 by the word ‘buzz’. Those numbers that are both +# divisible by 3 and 5 become ‘fizzbuzz’. + +import sys + +for i in range(1, int(sys.argv[1])+1): + if i%15==0: + print("fizzbuzz") + elif i%3==0: + print("fizz") + elif i%5==0: + print("buzz") + else: + print(i) diff --git a/challenge-001/paulo-custodio/test.pl b/challenge-001/paulo-custodio/test.pl index 633176940e..6aab7f4206 100644 --- a/challenge-001/paulo-custodio/test.pl +++ b/challenge-001/paulo-custodio/test.pl @@ -5,10 +5,30 @@ use warnings; use Test::More; use 5.030; -is capture("perl perl/ch-1.pl"), "5 PErl WEEkly ChallEngE\n"; -is capture("gforth forth/ch-1.fs"), "5 PErl WEEkly ChallEngE\n"; +my $LUA = ($^O eq 'msys') ? "lua.exe" : "lua"; -my $expected = <<END; +run("gcc c/ch-1.c -o c/ch-1"); +run("g++ cpp/ch-1.cpp -o cpp/ch-1"); +run("fbc basic/ch-1.bas -o basic/ch-1"); + +for (["Perl Weekly Challenge" => "5 PErl WEEkly ChallEngE"], + ["Champion" => "0 Champion"]) { + my($in, $out) = @$_; + + is capture( "$LUA lua/ch-1.lua $in"), "$out\n"; + is capture( "perl perl/ch-1.pl $in"), "$out\n"; + is capture( "gforth forth/ch-1.fs $in"), "$out\n"; + is capture("python python/ch-1.py $in"), "$out\n"; + is capture( "c/ch-1 $in"), "$out\n"; + is capture( "cpp/ch-1 $in"), "$out\n"; + is capture( "basic/ch-1 $in"), "$out\n"; +} + +run("gcc c/ch-2.c -o c/ch-2"); +run("g++ cpp/ch-2.cpp -o cpp/ch-2"); +run("fbc basic/ch-2.bas -o basic/ch-2"); + +for ([20 => <<END]) { 1 2 fizz @@ -30,10 +50,16 @@ fizz 19 buzz END + my($in, $out) = @$_; -is capture("perl perl/ch-2.pl"), $expected; -is capture("gforth forth/ch-2.fs"), $expected; - + is capture( "$LUA lua/ch-2.lua $in"), $out; + is capture( "perl perl/ch-2.pl $in"), $out; + is capture( "gforth forth/ch-2.fs $in"), $out; + is capture("python python/ch-2.py $in"), $out; + is capture( "c/ch-2 $in"), $out; + is capture( "cpp/ch-2 $in"), $out; + is capture( "basic/ch-2 $in"), $out; +} done_testing; @@ -43,3 +69,8 @@ sub capture { $out =~ s/[ \t\v\f\r]*\n/\n/g; return $out; } + +sub run { + my($cmd) = @_; + ok 0==system($cmd), $cmd; +} |
