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

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))))

Read More...


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))))

Read More...


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)))

Read More...


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?

Read More...


Side Projects

2021-09-23

Read More...


Docker Compose healthcheck

2021-09-09

The most important thing when running integration test using docker-compose is ensured that one container is started completely before others.

Sometime wait-for-it is not enough:

 1  cassandra:
 2    image: bitnami/cassandra:latest
 3    ports:
 4      - '7000:7000'
 5      - '9042:9042'
 6    volumes:
 7      - /path/to/init-scripts:/docker-entrypoint-initdb.d
 8
 9  wait-for-cassandra:
10    image: willwill/wait-for-it
11    command: cassandra:9042 -t 60
12    depends_on:
13      - cassandra:

Read More...


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?

Read More...


fish shell - very high CPU usage

2021-07-30

Sometimes my Macbook is loud like a plow, and the culprit is usually docker. This time it’s different, top -u tells me that fish shell is taking up > 100% CPU.

I just switched from zsh to fish about a year ago after having problems with git completion.

My favorite feature about fish is probably autosuggestions. Normally, when I want to run a command again, I can control+R, type a few letters to select the command I want to run. With fish shell, I can type that command and then -> or control+F is fine.

Read More...


Troubleshooting slow network file transfer

2021-06-25

We have a Hadoop data cluster. Recently, my colleague noticed that data transfer between servers was slow. Not all are slow; only half of them have problems.

The first tool that comes to mind is iperf3. After installing, I run iperf3 -s then iperf3 -c. While other servers have bandwidth > 900 Mbits/sec, the slow ones are just:

 1[ ID] Interval           Transfer     Bandwidth       Retr  Cwnd
 2[  4]   0.00-1.00   sec  11.9 MBytes  99.4 Mbits/sec    0    170 KBytes
 3[  4]   1.00-2.00   sec  11.1 MBytes  93.3 Mbits/sec    0    204 KBytes
 4[  4]   2.00-3.00   sec  10.9 MBytes  91.7 Mbits/sec    0    204 KBytes
 5[  4]   3.00-4.00   sec  8.26 MBytes  69.3 Mbits/sec  102    154 KBytes
 6[  4]   4.00-5.00   sec  10.9 MBytes  91.8 Mbits/sec    3    157 KBytes
 7[  4]   5.00-6.00   sec  10.9 MBytes  91.7 Mbits/sec    0    165 KBytes
 8[  4]   6.00-7.00   sec  10.9 MBytes  91.7 Mbits/sec    0    168 KBytes
 9[  4]   7.00-8.00   sec  10.9 MBytes  91.7 Mbits/sec    0    173 KBytes
10[  4]   8.00-9.00   sec  10.9 MBytes  91.7 Mbits/sec    0    173 KBytes
11[  4]   9.00-10.00  sec  10.9 MBytes  91.7 Mbits/sec    0    173 KBytes
12- - - - - - - - - - - - - - - - - - - - - - - - -
13[ ID] Interval           Transfer     Bandwidth       Retr
14[  4]   0.00-10.00  sec   108 MBytes  90.4 Mbits/sec  105             sender
15[  4]   0.00-10.00  sec   107 MBytes  89.5 Mbits/sec                  receiver

Read More...


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:

Read More...