Problem of the Day
A new programming or logic puzzle every Mon-Fri

Reminder System

Today's problem will lead us on the path to creating a custom reminder system. The reminder system works with the two follow formats:

  • in 10 (always in minutes)

  • at 0900 (you're free to use whichever format for this input you want as long as you use "at" as the identifier)

When the reminder time hits you're your program should play a system sound/beep ("\a" should work for a few languages).

Permalink: http://problemotd.com/problem/reminder-system/

Comments:

  • Opetich - 10 years, 7 months ago

    You are program? i'm no program!

    reply permalink

  • Max Burstein - 10 years, 7 months ago

    lol whoops. I fixed it

    reply permalink

  • Opetich - 10 years, 7 months ago

    :) Good job in getting problems everyday. even through i don't comment regulary it still is interesting thinking of them while on the bus.

    reply permalink

  • D - 10 years, 7 months ago

    This one's in Java 7. ReminderService.java:

    import java.io.Console;
    import java.util.Calendar;
    import java.util.concurrent.Executors;
    import java.util.concurrent.ScheduledExecutorService;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    import static java.util.concurrent.TimeUnit.MILLISECONDS;
    import static java.util.concurrent.TimeUnit.MINUTES;
    
    /**
     * Scheduling service that reads reminders from the console, and writes the
     * scheduled reminders to standard output when they're due.
     */
    public class ReminderService {
    
        public static final Pattern REMINDER_PATTERN_IN = Pattern.compile("^in (\\d+) (.*)$");
        public static final Pattern REMINDER_PATTERN_AT = Pattern.compile("^at (\\d\\d\\d\\d) (.*)$");
    
        private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
    
        public void start() {
            Console console = System.console();
            for (String line = console.readLine(); line != null; line = console.readLine()) {
                Matcher inMatcher = REMINDER_PATTERN_IN.matcher(line);
                Matcher atMatcher = REMINDER_PATTERN_AT.matcher(line);
                if (inMatcher.matches()) {
                    long minutes = Long.valueOf(inMatcher.group(1));
                    scheduleReminder(minutes, inMatcher.group(2));
                } else if (atMatcher.matches()) {
                    long minutes = atStringAsMinutes(atMatcher.group(1));
                    scheduleReminder(minutes, atMatcher.group(2));
                } else {
                    System.out.println("Format is \"in <minutes> <text>\" " +
                                       "or \"at <HHMM> <text>\"");
                }
            }
            scheduler.shutdownNow();
        }
    
        private long atStringAsMinutes(String atString) {
            assert atString.matches("^\\d\\d\\d\\d$");
    
            Calendar now = Calendar.getInstance();
            Calendar then = Calendar.getInstance();
            then.set(Calendar.HOUR_OF_DAY, Integer.valueOf(atString.substring(0, 2)));
            then.set(Calendar.MINUTE, Integer.valueOf(atString.substring(2, 4)));
            if (now.getTimeInMillis() > then.getTimeInMillis()) {
                // atString is already in the past; schedule for tomorrow
                then.add(Calendar.DAY_OF_YEAR, 1);
            }
    
            return MILLISECONDS.toMinutes(then.getTimeInMillis()-now.getTimeInMillis());
        }
    
        private void scheduleReminder(long minutes, final String reminderText) {
            scheduler.schedule(new Runnable() {
                @Override
                public void run() {
                    // "\007" is the ASCII bell character
                    System.out.println("\007Reminder: " + reminderText);
                }
            }, minutes, MINUTES);
        }
    }
    

    And the main method in ReminderRunner.java:

    public class ReminderRunner {
    
        public static void main(String[] args) {
            new ReminderService().start();
        }
    
    }
    

    Code reviews welcome!

    reply permalink

  • keemz - 10 years, 7 months ago

    run in command line like so: "python reminder.py at 1215" or "python reminder.py in 12"

    
    import time
    import sys
    from threading import Timer
    
    def alert():
        print "\a"
    
    def printHelp(): 
        print "please pass the mode and time arguments like so: \"in 10\" or \"at 0900\""
        exit()
    
    if len(sys.argv) is not 3:
        printHelp()
    
    mode = sys.argv[1]
    alarmTime = sys.argv[2]
    
    #process relative alarm time
    if mode == "in":
        alarmTime = int(alarmTime) * 60
        Timer(alarmTime, alert).start()
    
    #process explicit alarm times
    elif mode == "at":
    
        #time is not in the correct format
        if not len(alarmTime) == 4:
            printHelp()
    
        #get the current time and split by HH:MM:SS
        curTime = time.strftime("%H%M%S", time.localtime())
        curHour = int(curTime[0] + curTime[1])
        curMin = int(curTime[2] + curTime[3])
        curSec = int(curTime[4] + curTime[5])
    
        alHour = int(alarmTime[0] + alarmTime[1])
        alMin = int(alarmTime[2] + alarmTime[3])
    
        delay = 0
    
        #if the current time is past the alarm, schedule for next day
        if curHour > alHour or (curHour == alHour and curMin > alMin):
            delay = 24 * 60 * 60
    
        # calculate the required delay
        hrs = (alHour - curHour) * 60 * 60
        mins =(alMin - curMin) * 60
    
        delay = delay + hrs + mins - curSec
    
        Timer(delay, alert).start()
    
    else:
        printHelp()
    
    

    reply permalink

Content curated by @MaxBurstein