473,378 Members | 1,351 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Python Help -- Apply a procedure repeatedly

Hi, I'm trying to write a function that takes in as arguments a procedure with one argument and an integer. The integer represents how many times the procedure will execute on the argument.

Expand|Select|Wrap|Line Numbers
  1. def compose(f,g):
  2.     return lambda x: f(g(x))
  3.  
  4.  
  5. def repeatedlyApply (p,n):
  6.     i=0
  7.     q=0
  8.     while (i<n):
  9.         q = compose(p,p)
  10.         i=i+2
  11.     return q
  12.  
This is what I had in mind... I had q = q + compose(p,p) but it tells me it can't add a function and an integer. Which I see. But when I have what's written above, it returns the value of q and not a memory location.

I'm a brand new programmer... Any ideas would be greatly appreciated.
Feb 21 '07 #1
10 3375
bartonc
6,596 Expert 4TB
Hi, I'm trying to write a function that takes in as arguments a procedure with one argument and an integer. The integer represents how many times the procedure will execute on the argument.

Expand|Select|Wrap|Line Numbers
  1. def compose(f,g):
  2.     return lambda x: f(g(x))
  3.  
  4.  
  5. def repeatedlyApply (p,n):
  6.     i=0
  7.     q=0
  8.     while (i<n):
  9.         q = compose(p,p)
  10.         i=i+2
  11.     return q
  12.  
This is what I had in mind... I had q = q + compose(p,p) but it tells me it can't add a function and an integer. Which I see. But when I have what's written above, it returns the value of q and not a memory location.

I'm a brand new programmer... Any ideas would be greatly appreciated.
I'm going to look at your code when I get a break. I just wanted to whip off a quick note to welcome you and say "for a beginner you sure got a great handle". Thanks for joining,
Barton
Feb 22 '07 #2
I'm going to look at your code when I get a break. I just wanted to whip off a quick note to welcome you and say "for a beginner you sure got a great handle". Thanks for joining,
Barton
Thanks a lot, Barton!
Feb 22 '07 #3
bvdet
2,851 Expert Mod 2GB
Hi, I'm trying to write a function that takes in as arguments a procedure with one argument and an integer. The integer represents how many times the procedure will execute on the argument.

Expand|Select|Wrap|Line Numbers
  1. def compose(f,g):
  2.     return lambda x: f(g(x))
  3.  
  4.  
  5. def repeatedlyApply (p,n):
  6.     i=0
  7.     q=0
  8.     while (i<n):
  9.         q = compose(p,p)
  10.         i=i+2
  11.     return q
  12.  
This is what I had in mind... I had q = q + compose(p,p) but it tells me it can't add a function and an integer. Which I see. But when I have what's written above, it returns the value of q and not a memory location.

I'm a brand new programmer... Any ideas would be greatly appreciated.
What type of arguments do you intend to pass to repeatedlyApply? It appears that one of the arguments, 'p', is another function like 'sqrt'. Function compose returns an anonymous function which must be evaluated in order to return a value. Can you give us some more information so we can better help you?
This all I could come up with:
Expand|Select|Wrap|Line Numbers
  1. def compose(f,g):
  2.     return lambda x: f(g(x))
  3.  
  4. def repeatedlyApply (p, n, v):
  5.     for _ in [num for num in range(n) if num % 2 == 1]:
  6.         z = compose(p,p)
  7.         print z
  8.         v = z(v)
  9.     return v
  10.  
  11. from math import sqrt
  12.  
  13. print repeatedlyApply(sqrt, 5, 15823959.67)
  14.  
  15. """
  16. >>> <function <lambda> at 0x00E10F30>
  17. <function <lambda> at 0x00DD14F0>
  18. 2.81810517827
  19. >>>
  20. """
Feb 22 '07 #4
Okay, sorry about that... so say if I put in:

repeatedlyApply(lambda x: 2*x,3)("abc")

I should get: 'abcabcabcabcabcabcabcabc'

or repeatedlyApply(lambda x: x+1,10)(100)

should yield 110

The first argument I pass is a function with one argument of its own (In the first case "abc" and in the second "100") and the second is the number of times to repeat the operation on the variable and add to the total.
Feb 22 '07 #5
bvdet
2,851 Expert Mod 2GB
Okay, sorry about that... so say if I put in:

