Может ли кто-нибудь, пожалуйста, скрыть этот код Python на C++
def replace(good,bad,name): file = open(name) contents = file.read() contents = contents[:-4] file.seek(0) bad_keys = list(bad.keys()) bad_keys.sort(key = lambda x:len(x),reverse=True) good_keys = list(good.keys()) good_keys.sort(key=lambda x: len(x), reverse=True) for i in bad_keys: if i in contents: for k in good_keys: if good[k] == bad[i] and len(i.split()) == len(k.split()): contents = contents.replace(i,k) break print(contents) def file_score(good,bad,name): bad_score = 0 good_score = 0 file = open(name) for i in sorted(bad.keys()): if i in file.read(): file.seek(0) bad_score += bad[i]*(file.read()).count(i) file.seek(0) for i in sorted(good.keys()): if i in file.read(): file.seek(0) good_score += good[i]*(file.read()).count(i) file.seek(0) print(good_score,bad_score,sep='\n') if bad_score > good_score: replace(good,bad,name) else: file.seek(0) print(file.read()[:-4]) file.close() bad = {'bad': 5, 'worse': 3, 'worst': 6, 'very bad': 5, 'bad luck': 7, 'bad mood': 6} good = {'great': 6, 'good': 5, 'nice': 3, 'very good': 5, 'very great': 7, 'good mood': 6} name = input() file_score(good,bad,name)
Что я уже пробовал:
def replace(good,bad,name): file = open(name) contents = file.read() contents = contents[:-4] file.seek(0) bad_keys = list(bad.keys()) bad_keys.sort(key = lambda x:len(x),reverse=True) good_keys = list(good.keys()) good_keys.sort(key=lambda x: len(x), reverse=True) for i in bad_keys: if i in contents: for k in good_keys: if good[k] == bad[i] and len(i.split()) == len(k.split()): contents = contents.replace(i,k) break print(contents) def file_score(good,bad,name): bad_score = 0 good_score = 0 file = open(name) for i in sorted(bad.keys()): if i in file.read(): file.seek(0) bad_score += bad[i]*(file.read()).count(i) file.seek(0) for i in sorted(good.keys()): if i in file.read(): file.seek(0) good_score += good[i]*(file.read()).count(i) file.seek(0) print(good_score,bad_score,sep='\n') if bad_score > good_score: replace(good,bad,name) else: file.seek(0) print(file.read()[:-4]) file.close() bad = {'bad': 5, 'worse': 3, 'worst': 6, 'very bad': 5, 'bad luck': 7, 'bad mood': 6} good = {'great': 6, 'good': 5, 'nice': 3, 'very good': 5, 'very great': 7, 'good mood': 6} name = input() file_score(good,bad,name)
CPallini
Почему вы ничего не предприняли?
Richard MacCutchan
Обычная причина. :/
Rick York
Конечно, кто-то может преобразовать это для вас. Я бы подумал об этом, но сомневаюсь, что вам понравится моя ставка.