aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2022-07-05 21:45:38 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2022-07-05 21:45:38 +0100
commit460d840eb0dc303ade288768518636f8cec4e5a0 (patch)
treed220676ce80edd032f1bade2919dc483964f4478
parent0eb8756fec86c8437c1bc60d9a19022c0d62feb8 (diff)
downloadperlweeklychallenge-club-460d840eb0dc303ade288768518636f8cec4e5a0.tar.gz
perlweeklychallenge-club-460d840eb0dc303ade288768518636f8cec4e5a0.tar.bz2
perlweeklychallenge-club-460d840eb0dc303ade288768518636f8cec4e5a0.zip
- Added guest contributions by Robert DiCicco.
-rw-r--r--challenge-172/robert-dicicco/ruby/ch-1.rb50
-rw-r--r--challenge-172/robert-dicicco/ruby/ch-2.rb47
2 files changed, 97 insertions, 0 deletions
diff --git a/challenge-172/robert-dicicco/ruby/ch-1.rb b/challenge-172/robert-dicicco/ruby/ch-1.rb
new file mode 100644
index 0000000000..f27fbc502b
--- /dev/null
+++ b/challenge-172/robert-dicicco/ruby/ch-1.rb
@@ -0,0 +1,50 @@
+#!ruby.exe
+
+# AUTHOR: Robert DiCicco
+# DATE: 2022-07-05
+# Challenge 172 Prime Partition ( Ruby )
+
+require 'prime'
+
+arr = [] # array to hold primes
+result = [] # array to hold results
+
+m = ARGV[0].to_i # number to test
+
+n = ARGV[1].to_i # how many primes to sum
+
+(1..m).each { |p| # get primes <= number to test
+
+ if(Prime.prime?(p))
+
+ arr.push(p) # save it to arr
+
+ end
+
+}
+
+perm = arr.permutation(n).to_a # get permutations of length n from arr
+
+perm.each { |subperm| # get sum of each perm
+
+ sum = 0
+
+ (0..n).each { |ndx|
+
+ sum += subperm[ndx].to_i
+
+ }
+
+ if(sum == m) # if sum equals number to test
+
+ result.push(subperm.sort) # save sorted perm to result
+
+ end
+
+}
+
+print("$m = #{m} $n = #{n}\n")
+
+print(result.uniq) # remove all dup perms
+
+puts(" ")
diff --git a/challenge-172/robert-dicicco/ruby/ch-2.rb b/challenge-172/robert-dicicco/ruby/ch-2.rb
new file mode 100644
index 0000000000..d936889090
--- /dev/null
+++ b/challenge-172/robert-dicicco/ruby/ch-2.rb
@@ -0,0 +1,47 @@
+#!ruby.exe
+
+# AUTHOR: Robert DiCicco
+# DATE: 2022-07-05
+# Challenge 172 Five Numbers ( Ruby )
+
+# Got both of these routines off of StackOverflow
+
+# IDNRTW ( I Do Not Reinvent The Wheel )
+
+def median(array) #Define your method accepting an array as an argument.
+
+ array = array.sort #sort the array from least to greatest
+
+ if array.length.odd? #is the length of the array odd?
+
+ array[(array.length - 1) / 2] #find value at this index
+
+ else array.length.even? #is the length of the array even?
+
+ (array[array.length/2] + array[array.length/2 - 1])/2.to_f
+
+ #average the values found at these two indexes and convert to float
+
+ end
+
+end
+
+def percentile(values, percentile)
+
+ values_sorted = values.sort
+
+ k = (percentile*(values_sorted.length-1)+1).floor - 1
+
+ f = (percentile*(values_sorted.length-1)+1).modulo(1)
+
+ return values_sorted[k] + (f * (values_sorted[k+1] - values_sorted[k]))
+
+end
+
+arr = [10,12,14,16,18,24]
+
+med = median(arr)
+
+print("#{arr}\n")
+
+print("Min = #{arr.min} Q1 = #{percentile(arr,0.25)} Median = #{med} Q3 = #{percentile(arr,0.75)} Max = #{arr.max}\n")