From e31d4928b03fa7dde3732c03ec4adc8c60a91633 Mon Sep 17 00:00:00 2001 From: Egle Libby Date: Thu, 27 Oct 2016 21:35:52 +0600 Subject: [PATCH 1/7] create power method --- power.rb | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 power.rb diff --git a/power.rb b/power.rb new file mode 100644 index 0000000..603c70e --- /dev/null +++ b/power.rb @@ -0,0 +1,7 @@ +def power(base, exponent) + result = 1 + exponent.times { result *= base } + return result +end + +puts power(3,4) \ No newline at end of file From 34ce7ffa8d4e906613ed80aca890497e170c4afa Mon Sep 17 00:00:00 2001 From: Egle Libby Date: Thu, 27 Oct 2016 21:53:27 +0600 Subject: [PATCH 2/7] create factorial method --- factorial.rb | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 factorial.rb diff --git a/factorial.rb b/factorial.rb new file mode 100644 index 0000000..7ac273c --- /dev/null +++ b/factorial.rb @@ -0,0 +1,7 @@ +def factorial(number) + result = 1 + (1..number).each { |num| result *= num } + return result +end + +puts factorial(5) \ No newline at end of file From 8dadc4e0d8d472e3fde39a4c0957b4d2732cca65 Mon Sep 17 00:00:00 2001 From: Egle Libby Date: Thu, 27 Oct 2016 22:06:41 +0600 Subject: [PATCH 3/7] create uniques method --- uniques.rb | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 uniques.rb diff --git a/uniques.rb b/uniques.rb new file mode 100644 index 0000000..11d22b2 --- /dev/null +++ b/uniques.rb @@ -0,0 +1,9 @@ +def uniques(array) + result = [] + array.each do |item| + result << item unless result.include?(item) + end + return result +end + +p uniques([1,5,"frog", 2,1,3,"frog"]) \ No newline at end of file From 73e1b5234c32bc917077d21d30d5d34dba582254 Mon Sep 17 00:00:00 2001 From: Egle Libby Date: Thu, 27 Oct 2016 22:09:56 +0600 Subject: [PATCH 4/7] create combinations method --- combinations.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 combinations.rb diff --git a/combinations.rb b/combinations.rb new file mode 100644 index 0000000..8b5c7df --- /dev/null +++ b/combinations.rb @@ -0,0 +1,12 @@ +def combinations(arr1, arr2) + result = [] + arr1.each do |word1| + arr2.each do |word2| + combo = word1 + word2 + result << combo + end + end + return result +end + +p combinations(["on", "in"], ["to", "rope"]) \ No newline at end of file From 4b8382e8c5aa807c75cc34c6a6fc579f5ca23c21 Mon Sep 17 00:00:00 2001 From: Egle Libby Date: Thu, 27 Oct 2016 22:25:06 +0600 Subject: [PATCH 5/7] create is_prime method --- is_prime.rb | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 is_prime.rb diff --git a/is_prime.rb b/is_prime.rb new file mode 100644 index 0000000..752cc22 --- /dev/null +++ b/is_prime.rb @@ -0,0 +1,9 @@ +def is_prime?(number) + (2..Math.sqrt(number).round).each do |div| + return false if number % div == 0 + end + return true +end + +puts is_prime?(7) +puts is_prime?(14) \ No newline at end of file From e3092ac49afaa77b0f3ad32746412a5442de205e Mon Sep 17 00:00:00 2001 From: Egle Libby Date: Fri, 28 Oct 2016 17:08:00 +0600 Subject: [PATCH 6/7] create overlap method --- overlap.rb | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 overlap.rb diff --git a/overlap.rb b/overlap.rb new file mode 100644 index 0000000..cdfdb10 --- /dev/null +++ b/overlap.rb @@ -0,0 +1,6 @@ +def overlap(rect1, rect2) + rect1[0][0] < rect2[1][0] && rect1[1][1] > rect2[0][1] && rect1[1][0] > rect2[0][0] && rect1[0][1] < rect2[1][1] +end + +puts overlap( [ [0,0],[3,3] ], [ [1,1],[4,5] ] ) +puts overlap( [ [0,0],[1,4] ], [ [1,1],[3,2] ] ) \ No newline at end of file From b0aea324b8d23d14d4f71cb1921b03c51e6e7e03 Mon Sep 17 00:00:00 2001 From: Egle Libby Date: Fri, 28 Oct 2016 17:46:19 +0600 Subject: [PATCH 7/7] create counting game --- counting_game.rb | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 counting_game.rb diff --git a/counting_game.rb b/counting_game.rb new file mode 100644 index 0000000..7f85e83 --- /dev/null +++ b/counting_game.rb @@ -0,0 +1,28 @@ +def counting_game(friends, max_number) + current_number = 0 + person_nr = 0 + direction = 1 + until current_number == max_number + current_number += 1 + #if current number is higher than 1 and previous number was divisible by 11 then we skip one person + if current_number > 1 && (current_number - 1) % 11 == 0 + person_nr += (2 * direction) + else + person_nr += direction + end + + if person_nr > friends + person_nr -= friends + elsif person_nr < 1 + person_nr += friends + end + #if number is divisible by 7, we change the direction for the next turn + if current_number % 7 == 0 + direction = -(direction) + end + + puts "Person nr. #{person_nr} says #{current_number}." + end +end + +counting_game(10, 100) \ No newline at end of file