diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-12-21 21:21:55 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-12-21 21:21:55 +0000 |
| commit | 5f1b14f04d412ab7a7f0748821d4185bf3972bac (patch) | |
| tree | 25ab1f3a637bdfac9c3a421cfa7a75ca15af4387 | |
| parent | 52b76b289e7244237cd28bb3e271cb783a3c6dad (diff) | |
| parent | 70596c9ffd7af483e5346bf70e33ae11b4829643 (diff) | |
| download | perlweeklychallenge-club-5f1b14f04d412ab7a7f0748821d4185bf3972bac.tar.gz perlweeklychallenge-club-5f1b14f04d412ab7a7f0748821d4185bf3972bac.tar.bz2 perlweeklychallenge-club-5f1b14f04d412ab7a7f0748821d4185bf3972bac.zip | |
Merge pull request #5401 from pauloscustodio/devel
Add Python solution to challenge 029
| -rw-r--r-- | challenge-029/paulo-custodio/Makefile | 14 | ||||
| -rw-r--r-- | challenge-029/paulo-custodio/cmult.c | 3 | ||||
| -rw-r--r-- | challenge-029/paulo-custodio/perl/ch-2.pl | 10 | ||||
| -rw-r--r-- | challenge-029/paulo-custodio/python/ch-1.py | 30 | ||||
| -rw-r--r-- | challenge-029/paulo-custodio/python/ch-2.py | 21 | ||||
| -rw-r--r-- | challenge-029/paulo-custodio/t/test-2.yaml | 4 | ||||
| -rw-r--r-- | challenge-030/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-030/paulo-custodio/README | 1 | ||||
| -rw-r--r-- | challenge-030/paulo-custodio/perl/ch-1.pl | 18 | ||||
| -rw-r--r-- | challenge-030/paulo-custodio/perl/ch-2.pl | 26 | ||||
| -rw-r--r-- | challenge-030/paulo-custodio/python/ch-1.py | 16 | ||||
| -rw-r--r-- | challenge-030/paulo-custodio/python/ch-2.py | 19 | ||||
| -rw-r--r-- | challenge-030/paulo-custodio/t/test-1.yaml | 5 | ||||
| -rw-r--r-- | challenge-030/paulo-custodio/t/test-2.yaml | 12 |
14 files changed, 172 insertions, 9 deletions
diff --git a/challenge-029/paulo-custodio/Makefile b/challenge-029/paulo-custodio/Makefile index c3c762d746..2b745f7bea 100644 --- a/challenge-029/paulo-custodio/Makefile +++ b/challenge-029/paulo-custodio/Makefile @@ -1,2 +1,14 @@ -all: +all: test + +test: libcmult.so perl ../../challenge-001/paulo-custodio/test.pl + +cmult.o: cmult.c + gcc -c -fpic cmult.c + +libcmult.so: cmult.o + gcc -shared -o $@ cmult.o + +clean: + $(RM) cmult.o libcmult.so *~ + $(RM) -rf _Inline diff --git a/challenge-029/paulo-custodio/cmult.c b/challenge-029/paulo-custodio/cmult.c new file mode 100644 index 0000000000..8306d9597c --- /dev/null +++ b/challenge-029/paulo-custodio/cmult.c @@ -0,0 +1,3 @@ +float cmult(int a, float b) { + return (float)a * b; +} diff --git a/challenge-029/paulo-custodio/perl/ch-2.pl b/challenge-029/paulo-custodio/perl/ch-2.pl index c33fa95943..e444dcfaf6 100644 --- a/challenge-029/paulo-custodio/perl/ch-2.pl +++ b/challenge-029/paulo-custodio/perl/ch-2.pl @@ -7,10 +7,8 @@ # defined or standard C function. use Modern::Perl; -use Inline C => <<'END'; - int sum(int a, int b) { - return a+b; - } -END +use Path::Tiny; -say sum(@ARGV); +use Inline C => path("cmult.c")->slurp(); + +say sprintf("%.2f", cmult(@ARGV)); diff --git a/challenge-029/paulo-custodio/python/ch-1.py b/challenge-029/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..11e598d80c --- /dev/null +++ b/challenge-029/paulo-custodio/python/ch-1.py @@ -0,0 +1,30 @@ +#!/usr/bin/python3 + +# Challenge 029 + +# Task #1 +# 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 + +import sys +import re + +def print_expanded(text): + mo = re.search(r"[{]([^{}]*?)[}]", text) + if mo: + before = text[:mo.start(0)] + expand = mo.group(1) + after = text[mo.end(0):] + + for arg in expand.split(","): + print_expanded(before+arg+after) + else: + print(text) + +print_expanded(" ".join(sys.argv[1:])) diff --git a/challenge-029/paulo-custodio/python/ch-2.py b/challenge-029/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..e758df9016 --- /dev/null +++ b/challenge-029/paulo-custodio/python/ch-2.py @@ -0,0 +1,21 @@ +#!/usr/bin/python3 + +# Challenge 029 + +# Task #2 +# Write a script to demonstrate calling a C function. It could be any user +# defined or standard C function. + +import sys +import ctypes +import pathlib + +# Load the shared library into ctypes +libname = pathlib.Path().absolute() / "libcmult.so" +c_lib = ctypes.CDLL(libname) +c_lib.cmult.restype = ctypes.c_float + +x = int(sys.argv[1]) +y = float(sys.argv[2]) +result = c_lib.cmult(x, ctypes.c_float(y)) +print(f"{result:.2f}") diff --git a/challenge-029/paulo-custodio/t/test-2.yaml b/challenge-029/paulo-custodio/t/test-2.yaml index 45d285dccc..db5057e526 100644 --- a/challenge-029/paulo-custodio/t/test-2.yaml +++ b/challenge-029/paulo-custodio/t/test-2.yaml @@ -1,5 +1,5 @@ - setup: cleanup: - args: 2 3 + args: 3 3.14 input: - output: 5 + output: 9.42 diff --git a/challenge-030/paulo-custodio/Makefile b/challenge-030/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-030/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-030/paulo-custodio/README b/challenge-030/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-030/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-030/paulo-custodio/perl/ch-1.pl b/challenge-030/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..7160d8840b --- /dev/null +++ b/challenge-030/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,18 @@ +#!/usr/bin/perl + +# Challenge 030 +# +# Task #1 +# Write a script to list dates for Sunday Christmas between 2019 and 2100. For +# example, 25 Dec 2022 is Sunday. + +use Modern::Perl; +use DateTime; + +my @sunday_xmas; +for my $year (2019..2100) { + my $date = DateTime->new(year=>$year, month=>12, day=>25); + push @sunday_xmas, $year if $date->day_of_week == 7; +} + +say join(", ", @sunday_xmas); diff --git a/challenge-030/paulo-custodio/perl/ch-2.pl b/challenge-030/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..974dca199a --- /dev/null +++ b/challenge-030/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,26 @@ +#!/usr/bin/perl + +# Challenge 030 +# +# Task #2 +# Write a script to print all possible series of 3 positive numbers, where in +# each series at least one of the number is even and sum of the three numbers +# is always 12. For example, 3,4,5. + +use Modern::Perl; +use List::Util 'sum'; +use List::MoreUtils 'any'; + +my $sum = shift||12; + +for my $i (1..$sum) { + for my $j ($i+1..$sum) { + for my $k ($j+1..$sum) { + if (sum($i, $j, $k) == $sum) { + if (any {$_%2==0} $i, $j, $k) { + say "$i,$j,$k" + } + } + } + } +} diff --git a/challenge-030/paulo-custodio/python/ch-1.py b/challenge-030/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..4f15e8d7b5 --- /dev/null +++ b/challenge-030/paulo-custodio/python/ch-1.py @@ -0,0 +1,16 @@ +#!/usr/bin/python3 + +# Challenge 030 +# +# Task #1 +# Write a script to list dates for Sunday Christmas between 2019 and 2100. For +# example, 25 Dec 2022 is Sunday. + +import datetime + +sunday_xmas = [] +for year in range(2019, 2101): + dt = datetime.date(year, 12, 25) + if dt.isoweekday()==7: + sunday_xmas.append(year) +print(*sunday_xmas, sep=", ") diff --git a/challenge-030/paulo-custodio/python/ch-2.py b/challenge-030/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..29da0ffd41 --- /dev/null +++ b/challenge-030/paulo-custodio/python/ch-2.py @@ -0,0 +1,19 @@ +#!/usr/bin/python3 + +# Challenge 030 +# +# Task #2 +# Write a script to print all possible series of 3 positive numbers, where in +# each series at least one of the number is even and sum of the three numbers +# is always 12. For example, 3,4,5. + +import sys + +S = int(sys.argv[1]) + +for i in range(1, S+1): + for j in range(i+1, S+1): + for k in range(j+1, S+1): + if sum([i, j, k]) == S: + if any([x%2==0 for x in [i, j, k]]): + print(f"{i},{j},{k}") diff --git a/challenge-030/paulo-custodio/t/test-1.yaml b/challenge-030/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..679021db33 --- /dev/null +++ b/challenge-030/paulo-custodio/t/test-1.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: + input: + output: 2022, 2033, 2039, 2044, 2050, 2061, 2067, 2072, 2078, 2089, 2095 diff --git a/challenge-030/paulo-custodio/t/test-2.yaml b/challenge-030/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..b0782d35ac --- /dev/null +++ b/challenge-030/paulo-custodio/t/test-2.yaml @@ -0,0 +1,12 @@ +- setup: + cleanup: + args: 12 + input: + output: | + 1,2,9 + 1,3,8 + 1,4,7 + 1,5,6 + 2,3,7 + 2,4,6 + 3,4,5 |
