1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
{
"metadata": {
"name": "ch15"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": "Chapter 15 : File Input/Output and apmatrixes"
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": "example 15.1 page no : 162\n"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''\nexample 15.1 page no : 162\n'''\ntry:\n fileName = \"a.txt\"\n f = open(fileName,\"r\")\nexcept:\n print \"Unable to open the file named \" , fileName;\n import sys\n sys.exit(0)\n\n# reading file line by line\n \nfor i in f.readlines():\n print i\n\nf.close() ",
"language": "python",
"metadata": {},
"outputs": [
{
"ename": "SystemExit",
"evalue": "0",
"output_type": "pyerr",
"traceback": [
"An exception has occurred, use %tb to see the full traceback.\n",
"\u001b[1;31mSystemExit\u001b[0m\u001b[1;31m:\u001b[0m 0\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": "Unable to open the file named a.txt\n"
},
{
"output_type": "stream",
"stream": "stderr",
"text": "To exit: use 'exit', 'quit', or Ctrl-D.\n"
}
],
"prompt_number": 1
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": "example 15.2 page no : 163\n"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''\nexample 15.2 page no : 163\n'''\n\ntry:\n fileName = \"a.txt\"\n outputFile = \"b.txt\"\n f = open(fileName,\"r\")\n w = open(outputFile,\"w\")\nexcept:\n print \"Unable to open the file named \" , fileName;\n import sys\n sys.exit(0)\n\n# reading file line by line\n \nfor i in f.readlines():\n w.write(i)\n print i\n \nf.close()\nw.close() ",
"language": "python",
"metadata": {},
"outputs": [
{
"ename": "SystemExit",
"evalue": "0",
"output_type": "pyerr",
"traceback": [
"An exception has occurred, use %tb to see the full traceback.\n",
"\u001b[1;31mSystemExit\u001b[0m\u001b[1;31m:\u001b[0m 0\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": "Unable to open the file named a.txt\n"
},
{
"output_type": "stream",
"stream": "stderr",
"text": "To exit: use 'exit', 'quit', or Ctrl-D.\n"
}
],
"prompt_number": 2
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": "example 15.3 page no : 167\n"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''\nexample 15.3 page no : 167\n'''\n\nclass Set:\n def __init__(self,n):\n self.elements = []\n self.numElements = 0\n\n def getNumElements(self):\n return self.numElements\n\n def getElement(self,i):\n if (i < self.numElements):\n return slef.elements[i];\n else:\n print \"Set index out of range.\" \n import sys\n sys.exit(0)\n\n def find(self,s):\n for i in range(self.numElements):\n if (self.elements[i] == s): \n return i;\n return -1;\n\n def add(self,s):\n # if the element is already in the set, return its index\n index = self.find(s);\n if (index != -1):\n return index;\n # if the apvector is full, double its size\n # add the new elements and return its index\n self.elements.append(s)\n self.numElements += 1\n return self.numElements - 1;\n\ncities= Set (2);\ncity1 = 'ahmedabad'\ncity2 = 'rajkot'\nindex1 = cities.add(city1);\nindex2 = cities.add(city2)\nprint index1, index2",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "0 1\n"
}
],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": "",
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": "",
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
|