How to install htop on CentOS

htop is an interactive process viewer and manager for UNIX/Linux systems. It is a text-mode application and easy to use.

I usually use htop to show running process on my Linux servers even on my MacBook Pro.

Read More

C语言中创建动态数组

还是先上代码吧。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//
// Created by ismdeep on 2019-01-28.
//

#ifndef ISMDEEP_ARRAY_H
#define ISMDEEP_ARRAY_H

#include <stdint.h>

void * create_1d_arr(size_t size, size_t sizeof_item) {
void * arr = malloc(sizeof_item * size);
return arr;
}

void ** create_2d_arr(size_t rows, size_t cols, size_t sizeof_item) {
void ** arr = (void **)malloc(sizeof(size_t) * rows);
for (size_t row_id = 0; row_id < rows; ++row_id) {
arr[row_id] = malloc(sizeof_item * cols);
}
return arr;
}


#endif //ISMDEEP_ARRAY_H

Read More

计算排列数

1
2
3
4
5
6
(defun permutation-count (n c)
(let ((ans 1))
(loop for i from (- n (1- c)) upto n do (setf ans (* ans i))) ans))


(format t "~D~%" (permutation-count 6 5))

Read More

git commit 规范指南

Git 每次提交代码,都要写 Commit message(提交说明),否则就不允许提交。但是,一般来说,commit message 应该清晰明了,说明本次提交的目的。

目前,社区有多种 Commit message 的写法规范。本文介绍Angular 规范是目前使用最广的写法,比较合理和系统化,并且有配套的工具。前前端框架Angular.js采用的就是该规范。

Read More

45句话读懂《高效程序员的45个习惯》

1、不要抱怨,把注意力集中到解决问题上来。

2、了解清楚情况,比如团队风格,业务需求等,才动手编码。

3、指出问题,当然,更好的做法是礼貌一点。

4、勇敢的说出实情,然后努力的去解决问题。

5、用邓公的话来说:与时俱进,开拓进取。

Read More

Common Lisp 矩阵的逆

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
(defun mmul (A B)
(let* ((m (car (array-dimensions A)))
(n (cadr (array-dimensions A)))
(l (cadr (array-dimensions B)))
(C (make-array `(,m ,l) :initial-element 0)))
(loop for i from 0 to (- m 1) do
(loop for k from 0 to (- l 1) do
(setf (aref C i k)
(loop for j from 0 to (- n 1)
sum (* (aref A i j)
(aref B j k))))))
C))

;; Cofactor i j
(defun matrix-cofactor (A i j)
(let* (
(tmp 0)
(n (car (array-dimensions A)))
(ans (make-array `(,(- n 1) ,(- n 1)) :initial-element 0)))
(loop for ii from 0 to (- i 1) do
(loop for jj from 0 to (- j 1) do (setf (aref ans ii jj) (aref A ii jj)))
(loop for jj from (+ j 1) to (- n 1) do (setf (aref ans ii (- jj 1)) (aref A ii jj))))
(loop for ii from (+ i 1) to (- n 1) do
(loop for jj from 0 to (- j 1) do (setf (aref ans (- ii 1) jj) (aref A ii jj)))
(loop for jj from (+ j 1) to (- n 1) do (setf (aref ans (- ii 1) (- jj 1)) (aref A ii jj))))
ans))

;; Calculate det value of matrix A
(defun matrix-det (A)
(let* ((n (car (array-dimensions A))) (tmp 0))
(if (= n 1)
(aref A 0 0)
(let ((tmp 0) (MA 0))
(loop for j from 0 to (- n 1) do
(setf MA (matrix-cofactor A 0 j))
(if (= (mod j 2) 0)
(setf tmp (+ tmp (* (aref A 0 j) (matrix-det MA))))
(setf tmp (- tmp (* (aref A 0 j) (matrix-det MA))))))
tmp))))

;; Calculate inverse matrix of matrix A
(defun matrix-inverse (A)
(let* ((n (car (array-dimensions A))) (tmp 0) (inv-A (make-array `(,n ,n) :initial-element 0)) (det-val 0))
(setf det-val (matrix-det A))
(loop for i from 0 to (- n 1) do
(loop for j from 0 to (- n 1) do
(setf (aref inv-A j i) (/ (matrix-det (matrix-cofactor A i j)) det-val))))
inv-A))

(let ((g 0) (g0 0))
(setf g (make-array '(3 3) :initial-contents '((1 0 0) (2 2 0) (3 4 5))))
(setf g0 (make-array '(1 1) :initial-contents '((2))))
; (matrix-cofactor g 1 1)
(format t "~A~%" (matrix-inverse g)))

Read More

LU分解之doolittle算法

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
;; author: ismdeep
;; blog: https://ismdeep.com
;; blog entry: https://ismdeep.com/238
;; doolittle algorithm to decompose a square matrix

(defun mmul (A B)
(let* ((m (car (array-dimensions A)))
(n (cadr (array-dimensions A)))
(l (cadr (array-dimensions B)))
(C (make-array `(,m ,l) :initial-element 0)))
(loop for i from 0 to (- m 1) do
(loop for k from 0 to (- l 1) do
(setf (aref C i k)
(loop for j from 0 to (- n 1)
sum (* (aref A i j)
(aref B j k))))))
C))

