Every true traveler must know how to do 3 things: fix the fire, find the water and extract useful information from the nature around him. Programming won't help you with the fire and water, but when it comes to the information extraction - it might be just the thing you need.
Your task is to find the angle of the sun above the horizon knowing the time of the day. Input data: the sun rises in the East at 6:00 AM, which corresponds to the angle of 0 degrees. At 12:00 PM the sun reaches its zenith, which means that the angle equals 90 degrees. 6:00 PM is the time of the sunset so the angle is 180 degrees. If the input will be the time of the night (before 6:00 AM or after 6:00 PM), your function should return - "I don't see the sun!".

Input: The time of the day.
Output: The angle of the sun, rounded to 2 decimal places.
Example:
1、 assert sun_angle("07:00") == 15
2、 assert sun_angle("12:15") == 93.75
How it is used: One day it can save your life, if you'll be lost far away from civilization.
Precondition :
00:00 <= time <= 23:59
Best "Clear" Solution
def sun_angle(time):
h, m = list(map(int, time.split(':')))
angle = 15 * h + m / 4 - 90
return angle if 0 <= angle <= 180 else "I don't see the sun!"
if __name__ == '__main__':
print("Example:")
print(sun_angle("07:00"))
#These "asserts" using only for self-checking and not necessary for auto-testing
assert sun_angle("07:00") == 15
assert sun_angle("01:23") == "I don't see the sun!"
print("Coding complete? Click 'Check' to earn cool rewards!")
Best "Speedy" Solution
def sun_angle(time):
hh, mm = map(int, time.split(':'))
tt = hh*60+mm
return "I don't see the sun!" if (tt < 360 or 1080 < tt) else (tt-360)*0.25
if __name__ == '__main__':
print("Example:")
print(sun_angle("07:00"))
#These "asserts" using only for self-checking and not necessary for auto-testing
assert sun_angle("07:00") == 15
assert sun_angle("01:23") == "I don't see the sun!"
print("Coding complete? Click 'Check' to earn cool rewards!")
该文阐述了如何利用编程知识计算一天中太阳相对于地平线的角度,基于日出6:00AM(0度)和正午12:00PM(90度)的基准。提供了两个函数示例,输入一天中的时间,输出太阳角度或提示在夜间看不到太阳。这个技巧在野外生存中可能派上用场。
994

被折叠的 条评论
为什么被折叠?



