Петър обнови решението на 22.03.2015 17:11 (преди над 9 години)
+from collections import Counter
+import collections
+
+
+def extract_type(paramList, extractType):
+ resultString = ""
+ for i in paramList:
+ if extractType is type(i[0]):
+ count = i[1]
+ while count > 0:
+ resultString += i[0]
+ count -= 1
+ return resultString
+
+
+def reversed_dict(dataMap):
+ reversedCollection = dict()
+ for key, value in dataMap.items():
+ reversedCollection[value] = key
+ return reversedCollection
+
+
+def internal_flatten(hashMap, parentKey='', sep='.'):
+ result = []
+ for key, value in hashMap.items():
+ newKey = parentKey + sep + key if parentKey else key
+ if isinstance(value, collections.MutableMapping):
+ result.extend(internal_flatten(value, newKey, sep=sep).items())
+ else:
+ result.append((newKey, value))
+ return dict(result)
+
+
+def flatten_dict(hashMap):
+ return internal_flatten(hashMap)
+
+
+def internal_unflatten(hashMap):
+ result = dict()
+ for key, value in hashMap.items():
+ newKeys = key.split(".")
+ newMap = result
+ for newKey in newKeys[:-1]:
+ if newKey not in newMap:
+ newMap[newKey] = dict()
+ newMap = newMap[newKey]
+ newMap[newKeys[-1]] = value
+ return result
+
+
+def unflatten_dict(hashMap):
+ return internal_unflatten(hashMap)
+
+
+def reps(coll):
+ resultCollection = list()
+ repValues = [k for k, v in Counter(coll).items() if v > 1]
+ for item in coll:
+ for repVal in repValues:
+ if item == repVal:
+ resultCollection.append(item)
+ return tuple(resultCollection)