Iterate cartesian product (cross product) of sorted lists of positive numbers in order of decreasing product of entries (MVCE of a stub included)

Suppose I have several sorted lists of positive numbers, like so for example:

double[] a1 = new double[]{0.70, 0.20, 0.10}; double[] a2 = new double[]{0.80, 0.10, 0.05, 0.05}; double[] a3 = new double[]{0.60, 0.15, 0.14, 0.10, 0.01}; 

I want to iterate Cartesian product of these arrays in the order of decreasing product of entries, like so:

0000: Combo[product=3.36e-01, vals=[0.70, 0.80, 0.60], indexes=[0, 0, 0]] 0001: Combo[product=9.60e-02, vals=[0.20, 0.80, 0.60], indexes=[1, 0, 0]] 0002: Combo[product=8.40e-02, vals=[0.70, 0.80, 0.15], indexes=[0, 0, 1]] 0003: Combo[product=7.84e-02, vals=[0.70, 0.80, 0.14], indexes=[0, 0, 2]] 0004: Combo[product=5.60e-02, vals=[0.70, 0.80, 0.10], indexes=[0, 0, 3]] 0005: Combo[product=4.80e-02, vals=[0.10, 0.80, 0.60], indexes=[2, 0, 0]] ... 

E.g. in the example above the first entry is obvious (as arrays are sorted) and it is a combination of the first values: [0.70, 0.80, 0.60] with product 0.70*0.80*0.60 = 3.36e-01 and corresponding value indexes in arrays a1, a2, a3 are [0, 0, 0]. Now the second entry is less obvious, should we change 0.70 to 0.20? Or 0.60 to 0.15? Or 0.80 to 0.10? The second should be [0.20, 0.80, 0.60]with product 9.60e-02, indexes [1, 0, 0].

Here is a program in java to generate/print them: https://repl.it/repls/FilthyGreatRotation (all the logic is in printWholeCartesianProduct() method)
This program generates them in lexicographic order and then sorts the whole set by product.

Question: Is there an easy way to actually generate the combos in correct order in the first place?

The reason for this: I don’t have the lists in the first place, only iterators over some sorted collections of numbers. Possibly veeery long, length not known ahead of time, but it is known that the numbers in each iterator are sorted.

