Skip to content

Instantly share code, notes, and snippets.

View pinkopaque22's full-sized avatar

Patricia Ehrhardt pinkopaque22

View GitHub Profile
INSTRUCTIONS
Create a method named merge_us that expects two arguments and combines them, assuming they're hashes. The Hash class has a method named merge that will be helpful to you.
Create a method named my_keys that takes a hash argument and returns an Array of its keys. The Hash class has a method named keys that will be helpful to you.
Create a method named do_i_have? which expects two arguments. The arguments should represent a Hash and Array of keys. do_i_have? should return true if the Array has all of the keys in the hash, and false if it does not. To create this method, you could start with something like this:
def do_i_have?(hash, array_of_keys)
end
You want to make sure that every key in the hash argument is also contained in the array_of_keys argument. (Remember that hash is expected to be Hash and array_of_keys is expected to be an Array). To compare two objects for equality, you need them to be evaluated as the same object type. That is, you can't explicitly compare a Hash to an Array, but y
__Details__
Create a Book class with three instance methods:
set_title(title) should take an argument and use it to set the @title instance variable.
set_author(author) should take an argument and use it to set the @author instance variable.
Finally, description should accept no arguments and return a string formatted as: "Title was written by author" using the @title and @author instance variables.
__Specs__
@pinkopaque22
pinkopaque22 / Hashes-Hash Methods
Created December 18, 2015 00:21
Hashes in Ruby-Exercises
__Details__
This exercise will help you get familiar with some common Hash methods that Ruby provides.
merge_us method
Create a method named merge_us that expects two arguments and combines them, assuming they're hashes. The Hash class has a method named merge that will be helpful to you.
my_keys method
Create a method named my_keys that takes a hash argument and returns an Array of its keys. The Hash class has a method named keys that will be helpful to you.
do_i_have? method
@pinkopaque22
pinkopaque22 / Good Parents
Created December 19, 2015 07:18
Ruby's Advanced Classes
Parent classes should be as generic as possible. That is, they should be able to describe a broad range of things. A simple example of a class that lends itself to parenthood is a class named Dog. The purpose of this class will be to define the general attributes and behavior that all dogs share, regardless of breed. Attributes like having a name, four legs, two eyes, and a tail are shared amongst all dog breeds. Behavior like eating, sleeping, and barking are also shared amongst all dog breeds.
Let's take a look at inheritance in action by defining a Dog class. First we'll initialize the Dog class with a name attribute:
class Dog
attr_accessor :name
def initialize(name)
@name = name
@pinkopaque22
pinkopaque22 / most_frequent_item_count.py
Last active March 20, 2016 19:25
Solution for Codewars Most Frequent Item Count in Python
//Codewars Kata Level 7 (easy)//
MOST FREQUENT ITEM COUNT
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 occures 5 times in input array.
@pinkopaque22
pinkopaque22 / most_frequent_item_count.rb
Last active March 20, 2016 19:16
Codewars Solution for Most Frequent Item Count in Ruby
MOST FREQUENT ITEM COUNT
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
@pinkopaque22
pinkopaque22 / character_counter.rb
Created March 20, 2016 19:19
Codewars Solution to Character Counter in Ruby
CHARACTER COUNTER
You are going to be given a word. Your job will be to make sure that each character in that word has the exact same number of occurrences. You will return true if it is valid, or false if it is not.
For example:
"abcabc" is a valid word because 'a' appears twice, 'b' appears twice, and'c'appears twice.
"abcabcd" is NOT a valid word because 'a' appears twice, 'b' appears twice,'c' appears twice, but 'd' only appears once!
"123abc!" is a valid word because all of the characters only appear once in the word.
For this kata, capitals are considered the same as lowercase letters. Therefore: 'A' == 'a' .
INPUT
/////Problem/////
You are going to be given a word. Your job will be to make sure that each character in that word has the exact same number of occurrences. You will return true if it is valid, or false if it is not.
For example:
"abcabc" is a valid word because 'a' appears twice, 'b' appears twice, and'c' appears twice.
"abcabcd" is NOT a valid word because 'a' appears twice, 'b' appears twice, 'c' appears twice, but 'd' only appears once!
"123abc!" is a valid word because all of the characters only appear once in the word.
For this kata, capitals are considered the same as lowercase letters. Therefore: 'A' == 'a' .
//////Problem/////
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 a
@pinkopaque22
pinkopaque22 / binary_tree.rb
Created May 4, 2016 23:02
Binary Search Tree w/out Recursion
The "BST In-Order Traversal" Problem
Given a binary search tree, produce an in-order list of all keys in the tree without using recursion.
Explain and code an efficient solution. Analyze the time and space complexities.
Follow Up Problem:
Same problem as above but this time you also can't use a stack or any additional memory besides the tree.
class BinarySearchTree
attr_accessor :data, :left, :right