texts: Reading a data file for performing a FFT

texts:
I'm a bit concerned about function usage. There are so many different libraries/possible ways to do something that I'm not sure if the way I'm working at the moment is reasonable, or what I could modify to have a better code.

Also, I'm searching on how to optimize this code, since for a small 25MB file the memory use goes to 800MB, and a bigger file goes OOM. What makes the memory to go so high?

Before I had strict IO writing, and was expecting to be it that caused to evaluate everything on the code, but going lazy didn't solve anything.

{-# LANGUAGE OverloadedStrings,BangPatterns #-}
import qualified Data.Attoparsec.Char8 as Ap
import Data.Attoparsec

import Control.Monad

import System.IO
import System.Environment
import Data.Maybe
import Data.List (unzip4,zip4,foldl')
import Data.List.Split
import Data.Bits

import Numeric.FFT
--import Data.Vector.Storable (fromList)

import Data.Complex
import Data.String (fromString)
import Data.ByteString.Internal
import qualified Data.ByteString.Char8 as B
import qualified Data.ByteString.Lazy.Char8 as Bl

newtype FourD b a = FourD  { getFourD  :: (a,a,a,b) }
instance (Show b, Show a) => Show (FourD b a) where
    show (FourD (a,b,c,d)) = "( "++show a++" , "++show b++" , "++show c++" , "++show d++" )"
instance Functor (FourD c) where
    fmap f (FourD  (x,y,z,d)) = FourD  (f x,f y,f z,d)

mgrav_per_bit = [ 18, 36, 71, 143, 286, 571, 1142 ]

aToG a = sign * uresult
    where   twocomp = if a>128
                    then 256-a
                    else a
            uresult = foldr (+) 0  $ zipWith (*)   mgrav_per_bit (map ( g . testBit  twocomp) [0..7] )
            sign = if a>128
                    then -1
                    else 1
            g True = 1; g False = 0

amap f g = map FourD . uncurry4 zip4 . map4 f g . unzip4 . map getFourD
    where map4 f g (a,b,c,d) = ( f a,f  b,f  c, g d)
          uncurry4 f (a,b,c,d) = f a b c d

splitN n x =  helper x 0
    where
    l = length x
    helper x c = if (c+n)>l
                    then []
                    else (Prelude.take n x):(helper  (drop 1 x) (c + 1))

quare = foldr (\a b-> b+(magnitude a)) 0

getTime :: (FourD String a)-> String
getTime (FourD (_,_,_,t)) = t

filterAcc (FourD (x,_,_,t)) = if x > 50
                                then  (Bl.pack . B.unpack) $ "yes" `B.append` t
                                else  Bl.pack ""

parseAcc :: Parser (FourD B.ByteString Int)
parseAcc = do   satisfy (== 40)
                x <- Ap.decimal
                string ","
                y <- Ap.decimal
                string ","
                z <- Ap.decimal
                string ","
                time <-  takeTill (== 41)
                return $ FourD (x,y,z,time)
                where isDigit w = w >= 48 && w <= 57
                      isNot w = w/=29

readExpr input = case parse parseAcc  input of
     Done b val -> val
     Partial p-> undefined
     Fail a b c -> undefined

fftAcross = map ( map (/32) . fft ) . splitN 32

main = do

    filename <- liftM head getArgs
    filehandle <- openFile "lol.txt" WriteMode
    contents <- liftM B.lines $ B.readFile filename
    Bl.hPutStr (filehandle) . Bl.concat  . map (filterAcc) . splitAndApply . convertComplex $ contents where
        convertComplex = map ( fmap( fromIntegral . aToG). readExpr )
        splitAndApply  = (amap (map (floor .quare .(drop 1) ). fftAcross) ((map  head) . splitN 32))

   -- B.hPutStrLn (filehandle)  . B.unlines . map (B.pack . show ) .  amap (map (floor .quare) .  (filter (/=[])) . map ( (drop 1) . (map (/32)) . fft ) . splitN 32) . map ( fmap(fromIntegral . aToG)) . map readExpr $ contents


texts: Code for calculating number of variations

texts:
Following up from my previous question where I put up a solution in brute for algorithm to this problem from codechef.

  Given some number \$K\$ and a list of \$N\$ numbers, count the number of pairs (\$i\$, \$j\$) such that \$1 \le i \lt j \le N\$ and \$|a_i − a_j | \ge K\$.

  \$1 \le N \le 65000\$
  \$1 \le K \le 10^8\$
  \$0 \le a_i \le 10^8\$

I changed my code quite a bit and was able to get rid of that time-limit-exceded error.Thanks to the responses, here is the updated code:

#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>

std::vector<int>loadTestCases(int N){
    std::vector<int> testCases;
    for (int i = 0; i < N; i++)
    {
        int TestCase;
        std::cin >> TestCase;
        testCases.push_back(TestCase);
    }
    return testCases;
}

int main(){

    int args,k;
    std::cin >> args >> k;

    std::vector<int> nums = loadTestCases(args);
    int cases = 0;

    sort(nums.begin(),nums.end());

    for(int i=1;i<args;i++){
        int temp = std::abs(nums.at(i)-nums.at(i-1));
        if(temp >=k){
            cases = cases + i;
        }else{
            int j = i;
            while(temp < k && j > 0){
                j--;
                temp = std::abs(nums.at(i)-nums.at(j));
            }
            if(temp >= k){
                cases = cases + j + 1;
            }
        }
    }

    std::cout << cases << std::endl;
    return 0;
}

But according to me the code still looks a little ugly and can be optimized more , can someone help me to identify the parts where simplification without risking the time taken can be done?


texts: Removing JLabels by pressing JButtons which disable if in excess

texts:
The goal of this code is to move some JLabels from (JLabel[] pileone) off a JFrame when a button in JButton[] bpile1 (array of JButtons) is pressed and then disable the buttons that are at an index larger than the amount of JLabels on the screen, so that each clickable JButton has a Label it removes.

I am only allowed to use the very basic code that I have been taught in class (i.e. if statements (as below), loops (for, do, while), arrays, graphical objects, basic math commands).

public class pile1Listener implements ActionListener { // Event Handler
public void actionPerformed(ActionEvent e) {
    for (int i = 0; i < 3; i++) {
        if (e.getSource() == bpile1[i]) {
            for (int c = i; c > -1; c--) {
                pileone[pileoneno - c].setLocation(-100, -100);
            }
            pileoneno = pileoneno - (i + 1);
            total = total - (i + 1);
        }
        for (int x = 2; x > pileoneno; x--) {
            bpile1[x].setEnabled(false);
        }
    }
    changeturn();
    definewinner();
}
}

I want to make it more compact as there are two more ActionListeners, as seen below, which are essentially the same but there are different variables in place:

public class pile2Listener implements ActionListener { // Event Handler
public void actionPerformed(ActionEvent e) {
    for (int i = 0; i < 8; i++) {
        if (e.getSource() == bpile2[i]) {
            for (int c = (i); c > -1; c--) {
                piletwo[piletwono - c].setLocation(-100, -100);
            }
            piletwono = piletwono - (i + 1);
            total = total - (i + 1);
        }
        for (int x = 7; x > piletwono; x--) {
            bpile2[x].setEnabled(false);
        }
    }
    changeturn();
    definewinner();
}
}

Here is the other:

public class pile3Listener implements ActionListener { // Event Handler
public void actionPerformed(ActionEvent e) {
    for (int i = 0; i < 5; i++) {
        if (e.getSource() == bpile3[i]) {
            for (int c = (i); c > -1; c--) {
                pilethr[pilethrno - c].setLocation(-100, -100);
            }
            pilethrno = pilethrno - (i + 1);
            total = total - (i + 1);
        }
        for (int x = 4; x > pilethrno; x--) {
            bpile3[x].setEnabled(false);
        }
    }
    changeturn();
    definewinner();
}
}

I want to minimize duplication of code.


texts: R data.frame to function as.function.data.frame

texts:
I love relation and function. And I would love to move afap from one to the other.

I've been looking for the function as.function.data.frame (or alike on tbl) but couldn't get it.

The bells and whistles are not yet present (test, check...)

## returns function
## In some way relation are function
## | p1 | p2 | p3 | value |
## f(p1, p2, p3) = value
## only if functionnal dependency applies
## for a given combination of parameter, value is unique

as.function.data.frame =
  function(dt, fmla){

    require(dplyr)

    f = as.function(fmla)
    valx = body(f)

    ## enclose data
    f_env = new.env()
    assign('dt', dt, f_env)
    assign('valx', # later used to select the attribute
           f %>% body %>% as.character,
           f_env)
    environment(f) <- f_env

    ## the body
    body(f) <- quote(
      {
        ## the filter: easier to get it as a data.frame and later inner_join, than use the dplyr::filter. imho
        fltr =
          as.list(match.call())[-1] %>% ## -1 because the call is enclosed and contains on to much element.
          as.data.frame

        fltr %>%
          inner_join(dt, by = names(fltr)) %>%
          select_(valx) %>% ## select_ because a need to evaluate
          unlist %>% unname ## we want value not table (value may be table...)
      }
    )

    return(f)
  }

a start of a test

library(testthat)
## test it
test_that("test the as.function.data.frame",
          {
            dt_test =
              data.frame(
                par1 = c(1,1,0,0),
                par2 = c(1,0,1,0),
                val1 = 1:4,
                val2 = 5:8)

            dt_fun1 = dt_test %>% as.function(par1 + par2 ~ val1)
            dt_fun2 = dt_test %>% as.function(par1 + par2 ~ val2)

            expect_equal(dt_fun1(1,0), 2)
            expect_equal(dt_fun2(0,0), 8)
          }
)

Three points:

Has this function been implemented in an other package? (base, dplyr...)
Is it an okay way to build the function?
as.list(match.call())[-1]: I am looking for a way to get it like as.list(match.call()) but without the [-1] I get the full function as first argument and then the one I want.


texts: Scaling the last column in a table (with input) to match the container

texts:
I recently ran in to a problem with the presentation of data related to a graph. Each series of data has values that define the 'curve'. There can be a lot of these values, and I decided that there was not enough space in the 'legend' to include all the details for the curve. I decided to have a 'details' area, and users could select a curve/series, and the details would be displayed in an area under the graph. This is because there would otherwise be too much data to show in the legend.

The details consist of 4 items, a colour "patch", a series name, the series accuracy, and the data points in the series. A table is what I chose to display these values in.

I want the details area to occupy the base of the screen underneath the graph, and to grow/shrink as the chart grows/shrinks. The last column in the table, the data points, are the largest part of the data, and would 'flood' the area. I want to make it possible to select all the data, but not necessarily make it all visible, a scrolling system would be fine.

In essence, I have this:

What I want reviewed in this question, though, is just the dynamic nature of the 4-column, 1-row table at the bottom, this:

Although the table scales well when the screen-size changes, I believe there must be a better way to implement it.

Here is my extraction of the relevant code, with some test data:

.container {
    width: 80%;
    margin: 0 auto;
    border: 2px solid black;
}

#dummy {
    width: 100%;
    background-color: lightgreen;
}

.details {
    width: 100%;
    border-collapse: collapse;
}

.details td {
    border: 1px black solid;
    padding: 3px;
}

.details td:last-child {
    width: 100%;
    background-color: lightblue;
    padding-right: 7px;
}

.details td:last-child input {
    width: 100%;
}

text to show where a graph would be

    Manual visualization

          Series
          <input id="seriesName" type="text" size="15" readonly="readonly" value="Manual Sizing" >

          RSquared
          <input id="seriesRSquare" type="text" size="10" readonly="readonly" value="0.98765432" >

          Description
          <input id="seriesDetail" type="text" readonly="readonly"
            value="[1,23],[2,20],[4,20],[8,21],[16,23],[32,22],[64,26],[128,28],[256,41],[512,55],[1024,81],[2048,136],[4096,244],[8192,474],[16384,959],[32768,1987],[65536,3613],[131072,6957],[262144,14250],[524288,28215],[1048576,55882],[2097152,116210],[3145728,175000],[4194304,229976],[5242880,291501],[6291456,347649],[7340032,399349],[8388608,485512]">

My specific concerns for this are:

The 'input' in the last column has width: 100% because otherwise it is too small. The input area unfortunately extends past the td border though, so I have had to add a manual padding of 7 px to the right side of the td to make it work. This seems counter-intuitive
the use of read-only input members allows the data to be shown, and scrolled-through, if needed, but is it the 'right way'?

I am open to any suggestions on how to best present this data.


texts:
I wrote some code for linked lists and would like to know what things could/should be done differently.

I'm using an index to keep track of the most important information and have 2 sets of functions, one to use the list just to point to data and the other to store the data in the list.

Here's the code so far:

#include <stdlib.h>
#include <string.h>

//Basic structure
typedef struct nNode Node;
struct nNode {
    void *content;
    Node *next;
    Node *previous;
};

//An index will make everything simpler
typedef struct {
    Node *first;
    Node *last;
    size_t node_count;
} List;

//Initialize list index with default values
void init_list(List *root)
{
    root->first = NULL;
    root->last = NULL;
    root->node_count = 0;
}

//Link new element and return pointer to it if successful or NULL
Node *add_node(List *root, void *content)
{
    Node *temp = malloc(sizeof(Node));
    if(temp == NULL){
        return NULL;
    }

    //Fill node
    temp->content = content;
    temp->next = NULL;
    temp->previous = root->last; //NULL if first node

    //Handle first element insertion
    if(root->first == NULL){
        root->first = temp;
    }

    //Or update previous element
    else {
        root->last->next = temp;
    }

    //Update index
    root->last = temp;
    ++root->node_count;

    return temp;
}

//Unlink element, always successful
void delete_node(List *root, Node *node)
{
    //If it's the first, update index reference
    if(node->previous == NULL){
        root->first = node->next;
    }

    //Or it's safe to update previous node
    else {
        node->previous->next = node->next;
    }

    //If it's the last, update index reference
    if(node->next == NULL){
        root->last = node->previous;
    }

    //Not the last? Safe to update next node
    else {
        node->next->previous = node->previous;
    }

    //Update count and free memory
    free(node);
    --root->node_count;
}

//Iterate through all nodes and call a function on every iteration
void iterate(const List *root, void(*function)(Node*))
{
    Node *next; //store in case the node changes
    for(Node *ite = root->first; ite != NULL; ite = next){
        next = ite->next;
        (*function)(ite);
    }
}

//Iterate through all nodes backwards and call a function on every iteration
void iterate_backwards(const List *root, void(*function)(Node*))
{
    Node *previous; //store in case the node changes
    for(Node *ite = root->last; ite != NULL; ite = previous){
        previous = ite->previous;
        (*function)(ite);
    }
}

//Delete all elements in the list
void delete_list(List *root)
{
    iterate(root, (void(*)(Node*))free);
    init_list(root);
}

////////////////
////// The following functions also store the content in the list
///// instead of just pointing to it. They should be used together
//// in order to avoid memory leaks.
/////////////

//Add node and store a copy of content
Node *add_node_store(List *root, const void *content, size_t content_size)
{
    //Allocate space
    void *copy = malloc(content_size);
    if(copy == NULL){
        return NULL;
    }

    //Copy content
    memcpy(copy, content, content_size);

    //Check if node insertion succeeded
    void *response = add_node(root, copy);
    if(response == NULL){
        free(copy);
    }

    return response;
}

//Unlink node and free stored content
void delete_node_store(List *root, Node *node)
{
    free(node->content);
    delete_node(root, node);
}

//Free node and its content
static void free_node_store(Node *node)
{
    free(node->content);
    free(node);
}

//Delete all nodes on the list, free them and their contents
void delete_list_store(List *root)
{
    iterate(root, free_node_store);
    init_list(root);
}


texts:
After watching Raymond Hettingers talk Transforming Code into Beautiful, Idiomatic Python I got back to a function I wrote.

I'm not quite sure how to make it more pythonic but I think this might be a use-case to use the map function.

import logging
import string
import os

def _mounted_drives(possible_device_letters=string.ascii_uppercase):
    """Returns a set of mounted drive letters on a Windows machine."""
    result = set()
    for letter in possible_device_letters:
        mounted = os.path.exists(letter + ':')
        if mounted:
            logging.info('Found device ' + letter)
            result.add(letter)

    return result

if __name__ == '__main__':
    print(_mounted_drives())


texts:
this is my first time learning sqlite and I'm still wondering how to do it. I ended up with this sort of library code:

database_path = "userfiles\databases"

def connect(database):
    """Open a connection and a cursor to the database file provided."""
    conexion = sqlite3.connect(database_path + database + ".db")
    cursor = conexion.cursor()
    return {"conexion":conexion, "cursor":cursor}

def disconnect(conexion, cursor, savechanges=False):
    """Disconnect from database, closing the cursor first.
    and saving the cursor data with a commit."""
    cursor.close()
    if savechanges:
        conexion.commit()
    conexion.close()

def build_sqlquery(**sqlquery):
    """Create the query that we will send to the sqlite file."""
    if "select" in sqlquery:
        query_built = "SELECT " + str(sqlquery["select"])
        if "fromtable" in sqlquery:
            query_built = query_built + " FROM " + str(sqlquery["fromtable"])
    elif "insert_into" in sqlquery:
        query_built = "INSERT INTO " + str(sqlquery["insert_into"])
        if "values" in sqlquery:
            query_built = query_built + " VALUES " + str(sqlquery["values"])
    elif "delete" in sqlquery:
        query_built = "DELETE FROM " + str(sqlquery["delete"])
    elif "update" in sqlquery:
        query_built = "UPDATE " + str(sqlquery["update"])
        if "set_values" in sqlquery:
            query_built = query_built + " SET " + str(sqlquery["set_values"])
    if "where" in sqlquery:
        query_built = query_built + " WHERE " + str(sqlquery["where"])
    if "limit" in sqlquery:
        query_built = query_built + " LIMIT " + str(sqlquery["limit"])
    return query_built

def findifexists(cursor, pydata=False, **sqlquery):
    """Query the params into a working sqlite query and
    execute the result, including the pydata if found."""
    query_built = build_sqlquery(sqlquery)
    if not pydata:
        cursor.execute(query_built)
    else:
        cursor.execute(query_built, pydata)
    datos_de_registro = cursor.fetchone() is not None
    return datos_de_registro

And then, once that library is created, I executed my queries like this:

conn = connect("food")
exists = findifexists(conn["cursor"], (value1, value2, value3, condition1),
             select="(?,?,?)",
             fromtable="mytable",
             where="Something = ?")
disconnect(conn["conexion"], conn["cursor"])
if exists:
    print("Yes, it exists")
else:
    print("Nope")

Now my concern is that it feels like re-inventing the wheel, where I could just throw an SQLite query directly at the caller without building it up in the build_sqlquerymethod.

What do you think about it? What cons or pros does this have?
Also: If I want to execute multiple queries, It's okay to keep a conection and a cursor open to execute them one after another before closing them? Or should I connect, save something, disconnect, and then connect again and repeat the procces?


texts:
Is this useful or is there an obvious other way?

The context is solving the problem of bubbling events in an asp.net site built with user controls and nested user controls.  I found myself writing a ton of events.

So instead of events, i'm using a technique to allow any client to watch an arbitrary session value.

A shared context class for Page and UserControl.  Updates to session values go through here as well as registrations for session value changes.

public class WebSiteContext
{
    private WebSitePage _webSitePage;

    public WebSiteContext(WebSitePage webSitePage)
    {
        _webSitePage = webSitePage;
    }

    internal void SetSessionValue<T>(string key, T value)
    {
        object current = _webSitePage.Session[key];
        if (current != null && current.Equals(value)) return;
        _webSitePage.Session[key] = value;
        if (_dic.ContainsKey(key)) _dic[key](value);
    }

    internal Dictionary<string, Action<object>> _dic = new Dictionary<string, Action<object>>();

    internal void WhenSessionValueChanges(string key, Action<object> action)
    {
        _dic.Add(key, action);
    }
}

Base page constructor creates context instance.

public class WebSitePage : System.Web.UI.Page
{
    public WebSitePage()
    {
        this.WebSiteContext = new WebSiteContext(this);
    }

    public WebSiteContext WebSiteContext { get; set; }
}

Base user control property to get context from parent page.

public class WebSiteUserControl : System.Web.UI.UserControl
{
    public WebSiteContext WebContext {
       get {
          return (this.Page as WebSitePage).WebSiteContext;
       }
    }
}

A user control makes a change to a session value.

public partial class AdvertiserSelector : WebSiteUserControl
{
    protected void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
       this.WebContext.SetSessionValue(
          SessionKeys.current_advertiser_id,
          (sender as ComboBox).SelectedValue);
    }
}

