totally lost newbie

Discussion in 'Python' started by mark, May 27, 2007.

  1. mark

    mark Guest

    Hi all

    I posted earlier on this but have changed my approach so here is my
    latest attempt at solving a problem. I have been working on this for
    around 12 hours straight and am still struggling with it.

    Write a program that reads the values for a random list of cards from
    a file, where each line in the file specifies a single card with the
    rank and then the suit separated by a space. The rank should be an
    integer in the range of 1 to 13 (Ace:1, King:13), while the suit
    should be a lower case character in the set { 'h', 'd', 'c', 's' }.
    Sort the card entries into suits ordered by rank, and print out the
    ordered list. Hint: sort the list first by rank, and then by suit.

    The format of the cards.txt file is;

    1 h
    1 d
    13 c
    10 s

    and so on for the whole deck.

    Can someone help me to get the mycomp function to work.

    Any help appreciated

    J

    def read_cards(filename):

    cards = []
    for card in open(filename, 'r'):
    # strip the trailing newline character
    cards.append(card.strip())
    return cards

    filename = 'cards.txt'
    cards = read_cards(filename)



    def cards_str2tup(cards):

    cards_tup = []
    for card in cards:
    rank, suit = card.split()
    cards_tup.append((suit, int(rank)))
    return cards_tup

    def cards_tup2str(cards_tup):

    cards = []
    space = ' '
    for tup in cards_tup:
    suit, rank = tup
    s = str(rank) + space + suit
    cards.append(s)
    return cards

    def mycmp( a, b):
    #define the order in which the characters are to be sorted
    order = [ 'h', 'd', 'c', 's' ]
    # if the characters from each element ARENT the same
    if a[1] <> b[1]:
    #return the result of comparing the index of each elements
    character in the order list
    return cmp( order.index( a[1] ), order.index( b[1] ) )
    #otherwise
    else :
    #return the result of comparing each elements number
    return cmp( a[0], b[0] )

    cards.sort( mycmp )
    #print cards
     
    mark, May 27, 2007
    #1
    1. Advertising

  2. En Sun, 27 May 2007 07:19:15 -0300, mark <> escribió:

    > I posted earlier on this but have changed my approach so here is my
    > latest attempt at solving a problem. I have been working on this for
    > around 12 hours straight and am still struggling with it.


    Almost done. Just two things:
    - You have defined a function to convert the file format into tuples. But
    you are not consistent with the ordering: in the file they come
    rank+space+suit. When you convert to tuple you use (suit,rank). Inside the
    comparison function you use a[0] as rank and a[1] as suit again. Be
    consistent.
    - The sort expects a list of tuples, but you still use the lines read from
    the file; you forgot to call the function above to convert them.

    --
    Gabriel Genellina
     
    Gabriel Genellina, May 27, 2007
    #2
    1. Advertising

  3. In <>, mark wrote:

    > Hi all
    >
    > I posted earlier on this but have changed my approach so here is my
    > latest attempt at solving a problem. I have been working on this for
    > around 12 hours straight and am still struggling with it.
    >
    > Write a program that reads the values for a random list of cards from
    > a file, where each line in the file specifies a single card with the
    > rank and then the suit separated by a space. The rank should be an
    > integer in the range of 1 to 13 (Ace:1, King:13), while the suit
    > should be a lower case character in the set { 'h', 'd', 'c', 's' }.
    > Sort the card entries into suits ordered by rank, and print out the
    > ordered list. Hint: sort the list first by rank, and then by suit.
    >
    > The format of the cards.txt file is;
    >
    > 1 h
    > 1 d
    > 13 c
    > 10 s
    >
    > and so on for the whole deck.
    >
    > Can someone help me to get the mycomp function to work.
    >
    > Any help appreciated
    >
    > J
    >
    > def read_cards(filename):
    >
    > cards = []
    > for card in open(filename, 'r'):
    > # strip the trailing newline character
    > cards.append(card.strip())
    > return cards
    >
    > filename = 'cards.txt'
    > cards = read_cards(filename)
    >
    >
    >
    > def cards_str2tup(cards):
    >
    > cards_tup = []
    > for card in cards:
    > rank, suit = card.split()
    > cards_tup.append((suit, int(rank)))
    > return cards_tup
    >
    > def cards_tup2str(cards_tup):
    >
    > cards = []
    > space = ' '
    > for tup in cards_tup:
    > suit, rank = tup
    > s = str(rank) + space + suit
    > cards.append(s)
    > return cards
    >
    > def mycmp( a, b):
    > #define the order in which the characters are to be sorted
    > order = [ 'h', 'd', 'c', 's' ]
    > # if the characters from each element ARENT the same
    > if a[1] <> b[1]:
    > #return the result of comparing the index of each elements
    > character in the order list
    > return cmp( order.index( a[1] ), order.index( b[1] ) )
    > #otherwise
    > else :
    > #return the result of comparing each elements number
    > return cmp( a[0], b[0] )
    >
    > cards.sort( mycmp )
    > #print cards


    Maybe it's easier to use a key function instead of a compare function. A
    key function receives an element and must return something that is then
    sorted and the element ends up where the computed key is in the sorted
    list. Little example for sorting a list of strings first by length and
    strings of the same length by alphabetical order:

    def key_func(item):
    return (len(item), item)

    data = ['viking', 'spam', 'parrot', 'ham', 'eric']
    data.sort(key=key_func)
    print data

    Ciao,
    Marc 'BlackJack' Rintsch
     
    Marc 'BlackJack' Rintsch, May 27, 2007
    #3
  4. mark

    Kay Schluehr Guest

    On May 27, 12:19 pm, mark <> wrote:
    > Hi all
    >
    > I posted earlier on this but have changed my approach so here is my
    > latest attempt at solving a problem. I have been working on this for
    > around 12 hours straight and am still struggling with it.
    >
    > Write a program that reads the values for a random list of cards from
    > a file, where each line in the file specifies a single card with the
    > rank and then the suit separated by a space. The rank should be an
    > integer in the range of 1 to 13 (Ace:1, King:13), while the suit
    > should be a lower case character in the set { 'h', 'd', 'c', 's' }.
    > Sort the card entries into suits ordered by rank, and print out the
    > ordered list. Hint: sort the list first by rank, and then by suit.
    >
    > The format of the cards.txt file is;
    >
    > 1 h
    > 1 d
    > 13 c
    > 10 s
    >
    > and so on for the whole deck.
    >
    > Can someone help me to get the mycomp function to work.
    >
    > Any help appreciated
    >
    > J
    >
    > def read_cards(filename):
    >
    > cards = []
    > for card in open(filename, 'r'):
    > # strip the trailing newline character
    > cards.append(card.strip())
    > return cards
    >
    > filename = 'cards.txt'
    > cards = read_cards(filename)
    >
    > def cards_str2tup(cards):
    >
    > cards_tup = []
    > for card in cards:
    > rank, suit = card.split()
    > cards_tup.append((suit, int(rank)))
    > return cards_tup
    >
    > def cards_tup2str(cards_tup):
    >
    > cards = []
    > space = ' '
    > for tup in cards_tup:
    > suit, rank = tup
    > s = str(rank) + space + suit
    > cards.append(s)
    > return cards
    >
    > def mycmp( a, b):
    > #define the order in which the characters are to be sorted
    > order = [ 'h', 'd', 'c', 's' ]
    > # if the characters from each element ARENT the same
    > if a[1] <> b[1]:
    > #return the result of comparing the index of each elements
    > character in the order list
    > return cmp( order.index( a[1] ), order.index( b[1] ) )
    > #otherwise
    > else :
    > #return the result of comparing each elements number
    > return cmp( a[0], b[0] )
    >
    > cards.sort( mycmp )
    > #print cards


    You need to exploit the lexicographic order as in the following
    function.

    def sort_carddeck(card_deck_pairs):
    # card deck pairs have to be a list of
    # the form [(rank1, suit1), (rank1, suit2),...]
    suit_order = [ 'h', 'd', 'c', 's' ]

    def cmp(p1, p2):
    i1 = suit_order.index(p1[1])
    i2 = suit_order.index(p2[1])
    if i1<i2:
    return -1
    elif i1 == i2:
    if int(p1[0])<=int(p2[0]):
    return -1
    else:
    return 1
    else:
    return 1
    return sorted(card_deck_pairs, cmp)
     
    Kay Schluehr, May 27, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve
    Replies:
    10
    Views:
    580
    Steve
    Jan 13, 2004
  2. karthik kumar

    stl errors - totally lost

    karthik kumar, Dec 3, 2004, in forum: C++
    Replies:
    8
    Views:
    695
    Karl Heinz Buchegger
    Dec 3, 2004
  3. guardsman85
    Replies:
    2
    Views:
    578
    guardsman85
    Aug 29, 2006
  4. Steve Howell

    Re: totally lost newbie

    Steve Howell, May 27, 2007, in forum: Python
    Replies:
    1
    Views:
    519
    Marc 'BlackJack' Rintsch
    May 27, 2007
  5. Steve Howell

    Re: totally lost newbie

    Steve Howell, May 27, 2007, in forum: Python
    Replies:
    0
    Views:
    488
    Steve Howell
    May 27, 2007
Loading...

Share This Page