From c70cb4c675ee9c1ad8076ef6cab385112e3eea47 Mon Sep 17 00:00:00 2001 From: Noud Aldenhoven Date: Tue, 8 Oct 2019 20:12:28 +0200 Subject: Solutions to challenge 29 problems 1 and 2 in Perl 6 by Noud --- challenge-029/noud/perl6/ch1.p6 | 18 ++++++++++++++++++ challenge-029/noud/perl6/ch2.p6 | 18 ++++++++++++++++++ challenge-029/noud/perl6/make.sh | 1 + challenge-029/noud/perl6/rot13.c | 24 ++++++++++++++++++++++++ 4 files changed, 61 insertions(+) create mode 100644 challenge-029/noud/perl6/ch1.p6 create mode 100644 challenge-029/noud/perl6/ch2.p6 create mode 100755 challenge-029/noud/perl6/make.sh create mode 100644 challenge-029/noud/perl6/rot13.c diff --git a/challenge-029/noud/perl6/ch1.p6 b/challenge-029/noud/perl6/ch1.p6 new file mode 100644 index 0000000000..f0ef03667a --- /dev/null +++ b/challenge-029/noud/perl6/ch1.p6 @@ -0,0 +1,18 @@ +# Write a script to demonstrate brace expansion. For example, script would take +# command line argument Perl {Daily,Weekly,Monthly,Yearly} Challenge and should +# expand it and print like below: +# +# Perl Daily Challenge +# Perl Weekly Challenge +# Perl Monthly Challenge +# Perl Yearly Challenge + +sub brace_expansion(Str $s) { + if ($s ~~ /(.*)\{(.*)\}(.*)/) { + ["$_[0]$_[1]$2" for brace_expansion(Str($0)) X $1.split(',')]; + } else { + [$s]; + } +} + +for brace_expansion('Perl {Daily,Weekly,Monthly,Yearly} Challenge 20{18,19,20}.') { .say; } diff --git a/challenge-029/noud/perl6/ch2.p6 b/challenge-029/noud/perl6/ch2.p6 new file mode 100644 index 0000000000..9e2e762cfe --- /dev/null +++ b/challenge-029/noud/perl6/ch2.p6 @@ -0,0 +1,18 @@ +# Write a script to demonstrate calling a C function. It could be any user +# defined or standard C function. + +# I choose to call my self made ROT13 encryption C function in Perl 6. ROT13 +# is a letter subsitution cipher that replaces a letter with the 13th letter +# after it in the alphabet. +# +# To create the shared ROT13 library run: ./make.sh + +use NativeCall; + +sub rot13(Str) returns Str is native('./rot13.so') {*}; + +my $enc = rot13("hello perl weekly challenge!"); +my $dec = rot13($enc); + +say "ROT13 encryption: $enc"; +say "ROT13 decryption: $dec"; diff --git a/challenge-029/noud/perl6/make.sh b/challenge-029/noud/perl6/make.sh new file mode 100755 index 0000000000..787338ef60 --- /dev/null +++ b/challenge-029/noud/perl6/make.sh @@ -0,0 +1 @@ +gcc -shared -o rot13.so rot13.c diff --git a/challenge-029/noud/perl6/rot13.c b/challenge-029/noud/perl6/rot13.c new file mode 100644 index 0000000000..d66b13fb8a --- /dev/null +++ b/challenge-029/noud/perl6/rot13.c @@ -0,0 +1,24 @@ +#include +#include +#include + +char *rot13(char *s) { + const size_t s_len = strlen(s); + char *result = malloc((s_len + 1) * sizeof(char)); + + for (size_t i = 0; i < s_len; i++) { + if (isalpha(s[i])) { + if ((tolower(s[i]) - 'a') < 13) { + result[i] = s[i] + 13; + } else { + result[i] = s[i] - 13; + } + } else { + result[i] = s[i]; + } + } + + result[s_len] = '\0'; + + return result; +} -- cgit