对角线问题


问题

这里有 n * n 矩阵,要求左上到右下的对角线上都为 x,其他地方为 y。x、y 任意值,但不相等。

x y y ... y
y x y ... y
y y x ... y
. . . ... .
. . . ... .
y y y ... x


二分查找


二分查找是一个比较简单的算法,用 C++ 语言实现如下:

template <typename T>
int binary_search(
        const T& key,  // 搜索元素 key
        vector<int>::const_iterator data,  // 数组起始位置
        int N)  // 元素个数
{
    // 左闭右闭
    int low = 0;
    int high = N - 1;

    while (low <= high) {

        int mid = low + (high - low) / 2;

        if (key < *(data + mid))
            high = mid - 1;
        else if (key > *(data + mid))
            low = mid + 1;
        else
            return mid;
    }
    return -1;
}

用法:
// vector<int> arr = {1, 2, 3, 4, 5, 65};
// cout << binary_search(5, arr.cbegin(), 6) << endl;
// cout << binary_search(1, arr.cbegin(), 6) << endl;
// cout << binary_search(4, arr.cbegin(), 6) << endl;


TypeError: super(type, obj): obj must be an instance or subtype of type


问题

今天学习《Python Web 开发实战》自定义转换器这一小节,书中有段代码如下:

class ListConverter(BaseConverter):

    def __init__(self, url_map, separator="+"):
        super(ListConverter, self).__init__(url_map)
        self.separator = urllib.parse.unquote(separator)

    def to_python(self, value):
        return value.split(self.separator)

    def to_url(self, values):
        return self.separator.join(super(ListConverter, self).to_url(value)
                                   for value in values)

倒没什么问题,是可以正常运行的。


Flask 配置方法


第一种:硬编码到代码中

app = Flask(__name__)

# 配置硬编码
app.config.update(
    DEBUG=True
)
...

app.config 是 flask.config.Config 类的实例,继承自 Python 内置数据结构 dict,因此还可以:

app.config["DEBUG"] = True