Skip to content

Instantly share code, notes, and snippets.

@Surgo
Created January 31, 2011 01:52
Show Gist options
  • Save Surgo/803525 to your computer and use it in GitHub Desktop.
Save Surgo/803525 to your computer and use it in GitHub Desktop.
SRM490 - div2 - level one
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""SRM492 - div2 - level one
Suppose that we're given a moment of time written as HH:MM,
where HH is the hour and MM is the minutes. Let's say that
this moment is lucky if it is formatted AB:AB, AA:BB or AB:BA,
where both occurrences of A stand for the same digit and both
occurrences of B also stand for the same digit. It is allowed
for the digits represented by A and B to be the same as well.
You are given a String[] moments, where each element represents
a single moment of time. Return how many of these time moments
are lucky.
Definition:
Class: LuckyCounter
Method: countLuckyMoments
Parameters: String[]
Returns: int
Method signature: int countLuckyMoments(String[] moments)
"""
class LuckyCounter(object):
"""Time Travelling Cellar
Constraints:
- moments will contain between 1 and 50 elements, inclusive.
- Each element of moments will be formatted "HH:MM"
(quotes for clarity), where HH is between 00 and 23,
inclusive, and MM is between 00 and 59, inclusive.
>>> counter = LuckyCounter()
>>> print counter.countLuckyMoments(["12:21", "11:10", ])
1
>>> print counter.countLuckyMoments(["00:00", "00:59", "23:00", ])
1
>>> print counter.countLuckyMoments(["12:34", ])
0
>>> print counter.countLuckyMoments(
... ["12:11", "22:22", "00:01", "03:30", "15:15", "16:00", ])
3
"""
"""
12:21 is lucky, while 11:10 is not.
Only 00:00 is lucky here (note that it is formatted AB:AB, AA:BB and AB:BA at the same time).
"""
def __init__(self):
pass
def countLuckyMoments(self, moments):
return len([t for t in moments \
if (t[0]==t[3] and t[1]==t[4]) \
or (t[0]==t[1] and t[3]==t[4]) \
or (t[0]==t[4] and t[1]==t[3])])
if __name__ == '__main__':
import doctest
doctest.testmod()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment