« first day (945 days earlier)      last day (3071 days later) » 

11:06 PM
0
Q: Finding all divisors of an integer

blackenedimport functools import itertools import operator def prime_generator(n): """ Sieve of Eratosthenes Create a candidate list within which non-primes will be marked as None. """ cand = [i for i in range(3, n + 1, 2)] end = int(n ** 0.5) // 2 # Loop over candid...

 
@JeroenVannevel Can you help me with a best practices problem?
Suppose I have this snippet:
do
{
    Console.WriteLine("Purchase how many of {0} for ${1:0.00} each?", product.Key, product.Value);

    int quantity;
    var isValidQuantity = int.TryParse(Console.ReadLine(), out quantity);

    if (!isValidQuantity || quantity < 0)
    {
        Console.WriteLine("Invalid number input.");
        continue;
    }

    if (quantity > 0)
    {
        order.Add(product.Key, quantity);
    }
} while (false);
I don't like that while (false).
 
-1
Q: why destructor created a EXC_BAD_ACCESS on graph implemented using adjacency list

Kesong XieI was trying to implement a graph using adjacency list, and the destructor seems generated an error main.cpp #include <string> #include "Graph.cpp" using namespace std; int main(int argc, const char * argv[]) { Graph<char> g(10); g.addVertex('a'); //position 0 g.addVer...

 
But, if I change it to while (true), I have to stick in a bunch of breaks.
Is there a better way of doing this?
If I define isValidQuantity above the loop, then I have to re-check the condition already checked in the first if.
 
use while(true) with breaks
 
OK.
 
11:17 PM
while(false) with continue is much worse on the ugly scale
breaks are perfectly fine
 
Like:
do
{
    Console.WriteLine("Purchase how many of {0} for ${1:0.00} each?", product.Key, product.Value);

    int quantity;
    var isValidQuantity = int.TryParse(Console.ReadLine(), out quantity);

    if (!isValidQuantity || quantity < 0)
    {
        Console.WriteLine("Invalid number input.");
        continue;
    }

    if (quantity > 0)
    {
        order.Add(product.Key, quantity);
    }
    break;

} while (true);
I don't like how the break is the last statement in the loop.
That answer is several pages long. It could probably be split (and get me double the rep), but I don't think I'll bother.
 
why not just a while(true)?
let me take a closer look and rewrite
 
It still needs the break.
 
oh
you're looping too much
 
It essentially is a while (true), but it started with an unnecessary flag condition.
So, I turned it into a do-while, then removed the flag.
 
11:26 PM
Just put the order.Add outside the loop
 
Oh, duh.
 
@Hosch250 The logic doesn't make sense. What happens when quantity is 0.
 
@Brandin Nothing.
Essentially, the program asks whether we want to order something of each thing on the menu.
 
Why not? It doesn't makes sense. Adding a product of quantity 0 should work fine
 
0 means we don't want to order, and we don't need to add it to the order list.
 
11:30 PM
But if I said I wanted to order a quantity of 0, why not add it to the list? It should not harm anything.
The customer is always right. He said he wanted 0, so 0 he shall receive
 
We could just add it, but would increase the size of the list, making it less manageable.
For example, the dictionary could grow very large, taking more time to return from the function.
Possibly a premature optimization, but I learned to avoid passing lists and stuff between functions in C++.
 
Impossible. A dictionary is built to give efficient lookup
 
Doesn't mean it can't take a lot of memory, does it?
while (true)
{
    Console.WriteLine("Purchase how many of {0} for ${1:0.00} each?", product.Key, product.Value);

    int quantity;
    if (int.TryParse(Console.ReadLine(), out quantity) && quantity >= 0)
    {
        if (quantity > 0) { order.Add(product.Key, quantity); }
        break;
    }

    Console.WriteLine("Invalid number input.");
}
How's that?
Instead of checking for the error first, I checked for success and had the error be the default.
Hey there, @Caridorc.
 
@Hosch250 Hi
Some competition on:
4
Q: "Welcome to Buzzway Subs!"

PhrancisThere were a few recent questions which had a challenge like this: The subway shop provides catering for meetings and other events. All sandwich platters are $40 each. Cookie platters are $20 each. Beverages are not included in catering service. Write a program that prompts the user fo...

 
Yup.
Friendly competition, though.
 
11:38 PM
@Hosch250 Of course :)
 
I hope I didn't scare @Phrancis with my several-page long answer.
I think it is the longest one I've ever written.
 
@Hosch250 I just read all your answer, it is good: +1
 
Same to you.
We'll probably make it go hot, and have 15 votes apiece in the morning.
 
add string interpolation to your answers
we're not cavemen
 
@Hosch250 Maybe, just 41 views
 
11:42 PM
@JeroenVannevel He's using .NET 4.5.
But sure.
 
I don't see that specified anywhere
 
@JeroenVannevel well, the idea is yours, write your own answer. Answers need not be big
 
It was in the chat room yesterday.
Yeah, write your own answer.
 
I'm not motivated enough to do so
 
You can probably catch things we didn't.
Well, if you do, just tell me and I'll remove this section.
 
11:43 PM
and add expression-bodied members as well
I won't for sure
I'm going back to Modern Family in 5 minutes
 
@Caridorc Do you want to take one of these?
 
also this, lol:
change = payment + -total;
 
LOL.
 
@Hosch250 "these" standing for?
@JeroenVannevel ninja fixed in my answer
 
One of Jeroen's suggestions.
 
11:45 PM
new StringBuilder(""); // pointless initialization, minor mention
 
@JeroenVannevel Already did.
 
ah, missed that
 
The + - was ninja fixed, I am adding a note now
@JeroenVannevel String interpolation I do not really know much about. In ruby you do "Price is #{price}", is it the same in C#?
 
I did that.
 
yeah
Console.WriteLine("${myvar}");
notice how the interpolation character is the dollar sign
so you will have to escape that
I'm not 100% sure what that was again
might just be a backslash
but escaping the braces is using double braces, for example
 
11:51 PM
An expression bodied member is like this:
public decimal CalculateSalesTax(Dictionary<string, int> order) =>
    CalculateSubTotal(order) * SalesTax;
 
oh no wait
I'm confusing with NodeJS
 
@Hosch250 when would you prefer it over a normal method?
 
in C# it's just Console.WriteLine($"{myVar}");
 
In C#, the $ goes before the "".
 
So no need to escape the dollar
 
11:52 PM
@Caridorc Don't know.
 
Maybe short bodies...,, when return would be almost half as long as the expression itself
 
Only works with one statement.
That replaces:
public decimal CalculateSalesTax(Dictionary<string, int> order)
{
    return CalculateSubTotal(order) * SalesTax;
}
I'm done. I have to finish a paper, and have supper in a little bit.
 
Languages where half of the lines are lone braces... So much vertical spacing makes an overall view of the code impossible
 
The vertical spacing separates the logic from the definitions.
I would always put a line below the definition anyway, in Java.
 
@Hosch250, that is true, it is all about the compromises one is ok to make
 
11:59 PM
@Hosch250 one expression
not one statement
 
Oh, OK.
 

« first day (945 days earlier)      last day (3071 days later) »