当前位置:网站首页>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
边栏推荐
- Express ② (routing)
- [code analysis (1)] communication efficient learning of deep networks from decentralized data
- JS compares different elements in two arrays
- SQL learning | complex query
- 19c environment ora-01035 login error handling
- Oracle and MySQL batch query all table names and table name comments under users
- Lenovo Saver y9000x 2020
- Detailed explanation of Oracle tablespace table partition and query method of Oracle table partition
- 商家案例 | 运动健康APP用户促活怎么做?做好这几点足矣
- Personal learning related
猜你喜欢
UML统一建模语言
SQL learning | complex query
[machine learning] Note 4. KNN + cross validation
Oracle job scheduled task usage details
Technologie zéro copie
Set Jianyun x Feishu Shennuo to help the enterprise operation Department realize office automation
Express②(路由)
Basic SQL query and learning
MySQL [read / write lock + table lock + row lock + mvcc]
OSS cloud storage management practice (polite experience)
随机推荐
Jiannanchun understood the word game
Three characteristics of volatile keyword [data visibility, prohibition of instruction rearrangement and no guarantee of operation atomicity]
初探 Lambda Powertools TypeScript
UML统一建模语言
【项目】小帽外卖(八)
Database transactions
淘宝发布宝贝提示“您的消保保证金额度不足,已启动到期保障”
[machine learning] Note 4. KNN + cross validation
Opening: identification of double pointer instrument panel
ARGB transparency conversion
JS time to get this Monday and Sunday, judge the time is today, before and after today
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
Using Baidu Intelligent Cloud face detection interface to achieve photo quality detection
Reading notes: fedgnn: Federated graph neural network for privacy preserving recommendation
Apache Atlas Compilation and installation records
Oracle clear SQL cache
Leetcode | 38 appearance array
UML Unified Modeling Language
Analysis of unused index columns caused by implicit conversion of timestamp
About me