texts: Analyze Stock Market Bottom Timing without Looping over Data Frame Rows

texts:
I am replicating this blog post in Python.
The blog post shows that even if you know future stock market bottoms, only buying stocks when the stock market is below future stock market bottoms provides only a modest edge of 22%.

The code below replicates the blog post's result (approximately, I use different data).
However, in cell # In[8], it loops over the rows in data frame df.
This loop makes the logic clear (if the stock market is below the future stock market bottom, move cash to equity, else invest cash in Treasuries and maintain equity investment) but seems un-Pythonic (and like code I would have written in my high school Basic class 25 years ago).

In cell # In[8], can I avoid looping over rows?

Note, that I wrote this code in a Jupyter Notebook, and display() requires a Jupyter Notebook.

#!/usr/bin/env python
# coding: utf-8

# In[1]:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import pandas_datareader as pdr

# In[2]:

url = 'https://query1.finance.yahoo.com/v7/finance/download/%5EGSPC?period1=-1325635200&period2=1588118400&interval=1d&events=history'
gspc = pd.read_csv(url, index_col='Date', parse_dates=True)
display(gspc)

# In[3]:

url = 'https://query1.finance.yahoo.com/v7/finance/download/VFINX?period1=315619200&period2=1588118400&interval=1d&events=history'
vfinx = pd.read_csv(url, index_col='Date', parse_dates=True)
vfinx['Return'] = vfinx['Adj Close'].pct_change()
display(vfinx)

# In[4]:

dgs3mo = pdr.get_data_fred('DGS3MO', start=vfinx.index.min())
display(dgs3mo)

# In[5]:

df = gspc[['Close']].join(vfinx[['Return']]).join(dgs3mo / 100 / 365).dropna()
df.columns = ['SP500', 'RM', 'RF']
display(df)

# In[6]:

df.sort_index(ascending=False, inplace=True)
df['SP500_Fwd_Min'] = df.SP500.cummin()
df.sort_index(inplace=True)
display(df)

# In[7]:

df['To_Equity'] = np.where(df.SP500 <= df.SP500_Fwd_Min, 1, 0)
df['Smart_Cash'] = 0.
df['Smart_Equity'] = 0.
df['Dumb'] = 0.
df['Daily'] = 1

# In[8]:

for i in range(1, df.shape[0]):
    if (df.To_Equity[i - 1] == 1):
        df.Smart_Equity[i] = (df.Smart_Equity[i-1] + df.Smart_Cash[i-1] + df.Daily[i-1]) * (1 + df.RM[i])
        df.Smart_Cash[i] = 0
    else:
        df.Smart_Equity[i] = (df.Smart_Equity[i-1]) * (1 + df.RM[i])
        df.Smart_Cash[i] = (df.Smart_Cash[i-1] + df.Daily[i-1]) * (1 + df.RF[i])

    df.Dumb[i] = (df.Dumb[i-1] + df.Daily[i-1]) * (1 + df.RM[i])

df['Smart'] = df.Smart_Equity + df.Smart_Cash

# In[9]:

df[['Smart', 'Dumb']].plot(figsize=(12,8))

# In[10]:

np.log(df.Smart[-1]) - np.log(df.Dumb[-1])

# In[ ]:


texts: Python editing lists and converting to a dictionary

texts:
After validation my errors list returns

[(False, {u'first_name': u'First name is too short'}), (False, {u'last_name': u'Last name is too short'}), (False, {u'confirm_password': u'Password is too short'}), (False, {u'email': u'Please enter a valid email'})]

I use a for loop to create a new list and only add the one index in every element.  I am trying to get rid of every False.  I am looking for a way to improve my code.

Here is my code

errors = []
errors.append(self.validate_length(first_name, 'first_name', 2, "First name is too short"))
errors.append(self.validate_length(last_name, 'last_name', 2, "Last name is too short"))
errors.append(self.password_match(password, confirm_password))
errors.append(self.validate_email(email_address))

error = []
print errors
for elements in range(0, len(errors)):
    try:
        errors[elements][1]
        error.append(errors[elements][1])
    except:
        pass


texts: Is it better practice to have void method throw an exception or to have the method return a boolean?

texts:
This falls straight into the same category with the recent "Is it better practice to return if false or execute an if statement if true?" question.

Often, while writing code, I find myself presented with two options. Either I can do this:

public void myMethod() throws Exception {
    // Some code
    validate(some_object_or_condition);
    // Some code
}

private void validate(Object arg) throws Exception {
    // Code that validates the argument and throws an exception if it's invalid
}

Or this:

public void myMethod() throws Exception {
    // Some code
    if( !isValid(some_object_or_condition) ) throw new Exception();
    // Some code
}

private boolean isValid(Object arg) {
    // Code that returns a boolean based on whether or not the argument is valid
}

Is either one preferable over the other? When I was still just learning Java the latter felt more "object oriented" and somehow more natural. However, with experience the former has started to feel better because then the code looks cleaner to me, but I'm afraid people might find it less readable.

Edit/Add: This time I ran into this problem when implementing a game (just for the fun of it) where the user would place words into a grid. The playWord method may fail because of the word not actually being a real word (yielding an InvalidWordException) or because the user is trying to place a valid word into an illegal position (yielding a WordPlacementException). If the method does not fail, it should return the score of the word as an integer. So in my case the playWord method first calls the method that validates the word and later the method that validates the location of the word, and these provoked this question here.


texts:
I'm currently learning some jQuery and I am stuck on something. On a small exercise I did, I was asked to perform an action when you mouseover a div class.  I gave a different solution than the one suggested and I was wondering which one is written best and why.

HTML code

   <input class='data' type='text' name='firstname' value='Firstname 1'>
   <input class='data' type='text' name='lastname' value='Lastname 1'>

Here is my solution:

$(document).on('mouseover', '.success', function() {
    $(this).removeClass('success').addClass('delete');
});

And here is his solution:

$(document).on('mouseover', '.deletecell', function() {
    $('.success', this).addClass('delete').removeClass('success');
});

PS: Does it matter if addClass is first and removeClass is second?


texts:
I'm new to Databinding in MVP and I want to bind my DataGridView to a object list. This grid may be populated in two ways.

There are TextBoxes in the form and users can enter text, and when they press Add button, a new object is instantiated with the provided fields and then it is added to the list.

Other way is, when a user searches for objects, matching objects are loaded from database to the list. Also after searching users should be able to add new items to this list as in the first method.

Finally this list is binded to the DataGridView and details are shown in the grid. Now the user could do any changes to the items shown in the grid and if it's all right, the user could save them.

So in my presenter I have three methods for the above requirements.

    Class AttendancePresenter : BasePresenter
    {
        private IAttendance _Model;
        private readonly IAttendanceView _View;
        private readonly IDataService _DataService;
        private readonly IMessageService _MessageService;
        List<IAttendance> AttendanceList = new List<IAttendance>();

        public AttendancePresenter( IAttendance model, IAttendanceView view, IDataService dataService, IMessageService messageService ) : base(messageService)
        {
            _Model = model;
            _View = view;
            _DataService = dataService;
            _MessageService = messageService;

            SetModelPropertiesFromView(_Model, _View);
            WireUpViewEvents();
        }

        //Add new attendance to the list and the the list is binded to the grid
        private void _View_OnAddAttendance(object sender, EventArgs e)
        {

            SetModelPropertiesFromView(_Model, _View); // call base class method to update the Mode with uptodate data from the view
            IAttendance attendanceModel = new Attendance(_Model); // Use Model's copy constructor to get a clone
            AttendanceList.Add(attendanceModel);
            var bindingList = new BindingList<IAttendance>(AttendanceList);
            var source = new BindingSource(bindingList, null);
            _View.AttendanceGrid = source;
        }

        //Get attendance from the database and show on the grid
        void _View_OnSearchForAttendance(object sender, EventArgs e)
        {
            AttendanceList = _DataService.GetAttendance(_View.InTime, _View.PointID, AttendanceList);
            var bindingList = new System.ComponentModel.BindingList<IAttendance>(AttendanceList);
            var source = new BindingSource(bindingList, null);
            _View.AttendanceGrid = source;
        }

        //Pass attendance list to DAL to insert into the database
        private void _View_OnSaveAttendance(object sender, EventArgs e)
        {
             var result = _DataService.InsertAttendance(AttendanceList);
             AttendanceList.Clear();

            _MessageService.ShowResult(result);
        }

    }

I'm not sure the way I've binded things is right. And also I wonder if I could use a single binding list here as I'm binding same list to the same DataGridView! Please review my code and give your advice.


texts:
I wrote a program that does division and either does or doesn't show repeating groups. After designing it the first time I had to redesign it because although it worked it was unnecessarily complex. And now I am presenting to you my refined division program, for feedback and improvement ideas:

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.Scanner;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Division
{
    public static void main(String[] args)
    {
        Pattern p = Pattern.compile("(\\d+)(/)(\\d+)");
        Scanner in = new Scanner(System.in);

        System.out.print("Enter a fraction in the form x/y: ");
        String input = in.nextLine();
        Matcher m = p.matcher(input);

        while(!m.matches())
        {
            System.out.print("Enter a fraction in the form x/y: ");
            input = in.nextLine();
            m = p.matcher(input);
        }
        in.close();

        int top = Integer.parseInt(m.group(1));
        int bottom = Integer.parseInt(m.group(3));
        System.out.println("Result: " + divide(top,bottom,true));
    }

    public static String divide(int numerator, int denominator, boolean showRepeatingGroup)
    {
        int whole = (int)numerator / denominator;
        numerator = (numerator - (whole * denominator)) * 10;
        StringBuilder result = new StringBuilder(whole + ".");
        Set<Integer> remainders = new LinkedHashSet<Integer>();

        while(remainders.add(numerator))
        {
            whole = (int)numerator / denominator;
            result.append(whole);
            numerator = (numerator - (whole * denominator)) * 10;
        }

        ArrayList<Integer> remaindersList = new ArrayList<Integer>(remainders);
        if (result.charAt(result.length() - 1) != '0' && showRepeatingGroup)
        {
            int position = remaindersList.indexOf(numerator) + 2;
            result.insert(position,  "{");
            result.append("}");
        }

        return result.toString();
    }
}


texts:
I'm new to Java and coding in general. I'm about a week or two into learning Java, and I decided to make this simple four function calculator. Any feedback/tips?

import java.util.Scanner;

class Main {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    System.out.println("What is your first number?");
    float num1 = input.nextFloat();
    System.out.println("What is your second number?");
    float num2= input.nextFloat();
    System.out.println("What operation would you like to perform?");
    String Operation = input.next();
    switch (Operation) {
        case "addition":
            System.out.println(num1 + num2);
            break;
        case "subtraction":
            System.out.println(num1 - num2);
            break;
        case "multiplication":
            System.out.println(num1 * num2);
            break;
        case "division":
            if (num1 == 0 || num2 == 0) ;
        {
            System.out.println("You can not divide by or with a zero!");
        }

        System.out.println(num1 / num2);
        break;
        default:
            System.out.println("Invalid Operation!");
    }

   }
}


texts: Finding the biggest number in a list of numbers

texts:
This is the first time I tried to solve a problem and wrote code for it, rather than learning some code and trying out to see what I might be able to do with it. As such, it's not much, but I'd like any tips and help about how I wrote this. Basically, I tried to write a program to find the biggest number in a list of numbers.

def main():
    print("Enter a series of numbers separated only by spaces:", end='\t')
    n = input().split()
    n = [int(i) for i in n]
    while len(n) != 1:
        if n[0] > n[1]:
            n.remove(n[1])
        else:
            n.remove(n[0])
    print("the biggest number is: ", n[0])
main()

Feedback? Did I do anything kinda noobish?


texts: Efficiently displaying elements in an array

texts:
I wrote the below method to list out the elements of an array, as well as adding the Oxford comma to the list if desired. However, my for-loop looks, perhaps, a little convoluted. Is there a better way to handle clearly both the Oxford comma and the ability to add a conjunction?

function listOut($array = array(), $oxford = false, $conjunction = 'and') {
    $count = is_array($array) ? count($array) : 0;

    switch($count) {
        case 0: // Either not an array or an empty array
            return '';

        case 1: // Only one element in the array, return it
            return $array[0];

        case 2: // Two elements; join them together with the conjunction
            return "{$array[0]} {$conjunction} {$array[1]}";

        default: // At least two elements, join with commas and conjunction
            $s = ", ";
            $t = "";
            for($i = 0; $i < $count; $i++) {

                // Add a conjunction if it's the last element in the array
                if($i + 1 === $count) {
                    $s = $oxford ? $s . "$conjunction " : " $conjunction ";
                }

                // Only prepend comma if not the first element
                if($i > 0) {
                    $t .= $s;
                }

                // Add the element to the return string
                $t .= $array[$i];
            }
            return $t;
    }
}

$array1 = array('orange');
$array2 = array('white', 'gold');
$array3 = array('black', 'white', 'purple');

echo listOut($array1); // orange
echo listOut($array2); // white and gold
echo listOut($array3); // black, white and purple

echo listOut($array1, true); // orange
echo listOut($array2, true); // white and gold
echo listOut($array3, true); // black, white, and purple


texts: Powershell for syncing Minecraft save files

texts:
My code in full plus the task scheduler definition: https://gist.github.com/DaneWeber/0c5e7978bd3927734173e3afdc3d6338

This is my first non-trivial PowerShell script. This was a pretty major learning experience. I only went down this path once I discovered that xcopy wouldn't be sufficient.

My Ask: What do I not know that I don't know? That is, what about this strikes you as weird, clunky, or "not the PowerShell way"?

Problem I am solving: I want Minecraft worlds sync'ed across two (or more) computers. It doesn't seem like I should have to pay for an additional service to accomplish this.

Other solutions attempted:

Symlink/Junction of the Minecraft minecraftWorlds folder with one in OneDrive. Minecraft seemed to be unable/unwilling to see the data.
xcopy /d to update files. The problem is that Minecraft renames the files every save, so each save results in another large file, growing out of control.

Solution approach:

If any world exists in one location (local or cloud) but not the other: copy it.
If one location's world contains a file newer than all other files in the other location, replace the entire world with the newer one. (You have to be careful not to consider folders, since they have a modified date of their creation, even when you copy a folder.)
In case the script runs while the cloud service is still downloading data, copy files from the cloud if there are ones missing locally.

The script:

$minecraftSaveFolder = "minecraftWorlds"
$minecraftLocalPath = $env:LOCALAPPDATA + "\Packages\Microsoft.MinecraftUWP_8wekyb3d8bbwe\LocalState\games\com.mojang"
$minecraftLocalSave = $minecraftLocalPath + "\" + $minecraftSaveFolder
$minecraftCloudSave = $PSScriptRoot + "\MinecraftBackup"

$cloudWorlds = Get-ChildItem $minecraftCloudSave
$localWorlds = Get-ChildItem $minecraftLocalSave

$saves = Compare-Object $cloudWorlds $localWorlds -IncludeEqual

