Ruby

Ruby code posted by Maksym
created at 27 Oct 19:29, updated at 05 Nov 02:59

Edit | Back
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
filename = ARGV.first
contents = File.read(filename)

participant_maps = {} # relation of equivalent participants
participant_trans = {} # relations of transactions in which each participant was involved
counter = 1
contents.each do |line|
  line = line[2..line.length]
  line.split(',').each do |participant|
    if participant.include? '='
      left = participant.trim.first
      right = participant.trim.last
      # initializes left participant's equivalence and transaction map
      participant_maps[left] = [] if participant_maps[left].nil?
      participant_trans[left] = [] if participant_trans[left].nil?
      add_participant(participant_maps[left], participant_trans[left], counter, right)
      # initializes right participant's equivalence and transaction map
      participant_maps[right] = [] if participant_maps[right].nil?
      participant_trans[right] = [] if participant_trans[left].nil?
      add_participant(participant_maps[right], participant_trans[right], counter, left)
    else
      # initializes the single participant's equivalence and transaction map
      participant_maps[participant] = [] if participant_maps[participant].nil?
      participant_trans[participant] = [] if participant_trans[participant].nil?
      participant_trans[participant] << counter
    end
  end

  counter += 1
end

def add_participant(participant_map, participant_trans, trans_no, eq_participant)
    participant_trans << trans_no
    participant_map << eq_participant
    participant_map.uniq!
end

search_term = ""
do
  puts "Entering quit! will quit the application"
  puts "Enter the participant you wish to search_term for: "
  search_term = gets
  unless search_term.eq? "quit!"
    # ensures searched participant was found in the journal
    unless participant_maps[search_term].nil?
      results = []
      search_terms = []
      # gets equivalent participants, including current searched for participant
      search_terms << participant_maps[search_term]
      search_terms << search_term
      # gets all the transactions in which the participant and equivalent participants
      # were involved
      search_terms.each { |participant|
        results << participant_trans[participant]
      }

      print "Transactions: "
      puts results.uniq.to_s
    else
      puts "Participant #{search_term} is not a known participant"
    end
  end
end until search == "quit!"
2.4 KB in 4 ms with coderay