*"Life is all about sharing. If we are good at something, pass it on." - Mary Berry*

### SICP Exercise 2.42: Eight queens puzzle

2021-10-29

Exercise 2.42: The “eight-queens puzzle” asks how to place eight queens on a chessboard so that no queen is in check from any other (i.e., no two queens are in the same row, column, or diagonal).

One way to solve the puzzle is to work across the board, placing a queen in each column. Once we have placed

`k - 1`

queens, we must place the`k`

^{th}queen in a position where it does not check any of the queens already on the board.

### SICP Exercise 2.41: Triple sum

2021-10-18

Exercise 2.41: Write a procedure to find all ordered triples of distinct positive integers

`i`

,`j`

, and`k`

less than or equal to a given integer`n`

that sum to a given integer`s`

.

`unique-triples`

can be written easily base on `unique-pairs`

in 2.40:

1(define (unique-triples n) 2 (flatmap 3 (lambda (i) 4 (flatmap 5 (lambda (j) 6 (map (lambda (k) (list i j k)) 7 (enumerate-interval 1 (- j 1)))) 8 (enumerate-interval 1 (- i 1)))) 9 (enumerate-interval 1 n)))

### SICP Exercise 2.35: Counting leaves of a tree

2021-10-14

Exercise 2.35: Redefine count-leaves from section 2.2.2 as an accumulation:

1(define (count-leaves t) 2 (accumulate <??> <??> (map <??> <??>)))

The `count-leaves`

procedure from section 2.2.2:

1(define (count-leaves x) 2 (cond ((null? x) 0) 3 ((not (pair? x)) 1) 4 (else (+ (count-leaves (car x)) 5 (count-leaves (cdr x))))))

### SICP Exercise 2.27: Reversing nested lists

2021-10-12

Exercise 2.27: Modify your reverse procedure of exercise 2.18 to produce a deep-reverse procedure that takes a list as argument and returns as its value the list with its elements reversed and with all sublists deep-reversed as well. For example,

1(define x (list (list 1 2) (list 3 4))) 2 3x 4((1 2) (3 4)) 5 6(reverse x) 7((3 4) (1 2)) 8 9(deep-reverse x) 10((4 3) (2 1))

First, look at my `reverse`

procedure:

1#lang racket/base 2(require racket/trace) 3 4(define (reverse items) 5 (iter items null)) 6 7(define (iter remaining result) 8 (trace iter) 9 (if (null? remaining) 10 result 11 (iter (cdr remaining) (cons (car remaining) result)))) 12 13(trace reverse) 14(reverse (list (list 1 2) (list 3 4)))

### SICP Exercise 1.25: A simpler expmod?

2021-09-30

Exercise 1.25: Alyssa P. Hacker complains that we went to a lot of extra work in writing

`expmod`

. After all, she says, since we already know how to compute exponentials, we could have simply written:1(define (expmod base exp m) 2 (remainder (fast-expt base exp) m))Is she correct? Would this procedure serve as well for our fast prime tester? Explain.

First, look at the original algorithm:

1 (define (expmod base exp m) 2 (cond ((= exp 0) 1) 3 ((even? exp) 4 (remainder 5 (square (expmod base (/ exp 2) m)) 6 m)) 7 (else 8 (remainder 9 (* base (expmod base (- exp 1) m)) 10 m))))

### SICP Exercise 1.16: Iterative Exponentiation

2021-09-29

I am reading SICP.

Section 1.2.4 talks about the problem of computing the exponential of a given number.

The authors start with a recursive procedure:

1#lang sicp 2 3(define (expt b n) 4 (if (= n 0) 5 1 6 (* b (expt b (- n 1)))))

This requires O(n) steps and O(n) space.

then an iterative procedure:

1#lang sicp 2 3(define (expt b n) 4 (expt-iter b n 1)) 5 6(define (expt-iter b counter product) 7 (if (= counter 0) 8 product 9 (expt-iter b 10 (- counter 1) 11 (* b product))))

### SICP Exercise 1.14: orders of growth of count-change

2021-09-28

Exercise 1.14: Draw the tree illustrating the process generated by the

`count-change`

procedure of section 1.2.2 in making change for 11 cents. What are the orders of growth of the space and number of steps used by this process as the amount to be changed increases?

The `count-change`

procedure:

1(trace-define (count-change amount) 2 (cc amount 5)) 3 4(trace-define (cc amount kinds-of-coins) 5 (cond ((= amount 0) 1) 6 ((or (< amount 0) (= kinds-of-coins 0)) 0) 7 (else (+ (cc amount 8 (- kinds-of-coins 1)) 9 (cc (- amount 10 (first-denomination kinds-of-coins)) 11 kinds-of-coins))))) 12 13(define (first-denomination kinds-of-coins) 14 (cond ((= kinds-of-coins 1) 1) 15 ((= kinds-of-coins 2) 5) 16 ((= kinds-of-coins 3) 10) 17 ((= kinds-of-coins 4) 25) 18 ((= kinds-of-coins 5) 50)))

### URI network-path reference

2021-09-23

#### Problem

I’ve just updated the code to always prefix the post URI with a slash:

1if !strings.HasPrefix(p.URI, "/") { 2 p.URI = "/" + p.URI 3}

and I think that no need to update the HTML template:

1<a href="/{{ .URI }}">{{ .Title }}</a>

as the redundant slash will be removed. But I was wrong.

Looking at the link address, instead of seeing:

```
1http://localhost//2021/09/23/uri-network-path-reference
```

I saw:

```
1http://0.0.7.229/09/23/uri-network-path-reference
```

What’s going on?

#### Troubleshooting

The first question comes to my mind is what does `href`

do if it begins with two slashes?

### Do not use jq when working with large number

2021-09-07

We use etcd to store application configuration. On production, config is loaded from json file by using Python.

I’m wondering if we can use jq to do that. So, I tried something like this:

1host=${ETCD_HOST:-127.0.0.1} 2port=${ETCD_PORT:-2379} 3for key in $(jq -r 'keys[]' "$1"); do 4 value=$(jq -r ".$key" -c "$1") 5 ETCDCTL_API=3 etcdctl --endpoints="$host:$port" put "$key" "$value" 6done

Config is loaded into `etcd`

but some values are not the same as in the JSON file.
What is going on?

### Automate office tasks with Python

2021-06-10

Hàng tháng bạn nhận được một file excel về danh sách D. Từ danh sách này bạn cần filter cột C theo tiêu chí T1 để lấy ra danh sách D1. Có được D1 rồi bạn Save As thành t1.xlsx. Làm xong T1, bạn tiếp tục với T2 -> t2.xlsx … Làm tiếp cho đến Tn -> tn.xlsx. Có được các danh sách t1.xlsx -> tn.xlsx này rồi, giờ bạn cần gửi mail:

- t1.xlsx: gửi cho P1, P2, P3