aboutsummaryrefslogtreecommitdiff
path: root/challenge-029
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-12-21 17:50:19 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2021-12-21 17:50:19 +0000
commit77460c019eebb24f018f2e8ed1e25ec1c56e2486 (patch)
treee1f77df33eaf94c200bd6881bb7e35977f026369 /challenge-029
parent82300f89acda50a186fa56a9e737cb3edf9a1030 (diff)
downloadperlweeklychallenge-club-77460c019eebb24f018f2e8ed1e25ec1c56e2486.tar.gz
perlweeklychallenge-club-77460c019eebb24f018f2e8ed1e25ec1c56e2486.tar.bz2
perlweeklychallenge-club-77460c019eebb24f018f2e8ed1e25ec1c56e2486.zip
Add Python solution to challenge 029
Modify example C code to use other that int parameters and results
Diffstat (limited to 'challenge-029')
-rw-r--r--challenge-029/paulo-custodio/Makefile14
-rw-r--r--challenge-029/paulo-custodio/cmult.c3
-rw-r--r--challenge-029/paulo-custodio/perl/ch-2.pl10
-rw-r--r--challenge-029/paulo-custodio/python/ch-1.py30
-rw-r--r--challenge-029/paulo-custodio/python/ch-2.py21
-rw-r--r--challenge-029/paulo-custodio/t/test-2.yaml4
6 files changed, 73 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