diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-03-11 22:56:04 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-03-11 22:56:04 +0000 |
| commit | be0102cdd35ea1f97407f7e935e7702b9f0c02da (patch) | |
| tree | e785ad15dca22d40e44e8e395972e845b7faa3f5 | |
| parent | 52e26bb56a847fb01d666566d83b2fbf8c7ec2af (diff) | |
| parent | 8f489cb53f18d5023bf4f8e6a0115fe2ac1d580a (diff) | |
| download | perlweeklychallenge-club-be0102cdd35ea1f97407f7e935e7702b9f0c02da.tar.gz perlweeklychallenge-club-be0102cdd35ea1f97407f7e935e7702b9f0c02da.tar.bz2 perlweeklychallenge-club-be0102cdd35ea1f97407f7e935e7702b9f0c02da.zip | |
Merge pull request #3705 from pauloscustodio/paulo-custodio
Add Lua solution to challenge 093
| -rw-r--r-- | challenge-001/paulo-custodio/test.pl | 1 | ||||
| -rw-r--r-- | challenge-093/paulo-custodio/lua/ch-1.lua | 80 | ||||
| -rw-r--r-- | challenge-093/paulo-custodio/lua/ch-2.lua | 96 | ||||
| -rw-r--r-- | challenge-093/paulo-custodio/python/ch-1.py | 3 | ||||
| -rw-r--r-- | challenge-093/paulo-custodio/t/test-1.yaml | 10 | ||||
| -rw-r--r-- | challenge-093/paulo-custodio/t/test-2.yaml | 20 | ||||
| -rw-r--r-- | challenge-093/paulo-custodio/test.pl | 66 |
7 files changed, 210 insertions, 66 deletions
diff --git a/challenge-001/paulo-custodio/test.pl b/challenge-001/paulo-custodio/test.pl index ba7e6ee46f..66416d7421 100644 --- a/challenge-001/paulo-custodio/test.pl +++ b/challenge-001/paulo-custodio/test.pl @@ -153,6 +153,7 @@ sub run { sub value_or_eval { my($str) = @_; $str //= ""; + $str =~ s/^\|//gm; my $value = ($str =~ /^eval\b/) ? eval($str) : $str; $@ and die "eval '$str' failed: $@"; return $value; diff --git a/challenge-093/paulo-custodio/lua/ch-1.lua b/challenge-093/paulo-custodio/lua/ch-1.lua new file mode 100644 index 0000000000..b55104f074 --- /dev/null +++ b/challenge-093/paulo-custodio/lua/ch-1.lua @@ -0,0 +1,80 @@ +#!/usr/bin/env lua + +--[[ +Challenge 093 + +TASK #1 > Max Points +Submitted by: Mohammad S Anwar +You are given set of co-ordinates @N. + +Write a script to count maximum points on a straight line when given +co-ordinates plotted on 2-d plane. + +Example 1: +| +| x +| x +| x ++ _ _ _ _ + +Input: (1,1), (2,2), (3,3) +Output: 3 +Example 2: +| +| +| x x +| x +| x x ++ _ _ _ _ _ + +Input: (1,1), (2,2), (3,1), (1,3), (5,3) +Output: 3 +--]] + +function get_points() + if #arg < 6 or math.fmod(#arg, 2) ~= 0 then + io.write("Usage: ch-1.lua x y x y x y ...\n") + os.exit(1) + end + local P = {} + for i=1, #arg, 2 do + local x = tonumber(arg[i]) + local y = tonumber(arg[i+1]) + table.insert(P, {x,y}) + end + return P +end + +function in_line(pi, pj, pk) + -- compute cross product + dxc = pk[1] - pi[1]; + dyc = pk[2] - pi[2]; + + dxl = pj[1] - pi[1]; + dyl = pj[2] - pi[2]; + + cross = dxc * dyl - dyc * dxl; + + return cross == 0 and true or false +end + +function max_in_line(P) + local max_count = 0 + -- for each pair + for i=1, #P-1 do + for j=i+1, #P do + -- check other points against pair + local count = 0 + for k=1, #P do + if in_line(P[i], P[j], P[k]) then + count = count + 1 + end + end + max_count = math.max(max_count, count) + end + end + + return max_count +end + +io.write(max_in_line(get_points()), "\n") diff --git a/challenge-093/paulo-custodio/lua/ch-2.lua b/challenge-093/paulo-custodio/lua/ch-2.lua new file mode 100644 index 0000000000..047f1462eb --- /dev/null +++ b/challenge-093/paulo-custodio/lua/ch-2.lua @@ -0,0 +1,96 @@ +#!/usr/bin/env lua + +--[[ +Challenge 093 + +TASK #2 > Sum Path +Submitted by: Mohammad S Anwar +You are given binary tree containing numbers 0-9 only. + +Write a script to sum all possible paths from root to leaf. + +Example 1: +Input: + 1 + / + 2 + / \ + 3 4 + +Output: 13 +as sum two paths (1->2->3) and (1->2->4) +Example 2: +Input: + 1 + / \ + 2 3 + / / \ + 4 5 6 + +Output: 26 +as sum three paths (1->2->4), (1->3->5) and (1->3->6) +--]] + + +-- read standard input into array +function read_input() + local lines = {} + while true do + local line = io.read() + if line == nil then break end + lines[#lines + 1] = line + end + return lines +end + +-- get a character from lines, blank if out of bounds +function ch(lines, row, col) + if row<1 or row>#lines then return ' ' end + if col<1 or col>#(lines[row]) then return ' ' end + return string.sub(lines[row], col, col) +end + +-- parse a subtree at row,col +function parse_subtree(lines, row, col) + local tree = {} + tree.value = tonumber(ch(lines,row,col)) + + if ch(lines, row + 1, col - 1) == '/' then + tree.left = parse_subtree(lines, row + 2, col - 2) + end + + if ch(lines, row + 1, col + 1) == '\\' then + tree.right = parse_subtree(lines, row + 2, col + 2) + end + + return tree +end + +-- parse a tree, return root Node +function parse_tree(lines) + local col = string.find(lines[1], "%d") + return parse_subtree(lines, 1, col) +end + +-- return the subtree path len +function add_subtree_paths(node, cur_len, total_len) + cur_len = cur_len + node.value + if node.left then + total_len = add_subtree_paths(node.left, cur_len, total_len) + end + if node.right then + total_len = add_subtree_paths(node.right, cur_len, total_len) + end + if not node.left and not node.right then + total_len = total_len + cur_len + end + + return total_len +end + +-- sum all sub-tree paths +function add_tree_paths(tree) + return add_subtree_paths(tree, 0, 0) +end + +io.write(add_tree_paths(parse_tree(read_input())), "\n") diff --git a/challenge-093/paulo-custodio/python/ch-1.py b/challenge-093/paulo-custodio/python/ch-1.py index 7fb1a79e87..72ad9f00f7 100644 --- a/challenge-093/paulo-custodio/python/ch-1.py +++ b/challenge-093/paulo-custodio/python/ch-1.py @@ -6,7 +6,8 @@ # Submitted by: Mohammad S Anwar # You are given set of co-ordinates @N. # -# Write a script to count maximum points on a straight line when given co-ordinates plotted on 2-d plane. +# Write a script to count maximum points on a straight line when given +# co-ordinates plotted on 2-d plane. # # Example 1: # | diff --git a/challenge-093/paulo-custodio/t/test-1.yaml b/challenge-093/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..6631be58f7 --- /dev/null +++ b/challenge-093/paulo-custodio/t/test-1.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: 1 1 2 2 3 4 + input: + output: 2 +- setup: + cleanup: + args: 1 1 2 2 3 1 1 3 5 3 + input: + output: 3 diff --git a/challenge-093/paulo-custodio/t/test-2.yaml b/challenge-093/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..fdfe423b93 --- /dev/null +++ b/challenge-093/paulo-custodio/t/test-2.yaml @@ -0,0 +1,20 @@ +- setup: + cleanup: + args: + input: | + | 1 + | / + | 2 + | / \ + | 3 4 + output: 13 +- setup: + cleanup: + args: + input: | + | 1 + | / \ + | 2 3 + | / / \ + | 4 5 6 + output: 26 diff --git a/challenge-093/paulo-custodio/test.pl b/challenge-093/paulo-custodio/test.pl index cfa63724eb..01ed2b83cd 100644 --- a/challenge-093/paulo-custodio/test.pl +++ b/challenge-093/paulo-custodio/test.pl @@ -3,69 +3,5 @@ use strict; use warnings; use 5.030; -use Test::More; -use Path::Tiny; -my $input = "input.txt"; - -compile("gcc -o c/ch-1 c/ch-1.c"); -compile("g++ -o cpp/ch-1 cpp/ch-1.cpp"); -compile("fbc basic/ch-1.bas"); - -for (["1 1 2 2 3 4" => 2], - ["1 1 2 2 3 1 1 3 5 3" => 3]) { - my($in, $out) = @$_; - - is capture( "perl perl/ch-1.pl $in"), "$out\n"; - is capture("python python/ch-1.py $in"), "$out\n"; - is capture( "gforth forth/ch-1.fs $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"; -} - - -compile("gcc -o c/ch-2 c/ch-2.c"); -compile("g++ -o cpp/ch-2 cpp/ch-2.cpp"); -compile("fbc basic/ch-2.bas"); - -for ([<<'END' => 13], - 1 - / - 2 - / \ - 3 4 -END - [<<'END' => 26]) { - 1 - / \ - 2 3 - / / \ - 4 5 6 -END - my($in, $out) = @$_; - path($input)->spew($in); - - is capture( "perl perl/ch-2.pl < $input"), "$out\n"; - is capture("python python/ch-2.py < $input"), "$out\n"; - is capture( "gforth forth/ch-2.fs < $input"), "$out\n"; - is capture( "c/ch-2 < $input"), "$out\n"; - is capture( "cpp/ch-2 < $input"), "$out\n"; - is capture( "basic/ch-2 < $input"), " $out\n"; -} - - -unlink $input; -done_testing; - -sub capture { - my($cmd) = @_; - my $out = `$cmd`; - $out =~ s/[ \t\v\f\r]*\n/\n/g; - return $out; -} - -sub compile { - my($cmd) = @_; - 0==system($cmd) or die "Compile failed: $cmd\n"; -} +require '../../challenge-001/paulo-custodio/test.pl'; |
