جواب «Git History Leak» چالش 5 از جلسهی 57
سؤال
تاریخچهی گیت را شبیهسازی کن. ورودی: اول n، بعد n خط <commit> <action> <text> که action یا add است (این متن به فایل اضافه شد) یا remove (این متن از فایل حذف شد). بعد یک خط: متنی که دنبالش هستیم (مثلاً یک کلید). اگر این متن در هیچ commitی اضافه نشده چاپ کن clean. اگر اضافه شده، چاپ کن leaked in <اولین commit>؛ و بعد اگر بعداً حذف شده، removed in <commit>, but still in history: rotate the key، وگرنه still in the current file: remove it and rotate the key.
راهنما
دو متغیر: added_in = None و removed_in = None؛ در حلقه فقط اولین add و آخرین remove را نگه دار.
یک جواب درست
n = int(input())
history = []
for i in range(n):
commit, action, text = input().split(" ", 2)
history.append((commit, action, text))
wanted = input()
added_in = None
removed_in = None
for commit, action, text in history:
if text == wanted:
if action == "add" and added_in is None:
added_in = commit
elif action == "remove":
removed_in = commit
if added_in is None:
print("clean")
else:
print("leaked in", added_in)
if removed_in:
print("removed in " + removed_in + ", but still in history: rotate the key")
else:
print("still in the current file: remove it and rotate the key")
اول خودت امتحان کن؛ جوابهای دیگر هم قبول است. برگرد و خودت حلش کن