Speeding up Excel distance calculation using Bing API calls

Depending on what you want to return you can actually leverage just one API method from Bing to retrieve the data you are after, the Driving Route API. Also, as others have pointed out what's killing performance is the synchronous code.

What I've done below is limited the pull to a single request, and made the code asynchronous.

I've also changed this code to a sub, as I'd need to iterate over some sort of collection. For ease of use, I used a range.

I've structured my data as the from address being in the first column of the range, and the destination address being in the column immediately after that. Travel Distance and Travel Duration will be output offset relative to the from address (2 and 3 columns offset respectively).

I did a quick benchmark, this is taking just over 4 seconds for 250 requests. Hope it helps!

Option Explicit

Const BaseURL As String = "http://dev.virtualearth.net/REST/V1/Routes/Driving?wp.0="
Const APIKey  As String = "YOUR_KEY"
Private Const READYSTATE_COMPLETE  As Long = 4

Public Sub GetDistances(Addresses As Range)
    Dim Server           As Object
    Dim ServerItem       As Variant
    Dim Servers          As Object
    Dim Cell             As Range
    Dim URL              As String

    Set Servers = CreateObject("Scripting.Dictionary")

    'Send all the requests up front, but don't wait for them to complete
    For Each Cell In Addresses
        'See here: https://docs.microsoft.com/en-us/bingmaps/rest-services/examples/driving-route-example for more details on this api
        URL = BaseURL & Cell & "&wp.1=" & Cell.Offset(0, 1) & "&key=" & APIKey & "&DistanceUnit=mi&DurationUnit=min&output=xml"
        Set Server = CreateObject("MSXML2.ServerXMLHTTP")
        Server.Open "GET", URL, True 'Last param will make request async
        Server.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
        Server.send ("")
        Servers.Add Cell.Address, Server
    Next

    'Iterate each XML request sent to see if done
    For Each ServerItem In Servers.Keys()
        Set Server = Servers(ServerItem)

        While Server.readyState <> READYSTATE_COMPLETE
            DoEvents
        Wend

        'Parse result
        If Server.Status = 200 Then
            'Add result to the sheet to an offsetting column
            Addresses.Parent.Range(ServerItem).Offset(0, 2) = WorksheetFunction.FilterXML(Server.ResponseText, "/Response/ResourceSets/ResourceSet/Resources/Route/TravelDistance")
            Addresses.Parent.Range(ServerItem).Offset(0, 3) = WorksheetFunction.FilterXML(Server.ResponseText, "/Response/ResourceSets/ResourceSet/Resources/Route/TravelDuration") / 60
            'You can also return the lat/long from this request, see --> https://docs.microsoft.com/en-us/bingmaps/rest-services/examples/driving-route-example
        End If
    Next

End Sub

Sub ProcessData()
    Dim myRng As Range
    Dim t As Double
    t = Timer
    Set myRng = ThisWorkbook.Sheets(1).Range("A1:a250")
    GetDistances myRng
    Debug.Print Timer - t
End Sub


Is this a good implementation of a thread-safe singleton using the observer pattern?

The notifyListeners() method is private, and is never called anywhere. It should be synchronized on the same lock than the other methods using the list of listeners.

Unless lazy instantiation is really needed, I wouldn't use it. Just use the following code:

private static final ObserverSingleton INSTANCE = new ObserverSingleton();

public static ObserverSingleton getInstance() {
    return INSTANCE;
}

Also, I would use a Set<OnRequestFinishedListener> rather than a list, since your add method makes sure there is only one copy of a listener inside the list, and Set is precisely used for that. If order of insertion matters, use a LinkedHashSet<OnRequestFinishedListener>.

Declaring the List as ArrayList is bad practice. You should declare it as List, since the concrete type doesn't matter.


Is this the easiest way of doing it?

In my view, yes: I don't see how to dramatically simplify your code.
It only can be (very) slightly reduced using return false; instead of e.preventDefault;.

You also might use the more common $(document).ready() in place of $(window).load().
BTW it'd be more readable to rename your checkPets() function to something like mustPlayMusic(), which reflects that it returns a boolean.

So it results in:

