Last active
April 25, 2018 13:10
-
-
Save makoto/718496dfc91d11b282e31e09a66c1f2f to your computer and use it in GitHub Desktop.
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
a = ['a', 'c', 'd', ''] | |
b = ['b', 'c', 'd', ''] | |
compare(a,a) # 0 | |
compare(a,b) # -1 | |
compare(b,a) # 1 | |
a = ['a', 'c', 'd', ''] | |
b = ['c', 'd', ''] | |
compare(a,b) # -1 | |
a = ['c', 'd', ''] | |
b = ['a','c', 'd', ''] | |
compare(a,b) # -1 | |
def compare_equal(a,b) | |
return 0 if a == [''] && b == [''] | |
a_head = a[0] | |
a_tail = a[1..-1] | |
b_head = b[0] | |
b_tail = b[1..-1] | |
result = compare_equal(a_tail, b_tail) | |
return result if result != 0 | |
if result == 0 | |
a_head <=> b_head | |
end | |
end | |
def compare(a,b) | |
length = [a.length, b.length].min | |
a_tail_start = a.length - length | |
b_tail_start = b.length - length | |
a_tail = a[a_tail_start..-1] | |
b_tail = b[b_tail_start..-1] | |
result = compare_equal(a_tail, b_tail) | |
# a and b are in the same domain level | |
return result if result != 0 | |
# a is sub domain of the b | |
return -1 if a_tail_start != 0 || b_tail_start != 0 | |
# a and b are exactly the same | |
return 0 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment