Update all new Tasks
This commit is contained in:
parent
00a190b0a6
commit
91df62d461
5697 changed files with 93386 additions and 804 deletions
1
Task/Natural-sorting/Ruby/natural-sorting-1.rb
Normal file
1
Task/Natural-sorting/Ruby/natural-sorting-1.rb
Normal file
|
|
@ -0,0 +1 @@
|
|||
ar.sort_by{|str| str.downcase.gsub(/\Athe |\Aa |\Aan /, "").lstrip.gsub(/\s+/, " ")}
|
||||
24
Task/Natural-sorting/Ruby/natural-sorting-2.rb
Normal file
24
Task/Natural-sorting/Ruby/natural-sorting-2.rb
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
class NatSortString
|
||||
include Comparable
|
||||
attr_reader :scrubbed, :ints_and_strings, :i_s_pattern
|
||||
|
||||
def initialize(str)
|
||||
@str = str
|
||||
@scrubbed = str.downcase.gsub(/\Athe |\Aa |\Aan /, "").lstrip.gsub(/\s+/, " ")
|
||||
@ints_and_strings = @scrubbed.scan(/\d+|\D+/).map{|s| s =~ /\d/ ? s.to_i : s}
|
||||
@i_s_pattern = @ints_and_strings.map{|el| el.is_a?(Integer) ? :i : :s}.join
|
||||
end
|
||||
|
||||
def <=> (other)
|
||||
if i_s_pattern.start_with?(other.i_s_pattern) or other.i_s_pattern.start_with?(i_s_pattern) then
|
||||
ints_and_strings <=> other.ints_and_strings
|
||||
else
|
||||
scrubbed <=> other.scrubbed
|
||||
end
|
||||
end
|
||||
|
||||
def to_s
|
||||
@str.dup
|
||||
end
|
||||
|
||||
end
|
||||
18
Task/Natural-sorting/Ruby/natural-sorting-3.rb
Normal file
18
Task/Natural-sorting/Ruby/natural-sorting-3.rb
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
tests =
|
||||
{"Ignoring leading spaces" =>
|
||||
[ "ignore leading spaces: 2-2 ", " ignore leading spaces: 2-1 ", " ignore leading spaces: 2+0 ", " ignore leading spaces: 2+1 "],
|
||||
"Ignoring multiple adjacent spaces" =>
|
||||
[ "ignore m.a.s spaces: 2-2 ", "ignore m.a.s spaces: 2-1 ", "ignore m.a.s spaces: 2+0 ", "ignore m.a.s spaces: 2+1 "],
|
||||
"Equivalent whitespace characters" =>
|
||||
["Equiv. spaces: 3-3", "Equiv.\rspaces: 3-2", "Equiv.\x0cspaces: 3-1", "Equiv.\x0bspaces: 3+0", "Equiv.\nspaces: 3+1", "Equiv.\tspaces: 3+2"],
|
||||
"Case Indepenent sort" =>
|
||||
[ "cASE INDEPENENT: 3-2 ", "caSE INDEPENENT: 3-1 ", "casE INDEPENENT: 3+0 ", "case INDEPENENT: 3+1 "],
|
||||
"Numeric fields as numerics" =>
|
||||
[ "foo100bar99baz0.txt ", "foo100bar10baz0.txt ", "foo1000bar99baz10.txt ", "foo1000bar99baz9.txt "],
|
||||
"Title sorts" =>
|
||||
[ "The Wind in the Willows ", "The 40th step more ", "The 39 steps ", "Wanda "]}
|
||||
|
||||
tests.each do |title, ar|
|
||||
nat_sorts = ar.map{|s| NatSortString.new(s)}
|
||||
puts [title,"--input--", ar, "--normal sort--", ar.sort, "--natural sort--", nat_sorts.sort, "\n"]
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue