Question

In: Computer Science

In this homework, you are provided a user pool (hw2_users_4m.txt) and a list of target users...

In this homework, you are provided a user pool (hw2_users_4m.txt) and a list of target users (hw2_targets.txt). Find if each target user is in the user pool. In python

1. Use list to store user pool and write the output into a file.

user_id_1,True

user_id_2, False

with open('hw2_targets.txt', 'r') as i:
    targets = [line.strip() for line in i]

with open('hw2_users_4m.txt', 'r') as i:
    user_ids_list = [line.strip() for line in i]

start_time = time.time()
results_list = []
##################################
## write your code for using list
##################################


end_time = time.time()
run_time = end_time - start_time
print(round(run_time, 2))
with open('output_list.txt', 'w') as o:
    for result in results_list:
        o.write(result[0] + ',' + result[1])

Solutions

Expert Solution

Following updates have been made to the given code snippet:

1. Added import statement to use time() function.

2. Added for loops to iterate through every user in target list and determine even it exists in the given pool of users.

3. Storing our results in a list called results_list

4. Added a new line character while writing to output file.

Fianl program:

import time 

with open('hw2_targets.txt', 'r') as i:
    targets = [line.strip() for line in i]

with open('hw2_users_4m.txt', 'r') as i:
    user_ids_list = [line.strip() for line in i]

start_time = time.time()
results_list = []

for tar in targets:     #For each target user in the input file loop checks if it exists in the user pool
    target_exist="False"        #Variable to determine existence of a target user in given pool of users
    for user in user_ids_list:
        if tar == user:
            target_exist="True"
            results_list.append([tar,target_exist]) #Storing our result in the list
            #If a match is found no need to check in rest of the pool of users. Thus, breaking flow of inner loop
            break
    if target_exist=="False": #Match for target user not found in user pool thus, storing False as result for that target user
        results_list.append([tar,target_exist])
        
            
end_time = time.time()
run_time = end_time - start_time
print(round(run_time, 2))

with open('output_list.txt', 'w') as o:
    for result in results_list:
        o.write(result[0] + ',' + result[1])
        o.write("\n") #Writing new line character to the file to display each result in a new line

Related Solutions

In this homework, you are provided a user pool (hw2_users_4m.txt) and a list of target users...
In this homework, you are provided a user pool (hw2_users_4m.txt) and a list of target users (hw2_targets.txt). Find if each target user is in the user pool. In python 1. Use set to store user pool. with open('hw2_users_4m.txt', 'r') as i: user_ids_set = {line.strip() for line in i} start_time = time.time() results_set = [] ################################## ## write your code for using set ################################## end_time = time.time() run_time = end_time - start_time print(round(run_time, 2)) with open('output_set.txt', 'w') as o: for result...
Design a Python script that accepts as input a user-provided list and transforms it into a...
Design a Python script that accepts as input a user-provided list and transforms it into a different list in preparation for data analysis, the transformed list replaces each numeric element in the original list with its base-10 order of magnitude and replaces string elements with blanks. Example: This script accepts as input a user-provided list expected to contain non-zero numbers and strings. It then prints a transformed list replacing numbers with their order of magnitude, and strings as blanks. Type...
In this homework, you will implement a single linked list to store a list of employees...
In this homework, you will implement a single linked list to store a list of employees in a company. Every employee has an ID, name, department, and salary. You will create 2 classes: Employee and EmployeeList. Employee class should have all information about an employee and also a “next” pointer. See below: Employee Type Attribute int ID string name string department int salary Employee* next Return Type Function (constructor) Employee(int ID, string name, string department, int salary) EmployeeList class should...
Chapter 3 Homework You are considering an investment in the common stock of Target (Ticker: TGT)....
Chapter 3 Homework You are considering an investment in the common stock of Target (Ticker: TGT). Calculate the missing ratios (Current Ratio, Debt Ratio, and Profit Margin). Discuss the company’s strengths and weakness in the areas of liquidity, asset management, debt management and profitability relative to the competitor, Walmart Inc. (Ticker: WMT). Summarized Financial Data and Ratios from Hoovers. Target: 2017 Annual Balance Sheet (In Millions) Total Current Assets $   11,990    Total Current Liabilities $ 12,708 Net Fixed Assets...
Thinking about the types of users: Internal and External users, How do each type of user...
Thinking about the types of users: Internal and External users, How do each type of user effect the accounting process in an organization? What types of users have the most impact and why? responses must be 100 words minimum.
You are provided two problem statements below (Homework Problem 1). For each, provide a score on...
You are provided two problem statements below (Homework Problem 1). For each, provide a score on a scale of 1 to 10 with 10 being best. Provide justification for each score that you gave. Example Problem Statements 1. We want to improve employee performance in the Human Resources (HR) department. 2. We want to improve sales revenue by 10%.
For Homework 4, we are going to present the user with a series of menus, accept...
For Homework 4, we are going to present the user with a series of menus, accept as input the action they wish to take, and act appropriately. You must practice basic input validation to ensure that the menu option they chose is valid. Create “CPS132 Homework 4” project in Eclipse Create “Homework4.py” file Download "Homework4_incomplete.py" Paste the text into "Homeworkpy" Make sure to fill in the appropriate information in the header including your name, username, due date, Homework #4, and...
Choose a product you are familiar with. List the target market of that product, the behavior...
Choose a product you are familiar with. List the target market of that product, the behavior you would want consumers to do for that product, and the mental steps that may be necessary to move consumers to the behavior. Hint, here are some other mental steps/intervening variables, communication objectives in addition to awareness - knowledge of a benefit, preferring one brand over another
For certain software, independently of other users, the probability is 0.07 that a user encounters a...
For certain software, independently of other users, the probability is 0.07 that a user encounters a fault. What are the chances of the 30th user is the 5th person encountering a fault?
Purpose For many users, the system is the user interface. Everyone wants a system that is...
Purpose For many users, the system is the user interface. Everyone wants a system that is easy to learn and use. It is the analyst’s responsibility to ensure that the user interface will be designed in a way to support the user’s interface needs.   In this milestone you will explore the guidelines for user interface design. The analyst must consider a variety of interface formats. These include how data gets entered into the system (input design) and how data or...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT