Calculating probability when events occur at some fixed pattern in a week


Each day, Monday through Friday, a batch of components sent by a first supplier arrives at a certain inspection facility. Two days a week, a batch also arrives from second supplier.

This is an excerpt from a problem in “Probability and Statistics for Engineering and the Sciences” by J.L. Devore.

How do I find the probability that two batches arrive in a randomly selected day? How about one batch? Or no batch? In the textbook, it’s given that the probability of one batch arriving is 0.6 and two batch arriving is 0.6. But together it means that at least one batch arrives in a randomly selected day.

However, it can be seen that there is a probability that no batches arrive in a day. For example, in a particular Sunday, the first supplier (who only sends a batch monday through friday) will not a send. The second supplier, who sends them two days a week, may choose not to send it on a Sunday.

Here’s the full question and the solution:

Each day, Monday through Friday, a batch of components sent by a first supplierarrives at a certain inspection facility. Two days a week, a batch also arrives froma second supplier. Eighty percent of all supplier 1’s batches pass inspection, and90% of supplier 2’s do likewise. What is the probability that, on a randomlyselected day, two batches pass inspection? We will answer this assuming thaton days when two batches are tested, whether the first batch passes is independ-ent of whether the second batch does so.

enter image description here

Design pattern for wrapping multiple apis into single api

I have 3 different libraries that provide depth data as well as gesture information. I need to wrap these libraries into a single simpler to use for user api. The apis I am trying to wrap provide a set of utilities as well as information that are not equal. The core data such as depth images or color images are provided but tracking can have different method or amount of information provided may differ. For example, one api provides a list of hands being detected and each hand has internally an orientation info(l/r). Another api provides l/r hand objects with NULL if not found and info if found.

I need to wrap these apis into a single api in the same language(c++) that allows user to have to write way less code to get the core functionalities. Is there a pattern that provides best-practices for such a task? Or any examples in industry that do something similar? I understand that single tight-knit apis are better than a large api, but for my current use-case, a large api makes sense, so I need to do that.

C++ observer pattern via boost.signals2

I was looking for a thread safe implementation of the observer pattern. I read https://xinhuang.github.io/posts/2015-02-11-how-a-multi-threaded-implementation-of-the-observer-pattern-can-fail.html and was thinking about point 4 “Don’t reinvent the wheel, use Boost.Signals2 instead”. So I tried the following code and wanted to ask if it is safe to be used in a multithreaded application? What happens if an Observer is destructed during notifyObservers?

#include <boost/signals2.hpp> #include <iostream> #include <string>  class AbstractObserver { public:     using SignalType = boost::signals2::signal<void()>;      virtual ~AbstractObserver() = default;      virtual void notify() = 0;      void registerAtSubject(SignalType &sig)     {         connection_ = sig.connect([this]() { notify(); });     }  private:     boost::signals2::scoped_connection connection_; };  class Subject {     AbstractObserver::SignalType sig_; public:     void registerObserver(AbstractObserver &observer)     {         observer.registerAtSubject(sig_);     }      void notifyObservers() const     {         sig_();     } };  class Observer : public AbstractObserver {     std::string id_; public:     explicit Observer(std::string id) : id_(std::move(id))     {};      void notify() override     {         std::cout << "Observer " << id_ << " got notified" << std::endl;     } };  int main() {     Subject c;     {         Observer o2("B");         {             Observer o1("A");             c.registerObserver(o1);             c.notifyObservers();             c.registerObserver(o2);             c.notifyObservers();         }         c.notifyObservers();     }     c.notifyObservers(); } 

Design pattern and/or data structure for cache friendly memory allocation?

I am a bit new to this site. Feel free to edit or move me to where I or question would fit better.

I am a applied mathematician / computational engineer searching for design patterns and data structures exist for cache-friendly memory allocation (for fast numerical floating point algorithms)?

For example I have N activities for which I want to group data to be local in memory when allocated as I know that when activity # k is executed it will greatly benefit if all of the data it uses is within one cache window.

Use Repository Pattern in WP theme

In my template files I have a lot of WP_Query to get posts of an author or from a specif category ecc… I’m thinking to use Repository Patter in order to have cleaner code and a layer between database and theme.

This PostRepository class will have some methods like: – getByID – getByAuthor – getByCategory

Could be a good idea? Have somebody use somethink like this?

AST Visitor Pattern in Java

I am trying to make a simple expression evaluator in Java using Jacc (a parser generator). I am done with the AST creation and now need to create an evaluator. For this I made a simple test to try to understand how visitors work. Here is my code

interface Visitor {     public void visitB(B b);     public void visitC(C c);     public void visitD(D d); }  class PrintVisitor implements Visitor {     public void visitB(B b)     {         b.child1.accept(this);         b.child2.accept(this);         System.out.println("a B visited: " + b.name);     }      public void visitC(C c)     {         c.child.accept(this);         System.out.println("a C visited: " + c.name);     }      public void visitD(D d)     {         System.out.println("a D visited: " + d.name);     } }  abstract class A {     public String name;     abstract void accept(Visitor v); }  class B extends A {     public A child1;     public A child2;      public B(String name)     {         this.name = name;     }      @Override     void accept(Visitor v)     {         v.visitB(this);     } }  class C extends A {     public A child;      public C(String name)     {         this.name = name;     }      @Override     void accept(Visitor v)     {         v.visitC(this);     } }  class D extends A {     public D(String name)     {         this.name = name;     }      @Override     void accept(Visitor v)     {         v.visitD(this);     } }  class Main {     public static void main(String[] args)     {         B b1 = new B("b1");         B b2 = new B("b2");          C c1 = new C("c1");         C c2 = new C("c2");          D d1 = new D("d1");         D d2 = new D("d2");         D d3 = new D("d3");         D d4 = new D("d4");          /*             b1           /   \         c1    c2       /         \     b2           d2    /  \  d3   d4          */          b1.child1 = c1;         b1.child2 = c2;          c1.child = b2;         c2.child = d2;          b2.child1 = d3;         b2.child2 = d4;          PrintVisitor v = new PrintVisitor();         b1.accept(v);     } } 

I would appreciate if you guys commented on my understanding of visitors.

Excel custom table array pattern and increments

I’m having trouble finding a solution that allows me to copy the same formula but increase the table array rows by increments of 15. In my first attempt I tried simply by manually creating three appropriate formulas followed by the fill hand in hopes that it would catch the pattern but I had no luck.

I’m looking for an output like this:

=MAX(VLOOKUP($ A2,Sheet2!$ A2:$ B17,2,0),VLOOKUP($ A2,Sheet2!$ E2:$ F17,2,0),VLOOKUP($ A2,Sheet2!$ I2:$ J17,2,0)) =MAX(VLOOKUP($ A2,Sheet2!$ A17:$ B32,2,0),VLOOKUP($ A2,Sheet2!$ E17:$ F32,2,0),VLOOKUP($ A2,Sheet2!$ I17:$ J32,2,0)) =MAX(VLOOKUP($ A2,Sheet2!$ A32:$ B47,2,0),VLOOKUP($ A2,Sheet2!$ E32:$ F47,2,0),VLOOKUP($ A2,Sheet2!$ I32:$ J47,2,0))

I’m pretty amateur so any advise at all is welcomed. Thank you in advance.

Safe Swift Lazy Observe and Mutate Pattern for Arrays?

I am using the following pattern in my code. It works. But, it feels as though it is on the edge of what should work!

Should I consider an alternative?

I have an array in my class that maps an underlying data array. This is the sort of thing for which I could use a lazy sequence. However, I don’t want to re-evaluate every time an element is accessed. So, I lazily load my array, then track the data array with KVO. I also want my top-level array to be observable. I mutate that with a mutableArrayValue.

I have mocked up a simple working example of the pattern below.

mutableArrayValue would usually cause a lazy var to initialise, but it seems OK to call it during an initialisation.

Setting up an observation on a lazy var does not cause it to initialise. I don’t get a call here even though the effective setting of the array comes after the observation is created.

import Cocoa  class LazyData: NSObject {     @objc dynamic lazy var units: [Int] = {         print("Loading units")         return [1,2,3,4,5,6] }() }  class Lazy: NSObject {     let data: LazyData      init(with data: LazyData) {         self.data = data     }      var unitsObservation: NSKeyValueObservation?      func tenTimes(_ input: Int) -> Int { return 10 * input }      @objc dynamic lazy var tens: [Int] = {         print("Loading tens")          let tenProxy = mutableArrayValue(forKeyPath: #keyPath(Lazy.tens))          unitsObservation = data.observe(\.units) { object, change in             if change.kind == .insertion {                 for insertionIndex in change.indexes! {                     let ten = self.tenTimes(object.units[insertionIndex])                     tenProxy.insert(ten, at: insertionIndex)                 }             }         }          return data.units.map { tenTimes($  0) }     }() }  var lazyData = LazyData() var lazy = Lazy(with: lazyData)  print(lazy.tens)  let lazyDataProxy = lazyData.mutableArrayValue(forKeyPath: #keyPath(LazyData.units)) lazyDataProxy.insert(9, at: 0)  print(lazy.tens) 

What is the name of the pattern where the framework owns your event/state object and you don’t keep a pointer to it?

There’s a pattern sometime used that I’d like to know the name for. It’s when you have a domain-specific state object that keeps track of progress through some kind of multi-step process which is run by some framework or engine – but you yourself never keep a copy of or pointer to that state. In fact, you just create it, pass it to your framework, and then every once in awhile you get called back on it (or with it as a param), you modify it and return to the framework, and so on, but you never hold that object.

In C++ it can look funky when you get called back and decide your chain-of-processes is through and you just delete this and return, which normally is pretty wierd but in this case it’s just business.

In C++ it can look really funky because sometimes you do a “naked new” where you don’t even store your object into any kind of local/collection/whatever. Instead, just the act of creating your object and passing into it a parameter that represents the framework causes the object to “register” itself with the framework, passing ownership, and it looks like the new object is just lost, and so you get code that looks like this:

new FooProcess(&frameworkContext, param1, param2); while (true) { ... _event loop here_ ... } 

gRPC uses this pattern as seen in the official tutorial for asynchronous processing – search for occurrences of new CallData. And you can also look for delete this in there.

So, what is this pattern called?

(Bonus question: Once you’ve named the thing for me – thank you! – discuss for extra credit whether it’s a good idea or not – especially w.r.t. initially debugging your code and then with maintaining it when it fails under load or other resource pressure… The reason I’m asking this question is because I’d like to read what other people think of it but can’t figure out how to search for it.)