switch ($saves) {
  ( {$PSItem.SideIndicator -eq "<="}) {
    Write-Output "Copying from cloud: $($PSItem.InputObject.Name)"
    Copy-Item -Path $PSItem.InputObject.FullName -Destination $minecraftLocalSave -Container -Recurse
  }
  ( {$PSItem.SideIndicator -eq "=>"}) {
    Write-Output "Copying to cloud: $($PSItem.InputObject.Name)"
    Copy-Item -Path $PSItem.InputObject.FullName -Destination $minecraftCloudSave -Container -Recurse
  }
  ( {$PSItem.SideIndicator -eq "=="}) {
    $cloudLatest = (( Get-ChildItem ( $minecraftCloudSave + "\" + $PSItem.InputObject) -Recurse -File ).LastWriteTime | Measure-Object -Maximum)
    $localLatest = (( Get-ChildItem ( $minecraftLocalSave + "\" + $PSItem.InputObject) -Recurse -File ).LastWriteTime | Measure-Object -Maximum)
    if ($cloudLatest.Maximum -eq $localLatest.Maximum) {
      if ($cloudLatest.Count -gt $localLatest.Count) {
        Write-Output "Adding files from cloud: $($PSItem.InputObject.Name)"
        xcopy ($minecraftCloudSave + "\" + $PSItem.InputObject) $minecraftLocalSave /d /e /c /i /h /y
      }
      else {
        Write-Output "Already synchronized: $($PSItem.InputObject.Name)"
      }
    }
    elseif ($cloudLatest.Maximum -gt $localLatest.Maximum) {
      $world = $PSItem.InputObject.Name
      Write-Output "Over-writing local with $world"
      $old = Rename-Item -Path "$minecraftLocalSave\$world" -NewName ($world + "_old") -PassThru
      Copy-Item -Path "$minecraftCloudSave\$world" -Destination $minecraftLocalSave -Container -Recurse
      Remove-Item -Recurse -Force -Path $old
    }
    elseif ($cloudLatest.Maximum -lt $localLatest.Maximum) {
      $world = $PSItem.InputObject.Name
      Write-Output "Updating cloud with $world"
      $old = Rename-Item -Path "$minecraftCloudSave\$world" -NewName ($world + "_old") -PassThru
      Copy-Item -Path "$minecraftLocalSave\$world" -Destination $minecraftCloudSave -Container -Recurse
      Remove-Item -Recurse -Force -Path $old
    }
    else {
      Write-Error "Something went wrong with the folder comparison."
    }
  }
}


texts: C++ delegate implementation with member functions

texts:
I have been implementing a delegate class with C++11 that supports class member functions. I am interested in knowing if there are any potential problems with this implementation. My main concern is the private Key struct of the Delegate class. Can I always rely on the key being correct in all situations?  Meaning, can I always identify certain function of certain object with it?

In there I am storing 3 things:

hash of the member function pointer
pointer to the object
std::function wrapping the actual function

The hash is calculated from the function pointer with the getHash() function. I've originally used only that as key, but I run into a problem with inherited classes.  I've gotten the same hash for the inherited function with 2 different classes inheriting from same base class. For that reason, I've also stored the pointer to the actual object, using these 2 as key I am able to identify certain entry in the list of stored keys. I'd be really interested to know any potential issues with this implementation. This compiles with VS2013, and I am hoping it will also work with Clang, but I haven't tried it out yet.

template <typename...Params>
class Delegate {
private:

typedef std::function < void(Params...)> FunctionType;
struct Key
{
    size_t        m_funcHash;
    void*         m_object;
    FunctionType  m_func;

    Key(void* object, size_t funcHash, FunctionType func) : m_object(object), m_funcHash(funcHash), m_func(func)
    {}

    bool operator==(const Key& other) const {
        return other.m_object == m_object && other.m_funcHash == m_funcHash;
    }
};

std::vector <Key> m_functions;

public:
template <typename Class>
void connect(Class& obj, void (Class::*func) (Params...) )
{

    std::function < void (Class, Params...) >  f = func;
    FunctionType fun = [&obj, f](Params... params) { f(obj, params...); };
    size_t hash = getHash(func);
    m_functions.push_back(Key( &obj, hash, fun));
}

template <typename Class>
size_t getHash(void (Class::*func) (Params...)) {
    const char *ptrptr = static_cast<const char*>(static_cast<const void*>(&func));
    int size = sizeof (func);
    std::string str_rep(ptrptr, size);
    std::hash<std::string> strHasher;
    return strHasher(str_rep);
}

template <typename Class>
void disconnect( Class& obj, void (Class::*func) (Params...)  ) {
    size_t hash = getHash(func);

    for (unsigned int i = 0; i < m_functions.size(); ++i) {
        auto key = m_functions[i];
        if (key.m_funcHash == hash && key.m_object == &obj) {
            m_functions.erase(m_functions.begin() + i);
            --i;
        }
    }
}

template <typename ... Args>
void operator() (Args...args)
{
    for (auto& key : m_functions) {
        key.m_func (args...);
    }
}
};

class BaseClass {

public:
    virtual void print(const std::string& str) = 0;
};

class A : public BaseClass {
public:
    void print(const std::string& str) {
        std::cout << " Class A : Print [" << str << "]\n";
    }

};

class B : public BaseClass {
public:
    void print(const std::string& str) {
        std::cout << " Class B : Print [" << str << "]\n";
    }
};

int _tmain(int argc, _TCHAR* argv[])
{

    A a;
    B b;

    Delegate <const std::string&> delegate;

    delegate.connect(a, &A::print);
    delegate.connect(b, &B::print);

    delegate("hello");
    delegate("world");

    delegate.disconnect(a, &A::print);
    delegate("bye world");

    delegate.disconnect(b, &B::print);
    delegate("nobody there.");  // should not print anything

    std::cin.ignore();

    return 0;
}


texts: R code to read table, perform calculations, and write excel

texts:
I hear loops are best avoided in R.

Is there a better way to do something like this perhaps using lapply, etc.?

library(xlsx)

files <- list.files(path="/tmp/data", pattern="*.xls", full.names=T, recursive=TRUE)

for(i in 1:length(files)) {
  df <- read.table(files[i], header=TRUE)
  avgArea <- mean(df[df$Area>20,"Area"])
  avgMean <- mean(df[df$Area>20,"Mean"])
  fnNew <- paste(tools::file_path_sans_ext(files[i]), ".xlsx", sep="")
  wb <- createWorkbook(type="xlsx")
  sheet  <- createSheet(wb, sheetName="data")
  addDataFrame(df, sheet)
  rows  <- createRow(sheet, rowIndex=nrow(df)+1)
  cells <- createCell(rows, colIndex=1:4)
  setCellValue(cells[[1,2]], "Average")
  setCellValue(cells[[1,3]], avgArea)
  setCellValue(cells[[1,4]], avgMean)
  saveWorkbook(wb, fnNew)
}

An example .xls can be found here


texts: Will this code adjust its alignments automatically with respect to the user's screen?

texts:
HTML

<!Doctype html>

        <title>Welcome To DPS Raipur</title>
        <link rel="icon" type="image/png" href="logo.gif" />
        <link rel="stylesheet" type="text/css" href="index.css">

CSS

div.wrapper
{
    background-color: #D0D0D0;
    width: 1200px;
    height: 1000px;
    margin: 0 auto;
}

div.title
{
    background-color: #06472F;
    width: 1200px;
    height: 20px;
}

This is a website I am creating and I am completely a beginner, so I don't know whether this code will adjust its alignments automatically with respect to the user's screen. What should I do to make it like that? How can it be improved?

Also, I've been asked to create this site and I don't know how to put a site online. So, will it be OK if I keep all the web pages on my computer by in various folders?


texts:
I'm working on a small application that uses a WebService to get meta-data about television episodes.  What I'd like to do is start building out a local cache of the series so that I can reduce the number of calls to the service.  Data being stale is not a concern at all for this case.  I've chosen to use an XML file to implement the local repository.

Here's the code:

public interface IVideo { int SeriesId { get; set; } string Name { get; set; } }

public class TVShow : IVideo { public int SeriesId { get; set; } public string Name { get; set; } }

public class IVideoCollection : List<IVideo>, IXmlSerializable
{
    public IVideoCollection() : base() { }

    #region IXmlSerializable
    public System.Xml.Schema.XmlSchema GetSchema() { return null; }

    public void ReadXml(XmlReader reader)
    {
        reader.ReadStartElement("IVideoCollection");
        while (reader.IsStartElement("IVideo"))
        {
            Type type = Type.GetType(reader.GetAttribute("AssemblyQualifiedName"));
            XmlSerializer serial = new XmlSerializer(type);

            reader.ReadStartElement("IVideo");
            this.Add((IVideo)serial.Deserialize(reader));
            reader.ReadEndElement();
        }
        reader.ReadEndElement();
    }

    public void WriteXml(XmlWriter writer)
    {
        foreach (IVideo vid in this)
        {
            writer.WriteStartElement("IVideo");
            writer.WriteAttributeString("AssemblyQualifiedName", vid.GetType().AssemblyQualifiedName);
            XmlSerializer xmlSerializer = new XmlSerializer(vid.GetType());
            xmlSerializer.Serialize(writer, vid);
            writer.WriteEndElement();
        }
    }
    #endregion
}

public class SeriesRepository
{
    public IVideoCollection Collection;
    public string RepoPath {get;set;}

    public SeriesRepository()
    {
        Collection = new IVideoCollection();
        RepoPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "IVideoCollection.xml");
        if (File.Exists(RepoPath))
        {
            var xmlSerializer = new XmlSerializer(Collection.GetType());
            var xmlReader = XmlReader.Create(new StreamReader(RepoPath));
            Collection = (IVideoCollection)xmlSerializer.Deserialize(xmlReader);
        }
    }

    public void SaveChanges()
    {
        var xmlSerializer = new XmlSerializer(Collection.GetType());
        StreamWriter writer = new StreamWriter(RepoPath);
        xmlSerializer.Serialize(writer, Collection);
        writer.Flush();
        writer.Close();
    }
}

I'm trying looking to see if there are any obvious points of failure that I haven't seen.  Any feedback is appreciated.

The resulting XML looks like:

<?xml version="1.0" encoding="utf-8"?>
<IVideoCollection>
  <IVideo AssemblyQualifiedName="MyNamespace.TVShow, MyNameSpace, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
    <TVShow xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <SeriesId>55512345</SeriesId>
      <Name>This is the show name</Name>
    </TVShow>
  </IVideo>
</IVideoCollection>


texts: Eager Loading Deeply into a Model with a Collection Property Whose Type is Inherited

texts:
Visual Studio generated this great route for me, where I can load an entity:

// GET: api/Jobs/5
[ResponseType(typeof(Job))]
public async Task<IHttpActionResult> GetJob(int id)
{
    Job job = await db.Jobs.FindAsync(id);

    if (job == null)
    {
        return NotFound();
    }

    return Ok(job);
}

Here's the Job model this is based on:

public class Job
{
    public Job()
    {
        this.Regions = new List<Region>();
        this.Files = new List<JobFile>();
    }

    public int ID { get; set; }
    public string Name { get; set; }
    public List<Region> Regions { get; set; }
    public JobTypes JobType { get; set; }
    public int UserIDCreatedBy { get; set; }
    public int? UserIDAssignedTo { get; set; }
    public List<JobFile> Files { get; set; }
    public bool IsLocked { get; set; } // Lock for modification access
}

Here's the JobFile class, which Jobs have a list of:

public class JobFile
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Url { get; set; }
    public int Job_ID { get; set; }
}

and Pdf, a subclass of JobFile:

public class Pdf : JobFile
{
    public Pdf()
    {
        this.PdfPages = new List<PdfPage>();
    }

    public int Index { get; set; }

    public List<PdfPage> PdfPages { get; set; }
}

Now, when I hit that route, I'd like to eagerly load all the Pdfs for a Job, including their pages. I modified the route to look like this, and it works.

// GET: api/Jobs/5
[ResponseType(typeof(Job))]
public async Task<IHttpActionResult> GetJob(int id)
{
    Job job = await db.Jobs.FindAsync(id);

    // Lookup the PDFs for this job and include their PdfPages
    List<JobFile> jobPdfs = db.Pdfs.Include(pdf => pdf.PdfPages).Where(pdf => pdf.Job_ID == id).ToList<JobFile>();

    // Attach the job files to the job
    job.Files = jobPdfs;

    if (job == null)
    {
        return NotFound();
    }

    return Ok(job);
}

Is this the best way to eagerly load all these models? Could this somehow be collapsed into one statement? It seems right now it hits the database twice. Could I build off of the original

Job job = await db.Jobs.FindAsync(id);

to load the Pdfs and their PdfPages all in one query?

This question provided some helpful insight, but I'm not sure how I can capitalize on its conclusions. I think I need the ToList<JobFile>() (which according to the question does a trip to the database) because I actually need that data. So unless I can squash it into one more complicated Linq statement, perhaps it's unavoidable to make two trips.


texts: Optimize QuadTree to find K Nearest Neighbors

texts:
I'm looking a way to make my k nearest neighbors search more efficient. The context of the question is that I'm given a list of topics that have a unique ID (integer) and a (x,y) coordinate (floats) associated with each topic. Subsequently, I'm given another point and the number of nearest topics to find around the given point. The topics need to be returned in order of distance to the point. However, if the distance between two topics is <= .001, the topic with the larger ID should come first. Here's the full description if you're looking for a better description: http://www.quora.com/challenges#nearby.

My solution is implemented using a QuadTree. I'm looking for ways to make the search function of the QuadTree faster.

In addition to the four quadrants (children QuadTrees), the QuadTree class has two member variables x and y representing the point at which it is split into northeast, northwest, southeast, and southwest. Both findTopics() and findTopicsWithinRadius() are functions of the QuadTree.

class Topic:
    def __init__(self, x, y, val):
        self.id = val
        self.x = x
        self.y = y

# point: (x,y) tuple
# numResponses: integer
def findTopics(self, point, numResponses):
    radius = 1
    topicToDistance = {}
    update = topicToDistance.update
    while len(topicToDistance) < numResponses:
        # dont want to see topics in previous searches
        update(self.findTopicsWithinRadius(point, radius, radius/2))
        radius += radius # faster to add radius to itself than multiply by 2
    def firstGreaterCmp(a, b):
        if abs(a[1]-b[1]) <= .001:
            if a[0].id < b[0].id:
                return 1
        if a[1] > b[1]:
            return 1
        return -1
    topics = sorted(topicToDistance.items(), cmp=firstGreaterCmp)
    # sorting by key may be faster than using a cmp function
    topics = [x[0].id for x in topics]
    return topics[:numResponses]

# search for topics around the given point with distance in the range of [minimum, radius]
# minimum is used so topics are not considered more than once
# point: (x,y) tuple
# radius: integer
# minimum: int
def findTopicsWithinRadius(self, point, radius, minimum=0):
    topics = {}
    if not self.northwest:
        # this is a leaf
        for topic in self.topics:
            dist = distance((topic.x, topic.y), point)
            if minimum <= dist <= radius:
                topics[topic] = dist
        return topics
    # check subtrees
    if self.x >= point[0]-radius and self.y <= point[1]+radius:
        topics.update(self.northwest.findTopicsWithinRadius(point,radius, minimum))
    if self.x <= point[0]+radius and self.y <= point[1]+radius:
        topics.update(self.northeast.findTopicsWithinRadius(point,radius, minimum))
    if self.x >= point[0]-radius and self.y >= point[1]-radius:
        topics.update(self.southwest.findTopicsWithinRadius(point,radius, minimum))
    if self.x <= point[0]+radius and self.y >= point[1]-radius:
        topics.update(self.southeast.findTopicsWithinRadius(point,radius, minimum))
    return topics


texts:
I'm reading Real World Haskell, and this is my first try with the language.

This is the result of the exercises of chapter 3:

  Consider three two-dimensional points a, b, and c. If we look at the angle formed by the line segment from a to b and the line segment from b to c, it either turns left, turns right, or forms a straight line. Define a Direction data type that lets you represent these possibilities.
  Write a function that calculates the turn made by three 2D points and returns a Direction.
  Define a function that takes a list of 2D points and computes the direction of each successive triple. Given a list of points [a,b,c,d,e], it should begin by computing the turn made by [a,b,c], then the turn made by [b,c,d], then [c,d,e]. Your function should return a list of Direction.
  Using the code from the preceding three exercises, implement Graham's scan algorithm for the convex hull of a set of 2D points. You can find good description of what a convex hull is, and how the Graham scan algorithm should work.

For short, the Graham scan algorithm is:

find the point \$P\$ with the smallest \$y\$
sort the points \$A\$ according to the angle \$(Ox, PA)\$ and add P at the end
in the sorted list, form the turns \$(A_{i-1}, A_i, A_{i+1})\$ and
remove the points \$A_i\$ for which it is a "turn right".

Now my code is the following:

import Data.List
-- 9
data Direction = TurnLeft
               | TurnRight
               | StraightLine
                 deriving (Show, Eq)

-- 10
data Point = Point {
      x :: Float
    , y :: Float
    } deriving (Show, Eq)

turn :: Point -> Point -> Point -> Direction
turn a b c
      | z > 0     = TurnLeft
      | z < 0     = TurnRight
      | otherwise = StraightLine
    where
      bax = x a - x b
      bay = y a - y b -- coordinates of b->a
      bcx = x c - x b
      bcy = y c - y b -- coordinates of b->c
      z   = bax * bcy - bcx * bay

-- 11
turns :: [Point] -> [Direction]
turns []         = []
turns [_]        = []
turns [_,_]      = []
turns (x:y:z:zs) = (turn x y z) : (turns (y:z:zs))

-- 12
angle :: Point -> Point -> Point -> Float
angle a b c = - (bax * bcx + bay * bcy) /
              (sqrt ((bax * bax + bay * bay) * (bcx * bcx + bcy * bcy)))
    -- more efficient than acos for the algorithm and is the same for sorting
    where
      bax = x a - x b
      bay = y a - y b -- coordinates of b->a
      bcx = x c - x b
      bcy = y c - y b -- coordinates of b->c

argmin :: Ord b => (a -> b) -> [a] -> a
argmin _ []  = error "empty list"
argmin f (x:xs)
    | null xs = x
    | (f x) < (f argminrest) = x
    | otherwise              = argminrest
    where argminrest = argmin f xs

hull :: [Point] -> [Point]
hull pts =
    origin : ((map snd (filter keepPt results)) ++ [origin])
     where origin  = argmin y pts
           oxaxis  = Point (1 + x origin) (y origin)
           oxangle = angle oxaxis origin
           compare_angle pt1 pt2 = compare (oxangle pt1) (oxangle pt2)
           list    = reverse (sortBy compare_angle pts)
           -- origin is the first element of the list
           results = zip (turns (list ++ [origin])) (drop 1 list)
           keepPt (dir, _) = dir /= TurnRight

It works, at least according to the following unit test:

hull [(Point (-3) 1),(Point (-4) 1),(Point (-1) 4),(Point 0 0),(Point 2 2),(Point (-1) 3),(Point (-1) 2),(Point 1 0),(Point 3 (-1)),(Point (-1) (-1))]

which gives

[Point {x = -1.0, y = -1.0},Point {x = -3.0, y = 1.0},Point {x = -1.0, y = 2.0},Point {x = -1.0, y = 3.0},Point {x = -1.0, y = 4.0},Point {x = 2.0, y = 2.0},Point {x = 3.0, y = -1.0},Point {x = -1.0, y = -1.0}]

Is it an idiomatic code? How would you improve it?


texts: Verify that each named sequence is strictly increasing

texts:
The master launch sequence consists of several independent sequences for different systems. Your goal is to verify that all the individual system sequences are in strictly increasing order. In other words, for any two elements i and j (i < j) of the master launch sequence that belong to the same system (having systemNames[i] = systemNames[j]), their values should be in strictly increasing order (i.e. stepNumbers[i] < stepNumbers[j]).

The input systemNames[i] contains the name of the system to which the ith element of the master launch sequence belongs. Guaranteed constraints:

1 ≤ systemNames.length ≤ 5 · 104,
1 ≤ systemNames[i].length ≤ 10.

The input stepNumbers[i] is an array of positive integers that contains the value of the ith element of the master launch sequence. Guaranteed constraints:

stepNumbers.length = systemNames.length,
1 ≤ stepNumbers[i] ≤ 109.

Return true if all the individual system sequences are in strictly increasing order, otherwise return false.

Example

For systemNames = ["stage_1", "stage_2", "dragon", "stage_1", "stage_2", "dragon"] and stepNumbers = [1, 10, 11, 2, 12, 111], the output should be
    launchSequenceChecker(systemNames, stepNumbers) = true.

There are three independent sequences for systems stage_1, stage_2, and dragon. These sequences are [1, 2], [10, 12], and [11, 111], respectively. The elements are in strictly increasing order for all three.

Here is my code.

(defun launchSequenceChecker (systemNames stepNumbers)
  (setf mmm (remove-duplicates (copy-seq systemNames) :test #'equal))
  (setf lname (map 'list #'identity mmm))
    (setf lnum (make-list  (length mmm):initial-element '0))
  (setf lnum1 (make-list (length mmm):initial-element '1000000001))

    (loop named haha for x from 0 to (1- (length systemNames))
          for name = (aref systemNames x)
          for name1 = (aref systemNames (- (1-(length systemNames)) x))
          for num = (aref stepNumbers x)
          for num1 = (aref stepNumbers (- (1-(length systemNames)) x))
          for pos = (position name lname :test #'equal)
          for pos1 = (position name1 lname :test #'equal)

          if(>= (nth pos lnum) num)
          do(return-from haha nil)
          else
          if(<= (nth pos1 lnum1) num1)
          do(return-from haha nil)
          else
         do
           (setf (nth pos lnum) num )
          (setf (nth pos1 lnum1)num1)
           finally (return-from haha t)))

The problem I am having is that this code passes the tests but exceeds the 6-second time limit.


texts:
I am trying to learn how to build a very basic MVC structure before jumping on rails and other frameworks. I am working from a .txt file, and I organized a project into four parts: a model, a view, a controller, and a runner.

.txt file pattern:

line1 -> "question"
line2 -> "answer"
line3 -> "" #blank line
line4 -> "question"
line5 -> "answer"
line6 -> ""

and so on.

runner.rb:

require_relative 'view'
require_relative 'model'
require_relative 'controller'

file = 'flashcard_samples.txt'
go = Controller.new(file)

view.rb

require_relative 'model'

class View
  def initialize(question)
    @question = question
    puts "Question: " + question
  end
end

controller.rb

require_relative 'model'

class Controller
  def initialize(filename)
    a = Model.new(filename)
    a.play
  end
end

model.rb

require_relative 'view'

class Model
  attr_reader :data, :questions, :answers, :punchline

  def initialize(filename)
    @data = []
    @questions = []
    @answers = []
    @score = 0
    @file = File.open(filename)
  end
  def play
    welcome_message
    parse
    startquestions
  end
  def parse
    @file.each_line do |line|
      @data << line
    end
  end
  def checkanswer
    @goodanswer = false
    @oneanswer == @useranswer ? @goodanswer = true && @score += 1: @goodanswer = false && score += 0
  end
  def displaypunchline
    if @goodanswer == true
      puts "well done"
    else
      puts "the good answer was #{@oneanswer}"
    end
  end
  def displayscore
    puts "Your score is now: " + @score.to_s
  end
  def display_separation
    puts "--------------------------------------------------------------------------"
  end
  def getuserinput
    @useranswer = gets.chomp
  end
  def crazygameloop
    @counter = 1
    loop do
      break if @counter >= @data.length - 1
        @questions = @data.unshift[@counter-1].tr("\n","")
        @oneanswer = @data.unshift[@counter].tr("\n","")
        @onequestion = View.new(@questions)
        @data.unshift[@counter+1]
        @counter += 3
      getuserinput
      checkanswer
      displaypunchline
      displayscore
      display_separation
    end
  end
  def startquestions
    crazygameloop
  end
  def welcome_message
    puts "welcome in my shitty game"
    puts "|||||||||||||||||||||||||"
  end
end

So far it's working but it's un-DRY, not very readable and probably a bad/incomplete MVC implementation. I'm looking for advice on how I should re-organize my model in order to increase my view responsibilities and optimize my controller in case I would add other features. Any input appreciated.


texts: Simple timer start and stop from console

texts:
Just a simple timer application for review.  Stop and start the timer from the command line.

The plan is to use this in a poker application to fold out a player if they don't act after a time period (call the clock on them).  I was trying to use async await but it did not seem like the right tool for this.

MyTimer myTimer = new MyTimer();
while (true)
{
    string readLine = Console.ReadLine();
    if (readLine == "stop")
    {
        myTimer.TimerStop();
    }
    else if (readLine == "start")
    {
        myTimer.TimerStart();
    }
    else if (readLine == "q")
    {
        Environment.Exit(0);
    }
}

.

public class MyTimer
{
    private System.Timers.Timer aTimer = new System.Timers.Timer();
    private static void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e)
    {
        Debug.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
        Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
    }

    public void TimerStop()
    {
        aTimer.Enabled = false;
        Console.WriteLine("aTimer.Enabled = false");
    }
    public void TimerStart()
    {
        aTimer.Enabled = true;
        Console.WriteLine("aTimer.Enabled = true");
    }

    public MyTimer()
    {
        aTimer.Interval = 2000;
        // Hook up the Elapsed event for the timer.
        aTimer.Elapsed += OnTimedEvent;
        // Have the timer fire repeated events (true is the default)
        aTimer.AutoReset = true;
        // Start the timer
        aTimer.Enabled = true;
        Debug.WriteLine("aTimer.Enabled");
        Console.WriteLine("aTimer.Enabled");
    }
}


texts: More DRY way of conditionally filling variables?

texts:
I'm building a chart component and it behaves differently if data has been passed in or not.
Here are two ways I can think of to conditionally fill chart variables.
(data will be empty {} or have non-empty values)
// helper
function isEmptyObject(obj) {
  for (const i in obj) return false;
  return true;
}

const gridInterval = 5;

let yTopTickValue;
let yBottomTickValue;
let yTickCount;
let xTopTickValue;
let xBottomTickValue;
let xTickCount;

if (!isEmptyObject(data)) {
  yTopTickValue = Math.ceil(data.yMax / gridInterval) * gridInterval;
  yBottomTickValue = Math.floor(data.yMin / gridInterval) * gridInterval;
  yTickCount = (yTopTickValue - yBottomTickValue) / gridInterval + 1;

  xTopTickValue = Math.ceil(data.yMax / gridInterval) * gridInterval;
  xBottomTickValue = Math.floor(data.yMin / gridInterval) * gridInterval;
  xTickCount = (xTopTickValue - xBottomTickValue) / gridInterval + 1;
}

and
const gridInterval = 5;
const isDataEmpty = isEmptyObject(data);

const yTopTickValue = isDataEmpty ? null: Math.ceil(data.yMax / gridInterval) * gridInterval;
const yBottomTickValue = isDataEmpty ? null: Math.floor(data.yMin / gridInterval) * gridInterval;
const yTickCount = isDataEmpty ? null: (yTopTickValue - yBottomTickValue) / gridInterval + 1;

const xTopTickValue = isDataEmpty ? null: Math.ceil(data.yMax / gridInterval) * gridInterval;
const xBottomTickValue = isDataEmpty ? null: Math.floor(data.yMin / gridInterval) * gridInterval;
const xTickCount = isDataEmpty ? null: (xTopTickValue - xBottomTickValue) / gridInterval + 1;

Are there any other clever versions I'm missing?


texts: Extract the title from a webpage using the python 3 standard lib

texts:
The aim of this module is to extract the title from possibly broken hypertext. Without using any awesome third party modules like bs4 or lxml.

#-*-coding:utf8;-*-
#qpy:3
#qpy:console

'''
Extract the title from a web page using
the standard lib.
'''

from html.parser import HTMLParser
from urllib.request import urlopen
import urllib

def error_callback(*_, **__):
    pass

def is_string(data):
    return isinstance(data, str)

def is_bytes(data):
    return isinstance(data, bytes)

def to_ascii(data):
    if is_string(data):
        data = data.encode('ascii', errors='ignore')
    elif is_bytes(data):
        data = data.decode('ascii', errors='ignore')
    else:
        data = str(data).encode('ascii', errors='ignore')
    return data

class Parser(HTMLParser):
    def __init__(self, url):
        self.title = None
        self.rec = False
        HTMLParser.__init__(self)
        try:
            self.feed(to_ascii(urlopen(url).read()))
        except urllib.error.HTTPError:
            return
        except urllib.error.URLError:
            return
        except ValueError:
            return

        self.rec = False
        self.error = error_callback

    def handle_starttag(self, tag, attrs):
        if tag == 'title':
            self.rec = True

    def handle_data(self, data):
        if self.rec:
            self.title = data

    def handle_endtag(self, tag):
        if tag == 'title':
            self.rec = False

def get_title(url):
    return Parser(url).title

print(get_title('http://www.google.com'))


texts: LeetCode 1320: Minimum Distance to Type a Word Using Two Fingers I

texts:
I'm posting my C++ code for LeetCode's 1320. If you have time and would like to review, please do so. Thank you!
Problem

You have a keyboard layout as shown above in the XY plane, where each English uppercase letter is located at some coordinate, for
example, the letter \$A\$ is located at coordinate \$(0,0)\$, the letter
\$B\$ is located at coordinate \$(0,1)\$, the letter \$P\$ is located at
coordinate \$(2,3)\$ and the letter \$Z\$ is located at coordinate
\$(4,1)\$.

Given the string word, return the minimum total distance to type such string using only two fingers. The distance between coordinates
\$(x_1,y_1)\$ and \$(x_2,y_2)\$ is \$|x_1 - x_2| + |y_1 - y_2|\$.

Note that the initial positions of your two fingers are considered free so don't count towards your total distance, also your two fingers
do not have to start at the first letter or the first two letters.

Example 1:
Input: word = "CAKE" Output: 3
Explanation:
Using two fingers, one optimal way to type "CAKE" is:

Finger 1 on letter 'C' -> cost = 0
Finger 1 on letter 'A' -> cost = Distance from letter 'C' to letter 'A' = 2
Finger 2 on letter 'K' -> cost = 0
Finger 2 on letter 'E' -> cost = Distance from letter 'K' to letter 'E' = 1

Total distance = 3
Constraints:

\$2 \le \text{word.length} \le 300\$
Each word[i] is an English uppercase letter.

Accepted C++
#include <vector>
#include <string>
#include <algorithm>

class Solution {
    const unsigned short int ALPHABET_LEN = 26;
    const unsigned short int WIDTH = 6;
    const unsigned char A_LETTER = 'A';
public:

    std::size_t minimumDistance(const std::string word) {
        const std::size_t length = word.size();
        std::vector<int> dp(ALPHABET_LEN);
        int max_decrease = 0;
        int total_distance = 0;

        for (std::size_t index = 0; index < length - 1; index++) {
            const int second = word[index] - A_LETTER;
            const int third = word[index + 1] - A_LETTER;

            for (std::size_t first = 0; first < ALPHABET_LEN; first++) {
                dp[second] = std::max(dp[second], dp[first] + coordinates(second, third) - coordinates(first, third));
            }

            max_decrease = std::max(max_decrease, dp[second]);
            total_distance += coordinates(second, third);
        }

        return total_distance - max_decrease;
    }

private:
    unsigned short coordinates(unsigned short left, unsigned short right) {
        return std::abs(left / WIDTH - right / 6) + std::abs(left % WIDTH - right % WIDTH);
    }
};

Reference
On LeetCode, there is a class usually named Solution with one or more public functions which we are not allowed to rename.

Problem

Discuss


texts: Sorting algorithms with utility functions and iterators

texts:
My implementation of insertion, merge, and quick sort, with some utility testing functions. I just wanted to know how I can improve the efficiency and general cleanliness of my code.

(note: I know C++ has partition, but I want to do most of the implementation myself)

In particular, my main algorithms take iterators [first, last], but STL container's v.begin() and v.end() go 1 over the last element, so I have wrappers that call the main algorithm with first and end - 1.

How can I restructure this into the main algorithm (if that would be better)?

#include <iterator>
#include <vector>
using namespace std;

// Insert sort O(n^2) time O(1) space
template <typename Iterator>
void ins_sort(Iterator first, Iterator end) {
    for (Iterator cur = first; cur < end; ++cur) {
        auto key = *cur;
        Iterator ins = cur - 1;
        for (; first <= ins && key < *ins; --ins)
            *(ins + 1) = *ins;    // move 1 to right until correct position's found
        *(ins + 1) = key;
    }
}

// Merge sort O(nlogn) time O(n) space
template <typename Iterator>
void merge(Iterator first, Iterator middle, Iterator last) {
    using T = typename iterator_traits<Iterator>::value_type;
    vector<T> left(first, middle + 1);
    vector<T> right(middle + 1, last + 1);
    left.push_back(0xFFFFFFF); // sentinel
    right.push_back(0xFFFFFFF);
    for (int i = 0, j = 0, k = 0; k <= last - first; ++k) {
        if (left[i] < right[j]) first[k] = left[i++];
        else first[k] = right[j++];
    }
}

template <typename Iterator>
void merge_helper(Iterator first, Iterator last) {
    if (first < last) {
        Iterator pivot = first + (last - first)/2;   // half way point rounded down
        merge_helper(first, pivot);
        merge_helper(pivot + 1, last);
        merge(first, pivot, last);
    }
}

template <typename Iterator>
void mer_sort(Iterator first, Iterator end) {
    merge_helper(first, end - 1);
}

// Quick sort O(nlogn) time O(n) space
template <typename Iterator>
void quick_sort(Iterator first, Iterator last) {
    if (last - first > 0) {
        auto pivot = *(first + (last - first) / 2);
        Iterator left {first};
        Iterator right {last};
        while (left <= right) {
            while (*left < pivot) ++left;
            while (pivot < *right) --right;
            if (left <= right) { swap(*left, *right); ++left; --right; }
        }
        quick_sort(first, right);
        quick_sort(left, last);
    }
}
template <typename Iterator>
void qck_sort(Iterator first, Iterator end) {
    quick_sort(first, end - 1);
}

Testing code

#include <random>
#include <vector>
#include <iostream>
using namespace std;

class Rand_int {
public:
    Rand_int(int low, int high) : distribution{low, high} {
    }
    int operator()() {
        return distribution(engine);
    }

private:
    default_random_engine engine;
    uniform_int_distribution<> distribution;
};

template <typename T>
void print(vector<T> v) {
    for (auto x : v)
        cout << x << ' ';
    cout << '\n';
}

vector<int> randgen(int max, int num) {
    static Rand_int die{0, max};
    vector<int> res(num);
    for (int i = 0; i < num; ++i)
        res[i] = die();
    return res;
}


texts: Java job responsible for processing file statements

texts:
I am looking for any refactoring tips to improve code readability. Does it feel like procedural programming? If so, how can I improve it?

public class StatementJob implements Job {

    private static final Logger LOGGER = LoggerFactory.getLogger(StatementJob.class);
    private static final String JOB_IS_DISABLED = "Uploads statement job is disabled and will not run";
    private static final String UNABLE_TO_CONTINUE = "Unable to continue";

    private final Configuration jobConfiguration;
    private final Log log;

    public StatementJob(JobConfiguration jobConfiguration) {
        this.jobConfiguration = jobConfiguration;
        this.log = new JobLog();
    }

    @Override
    public Log execute() {

        LOGGER.info("Stating uploading statements");
        final JobProperties properties;
        try {
            properties = jobConfiguration.loadProperties();
        } catch (BadConfigurationException e) {
            return log.addError(e.getMessage());
        }

        if (!properties.isRunJob()) {
            return log.addSuccess(JOB_IS_DISABLED);
        }

        final Employee statementUploader = Employee.load(properties.getStatementUploader());
        final FileStatements fileStatements = new Statements(properties.getDirectories());

        fileStatements
                .getInboundStatements()
                .forEach(inboundStatement -> {

                    try {
                        fileStatements.moveToWipLocation(inboundStatement);
                        fileStatements
                                .getWipStatements()
                                .forEach(wipFileStatement -> {

                                    try {
                                        new UploadableDocument(statementUploader, wipFileStatement).upload();
                                        fileStatements.moveToArchiveLocation(wipFileStatement);
                                    } catch (FailedToUploadDocumentException e) {
                                        log.addError(e.getMessage());
                                        try {
                                            fileStatements.moveToErrorLocation(wipFileStatement);
                                        } catch (UnableToMoveFileToDirectoryException ex) {
                                            LOGGER.error(UNABLE_TO_CONTINUE, e);
                                            throw new FatalJobException(UNABLE_TO_CONTINUE, e);
                                        }
                                    } catch (UnableToReadFileException | UnableToMoveFileToDirectoryException e) {
                                        LOGGER.error(UNABLE_TO_CONTINUE, e);
                                        throw new FatalJobException("Unable to continue", e);
                                    }
                                });
                    } catch (UnableToMoveFileToDirectoryException e) {
                        LOGGER.error(UNABLE_TO_CONTINUE, e);
                        throw new FatalJobException("Unable to continue", e);
                    }
                });

        return log;
    }

}

What does the code do?

There is FileStatements which represents files, called Statement, stored in the file system in the inbound directory.
When StatementJob is executed it moves Statements from the inbound to work in progress directory.
Then Statements from the work in progress directory are uploded to some storage.
When the upload is succesfull, the Statements are moved to the archive statement folder. Statements which failed to upload are moved to the error statement folder.
Log is used to collect information about the job that will be emailed to whomever needs to know the status. This is very primitive at this point.


texts: Ping and write the status of a timer on a GUI thread

texts:
I am running a timer for updating some of my operations, such as buffers. But I also want ping to be added.

It is added, but I think it can be improved.  This is because I think it's running on the GUI thread, which is not preferred as it will freeze the GUI if something goes wrong with it.  I think such a thing has occurred, and I haven't been able to locate the problem until now.

But, I am not asking for the solution for that problem, as there are other stuff.  I am basically asking for ways to improve this piece of code as it doesn't seem to be optimal in any way.

private void timer1_Tick(object sender, EventArgs e)
{
    try
    {
        if (waveProvider.BufferedDuration.Milliseconds > 40 && AudioDevices.SelectedItem.ToString() != "Wasapi Loopback")
        {
            waveProvider.ClearBuffer();
            TimesBufferClear++;
        }
        currping.Text = "Current Buffer: " + waveProvider.BufferedDuration.Milliseconds.ToString() + " Clear: " + TimesBufferClear.ToString() + " Ping: " + pingSender.Send(otherPartyIP.Address).RoundtripTime.ToString() + " Buffer: " + SendStream.BufferMilliseconds;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message + "Timer");
    }
}

As you can see, I have a label which contains all the things I want to update.  It is fine, and I'd rather have one than many in this case.

But is this a good way to do it?  It doesn't seem that good when I look at it; it's just a mess, even if it works.

Also, the PingSender is created and disposed of outside of the thread.  I suppose that is better than just "using" it every timer run.

Are there any ideas of what can be done to improve it?


texts: Kullback-Leibler (KL) distance between inside and outside contour code

texts:
I am trying implement the distance measurement between two distributions. The detail is described in here and input image . Let short summarize the idea of the paper:

The input image is divided into inside region and outside region by using Heaviside function H
Calculate the distribution inside region and outside region, in which phi is boundary of inside and outside.
Calculate the Kullback-Leibler distance

I implement that scheme, but I have three problems:

Log function in paper is log or log2 in matlab?
Log(0) is infinite, But we know that distribution result will return many 0 values. How to ignore it? In my case, I plus with eps value, some people add h1(h1==0)=1, which is correct?
Could you see my code? Is it correct? I am not sure about my implementation.

This is my code to implement that scheme:

function main()
Img=imread('1.bmp');%please download at above link
Img=double(Img(:,:,1));
%% Initial boundary
c0=2;  %const value
phi = ones(size(Img(:,:,1))).*c0;
phi(26:32,28:34) = -c0;
%% Heaviside function
epsilon=1
Hu=0.5*(1+(2/pi)*atan(phi./epsilon));

%% Inside and outside image
inImg=Img.*(1-Hu);
outImg=Img.*(Hu);
%% Let caclulate KL distance
h1 =  histogram(inImg, 256, 0, 255); %Histogram of inside
h2 =  histogram(outImg, 256, 0, 255);%Histogram of outside
lamda1=KLdist(h1,h2) % distance from h1 to h2
lamda2=KLdist(h2,h1) % distance from h2 to h1
end

%%%%%%%%%% function for KL distance%%%%%%%%%%%%%%%
function [d1,d2]=KLdist(h1,h2)
d1=sum(h1.*log2(h1+eps)-h1.*log2(h2+eps))
d2=sum(h2.*log2(h2+eps)-h2.*log2(h1+eps))
end
%%%%%%%%%%function for histogram calculation%%%%%%
function [h,bins] = histogram(I, n, min, max)
I = I(:);
range = max - min;
drdb = range / double(n); % dr/db - change in range per bin
h = zeros(n,1);
bins = zeros(n,1);
for i=1:n
    % note: while the instructions say "within integer round off" I'm leaving
    %       this as float bin edges, to handle the potential float input
    %       ie - say the input was a probability image.
    low = min + (i-1)*drdb;
    high = min + i*drdb;
    h(i) = sum( (I>=low) .* (I<high) );
  bins(i) = low;
end
h(n) = h(n) + sum( (I>=(n*drdb)) .* (I<=max) ); % include anything we may have missed in the last bin.

h = h ./ sum(h); % "relative frequency"
end


texts:
Here is my code which will read XML files and import the data to MongoDb. Do you have any comments on my code?
const fs = require('fs');
const xml2js = require('xml2js');
const log = console.log;
const path = require( "path" );

const folderPath = 'D:/Data';
const folder = folderPath;

fs.readdirSync(folder).forEach(file => {

    const extname = path.extname(file);
    const filename = path.basename(file, extname);
    const absolutePath = path.resolve(folder, file);

    const parser = new xml2js.Parser({
        mergeAttrs: true,
        explicitArray: false
    });

    fs.readFile(absolutePath, (err, data) => {

        parser.parseString(data, (err, result) => {

            var MongoClient = require('mongodb').MongoClient;
            var url = "mongodb://localhost:27017/";

            MongoClient.connect(url, function (err, db) {
                if (err) throw err;

                var dbo = db.db("myDb");

                dbo.collection("entity").insertMany(result, function (err, res) {
                    if (err) throw err;

                    db.close();
                });
            });
        });
    });

});

I tested that it works for few files but for hundreds of files, it can throw an error.


texts:
As my first Python program I have written a simple BMI calculator. I want to know how i can improve this program. Would functions be useful? How can I improve the way that it is presented?

#Introduce BMI Calculator
print ('Welcome to BMI Calculator!')

#Ask if user would like to use Metric or Imperial units
input1 = input ('Would you like to input Metric units or Imperial units? ')

#If Imperial... If Metric... Calculate BMI...
if input1 == 'imperial':
    heightIN = float(input('Input your height in Inches (in): '))
    weightLB = float(input('Input your weight in Pounds (lbs):' ))
    BMI = round(((weightLB/(heightIN*heightIN))*703), 2)

    if BMI >= 19 and BMI <= 24:
        print ('Your BMI is', BMI, 'so, you are healthy weight!')

    elif BMI >= 25 and BMI <= 29:
        print ('Your BMI is', BMI, 'so, you are overweight!')

    elif BMI >= 30 and BMI <= 39:
        print ('Your BMI is', BMI, 'so, you are obese!')

    elif BMI > 39:
        print ('Your BMI is', BMI, 'so, you are extremely obese!')

    elif BMI < 19:
        print ('Your BMI is', BMI, 'so, you are underweight!')

elif input1 == 'metric':
    heightCM = float(input('Input your height in Centimeters (cm): '))
    weightKG = float(input('Input your weight in kilograms (kg): '))
    heightM = heightCM*0.01
    BMI = round((weightKG/(heightM*heightM)), 2)

    if BMI >= 19 and BMI <= 24:
        print ('Your BMI is', BMI, 'so, you are healthy weight!')

    elif BMI >= 25 and BMI <= 29:
        print ('Your BMI is', BMI, 'so, you are overweight!')

    elif BMI >= 30 and BMI <= 39:
        print ('Your BMI is', BMI, 'so, you are obese!')

    elif BMI > 39:
        print ('Your BMI is', BMI, 'so, you are extremely obese!')

    elif BMI < 19:
        print ('Your BMI is', BMI, 'so, you are underweight!')

else:
    print ('There was an error with your input. Please restart the program!')


texts: Representing LXC component configuration in django

texts:
I want to represent components of LXC in django; so I defined them in models: Container, Network, Profile and Devices where each one of them have a set of attributs, but when it comes to representing configuration (each component has a set of configuration fields), I don't know if it is the right approach to follow. Below is an example code, given the fact that I use MongoDB as a database engine.

class Container(models.Model):
    name = models.CharField()
    remote = models.CharField()
    architecture = models.CharField()
    creation = models.DateField()
    status = models.CharField()
    containerType = models.CharField()
    pid = models.IntegerField()
    config = EmbeddedModelField('ContainerConfig')

class ContainerConfig(models.Model):
    bootAutostart = models.BooleanField()
    bootAutoStartDelay = models.IntegerField()
    bootAutostartPriority = models.IntegerField()
    bootHostShutDownTimeOut = models.IntegerField()
    bootStopPrority = models.IntegerField()
    env = models.CharField()
    limitsCpu = models.CharField()
    limitsCpuAllowance = models.CharField()
    limitsCpuPriority = models.CharField()
    limitsDiskPriority = models.IntegerField()
    limitsHugePages64K = models.CharField()

Is it right to follow this approach?


texts:
I've written code that performs a merge of two streams of sorted time series data. The first stream is taken to be the primary and any successive streams are merged into it, using the nearest timestamp match to align data. It can be assumed that there will only ever be one match per primary timestamp.

const primary = [[1,0], [2, 5], [3, 10], [4, 0]];
const aux = [[1.3, 10], [3.4, 20], [6.9, 30]];

let j = 0;
const halfPeriod = (primary[1][0] - primary[0][0]) / 2;

aux.map(val => {
    tsAux = val[0];
    while(j < primary.length) {
        if (tsAux < primary[j][0] + halfPeriod) {
            primary[j].push(val[1]);
            return j++;
        } else {
            primary[j].push(null);
            j++;
        }
    }
});
console.log(primary);

I can't quite put my finger on it but something about writing it felt sub-optimal (maybe just because I haven't used any ES6/2017/2018 syntax!), so my question is: is my gut feel correct and is there a neater and more robust way to do this? As a bonus, can it be neatly extended to accommodate:

any number of auxiliary data streams rather than just one
a primary data stream that doesn't have consistent time increments?


texts:
I'm working with lots of code that looks like this:

-(void)switchToPercent{
    int tmp;

    tmp = (statisticsObject.idag3_orig != 0) ? statisticsObject.idag3_orig : [self compareValue:statisticsObject.idag2 withValue:statisticsObject.idag3];
    statisticsView.idag3.text = [NSString stringWithFormat:@"%i", tmp];

    tmp = (statisticsObject.igar3_orig != 0) ? statisticsObject.igar3_orig : [self compareValue:statisticsObject.igar2 withValue:statisticsObject.igar3];
    statisticsView.igar3.text = [NSString stringWithFormat:@"%i", tmp];

    tmp = (statisticsObject.veckan3_orig != 0) ? statisticsObject.veckan3_orig : [self compareValue:statisticsObject.veckan2 withValue:statisticsObject.veckan3];
    statisticsView.veckan3.text = [NSString stringWithFormat:@"%i", tmp];

    tmp = (statisticsObject.manaden3_orig != 0) ? statisticsObject.manaden3_orig : [self compareValue:statisticsObject.manaden2 withValue:statisticsObject.manaden3];
    statisticsView.manaden3.text = [NSString stringWithFormat:@"%i", tmp];

    tmp = (statisticsObject.kvartalet3_orig != 0) ? statisticsObject.kvartalet3_orig : [self compareValue:statisticsObject.kvartalet2 withValue:statisticsObject.kvartalet3];
    statisticsView.kvartalet3.text = [NSString stringWithFormat:@"%i", tmp];

    tmp = (statisticsObject.aret3_orig != 0) ? statisticsObject.aret3_orig : [self compareValue:statisticsObject.aret2 withValue:statisticsObject.aret3];
    statisticsView.aret3.text = [NSString stringWithFormat:@"%i", tmp];

    tmp = (statisticsObject.r123_orig != 0) ? statisticsObject.r123_orig : [self compareValue:statisticsObject.r122 withValue:statisticsObject.r123];
    statisticsView.r123.text = [NSString stringWithFormat:@"%i", tmp];

    [self formatText:statisticsView.idag3];
    [self formatText:statisticsView.igar3];
    [self formatText:statisticsView.veckan3];
    [self formatText:statisticsView.manaden3];
    [self formatText:statisticsView.kvartalet3];
    [self formatText:statisticsView.aret3];
    [self formatText:statisticsView.r123];
}

I'm trying to remove the repetition as much as I can. Originally, the business logic was repeated once for each of the properties shown, but I've moved that into a method named compareValue:withValue:. I've also introduced the conditional operator to remove larger ifelse blocks. I imagine the next step is to reduce the seven chunks of code into one, and iterate over a list of keywords, but how?


texts: Find the index of the element with maximum absolute deviation

texts:
I just completed a Codility test. The question presented was pretty simple but I may have over thought it.

Question:

  Find the element which has the highest absolute deviation from the mean. E.g. Array = [1,1,-1,1].The average is 0.5, so the absolute deviation for each element in the array is [0.5,0.5,1.5,0.5] (note that the -1.5 goes to 1.5 after taking the absolute values of each deviation value) and thus the index with the max absolute deviation is 2 (the index starts from 0).

My code was:

def solution(A):
    if len(A) > 0:
        average = sum(A)*1.0/len(A)
        max_avg = 0
        pos = 0
        for i in range(len(A)):
            if abs(A[i] - average) > max_avg:
                max_avg = abs(A[i] - average)
                pos = i
        return pos
    else:
        return -1

If there's a tie between the max deviation elements i.e. two or more indexes have the same absolute deviation then either of them can be given. The required time-complexity is \$O(N)\$ and space-complexity is \$O(1)\$. In theory, this problem should be really easy but this was my first technical test and I might have overlooked something. Did I possibly miss anything?


texts: Order a list of tuples or a NumPy array in a specific format

texts:
I have a list of tuples or a NumPy array (it can be either of them as the variable comes as a list and will end up being a NumPy array) and I want to order them in a specific way (that I am not able to phrase it).

Let's make an example:

boundary=[(1, 2), (1, 3), (3, 4), (2, 4)];

Desired output:

[(1, 2), (2, 4), (4, 3), (3, 1)]

(or a NumPy array of 2x4 will do also)

The logic behind it is to get the first row, read its last value and find that value in the array. Get that tuple, swap it to make the found value the first one (if needed) and repeat.

The idea is that ...N),(N... , N is the same. There will always be 2 and only 2 repetitions of the numbers.

I can figure out how to do it "C-like", iterating, searching, swapping etc, but I wonder if there is a pythonic way (those amazing one-liners) of achieving it. Ultimately I am just trying to increase my Python-fu.

boundary=np.array(boundary)
ordered=np.zeros([boundary.shape[0],boundary.shape[1]]);
idx=0
idy=0
for i in range(len(boundary)):
    ordered[i,0]=boundary[idx,idy]
    ordered[i,1]=boundary[idx,(idy+1)%2]
    ix,iy=np.where(boundary==boundary[idx,(idy+1)%2])
    for j in range(len(ix)):
        isused=np.where(np.all(ordered==boundary[ix[j],],axis=1))
        if isused[0].size==0:
            idx=ix[j]
            idy=iy[j]
            print idx, idy
            break


texts: ConcurrentDictionary, Store Key and Compression

texts:
So i have a  ConcurrentDictionary<int, string>

The "int" is merely the way i store the key, which i want more recommendations on.

Currently i do like this.

        public string memoize(Func<string, uint, string> functor, string code, uint id)
    {
        //Used for the lookup to identify if it's been inputed before
        int codeHash = code.GetHashCode();
        string functor_return;

        if (_compilerCache.TryGetValue(codeHash, out functor_return))
            return functor_return;

        functor_return = functor?.Invoke(code, id);
        _compilerCache[codeHash] = functor_return;

        return functor_return;
    }

Why do i use GetHashCode()?
I don't know, it's just the only way i could figure out rather than store the string value itself.

I also save this "Cache", and hence why the compression and key is important.

    //Serialize and Compress object to file
    public static void SerializeObject<T>(string filename, T obj)
    {
        using (Stream stream = File.Open(filename, FileMode.Create))
        using (var cStream = new GZipStream(stream, CompressionLevel.Optimal))
        {
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            binaryFormatter.Serialize(cStream, obj);
        }
    }

    //DeSerialize and Decompress object from file
    public static T DeSerializeObject<T>(string filename)
    {
        T objectToBeDeSerialized = default(T);
        try
        {
            using (Stream stream = File.Open(filename, FileMode.Open))
            using (var cStream = new GZipStream(stream, CompressionMode.Decompress))
            {
                BinaryFormatter binaryFormatter = new BinaryFormatter();
                objectToBeDeSerialized = (T)binaryFormatter.Deserialize(cStream);
            }

        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message + " : Error DeSerializing Cache, corrupted?");
        }

        return objectToBeDeSerialized;
    }

It all works, but i would like to improve it.
I don't really know if even GetHashCode is a good way to do this thing,
i just took it randomly as before i just stored the value itself.

The values are usually around 10-20 characters i guess, usually very similar (which should be compression friendly i guess).

So can anything be done to improve this?


texts: Alternative to env that accepts arguments

texts:
It is commonly known that env does not allow passing of arguments - at least not reliably. There are various workarounds described here, but they felt to hacky and unreadable to me. That's why I developed an alternative, ex.

ex:

#!/usr/bin/env bash

# save the string given in the sheband line
shebang_line="$1"

# remove it from $@
shift

# execute
# not quoting shebang_line is intentional
eval $(echo $shebang_line "$@")

test.py

#!/usr/local/bin/ex python "-i"
# quoting the arguments is possible
import sys

print('hello', sys.argv)

It works surprisingly well, but I don't like the eval-echo thing. I tried experimenting with arrays, but haven't found a solution. Any tips?


texts: Find a file in current or parent directories

texts:
As a Rust beginner, I would like to know how I could improve the following points considering the function below:

Handling Path and PathBuf types correctly;
Performance;
Rust writing style in general.

Here is the function, decorated with a full working example:

use std::env;
use std::path::{Path, PathBuf};

const RUSV_FILENAME: &str = ".rusv.json";

/**
 * Find a rusv file in the current or parent directories of the given directory.
 */
fn find_rusv_file(starting_directory: &Path) -> Option<PathBuf> {
    let mut directory = starting_directory;
    let rusv_filename = Path::new(&RUSV_FILENAME);

    loop {
        let filepath: PathBuf = [
            directory,
            rusv_filename
        ].iter().collect();

        if filepath.is_file() {
            return Some(filepath);
        }

        match directory.parent() {
            Some(parent) => directory = parent,
            None => return None,
        }
    }
}

fn main() -> std::io::Result<()> {
    let path = env::current_dir()?;

    match find_rusv_file(&path) {
        Some(filepath) => println!("Rusv file was found: {:?}", filepath),
        None => println!("No rusv file was found."),
    };

    Ok(())
}

My question is not about:

Creating a more generic function (e.g. by giving the filename as an argument);
Anything related to the main() function.


texts:
I was asked to code a solution in Python in 15 mins. Print all possible word combinations for a given phone number. Numbers 0 and 1 will not be part of the phone number, the numbers can be repeated, phone numbers can be of any length.
For example if the number is 245:

All combinations of 'ABC', 'GHI' and 'JKL" is:
['AGJ', 'AGK', 'AGL', 'AHJ', 'AHK', 'AHL', 'AIJ', 'AIK', 'AIL', 'BGJ', 'BGK', 'BGL', 'BHJ', 'BHK', 'BHL', 'BIJ', 'BIK', 'BIL', 'CGJ', 'CGK', 'CGL', 'CHJ', 'CHK', 'CHL', 'CIJ', 'CIK', 'CIL']

In 15 mins, all I could come up with was to combine 2 numbers at a time and then combine the result with the third number and so on.

numbs = {'2':'ABC', '3':'DEF', '4':'GHI', '5':'JKL', '6':'MNO', '7':'PQRS', '8':'TUV', '9':'WXYZ'}

def combo(phnum):
    if len(phnum) == 1: return list(numbs[phnum[0]])
    else: result = combo(phnum[:-1])
    return [(ch1+ch2) for ch1 in result for ch2 in numbs[phnum[-1]]]

After I going home, I checked out itertools and found out about product. Came up with the following one liner. Is there a better way to solve this problem?

from itertools import product
def combo(phnum):
    return [''.join(tup) for tup in list(product(*[numbs[ch] for ch in phnum]))]


texts:
This is my first small react app. I got a little experience in Vue.js before, but I wan't to learn the basics and best practices of react.js too. So, this is a small library app (containing link tipps with title, desc, topics) that shows all library items with the possibility to filter them by clicking on specific keywords. Pretty easy, but I want to know from you if I coded some bad practices or anti patterns or sth. like that.

main.js

import React from 'react'
import ReactDOM from 'react-dom'
import Library from './components/Library.jsx'
const libraryContainer = document.getElementById('library')
ReactDOM.render(<Library />, libraryContainer)

Library.jsx

import React from 'react'
import Filters from './Filters.jsx'
import LibraryItem from './LibraryItem.jsx'
import SmoothScroll from 'smooth-scroll'

const libraryContainer = document.getElementById('library')
const scroll = new SmoothScroll('a[href*="#"]', { offset: 100 })

export default class Library extends React.Component {
  state = {
    lib: [],
    filteredLib: [],
    topics: [],
    filter: '',
  }

  filterLib = () => {
    if (this.state.filter === '') return this.setState({ filteredLib: this.state.lib })

    this.setState(
      {
        filteredLib: this.state.lib.filter(item => {
          if (item.topics) return item.topics.includes(this.state.filter)
        }),
      },
      () => scroll.animateScroll(libraryContainer),
    )
  }

  handleFilters = topic => {
    const topicsClone = JSON.parse(JSON.stringify(this.state.topics))
    topicsClone.forEach(item => {
      if (item.id === topic.id) {
        item.active = !item.active
        item.active ? null : (topic.name = '')
      } else {
        item.active = false
      }
    })
    this.setState({ topics: topicsClone, filter: topic.name }, () => {
      this.filterLib()
    })
  }

  componentDidMount() {
    let topicsArr = []

    fetch('/api')
      .then(res => res.json())
      .then(data => {
        this.setState({ lib: data.lib, filteredLib: data.lib })
        data.topics.map((val, key) => {
          topicsArr.push({ name: val, id: key, active: false })
        })
        this.setState({ topics: topicsArr })
      })
      .catch(err => console.log(err))
  }

  render() {
    if (this.state.filteredLib.length > 0) {
      return (

          <Filters topics={this.state.topics} handler={this.handleFilters} />

              {this.state.filteredLib.map(item => {
                return <LibraryItem key={item._id} {...item} />
              })}

      )
    } else {
      return
Wir konnten leider nichts finden :(

    }
  }
}

Filters.jsx

import React from 'react'

export default function Filters(props) {
  const handleClick = topic => {
    props.handler(topic)
  }

  return (

Filter

        {props.topics.map(topic => {
          return (

 handleClick(topic)}>
              {topic.name}

          )
        })}

  )
}

LibraryItem.jsx

import React from 'react'

export default function LibraryItem(props) {
  return (

{props.type}

{props.title}

{props.teaser}

  )
}


texts:
I needed to write a simple function that could raise a value x to the n'th power. I found out I could write this function in 2 different ways:

(defun power1 (x n)
  "Raise x to the n'th power"
  (cond ((zerop n)
          1.0)
         ((< n 0.0)
          (/ 1.0 (reduce #'* (loop for i below (- n) collect x))))
         (t
          (reduce #'* (loop for i below n collect x)))))

(defun power2 (x n)
  "Raise x to the n'th power"
  (cond ((zerop n)
         1.0)
        ((< n 0.0)
         (/ 1.0 (power2 x (- n))))
        (t
         (* x (power2 x (- n 1))))))

Now, I was curious to known which of these 2 functions is better. So I ran some calls to these functions wrapped in a call to the time function. Here are some results:

(time (power1 2 100))
took 0 milliseconds (0.000 seconds) to run.
2,248 bytes of memory allocated.

(time (power2 2 100))
took 0 milliseconds (0.000 seconds) to run.
800 bytes of memory allocated.

(time (power1 2 1000))
took 0 milliseconds (0.000 seconds) to run.
82,232 bytes of memory allocated.

(time (power2 2 1000))
> Error: FLOATING-POINT-OVERFLOW detected

The first function takes more memory, since it creates an entire list, while the second function uses recursion. But the first one can handle larger numbers.

How can I choose which of these functions is the best? Choose for memory usage, readability, Lisp-like style, range of inputs it can handle, .... Maybe a function that uses tail-call optimization would be even better?

(defun power3 (x n)
  (labels ((pow (x n acc)
             (if (zerop n)
                 acc
                 (pow x (- n 1) (* acc x)))))
    (cond ((zerop n) 1.0)
          ((< n 0.0) (/ 1.0 (pow x (- n) 1.0)))
          (t (pow x n 1.0)))))

(time (power3 2 100))
took 1 milliseconds (0.001 seconds) to run.
800 bytes of memory allocated.


texts: How do I make this C# code more efficient, in terms of FPS and time complexity?

texts:
Image<Bgr, Byte> frame = _capture.QueryFrame();
Image<Gray, Byte> grayFrame = frame.Convert<Gray, Byte>();
grayFrame._EqualizeHist();
MCvAvgComp[][] facesDetected = grayFrame.DetectHaarCascade(_faces, 1.1, 1,
    Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.FIND_BIGGEST_OBJECT, new Size(20, 20));

if (facesDetected[0].Length == 1)
{
    MCvAvgComp face = facesDetected[0][0];

    #region Search ROI based on Face Metric Estimation -

    Int32 yCoordStartSearchEyes = face.rect.Top + (face.rect.Height * 3 / 11);
    Point startingPointSearchEyes = new Point(face.rect.X, yCoordStartSearchEyes);
    Size searchEyesAreaSize = new Size(face.rect.Width, (face.rect.Height * 3 / 11));
    Rectangle possibleROI_eyes = new Rectangle(startingPointSearchEyes, searchEyesAreaSize);

    #endregion

    int widthNav = (frame.Width / 10 * 2);
    int heightNav = (frame.Height / 10 * 2);
    Rectangle nav = new Rectangle(new Point(frame.Width / 2 - widthNav / 2, frame.Height / 2 - heightNav / 2),
        new Size(widthNav, heightNav));
    frame.Draw(nav, new Bgr(Color.Lavender), 3);
    Point cursor = new Point(face.rect.X + searchEyesAreaSize.Width / 2,
        yCoordStartSearchEyes + searchEyesAreaSize.Height / 2);

    grayFrame.ROI = possibleROI_eyes;
    MCvAvgComp[][] eyesDetected = grayFrame.DetectHaarCascade(_eyes, 1.15, 3,
        Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(20, 20));
    grayFrame.ROI = Rectangle.Empty;

    if (eyesDetected[0].Length != 0)
    {
        frame.Draw(face.rect, new Bgr(Color.Yellow), 1);

        foreach (MCvAvgComp eye in eyesDetected[0])
        {
            Rectangle eyeRect = eye.rect;
            eyeRect.Offset(possibleROI_eyes.X, possibleROI_eyes.Y);
            grayFrame.ROI = eyeRect;
            frame.Draw(eyeRect, new Bgr(Color.DarkSeaGreen), 2);

            frame.Draw(possibleROI_eyes, new Bgr(Color.DeepPink), 2);

            if (nav.Left < cursor.X && cursor.X < (nav.Left + nav.Width)
                && nav.Top < cursor.Y && cursor.Y < nav.Top + nav.Height)
            {
                LineSegment2D CursorDraw = new LineSegment2D(cursor, new Point(cursor.X, cursor.Y + 1));
                frame.Draw(CursorDraw, new Bgr(Color.White), 3);

                int xCoord = (frame.Width * (cursor.X - nav.Left)) / nav.Width;
                int yCoord = (frame.Height * (cursor.Y - nav.Top)) / nav.Height;

                Cursor.Position = new Point(xCoord, yCoord);
            }
        }
    }
    imageBoxFrame.Image = frame;
}


texts:
I've been doing some Codility challenges lately and the MaxCounters challenge got me stumped:

  You are given N counters, initially set to 0, and you have two
  possible operations on them:

  increase(X) — counter X is increased by 1,
  max counter — all counters are set to the maximum value of any counter.

  A non-empty array A of M integers is given. This array represents consecutive operations:

  if A[K] = X, such that 1 ≤ X ≤ N, then operation K is increase(X),
  if A[K] = N + 1 then operation K is max counter.

  [...]
  The goal is to calculate the value of every counter after all operations.

Below is my solution in Python.  I can't reach 100% on it even though I am pretty confident it is an O(n+m) complexity solution. Codility fails it and says its an O(n*m) complexity solution.

def solution(N, A):
    counter = [0] * N
    max_val = 0

    for v in A:

        if v <= N and v >= 1:
            if counter[v-1] < max_val+1:
                counter[v-1] = max_val+1
            else:
                counter[v-1] += 1
        else:
            max_val = max(counter)

    for i in range(len(counter)):
        if counter[i] < max_val:
            counter[i] = max_val

    return counter


texts:
I'm 100% sure improvements can be made. Any help is greatly appreciated. I should add that I am new to OOP and am not sure if I'm using this the right way.

<?php
defined('_VALID') or die('Restricted Access!');

class email
{
public function addtemplate($html, $public)
{
    global $conn;

    $sql = "INSERT INTO emailtemplate SET id = '', parentid = '" . $_SESSION['uid'] . "', html = '" . mysql_escape_string($html) . "', public = '" . $public . "', added = '" . time() . "'";
    $conn->execute($sql);

    if ($conn->affected_rows() == 1) {
        return true;
    } else {
        return false;
    }
}

public function addtext($subject, $text)
{
    global $conn;

    $sql = "INSERT INTO emailtext SET id = '', parentid = '" . $_SESSION['uid'] . "', subject = '" . mysql_escape_string($subject) . "', text = '" . mysql_escape_string($text) . "', added = '" . time() . "'";
    $conn->execute($sql);

    if ($conn->affected_rows() == 1) {
        return $conn->insert_id();
    } else {
        return false;
    }
}

public function addqueue($to, $text, $template)
{
    global $conn;

    $sql = "INSERT INTO emailqueue SET eid = '', parentid = '" . $_SESSION['uid'] . "', eto = '" . $to . "', etext = '" . $text . "', etemplate = '" . $template . "', added = '" . time() . "', status = 'pending'";
    $conn->execute($sql);

    if ($conn->affected_rows() == 1) {
        return true;
    } else {
        return false;
    }
}

public function delete($eid)
{
    global $conn;

    $sql = "DELETE FROM emailqueue WHERE eid = '" . $eid . "' AND parentid = '" . $_SESSION['uid'] . "' AND status = 'pending'";
    $conn->execute($sql);

    if ($conn->affected_rows() == 1) {
        return true;
    } else {
        return false;
    }
}

public function generate($eid)
{
    global $conn;

    $sql   = "SELECT q.*, x.*, t.*, c.id, c.fname, c.email, p.UID, p.company, p.hash FROM emailqueue AS q, emailtext AS x, emailtemplate AS t, clients AS c,
                signup AS p WHERE q.eid = '" . $eid . "' AND q.etemplate = t.id AND q.etext = x.id AND c.id = q.eto AND p.UID = q.parentid";
    $rs    = $conn->execute($sql);
    $email = $rs->getrows();
    $email = $email[0];

    $find    = array(
        '{name}',
        '{email}',
        '{company}',
        '{hash}'
    );
    $replace = array(
        $email['fname'],
        $email['email'],
        $email['company'],
        $email['hash']
    );

    $body  = str_replace('{bodytext}', $email['text'], stripslashes($email['html']));
    $final = str_replace($find, $replace, $body);

    $array['body'] = stripslashes($final);
    $array['subject'] = $email['subject'];
    $array['to'] = $email['email'];

    return $array;
}

public function from($eid)
{
    global $conn;

    $sql   = "SELECT q.*, s.UID, s.email, s.company FROM emailqueue AS q, signup AS s WHERE q.eid = '" . $eid . "' AND q.parentid = s.UID";
    $rs    = $conn->execute($sql);
    $email = $rs->getrows();
    $email = $email[0];

    $array['company'] = $email['company'];
    $array['email']   = $email['email'];

    return $array;
}

public function send($eid)
{
    $msg      = $this->generate($eid);
    $from    = $this->from($eid);

    $headers = "From: " . $from['company'] . " <noreply@emailsmsmarketing.com>\r\n";
    $headers .= "Reply-To: " . $from['email'] . "\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html; \r\n";

    if (mail($msg['to'], $msg['subject'], $msg['body'], $headers)) {
        return true;
    } else {
        return false;
    }
    }
}
?>


texts: Make two MySQL statements & loop quicker

texts:
Would a join be quicker here? I tested a join and it's almost the same time! Basically its a ray casting mechanism that finds all properties in a square on GMaps, and then there's a javascript function that finds the point in polygon. So at any time, the records fetched could be in the tens of thousands.

The first statement finds all the postcodes in the latLongs, the second one finds all the properties in that postcode.

Bear in mind I just knocked this up very quickly, as the raycasting javascript took up most of my designated time

    $latlongs = explode("), ", $_GET['bounds']);

$first = ($latlongs[0]);
$first = substr($first, 2);

$second = $latlongs[1];
$second = substr(substr($second, 1),0, -2);

$first = explode(", ", $first);
$second = explode(", ", $second);
//$result = mysql_query("SELECT * FROM `postcodeGoogle` WHERE lat");
$lat1 = $first[0];
$lat2 = $second[0];

$long1 = $first[1];
$long2 = $second[1];
$resultE = "";
$result = mysql_query("SELECT * FROM `postcode`.`postcodeGoogle` WHERE (`lat` BETWEEN '$lat1' AND '$lat2') AND (`long` BETWEEN '$long1' AND '$long2')")or die(mysql_error());

while($row = mysql_fetch_assoc($result)){

        $prop = mysql_query("SELECT * FROM `digital_hub`.`address_list` WHERE `postcode`='".$row['postcode']."'")or die(mysql_error());
        echo $row['postcode'];
        echo mysql_num_rows($prop);
        while($propRow=mysql_fetch_assoc($prop)){
            $resultE = $resultE."(".$row['lat'].", ".$row['long'].") || ";
        }
}
$resultE = substr($resultE, 0, -4);
echo $resultE;


texts: Parsing a slow-loading webpage with scrapy in combination with selenium

texts:
I've written a scraper in Python scrapy in combination with selenium to scrape 1000 company names and their revenue from a website. The site has got lazy-loading method enabled so it is not possible to make the site load all the items unless the scraper is able to scroll that page downmost. However, my scraper can reach the lowest portion of this webpage and parse the aforesaid category flawlessly. I've set explicit wait in my scraper instead of any hardcoded delay so that it doesn't take longer than necessary.

As this is my first time to work with selenium along with scrapy, there might be scopes to do betterment of this script to make it more robust.

import scrapy
from selenium import webdriver
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class ProductSpider(scrapy.Spider):
    name = "productsp"
    start_urls = ['http://fortune.com/fortune500/list/']

    def __init__(self):
        self.driver = webdriver.Chrome()
        self.wait = WebDriverWait(self.driver, 10)

    def parse(self, response):
        self.driver.get(response.url)
        check_height = self.driver.execute_script("return document.body.scrollHeight;")
        while True:
            self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
            try:
                self.wait.until(lambda driver: self.driver.execute_script("return document.body.scrollHeight;")  > check_height)
                check_height = self.driver.execute_script("return document.body.scrollHeight;")
            except:
                 break

        for item in self.driver.find_elements_by_css_selector(".row"):
            name = item.find_element_by_css_selector(".company-title").text
            revenue = item.find_element_by_css_selector(".company-revenue").text
            yield {"Title":name,"Revenue":revenue}


texts: Accessing the end-selector in a X-dimensional array

texts:
I have a large Array that comes from Instagram's Public API, example of link:

https://www.instagram.com/kinfolk/?__a=1

And I'm accessing $response['graphql']['user'] which in turn results:

The Array in a Link - It's too big to be posted here. Also has nice format!

Unfortunately, in order for me to access the links to the pictures and any other data, I have to go down a few levels:

protected function get_links() {
    $response = $this->instagram_object->get_data(); /* the JSON decoded data */

    if( is_wp_error($response) ) {
        return new \WP_Error( 'invalid-json', esc_html__( 'Ouch. The data was not parsed correctly. Cannot continue.' ) );
    }

    foreach ( $response['graphql']['user'] as $item ) {
        if ( isset( $item['edges'] ) ) {
            foreach( $item['edges'] as $edge ) {
                $image = array('real_link' => 'src' );
                $image['real_link'] = 'https://www.instagram.com/p/' . $edge['node']['code'] . '?taken-by=' . $this->username;
                $image['src'] = $edge['node']['thumbnail_resources'][0]['src'];
                $images_links[] = $image;
            }
        }
    }
    return $images_links;
}

The code in a non-CMS environment, pure PHP:

<?php

function get_links() {

    $link = 'https://www.instagram.com/kinfolk/?__a=1';
    $data = file_get_contents($link);
    $response = json_decode($data, true);

    $image          = array( 'real_link' => 'src' );
    $image_links    = [];

    foreach ( $response['graphql']['user'] as $item ) {
        if ( isset( $item['edges'] ) ) {
            foreach( $item['edges'] as $edge ) {
                $image['real_link'] = 'https://www.instagram.com/p/' . $edge['node']['shortcode'] . '?taken-by=' . 'kinfolk';
                $image['src'] = $edge['node']['thumbnail_resources'][0]['src'];
                $images_links[] = $image;
            }
        }
    }
    return $images_links;
}

$instagram_images_links = get_links();

foreach( $instagram_images_links as $image ) {
    echo $image['real_link'] . ' and also ' . $image['src'];
    echo "

";
}

?>

It's no secret. The code is really ugly. Having to go in a foreach->if->foreach is not desireable and I noticed a dip in peformance when the images are being rendered, on top of it, I also push to another array.

My goals are:

1) Cut down on the loops.

2) Find a better way to create the associative array that holds both the link and the image_src link that I need to use.

A few edits, key-points:

1) The if ( isset( $item['edges'] ) ) is necessary, because within the bigger dimension of the array which is $item ($response['graphql']['user']) there are intially 17 items in the array, some are strings, some are booleans - I must decide which are the edges so that I can iterate over them.

How can I achieve this?


texts:
# unit tests for the /brands endpoint

from flask.ext import restful
import unittest
import os
import json
import sys

# tells sql_models to use the testing database
os.environ['API_TESTING'] = '1'
sys.path.append('/opt/backend_api/')

import api
from models import sql_models
from config import settings

class TestBrandsEndpoint(unittest.TestCase):

    def setUp(self):
        ''' fire up a test instance of the flask app '''
        self.app = api.APP.test_client()
        self.app.testing = True
        self.api = api.API
        self.test_brand_name = 'Momcorp'

    def tearDown(self):
        '''
            tear down app, reverse any changes made
            - remove the brand inserted in test_post()
        '''
        brand_object = sql_models.Brand.get_by_name(self.test_brand_name)
        if brand_object is not None:
            sql_models.Brand.delete_brand(brand_object)
        pass

    def test_post(self):
        '''
            insert a new brand into the database
            - only given field for test is the mandatory brand name but could(should?) be extended for the rest
            - test that the response status code == 201 and the returned brand object has the correct name
        '''
        result = self.app.post( '/' + settings.BASE_API_VERSION + '/brands',
                                headers=[('user-api-key', '<redacted>')],
                                data={'name':self.test_brand_name})
        # print(json.loads(result.data))
        self.assertEqual(result.status_code, 201)
        self.assertEqual(json.loads(result.data)['brand']['name'], self.test_brand_name)

    def test_get(self):
        '''
            get all brands and all currently running offers
            - sets the brand's 'offers_running' status
            - returns serialized brands like so: {'brands':[serialized_brands]}
            - test status code + name, id fields are not null
        '''
        result = self.app.get('/' + settings.BASE_API_VERSION + '/brands', headers=[('user-api-key','<redacted>')])
        self.assertEqual(result.status_code, 200)
        for brand in json.loads(result.data)['brands']:
            self.assertIsNotNone(brand['name'])
            self.assertIsNotNone(brand['id'])

if __name__ == '__main__':
    unittest.main()

Looking for advice about the 'right way' to do this. Not sure if I should have a separate file and class for each endpoint or if all endpoints+request types should just be their own methods in a larger TestAllEndpoints(unittest.TestCase) class. Any advice or opinions are appreciated.