[5 kyu]Mongodb ObjectID

0x00 题目内容

MongoDB is a noSQL database which uses the concept of a document, rather than a table as in SQL. Its popularity is growing.

As in any database, you can create, read, update, and delete elements. But in constrast to SQL, when you create an element, a new field _id is created. This field is unique and contains some information about the time the element was created, id of the process that created it, and so on. More information can be found in the MongoDB documentation (which you have to read in order to implement this Kata).

If you take a close look at a Codewars URL, you will notice each kata’s id (the XXX in http://www.codewars.com/dojo/katas/XXX/train/javascript) is really similar to MongoDB’s ids, which brings us to the conjecture that this is the database powering Codewars.

So let us implement the following helper which will have 2 methods:

one which verifies that the string is a valid Mongo ID string, and

one which finds the timestamp from a MongoID string

Your solution have to work as follows:

The verification method will return true if an element provided is a valid Mongo string and false otherwise:

Mongo.is_valid('507f1f77bcf86cd799439011') # True
Mongo.is_valid('507f1f77bcf86cz799439011') # False
Mongo.is_valid('507f1f77bcf86cd79943901') # False
Mongo.is_valid('111111111111111111111111') # True
Mongo.is_valid(111111111111111111111111) # False
Mongo.is_valid('507f1f77bcf86cD799439011') # False (we arbitrarily only allow lowercase letters)

The timestamp method will return a date/time object from the timestamp of the Mongo string and false otherwise:

# Mongo.get_timestamp should return a datetime object

Mongo.get_timestamp('507f1f77bcf86cd799439011') # Wed Oct 17 2012 21:13:27 GMT-0700 (Pacific Daylight Time)
Mongo.get_timestamp('507f1f77bcf86cz799439011') # False
Mongo.get_timestamp('507f1f77bcf86cd79943901') # False
Mongo.get_timestamp('111111111111111111111111') # Sun Jan 28 1979 00:25:53 GMT-0800 (Pacific Standard Time)
Mongo.get_timestamp(111111111111111111111111) # False

When you will implement this correctly, you will not only get some points, but also would be able to check creation time of all the kata here ?

0x01 思路

Mongodb 中 的 _id (ObjectId) 使用12字节的存储空间
ObjectId使用12字节的存储空间

因此满足题意的话,应该需要满足以下条件

  • 输入为数字
  • 字符串为24个字符
  • 字符串只能由0-9a-f构成
  • 从前8位字符中计算时间戳

0x02 我的解决方法

from datetime import datetime
import re
class Mongo(object):

    @classmethod
    def is_valid(cls,s):
        return (True if isinstance(s,str) and re.search(r'^[0-9a-f]*$',s) and len(s) == 24 else False)    
    
    @classmethod
    def get_timestamp(cls,s):
        if cls.is_valid(s):
            return datetime.fromtimestamp(int(s[:8],base=16))
        else:
            return False

0x03 更机智的解法

使用bool处理正则匹配的结果,且使用{}限定了字符串长度

from datetime import datetime
import re

class Mongo(object):

    @classmethod
    def is_valid(cls, s):
        return isinstance(s, str) and bool(re.match(r'[0-9a-f]{24}$', s))
    
    @classmethod
    def get_timestamp(cls, s):
        return cls.is_valid(s) and datetime.fromtimestamp(int(s[:8], 16))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值