HomeTom - CS

Thursday, April 16, 2026

Card shuffling problem

A card shuffling algorithm is a method for randomly permuting a deck of cards so that every ordering is equally likely (in a “good” shuffle).

The most important and widely used algorithm is:

Fisher–Yates Shuffle (a.k.a. Knuth Shuffle)

This is the gold standard for unbiased shuffling.

Idea

You go through the deck from the last card to the first, and for each position, swap it with a randomly chosen earlier (or same) position.

Algorithm (array-based deck)

Let A be an array of n cards (array index: [0, ..., n-1]):

for i from n - 1 down to 1:
j = random integer such that 0 ≤ j ≤ i
swap A[i] and A[j]

Why it works

  • Each card has an equal probability of ending in any position.
  • It avoids subtle bias found in naive shuffles (like sorting with random comparator).

Complexity

  • Time: O(n)
  • Space: O(1) (in-place)

Variants you might see

1. Inside-out Fisher–Yates

Useful when building a shuffled array from scratch:

for i from 0 to n-1:
j = random integer in [0, i]
A[i] = A[j]
A[j] = current element

2. Riffle shuffle (card-deck realistic)

Models human shuffling:

  • Split deck into two halves
  • Interleave them probabilistically

Used in probability studies, but needs many repeats (~7 riffle shuffles) to approximate randomness.


Practical note

In most programming languages:

  • Use a built-in shuffle (e.g., random.shuffle in Python), which implements Fisher–Yates.


Tuesday, April 14, 2026

Where to host next.js app

10+ Best Next.js Hosting Providers for 2026

https://dev.to/ardunster/deploying-a-nextjs-website-to-a-virtual-private-server-gpm 

Monday, April 13, 2026

Avoid auto-zoom in mobile browser

Question: with a html5 based self-adapted web application on mobile, if click in a textbox, it zoom in. How to avoid zoom in and always keep screen fully fit?

This is a classic mobile browser behavior (especially on iOS Safari and Chrome on Android): when a text input is focused, the browser auto-zooms if it thinks the text is too small.

 

ChatGPT answer: 

✅ The main cause

Auto-zoom usually happens when:

  • The font size of the <input> or <textarea> is less than 16px

✅ Fix #1 (Best & recommended)

Set the font size of all input fields to at least 16px:

input, textarea, select {
font-size: 16px;
}

This alone prevents zoom on most mobile browsers.


⚠️ Fix #2 (Viewport meta tag)

You can also control zoom behavior via the viewport:

<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">

But be careful:

  • user-scalable=no disables pinch zoom entirely (bad for accessibility)
  • Apple (Safari) may ignore some of these settings

✅ Fix #3 (iOS-specific trick)

If you must use smaller font sizes visually, you can fake it:

input {
font-size: 16px;
transform: scale(0.9);
transform-origin: left top;
}

This keeps the logical font size ≥16px while appearing smaller.


✅ Fix #4 (Prevent layout shift)

Also ensure your layout doesn’t resize oddly:

html {
-webkit-text-size-adjust: 100%;
}

🧠 Summary

  • ✔ Best solution: use font-size: 16px or larger
  • ✔ Avoid disabling zoom unless absolutely necessary
  • ✔ Use viewport tweaks carefully
  • ✔ iOS Safari is the main browser enforcing this behavior

If you want, tell me your layout/framework (React, Vue, plain HTML), and I can give you a cleaner drop-in fix.

plain html

Got it—since you’re using plain HTML, you can fix this cleanly with just a couple of tweaks.


✅ The simplest working solution

1. Set input font size to at least 16px

This is the key fix (especially for iOS Safari):

<style>
input, textarea, select {
font-size: 16px;
}
</style>

👉 This alone prevents the zoom-in when focusing inputs on most mobile browsers.


2. Add a proper viewport meta tag

<meta name="viewport" content="width=device-width, initial-scale=1">

✔ Keeps your layout responsive
✔ Avoids unnecessary scaling issues
❌ Don’t add user-scalable=no unless you really want to disable zoom entirely


⚠️ If you want smaller-looking inputs (optional)

If 16px feels too big visually, you can “fake” a smaller size:

<style>
input {
font-size: 16px;
transform: scale(0.9);
transform-origin: left top;
}
</style>

✅ Full minimal example

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">

<style>
body {
margin: 0;
padding: 20px;
}

input {
font-size: 16px;
padding: 10px;
width: 100%;
box-sizing: border-box;
}
</style>
</head>
<body>

<input type="text" placeholder="Tap me (no zoom)" />

</body>
</html>

🧠 Why this works

Mobile browsers (again, mainly iOS Safari) auto-zoom when:

  • Text is too small to read comfortably (<16px)

So by meeting that threshold, the browser stops helping you—which is exactly what you want here.


If it’s still zooming after this, tell me:

  • your exact CSS for the input
  • or share a snippet

There are a couple of edge cases (like inherited font sizes or transforms) I can help debug quickly.

Monday, April 6, 2026

Productionize a Django website

 Recommended minimal permissions of files in a Django site:

# Database
sudo chown www-data:www-data db.sqlite3
sudo chmod 664 db.sqlite3

# DB directory
sudo chown www-data:www-data .
sudo chmod 775 .

# Static/media (if needed)
sudo chown -R www-data:www-data static media 

Sunday, April 5, 2026

Bluehost VPS Setup

Docs 

https://www.bluehost.com/blog/how-to-set-up-a-vps/

https://www.bluehost.com/help/article/dns-records-explained


Add SSL certificate to ubuntu server

 

Here’s a clear, practical way to add SSL (HTTPS) to a server on Ubuntu. The most common and easiest method is using Certbot with Let's Encrypt (free SSL certificates).


🔒 Method 1: Use Let’s Encrypt (Recommended)

1. Update your system

sudo apt update
sudo apt upgrade -y

2. Install Certbot

For Nginx:

sudo apt install certbot python3-certbot-nginx -y

For Apache:

sudo apt install certbot python3-certbot-apache -y

3. Get SSL certificate

Nginx:

sudo certbot --nginx

Apache:

sudo certbot --apache

You’ll be prompted to:

  • Enter your email
  • Agree to terms
  • Choose your domain
  • Optionally redirect HTTP → HTTPS (choose YES)

4. Verify SSL

Open your site:

https://yourdomain.com

5. Auto-renew SSL

Let’s Encrypt certs expire every 90 days, but auto-renew is usually set up automatically.

Test it:

sudo certbot renew --dry-run

⚙️ Method 2: Manual (Advanced / Custom setups)

If you're not using Apache/Nginx automation:

Generate certificate:

sudo certbot certonly --standalone -d yourdomain.com

Certificates will be stored in:

/etc/letsencrypt/live/yourdomain.com/

Then configure your server manually with:

  • fullchain.pem
  • privkey.pem

📌 Common Requirements

Make sure:

  • Your domain points to your server (DNS configured)
  • Ports are open:
sudo ufw allow 80
sudo ufw allow 443

🧠 Quick Troubleshooting

  • If Certbot fails → check DNS propagation
  • If port 80 is busy → stop conflicting services:
sudo systemctl stop nginx
 
 
 
 
Notes:
 
$ apt install apache2 openssl
$ a2enmod ssl
$ systemctl restart apache2
$ apt install certbot python3-certbot-apache
$ certbot --apache 

Host php and django on same apache server

To host both PHP and Django sites on the same Apache server, you just need to configure Apache to handle each type of application differently.

Here’s how it works in practice:


🔧 Key Idea

Apache can serve:

  • PHP via mod_php or PHP-FPM
  • Django via a WSGI interface (usually mod_wsgi)

Each site gets its own virtual host configuration.


🧱 Typical Setup

1. PHP Site

Handled normally with PHP enabled:

<VirtualHost *:80>
ServerName php-site.com
DocumentRoot /var/www/php-site

<Directory /var/www/php-site>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>

2. Django Site

Uses mod_wsgi to run the Django app:

<VirtualHost *:80>
ServerName django-site.com

