Спасимир обнови решението на 22.03.2015 03:00 (преди над 9 години)
+def extract_type(pairs, variety):
+ extracted = ''
+ for item in pairs:
+ if type(item[0]) is variety:
+ extracted += (str(item[0]) * item[1])
+ return extracted
+
+
+def reversed_dict(dictionary):
+ return {dictionary[k]: k for k in dictionary}
+
+
+def flatten_dict(dictionary):
+ def flatten(key, value):
+ if type(value) is dict:
+ return [(key + '.' + subkey, subvalue)
+ for subkey, subvalue
+ in flatten_dict(value).items()]
+ else:
+ return [(key, value)]
+
+ items = [item for subkey, subvalue
+ in dictionary.items()
+ for item in flatten(subkey, subvalue)]
+ return dict(items)
+
+
+def unflatten_dict(dictionary):
+ result = {}
+ for key, value in dictionary.items():
+ sub = result
+ for subkey in key.split('.')[:-1]:
+ if subkey not in sub:
+ sub[subkey] = {}
+ sub = sub[subkey]
+ sub[key.split('.')[-1]] = value
+ return result
+
+
+def reps(sequence):
+ return tuple([i for i in sequence if not sequence.count(i) == 1])