Sorting cards by suit and value using Shell Sort











up vote
1
down vote

favorite












The question description: put a deck of cards in order by suit (in the order spades, heart, clubs, diamonds) and by rank within each suit, with the restriction that the card must be laid out face down in a row, and the only allowed operations are to check the values of two cards and to exchange two cards (keeping them face down).



My solution:



from enum import Enum
import random


class Suit(Enum):
__order__ = "spade heart club diamond"
spade = 1
heart = 2
club = 3
diamond = 4


class Card(object):
def __init__(self, suit, value):
assert type(suit) == Suit
assert value > 0 and value < 14
self._suit = suit
self._value = value
self.value = self._get_value()

def _get_value(self):
return self._suit.value * 13 + self._value

def __lt__(self, other):
return self.value < other.value

def __str__(self):
return str((self._suit.name, self._value))

cards =

for s in Suit:
for i in range(13):
cards.append(Card(s, i+1))


random.shuffle(cards)


def shell_sort(cards):
h = 1
while(h<52/3):
h = 3*h + 1
# h = 30
# should be more than 29?
while(h>0):
for k in range(h, 52):
j = k
while(j >= h and cards[j] < cards[j-h]):
cards[j], cards[j-h] = cards[j-h], cards[j]
j -= h
h = int(h / 3)
return cards

sorted_cards = shell_sort(cards)

for c in sorted_cards:
# how to consider the suit, can the suit decrease the compare times?
print(c)


I think this can be further optimized since there should be no compares among different suits. Can that be integrated in the shell sort without firstly rank the suits (then the values inside)?










share|improve this question
























  • That is a silly constraint that forces you to use a sub-optimal method for sorting. Cards are so easy to sort, you only need to look at each once, and can immediately place it in its right location in the output.
    – Cris Luengo
    Dec 5 at 1:44










  • Note that you can assign suits the values 0, 13, 26 and 39, then each card has the value suit+value. Might make the sorting routine easier to write.
    – Cris Luengo
    Dec 5 at 1:46






  • 1




    @CrisLuengo You are right! But let's treat the algorithm exercise as an axercise.
    – lerner
    Dec 5 at 12:09















up vote
1
down vote

favorite












The question description: put a deck of cards in order by suit (in the order spades, heart, clubs, diamonds) and by rank within each suit, with the restriction that the card must be laid out face down in a row, and the only allowed operations are to check the values of two cards and to exchange two cards (keeping them face down).



My solution:



from enum import Enum
import random


class Suit(Enum):
__order__ = "spade heart club diamond"
spade = 1
heart = 2
club = 3
diamond = 4


class Card(object):
def __init__(self, suit, value):
assert type(suit) == Suit
assert value > 0 and value < 14
self._suit = suit
self._value = value
self.value = self._get_value()

def _get_value(self):
return self._suit.value * 13 + self._value

def __lt__(self, other):
return self.value < other.value

def __str__(self):
return str((self._suit.name, self._value))

cards =

for s in Suit:
for i in range(13):
cards.append(Card(s, i+1))


random.shuffle(cards)


def shell_sort(cards):
h = 1
while(h<52/3):
h = 3*h + 1
# h = 30
# should be more than 29?
while(h>0):
for k in range(h, 52):
j = k
while(j >= h and cards[j] < cards[j-h]):
cards[j], cards[j-h] = cards[j-h], cards[j]
j -= h
h = int(h / 3)
return cards

sorted_cards = shell_sort(cards)

for c in sorted_cards:
# how to consider the suit, can the suit decrease the compare times?
print(c)


I think this can be further optimized since there should be no compares among different suits. Can that be integrated in the shell sort without firstly rank the suits (then the values inside)?










share|improve this question
























  • That is a silly constraint that forces you to use a sub-optimal method for sorting. Cards are so easy to sort, you only need to look at each once, and can immediately place it in its right location in the output.
    – Cris Luengo
    Dec 5 at 1:44










  • Note that you can assign suits the values 0, 13, 26 and 39, then each card has the value suit+value. Might make the sorting routine easier to write.
    – Cris Luengo
    Dec 5 at 1:46






  • 1




    @CrisLuengo You are right! But let's treat the algorithm exercise as an axercise.
    – lerner
    Dec 5 at 12:09













up vote
1
down vote

favorite









up vote
1
down vote

favorite











The question description: put a deck of cards in order by suit (in the order spades, heart, clubs, diamonds) and by rank within each suit, with the restriction that the card must be laid out face down in a row, and the only allowed operations are to check the values of two cards and to exchange two cards (keeping them face down).



My solution:



from enum import Enum
import random


class Suit(Enum):
__order__ = "spade heart club diamond"
spade = 1
heart = 2
club = 3
diamond = 4


class Card(object):
def __init__(self, suit, value):
assert type(suit) == Suit
assert value > 0 and value < 14
self._suit = suit
self._value = value
self.value = self._get_value()

def _get_value(self):
return self._suit.value * 13 + self._value

def __lt__(self, other):
return self.value < other.value

def __str__(self):
return str((self._suit.name, self._value))

cards =

for s in Suit:
for i in range(13):
cards.append(Card(s, i+1))


random.shuffle(cards)


def shell_sort(cards):
h = 1
while(h<52/3):
h = 3*h + 1
# h = 30
# should be more than 29?
while(h>0):
for k in range(h, 52):
j = k
while(j >= h and cards[j] < cards[j-h]):
cards[j], cards[j-h] = cards[j-h], cards[j]
j -= h
h = int(h / 3)
return cards

sorted_cards = shell_sort(cards)

for c in sorted_cards:
# how to consider the suit, can the suit decrease the compare times?
print(c)


I think this can be further optimized since there should be no compares among different suits. Can that be integrated in the shell sort without firstly rank the suits (then the values inside)?










share|improve this question















The question description: put a deck of cards in order by suit (in the order spades, heart, clubs, diamonds) and by rank within each suit, with the restriction that the card must be laid out face down in a row, and the only allowed operations are to check the values of two cards and to exchange two cards (keeping them face down).



My solution:



from enum import Enum
import random


class Suit(Enum):
__order__ = "spade heart club diamond"
spade = 1
heart = 2
club = 3
diamond = 4


class Card(object):
def __init__(self, suit, value):
assert type(suit) == Suit
assert value > 0 and value < 14
self._suit = suit
self._value = value
self.value = self._get_value()

def _get_value(self):
return self._suit.value * 13 + self._value

def __lt__(self, other):
return self.value < other.value

def __str__(self):
return str((self._suit.name, self._value))

cards =

for s in Suit:
for i in range(13):
cards.append(Card(s, i+1))


random.shuffle(cards)


def shell_sort(cards):
h = 1
while(h<52/3):
h = 3*h + 1
# h = 30
# should be more than 29?
while(h>0):
for k in range(h, 52):
j = k
while(j >= h and cards[j] < cards[j-h]):
cards[j], cards[j-h] = cards[j-h], cards[j]
j -= h
h = int(h / 3)
return cards

sorted_cards = shell_sort(cards)

for c in sorted_cards:
# how to consider the suit, can the suit decrease the compare times?
print(c)


I think this can be further optimized since there should be no compares among different suits. Can that be integrated in the shell sort without firstly rank the suits (then the values inside)?







python algorithm python-3.x sorting playing-cards






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 5 at 0:09









200_success

128k15149412




128k15149412










asked Dec 5 at 0:04









lerner

1265




1265












  • That is a silly constraint that forces you to use a sub-optimal method for sorting. Cards are so easy to sort, you only need to look at each once, and can immediately place it in its right location in the output.
    – Cris Luengo
    Dec 5 at 1:44










  • Note that you can assign suits the values 0, 13, 26 and 39, then each card has the value suit+value. Might make the sorting routine easier to write.
    – Cris Luengo
    Dec 5 at 1:46






  • 1




    @CrisLuengo You are right! But let's treat the algorithm exercise as an axercise.
    – lerner
    Dec 5 at 12:09


















  • That is a silly constraint that forces you to use a sub-optimal method for sorting. Cards are so easy to sort, you only need to look at each once, and can immediately place it in its right location in the output.
    – Cris Luengo
    Dec 5 at 1:44










  • Note that you can assign suits the values 0, 13, 26 and 39, then each card has the value suit+value. Might make the sorting routine easier to write.
    – Cris Luengo
    Dec 5 at 1:46






  • 1




    @CrisLuengo You are right! But let's treat the algorithm exercise as an axercise.
    – lerner
    Dec 5 at 12:09
















That is a silly constraint that forces you to use a sub-optimal method for sorting. Cards are so easy to sort, you only need to look at each once, and can immediately place it in its right location in the output.
– Cris Luengo
Dec 5 at 1:44




That is a silly constraint that forces you to use a sub-optimal method for sorting. Cards are so easy to sort, you only need to look at each once, and can immediately place it in its right location in the output.
– Cris Luengo
Dec 5 at 1:44












Note that you can assign suits the values 0, 13, 26 and 39, then each card has the value suit+value. Might make the sorting routine easier to write.
– Cris Luengo
Dec 5 at 1:46




Note that you can assign suits the values 0, 13, 26 and 39, then each card has the value suit+value. Might make the sorting routine easier to write.
– Cris Luengo
Dec 5 at 1:46




1




1




@CrisLuengo You are right! But let's treat the algorithm exercise as an axercise.
– lerner
Dec 5 at 12:09




@CrisLuengo You are right! But let's treat the algorithm exercise as an axercise.
– lerner
Dec 5 at 12:09















active

oldest

votes











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209036%2fsorting-cards-by-suit-and-value-using-shell-sort%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Code Review Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209036%2fsorting-cards-by-suit-and-value-using-shell-sort%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Сан-Квентин

Алькесар

Josef Freinademetz