Hi,
I'm having difficulties doing a problem in my text book.
The q is---
'Write a program that creates a list of card objects and prints out
the cards grouped by suit and in rank order within each suit. The
program should read the values for the 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. Hint. sort the list by rank, and
then by suit.
I don't really understand what the q wants???
Any help will be paid well for... |
Request for Question Clarification by
answerguru-ga
on
16 Oct 2006 16:48 PDT
Hi macca1111-ga,
Although I cannot write the code for you (it is against Google
Answer's policy to answer homework questions). I can clarify what is
be asked and provide an approach to solving the problem.
Would this suffice as an answer? Please let me know.
Thanks,
answerguru-ga
|
Request for Question Clarification by
denco-ga
on
16 Oct 2006 17:04 PDT
Howdy macca1111-ga,
I don't how much you want towards the answer, so I am posting this as a request
for clarification, as I presume you are looking for hints on how to accomplish
your task.
The questions is speaking to a deck of cards. A deck of cards has 52 cards.
The following article has some suggested nomenclature.
Java - Class Card
http://nifty.stanford.edu/2004/EstellCardGame/api/Card.html
The characters used for R [Rank] are:
'a' (ace), '2', '3', '4', '5', '6', '7', '8', '9', 't' (10), 'j' (jack), 'q'
(queen), and 'k' (king).
The characters used for S [Suit] are:
'c' (clubs), 'd' (diamonds), 'h' (hearts), and 's' (spades).
The question specifies a file where each line is a single card.
"...
a c
9 s
3 d
t h
..."
The above would represent:
"...
Ace Clubs
9 Spades
3 Diamonds
10 Hearts
..."
The file would include all 52 cards, Ace through King of each suit; clubs,
diamonds, hearts and spades.
The program would create a list from that file, such as:
"...
a c
2 c
3 c
...
4 d
5 d
6 d
...
7 h
8 h
9 h
...
t s
j s
q s
k s
..."
The question might want the rank and suit spelled out.
"...
10 Spades
Ace Clubs
3 Hearts
King Diamonds
..."
The end sort would be the same, except spelled out.
Here are some pages that might help.
Python - Sets of objects
http://www.ibiblio.org/obp/thinkCS/python/english/chap15.htm
Python - Cards, decks and games
http://www.ibiblio.org/obp/thinkCS/python/english/app03.htm
Python - Searching and Sorting
http://aspn.activestate.com/ASPN/Cookbook/Python?kwd=Searching
Is the above what you are needing? Thanks!
Looking Forward, denco-ga - Google Answers Researcher
|
Clarification of Question by
macca1111-ga
on
16 Oct 2006 19:57 PDT
Tks for your help so far.
What is suppose to be in the file???
Am I suppose to load something in a file first???
Tks
|
Request for Question Clarification by
denco-ga
on
16 Oct 2006 21:52 PDT
Howdy macca1111-ga,
The questions states "the program should read the values for the 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."
That file has to come from someplace, so unless that file is provided for you,
you would have to generate that file, either with a program or by hand.
The file format is not very detailed in the question, so I would think a format
of either or these would be acceptable.
2 h
t s
a d
q c
...
or:
2 hearts
10 spades
ace diamonds
queen clubs
...
Either format, one would end up with a 52 line file, with one line per card.
Looking Forward, denco-ga - Google Answers Researcher
|
Clarification of Question by
macca1111-ga
on
16 Oct 2006 22:24 PDT
I have this from cutting and pasting from several different bits of code.
I does sort of the job. Would it be possible to have the suitList and
rankList in a file titled 'cards.txt' and then have the script load
the values?
class Card:
suitList = open('suits.txt','r')
rankList = open('cards.txt','r')
def __init__(self,suit,rank):
self.suit = suit
self.rank = rank
def __repr__(self):
return str(self)
def __str__(self):
return "%s of %s" % (self.rankList[self.rank],self.suitList[self.suit])
def __cmp__(self,other):
if self.suit > other.suit: return 1
if self.suit < other.suit: return -1
if self.rank > other.rank: return 1
if self.rank < other.rank: return -1
return 0
class Deck:
def __init__(self):
self.cards = []
for suit in range(4):
for rank in range(1,14):
self.cards.append(Card(suit,rank))
def __str__(self):
return '\n'.join(str(card) for card in self.cards)
def main():
a=Deck()
print a
|
Clarification of Question by
macca1111-ga
on
16 Oct 2006 22:41 PDT
I changed the Class as below
suitList = open('suits.txt','r')
suitList = suitList.read()
rankList = open('cards.txt,'r')
rankList = rankList.read()
yet now it only shows:
2 of C
3 of C
4 of C
5 of C
6 of C
7 of C
8 of C
9 of C
1 of C
0 of C
J of C
Q of C
K of C
As you can see the 10 is split between 2 lines.
The data in the txt file is CDHS as is kept only getting the first
character if I had them in quotes. I would also like to make it
display the full suit, not just C or H but Clubs or Hearts.
I think I'm getting close, but not quite.
|
Request for Question Clarification by
denco-ga
on
17 Oct 2006 22:45 PDT
Some tips for you, macca1111-ga.
- Use the letter "t" for the number "10"
- Perhaps you could take your sorted output list and substitute the word "Ace"
for the "A's" and the number "10" for the "T's" and Jack for the "J's"
- Maybe you could take your sorted output list and substitute the word "Clubs"
for "C's" and Hearts for the "H's"
The input list file should be "randomly" distributed to begin with so as to
show that a real sort was be processed by the program. You could do this by
taking a real deck of cards, doing a good shuffle, then dealing the cards out
one, by one. Transcribe this deal to make your input card list file.
|
Clarification of Question by
macca1111-ga
on
18 Oct 2006 02:33 PDT
I appreciate your comments denco-ga, and I have used the T instead of
10, yet your other comments really mean nothing to me.
If I create a file:
Ace of Spades
2 of Spades
how do I get the program to read the data and then use the Class with it.
According to the class, it needs a separate suit and rank input.
|
Request for Question Clarification by
denco-ga
on
18 Oct 2006 17:57 PDT
Howdy macca1111-ga,
First, the file you create, according to the instructions, should be along
the format of the following.
=== start file ===
2 s
8 c
a s
q h
t d
5 h
j c
3 h
k s
a c
...
=== end of file ===
The "..." in the file example above would be the rest of the deck of cards
in some "random" fashion.
Second, you will, again according to the instructions, do a two pass sort
on the file, the first pass, they hint, should be by rank, so after this
first pass, you will have a file with following format.
=== start file ===
a s
a c
a d
a h
2 c
2 s
2 h
2 d
3 h
3 s
3 c
3 d
...
=== end of file ===
The second pass would be a sort by suit, producing the following.
=== start file ===
a c
2 c
3 c
4 c
...
t c
j c
q c
k c
a d
2 d
...
a h
2 h
...
a s
2 s
...
q s
k s
=== end of file ===
You could then, if desired, replace "q's" with "Queen," etc.
Looking Forward, denco-ga - Google Answers Researcher
|
Clarification of Question by
macca1111-ga
on
18 Oct 2006 20:58 PDT
If I have a file called cards.txt:
2 s
4 h
J h
7 d
...
how would I import this file into the program and separate the suits
from the ranks?
I was thinking that in the Card Class I could put something like:
class Card:
suitList = ()
rankList = ()
Then in the main(): I could import the file and somehow populate the variables.
Somehow I need to separate (in the file) the ranks from the suits and
then get them into the suitList and rankList.
Am I on the right track???
|
Request for Question Clarification by
denco-ga
on
19 Oct 2006 02:41 PDT
Might I suggest you go back and relook at the links I first provided?
Python - Sets of objects
http://www.ibiblio.org/obp/thinkCS/python/english/chap15.htm
Python - Cards, decks and games
http://www.ibiblio.org/obp/thinkCS/python/english/app03.htm
Python - Searching and Sorting
http://aspn.activestate.com/ASPN/Cookbook/Python?kwd=Searching
|
Clarification of Question by
macca1111-ga
on
19 Oct 2006 04:13 PDT
I appreciate your response, however I think I'm going around in circles.
I have read and re-read the links, yet I can't seem to find anything
that gives me any ideas on opening a file, then loading the contents
(see below) and them change the H to Hearts and print this out.
Even if I don't sort it as it's already sorted, I can't seem to find
how to open a file;
2 H
4 S
...
then have this data loaded into the program.
Am I at least on the right track with the following???
class Card:
suitList = open('suits.txt','r')
rankList = open('cards.txt','r')
Kind regards
macca111
|
Request for Question Clarification by
denco-ga
on
19 Oct 2006 10:31 PDT
Here are a few more hints.
"easy file parsing"
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496910
"Sort a file"
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/440612
|
Clarification of Question by
macca1111-ga
on
19 Oct 2006 17:47 PDT
Hi,
I think this is pretty close. It doesn't work properly, yet all your
patience may be paying off...
The file I have the cards in looks like..
2 h
3 h
4 h
5 h
6 h
7 h
8 h
9 h
10 h
jack h
queen h
king h
ace h
I saved it cards.txt
Python Program
import string
class Card:
# Not sure if I need to set these first???
# suitList = ()
# rankList = ()
def __init__(self,suit,rank):
self.suit = suit
self.rank = rank
def __repr__(self):
return str(self)
def __str__(self):
return "%s of %s" % (self.rankList[self.rank],self.suitList[self.suit])
def __cmp__(self,other):
if self.suit > other.suit: return 1
if self.suit < other.suit: return -1
if self.rank > other.rank: return 1
if self.rank < other.rank: return -1
return 0
def getCard(cards):
# Break each card into suit and rank
rankList , suitList = string.split(cards," ")
return Card(rankList,suitList)
class Deck:
def __init__(self):
self.cards = []
for suit in range(4):
for rank in range(1,14):
self.cards.append(Card(suit,rank))
def main():
# Open the file with the cards in them
filename = raw_input('Enter the file name for the cards >>')
infile = open(filename,'r')
# Set the first card in the list
cards = getCard(infile.readline())
# Process the extra lines
for line in infile:
s = getCard(line)
infile.close()
a=Deck()
print a
main()
|
Request for Question Clarification by
denco-ga
on
20 Oct 2006 00:00 PDT
The question clearly asks for the "cards.txt" file to be in the following
style of format, and should not be presorted.
=== start file ===
2 s
8 c
a s
q h
t d
5 h
j c
3 h
k s
a c
...
=== end of file ===
|
Clarification of Question by
macca1111-ga
on
23 Oct 2006 04:18 PDT
denco-ga,
Thks for all your help.
I've tried too many ways to get this working. I just can't find
anything that shows me how to get the file from
2 h
4 s
A d
...
into the Card class rankList and suitList and sorted. I appreciate
your help with links etc, yet I must be missing something. I've
wasted yours and my time but I do appreciate your help.
Kind Regards
macca1111
|