Pascal Triangle Generator in Python, and then in Haskell – The Gubatron Method

Here’s in python, imperatively, and then in functional style without the need for loops.

def pascal(n):
if n == 1:
return [ 1 ]
if n == 2:
return [ 1, 1 ]
prev = pascal(n1)
results = []
for i in range(n):
if i == 0:
if i == n1:
results.append(prev[i] + prev[i1])
return [1] + results + [1]
# functional style, no loops
def pascal_fp(n):
if n == 1:
return [ 1 ]
prev = pascal_fp(n1)
return list(map(lambda x,y:x+y, [0] + prev, prev + [0]))

view raw

hosted with ❤ by GitHub

Here’s in Haskell, I call it the gubatron’s method, explained in the comments.
Saw it by looking at a pattern while trying to solve it in paper, it just clicked.
Not sure if this is how other people code this solution.

Gubatron's method
n=3 [1, 2, 1]
copy the list and append a 0 on the left of the first
and append a 0 at the end of the second
[0, 1, 2, 1]
[1, 2, 1, 0]
add them up!
n=4 [1, 3, 3, 1]
append 0s to both sides and add them up
n=4 [1, 3, 3, 1]
[0, 1, 3, 3, 1]
[1, 3, 3, 1, 0]
n=5 [1, 4, 6, 4, 1]
and so on
add two lists, for clarity
addLists :: Num c => [c] -> [c] -> [c]
addLists l1 l2 = zipWith (+) l1 l2
pascal :: (Eq a1, Num a1, Num a2) => a1 -> [a2]
pascal 1 = [ 1 ]
pascal n =
let prev = pascal(n1)
zero_prev = [0] ++ prev
prev_zero = prev ++ [0]
addLists zero_prev prev_zero
[1,2,3] -> "1 2 3"
listToString = unwords. map show
mapM_ -> map monadic so no weird IO errors are triggered
printTriangle n = mapM_ putStrLn (map listToString (map pascal [1..n]))
main = do
input <- getLine
printTriangle . (read :: String -> Int) $ input

view raw


hosted with ❤ by GitHub


Python in Functional Style: How to add 2 lists of integers without using loops

Usually you’d add a list of integers this way:

[pastacode lang=”python” manual=”a%20%3D%20%5B2%2C%202%2C%202%2C%202%5D%0Ab%20%3D%20%5B2%2C%202%2C%202%2C%202%5D%0Ac%20%3D%20%5B%5D%0Afor%20i%20in%20range(len(a))%3A%0A%20c.append(a%5Bi%5D%20%2B%20b%5Bi%5D)” message=”” highlight=”” provider=”manual”/]

You can do it functionally without any loops in different ways:

Using map and a lambda that adds them up

[pastacode lang=”python” manual=”c%20%3D%20list(map(lambda%20x%2Cy%3A%20x%2By%2C%20a%2C%20b))” message=”” highlight=”” provider=”manual”/]

or you can import the add operator as a named function

[pastacode lang=”python” manual=”from%20operator%20import%20add%0Ac%20%3D%20list(map(add%2C%20a%2C%20b))” message=”” highlight=”” provider=”manual”/]

Ever zipped two lists into a list of tuples?

There’s another more convoluted way if you want to play with “zip”.

Close End 10cm 80cm 5# 10pcs white Metal Zipper for Sewing ...
Imagine a jacket zipper and the teeth on each side of the zipper is one element on each one of the list.

When you zip the lists a and b, you end up with a list of tuples of matching elements from the given lists.

[pastacode lang=”python” manual=”%3E%3E%3E%20list(zip(a%2Cb))%0A%5B(2%2C%202)%2C%20(2%2C%202)%2C%20(2%2C%202)%2C%20(2%2C%202)%5D” message=”” highlight=”” provider=”manual”/]

you could now map a function to add the elements within each tuple on that list.

[pastacode lang=”python” manual=”%3E%3E%3E%20list(map(lambda%20tup%3A%20tup%5B0%5D%2Btup%5B1%5D%2C%20zip(a%2Cb)))%0A%5B4%2C%204%2C%204%2C%204%5D” message=”” highlight=”” provider=”manual”/]

