【每日一题20220511】星座星座

:mage:‍ 给定一个包含3个数字的元组,依次代表年、月、日。请根据占星术清单,编写一个函数,按照输入日期判断它属于哪个星座。

  • 水瓶座:1月21日至2月19日
  • 双鱼座:2月20日至3月20日
  • 白羊座:3月21日至4月20日
  • 金牛座:4月21日至5月21日
  • 双子座:5月22日至6月21日
  • 巨蟹座:6月22日至7月22日
  • 狮子座:7月23日至8月23日
  • 处女座:8月24日至9月23日
  • 天秤座:9月24日至10月23日
  • 天蝎座:10月24日至11月22日
  • 射手座:11月23日至12月21日
  • 摩羯座:12月22日至1月20日

【示例】
输入:2000,2,15
输出:水瓶座
解释:2月15号处于1月21日~2月19日之间,所以是’水瓶座’

题目难度:简单
题目来源:CodeWars-It is written in the stars

def solution(year: int, month: int, day: int)-> str:
    # your code here

assert solution(2000, 2, 15) == "水瓶座"
assert solution(1970, 6, 5) == "双子座"
assert solution(1987, 8, 23) == "狮子座"

def solution(year: int, month: int, day: int)-> str:
    
	month_per_days_limits = [20, 19, 20, 20, 21, 21, 22, 23, 23, 23, 22, 21]
	
	constellation_names = ["水瓶座","双鱼座","白羊座","金牛座","双子座","巨蟹座",
                           "狮子座","处女座","天秤座","天蝎座","射手座","摩羯座"]
	
	if day > month_per_days_limits[month-1]:
        return constellation_names[month-1]
    else:
        return constellation_names[month-2]
	
	

assert solution(2000, 2, 15) == "水瓶座"
assert solution(1970, 6, 5) == "双子座"
assert solution(1987, 8, 23) == "狮子座"
assert solution(1995, 7, 28) == "狮子座"
assert solution(2021, 1, 20) == "摩羯座"
def solution(year: int, month: int, day: int) -> str:
    star = {"水瓶座": [121, 219],
            "双鱼座": [220, 320],
            "白羊座": [321, 420],
            "金牛座": [421, 521],
            "双子座": [522, 621],
            "巨蟹座": [622, 722],
            "狮子座": [723, 823],
            "处女座": [824, 923],
            "天秤座": [924, 1023],
            "天蝎座": [1024, 1122],
            "射手座": [1123, 1221],
            "摩羯座": [1222, 120]
            }
    d = month * 100 + day
    for k, v in star.items():
        if v[1] < v[0]:
            return "摩羯座"
        elif v[0] <= d <= v[1]:
            return k


assert solution(2000, 2, 15) == "水瓶座"
assert solution(1970, 6, 5) == "双子座"
assert solution(1987, 8, 23) == "狮子座"
def solution(year: int, month: int, day: int)-> str:
    limits = [21, 20, 21, 21, 22, 22, 23, 24, 24, 24, 23, 22]
    signs = ["水瓶座", "双鱼座", "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "摩羯座"]

    if day >= limits[month - 1]:
        return signs[month - 1]
    else:
        return signs[month - 2]

def test_solution():
    assert solution(2000, 2, 15) == "水瓶座"
    assert solution(1970, 6, 5) == "双子座"
    assert solution(1987, 8, 23) == "狮子座"
    assert solution(2000, 12, 21) == "射手座"
    assert solution(2000, 1, 15) == "摩羯座"
1 个赞
def solution(year: int, month: int, day: int)-> str:
	constellation={"水瓶座":[121,219],"双鱼座":[220,320],"白羊座":[321,420],"金牛座":[421,521],
	               "双子座":[522,621],"巨蟹座":[622,722],"狮子座":[723,823],"处女座":[824,923],
	               "天秤座":[923,1023],"天蝎座":[1024,1122],"射手座":[1123,1221],"摩羯座":[1222,120]}
	for k,i in constellation.items():
		if month*100+day>=i[0] and month*100+day<=i[1]:
			return k

assert solution(2000, 2, 15) == "水瓶座"
assert solution(1970, 6, 5) == "双子座"
assert solution(1987, 8, 23) == "狮子座"
def solution(year: int, month: int, day: int)-> str:
    operation={'水瓶座':[(1,21),(2,19)],
'双鱼座':[(2,20),(3,20)],
'白羊座':[(3,21),(4,20)],
'金牛座':[(4,21),(5,21)],
'双子座':[(5,22),(6,21)],
'巨蟹座':[(6,22),(7,22)],
'狮子座':[(7,23),(8,23)],
'处女座':[(8,24),(9,23)],
'天秤座':[(9,24),(10,23)],
'天蝎座':[(10,24),(11,22)],
'射手座':[(11,23),(12,21)],
'摩羯座':[(12,22),(1,20)]
    }
    for item in operation.items():
        if item[1][1][0]==month:
            if day<=item[1][1][1]:
                return item[0]
        elif item[1][0][0]==month:
            if day>=item[1][0][1]:
                return item[0]

assert solution(2000, 2, 15) == "水瓶座"
assert solution(1970, 6, 5) == "双子座"
assert solution(1987, 8, 23) == "狮子座"