diff options
| author | Ian Goodnight <goodnight.ian@gmail.com> | 2022-01-02 18:53:45 -0500 |
|---|---|---|
| committer | Ian Goodnight <goodnight.ian@gmail.com> | 2022-01-02 18:53:45 -0500 |
| commit | 743b6f1f2a5ae0edb0d3c31a41eb9ef1787b20c0 (patch) | |
| tree | 958e94ad9c16fb732b09342bec0d5e22b7551e81 /challenge-145/iangoodnight/ruby | |
| parent | b3bd78828c9c0a169173683255a1bf92b6f3c0e8 (diff) | |
| download | perlweeklychallenge-club-743b6f1f2a5ae0edb0d3c31a41eb9ef1787b20c0.tar.gz perlweeklychallenge-club-743b6f1f2a5ae0edb0d3c31a41eb9ef1787b20c0.tar.bz2 perlweeklychallenge-club-743b6f1f2a5ae0edb0d3c31a41eb9ef1787b20c0.zip | |
week 145
Diffstat (limited to 'challenge-145/iangoodnight/ruby')
| -rwxr-xr-x | challenge-145/iangoodnight/ruby/ch-1.rb | 81 | ||||
| -rwxr-xr-x | challenge-145/iangoodnight/ruby/ch-2.rb | 125 |
2 files changed, 206 insertions, 0 deletions
diff --git a/challenge-145/iangoodnight/ruby/ch-1.rb b/challenge-145/iangoodnight/ruby/ch-1.rb new file mode 100755 index 0000000000..59b9777a5f --- /dev/null +++ b/challenge-145/iangoodnight/ruby/ch-1.rb @@ -0,0 +1,81 @@ +#!/usr/bin/ruby -w +## ch-1.rb + +# > https://theweeklychallenge.org/blog/perl-weekly-challenge-145/ +# +# ## Task 1 > Dot Product +# ======================= +# +# You are given 2 arrays of the same size, `@a` and `@b`. +# +# Write a script to implement `Dot Product`. +# +# **Example:** +# ``` +# @a = (1, 2, 3); +# @b = (4, 5, 6); +# +# $dot_product = (1 * 4) + (2 * 5) + (3 * 6) => 4 + 10 + 18 => 32 +# ``` + +################################################################################ +# PWC Solution ################################################################# +################################################################################ + +def dot_product(arr1, arr2) + sum = 0 + arr1.each_with_index do |multiplicand, idx| + sum += multiplicand.to_f * arr2[idx].to_f + end + (sum % 1).zero? ? sum.to_i : sum +end + +################################################################################ +# Utilities #################################################################### +################################################################################ + +def parse_test_case(file_path) + lines = File.read(file_path).split("\n").reject { |line| line.match(/^\s*#/) } + arr1, arr2, test = lines + a = arr1.split(/\s*,\s*/) + b = arr2.split(/\s*,\s*/) + [a, b, test] +end + +def test_solution(callback, *args, test) + result = callback.call(*args) + if test.to_f == result.to_f + puts "\e[32mPassed \u2690\e[0m" + else + puts "\e[31mFailed \u2715\e[0m" + end +end + +def print_params(target, a, b, test) + puts "#{target}:" + puts "@a = #{a.join(', ')}" + puts "@b = #{b.join(', ')}" + puts "Dot Product: #{test}" +end + +def print_and_run(target) + a, b, test = parse_test_case(target) + print_params(target, a, b, test) + test_solution(method(:dot_product), a, b, test) + puts "\n" +end + +def run_test_suite(dir) + tests = Dir.entries(dir).select { |f| File.file? File.join(dir, f) } + tests.sort.each do |test| + print_and_run(File.join(dir, test)) + end +end + +################################################################################ +# Main ######################################################################### +################################################################################ + +target = ARGV[0] || '../test_cases/ch-1' +print_and_run(target) if File.file?(target) +run_test_suite(target) if File.directory?(target) diff --git a/challenge-145/iangoodnight/ruby/ch-2.rb b/challenge-145/iangoodnight/ruby/ch-2.rb new file mode 100755 index 0000000000..a41ea3ccc8 --- /dev/null +++ b/challenge-145/iangoodnight/ruby/ch-2.rb @@ -0,0 +1,125 @@ +#!/usr/bin/ruby -w +# ch-2.rb + +# > https://theweeklychallenge.org/blog/perl-weekly-challenge-145/ +# +# ## Task2 > Palindromic Tree +# =========================== +# +# You are given a string `$s`. +# +# Write a script to create a `Palindromic Tree` for the given string +# +# I found this [blog] explaining `Palindromic Tree` in detail. +# +# **Example 1:** +# +# ``` +# Input: $s = 'redivider' +# Output: r redivider e edivide d divid i ivi v +# ``` +# +# **Example 2:** +# +# ``` +# Input: $s = 'deific' +# Output: d e i ifi f c +# ``` +# +# **Example 3:** +# +# ``` +# Input: $s = 'rotors' +# Output: r rotor o oto t s +# ``` +# +# **Example 4:** +# +# ``` +# Input: $s = 'challenge' +# Output: c h a l ll e n g +# ``` +# +# **Example 5:** +# +# ``` +# Input: $s = 'champion' +# Output: c h a m p i o n +# ``` +# +# **Example 6** +# +# ``` +# Input: $s = 'christmas' +# Output: c h r i s t m a +# ``` +# +# [blog]: https://medium.com/@alessiopiergiacomi/eertree-or-palindromic-tree-82453e75025b + +################################################################################ +# PWC Solution ################################################################# +################################################################################ + +def palindrome?(string) + reversed = string.reverse + string == reversed +end + +def eertree(string) + palindromes = [] + string.chars.each_index do |cursor| + string[cursor..string.length - 1].chars.each_index do |idx| + chars = idx + 1 + substring = string[cursor, chars] + next unless palindrome?(substring) + palindromes.push substring unless palindromes.include? substring + end + end + palindromes.join ' ' +end + +################################################################################ +# Utilities #################################################################### +################################################################################ + +def parse_test_case(filepath) + lines = File.read(filepath).split("\n").reject { |line| line.match(/^\s*#/) } + input, test = lines + input.strip! + test.strip! + [input, test] +end + +def print_params(path, input, test) + puts "#{path}:" + puts "Input: $s = #{input}" + puts "Output: #{test}" +end + +def test_solution(solution, input, test) + result = solution.call(input) + if test == result + puts "\e[32mPassed \u2690\e[0m\n\n" + else + puts "\e[31mFailed \u2715\e[0m\n\n" + end +end + +def print_and_run(target) + input, test = parse_test_case(target) + print_params(target, input, test) + test_solution(method(:eertree), input, test) +end + +def run_test_suite(dir) + tests = Dir.entries(dir).select { |f| File.file? File.join(dir, f) } + tests.sort.each { |test| print_and_run(File.join(dir, test)) } +end + +################################################################################ +# Main ######################################################################### +################################################################################ + +target = ARGV[0] || '../test_cases/ch-2' +print_and_run(target) if File.file?(target) +run_test_suite(target) if File.directory?(target) |
