Advertisement
avv210

CodefessReply-month

Dec 26th, 2022
1,108
0
Never
4
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.78 KB | None | 0 0
  1. def get_month(date_string):
  2.     # Split the date string into a list of three strings, representing the day, month, and year
  3.     date_parts = date_string.split("-")
  4.  
  5.     # Get the month string from the list
  6.     month_string = date_parts[1]
  7.  
  8.     # Convert the month string to an integer
  9.     month_number = int(month_string)
  10.  
  11.     # Define a list of month names
  12.     months = [
  13.         "January",
  14.         "February",
  15.         "March",
  16.         "April",
  17.         "May",
  18.         "June",
  19.         "July",
  20.         "August",
  21.         "September",
  22.         "October",
  23.         "November",
  24.         "December",
  25.     ]
  26.  
  27.     # Get the month name from the list using the month number
  28.     month_name = months[month_number - 1]
  29.  
  30.     # Create a dictionary that maps month numbers to month names
  31.     month_dict = {
  32.         1: "January",
  33.         2: "February",
  34.         3: "March",
  35.         4: "April",
  36.         5: "May",
  37.         6: "June",
  38.         7: "July",
  39.         8: "August",
  40.         9: "September",
  41.         10: "October",
  42.         11: "November",
  43.         12: "December",
  44.     }
  45.  
  46.     # Get the month name from the dictionary using the month number
  47.     month_name = month_dict[month_number]
  48.  
  49.     # Create a set of all the months in the year
  50.     month_set = {
  51.         "January",
  52.         "February",
  53.         "March",
  54.         "April",
  55.         "May",
  56.         "June",
  57.         "July",
  58.         "August",
  59.         "September",
  60.         "October",
  61.         "November",
  62.         "December",
  63.     }
  64.  
  65.     # Create a tuple of the month number and month name
  66.     month_tuple = (month_number, month_name)
  67.  
  68.     return month_tuple # Note: ganti return variable nya mau return tuples, sets, dicts, or lists?
  69.  
  70.  
  71. # Test the function with the input string '26 - 01 - 2022'
  72. print(get_month("26 - 01 - 2022"))
  73.  
Advertisement
Comments
  • Xaric2
    1 year
    # Python 0.73 KB | 0 0
    1. instead of all that unnecessary code just make it shorter with this
    2.  
    3. import calendar
    4.  
    5. def get_month(date_string):
    6.     # Split the date string into a list of three strings, representing the day, month, and year
    7.     try:
    8.         day, month, year = date_string.split("-")
    9.         # Convert the month string to an integer
    10.         month_number = int(month)
    11.     except (ValueError, TypeError):
    12.         # Handle invalid input
    13.         return None, None, None
    14.    
    15.     # Get the month name from the calendar module
    16.     month_name = calendar.month_name[month_number]
    17.    
    18.     return int(day), month_name, int(year)
    19.  
    20. # Test the function with the input string '26 - 01 - 2022'
    21. print(get_month("26-01-2022"))  # Output: (26, "January", 2022)
    • avv210
      1 year
      # text 0.15 KB | 1 0
      1. Thank you for your reply. But it is for university assignment. The requirements required the students to use tuples, sets, lists, and dicts data type. 😄
  • Xaric2
    1 year
    # Python 2.44 KB | 0 0
    1. My answer :)
    2.  
    3. import calendar
    4.  
    5. def get_month(date_string):
    6.     # Split the date string into a list of three strings, representing the day, month, and year
    7.     try:
    8.         day, month, year = date_string.split("-")
    9.         # Convert the month string to an integer
    10.         month_number = int(month)
    11.     except (ValueError, TypeError):
    12.         # Handle invalid input
    13.         return None, None, None
    14.    
    15.     # Get the month name from the calendar module
    16.     month_name = calendar.month_name[month_number]
    17.    
    18.     # Create a tuple of the day, month name, and year
    19.     date_tuple = (int(day), month_name, int(year))
    20.    
    21.     # Create a list of all the months in the year
    22.     months_list = [
    23.         "January",
    24.         "February",
    25.         "March",
    26.         "April",
    27.         "May",
    28.         "June",
    29.         "July",
    30.         "August",
    31.         "September",
    32.         "October",
    33.         "November",
    34.         "December",
    35.     ]
    36.    
    37.     # Create a set of all the months in the year
    38.     months_set = {
    39.         "January",
    40.         "February",
    41.         "March",
    42.         "April",
    43.         "May",
    44.         "June",
    45.         "July",
    46.         "August",
    47.         "September",
    48.         "October",
    49.         "November",
    50.         "December",
    51.     }
    52.    
    53.     # Create a dictionary that maps month numbers to month names
    54.     month_dict = {
    55.         1: "January",
    56.         2: "February",
    57.         3: "March",
    58.         4: "April",
    59.         5: "May",
    60.         6: "June",
    61.         7: "July",
    62.         8: "August",
    63.         9: "September",
    64.         10: "October",
    65.         11: "November",
    66.         12: "December",
    67.     }
    68.    
    69.     return date_tuple, months_list, months_set, month_dict
    70.  
    71. # Test the function with the input string '26 - 01 - 2022'
    72. date_tuple, months_list, months_set, month_dict = get_month("26-01-2022")
    73.  
    74. print("Date tuple:", date_tuple)  # Output: Date tuple: (26, 'January', 2022)
    75. print("Months list:", months_list)  # Output: Months list: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
    76. print("Months set:", months_set)  # Output: Months set: {'March', 'July', 'June', 'January', 'December', 'May', 'August', 'April', 'September', 'February', 'October', 'November'}
    77. print("Month dict:", month_dict)  # Output: Month dict: {1: 'January', 2: 'February', 3: 'March', 4: 'April', 5: 'May', 6: 'June', 7: 'July', 8: 'August', 9: 'September', 10: 'October', 11: 'November', 12: 'December'}
    78. ``
    79.  
Add Comment
Please, Sign In to add comment
Advertisement