473,978 Members | 2,597 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using recursion

6 New Member
new to python and I have this assingment: I need to write a recursive function that takes paramaters (aString,nTimes ) and it needs to print the aString parameter the number of times specified by nTimes. I cannot use a for or while loop, just recursion. How would I go about doing this? all i know how to do is get it started and use recursion to call function again.

def printNtimes(aSt ring,nTimes):

I also know how to do it using recursion with one parameter:
Expand|Select|Wrap|Line Numbers
  1. def n_lines(n):
  2.       if n>0:
  3.           print "Line"
  4.           n_lines(n-1)
Jul 24 '07 #1
2 1722
bartonc
6,596 Recognized Expert Expert
new to python and I have this assingment: I need to write a recursive function that takes paramaters (aString,nTimes ) and it needs to print the aString parameter the number of times specified by nTimes. I cannot use a for or while loop, just recursion. How would I go about doing this? all i know how to do is get it started and use recursion to call function again.

def printNtimes(aSt ring,nTimes):

I also know how to do it using recursion with one parameter:
Expand|Select|Wrap|Line Numbers
  1. def n_lines(n):
  2.       if n>0:
  3.           print "Line"
  4.           n_lines(n-1)
It's just the same; no big change:
Expand|Select|Wrap|Line Numbers
  1. def n_lines(anStr, n):
  2.       if n>0:
  3.           print anStr
  4.           n_lines(anStr, n-1)
  5.  
  6.  
  7. n_lines("hello", 5)
To learn how to do the pretty printing (which I've added to your post), please read the "REPLY GUIDELINE" on the right hand side of the page while you reply. They are required under the rules of our Posting Guidelines. Thanks
Jul 24 '07 #2
bvdet
2,851 Recognized Expert Moderator Specialist
It's just the same; no big change:
Expand|Select|Wrap|Line Numbers
  1. def n_lines(anStr, n):
  2.       if n>0:
  3.           print anStr
  4.           n_lines(anStr, n-1)
  5.  
  6.  
  7. n_lines("hello", 5)
To learn how to do the pretty printing (which I've added to your post), please read the "REPLY GUIDELINE" on the right hand side of the page while you reply. They are required under the rules of our Posting Guidelines. Thanks
A very minor change:
Expand|Select|Wrap|Line Numbers
  1. def n_lines(anStr, n):
  2.       if n:
  3.           print anStr
  4.           n_lines(anStr, n-1)
Jul 24 '07 #3

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

Similar topics

5
3364
by: hokiegal99 | last post by:
A few questions about the following code. How would I "wrap" this in a function, and do I need to? Also, how can I make the code smart enough to realize that when a file has 2 or more bad charcters in it, that the code needs to run until all bad characters are gone? For example, if a file has the name "<bad*mac\file" the program has to run 3 times to get all three bad chars out of the file name. The passes look like this:
12
2795
by: da Vinci | last post by:
Greetings. I want to get everyone's opinion on the use of recursion. We covered it in class tonight and I want a good solid answer from people in the "know" on how well recursion is accepted in modern day applications. Should we readily use it when we can or only when absolutly forced to use it?
4
319
by: Alexis | last post by:
Hi, I need to transform one xml document into a second xml document. They both have many nodes so xslt works fine, but there is one node I have not figure out how to transform. Here it is: Node in Input File. <FullAddress>JOHN SMITH@19495 BISCAYNE BLVD@MIAMI@FL/33180@USA</FullAddress>
11
3926
by: Ken | last post by:
Hello, I have a recursive Sierpinski code here. The code is right and every line works fine by itself. I wish for all of them to call the function DrawSierpinski. But in this cae it only calls the first recusive function and at soon as n = 4 its stop. How do I go about so that all 8 lines get 4 recusion thanks ken
75
5743
by: Sathyaish | last post by:
Can every problem that has an iterative solution also be expressed in terms of a recursive solution? I tried one example, and am in the process of trying out more examples, increasing their complexity as I go. Here's a simple one I tried out: #include<stdio.h> /* To compare the the time and space cost of iteration against
18
3745
by: MTD | last post by:
Hello all, I've been messing about for fun creating a trial division factorizing function and I'm naturally interested in optimising it as much as possible. I've been told that iteration in python is generally more time-efficient than recursion. Is that true? Here is my algorithm as it stands. Any suggestions appreciated!
12
5846
by: NOO Recursion | last post by:
Hi everyone! I am trying to write a program that will search a 12x12 for a thing called a "blob". A blob in the grid is made up of asterisks. A blob contains at least one asterisk. If an asterisk is in a blob, an asterisk that is contiguous to it is in the same blob. If a blob has more than two asterisks, then each asterisk in the blob is contiguous to at least one other asterisk in the blob. For example this 12x12 grid has 6 blobs. ...
13
2927
by: Mumia W. | last post by:
Hello all. I have a C++ program that can count the YOYOs that are in a grid of Y's and O's. For example, this Y O Y O O Y O Y O Y O O Y O Y Y O Y O Y O O Y O O Y Y O Y O
35
4811
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
0
10356
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10172
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11823
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10917
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7618
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6561
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
5161
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4733
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3765
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.