Running Algorithms in Python

sleeping

People speak natural languages such as English, Korean Spanish, etc., so we can give them instructions (algorithms) in one of these languages and they will understand. However, in order to give the algorithmic instructions to the computer, we should use formal language which is pretty straightforward and doesn’t allow interpretations. Let’s see some application of algorithms which are used to make programs for computers. One of the easiest and most commonly used formal language is Python. In this post, I will give a brief introduction to Python by implementing two simple examples. 

Sleep Cycle

You may have heard that sleep is best in 90 minutes cycle; if you wake up at the end of five or six 90-minute cycles you will feel rested in the morning! So, you want to figure out what time you should wake up if you go to bed at a certain time. Take the user’s bedtime (hour only on a 12-hour clock) as input and display both the time you should wake up if you want to get 5×90-minutes cycles (7.5 hours of sleep) and the time you should wake up if you want to get 6×90-minutes cycles (9 hours of sleep).

Click here to try the code and write your own algorithm.


bedtime = int(input(“At what time will you go to bed?”))

alarm_1 = (bedtime + 7.5)% 12

alarm_2 = (bedtime + 9)% 12

wake_up_hour = int(alarm_1 // 1)

wake_up_minutes = int((alarm_1 % 1) * 60)

print(you should wake up at {wake_up_hour}:{wake_up_minutes} or {alarm_2}.’)


 

Recommend0 recommendationsPublished in Coding & STEAM, Lessons, Teens

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.