Here's how I see this playing out:
You can either tell them, or not. Those are your choices.
If you tell company B about company A's offer, company B may accelerate their current hiring process, things such as cancelling the current take-home assignment and doing an abbreviated test instead. However, company B may also say that they cannot accommodate the other company's offer and will end your process immediately. So it depends on how much you want to work for each company; if you're willing to potentially sacrifice your candidacy at company B, then you should inform them about company A's offer.
If you do not tell company B about company A's offer, then they will continue their interview process with you as if there is no other offer (because, to their knowledge, there isn't one). However, company A will likely not wait until company B is done their recruitment to have you sign the offer, so at some point you'll have to "put up or shut up", and it will be unlikely that you can have both company B and company A's offers both in hand at the same time. If you sign company A's offer without notifying company B, then company B may be upset with you, to the point of possibly burning your bridge there. The other option here is to lie to company B about why you're not continuing their interview process, if you're comfortable with that, once you've signed A's offer. So once again, it depends on how much you care about company B, whether or not to notify them about A's offer.
All this is to say, the situation is really too unpredictable, without knowing the personalities of the people involved or companies involved. Here's what I would consider doing (YMMV):
It all depends on how long this take-home assignment from company B is. If it is "long" (4 hours work or more), I would tell company B about company A's offer right now. If the assignment is going to take a long time, it will also take them along time to review, and feet are going to be dragged. You're simply not going to get anywhere with company B before company A's offer expires. Inform B that you have an offer (never say who the offer is from), and ask them how they would like to proceed; if they end your recruitment, then so be it, and if not, then do whatever they ask (within reason, of course).
If the assignment is "short" (4 hours or less), then finish it as quickly as possible and return it ASAP. When you return the assignment, tell B that you have an offer already, and you need them to move quickly. When a company is told you have an offer in hand, they will do one of two things: They will either end the recruitment immediately (and you've lost your 4 hours work; this is why I'm only suggesting this if the assignment is short so you don't waste too much time), or they will speed up significantly to make sure you have both offers in hand and can consider both. You're obviously hoping for the latter, but the former is also possible, and if that happens then just go with the offer from company A.