Policy classes for making widgets I have been learning C++ for the past few weeks, and I made my first try with policy classes. Ultimately my goal is to use policy classes to manufacture wrapper classes on an std::vector. Part of what the wrapper class does is generate a custom allocator class that depends on the policy classes.

That's the inspiration for what I have below, which is a widget maker that makes widgets with a printing function that depends on the templated inputs.

Printing policy classes:

template <class T>
struct Chatty
{
  static void logVal(T val){
    std::cout << val << std::endl;
  }
  static void logS(std::string s){
    std::cout << s << std::endl;
  }
};

template <class T>
struct Silent
{
  static void logVal(T val){}
  static void logS(std::string s){}
};

Widget:

class Widget {
public:
  int x;
  int y;
  std::function<void(std::string)> print;
  Widget()
    :x(0), y(0), print([](std::string){})
  {};
};

WidgetManager:

template < template <class Created> class CreationPolicy, template <class Created> class LoggingPolicy>
class WidgetManager : public CreationPolicy<Widget>, public LoggingPolicy<Widget>
{
public:
  WidgetManager() {};
  static Widget* doAll(){
    Widget* w = WidgetManager::Create();
    std::function<void(std::string)> f1 = WidgetManager::logS;
    w->print = WidgetManager::logS;
    return w;
  }
};

I would appreciate feedback on all aspects of the code, but I also have two specific questions:

My biggest concern: is there a better way to assign functions to the Widgets I am making? For example, perhaps I could directly assign member functions of Widgets from my WidgetManager, but then I'd have to bind the WidgetMaker's static functions to individual instances of Widgets, which seems like more overhead. What's best performance-wise?
I am sure WidgetManager::doAll() is not the way to go, but I am struggling to find another way to call functions from various policies all together. What's a better way to do this?

In Widget, the members x and y should be private.  They should not be exposed to the interface in such a way.  You can either add the private keyword above them, or just move the public keyword below them (members are private by default, so both are essentially the same).


How to speed up drawing process of output from .tflite model I have piece of code where I draw  output from model pixel by pixel:
fun draw(
        canvas: Canvas) {
        val rotated = sensorOrientation % 180 == 90
        var rotatedW = frameWidth;
        var rotatedH = frameHeight;
        if(rotated) {
            rotatedW = frameHeight
            rotatedH = frameWidth
        }

        val multiplier =
            min(
                canvas.height.toFloat() / rotatedH.toFloat(),
                canvas.width.toFloat() / rotatedW.toFloat()
            )
        val w = (rotatedW * multiplier).toInt()
        val h = (rotatedH * multiplier).toInt()

        results?.let {
            var pos: Int
            val xw = w / 256f
            val xh = h / 256f
            val rectF = RectF()
            for (y in 0 until 256) {
                for (x in 0 until 256) {
                    pos = getIndexOfMax(results!![0][y][x])
                    if (pos > 0) {
                        val vertical = y / 256f * h
                        val horizontal = x / 256f * w
                        rectF.left = horizontal - xw
                        rectF.top = vertical - xh
                        rectF.right = horizontal + xw
                        rectF.bottom = vertical + xh
                        canvas.drawRect(rectF, boxPaint)
                    }

                }
            }
        }
    }

However this works really slow. Nested for is probably one of the main issues here. I don't know how can I speed it up. As you can see both counters are necessary here because I need to get certain field from results. I've heard that Bitmap uasage instead of Canvas would be partial solution because I am able to get certain pixels but I don't really know how to use it right. Method draw() is called here inside MainActivity:
trackingOverlay.addCallback(
            object : OverlayView.DrawCallback {
                override fun drawCallback(canvas: Canvas?) = tracker.draw(canvas!!)
            }
        )

And the trackingOverlay is an instance of OverlayView class which inherits from View:
class OverlayView(ctx: Context, attr: AttributeSet) : View(ctx, attr) {

    private val callbacks: MutableList<DrawCallback> =
        LinkedList()

    fun addCallback(callback: DrawCallback) { callbacks.add(callback) }

    @Synchronized
    override fun draw(canvas: Canvas) {
        for (callback in callbacks) {
            callback.drawCallback(canvas)
        }
    }

    interface DrawCallback {
        fun drawCallback(canvas: Canvas?)
    }
}

I've tried creating Bitmap before draw (to reuse it) and then use canvas.drawBitmap() but it needs transformation Matrix to fill whole screen, otherwise it covers only small chunk of it and even then I haven't noticed any performance increase.
EDIT
That's how getIndexOfMax() looks like:
private fun getIndexOfMax(array: FloatArray): Int {
        if (array.isEmpty()) return -1
        return when {
            array[0] > array[1] -> 0
            else -> 1
        }
    }

Results is 4 dim Array of Float

TL;DR
Introduce coroutines in your code.
Analysis
It is difficult to optimize a code, where multiple parts are missing, like:

what does getIndexOfMax do? Maybe this is the bottleneck
How do results look like? Arrays? Maps? HashMaps?

I found also some strange code parts, like:

