Skip to content

Instantly share code, notes, and snippets.

@RickGriff
RickGriff / my_array_solutions.rb
Created December 8, 2017 13:52
Solutions to my_array.rb Stretch Challenge
class Array
def my_each(&block)
for i in self
yield i
end
end
def my_map(&block)
arr = []
for i in self
@RickGriff
RickGriff / count_by_x.rb
Created December 11, 2017 22:00
Codewars Challenge 2 - Count by X
#Create a function with two arguments that will return a list of length (n) with multiples of (x).
#Assume both the given number and the number of times to count will be positive numbers greater than 0.
#Return the results as an array (or list in Python, Haskell or Elixir).
#Examples:
#count_by(1,10) should return [1,2,3,4,5,6,7,8,9,10]
#count_by(2,5) should return [2,4,6,8,10]
#My Solution:
def count_by(x, n)
@RickGriff
RickGriff / sum_diagonal.rb
Created December 11, 2017 22:07
Codewars Bonus Challenge 4 - Find The Sum of Diagonal 1's
#Given a "square" array of subarrays, find the sum of values from the first value of the first array,
#the second value of the second array, the third value of the third array, and so on...
#Example 1:
#exampleArray = [[1, 0, 0, 0],
# [0, 1, 0, 0],
# [0, 0, 1, 0],
# [0, 0, 0, 1]]
@RickGriff
RickGriff / tube_strike.rb
Last active January 7, 2018 22:33
Codewars Challenge: Tube Strike Options Calculator
# Tube strike options calculator
# The sweaty bus ride
# There is a tube strike today so instead of getting the London Underground home you have decided to take the bus. It's a hot day and you have been sitting on the bus for over an hour, but the bus is hardly moving. Your arm is sticking to the window and sweat drips off your nose as you try to read your neighbour's book when you say to yourself, "This is ridiculous. I could have walked faster than this!" Suddenly you have an idea for an app that helps people decide whether to walk or take the bus home when the London Underground is on strike.
# You rush home (relatively speaking) and begin to define the function that will underpin your app.
# Function specification
# You must create a function which takes three parameters; walking distance home, distance the bus must travel, and the combined distance of walking from the office to the bus stop and from the bus stop to your house. All distances are in kilometres.
# So for example, if your home is 5km away b
@RickGriff
RickGriff / count_most_frequent.rb
Created January 7, 2018 22:36
Codewars Challenge: Find Count of Most Frequent Item in Array
# Write a program to find count of the most frequent item of an array.
# Assume that input is array of integers.
# Ex.:
# input array: [3, -1, -1, -1, 2, 3, -1, 3, -1, 2, 4, 9, 3]
# ouptut: 5
# Most frequent number in example array is -1. It occurs 5 times in input array.
-----
@RickGriff
RickGriff / get_middle_character.rb
Created January 7, 2018 22:48
Codewars Challenge: Get the Middle Character
# You are going to be given a word. Your job is to return the middle character of the word. If the word's length is odd, return the middle character. If the word's length is even, return the middle 2 characters.
# #Examples:
# runBF("test\0") should return "es"
# runBF("testing\0") should return "t"
# runBF("middle\0") should return "dd"
@RickGriff
RickGriff / disemvowel_trolls.rb
Last active January 8, 2018 12:30
Codewars Challenge: Disemvowel Trolls
# Trolls are attacking your comment section!
# A common way to deal with this situation is to remove all of the vowels from the trolls' comments, neutralizing the threat.
# Your task is to write a function that takes a string and return a new string with all vowels removed.
# For example, the string "This website is for losers LOL!" would become "Ths wbst s fr lsrs LL!".
# Note: for this kata y isn't considered a vowel.
@RickGriff
RickGriff / pry-orm.rb
Created January 13, 2018 11:09
Bloc Rails ORM Assignment 33 - Exploring Objects with Pry
[5] pry(#<Post>):1> nesting
Nesting status:
--
0. main (Pry top level)
1. #<Post>
[6] pry(#<Post>):1> self.to_s
=> "#<Post:0x000000035f51d8>"
[7] pry(#<Post>):1> self
=> #<Post:0x000000035f51d8
id: 1,
@RickGriff
RickGriff / format_names.rb
Last active January 21, 2018 21:17
Codewars Challenge: Format Names
# Given: an array containing hashes of names
# Return: a string formatted as a list of names separated by commas except for the last two names, which should be separated by an ampersand.
# Example:
# list([ {name: 'Bart'}, {name: 'Lisa'}, {name: 'Maggie'} ])
# # returns 'Bart, Lisa & Maggie'
# list([ {name: 'Bart'}, {name: 'Lisa'} ])
@RickGriff
RickGriff / spinwords.rb
Last active January 21, 2018 21:19
Codewars Challenge: Stop gninnipS My sdroW!
#Write a function that takes in a string of one or more words, and Drop one or more filereturns the same string, but with all
#five or more letter words reversed (Just like the name of this Kata).
#Strings passed in will consist of only letters and spaces.
#Spaces will be included only when more than one word is present.
#Examples:
#spinWords( "Hey fellow warriors" ) => returns "Hey wollef sroirraw"
#spinWords( "This is a test") => returns "This is a test"
#spinWords( "This is another test" )=> returns "This is rehtona test"