-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3_30_hackerrank.rb
More file actions
40 lines (28 loc) · 952 Bytes
/
3_30_hackerrank.rb
File metadata and controls
40 lines (28 loc) · 952 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/bin/ruby
require 'json'
require 'stringio'
#
# Complete the 'groupTransactions' function below.
#
# The function is expected to return a STRING_ARRAY.
# The function accepts STRING_ARRAY transactions as parameter.
#dfdfdfdd
#sorted by: descending transaction count!
#then ascending by name!
# iterate through transactions, store in a hash, key is the item, val is num times appears
# sort transaction count desc
# sort by name asc
# print out in order with space in b/t key and val
def groupTransactions(transactions_arr)
# Write your code here
transaction_hash = Hash.new(0)
transactions_arr.each do |item|
transaction_hash[item] += 1
end
transaction_hash = transaction_hash.sort_by {|key,val| val}.reverse
transaction_hash = transaction_hash.sort_by {|k,v| k}
transaction_hash.each do |item_pair|
p "#{item_pair[0]} #{item_pair[1]}"
end
end
fptr = File.open(ENV['OUTPUT_PATH'], 'w')