Categories
Computer Science

Summary and thinking on Bulb Switcher problem

Bulb Switcher

Problem:

There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it’s off or turning off if it’s on). For the ith round, you toggle every i bulb. For the nth round, you only toggle the last bulb. Find how many bulbs are on after n rounds.

You can find the problem at https://leetcode.com/problems/bulb-switcher/.

I’m not going to talk about how to actually code the problem here, just want to share how I thought and finally got to the best solution.

Categories
Computer Science

JavaScript (jQuery AngularJS) Notes

Better way to compare two floats

When you compare two floats, you’d better not directly compare them. Because in the calculation handled by computer, some minor difference will be introduced into infinities. Try to compare floats if they are equal using:

Math.abs(1 / 3 - (1 - 2 / 3)) < 0.0000001; // true
Categories
Computer Science

Compile & install 32 bit Python 2.7 on 64 bit CentOS 6.5

You might want to compile a version of Python other than shipped one on your CentOS system. For example, running a Python script using a library that only has a 32bit build. Here are the steps on how to compile & install Python 2.7.12 from source code on CentOS 6.5. All operations tested on a clean CentOS 6.5 (AWS AMI: CentOS 6.5 x86_64(HVM) — 20141008–0 — minimal install with cloud helpers plus Intel SRIOV (ami-4dc28f7d))

Categories
Computer Science

How to check Python version (both command-line and programmatically)

Sometimes you might want to confirm if your program will run / is running under Python 2.x or 3.x. We have a very simple way to do the stuff.