MVCE to play with (same as in https://repl.it link above):

import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.StringJoiner; import java.util.function.Consumer; import java.util.stream.Collectors;  public class Main {      public static void main(String[] args) {         List<List<Double>> data = createData();         printWholeCartesianProduct(data);     }      public static List<List<Double>> createData() {         double[] a1 = new double[]{0.70, 0.20, 0.10};         double[] a2 = new double[]{0.80, 0.10, 0.05, 0.05};         double[] a3 = new double[]{0.60, 0.15, 0.14, 0.10, 0.01};         return createData(a1, a2, a3);     }      public static void  printWholeCartesianProduct(List<List<Double>> data) {         final DecimalFormat df = new DecimalFormat("0.00");          // print input data         String matrix = data.stream()             .map(l -> l.stream().map(df::format).collect(Collectors.joining(", ")))             .map(row -> "[" + row + "]")             .collect(Collectors.joining("\n"));         System.out.println("Input data:\n" + matrix);          // collect combos as they are generated         final List<Combo> combos = new ArrayList<>();         Consumer<int[]> callback = indexes -> {             double[] v = new double[indexes.length];             double prod = 1;             for (int i = 0; i < indexes.length; i++) {                 List<Double> col = data.get(i);                 int index = indexes[i];                 v[i] = col.get(index);                 prod *= v[i];             }             combos.add(new Combo(prod, v, indexes.clone()));         };          // generate combos         int[] c = new int[data.size()];         int ptr = c.length - 1;         while (ptr >= 0) {             callback.accept(c);             c[ptr]++; // increment             if (c[ptr] == data.get(ptr).size()) { // carry                 do {                     ptr--;                 } while(ptr >= 0 && c[ptr] == data.get(ptr).size() - 1);                 if (ptr < 0) {                     break;                 }                 c[ptr]++;                 // zero out                 while (++ptr <= c.length - 1) {                     c[ptr] = 0;                 }                 ptr = c.length - 1;             }         }          // cheating - sort after generation and print result         combos.sort((o1, o2) -> Double.compare(o2.product, o1.product));         StringBuilder sb = new StringBuilder();         double totalP = 0;         for (int i = 0; i < combos.size(); i++) {             sb.append(String.format("%04d: ", i)).append(combos.get(i)).append("\n");             totalP += combos.get(i).product;         }         System.out.printf("Cartesian product in descending product (total p=%.3e):\n%s", totalP, sb.toString());     }      public static List<Double> asList(double[] a) {         return Arrays.stream(a).boxed().collect(Collectors.toList());     }      public static List<List<Double>> createData(double[]... arrays) {         final List<List<Double>> vals = new ArrayList<>();         Arrays.stream(arrays).forEachOrdered(a -> vals.add(asList(a)));         return vals;     }      static class Combo {         final double product;         final double[] vals;         final int[] indexes;          Combo(double product, double[] vals, int[] indexes) {             this.product = product;             this.vals = vals;             this.indexes = indexes;         }          @Override         public String toString() {             return new StringJoiner(", ", Combo.class.getSimpleName() + "[", "]")                 .add("product=" + String.format("%.2e", product))                 .add("vals=[" + Arrays.stream(vals).boxed().map(v -> String.format("%.2f", v)).collect(                     Collectors.joining(", ")) + "]")                 .add("indexes=" + Arrays.toString(indexes))                 .toString();         }     } } ``` 

Offer optional shipping insurance to customers with minimum order of $100

I found this question: Additional, optional fee on Woocommerce Checkout that may work for me except that I would need to use a 2.5% fee and not a flat $ rate. The 2.5% fee would be calculated for each minim order sub-total of $ 100.

Can this be done using a percentage fee and a minimum order amount?

For a creature’s natural attacks, is it possible to change the order of attacks?

As a Master of Many Forms, I recently started to use the Harpoon Spider as a form.

Full attack is :

Bite +11 melee and 2 fangs +2 ranged

The ex attack harpooning works like this

Harpooning (Ex): A dread harpoon spider can fire its harpoonlike fangs up to 20 feet (no range increment). A successful hit deals 1d6+1 points of damage as the harpoon hooks the flesh of the target and immediately exudes a thick, sticky glue. The spider can reel in a harpoon as a free action; treat this as a trip attack (+19 bonus) against any creature attached to the fang. Failure indicates that the harpoon rips free (and deals another 1d6+1 points of damage to the victim). This ability is otherwise like the ordinary harpoon spider’s harpooning ability.

Now, the question is, would it be possible to use the harpoon first, reel the target in and then bite, all in a full attack? Or do I have to wait until the next turn to bite?

Must the layers of the Prismatic Wall spell be destroyed in order?

Must the layers of the Prismatic Wall spell be destroyed in order?

You’d think this is answered read-as-written on page 269 of the 5e PHB:

The wall can be destroyed, also one layer at a time, in order from red to violet, by means specific to each layer.

However, I know I’ve seen multiple people around the internet talk about strategies of intentionally destroying certain layers of their own wall like the red, orange, or indigo layers, and holing up inside the wall and shooting or casting spells through the wall without those layers blocking them.

It also begs a question like “If a fireball hits the wall as part of battle and does 25 damage, does it destroy the blue layer individually?”

I just want some validation that you really do have to hack each layer down from red to violet as opposed to losing middle layers intentionally or unintentionally as part of the overall battle.

Order custom taxonomy hierarchy

I have some custom taxonomy for regions.

Commonly it would look something like this.

USA (parent) - Arizona (child 1) - - Phoenix (child 2) 

however, there will be cases where it will only be like

USA - Arizona 

or

UK - Wales 

and maybe even just

Japan 

In my for loop I get the taxonomy data as $ location = get_the_terms( $ id, 'listings_region' );

and then in the html/php I can simply write $ location[0]->name and then I get the the name of the first object in the array.

I have however noticed that the taxonomy comes back not in the correct hierarchy – instead, alphabetically.

When I echo '<pre>'; print_r($ location); echo '</pre>', I get the array and [0] would be Arizona and [1] would be USA.

How can I retrieve it the taxonomy array in the correct order so that ideally [0] is always the parent, [1] is the first child, etc?

Thanks in advance.

If you pass through the order of colors in Prismatic Wall one way, do you reverse the order of colors passing through the other way?

I set up a Prismatic Wall and a non-protected creature passes through it, say from left to right. On page 269 of the 5e Player’s Handbook, the spell reads:

“When a creature attempts to reach into or pass through the wall, it does so one layer at a time through all the wall’s layers. As it passes or reaches through each layer, the creature must make a Dexterity saving throw or be affected by that layer’s properties as described below.”

The layers are listed from Red to Violet, so the enemy passed through my wall in that order and made it through. They are now on the right side, and I have contrived a way to push them back through the wall to the left side. Do I reverse the order they encounter the layers and go Violet to Red, or do I always go Red to Violet whenever anything passes through the wall?

In other words, does a Prismatic Wall have a “sidedness” to it, and, if so, when is that sidedness established?

An operational semantics for lambda-calculus normal order evaluation strategy

TAPL book, page 56 reads:

Under the normal order strategy, the leftmost, outermost redex is always reduced first.

I understand this as a restriction of the full beta-reduction evaluation strategy, whose reduction rules are given as follows:

$ $ \frac{t_1 \to t_1′}{t_1 t_2 \to t_1′ t_2}$ $

$ $ \frac{t_2 \to t_2′}{t_1 t_2 \to t_1 t_2′}$ $

$ $ \frac{t_1 \to t_1′}{λx. t_1 \to λx. t_1′}$ $

$ $ \frac{(λx. t_1) t_2}{[x \to t_2] t_1}$ $

My question is how to transform these rules so that the normal order strategy condition holds.

Does sorting a list by alphabetical order make sense in all languages?

I am most of the way through adding I18n to my application with the primary goal being to get it usable in Japanese as well as English. One of the functions of my website is that there are tables which when you click on the headers the column is sorted by the content which is usually alphabetical order. Now I know nothing at all about the Japanese language and I can’t work out if sorting by alphabetical order is actually possible or if it actually makes sense and is useful.

Is alphabetical order as useful or possible in other languages which do not use the latin alphabet?

Tab order through a matrix of radio buttons

I’m building an HTML form where the user can select one of three options for each day of the week. The wireframe proposed by the designer looks like this: a matrix of radio buttons, with columns for each week day and rows for each option.

matrix of radio buttons

I can understand why the designer chose to put the days are on the horizontal axis: It reduces the height of the matrix, and we are used to time being on the horizontal axis.

Of course it is no problem to make the radio buttons behave correctly. But I’m wondering how to set it up for maximum accessibility. Especially keyboard navigation is a concern here. The default tab order would be in order of the dom, row-by-row:

row-by-row tab order

IMHO this default order does not make sense from a UX point of view. I think users would expect to choose an option for Monday and then proceed to Tuesday, Wednesday etc. In other words, column-by-column:

enter image description here

First question: Do you agree that in this design, the default tab order would result in bad UX for keyboard navigation?

Second question: What is the best way forward here? Find a fancy solution to make it work anyway? (Eg. a different DOM order with CSS grid positioning? Not tabindex anyway!) Or simply go back to the designer and ask them to pivot the matrix?