Title / Description
Code 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!"
Author
Highlight as C C++ CSS Clojure Delphi ERb Groovy (beta) HAML HTML JSON Java JavaScript PHP Plain text Python Ruby SQL XML YAML diff code