Why results?.let {? This can be removed completely.
var pos: Int should be moved inside the inner loop and made a val

Suggestion
I have too little context and I'm not familiar with Android, so the only thing I can suggest is using coroutines. My Example:
suspend fun draw(canvas: Canvas) = coroutineScope {
    val rotated = sensorOrientation % 180 == 90
    var rotatedW = frameWidth;
    var rotatedH = frameHeight;
    if (rotated) {
        rotatedW = frameHeight
        rotatedH = frameWidth
    }

    val multiplier =
        min(
            canvas.height.toFloat() / rotatedH.toFloat(),
            canvas.width.toFloat() / rotatedW.toFloat()
        )
    val w = (rotatedW * multiplier).toInt()
    val h = (rotatedH * multiplier).toInt()

    val xw = w / 256f
    val xh = h / 256f
    val rectF = RectF()
    for (y in 0 until 256) {
        for (x in 0 until 256) {
            launch {   // <-----
                val pos = getIndexOfMax(results!![0][y][x])
                if (pos > 0) {
                    val vertical = y / 256f * h
                    val horizontal = x / 256f * w
                    rectF.left = horizontal - xw
                    rectF.top = vertical - xh
                    rectF.right = horizontal + xw
                    rectF.bottom = vertical + xh
                    canvas.drawRect(rectF, boxPaint)
                }
            }
        }
    }
}


Selection and Insertion sorts from scratch in Java I am trying to find a good, basic way to make selection and insertion sorts so that I can manipulate them for other sorting techniques. How do these look? Is there a simpler way to write them?

package javaapplication59;

public class JavaApplication59 {

public static void main(String[] args) {
    selectionSort ss = new selectionSort();
    insertionSort is = new insertionSort();
    int[] arr = {2, 3, 4, 65, 6, 7, 3, 45, 56, 23, 34, 5, 4, 34, 6, 2, 57, 4, 45, 345};
    arr = is.sort(arr);
    System.out.print("{");
    for (int el : arr) {
        System.out.print(el + ",");
    }
    System.out.println("\b}");
}

}

class insertionSort {

public int[] sort(int[] a) {
    int insrt, j;
    boolean keepGoing;
    for (int i = 1; i < a.length; i++) {
        insrt = a[i];
        j = i - 1;
        keepGoing = true;
        while ((j >= 0) && keepGoing) {
            if(insrt<a[j]){
                a[j+1] = a[j];
                j--;
                if(j==-1)
                    a[0] = insrt;
            }
            else{
                keepGoing = false;
                a[j+1] = insrt;
            }

        }

    }
    return a;
}

}

class selectionSort {

public int[] sort(int[] arr) {
    int min, minIndex;
    for (int i = 0; i < arr.length; i++) {
        min = arr[i];
        minIndex = i;
        for (int j = i + 1; j < arr.length; j++) {
            if (arr[j] < min) {
                min = arr[j];
                minIndex = j;
            }
        }
        arr[minIndex] = arr[i];
        arr[i] = min;
    }
    return arr;
}

}

Please try to follow the code formatting conventions of the language that you're using. This makes your code much more readable for other programmers.
If you use a Java IDE like Eclipse, NetBeans or IntelliJ it can format the code for you. In Eclipse this is triggered by pressing Ctrl+Shift+F.

Your use of CamelCase for identifiers is correct but only methods should start with lower case, class names start with a capital letter.

Don't abbreviate english words unnecessarily. Writing insrt instead of insert gains you nothing and makes the code harder to read.

You're defining classes that have the same behavior, so it would be good to define a common interface so you can use them interchangeable.

interface Sort {
    void sort(int[] a);
}

class InsertionSort implements Sort {
    // ...
}

class SelectionSort implements Sort {
    // ...
}

For your print statement you can use StringJoiner. It's specially built for your use case and allows you to specify a delimiter, prefix and suffix.

public static void main(String[] args) {
    Sort ss = new SelectionSort();
    Sort is = new InsertionSort();
    int[] arr = { 2, 3, 4, 65, 6, 7, 3, 45, 56, 23, 34, 5, 4, 34, 6, 2, 57, 4, 45, 345 };
    is.sort(arr);
    StringJoiner sj = new StringJoiner(",", "{", "}");
    for (int el : arr) {
        sj.add(String.valueOf(el));
    }
    System.out.println(sj);
}

Since you sort the array in-place (you don't create a new array during sort) for both algorithms you don't have to return the array. The reference that you hold on it outside is enough.

Your Insertion Sort can be simplified by getting rid of the keepGoing boolean.
You move the conditional from the if statement into the head of the while loop.
You want the while loop to continue while a[j] > insrt, no need for an extra variable. Then you move the actual insertion out of the else clause and put it after the while loop.
Your're going to be at the right insertion spot once the while loop has finished.

class InsertionSort implements Sort {

    public void sort(int[] a) {
        int insert, j;
        for (int i = 1; i < a.length; i++) {
            insert = a[i];
            j = i - 1;
            while ((j >= 0) && a[j] > insert) {
                a[j + 1] = a[j];
                j--;
            }
            a[j + 1] = insert;
        }
    }
}

You SelectionSort looks fine to me.


Changing attributes in different objects First some background - I am developing a Python program which has thousands of lines spread across many files. I have intermediate programming skills - and no commercial OOP experience, and have taught myself Python recently. I have come across a problem which I can solve, in a number of different ways, but I'm not really happy with any of my solutions - they don't feel quite right from an OOP point of view. I have tried to make things generic so they can be used for lots of different options, but by doing so it seems massively convoluted.

I have distilled the problem down into a comparatively small piece of code for review. I have a main object, and I have an object which holds some options (which are just true/false for now, but could be other things in the future). I then have a third object (a tkinter button) that each show the status of the option (via being sunken or raised), and when clicked, change the value.

I felt the right way to do this would be for the functionality (in italics above) to be contained entirely in the toggleButton class. At first I just passed it self.options.Option1 (or whatever), but that didn't work. I then read up on does Python pass by value or reference, which (I know I am simplifying here) seemed to say that when you pass an object it passes the object, but when you pass something like an integer or boolean it passes the value. So I changed it to what you see now - where you pass the variable, an integer representing the variable, and changing the options class to have a function to change the option. It all seems so needlessly verbose. Is there a better way?

from Tkinter import *

class optionsClass():
    _OPTION1 = 1
    _OPTION2 = 2

    def __init__(self):
        self.Option1 = True
        self.Option2 = False

    def change(self, optionid):
        if optionid == optionsClass._OPTION1:
            if self.Option1 == True:
                self.Option1 = False
            else:
                self.Option1 = True
        if optionid == optionsClass._OPTION2:
            if self.Option2 == True:
                self.Option2 = False
            else:
                self.Option2 = True

class toggleButton(Button):
    def __init__(self, master, targetObject, targetoptionid, initialval
                    , **kwargs):
        Button.__init__(self, master, command = self.callback, **kwargs)
        self.targetoptionid = targetoptionid
        self.targetObject = targetObject
        if initialval==True:
            self.config(relief=SUNKEN)
        else:
            self.config(relief=RAISED)

    def callback(self):
        self.targetObject.change(self.targetoptionid)
        if self.cget("relief") == SUNKEN:
            self.config(relief=RAISED)
        else:
            self.config(relief=SUNKEN)

class mainframe(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.options = optionsClass()
        option1box = toggleButton(self, self.options, optionsClass._OPTION1,
                self.options.Option1, text="Option 1")
        option1box.grid(column=0, row=0)
        option2box = toggleButton(self, self.options, optionsClass._OPTION2,
                self.options.Option2, text="Option 2")
        option2box.grid(column=0, row=1)
        testbutton= Button(self, command = self.outputoptions,
                             text="Return option values")
        testbutton.grid(column=0, row=2)

    def outputoptions(self):
        print "Option 1 is " + str(self.options.Option1)
        print "Option 2 is " + str(self.options.Option2)

top = Tk()
m = mainframe(top)
m.pack()
top.mainloop()

Things to bear in mind...

I would like the options class to work for things other than boolean without much difficulty.
I like the convenience and readability of the main class to simply say self.options.Option1.
Ideally there will still be three classes, as mainframe and option do a lot more in the actual program, and toggleButton is used multiple times and is therefore a good candidate to be a class.

First, to answer your question on passing by value/reference, Python doesn't pass mutable (e.g. list, dict) and immutable (e.g. str, tuple) objects any differently - the difference appears because they are[n't] mutable, i.e. can[not] be changed in-place. Names in Python are just references to objects.

When you pass an immutable object:

def calling():
    foo = "bar" # bind str object to name 'foo' in scope of 'calling'
    called(foo) # pass reference to str object to 'called'
    print(foo) # still "bar"

def called(baz): # first arg object will be bound to name 'baz' in scope of 'called'
    baz = "{0}!".format(baz) # rebinds name 'baz' to **new str object**
    print(baz)

In this case, the string is immutable, it can't be changed in-place, so changes in called won't be reflected in calling, as calling still references the original object.

By contrast, with a mutable type:

def calling():
    foo = [] # bind list object to name 'foo' in scope of calling function
    called(foo) # pass reference to list object to 'called'
    print(foo) # see change from 'called'

def called(baz): # first arg object will be bound to name 'baz' in scope of 'called'
    baz.append("hello, world") # object bound to name 'baz' (and 'foo') **is changed**
    print(baz)

You should read and consider following the Python Style Guide, PEP-0008 - class names should be CapitalizedWords and variable, function and method names should be lowercase_with_underscores.

optionsClass can be streamlined. At the very least, note that

if self.Option1 == True:
    self.Option1 = False
else:
    self.Option1 = True

can be simplified to:

self.Option1 = not self.Option1

Dictionaries would allow you to simplify further:

class OptionsClass():

    id_map = {'Option1': 1, 'Option2': 2}

    def __init__(self, options=None):
        if options is None:
            options = {1: True, 2: False}
        self.options = options

    def change(self, option_id):
        self.options[option_id] = not self.options[option_id]

    def __getattr__(self, attr_name):
        return self.options[id_map[attr_name]]

Now you can use it:

>>> o = OptionsClass()
>>> o.Option1
True
>>> o.Option2
False
>>> o.change(1)
>>> o.Option1
False


Python3: using generator to process a very large list of integers This is from a leetcode question I am working on. The problem is the following:
create an algorithm that works in the the following fashion:
arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
the output is:
[2,7,14,8]
by applying the following series of operations:
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
The input is a very large list for arr and an even longer list for queries. The output has to be a list and I have tried this several ways.
first attempt: (building a gigantic list in memory and returning it using append() )
def XORsubqueries(array, queries):
    result = []
    for pair in queries:
        value = array[pair[0]]
        i = pair[0]
        while i < pair[1]:
            if pair[0] == pair[1]:
                result.append(array[pair[i]] ^ array[pair[i]])
                break
            else:
                i += 1
                value ^= array[i]
        result.append(value)
    return result

As you can imagine this takes FOREVER (53 seconds on my i5 dual core).  I then remembered about generator expressions for lazily building values and thought they might come in handy just for this kind of thing.
def XORsubqueries(array, queries):

    result = [0] * len(array)
    def XOR(array, queries):
        for pair in queries:
            value = array[pair[0]]
            i = pair[0]
            while i < pair[1]:
                i += 1
                value ^= array[i]
                yield value
    return list(XOR(array, queries))

If i remove the list off that return statement it figures it out in like 0.0001 seconds ! But I have to return it as a list.  Is there a way I can rewrite my algorithm to do what I am trying to do with this generator or did I paint myself into a corner ?

I would guess the biggest slowdowns are the append and list functions. In your second example, you attempt to pre-create a list, which is a good idea, but a) you use the wrong size, and b) you never use the created list. Here's an example using a pre-made list:
def xor_subqueries(array, queries):
    result = [0] * len(queries)
    for i,q in enumerate(queries):
        val = array[q[0]]
        for j in range(q[0]+1, q[1]+1):
            val ^= array[j]
        result[i] = val
    return result

Pretty straight forward. I'm not so sure this task calls for a generator function, but here's an example of using one:
def xor_subqueries(array, queries):
    def genx():
        for q in queries:
            val = array[q[0]]
            for j in range(q[0]+1, q[1]+1):
                val ^= array[j]
            yield val

    result = [0] * len(queries)
    for i,q in enumerate(genx()):
        result[i] = q
    return result

However, I have read that list comprehension is faster. You could easily re-write the above function using list comprehension (return [x for x in genx()]), but here is an example using the built-in functions reduce and xor which stand-in for your generator function.
from functools import reduce
from operator import xor

def xor_subqueries(array, queries):
    return [reduce(xor, array[q[0]:q[1]+1], 0) for q in queries]

It calls reduce on the sub-list array[q[0]:q[1]+1]). reduce calls xor on each element of the sub-list with the previously calculated value and an initial value of 0.


Retirement Calculator I created a program that determines how many years you have left before retiring, and in a year when you can retire.

This is my code below:

$(function(){
 const currentYear = (new Date).getFullYear();
 var convert = function(a){
     return parseInt(a.val());
 }
 var result = function(a, b){
     $('.currentYear').text(currentYear);
     if (b>a){
  $('.newAge').text(b - a);
  $('.retireYear').text(currentYear + (b - a));
  message1.dialog("open");
  return message1
     } else {
  $('.newAge').text(a - b);
  $('.retireYear').text(currentYear - (a - b));
  message2.dialog("open");
  return message2
     }
 }
 const message1 = $("#message1").dialog({autoOpen: false});
 const message2 = $("#message2").dialog({autoOpen: false});
        const form = $("#form").dialog({
            autoOpen: false,
            modal: true,
            buttons:[{
             text: "Сount",
             icon: "ui-icon-contact",
             click: function(){
                 $( this ).dialog( "close" );
                    var str1 = convert($('#currentAge'));
                    var str2 = convert($('#retireAge'));
                    result(str1, str2);
             }
            }]
        });
        $("#btn").click(function(){
         form.dialog("open");
     });
});
<link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet"/>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">

 <button type="button" class="btn btn-primary" id="btn">Retirement Calculator</button>

<form id="form">

        What is your current age?
        <input type="number" class="form-control" id="currentAge" oninput="validity.valid||(value='');" min="0" style="width: 90px">

        At what age would you like to retire?
        <input type="number" class="form-control" id="retireAge" oninput="validity.valid||(value='');" min="0" style="width: 90px">

</form>

You have  years left until you can retire.

It's , so you can retire in .

You could retire  years ago.

It's , so you could retire in .

I will appreciate any advice about code.

Since you're using some ES6+ features, I'd definitely recommend looking into others, such as let, arrow functions, and so on. This website is a good start, and you can find more information by searching "javascript es6".
Adding onto the last point, whenever you don't change a variable, prefer using const for consistency's sake. If you do need to change a variable, use let. Note that you can change properties on objects declared with const, but you still can't reassign the variable. You can look up more details on the website I linked.
The result function can be improved.

First off, it doesn't look like the return value is being used at all, so there's no need to return anything from it.

Secondly, if an element is referenced multiple times from jQuery, it's usually a good idea to cache it in a variable, so jQuery doesn't have to unnecessarily query the DOM multiple times. In small cases like this the performance difference doesn't matter much, but it's still a decent habit to pick up.

Here's what you'd end up with for the result function, as an example:

const result = (a, b) => {
  const $newAge = $(".newAge")
  const $retireYear = $(".retireYear")

  $(".currentYear").text(currentYear)
  if (b > a) {
    $newAge.text(b - a)
    $retireYear.text(currentYear + (b - a))
    message1.dialog("open")
  } else {
    $newAge.text(a - b)
    $retireYear.text(currentYear - (a - b))
    message2.dialog("open")
  }
}


Type to byte array conversion in Swift I need the byte representation of a given type, especially double, float, int8, etc.

typealias Byte = UInt8

enum ByteOrder {
    case BigEndian
    case LittleEndian
}

func pack<T>(var value: T, byteOrder: ByteOrder) -> [Byte] {
    let valueByteArray = withUnsafePointer(&value) {
        Array(UnsafeBufferPointer(start: UnsafePointer<Byte>($0), count: sizeof(T)))
    }
    return (byteOrder == .LittleEndian) ? valueByteArray : valueByteArray.reverse()
}

func unpack<T>(valueByteArray: [Byte], toType type: T.Type, byteOrder: ByteOrder) -> T {
    let bytes = (byteOrder == .LittleEndian) ? valueByteArray : valueByteArray.reverse()
    return UnsafePointer<T>(bytes).memory
}

