Skip to main content
Code Review

Return to Answer

Better explanation
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 478

You should takeYour code is full of variables named somethingSet — but they aren't actually sets! Why not?

This solution, which takes advantage of Python's built-in set operations, is shorter. Just being able to write have_nots = all_centers - haves is worth it.

from collections import defaultdict
num_centers = int(raw_input('Number of data centers: '))
print("Input data set information as: x y z")
all_centers = set(xrange(1, num_centers + 1))
centers_with_data = defaultdict(set)
# Grab dataset ID information from stdin
for center in range(1, num_centers + 1):
 for data_set in map(int, raw_input("Data set %s: " % (center)).split()):
 centers_with_data[data_set].add(center)
print "One possible solution:\n"
for data_set, haves in centers_with_data.iteritems():
 have_nots = all_centers - haves
 donor = next(iter(haves)) # Pick an arbitrary source
 for acceptor in have_nots:
 print "%d %d %d" % (data_set, donor, acceptor)

You should take advantage of Python's built-in set operations.

from collections import defaultdict
num_centers = int(raw_input('Number of data centers: '))
print("Input data set information as: x y z")
all_centers = set(xrange(1, num_centers + 1))
centers_with_data = defaultdict(set)
# Grab dataset ID information from stdin
for center in range(1, num_centers + 1):
 for data_set in map(int, raw_input("Data set %s: " % (center)).split()):
 centers_with_data[data_set].add(center)
print "One possible solution:\n"
for data_set, haves in centers_with_data.iteritems():
 have_nots = all_centers - haves
 donor = next(iter(haves)) # Pick an arbitrary source
 for acceptor in have_nots:
 print "%d %d %d" % (data_set, donor, acceptor)

Your code is full of variables named somethingSet — but they aren't actually sets! Why not?

This solution, which takes advantage of Python's built-in set operations, is shorter. Just being able to write have_nots = all_centers - haves is worth it.

from collections import defaultdict
num_centers = int(raw_input('Number of data centers: '))
print("Input data set information as: x y z")
all_centers = set(xrange(1, num_centers + 1))
centers_with_data = defaultdict(set)
# Grab dataset ID information from stdin
for center in range(1, num_centers + 1):
 for data_set in map(int, raw_input("Data set %s: " % (center)).split()):
 centers_with_data[data_set].add(center)
print "One possible solution:\n"
for data_set, haves in centers_with_data.iteritems():
 have_nots = all_centers - haves
 donor = next(iter(haves)) # Pick an arbitrary source
 for acceptor in have_nots:
 print "%d %d %d" % (data_set, donor, acceptor)
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 478

You should take advantage of Python's built-in set operations.

from collections import defaultdict
num_centers = int(raw_input('Number of data centers: '))
print("Input data set information as: x y z")
all_centers = set(xrange(1, num_centers + 1))
centers_with_data = defaultdict(set)
# Grab dataset ID information from stdin
for center in range(1, num_centers + 1):
 for data_set in map(int, raw_input("Data set %s: " % (center)).split()):
 centers_with_data[data_set].add(center)
print "One possible solution:\n"
for data_set, haves in centers_with_data.iteritems():
 have_nots = all_centers - haves
 donor = next(iter(haves)) # Pick an arbitrary source
 for acceptor in have_nots:
 print "%d %d %d" % (data_set, donor, acceptor)
lang-py

AltStyle によって変換されたページ (->オリジナル) /