

def is_int(s):
    s = s.strip()
        
    total_len = len(s)
    if total_len==0:
        return False
    
    idx = 0
    if s[idx] in '+-':
        idx += 1
    while idx < total_len:
        if s[idx] not in '0123456789':
            return False
        idx += 1
    return True

def is_float(s):
    parts = s.split('.')
    if len(parts) == 2:
        return is_int(parts[0]) and is_int(parts[1])
    elif len(parts) == 1:
        return is_int(parts[0])
    
    return False

def is_valid_date(year, month, day):
    day_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    
    if month<1 or month>12 or day<1 or day>31:
        return False
    
    if is_leapyear(year):
        day_month[1] += 1
        
    if day > day_month[month-1]:
        return False
    
    return True
        
def is_leapyear(year):
    return year % 400 == 0 or (year % 4 == 0 and year % 100 != 0)

def output(items, nums_per_row=6, msg=""):
    if msg: 
        print(msg)
    #for idx in range(len(items)):
    for idx, item in enumerate(items):
        if type(item) == tuple:
            print(f"{item[0]}:{item[1]:2d}", end=" ")
        else:
            print(f"{item:4d}", end=" ")
        if (idx+1) % nums_per_row == 0:
            print()

def get_daysofyear(year, month, day):
    day_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    
    days = day + sum(day_month[:month-1])
    
    if month>=3 and is_leapyear(year):
        days += 1
    
    return days


def get_input_date():
    in_str = input("请输入日期(yyyy-mm-dd):")
    parts = in_str.split('-')
    if len(parts) != 3:
        return -1, -1, -1
    
    if not (is_int(parts[0]) and is_int(parts[1]) and is_int(parts[2])):
        return -1, -1, -1

    year, month, day = int(parts[0]), int(parts[1]), int(parts[2])
    if not is_valid_date(year, month, day):
        return -1, -1, -1
    
    return year, month, day

def get_user_input(msg="请输入一个整数:"):
    while True:
        in_str = input(msg)
        if not is_int(in_str):
            print("输入错误，请重试!")
            continue
        
        n = int(in_str)
        if n < 1:
            print("输入错误，请重试!")
            continue
        
        return n


def is_prime(n):
    if n == 1:
        return False
    
    for k in range(2, n):
        if n%k == 0:
            return False
        
    return True


def read_text(filename="stories.txt", encoding="utf-16"):
    text = ""
    with open(filename, mode="r", encoding=encoding) as handler:
        text = handler.read()
    return text
    