Created
June 3, 2011 02:22
-
-
Save captainpete/1005737 to your computer and use it in GitHub Desktop.
Tom's coding challenge. MiniTest suite
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
class Library | |
def self.leaf_paths_of(paths) | |
PathTree.new(paths).reduce | |
end | |
end | |
class PathTree < Hash | |
def initialize(paths = []) | |
paths.each { |path| self << path } | |
end | |
def << path | |
first, *rest = path.split '/' | |
self[first] ||= nil | |
(self[first] ||= PathTree.new) << rest.join('/') unless rest.empty? | |
end | |
def reduce | |
map do |key, val| | |
if val.respond_to? :reduce | |
val.reduce.map { |v| [key, v].join('/') } | |
else | |
[key] | |
end | |
end.flatten | |
end | |
end |
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
class Library | |
def self.leaf_paths_of(paths) | |
paths.inject([]) do |memo, path| | |
memo.reject! { |p| path.start_with?(p + '/') } | |
memo << path unless memo.any? { |p| p.start_with?(path + '/') } | |
end | |
end | |
end |
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
require 'minitest/autorun' | |
require './library' | |
describe Library do | |
describe "leaf paths of" do | |
it "returns a path, if it doesn't have a sub dir in the list" do | |
Library.leaf_paths_of(["a"]).must_equal ["a"] | |
end | |
it "keeps unrelated paths" do | |
Library.leaf_paths_of(["a", "b"]).must_equal ["a", "b"] | |
end | |
it "removes the base path if we have a subdir for it" do | |
Library.leaf_paths_of(["a", "a/aa"]).must_equal ["a/aa"] | |
end | |
it "keeps all sub dirs of the base path" do | |
Library.leaf_paths_of(["a", "a/aa", "a/ab", "a/ac"]).must_equal ["a/aa", "a/ab", "a/ac"] | |
end | |
it "doesn't touch unrelated paths if a base dir is removed" do | |
Library.leaf_paths_of(["a", "a/aa", "b"]).must_equal ["a/aa", "b"] | |
end | |
it "removes all base paths if they all have sub dirs" do | |
Library.leaf_paths_of(["a", "a/aa", "b", "b/bb"]).must_equal ["a/aa", "b/bb"] | |
end | |
it "makes sure we only get leaves" do | |
Library.leaf_paths_of(["a", "a/aa", "a/aa/aaa"]).must_equal ["a/aa/aaa"] | |
end | |
it "leaves similar named subdirs in another base path alone" do | |
Library.leaf_paths_of(["a", "a/aa", "b", "b/a", "b/aa"]).must_equal ["a/aa", "b/a", "b/aa"] | |
end | |
it "keeps similar named subdirs in another base path if nested further" do | |
Library.leaf_paths_of(["a", "a/aa", "a/aa/aaa", "b", "b/aa"]).must_equal ["a/aa/aaa", "b/aa"] | |
end | |
it "handles regular brackets in the path" do | |
Library.leaf_paths_of(["a", "a/aa (123)"]).must_equal ["a/aa (123)"] | |
end | |
it "handles square brackets in the path" do | |
Library.leaf_paths_of(["a", "a/aa [123]"]).must_equal ["a/aa [123]"] | |
end | |
it "handles square brackets with a dash in the path" do | |
Library.leaf_paths_of(["a", "a/aa [mp3-123-456]"]).must_equal ["a/aa [mp3-123-456]"] | |
end | |
end | |
end |
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
Loaded suite library_spec | |
Started | |
............ | |
Finished in 0.001337 seconds. | |
12 tests, 12 assertions, 0 failures, 0 errors, 0 skips |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment