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

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

Към профила на Валентин Латунов

Резултати

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

Код

def extract_type(vectors, type_):
extracted = [''.join([str(vector[0]) for i in range(vector[1])])
for vector in vectors if type(vector[0]) is type_]
return ''.join(extracted)
def reversed_dict(dictionary):
return {dictionary[x]: x for x in dictionary}
def flatten_dict(dictionary, subname=''):
result = {}
for key in dictionary:
if type(dictionary[key]) is not dict:
result[subname + key] = dictionary[key]
else:
result.update(flatten_dict(dictionary[key], subname + key + '.'))
return result
def unflatten_dict(dictionary):
result = {}
for key in dictionary:
key_part = key.partition('.')
if key_part[2]:
if key_part[0] not in result.keys():
result[key_part[0]] = {}
if type(result[key_part[0]]) is dict:
result[key_part[0]][key_part[2]] = dictionary[key]
else:
result[key] = dictionary[key]
for key in result:
if type(result[key]) is dict:
result[key] = unflatten_dict(result[key])
return result
def reps(collection):
return tuple([x for x in collection if collection.count(x) > 1])

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.186s

OK

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

Валентин обнови решението на 17.03.2015 21:39 (преди около 9 години)

+def extract_type(vectors, type_):
+ result = ''
+ for vector in vectors:
+ if type(vector[0]) == type_:
+ i = 0
+ while i < vector[1]:
+ i += 1
+ result += str(vector[0])
+ return result
+
+
+def reversed_dict(dictionary):
+ return {dictionary[x]: x for x in dictionary}
+
+
+def flatten_dict(dictionary, subname=''):
+ result = {}
+ for key in dictionary:
+ if type(dictionary[key]) != dict:
+ result[subname + key] = dictionary[key]
+ else:
+ result.update(flatten_dict(dictionary[key], subname + key + '.'))
+ return result
+
+
+def unflatten_dict(dictionary):
+ result = {}
+ for key in dictionary:
+ key_part = key.partition('.')
+ if key_part[2]:
+ if key_part[0] not in result.keys():
+ result[key_part[0]] = {}
+ if type(result[key_part[0]]) == dict:
+ result[key_part[0]][key_part[2]] = dictionary[key]
+ else:
+ result[key] = dictionary[key]
+ for key in result:
+ if type(result[key]) == dict:
+ result[key] = unflatten_dict(result[key])
+ return result
+
+
+def reps(collection):
+ return tuple([x for x in collection if collection.count(x) > 1])

Валентин обнови решението на 21.03.2015 22:55 (преди около 9 години)

def extract_type(vectors, type_):
- result = ''
- for vector in vectors:
- if type(vector[0]) == type_:
- i = 0
- while i < vector[1]:
- i += 1
- result += str(vector[0])
- return result
+ extracted = [''.join([str(vector[0]) for i in range(vector[1])])
+ for vector in vectors if type(vector[0]) is type_]
+ return ''.join(extracted)
def reversed_dict(dictionary):
return {dictionary[x]: x for x in dictionary}
def flatten_dict(dictionary, subname=''):
result = {}
for key in dictionary:
- if type(dictionary[key]) != dict:
+ if type(dictionary[key]) is not dict:
result[subname + key] = dictionary[key]
else:
result.update(flatten_dict(dictionary[key], subname + key + '.'))
return result
def unflatten_dict(dictionary):
result = {}
for key in dictionary:
key_part = key.partition('.')
if key_part[2]:
if key_part[0] not in result.keys():
result[key_part[0]] = {}
- if type(result[key_part[0]]) == dict:
+ if type(result[key_part[0]]) is dict:
result[key_part[0]][key_part[2]] = dictionary[key]
else:
result[key] = dictionary[key]
for key in result:
- if type(result[key]) == dict:
+ if type(result[key]) is dict:
result[key] = unflatten_dict(result[key])
return result
def reps(collection):
return tuple([x for x in collection if collection.count(x) > 1])

Валентин обнови решението на 23.03.2015 16:36 (преди около 9 години)

def extract_type(vectors, type_):
extracted = [''.join([str(vector[0]) for i in range(vector[1])])
- for vector in vectors if type(vector[0]) is type_]
+ for vector in vectors if type(vector[0]) is type_]
return ''.join(extracted)
def reversed_dict(dictionary):
return {dictionary[x]: x for x in dictionary}
def flatten_dict(dictionary, subname=''):
result = {}
for key in dictionary:
if type(dictionary[key]) is not dict:
result[subname + key] = dictionary[key]
else:
result.update(flatten_dict(dictionary[key], subname + key + '.'))
return result
def unflatten_dict(dictionary):
result = {}
for key in dictionary:
key_part = key.partition('.')
if key_part[2]:
if key_part[0] not in result.keys():
result[key_part[0]] = {}
if type(result[key_part[0]]) is dict:
result[key_part[0]][key_part[2]] = dictionary[key]
else:
result[key] = dictionary[key]
for key in result:
if type(result[key]) is dict:
result[key] = unflatten_dict(result[key])
return result
def reps(collection):
return tuple([x for x in collection if collection.count(x) > 1])