Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. require 'highline'
  2. require_relative 'union_find'
  3.  
  4. def ask_for_int(cli, question)
  5. cli.ask(question, ->(answer) { Integer(answer) if answer =~ /^\d+$/ })
  6. end
  7.  
  8. def init_connection_from(cli, max_nodes)
  9. %w{from to}.map do |direction|
  10. cli.say(direction.humanize)
  11. cli.choose do |menu|
  12. menu.choices(*(0..max_nodes - 1)) { |node| node }
  13. end
  14. end
  15. end
  16.  
  17. def init_from(cli)
  18. size = ask_for_int(cli, 'How many nodes in the network?')
  19. connections = []
  20.  
  21. loop do
  22. cli.say('New connection:')
  23. nodes = init_connection_from(cli, length)
  24. connections << nodes
  25. break if cli.agree('Finish?')
  26. end
  27.  
  28. { size: size, connections: connections }
  29. end
  30.  
  31. cli = HighLine.new
  32. user_input = init_from(cli)
  33.  
  34. union_find = UnionFind::UnionFind.new user_input.size
  35. union_find.init_connections user_input.connections
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement