Skip to content

Instantly share code, notes, and snippets.

@rinx
Created February 6, 2015 17:18
Show Gist options
  • Save rinx/9f146cd6c8bc8b2f3fae to your computer and use it in GitHub Desktop.
Save rinx/9f146cd6c8bc8b2f3fae to your computer and use it in GitHub Desktop.
sort num and values
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
=begin
if input is below...
>>>
AAAA
30.3
BBBB
502.0
CCCC
40.8
DDDD
3.1
EEEE
80.2
FFF
22.0
GGG
30.4
HHH
30.2
<<<
the output will be...
>>>
3.1 DDDD
22.0 FFF
30.2 HHH
30.3 AAAA
30.4 GGG
40.8 CCCC
80.2 EEEE
502.0 BBBB
<<<
=end
nums = [] #initialize array
pairs = {} #initialize hash
i = 1
while line = STDIN.gets
if line =~ /^$/ then
break
end
if i % 2 == 1 then
str = line
else
num = line
#contain values
nums << num.to_f
pairs.store(num.to_f.to_s.chomp, str.chomp)
end
i += 1
end
nums.sort!
nums.each do |n|
puts "#{n} #{pairs[n.to_s]}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment