From df019028c88119bc2565d175e3c907ec7af48081 Mon Sep 17 00:00:00 2001 From: Ahmed Khokar Date: Tue, 22 Nov 2016 22:42:40 -0500 Subject: [PATCH 1/4] First two problems done --- factorial.rb | 10 ++++++++++ power.rb | 9 +++++++++ uniques.rb | 0 3 files changed, 19 insertions(+) create mode 100644 factorial.rb create mode 100644 power.rb create mode 100644 uniques.rb diff --git a/factorial.rb b/factorial.rb new file mode 100644 index 0000000..91dae4b --- /dev/null +++ b/factorial.rb @@ -0,0 +1,10 @@ +def factorial(number) + result = 1 + (1..number).each do |num| + result = result * num + end + return result +end + +puts factorial(5) + diff --git a/power.rb b/power.rb new file mode 100644 index 0000000..cef3a6b --- /dev/null +++ b/power.rb @@ -0,0 +1,9 @@ +def power(base, exponent) + result = 1 + exponent.times do + result = result * base + end +return result +end + +puts power(8,3) \ No newline at end of file diff --git a/uniques.rb b/uniques.rb new file mode 100644 index 0000000..e69de29 From 612d45e2d9ede888f2cf2740666a5dbae66303ed Mon Sep 17 00:00:00 2001 From: Ahmed Khokar Date: Tue, 22 Nov 2016 23:11:24 -0500 Subject: [PATCH 2/4] Finished four problems total --- combinations.rb | 0 primes.rb | 16 ++++++++++++++++ uniques.rb | 12 ++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 combinations.rb create mode 100644 primes.rb diff --git a/combinations.rb b/combinations.rb new file mode 100644 index 0000000..e69de29 diff --git a/primes.rb b/primes.rb new file mode 100644 index 0000000..3b1354e --- /dev/null +++ b/primes.rb @@ -0,0 +1,16 @@ +def is_prime? (number) + result = true + quotient = 2 + while (quotient < number) and result == true + if number%quotient == 0 + result = false + else + quotient += 1 + end + end + return result +end + +puts is_prime?(7) +puts is_prime?(3) +puts is_prime?(14) \ No newline at end of file diff --git a/uniques.rb b/uniques.rb index e69de29..2adb6d6 100644 --- a/uniques.rb +++ b/uniques.rb @@ -0,0 +1,12 @@ +def uniques(originalArray) + result = [] + originalArray.each do |item| + unless result.include? item + result.push(item) + end + end + return result +end + + +puts uniques([1,5,5,2]) From 6b2e6266ab703da5e28671a0f2fe9f91528e0ac9 Mon Sep 17 00:00:00 2001 From: Ahmed Khokar Date: Tue, 22 Nov 2016 23:28:28 -0500 Subject: [PATCH 3/4] Combinations --- combinations.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/combinations.rb b/combinations.rb index e69de29..8797f64 100644 --- a/combinations.rb +++ b/combinations.rb @@ -0,0 +1,11 @@ +def combinations(arrayOne, arrayTwo) + result=[] + arrayOne.each do |string| + arrayTwo.each do |string2| + result.push(string + string2) + end + end + return result +end + +puts combinations(["hi", "bye", "hey", "jeff"], ["kayla", "john", "joe", "josh"]) From 0f65e73bcfd5281d5f26ed2369497af33aa1920e Mon Sep 17 00:00:00 2001 From: Ahmed Khokar Date: Wed, 23 Nov 2016 00:11:45 -0500 Subject: [PATCH 4/4] Overlap done --- overlap.rb | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 overlap.rb diff --git a/overlap.rb b/overlap.rb new file mode 100644 index 0000000..5569b03 --- /dev/null +++ b/overlap.rb @@ -0,0 +1,35 @@ +def overlap(recArray1, recArray2) + + width1 = (recArray1[0][0]...recArray1[1][0]) + height1 = (recArray1[0][1]...recArray1[1][1]) + width2 = (recArray2[0][0]...recArray2[1][0]) + height2 = (recArray2[0][1]...recArray2[1][1]) + + heightOverlap = false + widthOverlap = false + overlap = false + + width1.each do |range| + if width2.include? range + widthOverlap = true + end + end + + height1.each do |range| + if height2.include? range + heightOverlap = true + end + end + + if heightOverlap == true and widthOverlap == true + overlap = true + else + overlap = false + end + return overlap +end + +puts overlap([[0,0], [3,3]], [[1,1], [4,5]]) +puts overlap([[0,0], [1,4]], [[1,1], [3,2]]) + +