WSGIDaemonProcess django_site python-home=/var/www/django-site/venv python-path=/var/www/django-site
WSGIProcessGroup django_site
WSGIScriptAlias / /var/www/django-site/project/wsgi.py

<Directory /var/www/django-site/project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>

Alias /static /var/www/django-site/static
<Directory /var/www/django-site/static>
Require all granted
</Directory>
</VirtualHost>

⚠️ Things to Watch Out For

1. Port / Domain Separation

  • Use different domains (recommended), e.g.:
    • example.com → PHP
    • app.example.com → Django
  • Or different ports (less common)

2. Python Environment

  • Always use a virtual environment for Django
  • Keep dependencies isolated from system Python

3. Performance Considerations

  • PHP runs inside Apache (or via PHP-FPM)
  • Django runs as a separate WSGI process
    ➡️ They won’t conflict, but you need enough memory for both

4. Static Files

  • Apache can serve static files for both apps
  • Django static files should be collected (collectstatic)

✅ Alternative (Often Better)

Many modern setups use:

  • Apache or Nginx as a reverse proxy
  • Django served via:
    • gunicorn or uwsgi
  • PHP via PHP-FPM

This gives better scalability and separation.


🧠 Bottom Line

Yes—you can run both on the same Apache server by:

  • Using VirtualHosts
  • Enabling both PHP support and mod_wsgi
 
 
 


 

Host Php/Django On Ubuntu

On Ubuntu this is very straightforward. I’ll walk you through a clean, practical setup where Apache hosts both a PHP site and a Django app.


🧰 1. Install Required Packages

sudo apt update
sudo apt install apache2 php libapache2-mod-php python3 python3-venv python3-pip libapache2-mod-wsgi-py3

Enable Apache modules:

sudo a2enmod wsgi
sudo a2enmod rewrite
sudo systemctl restart apache2

🐘 2. Set Up Your PHP Site

Create a directory:

sudo mkdir -p /var/www/php-site
sudo chown -R $USER:$USER /var/www/php-site

Test PHP:

echo "<?php phpinfo(); ?>" > /var/www/php-site/index.php

Create config:

sudo nano /etc/apache2/sites-available/php-site.conf
<VirtualHost *:80>
ServerName php.local
DocumentRoot /var/www/php-site

<Directory /var/www/php-site>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>

Enable it:

sudo a2ensite php-site

🐍 3. Set Up Your Django App

Create project

mkdir -p /var/www/django-site
cd /var/www/django-site

python3 -m venv venv
source venv/bin/activate

pip install django
django-admin startproject project .

Test locally:

python manage.py runserver

Configure WSGI + Apache

sudo nano /etc/apache2/sites-available/django-site.conf
<VirtualHost *:80>
ServerName django.local

WSGIDaemonProcess django_site python-home=/var/www/django-site/venv python-path=/var/www/django-site
WSGIProcessGroup django_site
WSGIScriptAlias / /var/www/django-site/project/wsgi.py

<Directory /var/www/django-site/project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>

Alias /static /var/www/django-site/static
<Directory /var/www/django-site/static>
Require all granted
</Directory>
</VirtualHost>

📦 4. Prepare Django for Production

Edit settings:

ALLOWED_HOSTS = ["django.local"]
STATIC_ROOT = "/var/www/django-site/static"

Then:

python manage.py collectstatic

🌐 5. Enable the Django Site

sudo a2ensite django-site
sudo systemctl reload apache2

🧪 6. Local Testing (Important)

Edit your hosts file:

sudo nano /etc/hosts

Add:

127.0.0.1 php.local
127.0.0.1 django.local

⚠️ Common Gotchas

🔐 Permissions

sudo chown -R www-data:www-data /var/www/django-site

📁 Static files not loading?

Make sure:

  • collectstatic was run
  • Apache Alias /static is correct

🧠 mod_wsgi vs modern setup

This works well, but many production setups prefer:

  • Apache → reverse proxy
  • Django → Gunicorn
  • PHP → PHP-FPM

More scalable, but slightly more complex.


✅ Final Result

You’ll have:

  • http://php.local → PHP site
  • http://django.local → Django app

Running on the same Apache server without conflict.

 

Blog Archive

Followers