Tag Archives: Python

Implementing a Stack in python

To implement a Stack in python, we simply need to extend the existing list class! class Stack(list): def push(self, data): self.append(data) def tos(self): if self: return self[-1] def peek(self, index): if self and 0 <= index < len(self): return self[index] def __iter__(self): if self: ptr = len(self) – 1 while ptr >= 0: yield self[ptr] […]

0  

Iterating over a custom ‘Jagged Array’ in Python

A jagged array is an array whose elements are arrays. The elements of a jagged array can be of different dimensions and sizes. A jagged array is sometimes called an "array of arrays." C# natively differentiates between a jagged and a multidimensional array. To iterate over a every single element of a Jagged Array in […]

0  

read(), readline(), readlines() & xreadlines() func in Python

While doing a regression analysis, I had to read & parse through a large .dat file. I called readline() func but was surprised to see that I was actually iterating through every single character in the first line! with open(filePath, ‘r’) as f: for line in f.readline(): print line Then realization dawned upon me! * […]

2  

Convert vowels to uppercase in a string

Given a string – say “hello world”, I was interested in capitalizing the vowels in it – IN A CLEAN & EFFICIENT WAY! In the first approach, I use a StringBuilder object to hold the intermediate String while in the second approach, I’m using a C# feature – Linq. In the second approach, there is […]

0