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.

## All 3 Replies

The first two only require one liners of logic, so here:

``````n = int(raw_input("How many squares? "))
squares = [x*x for x in range(1,n+1)]
print square``````
``````def even_only(argList):
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.

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.

The first two only require one liners of logic, so here:

``````n = int(raw_input("How many squares? "))
squares = [x*x for x in range(1,n+1)]
print square``````
``````def even_only(argList):
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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.