Недавно я получил конкурсное задание по программированию, которое мне не удалось выполнить. Просто любопытно узнать лучшее решение проблемы
«А» - это индексированный нулем массив из N целых чисел. Элементами A являются целые числа в диапазоне от [−99,999,999 до 99,999,999]
«Карри» - это строка, состоящая из N символов, так что каждый символ представляет собой «P», «Q» или «R» и соответствующий индекс массива - это вес каждого ингредиента.
Карри идеально подходит, если сумма общих весов 'P', 'Q' и 'R' равна.
написать функцию
makeCurry(Array)
таким образом, чтобы при заданном нулевом индексе массива Array, состоящего из N целых чисел, возвращалось идеальное карри этого массива.
Функция должна возвращать строку "noLuck" если для этого массива не существует идеального карри.
Например, для данного массива Array, такого, что
A[0] = 3 A[1] = 7 A[2] = 2 A[3] = 5 A[4] = 4
, функция может вернуть "PQRRP", как объяснено выше. Учитывая массив A такой, что
A[0] = 3 A[1] = 6 A[2] = 9
функция должна возвращать "noLuck".
Подход, который я пробовал, был следующим:
import collections
class GetPerfectCurry(object):
def __init__(self):
self.curry = ''
self.curry_stats = collections.Counter({'P': 0, 'Q': 0, 'R': 0})
pass
def get_perfect_curry(self, A):
if len(A) == 0:
return "noLuck"
A.sort(reverse=True)
for i, ele in enumerate(A):
self.check_which_key_to_add_new_element_and_add_element(ele)
if self.curry_stats['P'] == self.curry_stats['Q'] == self.curry_stats['R']:
return self.curry
else:
return "noLuck"
def check_which_key_to_add_new_element_and_add_element(self, val):
# get the maximum current value
# check if addition of new value with any of the other two key equals the max value
# if yes then add that value and append the key in the curry string
current_max_key = max(self.curry_stats, key=self.curry_stats.get)
check_for_equality = False
key_to_append = None
for key, ele in enumerate(self.curry_stats):
if ele != current_max_key:
if self.curry_stats[ele] + val == self.curry_stats[current_max_key]:
check_for_equality = True
key_to_append = ele
if check_for_equality:
self.curry_stats.update(str(key_to_append) * val)
self.curry += str(key_to_append)
pass
else:
# if no value addition equals the current max
# then find the current lowest value and add it to that key
current_lowest_key = min(self.curry_stats, key=self.curry_stats.get)
self.curry_stats.update(str(current_lowest_key)*val)
self.curry += str(current_lowest_key)
if __name__ == '__main__':
perfect_curry = GetPerfectCurry()
A = [3, 7, 2, 5, 4]
# A = [3, 6, 9]
# A = [2, 9, 6, 3, 7]
res = perfect_curry.get_perfect_curry(A)
print(res)
Но это было неверно. Последние четыре часа почесал голову, чтобы найти лучшее решение этой проблемы