public
Created

My take on an adder function problem I found here http://blog.jazzychad.net/2012/08/01/array-iteration-problem.html

  • Download Gist
add1.py
Python
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
#!/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
add1_profile_result.txt
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
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.