Studying Python with freeCodeCamp part2

Continuation of Scientific Computing with Python

I'm studying Python steadily at freeCodeCamp. In the previous article (https://qiita.com/makky0620/items/0f5dbcdd77b5b10cce96), I challenged ** Python for Everybody ** and ** Arithmetic Formatter **. This time I will challenge ** Time Calculator **.

Second question: Time Calculator

The final thing I want is the ʻadd_time` method, and the behavior is as follows

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)

Implementation policy

Personal point: Receive the date and time including the day of the week

As a method to convert date and time and character string to each other

there is. strftime () can output the day of the week by using a formatting code such as % A or% a. There is also a formatting code of % p for 12-hour tables like AM / PM.

import datetime

dt = datetime.datetime(2020, 7, 13)
print(dt.strftime('%A, %a'))
# Monday, Mon

However, strptime () could not receive the day of the week with% a etc. (Because you don't know the day of the week unless the date is decided ...)

import datetime

dt_str = "7:49 Wednesday"
format = "%H:%M %A"
dt = datetime.strptime(dt_str, format)

print(dt.strftime("%H:%M %A"))
# 07:49 Monday

So, I made a list of days of the week and implemented it.

weeks = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

def get_week_by_diff(week, diff):
    index = weeks.index(week)
    target_index (index + diff) % len(weeks)
    return weeks[target_index]

Finally

I felt it was easier than the previous problem.

The next issue is * Budget App *.

Recommended Posts

Studying Python with freeCodeCamp part1
Studying Python with freeCodeCamp part2
Image processing with Python (Part 2)
Bordering images with python Part 1
Scraping with Selenium + Python Part 1
Image processing with Python (Part 1)
Solving Sudoku with Python (Part 2)
Image processing with Python (Part 3)
Scraping with Selenium + Python Part 2
Studying python
Playing handwritten numbers with python Part 1
Studying Python Part.1 Creating an environment
Automate simple tasks with Python Part0
[Automation with python! ] Part 2: File operation
Excel aggregation with Python pandas Part 1
QGIS + Python Part 2
FizzBuzz with Python3
Scraping with Python
Statistics with python
FM modulation and demodulation with Python Part 3
Process Pubmed .xml data with python [Part 2]
Scraping with Python
QGIS + Python Part 1
Automate simple tasks with Python Part1 Scraping
Twilio with Python
Integrate with Python
Play with 2016-Python
100 Language Processing Knock with Python (Chapter 2, Part 2)
AES256 with python
Working with Azure CosmosDB from Python Part.2
[Python] Start studying
python starts with ()
Excel aggregation with Python pandas Part 2 Variadic
100 Language Processing Knock with Python (Chapter 2, Part 1)
with syntax (Python)
FM modulation and demodulation with Python Part 2
Python: Scraping Part 1
Bingo with python
Zundokokiyoshi with python
[Part1] Scraping with Python → Organize to csv!
Excel with Python
Python3 Beginning Part 1
Microcomputer with Python
Python: Scraping Part 2
Cast with python
How to measure execution time with Python Part 1
Create fractal shapes with python part1 (Sierpinski Gasket)
[Cloud102] # 1 Get Started with Python (Part 1 Python First Steps)
How to measure execution time with Python Part 2
Serial communication with Python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Try scraping with Python.
Learning Python with ChemTHEATER 03
"Object-oriented" learning with python
Run Python with VBA
Handling yaml with python