This is the "Gemstones" problem on Hackerrank.
John has collected various rocks. Each rock has various minerals embeded in it. Each type of mineral is designated by a lowercase letter in the range
a-z. There may be multiple occurrences of a mineral in a rock. A mineral is called a gemstone if it occurs at least once in each of the rocks in John's collection. Given a list of minerals embedded in each of John's rocks, display the number of types of gemstones he has in his collection. For example, the array of mineral composition strings
[abc, abc, bc]The minerals b and c appear in each composite, so there are 2 gemstones.
This is my Python code:
def gemstones(arr): for i in range(len(arr)): arr[i] = "".join(set(arr[i])) long = max(arr, key=len) arrlen = len(arr) flag,count = 0,0 for i in long: for j in range(arrlen): if i not in arr[j]: flag = 1 if flag is 0: count += 1 flag = 0 return count
Are there ways to improve this code? I feel I'm not using the full functionality of Python.
A test case:
>>> arr = ['abcdde', 'baccd', 'eeabg'] >>> print(gemstones(arr)) 2