Permalink
Browse files

Added a new Ruby solutions based on the original but clearer and clea…

…ned up
  • Loading branch information...
1 parent 3c2e64e commit 3ac4fcd037000ab9874c8fe43d6520de400d0913 Ben committed Jan 24, 2010
Showing with 19 additions and 28 deletions.
  1. +9 −0 ruby_ben/README
  2. +10 −28 ruby_ben/solution.rb
View
@@ -0,0 +1,9 @@
+Consolidated solution based on original winning Ruby script
+
+I like how each line performs one of the original requirements
+But I don't like how the file operations are on different lines
+and how I have to deal with the header line (range up front and adding it back onto the front)
+
+Also, the two layer sorting could be clearer w/ multi lines
+
+If anyone can improve and make more clear, please let me know
View
@@ -1,31 +1,13 @@
-output = ""
-output2 = []
+lines = []
-File.open("../input.txt") do |file|
- header = file.gets
-
- output += header
-
- while(lines = file.gets)
- fields = lines.split("\t")
-
- fields[1] = fields[1].gsub(/[^a-zA-Z]/, '').reverse
-
- output2 << fields if(!fields[1].empty?)
- end
-end
+File.open('../input.txt').each { |line| lines << line }
-output2 = output2.sort do |x, y|
- first = x[1] <=> y[1]
+lines = lines[1..lines.size].
+ map { |line| line.split("\t") }.
+ each { |line| line[1] = line[1].gsub(/[^a-zA-Z]/, '').reverse }.
+ reject { |line| line[1].to_s.empty? }.
+ sort { |x, y| (x[1] <=> y[1]) == 0 ? (x[5].to_f <=> y[5].to_f) : (x[1] <=> y[1]) }.
+ collect { |line| line.join("\t") }.
+ insert(0, lines[0])
- if first == 0
- first = x[5].to_f <=> y[5].to_f
- end
-
- first
-end
-
-
-output2.each { |line| output += line.join("\t") }
-
-File.open("output.txt", 'w') { |f| f.write(output) }
+File.open("output.txt", 'w') { |f| f.write(lines) }

0 comments on commit 3ac4fcd

Please sign in to comment.