Skip to main content

Functions and OOPs import datetime||Python Hands on datetime \ hacker rank solution

 #!/bin/python3


import math
import os
import random
import re
import sys


import datetime
#
# Complete the 'dateandtime' function below.
#
# The function accepts INTEGER val as parameter.
# The return type must be LIST.
#

def dateandtime(val,tup):
    emtlist=[]
    if val==1:
        d1=datetime.date(tup[0],tup[1],tup[2])
        emtlist.append(d1)
        dd1=d1.strftime("%d/%m/%Y")
        emtlist.append(dd1)
    if val==2:
        d = datetime.datetime.fromtimestamp(int(tup[0]))
        d = d.date()
        emtlist.append(d)
    if val==3:
        t1=datetime.time(tup[0],tup[1],tup[2])
        emtlist.append(t1)
        hour_format=t1.strftime("%I")
        emtlist.append(hour_format)
    if val==4:
        d3=datetime.date(tup[0],tup[1],tup[2])
        weekday=d3.strftime("%A")
        emtlist.append(weekday)
        month=d3.strftime("%B")
        emtlist.append(month)
        day=d3.strftime("%j")
        emtlist.append(day)
    if val==5:
        d4=datetime.datetime(tup[0],tup[1],tup[2],tup[3],tup[4],tup[5])
        emtlist.append(d4)
    return emtlist
        
    # Write your code here

if __name__ == '__main__':
    val = int(input().strip())
    
    if val ==1 or val==4 or val ==3:
        qw1_count=3
    if val==2:
        qw1_count=1
    if val ==5:
        qw1_count=6
    qw1 = []

    for _ in range(qw1_count):
        qw1_item = int(input().strip())
        qw1.append(qw1_item)
        
    tup=tuple(qw1)
    
    ans = dateandtime(val,tup)
    
    print(ans)

Comments

Popular posts from this blog

Class and object Task 1| cinema tickets | hacker rank|tcs fresco play

  class   Movie :      def   __init__ ( self , nameofmovie , nooftickets , totalcost ):          self . nameofmovie = nameofmovie          self . nooftickets = nooftickets          self . totalcost = totalcost      def   __str__ ( self ):          return   "Movie : " + str ( self . nameofmovie ) + "\n" + "Number of Tickets : " + str ( self . nooftickets ) + "\n" + "Total Cost : " + str ( self . totalcost )                   # Write your code here if   __name__  ==  '__main__' :

Magic constant generator -python3/hacker rank solution / tcs fresco play

  def   generator_Magic ( n1 ):      # the value starts from 3 and m is formula for constant,      #for generator  yield should use      for   a   in   range ( 3 , n1 + 1 ):          m = a * ( a ** 2 + 1 ) / 2          yield   m               # Write your code here if   __name__  ==  '__main__' :

Handling Exceptions 1 | hacker rank solution

  #!/bin/python3 import   math import   os import   random import   re import   sys # # Complete the 'Handle_Exc1' function below. # # def   Handle_Exc1 ():      a = int ( input ())      b = int ( input ())      if   a > 150   or   b < 100 :          raise   ValueError   ( "Input integers value out of range." )        if   a + b > 400 :          raise   ValueError ( "Their sum is out of range" )      print ( "All in range" )      # Write your code here if   __name__  ==  '__main__' :      try :          Handle_Exc1 ()   ...