datascienceschool / book

23 stars 23 forks source link

01%20python/02.14%20%ED%8C%8C%EC%9D%B4%EC%8D%AC%EC%9D%98%20%EC%9E%90%EB%A3%8C%ED%98%95 #73

Open utterances-bot opened 1 year ago

utterances-bot commented 1 year ago

2.14 파이썬의 자료형 — 데이터 사이언스 스쿨

https://datascienceschool.net/01%20python/02.14%20%ED%8C%8C%EC%9D%B4%EC%8D%AC%EC%9D%98%20%EC%9E%90%EB%A3%8C%ED%98%95.html

scrawl-dev commented 1 year ago

import math import datetime import dateutil from dateutil.relativedelta import relativedelta

age = 0 age = math.ceil((Today - Birthday).days/365) # 올림, 내림 (import math)

today = "2022-12-05" birthday = "2000-07-18"

Today = dt.datetime.strptime(today, '%Y-%m-%d') # str 날짜 변환 (import datetime) Birthday = dt.datetime.strptime(birthday, '%Y-%m-%d')

if Today.month > Birthday.month: age = age

if Today.month == Birthday.month:

    if Today.day > Birthday.day:
        age = age
    else:
        age -= 1

else: age -= 1

with-serendipity commented 1 year ago

to = today.split('-')

birth = birthday.split('-')

def agecal(x,y): a = int(x[0]) - int(y[0]) b = int(x[1]) - int(y[1]) c = int(x[2]) - int(y[2])

if b < 0:
    a -= 1
    b += 12

if c < 0:
    b -= 1

    if int(y[1]) == 1 or 3 or 5 or 7 or 8 or 10 or 12:
        k = 31 
    elif int(y[1]) == 4 or 6 or 9 or 11:
        k = 30
    else:
        if int(y[0]) % 4 == 0 & int(y[0]) % 100 == 1 or int(y[0]) % 400 == 0:
            k = 29
        else:
            k = 28

    c += k

print ("만 나이는 %d년, %d개월, %d일 입니다." % (a, b, c))
return a, b, c, k

agecal(to,birth)

EulogiaKoine commented 1 year ago

연습문제 14.1 답변

Answer 1

# Answer 1
def isLeapYear(y):
    return not(y % 400) or (y % 100 and not(y % 4))

def yearToDate(y):
    return 365 * y + (y // 400) + (y // 4) - (y // 100)

def monthToDate(m):
    days = {1: 31, 2: 28, 3: 31, 4: 30, 5: 31, 6: 30, 7: 31, 8: 31, 9: 30, 10: 31, 11: 30, 12: 31}
    d = 0
    for i in range(1, m + 1):
        d += days[m]
    return d

def strToDate(str):
    str = str.split('-')
    return yearToDate(int(str[0]) - 1) + monthToDate(int(str[1])) + (1 if isLeapYear(int(str[0])) else 0) + int(str[2])

def calculateAge(bday, today):
    return (strToDate(today) - strToDate(bday)) // 365

Answer 2

# Answer 2
def calculateAge2(bday, today):
    bday = list(map(lambda v: int(v), bday.split('-')))
    today = list(map(lambda v: int(v), today.split('-')))
    y = today[0] - bday[0]
    if today[2] < bday[2]:
        today[1] -= 1
    if today[1] < bday[1]:
        return y - 1
    return y
lakeastern commented 1 year ago

(int(today[0:4] + today[5:7] + today[8:10]) - int(birthday[0:4] + birthday[5:7] + birthday[8:10])) // 10000

woominKIM commented 1 year ago

연습문제 2.14.1



today="2023-09-01"
birthday="1998-11-21"
t_B= list(map(int,birthday.split("-")))
t_T= list(map(int,today.split("-")))
age=t_T[0]-t_B[0] if t_T[1]>=t_B[1]&t_T[2]>=t_B[2] else t_T[0]-t_B[0]-1
print(age)

JKH-ML commented 4 days ago
# 연습 문제 2.14.1
# 오늘 날짜를 나타내는 문자열과 생일을 나타내는 문자열이 
# 다음과 같을 때 만 나이를 구하라. 
# (힌트: 숫자로 바꾼 뒤 빼기 연산을 한다.)
today = "2024-09-29"
birthday = "1900-01-19"
# 생일이 지나면 1살 추가
if int(today[5:7]+today[9:11]) > int(birthday[5:7]+birthday[9:11]): 
    age = int(today[0:4]) - int(birthday[0:4])
else:
    age = int(today[0:4]) - int(birthday[0:4]) - 1
print(age)