Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 23rd, 2012, 01:50 PM
norm850 norm850 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 15 norm850 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 13 m 33 sec
Reputation Power: 0
Recursive code to print samples size of size n

Hey guys, I need to write code that will take a list and print samples of size n with replacement, recursively. I have written a program that will print samples of size n from a list, but now how do I do it recursively?

Here is my code for the program done not recursively:
Code:
S = []
L = ['a','b','c','d']
N = len(L)
for k1 in range(N):
    if L[k1]:
        S.append(L[k1])
    for k2 in range(N):
        S.append(L[k2])
        for k3 in range(N):
            S.append(L[k3])
            print S
            L[k3] = S.pop()
        L[k2] = S.pop()
    L[k1] = S.pop()


So this code will print [a,a,a], [a,a,b], [a,a,c], [a,b,a],..., [d,d,c], [d,d,d].

Reply With Quote
  #2  
Old September 23rd, 2012, 07:36 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,458 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 26 m 43 sec
Reputation Power: 403
An easy way to solve this sort of problem: learn lisp, find a lisp code for it. Translation to python is straightforward. I didn't need to resort to that, and came up with:
Code:
def f(A,B,N):
    if not N:
        print(B)
    else:
        for C in A:
            f(A,B+C,N-1)

print('')
f('abcd','',2)
print('')
f('abcd','',3)
print('')
f('abcd','',5)
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Recursive code to print samples size of size n

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap