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

adding answers to VCS Prep Ruby Challenges #115

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
25 changes: 25 additions & 0 deletions combinations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# INSTRUCTIONS
# method takes two arrays of strings
# returns an array of new strings
# each string is a combination of item in first array with item in second array

# APPROACH
# initialize new array
# I'm going to need 2 nested iterators which will combine the strings using "each"
# and finally return the new array

def combinations(arr1, arr2)
combined_array = []
arr1.each do |first_string|
arr2.each do |second_string|
new_word = first_string + second_string
combined_array.push(new_word)
end
end
puts combined_array
end

arr1 = ["on","in"]
arr2 = ["to", "rope"]

combinations(arr1, arr2)
23 changes: 23 additions & 0 deletions combinations2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# TASK
# Write a method that takes 2 arrays of strings
# Returns 1 array with all of the combinatiosn of the items in them

# APPROACH
# Create 2 each loops, one nested inside another
# Iterate over items and select them
# Concatenate items and push to new array

def combinations(arr1,arr2)
new_array = []
arr1.each do |i|
arr2.each do |j|
new_array.push(i + j)
end
end
puts new_array
end

arr1 = ["on", "in"]
arr2 = ["to", "rope"]

combinations(arr1,arr2)
59 changes: 59 additions & 0 deletions counting_game.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# INSTRUCTIONS
# 10 friends sit in a circle and count from 1 to 100.
# When the number is divisible by 7, they switch directions
# When the number is divisible by 11, they skip the next person
# Find who says the number "100"

# APPROACH
# create a while loop that continues until num == 100
# inside that loop create a variable, person
# person will begin at 1
# when one loop finishes, it will increment
# when person equals anything larger than 10, we will subtract 10 from it
# when person equals anything smaller than 1, we will add 10 to it
# also inside that while loop, call a method called count direction
# that decides whether to increment or decrement person
# this count direction method will check if num if divisible by 7
# it will also check if num if divisible by 11, and skip if necessary
# the main method will puts the person at the end

def counting_game(num_players, final_num)
player = 1
num = 1
reversed = false
while num <= final_num

puts "Player: #{player}"
puts "Num: #{num}"

if num % 7 == 0
reversed == false ? reversed = true : reversed = false
end

if num % 11 == 0
increment = 2
else
increment = 1
end

if reversed
player -= increment
else
player += increment
end

if player > num_players
player -= num_players
end

if player < 1
player += num_players
end

num += 1

end
end

counting_game(10, 100)

12 changes: 12 additions & 0 deletions factorial.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# will take a number
# return the product of every number up to and including that number

def factorial(num)
product = 1
1.upto(num) do |i|
product *= i
end
puts product
end

factorial(5)
23 changes: 23 additions & 0 deletions is_prime?.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# INSTRUCTIONS
# method takes a number
# returns true if the number is prime

# APPROACH
# I'll know for sure that a number is prime if it has no factors
# I can check if the number has any factors by iterating from 2 up to the number
# If the original number can be evenly divided by the iterated number, then I'll know it's not prime
# Otherwise, it is prime!
# Let's do this!

def is_prime?(num)
prime = true
2.upto(num - 1) do |i|
if num % i == 0
prime = false
end
end
puts prime
end

is_prime?(7)
is_prime?(14)
57 changes: 57 additions & 0 deletions overlap.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# INSTRUCTIONS
# method takes two rectangles
# defined by the coordinates of their corners
# for example => [[0,0],[3,3]] and [[1,1],[4,6]]
# I must determine whether the rectangles overlap
# all coordinates are positive ints
# it doesn't count if the edges only touch but don't cross

# APPROACH
# By definition, "overlapping" is when the 2 rectangles share an area
# I can detect if they share an area by splitting each large rectangle into many small cells
# If any of the bottom left coordinatees of the cells match, I will know that the rectangles overlap
# So first I need to generate an array of x and y cell coordinates for each rectangle
# then I can loop through the array of one rectangle
# and loop through the array of the other rectangle inside
# if any of the coordinates match, then the rectangles overlap

def overlap?(rect1, rect2)

rect1_coords = coordinate_generator(rect1)
rect2_coords = coordinate_generator(rect2)
z = rect1_coords - rect2_coords

if z == rect1_coords
puts false
else
puts true
end

end

def coordinate_generator(rect)

rect_x_coord = []
rect_y_coord = []
y = rect[0][1]

while y < rect[1][1]
x = rect[0][0]
while x < rect[1][0]
rect_x_coord.push(x)
rect_y_coord.push(y)
x += 1
end
y += 1
end

combined_coords = rect_x_coord.zip(rect_y_coord)
return combined_coords

end

rect1 = [[0,0],[3,3]]
rect2 = [[1,1],[4,5]]
rect3 = [[7,7],[9,9]]

overlap?(rect1,rect2)
15 changes: 15 additions & 0 deletions power.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# define a method power that takes base and exponent
# it will multiply base by base exponent times
# and return the result

def power(base, exponent)
result = 1
exponent.times do |i|
result *= base
end
puts result
end

power(3,4)


24 changes: 24 additions & 0 deletions uniques.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# INSTRUCTIONS
# method takes an array of items
# return that array without duplicates
# without Ruby's "uniq" method

# HOW I'LL DO THIS
# Take array
# Def new array
# use an enumerable method, perhaps "include?"
# call include? on new array, if false, push item to new array

def uniques(array)
unique_array = []
array.each do |item|
if !unique_array.include?(item)
unique_array.push(item)
end
end
puts unique_array
end

array = [1,5,"frog",2,1,3,"frog"]

uniques(array)