In the code below, how can I change the set_ref method so that it automatically changes to value of the reference key to match the value of the key it is referencing every time I print sheet?
For Example:
In the code below, I set A1 value to 10, then I use the set_ref method to make A3 reference A1 so it’s also 10. Now when I set value of A1 to 20, A3 is still 10. What do I need to do to automatically get A3 value to change to 20?
#create sheets class with constructor to initialize "cells" instance variable as a dictionary
class Sheet:
def __init__(self) -> None:
self.cells = dict()
# set_value method takes key as a string and value as an int, set cells with
# key index along with its value
def set_value(self, key: string, value: int) -> None:
self.cells[key] = value
# get_value method takes the key as a string and return the value of the cell
# based on key index
def get_value(self, key: string) -> int:
return self.cells[key]
# set ref takes new key, the reference key and the old key, the key being referenced
# use set value method to set new value that has new key and the value of the key that it references
def set_ref(self, ref_key: string, key:string):
self.set_value(ref_key, self.cells[key])
# repr dunder method returns output as a string
# for each key in all the cells:
# add to the output= key: and its value and return key:value pair to us
def __repr__(self):
output = "--------\n"
for key in self.cells.keys():
output += f'{key}: {self.get_value(key)}\n'
output += "--------"
return output
# sheet object instantiation
sheet = Sheet()
sheet.set_value('A1', 10)
sheet.set_ref('A3', 'A1')
print(sheet) # A3 is now 10, just like A1
sheet.set_value('A1', 20)
print(sheet) # A3 is still 10, not 20
>Solution :
Assuming that keys are always str and values are always int you can tweak your code a bit:
class Sheet:
def __init__(self) -> None:
self.cells = dict()
# set_value method takes key as a string and value as an int, set cells with
# key index along with its value
def set_value(self, key: str, value: int) -> None:
self.cells[key] = value
# get_value method takes the key as a string and return the value of the cell
# based on key index
def get_value(self, key: str) -> int:
if type(self.cells[key]) == int:
return self.cells[key]
return self.get_value(self.cells[key])
# set ref takes new key, the reference key and the old key, the key being referenced
# use set value method to set new value that has new key and the value of the key that it references
def set_ref(self, ref_key: str, key:str):
self.set_value(ref_key, key)
# repr dunder method returns output as a string
# for each key in all the cells:
# add to the output= key: and its value and return key:value pair to us
def __repr__(self):
output = "--------\n"
for key in self.cells.keys():
output += f'{key}: {self.get_value(key)}\n'
output += "--------"
return output
# sheet object instantiation
sheet = Sheet()
sheet.set_value('A1', 10)
sheet.set_ref('A3', 'A1')
print(sheet) # A3 is now 10, just like A1
sheet.set_value('A1', 20)
print(sheet)
output:
--------
A1: 10
A3: 10
--------
--------
A1: 20
A3: 20
--------