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

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

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

Резултати

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

Код

def extract_type(list, type):
result = ""
for item in list:
if isinstance(item[0], type):
result += str(item[0]) * item[1]
return result
def reversed_dict(dictionary):
reversed_dict = {}
for item in dictionary:
reversed_dict[dictionary[item]] = item
return reversed_dict
def flatten_dict(dictionary, parent_key=''):
flatten_dictionary = []
for key, value in dictionary.items():
new_key = key
if parent_key:
new_key = parent_key + '.' + key
if isinstance(value, dict):
flatten_dictionary.extend(flatten_dict(value, new_key).items())
else:
flatten_dictionary.append((new_key, value))
return dict(flatten_dictionary)
def unflatten_dict(dictionary):
unflatten_dictionary = {}
for key, value in dictionary.items():
currentKey = key.split('.')
temp_dictionary = unflatten_dictionary
for item in currentKey[:-1]:
if item not in temp_dictionary:
temp_dictionary[item] = {}
temp_dictionary = temp_dictionary[item]
temp_dictionary[currentKey[-1]] = value
return unflatten_dictionary
def reps(array):
rep_array = []
for item in array:
if array.count(item) > 1:
rep_array.append(item)
return tuple(rep_array)

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.134s

OK

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

Гален обнови решението на 19.03.2015 23:17 (преди над 9 години)

+def extract_type ( list, type ):
+ result = ""
+
+ for item in list :
+ if isinstance ( item[0], type ):
+ result += str ( item[0] ) * item[1]
+
+ return result
+
+def reversed_dict ( dictionary ):
+ reversedDict = {}
+
+ for item in dictionary :
+ reversedDict[ dictionary[item] ] = item
+
+ return reversedDict
+
+def flatten ( dictionary, parentKey = '' ):
+ flattenDictionary = []
+
+ for key, value in dictionary.items():
+ newKey = key
+ if ( parentKey ):
+ newKey = parentKey + '.' + key
+
+ if isinstance ( value, dict ):
+ flattenDictionary.extend ( flatten ( value, newKey ).items() )
+ else:
+ flattenDictionary.append ( ( newKey, value ) )
+
+ return dict ( flattenDictionary )
+
+def unflatten_dict ( dictionary ):
+ unflattenDictionary = {}
+
+ for key, value in dictionary.items():
+ currentKey = key.split ( '.' )
+ tempDictionary = unflattenDictionary
+
+ for item in currentKey[:-1]:
+ if item not in tempDictionary:
+ tempDictionary[item] = {}
+ tempDictionary = tempDictionary[item]
+
+ tempDictionary[ currentKey[-1] ] = value
+
+ return unflattenDictionary
+
+def reps ( array ):
+ repArray = []
+ for item in array:
+ if array.count ( item ) > 1:
+ repArray.append ( item )
+
+ return repArray

Гален обнови решението на 21.03.2015 17:41 (преди над 9 години)

-def extract_type ( list, type ):
- result = ""
+def extract_type(list, type):
+ result = ""
- for item in list :
- if isinstance ( item[0], type ):
- result += str ( item[0] ) * item[1]
+ for item in list:
+ if isinstance(item[0], type):
+ result += str(item[0]) * item[1]
- return result
+ return result
-def reversed_dict ( dictionary ):
- reversedDict = {}
- for item in dictionary :
- reversedDict[ dictionary[item] ] = item
+def reversed_dict(dictionary):
+ reversed_dict = {}
- return reversedDict
+ for item in dictionary:
+ reversed_dict[dictionary[item]] = item
-def flatten ( dictionary, parentKey = '' ):
- flattenDictionary = []
+ return reversed_dict
+
+def flatten_dict(dictionary, parent_key=''):
+ flatten_dictionary = []
+
for key, value in dictionary.items():
- newKey = key
- if ( parentKey ):
- newKey = parentKey + '.' + key
+ new_key = key
- if isinstance ( value, dict ):
- flattenDictionary.extend ( flatten ( value, newKey ).items() )
- else:
- flattenDictionary.append ( ( newKey, value ) )
+ if parent_key:
+ new_key = parent_key + '.' + key
- return dict ( flattenDictionary )
+ if isinstance(value, dict):
+ flatten_dictionary.extend(flatten_dict(value, new_key).items())
+ else:
+ flatten_dictionary.append((new_key, value))
-def unflatten_dict ( dictionary ):
- unflattenDictionary = {}
+ return dict(flatten_dictionary)
- for key, value in dictionary.items():
- currentKey = key.split ( '.' )
- tempDictionary = unflattenDictionary
- for item in currentKey[:-1]:
- if item not in tempDictionary:
- tempDictionary[item] = {}
- tempDictionary = tempDictionary[item]
-
- tempDictionary[ currentKey[-1] ] = value
+def unflatten_dict(dictionary):
+ unflatten_dictionary = {}
- return unflattenDictionary
+ for key, value in dictionary.items():
+ currentKey = key.split('.')
+ temp_dictionary = unflatten_dictionary
-def reps ( array ):
- repArray = []
- for item in array:
- if array.count ( item ) > 1:
- repArray.append ( item )
+ for item in currentKey[:-1]:
+ if item not in temp_dictionary:
+ temp_dictionary[item] = {}
+ temp_dictionary = temp_dictionary[item]
- return repArray
+ temp_dictionary[currentKey[-1]] = value
+
+ return unflatten_dictionary
+
+
+def reps(array):
+ rep_array = []
+
+ for item in array:
+ if array.count(item) > 1:
+ rep_array.append(item)
+
+ return tuple(rep_array)