From 46feeb6ccc739ebc3271a11c8900e9b52b774442 Mon Sep 17 00:00:00 2001 From: John Sawyer Date: Sun, 30 Oct 2016 20:38:02 -0400 Subject: [PATCH] Move files over from cloud9 --- combinations.rb | 9 +++++++++ counting.rb | 42 ++++++++++++++++++++++++++++++++++++++++++ factorial.rb | 7 +++++++ overlap.rb | 13 +++++++++++++ power.rb | 7 +++++++ primes.rb | 5 +++++ uniques.rb | 11 +++++++++++ 7 files changed, 94 insertions(+) create mode 100644 combinations.rb create mode 100644 counting.rb create mode 100644 factorial.rb create mode 100644 overlap.rb create mode 100644 power.rb create mode 100644 primes.rb create mode 100644 uniques.rb diff --git a/combinations.rb b/combinations.rb new file mode 100644 index 0000000..c9dac9c --- /dev/null +++ b/combinations.rb @@ -0,0 +1,9 @@ +def combinations(arr1, arr2) + return_arr = [] + arr1.each_with_index do |word1, index1| + arr2.each_with_index do |word2, index2| + return_arr << word1 + word2 + end + end + return_arr +end \ No newline at end of file diff --git a/counting.rb b/counting.rb new file mode 100644 index 0000000..9bd02a0 --- /dev/null +++ b/counting.rb @@ -0,0 +1,42 @@ +def counting(players, count) + def rotate_player(up, player, players) + if up + if player < players + player += 1 + else + player = 1 + end + else + if player > 1 + player -= 1 + else + player = players + end + end + end + + up = true + player = 1 + + (1..count).each do |x| + #skip a player if count is divisible by 11 + # if x % 11 == 0 + # player = rotate_player(up, player, players) + # end + #print number and corresponding number + + puts "The number #{x} was spoken by player #{player}" + + #change direction if divisible by seven + if x % 7 == 0 + up = !up + end + + player = rotate_player(up, player, players) + + if x % 11 == 0 + player = rotate_player(up, player, players) + end + end + +end \ No newline at end of file diff --git a/factorial.rb b/factorial.rb new file mode 100644 index 0000000..d5d09e2 --- /dev/null +++ b/factorial.rb @@ -0,0 +1,7 @@ +def factorial(num) + answer = 1 + (1..num).each do |x| + answer *= x + end + answer +end \ No newline at end of file diff --git a/overlap.rb b/overlap.rb new file mode 100644 index 0000000..6a8e4e5 --- /dev/null +++ b/overlap.rb @@ -0,0 +1,13 @@ +def overlap(n, m) + #list of sides: + firstleft = n[0][0] + firstbottom = n[0][1] + firstright = n[1][0] + firsttop = n[1][1] + secondleft = m[0][0] + secondbottom = m[0][1] + secondright = m[1][0] + secondtop = m[1][1] + + firstleft < secondright && firsttop > secondbottom && firstright > secondleft && firstbottom < secondtop +end \ No newline at end of file diff --git a/power.rb b/power.rb new file mode 100644 index 0000000..a123ba0 --- /dev/null +++ b/power.rb @@ -0,0 +1,7 @@ +def power(base,exponent) + answer = 1 + exponent.times do + answer *= base + end + answer +end \ No newline at end of file diff --git a/primes.rb b/primes.rb new file mode 100644 index 0000000..56402a8 --- /dev/null +++ b/primes.rb @@ -0,0 +1,5 @@ +def is_prime?(num) + return false if num <= 1 + Math.sqrt(num).to_i.downto(2).each {|i| return false if num % i == 0} + true +end \ No newline at end of file diff --git a/uniques.rb b/uniques.rb new file mode 100644 index 0000000..aa56c78 --- /dev/null +++ b/uniques.rb @@ -0,0 +1,11 @@ +def uniques(x) + final = [] + #loop over entire array + x.each_with_index do |v, i| + unless final.include?(v) + final.push(v) + end + end + #return array + final +end \ No newline at end of file