exercism / exercism/website-copy

A few suggestion for python - clock core exercise

Open
#1,112 6 comments 1 reaction 0 assignees View on GitHub
track/python type/mentor-notes
Dominant language
HTML
Stars
217
Forks
944
PR merge metrics
No merged PRs in 30d

Description

I'd like to add a few suggestion to make the notes better:

* I Think one possbile solution and also Pythonic (at the same time) could be this one:
```python
MINUTES_PER_HOUR = 60
MINUTES_PER_DAY = 1440

class Clock:
def __init__(self, hour, minute):
self.hour, self.minute = divmod((hour * MINUTES_PER_HOUR + minute) % MINUTES_PER_DAY, 60)

def __str__(self):
return f"{self.hour:02}:{self.minute:02}"

def __eq__(self, other):
if isinstance(other, Clock):
return (self.hour, self.minute) == (other.hour, other.minute)
return NotImplemented

def __add__(self, minutes):
return Clock(self.hour, self.minute + minutes)

def __sub__(self, minutes):
return Clock(self.hour, self.minute - minutes)
```
* It's not good idea to do this as the return statement:
```python
return self.__class__(self.h, self.m - minutes) # Because Explicit is better than implicit!
# So, this is the something that you can find in python built-in modules.
return Clock(self.h, self.m - minutes)
```
* Secondly, this is **the oldest** way that someone could substitute string in python, why this one?, you cold string format method or even f-string
```python
def __str__(self):
return '%02d:%02d' % (self.h, self.m)
# Instead
return f"{self.hour:02}:{self.minute:02}"
```
* No need to mention this is too mathematic:
```python
self.h, self.m = divmod((hour * 60 + minute) % (24 * 60), 60)
```
* And this one could much more simpler, like:
```python
return self.h == other.h and self.m == other.m
# Instead
return (self.hour., self.minute) == (other.hour, other.minute) # I really don't get it why h and m? why not hour and minute?
# tuple comparison is well worth teaching.
```

* Most people are using python 3, why you guys always subclass object:
```python
class Clock(object):
# Instead
class Clock:
```
* Again, no need to mention, divmod is here to solve this problem:
```python
class Clock(object):
def __init__(self, hour, minute):
while minute >= 60:
hour += 1
minute -= 60
while minute < 0:
hour -= 1
minute += 60
self.minute = minute
self.hour = hour % 24
```

Having said this, I saw this implementation which is far better but maybe hard for a newbie python developer to understand:
```python
from datetime import date, datetime, time, timedelta

class Clock:
def __init__(self, hours, minutes):
start = datetime.combine(date.today(), time(0, 0))
self.dt = start + timedelta(hours=hours, minutes=minutes)

def __str__(self):
return self.dt.strftime('%H:%M')

def __eq__(self, other):
if not isinstance(other, Clock):
return NotImplemented
return (self.dt.time()) == (other.dt.time())

def __add__(self, minutes):
self.dt += timedelta(minutes=minutes)
return self

def __sub__(self, minutes):
self.dt -= timedelta(minutes=minutes)
return self
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.