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

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

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

Резултати

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

Код

from collections import Counter
def extract_type(symbols, only_type):
extracted = ""
for item in symbols:
if type(item[0]) == only_type:
for i in range(item[1]):
extracted += str(item[0])
return extracted
def reversed_dict(dictionary):
reversed = {}
for key in dictionary:
if dictionary[key] not in reversed:
reversed[dictionary[key]] = key
return reversed
def reps(numbers):
reps_only = ()
occurences = Counter(numbers)
for item in numbers:
if occurences[item] > 1:
reps_only += (item,)
return reps_only
def flatten_dict(nested, parent_key='', separator='.'):
unnested = {}
for item in nested:
key = item
value = nested[item]
if parent_key:
new_key = parent_key + separator + key
else:
new_key = key
if isinstance(value, dict):
unnested.update(flatten_dict(value, new_key))
else:
unnested[new_key] = value
return unnested
def unflatten_dict(unnested):
nested = {}
for item in unnested:
key = item
value = unnested[item]
parts = key.split('.')
separated = nested
for part in parts[:-1]:
if part not in separated:
separated[part] = dict()
separated = separated[part]
separated[parts[-1]] = value
return nested

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.167s

OK

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

Светлин обнови решението на 18.03.2015 02:28 (преди над 9 години)

+from collections import Counter
+
+
+def extract_type(symbols, only_type):
+ extracted = ""
+ for item in symbols:
+ if type(item[0]) == only_type:
+ for i in range(item[1]):
+ extracted += item[0]
+ return extracted
+
+
+def reversed_dict(dictionary):
+ reversed = {}
+ for key in dictionary:
+ if dictionary[key] not in reversed:
+ reversed[dictionary[key]] = key
+ return reversed
+
+
+def reps(numbers):
+ reps_only = ()
+ occurences = Counter(numbers)
+ for item in numbers:
+ if occurences[item] > 1:
+ reps_only += (item,)
+ return reps_only
+
+
+def flatten_dict(nested, parent_key='', separator='.'):
+ unnested = {}
+ for item in nested:
+ key = item
+ value = nested[item]
+
+ if parent_key:
+ new_key = parent_key + separator + key
+ else:
+ new_key = key
+ if isinstance(value, dict):
+ unnested.update(flatten_dict(value, new_key))
+ else:
+ unnested[new_key] = value
+ return unnested
+
+
+def unflatten_dict(unnested):
+ nested = {}
+ for item in unnested:
+ key = item
+ value = unnested[item]
+ parts = key.split('.')
+ separated = nested
+ for part in parts[:-1]:
+ if part not in separated:
+ separated[part] = dict()
+ separated = separated[part]
+ separated[parts[-1]] = value
+ return nested

Светлин обнови решението на 22.03.2015 01:44 (преди над 9 години)

from collections import Counter
def extract_type(symbols, only_type):
extracted = ""
for item in symbols:
if type(item[0]) == only_type:
for i in range(item[1]):
- extracted += item[0]
+ extracted += str(item[0])
return extracted
def reversed_dict(dictionary):
reversed = {}
for key in dictionary:
if dictionary[key] not in reversed:
reversed[dictionary[key]] = key
return reversed
def reps(numbers):
reps_only = ()
occurences = Counter(numbers)
for item in numbers:
if occurences[item] > 1:
reps_only += (item,)
return reps_only
def flatten_dict(nested, parent_key='', separator='.'):
unnested = {}
for item in nested:
key = item
value = nested[item]
if parent_key:
new_key = parent_key + separator + key
else:
new_key = key
if isinstance(value, dict):
unnested.update(flatten_dict(value, new_key))
else:
unnested[new_key] = value
return unnested
def unflatten_dict(unnested):
nested = {}
for item in unnested:
key = item
value = unnested[item]
parts = key.split('.')
separated = nested
for part in parts[:-1]:
if part not in separated:
separated[part] = dict()
separated = separated[part]
separated[parts[-1]] = value
- return nested
+ return nested