PDA

View Full Version : Quick Question on Python



xyz823
13-03-2012, 04:56 PM
This is the question I'm having trouble with

Write a function pair_swap(pair) that takes a two-element tuple pair as a parameter and returns a new tuple consisting of the elements of pair swapped.

For example:

print pair_swap((10, 20)) should output (20, 10)

print pair_swap(("John Smith", 3512344)) should output (3512344, 'John Smith')

plod
13-03-2012, 06:33 PM
Snakes in planes?

Erayd
13-03-2012, 07:02 PM
This appears to be a homework question. Where are you getting stuck?

Simply posting the question with no additional data is essentially just asking us to do your homework for you...

gary67
13-03-2012, 07:22 PM
This is the question I'm having trouble with

Write a function pair_swap(pair) that takes a two-element tuple pair as a parameter and returns a new tuple consisting of the elements of pair swapped.

For example:

print pair_swap((10, 20)) should output (20, 10)

print pair_swap(("John Smith", 3512344)) should output (3512344, 'John Smith')

So are these print pair_swap equations not actually working as I don't really follow what your asking

xyz823
13-03-2012, 07:28 PM
This appears to be a homework question. Where are you getting stuck?

Simply posting the question with no additional data is essentially just asking us to do your homework for you...

I've gone through and checked the dir(tuple) but I'm not quite sure what functions I should be using to achieve the required output. A friend suggested we change the tuple to a list and then try reversing it then converting back to a tuple but I don't think thats been covered yet. Are we on the right track or is there another way to solve this? I've had a quick flick through the text book but haven't really found an answer and I'm studying for a different test tomorrow so had to give up for the night. I managed the other 16 questions quite easily, just stuck on how to solve this one


So are these print pair_swap equations not actually working as I don't really follow what your asking

I need to create a function and then run the examples to give the output shown.

Erayd
13-03-2012, 07:39 PM
...but I'm not quite sure what functions I should be using to achieve the required output. A friend suggested we change the tuple to a list and then try reversing it then converting back to a tuple but I don't think thats been covered yet.Half on the right track, half on the wrong track.

You don't need to convert it to a list, but because tuples are immutable you can't convert it in place. Reversing it is definitely a good path to follow :).

This whole problem can be done in a single (short) line of code; you don't need anything particularly complex.

xyz823
13-03-2012, 08:46 PM
Ah that was easy! Just trying to overcomplicate a simple thing! One more question I've got from another quiz on basic formatting.

The Question:

Using the str.format method and without using the global format function, write a function format_row(rank, name) that takes an integer rank in the range 0 - 9 and a string name of less than 18 characters in length and returns a 24-character result string as follows:

The result string starts and ends with '|'
Following the starting '|' there's a space character, the 1-digit rank, a period (i.e., '.') and another space.
The name follows the rank field (item 2) and is left justified.

This function might be used in, for example, a tabular display of sportspeople rankings.

My code:


template = '| {0}. {1}|'
def format_row(rank,name):
return template.format(rank, (format(name, "18")))

print format_row(1, 'Fred Bloggs')

print format_row(9, 'Michael Angus Ng')

print format_row(2,'abcdefghijklmnopqr')

The output:

| 1. Fred Bloggs |
| 9. Michael Angus Ng |
| 2. abcdefghijklmnopqr|

What the quiz tells me when I submit it:

def format(obj, fmt): assert False, 'Illegal use of the global format function'
print format_row(2,'abcdefghijklmnopqr')

I must be overlooking something surely as my output is exactly what the quiz shows? Or is it something to do with the global format function?

Erayd
14-03-2012, 11:39 AM
Or is it something to do with the global format function?Well the question tells you not to use it ("...without using the global format function..."), and you are using it - so you need to come up with a different solution that follows the instructions correctly ;).

xyz823
14-03-2012, 12:35 PM
Well the question tells you not to use it ("...without using the global format function..."), and you are using it - so you need to come up with a different solution that follows the instructions correctly ;).

Back to reading the text book then! I'll let you know how I get on.