Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complete VCS prep ruby challenges 1-6 plus Bigger Challenge #116

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions combinations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def combinations( strings1, strings2)
strings1.collect { |string1|
strings2.collect { |string2|
string1 + string2
}
}.flatten
end

p combinations(["on","in"],["to","rope"])
# ["onto", "onrope", "into", "inrope"]
13 changes: 13 additions & 0 deletions counting_game.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
def counting_game(num_players, num_count_to)
clockwise = true
person = 0

num_count_to.times do |count|
puts "Player " + (person+1).to_s + " says " + (count+1).to_s
clockwise = !clockwise if (count+1)%7 == 0
(count+1)%11 == 0 ? move = 2 : move = 1
clockwise ? person = (person+move)%num_players : person = (person-move)%num_players
end
end

counting_game(10, 100)
11 changes: 11 additions & 0 deletions factorial.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def factorial(end_number)
return 0 if end_number == 0
result = 1
(1..end_number).each do |num|
result *= num
end
result
end

puts factorial(5)
# 120
21 changes: 21 additions & 0 deletions is_prime.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
$primes = [2]

def is_prime?(number)
return false if number < 1
return true if $primes.include?(number)
return false if $primes[-1] > number

test_number = $primes[-1]
begin
test_number += 1
next if $primes.any? { |prime| test_number%prime == 0 }

$primes << test_number
return true if test_number == number
end while test_number < number

return false
end

p is_prime? 23 # true
p $primes # [2, 3, 5, 7, 11, 13, 17, 19, 23]
13 changes: 13 additions & 0 deletions overlap.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
def overlap(rect1, rect2)
rect1_unincl_yrange = (rect1[0][0]+1...rect1[1][0])
rect1_unincl_xrange = (rect1[0][1]+1...rect1[1][1])
if ( rect1_unincl_xrange.include? rect2[0][0] ) || ( rect1_unincl_xrange.include? rect2[1][0] )
if ( rect1_unincl_yrange.include? rect2[0][1] ) || ( rect1_unincl_yrange.include? rect2[1][1] )
return true
end
end
return false
end

puts overlap( [ [0,0],[3,3] ], [ [1,1],[4,5] ] ) # true
puts overlap( [ [0,0],[1,4] ], [ [1,1],[3,2] ] ) # false
10 changes: 10 additions & 0 deletions power.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def power(base, exponent)
result = 1
exponent.times do
result *= base
end
result
end

puts power(3,4)
# 81
21 changes: 21 additions & 0 deletions prime.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
$primes = [2]

def is_prime?(number)
return false if number < 1
return true if $primes.include?(number)
return false if $primes[-1] > number

test_number = $primes[-1]
begin
test_number += 1
next if $primes.any? { |prime| test_number%prime == 0 }

$primes << test_number
return true if test_number == number
end while test_number < number

return false
end

p is_prime? 23 # true
p $primes # [2, 3, 5, 7, 11, 13, 17, 19, 23]
12 changes: 12 additions & 0 deletions uniques.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def uniques(array)
uniq_items = []
until array.empty?
first_element = array.slice!(0)
array.delete_if { |el| el == first_element }
uniq_items << first_element
end
uniq_items
end

p uniques([1,5,"frog", 2,1,3,"frog"])
# [1, 5, "frog", 2, 3]