#! /usr/bin/python # array1.py this is actually a list # x = raw_input("Enter integer data in one line: ") y = x.split() n = len(y) data = [] for i in range(n): if y[i].isdigit(): data.append(int(y[i])) else: print 'Non-integer data' for i in range(n): print data[i] #! /usr/bin/python # cTofP.py x = raw_input("Enter the Celsius value: ") if x.isdigit(): c = float(x) f = c*9.0/5.0+32.0 print c, 'C', f, 'F' else: print "Wrong data" #! /usr/bin/python x = raw_input("Enter three integers: ") dl = x.split() d1, d2, d3 = dl[0], dl[1], dl[2] lrg = d1 if d2 > lrg: lrg = d2 if d3 > lrg: lrg = d3 print 'large(', d1, ',', d2, ',', d3, ') = ', lrg #! /usr/bin/python # fact1.py # x = raw_input("Enter a non -ve integer: ") if x.isdigit(): n = int(x) fact = 1 for i in range(n+1)[1:]: fact *= i print n, '!=', fact else: print "Not a number" #! /usr/bin/python # fact1a.py # x = raw_input("Enter a non -ve integer: ") if x.isdigit(): n = int(x) i = fact = 1 while i <= n: fact = fact*i i = i+1 print n, '!=', fact else: print "Not a number" #! /usr/bin/python # fact2.py # def fact(n): if n == 0: return 1 else: return n*fact(n-1) # main x = raw_input("Enter a non -ve integer: ") if x.isdigit(): n = int(x) print n, '!=', fact(n) else: print "Not a number" #! /usr/bin/python # firstP.ml print 'First Python program' #! /usr/bin/python # insertionSort1.py # import array def insertionSort(data, n): for i in range(n)[1:]: temp = data[i] j = i-1 while j >= 0: if data[j] > temp: data[j+1] = data[j] j = j - 1 else: break data[j+1] = temp x = raw_input("Enter the number of data: ") n = int(x) y = raw_input("Enter integer data:") z = y.split(); data = array.array('l') for i in range(n): if z[i].isdigit(): data.append(int(z[i])) else: print 'Data error' print 'Input data:' for i in range(n): print data[i] insertionSort(data,n) print 'Sorted data:' for i in range(n): print data[i] # module stack stack.py # def create(): return [] def isEmpty(s): if len(s) == 0: return True else: return False def push(s, d): s.append(d) def pop(s): if isEmpty(s) == True: print 'Empty stack' else: s.pop() def top(s): if isEmpty(s) == True: print 'Empty stack' else: return s[len(s)-1] #! /usr/bin/python # testStack.py uses the module stack.py # import stack s = stack.create() if stack.isEmpty(s): print 'Empty stack' else: print 'Non-empty stack' stack.push(s,5) if stack.isEmpty(s): print 'Empty stack' else: print 'Non-empty stack' stack.push(s,15) stack.push(s,51) stack.push(s,17) stack.push(s,23) print stack.top(s) stack.pop(s) print stack.top(s)