-
-
Save anon0mys/f9f247123d3050164d630c434c8ec701 to your computer and use it in GitHub Desktop.
Eval
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Palindrome a word phrase that reads the same backwards | |
# write code that will return true for the following palindrome | |
# "A man, a plan, a canal: Panama" | |
module Palindrome | |
def self.is_palindrome(word) | |
normalized = word.downcase.gsub(/\W/, '') | |
normalized == normalized.reverse | |
end | |
end | |
# Palindrome.is_palindrome("A man, a plan, a canal: Panama") => true | |
############################################################################################################################## | |
#What would you say is wrong with this code | |
class CommentsController < ApplicationController | |
def users_comments | |
posts = Post.all | |
comments = posts.map(&:comments).flatten | |
@user_comments = comments.select do |comment| | |
comment.author.username == params[:username] | |
end | |
end | |
end | |
# 1. I would do the user lookup first | |
# 2. Mapping over the entire posts table to reduce it into a list of comments is going | |
# to take forever... | |
# 3. It looks like comment.author is a user. I would make sure the field is a foreign | |
# key reference to user, so that you could just do user.comments | |
class CommentsController < ApplicationController | |
def users_comments | |
user = User.where(username: params[:username]) | |
@user_comments = user.comments | |
end | |
end | |
############################################################################################################################## | |
files = { | |
'Input.txt' => 'Randy', | |
'Code.py' => 'Stan', | |
'Output.txt' => 'Randy' | |
} | |
module FileOwners | |
def self.group_by_owners(files) | |
files.reduce({}) do |result, (file, owner)| | |
if result[owner] | |
result[owner].push(file) | |
else | |
result[owner] = [file] | |
end | |
result | |
end | |
end | |
end | |
puts FileOwners.group_by_owners(files) | |
#returns hash containing array of file names | |
# for each owner | |
# {randy => [Input.text, Output.txt], stand => [Code.py]} | |
# | |
############################################################################################################################## | |
# How would you define a Person model so that any Person can be | |
# assigned as a parent of another Person. | |
# What columns would you need to define in the migration creating the table for person | |
# so you could do the following | |
# john = Person.create(name: "John") | |
# jim = Person.create(name: "Jim", parent: john) | |
# bob = Person.create(name: "Bob", parent: john) | |
# john.children.map(&:name) | |
# => ["Jim", "Bob"] | |
# | |
class Person < ActiveRecord::Base | |
belongs_to :parent, class_name: 'Person', optional: true | |
has_many :children, class_name: 'Person' | |
end | |
def change | |
add_reference :users, :parent_id, index: true, null: true | |
end | |
############################################################################################################################## | |
#config/routes.rb | |
# what routes, http verbs will the following snippet generate | |
resources :posts do | |
member do | |
get 'comments' | |
end | |
collection do | |
post 'bulk_upload' | |
end | |
end | |
# GET, POST to /posts | |
# GET to /posts/new | |
# GET to /posts/:id/edit | |
# GET, PUT, PATCH, DELETE to /posts/:id | |
# GET to /posts/:id/comments | |
# GET to /posts/bulk_upload |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment