当前位置:网站首页>freeCodeCamp----time_ Calculator exercise

freeCodeCamp----time_ Calculator exercise

2022-04-23 13:51:00 Lily's autumn

Catalog

1 Subject requirements

2 example

3 Process analysis

 4 Source code

5 It's easy to get wrong

 6 Be confused


1 Subject requirements

Given an original time , A duration , Add the two together to 12 The hour system shows , The suffix is required to be morning or afternoon 、 Zhou display 、 A few days later, it shows that .

2 example

add_time("3:00 PM", "3:10")
# Returns: 6:10 PM

add_time("11:30 AM", "2:32", "Monday")
# Returns: 2:02 PM, Monday

add_time("11:43 AM", "00:20")
# Returns: 12:03 PM

add_time("10:10 PM", "3:30")
# Returns: 1:40 AM (next day)

add_time("11:43 PM", "24:20", "tueSday")
# Returns: 12:03 AM, Thursday (2 days later)

add_time("6:30 PM", "205:12")
# Returns: 7:42 AM (9 days later)

3 Process analysis

Made a simple flow chart , as follows , It basically explains the process of data processing from parameter input .

 4 Source code

Not much to say , Attach directly , The process was explained very clearly .

import re
def add_time(start, duration, day = None):
    #  Split the data first 
    aday = re.findall(r'AM|PM', start)

    #  On time 、 Put... Into the array 
    orginal_time = re.findall(r'\d+', start)
    time_plus = re.findall(r'\d+', duration)

    #  First judge the original time is AM still PM, if PM, The original hour needs to be added 12 Then calculate later 
    if 'PM' in aday:
      orginal_time[0] = str(int(orginal_time[0]) + 12)

    #  Add them separately first 
    # print(orginal_time)
    new_hour = int(orginal_time[0]) + int(time_plus[0])
    new_min = int(orginal_time[1]) + int(time_plus[1])

    #  First judge the number of minutes 
    #  First determine whether it is greater than 59
    #  Note that you need to round first , Add to new_hour On , If the order is wrong, an error will be reported , because new_min Has been reassigned 
    if new_min > 59:
      new_hour += int(new_min/60)
      new_min = new_min%60
    # print('new_hour:'+str(new_hour))
    # print('new_min:'+str(new_min))
    #  Less than 10, Add... To the front 0
    if new_min < 10:
      new_min = '0' + str(new_min)

    new_day = 0
    if new_hour > 23:
      new_day = int(new_hour/24)
      new_hour = new_hour%24
      if new_hour < 12:
        aday[0] = 'AM'
      if new_hour == 12:
        aday[0] = 'PM'
      else:
        if new_hour > 12:
          new_hour -= 12;
          aday[0] = 'PM'
    else:
      if new_hour < 12:
        aday[0] = 'AM'
      if new_hour == 12:
        aday[0] = 'PM'
      else:
        if new_hour > 12:
          new_hour -= 12;
          aday[0] = 'PM'

    # print(new_hour)
    # print(new_min)
    new_time = str(new_hour) + ':' + str(new_min) + ' ' + aday[0]

    #  If the second parameter is not empty , The judgment is a few days later , And calculate the day of the week 
    new_day_out = ''
    if new_day == 1:
      new_day_out = ' (next day)'
    if new_day >= 2:
      new_day_out = ' (' + str(new_day) + ' days later' + ')'

    week = ['monday','tuesday','wednesday','thurday','friday','saturday','sunday']
    #  Judge day Is the value of week Inside , Be careful not to be case sensitive , because day The value of may be case mixed 
    #  Here are some tips , take day Convert all to lowercase and then judge , When outputting, just capitalize the initial letter 
    if day:
      if day.lower() in week: 
      #  Find the index 
        location = week.index(day.lower())
        temp = location + new_day
        if(temp > 6):
          temp = int(temp%7)
          new_day_out = ', ' + week[temp-7].title() + new_day_out
        else:
          new_day_out = ', ' + week[temp].title() + new_day_out

    new_out = new_time + new_day_out

    return new_out

5 It's easy to get wrong

1. First judge the original time is AM still PM,PM Add... To the hours 12;

2. Note that when the number of hours and minutes is less than 10 when , Minutes need to be filled in front 0, No need for hours ;

3. Various punctuation marks during output , Space 、 comma 、 Brackets , Especially the last space , Can't see , Easy to ignore ;

4. In a week 7 God , When the added time is 7 Days later , It should be noted that when extracting the day of the week, the index will exceed the range and report an error , One more step is needed to judge ;

 6 Be confused

In a given test case , The fourth use case is as follows : 

def test_period_change_at_twelve(self):
        actual = add_time("11:40 AM", "0:25")
        expected = "12:05 PM"
        self.assertEqual(actual, expected, 'Expected period to change from AM to PM at 12:00')

The explanation given is : 'Expected period to change from AM to PM at 12:00', That is to say, when more than 12 a.m. , Need to put AM Switch to PM, And its expected output is :12:05 PM .

But when it comes to the penultimate use case , as follows :

def test_two_days_later_with_day(self):
        actual = add_time("11:59 PM", "24:05", "Wednesday")
        expected = "12:04 AM, Friday (2 days later)"
        self.assertEqual(actual, expected, 'Expected calling "add_time()" with "11:59 PM", "24:05", "Wednesday" to return "12:04 AM, Friday (2 days later)"')

The expected output given is :12:04 AM, This is obviously contradictory to the previous use case , Or my understanding is wrong , These two use cases cannot pass at the same time , So I changed the expected output of the following item to the following :

expected = "0:04 AM, Friday (2 days later)"

then , It must pass . 

It's ridiculous , Evil has broken the door .

 

版权声明
本文为[Lily's autumn]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230555127075.html