-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgemStones.py
More file actions
82 lines (59 loc) · 2.03 KB
/
gemStones.py
File metadata and controls
82 lines (59 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""
John has discovered various rocks. Each rock is composed of various elements, and each element is represented by a lowercase latin letter from 'a' to 'z'. An element can be present multiple times in a rock. An element is called a 'gem-element' if it occurs at least once in each of the rocks.
Given the list of rocks with their compositions, display the number of gem-elements that exist in those rocks.
Input Format
The first line consists of N, the number of rocks.
Each of the next N lines contain rocks' composition. Each composition consists of lowercase letters of English alphabet.
Output Format
Print the number gem-elements that exist in those rocks.
Constraints
1 ≤ N ≤ 100
Each composition consists of only small latin letters ('a'-'z').
1 ≤ Length of each composition ≤ 100
Sample Input
3
abcdde
baccd
eeabg
Sample Output
2
Explanation
Only "a", "b" are the two kind of gem-elements, since these are the only characters that occur in each of the rocks' composition.
"""
def gemCheck(letter, rocks):
"""
Determines if rock is a gem, as per above instructions
Input:
letter: letter from the longest string
rocks: array of strings representing rocks
Output:
True if letter is present in each string in rocks
"""
ltrRep = 0
for rock in rocks:
if letter in rock:
ltrRep += 1
if ltrRep / len(rocks) == 1:
return True
n = int(raw_input())
rocks = []
gems = 0
# Input
for i in range(int(n)):
rocks.append(raw_input().rstrip())
longest = 0
# find the longest string in array of rocks
for j in range(int(n)):
if len(rocks[j]) > longest:
longest = j
longestGem = "".join(rocks[longest])
# dictionary to track duplicate letters
duplicates = {'a':0, 'b':0, 'c':0, 'd':0, 'e':0,'f':0,'g':0,'h':0,'i':0,'j':0,'k':0,'l':0,'m':0,'n':0,'o':0,'p':0,'q':0,'r':0,'s':0,'t':0,'u':0,'v':0,'w':0,'x':0,'y':0,'z':0}
for letter in longestGem:
#check if the letter has already been checked
if duplicates[letter] == 0:
duplicates[letter] = 1
# call gemCheck function
if gemCheck(letter, rocks):
gems += 1
print gems