From e1fcb94be56651e55fcf1e18fa40e0b27905cbf0 Mon Sep 17 00:00:00 2001 From: yxlau Date: Sat, 26 Nov 2016 14:10:42 +0800 Subject: [PATCH 01/10] Add power.rb (done) --- power.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 power.rb diff --git a/power.rb b/power.rb new file mode 100644 index 0000000..7d3d199 --- /dev/null +++ b/power.rb @@ -0,0 +1,14 @@ +# Write a method power which takes two integers (base and exponent) and returns the base raised to the power of exponent. Do not use Ruby’s ** operator for this! + +# > power(3,4) +# => 81 # (3*3*3*3) + +def power(base, exponent) + val = 1 + exponent.times do + val = val * base + end + val +end + +puts power(3,4) \ No newline at end of file From 7e72605a555cdd9728cfa6b30d9476b789cfe596 Mon Sep 17 00:00:00 2001 From: yxlau Date: Sat, 26 Nov 2016 14:13:11 +0800 Subject: [PATCH 02/10] Add factorial.rb (done) --- 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..30fc44e --- /dev/null +++ b/factorial.rb @@ -0,0 +1,7 @@ +def factorial(num) + tmp = 1 + 1.upto(num) do |i| + tmp = tmp * i + end + tmp +end From c643facf3795c1c17af5db9f168c6597d70a7545 Mon Sep 17 00:00:00 2001 From: yxlau Date: Sat, 26 Nov 2016 17:32:47 +0800 Subject: [PATCH 03/10] Add uniques.rb (done) --- uniques.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 uniques.rb diff --git a/uniques.rb b/uniques.rb new file mode 100644 index 0000000..9f3b1fe --- /dev/null +++ b/uniques.rb @@ -0,0 +1,12 @@ +def uniques(array) + no_duplicates = [] + array.each do |item| + unless no_duplicates.include? item + no_duplicates << item + end + end + no_duplicates + +end + +uniques([1,5,"frog", 2,1,3,"frog"]) \ No newline at end of file From 24c123e972d8db407d5a615ce2e254fc910d51b1 Mon Sep 17 00:00:00 2001 From: yxlau Date: Sat, 26 Nov 2016 17:41:41 +0800 Subject: [PATCH 04/10] Add combinations.rb (done) --- combinations.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 combinations.rb diff --git a/combinations.rb b/combinations.rb new file mode 100644 index 0000000..e17daeb --- /dev/null +++ b/combinations.rb @@ -0,0 +1,11 @@ +def combinations(a, b) + combos = [] + a.each do |a_item| + b.each do |b_item| + combos << a_item + b_item + end + end + combos +end + +combinations(["on", "in"], ["to", "rope"]) \ No newline at end of file From 6c4c2183e99dfdc714d96f3326199ec33a204298 Mon Sep 17 00:00:00 2001 From: yxlau Date: Sat, 26 Nov 2016 17:49:04 +0800 Subject: [PATCH 05/10] Add primes.rb (done) --- primes.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 primes.rb diff --git a/primes.rb b/primes.rb new file mode 100644 index 0000000..d9efd19 --- /dev/null +++ b/primes.rb @@ -0,0 +1,14 @@ +def is_prime?(num) + i = 1 + factors = [] + while i <= num + if num % i == 0 + factors << i + end + i += 1 + end + if factors.length > 2 + return false + end + return true +end From 6942f393e2afc74270d5e190992c65d477d0e878 Mon Sep 17 00:00:00 2001 From: yxlau Date: Sat, 26 Nov 2016 19:22:08 +0800 Subject: [PATCH 06/10] Add overlap.rb (done) --- overlap.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 overlap.rb diff --git a/overlap.rb b/overlap.rb new file mode 100644 index 0000000..1dfb1ee --- /dev/null +++ b/overlap.rb @@ -0,0 +1,15 @@ +# NOTE: this function assumes that the two coordinates provided for each rectangle are left-bottom and right-top, in that order. +def overlap(a, b) + # if x coordinates overlap + if (b[0][0] < a[1][0] && b[0][0] > a[0][0]) || (b[1][0] < a[1][0] && b[1][0] > a[0][0]) + # check if y coordinates overlap + if (b[0][1] < a[1][1] && b[0][1] > a[0][1]) || (b[1][1] < a[1][1] && b[1][1] > a[0][1]) + return true + end + end + return false + +end + +overlap( [ [0,0],[3,3] ], [ [1,1],[4,5] ] ) +overlap( [ [0,0],[1,4] ], [ [1,1],[3,2] ] ) \ No newline at end of file From 9751d84613eb1935f3090f52aa0a18452a1a3de3 Mon Sep 17 00:00:00 2001 From: yxlau Date: Sat, 26 Nov 2016 22:38:24 +0800 Subject: [PATCH 07/10] Add counting.rb (done) --- counting.rb | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 counting.rb diff --git a/counting.rb b/counting.rb new file mode 100644 index 0000000..fe993d1 --- /dev/null +++ b/counting.rb @@ -0,0 +1,31 @@ +def counting(player_num, count_limit) + count = 1 + players = [] + i = 0 + + # put players in array + (1...(player_num+1)).each do |i| + players << "player " + i.to_s + end + + + while count <= count_limit + increment = 1 + # if count is divisible by 7, reverse the array (since we cant switch the 'step') + if count % 7 == 0 + current_player = players[i] + players = players.reverse + i = players.index(current_player) + # if count is divisible by 11, skip the next person + end + if count % 11 == 0 + increment = 2 + end + puts "#{players[i]} says #{count}" + count += 1 + i = (i + increment) % 10 + end + +end + +counting(10, 100) \ No newline at end of file From 4db2d83e6ab2cf08dfc8e9ccaad366942b000eac Mon Sep 17 00:00:00 2001 From: yxlau Date: Mon, 28 Nov 2016 09:12:32 +0800 Subject: [PATCH 08/10] Modify handling of directional change --- counting.rb | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/counting.rb b/counting.rb index fe993d1..0d4f743 100644 --- a/counting.rb +++ b/counting.rb @@ -2,6 +2,7 @@ def counting(player_num, count_limit) count = 1 players = [] i = 0 + direction = 1 # put players in array (1...(player_num+1)).each do |i| @@ -11,19 +12,17 @@ def counting(player_num, count_limit) while count <= count_limit increment = 1 - # if count is divisible by 7, reverse the array (since we cant switch the 'step') + # if count is divisible by 7, switch the direction if count % 7 == 0 - current_player = players[i] - players = players.reverse - i = players.index(current_player) + direction *= -1 # if count is divisible by 11, skip the next person end if count % 11 == 0 - increment = 2 + increment = 2 end puts "#{players[i]} says #{count}" count += 1 - i = (i + increment) % 10 + i = (i + increment * direction) % 10 end end From f74e4ab2bf9667010ddd682baded9afef61a4ded Mon Sep 17 00:00:00 2001 From: yxlau Date: Mon, 28 Nov 2016 09:20:28 +0800 Subject: [PATCH 09/10] Modify player tracking --- counting.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/counting.rb b/counting.rb index 0d4f743..26632e0 100644 --- a/counting.rb +++ b/counting.rb @@ -1,13 +1,13 @@ def counting(player_num, count_limit) count = 1 players = [] - i = 0 + player_id = 0 direction = 1 - # put players in array - (1...(player_num+1)).each do |i| - players << "player " + i.to_s - end + # # put players in array + # (1...(player_num+1)).each do |i| + # players << "player " + i.to_s + # end while count <= count_limit @@ -20,9 +20,9 @@ def counting(player_num, count_limit) if count % 11 == 0 increment = 2 end - puts "#{players[i]} says #{count}" + puts "player#{player_id + 1} says #{count}" count += 1 - i = (i + increment * direction) % 10 + player_id = (player_id + increment * direction) % player_num end end From 39d69417e9ddd8edcb0dd452dc5948893bec3a3b Mon Sep 17 00:00:00 2001 From: yxlau Date: Mon, 28 Nov 2016 09:22:37 +0800 Subject: [PATCH 10/10] Remove unnecessary array --- counting.rb | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/counting.rb b/counting.rb index 26632e0..19463b9 100644 --- a/counting.rb +++ b/counting.rb @@ -1,14 +1,8 @@ def counting(player_num, count_limit) count = 1 players = [] - player_id = 0 + player_id = 1 direction = 1 - - # # put players in array - # (1...(player_num+1)).each do |i| - # players << "player " + i.to_s - # end - while count <= count_limit increment = 1 @@ -20,7 +14,7 @@ def counting(player_num, count_limit) if count % 11 == 0 increment = 2 end - puts "player#{player_id + 1} says #{count}" + puts "player#{player_id} says #{count}" count += 1 player_id = (player_id + increment * direction) % player_num end