Frequency Queries

  • + 0 comments

    What's wrong here, which corner case may I be missing? (suceeds with some of them)

    def freqQuery(queries):
        '''
        Function to conduct the operation as defined in the query list upon an array
    
        Inputs:
        queries         - [List(tuples)] List containing tuples with (actionType, value)
    
        Returns:
        None
        '''
        myArray = []
        outputArray = []
        
        for query in queries:
            queryValue = query[1]
            
            # debug
            print(f"myArray: {myArray}")
            print(f"query type: {query[0]}, query value: {queryValue}")
            
            # Check type of query and perform mentioned action
            match query[0]:
                case 1:
                    # Append to list
                    myArray.append(queryValue)
                case 2:
                    # Convert to set and check if the element exists
                    mySet = set(myArray)
                    if mySet.intersection([queryValue]):
                        myArray.remove(queryValue)
                case 3:
                    # Create a dictinoary of frequencies
                    myDict = dict(())
                    for element in myArray:
                        count = 0
                        if myDict.get(element) == None:
                            myDict.update({element: count+1})
                        else:
                            myDict.update({element: myDict.get(element)+ 1})
    
                    # Convert the dict to list for easy iteration
                    frequencies = list(myDict.items())
                    
                    # Check for matching or NO frequencies
                    if not frequencies:
                        outputArray.append(0)
                    else:
                        for index, frequency in enumerate(frequencies):
                            if frequency[1] == queryValue:
                                outputArray.append(1)
                                break
                            else:
                                outputArray.append(0)
                                break
                                 
        return outputArray