Решение на Пет малки функции от Александър Наджарян

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

Към профила на Александър Наджарян

Резултати

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

Код

def extract_type(symbols, wanted):
result = []
for charater, repetitions in symbols:
if type(charater) is wanted:
result.append(str(charater) * repetitions)
return ''.join(result)
def reversed_dict(original):
return {original[key]: key for key in original}
def flatten_dict(original, previous_key=None):
flattened = {}
for key, value in original.items():
if previous_key:
key = '.'.join([previous_key, key])
if isinstance(value, dict):
flattened.update(flatten_dict(value, key))
else:
flattened[key] = value
return flattened
def unflatten_dict(original):
result = {}
for key, value in original.items():
parts = key.split('.')
unflattened = result
for part in parts[:-1]:
if part not in unflattened:
unflattened[part] = {}
unflattened = unflattened[part]
unflattened[parts[-1]] = value
return result
def reps(elements):
return tuple(filter(lambda x: elements.count(x) > 1, elements))

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

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

OK

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

Александър обнови решението на 18.03.2015 15:38 (преди над 9 години)

+def extract_type(symbols, wanted):
+ return ''.join(str(x)*y for x, y in symbols if type(x) == wanted)
+
+
+def reversed_dict(original):
+ return {original[key]: key for key in original}
+
+
+def flatten_dict(original, previous_key=None):
+ flattend = {}
+
+ for key, value in original.items():
+ if previous_key:
+ key = '.'.join([previous_key, key])
+
+ if isinstance(value, dict):
+ flattend.update(flatten_dict(value, key))
+
+ else:
+ flattend[key] = value
+
+ return flattend
+
+
+def unflatten_dict(original):
+ result = {}
+
+ for key, value in original.items():
+ parts = key.split('.')
+ unflattend = result
+
+ for part in parts[:-1]:
+ if part not in unflattend:
+ unflattend[part] = {}
+ unflattend = unflattend[part]
+
+ unflattend[parts[-1]] = value
+
+ return result
+
+
+def reps(elements):
+ return tuple(filter(lambda x: elements.count(x) > 1, elements))

Александър обнови решението на 19.03.2015 00:44 (преди над 9 години)

def extract_type(symbols, wanted):
- return ''.join(str(x)*y for x, y in symbols if type(x) == wanted)
+ return ''.join(str(x)*y for x, y in symbols if type(x) is wanted)
def reversed_dict(original):
return {original[key]: key for key in original}
def flatten_dict(original, previous_key=None):
flattend = {}
for key, value in original.items():
if previous_key:
key = '.'.join([previous_key, key])
if isinstance(value, dict):
flattend.update(flatten_dict(value, key))
else:
flattend[key] = value
return flattend
def unflatten_dict(original):
result = {}
for key, value in original.items():
parts = key.split('.')
unflattend = result
for part in parts[:-1]:
if part not in unflattend:
unflattend[part] = {}
unflattend = unflattend[part]
unflattend[parts[-1]] = value
return result
def reps(elements):
return tuple(filter(lambda x: elements.count(x) > 1, elements))

Александър обнови решението на 23.03.2015 16:51 (преди над 9 години)

def extract_type(symbols, wanted):
- return ''.join(str(x)*y for x, y in symbols if type(x) is wanted)
+ result = []
+ for charater, repetitions in symbols:
+ if type(charater) is wanted:
+ result.append(str(charater)*repetitions)
+ return ''.join(result)
def reversed_dict(original):
return {original[key]: key for key in original}
def flatten_dict(original, previous_key=None):
- flattend = {}
+ flattened = {}
for key, value in original.items():
if previous_key:
key = '.'.join([previous_key, key])
if isinstance(value, dict):
- flattend.update(flatten_dict(value, key))
+ flattened.update(flatten_dict(value, key))
else:
- flattend[key] = value
+ flattened[key] = value
- return flattend
+ return flattened
def unflatten_dict(original):
result = {}
for key, value in original.items():
parts = key.split('.')
- unflattend = result
+ unflattened = result
for part in parts[:-1]:
- if part not in unflattend:
- unflattend[part] = {}
- unflattend = unflattend[part]
+ if part not in unflattened:
+ unflattened[part] = {}
+ unflattened = unflattened[part]
- unflattend[parts[-1]] = value
+ unflattened[parts[-1]] = value
return result
def reps(elements):
return tuple(filter(lambda x: elements.count(x) > 1, elements))

Александър обнови решението на 23.03.2015 16:52 (преди над 9 години)

def extract_type(symbols, wanted):
result = []
for charater, repetitions in symbols:
if type(charater) is wanted:
- result.append(str(charater)*repetitions)
+ result.append(str(charater) * repetitions)
return ''.join(result)
def reversed_dict(original):
return {original[key]: key for key in original}
def flatten_dict(original, previous_key=None):
flattened = {}
for key, value in original.items():
if previous_key:
key = '.'.join([previous_key, key])
if isinstance(value, dict):
flattened.update(flatten_dict(value, key))
else:
flattened[key] = value
return flattened
def unflatten_dict(original):
result = {}
for key, value in original.items():
parts = key.split('.')
unflattened = result
for part in parts[:-1]:
if part not in unflattened:
unflattened[part] = {}
unflattened = unflattened[part]
unflattened[parts[-1]] = value
return result
def reps(elements):
return tuple(filter(lambda x: elements.count(x) > 1, elements))