Python: Revere string

Probably the easiest and close to the fastest way to reverse a string is to use Python’s extended slice syntax. This allows you to specify a start, stop and step value to use when creating a slice. The syntax is: [start:stop:step].

s = "String to reverse."
print s[::-1]

If start is omitted it defaults to 0 and if stop is omitted it defaults to the length of the string. A step of -1 tells Python to start counting by 1 from the stop until it reaches the start.

When working with large strings, or when you just don’t want to reverse the whole string at once, you can use the reversed() built-in. reversed() returns an iterator and is arguably the most Pythonic way to reverse a string.

 

s = "String to reverse."
print ("".join(reversed(s)))
Advertisements

Living in the Singapore, cost of living, finding the room or apartment

It’s been almost 9 months I’ve worked in Singapore. I will be sharing some information for those whom can benefit from it.

The average cost that I spend per months goes as follow, all values are in SGD:

  1. Rent – 750 for common room(need to pay deposit 1 month when starting the contract )
  2. Transport(bus and mrt) – 70-80
  3. Communications(Singtel) – 70
  4. Food(fruits, lunch, dinner, snacks etc) – 500

Total: ~1400

The best way to find the room or apartment is to ask the agent to find it for you(you will need to pay the agent fee which equates to the half of the rent(for both room or apartment)). I find the agents from  http://www.orangetee.com most useful(can recommend to email to george.kyaw@orangetee.com please note he only handles the apartment rentals)

If you don’t want to pay agent fee, then your best chance is to go to easyroommate.com and put your add there.

Another places where to find the room/apartment is https://www.gumtree.sg/, http://www.propertyguru.com.sg , nestia.com (in no particular order). Or facebook groups.

More about JavaScript closures

From my understanding closures is extremely important concept in JavaScript. Let’s learn it in details:

Closures are functions that refer to independent (free) variables (variables that are used locally, but defined in an enclosing scope). In other words, these functions ‘remember’ the environment in which they were created.

Lexical Scooping

Lexical scooping is a type of closure.

function init() {
var name = “Mozilla”; // name is a local variable created by init
function displayName() { // displayName() is the inner function, a closure
alert (name); // displayName() uses variable declared in the parent function
}
displayName();
}
init();

Here you can notice that init() has function inside. Then when init is called, that function is called too.

Git: how to use cherry pick using smartgit

If you have have multiple git branches and you have pushed your code to branch x, but you also want it to be available on other branch too ex.: branch Y, then perform following steps:

  1. Checkout the branch Y, the place where you want to push your code
  2. Then from toolbar above, Branch->Cherry Pick
  3. Click Branches, and un-select the Head. Then select the branch X. Choose the commit you want to add to branch Y.
  4. Select and press Cherry pick and commit
  5. then press Push
  6. Done

Now branch Y has commit from branch X.

Let’s learn JS! Reading the “JS Good Parts book”

I’ve decided to start learning Javascript because I want understand how it works.

Let’s start with Functions:

Function invocation:

In addition to the declared parameters, every function receives two additional parameters: this and arguments. The this parameter is very important in object oriented programming, and its value is determined by the invocation pattern. There are four patterns of invocation in JavaScript: the method invocation pattern, the function invocation pattern, the constructor invocation pattern, and the apply invocation pattern. The patterns differ in how the bonus parameter this is initialized.

Note: if too many or too little parameters passed to function they will be ignored or set to undefined. There is no type checking. Also this will not throw any runtime errors.

1.Method invocation pattern

// Create myObject. It has a value and an increment
// method. The increment method takes an optional
// parameter. If the argument is not a number, then 1
// is used as the default.
var myObject = {
value: 0,
increment: function (inc) {
this.value += typeof inc === ‘number’ ? inc : 1;
}
};
myObject.increment( );
document.writeln(myObject.value); // 1
myObject.increment(2);
document.writeln(myObject.value); // 3

2. Function invocation pattern

 

3. Constructor invocation pattern

 

4. Apply invocation pattern

 

JS: function inside () followed by (). ~ inside the if statment

if the function is inside the () and followed by () ex:

( fucntion(){

})

()

This means that we declared the function and run it immediately

~ inside if statement means “not”, and works similar to !. ex:
if(~tagsArray.indexOf(item)) {

something;
}

means if no items inside the array do something

JS lesson 2: CSS pseudo class

We can use CSS pseudo class to select items we need.

<select>
<optgroup>
<option selected>1</option>
<option>2</option>
<option>3</option>
</optgroup>
</select>
select option:selected === select > optgroup > option:selected

select option:myproperty  // this is wrong

Javascrint and Jquery way of finding elements
select option:first-child === $(select).find(option).first();
select option:last-child === $(select).find(option).first();