aboutsummaryrefslogtreecommitdiff
path: root/challenge-145/iangoodnight/ruby/ch-1.rb
blob: 59b9777a5fdfc6e645d3df2644edce185c6b1ae4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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)