ARTS第三周

Algorithm

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
Example 1:

1
2
Input: 121
Output: true

Example 2:

1
2
3
Input: -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Example 3:

1
2
3
Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

answer

1
2
3
4
5
6
7
class Solution(object):
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
return str(x) == str(x)[::-1]

Rewiew

https://towardsdatascience.com/10-git-commands-you-should-know-df54bea1595c
文章介绍了平时开发过程中常用的git命令,以及如何利用别名更快的使用git命令。

Tip

近期在做项目时有一个类似规则引擎的需求,就是根据消息中某些字段的值判断需要执行的处理函数,期望能通过配置实现。调研了部分规则引擎,最终选择business_rules的开源工具,git地址为https://github.com/venmo/business-rules 。使用方式简单,编写变量类,动作类,通过规则配置变量条件与动作的映射关系。实例代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import datetime

from business_rules.variables import BaseVariables
from business_rules.variables import numeric_rule_variable, string_rule_variable, select_rule_variable
from business_rules.actions import BaseActions, rule_action
from business_rules.fields import FIELD_NUMERIC, FIELD_SELECT
from business_rules import run_all


class ProductVariables(BaseVariables):

def __init__(self, product):
self.product = product

@numeric_rule_variable
def current_inventory(self):
return self.product.current_inventory

@numeric_rule_variable(label='Days until expiration')
def expiration_days(self):
last_order = self.product.orders[-1]
return (last_order.expiration_date - datetime.date.today()).days

@string_rule_variable()
def current_month(self):
return datetime.datetime.now().strftime("%B")



class ProductActions(BaseActions):

def __init__(self, product):
self.product = product

@rule_action(params={"sale_percentage": FIELD_NUMERIC})
def put_on_sale(self, sale_percentage):
self.product.price = (1.0 - sale_percentage) * self.product.price
self.product.save()

@rule_action(params=[{'fieldType': FIELD_SELECT,
'name': 'stock_state',
'label': 'Stock state',
'options': [
{'label': 'Available', 'name': 'available'},
{'label': 'Last items', 'name': 'last_items'},
{'label': 'Out of stock', 'name': 'out_of_stock'}
]}])
def change_stock_state(self, stock_state):
self.product.stock_state = stock_state
self.product.save()


rules = [
# expiration_days < 5 AND current_inventory > 20
{ "conditions": { "all": [
{ "name": "current_inventory",
"operator": "greater_than",
"value": 20,
},
]},
"actions": [
{ "name": "put_on_sale",
"params": {"sale_percentage": 0.25},
},
],
},
]
class Product(object):

def __init__(self):
self.current_inventory = 100
self.price = 1

def save(self):
print("save....{},{}".format(self.current_inventory, self.price))

product = Product()

run_all(rule_list=rules,
defined_variables=ProductVariables(product),
defined_actions=ProductActions(product),
stop_on_first_trigger=True
)

ProductVariables为变量类,定义了某变量与变量类成员变量的关系;ProductActions为动作类,定义了处理函数;
rules定义了某条件下调用的处理函数;通过调用run_all()即可运行该规则引擎。

Share

https://www.cloudamqp.com/blog/2015-05-18-part1-rabbitmq-for-beginners-what-is-rabbitmq.html
对rabbit mq进行介绍的系列文章,可以对rabbit mq有初步认识,理解相关概念。