944,205 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 557
  • Python RSS
Nov 6th, 2009
-1

I require help with making 3 simple program/functions

Expand Post »
1. I need to make a program that creates a list that contains the first n perfect squares. All I know is that it starts with

n = int(raw_input("How many squares? "))

and ends with

print squares

An example of how this program should work is:

How many squares? 5
[1, 4, 9, 16, 25]

2. I need to create a function that takes a list as its only argument. It's needs to return a new list containing all (and only) the elements of 1 which are divisible by 2. The original list needs to remain the same.

Example of how this works is:

even_only([1, 3, 6, 10, 15, 21, 28]) should return
[6, 10, 28]

3. I need a function that translates a single English sentence into pig latin.

I honestly have no idea how to start any of these and any help would be greatly appreciated.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Judgment is offline Offline
5 posts
since Oct 2009
Nov 7th, 2009
0
Re: I require help with making 3 simple program/functions
The first two only require one liners of logic, so here:

Python Syntax (Toggle Plain Text)
  1. n = int(raw_input("How many squares? "))
  2. squares = [x*x for x in range(1,n+1)]
  3. print square
Python Syntax (Toggle Plain Text)
  1. def even_only(argList):
  2. return filter(lambda m: m%2==0, argList)

As for the pig latin, you'll need to break the sentence down into words with split, do something to each word that will make it pig latin, then remake the sentence with join.
Last edited by Mathhax0r; Nov 7th, 2009 at 12:01 am.
Reputation Points: 12
Solved Threads: 15
Junior Poster in Training
Mathhax0r is offline Offline
64 posts
since Aug 2009
Nov 7th, 2009
0
Re: I require help with making 3 simple program/functions
Click to Expand / Collapse  Quote originally posted by Judgment ...
1. I need to make a program that creates a list that contains the first n perfect squares. All I know is that it starts with

n = int(raw_input("How many squares? "))

and ends with

print squares

An example of how this program should work is:

How many squares? 5
[1, 4, 9, 16, 25]

Create a loop structure that does n loops. Inside the loop, for each iteration, take the number of the iteration times itself and add it to a vector.

2. I need to create a function that takes a list as its only argument. It's needs to return a new list containing all (and only) the elements of 1 which are divisible by 2. The original list needs to remain the same.

Example of how this works is:

even_only([1, 3, 6, 10, 15, 21, 28]) should return
[6, 10, 28]

3. I need a function that translates a single English sentence into pig latin.

I honestly have no idea how to start any of these and any help would be greatly appreciated.
Use the modular division operator on each number inside a loop. If the result of modular division is 0 then the number is even, if the result is 1 then the number is odd.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
sfeldma3 is offline Offline
2 posts
since Oct 2009
Nov 7th, 2009
0
Re: I require help with making 3 simple program/functions
Click to Expand / Collapse  Quote originally posted by Mathhax0r ...
The first two only require one liners of logic, so here:

Python Syntax (Toggle Plain Text)
  1. n = int(raw_input("How many squares? "))
  2. squares = [x*x for x in range(1,n+1)]
  3. print square
Python Syntax (Toggle Plain Text)
  1. def even_only(argList):
  2. return filter(lambda m: m%2==0, argList)

As for the pig latin, you'll need to break the sentence down into words with split, do something to each word that will make it pig latin, then remake the sentence with join.
For homework problems please give help in the form of hints, not total solutions or the OP will not learn anything.
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Need helping writing Prime number code
Next Thread in Python Forum Timeline: several questions about Glade & pygtk





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC