Home > front end >  The max value that gets returned is not based on the requested date, what am i doing wrong?
The max value that gets returned is not based on the requested date, what am i doing wrong?

Time:09-26

For school we have to make an assignment -->

"The mayor of Amsterdam wants to store the covid levels in the water of for day. He wants to be able give you a date and then get the maximum covid level for that day. Your job is to implement a way to store the input as a data structure so that you can store the readings for each day and easily find the maximum. The first element in the input is the desired date (e.g., ’2022-09-08;’). What follows is a list of triplets, separated with a semicolon ’;’. One triplet represents one reading instance with the structure ’date,id,covid level;’. Find the maximum covid level found in the sewage system on the desired date and output the sensor ID and the covid level."

The program takes as input:

(string)’yyyy-mm-dd’, (int)sensor id, (int)covid level. The expected output is sensor id,covid level.

For example: input: 2022−09−08; 2022−09−08, 23, 371; 2022−09−08, 2, 3171; 2022−09−08, 12, 43; 2021− 03 −21, 4, 129

Output: 2, 3172

I have made some code, however my code returns the maximum value overall. How can i bring the requested date into the equation, specifically in the get_max() function. If anyone could help, that would be great! Below is my code. the required data structure is a dictionary!


def add_value(dict_obj, key, value):
    if key not in dict_obj:
        dict_obj[key] = list()
    dict_obj[key].append(value)
    #print(dict_obj)

# get maximum for specific day
def get_max(dic, datum):
    #for keys in dic:
        #if dic.keys() == datum:
            max_val = max(dic.values(), key=lambda x: x[1][1])
        #max_key = next(k for k, v in dic.items() if v == max_val)
            return max_val


if __name__ == "__main__":
    input = input()
    input = input.split(";")
    requested_date = input[0]
    input.pop(0)
    data = []
    for d in input:
        date, id, value = d.split(',', 2)
        instance = (date, (int(id), int(value)))
        data.append(instance)
    new_dict = {}
    for element in range(len(data)):
        add_value(new_dict, data[element][0], [data[element][1][0], data[element][1][1]])
    print(get_max(new_dict, requested_date))

ps, don't mind the commented lines in the get_max function, i just have been trying some things, but nothing seems to work, idk what i am missing here.

CodePudding user response:

I would make it a two-dimensional dict, and would do it immediately in the loop where you read all data. Then you can use the max formula with a lambda to get the key where the max value is:

if __name__ == "__main__":
    input = input()
    input = input.split(";")
    requested_date = input.pop(0)
    data = dict()
    for sensor in input:
        date, id, value = sensor.split(',', 2)
        date = date.strip()
        if not date in data.keys():
            data[date] = dict()

        data[date][int(id)] =  int(value)

    max_on_date = max(data[requested_date], key=lambda key: data[requested_date][key])
    print(max_on_date, data[requested_date][max_on_date], sep=', ')

CodePudding user response:

Build a dictionary keyed on date. Each associated value should be list of tuples. Create each tuple in the form (level, id) to simplify finding the maxima

Something like this:

if __name__ == '__main__':
    db = {}
    data = input()
    dd, *values = data.split(';')
    for value in values:
        date, ident, level = value.split(',')
        db.setdefault(date.strip(), []).append((int(level), int(ident)))
    if (values := db.get(dd.strip())):
        level, ident = max(*values)
        print(f'{dd} {ident=} {level=}')

Output:

2022−09−08 ident=2 level=3171
  • Related