Verified Commit c87e1b5b authored by Ankit R Gadiya's avatar Ankit R Gadiya
Browse files

Files Added

parents
Pipeline #37519490 passed with stages
in 27 seconds
html/
*.swp
image: ankitrgadiya/pandoc
pages:
script:
- make all
- mv html public
artifacts:
paths:
- public
only:
- master
# Makefile to generate files
# SEO Variables
BASEURL := https://argp.in
GOOGLE_VERIFICATION := J0CD-ynr2w5EhpI_VrP-OOrThFQcJyRyTnWTWz2dfgU
# Directories
SOURCE := source
TEMPL := template
HTML := html
STATIC := static
FOLDERS := $(subst $(SOURCE),$(HTML),$(shell find $(SOURCE) -type d))
# Files
MD_FILES := $(shell find $(SOURCE) -type f -name "*.md")
HTML_FILES := $(subst $(SOURCE),$(HTML),$(patsubst %.md,%.html,$(MD_FILES)))
HTML_TEMPL := $(wildcard $(TEMPL)/*.html)
STATIC_FILES := $(subst $(STATIC),$(HTML),$(wildcard $(STATIC)/*))
SITEMAP := $(HTML)/sitemap.txt
TREE := $(HTML)/list.txt
# Parser options
PANDOC := pandoc
HTML_FLAGS := \
--standalone \
--from markdown \
--to html \
--template $(TEMPL)/default.html \
--mathjax \
--include-after-body=$(TEMPL)/footer.html \
--include-before-body=$(TEMPL)/navigation.html \
--strip-comments \
--variable google_verification=$(GOOGLE_VERIFICATION)
# Default target
all: $(FOLDERS) $(HTML_FILES) $(STATIC_FILES) $(SITEMAP) $(TREE)
# Create folders
$(FOLDERS):
mkdir -p $@
# HTML Files
$(HTML_FILES): $(HTML_TEMPL)
html/%.html: source/%.md
$(PANDOC) $(HTML_FLAGS) --output $@ $<
# Static Assets
$(STATIC_FILES): $(wildcard $(STATIC)/*)
cp -r $(STATIC)/* $(HTML)
# Sitemap
$(SITEMAP): $(HTML_FILES)
find $(HTML)/ -name "*.html" -type f -printf "$(BASEURL)/%P\n" > $@
# Tree
$(TREE): $(HTML_FILES) $(STATIC_FILES) $(FOLDERS)
cd $(HTML) && \
tree -P '*.html|*.css|*.txt' --charset=ascii --dirsfirst -o list.txt
# Clean
clean:
rm -rf $(HTML)/*
# Server
server:
caddy -host 0.0.0.0 -port 8000 -root html/
# PHONY targets
.PHONY: all clean server
This repository contains source code of my website - https://argp.in.
---
title: Error 404
subtitle: Page not found
---
The page you are looking for either do not exist or was moved to other location.
---
title: Docker Images
subtitle: Documentation for Docker images
---
[Argp](/index.html) / [Docs](/docs/index.html) / Docker
Following is the list of [Docker](https://docker.com/) images I wrote/maintain.
The pre-built images are available at [Docker
Hub](https://hub.docker.com/u/ankitrgadiya/).
* [Nnpy](/docs/docker/nnpy.html)
* [Pandoc](/docs/docker/pandoc.html)
---
title: "Docker &mdash; Nnpy"
redirect: "/docs/nnpy/docker.html"
---
[Argp](/index.html) / [Docs](/docs/index.html) /
[Docker](/docs/docker/index.html) / Nnpy
---
title: Docker &mdash; Pandoc
subtitle: Docker image for Pandoc
---
[Argp](/index.html) / [Docs](/docs/index.html) /
[Docker](/docs/docker/index.html) / Pandoc
[![Docker Automated build](https://img.shields.io/docker/automated/ankitrgadiya/pandoc.svg)](https://hub.docker.com/r/ankitrgadiya/pandoc/builds/)
[![Docker Stars](https://img.shields.io/docker/stars/ankitrgadiya/pandoc.svg)](https://hub.docker.com/r/ankitrgadiya/pandoc/)
[![Docker Pulls](https://img.shields.io/docker/pulls/ankitrgadiya/pandoc.svg)](https://hub.docker.com/r/ankitrgadiya/pandoc/)
[![GitHub issues](https://img.shields.io/github/issues/ankitrgadiya/docker-pandoc.svg)](https://github.com/ankitrgadiya/docker-pandoc/issues)
[![GitHub last commit](https://img.shields.io/github/last-commit/ankitrgadiya/docker-pandoc.svg)](https://github.com/ankitrgadiya/docker-pandoc/commits)
[![GitHub](https://img.shields.io/github/license/ankitrgadiya/docker-pandoc.svg)](https://github.com/ankitrgadiya/docker-pandoc/blob/master/LICENSE)
## About
The Pandoc image comes with the latest [Pandoc](https://pandoc.org/) binary
from [Github Releases](https://github.com/jgm/pandoc/releases). This image is
meant to be used by CI/CD services to generate various file formats supported
by Pandoc. However, this can also be used instead of distribution specific
versions of Pandoc to get the latest version.
## Usage
The `pandoc` command can be used by running the container.
```bash
$ docker run -it ankitrgadiya/pandoc pandoc <ARGS>
```
Although even for a very basic setup you'll probably need to mount the
directories in the container so the files that Pandoc writes can be persistent.
The `-v` option of `run` subcommand can be used to mount directories. To mount
the current directory in the container you can substitute `pwd` command in
place of directory. Note that by default the container starts in `/` directory
but you can use `-w` option to override the default working directory.
```bash
$ docker run -v `pwd`:/root/ -w="/root/" -it ankitrgadiya/pandoc pandoc <ARGS>
```
The image also comes with GNU Make installed so instead of running commands on
individual files, you can create a receipe in `Makefile` and run `make` command
instead.
```bash
$ docker run -v `pwd`:/root/ -w="/root/" -it ankitrgadiya/pandoc make
```
## Links
* [Source](https://github.com/ankitrgadiya/docker-pandoc)
* [Pre-Built Image](https://hub.docker.com/r/ankitrgadiya/pandoc)
## License
[BSD
3-Clause](https://github.com/ankitrgadiya/docker-pandoc/blob/master/LICENSE)
License.
---
title: "Docs"
subtitle: "List of all the documentation hosted on this website"
---
[Argp](/index.html) / Docs
* [Docker](/docs/docker/index.html)
* [Nnpy](/docs/docker/nnpy.html)
* [Pandoc](/docs/docker/pandoc.html)
* [Nnpy](/docs/nnpy/index.html)
* [Docker](/docs/nnpy/docker.html)
* [Nginx](/docs/nnpy/nginx.html)
* [Setup](/docs/nnpy/setup.html)
* [Project Euler](/docs/project-euler/index.html)
* [Problem 1](/docs/project-euler/prob1.html)
* [Problem 2](/docs/project-euler/prob2.html)
* [Simple Template](/docs/simple-template/index.html)
* [Jekyll](/docs/simple-template/jekyll.html)
* [Pandoc](/docs/simple-template/pandoc.html)
---
title: Nnpy &mdash; Docker
subtitle: Documentation of the docker image of Nnpy
---
[Argp](/index.html) / [Docs](/docs/index.html) / [Nnpy](/docs/nnpy/index.html)
/ Docker
For easier deployment, Nnpy Docker image can be used instead of manually
installing everything. The Docker image uses [Alpine
Linux](https://alpinelinux.org/) and is therefore very lightweight.
The repository includes a `Dockerfile` which can be used to build the Docker
image manually. To build the image execute the following command.
```bash
$ docker build -t nnpy .
```
Alternatively, you can pull the pre-built image from [Docker
Hub](https://hub.docker.com/r/ankitrgadiya/nnpy/).
```bash
$ docker pull ankitrgadiya/nnpy
```
Nnpy uses the [SQLite](https://www.sqlite.org/) database to store the data. To
use the image, you need a database in place.
Once, you have the image pulled/built and SQLite database in place, to run the
container you can execute the following command.
```bash
$ docker run -v /absolute/path/nnpy.db:/app/nnpy.db -p 8080:80 -d ankitrgadiya/nnpy
```
* `-v` option mounts the `nnpy.db` inside the container at `/app/nnpy.db`.
* `-p` option exposes port 80 of container and maps it to port 8080 of the
machine, effectively serving Pastebin at port 8080 of the local machine.
* `-d` option makes the container run in daemon mode.
---
title: Nnpy
subtitle: A very simple Pastebin
---
[Argp](/index.html) / [Docs](/docs/index.html) / Nnpy
[![Docker Automated build](https://img.shields.io/docker/automated/ankitrgadiya/nnpy.svg)](https://hub.docker.com/r/ankitrgadiya/nnpy/builds/)
[![Docker Stars](https://img.shields.io/docker/stars/ankitrgadiya/nnpy.svg)](https://hub.docker.com/r/ankitrgadiya/nnpy/)
[![Docker Pulls](https://img.shields.io/docker/pulls/ankitrgadiya/nnpy.svg)](https://hub.docker.com/r/ankitrgadiya/nnpy/)
[![Build Status](https://travis-ci.com/ankitrgadiya/nnpy.svg?branch=master)](https://travis-ci.com/ankitrgadiya/nnpy)
[![GitHub issues](https://img.shields.io/github/issues/ankitrgadiya/nnpy.svg)](https://github.com/ankitrgadiya/nnpy/issues)
[![GitHub last commit](https://img.shields.io/github/last-commit/ankitrgadiya/nnpy.svg)](https://github.com/ankitrgadiya/nnpy/commits)
[![GitHub](https://img.shields.io/github/license/ankitrgadiya/nnpy.svg)](https://github.com/ankitrgadiya/nnpy/blob/master/LICENSE)
Nnpy is a very simple Pastebin and a clone of [nnmm](https://nnmm.nl/). It
is implemented in [Python 3](https://www.python.org/) and uses the
[Flask](http://flask.pocoo.org/) web framework. To store data, it uses the
[SQLite](https://www.sqlite.org/) database. It does not include a form to
submit data. Instead, it relies on other software to send *POST* request
directly.
A simple example of pasting the output of `ls` command to Nnpy using
`curl`.
```bash
$ ls -l | curl --data-urlencode c@- https://example.com
https://example.com/U01mj
```
Another example of pasting the contents of a plain text file to nnpy by
redirecting the input to `curl` command.
```bash
$ curl --data-urlencode c@- https://exampe.com < plain.txt
https://example.com/jSnn4
```
* [Docker](/docs/nnpy/docker.html)
* [Nginx](/docs/nnpy/nginx.html)
* [Setup](/docs/nnpy/setup.html)
## Links
* [Github:ankitrgadiya/nnpy](https://github.com/ankitrgadiya/nnpy)
## License
[BSD 3-Clause](https://github.com/ankitrgadiya/nnpy/blob/master/LICENSE) License
---
title: Nnpy &mdash; Nginx
subtitle: Setting up Nginx proxy for Nnpy
---
[Argp](/index.html) / [Docs](/docs/index.html) / [Nnpy](/docs/nnpy/index.html)
/ Nginx
After installing Nnpy, a proxy server can be used to forward requests to nnpy
server. This section provides information to configure
[Nginx](https://www.nginx.org/) as a proxy server under
[Debian](https://www.debian.org/).
Add a new file named `nnpy` under `/etc/nginx/sites-available/` directory for
Nnpy with the following content and modify the necessary things.
```nginx
server {
listen 80;
# SSL
# listen 443 ssl;
# ssl_certificate /etc/nginx/certs/debian/final.crt;
# ssl_certificate_key /etc/nginx/certs/debian/debian.key;
# ssl_client_certificate /etc/nginx/certs/debian/client-ca.crt;
# ssl_verify_client optional;
server_name example.com;
location / {
include /etc/nginx/uwsgi_params;
uwsgi_pass unix:/var/run/nnpy.sock;
}
}
```
To enable it so that Nginx can see and process it, create a symlink to this
file in `/etc/nginx/sites-enabled/` directory.
```bash
$ ln -s /etc/nginx/sites-enabled/nnpy /etc/nginx/sites-available/nnpy
```
The virtual host for Nnpy is enabled now. It is always a good thing to verify
the configuration for errors. Nginx command, if run with `-t` option does the
error checking.
```bash
$ nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
```
To apply the new configurations Nginx needs to reload.
```bash
$ service nginx reload
```
---
title: Nnpy &mdash; Setup
subtitle: Setting up Nnpy locally and adding Systemd service
---
[Argp](/index.html) / [Docs](/docs/index.html) / [Nnpy](/docs/nnpy/index.html)
/ Setup
Nnpy requires packages under [Debian](https://www.debian.org/) distribution
(similar packages for other distributions).
* virtualenv
* python3
* sqlite3
* python3-dev (Build Only)
* build-essential (Build Only)
After installing packages, create a virtual environment under the root
directory of Nnpy and activate it.
```bash
$ virtualenv -p python3 env/
Already using interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in /app/env/bin/python3
Also creating executable in /app/env/bin/python
Installing setuptools, pkg_resources, pip, wheel...done.
$ source env/bin/activate
```
Now, install Python modules which are used by Nnpy.
* flask
* uwsgi
```bash
(env) $ pip3 install -r requirements.txt
```
The packages marked as “Build Only” in the list above are required to build the
dependencies and can be uninstalled later.
Now we need to create a database with the `pastes` table with two columns for
`id` and `data`.
```bash
$ cat nnpy.sql | sqlite3 nnpy.db
```
Nnpy is ready to run now. It can be run directly by starting the web server.
Alternatively, it can create a *UNIX* socket which can be used by proxy servers
like [Nginx](https://www.nginx.org/) to proxy the web requests to Nnpy. The
`wsgi.ini` file handles the configuration. Default settings will run it in
*HTTP* mode and start web server which will listen at port 5000.
Execute the subsequent command to test `uwsgi` in place.
```bash
(env) $ uwsgi --ini wsgi.ini
```
[Systemd](https://en.wikipedia.org/wiki/Systemd), which is the default init
system and service manager in a lot of popular Linux Distributions, use service
files to manage services. Please create a new file with the following content
and save it as `nnpy.service`.
```ini
[Unit]
Description=nnpy paste
After=network.target
[Service]
WorkingDirectory=/path/to/nnpy/
Environment='PATH=/path/to/nnpy/env/bin'
ExecStart=/path/to/nnpy/env/bin/uwsgi --ini nnpy.ini
Restart=on-failure
RestartSec=5s
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all
Group=www-data
[Install]
WantedBy=multi-user.target
```
To add the service file to Systemd, you need to copy the file into
`/etc/systemd/system/` folder.
```bash
$ cp nnpy.service /etc/systemd/system/
```
Now, Systemd daemon needs to reload to recognize and process the service file.
```bash
$ sudo systemctl daemon-reload
```
Now the web app can be run by executing the following command.
```bash
$ sudo service nnpy start
```
To set the service to run on boot, execute the following command.
```bash
$ sudo systemctl enable nnpy.service
```
---
title: Project Euler
subtitle: Project Euler Problems
---
[Argp](/index.html) / [Docs](/docs/index.html) / Project Euler
[![GitHub last commit](https://img.shields.io/github/last-commit/ankitrgadiya/project-euler.svg)](https://github.com/ankitrgadiya/project-euler/commits)
[![GitHub language count](https://img.shields.io/github/languages/count/ankitrgadiya/project-euler.svg)](https://github.com/ankitrgadiya/project-euler)
[![GitHub stars](https://img.shields.io/github/stars/ankitrgadiya/project-euler.svg)](https://github.com/ankitrgadiya/project-euler/stargazers)
[![GitHub](https://img.shields.io/github/license/ankitrgadiya/project-euler.svg)](https://github.com/ankitrgadiya/project-euler/blob/master/LICENSE)
## About Project Euler
[Project Euler](https://projecteuler.net/) is a project by volunteers which has
a large archive of mathematical problems ranging from easy to hard level.
Programmers are to write code in any programming language to solve the problem
and find the correct solution. Every problem has a thread associated with it
where users can post their code and check code of fellow users.
## Problems
The following list contains problems that I’ve solved with the link to the
explanation. If you’ve not tried it on your own, I would recommend giving it a
try by yourself first before looking at the solution.
* &#x2714; [Problem 1](/docs/project-euler/prob1.html)
* &#x2714; [Problem 2](/docs/project-euler/prob2.html)
## Links
* [Source Code](https://github.com/ankitrgadiya/project-euler/)
## License
[BSD
3-Clause](https://github.com/ankitrgadiya/project-euler/blob/master/LICENSE)
License.
---
title: "Project Euler &mdash; Problem 1"
subtitle: "Multiples of 3 and 5"
---
[Argp](/index.html) / [Docs](/docs/index.html) / [Project
Euler](/docs/project-euler/index.html) / Problem 1
## Problem
If we list all the natural numbers below 10 that are multiples of 3 or 5, we
get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
## Solution
The most [brute force](https://en.wikipedia.org/wiki/Brute-force_search) way to
approach this problem is to iterate over every number from 1 through 999, that
is, $n - 1$ and check if it is a multiple of 3 or 5 using the
[modulo](https://en.wikipedia.org/wiki/Modulo_operation) operator. If so add it
to the total sum. This is a simple and correct approach and will definitely
give correct answers.
```python
sum = 0
for i in range(1000):
if i % 3 is 0 or i % 5 is 0:
sum += i
print(sum)
```
However, this brute force approach is very slow as it goes through literally
$n$ numbers. Therefore, the runtime of this algorithm is linear or in other
words, the complexity of this algorithm is $O\big(n\big)$.
A smarter approach would be to not go through all the numbers, but instead, add
the multiples of 3 and 5. Then reduce, from the total sum, the summation of
multiples of 15 as it got added twice.
```python
def sumOfMultiples(x, limit):
Sum = 0
for i in range(x, 1000, x):
Sum += i
return Sum
print(sumOfMultiples(3, 1000) + sumOfMultiples(5, 1000)
- sumOfMultiples(15, 1000))
```
This approach, in practice, is faster than the previous brute force approach as
we are churning fewer numbers but theoretically, it’s runtime is still linear.
As it turns out, multiples of any number are always in an [Arithmetic
Progression](https://en.wikipedia.org/wiki/Arithmetic_progression). We can
utilize this fact to attain constant runtime.
In this problem, we are dealing with a special case where $a_1$ or initial term
and $d$ or the common difference for the arithmetic progression are same.
$$
\begin{equation} \label{a} \tag{1}
a_1 = d
\end{equation}
$$
The n<sup>th</sup> term or $a_n$ of the arithmetic progression then is as
follows.
$$
\begin{equation}
\begin{split}
a_n & = a_1 + \big( n - 1 \big) \cdot d \\
\end{split}
\end{equation}
$$
But as mentioned earlier $\eqref{a}$, the common difference is same as the
initial term. So, we can substitute the value to get $a_n$ as $n$ times the
inital term.
$$
\begin{equation}
\begin{split}
a_n & = a_1 + \big(n - 1 \big) \cdot a_1 \quad \eqref{a} \\
& = a_1 \cdot n
\end{split}
\end{equation} \label{b} \tag{2}
$$
The summation of $n$ terms of arithmetic progression is given by the following
equation.
$$
\begin{equation}
\begin{split}
S_n & = \bigg(\frac{n}{2}\bigg) \cdot \big(a_1 + a_n\big) \\
\end{split}
\end{equation}
$$
We can substitute the value of $a_n$ from $\eqref{b}$ in the above equation
to get our final formula for the sum of multiples.
$$
\begin{equation}
\begin{split}
S_n & = \bigg(\frac{n}{2}\bigg) \cdot \big(a_1 + \big(a_1 \cdot n \big) \big)
\quad \eqref{b} \\
& = \Bigg(\frac{a_1 \cdot n \cdot \big( n + 1 \big)}{2} \Bigg)
\end{split}
\end{equation}
$$
The above formula can now be used to calculate the sum of multiples in constant
time as the execution does not depend upon the size of the number. And this is
how we can theoretically achieve the runtime complexity of $O(1)$.