Решение на Пет малки функции от Петър Гетов

Обратно към всички решения

Към профила на Петър Гетов

Резултати

  • 10 точки от тестове
  • 1 отнета точка
  • 9 точки общо
  • 19 успешни тест(а)
  • 0 неуспешни тест(а)

Код

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 unflatten_dict(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 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)

Лог от изпълнението

...................
----------------------------------------------------------------------
Ran 19 tests in 0.171s

OK

История (2 версии и 1 коментар)

Петър обнови решението на 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)

Петър обнови решението на 22.03.2015 17:46 (преди над 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):
+def unflatten_dict(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)