# A function that takes an interger and returns set of sets.

Page 1 of 1

## 2 Replies - 716 Views - Last Post: 22 January 2013 - 08:05 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=308557&amp;s=08b30381b414b170bf62915c2060abbe&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ramiz127

Reputation: 0
• Posts: 7
• Joined: 17-January 13

# A function that takes an interger and returns set of sets.

Posted 21 January 2013 - 11:03 PM

I need a function rep(n) that takes an integer and returns set of sets representing n. It must behave this way:
>>> rep(0)
[]
>>> rep(1)
[[]]
>>> rep(2)
[[], [[]]]
>>> rep(3)
[[], [[]], [[], [[]]]]
Python is new to me, so I am struggling a lot with the syntax. I am not sure how to write this function.
This is what I makes sense to me but I am sure this is nowhere near python code"
```def rep(n):
[x for x in range(n)]
0 = []
1 = [[]]
for i in range(n)
return '[]' if i==0 else '[[]]' if n==1 else range(i)

```

Is This A Good Question/Topic? 0

## Replies To: A function that takes an interger and returns set of sets.

### #2 sepp2k

• D.I.C Lover

Reputation: 2620
• Posts: 4,175
• Joined: 21-June 11

## Re: A function that takes an interger and returns set of sets.

Posted 22 January 2013 - 05:05 AM

Could you describe your output format in words instead of by example?

And yes, the code you posted is not valid Python code¹ - so much so that I find it hard to tell what the code is trying to do. So if you could describe the logic you're trying to implement in words or pseudo code, that would help.

¹
• The body of a function definition or loop needs to be indented.
• A list comprehension that's not assigned to a variable, passed to a function or otherwise used doesn't do anything.
• You can't assign to numeric constants.
• Your for loop is missing a colon.
• If the first thing you do in a loop is to return from the function, you don't need the loop.

This post has been edited by sepp2k: 22 January 2013 - 05:07 AM

### #3 jon.kiparsky

• Beginner

Reputation: 11073
• Posts: 18,911
• Joined: 19-March 11

## Re: A function that takes an interger and returns set of sets.

Posted 22 January 2013 - 08:05 AM

You're going to need do to this recursively: if the input to the function, n, is zero, you return the empty list. If not, you call the function on n-1 and return the list containing the result of that call.

So if the function is called peano, peano of 0 returns []
peano(1) returns list(peano (0))

peano (2) returns list(peano(1))

and so forth

EDIT: "and so forth" does NOT mean that your code should enumerate cases for 1, 2, 3, etc! There are two cases that you have to consider: n == 0, and n >0.

This is assuming that n is a positive integer, of course.

This post has been edited by jon.kiparsky: 22 January 2013 - 08:08 AM