The function below takes one parameter: a string (date_string) containing a date in the mm/dd/year format. Complete the function to return a list containing integers for each of the date components in month, day, year order. For example, if given the string '06/11/1930', your code should return the list [6, 11, 1930].

Respuesta :

Answer:

The solution code is written in Python 3.

  1. def convertDate(date_string):
  2.    date_list = date_string.split("/")
  3.    for i in range(0, len(date_list)):
  4.        date_list[i] = int(date_list[i])
  5.    return date_list  
  6. print(convertDate('06/11/1930'))

Explanation:

Firstly, create a function convertDate() with one parameter, date_string. (Line 1).

Next, use the Python string split() method to split the date string into a list of date components (month, day & year) and assign it to variable date_list. (Line 3) In this case, we use "/" as the separator.

However, all the separated date components in the date_list are still a string. We can use for-loop to traverse through each of the element within the list and convert each of them to integer using Python int() function. (Line 5 - 6)

At last return the final date_list as the output (Line 8)

We can test our function as in Line 11. We shall see the output is as follow:

[6, 11, 1930]

The function that takes one parameter: a string (date_string) containing a date in the mm/dd/year format and return a list containing integers for each of the date components in month, day, year order is as follows:

def convert_date(date_string):

    list1 = []

    splits = date_string.split("/")

    for i in splits:

         list1.append(int(i))

    return list1

print(convert_date('06/11/1930'))

Code explanation:

The code is written in python.

  • We declared a function named "convert_date" and it has a parameter named "date_string".
  • An empty list is declared.
  • We used the split() function to remove "/".
  • We used for loop to loop the variable "splits" and append the integer value of each value in the splits to the list1.
  • We returned list1.
  • Finally, we call the function with the required parameter.

learn more on function here: https://brainly.com/question/26104476

Ver imagen vintechnology