Results 1 to 9 of 9
  1. #1
    Senior Member
    Join Date
    Aug 2009
    Posts
    1,725

    Default Quick Question on Python

    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')

  2. #2
    Large Member plod's Avatar
    Join Date
    Mar 2005
    Posts
    7,413

    Default Re: Quick Question on Python

    Snakes in planes?
    samsung, discovering a new galaxy every month

  3. #3
    Gone Erayd's Avatar
    Join Date
    Dec 2004
    Location
    Wellington, NZ
    Posts
    5,761

    Default Re: Quick Question on Python

    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...
    If you are interested in reading fanfiction on a mobile device or ebook reader, please visit flagfic.com.

  4. #4
    Soaring like an Eagle gary67's Avatar
    Join Date
    Aug 2007
    Location
    In a field in Hanmer
    Posts
    14,909

    Default Re: Quick Question on Python

    Quote Originally Posted by darkstar09 View Post
    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

  5. #5
    Senior Member
    Join Date
    Aug 2009
    Posts
    1,725

    Default Re: Quick Question on Python

    Quote Originally Posted by Erayd View Post
    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

    Quote Originally Posted by gary67 View Post
    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.
    Last edited by xyz823; 13-03-2012 at 06:32 PM.

  6. #6
    Gone Erayd's Avatar
    Join Date
    Dec 2004
    Location
    Wellington, NZ
    Posts
    5,761

    Default Re: Quick Question on Python

    Quote Originally Posted by darkstar09 View Post
    ...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.
    If you are interested in reading fanfiction on a mobile device or ebook reader, please visit flagfic.com.

  7. #7
    Senior Member
    Join Date
    Aug 2009
    Posts
    1,725

    Default Re: Quick Question on Python

    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:

    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?

  8. #8
    Gone Erayd's Avatar
    Join Date
    Dec 2004
    Location
    Wellington, NZ
    Posts
    5,761

    Default Re: Quick Question on Python

    Quote Originally Posted by darkstar09 View Post
    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 .
    If you are interested in reading fanfiction on a mobile device or ebook reader, please visit flagfic.com.

  9. #9
    Senior Member
    Join Date
    Aug 2009
    Posts
    1,725

    Default Re: Quick Question on Python

    Quote Originally Posted by Erayd View Post
    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.

Similar Threads

  1. A quick question
    By bot in forum PC World Chat
    Replies: 13
    Last Post: 20-04-2011, 08:41 PM
  2. Quick question
    By Renmoo in forum PC World Chat
    Replies: 8
    Last Post: 13-02-2010, 09:04 PM
  3. Quick question...
    By pcuser42 in forum PressF1
    Replies: 3
    Last Post: 17-02-2007, 08:14 AM
  4. Quick UPS Question
    By bob_doe_nz in forum PC World Chat
    Replies: 2
    Last Post: 07-07-2005, 10:15 PM
  5. Replies: 11
    Last Post: 09-06-2004, 11:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •