Skip to content

Instantly share code, notes, and snippets.

@huseyinyilmaz
Created August 2, 2012 08:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huseyinyilmaz/3235444 to your computer and use it in GitHub Desktop.
Save huseyinyilmaz/3235444 to your computer and use it in GitHub Desktop.
My take on an adder function problem I found here http://blog.jazzychad.net/2012/08/01/array-iteration-problem.html
#!/usr/bin/env python2
"""
My take on an adder function problem I found here
http://blog.jazzychad.net/2012/08/01/array-iteration-problem.html
"""
def add1(arr, val, n):
"""add1 - increments items in an array matching specified value
param: arr - array of integers to manipulate
param: val - integer, value to increment
param: n - integer, control value specifying behavior of manipulation
n == 0 means increment all occurrences of val
n > 0 means increment up to n occurrences of val
from left-to-right (forward)
n < 0 means increment up to n occurrences of val
from right-to-left (backward)
return: arr with proper values incremented
>>> add1([1,4,1,5,1], 1, 0)
[2, 4, 2, 5, 2]
>>> add1([1,4,1,5,1], 1, 2)
[2, 4, 2, 5, 1]
>>> add1([1,4,1,5,1], 1, -2)
[1, 4, 2, 5, 2]
"""
# reverse list if necessary
if n < 0:
is_reversed = True
arr = reversed(arr)
n *= -1
else:
is_reversed = False
# Hack for read only cascaded function scopes.
# In python 3, I could use nonlocal instead of this hack
class NameSpace:pass
ns = NameSpace()
ns.n = n
def addone_n_times(v):
"""
Adds one to given argument only
n time after nth addition it will return argument
itself
"""
if ns.n > 0 and v == val:
ns.n -= 1
resp = v + 1
else:
resp = v
return resp
if n == 0:
map_func = lambda x: x + 1 if x == val else x
else:
map_func = addone_n_times
result = map(map_func, arr)
result = list(reversed(result)) if is_reversed else result
return result
Filename: add.py
Line # Mem usage Increment Line Contents
==============================================
6 @profile
7 def add1(arr, val, n):
8 """add1 - increments items in an array matching specified value
9
10 param: arr - array of integers to manipulate
11 param: val - integer, value to increment
12 param: n - integer, control value specifying behavior of manipulation
13 n == 0 means increment all occurrences of val
14
15 n > 0 means increment up to n occurrences of val
16 from left-to-right (forward)
17
18 n < 0 means increment up to n occurrences of val
19 from right-to-left (backward)
20
21 return: arr with proper values incremented
22
23 >>> add1([1,4,1,5,1], 1, 0)
24 [2, 4, 2, 5, 2]
25 >>> add1([1,4,1,5,1], 1, 2)
26 [2, 4, 2, 5, 1]
27 >>> add1([1,4,1,5,1], 1, -2)
28 [1, 4, 2, 5, 2]
29 """
30
31 319.6797 MB 0.0000 MB # reverse list if necessary
32 319.6836 MB 0.0039 MB if n < 0:
33 319.6836 MB 0.0000 MB is_reversed = True
34 319.6836 MB 0.0000 MB arr = reversed(arr)
35 319.6836 MB 0.0000 MB n *= -1
36 else:
37 is_reversed = False
38
39 # Hack for read only cascaded function scopes.
40 # In python 3, I could use nonlocal instead of this hack
41 319.6836 MB 0.0000 MB class NameSpace:pass
42 319.6836 MB 0.0000 MB ns = NameSpace()
43 319.6836 MB 0.0000 MB ns.n = n
44
45 319.6836 MB 0.0000 MB def addone_n_times(v):
46 """
47 Adds one to given argument only
48 n time after nth addition it will return argument
49 itself
50 """
51 if ns.n > 0 and v == val:
52 ns.n -= 1
53 resp = v + 1
54 else:
55 resp = v
56 return resp
57
58 319.6836 MB 0.0000 MB if n == 0:
59 map_func = lambda x: x + 1 if x == val else x
60 else:
61 319.6836 MB 0.0000 MB map_func = addone_n_times
62
63 396.2891 MB 76.6055 MB result = map(map_func, arr)
64
65 396.2891 MB 0.0000 MB result = list(reversed(result)) if is_reversed else result
66 396.2891 MB 0.0000 MB return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment