lab08

.py

School

Purdue University *

*We aren’t endorsed by this school

Course

177

Subject

Computer Science

Date

Dec 6, 2023

Type

py

Pages

3

Uploaded by SuperTree11868

Report
# -*- coding: utf-8 -*- file2 = 'sample_data.txt' def load_inventory(filename): data = open(filename, "r").readlines() fin_dict = dict() for line in data: line = line.strip() line_str = line.split(",") key = line_str[0] value = tuple((line_str[1], int(line_str[2]), int(line_str[3]), int(line_str[4]), int(line_str[5]))) fin_dict.update({key:value}) return fin_dict diction = load_inventory(file2) def get_stats(data): stat = (data[1][1], data[1][2], data[1][3], data[1][4]) return stat def get_spot(data): spot = (data[1][0]) return spot #statis = tuple((stats[1], stats[2], stats[3], stats[4])) #return statis def get_name(data): name = data[0] return name x = ('Starlight',('Main Hand', 1, -1, 3, 2)) y = ('Sunflare Rapier', ('Main Hand', 2, 0, 3, -1)) z = 'Starlight' w = 'Sunflare Rapier' def stat_num(stat): if stat == "Damage": n = 0 elif stat == "Defense": n = 1 elif stat == "Speed": n = 2 elif stat == "Magic": n = 3 return n def compare(data1, data2, typ): stat1= get_stats(data1) stat2 = get_stats(data2) if typ == "Damage": if stat1[0] >= stat2[0]: return get_name(data1) else: return get_name(data2) if typ == "Defense": if stat1[1] >= stat2[1]: return get_name(data1)
else: return get_name(data2) if typ == "Speed": if stat1[2] >= stat2[2]: return get_name(data1) else: return get_name(data2) if typ == "Magic": if stat1[3] >= stat2[3]: return get_name(data1) else: return get_name(data2) def replace_better(data, typ, name1, name2): thing1 = (name1, data.get(name1)) thing2 = (name2, data.get(name2)) final = compare(thing1, thing2, typ) final2 = (final, data.get(final)) spot = get_spot(final2) return (spot, final) def optimize_equipment(data, stat): equipment = dict() equipment.update({'Main Hand':''}) equipment.update({'Off Hand':''}) equipment.update({'Head':''}) equipment.update({'Hands':''}) equipment.update({'Chest':''}) equipment.update({'Legs':''}) equipment.update({'Neck':''}) equipment.update({'Cloak':''}) equipment.update({'Neck':''}) items = data.items() for i in items: equip_name = i[0] spot = get_spot(i) if equipment.get(spot) == "": equipment.update({spot:equip_name}) else: old = equipment.get(spot) new = equip_name better = replace_better(data, stat, old, new) equipment[better[0]] = better[1] shitters = [] for i in equipment: j = equipment.get(i) formatt = (j, data.get(j)) stuts = get_stats(formatt) num = stat_num(stat) if stuts[num] < 0: shitters += [i] for word in shitters: equipment.pop(word) return equipment qual = optimize_equipment(diction, "Defense") qual2 = optimize_equipment(diction, "Damage") #qual2 = optimize_equipment(diction, 'Damage')
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help