Friday, October 22, 2010

Upgrade to Ubuntu 10.10 from Desktop version of Ubuntu 10.04

You want to upgradet to latest Ubuntu version(10.10) and you don't get it in your update menu(upgrade to Ubuntu 10.10), then you need to read the following :

Network Upgrade for Ubuntu Desktops (Recommended) :

  1. System -> Administration menu->Software Sources application
  2. Open Updates from the Software Sources application window
  3. Change the Release Upgrade drop down to "Normal Releases" and close the application
  4. Press Alt-F2 and type update-manager
  5. Click the Check button to check for new updates.
  6. If there are any updates to install, use the Install Updates button to install them, and press Check again after that is complete.
  7. A message will appear informing you of the availability of the new release.Click Upgrade.Follow the on-screen instructions.

Thursday, October 21, 2010

Can't find equal sign on Android keyboard

Nope, you are not blind. They managed to hide it on the 3rd page of your keyboard !!! Press the "?123"(12# in some keyboards) Symbol on the bottom Left(right in some local keyboards). Then Press "ALT"(my Norwegian local keyboard ALT button was "1/2") on the Left-hand side to bring up a new variety of symbols including the equal sign.

Tuesday, October 19, 2010

How to find an element in a List and return the index, Python

Check if value exist in a list, L ?
if value in L:
            print "list contains", value
Returns the index, where value exist in List, L :

i = L.index(value)




Array of classes in Python


b = [bullet() for x in range(50)]

Reverse a string, Python

def reverseString(s):
   """Reverses a string given to it."""
   return s[::-1]

Python string manipulation problem

In python string are immutable so if you want to just change a character in a string you get following error :

>>>> idx = 1
>>>> s1 = "pxthon"
>>>> s1[idx] = 'y'

> Traceback (most recent call last):
> File "", line 1, in ?
> TypeError: object does not support item assignment



But you can always go for a way out which is not pretty and can lead to some problems if you are not careful, but works :

s1 = s1[0:idx] + 'y' + s1[idx+1:]

How to append a dictionary in Python

groups = {'IRISH' : 'green', 'AMERICAN' : 'blue'}
# if you want to later add a new one, i.e., 'ITALIAN' : 'orange'

groups['ITALIAN'] = 'orange'

Python for Java Programmers

If you are a Java programmer and need to learn python, try this link. It is pretty useful.

random function python

random.sample(100,2) -> returns 2 random numbers in range of 100 ...

Python tutorials, video

To learn more about Python you can visit ShowMeDo’s list of Python videoswhich contains 94 Python tutorials including almost 2-hours worth of tuition in the Python Newbies on XP series.

TSP & Python

Montgomery (ShowMeDo author) has started a very nice blog series on Solving the classic Travelling Salesman Problem (TSP) with Python.

So far there are 3 posts in the series:

Yield, Generators & Iterables, non separable concepts in Python


Iterables

To understand what yield does, you must understand what generators are. And before generators come iterables. When you create a list, you can read its items one by one, and it's called iteration :
>>> mylist = [1, 2, 3]
>>> for i in mylist :
...    print(i)
1
2
3
Mylist is an iterable. When you use a comprehension list, you create a list, and so an iterable :
>>> mylist = [x*x for x in range(3)]
>>> for i in mylist :
...    print(i)
0
1
4
Everything you can use "for... in..." on is an iterable : lists, strings, files... These iterables are handy because you can read them as much as you wish, but you store all the values in memory and it's not always what you want when you have a lot of values.

Generators

Generators are iterables, but you can only read them once. It's because they do not store all the values in memory, they generate the values on the fly :
>>> mygenerator = (x*x for x in range(3))
>>> for i in mygenerator :
...    print(i)
0
1
4
It just the same except you used () instead of []. BUT, you can not perform for i in mygenerator a second time since generators can only be used once: they calculate 0, then forget about it and calculate 1 and ends calculating 4, one by one.

Yield

Yield is a keyword that is used like return, except the function will return a generator.
>>> def createGenerator() :
...    mylist = range(3)
...    for i in mylist :
...        yield i*i...
>>> mygenerator = createGenerator() # create a generator
>>> print(mygenerator) # mygenerator is an object !
<generator object createGenerator at 0xb7555c34>
>>> for i in mygenerator:
...     print(i)
0
1
4
Here it's a useless example, but it's handy when you know your function will return a huge set of values that you will only need to read once.
To master yield, you must understand that when you call the function, the code you have written in the function body does not run. The function only returns the generator object, this is bit tricky :-)