From b87162854d61864369d40001376708c493cdc222 Mon Sep 17 00:00:00 2001 From: Jake Tingley Date: Tue, 18 Oct 2016 17:12:40 -0400 Subject: [PATCH 1/7] adding power.rb --- 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..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 From 34ce241ab5f9b0517441175f3d9222d15ada6e6c Mon Sep 17 00:00:00 2001 From: Jake Tingley Date: Tue, 18 Oct 2016 21:39:30 -0400 Subject: [PATCH 2/7] adding factorial.rb --- factorial.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 factorial.rb 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 From 42579178109b6d271f499f63d9322f518f6cd749 Mon Sep 17 00:00:00 2001 From: Jake Tingley Date: Sat, 22 Oct 2016 11:09:24 -0400 Subject: [PATCH 3/7] finished combinations.rb --- combinations.rb | 11 +++++++++++ countinggame.rb | 14 ++++++++++++++ is_prime.rb | 7 +++++++ overlap.rb | 2 ++ uniques.rb | 6 ++++++ 5 files changed, 40 insertions(+) create mode 100644 combinations.rb create mode 100644 countinggame.rb create mode 100644 is_prime.rb create mode 100644 overlap.rb create mode 100644 uniques.rb 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..21844fe --- /dev/null +++ b/countinggame.rb @@ -0,0 +1,14 @@ +10 friends are sitting in a circle around a table and decide to play a new game. In it, they count up through the numbers from 1 to 100. The first person says "1", the second says "2" and so on... but with a few catches: + +Whenever the number is divisible by 7, they switch directions. So person 6 will say "6", person 7 will say "7", then person 6 again will say "8". +Whenever the number is divisible by 11, they skip the next person for the following number. For instance, if person 3 says "33", person 5 will say "34" instead (person 4 gets skipped). +Your job is to code a program which outputs each number and which person said it. Use it to show that player 1 will say the number "100". + +Tips: + +Remember to stick with brute force instead of trying to "figure out" the trick to the problem. +Name your variables well! +Ignore the skipping to start out with. Only add it when you're ready. +Advanced Option: + +Make your method take two inputs -- the number of players and the number you're counting up to. Then see who says the last number each time! \ No newline at end of file diff --git a/is_prime.rb b/is_prime.rb new file mode 100644 index 0000000..f2b0d91 --- /dev/null +++ b/is_prime.rb @@ -0,0 +1,7 @@ +Write a method is_prime? which takes in a number and returns true if it is a prime number. + +is_prime?(num) + divide by every number from 2..num-1 + if any % = 0 then it isn't prime + if any % is greater than 0 then move to the next potential divisor + \ No newline at end of file diff --git a/overlap.rb b/overlap.rb new file mode 100644 index 0000000..d527a9e --- /dev/null +++ b/overlap.rb @@ -0,0 +1,2 @@ +Write a method overlap which takes two rectangles defined by the coordinates of their corners, e.g. [[0,0],[3,3]] and [[1,1],[4,6]], and determines whether they overlap. You can assume all coordinates are positive integers. + diff --git a/uniques.rb b/uniques.rb new file mode 100644 index 0000000..f5f97ff --- /dev/null +++ b/uniques.rb @@ -0,0 +1,6 @@ +Write a method uniques which takes an array of items and returns the array without any duplicates. Don’t use Ruby’s uniq method! + +uniques(arr) + compare two items + if they are equal then pop one off + return remaining items in array. \ No newline at end of file From 0cab6e282d42ef019b9e322427572de34dfd07f1 Mon Sep 17 00:00:00 2001 From: Jake Tingley Date: Sun, 23 Oct 2016 02:11:04 -0400 Subject: [PATCH 4/7] finished uniques.rb --- uniques.rb | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/uniques.rb b/uniques.rb index f5f97ff..c06e9d6 100644 --- a/uniques.rb +++ b/uniques.rb @@ -1,6 +1,12 @@ -Write a method uniques which takes an array of items and returns the array without any duplicates. Don’t use Ruby’s uniq method! +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(arr) - compare two items - if they are equal then pop one off - return remaining items in array. \ No newline at end of file + +uniques([1,5,"frog", 2,1,3,"frog"]) \ No newline at end of file From c11e1129264d14487c6328673850bf759cdc31bb Mon Sep 17 00:00:00 2001 From: Jake Tingley Date: Sun, 23 Oct 2016 11:29:12 -0400 Subject: [PATCH 5/7] finished is_prime.rb --- is_prime.rb | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/is_prime.rb b/is_prime.rb index f2b0d91..d467dbc 100644 --- a/is_prime.rb +++ b/is_prime.rb @@ -1,7 +1,11 @@ -Write a method is_prime? which takes in a number and returns true if it is a prime number. +def is_prime?(num) + (2...num).each do |divisor| + if num % divisor == 0 + return false + end + end + return true +end -is_prime?(num) - divide by every number from 2..num-1 - if any % = 0 then it isn't prime - if any % is greater than 0 then move to the next potential divisor - \ No newline at end of file +is_prime?(7) +is_prime?(14) \ No newline at end of file From c099f0ccc3447ef5d70d6223c6d5e24ed8e78da5 Mon Sep 17 00:00:00 2001 From: Jake Tingley Date: Sun, 23 Oct 2016 11:53:19 -0400 Subject: [PATCH 6/7] overlap.rb is working --- overlap.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/overlap.rb b/overlap.rb index d527a9e..3740491 100644 --- a/overlap.rb +++ b/overlap.rb @@ -1,2 +1,12 @@ -Write a method overlap which takes two rectangles defined by the coordinates of their corners, e.g. [[0,0],[3,3]] and [[1,1],[4,6]], and determines whether they overlap. You can assume all coordinates are positive integers. +# 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 From 0241d578a562f3cbee9224c8ae3d18e3cda88612 Mon Sep 17 00:00:00 2001 From: Jake Tingley Date: Sun, 23 Oct 2016 12:30:58 -0400 Subject: [PATCH 7/7] finished countinggame.rb --- countinggame.rb | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/countinggame.rb b/countinggame.rb index 21844fe..0f7e914 100644 --- a/countinggame.rb +++ b/countinggame.rb @@ -1,14 +1,25 @@ -10 friends are sitting in a circle around a table and decide to play a new game. In it, they count up through the numbers from 1 to 100. The first person says "1", the second says "2" and so on... but with a few catches: +def counting_game(total_players, high_count) + clockwise = true + person = 0 -Whenever the number is divisible by 7, they switch directions. So person 6 will say "6", person 7 will say "7", then person 6 again will say "8". -Whenever the number is divisible by 11, they skip the next person for the following number. For instance, if person 3 says "33", person 5 will say "34" instead (person 4 gets skipped). -Your job is to code a program which outputs each number and which person said it. Use it to show that player 1 will say the number "100". + 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 -Tips: - -Remember to stick with brute force instead of trying to "figure out" the trick to the problem. -Name your variables well! -Ignore the skipping to start out with. Only add it when you're ready. -Advanced Option: - -Make your method take two inputs -- the number of players and the number you're counting up to. Then see who says the last number each time! \ No newline at end of file +counting_game(10, 100) \ No newline at end of file