当前位置:网站首页>freeCodeCamp----time_ Calculator exercise
freeCodeCamp----time_ Calculator exercise
2022-04-23 13:51:00 【Lily's autumn】
Catalog
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
边栏推荐
- UML Unified Modeling Language
- Influence of openssh version on SSH mutual trust creation in RAC environment
- Using Jupiter notebook in virtual environment
- Static interface method calls are not supported at language level '5'
- [code analysis (5)] communication efficient learning of deep networks from decentralized data
- Function executes only the once function for the first time
- JUC interview questions about synchronized, ThreadLocal, thread pool and atomic atomic classes
- 自动化的艺术
- FDFS start
- Oracle creates tablespaces and modifies user default tablespaces
猜你喜欢
redis如何解决缓存雪崩、缓存击穿和缓存穿透问题
SQL learning window function
Using Baidu Intelligent Cloud face detection interface to achieve photo quality detection
Leetcode | 38 appearance array
【报名】TF54:工程师成长地图与卓越研发组织打造
Dolphin scheduler source package Src tar. GZ decompression problem
Technologie zéro copie
Handling of high usage of Oracle undo
UML Unified Modeling Language
初探 Lambda Powertools TypeScript
随机推荐
Zero copy technology
Move blog to CSDN
Oracle modify default temporary tablespace
Oracle defines self incrementing primary keys through triggers and sequences, and sets a scheduled task to insert a piece of data into the target table every second
剑南春把文字游戏玩明白了
零拷貝技術
Set Jianyun x Feishu Shennuo to help the enterprise operation Department realize office automation
About me
Utilisation de GDB
MySQL [SQL performance analysis + SQL tuning]
Get the attribute value difference between two different objects with reflection and annotation
Generate 32-bit UUID in Oracle
自动化的艺术
Static interface method calls are not supported at language level '5'
Oracle calculates the difference between two dates in seconds, minutes, hours and days
MySQL index [data structure + index creation principle]
Jiannanchun understood the word game
Handling of high usage of Oracle undo
Leetcode brush question 𞓜 13 Roman numeral to integer
The query did not generate a result set exception resolution when the dolphin scheduler schedules the SQL task to create a table