This file contains hidden or 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
class Solution: | |
def connect(self, root): | |
self.do_connect(root, None) | |
def do_connect(self, node, right_sib): | |
if not node: | |
return | |
node.next = right_sib if right_sib else None | |
if node.left: | |
self.do_connect(node.left, right_sib=node.right) |
This file contains hidden or 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
class Node | |
attr_accessor :next, :left_child, :right_child | |
end | |
def add_next_right_ptrs(node, right_sib) | |
node.next = right_sib || nil | |
if node.left_child | |
add_next_right_ptrs(node.left_child, right_sib=node.right_child) | |
end | |
if node.right_child |
This file contains hidden or 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
def palindrome?(s) | |
s = s.downcase.split(/[^a-zA-Z0-9]+/).join.split "" | |
s.zip(s.reverse).each do |letters| | |
return false if letters[0] != letters[1] | |
end | |
return true | |
end |
This file contains hidden or 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
require 'net/http' | |
require 'json' | |
HOST = 'shopicruit.myshopify.com' | |
PRODUCT_PATH = '/products.json' | |
def get_computer_and_keyboard_cost() | |
json_str = Net::HTTP.get HOST, PRODUCT_PATH | |
json_dict = JSON.parse json_str |
This file contains hidden or 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
defmodule Abilities1 do | |
defimpl Canada.Can, for: User do | |
def can?(%User{id: user_id}, action, %Testimonial{id: testimonial_id}) | |
when action in [:create], do: true | |
def can?(%User{id: user_id}, action, %Testimonial{id: testimonial_id}) | |
when action in [:edit], do: true | |
end | |
end | |
defmodule BetterAbilities do |
This file contains hidden or 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
defmodule Blog.Abilities do | |
defimpl Canada.Can, for: Blog.User do | |
def can?(%Blog.User{}, :index, Blog.User), do: true | |
def can?(%Blog.User{}, _, _), do: true | |
def can?(nil, _, _), do: false |
This file contains hidden or 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
require Abilities | |
defmodule User do | |
defstruct id: 1, dude_id: 1 | |
end | |
defmodule M do | |
use Abilities | |
This file contains hidden or 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
defmodule Abilities do | |
defmacro __using__(_opts) do | |
quote do | |
Module.register_attribute __MODULE__, :rules, accumulate: true | |
@before_compile unquote(__MODULE__) | |
end | |
end | |
def gather_rules(rules) do | |
rules |
This file contains hidden or 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
# file1.ex | |
defmodule A do | |
defmacro __using__(_env) do | |
quote do | |
Module.register_attribute __MODULE__, :rules, accumulate: true | |
end | |
end | |
defmacro register() do |
This file contains hidden or 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
defimpl Canada.Can, for: User do | |
def can?(%User{}, :show, %Post), do: true | |
end | |
defimpl Canada.Can, for: User do # this overwrites the first implementation rather than extending it | |
def can?(%User{}, :delete, %Post), do: false | |
end |