Question

In: Computer Science

Complete the code so that it can convert the date to day of week using python,...

Complete the code so that it can convert the date to day of week using python, the code should pass the doctest

def convert_datetime_to_dayofweek(datetime_string):
"""
This function takes date in the format MON DAY YEAR HH:MM(PM/AM)
and returns the day of the week
Assume input string is UTC
  
>>> convert_datetime_to_dayofweek('Jun 1 2005 1:33PM')
'Wednesday'
>>> convert_datetime_to_dayofweek('Oct 25 2012 2:17AM')
'Thursday'
"""
# code goes here

Solutions

Expert Solution

Code -

import datetime
from datetime import date
  
def convert_datetime_to_dayofweek(date):
month, day, year, time = (i for i in date.split(' '))
months = dict(Jan =1 ,Feb = 2 , Mar =3 , Apr = 4 , May = 5 , Jun = 6,
Jul = 7 , Aug =8 , Sept=9 , Oct = 10 , Nov=11 , Dec=12)
  
  
day_of_week = datetime.date(int(year), int(months[month]), int(day))
return day_of_week.strftime("%A")
  
if __name__=='__main__':
date = 'Oct 25 2012 2:17AM'
print(convert_datetime_to_dayofweek(date))

Explanation -

1. import datetime module .

datetime module is used to manipulate dates and time.

Also , import date class from datetime module.date class will manipulate the dates

2.define function convert_datetime_to_dayofweek which accepts single parameter date.

3.iterate through every item in date after splliting it at space. That is wherever space is present , date string will be split.Store it in month,day,year and time variable respectively.

Example -

date - Oct 25 2012 2:17AM

month, day, year, time = (i for i in date.split(' '))

Here month = oct , day=25,year=2012,time=2:17 AM

4.Since month is in name format we need to convert in into integer . define a dictionary months with key as month name and value as integer like Jan:1 , Feb :2 and so on

months = dict(Jan =1 ,Feb = 2 , Mar =3 , Apr = 4 , May = 5 , Jun = 6,
Jul = 7 , Aug =8 , Sept=9 , Oct = 10 , Nov=11 , Dec=12)

5.Use datetime.date and pass year,month and day as arguments . Pass them as arguments after converting it into int.Hence use int(year), int(months[month]), int(day).

months[month] will give us the value of key month.

if month= Jun

then month[Jun] = 6

6.Use strftime("%A") to return the weekday from day_of_week.

7.Finally call the function convert_datetime_to_dayofweek in main and pass the date as argument to the function.


Related Solutions

can you please convert this python code into java? Python code is as shown below: #...
can you please convert this python code into java? Python code is as shown below: # recursive function def row_puzzle_rec(row, pos, visited):    # if the element at the current position is 0 we have reached our goal    if row[pos] == 0:        possible = True    else:        # make a copy of the visited array        visited = visited[:]        # if the element at the current position has been already visited then...
USE PYTHON to find out What day of the week is a given date? i.e. On...
USE PYTHON to find out What day of the week is a given date? i.e. On what day of the week was 5 August 1967? if it is given that 1 January 1900 was a tuesday Write a function is_leap() which takes 1 input argument, an integer representing a year, and returns True if the year was a leap year, and False if it was not. .Then, write a function days_since() which takes 3 input integers day, month, year, representing...
What day of the week is a given date? i.e. On what day of the week...
What day of the week is a given date? i.e. On what day of the week was 5 August 1967? if it is given that 1 January 1900 was a tuesday Write a function is leap() which takes 1 input argument, an integer representing a year, and returns True if the year was a leap year, and False if it was not. .Then, write a function days_since() which takes 3 input integers day, month, year, representing a date (after 1st...
how to correct this java code so that i get the correct day of week? and...
how to correct this java code so that i get the correct day of week? and test the year public static void main(String[] args) {        //               Scanner s = new Scanner(System.in); //needed info //year month, day int year, month, dayOfMonth; // add day of week , century yr int dayOfWeek, century, yearOfCentury;    //user inputs year System.out.print("Enter year: (example, 2020):"); year = s.nextInt(); //user inputs month by number System.out.print("Enter month: 1-12:"); month = s.nextInt();...
Write a query to return the date and day of the week of the first day...
Write a query to return the date and day of the week of the first day of the month two years from today. If today is 10/16/20, then the expect output is 10/01/2022 and the day of the week is thursday. I am using postgresql.
Complete the following in syntactically correct Python code. Write a program, using a for loop, that...
Complete the following in syntactically correct Python code. Write a program, using a for loop, that calculates the amount of money a person would earn over a period of time if his or her salary is 1 penny for the first day, 2 pennies for the second day, 4 pennies for the third day, and continues to double each day. 1.      The program should ask the user for the number of days the employee worked. 2.      Display a table showing the salary...
Complete the R code using Rstudio so that it calculates and returns the estimates of β,...
Complete the R code using Rstudio so that it calculates and returns the estimates of β, the intercept and regression weight of the logistic regression of approximate GPA on Rouder-Srinivasan preference. ## Data Preference <- c( 0, 0, 0, 0, 0, 1, 1, 1, 1) # 0: Rouder; 1: Srinivasan GPA <- c(2.0, 2.5, 3.0, 3.5, 4.0, 2.5, 3.0, 3.5, 4.0) Count <- c( 4, 5, 21, 22, 8, 2, 1, 4, 7) # Define the deviance function deviance <-...
Python 3 Fix the code so i can make the window larger or smaller and the...
Python 3 Fix the code so i can make the window larger or smaller and the fields adjusts everytime according to the window size import tkinter as tk from tkcalendar import DateEntry from openpyxl import load_workbook window = tk.Tk() window.title("daily logs") # window.resizable(0,0) # labels tk.Label(window, text="Bar code").grid(row=0, sticky="W", pady=20, padx=20) tk.Label(window, text="Products failed").grid(row=1, sticky="W", pady=20, padx=20) tk.Label(window, text="Money Lost").grid(row=2, sticky="W", pady=20, padx=20) tk.Label(window, text="sold by").grid(row=3, sticky="W", pady=20, padx=20) tk.Label(window, text="Failed date").grid(row=4, sticky="W", pady=20, padx=20) # entries barcode = tk.Entry(window)...
FOR PYTHON: Write a python program for a car salesperson who works a five day week....
FOR PYTHON: Write a python program for a car salesperson who works a five day week. The program should prompt for how many cars were sold on each day and then prompt for the selling price of each car (if any) on that day. After the data for all five days have been entered, the program should report the total number of cars sold and the total sales for the period. See example output. NOTE: duplicate the currency format shown...
Python 3 Fix the code so i can make the window larger and the fields increase...
Python 3 Fix the code so i can make the window larger and the fields increase size import tkinter as tk from tkcalendar import DateEntry from openpyxl import load_workbook window = tk.Tk() window.title("daily logs") # window.resizable(0,0) # labels tk.Label(window, text="Bar code").grid(row=0, sticky="W", pady=20, padx=20) tk.Label(window, text="Products failed").grid(row=1, sticky="W", pady=20, padx=20) tk.Label(window, text="Money Lost").grid(row=2, sticky="W", pady=20, padx=20) tk.Label(window, text="sold by").grid(row=3, sticky="W", pady=20, padx=20) tk.Label(window, text="Failed date").grid(row=4, sticky="W", pady=20, padx=20) # entries barcode = tk.Entry(window) product = tk.Entry(window) money = tk.Entry(window) #...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT