How do i reduce this to a single function - the code is largely similar, just a direction of search toggle.
Chris Angelico
rosuav at gmail.com
Fri Nov 7 17:31:26 EST 2014
On Fri, Nov 7, 2014 at 9:16 PM, Veek M <vek.m1234 at gmail.com> wrote:
> def jump_to_blockD(self):
> end = len(self.b)
> row, col = self.w.cursor
> while row <= end:
> try:
> new_col = self.b[row].index('def')
> self.w.cursor = row, new_col
> break
> except ValueError:
> pass
> row += 1
>
> def jump_to_blockU(self):
> end = 0
> row, col = self.w.cursor
> while row >= end:
> try:
> new_col = self.b[row].rindex('def')
> self.w.cursor = row, new_col
> break
> except ValueError:
> pass
> row -= 1
Start by translating this into for loops, rather than while, and then
it'll be much easier to put all the differences in the signature. Tell
me if this translation is faithful:
def jump_to_blockD(self):
for row in range(self.w.cursor[0], len(self.b)+1, 1):
try:
new_col = self.b[row].index('def')
self.w.cursor = row, new_col
break
except ValueError:
pass
def jump_to_blockU(self):
for row in range(self.w.cursor[0], -1, -1):
try:
new_col = self.b[row].rindex('def')
self.w.cursor = row, new_col
break
except ValueError:
pass
Try those two, see if they function the same as your original code. If
they do, you should be able to figure out how to merge them, in their
new form.
ChrisA
More information about the Python-list
mailing list