Question

In: Computer Science

The Python Question is as follows: You have been asked by a manager of a store...

The Python Question is as follows:

You have been asked by a manager of a store to identify the items most commonly bought together (the manager would like to place these items in close proximity).

You are given a file, receipts.txt , that contains the receipts of the last 1000 transactions in the following format, where each line of the file is a single receipt:

eggs, bread, milk, lettuce

cheese, milk, apples

bread, milk

bread, cheese, milk

Write a function in python that will open the file, process it, and run the most common combinations(combinations that occur at least twice). For the example above, you should return a dictionary containing :

{ (bread, milk) :3, (cheese, milk) :2 }

NOTE: Code in python.

Solutions

Expert Solution

def createDictionary():
    itemDict = dict()
    with open('receipts.txt', 'r') as file:
        #Read complete content
        content = file.readlines()
  
        #Now process line by line
        for line in content:
            #items will store each item in a single line
            items = list()
            #Store items in a temporary list first
            temp = line.split(',')
            for item in temp:
                items.append(item.strip())
          
            #In a dictionary we can have tuple as a key.
            #Since (a,b) is same as (b,a).
            #To avoid issues due to order of the pair generated,
            #we will first create a pair
            #Then sort it using sorted() function for lists
            #Then convert it to tuple. Thus every pair of a & b will be (a,b) only.
      
            #Create item pairs and store in itemDict
            for i in range(len(items)-1):
                for j in range(i+1, len(items)):
                    pair = [items[i], items[j]]
                    pair = sorted(pair)
                    pair = tuple(pair)
              
                    #If pair found in itemDict, increment frequency else set it to 1
                    if pair in itemDict.keys():
                        itemDict[pair] = itemDict[pair] + 1
                    else:
                        itemDict[pair] = 1
                  
    #Generate Initial dictionary
    for key, value in itemDict.items():
        print(key, value)
  
    #Create dictionary with frequencies more than or equal to 2 only
    finalDict = dict()
    for key, value in itemDict.items():
        if value>=2:
            finalDict[key] = value

    print()
    #Print final dictionary
    for key, value in finalDict.items():
        print(key, ":", value)
  
    return(finalDict)

Initial outputs are the raw dictionary generated, includeing frequency of 1 also.

Then final dictionary has been generated and printed with frequency of 2 or more only.

Remove the print statements that you do not need.


Related Solutions

QUESTION 1 You are the investment manager for Ultimate Asset Managers and have been asked to...
QUESTION 1 You are the investment manager for Ultimate Asset Managers and have been asked to give a presentation to the new intake of graduates for the company’s internship programme. REQUIRED: Q.1.1 Name and discuss any two primary responsibilities/activities of financial managers. Q.1.2 Explain to the graduates what role contractual institutions play in the operation of financial markets. In your answer, provide an example of a contractual institution.
You are the risk manager of a hospital and have been asked to make a speech...
You are the risk manager of a hospital and have been asked to make a speech to doctors and nurses about how to prevent malpractice claims. draft a short speech outlining the most important guidelines and best practices for healthcare providers to limit medical mistakes and reduce potential liability. Provide specific steps they can take to protect themselves and the organization from malpractice and other kinds of claims against providers.
As a programmer in a java project, you have been asked by your project manager to...
As a programmer in a java project, you have been asked by your project manager to describe the most efficient way to store the following assigned numbers 10,20,30,1000,200,350 for an operation which involves on calculation such as sum and average.
As a new advertising manager of a company you have been asked to write out a...
As a new advertising manager of a company you have been asked to write out a proposal stating why the company should advertise and it’s benefits to the company. chose the Accounting subject to allow me post the question, but the course is Advertising.
You have just been hired as the marketing manager at a small retail clothing store. In...
You have just been hired as the marketing manager at a small retail clothing store. In the past, the store has done all the inventory purchasing, marketing and sales promotions based on intuition. Of course, as a marketing professional, you know that this is not the best strategy. You must convince your new boss to use a more empirical approach based on marketing metrics. Write a few organized paragraphs explaining to your boss what marketing metrics should be used and...
You have been asked to make a short presentation to the manager of your local community...
You have been asked to make a short presentation to the manager of your local community mental health centre about the value in adopting a MedTEAM or an Illness Management and Recovery (IMR) approach to working with people with psychiatric disabilities. Pick ONE aspect of MedTEAM or IMR that you'd like to promote. state: how that aspect will benefit client outcomes and at least one point of evidence (with reference) supporting your claim. also discuss the psychiatric rehabilitation guiding principle/s...
You have been asked to present your recommendation to the mayor and city manager with respect...
You have been asked to present your recommendation to the mayor and city manager with respect to management of the arena. You must outline a minimum of three primary reasons why the city should continue with public management and you must also demonstrate why private.
Question 3 You are the controller of company P and you have been asked to review...
Question 3 You are the controller of company P and you have been asked to review this situation to see if it is in the best interest of the company. Company P would like to sell bonds to obtain financing. Company P has an 80% interest in company S and interest rates are down. Company S is smaller than company P and has a lower credit rating. Company P wants to reduce interest costs on company debt S. You have...
Pretend you are again a manager of your favorite manufacturing company, Avon. You have been asked...
Pretend you are again a manager of your favorite manufacturing company, Avon. You have been asked to determine whether a product (Liquid Eyeliner) should be manufactured in-house or outsourced to another vendor. Discuss the relevant costs you would consider for this decision as well as irrelevant costs and sunk costs. How would you use differential analysis to arrive at this decision? Are there any other items that should be considered when making this decision?
What is the best interview question and/or the most inappropriate question you have ever been asked?...
What is the best interview question and/or the most inappropriate question you have ever been asked? Why do you feel that way and how did you respond?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT