How to create all possible pair combinations without duplicates in Google Sheets?

google-sheets It is a hard task for native functions. Try a script and use it as a custom function: function getTournament(teams_from_range) { // teams_from_range — 2D Array var teams = []; // convert to list teams_from_range.forEach(function(row) { row.forEach(function(cell) { teams.push(cell); } ); } ); return getTournament_(teams); } function getTournament_(teams) { var start = 0; var … Read more

How to avoid large if-statements and instanceof

An elegant way of avoiding instanceof without inventing some new artificial method in the base class (with a non-descriptive name such as performAction or doWhatYouAreSupposedToDo) is to use the visitor pattern. Here is an example: Animal import java.util.*; abstract class Animal { String name; public Animal(String name) { this.name = name; } public abstract void … Read more

What is the meaning of list[:] in this code? [duplicate]

This is one of the gotchas! of python, that can escape beginners. The words[:] is the magic sauce here. Observe: >>> words = [‘cat’, ‘window’, ‘defenestrate’] >>> words2 = words[:] >>> words2.insert(0, ‘hello’) >>> words2 [‘hello’, ‘cat’, ‘window’, ‘defenestrate’] >>> words [‘cat’, ‘window’, ‘defenestrate’] And now without the [:]: >>> words = [‘cat’, ‘window’, ‘defenestrate’] … Read more

How can I collect the results of a repeated calculation in a list, dictionary etc. (make a copy of a list with each element modified)?

General approaches There are three ordinary ways to approach the problem: by explicitly using a loop (normally a for loop, but while loops are also possible); by using a list comprehension (or dict comprehension, set comprehension, or generator expression as appropriate to the specific need in context); or by using the built-in map (results of … Read more

How to iterate std::set?

You must dereference the iterator in order to retrieve the member of your set. std::set<unsigned long>::iterator it; for (it = SERVER_IPS.begin(); it != SERVER_IPS.end(); ++it) { u_long f = *it; // Note the “*” here } If you have C++11 features, you can use a range-based for loop: for(auto f : SERVER_IPS) { // use … Read more

Reverse Sorted Dictionary in .NET

The SortedDictionary itself doesn’t support backward iteration, but you have several possibilities to achieve the same effect. Use .Reverse-Method (Linq). (This will have to pre-compute the whole dictionary output but is the simplest solution) var Rand = new Random(); var Dict = new SortedDictionary<int, string>(); for (int i = 1; i <= 10; ++i) { … Read more