<commit_before># Name: <NAME>
# Section: G3

# lab3 (to be done in week 5)
# deadline: 7/Feb/2015 (Sat), 23:59hrs
require 'set'

def select_tweeters(followers)
  # return [0, 1, 2, 3, 4] # returns a silly solution for now
  result = []
  start_time = Time.now
  count = 0

  result_quality = 0
  # do grouping
  grouped_users = [];
  followers.each_with_index do |follower_list, userid|
    follower_qty = follower_list.length;

    list = grouped_users[follower_qty];
    if list.nil?
      list = []
      grouped_users[follower_qty] = list
    end

    list << userid
  end
  top_users = grouped_users.flatten.compact.reverse

  top_n = sqrt(top_users.length)
  top_n = top_users.length / 5 if top_n < 7   # 20%
  top_n = top_users.length / 2 if top_n < 7   # 50%
  top_n = top_users.length if top_n < 7       # 100%
  # top_n = 34
  top_users[0..top_n].combination(5) do |combi|
    count += 1

=begin
# set style
s = Set.new
combi.each do { |num| s.merge(followers[num]) }
s.subtract(combi)
=end

    # array style
    # obtain a list of ALL followers of the chosen users
    follower_list = []
    combi.each do |num|
      # use concat to avoid creating new arrays
      # or having to flatten the array later
      follower_list.concat(followers[num])
    end

    # remove dupes
    follower_list.uniq!

    # remove the candidate users
    combi.each do |num|
      follower_list.delete(num)
    end

    # p combi
    # puts "Size=" + follower_list.size.to_s + ", current result size=" + result_quality.to_s
    # puts

    if follower_list.size > result_quality
      result = combi
      result_quality = follower_list.size
    end

    if (Time.now - start_time) > 0.25
      break
    end
  end
  # puts "Count = " + count.to_s

=begin
  # naive brute force until 1.0 sec == 232 quality
  count = 0
  Array(0..followers.length-1).combination(5) do |combination|
    count += 1

    s = Set.new
    combination.each do |num|
      s.merge(followers[num])
    end
    s.subtract(combination)

    if s.size > result.length
      result = combination
      # puts "Size=" + s.size.to_s + ", " + result.to_s
    end

    if (Time.now - start_time) > 5.985
      break
    end
  end
  # puts "Total iterations: " + count.to_s
=end

=begin
  # random users
  result = []
  1.upto(5) do
    result << rand(followers.length)
  end


=begin
  # users with top 5 number of followers
  result = []

  # do grouping
  grouped_by_follower_qty = [];
  followers.each_with_index do |followers, userid|
    follower_qty = followers.length;

    hash = grouped_by_follower_qty[follower_qty];
    if hash == nil
      hash = {}
      grouped_by_follower_qty[follower_qty] = hash
    end

    hash[userid] = followers
  end

  # extract results
  grouped_by_follower_qty.reverse_each do |hash|
    hash.each do |userid, followers|
      result << userid unless result.length > 4
    end unless hash.nil?
  end
=end

  return result
end
<commit_msg>Cleanup<commit_after># Name: LEE <NAME>
# Section: G3

# lab3 (to be done in week 5)
# deadline: 7/Feb/2015 (Sat), 23:59hrs

def select_tweeters(followers)
  result = []
  start_time = Time.now
  count = 0
  result_quality = 0

  # do grouping
  grouped_users = [];
  followers.each_with_index do |follower_list, userid|
    follower_qty = follower_list.length;

    list = grouped_users[follower_qty];
    if list.nil?
      list = []
      grouped_users[follower_qty] = list
    end

    list << userid
  end
  top_users = grouped_users.flatten.compact.reverse

  # determine top-n users to inspect
  top_n = sqrt(top_users.length)
  top_n = top_users.length / 5 if top_n < 7   # 20%
  top_n = top_users.length / 2 if top_n < 7   # 50%
  top_n = top_users.length if top_n < 7       # 100%
  # top_n = 34
  top_users[0..top_n].combination(5) do |combi|
    count += 1


    # array style
    # obtain a list of ALL followers of the chosen users
    follower_list = []
    combi.each do |num|
      # use concat to avoid creating new arrays
      # or having to flatten the array later
      follower_list.concat(followers[num])
    end

    # remove dupes
    follower_list.uniq!

    # remove the candidate users
    combi.each do |num|
      follower_list.delete(num)
    end

    # p combi
    # puts "Size=" + follower_list.size.to_s + ", current result size=" + result_quality.to_s
    # puts

    # update tracker variables if necessary
    if follower_list.size > result_quality
      result = combi
      result_quality = follower_list.size
    end

    # if (Time.now - start_time) > 5.985 # 0.25
    #   break
    # end
  end

  # puts "Count = " + count.to_s

  return result
end

{'fraction_non_alphanumeric': 0.09494324045407637, 'fraction_numerical': 0.0196078431372549, 'mean_word_length': 2.24798927613941, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '150821', 'n_tokens_mistral': 1985, 'n_tokens_neox': 1655, 'n_words': 443}