当前位置:网站首页>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
边栏推荐
- Oracle clear SQL cache
- PG SQL intercepts the string to the specified character position
- About me
- Django::Did you install mysqlclient?
- [code analysis (5)] communication efficient learning of deep networks from decentralized data
- OSS cloud storage management practice (polite experience)
- Port occupied 1
- Analysis of unused index columns caused by implicit conversion of timestamp
- sys. dbms_ scheduler. create_ Job creates scheduled tasks (more powerful and rich functions)
- [code analysis (6)] communication efficient learning of deep networks from decentralized data
猜你喜欢
elmo(BiLSTM-CRF+elmo)(Conll-2003 命名实体识别NER)
Tersus notes employee information 516 MySQL query (time period uniqueness judgment of 2 fields)
SQL learning window function
淘宝发布宝贝提示“您的消保保证金额度不足,已启动到期保障”
Information: 2021 / 9 / 29 10:01 - build completed with 1 error and 0 warnings in 11S 30ms error exception handling
Small case of web login (including verification code login)
Express②(路由)
Usereducer basic usage
Leetcode brush question 𞓜 13 Roman numeral to integer
Detailed explanation of redis (Basic + data type + transaction + persistence + publish and subscribe + master-slave replication + sentinel + cache penetration, breakdown and avalanche)
随机推荐
Interval query through rownum
Publish custom plug-ins to local server
Apache seatunnel 2.1.0 deployment and stepping on the pit
Two ways to deal with conflicting data in MySQL and PG Libraries
Use future and countdownlatch to realize multithreading to execute multiple asynchronous tasks, and return results after all tasks are completed
Express ② (routing)
【报名】TF54:工程师成长地图与卓越研发组织打造
MySQL index [data structure + index creation principle]
Django::Did you install mysqlclient?
Dolphin scheduler source package Src tar. GZ decompression problem
leetcode--357. 统计各位数字都不同的数字个数
Detailed explanation and usage of with function in SQL
Troubleshooting of expdp export error when Oracle table has logical bad blocks
GDB的使用
MySQL [read / write lock + table lock + row lock + mvcc]
pycharm Install packages failed
redis如何解决缓存雪崩、缓存击穿和缓存穿透问题
Basic SQL query and learning
Leetcode brush question 897 incremental sequential search tree
RAC environment alert log error drop transient type: systp2jw0acnaurdgu1sbqmbryw = = troubleshooting