Question

Write a program that counts the distribution of the hour of the day for all the...

Write a program that counts the distribution of the hour of the day for all the event logs in the messages file. You can pull the hour from the “Nov, Oct, etc. ” line by finding the time string and then splitting that string into parts using the colon character. Once you have accumulated the counts for each hour, print out the counts, one per line, sorted by hour as shown below.  

Sample Execution: python timeofday.py Enter a file name: all_messages

04 3

06 1

07 1

09 2

Please Python Programming

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer Code:

filename = input("Enter a file name: ") # user input for file

data = {} # dictionary to store the data

fileObj = open(filename, 'r') # opening the file in read mode

for line in fileObj.readlines(): # for to read data line by line

time = line.split(" ")[2] # spliting the row by space anf storing the time stamp

hour = time.split(':')[0] # spliting the time stamp to get the hour value

if hour in data.keys(): # check if hour already in dictionay

data[hour] += 1 # if yes then increment the value by 1

else:

data[hour] = 1 # else assignment the value 1, as it is his first occurence in the file

fileObj.close() # closing the file

for key in sorted(data): # sorted function sort the dictionary by keys and return the list of sorted keys

print(str(key) + " " +str(data[key])) # printing the desired output

Code screenshot for indentation:

timeofday.py > ... 1 filename = input(Enter a file name: ) # user input for file 2 3 4 5 data = {} # dictionary to store th

Content of Input file used:

all_messages 1 Oct 20 12:05:35 ostnamel 2 Oct 20 04:05:36 stname23 3 Oct 21 12:05:43 hosame123 4 Dec 21 04:05:48 honame123 5

Sample output:

root@kali:-/code# python3 timeofday.py Enter a file name: all_messages 04 7 05 2 09 2 12 2 16 3 root@kali:-/code# I

Note: Please let me know if you have any doubt..

Add a comment
Know the answer?
Add Answer to:
Write a program that counts the distribution of the hour of the day for all the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I am new to Python and am having trouble coming up with writing code to the...

    I am new to Python and am having trouble coming up with writing code to the following problem... The program must: Prompt for a file name Opens that file and reads through the file Displays a custom error message if the file does not exist You can pull the hour out from the 'From ' line by finding the time and then splitting the string a second time using a colon. From [email protected] Sat Jan 5 09:14:16 2008 Accumulated the...

  • Write a program that determines if a string (passed as a command-line argument) has all unique...

    Write a program that determines if a string (passed as a command-line argument) has all unique characters. The program must print the number of times each existing character appears, and then print whether or not they are all unique. Special requirements: You are NOT allowed to modify the input string or create any additional data structures (no arrays, lists, dictionaries, or other strings). You may have only one string (the one received as the command-line argument). You may have one...

  • (Python) Write a program called sales.py that uses a list to hold the sums of the...

    (Python) Write a program called sales.py that uses a list to hold the sums of the sales for each month. The list will have 12 items with index 0 corresponds with month of “Jan”, index 1 with month “Feb”, etc. o Write a function called Convert() which is passed a string which is a month name (ex. “Jan”) and returns the matching index (ex. 0). Hint – use a list of strings to hold the months in the correct order,...

  • Write a c++ program in that file to perform a “Search and Replace All” operation. This...

    Write a c++ program in that file to perform a “Search and Replace All” operation. This operation consists of performing a case-sensitive search for all occurrences of an arbitrary sequence of characters within a file and substituting another arbitrary sequence in place of them. Please note: One of the biggest problems some students have with this exercise occurs simply because they don’t read the command line information in the course document titled “Using the Compiler’s IDE”. Your program: 1. must...

  • Overview: file you have to complete is WordTree.h, WordTree.cpp, main.cpp Write a program in C++ that...

    Overview: file you have to complete is WordTree.h, WordTree.cpp, main.cpp Write a program in C++ that reads an input text file and counts the occurrence of individual words in the file. You will see a binary tree to keep track of words and their counts. Project description: The program should open and read an input file (named input.txt) in turn, and build a binary search tree of the words and their counts. The words will be stored in alphabetical order...

  • The code should work exactly the same as given example. The files: ---------------------------------------------------...

    The code should work exactly the same as given example. The files: ---------------------------------------------------------------------------------------------------- -------- FILE NAME ------- album0 ---------- FILE ------- <album> 323 Licensed to Ill Beastie Boys 25.0 </album> <album> 70 Enter the Wu_Tang: 36 Cham... Wu Tang Clan 25.99 </album> turning to Alice, she went on, `What's your name, child?' and there stood the Queen in front of them, with her arms folded, <album> 115 Daydream Nation Sonic Youth 5.0 </album> <album> 414 52nd Street Billy Joel 25.75...

  • The file containing the JAVA files, pseudocode file and doc file that have written for this...

    The file containing the JAVA files, pseudocode file and doc file that have written for this lab. Preamble The file releasedates.txt contains a list of video games and their release dates. Each line of the file contains the release date, a tab character, and then the name. The list is currently totally unsorted. The object of today's lab is to write a series of methods that allow us to perform the following tasks: read contents from a file and store...

  • Write a Python program named aIP.py which will read data from a file named wireShark.txt and...

    Write a Python program named aIP.py which will read data from a file named wireShark.txt and extract all the pairs of source and destination ip addresses and output them in pairs to another file called IPAddresses.txt , one per line, listing source and destination. Example of output: Source                      Destination 192.168.1.180         239.255.255.250 Detailed Requirements: You will read from a file called wireShark.txt which, to avoid problems with finding paths, will be located in the same directory as your code You will...

  • For this c++ assignment, Overview write a program that will process two sets of numeric information....

    For this c++ assignment, Overview write a program that will process two sets of numeric information. The information will be needed for later processing, so it will be stored in two arrays that will be displayed, sorted, and displayed (again). One set of numeric information will be read from a file while the other will be randomly generated. The arrays that will be used in the assignment should be declared to hold a maximum of 50 double or float elements....

  • The assignment : Assignment You will be developing a speeding ticket fee calculator. This program will...

    The assignment : Assignment You will be developing a speeding ticket fee calculator. This program will ask for a ticket file, which is produced by a central police database, and your program will output to a file. Furthermore, your program will restrict the output to the starting and ending dates given by the user. The ticket fee is calculated using four multipliers, depending on the type of road the ticket was issued: Interstate multiplier: 5.2252 Highway multiplier: 9.4412 Residential multiplier:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT