forked from bmanandhar/python-hacker-rank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaxNumGroups.py
40 lines (34 loc) · 1.3 KB
/
maxNumGroups.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Dec 5 17:43:43 2019
@author: bijayamanandhar
"""
def maximumGroups(complianceFactors):
# Write your code here
# max number of groups, initial value zero
maxNumOfGroups = 0
# checking if the element has been already been counted
tempList = []
# iterates all elements except the last
for i in range(len(complianceFactors) - 1):
# counter for each element
tempTotal = 0
# if the element has not already been counted
if complianceFactors[i] not in tempList:
# keeps all elements for ref once counted
tempList.append(complianceFactors[i])
# adds 1 to counter if present
tempTotal += 1
# generates another list for couting the element considered
listToCount = complianceFactors[i+1:]
# checking the new list if the element is present
for j in range(len(listToCount)):
# when considered element is found
if listToCount[j] == complianceFactors[i]:
# adds 1 to counter
tempTotal += 1
# adds the num of group
maxNumOfGroups += tempTotal // 2
return maxNumOfGroups
print(maximumGroups([4,5,2,2,4,4,3,3]) == 3) # True