3 Replies - 3503 Views - Last Post: 27 May 2013 - 01:50 AM Rate Topic: -----

#1 Brilliantwarrior  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 05-December 12

Function to return a list of natural numbers

Posted 25 March 2013 - 07:34 AM

I am currently learning Python 3, independently. I'm having difficulty with creating an algorithm to write the function, once I know what I need to do, then I can do it.
Task- Write a function naturalNumbers which takes a positive integer n as input, and returns a list [1, 2, ] consisting of the first n natural numbers.
def naturalNumbers(n):
   d = n+1
   for a in range(1,d):
      b = [d-a]
      print(B)/> 


I'm supposed to use: a For Statement, List Repetition and/or List Concatenation.
A few pointers in the right direction would be greatly appreciated, thanks.

I'm not sure why the b in "print(B)" is capitalizing, or adding a forward slash and bracket.

Is This A Good Question/Topic? 0
  • +

Replies To: Function to return a list of natural numbers

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5537
  • View blog
  • Posts: 11,866
  • Joined: 02-June 10

Re: Function to return a list of natural numbers

Posted 25 March 2013 - 07:42 AM

Its a bug in the syntax highlighter thinking it is a the sunglasses smiley. If you use a variable other than B it won't do that. But

B ) is the sunglass smiley

Or add a space between them
Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7807
  • View blog
  • Posts: 13,201
  • Joined: 19-March 11

Re: Function to return a list of natural numbers

Posted 25 March 2013 - 09:25 AM

You want to return a list of numbers, not print them out.

def f(n): 
  return range(1,n+1)



will do the job.

There are more convoluted solutions, for example this one with a listcomp:

def g(n):
  return [i+1 for i in range(n)]



or this one, which is just annoying:

deg h(n):
  l = []
  i = 1
  while i<= n:
    l.append(i)
    i+=1
  return l




But the first is probably the simplest. There isn't really an algorithm to find here...

EDIT: sorry, didn't see your requirements. I guess the first one is out, even though it's the right answer. The last one should never be written in python, but it's basically a for loop. (if you replace the while with a for i in range(n) then it's really, really embarrassing, no?)

I don't know what a "list repetition" is, but the list comprehension in the second example is in a sense equivalent to a for loop, so that's probably your best bet.
As an interesting exercise, try to work out in what ways a listcomp is and is not equivalent to a for loop. This is not extremely difficult to do, but there are lots of people who don't get this, so you'll be well ahead of the pack if you understand this and can provide examples that prove the point.

This post has been edited by jon.kiparsky: 25 March 2013 - 09:38 AM

Was This Post Helpful? 1
  • +
  • -

#4 Reloader  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 27-May 13

Re: Function to return a list of natural numbers

Posted 27 May 2013 - 01:50 AM

I myself was intrigued with this problem... took me two days to solve it the way it is meant to be solved (for loop, List Creation or Concatenation), but eventually find out that solution is easier than anything :)

So, here it is:

def naturalNumbers(n):
   mylist = []
   for x in range(1, n+1):
      mylist = mylist + [x]
   return mylist

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1