Now in my web page code behind, instead of subscribing to an event, an action is registered for a session value change.

namespace WebApplication1.Screens.CampaignSpend
{
    public partial class Default : WebSitePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.WebSiteContext.WhenSessionValueChanges(SessionKeys.current_advertiser_id, c => GridView1.DataBind());
        }
    }
}


texts: Filtering the attributes for a custom tag

texts:
I have function that takes all of the attributes passed to a custom tag, and returns a select subset. I am try to convert the current function, which does not use closures

string function passThrough(required struct attr)   output="false"  {

  local.result = "";

  for(local.myKey in arguments.attr)    {
    if (variables.myKey.left(5) == "data-" || variables.myKey.left(2) == "on" || variables.myKey.left(3) == "ng-")  {

        local.result &= ' #local.myKey.lcase()#="#arguments.attr[local.myKey].encodeForHTMLAttribute()#"';
        } // end if
    }   // end for

return local.result;
}

Into one that does

string function passThrough(required struct attr)   output="false"  {

  arguments.attr.filter(
    function(key, value) {
        return (key.left(5) == "data-" || key.left(2) == "on" || key.left(3) == "ng-");
        }
    ).each(
    function(key, value)    {
        local.result &= ' #key.lcase()#="#value.encodeForHTMLAttribute()#"';
        }
    );

return local.result;
}

Both of these are invoked with

...  invoke("bootstrap", "passThrough", {attr = attributes});

Is this the right approach? Should arguments passed to the closure functions be scoped?


texts: Length limited listagg for muliple m:n relations in a view

texts:
I have a few views that all follow the same pattern. They select a primary key from some main table and then a few semicolon seperated strings of captions of related rows over many-to-many relations. All columns that are used in joins are either primary keys or have a manually set index and are of type NUMBER(18). The columns that hold the captions are either VARCHAR2 or - rarely - CLOB. Usually the view will be joined with the main table to select between 1 and 50 rows of the main table.

