Write a Python script named cluster_counter.py that takes command line arguments
ID: 3774569 • Letter: W
Question
Write a Python script named cluster_counter.py that takes command line arguments. The first argument will be a filename. The file loaded will contain an n by m matrix of different characters. This n by m matrix denotes different clusters where each same character represents a group. Casing is not important (i.e A and a are the same group). Touching characters (meaning adjacent and diagonal) of the same group are assumed to be in the same cluster. Write a program that tells the user how many groups exist and for each group, how many sub clusters exist.
---------------------------
Example file test.m
A B A A A
A B A A A
B B A A A
python cluster_counter.py test.m
You have 2 groups: A, B
The Number of clusters are listed below
A : 2
B : 1
Explanation / Answer
import string
lists = []
with open('prepare.in', 'r') as f:
x = f.readlines()
#b = [x for x in input.readlines().split(' ')]
#a = [x.split(' ') for x in b]
#b = [int(x) for x in input.readline().split(' ')]
#lists.append(a)
for i in x:
j = string.replace(i, ' ', '')
j = j.split(' ')
lists.extend(j)
#with open('prepare.out', 'w') as output:
# output.write(str(sum([max(a[i],b[i]) for i in range(len(a))])))
# output.write(' ')
s = "You have " + str(len(set(lists)))+ " groups:"
for i in set(lists):
s = s + " " + i
print s
print ("The number of clusters are listed")
j = 1
for i in set(lists):
print i,":",j
j=j+1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.