diff --git a/combinations.rb b/combinations.rb new file mode 100644 index 0000000..e87491b --- /dev/null +++ b/combinations.rb @@ -0,0 +1,11 @@ +def combinations(arr1, arr2) + new_array = [] + arr1.each{ |x| + arr2.each{ |y| + new_array.push(x+y) + } + } + return new_array +end + +combinations(["on","in"],["to","rope"]) \ No newline at end of file diff --git a/countinggame.rb b/countinggame.rb new file mode 100644 index 0000000..0f7e914 --- /dev/null +++ b/countinggame.rb @@ -0,0 +1,25 @@ +def counting_game(total_players, high_count) + clockwise = true + person = 0 + + high_count.times do |count| + puts "Player " + (person+1).to_s + " says " + (count+1).to_s + + if (count + 1) % 7 == 0 + clockwise = !clockwise + end + + if (count + 1) % 11 == 0 + jump = 2 + else jump = 1 + end + + if clockwise + person = (person + jump) % total_players + else person = (person - jump) % total_players + end + + end +end + +counting_game(10, 100) \ No newline at end of file diff --git a/factorial.rb b/factorial.rb new file mode 100644 index 0000000..4f598f6 --- /dev/null +++ b/factorial.rb @@ -0,0 +1,14 @@ +=begin Write a method factorial which takes a number and returns the product of every number up to the current number multiplied together. +=end + +def factorial(num) + count = 1 + product = 1 + num.times { + product *= count + count += 1 + } + return product +end + +factorial(6) \ No newline at end of file diff --git a/is_prime.rb b/is_prime.rb new file mode 100644 index 0000000..d467dbc --- /dev/null +++ b/is_prime.rb @@ -0,0 +1,11 @@ +def is_prime?(num) + (2...num).each do |divisor| + if num % divisor == 0 + return false + end + end + return true +end + +is_prime?(7) +is_prime?(14) \ No newline at end of file diff --git a/overlap.rb b/overlap.rb new file mode 100644 index 0000000..3740491 --- /dev/null +++ b/overlap.rb @@ -0,0 +1,12 @@ +# works on given examples but doesn't work on all inputs +# maybe you can create an array with all corners and compare that way + +def overlap(rect1, rect2) + if rect1[1][0] > rect2[0][0] && rect1[1][1] > rect2[0][0] + return true + else + return false + end +end + +overlap( [ [1,7],[5,4] ], [ [4,5],[7,1] ] ) \ No newline at end of file diff --git a/power.rb b/power.rb new file mode 100644 index 0000000..034f6fe --- /dev/null +++ b/power.rb @@ -0,0 +1,7 @@ +def power (base, exponent) + product = 1 + exponent.times{ product *= base } + return product +end + +power (5,2) \ No newline at end of file diff --git a/uniques.rb b/uniques.rb new file mode 100644 index 0000000..c06e9d6 --- /dev/null +++ b/uniques.rb @@ -0,0 +1,12 @@ +def uniques(arr) + unique_array = [] + arr.each do |x| + if unique_array.include?(x) == false + unique_array.push(x) + end + end + return unique_array +end + + +uniques([1,5,"frog", 2,1,3,"frog"]) \ No newline at end of file