The creation of the semicolon seperated strings is done with the listagg function from Oracle 11gR2 because my research showed that this is the easiest and also most performant solution. There is just one problem: There could be so many related rows that the listagg function reaches the limit of 4000 chars of its VARCHAR2 result. I'm currently solving this by limiting the set that listagg operates on to 50 rows. This is just a random number that I've chosen, it should prevent errors and the users don't care for more than 50 results anyway. But of course, it's still ugly code.

If anyone has any ideas how to improve the following code, I'd be very happy. I'm especially interested in improvements that lead to less complexity, more performance or a better solution for the 4000 chars limit.

CREATE OR REPLACE VIEW MAILS_RELATED
(
  MAIL_ID,
  CUSTOMERS,
  PROJECTS,
  [some more columns]
)
AS
SELECT m.MAIL_ID,
       cust_list.CUSTOMERS,
       proj_list.PROJECTS,
       [some more columns]
  FROM MAILS m LEFT OUTER JOIN (
         SELECT inner.MAIL_ID,
                LISTAGG(inner.CUSTOMER_NAME, '; ')
                  WITHIN GROUP (ORDER BY inner.CUSTOMER_NAME) AS CUSTOMERS
           FROM (
                SELECT rel.MAIL_ID,
                       cust.CUSTOMER_NAME,
                       ROW_NUMBER() OVER
                         (PARTITION BY rel.MAIL_ID ORDER BY cust.CUST_ID) AS RN
                  FROM CUSTOMERS_MAILS rel INNER JOIN
                       CUSTOMERS cust ON (cust.CUST_ID = rel.CUST_ID)
                ) inner
          WHERE inner.RN <= 50
          GROUP BY inner.MAIL_ID
       ) cust_list ON (cust_list.MAIL_ID = m.MAIL_ID) LEFT OUTER JOIN (
         SELECT inner.MAIL_ID,
                LISTAGG(inner.PROJECT_NAME, '; ')
                  WITHIN GROUP (ORDER BY inner.PROJECT_NAME) AS PROJECTS
           FROM (
                SELECT rel.MAIL_ID,
                       proj.PROJECT_NAME,
                       ROW_NUMBER() OVER
                         (PARTITION BY rel.MAIL_ID ORDER BY proj.PROJ_ID) AS RN
                  FROM PROJECTS_MAILS rel INNER JOIN
                       PROJECTS proj ON (proj.PROJ_ID = rel.PROJ_ID)
                ) inner
          WHERE inner.RN <= 50
          GROUP BY inner.MAIL_ID
       ) proj_list ON (proj_list.MAIL_ID = m.MAIL_ID) LEFT OUTER JOIN
       [some more joins]


texts: JS exercise to process an array of animals

texts:
My background is primarily in C#. Though, I've gotten comfortable with JavaScript, I feel that stylewise my JS code looks more like C# code.

Does the following code's style conform to "standard JS style"?

processArray = (myArray) => {

   if(typeof myArray === "undefined" || myArray == null || myArray.length == 0)
      return null;

   let animals = [];
   let animal = {};

   for(let item of myArray) {

      animal = getAnimal(item);
      if(item !== null)
         animals.push(animal);
   };

   return animals;
}

getAnimal = (data) => {

   if(typeof data === "undefined" || data == null)
      return null;

   const animal = {
      id: data.id,
      type: data.type,
      name: data.name
   };

   return animal;
}

It's important to note that I'm not so much talking about formatting such as indentations, opening curly braces on the same line as the function name, etc.

What I'm really talking about is the way the code is structured. For example, as an "old school" C# developer, I like creating variables and assigning my data to them and cleanly returning them in my methods.

I see that a lot of JS developers chain one function to another or use a lot of callbacks. In other words, I see a lot of intermingling of functions. I think C# has also borrowed a few patterns from JS but for the most part, the coding style I used here is pretty typical "old school" C#.

Maybe an analogy will help clarify what I'm talking about. It's like a person whose native language is German/Italian/Japanase/etc. speaking English with an accent. Even if the grammar and even choice of words are perfect, the accent always gives it away.

So, I'm trying to understand if I'm writing JS code with a C# accent :-)


texts: Advent of Code 2020 - Day 1: finding 2 or 3 numbers that add up to 2020

texts:
Next: Advent of Code 2020 - Day 2: validating passwords
Problem statement
I decided to take a shot at Advent of Code 2020 to exercise my Rust knowledge.  Here's the task for Day 1:

Day 1: Report Repair
[...]
Specifically, they need you to find the two entries that sum to 2020
and then multiply those two numbers together.
For example, suppose your expense report contained the following:
1721
979
366
299
675
1456

In this list, the two entries that sum to 2020 are 1721 and 299.
Multiplying them together produces 1721 * 299 = 514579, so the correct
answer is 514579.
[...]
Part Two
[...] They offer you a second one if you can find three numbers in
your expense report that meet the same criteria.
Using the above example again, the three entries that sum to 2020 are
979, 366, and 675. Multiplying them together produces the answer,
241861950.

The full story can be found on the website.
My solution
src/day_1.rs
use {anyhow::Result, std::io::prelude::*};

pub type Number = u64;

pub fn parse_data<R: BufRead>(reader: R) -> Result<Vec<Number>> {
    reader.lines().map(|line| Ok(line?.parse()?)).collect()
}

// Finds a pair of numbers in `data` that add up to `sum`
pub fn find_pair(mut data: Vec<Number>, sum: Number) -> Option<(Number, Number)> {
    data.sort_unstable();

    find_pair_in_sorted(&data, sum)
}

// Finds a triple of numbers in `data` that add up to `sum`.
pub fn find_triple(mut data: Vec<Number>, sum: Number) -> Option<(Number, Number, Number)> {
    data.sort_unstable();

    while let Some(number) = data.pop() {
        let sub_sum = match sum.checked_sub(number) {
            Some(sub_sum) => sub_sum,
            None => continue,
        };

        if let Some((a, b)) = find_pair_in_sorted(&data, sub_sum) {
            return Some((a, b, number));
        }
    }

    None
}

// Finds a pair of numbers in `data` that add up to `sum`
// by binary searching for `sum - n` for each number `n` in `data`.
// `data` must be sorted in ascending order.
pub fn find_pair_in_sorted(mut data: &[Number], sum: Number) -> Option<(Number, Number)> {
    while let Some((&number, new_slice)) = data.split_last() {
        data = new_slice;

        let target = match sum.checked_sub(number) {
            Some(target) => target,
            None => continue,
        };

        if data.binary_search(&target).is_ok() {
            return Some((target, number));
        }
    }

    None
}

src/bin/day_1_1.rs
use {
    anyhow::{anyhow, Result},
    aoc_2020::day_1::{self as lib, Number},
    std::{fs::File, io::BufReader},
};

const PATH: &str = "./data/day_1/input";
const SUM: Number = 2020;

fn main() -> Result<()> {
    let file = BufReader::new(File::open(PATH)?);
    let data = lib::parse_data(file)?;

    let (a, b) = lib::find_pair(data, SUM)
        .ok_or_else(|| anyhow!("cannot find pair that adds up to {}", SUM))?;
    println!("{} * {} = {}", a, b, a * b);

    Ok(())
}

src/bin/day_1_2.rs
use {
    anyhow::{anyhow, Result},
    aoc_2020::day_1::{self as lib, Number},
    std::{fs::File, io::BufReader},
};

const PATH: &str = "./data/day_1/input";
const SUM: Number = 2020;

fn main() -> Result<()> {
    let file = BufReader::new(File::open(PATH)?);
    let data = lib::parse_data(file)?;

    let (a, b, c) = lib::find_triple(data, SUM)
        .ok_or_else(|| anyhow!("cannot find triple that adds up to {}", SUM))?;
    println!("{} * {} * {} = {}", a, b, c, a * b * c);

    Ok(())
}

Crates used: anyhow 1.0.37
cargo fmt and cargo clippy have been applied.


texts:
I am working with MongoDB through Docker, and I have a terrible bash command to pull the Docker Machine IP so I can sanely connect locally.

export dockerip="$(docker-machine ls | awk '{print $5}' | sed -n '2p' | sed 's/tcp:\/\///' | sed 's/\:2376//')"
mongo --host "$dockerip" --port 27017

How can I optimise the docker-machine line?


texts:
I was trying to use the ES6 Promise with the "co" module to write the async code without callback. Though actual async call is neat and clean, I still have to add wrapper method to return Promise object which made it little bulky. Any feedback to improve this code quality will be appreciated.

crud.js
'use strict';
const db = require("./dbmanager").db;
const co = require("co");

module.exports = {

  getAllDocuments: (collectionName)=> {

    //This line of code returning "Promise" object make code bulky. Now I have two wrapper, one for "Promise" and second for "co".
    return new Promise((resolve, reject) => {

      co(function*() {
        let docs = yield db.collection(collectionName).find().toArray();
        resolve(docs);

      }).catch((err) => {
        reject(err);
      });

    });
  }

};

book.js

'use strict';
const dbcrud   = require("./../mongodb/crud");
const co = require("co");
module.exports = {

  getTransactions: (callback) => {
    co(function*() {

      var books = yield dbcrud.getAllDocuments('books');
      callback(null, books);

    }).catch((err) => {
      console.log("Error " + JSON.stringify(err));
      callback(err, null);
    });
  }

};


texts:
I am currently learning C++, so I made this basic password wallet for Ubuntu.
Its source code is in the /opt directory and it has a  symlink so i can launch it from any directory using pw in the terminal.
Here is what it does:
It asks for a password (pass) and a site (which is related to the pass). It decrypts the file (test.enc) that contains the passwords to another file (text.txt), writes the pass and site to test.txt, encrypt it back to test.enc and delete it (test.txt).
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(){
        char pass[30];
        string site;
        getline(cin, site);
        cin >> pass;
        system("cd /opt/PW/ && openssl enc -aes-256-ecb -d -iter 100000 -in test.enc -out test.txt");
        ofstream MyFile("/opt/PW/test.txt", ios::app);
        MyFile << pass << ":" << site << endl;
        MyFile.close();
        system("cd /opt/PW/ && openssl enc -aes-256-ecb -iter 100000 -in test.txt -out test.enc && rm -rf test.txt");
}

It's for personal use only.
I am curious to know how it can be exploited.


texts:
I've worked with PHP for a few years now and have a degree of understanding about classes / objects, but I've never written my own until now.

In that vein then, I'm looking for a bit of confirmation of the code below and that I am doing things properly before I go any further. Any advice / critique is greatly appreciated.

class People {

protected $people = array();

public function __construct() {
    $this->people = $people;

    $defaults = array(
        'model_type' => 'post_type',
        'post_type' => 'post'
    );
    $options = wp_parse_args(get_option('my_options'), $defaults);

    $this->model = $options['model_type'];
    $this->type = $options['post_type'];
}

public function get_people() {
    switch($this->model) {
        case 'user' :
            $people = get_users();
            if(!empty($people)) {
                foreach($people as $person) {
                    $new_person = new Person( $person->ID, $person->first_name, $person->user_email );
                    $this->people[] = $new_person;
                }
            }
        break;

        case 'post_type' :
            $args = array(
                'posts_per_page'   => -1,
                'orderby'          => 'post_title',
                'order'            => 'DESC',
                'post_type'        => $this->type,
                'post_status'      => 'publish',
            );
            $people = get_posts( $args );
            if(!empty($people)) {
                foreach($people as $person) {
                    $new_person = new Person( $person->ID, $person->post_title, get_post_meta($person->ID, '_email_address', true) );
                    $this->people[] = $new_person;
                }
            }
    }

    return $this->people;
}

}

