2 Replies - 278 Views - Last Post: 22 January 2013 - 08:05 AM Rate Topic: -----

#1 ramiz127  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2102
  • View blog
  • Posts: 3,207
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7652
  • View blog
  • Posts: 12,908
  • 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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1