repeatedlyApply(lambda x: 2*x,3)("abc")

I should get: 'abcabcabcabcabcabcabcabc'

or repeatedlyApply(lambda x: x+1,10)(100)

should yield 110

The first argument I pass is a function with one argument of its own (In the first case "abc" and in the second "100") and the second is the number of times to repeat the operation on the variable and add to the total.
Something like this?
Expand|Select|Wrap|Line Numbers
  1. def repeatedlyApply (f, x, y, n):
  2.     q = 0
  3.     for i in range(n):
  4.         q += f(x,y)
  5.     return q
  6.  
  7. func = lambda x, y: x**2+y**2-8
  8.  
  9. print repeatedlyApply(func, 6, 4, 6)
  10.  
  11. >>> 264
Feb 22 '07 #6
ghostdog74
511 Expert 256MB
Okay, sorry about that... so say if I put in:

repeatedlyApply(lambda x: 2*x,3)("abc")

I should get: 'abcabcabcabcabcabcabcabc'

or repeatedlyApply(lambda x: x+1,10)(100)

should yield 110

The first argument I pass is a function with one argument of its own (In the first case "abc" and in the second "100") and the second is the number of times to repeat the operation on the variable and add to the total.


you might be better off using map() or list comprehension. just an example only
Expand|Select|Wrap|Line Numbers
  1. >>> map(lambda x: 3*x, ["abc"])
  2. ['abcabcabc']
  3.  
Feb 22 '07 #7
Thanks guys! Exactly what I needed, :-)

Hopefully when I get better at this stuff, I can come back here and contribute.
Feb 22 '07 #8
bartonc
6,596 Expert 4TB
Thanks guys! Exactly what I needed, :-)

Hopefully when I get better at this stuff, I can come back here and contribute.
Come back any time with questions, too. Keep posting,
Barton
Feb 22 '07 #9
Hi!

I've just found this through google.

You can define:
Expand|Select|Wrap|Line Numbers
  1. from functools import partial
  2. from itertools import repeat
  3.  
  4. def compose(f, g): #  also in the functional module
  5.     def func(*args, **kwargs):
  6.         return f(g(*args, **kwargs))
  7.     return func
  8.  
  9. mcompose = partial(reduce, compose)
  10. apply_n_times = compose(mcompose, repeat)
Then you get:
Expand|Select|Wrap|Line Numbers
  1. >>> def f(x):
  2. ...     return 2 * x
  3. >>> apply_n_times(f, 3)("abc")
  4. 'abcabcabcabcabcabcabcabc'
  5. >>> apply_n_times(f, 3)(2)
  6. 16
Oct 29 '08 #10
boxfish
469 Expert 256MB
It's great that you could answer this question, but it's really old and was answered already. If you want to answer any fairly recently asked questions, I'm sure your help will be appreciated. Please read the posting guidelines.
Thanks.
Oct 29 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
226
by: Stephen C. Waterbury | last post by:
This seems like it ought to work, according to the description of reduce(), but it doesn't. Is this a bug, or am I missing something? Python 2.3.2 (#1, Oct 20 2003, 01:04:35) on linux2 Type...
176
by: Thomas Reichelt | last post by:
Moin, short question: is there any language combining the syntax, flexibility and great programming experience of Python with static typing? Is there a project to add static typing to Python? ...
267
by: Xah Lee | last post by:
Python, Lambda, and Guido van Rossum Xah Lee, 2006-05-05 In this post, i'd like to deconstruct one of Guido's recent blog about lambda in Python. In Guido's blog written in 2006-02-10 at...
10
by: Jerry | last post by:
I have just started to do some semi-serious programming (not one-off specialized scripts) and am loving Python. I just seem to get most of the concepts, the structure, and the classes (something I...
2
by: Gigs_ | last post by:
Is there any way to convert ocaml code to python? but not manually thx
3
by: tkpmep | last post by:
Instead of starting IDLE as I normally do, I started the Python interpreter and tried to run a program. I got a Python prompt (>>>), and then tried unsuccessfully to run a Python script named...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.