class Person {

public $id;
public $name;
public $email;

public function __construct( $id, $name, $email ) {
    $this->id = $id;
    $this->name = $name;
    $this->email = $email;
}

}


texts:
This is my first COBOL program, and I'd like to get a critique as I am unfamiliar with best practices.

Particularly, I'd like to know how to get the input and output to be more elegant and less dependent on how the file fields are structured.

Some type of analogy to C's scanf(), getch(), or any of the Java console IO routines would be helpful.

https://gist.github.com/anonymous/5427201

        DATA DIVISION.
    WORKING-STORAGE SECTION.
    01 TIME-STORAGE.
        03 CURRENT-TIME-NUMERIC         PIC 9(8).
        03 ETA-NUMERIC PIC 9(8) REDEFINES CURRENT-TIME-NUMERIC.

        03 CURRENT-TIME REDEFINES CURRENT-TIME-NUMERIC.
            05  CURRENTHOUR     PIC 99.
            05  CURRENTMINUTE   PIC 99.
            05  FILLER          PIC 9(4).
        03 ARIVAL-TIME-NUMERIC PIC 9(8).
        03 ARIVAL-TIME REDEFINES ARIVAL-TIME-NUMERIC.
            05  ARRIVALHOUR     PIC 99.
            05  ARRIVALMINUTE   PIC 99.
            05  FILLER          PIC 9(4).
    PROCEDURE DIVISION.
    BEGIN-BUG-NORMAN.
        DISPLAY "when will you be ariving?"
        DISPLAY "HH [enter]"
        ACCEPT ARRIVALHOUR
        DISPLAY "MM [enter]"
        ACCEPT ARRIVALMINUTE
        ACCEPT CURRENT-TIME FROM TIME.
        SUBTRACT ARIVAL-TIME-NUMERIC
            FROM CURRENT-TIME-NUMERIC
            GIVING ETA-NUMERIC.
        DISPLAY "RESULTING ETA:"
        DISPLAY ETA-NUMERIC.
        DISPLAY "HOURS:"
        DISPLAY CURRENTHOUR
        DISPLAY "MINUTES:"
        DISPLAY CURRENTMINUTE

        STOP RUN.


texts: Get Instagram photo links of a hashtag using Selenium

texts:
I wrote this methods that store instagram links in an ArrayList based on a hashtag or list of hashtags and the number of desired links.
Something to notice is that if I tell the program to get 10 links it will get 12 links instead because of how Instagram places the photos in a grid style but after 10 iterations it stops and the ArrayList is cleared, is this considered bad or it doesnt matter?

private void getLinks(TextArea logbox) throws InterruptedException {
        utils.addLogLine(logbox, "[+] Obteniendo links con los tags especificados...");

        int numberOfRows = (int) Math.ceil(((float) numberOfLikes / 3));
        int numberOfRowsPerTag = (int) Math.ceil(((float) numberOfRows / tags.size()));

        //Page needs to load more images after first load
        if (numberOfRowsPerTag > 4) {
            for (int x = 0; x < tags.size(); x++) {
                searchTag(tags.get(x));
                int numberOfPageDowns = (int) Math.ceil(((float) numberOfRowsPerTag / 4));

                WebElement loadMoreBtn = utils.fluentWait(By.xpath("//*[@id=\"react-root\"]/section/main/article/div[2]/a"), driver);
                loadMoreBtn.click();
                WebElement pageBody = utils.fluentWait(By.tagName("body"), driver);

                for (int y = 0; y < numberOfPageDowns; y++) {
                    pageBody.sendKeys(Keys.PAGE_DOWN);
                    pageBody.sendKeys(Keys.PAGE_DOWN);
                    utils.waitGivenSeconds(2);
                }

                for (int z = 0; z < numberOfRowsPerTag; z++) {
                    int row = z + 1;
                    getPhotolinksFromRow(row);
                }
                utils.addLogLine(logbox, "[+] Links para #" + tags.get(x) + " añadidos!");
            }
        } else {
            for (int x = 0; x < tags.size(); x++) {
                searchTag(tags.get(x));
                for (int z = 0; z < numberOfRowsPerTag; z++) {
                    int row = z + 1;
                    getPhotolinksFromRow(row);
                }
                utils.addLogLine(logbox, "[+] Links para #" + tags.get(x) + " añadidos!");
            }
        }
    }

private void getPhotolinksFromRow(int row) {
        WebElement firstPhoto = utils.fluentWait(By.xpath("//*[@id=\"react-root\"]/section/main/article/div[2]/div[1]/div[" + row + "]/div[1]/a"), driver);
        WebElement secondPhoto = utils.fluentWait(By.xpath("//*[@id=\"react-root\"]/section/main/article/div[2]/div[1]/div[" + row + "]/div[2]/a"), driver);
        WebElement thirdPhoto = utils.fluentWait(By.xpath("//*[@id=\"react-root\"]/section/main/article/div[2]/div[1]/div[" + row + "]/div[3]/a"), driver);

        String firstLink = firstPhoto.getAttribute("href");
        String secondLink = secondPhoto.getAttribute("href");
        String thirdLink = thirdPhoto.getAttribute("href");

        String[] firstLinkStripped = firstLink.split("\\?");
        String[] secondLinkStripped = secondLink.split("\\?");
        String[] thirdLinkStripped = thirdLink.split("\\?");

        List<String> tempLinks = Arrays.asList(firstLinkStripped[0], secondLinkStripped[0], thirdLinkStripped[0]);
        links.addAll(tempLinks);
    }


texts: Better performance in solution for the Dole Out Cadbury challenge

texts:
Dole Out Cadbury
Problem Description

You are a teacher in reputed school. During Celebration Day you were assigned a task to distribute Cadbury such that maximum children get the chocolate. You have a box full of Cadbury with different width and height. You can only distribute largest square shape Cadbury. So if you have a Cadbury of length 10 and width 5, then you need to break Cadbury in 5X5 square and distribute to first child and then remaining 5X5 to next in queue
Constraints
\$0<P<Q<1501\$
\$0<R<S<1501\$
Input Format
First line contains an integer P that denotes minimum length of Cadbury in the box
Second line contains an integer Q that denotes maximum length of Cadbury in the box
Third line contains an integer R that denotes minimum width of Cadbury in the box
Fourth line contains an integer S that denotes maximum width of Cadbury in the box
Output
Print total number of children who will get chocolate.
Timeout
1
Example
Input
5
7
3
4

Output
24

Explanation
Length is in between 5 to 7 and width is in between 3 to 4.
So we have 5X3,5X4,6X3,6X4,7X3,7X4 type of Cadbury in the box.
If we take 5X3:
First, we can give 3X3 square Cadbury to 1st child .Then we are left with 3X2. Now largest square is 2X2 which will be given to next child. Next, we are left with two  1X1 part of Cadbury which will be given to another two children.
And so on.

Here is my code, which works but returns time-limit-exceeded. I'm looking for optimizations.
def cadbury(l,b):
    count = 0
    while True:
        lon=max(l,b)
        sh=min(l,b)
        count+=1
        diff=lon-sh
        if diff==0:
            return count
        else :
            l=min(l,b)
            b=diff
minl=int(input())
maxl=int(input())
minw=int(input())
maxw=int(input())
count=0
for i in range(minl,maxl+1):
    for j in range(minw,maxw+1):
        count+=cadbury(i,j)
print(count)


texts:
I'm toggling a state when the browser is resized:

const [expanded, setExpanded] = useState(true)
useEffect(() => {
  const listener = () => {
    if (document.documentElement.clientWidth > 1024) return
    setExpanded(false)
  }
  window.addEventListener('resize', listener)
  return () => window.removeEventListener('resize', listener)
}, [])

I've the react-hooks/exhaustive-deps rule enabled so ESLint complains:

  React Hook useEffect has a missing dependency: 'setExpanded'. Either include it or remove the dependency array

How should I rewrite this? Or should I simply silence the ESLint warning?


texts: Checking if an integer permutation is cyclic in Java

texts:
A an integer permutation array[] is said to be cyclic if and only if we can choose an arbitrary element at position i, move it to the array[i]'s position, move the array[i]'s element to array[array[i]]'s position, and finally end up to the ith position. For example, \$\langle 1, 2, 0 \rangle\$ is cyclic, whereas \$\langle 1, 0, 2 \rangle\$ is not since if we start from 2, we sill end up in an infinite loop. My attempt is as follows:

package net.coderodde.math;

import java.util.Objects;

/**
 * This class checks that a given permutation consists of a single cycle
 * covering the entire sequence. The idea is that if the input array is
 * {@code 1, 0, 2}, it returns {@code false} since 2 is in its correct position.
 * On the other hand, {@code 1, 2, 0} returns {@code true} since it consists of
 * only one permutation cycle.
 *
 * @author Rodion "rodde" Efremov
 * @version 1.6 (Feb 22, 2019)
 */
public class PermutationCyclicity {

    private static final String NUMBER_OUT_OF_BOUNDS_EXCEPTION_FORMAT =
            "array[%d] = %d, array.length = %d";

    public boolean isCyclic(int[] array) {
        check(array);
        int nextNumber = array[array[0]];
        int visitedNumbers = 1;

        while (nextNumber != array[0]) {
            nextNumber = array[nextNumber];
            visitedNumbers++;
        }

        return visitedNumbers == array.length;
    }

    private void check(int[] array) {
        checkArrayHasLength(array);
        checkArrayContentsWithinBounds(array);
        checkArrayElements(array);
    }

    private void checkArrayHasLength(int[] array) {
        Objects.requireNonNull(array, "The input array is null.");

        if (array.length == 0) {
            throw new IllegalArgumentException("The array has length zero.");
        }
    }

    private void checkArrayContentsWithinBounds(int[] array) {
        for (int i = 0; i < array.length; i++) {
            int num = array[i];

            if (num < 0 || num >= array.length) {
                String exceptionMessage =
                        String.format(NUMBER_OUT_OF_BOUNDS_EXCEPTION_FORMAT,
                        i,
                        num,
                        array.length);

                throw new IllegalArgumentException(exceptionMessage);
            }
        }
    }

    private void checkArrayElements(int[] array) {
        int[] counts = new int[array.length];

        for (int i : array) {
            if (++counts[i] > 1) {
                throw new IllegalArgumentException(
                        "The value " + i + " appears more than once.");
            }
        }
    }

    public static void main(String[] args) {
        PermutationCyclicity pc = new PermutationCyclicity();
        System.out.println(pc.isCyclic(new int[] { 1, 2, 3, 0 }));

        try {
            pc.isCyclic(new int[] { 1, 0, 1 });
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }

        try {
            pc.isCyclic(new int[] { 1 });
        } catch (Exception ex) {
            System.out.println(ex.getLocalizedMessage());
        }

        System.out.println(pc.isCyclic(new int[] { 1, 0, 2 }));
    }
}

Critique request

I would like to hear comments regarding exception throwing and naming conventions, yet feel free to tell me anything that comes to mind.


texts:
currently i am working on javascript. Learning deep etc. So i decided to made my own tic tac toe game 4x4 without any tips. Then i stucked. If anyone can help me will be perfect. Game is playable right now. But i can't set the algorithm who win the game? What should i refer for winner? All winner positions to an array, or save the player moves and put it in the algorithm?

Game is on 4x4 table, 3 in a row(vertical, horizontal or diagonal) won the game.

I am adding jsfiddle right here. Thanks in advance.

https://jsfiddle.net/vsmile/fbgq1429/3/

$(document).ready(function()
{
    const PLAYER = "X";
    const COMPUTER = "O";
    let game_status = true;
    var emptyCells = [];
    var playerPos = [];
    var computerPos = [];

$('.col').click(function() {
    isGameOver(PLAYER);

    //Do Not Click Filled Div
    if ($(this).text().length !== 0) {
        return;
    }

    //Save Player Moves
    playerPos.push([Number($(this).attr("data-x")), Number($(this).attr("data-y"))]);

    //Make Click
    $(this).html(PLAYER);

    //Clear Empty Cells To Fill Later!
    emptyCells = [];

    //Let Computer Move
    computerMove();
});

function computerMove(){
    isGameOver(COMPUTER);

    //Save Computer Moves
    computerPos.push([Number($(this).attr("data-x")),Number($(this).attr("data-y"))]);

    //Add All Empty Cells To An Array
    for(let x=1; x<=4; x++){
        for (let y=1; y<=4; y++) {
            if ($('.col[data-x='+x+'][data-y='+y+']').text().length == 0) {
                emptyCells.push($('.col[data-x='+x+'][data-y='+y+']'));
            }
        }
    }

    //Click Random Empty Cell
    if (emptyCells.length > 0) {
    let computerChoice = emptyCells[Math.floor(Math.random() * emptyCells.length)];
    computerChoice.text(COMPUTER);
    }
}

function isGameOver($req) {

    //THIS IS WHERE I STUCK!

    //Vertical Check

    //Horizontal Check

    //Diagonal Check

}
});

This is the HTML codes:

EDIT:

If i put all correct solutions, it works like charm. But i need dynamically improve my code. For example;

    const grid = [
[' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' '],
];

const wonMoves = [
[[1,1], [2,2], [3,3]],
[[1,1], [1,2], [1,3]],
..
..

];

After a move. I can change grid[i][j]; then i can check if it matches the correct solutions.But what if tictactoe game 10x10 board? Now what? This should be dynamic. Thats what i need, that code improvement would be great. Thanks.


texts: Finding dictionary keys whose values are duplicates

texts:
I currently have a dictionary (Duplicate_combos) that has a unique identifying number for the key value and the value is a list with two elements, a company code and then either a yes or no (both of these values are currently stored as strings). I am essentially just trying to see where the company code is equal and the second term is no for both.
So if this was my dictionary:

{1234: ['123' , 'No'] , 1235:['123', 'No'], 1236: ['123','Yes'], 1237: [124,'No']}

I would only want to return 1234 and 1235. The code below is what I currently have and I really need to optimize it because while it does work when I tested it on a small data set, I will need to use it on a much larger one (43,000 lines) and in early testing, it is taking 45+ minutes with seemingly no sign of ending soon.

def open_file():

    in_file = open("./Data.csv","r")
    blank = in_file.readline()
    titles = in_file.readline()
    titles = titles.strip()
    titles = titles.split(',')

    cost_center = [] # 0
    cost_center_name = []# 1
    management_site = [] # 15
    sub_function = [] #19
    LER = [] #41
    Company_name = [] #3
    Business_group = [] #7
    Value_center = [] #9
    Performance_center = [] #10
    Profit_center = [] #11

    total_lines = {}

    for line in in_file:

        line = line.strip()
        line = line.split(',')
        cost_center.append(line[0])
        cost_center_name.append(line[1])
        management_site.append(line[15])
        sub_function.append(line[19])
        LER.append(line[41])
        Company_name.append(line[3])
        Business_group.append(line[7])
        Value_center.append(line[9])
        Performance_center.append(line[10])
        Profit_center.append(line[11])

        # create a dictionary of all the lines with the key being the unique cost center number (cost_center list)
        total_lines[line[0]] = line[1:]

    return(cost_center, cost_center_name, management_site, sub_function, LER, Company_name, Business_group, total_lines, titles, Value_center, Performance_center, Profit_center)

def find_duplicates(Duplicate_combos):

    Real_duplicates = []
    archive_duplicates = []

    # loop through the dictionary of duplicate combos by the keys
    for key in Duplicate_combos:
       code = Duplicate_combos[key][0]
       for key2 in Duplicate_combos:
           # if the two keys are equal to each other, it means you are comparing the key to itself, which we don't want to do so we continue
            if key == key2:
               continue
            # if the company codes are the same and they are BOTH NOT going to be consolidated, we have found a real duplicate
            elif Duplicate_combos[key2][0] == code and Duplicate_combos[key2][1] == 'No' and Duplicate_combos[key][1] == 'No':
                # make sure that we haven't already dealt with this key before
                if key not in archive_duplicates:
                    Real_duplicates.append(key)
                    archive_duplicates.append(key)

                if key2 not in archive_duplicates:
                    Real_duplicates.append(key2)
                    archive_duplicates.append(key2)
            continue
    return(Real_duplicates)


texts: Reverse Polish Notation calculator in Python

texts:
After reading the definition, I made a simple Reverse Polish Notation (RPN) calculator in Python.

Originally it had just 4 operators (using import operator and a lookup table) and only did integers. After looking at some example calculations, I amended it to work on floats and added a raising to powers. Then I saw some more advanced operations here and added math functions.

Owing to the large amount of math functions available, is there an easier way to include them without having to have math.<function> in the operators lookup?

# Reverse Polish Notation calculator
# based on http://en.wikipedia.org/wiki/Reverse_Polish_notation

import math
import operator
ops = {'+':operator.add,
       '-':operator.sub,
       '*':operator.mul,
       '/':operator.div,
       '^':operator.pow,
       'sin':math.sin,
       'tan':math.tan,
       'cos':math.cos,
       'pi':math.pi}

def is_number(s):
    try:
        float(s)
        return True
    except ValueError:
        pass

def calculate(equation):
    stack = []
    result = 0
    for i in equation:
        if is_number(i):
            stack.insert(0,i)
        else:
            if len(stack) < 2:
                print 'Error: insufficient values in expression'
                break
            else:
                print 'stack: %s' % stack
                if len(i) == 1:
                    n1 = float(stack.pop(1))
                    n2 = float(stack.pop(0))
                    result = ops[i](n1,n2)
                    stack.insert(0,str(result))
                else:
                    n1 = float(stack.pop(0))
                    result = ops[i](math.radians(n1))
                    stack.insert(0,str(result))
    return result

def main():
    running = True
    while running:
        equation = raw_input('enter the equation: ').split(' ')
        answer = calculate(equation)
        print 'RESULT: %f' % answer
        again = raw_input('\nEnter another? ')[0].upper()
        if again != 'Y':
            running = False

if __name__ == '__main__':
    main()

Simple/straightforward test:

enter the equation: 6 4 5 + * 25 2 3 + / -
stack: ['5', '4', '6']
stack: ['9.0', '6']
stack: ['3', '2', '25', '54.0']
stack: ['5.0', '25', '54.0']
stack: ['5.0', '54.0']
RESULT: 49.000000

Math functions test:

enter the equation: 5 8 2 15 * sin * + 2 45 tan + /
stack: ['15', '2', '8', '5']
stack: ['30.0', '8', '5']
stack: ['0.5', '8', '5']
stack: ['4.0', '5']
stack: ['45', '2', '9.0']
stack: ['1.0', '2', '9.0']
stack: ['3.0', '9.0']
RESULT: 3.000000


texts: Interface Segregation Principle in constructor

texts:
I'm wondering if providing default behavior is ever worth the confusion caused by ignoring generic type passed in. Another question brought up this issue:

public class Activate<D extends CanQuack & CanWalk> {
    D d;

    //Must match D's type
    Activate(D d) {
        this.d = d;
    }

    //Completely ignore D's type
    Activate() {
        this.d = (D)new MyWaterFowl(); //Provide default behavior.
    }

    void setDuckLikeThing(D d) {
        this.d = d;
    }

    void doDuckLikeThings() {
        d.quack();
        d.walk();
        //d.swim(); //Doesn't work
        //And shouldn't since that was the whole point of ISP.
    }
}

interface CanWalk { public void walk(); }
interface CanQuack { public void quack(); }
interface CanSwim { public void swim(); }

public class MyWaterFowl implements CanWalk, CanQuack {

    public void walk() { System.out.println("I'm walkin` here!"); }
    public void quack() { System.out.println("Quack!"); }
    public void swim() { System.out.println("Stroke! Stroke! Stroke!"); }
}

public class MainClass {

    public static void main(String[] args) {
       //Works
       Activate<MyWaterFowl> a = new Activate<>(new MyWaterFowl());
       a.doDuckLikeThings();

       //Works
       Activate<MyAmericanEagle> ae = new Activate<>( new MyAmericanEagle() );
       ae.doDuckLikeThings();

       //Works but nothing to do with MyAmericanEagle is happening here
       Activate<MyAmericanEagle> ad = new Activate<>();
       ad.doDuckLikeThings();

       //Rightly produces an error
       //Activate<MyAmericanEagle> aw = new Activate<>( new MyWaterFowl() );
       //ae.doDuckLikeThings();

       //Rightly produces an error
       //Activate<MyWaterFowl> wa = new Activate<>( new MyAmericanEagle() );
       //ae.doDuckLikeThings();

    }
}

So would you ever want a library to leave you wondering why MyAmericanEagle gets completely ignored just to provide some default behavior?


texts: Ugly code unit testing with PHPUnit and Doctrine2 (an ORM)

texts:
I am trying to unit test a small class that I consider part of the "business-logic" layer of my project.  It mostly just interact's with Doctrine's entity manager and a couple of entities with a many-to-many association.  I'm finding that unit testing it while mocking out its dependencies is just ludicrous.  Just the code to set up mocks is going to be at least twice as long as the class itself.  What am I doing wrong, or should I just not bother doing the mocks and let it interact with a real test database?

Here is the class I am trying to test:

class UserPermissions{

    protected $em;

    public function __construct($entityManager){
        $this->em = $entityManager;
    }

    /**
     * returns user's permissions for all products or specific product
     *
     **/
    public function getFromUUID($uuid, $productId=NULL){

        $user = $this->em->find('\MyProject\Entity\User',$uuid);
        $product = $productId ? $this->em->find('\MyProject\Entity\Product',$productId) : NULL;

        if ($user===null) {
            return null;
        } else {
            $userProducts = $user->getUserProducts();
            $result = [];

            if ($productId) {
                $criteria = Criteria::create()
                    ->where(Criteria::expr()->eq("product",$product))
                    ;
                $userProducts = $userProducts->matching($criteria);
            }

            for ($i = 0; $i < count($userProducts); $i++) {
                $productI = $userProducts[$i];
                array_push($result, array(
                    "product" => $productI->getProduct()->getId(),
                    "permissions" => $productI->getPermissions(),
                ));
            }

            return $result;
        }
    }
}

And here is what my test class is starting to look like:

class UserPermissionsTest extends PHPUnit_Framework_TestCase
{
    protected $testService;
    protected $mockProduct;
    protected $mockUser;
    protected $mockUserProduct;

    protected function setUp(){

        $em = $this->getMockBuilder('\Doctrine\ORM\EntityManager')
                   ->disableOriginalConstructor()
                             ->getMock();

        $emMap = array(
            array('\MyProject\Entity\User','a',$this->mockUser),
            array('\MyProject\Entity\Product','b',$this->mockProduct),
        );

        $em->expects($this->any())
           ->method('find')
             ->will($this->returnValueMap($emMap));

//      $userMockBuilder = $this->getMockBuilder('\MyProject\Entity\User')
//                              ->disableOriginalConstructor()

        $user = $this->getMockBuilder('\MyProject\Entity\User')
                     ->disableOriginalConstructor()
                                 ->getMock();

        $user->expects($this->any())
             ->method('getUserProducts')
                 ->will($this->returnValue($this->mockUserProduct);

        $this->mockUser=$user;

        $userProduct = $this->getMockBuilder('\MyProject\Entity\UserProduct')
                       ->disableOriginalConstructor()
                                   ->getMock();

        $userProduct->expects($this->any())
                    ->method('matching')
                        ->will($this->returnValue($this->mockProduct);

        $this->mockUserProduct = $userProduct;

        $product =$this->getMockBuilder('\MyProject\Entity\Product')
                     ->disableOriginalConstructor()
                                 ->getMock();

        $product->expects($this->any())
                    ->method('getProduct')
                        ->will($this->returnValue($this->mockProduct);

.... and so on.  It's obviously not done yet.


texts: Stitching together badly formatted log files

texts:
I needed to stitch together a large number of badly formatted log files. I [painfully] developed a script for it in R, one that successfully dealt with the various flaws in the dataset. However, this script is rather slow. Considering I'm a neophyte to the field (and don't know what I do not know), I was hoping I could get some suggestions on how to speed this code up - using either R, or any other Windows-based/CLI tools.

#Getting path for all files within a particular directory.
files = list.files(path = dirs[j], full.names = T)

#The files are in csv format and in the ideal case have exactly 5 columns. However, the 5th column can contain an arbitary number of commas. If I try to fread with sep = ",", certain rows can be of arbitarily high length. If I use select = 1:5 to subset each row, I lose data.

#My solution was to read each line into a single column and then seperate into columns within the script based on the location of the first 4 commas.
data <- rbindlist(lapply(files,fread,sep = "\n",fill = T,header = F))

#Removing empty rows.
retain <- unlist(lapply(data, function(x) {
       str_detect(x,".")
   }))
data[retain,] -> data

#Removing rows where there is no data in the 5th column.
retain <- unlist(lapply(data, function(x) {
       str_detect(trimws(x,which ='both') ,".+,.+,.*,.*,.+")
   }))
data[retain,] -> data

#This replaces the first 4 commas with a tab-delimiter.
for(i in 1:4){
data <- data.frame(lapply(data, function(x) {
                    str_replace(x,",","\t")
              }),stringsAsFactors = F)
}

#This splits the row into 5 seperate columns, always.
data <- unlist(lapply(data, function(x) {
  unlist(strsplit(x,"\t",fixed = T))
}))

#Changes the format from a character vector to a data table.
data = data.frame(matrix(data,ncol=5,byrow = T),stringsAsFactors = F)


texts:
Here are all the threads write the log file.  I want to know if the below script is fine, if it needs any modifications, and whether I am utilising threads properly to execute this code quickly.

open(LOG,"+>","log.txt")or die "cant open the file";

my $file_total= scalar @listOfFiles;

    my $div = $file_total/$thread_count;
    $div = ceil($div);
    my $start = 0;
    my $end = $div;

    for ($count = 1; $count <=$thread_count ; $count++)
    {
        my $thread = threads->new(\&register, $start, $end );
        push(@threads,$thread);
        $start = $end;
        $end = $end + $div;
        if ($end > $file_total)
        {
            $end = $file_total;
        }
    }

    foreach (@threads)
    {
       $_->join;
    }
    close(LOG);
    sub register
    {
         my $lstart = shift;
         my $lend = shift;

         for (my $index = $lstart; $index < $lend; ++$index)
         {
             print LOG $listOfFiles[$index];
             process......
             ..........
             .......
         }
     }

process... placeholder: I have a huge number of documents, say, 1 million.  I am collecting all the 1 million filenames, storing them in the listOfFiles array, and dividing the array index based on the number of threads the user wants.  I am passing every thread and the start and end indices to fetch the document name, process the document, and store the status of the output to a log file.


texts:
This used to be university work now I put it our here for review purposes.

Could this be constructed better?
Is there any easier path, performance-wise?
Could this be optimised?
Which features and functionalities could/should be added to this game?

<!doctype html>

    <title>Math.floor</title>

 Math.floor: The Maths Game


texts:
The following classes are a simplification of an auto-generated code of an ORM (targeting Microsoft's Dynamics CRM):

public class Entity
{
    public Dictionary<string, object> Fields { get; set;}
    public virtual T GetFieldValue<T>(string fieldName)
    {
         return (T)Fields[fieldName];
    }
    protected virtual void SetFieldValue(string fieldName, object value)
    {
         Fields[fieldName] = value;
    }
}
partial class Account : Entity
{
    public string Name
    get
    {
         return GetFieldValue<string>("Name");
    }
    set
    {
         SetFieldValue("Name", value);
    }
    public string Email
    get
    {
         return GetFieldValue<string>("Email");
    }
    set
    {
         SetFieldValue("Email", value);
    }
}

When an Entity's derived class is retrieved from the database, the base class Fields property is automatically assigned (again, this is not something in my control) with the selected fields.

I'd like a review of the following class, intended to validate "partially" filled objects:

public class EntitySubset<T> : Entity where T : Entity
{
    public EntitySubset(T subset)
    {
        foreach (var field in subset.Fields)
        {
            SetFieldValue(field.Key, field.Value);
        }
    }
    public override U GetFieldValue<U>(string fieldName)
    {
        if (!Fields.ContainsKey(fieldName))
        {
            throw new Exception($"{typeof(T).Name} must contain {fieldName} as a key!");
        }
        return base.GetFieldValue<U>(fieldName);
    }
}

an example usage:

private List<Account> GetSimiliarAccounts(EntitySubset<Account> targetAccount)
{
    var similarityFields = new[] { "Name", "Email" };
    var similiarAccounts = db.Where(account =>
       similarityFields.All(f =>
          account.GetFieldValue<object>(f) == targetAccount.GetFieldValue<object>(f))
    ).ToList();
    return similiarAccounts;
}

so that if GetSimiliarAccounts is called as follows:

GetSimiliarAccounts(new EntitySubset<Account>(new Account { Email = "b" }));

then the output is:

System.Exception: 'Account must contain Name as a key!'

Note that EntitySubset<T> and its error messaging is intended for developers to "fail fast", namely: to quickly spot and fix bugs stemming from a partially filled object.

What do you think of EntitySubset<T> as a validation implementation?


texts: Determine numeric month based on actual moth

texts:
The following script uses a switch statement to determine the numeric month based on the actual month. Is there a more succinct way (read: elegant, or less time to write) to accomplish the same result? Is there a down-side or up-side to either approach?

jsFiddle

var month = "May";
            var m;
            switch(month) {
                case "January":
                    m = 0;
                    break;
                case "February":
                    m = 1;
                    break;
                case "March":
                    m = 2;
                    break;
                case "April":
                    m = 3;
                    break;
                case "May":
                    m = 4;
                    break;
                case "June":
                    m = 5;
                    break;
                case "July":
                    m = 6;
                    break;
                case "August":
                    m = 7;
                    break;
                case "September":
                    m = 8;
                    break;
                case "October":
                    m = 9;
                    break;
                case "November":
                    m = 10;
                    break;
                case "December":
                    m = 11;
                    break;
                default:
                //
            }
        console.log(m);


texts:
I am have made a small copy-to-clipboard script.
I have made a method called copyToClipboard that does the following:

create an input box;
read the content of an HTML element;
set the input box value to the content previously read;
append the input box to a container;
select and copy its value to the clipboard;
remove the input box (no longer useful);

class ActivationCodeComponent{
  constructor() {
    this.copyButton = document.querySelector('#copy_button');
  }

  copyToClipboard () {
    const el = document.createElement('input');
    const codeBox = document.querySelector('#box');
    const activationCodeContainer = document.querySelector('#activation_code');
    el.value = activationCodeContainer.textContent;
    el.setAttribute('readonly', '');
    codeBox.appendChild(el);
    el.select();
    document.execCommand('copy');
    codeBox.removeChild(el);
  }

  handleClick() {
    this.copyButton.addEventListener("click", this.copyToClipboard);
  }

  init() {
    this.handleClick();
  }
}
const activationCodeComponent = new ActivationCodeComponent();
activationCodeComponent.init();
.container  {
   text-align: center;
}

#box  {
  border: 1px solid #ccc;
  width: 200px;
  margin: 20px auto;
  padding: 0 10px;
}

.code {
  letter-spacing: 2px;
  color: #069;
  font-size: 16px;
  font-weight: bold;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet"/>

Your activation code

B12A7

  <button id="copy_button" class="btn btn-sm btn-success text-uppercase">Copy code</button>

The problem
The validity of the script is too dependent on a set of HTML elements with the IDs I have used.
Could it be made more flexible?


texts:
Learn You a Haskell shows the union function:

  union also acts like a function on sets. It returns the union of two
  lists. It pretty much goes over every element in the second list and
  appends it to the first one if it isn't already in yet. Watch out
  though, duplicates are removed from the second list!

Example:

ghci> [1..7] `union` [5..10]
[1,2,3,4,5,6,7,8,9,10]

Please critique my implementation.

import Data.List (nub)

union' :: (Eq a) => [a] -> [a] -> [a]
union' xs ys = nub (xs ++ ys)

This 1-line implementation seems easy to understand, however I'm concerned at its bad/mediocre performance. Consider that I'm appending xs to ys, and then performing nub on the whole list.


texts: Backbone view that "starts" the application

texts:
I use the the View below to get things started.  I'm not interested in Marionette.

This code is what is constructed when the application first loads.  It in turn loads the composite view which is common to all loads. It also constructs other objects needed for the application.

I'm looking for feedback on code structure and not naming conventions.

/***********************************************************************************************************************
** BVApp
*/

    var BVApp = Backbone.View.extend({
        Name: 'BVApp',
        el: window,
        initialize: function () {
            var token = Storage.getToken(),
                pub_req = $A.Reg.get('pub_req');

            // View like objects - inject DOM elements needed
            var BodyInstance = new Body(document.body);
            var ErrorHandlerInstance = new ErrorHandler(document.body);
            var TitleAjaxInstance = new TitleAjax($A.el("#timer"));

            this.setFrame();
            this.loadCommonViews();
            if (pub_req) {
                Page.flip('splash');
            } else if (!token) {
                Page.flip('splash');
            } else {
                this.loadViews(token);
            }
        },
        setFrame: function () {
            var test = $A.el('#super-1');
            if(test){
                test.style.display='none';
            }
            $A.el('#mega_wrap').style.display='inherit';
        },
        loadCommonViews: function () {
            $A.Mod.add(new BVFave({el: $A.el('#fm'), collection: new BCFave([], {data: {h_token: "1FOO", model: "FavoriteReader"}})}));
            $A.Mod.add(new BVFeed({el: $A.el('#at_view'), collection: new BCFeed([], {data: {h_token: "1FOO", model: "FeedReader"}})}));
        },
        loadViews: function (token) {
            var composite_view = $A.Mod.add(new BVComposite());
            composite_view.render(token);
            Page.flip(Storage.get('page'));
        }
    });


texts:
I'm trying to create a generic notification service that works with

Mail (smtp )
Sms (Third part vendor)
Push events (For future)

  Purpose : Create a notification service and it should be pluggable

A Common interface for all notification service

public interface INotificationService
{
    bool Notify();
}

Mail Service

public class MailService : INotificationService
{
    readonly string address;
    readonly string subject;
    readonly string message;

    public MailService(string adrss, string subject, string mesg)
    {
        this.address = adrss;
        this.subject = subject;
        this.message = mesg;
    }

    public bool Notify()
    {
        //pseudo code
        Console.WriteLine("Email service invoked " + address + subject + message);
        return true;

    }
}

Sms Service

public class SmsService : INotificationService
{
    readonly string phoneNumber;
    readonly string message;
    readonly ISmsSender sender;
    public SmsService(string phone, string msg, ISmsSender send)
    {
        this.phoneNumber = phone;
        this.message = msg;
        this.sender = send;
    }

    public bool Notify()
    {
        Console.WriteLine("Sms service invoked" + phoneNumber, message);
        return sender.SendSms(message);

    }
}

ISms Sender interface Mocking third party sms service

public interface ISmsSender
{
    bool SendSms(string msg);
}

public class SmsSender : ISmsSender
{
    public bool SendSms(string msg)
    {
        return true;
    }
}

Consuming program

 INotificationService smsNotification = new
                                       SmsService("12346789", "content" ,new SmsSender());
smsNotification.Notify();

INotificationService mailNotification =
                                   new MailService("mail@gmail.com", "subject", "content");
mailNotification.Notify();

Please find the source code and have added couple of unit test here

Questions

Is it a good design or Any better design
Based on the code coverage still 42% are not covered.
Passing all necessary arguments in the constructor have any impact on maintainability


texts:
Is authentication implemented correctly?

At the entry point to the app, which is App.js, query the Django server, which responds whether the current user is authenticated after checking request.user.isAuthenticated

I am not using redux, just storing authentication state in a local object (this can't be tampered with I assume?). This is reset on signout and I guess it is also lost and re-queried whenever the app is reloaded.

All app routes except the login route are behind PrivateRoute, which will redirect to login unless myAuth.isAuthenticated === true.

Any additional comments welcome.

App.js

import React, {Component} from "react";
import ReactDOM from "react-dom";
import MainContainer from "../containers/MainContainer";
import {Provider} from "react-redux";
import store from "../store";
import BrowserRouter from "react-router-dom/es/BrowserRouter";
import {Redirect, Route, Switch} from "react-router-dom";
import LoginPage from "./LoginPage";
import myAuth from "../myAuth";
import {APP_LOGIN_PATH} from "../constants/paths";

class App extends Component {
    state = {
        loaded: false,
    };

    componentDidMount() {
        myAuth.authenticate(() => this.setState({
            loaded: true,
        }))
    }

    render() {
        const { loaded } = this.state;
        return (
            loaded ? (
                <BrowserRouter>
                    <Switch>
                        <Route path={ APP_LOGIN_PATH } component={ LoginPage } />
                        <PrivateRoute path="/app" component={ MainContainer } />
                    </Switch>
                </BrowserRouter>
            ) : (

Loading...

            )
        )
    }
}

const PrivateRoute = ({ component: Component, ...rest }) => (
    <Route {...rest} render={(props) => (
        myAuth.isAuthenticated === true
        ? <Component {...props} />
        : <Redirect to={{ pathname: APP_LOGIN_PATH, state: { from: props.location }}} />
    )} />
);

const wrapper = document.getElementById("app");

wrapper ? ReactDOM.render(
    <Provider store={ store }>
        <BrowserRouter>
            <App />
        </BrowserRouter>
    </Provider>
    , wrapper) : null;

myAuth.js

const myAuth = {
    isAuthenticated: false,
    isStaff: false,
    isSuperuser: false,

    authenticate(cb) {
        console.log("authenticate");
        fetch("/session_user/")
            .then(response => {
                if (response.status !== 200) {
                    console.log("Something went wrong during authentication");
                }
                return response.json();
            })
            .then(data => {
                this.isAuthenticated = data.isAuthenticated;
                this.isStaff = data.isStaff;
                this.isSuperuser = data.isSuperuser;
            })
            .then(cb)
    },

    signout(cb) {
        fetch("/logout/")
            .then(response => {
                if (response.status !== 200) {
                    console.log("Something went wrong while logging out");
                }
            })
            .then(data => {
                this.isAuthenticated = false;
                this.isStaff = false;
                this.isSuperuser = false;
            })
    }
};

export default myAuth;


texts: Python scanner for the first free port in a range

texts:
I have a distributed application (YARN), which runs a WebApp.

This application use a default port to start (8008), before I start I need to check if port is in use.

A container may run in the same virtual machine, hence port may be in use.
(Max I have 4 containers in WebApp).

I created the following code which seem to work, but want to see if there are some clean ups/improvements suggested.

def port_in_use(port):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    result = sock.connect_ex(('127.0.0.1', port))
    if result == 0:
      return True
    else:
      return False

def start_dashboard():
    base_port = os.getenv('DASHBOARD_PORT_ENV_VAR', 8008)
    scan_ports = True
    attempts = 0
    max_attempts = 10
    while(scan_ports and attempts <= max_attempts):
      if port_in_use(base_port):
         base_port += 1
         attempts += 1
      else:
         scan_ports = False
    if attempts == max_attempts:
       raise IOError('Port in use')
    dashboard.configure(port=base_port)
    dashboard.launch()


texts: Add 100 to all values in nested dictionary

texts:
I want to change all values in a multidimensional dictionary. I have written this line of code, which does what I want it to do and I am trying to find out if this is the optimal way or just some convoluted solution I came up with:

 dict_data = dict([x,dict([y, dict_data[x][y]+100] for y in dict_data[x])] for x in dict_data)


texts: JavaScript function that composes other functions to create a master function

texts:
I wrote a little Compose function, which takes a variable number of arguments of type function and then calls each of those in order, passing in the value of the previously called function.

My function works just like I want it to, however since the function provides a way to program functionally, I figured it's application should be functional as well--but it's not.

The Compose function:

const Compose = (...functions) => {
    return val => {
        let i = 0,
            len = functions.length,
            fnResult = functions[i](val);

        while (++i < len) {
            fnResult = functions[i](fnResult);
        }

        return fnResult;
    };
};

Application:

const add7 = num => num + 7;
const squared = num => num * num;
const subtract13 = num => num - 13;

const add7ThenSquareThenSubtract13 = Compose(add7, squared, subtract13);

add7ThenSquareThenSubtract13(5);   // 131
add7ThenSquareThenSubtract13(0);   // 2
add7ThenSquareThenSubtract13(100); // 11436

What it's doing under the hood:

// Let's say 'val' is 5.

// 1. Call add7(val), result is 12.
// 2. Call squared(12), result is 144
// 3. Call subtract13(144), result is 131

// subtract13( squared( add7(val)  ) ) === 131

Anyone have any idea how I can make the logic inside the Compose function more functional? I am having a hard time since I am just now learning to think in a functional way. Is it even possible? I don't know of any ways to loop an array without using for or while.

UPDATE:

Never mind, I figured it out. Didn't think I could use reduce for this but I suppose I can:

const Compose = (...functions) => {
    return val => {
        return functions.reduce((prevVal, currFunc) => {
            return currFunc(prevVal);
        }, functions[0](val));
    };
};

It's not pretty, but it works--and it's functional. ;)


texts: Finding subsets from specified array, avoiding duplicates

texts:
I am printing subsets from an array whose sum has been specified, while avoiding duplicates.

I think there may be improvements, potentially on how using map is bad idea, or how can I avoid map to filter the duplicates.

Additionally, in my code I want to reverse the sort order of an int array using streams which is asked over here, are there any alternatives rather than sorting and reversing the array?

public class FindSubSetArray {

    private static final int TARGET_SUM = 24;
    private static Map<String,Integer> subSet = new HashMap<>();
    private static int count = 0;

    public static void main(String[] args) {
        int[] array= {2, 5, 1, 2, 4, 1, 6, 5, 2, 2};
        Arrays.sort(array);
        findSubset(array, 0, 0, "");
        subSet.keySet().stream().forEach(System.out::println);
    }

    public static void findSubset(int[] array, int index, int current, String subSetString) {

        if (current > TARGET_SUM || array.length < index) return;

        for (int i = index; i < array.length; i++) {
            int presentSum = current + array[i];
            if (presentSum == TARGET_SUM) {
     //         System.out.println(subSetString + " " + array[i]);
                subSet.put(subSetString + " " + array[i], count++);
            } else {
                findSubset(array, i + 1, presentSum, subSetString + " " + array[i]);
            }
        }
    }
}


texts: Legacy Data and Current System Data Merged together in a single SQL Database

texts:
There was a lot going on in this Database that I couldn't change, a lot of bad practices because the data was originally being pulled from an old mainframe database and then transformed by a horrible looking VB service and inserted into several (horrible) tables in an SQL Database so the information could be used by a VB.NET website (1.1 or something like that?)

I would like to know how clean this code is and whether or not there is anything that can be done to further my knowledge in retrieving information from Legacy Data stores.

This was an Ad-Hoc, and from the looks of the first Where clause it would be a very interesting Stored Procedure to say the least.

WITH Table1 AS
(
    SELECT
        CaseParticipantTable.DOCK_NO AS CaseNbr, ActivityTable.JUDGMENT_DATE AS JudgmentDate, ActivityTable.AMOUNT_TYPE AS AmountType,
            ActivityTable.AMOUNT AS JudgmentAmount, CaseParticipantTable.FILING_NAME AS CaseParticipant
    FROM
        UJSJudgementQuery.dbo.CJ_SC_CASE_PARTICIPANT AS CaseParticipantTable
        INNER JOIN UJSJudgementQuery.dbo.CJ_SC_JUDGMENT_ACTIVITY AS ActivityTable ON CaseParticipantTable.DOCK_NO = ActivityTable.DOCK_NO
    WHERE CaseParticipantTable.FILING_NAME IN ('/CountyName COUNT AUDITORS OFFICE', '/CountyName COUNTY AUDITOR',
                    '/CountyName COUNTY AUDITOR''S OFFI', '/CountyName COUNTY AUDITORS OFFI', '/CountyName COUNTY AUDITORS OFFIC',
                    '/CountyName COUNTY AUDITORS OFFICE', '/CountyName COUNTY TREASURER', '/CountyName County Treasurer',
                    '/CountyName COUNTY/ AUDITOR', '/CountyName COUNTY/AUDITORS OFFIC', '/CountyName COUNTY C/O AUDITORS')
        AND AMOUNT > 0
), ActiveJudgments AS (
    SELECT Table1.*, Judgments.JUDGMENT_STATUS
    FROM
        Table1
        INNER JOIN UJSJudgementQuery.dbo.CJ_SC_JUDGMENTS AS Judgments ON Table1.CaseNbr = Judgments.DOCK_NO
    WHERE Judgments.JUDGMENT_STATUS = 'A'
), NoInactives AS (
    SELECT ActiveJudgments.*
    FROM ActiveJudgments
    WHERE ActiveJudgments.CaseNbr NOT IN (SELECT Table1.CaseNbr
                                            FROM
                                                Table1
                                                INNER JOIN UJSJudgementQuery.dbo.CJ_SC_JUDGMENTS AS Judgments ON Table1.CaseNbr = Judgments.DOCK_NO
                                            WHERE Judgments.JUDGMENT_STATUS = 'I')
)
SELECT DISTINCT
    NoInactives.CaseNbr
    , CaseParticipantTable.FILING_NAME AS Debtor
    , NoInactives.JudgmentDate
    , NoInactives.JudgmentAmount
    , NoInactives.JUDGMENT_STATUS
FROM
    NoInactives
    INNER JOIN UJSJudgementQuery.dbo.CJ_SC_CASE_PARTICIPANT AS CaseParticipantTable ON NoInactives.CaseNbr = CaseParticipantTable.DOCK_NO
WHERE (CaseParticipantTable.PARTICIPANT_CODE = 'DBT' OR CaseParticipantTable.PARTICIPANT_CODE = 'DEF')
ORDER BY Debtor ASC

The ORDER BY at the end is just for checking my data, it is either by date or by Debtor Name

I am looking for suggestions on future query writing


texts: Computationally efficient way of comparing and merging like object keys

texts:
Though I can achieve most ends via a series of conditionals and arithmetic, especially when iterating over or comparing arrays I frequently hear of much more efficient implementations.

Hailing from PHP, I am accustomed to having array_merge() accessible; this JS function is different in that key that don't match the first array should be discarded (and generally, I'm using this sort of thing as a way of generating config objects for various parameterized components of a larger project).

I was hoping a serious comp-sci nerd could comment to the efficiency of this implementation of the same in JavaScript, and if it can be improved, explain—even very briefly—what's happening, computationally as it were. I have the intuitive sense that there it a bit-wise operation that could achieve this faster:

function mapConfig(template, cfg) {
    var configured_template = {};
    for (key in cfg) {
        template[key] = key in template ?  cfg[key] : false;
    }
    return template
}

var template = {key1:false, key2:false, key3:false};
var cfg = {key1:true, keyTWO:"puppies", key3:false};

var cfg_obj = mapConfig(template, cfg);
console.log(cfg_obj);

//output >>> {key1:true, key2:false, key3:false}

I appreciate any insights anyone could offer.


texts: PHP Class for sending a response to Flash

texts:
I've created this code to send a response to Flash which can be handled with AS2/AS3:

<?php
    /**
     * Basic server-side API controls for Flash-PHP communication
     * @author Marty Wallace
     * @version 1.0.2
     */
    class API
    {
        // MySQL
        private $con;
        private $sql_hostname = '';
        private $sql_username = '';
        private $sql_password = '';
        private $sql_database = '';

        // Properties
        public $response;

        /**
         * Constructor
         */
        public function __construct()
        {
            // Attempt connection
            $this->con = mysql_connect(
                $this->sql_hostname,
                $this->sql_username,
                $this->sql_password
            );

            // Connection could not be established
            if(!$this->con)
            {
                $this->response = array(
                    "status" => "no_connection",
                    "message" => "Could not connect to MySQL, try again later."
                );

                $this->respond();
            }

            // Select database
            mysql_select_db($this->sql_database);
        }

        /**
         * Send response back to Flash
         */
        public function respond()
        {
            $ar = array();
            foreach($this->response as $key => $value)
                array_push($ar, $key . "=" . $value);

            die(implode("&", $ar));
        }
    }
?>

I'm not that great with PHP and the respond() function seems like it could be written a little better.. Any suggestions?


texts:
A while back I was asked the following question in a phone interview, and to be honest it stumped me. After many long nights of rolling the problem around in my head I think I came up with a decent solution.

The Question:

  Given a list of intervals like:

  [(5, 7), (11, 116), (3, 6), (10, 12), (6, 12)]

  Write a function that will merge overlapping intervals.

My solution:

def merge_intervals(intervals):
    """
    A simple algorithm can be used:
    1. Sort the intervals in increasing order
    2. Push the first interval on the stack
    3. Iterate through intervals and for each one compare current interval
       with the top of the stack and:
       A. If current interval does not overlap, push on to stack
       B. If current interval does overlap, merge both intervals in to one
          and push on to stack
    4. At the end return stack
    """
    si = sorted(intervals, key=lambda tup: tup[0])
    merged = []

    for tup in si:
        if not merged:
            merged.append(tup)
        else:
            b = merged.pop()
            if b[1] >= tup[0]:
                new_tup = tuple([b[0], tup[1]])
                merged.append(new_tup)
            else:
                merged.append(b)
                merged.append(tup)
    return merged

if __name__ == '__main__':

    l = [(5, 7), (11, 116), (3, 4), (10, 12), (6, 12)]
    print("Original list of ranges: {}".format(l))
    merged_list = merge_intervals(l)
    print("List of ranges after merge_ranges: {}".format(merged_list))

My questions are:

Run time: Without counting sorting I believe this runs in \$O(n)\$. With counting sorting that would go up to \$O(n*\log(n))\$. Is this correct?
Stylistically does anything need to be improved?
Is there a more efficient way of solving this problem?


texts: RxJs - Flat nested swithMaps and pass variables on Http interceptor

texts:
How can the following method be flattened? I tried to pipe all the operators to the first pipe (from(this.fireAuth.currentUser)) but I lost access to the previous variables. It's an  Angular HTTP interceptor that adds a Bearer Token when the user has logged in, the chain should continue as usual when the user has not logged in yet.
  intercept(
    request: HttpRequest<any>,
    next: HttpHandler
  ): Observable<HttpEvent<any>> {
    const isApiUrl = request.url.startsWith(apiCloudURL);
    return from(this.fireAuth.currentUser).pipe(
      switchMap(user => {
        if (!user) return next.handle(request);
        return from(user.getIdToken()).pipe(
          switchMap(token => {
            if (isApiUrl) request = this.addToken(request, token);
            return next.handle(request).pipe(
              catchError(error => {
                if ( isApiUrl && token && error instanceof HttpErrorResponse && error.status === 403) {
                  return this.handle403Error(request, next);
                }
                else{
                  return throwError(error);
                }
              })
            );
          })
        )
      })
    );
  }

Piping to the first observable... I have no access to the token var in the catchError block
  intercept(
    request: HttpRequest<any>,
    next: HttpHandler
  ): Observable<HttpEvent<any>> {
    const isApiUrl = request.url.startsWith(apiCloudURL);
    return from(this.fireAuth.currentUser).pipe(
      switchMap(user => {
        if (!user) return next.handle(request);
        return from(user.getIdToken());
      }),
      filter<string>(x => typeof(x) === "string"), //x can be string to HttpEvent<any>
      switchMap(token => {
        if (isApiUrl) request = this.addToken(request, token);
        return next.handle(request)
      }),
      catchError(error => {
        if ( isApiUrl && error instanceof HttpErrorResponse && error.status === 403) {
          return this.handle403Error(request, next);
        }
        else{
          return throwError(error);
        }
      })
    );
  }

I'm also wondering what's happening in the scenario where user is falsy, does it mean the request will be canceled because it does not pass the filter?
Thanks.


texts:
I have this code that finds exact matches from a 2D array in a range of two columns on a sheet.

It works ok until the data is formatted as a table then it is slow, really slow. There must be a better way to do this.

Sub compareManifest()
Dim manifest As Worksheet: Set manifest = ActiveWorkbook.Worksheets("Manifest")
Dim approved As Worksheet: Set approved = ActiveWorkbook.Worksheets("Approved Master")
Dim result As Range
Dim findInRange As Range
Dim softwareArray As Variant
' 2d array of software name and version
softwareArray = manifest.Range("A2:B" & lastRow(manifest))
' one of several ranges of software names and versions
Set findInRange = approved.Range("E1:E" & lastRow(approved))
For i = LBound(softwareArray) To UBound(softwareArray)
    Set result = findSoftware(approved, softwareArray(i, 1), softwareArray(i, 2), findInRange)
    If Not result Is Nothing Then
        ' write back some data from successful matches which is the whole point of this exercise
        manifest.Cells(i + 1, 3) = result.Offset(0, -2)
    End If
Next i
End Sub

Function findSoftware(sheet, softwareName, softwareVersion, findInRange) As Range
Dim found As Range
Dim firstFoundAddress As String
Set found = findInRange.Find(what:=softwareName, LookIn:=xlValues, lookat:=xlWhole)
If Not found Is Nothing Then
    firstFoundAddress = found.Address
    Do
        If softwareVersion = sheet.Range(found.Address).Offset(0, 1) Then
            Set findSoftware = sheet.Range(found.Address)
            firstFoundAddress = found.Address
        Else
            Set found = findInRange.FindNext(found)
        End If
    Loop While firstFoundAddress <> found.Address
End If
End Function


texts: Word/expression list frequency distribution

texts:
I have another Python issue, creating a frequency distribution out of a text matching a predefined wordlist. In fact, I work with more than 100,000 text files (each consisting of about 15,000 words), which I want to inread and accordingly match against a word/expression list (vocabulary_dict) of about 6,000 entries. The result should be a dictionary of all that entries attached with their respective frequency.

Here is what I am currently doing:

sample text = "As the prices of U.S. homes started to falter, doubts arose throughout the global financial system. Banks became weaker, private credit markets stopped functioning, and by the end of the year it was clear that the world banks had sunk into a global recession."

vocabulary_dict = ['prices', 'banks', 'world banks', 'private credit marktes', 'recession', 'global recession']

def list_textfiles(directory):
    # Creates a list of all files stored in DIRECTORY ending on '.txt'
    textfiles = []
    for filename in listdir(directory):
        if filename.endswith(".txt"):
            textfiles.append(directory + "/" + filename)
    return textfiles

for filename in list_textfiles(directory):
    # inread each report as textfile, match tokenized text with predefined wordlist and count number of occurences of each element of that wordlist
    sample_text = read_textfile(filename).lower()
    splitted = nltk.word_tokenize(sample_text)
    c = Counter()
    c.update(splitted)
    outfile = open(filename[:-4] + '_output' + '.txt', mode = 'w')
    string = str(filename) # write certain part of filename to outfile
    string_print = string[string.rfind('/')+1:string.find('-')] + ':' + string[-6:-4] + '.' + string[-8:-6] + '.' + string[-12:-8]
    for k in sorted(vocabulary_dict):
    # recognize if wordlist element consist of one or more tokens, accordingly find matching token length in text file (report)
        spl = k.split()
        ln = len(spl)
        if ln > 1:
            if re.findall(r'\b{0}\b'.format(k),sample_text):
                vocabulary_dict[k.lower()] += 1
        elif k in sample_text.split():
            vocabulary_dict[k.lower()] += c[k]
    outfile.write(string_print + '\n')
    # line wise write each entry of the dictionary to the corresponding outputfile including comapany name, fiscal year end and tabulated frequency distribution
    for key, value in sorted( vocabulary_dict.items() ):
        outfile.write( str(key) + '\t' + str(value) + '\n' )
    outfile.close()

# Output accoring to the above stated example should be in the form:
"selected part of filename (=string1)"
'prices' 1
'banks' 2
'world banks' 1
'private credit marktes' 1
'recession' 1
'global recession' 1

The code works quite well, but I still think there is room for optimization, since the time to process a textfile is ca. 1 minute.

Is there a way to make the matching of text to the word/expression list faster`?