$(document).ready(function(){
  $('.pets form').submit( function(e){
    if(mustPlayMusic()) {
      $('body').append('<audio></audio>');
      $('audio').attr({'src': 'audio.mp3', 'autoplay': 'autoplay'});
      return false;
    }
});

On the other hand, something puzzles me: the checkPets()||mustPlayMusic() function doesn't match the conditions you detailed above.

It should be:

function checkPets () {
  var cat = $('#checkbox1').is(":checked");
  var dog1 = $('#checkbox2').is(":checked");
  var dog2 = $('#checkbox3').is(":checked");
  return (cat && (dog1 || dog2)) || (dog1 && dog2);
}


Merge the multiple observables responses into a single array

Use mergeMap on the call to getDevices, then mergeAll to flatten the results, and toArray to convert back to an array:
getCount().pipe(
  mergeMap(count => range(0, Math.ceil(count / pageSize))),
  mergeMap(offset => getDevices(offset, pageSize)),
  mergeAll(),
  toArray()
).subscribe(res => {
  console.log(JSON.stringify(res));
});

See here: https://stackblitz.com/edit/so-rxjs-merge-results?file=index.ts


Maximum sum subarray efficiency -- divide and conquer w/ vectors

Most algorithms use iterators as the interface between themselves and the container. It abstracts away the actual container type and in cases likes this removes the need for a copy.

Prefer operator[] over at() when you already know that your index into the container is valid (the at() validates the index before doing the operation (useful when using unvalidated input but otherwise expensive).

template<typename I>
int sumSpecial(I loop, I end)
{
    int sum     = 0;
    int maxsum  = INT_MIN
    for(;loop != end; ++loop)
    {
        sum += *loop;
        maxsum = max(maxsum, sum);
    }
    return maxsum;
}
template<typename I>
int algorithm_3(I begin, I end){

    auto n = std::distance(begin, end);

    // BASE CASE
    if(n == 1){
        return *begin;
    }

    // RECURSIVE CALLS
    int m = n / 2;
    I mid = begin;
    std::advance(mid, m);

    int left_MSS = algorithm_3(begin, mid);
    int right_MSS = algorithm_3(mid, end);

    // DETERMINE THE LEFTSUM AND RIGHTSUM FOR CASE 3: THE SUFFIX + PREFIX

    int leftsum  = sumSpecial(std::reverse_iterator<I>(mid), std::reverse_iterator<I>(begin));
    int rightsum = sumSpecial(mid, end);

    int ans = max(left_MSS, right_MSS);

   return max(ans, leftsum + rightsum);
}

// Usage
int main()
{
    std::vector<int>   data = getData();
    std::cout << algorithm_3(std::begin(data), std::end(data));
}


So, let's start with the Worksheet event and what you're trying to do. You only want to trigger the pivot update if the changed cell is B8. There's no need to intersect, just check the target's address.

Private Sub Worksheet_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    If Target.Address = "B8"
            UpdatePivotFieldFromRange "B8", "B3", Outfield
    End If
End Sub

Note that I properly indented the code and got rid of that horrid line continuation.

Now, we can move onto UpdatePivotFieldFromRange. Kinda...

You're immediately recreating the target range at the beginning of this sub, so just pass Target in as a range instead of a string. That changes the event to this:

Private Sub Worksheet_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    If Target.Address = "B8"
            UpdatePivotFieldFromRange Target, "B3", Outfield
    End If
End Sub

And your declaration of UpdatePivotFieldFromRange to this:

Public Sub UpdatePivotFieldFromRange(rng As Range, Fieldname As String, PivotTableName As String)

But that's still not right. There's no reason for this to be public, declare it private instead. I also don't like the name rng. It's not meaningful, but I'll leave it to you to give it a meaningful name.

There is so much wrong here I'm not sure where to start.

For Each Sheet In Application.ActiveWorkbook.Worksheets
    On Error Resume Next
    Set pt = Sheet.PivotTables(PivotTableName)
Next
If pt Is Nothing Then GoTo Ex

There's no reason to call Application. ActiveWorkbook.Worksheets works just fine.
But you're begging for bugs. If the user clicks on another workbook while this is running, the sub is suddenly working on a different workbook. Use a Workbook typed variable instead.

Dim book As Workbook
Set book = ActiveWorkbook

For Each sheet In book.Worksheets

The next thing to note is you're setting On Error GoTo Next at each iteration of the loop. It should be moved outside of the loop.
But, you shouldn't ever use On Error GoTo Next. Seriously, Never do this. You're telling vba to simply swallow the error. It's analogous to having an empty catch block. Find a way to check to see if the pivot table exists instead.
Finally, you're using GoTo to jump to your error handler. You should also never do this. It's much better to Raise an Error.

That said, I do like that you're always ensuring that ScreenUpdating gets turned back on. (Well, you would be, if you set OnError GoTo Ex directly upon entering the subroutine.)

Again, there's no reason for a private function to be declared Public. Look into scoping and declaration in vba. It's important. If these were functions instead of Subs, they would be available as user formulas.

Secondly, do you really intend for this to hide every field except the one you're looking for? I doubt it.

Public Sub SelectPivotTableItem(Field As PivotField, ItemName As String)
Dim Item As PivotItem
For Each Item In Field.PivotItems
    Item.Visible = (Item.Caption = ItemName)
Next
End Sub

I have a feeling that what you really meant to do was turn the particular field on/off like this.

Public Sub SelectPivotTableItem(Field As PivotField, ItemName As String)
    Dim Item As PivotItem
    For Each Item In Field.PivotItems
        If Item.Caption = ItemName Then
            Item.Visible = True
        End If
    Next
End Sub


Abstract Person with Employees and Clients

Yes. When some classes have things in common but common things different having a abstract base class is a good way to go.

Having a common base class gives you a number of advantages:

Reuse between the classes. Your Find and GetAll methods don't have to be rewritten for each type of person. So you avoid having to test this N times, and you avoid accidental (and subtle) differences in implementation (=> bugs!). (N = the number of classes extending from Person)
A consistent interface. Other code using a Person doesn't (necessarily) need to know what kind of Person it's working with. For example you could write a script to get all people and update them without having to worry about the differences between clients/employees.

But you need to make sure of a few things:

The implementations in your base class must work for all children, now and in the future. So if you want to add an Employer class you don't want to have to change the base class methods. If you do so you have to test your other class implementations to make sure they still work. That being said, minor modifications often aren't too much of a big deal.
If your classes have a small amount in common but large differences you could end up only partially being able to rely on a consistent interface. So you lose the flexibility of just operating on Person objects. If your language supports it traits might be a better way to go here. Also if you find yourself in this situation your classes might be getting too big. Try breaking them down. (Nobody likes a god object!)

But broadly speaking; Yes, go for it!


First let's take a quick look at what your code is doing. Your innermost line of code is:

n[i]<-sum(abs(a[i,]-b[j,])^2)

Due to how you've structured your for loops, this is run for times

with i=1 and j=1, setting n[1] to the squared distance between row 1 with a and row 1 of b,
with i=1 and j=2, overwriting n[1] to the squared distance between row 1 of a and row 2 of b,
with i=2 and j=1, setting n[2] to the squared distance between row 2 of a and row 1 of b, and
with i=2 and j=2, overwriting n[2] to the squared distance between row 2 of a and row 2 of b.

So we end up with n = c(34, 20), the squared distances between each row of a and the last row of b.

It seems most likely to me that you are trying to compute the distances between each pair of points (since your n is structured as a vector). In this case, check out what we accomplish with the following, much simpler code:

(a-b)^2
#      [,1] [,2]
# [1,]    4   16
# [2,]    4   16

The resulting matrix is the squared difference of each element in the two matrices. All we need to do is to sum up the rows:

n <- rowSums((a-b)^2)
n
# [1] 20 20

Or if we wanted the actual distance instead of the squared distance:

n <- sqrt(rowSums((a-b)^2))
n
# [1] 4.472136 4.472136

Note that we dramatically simplified the calculation; a nice side benefit is that this code is much faster than using a for loop in R.


Create new DataFrame containing mean information

pandas is deep

The pandas toolkit is very deep and can take a while to grok some of the basics.  In the case of your code, you are replicating the functionality in pandas.DataFrame.groupby().  groupby gathers all of the elements together with matching values in the fields indicated and then allows those elements to be worked on as a group.  In the case of:

for act in tuple(all_err.action.unique()):
    for filt in tuple(all_err.filt.unique()):

This code is referencing all values of the action and filt columns into two variables.  Then:

mean_val = all_err[(all_err.action == act) & (all_err.filt == filt)].val.mean()
mean_vals.append((act, filt, mean_val))

These lines perform a mean on the val column for each row that matches in the two grouped columns, and stores those results into a list.  Pandas has a shortcut for these sorts of operations called groupby.  The above four lines, plus the list initialization, can be equivalently written as:

all_err.groupby(['action', 'filt']).val.mean()

How?

Starting with:

all_err.groupby(['action', 'filt'])

produces a pandas.core.groupby.DataFrameGroupBy.  This object instance understands how to iterate over the dataframe a group at a time, allowing access to these groups in various ways.  So then,

all_err.groupby(['action', 'filt']).val

selects the val column to work against, and finally,

all_err.groupby(['action', 'filt']).val.mean()

asks for the mean value of that column.

Results:

mean_err = all_err.groupby(['action', 'filt']).val.mean()
print(mean_err)

  Action  Filt  Error
0    run   raw    1.5
1    run  deep    1.5
2   jump   raw    2.5
3   jump  deep    2.5


I have three points to make:

GetNumber:
The naming indicates it may be for integer conversion. While the truth is far from that, renaming might be a good idea.
Instead of having the function outside the main loop, have the max passed into PlayGame and simply do the operation in the game loop:
Function PlayGame([int]$max) {
    #...
    $secretNum = Get-Random -minimum 1 -maximum ($max + 1)
    #...
}
PlayGame 100

Multiline strings:
Instead of including new line characters in the string, use a multiline string, like so:
    Write-Host @"
This is a guess-the-number game.
You have 6 guesses to guess a number between 1 and 100.
"@

Note that any indentation you add ends up in the resulting string.

Type comparison
In PowerShell, as written in this Stack Overflow post:

On comparison a right value is converted to the type of a left value. Thus, '2' -gt 9 becomes '2' -gt '9', that is False, and '2' -gt 10 becomes '2' -gt '10', that is True.

This is a potential bug. I would recommend a helper method that converts the input to an integer, and tests its boundaries. Obviously in a number guessing game, 100000000 isn't a valid guess for a limit of 100. Not to mention the above type coercion.
If you don't really care about people putting in massive amounts because they're idiots, than you can just swap the order of the operators, and the guess will be coerced to an integer.


I personally would suggest using argparse.
To make a simple one would be fairly easy, and could only require add_argument.
You only really need to know:

The first to arguments store a single letter argument such as -d, and a long version such as --directory.
It takes an argument of action='store_true' to make the variable a flag.
It takes help as the help text.

And so you could use:

import argparse

parser = argparse.ArgumentParser(prog='main avg')
parser.add_argument('-d', '--directory',
                    help='path to any directory, the program will generate the dataset, or calculate the average in the folder.')
parser.add_argument('--cd', action='store_true',
                    help='creates gray scale dataset.')
parser.add_argument('--cdrgb', action='store_true',
                    help='creates rgb scale dataset.')
parser.add_argument('--avg', action='store_true',
                    help='calculates the alignment and average of the picture in greyscale mode.')
parser.add_argument('--avgrgb', action='store_true',
                    help='calculates and outputs a rgb average, needs the data from -avg.')
parser.add_argument('--graph', action='store_true',
                    help='calculates and outputs the movement and angles graphs, needs the data from -avg.')
parser.add_argument('-s', action='store_true',
                    help='sound at the end of process.')

args = parser.parse_args()
if args.cd:
    run_create_test_dataset(args.directory)
elif args.cdrgb:
    run_create_rgb_dataset(args.directory)
...
else:
    print('No options chosen')

After this, you may want to look into add_mutually_exclusive_group, if they're mutually exclusive.


An object for passing a chain of errors in JavaScript

Cosmetically speaking, you can use the ES6 destructuring operator to make your arguments a bit more readable:

class RError extends Error {
    constructor({name, message, cause}) {
        super(name, message);
        this.cause = cause;
    }
}

As for drawbacks, the only one I can think of is having to do it yourself instead of having the language do it for you (Like, in example, in Java, which is where I guess this is coming from). So you'll need to remember to do it every time you have a rethrown exception. Personally, I wouldn't find this acceptable, as I would surely forget.


Simplify Google places autocomplete address for loop

Use if.else if.else if ladder:
Instead of multiple conditions of if.if.if, you should use if.else if.else if ladder. The advantage of latter is that once a condition is met, the next conditions are skipped. But using only if.if.if means all the ifs are walked through irrespective of any if matching the condition. Read more about it here

It is generally a good idea to save repeating variables like place.address_components[i][componentForm[addressType]] in a variable to avoid its repetition and errors

Use object to store address avoiding all if conditions:
Use object to store the address. place.address_components[i][componentForm[addressType]] is common value to be store. The only thing changing is addressType. So, make addressType as key with value place.address_components[i][componentForm[addressType]]. This will skip the need for if conditions.

You will get address object of like:
{
  "street_number": "1600",
  "route": "Amphitheatre Pkwy",
  "locality": "Mountain View",
  "administrative_area_level_1": "CA",
  "postal_code": "94043"
}

And this can be printed where ever you want by using Object.key
If you want to preserve the naming conventions in code such as state instead of administrative_area_level_1 that can also be done by creating another mapping object

Updated Code

var place = {"address_components":[{"long_name":"1600","short_name":"1600","types":["street_number"]},{"long_name":"Amphitheatre Pkwy","short_name":"Amphitheatre Pkwy","types":["route"]},{"long_name":"Mountain View","short_name":"Mountain View","types":["locality","political"]},{"long_name":"Santa Clara County","short_name":"Santa Clara County","types":["administrative_area_level_2","political"]},{"long_name":"California","short_name":"CA","types":["administrative_area_level_1","political"]},{"long_name":"United States","short_name":"US","types":["country","political"]},{"long_name":"94043","short_name":"94043","types":["postal_code"]}],"formatted_address":"1600 Amphitheatre Parkway, Mountain View, CA 94043, USA","geometry":{"location":{"lat":37.4224764,"lng":-122.0842499},"location_type":"ROOFTOP","viewport":{"northeast":{"lat":37.4238253802915,"lng":-122.0829009197085},"southwest":{"lat":37.4211274197085,"lng":-122.0855988802915}}},"place_id":"ChIJ2eUgeAK6j4ARbn5u_wAGqWA","types":["street_address"]}

var componentForm = {
  street_number: 'short_name',
  route: 'long_name',
  locality: 'long_name',
  administrative_area_level_1: 'short_name',
  postal_code: 'short_name'
};

var addressObj = {}

for (var i = 0; i < place.address_components.length; i++) {
  var addressType = place.address_components[i].types[0];
  if (componentForm[addressType]) {
      addressObj[addressType] = place.address_components[i][componentForm[addressType]]
  }
}

console.log(addressObj)

document.getElementById('fullAddress').innerHTML = addressObj.street_number + ' ' + addressObj.route + ', ' + addressObj.locality + ', ' + addressObj.administrative_area_level_1 + ' ' + addressObj.postal_code;

The above json is copied from https://developers.google.com/maps/documentation/geocoding/intro
Hope it helps. Revert for any doubts.


Improve this function that compares jQuery versions

I'm sorry, but that code is completely broken.

It doesn't work if either version contains only two numbers such as the current "1.6".
It uses string comparison instead of integer comparison, so that it will return true if you "need" (theoretical) version "1.4.10" but only "1.4.2" is included, because "10" < "2" is true.
It doesn't stop comparing minor version numbers, if the major number is already bigger. For example it will return false if "1.4.2" is "needed", but "1.5.1" is included, because "2" > "1"
And finally you should keep in mind that "newer" isn't necessarily better. For example, the new 1.6 version changes how .attr() works, and scripts that rely on the old functionality of .attr() may break.


There is much wrong with your code..

You are iterating over every record to find a username, what happens if you have lots of users ?
You are not verifying the password !?
It seems like you are exposing the REST services with all the user id's and passwords !?
It seems that if the user knows the /loggedin URL, then the user can skip logging in since you do not validate loggedIn anywhere. Even worse, loggedIn is a variable local to LoginCtrl.

It is good to try and write everything yourself, it brings valuable experience. But please, use this only for throwaway websites.


Indeed the recommended approach for this (or at least what I would recommend) is to use callback interfaces.

The most important aspect though is designing this interface. In this case you should be sure that you're passing everything you need. However, I think you can rename the method to onOK or okPressed or similar. You might also consider returning a boolean to determine whether or not the dialog should really be closed (the input might need to be validated before it is accepted?) (there are ways to stop an AlertDialog from closing, you can find more information in earlier StackOverflow questions).

Other comments:

You should use String resources properly, this will separate the code from the text neatly. Use the resource id's instead.

alert.setTitle(R.string.alertInputTitle);
alert.setMessage(R.string.alertInputMessage);

Some strings are actually included in Android by default, such as android.R.string.ok and android.R.string.cancel, use them when possible.

When you want the negative button to just close the dialog, you can actually pass null as the DialogInterface.OnClickListener.

alert.setNegativeButton(android.R.string.cancel, null);


Congratulations. This was pretty readable code, by brainfuck standards. I was able to follow it.

By initializing your max cell to 256, you've made your code portable to work on non-wrapping brainfuck interpreters as well.  If you just wanted it to work on a wrapping brainfuck interpreter (i.e., one that works modulo 256), you could have left the cell set to 0.  I considered changing the program to just loop 256 times instead of comparing the character against the maximum value, but it turns out that 255 times is much easier than looping 256 times when the machine operates modulo 256.  You already have a comparison routine, so it's easy to just use it again.  (Though preferably not by copying and pasting the code — more on that below.)

While there may be simpler ways to print a number as a decimal string, you asked to optimize for readability, so I have opted to stay close to your original strategy.

The key to readability, I think, is to add strategic comments.  In brainfuck, any characters other than the eight commands (><+-.,[]) are ignored.  You can also add comments of the form [-][Any comment text goes here], with two caveats:

The pointer must be pointing to a cell containing 0 or a cell that you want to be zeroed.
Any character can appear inside, as long as square brackets occur in pairs.

The main goals of your comments should be to clarify:

What the intention of each chunk of code is.

It is not so important, in my opinion, to be able to follow every single micro-operation as long as you can understand the big picture.
What the memory layout is, and why it is laid out that way.

I've given "variable names" to all important cells.  There is also a long explanatory comment in the code below about what I call "frames", or groups of consecutive cells.
Which cell is the current one at any point in the program.

It's a relatively minor bug if a cell contains the wrong value.  However, if the pointer is not where it is supposed to be, the program blows up in a spectacular way.  Therefore, I have developed the notation to mark the current pointer location in parentheses at strategic points throughout the code.

You raise a valid concern about repeating the comparison routine three times in your code.  The way to handle that in brainfuck is to let the routine crawl along the cells, frame by frame, like a construction crew working on a section of highway.  Just put the appropriate signposts to let it know where to stop crawling.

[-][
    # See MEMORY_LAYOUT below for explanation of SETUP.
]
SETUP
    +++++ +++ [ -
        > +++++ ++      (Colon)
        > ++++          (Space)
        > +             (Newline)

        >>>>>>
        ++++ [ -
            > +++++ +++ (Loop_limit)
            <
        ]

        >>>>> >
        > +++++ ++      (C_limit)
        > +++++ +       (c_ascii)
        >>>>
        > +++++ ++      (X_limit)
        > +++++ +       (x_ascii)
        >>>>
        > +++++ ++      (I_limit)
        > +++++ +       (i_ascii)

        <<<<< <<<<< <<<<< <<<<< <<<<< <<<<
    ]
    > ++                (Colon)
    >> ++ >             (all_done)
    >> + >>>>           (Loop_limit)
    >> + >>>> ++        (C_limit)
    >> + >>>> ++        (X_limit)
    >> + >>>> ++        (I_limit)

    <<<<< <<<<< <<<<< <<<<< <<<<< <<<
END_SETUP

MEMORY_LAYOUT
[-][
    # Using Roman abbreviations i, x, and c for units, tens, and hundreds,
    # respectively.  Braces { } indicate a frame: a repeated consecutive group
    # of cells.  Cells named with Uppercase are constants.  At all times,
    # parentheses indicate the current cell; neighboring cells may be noted
    # as well in the code.

    # The main loop starts working from the last frame.  It compares the last
    # two cells in the frame, and performs a carry if applicable.  It then
    # proceeds to the preceding frame and repeats.  The first "frame" is
    # special, as noted by the first cell being 0.  The comparison routine
    # will not be performed on it.  However, when ascii reaches Loop_limit,
    # all_done will be incremented to 1, since it is located at the offset
    # within the frame where carrying would increment a value.

  { (0)  Colon=":"=58  Space=" "=32  Newline="\n"=10  not_all_done=0 all_done=0 }
  { Compare_frame_0=1  0 0 0  Loop_limit=256       ascii="\0"=0   }
  { Compare_frame_1=1  0 0 0  C_limit=":"=58       c_ascii="0"=48 }
  { Compare_frame_2=1  0 0 0  X_limit=":"          x_ascii="0"    }
  { Compare_frame_3=1  0 0 0  I_limit=":"          i_ascii="0"    }
]
END_MEMORY_LAYOUT

>>>>
+
WHILE (not_all_done) [ -
    # Print one line of output
    >>>>> >>>>> >>> .                   (c_ascii)
    >>>>> > .                           (x_ascii)
    >>>>> > .                           (i_ascii)
    <<<<< <<<<< <<<<< <<<<< <<<<< <<< . (Colon)
    > .                                 (Space)
    >>>>> >>>> .                        (ascii)
    INCR (ascii)
    +
    <<<<< <<< .                         (Newline)

    >>>>> >>>>> >>>>> >>>>> >>>>> >
    INCR (i_ascii)
    +

    <<<<<
    REPEAT (Compare_frame_?) [
        >>>>>
        IFEQ eq_flag=0 ascii_save=0 ?_limit (?_ascii)
            [ -
                < - < + >>
            ] ascii_save=ascii; diff=limit=limit MINUS ascii; ascii=0

            <<< + (eq_flag=1)
            >> [
                << - >> [ - > + < ]
            ]    ifneq { eq_flag=0 ; ascii=diff }; (diff=0)
            # Restore diff
            > [ - < + > ]
            # Restore limit and ascii
            << (ascii_save) [ - > + > + << ]
            < (eq_flag) [
            THEN
                [-][
                    # Perform carrying.  If ascii == Loop_limit, then
                    # "carrying" will set the all_done flag.
                ]
                <<< +
                >>>>> > ----- -----
                <<<
            ]
        END_IFEQ (eq_flag=0) ascii_save=0 ?_limit ?_ascii

        <<<<< <<<
        (Compare_frame_next)
    ]

    >>>>>
    BOOL_NEGATE_AND_CLEAR not_all_done=0 (all_done)
        < +
        > [ - < - > ]
        < (not_all_done)
    END_BOOL_NEGATE_AND_CLEAR
]

It is possible to reduce the frame size by one.  However, you would probably have to reuse the same cell for not_all_done and all_done, which would hurt readability.


Giveth me thy easier user input in C++ - follow up

Redefinition of default arguments

First of all, you cannot redefine default arguments. From [dcl.fct.default], slightly abridging the example:

  A default argument shall not be redefined by a later declaration (not even to the same value). [ Example:

void m() {
    void f(int, int);     // has no defaults
    f(4);                 // error: wrong number of arguments
    void f(int, int = 5); // OK
    f(4);                 // OK, calls f(4, 5);
    void f(int, int = 5); // error: cannot redefine, even to
                          // same value
}

  —end example ]

So the definition of get_input should just be:

template <typename TInput, typename TPrompt>
TInput easy_input::get_input(
    const TPrompt& prompt,
    std::istream& input_stream)
{ ... }

Otherwise, the code is ill-formed and should not compile (though apparently earlier versions of gcc happily allow this).

Include guards AND pragma

Given that you have include guards, it's unnecessary to additionally have the conditional #pragma once. If you don't want to unconditionally use the pragma, just stick with the include guards.

Default Prompt and Error

You use the error message in a single place - it'd be better to just write out the string literal there. It'll be easier to find when grepping through code, and there's no advantage I see in having the global constant.

With the default prompt, rather than all the additional complexity of default arguments, I think it'd be simpler to just add an overload:

template <typename TInput>
TInput get_input(std::istream& input_stream = std::cin);

template <typename TInput, typename TPrompt>
TInput get_input(const TPrompt& prompt,
    std::istream& input_stream = std::cin)
{
    std::cout << prompt;
    return get_input<TInput>(input_stream);
}

Ultimately though...

My biggest takeaway from 5gon12eder's excellent answer to your original question was that there really isn't anything easy about this input, and it may just be easier to avoid it and stick to being explicit with the streams.

Here's something this solution can't handle. Let's say I want to input two ints. I could write:

int a, b;
std::cout << "$";
std::cin >> a >> b;

And end up doing something like:

$42 15

And that works. But if I tried to do:

int a = get_input<int>("$");
int b = get_input<int>();

and tried to input both on the same line... I'd get a lexical error since "42 15" isn't an int. Maybe you're OK with that? But it's limiting and sadly not easy. Then again, nothing IO related in C++ is really easy anyway.


Make the Random generator a class-level member so you're not generating a new one with the default seed each time. Also, check your population for null:

/// <summary>
///  random number generator for the enumerable sampler.
/// </summary>
private static readonly Random random = new Random();

/// <summary>
///  returns a random sample of the elements in an IEnumerable
/// </summary>
public static IEnumerable<T> Sample<T>(this IEnumerable<T> population, int sampleSize)
{
    if (population == null)
    {
        return null;
    }

    List<T> localPopulation = population.ToList();
    if (localPopulation.Count < sample.size) return localPopulation;

    List<T> sample = new List<T>(sampleSize);

    while(sample.Count < sampleSize)
    {
        int i = random.Next(0, localPopulation.Count);
        sample.Add(localPopulation[i]);
        localPopulation.RemoveAt(i);
    }

    return sample;
}


You should use bufio.Scanner rather than a bufio.Reader. It won't make your code any faster, but it'll simplify things a bit.

One reason your code is slow is that you're converting every line from []byte to string. You can avoid this by using the raw bytes from scanner.Bytes() until you need to print the string. The regexp module provides functions for matching against []byte, so the translation is rather easy.

A secondary improvement would be to change your regexp to:

`^\[(TRACE|DEBUG|INFO|WARN|ERROR|FATAL) (\d{2}:\d{2}:\d{2})\]`

By anchoring your regexp and tightening the syntax it recognizes, you'll avoid scanning the entire log line when the regexp doesn't match. Use of raw strings (with backtick), means you don't have to escape the backslashes. This probably won't be a big speed improvement though, since most of your log lines should be single lines.

In terms of structuring the code, I'd probably separate out the part that parses the logs from the part that selects log entries and prints them out.

Perhaps something like this:

type LogReader struct {
    s bufio.Scanner
    ... some internal state
}

type LogEntry struct {
    level []byte
    timestamp []byte
    lines [][]byte
}

func (lr *logReader) Next() (LogEntry, error) {
    ... code here
}

func (lr *logReader) Done() bool {
    ... code here
}

This code should deal entirely in []byte and not string, for the reasons given before.

With this separation, the main code is much easier to read, and you have a nice LogReader abstraction that you can write unit tests for. Here's the main loop using a LogReader:

lr := NewLogReader(filename)
for !lr.Done() {
    e, err := lr.Next()
    if err != nil {
        ... handle errors
    }
    if isStampRelevant(e.level, e.timestamp) && isMessageRelevant(e.level, e.lines) {
        fmt.Println(string(bytes.Join(e.lines, []byte("\n")))
    }
 }


Flattening a List containing Empty Lists

In general flatten is intended to flat a tree, not a list, that is to collect all the non-null leaves of the tree in the same order as a classical traversal of the tree. See for instance the definition of flatten in the Racket Manual:

Flattens an arbitrary S-expression structure of pairs into a list. More precisely, v is treated as a binary tree where pairs are interior nodes, and the resulting list contains all of the non-null leaves of the tree in the same order as an inorder traversal.

With this characterization of the function, we do not see empty lists as something that must be “removed” from a list, and here is a simple definition that follows this idea:
(define (flatten tree)
  (let ((lst '()))
    (define (traverse subtree)
      (cond ((empty? subtree) '())
            ((cons? subtree)
               (traverse (car subtree))
               (traverse (cdr subtree)))
            (else (set! lst (cons subtree lst)))))
    (traverse tree)
    (reverse lst)))

The function visits the tree in the classical inorder traversal, and collects all the non-null leaves every time it finds one of them.
From this you can define a more optimized version, if you need to.


(Ab)using scanl to generate a random number stream, is there a better way?

If you just need to generate a stream of random numbers, you can use standard "randomRs" function from System.Random:

randomRs :: (RandomGen g, Random a) => (a, a) -> g -> [a]

Usage:

Prelude System.Random> newStdGen >>= print . take 5 . randomRs (1,10)
[2,10,10,3,5]

If you want to implement it by yourself, it can be done with unfoldr function instead of scanl. unfoldr allows to (surprise!) unfold a list from the seed value. Exactly what you need! Let's examine its type:

unfoldr :: (b -> Maybe (a, b)) -> b -> [a]

First argument is unfolding function and the second argument is the seed value. Unfolding function takes current seed value and produces Nothing or Just (a, b). Nothing means we should stop generating the list and Just (a, b) produces a new value of type "a" which is added to the genererated list and a new value of seed.

Since we want to produce a stream of random values, we never return Nothing from our unfolding function. Here is the code:

generateRandoms2 :: (RandomGen g, Random a) => a -> a -> g -> [a]
generateRandoms2 min max = unfoldr (Just . randomR (min, max))

randomR takes a range for generation and (implicitly here!) a generator and returns a random value and a new value of the generator. Informally:

randomR (1, 2) :: (RandomGen g) => g -> (a, g)

That's exactly what a typical unfolding function needs to do! So we just wrap this in Just and we are done.

Usage:

*Random Data.List> newStdGen >>= print . take 10 . generateRandoms2 1 10
[10,6,2,7,3,7,5,9,4,5]

In fact, we don't use the full power of unfoldr here (since we do not need to distinguish whether we should finish the generation or not), so we can even write a simpler recursive function:

generateRandoms3 :: (RandomGen g, Random a) => a -> a -> g -> [a]
generateRandoms3 min max g = x : generateRandoms3 min max g'
   where (x, g') = randomR (min, max) g

Here we just produce random number one by one in a lazy fashion.

Hope this helps!


The one thing that is immediately apparent to me is that parsing of command line arguments could be done better using the built-in argparse module but for an intro you can start with the tutorial.

Here is how the routine could be written, assuming all parameters are mandatory:

import argparse

# check command line options
parser = argparse.ArgumentParser()

parser.add_argument("--type", dest="type_of_payment", type=str, choices=["diff", "annuity"], required=True, help="Add some help text here")
parser.add_argument("--periods", dest="period_count", type=int, required=True)
parser.add_argument("--payment", dest="monthly_payment", type=float, required=True)
parser.add_argument("--interest", dest="credit_interest", type=float, required=True)
parser.add_argument("--principal", dest="credit_principal", type=float, required=True)

args = parser.parse_args()

# show the values
print(f"Type of payment: {args.type_of_payment}")
print(f"Periods: {args.period_count}")
print(f"Payment: {args.monthly_payment}")
print(f"Interest: {args.credit_interest}")
print(f"Principal: {args.credit_principal}")

This is more flexible, because:

you can provide parameters in any order
you can assign default values
you can specify the expected type and also restrict to a specific range of values - in this example type_of_payment must be either "diff" or "annuity"
you can also define your own function if you require more fine-tuned validation of certain parameters
you can easily define groups of mutually exclusive parameters

This is an example but you can customize it further. Then your code will become quite shorter = less distraction and more focus on the important stuff.


Convert a double list to a grouped string

Disclaimer: Edit
OP later specified that the numbers must be grouped only if they are adjacent. My code does not do that. It groups all equal elements. Please refer to @Xiaoy312's answer for that. I have my own solution, but as that is not a significant improvement over @Xiaoy312's I'm not posting it. To me, @Xiaoy312's answer is overkill for the specific use case the OP mentions, it promotes good programming practices and code reusability.
Old answer
What you're trying to achieve here is similar to Run-Length Encoding, a basic data compression technique.
Off the bat, I'll make a few observations:

You are repeatedly concatenating immutable strings. Usually not a great idea, as it involves large overheads in creating and discarding a whole lot of string objects. Use StringBuilder in the System.Text namespace instead, and append to it instead of doing repeated string concatenation.

Too much code in one function. Split it up! An obvious candidate is the part which counts the number of occurrences of a value in the list. Try to follow separation of concerns and the Single Responsibility Principle.

You do redundant counting for values. For a particular value, you should cache its count and not repeat the count for the same value again (memoization).

Try LINQ. It might even make this code into a one-liner. And be more idiomatic at the same time, if a bit slower.

You could do this better by doing the logic in 2 passes - in the first, you construct a map between the unique values in the list and their count, and in the second, you use this map to generate the string representation.

Ideally for comparing 2 doubles for equality, keeping in mind their error margins and inexact representation of certain values, you would check that the modulus of the difference of the 2 values was less than some small epsilon. However, for code like this, that's probably overkill.

Try to program as generic as possible. Use an IEnumerable<T> instead of a very specific List<double>, you will be able to use the resulting code for many different types of collections. This also involves typing to interfaces instead of concrete classes.

Using most of the above suggestions, herein follows the code (test fiddle here):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class RLEofList
{

    private static Dictionary<double, int> CountMap(List<double> input)
    {
        var countMap = new Dictionary<double, int>(input.Count);
        foreach(double item in input)
        {
            if(!countMap.ContainsKey(item))
            {
                countMap.Add(item, input.Count(x => x == item));
            }
        }
        return countMap;
    }

    public static string ListToRLEString(List<double> input)
    {
        var countMap = CountMap(input);
        var accumulator = new StringBuilder(input.Count);//rough guess for capacity of StringBuilder
        foreach(KeyValuePair<double, int> itemWithCount in countMap)
        {
            if(itemWithCount.Value > 1)
            {
                accumulator
                .Append(itemWithCount.Value)
                .Append("*");
            }
            accumulator
            .Append(itemWithCount.Key)
            .Append(" ");
        }
        return accumulator.ToString();
    }

    public static void Main()
    {
        var list = Console
            .ReadLine()
            .Split(',')
            .ToList()
            .ConvertAll<double>(x => Double.Parse(x));
        Console.WriteLine(ListToRLEString(list));
    }
}

The piece of code above will accept input as a single line of comma-separated numbers as per the example in the question. You might need to use Regex.Split for more complicated input patterns.
I'm not collapsing this code into a LINQ one-liner as I feel this is more educational.


generate a file name with format Name(Number).extension

There are a couple of improvements that I think you might make to this code.

Provide complete code to reviewers

This is not so much a change to the code as a change in how you present it to other people.  Without the full context of the code and an example of how to use it, it takes more effort for other people to understand your code.  This affects not only code reviews, but also maintenance of the code in the future, by you or by others.  One good way to address that is by the use of comments.  Another good technique is to include test code showing how your code is intended to be used.

Use standard functions where appropriate

Since you're already using the experimental/filesystem routines, why not make better use of them?  Here is a function called uniqueName which shows one way to do that using your current strategy:

fs::path uniqueName(const std::string &name) {
    fs::path possibleName{name};
    auto stem = possibleName.stem().string();
    auto ext = possibleName.extension().string();
    for (int i=1; fs::exists(possibleName); ++i) {
        std::ostringstream fn;
        fn << stem << "(" << i << ")" << ext;
        possibleName.replace_filename(fn.str());
    }
    return possibleName;
}

As suggested by the name of the function, this routine takes the name of a file and either returns it unaltered if no such file exists in the current directory or returns an altered filename as per your schema.  Be aware that after this function is called, some other process could create a file with the same name.

Create a list and use it

As shown in the review by @yuri, you could create a list and then use that.  It has the same potential problem noted above in that another process could create additional files after the list is created.  This version does not require any regex:

std::unordered_set<std::string> create_file_list()
{
    std::unordered_set<std::string> m{};
    for (const auto item : fs::directory_iterator{fs::current_path()}) {
        m.emplace(item.path().filename().string());
    }
    return m;
}

This creates a list of just the filenames (stripping the path).  This can then be used in a minor variation of the routine shown above.  Note that only a single line is different.

fs::path uniqueName(const std::string &name, const std::unordered_set<std::string> files) {
    fs::path possibleName{name};
    auto stem = possibleName.stem().string();
    auto ext = possibleName.extension().string();
    for (int i=1; files.find(possibleName.string()) != files.end(); ++i) {
        std::ostringstream fn;
        fn << stem << "(" << i << ")" << ext;
        possibleName.replace_filename(fn.str());
    }
    return possibleName;
}


Circle check algorithm optimization for directed graph

Avoid data duplication

You have:

a dictionary self.out_neighbour
a dictionary self.in_neighbour
a set self.nodes

all of which store essentially the same data in slightly different formats. You can avoid duplicating the input data by doing the following:

Combine out_neighbour and in_neighbour into a doubly-linked list. This would have the form:

{
  "3":  {
            "in":  [2],
            "out": [1, 4]
        },
  "4":  {
            "in":  [3]
            "out": []
        },
   ...etc
}

All nodes in the input would appear as keys in a single dictionary (eg. nodes_dict). This also means you can:

Eliminate the self.nodes list.

self.nodes is really only referred to once in your check_circle (or check_circle_v2) method, where you have:

        for root in self.nodes:

An equivalent list can be generated from the dictionary using:

        for root in self.nodes_dict.keys()

Time complexity:

Depth-first search for cycle detection in directed graphs is supposed to scale according to \$\mathcal{O}(n)\$, where n is the number of vertices. Yours scales with \$\mathcal{O}(n^2)\$, because the list self.being.visited is itself read through by if n in self.being_visited every time you visit a node.

If speed is an issue, it may be better to use a dict for self.being.visited. The code to check it is exactly the same, but works in \$\mathcal{O}(1)\$.

Your question 1:

The efficiency improvment gained by using check_circle_v2 can be no more than a constant factor of the original implementation. This is because depth-first search is still being called, which has a runtime of \$\mathcal{O}(n)\$ in this case.

Also, what this constant factor is, exactly, depends on the ratio of no-in-degree nodes to nodes having ins.

The best case is a graph of size \$n\$, with \$n - 1\$ nodes each having an edge pointing to them (the only visual example I can think of is like a chaos flag from Warhammer40K).

The worst case is the reverse of the chaos flag graph, with all arrows pointing inward. Your check_circle_v2 will call dfs on every node except the center node. So, in terms of time complexity and worst case performance, check_circle_v2 is no better than the original.

Your question 2:

The statement is true. Requiring the graph to have no in-degree nodes means that:

No. of nodes with (in > 0) \$=\$ No. of nodes

For the case of a non-cycle, one of its nodes lacks an in-pointing edge. So, by definition, for a non-cycle:

No. of nodes with (in > 0) \$=\$ No. of nodes \$- 1\$

The only way to have a non-cycle in the graph given the first requirement is to create an in-degree node for it without adding a new node. And the only way to do that is to connect the non-cycle to the existing graph, with one or more edges pointing to it from the graph. Then, the problem  must be restated in terms of the whole graph, which cannot be a non-cycle while fulfilling the first condition.

(Terminology note: It's cycle, not circle. Also, vertice is commonly used instead of node.)


Three-table queryset (full outer join?) in Django

EDIT Removed previous version which was pretty much telling you to use the second method shown above. Your first method loads everything from the database twice. (Django may do some caching, but still) I'm not sure why you are putting everything in the dictionary.

The problem with the current implementation is that sorting is done by python rather then in the database. It would be better to have the sorting done in the database. There are two ways I see of doing that.

You could have the three different sources sorted by the database. Then you could perform a merge pass to combine them. However, you are probably going to have need a lot of data before that becomes a worthwhile strategy. Sorting will be implemently in C for python which will make it have a speed advantage over a merge which you would have to implement in python.
You could redesign your database so Urls, Quotes, and Notes are stored in the same table. That way you could request the database to sort them.

Both of these strategies are really only helpful if you end up with a lot of data. If you have that much data you aren't going to want to present it all to the user anyways. The result is that its probably not useful.


Consider this case:

A: 1111111111
B: 1111111111
Query: n+1

The other post terminates very quckly. It knows that since both numbers end in 1, it must carry and thus doesn't have to scan throughout the entire string. On the other hand, your method tries to find clear bits but since there aren't any, it ends up going through the whole bitstring.

Probably, yours is faster over all, but this pathological case happens to have been put first in the test cases and so your program fails.

                if(carry && a.get(iter) && b.get(iter))
                {
                    carry = true;
                    value = 1;
                }
                else if((carry && a.get(iter)) || (carry && b.get(iter))  || (a.get(iter) && b.get(iter)))
                {
                    carry = true;
                    value = 0;
                }
                else if(carry || a.get(iter) || b.get(iter))
                {
                    carry = false;
                    value = 1;
                }
                else
                {
                    carry = false;
                    value = 0;
                }

This whole bit kinda hard to follow you could instead do something like:

int bits = a.get(iter) ? 1 : 0 + b.get(iter) ? 1 : 0 + carry ? 1 : 0;
value = bits % 2;
carry = bits / 2;

I think its a clearer

                int a_index = a.nextClearBit(number_bits - index);
                int b_index = b.nextClearBit(number_bits - index);

Yeah, not gonna fly. nextClearBit has to scan through the bitset bit by bit in order to do that. You are gonna need to come up with a way to look through the bits without looking at each individual bit. I've solved this problem, and I used a specialized data structure to do it.


A few small comments:

When not supplying any argument to an exception, the () can be skipped: raise NotImplementedError.
I'm assuming ParseFactory is actually longer, otherwise there would be no need for it to be a class. A parse function on its own would be perfectly fine.
It would be better to make a dictionary of parsers, especially if the number of parsers grows:

class ParserFactory(object):
    parsers = {"Unitedexchange": UnitedexchangeParser,
               "Dubai": DubaiParser}

    def parse(self, source):
        parser = ParserFactory.parsers[source.name]
        return parser.get_response()

This does not solve the problem of having to implement every parser.

If they are sufficiently similar, namely almost all have the same get_response, you could just put a standard implementation in BaseParser and use

class DubaiParser(BaseParser):
    pass

Or, you could use type to create those classes that do not need any modifications dynamically and put them in a dictionary:

parsers = {"DubaiParser": None, "OtherParser": None, ...}
for name in parsers:
    parser = type(name, (BaseParser,), {})
    parser.__module__ = __name__
    parsers[name] = parser

There is even the possibility to override some methods, but this only makes sense if there are two different implementations of get_response

def get_response_json(self):
    headers = self._prepare_auth_header()
    response = requests.get(self.url, headers=headers)
    return response.json()["data"]

other_methods = {"get_response": get_response_json}

parsers = {"DubaiParser": None, "OtherParser": None, ...}
different_get_response = {"OtherParser", ...}
for name in parsers:
    if name in different_get_response:
        parser = type(name, (BaseParser,), {}, lambda ns: ns.update(other_methods)
    else:
        parser = type(name, (BaseParser,), {})
    parser.__module__ = __name__
    parsers[name] = parser


Reducing code with two identical objects

You can combine the two identical assignments at the end into the same line rather than recalculating the same value again:

for (var i = 0; i < ticks.length && i < ticks2.length; i++) {

    var tick = ticks[i];
    var tick2 = ticks2[i];
    var tickCenter = tick.offsetWidth / 2
    var offset = (i - currentTick) * pixelsPerTick;
    var percent = 1 - (Math.abs(i - currentTick) / 10);

    tick.style.top = tick2.style.top = ((offset + center - tickCenter) | 0) + "px";
    tick.style.opacity = tick2.style.opacity = percent;
}

I also fixed your condition in the for loop so that it safely checks both .length values.

You could also avoid some of the intermediate values, but I personally would preserve them the way you have them because of the readability/understandability advantage of having it this way vs. putting them all in one giant formula.


Why do you use BitSet? You're not saving kilobytes of memory by representing your printers as a set of bits. Go for code clarity and represent each printer with a dedicated instance of Printer class that holds the allocation status of the printer in question. Store them in a List or other data structure that suits your needs. You could even have two LinkedLists for allocated and free printers and just pop the first from the free list and push it to the end of the allocated list. You need to synchronize anyway. No need to count sizes to see if there are available printers. Just check isEmpty().

bitSet is a bad name for printer reservartion status. Java is a strongly typed language so you don't need to repeat the type in the field name. Use printerAllocationStatus if you really need to use a BitSet (see above).

You're stacking on responsibilities by implementing hasEnoughPrinters in your allocator. Instead implement getNumberOfFreePrinters and let the caller decide what to do with the information. The method is of limited use anyway, since printer allocation status might change right after the caller has checked for availability making the data the caller has completely useless.

getNextPrinter does not communicate the side effect of changing printer allocation status in it's name. Use allocatePrinter instead.

Use consistent terminology. In a PritnerAllocator a printer is allocated or free, not assigned.


Selection algorithm using merge sort and IEnumerable

I was able to get a performance increase (500 ms in average instead of 600 ms) by splitting the code in two methods : one that return a sequence with a single element, one that merge IEnumerables. I think this is faster because the implementation of the yield return statements is simpler for the compiler (AFAIK it is done using a state machine) .

public IEnumerable<T> MergeSort<T>(List<T> list, int left, int right, Comparer<T> comparer)
{
    if (left == right)
    {
        return SingleValue(list[left]);
    }

    int mid = (left + right) / 2;
    var firstEnumerable = MergeSort(list, left, mid, comparer);
    var secondEnumerable = MergeSort(list, mid + 1, right, comparer);
    return Merge(firstEnumerable, secondEnumerable, comparer);
}

public static IEnumerable<T> SingleValue<T>(T value)
{
    yield return value;
}

public static IEnumerable<T> Merge<T>(IEnumerable<T> firstEnumerable, IEnumerable<T> secondEnumerable, Comparer<T> comparer)
{
    using (var firstEnumerator = firstEnumerable.GetEnumerator())
    using (var secondEnumerator = secondEnumerable.GetEnumerator())
    {
         //same as before
    }
}

Performance can be improved further by checking the range of elements to sort inside MergeSort method. Above a certain threshold, another sort can be used (eg : InsertionSort or SelectionSort)

public IEnumerable<T> MergeSort<T>(List<T> list, int left, int right, Comparer<T> comparer)
{
    if (right - left <= threshold)
    {
        return SelectionSort(list, left, right, comparer);
    }

    //...
}

I think SelectionSort is a good candidate because there is a way to implement it lazily : it can return the smallest number very early without having to sort the whole list  (eg: using a yield return). The partial merge sort now take about 15 ms to get the first 50th smallest numbers out of 1M integers.


Your Queue_Stack only works with Integer.  I tried using String and got a ClassCastException.

I made some modifications to your code.  I added a test to make sure you don't get an error if you dequeue more than you enqueue.  I used the underlying iterator to get rid of the temp Stack.

import java.util.Iterator;
import java.util.Optional;
import java.util.Stack;

public class Queue_Stack<T> implements Iterable<T> {

    Stack<T> stack = new Stack<>();

    public void enqueue(T item) {
        stack.push(item);
    }

    public Optional<T> dequeue() {
        if (!stack.empty()) {
            T item = stack.iterator().next();
            stack.remove(0);
            return Optional.of(item);
        } else {
            return Optional.empty();
        }
    }

    @Override
    public Iterator<T> iterator() {
        return (Iterator<T>) stack.iterator();
    }

    public static void main(String args[]) {
        Queue_Stack<String> qs = new Queue_Stack<>();

        qs.enqueue("zeta");
        qs.enqueue("alpha");
        qs.enqueue("beta");
        qs.enqueue("gamma");

        Iterator<String> iter = qs.iterator();
        while (iter.hasNext()) {
            System.out.println(iter.next());
        }

        System.out.println(qs.dequeue());
        System.out.println(qs.dequeue());
        System.out.println(qs.dequeue());
        System.out.println(qs.dequeue());
        System.out.println(qs.dequeue());
    }
}


Prefer enum class with a name, and probably rename the enums, e.g. BOOL_TYPE becomes TYPE::BOOL or however you'd like to do the naming. Why do you want them to be an unnamed enum type?

You're printing an error to cout when there's an error. At the very least, print to cerr, so that someone running this might be able to filter output if they'd like to. I would personally either return a variant<variable, error_message_type> or throw an exception if there's an error like that though. Otherwise errors could easily slip through unnoticed.

This validation loop is inefficient and confusing.

Prefer extracting the conditional if possible. It appears to be ensuring that all the types are the same if it is a "VARIABLE_" type. Perhaps "variadic" may be more appropriate?

    int idx = 0;
    for (auto i : list)
    {
        if ((indexes[0] & 0x80) ? i.index() != (indexes[0] & 0x03), idx++ : i.index() != indexes[idx++])
        {
            std::cout << "Invalid argument type" << std::endl;
            return variable(0);
        }
    }

indexes could take a type enum instead of int if it had a name. Why is it called indexes? You may be able to leverage more of std::variant and possibly not need the enum at all.

for (auto i : list) You probably want auto & i, since this will result in string copies in the case of string variants.

return fn(std::forward<decltype(list)>(list));
This is unnecessary. std::forward is useful for templated types, but you know the type of list, it's a variableList &&.

You could move the list, but I'm not sure that's what you really want.


Splitting each word using longest substring that matches in a vocab/dictionary

The following is a basic syntax and usage pass without looking into your algorithm in too much depth. I'll first show the suggested code and then highlight significant differences where it can offer improvements on the original.

#!/usr/bin/env python3

from typing import Iterable, Set

def tokenize(
    text: str,
    vocab: Set[str],
    max_input_chars_per_word=10,
    unk_token="[UNK]"
) -> Iterable[str]:
    for token in text.split():
        n = len(token)
        if n > max_input_chars_per_word:
            yield unk_token
            continue

        start = 0
        sub_tokens = []

        while start < n:
            end = n
            cur_substr = None
            while start < end:
                substr = token[start:end]
                if start > 0:
                    substr = "##" + substr
                if substr in vocab:
                    cur_substr = substr
                    break
                end -= 1
            if cur_substr is None:
                yield unk_token
                break
            sub_tokens.append(cur_substr)
            start = end
        else:
            yield from sub_tokens

def test():
    vocab = {"the", "go", "##es", "to", "eat", "pum", "##pkins", "of", "##gos", "##stein"
             "#400", "1", "boredom", "##folk", "man",
             "##go", "out", "folks", "##0",
             "un", "##aff", "##able"}

    s = "the unaffable folks goes to eat 1400 folkspkinsgosgo pumpkins and 10 mangos out of boredom"

    result = tokenize(s, vocab)
    assert list(result) == [
        'the',
        'un',
        '##aff',
        '##able',
        'folks',
        'go',
        '##es',
        'to',
        'eat',
        '[UNK]',
        '[UNK]',
        'pum',
        '##pkins',
        '[UNK]',
        '1',
        '##0',
        'man',
        '##gos',
        'out',
        'of',
        'boredom']

test()

Rationale

Add PEP484 type hints such as Iterable for the return of your function.
Require that you accept a set for vocab. All you do on it is membership testing, i.e. if substr in vocab:, so a set will be much faster.
Make this function a generator and yield instead of building up an output_tokens list.
Store the token length in a variable such as n, considering you need it so often.
Do not split out token into chars, and do not call join. All you need is the token string.
Do not track an is_bad flag. Use a for/else to detect whether a break occurred.
Use your test data in actual assert tests.


Let's proceed top-down.  I'll suggest cleverer ways to do a bunch of stuff, but the review will culminate in the two most important points:

this is a huge security hole, and
you should organize your code into functions.

Introductory message

A good way to write a multiline string is using """the longstring""" syntax.

In this case, though, that introductory message could just as well be the docstring for the program.  You could use the special __doc__ variable to fetch the docstring by introspection.

"""This program reads boolean expressions and outputs the truth table.
You may use the operators ~, ∨, and ∧, or
their text equivalents, not, or, and and.
You may use parenthesis, braces, and brackets to group your expressions,
and you must use single letters for your variable names, as 'a∨b'"""

print(__doc__)

Note that "equation" is not the right word: you don't want a string with an equals sign.  You just want a "boolean expression".

Canonicalizing the expression

Doing string substitutions in multiple passes is bad practice.  In the best case, you waste a bit of effort making multiple passes over the string.  In the worst case, passing previously substituted text through another round could be incorrect — though fortunately that's not the case here.

I recommend doing the substitution in one pass using a regular expression.

REPLACEMENTS = {
   '~': ' not ',
   'v': ' or ',
   '^': ' and ',
   '[': '(',
   ']': ')',
   '{': '(',
   '}': ')',
}
expr = re.sub('|'.join(re.escape(sym) for sym in REPLACEMENTS.keys()),
              lambda sym: REPLACEMENTS[sym.group(0)],
              expr).strip()

Extracting the variables

This, too, can be simplified using a regular expression.  \b[A-Za-z]\b looks for any single letter bounded by word boundaries on both sides.

In addition, deduplication is more elegant using a set.

vars = sorted(set(re.findall(r'\b[A-Za-z]\b', expr)))

Generating the boolean values

This is a perfect job for itertools.product(), and in fact the documentation has just such an example:

product(range(2), repeat=len(vars))

Evaluating the expression

First of all, you should use eval(), not exec(), since you want to evaluate the expression and get its value, rather than running some code for its side-effect.  result = eval(expr) is better than exec('result = ' + expr).

Most importantly, though, calling eval() or exec() like that is a huge security hole — it lets the user execute arbitrary Python code.  For example, the user might enter this to be executed:

__import__('os').system('rm -rfi /')

The consensus is that Python can't be safely sandboxed.  We can try to do better by evaluating the expression in a scope that contains just the relevant variables.

NO_GLOBALS = {'__builtins__': {}}
for vals in product(range(2), repeat=len(vars)):
    locals = dict(zip(vars, vals))
    result = eval(expr, NO_GLOBALS, locals)

However, I suspect that even that won't be secure.  The lambda keyword is still available, for example, and that makes me nervous.  Ultimately, the only safe approach may be to write your own code to parse and evaluate the expression — Python itself is too powerful.

Suggested solution

As a final remark: the problem can be divided neatly into functions.  Naming these chunks of work make your code easier to follow.

"""This program reads boolean expressions and outputs the truth table.
You may use the operators ~, ∨, and ∧, or
their text equivalents, not, or, and and.
You may use parenthesis, braces, and brackets to group your expressions,
and you must use single letters for your variable names, as 'a∨b'"""

from itertools import product
import re

def canonicalize(expr):
    REPLACEMENTS = {
       '~': ' not ',
       'v': ' or ',
       '^': ' and ',
       '[': '(',
       ']': ')',
       '{': '(',
       '}': ')',
    }
    return re.sub('|'.join(re.escape(sym) for sym in REPLACEMENTS.keys()),
                  lambda sym: REPLACEMENTS[sym.group(0)],
                  expr).strip()

def extract_variables(expr):
    return sorted(set(re.findall(r'\b[A-Za-z]\b', expr)))

def truth_table(expr):
    expr = canonicalize(expr)
    vars = extract_variables(expr)
    NO_GLOBALS = {'__builtins__': {}}

    # Print header
    print('\t'.join(vars + [expr]))

    # Print body
    for vals in product(range(2), repeat=len(vars)):
        locals = dict(zip(vars, vals))
        result = eval(expr, NO_GLOBALS, locals)
        print('\t'.join([str(v) for v in vals] + [str(result)]))

def prompt_expr():
    print(__doc__)
    print()
    return input()

if __name__ == '__main__':
    truth_table(prompt_expr())


Find the best way to get the max sum in a list where sum of three consecutive elements is not allowed

I don't fully understand your code, but it seems to look only at the presence or absence of the previous element when deciding whether to include the current one, so I constructed a test case and verified that it fails:

print(get_max_sum(6, [10, 10, 0, 0, 10, 10]))

should give 40 but actually gives 30.

    if main_output[0]['sum'] >= main_output[1]['sum']:
        return main_output[0]['sum']
    else:
        return main_output[1]['sum']

doesn't need to be so verbose. Prefer

   return max(main_output[0]['sum'], main_output[1]['sum'])

    output_1 = {'skip': True, 'sum': input[0]+input[1], 0: input[0], 1:input[1]}
    output_2 = {'skip': False, 'sum': input[0]+input[2], 0: input[0], 2:input[2]}

What you need to keep track of is the current run length, which can be from 0 (previous element not included) to 2 (both previous elements included, so must skip this one). The requirement to include the first element means that a special case is required at the start. So we get

    best_by_run_length = [float('-inf'), input[0], float('-inf')]
    for elt in input[1:]:
        best_by_run_length = [
            # Run length 0: don't include elt, preceding run length is unconstrained
            max(best_by_run_length),
            # Run length 1: include elt, preceded by run length of 0
            elt + best_by_run_length[0],
            # Run length 2: include elt, preceded by run length of 1
            elt + best_by_run_length[1]
        ]
    return max(best_by_run_length)

print(get_max_sum(8, [3, 2, 3, 2, 3, 5, 1, 3]))
print(get_max_sum(5, [10, 3, 5, 7, 3]))
print(get_max_sum(5, [10, 1, 5, 7, 3]))
print(get_max_sum(5, [1, 2, 3, 4, 5]))

How do you know that the output is correct? Rather than print the result, it's much more useful to print the comparison of the result against the expected value. Or you could take the testing up a level by using doctest (Python 2, Python 3).


Memory management

Right now, your code leaks memory--every time you call reverse, it allocates some memory, and none of your other code frees it again.

If all you're doing is reversing one string, then exiting, that's of little consequence--but if you try to use this in real code, leaking memory like this is generally unacceptable.

const correctness

Since you're not modifying the input string, you might as well use const in the function's signature:

char* reverse(char const * str);

Buffer overrun protection

Right now you have:

scanf("%s", word);

This is essentially equivalent to gets(word);. That is to say, it provides absolutely no protection against the user entering a string longer than you've provided space to store. When using %s with scanf (or cousins like fscanf, sscanf, etc.) you need to specify the maximum length:

scanf("%49s", word);

Alternatively, consider using fgets, which also requires you to specify the buffer size.

Note that there's a difference in the size you specify though. With fgets, you specify the size of the buffer, but with scanf you specify the number of characters it's allowed to read, which is one less than the size of the buffer itself. Also note that fgets normally retains the \n at the end of what was entered though (if you get data without a \n on the end, it means the buffer you supplied wasn't large enough to hold all the data that was entered).

Unnecessary Cast

When you allocate your memory:

char* reversed = (char*) malloc(sizeof(char) * strlen(str) + 1);

...you're currently casting the result of malloc. This is generally frowned upon by C programmers--it's unnecessary and it can cover up a bug of having failed to include the right header to declare/prototype malloc correctly.

char* reversed = malloc(sizeof(char) * strlen(str) + 1);

The cast is only necessary if you decide to write C that can also compile as C++. This tends to give the worst of both worlds, and should generally be avoided--if you're going to compile this as C++, use std::string and std::reverse.

Size Computation

When you allocate memory, you multiply the length by sizeof(char), but sizeof(char) is guaranteed to be 1, so the multiplication accomplishes nothing.

Even if you decide to leave the multiplication in (since it is necessary for types other than char), I prefer to use code like this:

char* reversed = malloc(sizeof(*reversed) * strlen(str) + 1);

This has the advantage that when/if you (for example) decide to support wide characters, you can change it to something like:

wchar_t* reversed = malloc(sizeof(*reversed) * (wcslen(str) + 1));

...so the argument to sizeof doesn't need to change at all (and believe me--if you decide to do something like this, you have enough headaches to deal with, so even a small help will be welcome).


You should the parameters for null values. strlen() does not tolerate nulls passed to it as parameters.

I'm not a complete zealot when it comes to the use of goto, but I'm not sure what benefit it gives in this code. Replacing the gotos with return true or return false would make the code a little simpler and more readable.

  if (no_match) {
       goto no_match;
  }

compared to this for example:

  if (no_match)
     return false;

For the bug you mentioned you will need to compare the lengths of the first and second wildcard parameters IF there is no special character in the second parameter.


condition a > 0 and b > 0 or a < 0 and b < 0 often can be write as a * b > 0 (it depends on multiply cost)
maybe you can use it something as total_win = ( poss_diff.Points * poss_diff.Total > 0 )
len(poss_diff[total_win]) can be translate to total_win.sum()


How to shorten two methods with the same theme?

Something along these lines:

private void ValueChangedImp(object sender, EventArgs e, Func<int> get_value, Action<Layer, int> set_value, Func<int> get_pixels)
{
    if (ignoreChange) return; // Sometimes I only want to change the value without the event firing (like when undo-ing)

    PushUndoStack();

    int value = get_value();

    foreach (Layer layer in doc.TileLayers)
        set_value(laywe, value);

    hscDoc.Maximum = get_pixels();
    ClampScrollbarValue(hscDoc);
}

private void numWidth_ValueChanged(object sender, EventArgs e)
{
    ValueChangedImp(sender, e, () => (int)numWidth.Value, (layer, value) => layer.Width = value, () => doc.WidthInPixels);
}

private void numHeight_ValueChanged(object sender, EventArgs e)
{
    ValueChangedImp(sender, e, () => (int)numHeight.Value, (layer, value) => layer.Height = value, () => doc.HeightInPixels);
}


Removing checks that are only necessary on first pass

You can remove the check for RawBody.IsNull() because you don't read the value of RawBody inside this method. If at the other places where some similiar code is called the rawBody is read, I would suggest to extract this check to a separate method and setting a flag which indicates that the check is already done. Like

private bool _bodyValueIsAssigned = false;
private void AssignValueIfNotAssigned(Frame frame)
{
    if (_bodyValueIsAssigned) { return; }

    if (RawBody.IsNull())
    {
        RawBody = frame;
    }

    if (FilteredBody.IsNull())
    {
        FilteredBody = frame;
    }

    _bodyValueIsAssigned  = true;
}

This can be called for the point where you call the Update() method. Without seeing more about how this is called (from an event or something similiar) there can't be said more.

If Filters is a List<T> or an array you should use performance wise a for loop instead of a foreach. See: https://stackoverflow.com/a/365658


So although this code is a small example it still can benefit from having a few methods sprinkled in to help clear up what you are trying to do. For instance what if you decided that you wanted to change the max and min to be entered in as a parameter to starting the program? You would have to change max and min to be equal to the int value of args[0] and args[1] although for a small example that is not a big deal, but it would be a little more simple to of had a method similar to this.

//gets deafult secret number
private int getSecretNumber(){
    int MIN = 1;
    int MAX = 100;

    return  getSecretNumber(MAX, MIN);
}
//converts a string variable to a int and returns a secret number
private int getSecretNumber(String strMax, String strMin){
    //code to convert string to int
    getSecretNumber(max, min);
}
//generates a secret number between 2 numbers
private int getSecretNumber(int MAX, int MIN){
    return random.nextInt(MAX - MIN + 1) + MIN;
}

Now you will have saved your self some time in the future if/when the design requirements change. if implemented correctly you might not have any code changes.. say something like this.

public static void main(String args[]) {
    int comp = getSecretNumber(); //gets default
    if (args.length == 2)
        comp = getSecretNumber(args[0], args[1]);

    //etc...

It is always a good idea to seperate your business code and your logic code. The business code is the rules for your code. "Do A,B,C give me D, and Save to E" the logic code is the nitty gritty details of how to do A-E. Everyone calls it something different but the concept is always the same. Keep the two as seperate as possible it makes changing your code easier.

Those are my tips. Other than that I don't see any problems with your code other than you don't have a way to escape the loop other than guessing the correct number.


Use the -u and -e switches to bash, when possible, because they help to find bugs.  Your log decoder may return errors that you want to ignore (making -e unhelpful) but -u is safe here.

Move the echo … exit pattern into a function:

die() { printf "%s\n" "$@" >&2; exit 1; }

Quotes aren't needed on the left-hand side of [[ tests.  For "die unless foo" assertions, consider writing them in the form foo || die "test failed":

[[ -z $cmd ]] && die "…"

(( $# == 1 || $# > 2 )) is more clearly written in the affirmative, as (( $# == 0 || $# == 2 ))

Collect the file list once instead of twice.  Use globbing instead of find to get proper handling of filenames with spaces in them.  Include contributing variables in error messages.  Do a cd first so that you don't need to decompose filenames later.  (( expressions return false when zero so there's usually no need to explicitly write x == 0:

shopt -s nullglob
pushd "$log_dir" || die "can't chdir to $log_dir"
declare -a in=( *.cbllog )
popd || die "can't return to original working directory"
(( ${#in} )) || die "no cbllog files in $log_dir"
…
for path in "${in[@]}"; do

Your command detector has a bug if cbl-log is an alias or function.  Use type -P instead.  If you truncate using read instead of head, the assignment doubles as an emptyness test:

read cmd < <( type -P {./,}cbl-log ) || die "cbl-log should be in PATH or current directory"

Your basename implementation has a bug if log_dir contains globbing characters.  This is obsoleted by the pushd above but something like file=${path##*/} or file=$( basename "$path" ) would work.

mkdir -p is a no-op when the directory exists; no need to test for that yourself.

You'll need execute permission on $out_dir along with write.

Putting it all together:

    #!/bin/bash -u
    die() { printf "%s\n" "$@" >&2; exit 1; }

    (( ${#} == 0 || ${#} == 2 )) || die "Usage:    cbldecode"  "          cbldecode <source directory> <target directory>"
    log_dir=${1:-$PWD}
    out_dir=${2:-"decoded"}
    read cmd < <( type -P {./,}cbl-log ) || die "cbl-log should be in PATH or current directory"

    pushd "$log_dir" || die "can't chdir to $log_dir"
    shopt -s nullglob
    declare -a in=( *.cbllog )
    popd || die "can't return to original working directory"
    (( ${#in} )) || die "no cbllog files in $log_dir"

    mkdir -p "$out_dir"
    [[ -w $out_dir && -x $out_dir ]] || die "insufficient permissions on $out_dir"

    for basename in "${in[@]}"; do
        "$cmd" logcat "$log_dir/$basename" "$out_dir/${basename%.cbllog}"
    done

For faster conversion, add & to the cbl-log invocation, or adapt the script to send basenames to xargs -P.


Several suggestions:

You could use MemoryCache class as of .NET 4, which would free you from System.Web assembly.
You should use lock in order to put the value to cache.
You should take care of value types objects

So, here is the code:

public static T GetCached<T>(string key, Func<T> initializer, TimeSpan slidingExpiration, DateTime absoluteExpiration)
{
    var httpContext = HttpContext.Current;

    if (httpContext != null)
    {
        key = string.Intern(key);
        lock(key) // locking on interned key
        {
            var obj = httpContext.Cache[key];
            if (obj == null)
            {
                obj = initializer();
                httpContext.Cache.Add(key, obj, null, absoluteExpiration, slidingExpiration, System.Web.Caching.CacheItemPriority.Default, null);
            }
            // taking care of value types
            if (obj == null && (typeof (T)).IsValueType)
            {
                return default(T);
            }
            return (T)obj;
        }
    }
    else
    {
        return initializer(); // no available cache
    }
}


If you want to look for a particular chunk of bytes in a PNG file, don't scan it naïvely byte by byte.  Not only would it be slow, you could also accidentally match some data that happens to look like a marker.

Instead, you should take advantage of the chunk layout described in the PNG specification.  Each chunk is tagged with a type and a length.  If the tag is of a type that you are not interested in, seek ahead to the next chunk — the length field will tell you how many bytes to skip.


Logger which writes asynchronously to file from multiple threads

To make a fast logger you need to

minimise the use of mutexes.
detach the logging from the writing.
minimise copying
pre-alloc the buffers

no allocs during logging.

make a ring buffer

fixed number of logging
decide what should happen if its full
slightly slower using a dequeue with minimises allocs

use atomic indexes to the (ring) buffer

if your system if fully loaded, you have to log often use the cv or wait until the logger thread gets scheduled by the OS.
use the cv.notice_one where the index is updated if the front is sufficient in front of the back end.

So a few of the things that are issues

The logger_thread blocks further logging while it writes.
The write blocks other threads logging

Running into a mutex that is taken causes a task switch, typically  taking from 1000ns to 22000ns on a x86, nearer the lower if its started again on the same hardware thread.


Improving a filter array function, to match elements and conditions

It looks like you're using TypeScript, so a couple pointers:
Avoid any - any effectively disables type-checking, which defeats the whole point of using TypeScript. Better to type variables properly:
let filtered: Array<{ name: string; condition_matrix: Array<number>; }> = [];

Or, even better, let TypeScript infer the types automatically when possible, by transforming the data all at once with array methods (see below). No need to denote types when TS can infer them automatically - eg let isPossibility: boolean = true; can be just let isPossibility = true;
Avoid let, prefer const - see here, using const indicates that the variable will never be reassigned, which reduces cognitive overhead.
Semantic array methods While for loops work, they should only be used as a last resort, when other array methods aren't appropriate. Here, rather than breaking inside a for loop to check if an item passes a condition, it would be better to use .some. Also, rather than .pushing to an outer array based on the condition, use .filter instead:
this.outputArray = originalArray.filter(
  item => this.selectedConditions.some(
    condition => item.condition_matrix.includes(condition)
  )
);

(Better to use .includes than to use an indexOf check against 0)
Or:
Time complexity If performance is an issue and you have a lot of conditions, rather than iterating all of the conditions each time, consider constructing a Set of all of the selectedConditions first; Set#has is O(1), which is an improvement over iterating over arrays with O(n).
const allConditions = new Set(this.selectedConditions);

Conditions list typo? The data structure in your code is an array of objects, but you're using item.condition_matrix.indexOf( this.selectedConditions[i] ). Did you mean:
item.condition_matrix.indexOf( this.selectedConditions[i].id )

?
(If so, and you want to use the Set method, map to IDs first:
const allConditions = new Set(
  this.selectedConditions.map(({ id ]) => id)
);

)
Side-effects? The result of the whole function is to set a value (to this.outputArray). Is this required? Code is generally easiest to understand and reason about when it does not contain side-effects. Imagine seeing some code you haven't looked at before, and compare:
this.calculate();
console.log('Result:', this.result);

vs
console.log('Result:', this.calculate());

The second is easier to make sense of because the method is used for its return value instead of for its side-effects (which, to understand, you'd have to read through the method to find).
Could your filterElements method return a value instead of assigning to this.outputArray?


Find words that can be typed using only one row on a keyboard

Algorithm/Data Structure Selection

Right now you're using three separate sets, one of the letters in each row of the keyboard.

Instead, I think I'd use a single map from character to row number. Then you can start with the first character in the string, and find what row it's on. Then you check whether the other characters in the word are in the same row. Right now, you may look up each character in a string up to three separate times. Changing the algorithm, you can reduce that to (at most) one lookup per character.

Since you have a dense set of inputs for the map (i.e., only ASCII letters) I'd probably also use an array/vector for the mapping, instead of an unordered map. A vector/array will avoid hashing, and it's a dense structure, so you get really good cache utilization.

Avoid Unnecessary Transformations

It's probably simpler to create a mapping for all possible input characters instead of using toupper on the input, then looking it up in the set/map.

Consider other Looping Constructs

Instead of a loop like this:

            size_t wordSize = words.size();
            for(size_t j = 0; j < wordSize; ++j)
            {
                if(row.find(toupper(words[j])) == row.end())
                {
                   return false;
                }
            }

I'd probably use a loop more like this:

for (auto c : words[j] )
    if (row.find(c) == row.end())
        return false;
return true;

...or you could use a standard algorithm instead:

return std::all_of(words[j].begin(), words[j].end(),
    [&](char c) { return row.find(c) != row.end(); });

(Though, as outlined above, I'd rather change it to look up the row number instead).


Decouple everything.

I am certain that Scala veterans will have more to say than I do and possibly comment over my comments.

Here are some changes and respective comments to your code.

import dispatch._, Defaults._

// decouple from any "master" class.
case class Track(name: String)

case class Album(name: String)

case class Artist(name: String)

// This is called a structural type. We are using a def
// because a val restricts extensibility.
type Named = {def name: String}

// Separate out the endpoints into their own category
object endpointResolver {

  // Use a partial function which you can re-use in order parts
  // of your program. You may use this in your testing as well.
  val endPoints: PartialFunction[Named, String] = {
    case _: Track => "track.json"
    case _: Album => "album.json"
    case _: Artist => "artist.json"
  }

  // use an implicit class. This allows you to add methods
  // to existing classes seamlessly.
  implicit class nrEndpoint(namedRequest: Named) {
    def endpoint: String =
      endPoints apply namedRequest
  }

}

object Spotify {

  // Specify the return type. I was not sure what it was initially.
  def getResponse(req: Named): Req = {

    val params = Map("q" -> req.name)

    val spotifyUrl = host("ws.spotify.com")

    // import and utilise this implicit class
    import endpointResolver.nrEndpoint

    spotifyUrl / "search"/ "1" / req.endpoint <<? params

  }

  val foo = Album("foo")

  // OK is another example of an implicit being used. Use an IDE
  // such as IntelliJ IDEA to navigate through the vast depths
  // of Scala source.
  val songs = Http(getResponse(foo) OK as.String)

}


Lexer Implementation in Rust - `PhantomData` Awkwardness

such as files or in-memory strings

I'm going to give the advice that people dread: I don't think your abstraction is going to work here. When I first read "in-memory strings", I expected a String, not a &str. Since you mentioned a file, I think it's still a valid comparison. I don't believe you can implement this trait for such a type:

struct OwnedStringSource {
    pub buff: String,
}

impl<'a> Source<'a> for OwnedStringSource {
    fn at(&self, offset: usize) -> Option<(char, usize)> { None }

    fn slice(&self, start: usize, end: usize) -> &'a str {
        // Hmm.... what to put here?
    }
}

That is, there's no way to say a reasonable lifetime for 'a. I also think that it's the same root problem as Can I write an Iterator that yields a reference into itself?.

Aside from that, your original error was probably something like:

error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait, self type, or predicates
   --> src/main.rs:117:6
    |
117 | impl<'a, S> Iterator for TokenIter<S>
    |      ^^ unconstrained lifetime parameter

The best advice I've gotten about that specific error was helpful... after thinking about it for a while. Paraphrased and elided for this situation:

  what [the error is] trying to tell you is that it cannot get [the generic type] back from either
  the implemented trait [...] or the type implemented on [...]. [The where clause]
  is not enough to extract [the generic] from [the type] because one [...] type can have multiple
  [...] impls with various arguments

Instead, I might suggest that you use an associated type instead of the generic parameter:

trait Source {
    type Slice;

    fn at(&self, offset: usize) -> Option<(char, usize)>;
    fn slice(&self, start: usize, end: usize) -> Self::Slice;
}

This separates the lifetimes from the trait. Specific implementations can still participate in it:

impl<'a> Source for StringSource<'a> {
    type Slice = &'a str;
    // ...
}

And you can just bubble up the inner type out of the iterator:

impl<'a, S> Iterator for TokenIter<S>
    where S: Source,
{
    type Item = S::Slice;
    // ...
}

You potentially might need to add extra bounds on that generic there (S::Slice: AsRef<str>) depending on what you need to be able to do with the slice in the iterator implementation.

trait Source {
    type Slice;

    fn at(&self, offset: usize) -> Option<(char, usize)>;
    fn slice(&self, start: usize, end: usize) -> Self::Slice;
}

struct StringSource<'a> {
    pub buff: &'a str
}

impl<'a> Source for StringSource<'a> {
    type Slice = &'a str;

    fn at(&self, offset: usize) -> Option<(char, usize)> {
        self.buff[offset..].chars().nth(0).map(|ch| { (ch, offset + ch.len_utf8()) })
    }

    fn slice(&self, start: usize, end: usize) -> &'a str {
        &self.buff[start..end]
    }
}

struct TokenIter<S> {
    source: S,
    idx: usize,
}

impl<S> TokenIter<S> {
    fn new(source: S) -> Self {
        TokenIter {
            source: source,
            idx: 0,
        }
    }
}

impl<S> Iterator for TokenIter<S>
    where S: Source,
{
    type Item = S::Slice;

    fn next(&mut self) -> Option<Self::Item> {
        let ts = self.idx;
        self.source.at(ts).map(|(_ch, next)| {
            self.idx = next;
            self.source.slice(ts, next)
        })
    }
}

fn main() {
    let source = StringSource{ buff: "hello world" };
    let iter = TokenIter::new(source);
    println!("{:?}", iter.collect::<Vec<_>>());
}


The performance can be improved a bit more by declaring the function using cpdef (or cdef) instead of def (when the calling code is using Cython types too, check out http://docs.cython.org/src/userguide/language_basics.html#python-functions-vs-c-functions for more info).

Another idea that might help is to check whether the random number is greater than 0.5 and, if it is, count backwards from the end of the array rather than the front (assuming that the values in the array always add up to one).

Also, if this function takes up half of the program's running time then it seems likely that the program could be restructured to use a different algorithm which calls this function less often, but this would probably require much more effort than the other suggestions.


Merge dataframes on timestamps and time intervals using data.table in R

You'll need to use by.x to get around setting x's key.

Also, note that foverlaps can merge on any number of keys, and then do the overlaps only on the final two. What that means here is that you can do the x.Receiver == i.Receiver filter up-front, which also simplifies the clean-up step.

x = as.data.table(x)
x[ , end := DateTimePST]
y = as.data.table(y)

setkey(y, Receiver, Start, End)

result = foverlaps(x, y, by.x = c('Receiver', 'DateTimePST', 'end'), type = 'within')
result[ , end := NULL]

setDF(result)

I do find it a bit awkward/strange that you've got to define end when by.x = c('Receiver', 'DateTimePST', 'DateTimePST') would appear to be fine, and have filed a feature request.

If you don't need DateTimePST in your output, you might find the non-equi-join version more compact / easier to read:

y[x, on = .(Receiver == Receiver, Start < DateTimePST, End > DateTimePST)]
#    Station Receiver               Start                 End
# 1:  YBAAG4   102140 2014-11-08 13:11:36 2014-11-08 13:11:36
# 2:  YBCSSW   112568 2013-10-02 18:47:24 2013-10-02 18:47:24
# 3:  YBCSSW   112568 2016-09-22 08:01:16 2016-09-22 08:01:16
# 4:   YBBCD   121907 2017-11-22 12:53:55 2017-11-22 12:53:55
#    TagID
# 1: 20161
# 2: 23055
# 3: 45428
# 4:  2627

Note the order y[x -- we're using x to "look up" rows of y.

If you do need DateTimePST, it's just a bit of an ugly extension:

y[x, c(.SD, list(DateTimePST=DateTimePST)),
  on = .(Receiver == Receiver, Start < DateTimePST, End > DateTimePST)]

Finally, we can think of this as adding columns to x by a non-equi-join with y by reversing the order:

x[y, `:=`(Start = i.Start, End = i.End, Station = i.Station),
  on = .(Receiver == Receiver, DateTimePST > Start, DateTimePST < End)]


You iterate through the same array twice (once while initializing and once while assigning values), you can do both inside the same loop.

for (int i = 0; i < w; i++)
{
    res[i] = new int[h];
    for (int j = 0; j < h; j++)
    {
        res[i][j] = A[j][i];
    }
}