当前位置:网站首页>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 middleware ③ (custom Middleware)
- Use future and countdownlatch to realize multithreading to execute multiple asynchronous tasks, and return results after all tasks are completed
- Operations related to Oracle partition
- [machine learning] Note 4. KNN + cross validation
- Solution of discarding evaluate function in surprise Library
- Analysis of unused index columns caused by implicit conversion of timestamp
- Dolphin scheduler configuring dataX pit records
- [VMware] address of VMware Tools
- 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
猜你喜欢

Solution of discarding evaluate function in surprise Library

2021年秋招,薪资排行NO

Dolphin scheduler configuring dataX pit records

OSS cloud storage management practice (polite experience)

Oracle job scheduled task usage details

服务器中挖矿病毒了,屮

Multithreading

Information: 2021 / 9 / 29 10:01 - build completed with 1 error and 0 warnings in 11S 30ms error exception handling

Window analysis function last_ VALUE,FIRST_ VALUE,lag,lead

Tersus notes employee information 516 MySQL query (time period uniqueness judgment of 2 fields)
随机推荐
Kettle--控件解析
MySQL [acid + isolation level + redo log + undo log]
Lenovo Savior y9000x 2020
JS compares different elements in two arrays
Using Jupiter notebook in virtual environment
pycharm Install packages failed
UNIX final exam summary -- for direct Department
Special window function rank, deny_ rank, row_ number
Analysis of unused index columns caused by implicit conversion of timestamp
【项目】小帽外卖(八)
Jiannanchun understood the word game
Technologie zéro copie
Oracle database combines the query result sets of multiple columns into one row
[code analysis (4)] communication efficient learning of deep networks from decentralized data
Oracle view related
SSM project deployed in Alibaba cloud
Oracle kills the executing SQL
聯想拯救者Y9000X 2020
零拷貝技術
低频量化之明日涨停预测