What do you think?

Especially I am interested in your opinion on

CFByteOrder does offer already a byte order, but I decided to go with a different definition.
We can use the type inference and get rid of forType type: T.Type, which works fine for

let result: double = unpack(someArray, .LittleEndian)

but fails for

let result = unpack(someArray, .LittleEndian)

Is the following nicer? I don't think so, but what about you. :-)

func unpack<T>(valueByteArray: [Byte], byteOrder: ByteOrder) -> T {
    let bytes = (byteOrder == .LittleEndian) ? valueByteArray : valueByteArray.reverse()
    return bytes.withUnsafeBufferPointer {
        return UnsafePointer<T>($0.baseAddress).memory
    }
}

What do you think about encapsulating the two function in a class, e.g. ByteBackPacker and convert them to class functions?
What about giving byteOrder: ByteOrder a default value, e.g. .LittleEndian, because this is the native byte order in iOS and Mac OS X, the primary platforms for Swift, I assume?

Update:
I created a ByteBackpacker project on github. Let me know what you think about it.

Using a dedicated enumeration type ByteOrder is a good idea.
CFByteOrder is (ultimately) a type alias for Int, so you can
assign arbitrary integers to it, not just CFByteOrderBig/LittleEndian.

If you don't pass the type in the unpack function then the compiler
has to infer it from the context and either of these would work:

let d1 = unpack(bytes, byteOrder: .BigEndian) as Double
let d2 : Double = unpack(bytes, byteOrder: .BigEndian)

If you pass the type as an argument then the compiler infers
the return type:

let d3 = unpack(bytes, toType: Double.self, byteOrder: .BigEndian)

I think it is a matter of taste which version you choose, I would
prefer passing the type explicitly.

Encapsulating the functions into a type is also a good idea.
But I would use a struct which is initialized with the byte order,
and make that instance methods and not class methods. The reason
is that you probably will do several calls with the same byte order
and you don't have to pass it to every call:

struct BytePacker {
    let byteOrder : ByteOrder

    init (byteOrder: ByteOrder) {
        self.byteOrder = byteOrder
    }

    func pack<T>(var value: T) -> [Byte] { ... }
    func unpack<T>(valueByteArray: [Byte], toType type: T.Type) -> T { ... }
}

which can be used as

let packer = BytePacker(byteOrder: .BigEndian)
let bytes = packer.pack(12.34)
let d = packer.unpack(bytes, toType: Double.self)

There is a problem with

return UnsafePointer<T>(bytes).memory

in your first version of unpack() because is not guaranteed that the
elements of a Swift Array are stored in contiguous memory.

return bytes.withUnsafeBufferPointer {
    return UnsafePointer<T>($0.baseAddress).memory
}

as in your second version of unpack() is the correct solution
because withUnsafeBufferPointer() calls the block with a pointer to contiguous
storage.

Your code assumes that the host byte order is little-endian,
which is the case for all current OS X and iOS platforms.
On the other hand, Swift is open source now and people already start
to port it to different platforms. I have no idea if Swift will be
ported to a big-endian platform at some time.

I would at least make that assumption explicit by defining

enum ByteOrder {
    case BigEndian
    case LittleEndian

    static var hostByteOrder = ByteOrder.LittleEndian
}

and then replace

return (byteOrder == .LittleEndian) ? valueByteArray : valueByteArray.reverse()

by

return (byteOrder == ByteOrder.hostByteOrder) ? valueByteArray : valueByteArray.reverse()

In addition, I would add an assertion

assert(UInt(littleEndian: 1) == 1)

e.g. to the init method of BytePacker.

Alternatively, you can determine the actual host byte order at
runtime:

enum ByteOrder {
    case BigEndian
    case LittleEndian

    static var hostByteOrder = ByteOrder()

    init() {
        self = (UInt(littleEndian: 1) == 1) ? .LittleEndian : .BigEndian
    }
}

but I don't know if that is worth the hassle. (Note that
the static property hostByteOrder is computed only once in
the lifetime of the app, not each time that it is used.)

Your generic pack/unpack functions can be called with
arguments of any type, and that would give wrong results or crashes for

reference types, where the instance is just a pointer to the object,
"complex types" like Array or String, which are a struct but
use opaque pointers to the actual memory holding the elements.

There is – as far as I know – no protocol which comprises all integer and
floating point types. What you could do is to define a protocol

protocol PackableType { }

and make all types which can safely be packed and unpacked explicitly conform to it:

extension Int : PackableType { }
extension UInt16 : PackableType { }
// More integer types ...

extension Double : PackableType { }
// More floating point types ...

(Unfortunately, there are many integer types and I don't know if the repetition
can be avoided.)

Now you can restrict the type <T> to <T : PackableType>.
Alternatively, take a different approach and make the pack/unpack
methods operate on the type itself by defining a protocol extension:

extension PackableType {

    func pack(byteOrder byteOrder: ByteOrder) -> [Byte] {
        var value = self
        let valueByteArray = withUnsafePointer(&value) {
            Array(UnsafeBufferPointer(start: UnsafePointer<Byte>($0), count: sizeofValue(value)))
        }
        return (byteOrder == ByteOrder.hostByteOrder) ? valueByteArray : valueByteArray.reverse()
    }

    static func unpack(valueByteArray: [Byte], byteOrder: ByteOrder) -> Self {
        let bytes = (byteOrder == ByteOrder.hostByteOrder) ? valueByteArray : valueByteArray.reverse()
        return bytes.withUnsafeBufferPointer {
            return UnsafePointer($0.baseAddress).memory
        }
    }
}

(Note how the compiler infers the type in return UnsafePointer($0.baseAddress).memory
automatically.)

This would now be used as

let bytes = 12.34.pack(byteOrder: .LittleEndian)
let double = Double.unpack(bytes, byteOrder: .LittleEndian)

and solve your question "how to define the return type in unpack" in a
different way.


Delays using multiple for loops I got this script which work perfectly but I got some delays because of this 2 for loops [i][j] is there any way to do the same function but with a better and effective process like foreach or other.

User.find({}).lean(true).exec((err, users) => {
        let getTEvent = [];
        //nested loops() //callbacks

        for (let i =0 ; i < users.length; i++) {
          if(users[i].events && users[i].events.length) {
const dt = datetime.create();
dt.offsetInDays(0);
const formatted = dt.format('d/m/Y');
// console.log(formatted)
for (let j = 0; j < users[i].events.length; j++) {
    if(users[i].events[j].eventDate === formatted) {
      getTEvent.push({events: users[i].events[j]});
      }
    }
  }
}

   return res.json(getTEvent)
    });

The main role of this code is:

find the data in Mongodb: find all users with or without events
loop through data and select events made by any users
push the results in an array which is getEventin order to be useful later for the client side

Details:

The User is the model: const User = require('../models/users.model');
and events is [] array.

This is Mongodb structure of that models:

I think the main cause of your delays is not a nested for but rather a fact that you extract all data from your mongodb collection into memory. What you can do is

You can calculate formatted date once.
Then you can query just those users that contain given event date instead of populating all users in memory. Here's the guide

This will look roughly like the following

const dt = datetime.create();
dt.offsetInDays(0);
const formatted = dt.format('d/m/Y');

User.find({
        "events": {
            eventDate: formatted
        }
    }).lean(true).exec((err, users) => {
        let getTEvent = [];
        //nested loops() //callbacks

        for (let i = 0 ; i < users.length; i++) {
          if(users[i].events && users[i].events.length) {
            for (let j = 0; j < users[i].events.length; j++) {
                if(users[i].events[j].eventDate === formatted) {
                  getTEvent.push({events: users[i].events[j]});
                }
            }
          }
        }

   return res.json(getTEvent)
});


Caching/memoizing promise results I'm wondering what the best approach might be.

One approach is to chain cachedPromise and "regular", thus, if cachedPromise fails, we call a regular one (and caching results).

The second is to have a wrapper function, something like a decorator in python, and being able to wrap any promise-returning function when needed.

Here's code illustrating both approaches:

// this is for node.js environment
// in modern browsers Promise is already implemented
// as a standard feature
var Promise = require('bluebird');

var cachedPromise = {
    _cache: {},
    get: function(key) {
        var isCached = this._cache.hasOwnProperty(key);
        var data = this._cache[key];
        return new Promise(function(resolve, reject) {
            isCached ? resolve(data) : reject();
        });
    },
    store: function(key, value) {
        this._cache[key] = value;
    }
}

var WrappedPromise = function(promiseFunc) {
    var cache = {};
    return function(key) {
        if (cache.hasOwnProperty(key)) {
            return Promise.cast(cache[key]);
        } else {
            var promise = promiseFunc(key);
            return promise.then(function(data) {
                cache[key] = data;
                return promise;
            });
        }
    }
}

var mockRequestPromise = function(name){
    return new Promise(function(resolve, reject) {
        setTimeout(function() {
            resolve({'name': name});
        }, 1000);
    });
}

var requestChained = function(name) {
    return cachedPromise.get(name).catch(function() {
        var promise = mockRequestPromise(name);
        return promise.then(function(data) {
                   cachedPromise.store(name, data);
                   return promise;
               })
    });
}

var requestWrapped = WrappedPromise(mockRequestPromise);

If you have any considerations regarding this issue, I will be much obliged.

Code looks fine - but it strikes me as somewhat unnecessary.

A promise can only be resolved/rejected once, so if you just keep the promise object itself around, its result is automatically cached within. I.e. if you call .then() on an already-resolved/already-rejected promise object, it'll just immediately invoke your handler(s) with whatever data it was originally resolved/rejected. It won't re-do whatever caused it to be resolved/rejected. So just keep the promise object itself around and you can use it whenever you want.

In order words, caching promise objects makes sense (e.g. caching web request promises), but caching promise results is built-in and happens automatically.

So to that end, it'd be smarter to add caching at a higher level. I.e. don't wrap generic promises, instead wrap specific tasks like network requests and such where caching the promise is desired and you can have more control over what keys to use, when to invalidate the cache etc..

I'd do something like this

// *specialized* function for, say, network requests,
// where caching is desired. In this case, we're caching
// promises by the URL they're fetching
var fetch = (function () {
  var cache = {};
  return function (url) {
    cache[url] = cache.hasOwnProperty(url) ? cache[url] : new Promise(/* request magic goes here */);
    return cache[url];
  };
});

(Obviously, you'll want a way to invalidate the cache etc., this is just to illustrate the point)


Determine if an array of arrays of characters can be concatenated in order, into a substring of haystack Algorithm: Determine if an array of arrays of characters can be
  concatenated in order, into a substring of haystack.

  Example:

[['a','A','@'], ['b','B','8'], ['c','(','[']]

  This list can be combined by selecting from each of the subarrays in
  order, such as:

  abc, ab(, ab[, Abc, Ab( etc.

  Are any of these combinations substrings of the search string?

"123 easy as Ab(" # True
"123 easy as Abb" # False
"123 easy as @b(" # True
"123 easy as Abc" # False

Python 3

combos = [['a','A','@'], ['b','B','8'], ['c','(','[']]
search = "123 easy as Ab(" # True

def find_mutated_string(offset, combos, search):
    for i, char in enumerate(search):
        for mutant in combos[offset]:
            if mutant == char:
                if len(combos) == (offset+1):
                    # Last letter combo found
                    return True
                else:
                    return find_mutated_string(offset+1, combos, search[i+1:])
    # Didn't find mutant char in search string
    return False

print(find_mutated_string(0, combos, search))

bug

You don't reset the offset when there is a mismatch, so "123 easy as b(" also returns True. Just add:

    else:
        offset = 0

optional parameter offset

The caller of the function should not care about the offset if he wants to check whether a combination is part of the string. I would change the method signature to def find_mutated_string2(combos, search, offset=0). Then your user doesn't need to worry about this

in

Python has the in statement.

Your code says

    for mutant in combos[offset]:
        if mutant == char:

but actually mean: if char in combos[offset]

Since in in a list traverses the list to look for a match, while set uses a lookup, defining the combos as sets helps here

combos = [{"@", "A", "a"}, {"8", "B", "b"}, {"(", "[", "c"}]

def find_mutated_string2(combos, search, offset=0):
    for i, char in enumerate(search):
        if char in combos[offset]:
            if len(combos) == (offset + 1):
                return True
            else:
                return find_mutated_string2(combos, search[i+1:], offset+1)
        else:
            offset = 0
    return False

match substring

You could write a helper function that takes a substring and checks whether this matches the combo's

def matches_combos(substring, combos):
    return len(substring) == len(combos) and all(
        char in combo for char, combo in zip(substring, combos)
    )

This can be easily tested:

test_cases = {
    "abc": True,
    "Ab[": True,
    "abd": False,
    "ab": False,
    "abcd": False,
}
for substring, answer in test_cases.items():
    result = matches_combos(substring, combos)
    assert result == answer

recursion

If your string is long, you will run into the recursion limit.

for i in range(10000):
    try:
        _ = find_mutated_string2(combos, "a"*i)
    except RecursionError:
        print(f"fails for string length: {i}")
        break

fails for string length: 5921

for a recursionlimit of 3000

You can easily rewrite this proble iteratively

def find_mutated_string3(combos, search):
    if len(search) < len(combos):
        return False
    for i in range(len(search) - 2):
        substring = search[i : i + len(combos)]
        if matches_combos(substring, combos):
            return True

or using any

def find_mutated_string4(combos, search):
    if len(search) < len(combos):
        return False
    return any(
        matches_combos(search[i : i + len(combos)], combos)
        for i in range(len(search) - 2)
    )


PHP AJAX handler to fill an HTML table I would like to know if this code follows recommended practices and standards for AJAX/PHP calls. Can this AJAX + PHP code be improved in terms of security, performance, etc.?

ajax.js:

function getTableData() {
    const xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
        if (this.readyState === 4 && this.status === 200) {
            document.querySelector('#test-table tbody').innerHTML = this.responseText;
        }
    }
    xmlhttp.open('GET', 'ajax.php', true);
    xmlhttp.send();
}

$(document).ready(function () {
    getTableData();
});

ajax.php:

<?php
$data = prepareDataForTable();
$data_length = count($data);
$columns = 4;

for ($row = 0; $row < $data_length; $row += $columns) {
    echo '
';
        for ($col = 0; $col < $columns && ($col + $row) < $data_length; $col++) {
            echo '' . $data[$col + $row] . '	';
        }
    echo '
';
}
?>

ajax.html:

I specifically want to know if this is a good way to send information back to the client - by running loops and echoing output.

Trying to follow your mathematical loop conditions makes my eyes go crossed in a hurry.

Perhaps you'd entertain a less-math-heavy alternative:

foreach (array_chunk($array, 4) as $row) {
    echo '
' , implode('	', $row) , '	' , str_repeat('	', 4-count($row)) , '
';
}

Granted this will be making a no-output call of str_repeat() on all rows except potentially the final row, but I think this technique will be more developer friendly.  I'm assuming that I understand your $data (Demo).  I could have written a conditional for str_repeat() but felt that would only bloat the code.

I tend to lean toward passing json back from an ajax call, but for this situation sending back html is the simpler approach.


Setting a variable for a view template As you can see from the code below, my set function accepts two types of parameters:  either strings or arrays. Now, ideally I would like to be using operator overloading to handle this, but since PHP doesn't offer anything like that I am stuck with code like this. Is there a better way to write this (or perhaps a better way to address the problem altogether)?

class View {
   // stores file passed to View
   private $template;

   // setter function for template variables
   public function set($var, $content) {
      if (is_array($var) && is_array($content)) {
         if (sizeof($var) == sizeof($content)) {
            foreach ($var as $vari) {
               foreach ($content as $contenti) {
                  $this->template = str_replace("{" . "$vari" . "}", $contenti, $this->template);
               }
            }
         }
      }
      else {
         $this->template = str_replace("{" . "$var" . "}", $content, $this->template);
      }
   }
}

First, there is a simple way to flatten out those loops, but it is not what I would suggest.  Here is the simple way:

if ((sizeof($var) == sizeof($content))) {
    $count = sizeof($var);

    for ($i =0; $i < $count; $i++) {
        $this->template = str_replace(
            '{' . $var[$i] . '}', $content[$i], $this->template);
    }
}

Note that you are currently silently doing nothing when the size of your arrays don't match.  I think that is a bug.

My Suggestions

$var and $content are not very descriptive: I would suggest $match and $replacement.

str_replace can handle arrays as input.  I would suggest filling your match with the '{' and '}' beforehand.  The code then becomes:

public function set($match, $replacement)
{
   $this->template = str_replace($match, $replacement, $this->template);
}

I would place any sanity checks before the str_replace that you require.  str_replace uses blank replacements when the matches array is larger than the replacements.  If you want to avoid that use:

   if (is_array($match) && is_array($replacement) &&
       sizeof($match) > sizeof($replacement))
   {
      throw new InvalidArgumentException(
         __METHOD__ . ' Each match must have a replacement');
   }

This avoids arrow code which will make the code easier to test, reduce its complexity and make it easier to maintain.


Bash script to automate dev environment setup After blowing up my environment again messing around with installing random programs and packages I decided to write a bash script I can clone from github and use to reinstall everything like I had before
The idea is that I'll be able to keep adding to it as I find more programs to use so that it will always be a stable starting point. Right now it's just a list of whatever I used to reinstall things copy-pasted into a single file.
Do people bother organizing bash scripts? If so what sort of structure do you use? I keep my actual code relatively clean but this whole thing felt like an aside to keep things orderly in the future
echo Email for github?

# get email, assume github username is same and split it off from domain
read email
name=$(echo $email | grep -o '^[^@]*')

mkdir $HOME/GoProjects
mkdir $HOME/GoProjects/src
mkdir $HOME/GoProjects/bin
mkdir $HOME/PyProjects
mkdir $HOME/JSProjects
mkdir $HOME/pemKeys

# basic update and upgrade
sudo apt update && sudo apt -y upgrade

# install Chromium
sudo apt install -y chromium

#install Go
wget https://dl.google.com/go/go1.14.3.linux-amd64.tar.gz
sudo tar xvfz go1.14.3.linux-amd64.tar.gz -C /usr/local/
rm -f go1.14.3.linux-amd64.tar.gz

cat >> $HOME/.profile << EOF
export GOROOT=/usr/local/go
export GOPATH=$HOME/GoProjects
export GOBIN=$HOME/GoProjects/bin
export PATH=$PATH:/usr/local/go:/usr/local/go/bin:$HOME/GoProjects:$HOME/GoProjects/bin
EOF

. $HOME/.profile

go get github.com/lib/pq

# install VScode
curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > packages.microsoft.gpg
sudo install -o root -g root -m 644 packages.microsoft.gpg /usr/share/keyrings/
sudo sh -c 'echo "deb [arch=amd64 signed-by=/usr/share/keyrings/packages.microsoft.gpg] https://packages.microsoft.com/repos/vscode stable main" > /etc/apt/sources.list.d/vscode.list'

sudo apt install -y apt-transport-https
sudo apt update -y
sudo apt install -y code

rm -f packages.microsoft.gpg

# install postgres
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
RELEASE=$(lsb_release -cs)
echo "deb http://apt.postgresql.org/pub/repos/apt/ ${RELEASE}"-pgdg main | sudo tee  /etc/apt/sources.list.d/pgdg.list
sudo apt update -y
sudo apt -y install postgresql-11

sudo apt install -y build-essential

#install docker
sudo apt install -y apt-transport-https ca-certificates curl gnupg2 software-properties-common
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable"
sudo apt update -y
apt-cache policy docker-ce
sudo apt install -y docker-ce
sudo chmod 666 /var/run/docker.sock

#Add anaconda
sudo apt install -y libgl1-mesa-glx libegl1-mesa libxrandr2 libxrandr2 libxss1 libxcursor1 libxcomposite1 libasound2 libxi6 libxtst6
wget https://repo.anaconda.com/archive/Anaconda3-2020.02-Linux-x86_64.sh
bash Anaconda3-2020.02-Linux-x86_64.sh -b -p
rm -f Anaconda3-2020.02-Linux-x86_64.sh

sudo apt install -y libpq-dev python3-dev
pip install psycopg2

#Add protobuff compiler and grpc
sudo apt install -y protobuf-compiler
export GO111MODULE=on
go get github.com/golang/protobuf/protoc-gen-go
go get -u google.golang.org/grpc

#Need to add NPM
sudo apt install  -y nodejs
sudo apt install -y npm

#setup github
git config --global user.name $name
git config --global user.email $email
git config --global color.ui true

yes "" | ssh-keygen -t rsa -C $email
cat $HOME/.ssh/id_rsa.pub

rm -rf ./SetupDebianDevEnv

I think you could benefit from using Ansible in this scenario then you can easily deploy your configuration to other machines and you will have learned a valuable IT skill.
And rather than use bash scripts, you use yaml configuration files.
As for your script: there is a fair amount of repetitive code. Rather than repeat the wget or curl, you could gather your sources into an array, then run wget/curl in a loop, because you are performing multiple operations that are similar. Upside: more concise code. Downside: less separation between the various steps. I can understand you did it this way. But if your file grows because you keep adding sources, you'd have to reconsider the approach and start using loops.
This is something you could have done here too:
mkdir $HOME/GoProjects
mkdir $HOME/GoProjects/src
mkdir $HOME/GoProjects/bin
mkdir $HOME/PyProjects
mkdir $HOME/JSProjects
mkdir $HOME/pemKeys

An array would make sense. As mentioned above, don't forget to quote !
Same for the apt install, but since you are adding some repo sources, that needs to be done beforehand, and it would require reorganization of your script.
The other issue is you are downloading specific software versions that will probably be outdated by the time you reinstall your computer. Thus, an outdated script becomes less valuable, if you have yet to update or patch your install to have a setup that is current.
As an example:
#install Go
wget https://dl.google.com/go/go1.14.3.linux-amd64.tar.gz
sudo tar xvfz go1.14.3.linux-amd64.tar.gz -C /usr/local/
rm -f go1.14.3.linux-amd64.tar.gz

I would use variables instead, so that the package name is defined only once:
#install Go
go_file="go1.14.3.linux-amd64.tar.gz"
wget "https://dl.google.com/go/$go_file"
sudo tar xvfz "$go_file" -C /usr/local/
rm -f "$go_file"

It is a minor change that will make maintenance of this file more manageable.
Also, you could move the code to dedicated functions. That makes it a bit easier to separate the various steps you are performing. Also, you can easily disable certain items by simply commenting out the function call (= changing only 1 line). In fact it would be nice if you could run your script à la carte, it is perfectly possible that you will want to run it again, but discard certain items that are already installed. For instance if your script crashed in the middle and you don't want to waste time reinstalling everything from the beginning, which is wasteful.
The other benefit of calling functions in sequence, is that you can easily reorder them. At some point you may find out that a certain piece of software should or must be installed before another, because of some dependencies.
I would install software packages from the regular OS repositories whenever possible. Do you really need to install Postgresql from source ?
I would also avoid PIP and install the Python packages from apt-get instead, unless they are not available for your OS.


School House Points (Python + SQLite) I am creating a database to store house points for our school running events. I would like to be able to tally all the points for the houses and it is working fine, however, I think it may be a bit verbose at this point.

Is there a simpler/better way with maybe more efficient code?

Races are stored in a races table (competitorID, raceID, time).

Student data is stored in a competitor table (competitorID, names, house).

The code will query the DB and bring back unique races, and then assign a point value based on their position (calculated by their race time).

import sqlite3
conn = sqlite3.connect('house.db')
conn.row_factory = sqlite3.Row

# House Totals
blueTotal = 0
greenTotal = 0
redTotal = 0
yellowTotal = 0

# Assign points from 1st = 15 through to 10-Last being 1
def points():
    global pointvalue
    if count == 0:
        pointvalue = 15
        places()
    if count == 1:
        pointvalue = 12
        places()
    if count == 2:
        pointvalue = 10
        places()
    if count == 3:
        pointvalue = 8
        places()
    if count == 4:
        pointvalue = 7
        places()
    if count == 5:
        pointvalue = 6
        places()
    if count == 6:
        pointvalue = 5
        places()
    if count == 7:
        pointvalue = 4
        places()
    if count == 8:
        pointvalue = 3
        places()
    if count == 9:
        pointvalue = 2
        places()
    if count > 10:
        pointvalue = 1
        places()

#Add points to houses
def places():
    global blueTotal, greenTotal, redTotal, yellowTotal, pointvalue
    if competitors['house'] == "blueTotal":
        blueTotal += pointvalue
    if competitors['house'] == "redTotal":
        redTotal += pointvalue
    if competitors['house'] == "yellowTotal":
        yellowTotal += pointvalue
    if competitors['house'] == "greenTotal":
        greenTotal += pointvalue

for x in range(1,50):
    competitorDetails = conn.execute('SELECT firstname, surname, house '
                                     'FROM race, competitor '
                                     'WHERE competitor.competitorID = race.competitorID '
                                     'AND race.raceID = ' + str(x))
    count=0
    for competitors in competitorDetails:
        points()
        count += 1

print("blueTotal " + str(blueTotal))
print("greenTotal " + str(greenTotal))
print("redTotal " + str(redTotal))
print("yellowTotal " + str(yellowTotal))

DRY
This repetitive code is an anti-pattern:
blueTotal = 0
greenTotal = 0
redTotal = 0
yellowTotal = 0

Please define
houses = 'blue green red yellow'.split()

and then you can use array access to perform "the same action" across all houses:
for house in houses:
    total[house] = 0

One could also assign total = collections.defaultdict(int), but that would
be a horse of a different color.
(https://docs.python.org/3/library/collections.html#collections.defaultdict)
global
The global keyword is usually not your friend,
it leads to unfortunate coupling.
Here, it is offering you a hint that you want to define a class,
set totals to zero in the __init__() constructor,
and then have places() access self.total[].
arg passing
This is a bit crazy:
    for competitors in competitorDetails:
        points()

Yes, you can treat competitors as an implicit argument
by making it global, but there is absolutely no reason to,
and the current code makes it extremely difficult for readers
to understand what is going on. Please, please make competitors
an explicit argument, passing it in with points(competitors).
formatting
Clearly this works:
print("blueTotal " + str(blueTotal))

but the explicit call to str is slightly verbose.
Consider re-phrasing such print statements in one of these ways:
print("blueTotal", blueTotal)
print(f'blueTotal {blueTotal}')


Extracting key information from a directory of XML files I have written some VBA to loop through a directory of 15,000 XML files and extract key information from these files into Excel - this is unbearably slow. Can someone please assist me in optimising the module?

Sub ImportXMLtoList()
 Application.DisplayAlerts = False
 Application.ScreenUpdating = False
 oldStatusBar = Application.DisplayStatusBar
 Application.DisplayStatusBar = True

 Dim myDir As String
 Dim StrFile As String
    StrFile = Dir("\\Ntsydfsp152\shared\Temp\Lucsan\LMIG\Output\XML\SBL\Staging\")
    myDir = "\\Ntsydfsp152\shared\Temp\Lucsan\LMIG\Output\XML\SBL\Staging\"
    myFile = "LMIG_List_XML_Files.xlsm"

 'clear sheet1
 Sheets(1).Range("a2:g30000").Clear
 x = 2

 Do While Len(StrFile) > 0

 Application.StatusBar = StrFile & " Row: " & x
    Sheets(1).Cells(x, 1).Value = StrFile
    Workbooks.OpenXML Filename:=myDir & StrFile, LoadOption:=xlXmlLoadImportToList
    temp2 = ActiveWorkbook.Sheets(1).Range("g2")
    temp3 = ActiveWorkbook.Sheets(1).Range("e2")
    temp4 = ActiveWorkbook.Sheets(1).Range("ak3")
    temp5 = ActiveWorkbook.Sheets(1).Range("av4") & "/" & ActiveWorkbook.Sheets(1).Range("ax4") & "/" & ActiveWorkbook.Sheets(1).Range("aw4") & "/" &     ActiveWorkbook.Sheets(1).Range("ay4")

    ActiveWorkbook.Close

    Sheets(1).Cells(x, 2) = temp2
    Sheets(1).Cells(x, 3) = temp3
    Sheets(1).Cells(x, 4) = temp4
    Sheets(1).Cells(x, 5) = temp5
    x = x + 1

    StrFile = Dir
 Loop

 Application.DisplayAlerts = True
 Application.ScreenUpdating = True
 Application.StatusBar = False
 Application.DisplayStatusBar = oldStatusBar

End Sub

A few short comments:
Use Option Explicit:
Why? because It makes thinks easier by orders of magnitude when developing.
You get more useful error messages and it becomes harder to misspell variable names without realizing.
That brings us right to the next:
Variable Names:
Your variable names are not consistently cased. You are mixing up "camel casing" (e.g. myDir) and "Pascal Casing" (e.g. StrFile)
Also temp 1 through 4 are not really good variable names. Neither is x, but hey ;)
Error Handling:
You should really make sure your "Application" is in a clean state after exit, even when you got unexpected errors:
Sub ImportXMLToList()
    On Error GoTo ErrorHandler

    ' Code goes here:

:CleanExit
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    Application.StatusBar = False
    Application.DisplayStatusBar = oldStatusBar

    Exit Sub
:ErrorHandler
    'Close resources, maybe errormessage
    Resume CleanExit
End Sub


Concise nested statements I really like the ruby ? : syntax for if/else statements.

However, I'm not sure how to do the following with a nested statement:

if information.is_empty?
  if leader? || possessor?
    link_to('update ' + contextual('your') + ' credentials!', edit_user_path)
  else
    user.name + ' hasn\'t entered this info!'
  end
else
  information
end

Should I bother? Does the ? : syntax (for want of a term!) actually make code pretty unreadable and non-self-documenting?

If you think I should use the ? : syntax, how would I tackle this?

The ?: is called a ternary operator. It's not specific to Ruby; a lot of languages have it (in fact, I believe I linked you to that wikipedia page in a comment very recently).
Go easy on the newlines; there's a lot of whitespace there (update: question was edited; now it's gone)
Use string interpolation
We've talked about that is_ prefix on ? methods already :)

To answer your question: Ternaries have their use, but like everything else, they can also be abused.

In practice, there's no difference between a ternary and and a good ol' if...else, so you can use either - but that argument goes both ways. Don't use ternaries just because you can.

A good case for a ternary (in my opinion), would be something like this (just as an example):

puts some_boolean_value ? "yes" : "no"

or something like

"You have #{messages.count} new #{messages.one? ? 'message' : 'messages'}"

There's no logic going on in either the if or the else branch, and both are very short, so it's short and sweet to use a ternary.

In Ruby you can also spell the first one out like:

puts if some_boolean_value
  "yes"
else
  "no"
end

It's neat that Ruby treats if...else as an expression, but here it seems like a waste of space for something so simple.

In your case: Your code does have more branching logic, so I'd definitely avoid ternaries, and keep it as-is. Personally, I'd never nest ternary expressions inside each other, just for readability reasons.

Alternatively, you can return early (assuming this code is in a method):

return information unless information.is_empty?

if leader? || possessor?
  link_to("update #{contextual "your"} credentials", edit_user_path)
else
  "#{user.name} hasn't entered this info!"
end

If you wrote it all in ternaries, it'd be pretty ridiculous:

information.is_empty? ? leader? || professor? ? link_to("update #{contextual "your"} credentials", edit_user_path) : "#{user.name} hasn't entered this info!" : information # what?

The middle road would be to use a ternary for the innermost nested branch only

if information.is_empty?
  leader? || professor? ? link_to("update #{contextual "your"} credentials", edit_user_path) : "#{user.name} hasn't entered this info!"
else
  information
end

But, as you can see, that innermost nested branch is also the one with the most going on, so it's still less readable than a normal if...else


SQL update scripts taking too much memory and doesn't free it up I have this piece of code that executes update scripts on the database. I have 20 .sql files that contains about 400 update scripts. The overall size of the files is around 60MB. When I execute all the update scripts, it eats up about 400 MB of RAM and doesn't free it. I'm looking for ways to optimize this code.

private void ExeCuteCommand(SqlConnection conn, string script, bool flag)
    {
        SqlCommand command = new SqlCommand();
        command.Connection = conn;
        command.CommandType = CommandType.Text;
        command.CommandText = script;

        if (flag)
            command.ExecuteNonQuery();
    }

EDIT:

This is the calling method, basicall what it does is loop through the files, get the contents and pass the contents to the ExecuteCommand method.

private void ExecuteUpdateScripts(SqlConnection conn)
    {
        int step = 1;
        foreach (string update in Directory.GetFiles(scriptDirectory, @"SCRIPT UPDATE ??.sql"))
        {
            string updateScript = File.ReadAllText(updateSQL);
            ExeCuteCommand(conn, updateScript, true);

            step++;
        }
    }

I have looked at the Visual Studio profiler and this is the line the consumes a lot of memory command.ExecuteNonQuery();

As it seems you execute the SqlCommand only if flag == true so the first thing you should do is place a guard clause at the top of the method.
The SqlCommand object implements the IDisposable interface, therefor you should enclose it in an using statement which takes care of the disposing of this object.
the default value of the CommandType property is CommandType.Text so there is no need to assign it.
there is a overloaded constructor, which takes the connection and the querystring so you should use it.
Execute is not a compound word, so you shouldn't use PascalCase for this word. The method should be named ExecuteCommand().
a variable named flag does not describe what its purpose is. Much better would be shouldBeExecuted.
you should always use braces {} although they are optional for single line if statements. Omiting them will lead to potential error prone code.

Applying these points will lead to

private void ExecuteCommand(SqlConnection conn, string script, bool shouldBeExecuted)
{
    if (!shouldBeExecuted) { return; }

    using (SqlCommand command = new SqlCommand(script, conn))
    {
        command.ExecuteNonQuery();
    }
}


Checking if a directory exists in FTP without relying on exception handling I've written a method to check if a directory already exists on an FTP server. The only way I could see to make this work was to return a different result from the method if an exception is thrown - very bad practice in my opinion. Is there a better way to write this?

public bool DirectoryExists(string directory)
{
    if (String.IsNullOrEmpty(directory))
        throw new ArgumentException("No directory was specified to check for");

    // Ensure directory is ended with / to avoid false positives
    if (!directory.EndsWith("/"))
        directory += "/";

    try
    {
        var request = (FtpWebRequest)WebRequest.Create(directory);
        request.Method = WebRequestMethods.Ftp.ListDirectory;
        request.Credentials = new NetworkCredential(Username, Password);

        using (request.GetResponse())
        {
            return true;
        }
    }
    catch (WebException)
    {
        return false;
    }
}

The connection hostname, username and password referred to within this block of code are auto properties set when the class is initialized.

don't omit braces {} for single if statements. Using braces in this case will make your code less errorprone.
you want to return a state, which represents a specific states which shouldn't be restricted to only two states. This just calls for an enum.

public enum FtpResponse
{
    DirectoryExists, DirectoryNotFound, DirectoryNotSpecified
}

public FtpResponse DirectoryExists(string directory)
{
    if (String.IsNullOrEmpty(directory))
    {
         return FtpResponse.DirectoryNotSpecified;
    }
    // the rest of the code

}


Advent Of Code 2017 Day 4 (part 2) in Functional Programming (FP) I wanted to practice functional programming (FP) without using any library but using vanilla JavaScript only. So I took a problem from Advent of Code (the 2nd part of Day 4)

You can only access the 2nd part if you type in the solution for the 1st part: 466 or look at my solution for part 1

  --- Part Two --- For added security, yet another system policy has been put in place. Now, a valid passphrase must contain no two words
  that are anagrams of each other - that is, a passphrase is invalid if
  any word's letters can be rearranged to form any other word in the
  passphrase.

  For example:

  abcde fghij is a valid passphrase.
  abcde xyz ecdab is not valid - the letters from the third word can be rearranged to form the first word.
  a ab abc abd abf abj is a valid passphrase, because all letters need to be used when forming another word.
  iiii oiii ooii oooi oooo is valid.
  oiii ioii iioi iiio is not valid - any of these words can be rearranged to form any other word. Under this new system policy, how
  many passphrases are valid?

My solution in FP:

const INPUT =
`pphsv ojtou brvhsj cer ntfhlra udeh ccgtyzc zoyzmh jum lugbnk
spjb xkkak anuvk ejoklh nyerw bsjp zxuq vcwitnd xxtjmjg zfgq xkpf
...
juo pmiyoh xxk myphio ogfyf dovlmwm moevao qqxidn`;

const get = input => input.split('\n');
const countDuplicate = words => words.reduce((acc, word) => {
  return Object.assign(acc, {[word]: (acc[word] || 0) + 1});
}, {});
const onlyUniqueWords = phrases => {
  const words = phrases.split(' ');
  const duplicateWords = countDuplicate(words);
  return !Object.values(duplicateWords).some(w => w > 1);
};
const sortWords = words => words.map(word =>  word.split('').sort().join(''));
const noAnagrams = phrases => {
  const words = phrases.split(' ');
  const sortedWords = sortWords(words).sort().join(' ');
  return onlyUniqueWords(sortedWords);
};
const phrasesWithNoAnagrams = get(INPUT)
.filter(noAnagrams);
console.log("solution ", phrasesWithNoAnagrams.length);

Is there a better way to write it in FP with pure JavaScript, i.e. no additional FP library? Any other improvement suggestions are welcomed.

I don't see much that I would change about this code. I recently discovered that the spread syntax can be used to split a string into an array so that could be used to eliminate calling the split() function.

Instead of

const sortWords = words => words.map(word =>  word.split('').sort().join(''));

use the spread syntax:

const sortWords = words => words.map(word =>  [...word].sort().join(''));

In this reduction:

const countDuplicate = words => words.reduce((acc, word) => {
  return Object.assign(acc, {[word]: (acc[word] || 0) + 1});
}, {});

I presume that the only reason you used brackets with a return statement is because it would be too long for a single line. You could avoid those by pulling out the arrow function to a separate line:

const countWord = (acc, word) => Object.assign(acc, {[word]: (acc[word] || 0) + 1});
const countDuplicate = words => words.reduce(countWord, {});

I know that is only one line shorter but at least there is no need for the brackets and return statement.


Sorting large 1GB file with 100 millions numbers using merge sort My task is to sort 1GB file with 100 million numbers using merge sort without recursion. My idea is to split file into 4 pieces, then to 2 and at the end to one file.

#include<iostream>
#include<fstream>
#include <cstdlib>
#include <vector>
#include <algorithm>
using namespace std;
std::vector <long> vec;

int i=0,numberOfpieces=0;
string line,filename;

//sorting and merge 2 files
void sort_merge(string result, string file1, string file2){

fstream wynik((result+".txt"), std::ios::out);

    string line, line2;
    long s1,s2;
    fstream sorted_1,sorted_2;
    sorted_1.open(file1+".txt");
    sorted_2.open(file2+".txt");

    getline(sorted_1,line);
    s1=atol(line.c_str());
    getline(sorted_2,line2);
    s2=atol(line2.c_str());

    do
    {
        if(s1==s2)
        {
            wynik<<s1<<endl;
            getline(sorted_1,line);
            s1=atol(line.c_str());
            getline(sorted_2,line2);
            s2=atol(line2.c_str());
        }
        if(s1<s2)
        {
            wynik<<s1<<endl;
            getline(sorted_1,line);
            s1=atol(line.c_str());
        }
        if(s1>s2)
        {
            wynik<<s2<<endl;
            getline(sorted_2,line2);
            s2=atol(line2.c_str());
        }
        if(sorted_1.eof())
        {
            do
            {
                wynik<<s2<<endl;
                getline(sorted_2,line2);
                s2=atol(line2.c_str());

            }
            while(!sorted_2.eof());
            break;
        }

        if(sorted_2.eof())
        {
            do
            {
                wynik<<s1<<endl;
                getline(sorted_1,line);
                s1=atol(line.c_str());
            }
            while(!sorted_1.eof());
        }

    }
    while (!sorted_1.eof() ||!sorted_2.eof());}

int main()
{
    fstream file;
    file.open("file.txt");
    if (file.good() == true )
    {
        do
        {
            for(i=1;i<=25000000;i++){ //splittining file into 4 pieces
            getline(file,line);
            vec.push_back(atol(line.c_str()));
            if(file.eof()){ //in case of last piece when quantity of numbers is less than 50
                break;
            }
            }
            sort(vec.begin(), vec.end() );//sorting vector
            std::string nm = std::to_string(numberOfpieces); //creating indyvidual name for piece
            filename="sorted_piece_"+nm+".txt";
            fstream sorted_piece(filename, std::ios::out);

            for ( auto &i : vec ) { //saving into file
                sorted_piece<< i <<endl;
            }

            numberOfpieces++;
            vec.clear();
        }
        while ( !file.eof() );
    }
string outFile;
string sorted1;
string sorted2;
int j=0,counti=0;

for(j=0;j<numberOfpieces;j+=2){//merging into 2 pieces
    std::string j1 = std::to_string(j);
    std::string j2 = std::to_string(j+1);
    std::string c = std::to_string(counti);
    outFile=c+"_sorted_piece";
    sorted1="sorted_piece_"+j1;
    sorted2="sorted_piece_"+j2;
    sort_merge(outFile,sorted1,sorted2);
    counti++;
}
outFile=numbersSorted;
sorted1=1_sorted_piece;
sorted2=2_sorted_piece;
sort_merge(outFile,sorted1,sorted2);//merging into 1 piece

return 0;
}

Unfortunately it is slow and I don't know exactly what is the best way to improve it, because vector sorting has \$O(nlog(n))\$ complexity. Also, my algorithm with merging 2 files have \$O(n)\$ complexity. Maybe splitting into files takes too much time, but I can't hold vectors that are too long in RAM.

Don't do this

using namespace std;

This is a bad habit that will get you into a lot of trouble. Please break the habit before it becomes ingrained.

see: Why is “using namespace std” considered bad practice?

Note: The reason the "standard" library uses the namespace std:: is so that it is not a large burden to use as a prefix.

Prefer '\n' over std::endl

Others have mentioned it.
The problem is that std::endl performs a flush of the stream (in addition to adding '\n') as a result this makes using the stream very inefficient as you need to wait to write every value to disk before continuing.

Let the system flush its own internal buffer at the optimum time. It will do this automatically without any of your help.

Reading Value

I see that you are not using the standard stream to integer conversion.

long value;
stream >> value;

but rather using the line reader than using atol() to convert the string into a number.

I assume you have done the appropriate speed tests and found this to be faster on the scale that you are using and you don't need all the facilities that the stream operator>> provides.

If this is true then I would wrap up that call so that you can use atol() and operator>> to make your code more readable.

struct Number
{
    long value;
    static std::string  reusableBuffer;
    operator long() const {return value;}
    friend std::istream& operator>>(std::istream& str, Number& data) {
        std::getline(str, data.reusableBuffer);
        data.value = std::atol(data.reusableBuffer);
        return str;
    }
};

Now you can read numbers using operator>> but still have the speed you have verified with atol().

Never test for eof in a loop

while (!sorted_1.eof() ||!sorted_2.eof());}

The problem becomes that if there is an error on the stream then you end up going into an infinite loop (if there is an error reading from the stream it goes into a bad state until you  reset it. While in a bad state it will refuse to read any more values and thus never reach the end of the file.

Rather than testing for eof() you should check to make sure the stream is good() (if it reaches eof() or error() then it will no longer be good() so your loop will still exit correctly).

The easy way to test for good() is to simply use the stream in a boolean context (an expression that is expected to be bool) and the compiler will convert the stream to bool which results in a call to good().

while(sorted_1 || sorted_2);

Merge I think can be simplified.

I think we can simplify that merge algorithm a lot.

Number number;
if (sorted1 >> number) {
    sorted1Value = number;
}
if (sorted2 >> number) {
    sorted2Value = number;
}
while (sorted1 && sorted2) {
    // Don't need to explicitly test if they are equal.
    // If they are equal always use the sorted1 file.
    // that way you get a stable sort (an important property for
    // sorted values in some situations).
    //
    // It also makes the code simpler.
    if (sorted1Value <= sorted2Value) {
        result << sorted1Value << "\n";
        sorted1 >> number;
        sorted1Value = number;
    }
    else {
        result << sorted2Value << "\n";
        sorted2 >> number;
        sorted2Value = number;
    }
}
// At this point one of the streams is empty.

Copy one file to another.

When you have merged all the values out of one file. Then the remaining numbers from the other file are simply copied to the destination. But there is no need to spend extra time converting those numbers from text to an integer then back into text. Simply copy the raw data from one file to the other.

// At this point one of the streams is empty.
if (stream1) {
    result << sorted1Value;
    result << stream1.rdbuf()
}
if (stream2) {
    result << sorted2Value;
    result << stream2.rdbuf()
}

Intermediate format

So the intermediate files don't need to be in human readable form. You spend a lot of time converting integers to and from text. Rather than doing this you could write the binary representation of the number to an intermediate file and then read it directly back into the number when merging.

// writing
result.write(reinterpret_cast<char*>(&sorted1Value), sizeof(sorted1Value));

// reading
stream1.read(reinterpret_cast<char*>(&sorted1Value), sizeof(sorted1Value));

Merging more than one file.

Currently you perform the merge operation 3 times. Once to merge parts 1 and 2 into a temp file. Then you merge 3 and 4 into a second temp file and finally you merge the temp files into the output.

Merging from different streams is not that memory intensive. So you should read from all the temporary files at the same time (within reason as long as you have less than a thousand part file).

So you should open all 4 temp files and read the values from them. Each iteration find the largest value from each of the 4 files write that to the output and read the next value.

Global State.

Global state

std::vector <long> vec;

int i=0,numberOfpieces=0;
string line,filename;

Is always considered a bad thing. Never use it. You can still get the same efficiencies by using local values in the appropriate places.

One thing you way want to do to increase the efficiency of std::vector is to reserve its size. That way it never needs to do any reallocation (which is expensive).

vec.reserve(25000000);

Note your use of <= in the for loop is very unusual.

for(i=1;i<=25000000;i++)

Normally this is written as:

for(i = 0; i < 25000000; ++i)

This is because indexes into arrays (vectors) always start at zero.


LeetCode: Queue Reconstruction by Height C# https://leetcode.com/problems/queue-reconstruction-by-height/

  Suppose you have a random list of people standing in a queue. Each
  person is described by a pair of integers \$(h, k)\$, where \$h\$ is the
  height of the person and \$k\$ is the number of people in front of this
  person who have a height greater than or equal to \$h\$. Write an
  algorithm to reconstruct the queue.

  Note: The number of people is less than 1,100.

Example

Input:
[[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]

Output:
[[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]]

Please review for performance

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;

namespace SortingQuestions
{
    /// <summary>
    /// https://leetcode.com/problems/queue-reconstruction-by-height/
    /// </summary>
    [TestClass]
    public class ReconstructQueueTest
    {
        [TestMethod]
        public void ReconstructQueueExampleTest()
        {
            int[][] people = new int[6][];
            people[0] = new[] { 7, 0 };
            people[1] = new[] { 4, 4 };
            people[2] = new[] { 7, 1 };
            people[3] = new[] { 5, 0 };
            people[4] = new[] { 6, 1 };
            people[5] = new[] { 5, 2 };

            int[][] expected = new int[6][];
            expected[0] = new[] { 5, 0 };
            expected[1] = new[] { 7, 0 };
            expected[2] = new[] { 5, 2 };
            expected[3] = new[] { 6, 1 };
            expected[4] = new[] { 4, 4 };
            expected[5] = new[] { 7, 1 };
            int[][] res = ReconstructQueueClass.ReconstructQueue(people);
            for (var index = 0; index < res.Length; index++)
            {
                CollectionAssert.AreEqual(expected[index], res[index]);
            }
        }
    }

    public class ReconstructQueueClass
    {
        public static int[][] ReconstructQueue(int[][] people)
        {
            int[][] res = new int[people.Length][];
            Array.Sort(people, new PairComparer());

            List<int> list = new List<int>();

            for (int i = 0; i < people.Length; i++)
            {
                list.Add(i); //a list of indices 0,1,2,3,4...
                res[i] = new int[2];
            }

            for (int i = 0; i < people.Length; i++)
            {
                int index = list[people[i][1]]; //the index in the result is the number of people before you
                res[index][0] = people[i][0];
                res[index][1] = people[i][1];
                list.RemoveAt(people[i][1]); // we remove the index from the list so we keep only the un used ones
            }
            return res;
        }
    }

    /// <summary>
    /// sort the people, have min height first
    /// for the same height for example 5,0 and 5,2
    /// 5,2 is before 5,0
    /// </summary>
    public class PairComparer : IComparer<int[]>
    {
        public int Compare(int[] x, int[] y)
        {
            if (x[0] != y[0])
            {
                return x[0] - y[0];// we want min value first
            }
            return y[1] - x[1];
        }
    }
}

API

I assume it's part of the platform requirements, but int[] is a terrible data-structure for storing the (k, h) pair, because it could have any length and implies an ordering that doesn't really exist. This should be a small class or immutable struct. (Not a Tuple, because Tuples are structural, and therefore only solve part of the problem).

Even if this was imposed upon me, I would want to map it to something sensible before doing anything else. This mapping would act as a (necessary) validation layer, to check I'm not being given nonsense.

ReconstructQueueList

This should document what it does.

I don't see the point in initialising every element of res, when you could just assign them a copy of the existing person array when you set them. This makes the code much tidier. I would move the declaration of res toward where it is actually used.

list is a completely meaningless variable name. These are the positions in the queue that you have yet to be assigned, so I will call it positions; however, I'm sure you can think of something much better. I would create positions with Enumerable.Range(0, people.Length), which is more compact and has less scope for errors. I doubt the added overhead will be significant, but feel free to measure it.

PairComparer

PairComparer is not a fantastic name for something that sorts people.

It's good that you've given some description of what the custom comparer does. However, I don't understand the description you provide.

I'd prefer to see x[0].CompareTo(y[0]) instead of x[0] = y[0]: it's much clearer what is going on, and it doesn't risk overflowing for numbers with large magnitude.

Performance

This section is pretty underwhelming

If we ignore the problem size limit of 1100 (where did they find that number?), the main performance concern is the List.Remove(int) calls, which means this algorithm is quadratic in the worst case (though linear in the best case). You can address this by using a data-structure which allows log(n) removal by index. I threw together a simple OrderedShrinkList, where removing every has a cost of n log(n) and n is the number of elements with which it begins, and ran some benchmarks.

Swapping this appears to give a significant performance boost for large problems: it is 10 times faster on a particular random instance of size 200000 gist of code and results (it seems to be slower for smaller ones).

Running small problems (e.g. size 1100 and smaller), the potential inefficiency of List based method is not immediately revealed, and the overhead of my shoddy OrderedShrinkList seems to make it significantly slower. I've run out of energy to keep trying things, but a linked list might actually improve things, because it replacing a linear 'move' with a linear scan, and you can choose at which end to start.

Note: I did run benchmark for some different seeds, and the general trend was similar, but this isn't the most rigorous test ever.


Simple Node.js user management system I am working on a application that is made completely in JavaScript (other than the database) and I've created a simple user management system for the application.

This user management system creates a query to the database containing the user info (the database contains the user info not the query) based on a few command line arguments, decrypts the data in the database, and then prints the result to the console.

I have been programming for 2 years but am self-taught and was looking for some feedback/criticism on my code.

Note: Some of the modules in the Main script are not included but, for the most part, their functions should be intuitive.

Edit: for more information about the DataBase class see this question.

Here is my code:

Main script:

// this is a instance of a `DataBase` class:
const users = require('../../Code/MiddleWare/Database-comms/createUser').users
const lookup = require('./lookup')
const decryptAll = require('./decrypt')

void async function() {
  if(process.argv[2] === '-e') {
    console.dir(decryptAll(await lookup('Email', process.argv[3], users, process.argv[4] === '-ex')), {colors: true, depth: Infinity})
  } else if(process.argv[2] ? process.argv[2].match(/^\d+$/) : false) {
    console.dir(decryptAll(await lookup('ID', parseInt(process.argv[2]), users, process.argv[3] === '-ex', false)), {colors: true, depth: Infinity})
  } else {
    console.dir(decryptAll(await lookup('Username', process.argv[2] ? process.argv[2] : '', users, process.argv[3] === '-ex')), {colors: true, depth: Infinity})
  }
}()

"lookup" module:

module.exports = async function(type, string, users, exact,  encrypted = true) {
  const aes = require('../../Code/MiddleWare/Security/index').encryption.AES
   const retval = []
   await users.query('Users').then((data) => {
     for(let i of data) {
        if(encrypted ? (exact? aes.decrypt(i[type]) === string : aes.decrypt(i[type]).match(string)) : i[type] === string) {
          retval.push(i)
        }
     }
   })

   return retval.length === 0 ? false : retval
}

The "decrypt" module:

const decrypt = require('../../Code/MiddleWare/Security/encryption').AES.decrypt

module.exports = function(array) {
  if(!array) {
    return false
  }

  const retval = []

  function decryptAll(obj) {
    return {ID: obj.ID, Username: decrypt(obj.Username), Email: decrypt(obj.Email), Data: decrypt(obj.Data), Password: obj.Password}
  }

  for(const i of array) {
    const item = decryptAll(i)
    item.Data = JSON.parse(item.Data)
    retval.push(item)
  }

  return retval
}

Main script

Consider using the commander module. One benefit you get is that it parses arguments for you. Another is that the API that declares the arguments and options  autogenerates --help text for you. This way, you avoid having to write all the argument parsing yourself and you get a self-documenting CLI.

Lookup module

Move that require call outside the function. You only need to import aes once, not on every call to the lookup function.
When inside an async function, you can use await to write async operations in a synchronous-looking fashion. When you use await, you can assign the resolved value of an async operation to a variable in the same way you do it with synchronous operation.
The code inside the for-of is essentially a filter operation. Consider using array.filter() instead, assuming data is just an array.
I also recommend breaking out that nested ternary into nested if statements for readability. You could arrange the conditions so that it's cascading rather than nested.
I recommend returning an empty array instead of false when no matches are found. This way, the consuming code won't have to deal with type checking.

Decrypt module

The decryptAll function does not appear to rely on any variables in the scope of the exported function. You can move decryptAll out of that function.
That for-of is essentially a mapping function (transforming one array's values into another array of values). Use array.map() instead.
Since decryptAll is just mapping select properties into a new object, you can use a combination of destructuring and shorthand properties to simplify it.

Bottom line, your code could look like this:

Lookup

const aes = require('../../Code/MiddleWare/Security/index').encryption.AES

module.exports = async function(type, string, users, exact,  encrypted = true) {
  return await users.query('Users').filter(i => {

    // Technically, the encrypted block is a nested ternary in the
    // false portion of the first ternary. But written this way,
    // it looks like a flat list.
    return !encrypted ? i[type] === string
      : exact ? aes.decrypt(i[type]) === string
      : aes.decrypt(i[type]).match(string)
  })
}

Decrypt

const decrypt = require('../../Code/MiddleWare/Security/encryption').AES.decrypt

// Destructure arguments, then return an object with shorthand properties.
// Eliminates obj.
const decryptAll = ({ ID, Username, Email, Data, Password }) => {
  return { ID, Username, Email, Data, Password }
}

module.exports = function(array) {
  // We're expecting an array so the check is no longer needed.

  const retval = array.map(i => {
    const item = decryptAll(i)
    const itemWithDecryptedData = { ...item, Data: JSON.parse(item.Data) }
    return itemWithDecryptedData
  })

  return retval
}


SQL Query to check if 100+ (or Dynamic) columns in table is null Let assume a table has more than 100 of columns & I want to display all the rows when all of its columns (Col1, Col2, ..., ColN) are nulls except for the column(s) I mentioned (PK).

This is just a sample table I created to explain this question.

Sample Table with data

+----+------+------+------+------+
| PK | Col1 | Col2 | Col3 | Col4 |
+----+------+------+------+------+
|  1 | 45   | 12   | 171  | 15   |
|  2 | 13   | NULL | 15   | NULL |
|  3 | 17   | 14   | 130  | NULL |
|  4 | 10   | 11   | 110  | 12   |
|  5 | 45   | 15   | 185  | 54   |
|  6 | NULL | NULL | NULL | NULL |
|  7 | 16   | 48   | NULL | 25   |
|  8 | NULL | NULL | NULL | NULL |
|  9 | 13   | 45   | 41   | 64   |
| 10 | NULL | NULL | NULL | NULL |
| 11 | 21   | 12   | 165  | 56   |
| 12 | NULL | NULL | NULL | NULL |
+----+------+------+------+------+

Image: https://i.stack.imgur.com/z17GK.png

In this case, 6, 8, 10 and 12 rows (Column name: PK) are the result I want to see since all of the columns (Col1 to Col4) are null except for the PK column.

Desired Output

+----+------+------+------+------+
| PK | Col1 | Col2 | Col3 | Col4 |
+----+------+------+------+------+
|  6 | NULL | NULL | NULL | NULL |
|  8 | NULL | NULL | NULL | NULL |
| 10 | NULL | NULL | NULL | NULL |
| 12 | NULL | NULL | NULL | NULL |
+----+------+------+------+------+

Image: https://i.stack.imgur.com/prwFo.png

The typical way to do this is to write a query like this.

SELECT * FROM TableName WHERE COALESCE(Col1, Col2, Col3, Col4) is null

If the above table has more 100 columns as I said, writing this query will be hard especially if you have to do it often.

Therefore, I have created a query like this.

DECLARE @query NVARCHAR(MAX);

SELECT @query = ISNULL(@query+', ','') + [name]
                FROM  sys.columns
                WHERE object_id = OBJECT_ID('TableName')
                AND  [name] != 'PK';

SET @query = N'SELECT * FROM TableName WHERE COALESCE('+ @query +') IS NULL';

EXECUTE(@query)

Does this SQL code follow common best practices? How is the quality of this code? (Ex: Performance)

Does this SQL code follow common best practices?

No.

SET @query = N'SELECT * FROM TableName WHERE COALESCE('+ @query +') IS NULL';

1) It is common best practice to not use 'Select *' in production code. One's apologetics might include 'Well, this is dynamic code ...' and if you're okay with that (or the customer is) then carry on.

2) I would use a Socratic method and ask what is the purpose of returning columns you know are null? Why wouldn't you write the query to only return the PK if all other columns are null?

  How is the quality of this code?

It looks is fine.

I would suggest if you want a better evaluation, to provide sample objects / DDL to allow others to run it locally. Perhaps to evaluate the query plan.


Rotate image via javascript using CSS I'm doing a clock in javascript and I am not sure this way is the best way to rotate the pointers.

Is this ok, or is there a better way?

// browser vendors
var browsers = [
    'webkit',
    'Moz',
    'ms',
    'O',
    ''];

// function to move the pointers
function moveMe(el, unit) {
    var deg = unit * 360 / 60;
    browsers.each(function (b) {
        el.style[b + 'Transform'] = 'rotate(' + deg + 'deg)';
    });
}

// function to check the time
function checkTime() {
    var date = new Date();
    var seconds = date.getSeconds();
    var minutes = date.getMinutes();
    var hours = date.getHours();
    moveMe(sPointer, seconds);
    !seconds && moveMe(mPointer, minutes);
    !minutes && moveMe(hPointer, hours);
}

The fiddle I'm playing with is here.

First of all, your code use Array.each() method, which is a non-standard Javascript method, added by Mootools. I would rather use a standard loop, in order to keep a vanilla JS code.

Then, instead of setting all properties every time, you should test which properties are supported by the browser and set them accordingly.

Finally, since you are moving minutes only when !seconds (i.e. seconds == 0), the clock initialization is a bit weird. On page load, you should always move minutes and hours.


Inserting two rows that are almost identical to each other I'm trying to make test data for an application I'm writing, and some of my test data is very repetitive, involving inserting a row and then inserting another row that is identical except for one column. As such, I have very repetitive INSERT statements that make the file longer than I think it needs to be.

INSERT INTO [dbo].[InvoiceItem] (invoice_item_name, amount, invoice_id) VALUES
  ('TUITION',
    (SELECT tuition_amount FROM [dbo].[Grade] WHERE grade_id =
      (SELECT grade_id FROM [dbo].[Student] WHERE first_name = 'Murdoch')),
    (SELECT invoice_id FROM [dbo].[Invoice] WHERE student_id =
      (SELECT student_id FROM [dbo].[Student] WHERE first_name = 'Murdoch' AND invoice_date = '2019-09-05'))),
  ('TUITION_SERVICE_FEE',
    (SELECT tuition_amount FROM [dbo].[Grade] WHERE grade_id =
      (SELECT grade_id FROM [dbo].[Student] WHERE first_name = 'Murdoch')) * 0.03,
    (SELECT invoice_id FROM [dbo].[Invoice] WHERE student_id =
      (SELECT student_id FROM [dbo].[Student] WHERE first_name = 'Murdoch' AND invoice_date = '2019-09-05'))),
  ('TUITION',
    (SELECT tuition_amount FROM [dbo].[Grade] WHERE grade_id =
      (SELECT grade_id FROM [dbo].[Student] WHERE first_name = 'Hartwell')),
    (SELECT invoice_id FROM [dbo].[Invoice] WHERE student_id =
      (SELECT student_id FROM [dbo].[Student] WHERE first_name = 'Hartwell' AND invoice_date = '2019-09-05'))),
  ('TUITION_SERVICE_FEE',
    (SELECT tuition_amount FROM [dbo].[Grade] WHERE grade_id =
      (SELECT grade_id FROM [dbo].[Student] WHERE first_name = 'Hartwell')) * 0.03,
    (SELECT invoice_id FROM [dbo].[Invoice] WHERE student_id =
      (SELECT student_id FROM [dbo].[Student] WHERE first_name = 'Hartwell' AND invoice_date = '2019-09-05'))),

Is there a way to clean this up by any chance?

You should create a Stored Procedure that does this with a parameter called @firstname

it would come out way cleaner.

here is what the create would look like, I added in the @InvoiceDate parameters as well thinking that it was also something that was not static in the insert statement.

CREATE PROCEDURE Sproc_Name
    @FirstName nvarchar(50)
    ,@InvoiceDate DateTime
AS
INSERT INTO [dbo].[InvoiceItem] (invoice_item_name, amount, invoice_id) VALUES
('TUITION',
    (SELECT tuition_amount FROM [dbo].[Grade] WHERE grade_id =
    (SELECT grade_id FROM [dbo].[Student] WHERE first_name = @FirstName)),
    (SELECT invoice_id FROM [dbo].[Invoice] WHERE student_id =
    (SELECT student_id FROM [dbo].[Student] WHERE first_name = @FirstName AND invoice_date = @InvoiceDate))),
('TUITION_SERVICE_FEE',
    (SELECT tuition_amount FROM [dbo].[Grade] WHERE grade_id =
    (SELECT grade_id FROM [dbo].[Student] WHERE first_name = @FirstName)) * 0.03,
    (SELECT invoice_id FROM [dbo].[Invoice] WHERE student_id =
    (SELECT student_id FROM [dbo].[Student] WHERE first_name = @FirstName AND invoice_date = @InvoiceDate)))
GO

and then you would call it like this

EXECUTE Sproc_Name @FirstName = "Murdoch", @InvoiceDate = CONVERT(date, GETDATE())
-- CONVERT(date, GETDATE()) gives you 2018-04-05

I just used GetDate() for illustrative purposes, you can put in a string like "2018-04-05" and it should work as well.