New answers tagged

2

You seem to have some extra code in your function that is not needed. The code below does the same thing as your function: const sortStack = (stack) => { sorted = new Stack(); while (!stack.isEmpty()) { tmp = stack.pop(); while (tmp < sorted.peek()) { stack.push(sorted.pop()); } sorted.push(tmp); } return sorted; } The ...


0

Problems First of all, your current implementation is not truly in-place, since the slice operation creates a new list of that size, which breaches the required max constant extra space usage. In addition you perform double work in the index_of_smallest_element when you first find the value you want, and then have to run through the entire list again to ...


4

First off, this is quite good for beginner code. You'll do well if you keep the good habits you've started following. A few critiques: class Card : IComparable<Card> Do you intend this class to be subclassed? If not, seal it. Are cards comparable by reference? That is, suppose we have two different instances of the queen of spades. Is it possible ...


6

Just a few things not already mentioned by other answers: Are you sure that you will need DrawRandomCard() ? How many card games have you played where you just draw from a random position in the deck? The only times I can think of this is useful is when you do a magic trick, which doesn't really work the same in code as in real life :) Normally in card ...


5

Encapsulation and Mutability Your Card class is a perfect candidate for being immutable: make Kind and Suit readonly fields or getter-only properties (prefer the later in most cases): public Kind Kind { get; } public Suit Suit { get; } You should also hide away the Deck list in CardDeck: making it readonly and private (or protected, if you are happy to ...


7

enum Kind { ... Queen, King, Joker // ??? } Jokers is the joker. You're not considering jokers class Card : IComparable<Card> { ... I'm not convinced that there is a default comparison for cards? It could be misunderstood by consumers. On the other hand a deck of cards is always sorted by Suit and then Kind, but the ...


5

List<T> You have just learned about List<T> and are eager to use it. Perfectly understandable, but think about what it is designed for and what alternatives are available for this use case. Here is an overview of common Collections in C# To be fair, there is no common collection available designed for a deck of cards. What you need, is ...


1

There is a big problem in your code. sorted sorts the input list. So you are sorting input list twice. For finding out that your list is sorted, you only need to check did you make any swap in the last iteration or not. If you don't, your list is sorted. def get_input(): input_str = input("Enter elements to be sorted: ") try: lst = list(map(...


3

Regarding the code itself I think functions should usually return an output and then this should be printed. It is also worth introducing some way of alerting the user if there input causes an error. I entered 5, -3, 0 and this raised an error because your code splits on spaces not commas. Additionally, you are mapping the list to int so entering a character ...


5

Separate code and test. It's great that you've written a unit test, but the test code should be in a separate class (and typically a separate project) from the code under test. The testing could also be a bit more exhaustive. Given the same input you can test each value of n: it'll still run in milliseconds. A separate test could handle repeated elements. A ...


4

You are using an iterative approach which is normally faster than a recursive. That's a good optimization. if (array[i].CompareTo(pivotValue) > 0) { continue; } Swap(ref array[i], ref array[startIndex]); startIndex++; Why not just: if (array[i].CompareTo(pivotValue) < ...


Top 50 recent answers are included