"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\n",
"import string\n",
"\n",
"def qsort(p):\n",
" if p == []: \n",
" return []\n",
" else:\n",
" pivot = p[0]\n",
" lesser = qsort([x for x in p[1:] if x < pivot])\n",
" greater = qsort([x for x in p[1:] if x >= pivot])\n",
" return lesser + [pivot] + greater\n",
" \n",
"#Variable Declaration \n",
"str=\"Function pointers provide flexibility.\" \n",
"\n",
"#sorting the string\n",
"str=qsort(str)\n",
"str=string.join(str)\n",
"\n",
"#Result\n",
"print \"sorted strng: \",str\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"sorted strng: . F b c d e e e f i i i i i i l l n n n o o o p p r r s t t t u v x y\n"
]
}
],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 20.8, Page Number:482
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"import string\n",
"\n",
"def qsort(p):\n",
" \"\"\"Quicksort using list comprehensions\"\"\"\n",
" if p == []: \n",
" return []\n",
" else:\n",
" pivot = p[0]\n",
" lesser = qsort([x for x in p[1:] if x < pivot])\n",
" greater = qsort([x for x in p[1:] if x >= pivot])\n",
" return lesser + [pivot] + greater\n",
" \n",
"#Variable Declaration \n",
"num=[10,4,3,6,5,7,8]\n",
"\n",
"#sorting the string\n",
"num=qsort(num)\n",
"\n",
"#Result\n",
"for i in range(7):\n",
" print num[i],\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"3 4 5 6 7 8 10\n"
]
}
],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 20.9, Page Number:484
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"def space(count,ch=None):\n",
" if ch==None:\n",
" for i in xrange(count,0,-1):\n",
" print '',\n",
" else:\n",
" for i in xrange(count,0,-1):\n",
" print ch,\n",
" \n",
"\n",
"fp1=space\n",
"fp2=space\n",
"\n",
"fp1(22) #outputs 20 spaces\n",
"print \"|\\n\" \n",
"\n",
"fp2(30,'x') #output 30 xs \n",
"print \"|\\n\"\n",
" "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
" |\n",
"\n",
"x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x |\n",
"\n"
]
}
],
"prompt_number": 10
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"