Notice how we don’t convert to list after zip, we can work directly with the zip iterator, we only convert to list with the final map iterator.

Python 2 & 3 Note:

In Python 2 it’s not necessary to use list(), the map() and zip() methods return lists there. But stay away from Python 2, a lot of projects are now discontinuing support.

[linux/ubuntu] How to suppress useless mod_openssl/lighttpd error messages from appearing in /var/log/syslog

Sometimes you have a bunch of useless errors creating unnecessary disk I/O on your server, disk I/O that should be used towards serving your user’s requests efficiently.

In this case a site running on lighttpd keeps logging several times per second the following message, creating too much noise and making it hard to see meaningful things I should pay attention to could appear on /var/log/syslog.

Aug  7 19:36:03 ip-172-30-1-251 lighttpd[287019]: message repeated 44 times: [ 2020-08-07 19:36:02: (mod_openssl.c.1796) SSL: 1 error:14209102:SSL routines:tls_early_post_process_client_hello:unsupported protocol]

I tried disabling syslog error messages for SSL, and all syslog output on the lighttpd configuration to no avail. Good thing you can configure rsyslog in Linux to do amazing things with log messages before they make it into the log.

To silence this message, all I had to do was edit an rsyslog config file to filter out my undesired message, and restart the service (no need to restart your host os)

  1. Edited /etc/rsyslog.d/50-default.conf before any mention of /var/log/syslog, to have the following condition (ideally at the top of the config file):

[pastacode lang=”bash” manual=”if%20%24msg%20contains%20’tls_early_post_process_client_hello’%20then%20stop” message=”” highlight=”” provider=”manual”/]

  1. Restarted the rsyslog service, no more noise on /var/log/syslog
     sudo service rsyslog restart

[CODING/SOLVED] gradle build (android) breaks after upgrading a dependency with NullPointerException thrown at ProgramClass.constantPoolEntryAccept

You’ve just upgraded one of your Android project’s dependencies and when you ./gradlew assembleRelease the build process breaks.

You invoke it again with --stacktrace to find the following exception:


[pastacode lang=”java” manual=”java.lang.NullPointerException%0Aat%20proguard.classfile.ProgramClass.constantPoolEntryAccept(” message=”” highlight=”” provider=”manual”/]

This is a ProGuard bug, which my friend, has been solved by the ProGuard team ages ago, and your build environment is using an old ProGuard version.

Add this to your build.gradle to force it to use the latest version (as of today it’s 6.2.2, check the latest version here)

[pastacode lang=”java” manual=”buildscript%20%7B%0A%20%20%20%20…%0A%20%20%20%20dependencies%20%7B%0A%20%20%20%20%20%20%20%20…%0A%20%20%20%20%20%20%20%20classpath%20’net.sf.proguard%3Aproguard-gradle%3A6.2.2’%0A%20%20%7D%0A%7D%0A%7D” message=”force a newer proguard version for your android build” highlight=”1,3,5″ provider=”manual”/]

nginx server configuration for a wordpress instance served from a URL’s subdirectory

You want to serve a wordpress instance on a website’s domain url but not at the path’s root, you want it under a sub-directory, for example “blog”, the same as this blog: 

Here’s how my NGINX’s server block for ‘’ looks like at the moment (https/ssl hasn’t been configured yet)

[pastacode lang=”javascript” path_id=”fc6f967d7689931c81e9e9691b714f29″ file=”” highlight=”” lines=”” provider=”gist”/]

Here is the equivalent in lighttpd, too bad lighttpd has no plans for HTTP2, it’s much friendlier and flexible to configure than nginx in my humble opinion.

[pastacode lang=”javascript” path_id=”d6e2f73026cf61ac74e1d45ce8a401b5″ file=”” highlight=”” lines=”” provider=”gist”/]

I used to host this website and wordpress on lighttpd, lighttpd’s config file is very powerful, it’s all based on matching server variables and applying rules, I will miss it dearly, things like having a compressed file cache and it’s flexibility, but I have to move on to nginx if I want to use http2, the lighttpd has no plans for http2 support and it’s just much faster and efficient than http 1.1.