;; Using doolittle algorithm to decomposes a square matrix A for LU = A and LDV = A
(defun doolittle-lu (A)
(let* ((n (car (array-dimensions A))) (tmp 0)
(L (make-array `(,n ,n) :initial-element 0))
(U (make-array `(,n ,n) :initial-element 0))
(D (make-array `(,n ,n) :initial-element 0))
(V (make-array `(,n ,n) :initial-element 0))
(lu-mmul 0)
(ldv-mmul 0))

(loop for j from 0 to (- n 1) do (setf (aref U 0 j) (aref A 0 j)))
(loop for i from 0 to (- n 1) do (setf (aref L i i) 1))
(loop for i from 1 to (- n 1) do
; generate L
(loop for j from 0 to (- i 1) do
(setf tmp (aref A i j))
(loop for k from 0 to (- j 1) do (setf tmp (- tmp (* (aref L i k) (aref U k j)))))
(setf (aref L i j) (/ tmp (aref U j j)))
)
; generate U
(loop for j from i to (- n 1) do
(setf tmp (aref A i j ))
(loop for k from 0 to (- i 1) do (setf tmp (- tmp (* (aref L i k) (aref U k j)))))
(setf (aref U i j) tmp)
)
)

; output A
(format t "---------- A ------------------~%")
(loop for i from 0 to (- n 1) do
(loop for j from 0 to (- n 1) do
(format t "~4A " (aref A i j)))
(format t "~%"))

; output L
(format t "~%~%---------- L ------------------~%")
(loop for i from 0 to (- n 1) do
(loop for j from 0 to (- n 1) do
(format t "~4A " (aref L i j)))
(format t "~%"))

; output U
(format t "~%~%---------- U ------------------~%")
(loop for i from 0 to (- n 1) do
(loop for j from 0 to (- n 1) do
(format t "~4A " (aref U i j)))
(format t "~%"))

; output L * U
(setf lu-mmul (mmul L U))
(format t "~%~%---------- L * U --------------~%")
(loop for i from 0 to (- n 1) do
(loop for j from 0 to (- n 1) do
(format t "~4A " (aref lu-mmul i j)))
(format t "~%"))

; generate D
(loop for i from 0 to (- n 1) do
(setf (aref D i i) (aref U i i)))

; generate V
(loop for i from 0 to (- n 1) do
(loop for j from i to (- n 1) do
(setf (aref V i j) (/ (aref U i j) (aref U i i)))))

; output D
(format t "~%~%---------- D ------------------~%")
(loop for i from 0 to (- n 1) do
(loop for j from 0 to (- n 1) do
(format t "~4A " (aref D i j)))
(format t "~%"))

; output V
(format t "~%~%---------- V ------------------~%")
(loop for i from 0 to (- n 1) do
(loop for j from 0 to (- n 1) do
(format t "~4A " (aref V i j)))
(format t "~%"))


; output L * D * V
(setf ldv-mmul (mmul (mmul L D) V))
(format t "~%~%---------- L * D * V ----------~%")
(loop for i from 0 to (- n 1) do
(loop for j from 0 to (- n 1) do
(format t "~4A " (aref ldv-mmul i j)))
(format t "~%"))


;; Return L and U
(values L U)))


(let ((g 0))
(setf g (make-array '(4 4) :initial-contents '((2 1 -5 1) (1 -3 0 -6) (0 2 -1 2) (1 4 -7 6))))
(doolittle-lu g))

Read More

整数拆分算法(递归实现)

对于输入的数字n,那么要解决的问题就是(n,n),对于第一个n是表示要拆分的数字n,第二个n表示拆分当前数字能使用的最大数字。

那么 (n,m) 可以表示成 $ \sum_{cur = 1}^{m} cur + (n - i, max(m, n - i)) $

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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MIN(a,b) ((a) < (b)) ? (a) : (b)

#define MAX_VAL 1024

int a[MAX_VAL];
int cur_index;

void do_print() {
for (int i = 0; i <= cur_index; ++i) {
printf("%d ", a[i]);
}
printf("\n");
}

void split(int val, int max_item) {
if (val <= 0) {
do_print();
return;
}

for (int cur_item = MIN(max_item, val); cur_item >= 1; --cur_item) {
++cur_index;
a[cur_index] = cur_item;
split(val - cur_item, cur_item);
--cur_index;
}
}

int main() {
memset(a, 0, sizeof(int));
int val;
scanf("%d", &val);
if (val > MAX_VAL) {
fprintf(stderr, "Can not process input value with is bigger than 1024.\n");
return 0;
}
cur_index = -1;
split(val, val);
return 0;
}

Read More

Linux 通过 OpenMP 实现并行编程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

#define PARALLEL_SIZE 4

int fibonacci(int n)
{
return n <= 2 ? 1 : fibonacci (n - 1) + fibonacci (n - 2);
}

int main()
{
#pragma omp parallel for
for (int i = 0; i < PARALLEL_SIZE; i++)
{
cout << fibonacci(50) << endl;
}
return 0;
}

Read More

老男孩Python全栈工程师教程第9期

全栈9期(第一部分):基础+模块+面向对象+网络编程

全栈9期(第二部分):并发编程+数据库+前端

全栈9期(第三部分):django基础

全栈9期(第四部分):django进阶

全栈9期(第五部分):django高级

Read More