texts:
I'm new to Postgres and PostGIS, but not to geospatial applications.

I have a table loaded up with graph data in the form of links (edges). The link database has about 60,000,000 rows. I am trying to extract the nodes to allow for quicker searching. For some of my proof-of-concept work I was able to use the link table for the searches, but there will be lots of duplicates, and there's no guarantee that either the source column or the target column contains all nodes.

This is Postgres & PostGIS, so I am also using the table to cache a geometry->geography conversion. Yes I do need to use geography fields. I'm also copying the geometry information "just in case".

Table creation SQL:

-- Recreate table and index
DROP TABLE IF EXISTS nodes;

CREATE TABLE nodes (node integer PRIMARY KEY, geog geography(POINT,4326) );

CREATE INDEX geogIndex ON nodes USING GIST(geog);

SELECT AddGeometryColumn('nodes', 'geom', 4326, 'POINT', 2);

-- Insert all unique nodes from the source column
INSERT INTO nodes (node,geog,geom)
       SELECT DISTINCT ON (source) source,geography( ST_Transform(geom_source,4326)),geom_source
       FROM view_topo;

-- Insert any nodes in the target column that we don't have already
INSERT INTO nodes (node,geog,geom)
       SELECT DISTINCT ON (target) target,geography( ST_Transform(geom_target,4326)),geom_target
       FROM view_topo
       WHERE NOT EXISTS( SELECT 1 FROM nodes WHERE nodes.node = view_topo.target);

VACUUM ANALYZE;

I left the first INSERT running overnight and it took about 2-3hrs to run. This resulted in about 40,000,000 unique nodes being added.

I have just enabled the second INSERT and the VACUUM ANALYZE. I am expecting it to take until at least lunchtime.

Luckily this is only a batch job that has to be executed once after I've loaded a new link table, but is there a better way? Is there a faster way?


texts: Removing entries with duplicate columns from large .vcf files

texts:
First of all, please note that the input (.vcf) file is very large (>60 GB).

So I am reading the .vcf files line by line, but since I have to check for duplicate of two columns present in the input vcf file, I extract those two columns from the input file to a text file ("dup_posID.txt") and then check for duplicates, and remove the duplicates and save it to a new text file ("dup_posID_re.txt").
Then I create a new vcf file (.redp.vcf) by comparing the recoded text file.

The code works fine and am getting the desired result. But its taking a lot of time. more that 24 hours. How can I reduce that time?

vcf_File_name = "chr7_dup_fin"

with  open(vcf_File_name+".vcf","r") as f, open(vcf_File_name+"dup_posID.txt", "w+") as D:

    for v_line in  f:

        if v_line[0] != '#':
            v_cols = re.split('[ ]+|[   ]+',v_line.strip())
            r= v_cols[2].strip() +"\t"+ v_cols[1].strip() + "\n"
            req1 += r
            #print(req1)
    D.write(req1)

D1=open(vcf_File_name+"dup_posID.txt","r")

D2 = open (vcf_File_name+"dup_posID_re.txt", "w+")

lines1 = D1.readlines()

for line1 in lines1:
    for line1 in lines1:
        cols= re.split('[   ]', line1.strip())
        try:
            dup[cols[0]] += 1
            #print cols[0]
        except:

            try:
                dup_pos[cols[1]] += 1
                #print "Duplicate Position: "+cols[1]
            except:
                dup_pos[cols[1]] = 0
                dup[cols[0]] = 0

                p = cols[0].strip()+"\t"+cols[1].strip()+"\n"
                q += p
D2.write(q)

D2.close()

with  open(vcf_File_name+".vcf","r") as f, open(vcf_File_name+".redp.vcf","w+") as f1 :
    D2=open(vcf_File_name+"dup_posID_re.txt", "r")
    lines2=D2.readlines()
    for v_line in  f:

        if v_line[0] != '#':
            v_cols = re.split('[ ]+|[   ]+',v_line.strip())

            st = v_cols[2]+"\t"+v_cols[1]+"\n"
            if st in lines2:
                lines2.remove(st)
                s = "\t".join(v_cols)
                s += "\n"

                #req += s
                f1.write(s)
                #f1.write("\n")
        else:
            s = v_line
            f1.write(s)
    f1.close()

The input .vcf file has 2512 columns and is tab separated.
Example:

2   26813   rs574377045  C  A   100 PASS    AC=2;AF=0.000399361;AN=5008;NS=2504;DP=18198;EAS_AF=0.001;AMR_AF=0.0014;AFR_AF=0;EUR_AF=0;SAS_AF=0;VT=SNP;AA=C GT   0|0
2   26816   rs535194004  C  T   100 PASS   AC=1;AF=0.000199681;AN=5008;NS=2504;DP=18346;EAS_AF=0;AMR_AF=0;AFR_AF=0.0008;EUR_AF=0;SAS_AF=0;VT=SNP;AA=C      GT   0|0

Here, one line extends till 2512 columns.


texts:
I have a table in MySQL with approx. 20 million rows and the following columns:

id | word_eng | word_indic

I have to translate the English word (the value in column word_eng) into Indian language (the value in column word_indic) using the Google Translate API.

I have written PHP code which spawns multiple cURL requests and fetches data from API and updates it into the table. But this process is quite slow; it takes about 100 to 200 words per second.

I am using RollingCurl for multiple cURL requests. I am running this as a cron job.

What's the best way to make it as fast as possible?

<?php
include_once('db.php');
include_once('functions.php');
include_once('rolling-curl-master/RollingCurl.php');

$table = $argv[1];

$q = "SELECT * from $table where word_indic is null limit 500000";
$result = $conn->query($q); $n = 0;

$urls = array();

while ($row = $result->fetch_assoc())
{
    $id = $row['id'];
    $word = rawurlencode(getName($row['name_eng']));

//getName is a simple function which does some trimming and cleaning up of string

    $url = 'https://www.google.com/inputtools/request?text='.rawurlencode($word).'&ime=transliteration_en_te&id='.rawurlencode($id);
    array_push($urls, $url);
}
//print_r($urls);

unset($url);

$rc = new RollingCurl("request_callback");

// the window size determines how many simultaneous requests to allow.
$rc->window_size = 300;

foreach ($urls as $url)
{
    // add each request to the RollingCurl object
    $request = new RollingCurlRequest($url);
    $rc->add($request);
}
$rc->execute();

function request_callback($response, $info)
{
    // parse the page title out of the returned HTML
    if (preg_match("~<title>(.*?)</title>~i", $response, $out)) {
        $title = $out[1];
    }

    //echo "$title
";
    //print_r($info);
    $parts = parse_url($info['url']);
    parse_str($parts['query'], $query);
    $id = $query['id'];
    $text = $query['text'];

    //echo "
";
    $trans = json_decode($response)[1][0][1][0];

    global $conn; global $table; global $urls; global $n;
    if ($trans != '' and !preg_match('/[a-z]/', $trans))
    {
        $conn->query("update $table set word_indic='$trans' where id='$id'"); $n++;
    }
}

?>

Table structure:

CREATE TABLE `words` (
 `id` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
 `word_eng` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
 `word_indic` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
 PRIMARY KEY (`id`),
 FULLTEXT KEY `word_eng` (`word_eng`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci


texts: Please critique my Enumerable DataReader with yield and log data access pattern

texts:
We are generating our data access code following this pattern:

public static IEnumerable<SomeDataOutputDTO> GetSomeData(SomeDataInputDTO dto, IDbConnection dbConnection) {
    var queryString = @"Some SQL query";
    using (var command = (OracleCommand)dbConnection.CreateCommand()) {
        command.CommandText = queryString;
        command.BindByName = true;
        command.Parameters.Add("SomeParam", OracleDbType.Int16, dto.SomeParam, ParameterDirection.Input);
        command.Parameters.Add("SomeOtherParam", OracleDbType.Int16, dto.SomeOtherParam, ParameterDirection.Input);
        var success = false;
        try {
            using (var reader = command.ExecuteReader()) {
            success = true;
            while (reader.Read()) {
                object data;
                var newDto = new SomeDataOutputDTO();
                data = reader["first_column"];
                newDto.FirstColumn = (short)Convert.ChangeType(data, typeof(short));
                data = reader["second_column"];
                if (data != DBNull.Value) {
                    newDto.SecondColumn = (short)Convert.ChangeType(data, typeof(short));
                }

                yield return newDto;
            }
        }
    }
    finally {
        if (success) {
            QueryLogger.Debug(queryString, command.Parameters, "TheseQueries.GetSomeData");
        }
        else {
            QueryLogger.Error(queryString, command.Parameters, "DocumentTypeQueries.GetDocumentTypesByApplyType");
        }
    }
}

I want to know if there are any pitfalls regarding this code.
As you can see the control flow is a bit complex since it returns an Enumerable and to do that it contains a

yield return

statement.

Note also that there are two using() blocks and one try block in the middle. The purpose of the try-finally block is to ensure proper logging of the query, in Error level if there was an error, or Debug level if the query ran OK. The purpose of the using blocks is to ensure proper disposal of the command and reader objects.

The QueryLogger helper class uses log4net, not that it should matter much.

Please also remember that this is code generated by a tool, so whether it's readable or easy to maintain is not a concern. My concerns are with proper resource disposal, proper logging, etc.

Also, returning an IEnumerable is very important because it allows to use LINQ when consuming the results. Of particular interest when using LINQ are:

Take / Skip
ToLookup / ToDictionary
ToList
FirstOrDefault


texts:
I'm trying out a new approach to SASS stylesheets, which I'm hoping will make them more organizined, maintainable, and readable.

I often feel like there is a thin line between code that is well structured and code that is entirely convoluted. I would appreciate any thoughts as to which side of the line this code falls.

I don't want to tell you too much more about what these styles are intended to produce -- my hope is that the code will explain this for itself. Also note that this is part of a larger project, so don't worry about missing dependencies, etc.

Questions for review

How would you make this code easier to read/maintain?
Can you understand what these styles are trying to produce?
Is the purpose of the mixins/placeholders clear?

File structure:

theme/sass/partials/
    widget/
        collapsable/
            _appendicon.scss
            _closeall.scss
            _toggleswitch.scss
        collapsable.scss
        collapsablered.scss
    _button.scss

collapsable.scss

/**
 * Collapsable widget.
 *
 * The widget has "open" and "closed" states.
 * The widget has a Toggle Switch, which is visible in
 * both open and closed states.
 *    All other content is hidden in the closed state.
*/
@mixin setOpenState {
  &,
  &.state-open {
    @content;
  }
}

@mixin setClosedState {
  &.state-closed {
    @content;
  }
}

@mixin setToggleSwitchStyles {
  &>h1:first-child, .collapseableToggle {
    @content;
  }
}

@import "collapsable/closeall";
@import "collapsable/appendicon";
@import "collapsable/toggleswitch";

%collapsable {
  @include setOpenState {
    @include setToggleSwitchStyles {
      @extend %toggleSwitch;
    }
  }

  @include setClosedState {
    @extend %closeAllExceptToggle;

    @include setToggleSwitchStyles {
      @extend %toggleSwitchClosed;
    }
  }
}

collapsablered.scss

@import "collapsable";
@import "../button";

%collapsableRed {
  @extend %collapsable;

  @include setOpenState {
    @include setToggleSwitchStyles {
      @extend %buttonWithRedBg;
    }
  }

  @include setClosedState {
    @include setToggleSwitchStyles {
      @extend %buttonWithDarkBg;
    }
  }
}

collapsable/_closeall.scss

%closeAllChildren {
  * {
    display: none;
  }
}

%closeAllExceptToggle {
  @extend %closeAllChildren;

  @include setToggleSwitchStyles {
    display: block;

    .icon-sprite {
      display: inline-block;
    }
  }
}

collapsable/_appendicon.scss

@import "compass/utilities/general/clearfix";
@import "../../icon";

@mixin appendIcon {
  @include pie-clearfix;

  .icon-sprite {
    margin-right: 5px;
    vertical-align: -3px;
  }

  &:after {
    content: '';

    position: relative;
    top: 2px;

    float: right;

    @content;
  }
}

%withCloseIcon {
  @include appendIcon {
    @extend .icon-close;   // defined in _icon.scss
  }
}

%withOpenIcon {
  @include appendIcon {
    @extend .icon-rChevronDk;   // defined in _icon.scss
    top: 1px;
  }
}

collapsable/_toggleswitch.scss

%toggleSwitch {
  cursor: pointer;
  @extend %withCloseIcon;
}

%toggleSwitchClosed {
  @extend %toggleSwitch;
  @extend %withOpenIcon;
}

partials/_button.scss

@import "typography";

%buttonWithRedBg {
  @extend %textOnRedBg;  // defined in _typography.scss

  cursor: pointer;

  &:hover {
    background-color: $redDk;
  }

  &:active {
    background-color: $black;
  }
}

%buttonWithDarkBg {
  @extend %textOnDarkBg;  // defined in _typography.scss

  cursor: pointer;

  &:hover {
    background-color: #000;
  }

  &:active {
    background-color: $redDk;
  }
}


texts: Counting block combinations I: Project Euler 114

texts:
Problem statement (For more detailed description (including pictures), please, visit the link):

  A row measuring seven units in length has red blocks with a minimum
  length of three units placed on it, such that any two red blocks
  (which are allowed to be different lengths) are separated by at least
  one grey square. There are exactly seventeen ways of doing this.

  How many ways can a row measuring fifty units in length be filled?

Code:

from scipy import special
import time
import itertools

def partitionfunc(n,k,l=3):
    '''n is the integer to partition, k is the
    length of partitions, l is the min partition element size'''
    if k < 1:
        return 0
    if k == 1:
        if n >= l:
            yield (n,)
        return 0
    for i in range(l,n+1):
        for result in partitionfunc(n-i,k-1,i):
            yield (i,)+result

def valid_partitions(p):
    total =  p
    count = 0 #Max. number of tiles that can be placed on a row.
    while True:
        count += 1
        total -=3
        if total <= 3:
            break
        total-=1
    '''Find all the valid partitions with length [1,count] that can be placed on the row with length p'''
    data = []
    for k in range(1,count+1):
        min_part = k*3
        for n in range(min_part,p+1):
            Allowed = []
            LIST = list(partitionfunc(n,k))
            for b in LIST:
                if sum(b) + (len(b)-1) <= p:
                    Allowed.append(b)
            data+= Allowed
    return data

def count_permutations(array):
    '''Counts how many possible permutations are there for the particular partition'''
    get_unique_elements = set(array)
    total_length = len(array)
    lengths = [array.count(x) for x in get_unique_elements]
    answer = 1
    for b in lengths:
        answer*= special.comb(total_length, b)
        total_length-= b
    return answer

def calculate_ways(m,n):
    return special.comb(n-m+1,n-2*m+1)

def final(w):
    total_variations = 0
    data = valid_partitions(w)
    for q in data:
        m = len(q)
        remain = w - sum(q) - m + 1
        n = 2*m -1 + remain
        total_variations+= calculate_ways(m,n)*count_permutations(q)
    return int(total_variations+1)

if __name__ == '__main__':
    start = time.time()
    print('Answer: {}'.format(final(50)))
    print(time.time()-start)

I'll explain the reasoning behind the code using example provided by the Euler:

Step 1

First, we find maximum number of tiles that can be placed on the row with length 7. At most only 2 tiles can be placed, hence number equals 2 (call this number count).

Then we iterate through [1,count] and and find all the valid partitions of the number seven:

In our case:

1 tile: (3), (4), (5), (6), (7)  (The reason we omit (1) and (2) is because minimum length of red tile is 3)

2 tiles: (3, 3) (Note, that although (3,4) is a partition of 7 too, but it won't work in our case, because it is specified that there must be at least 1 tile gap between two red tiles.

Step 2.

For each partition obtained in step 1, we calculate number of ways the partition can be placed on the row.

For example:

(5) Represents 1 tile with length 5. There are 3 ways to place such tile.

(3,3) Represents 2 tiles both with length 3. There is 1 way to place them on the row.

When we add up the numbers, we end up with 16. We need to add 1, because (I'm curious why) the row that doesn't contain any red tiles is a valid case too. The final answer is 17.

I believe that there are a lot of things that can be improved. I'm glad to hear any suggestions!

P.S The problem I would like to specifically point out is the variable/function names. I believe there is a possibility to make them way more descriptive/clear then they are now.


texts: Shell script to build executable from node.js

texts:
I'm trying ompile archive node.js executable into one executable using node-packer.
For my all packages, executables of node.js are in {projectRoot}/node_modedules/.bin

The project structure as follow:

|- node_modules
|   |-- .bin
|       |--- executable with source code
|- compiled-bin
|   |-- compiled executable

I wrote a shell script to do it. Hope you can review it:

#!/bin/bash
for filepath in ./node_modules/.bin/*; do
    export baseName="${filepath##*/}"
    nodec $filepath --clean-tmpdir --make-args=-j10 --output=./compile-bin/$baseName.out
    chmod +x ./compile-bin/$baseName.out
done

This is dockerfile, I used to run the above script in container:

FROM node:8.3.0 as package

WORKDIR /usr/src/app
COPY . /usr/src/app

# Install dependencies
RUN apt-get update
RUN apt-get install -y -q --no-install-recommends \
  build-essential \
  python \
  make \
  squashfs-tools \
  gcc \
  g++ \
  clang-3.5 \
  clang++-3.5

# Install nodec compiler
RUN curl -L http://enclose.io/nodec/nodec-linux-x64.gz | gunzip > /usr/local/bin/nodec
RUN chmod +x /usr/local/bin/nodec

# Install npm packages
RUN npm i --no-optional \
  && ls -lh

# Run build scripts
RUN mkdir compile-bin \
  && chmod +x ./build-script \
  && sync \
  && ./build-script


texts: Add up values from one field for multiple months

texts:
I have data as follows:

Payment Date  11/01/18  01/01/2019  11/01/09  11/01/20
Total Amount   4536.87     4654.98   4698.63   4687.69

I need to add up all of the Total Amount starting from 11/01/18 through 11/01/20.

For the months in between dates, I need to use the amount from previous month. I also need to change this to account for bi-weekly and semi-monthly.

For example, for month 12/01/2018, I need to use Total Amount from 11/01/18, in this case 4536.87 and so on.

This is what I have so far. Is there a better way to do this?

public void aggretagePaymentAmounts(List<PaymentFields> fieldsType,
            Payments payments, int numberOfTrialPayments){
    AtomicInteger cnt = new AtomicInteger(1);
    Iterator<PaymentFields> fieldsTypeIterator = fieldsType.iterator();
    PaymentFields fieldType = fieldsTypeIterator.next();
    PaymentFields nextFieldType = null;
    if (fieldsTypeIterator.hasNext()) {
        nextFieldType = fieldsTypeIterator.next();
    }

    LocalDate monthDate = LocalDate.parse(fieldType.getPaymentDate());

    while (cnt.getAndIncrement() <= numberOfPayments) {

        monthDate = monthDate.plusMonths(1);
        //what about semi-monthly and bi-weekly?
payments.aggregate(fieldType.getPaymentAmount());

        if (nextFieldType != null) {
            LocalDate nextFieldTypeDate = LocalDate.parse(nextFieldType.getPaymentDate());
            if (nextFieldTypeDate.getMonthValue() == monthDate.getMonthValue()) {
                fieldType = nextFieldType;
                if (fieldsTypeIterator.hasNext()) {
                    nextFieldType = fieldsTypeIterator.next();
                } else {
                    nextFieldType = null;
                }
            }
        }
    }
}

public class Payments {
    private BigDecimal paymentAmount;//getters/setters not shown here

    public void aggregate(BigDecimal tmpPaymentAmount){
        paymentAmount = add(paymentAmount, tmpPaymentAmount);
    }

    private static BigDecimal add(BigDecimal... addends) {
        return Arrays.stream(addends)
                .filter(Objects::nonNull)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
    }
}


texts: Retrieve next card(s) base on the provided parameter

texts:
How can I remove the duplicated params[:next_cards]?

def index
  if params[:next_cards] # check nil
    render json: current_user.next_cards(params[:next_cards].to_i),
           status: :ok
  else
    render json: current_user.cards, status: :ok
  end
end

Inside the user.rb module,

  def next_cards(num_of_cards=nil)
    if num_of_cards
      cards.next_cards.limit(num_of_cards)
    else
      cards.next_cards
    end
  end

Inside the card.rb module,

  def self.next_cards
    joins(:meta_sm2).merge(MetaSm2.next_cards).order('RANDOM()')
  end

Inside the meta_sm2.rb model,

  scope :next_cards, -> { where('next_repetition <= :current_time', current_time: Time.now) }


texts:
This is a pretty simple working example using React-Redux to select a user from app.com/user/:userId and then fetch/display that user's info from a remote API
Most of the examples/code I can find online use class-based components but since I already have part of a project written using a functional approach I'm looking for feedback on how I'm integrating redux into functional components. I'm also not a Javascript expert by any means either so general critique of my JS is welcome as well!
Store.js:
import { createStore } from "redux";

const initialUser = { user: [] };

const userReducer = (state = initialUser, action) => {
  switch (action.type) {
    case "ADD_USER":
      return Object.assign({}, state, { user: action.user });
    default:
      return state;
  }
};

export default function Store() {
  return createStore(userReducer);
}

Actions.js:
const validResp = (resp) => {
  return resp.data.code === 200;
};

const extractUser = (resp) => {
  return resp.data.data;
};

export const addUser = (resp) => {
  let user = "";

  if (validResp(resp)) {
    user = extractUser(resp);
  }

  return {
    type: "ADD_USER",
    user: user
  };
};

index.js:
import React from "react";
import ReactDOM from "react-dom";
import { Provider } from "react-redux";
import { BrowserRouter as Router, Route } from "react-router-dom";

import AppContainer from "./AppContainer";
import Store from "./Store";

const rootElement = document.getElementById("root");
ReactDOM.render(
  <Provider store={Store()}>
    <Router>
      <Route path="/user/:userId" component={AppContainer} />
    </Router>
  </Provider>,
  rootElement
);

AppContainer.js:
import React from "react";
import { useEffect } from "react";
import { useDispatch, useSelector } from "react-redux";
import { useParams } from "react-router-dom";
import axios from "axios";

import { addUser } from "./Actions";

const baseUrl = "https://gorest.co.in/public-api/users/";

export default function AppContainer() {
  const url = baseUrl + useParams().userId;
  const user = useSelector((state) => state.user);
  const dispatch = useDispatch();

  useEffect(() => {
    axios.get(url).then((resp) => dispatch(addUser(resp)));
  });

  return <App user={user} />;
}

function App(props) {
  return (

      Name: {props.user.name}

      id: {props.user.id}

  );
}


texts: Populating Day/Month Selections in UserForm Controls

texts:
So I have a simple userform where I require the User to input the target month, and the day to analyse data up to.

This is just a small section of code to govern populating the month selection and then determining days based on the month.

Particular questions:

Is this a smart way to handle dates generally?
Is this a good way to ensure that the User cannot enter a non-existent date?
Any edge cases I haven't considered?

And, as always, if you were handed this code to maintain, what would you be thinking as you read through it?

Private Sub UserForm_Initialize()

    PopulateMonthBox Me.UF_BankRec_cbx_Month

End Sub

Private Sub PopulateMonthBox(ByRef monthBox As MSForms.ComboBox)

    Dim ix As Long
    Dim monthText As String

        For ix = 1 To 12
            monthText = MonthName(ix)
            monthBox.AddItem monthText
        Next ix

End Sub

Private Sub UF_BankRec_cbx_Month_Change()

    Dim dayBox As MSForms.ComboBox
    Set dayBox = Me.UF_BankRec_cbx_EndDay

    Dim monthBox As MSForms.ComboBox
    Set monthBox = Me.UF_BankRec_cbx_Month

    Dim monthText As String
        monthText = monthBox.Text

        dayBox.Clear
        PopulateDayBox dayBox, monthText

End Sub

Private Sub PopulateDayBox(ByRef dayBox As MSForms.ComboBox, ByVal monthText As String)

    Dim ix As Long, dayLimit As Long
    Dim ixMonth As Long, ixNextMonth As Long
    Dim ixYear As Long
    Dim firstDayNextMonth As Date, lastDayThisMonth As Date

        ixMonth = Month("01/" & monthText & "/2000")
        ixNextMonth = ixMonth + 1
        ixYear = Year(Now)

        firstDayNextMonth = DateSerial(ixYear, ixNextMonth, 1)
        lastDayThisMonth = DateAdd("d", -1, firstDayNextMonth)

        dayLimit = Day(lastDayThisMonth)

        For ix = 1 To dayLimit
            dayBox.AddItem ix
        Next ix

End Sub


texts:
In Tavern on the Meta, our spam detector is a bot named SmokeDetector.

While you can ignore the bot, you can't ignore specific replies to Smokey without ignoring users, so I wrote a UserScript to hide SmokeDetector messages and replies/calls to it by other users.

Click Here to install. It's also available on GitHub and StackApps.

I used the startsWith and includes polyfills, because I was too lazy to use indexOf.

I tried to get as much duplicate logic out of it as possible, hence the innerText function, but it just feels icky at points. I'm not particularly sure how to improve it beyond extracting logic out of the main function a bit more.

// ==UserScript==
// @name SmokeTheSmokeDetector
// @description No more smokey spam
// @version 0.0.1
// @match *://chat.stackexchange.com/rooms/*
// @match *://chat.stackoverflow.com/rooms/*
// @match *://chat.meta.stackexchange.com/rooms/*
// @author The-Quill
// @downloadURL  https://github.com/The-Quill/SmokeTheSmokeDetector/raw/master/SmokeTheSmokeDetector.user.js
// @updateURL https://github.com/The-Quill/SmokeTheSmokeDetector/raw/master/SmokeTheSmokeDetector.user.js
// @run-at document-end
// ==/UserScript==

if (!String.prototype.startsWith) {
    String.prototype.startsWith = function(searchString, position) {
        position = position || 0;
        return this.substr(position || 0, searchString.length) === searchString;
    };
}
if (!String.prototype.includes) {
    String.prototype.includes = function(search, start) {
        'use strict';
        if (typeof start !== 'number') {
            start = 0;
        }
        if (start + search.length > this.length) {
            return false;
        }
        return this.indexOf(search, start) !== -1;
    };
}
var SmokeyIdsBasedOnSite = {
    "chat.meta.stackexchange.com": 266345,
    "chat.stackexchange.com": 120914,
    "chat.stackoverflow.com": 3735529
};
var SmokeyUserId = SmokeyIdsBasedOnSite[document.location.host];
function innerText(element, selector){
    if (!selector) return "";
    var foundElement = element.querySelector(selector);
    if (!foundElement || !foundElement.innerText) return "";
    return foundElement.innerText;
}
function hideSmokeyMessage(message){
    var messageBlock = message.parentNode.parentNode;
    if (messageBlock.classList.contains('user-' + SmokeyUserId)){
        var text = innerText(message, '.content');
        if (!text.includes('fault.') && !text.includes('privileged user')){
            var parentNode = message.parentNode;
            parentNode.removeChild(message);
            if (parentNode.children.length === 0){
                messageBlock.style.display = "none";
            }
        }
        return;
    }
    var messageText = innerText(message, '.content');
    var isSmokeyResponse = innerText(message, '.mention') === '@SmokeDetector';
    if (messageText.startsWith('sd') || (messageText.startsWith('!!/') && messageText !== '!!/blame' && messageText !== '!!/amiprivileged') || isSmokeyResponse){
        var parentNode = message.parentNode;
        parentNode.removeChild(message);
        if (parentNode.children.length === 0){
            messageBlock.style.display = "none";
        }
        return;
    }
}
function nodeInsertionListener(){
    var messages = document.querySelectorAll(".monologue .message");
    Array.prototype.slice.apply(messages).forEach(hideSmokeyMessage);
}
window.addEventListener("DOMNodeInserted", nodeInsertionListener);


texts: Reordering an array: Even elements descending, odd elements ascending

texts:
I am trying to work out a solution. I have to order the elements in the array a,  such that a[0] is greater than a[1],  a[1] is less than a[2], a[2] is greater than a[3], and so on...

Basically my method checks if the index is odd or even , and simultaneously makes a decision to swap elements to suit the criteria of the question.

I am getting the following output: 7, 1, 6, 2, 5, 3, 4

public class ArrayRearrange {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] a = { 2, 1, 4, 6, 5, 3, 7 };

        for (int i = 0; i < a.length; i++) {
            for (int j = i + 1; j < a.length; j++) {
                if (i % 2 == 0 && a[i] < a[j]) {
                    int temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
                if (i % 2 != 0 && a[i] > a[j]) {
                    int temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }
        for(int i=0;i<a.length;i++)
        System.out.print(a[i]+" ");

    }

}

Let me know if there's a better way to do it.


texts:
More readable way to do this?
renderHtmlTable(function(tableItems) {

  var tableArray,_i,item,_len;
  tableArray = ['

' +
                        'Header 1	' +
                        'Header 2	' +
                        'Header 3	' +
                        'Header 4	' +
                        '

'];

  for (_i = 0, _len = tableItems.length; _i < _len; _i++) {
    item = tableItems[_i];

    tableArray.push('
' + item.foo + '	' +
                      '' + item.bar + '	' +
                      '' + item.baz + '	' +
                      '' + item.qux + '
')
  }

  tableArray.push('
');

 ($('#TableDiv')).html(function() {
   return lessonArray.join("");
 });


texts: Working out how many hours worked after a certain time

texts:
I am working on some custom rules in a time managment system and need to know how many hours in a shift have been before a certain time (19:00pm) and how many hours after. Shifts can start in the evening and finish in the morning so need to take that into account.

So far I have the below (this is just a snippet for one day) however it seems very clumsy and eloborate what I have written, can't help but feel I am missing a simpler solution, anyone have any ideas?

    DateTime cutOffTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 19, 00, 00);
    string cutOffTimeOfDay = GetTimeOfDayFromDateTime(cutOffTime);

    double baseMondayHours = 10.00;
    baseMondayHours = (baseMondayHours - 0.5);

    if (GetTimeOfDayOnlyFromDateTime(monday.ShiftStart.Value) == "AM" && GetTimeOfDayOnlyFromDateTime(monday.ShiftEnd.Value) == "PM"
        && monday.ShiftEnd.Value.TimeOfDay < cutOffTime.TimeOfDay)
    {
        postCutOffMondayHours = 0;
        baseMondayHours = monday.HoursWorked.Value;
    }
    else
    {
        string endTimeOfDay = GetTimeOfDayFromDateTime(monday.ShiftEnd.Value);
        double hoursAfterCutOff = GetDuration(cutOffTimeOfDay, endTimeOfDay);

        postCutOffMondayHours = hoursAfterCutOff;
        baseMondayHours = (baseMondayHours - hoursAfterCutOff);
    }

public static string GetTimeOfDayFromDateTime(DateTime d)
{
    return d.ToString("HH:mm") + " " + d.ToString("tt", CultureInfo.InvariantCulture);
}

public static string GetTimeOfDayOnlyFromDateTime(DateTime d)
{
    return d.ToString("tt", CultureInfo.InvariantCulture).ToUpper();
}

    public static double GetDuration(string startTime, string endTime)
    {
        DateTime start = DateTime.Parse(startTime);
        DateTime end = DateTime.Parse(endTime);
        if (start > end)
            end = end.AddDays(1);

        TimeSpan duration = end.Subtract(start);

        return duration.TotalHours;
    }


texts:
It appears to me that this has been before in all languages except dart...

Referenced in order btw, any improvements, any tips, any feed back would be appreciated...

// The Goal: raise (x + 1) to a power and get a list of strings output

final int power = 6; //determines which power
final String firstVar = "y"; //
final int secondNumber = 1;

void main() {
  //reference first

  List<List<double>> pyrmide = [
    [1], // anything to the power of zero is one
    [1,1]]; // needs [1,1] to work properly

  //reference second

  for(int i = 1; i < power // this builds the pyrmide
      ; i += 1) {
    List<double> nextLayer = [1,1]; // outside numbers

    for(int j = 0; j < (pyrmide.length - 1); // finds inside numbers
        j += 1) {
       nextLayer
         .insert(j + 1,
          pyrmide[i][j] + pyrmide[i][j + 1] // finds number from the above layers
          );
    }

    pyrmide.add(nextLayer); // adds to the pyrmide next layer to be found
  }

  List<String> output = []; // will output something similar to ["x^2", "2x", "1"]

  //reference third

  for(int k = 0; // k finds the power of secondvar
      k < pyrmide.length; k += 1) {
    int l = power - k; // finds the power of firstvar

    if (l != 0 && k != 0) // don't want x^0 or 1x^power
    {
    output.add("${(pyrmide[power][k]*secondNumber).toString()}$firstVar^${l.toString()}");
    } else if (l == 0) {
      output.add("${(pyrmide[power][k] * secondNumber).toString()}");
    } else if (k == 0) {
      output.add("$firstVar^$l");
    }
  }

  print(output);
}

  /* it turns that pyrmides predicts powers of (x+1)
   power of zero: 1
   power of one: 1,1 (x + 1)
   power of two: 1, 2, 1 (x^2 + 2x + 1)
  */

  /* write the pyrmide down, the outside numbers are always one
   you only have to find 1 inside number for the power of 2, 2 inside numbers for power of 3, so on
  */

  /* every term in any power of (x + 1) will always have its sum of the powers inside of it equal the power you raised (x + 1) to get to it*/
```


texts: Reverse the words, move the vowels and downcase everything

texts:
I'm brand new to Ruby.
The function takes in a string of any number of words, and reverses the order of the words. Also, for each word, it takes the vowels and moves it to the end of the word. It also downcases everything. Thus, Hello World! would become wrld!o hlleo.
I am trying to use some Ruby features, hence why it is a one-liner so to speak. Basically I am just looking for style suggestions. Is it appropriate to do such a thing in this manner (one line?). I'm sure there are functions that could accomplish the task quicker, so I am open to those suggestions too, since my code is very long and convoluted. Also I should mention I wanted to write this with only base Ruby, no extra packages/gems.
Someone suggested Rubocop and the Style Guide so I will check those out.
  def funky_words(s)
    s.strip.gsub(/\s+/, " ").split(" ").reverse.instance_eval{map{|elt| elt.gsub(/([aeiou])/i,"")}}.
    zip(s.strip.split(" ").reverse.map{|elt| elt.scan(/([aeiou])/i).flatten}.instance_eval{map{|elt| elt.join}}).
    map(&:join).join(" ").downcase
    #first "line" reverses word order removes vowels, second "line" captures vowels, last "line" joins vowels and all words
  end


texts:
I'm starting out and need feedback in order to discover the things I'm doing wrong.

This is supposed to behave like strcmp():

int compare(char *str1, char *str2)
{
    while(*str1 || *str2)
    {
        if(*str1 != *str2)
        {
           break;
        }
        ++str1;
        ++str2;
    }
    return *str1 - *str2;
}

How can it be improved? Are there any flaws?

Also, would any of the following styles be considered better?

int compare(char *str1, char *str2)
{
    while((*str1 || *str2) && *str1 == *str2)
    {
        ++str1;
        ++str2;
    }
    return *str1 - *str2;
}

int compare(char *str1, char *str2)
{
    while((*str1 || *str2) && *str1 == *str2) ++str1, ++str2;
    return *str1 - *str2;
}


texts: Creating instances of all subclasses in Python

texts:
I have a superclass with several subclasses. Each subclass overrides the "solve" method with some a different algorithm for estimating the solution to a particular problem. I'd like to efficiently create an instance of each subclass, store those instances in a list, and then take the sum (or average) of the estimates that the subclasses give:

class Technique():

    def __init__(self):
        self.weight = 1

    def solve(self, problem):
        pass

class Technique1(Technique):

    def __init__(self):
        super()

    def solve(self,problem):
        return 1

class Technique2(Technique):

    def __init__(self):
        super()

    def solve(self,problem):
        return 6

 class Technique3(Technique):

    def __init__(self):
        super()

    def solve(self,problem):
        return -13

testCase = 3
myTechniques = []
t1 = Technique1()
myTechniques.append(t1)
t2 = Technique1()
myTechniques.append(t2)
t3 = Technique1()
myTechniques.append(t3)

print(sum([t.solve(testCase) for t in myTechniques])

The code at the end feels very clunky, particularly as the number of subclasses increases. Is there a way to more efficiently create all of the instances?


texts: Finding the array indices of three specific headers

texts:
I'm considering how to refactor code to make it clean & clear.

There are some principles to make code clean; in this case I think it's Single Responsibility and One level of abstraction.

The original code is like this:

for (int i = 0; i < header.length; i++) {
  if (header[i].equals(Constant.ID)) {
    indexOfID = i;
  } else if (header[i].equals(Constant.NAME)) {
    indexOfName = i;
  } else if (header[i].equals(Constant.CATEGORY)) {
    indexOfCategory = i;
  }
}

Then I separated the loop into smaller functions.

private int getIndexOfColumnType(String[] header, String type) {
  int index = -1;
  int i = 0;
  int size = header.length;

  while (index < size && index < 0) {
    if(header[i].equals(type)) {
      index = i;
    }
    i++;
  }
  return index;
}

And call it

int indexOfID = getIndexOfColumnType(header, Constant.ID);
int indexOfName = getIndexOfColumnType(header, Constant.NAME);
int indexOfCategory = getIndexOfColumnType(header, Constant.CATEGORY);

In this way, I need to use 3 loops; so I wonder which is better for performance
because header array can be large.

Please tell me your idea.


texts: Algorithms for traversing unordered tree

texts:
I have been doing some programming 'exercises'.  I'm trying to come up with the most efficient algorithm for tree traversal.

Consider the following function signature:

CNode * CNode::find(int number) const

where CNode structure is not organized as a binary search tree (otherwise obviously a faster algorithm would have been used - not involving the entire tree traversal), so all the nodes have to be inspected.

I have implemented 3 algorithms so far:

Stack-based search

CNode * CNode::findStackBased(int number)
{
  Node *tmp = this;
  stack<CNode*> onStack;
  while (tmp != NULL || !onStack.empty())
  {
    if (tmp == NULL)
    {
      tmp = onStack.top();
      onStack.pop();
    }

    if (tmp->number == number)
    {
      return tmp;
    }

    if (tmp->right)
    {
      onStack.push(tmp->right);
    }

    tmp = tmp->left;
  }

  return NULL;
}

Recursive search

CNode * CNode::findRecursion(int number)
{
  if (this->number == number)
  {
    return this;
  }

  Node * result = NULL;
  if (this->left != NULL && (result = left->findRecursion(data)))
  {
    return result;
  }

  if (this->right != NULL)
  {
    return this->right->findRecursion(data);
  }

  return NULL;
}

Walking along the edge

CNode * CNode::findWalkAlongTheEdge(int number)
{
  Node *tmp = this;
  while (tmp != NULL)
  {
    if (tmp->number == number)
    {
      return tmp;
    }

    if (tmp->left != NULL)
    {
      tmp = tmp->left;
      continue;
    }
    else if (tmp->right != NULL)
    {
        tmp = tmp->right;
        continue;
    }
    else
    {
      while (1)
      {
        if (tmp->parent->left == tmp)
        {
          if (tmp->parent->right != NULL)
          {
            tmp = tmp->parent->right;
            break;
          }
        }
        else
        {
          if (tmp->parent == this)
          {
            return NULL;
          }

          tmp = tmp->parent;
        }
      }
    }
  }
  return NULL;
}

Each of the above have have some advantages/drawbacks, but performance wise 'walking along the edge is the winner so far(out of the 3 given above).

My question is:

Do you see any way to optimize it further (recursive implementation already benefits from the tail recursion optimization, at least gcc seems to optimize it this way)?


texts:
I've a feeling I'm overdoing my Auth class, and that it could be done in a simpler and more understandable way.

Could you give me advice on this, please?

This is for my learning process and I just want to learn. This is why I am 'reinventing the wheel'.

class Auth
{

    protected $pdo;
    protected $session = null;
    protected $error = array();

    /**
    *
    */
    function __construct($PDO)
    {
        $this->pdo = $PDO;
    }

    /**
    *
    */
    public function authenticate_user($password,$email)
    {
        try
        {
            $select_user = $this->pdo->prepare('SELECT * FROM users WHERE user_email = :email LIMIT 1');
            $select_user->execute(array(':email' => $email));
            $user_data = $select_user->fetch(PDO::FETCH_ASSOC);
            if (password_verify($password,$user_data['user_pass']))
            {
                $this->set_session($user_data);
                return true;
            }
            else
            {
                $this->error['authenticate_user'] = 'Incorrect email or password';
            }
        }
        catch (PDOException $e)
        {
            echo $e->getMessage();
        }
    }

    /**
    *
    */
    public function set_session($user_data)
    {
        if ($user_data)
        {
            $this->session = $user_data;
        }
    }

    /**
    *
    */
    public function unset_session()
    {
        if (isset($this->session))
        {
            $this->session =  null;
        }
    }

    /**
    *
    */
    public function get_session()
    {
        if (isset($this->session))
        {
            return $this->session;
        }
        else
        {
            return false;
        }
    }

    /**
    *
    */
    public function is_logged_in()
    {
        if (isset($this->session))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    /**
    *
    */
    public function get_error($key)
    {
        if (isset($this->error[$key]))
        {
            return $this->error[$key];
        }
        else
        {
            return false;
        }
    }

}

And on the login.php:

  if (!empty($_POST))
  {
      $response = $Auth->authenticate_user($_POST['password'],$_POST['email']);
  }

  session_start();

  if (isset($response))
  {
      $_SESSION = $Auth->get_session();
  }

  print_r($_SESSION);

  echo $Auth->get_error('authenticate_user');

And in the file that loads all the other files (at the top of everything):

if (isset($_SESSION)
{
  $Auth->set_session($_SESSION);
}


texts: Bash script for an ABC organization fulfilling the following requirements

texts:
Write a bash script for an ABC organization fulfilling the following requirements:

Takes an Employee's Name, designation, age, years of service and salary, extra working hours as an input.
Calculates remaining years of service. An Employee retires at the age of 60.
Check if the Employee is valid for salary appraisal. If an employee has been working for more than an year he deserves to be given an appraisal of 10%.
If an employee has given extra working hours within a month to the organization calculate bonus amount.
Per hour amount varies depending upon the designation. See the given table to calculate bonus according to designations.
Designation Per hour-amount
Higher-Management   1K
Lower-management    800PKR
Other staff 500 PKR
Print the Employee’s information along with bonus and appraisal amount

read -p "Enter employee name: " ename # printing message and scanning variable

read -p "Enter designation: " designation # printing message and scanning variable

read -p "Enter age: " age # printing message and scanning variable

read -p "Enter years of service: " years # printing message and scanning variable

read -p "Enter salary: " salary # printing message and scanning variable

read -p "Enter extra working hours: " hours # printing message and scanning variable

years_remaining=$(( 60-age )) # calculating remaining years

#code to calculate appraisal

if [ "$years" -gt 1 ] # if condition

then

appraisal=$(( salary/10 )) # appraisal value

else

appraisal=0 # appraisal value

fi # end of if statement

#code to calculate the bonus

if [ "$designation" = "Higher-Management" ] # if condition

then

bonus=$(( 1000*hours )) # calculating bonus

elif [ "$designation" = "Lower-management" ] # else if condition

then

bonus=$(( 800*hours )) # calculating bonus

elif [ "$designation" = "Other staff" ] # else if condition

then

bonus=$(( 500*hours )) # calculating bonus

else

bonus=0

fi # end of if statement

echo "Employee name: $ename" # printing message

echo "Age: $age" # printing message

echo "Designation: $designation" # printing message

echo "Years of service: $years" # printing message

echo "Salary: $salary" # printing message

echo "Extra working hours: $hours" # printing message

echo "Bonus: $bonus" # printing message

echo "Appraisal: $appraisal" # printing message

echo "years_remaining:$years_remaining " # printing message


texts: Scala app to transpose columns into rows

texts:
This is the first application or really any Scala I have every written. So far it functions as I would hope it would. I just found this community and would love some peer review of possible improvements or just tell me flat out what I'm doing wrong.

This code is supposed to read a table of tables then take all of the values from that table transpose the columns into rows and write them all to a master table with three additional columns. So far it does everything except the looping which I just haven't gotten to yet. This question is not about how to do the looping and really only hoping to critique what I have here so far. I did change some variable names and a few things I had to mask.

//how to call
//spark-submit --verbose --deploy-mode cluster --master yarn --class App scala-maven-0.1-SNAPSHOT.jar
import org.apache.spark.sql.{ SQLContext, SparkSession }
import org.apache.spark.sql._
import org.apache.spark.sql.types._
import org.apache.spark.sql.functions._
import java.util.Calendar

//this will call createStuff function for each table_name value in table
//TODO: Add loop and call CreateStuff with values from list returned from stuff_to_run
object App {
  def main(args: Array[String]) {
    //do I have to create the SparkSession value twice or can I pass this into the function?
    val spark = SparkSession.builder().appName("App").enableHiveSupport().getOrCreate()
    val stuff_to_run = spark.sql("select table_name from schema.table").rdd.map(r => r(0)).collect.toList
    println(stuff_to_run)
    CreateStuff("stuff");
  }

  def CreateStuff(stuffName: String): Unit = {
    var tablename = stuffName
    var column_stack = ""
    val spark = SparkSession.builder().appName("App").enableHiveSupport().getOrCreate()
    //is there a better way to get year and month
    val Year = Calendar.getInstance.get(Calendar.YEAR)
    val Month = Calendar.getInstance.get(Calendar.MONTH) + 1
    val twodigitmonth = "%02d".format(Month)
    val yrmnth = Year.toString + twodigitmonth.toString
    //get all of the data from the base table
    val raw_table = spark.sql("select *, '" + tablename + "' as stuffname, '" + yrmnth + "' as yrmnth from schema." + tablename)
    //transpose all columns into rows
    val string_table = raw_table.select(raw_table.columns.map(c => col(c).cast(StringType)): _*)
    //get column besides 3 defined these are the only ones which are not dynamic
    val selectColumns2 = string_table.columns.toSeq.filter(x => (x != "val1" && x != "stuffname" && x != "yrmnth"))
    val columnCount = selectColumns2.size
    //comma and quote
    selectColumns2.foreach { e =>
      column_stack += "'" + e + "', " + e + ", "
    }

    val collist = column_stack.mkString("").dropRight(1)
    import spark.sqlContext.implicits._

    val unPivotDF = string_table.select($"val1", $"stuffname", $"yrmnth",
      expr("stack(" + selectColumns2.size + ", " + collist.dropRight(1) + ") as (fieldname, fieldvalue)"))
    //unPivotDF.show()
    //insert records into combined table
    unPivotDF.createOrReplaceTempView("stuff_temp")
    spark.sql("insert into schema.combined_stuff select * from stuff_temp")
  }
}


texts:
I wrote two functions for determining leap years.
("Kabisat" means "leap year" in Indonesia.)

def kabisat? y
  return false if y%4!=0
  return true if y%100!=0
  y%400==0
end

def kabisat_2? y
  return true if y%400==0
  return false if y%100==0
  y%4==0
end

require 'date';

(1..Date.today.year).each { |y|
  puts y if kabisat?(y)!=kabisat_2?(y)
}

Is it correct? And it is possible to write a shorter one?


texts: How to view files on Linux based server with UNIX ID

texts:
I am using a Linux-based server and I have a q id (UNIX ID). I wanted to know how I could view all the directories and files I have on the server, including /tmp. I have code which creates a folder inside /tmp but I don't know where to look to see if it actually works. I am not very familiar with Linux so this might be a dumb question but I'd appreciate the help!
This is the code I have for creating a folder inside /tmp and moving a file into it:
$newDir = "/tmp/FileDrop" . microtime(true);
mkdir($newDir, 0777, true);
move_uploaded_file($tmpName, "$newDir/$fileName");


texts:
I am studying algorithms and I solved this problem under Dynamic Programming section:

  Alice is a kindergarten teacher. She wants to give some candies to the
  children in her class.  All the children sit in a line ( their
  positions are fixed), and each  of them has a rating score according
  to his or her performance in the class.  Alice wants to give at least
  1 candy to each child. If two children sit next to each other, then
  the one with the higher rating must get more candies. Alice wants to
  save money, so she needs to minimize the total number of candies given
  to the children.

  Input Format

  The first line of the input is an integer N, the number of children in
  Alice's class. Each of the following N lines contains an integer that
  indicates the rating of each child.

Code:

import java.io.*;
import java.util.*;

public class Candie{

 static int candieCalculator(int[] numbers) {
    int len = numbers.length;
    int result=0;

    if (len==1) {
        return 1;
    }

    for (int i=0; i<len; i++) {
        result++;
        if (i==0) {
            if (numbers[i] > numbers[i+1]) {
                result++;
            }
        }
        else if (i==len-1) {
            if (numbers[i] > numbers[i-1]) {
                result++;
            }
        }
        else {

            if (numbers[i] > numbers[i+1]) {
                result++;
            }

            if (numbers[i] > numbers[i-1]) {
                result++;
            }
        }
    }

    return result;
 }

 public static void main(String[] args) {

    Scanner in = new Scanner(System.in);
    int n = in.nextInt();
    int[] numbers = new int[n];

    for(int i=0; i < n; i++) {
        numbers[i] = in.nextInt();
    }
    System.out.println(candieCalculator(numbers));

 }
}

Sample input:

  3  1  2  2

Sample output:

4

What do you think? Also this solution is considered as DP? It doesn't look DP to me.


texts: Custom log4net raising application-level events

texts:
Use Case: I have a form application in which I log events to a file/database and have a need to also raise those events to the application itself (for display purposes).  Think old school updating status bar with the same content that you will log to a file.

The solution I put in place is a custom log4net class (since we're already using that for the actual file and databases logging).  This simple class just raises an event when the message payload of the caller.  This way I can use a single call to the log instead of multiple calls to different loggers.

This is a working class, I'm just looking for input on pitfalls on the code or potential performance issues that might be found (and to also share the idea for others that need it).  I'm also wondering missing some best practices or something simple that I've overlooked.

I the case of my requirements, this event may be called 30 times/sec across multiple different threads.

MemoryQueueEventArgs isn't the actual final class name, it was another direction I was going to go, I just haven't renamed it yet.

public class MemoryQueueEventArgs : EventArgs
{
  // Customize to your business requirements
  public DateTime CreateDate { get; set; }
  public string Message { get; set; }
  public string Level { get; set; }
  public string LoggerName { get; set; }
  internal int Count { get; set; }
}

public class MemoryQueueAppender : AppenderSkeleton
{
  private object objectLock = new Object();

  #region Public Methods
  public static MemoryQueueAppender GetActiveAppender(Type type)
  {
    IAppender[] appenders = log4net.LogManager.GetLogger(type).Logger.Repository.GetAppenders();
    return (appenders
     .FirstOrDefault(a => a.GetType().Name == "MemoryQueueAppender") as MemoryQueueAppender)
    ;
  }

  protected event EventHandler<MemoryQueueEventArgs> _LogUpdate;
  public event EventHandler<MemoryQueueEventArgs> LogUpdate
  {
    add
    {
      lock (objectLock)
      {
        _LogUpdate += value;
      }
    }
    remove
    {
      lock (objectLock)
      {
        _LogUpdate -= value;
      }
    }

  }
  #endregion Public Methods

  protected override void Append(log4net.Core.LoggingEvent loggingEvent)
  {
    MemoryQueueEventArgs mqea = _CreateMemoryQueueEventArgs(loggingEvent);
    this.OnLogUpdate(mqea);
  }

  protected override void Append(log4net.Core.LoggingEvent[] loggingEvents)
  {
    base.Append(loggingEvents);
  }

  #region Private Methods

  private MemoryQueueEventArgs _CreateMemoryQueueEventArgs(log4net.Core.LoggingEvent loggingEvent)
  {
    return new MemoryQueueEventArgs
    {
      CreateDate = DateTime.Now,
      Level = loggingEvent.Level.Name,
      LoggerName = loggingEvent.LoggerName,
      Message = RenderLoggingEvent(loggingEvent),
    };
  }

  protected virtual void OnLogUpdate(MemoryQueueEventArgs e)
  {
    EventHandler<MemoryQueueEventArgs> handler = _LogUpdate;
    if (handler != null)
    {
      handler(this, e);
    }
  }

  #endregion Private Methods

}

Calling Code:

public TestDevelopment()
{
  MemoryQueueAppender appender = MemoryQueueAppender.GetActiveAppender(this.GetType());
  appender.LogUpdate += appender_LogUpdateEvent;
}

void appender_LogUpdateEvent(object sender, MemoryQueueEventArgs e)
{
  System.Diagnostics.Debug.WriteLine(string.Format("[{0}] {1}", e.Level, e.Message));
}


texts: Developing an alert notification in AngularJS

texts:
I'm studying Angular and want to develop my own notification alert. This is what I done, I like it and it's working like a charm but I think that it can be improved:

Do I need to use it in conduction with a service? If yes, how?
Will it be better if it became an E (element)?
I needed to put style="display: none;" (see the HTML code) so it does not appear when the page is loaded and I think that it's not the best way. What do I do?
How can I show and hide more classy, with some animation? CSS? Add/Remove [custom] class?

This is the directive:

myApp.directive('notification', ['$timeout', function ($timeout) {
  return {
    restrict: 'A',
    controller: ['$scope', function ($scope) {
      $scope.notification = {
        status: 'hide',
        type: 'success',
        message: 'Welcome! It\'s yet another angular alert ;)'
      };
    }],
    link: function(scope, elem, attrs) {
      // watch for changes
      attrs.$observe('notification', function (value) {
        if (value === 'show') {
          // shows alert
          $(elem).show();

          // and after 3secs
          $timeout(function () {
            // hide it
            $(elem).hide();

            // and update the show property
            scope.notification.status = 'hide';
          }, 3000);
        }
      });
    }
  };
}]);

This is the HTML:

{{notification.message}}

A simple example on how to use it:

<button id="submit" name="submit" class="btn btn-default" type="submit" ng-click="notification.status = 'show'; notification.message = 'Oh yeah!'; notification.type = 'info';">Show</button>

This is the Plunkr.


texts:
I need to sort a large array (5 columns x ~500,000 rows) of string variables by two columns. I have code that works, but it takes an unacceptably long time, particularly because I have to do this on five sheets with different data.

I have been doing research, but the code I found tend to be for either a single column, or exclusively for integers. I need to be able to change it to work with between 3-8 columns in the array as well, but I'm hopeful I can figure that out myself.

Link 1
Link 2
Link 3

Sorting on two columns:

   ActiveWorkbook.Worksheets("Quantity Available").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Quantity Available").Sort.SortFields.Add Key:=Range( _
        "C2:C" & LastRowAvail), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Quantity Available").Sort.SortFields.Add Key:=Range( _
        "E2:E" & LastRowAvail), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Quantity Available").Sort
        .SetRange Range("A1:F" & LastRowAvail)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .Apply
    End With

Sorting on one column:

   ActiveWorkbook.Worksheets("Main Tab").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Main Tab").Sort.SortFields.Add Key:=Range( _
        "U2:U" & LastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Main Tab").Sort
        .SetRange Range("A1:AU" & LastRow)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .Apply
    End With


texts:
The following code is an extract of a small library I wrote. Full code is available here.

My question is about the following function. I added comments on each state. I'm wondering if I could do things easier, I've the feeling I'm using too many flatMap:

/**
  * request
  *
  * @param {string} method
  * @param {RxRestItem|FormData|URLSearchParams|Body|Blob|undefined|Object} [body]
  * @returns {Observable<RxRestItem|RxRestCollection>}
  */
request(method: string, body?: BodyParam): Observable<RxRestItem|RxRestCollection> {
  let requestOptions = {
    method: method,
    headers: <Headers> this.requestHeaders,
    //this is just a function that transforms a Body to json if needed
    body: this.requestBodyHandler(body)
  }

  let request = new Request(this.URL + this.requestQueryParams, requestOptions);

  //let's start from our Request object
  return Observable.of(request)

  //here, we call functions that can alter the request, think middlewares (see below for this method)
  .flatMap(this.expandInterceptors(Config.requestInterceptors))

  //now we have the final request, we do a `fetch` on it
  .flatMap(request => fetch(request))

  //Transform the response to a javascript object
  .flatMap(body => Observable.fromPromise(this.responseBodyHandler(body)))

  //Here the library transforms object to internal types
  .map(e => {
    if (!Array.isArray(e)) {
      let item: RxRestItem

      if (this instanceof RxRestItem) {
        item = this
        item.element = e
      } else {
        item = new RxRestItem(this.$route, e)
      }

      item.$fromServer = true
      return item
    }

    return new RxRestCollection(this.$route, e.map(e => {
      let item = new RxRestItem(this.$route, e)
      item.$fromServer = true
      return item
    }))
  })

  //response middlewares
  .flatMap(this.expandInterceptors(Config.responseInterceptors))

  .catch(body => {
    //this was a real error
    if (!(body instanceof Response)) {
      return Observable.throw(body)
    }

    //error interceptors
    return Observable.of(body)
    .flatMap(this.expandInterceptors(Config.errorInterceptors))
  })
}

The middleware function feels complicated too, it takes an array of functions and apply them (like a reduce). However, this interceptor function can return a Promise, an Observable or a changed value:

/**
  * expandInterceptors
  *
  * @param {RequestInterceptor[]|ResponseInterceptor[]|ErrorInterceptor[]} interceptors
  * @returns {Observable<any>} fn
  */
expandInterceptors(interceptors: RequestInterceptor[]|ResponseInterceptor[]|ErrorInterceptor[]) {
  return function(origin: any): Observable<any> {
    return (<any>interceptors).reduce(
      (obs: Observable<any>, interceptor: any) =>
        obs.concatMap(value => {
          let result = interceptor(value)
          if (result === undefined) {
            return Observable.of(value)
          }

          if (result instanceof Observable || result instanceof Promise) {
            return result
          }

          return Observable.of(result)
        }),
      Observable.of(origin)
    )
  }
}

Do you think that I'm doing things the right way? Can this code be simplified?

I'm new to reactive programming and still trying to understand some of it's core concepts!


texts:
I'm porting a 2-D Terrain Generator from Python to C++ as Python is too slow due to the number of features that have been added.

I'm new to C++ and would like to know of any criticisms that you can make of the code at this time before I continue with the simpler parts of the generation.  Right now I'm interested in maintaining my use of arrays rather than std::vector so any improvements besides that would be greatly appreciated.

I'm also aware that this code is currently uncommented, I intend to fix that in the future however right now I'm more interested in the code itself.

#include "stdafx.h"
#include <iostream>
#include <conio.h>

double** white_noise(int w, int h);
double** smooth_noise(double** noise, int w, int h, int octave);
double** perlin_noise(double** noise, int w, int h, int octaves, double persistance);
double interpolate(double a, double b, double alpha);

int main()
{
    const int w = 400;
    const int h = 400;

    double** noise = white_noise(w, h);
    double** perlinnoise = perlin_noise(noise, w, h, 8, 0.7);

    // Code that does things will go here(function calls to add sea-level etc.)

    for (int i = 0; i < w; i++) {
        delete[] noise[i];
        delete[] perlinnoise[i];
    }
    delete[] noise;
    delete[] perlinnoise;
}

double** white_noise(int w, int h) {
    double** noise = new double*[w];
    for (int i = 0; i < w; i++) {
        noise[i] = new double[h];
    }

    for (int i = 0; i < w; i++) {
        for (int j = 0; j < h; j++) {
            noise[i][j] = (float)rand() / RAND_MAX;
        }
    }

    return noise;
}

double** smooth_noise(double** noise, int w, int h, int octave) {
    double** smooth_noise = new double*[w];
    for (int i = 0; i < w; i++) {
        smooth_noise[i] = new double[h];
    }

    int period = 1 << octave;
    double frequency = 1.0 / period;

    for (int i = 0; i < w; i++) {
        int sample_i0 = i / period * period;
        int sample_i1 = (sample_i0 + period) % w;
        double horizontal_blend = (i - sample_i0) * frequency;

        for (int j = 0; j < h; j++) {
            int sample_j0 = j / period * period;
            int sample_j1 = (sample_j0 + period) % h;
            double vertical_blend = (j - sample_j0) * frequency;

            double top = interpolate(noise[sample_i0][sample_j0],
                noise[sample_i1][sample_j0],
                horizontal_blend);
            double bottom = interpolate(noise[sample_i0][sample_j1],
                noise[sample_i1][sample_j1],
                horizontal_blend);
            smooth_noise[i][j] = interpolate(top, bottom, vertical_blend);
        }
    }

    return smooth_noise;
}

double** perlin_noise(double** noise, int w, int h, int octaves, double persistance) {
    double** perlin_noise = new double*[w];
    for (int i = 0; i < w; i++) {
        perlin_noise[i] = new double[h];
    }
    for (int i = 0; i < w; i++) {
        for (int j = 0; j < h; j++) {
            perlin_noise[i][j] = 0;
        }
    }

    double*** smooth_noises = new double**[octaves];
    for (int i = 0; i < octaves; i++) {
        smooth_noises[i] = smooth_noise(noise, w, h, i);
    }

    double amplitude = 1;
    double totalamplitude = 0;

    for (int octave = octaves - 1; octave > 0; octave--) {
        amplitude *= persistance;
        totalamplitude += amplitude;

        for (int i = 0; i < w; i++) {
            for (int j = 0; j < h; j++) {
                perlin_noise[i][j] += smooth_noises[octave][i][j] * amplitude;
            }
            delete[] smooth_noises[octave][i];
        }
        delete[] smooth_noises[octave];
    }
    delete[] smooth_noises;

    for (int i = 0; i < w; i++) {
        for (int j = 0; j < h; j++) {
            perlin_noise[i][j] /= totalamplitude;
        }
    }

    return perlin_noise;
}

double interpolate(double a, double b, double alpha) {
    return a * (1 - alpha) + alpha * b;
}


texts:
I've implemented the well known Luhn Algorithm in Python. It's a simple one, so it's good for beginners.

import random
from math import ceil

def Luhn(digits):
    if digits >= 2:
        num = random.randrange(10**(digits-2),10**(digits-1))
        num_digits = list(str(num))

        for i in range(0,digits-1):
            num_digits[i] = int(num_digits[i])

        if digits % 2 == 0:
            range_start = 0
        else:
            range_start = 1

        for i in range(range_start,ceil((digits+range_start-1)/2)):
            if digits % 2 == 0:
                num_digits[2*i] *= 2
                if num_digits[2*i] > 9:
                    num_digits[2*i] -= 9
            else:
                num_digits[2*i-1] *= 2
                if num_digits[2*i-1] > 9:
                    num_digits[2*i-1] -= 9

        checksum = sum(num_digits)
        last_digit = checksum % 10

        if last_digit != 0:
            checknum = 10 - last_digit
        else:
            checknum = 0

        num = num*10+checknum
        return num
    else:
        return None

It's a function that takes 1 parameter (digits) and returns a valid number with a given number of digits.
The code is pretty straightforward, except for this part:

if digits % 2 == 0:
    range_start = 0
else:
    range_start = 1

for i in range(range_start,ceil((digits+range_start-1)/2)):
    if digits % 2 == 0:
        num_digits[2*i] *= 2
        if num_digits[2*i] > 9:
            num_digits[2*i] -= 9
    else:
        num_digits[2*i-1] *= 2
        if num_digits[2*i-1] > 9:
            num_digits[2*i-1] -= 9

Basically what it does is the 'multiplication by 2' part of the algorithm. This part was intentional, so don't take this in consideration. I just wanted to challenge myself.

I would like to get some feedback about the code and things that can be changed.


texts:
The parameters list contain the array that is passed to and the size of the array respectively. Is there anywhere I could improve my code?

void insertionSort(int a[], int s) {
    for (int i = 0; i < s - 1; i++) {
        int j = i + 1;
        int key = a[j];
        while (i >= 0 && a[i] > key) {
            a[i+1] = a[i];
            i--;
        }
        a[i+1] = key;
    }
}


texts: Function queue for synchronous execution

texts:
The purpose of this code is to allow to add functions to a queue, that will execute them sequentially.

With this in mind, it also allows to execute them with a defined delay, to prevent them from blocking the browser.

(function(window, undefined){
    'use strict';
    var queue = [];
    var running = false;
    var stop = false;
    var log = function(data){
        //some browsers may not have the .error method
        (window.console.error || window.console.log).call(window.console, data);
    };
    var runNext = function(){
        //I have to do like this, so I don't insert it back
        var fn = queue[0];
        setTimeout(function(){
            try
            {
                //sometimes, when it is to stop, it's already here
                if(!stop)
                {
                    queue.shift();//remove it from the queue
                    fn.fn();
                }
            }
            catch(e)
            {
                //simply log it, to give a chance to other functions
                log(e);
            }

            if(queue.length && !stop)
            {
                setTimeout(runNext, 1);
            }
            else
            {
                running = false;
            }
        }, fn.delay);
    };

    window.FnQueue = {
        add: function(fn, delay){
            if(!(fn instanceof Function))
            {
                log('FnQueue.add - fn must be a function');
                return window.FnQueue;
            }
            if(isNaN(delay) || delay < 1)
            {
                log('FnQueue.add - delay must be a number equal or higher than 1');
                return window.FnQueue;
            }
            queue.push({
                fn:fn,
                delay:delay
            });
            //starts the execution only if it wasn't running and stopped
            if(!running && !stop)
            {
                running = true;
                setTimeout(runNext, 1);
            }
            return window.FnQueue;
        },
        clear: function(){
            queue = [];
            return window.FnQueue;
        },
        stop: function(){
            stop = true;
            return window.FnQueue;
        },
        resume: function(){
            stop = false;
            if(!running && queue.length)
            {
                setTimeout(runNext, 1);
            }
            return window.FnQueue;
        },
        getQueue: function(){
            return queue;
        },
        isRunning: function(){
            return running && !stop;
        }
    };
})(Function('return this')());

What can I improve on my code to make it less spaghetti-ish and more distributed? What inefficient/unnecessary code can I improve?


texts:
If I create a class that implements IComparable<T>, I must implement CompareTo<T>. It is also recommended that I implement IEquatable<T> and the non-generic IComparable. If I do all that, I am required or encouraged to:

Override GetHashCode()
Implement CompareTo(Object)
Override Equals(Object)
Implement Operator ==(T, T)
Implement Operator !=(T, T)
Implement Operator >(T, T)
Implement Operator <(T, T)
Implement Operator >=(T, T)
Implement Operator <=(T, T)

That's 9 additional methods, most of which depend on the logic that compares two instances of the class. Rather than having to implement all those methods in any class that implements IComparable<T>, I decided to create a base class that implements IComparable<T> and the other recommended interfaces (similar to the way Microsoft provides Comparer as a base class for implementations of IComparer<T>)

It doesn't make sense to compare instances of two different classes that each inherit from the base class. preventing that was the main reason for making the class generic (although it makes coding a derived class a little more complicate).

I would like to ask for a review of the code for the base class. Am I missing something? Can it be simplified? Is this a bad idea?

Here is the base class

public abstract class Comparable<T> : IComparable, IComparable<T>, IEquatable<T> where T: Comparable<T> {
    public abstract override int GetHashCode();

    public abstract int CompareTo(T other);

    public int CompareTo(object obj) {
        T other = obj as T;
        if (other == null && obj != null) {
            throw new ArgumentException($"Objects of type {typeof(T).Name} can only be compared to objects of the same type", nameof(obj));
        }
        return CompareTo(other);
    }

    public override bool Equals(object obj) {
        return CompareTo(obj) == 0;
    }

    new public bool Equals(T other) {
        return CompareTo(other) == 0;
    }

    private static int Compare(Comparable<T> comp1, Comparable<T> comp2) {
        if (comp1 == null) {
            return ((comp2 == null) ? 0 : -1);
        }
        return comp1.CompareTo(comp2);
    }

    public static bool operator == (Comparable<T> comp1, Comparable<T> comp2) {
        return Compare(comp1, comp2) == 0;
    }

    public static bool operator != (Comparable<T> comp1, Comparable<T> comp2) {
        return Compare(comp1, comp2) != 0;
    }

    public static bool operator > (Comparable<T> comp1, Comparable<T> comp2) {
        return Compare(comp1, comp2) > 0;
    }

    public static bool operator < (Comparable<T> comp1, Comparable<T> comp2) {
        return Compare(comp1, comp2) < 0;
    }

    public static bool operator >= (Comparable<T> comp1, Comparable<T> comp2) {
        return Compare(comp1, comp2) >= 0;
    }

    public static bool operator <= (Comparable<T> comp1, Comparable<T> comp2) {
        return Compare(comp1, comp2) <= 0;
    }
}

Below is a minimal implementation of the base class.

public class SeasonCompare : Comparable<SeasonCompare> {
    public int Number {get; set;}

    public override int GetHashCode() {
        return Number;
    }

    public override int CompareTo(SeasonCompare other) {
        if (other == null) {
            return 1;
        }
        return Number.CompareTo(other.Number);
    }
}


texts:
This program counts the number of the occurence of each character that is used in a file.

I tried to use everything that Java offers to me properly. If you have tips which library functions would have been better, how to make the code more readable, than let me know. If you have a tip how to improve the performance I would be very thankful, also if the readability would decrease than.

import java.util.Map;
import java.util.HashMap;
import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;

public class Main {
    public static void main(String[] args) {
        if (args[0].contains("help")) {
            System.out.println("usage: java Main filename.txt");
        }

        Map<Character, Integer> map = new HashMap<>();
        char[] chars = fileToCharList(args[0]);
        for (int i = 0; i < chars.length; i++) {
            if (map.containsKey(chars[i])) {
                Integer number = map.get(chars[i]);
                map.put(chars[i], ++number);
            } else {
                map.put(chars[i], 1);
            }
        }

        for (Map.Entry<Character, Integer> entry : map.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.
              getValue());
        }
    }

    private static char[] fileToCharList(String fileName) {
        File file = new File(fileName);
        StringBuilder sb = new StringBuilder();
        try (BufferedReader br = new BufferedReader(new FileReader(
          file))) {
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString().toCharArray();
    }
}


texts:
As an exercise to help me learn Haskell, I decided to write a small "program" to find the Pearson Correlation Coefficient for 2 lists of numbers.  I'm pretty unhappy with how it turned out, because I feel like the typing is a mess, and it is difficult to read.

I was hoping that someone with experience would help me to improve my code, and tell me how they would have approached the problem.

import Data.List

summation :: (Integral a, Fractional b) => a -> a -> (a -> b) -> b
summation i n e = if (i < n)
                  then (e i + summation (i+1) n e)
                  else (e i)

mean :: (Real a, Fractional b) => [a] -> b
mean x = (1 / (genericLength x)) *
         (summation 1 (length x) (\i -> realToFrac (x !! (i-1))))

covariance :: (Real a, Fractional b) => [a] -> [a] -> b
covariance x y = (1 / (genericLength x)) *
                 (summation 1 (length x) (\i -> ((realToFrac (x !! (i-1)) - mean x) * (realToFrac (y !! (i-1)) - mean x))))

stddev :: (Real a, Floating b) => [a] -> b
stddev x = ((1 / (genericLength x)) *
             (summation 1 (length x) (\i -> (realToFrac (x !! (i-1)) - mean x) ** 2))) ** (1/2)

pearson :: (Real a, Floating b) => [a] -> [a] -> b
pearson x y = covariance x y / (stddev x * stddev y)


texts:
I wrote wrapper for Connection. It uses my Connection pool implementation. When the wrapper closes, the connection goes back to pool.

public class ConnectionWrap implements Connection {
    private Connection delegate;
    private ConnectionPool pool;

    public ConnectionWrap(Connection delegate, ConnectionPool pool) {
        this.delegate = delegate;
        this.pool = pool;
    }

    @Override
    public Statement createStatement() throws SQLException {
        return delegate.createStatement();
    }

    @Override
    public void setAutoCommit(boolean autoCommit) throws SQLException {
        delegate.setAutoCommit(autoCommit);
    }

    @Override
    public void commit() throws SQLException {
        delegate.commit();
    }

    @Override
    public void rollback() throws SQLException {
        delegate.rollback();
    }

    @Override
    public void close() throws SQLException {
        pool.putConnection(delegate);
 //and more other methods
       }
    }

Connection have over 20 methods and iam delegate it is all. Can this be done in a shorter way?


texts:
I am writing a test web application in Golang.

Create post handler:

type NewPostReq struct {
    Title       string `validate:"required"`
    Description string `validate:"required"`
    Body        string `validate:"required"`
    Tags        []struct {
        Tag string `validate:"required"`
    } `validate:"min=2,dive"`
}

// PostResource the Post's json response
type PostResource struct {
    ID          uint   `json:"id"`
    Title       string `json:"title"`
    Description string `json:"description"`
    Body        string `json:"body"`
    Tags        []struct {
        ID  uint   `json:"id"`
        Tag string `json:"tag"`
    } `json:"tags"`
}

func CreatePost(c *Context) (err error) {
    req := new(NewPostReq)

    err = BindAndValidate(c.Request, req)
    if err != nil {
        return
    }

    post := new(Post)
    err = CopyTo(req, post)
    if err != nil {
        return
    }
    DB.Create(post)

    pr := new(PostResource)
    err = CopyTo(post, pr)
    if err != nil {
        return
    }

    err = c.JSON(201, pr)
    return
}

I am not sure if this is a good approach. I think I am repeating myself during error handling.

There is dependencies code:

type Handler func(*Context) error

func (mh Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    c := &Context{Response: w, Request: r}
    err := mh(c)

    if err != nil {
        switch e := err.(type) {
        case *HTTPError:
            c.JSON(e.HTTPStatusCode, e)
        default:
            http.Error(c.Response, e.Error(), http.StatusInternalServerError)
        }
    }
}

func Bind(r *http.Request, v interface{}) (err error) {
    err = json.NewDecoder(r.Body).Decode(v)
    return
}

func ValidateStruct(v interface{}) (err error) {
    errs := validate.Struct(v)
    if errs != nil {
        err = errs
        return
    }
    return nil
}

func BindAndValidate(r *http.Request, v interface{}) (err error) {
    err = Bind(r, v)
    if err != nil {
        return
    }
    err = ValidateStruct(v)
    if err != nil {
        err = NewFormValidationError(err.(validator.ValidationErrors))
    }
    return

}

// CopyTo copy data from struct to another struct
func CopyTo(from interface{}, to interface{}) (err error) {
    b, err := json.Marshal(from)
    if err != nil {
        return
    }
    err = json.Unmarshal(b, to)
    return
}


texts: Subset sum whose set contains only positive integers

texts:
I was trying to write a dynamic programming algorithm using a bottom up approach that solves the subset sum problem's version where the solution can be either an empty set and the initial set can only contain positive integers.

The following is my implementation, but I am not sure it is correct for all cases.

def _get_subset_sum_matrix(subset, s):
    m = [[0 for _ in range(s + 1)] for _ in range(len(subset) + 1)]

    for i in range(1, s + 1):
        m[0][i] = 0

    for j in range(0, len(subset) + 1):
        m[j][0] = 1

    return m

def subset_sum(subset, s):
    m = _get_subset_sum_matrix(subset, s)

    for i in range(1, len(subset) + 1):

        for j in range(1, s + 1):

            if subset[i - 1] == j:
                m[i][j] = 1
            else:
                # We can include the current element,
                # because it is less than the current number j.
                if subset[i - 1] <= j:
                    m[i][j] = max(m[i - 1][j], m[i - 1][j - subset[i - 1]])
                else:
                    m[i][j] = m[i - 1][j]

    return m[-1][-1]

You can imagine the idea of my algorithm as follows.

I have the numbers of the set in the vertical axis on the left, where the first element is actually the empty set. These numbers are not considered as only numbers, but, as I go down from the empty set (the first element), I start considering greater sets, that include all previous elements plus the current one.

Example, suppose I have the set S = {1, 2, 3}. I first consider the empty set, then the union of the empty set and {1}, then the union of {1} and {2}, and finally the union of {1, 2} and {3}.

In the horizontal axis you can imagine I have an increasing sequence of numbers up to the number we want to obtain (by summing the numbers of a certain subset of S). Example, suppose we want to obtain 4, then the increasing sequence would be 0, 1, 2, 3, 4.

So, I first start considering I want to obtain the number 0, and then 1, 2, etc, as it is usually done in a dynamic programming algorithm using a bottom-up approach.

Apart from the setup of the matrix, my algorithm assigns 1 to m[i][j], for some i = 0, 1, ..., N, where N is the size of the set S, and for some j = 0, 1, ... , M, where M is the number we want to obtain, when either the current number in the subset, that is S[i - 1], is equal to the number we want to obtain M_j, or when the previous solution to the subproblem, where the number we want to obtain is M_j - S[i - j], was 1.

That might seem a confusing explanation, and I think the code is self-explanatory.

Is my algorithm correct for all instances of the problem?

Is there a way I can improve it?


texts:
I'm quite new to Python and would love to gather feedback on my code. I've written a piece a script that allows me to search all file or directory names within a folder matching a particular pattern. The caller can specify if the function should be recursive or not.

It's only a basic script I would love to know how it could be improved!

import sys
import os
import string

def rename(source, pattern, replacement, dirs = False, recurse = False):
    """
    Rename a file or directory

    source = source directory
    pattern = old string
    replacement = new string
    dirs = apply to directorys
    recurse = move recursivley
    """
    dir = os.path.abspath(source)

    for item in os.listdir(dir):
            # Get the full path
            item = os.path.join(source, item)
            if os.path.isfile(item) and pattern in item:
                 os.rename(item, string.replace(item, pattern, replacement))
            elif os.path.isdir(item):
                if recurse:
                    # Move to the next level, before renaming
                    rename_items(item, pattern, replacement, dirs, recurse)
                if pattern in item and dirs:
                    os.rename(item, string.replace(item, pattern, replacement))

if __name__ == '__main__':

    source = sys.argv[1]
    pattern = sys.argv[2]
    replacement = sys.argv[3]

    rename(source, pattern, replacement, True, True)


texts:
I am practising recursion and have written the small code for summing a+b as below:

#include <stdio.h>

int b=6;
static int cnt;

void main(void)
{
    int a=9,sum;

    sum=succ(a);

    printf("Sum returned : %d\n",sum);
}

int succ(x)
{
    cnt++;
    return(cnt<=b ? succ(x+1) : x++);
}

I am getting the results fine using this. Can anyone suggest optimization or better code for this? I also want to  understand how stacking happens in this case for variables cnt and x. Are they stacking in pairs like 0-9, 1-10 ,2-11,3-12 ,4-13, then at last when cnt is 5 it return x from the post-increment return?


texts: Adapting new design pattern with post handler

texts:
I'm currently using this pattern in my JavaScript code. Somewhere in my code I use Dnianas.Post.init. init fires and it will bind DOM events.

At first everything was OK. But then, when you look at likePost method you will see that, I'm not using the var keyword to declare my variables. Because I if do that, then it won't be available to renderCreatedPost method. So basically I'm forced to do that.

I'm trying to adapt a new pattern and refactor my code that allow for that.

As you may know, defining variables without the var keyword is a bad practice and will result in errors in strict mode.

Dnianas.Post = {

    init: function () {
        this.bindEvents();
    },

    bindEvents: function () {
        $(document).on('submit', '#postForm', this.createPost);
        $(document).on('click', '#likePost', this.likePost);
    },

    createPost: function (event) {
        var self = Dnianas.Post;

        var request = $.ajax({
            url: '/posts/create',
            data: $(this).serialize(),
            beforeSend: function () {
                $loading.show();
            },
            complete: function () {
                $loading.hide();
            }
        });

        request.done(function (data) {
            self.renderCreatedPost(data);
        });

        event.preventDefault();
    },

    renderCreatedPost: function (data) {
        if (data.success == 'false') {
            $('.error').empty().append(data.message).slideDown(100).delay(3000).slideUp(250);
        } else {
            this.postForm[0].reset();
            $(data.post_html).insertAfter(this.postForm).hide().delay(100).slideDown(400);
            $('.no-posts').hide();
        }
    },

    likePost: function () {
        self = Dnianas.Post;
        $likePostEl = $(this);
        $postLikeCountEl = $likePostEl.children('#likeCount');
        postLikeCount = $postLikeCountEl.data('count');

        var request = $.ajax({
            url: '/posts/like',
            data: {
                post_id: $likePostEl.parents().get(2).dataset.id,
                user_id: $likePostEl.parents().get(2).dataset.userid,
                like_count: postLikeCount,
                _token: token
            }
        });

        request.done(function (data) {
            self.renderPostLike(data);
        });
    },

    renderPostLike: function (data) {
        if (data.like) {
            $likePostEl.children('.likeIcon').addClass('liked');
            $postLikeCountEl.addClass('liked');
        } else {
            $likePostEl.children('.likeIcon').removeClass('liked');
            $postLikeCountEl.removeClass('liked');
        }
        $postLikeCountEl.text(data.like_count);
    },

    getMaxId: function($element) {
        // Filter through the ids
        var ids = $element.map(function () {
            return +$(this).data('id') || 0;
        });

        // Get the highest id attribute
        last_id = Math.max.apply(Math, ids);

        // Return the maximum id.
        return last_id;
    },

    getNewPosts: function () {
        var self = Dnianas.Post;
        var ids = [];
        var $postEl = $('.poster_memb .tab_post');
        last_id = 0;

        // If there was posts.
        if ($postEl.length > 0) {
            self.getMaxId($postEl);
        }

        var request = $.ajax({
            url: '/posts/latest/' + last_id,
            type: 'GET',
            dataType: 'JSON'
        });

        request.done(function (data) {
            self.renderLatestPost(data);
        });
    },

    renderLatestPost: function (data) {
        if (data && data.is_new == 'true' && data.html) {
            $('.no-posts').hide();
            $(data.html).insertAfter('#postForm').hide().slideDown(500);
        }

        // Run the function every 10 seconds.
        setTimeout(this.getNewPosts, 10000);
    }
};


texts:
I'm very new to AngularJS. I'm using a directive for translation of category objects like:

{
"id":17,
"order":2,
"published":false,
"createdAt":"2014-12-29T16:22:43.000Z",
"updatedAt":"2014-12-29T16:23:22.000Z",
"CategoryTranslations":[
   {
      "id":13,
      "name":"Fishes",
      "description":"Fishes desc",
      "CategoryId":17,
      "LanguageId":1,
      "Language":{
         "id":1,
         "code":"en_EN",
         "name":"English"
      }
   }
]
 }

Filter:

app.filter('categoryTranslations', function() {
return function(categories, locale) {
    var translated = [];
    if (typeof categories != 'undefined') {
        for (var i = 0; i < categories.length; i++) {
            var category = categories[i];
            for (var j = 0; j < category.CategoryTranslations.length; j++) {
                var translation = category.CategoryTranslations[j];
                if (translation.Language.code == locale) {
                    category.defaultTranslation = translation;
                    translated.push(category);
                    break;
                }
            }
        }
    }
    return translated;
};
});

I really don't like the code and am looking for a better way to solve this problem.


texts:
I am new to Design Pattern implementation and would like to strongly solidify my knowledge to become as good of a programmer as I can. Is this a proper implementation of the Abstract Factory design? The idea here is that I can interchangeably utilize either a ConsoleLogger or a FileLogger for three different "levels" of logging (Execution, Output, and Error). I want the client to be able to log Execution and Output to console, but Error logs to a file if necessary.

Logger Interface:

public interface ILogger {
    public void log(String content);
}

Base Logging Factory:

import java.util.HashMap;

public abstract class LoggingFactory {
    static {
        factoryMap = new HashMap<String, LoggingFactory>();
    }

    private static HashMap<String, LoggingFactory> factoryMap;

    public abstract ILogger getLogger();

    public static void register(String type, LoggingFactory factory) {
        factoryMap.put(type, factory);
    }

    public static LoggingFactory getFactory(String type) {
        return factoryMap.get(type);
    }
}

ConsoleOutputLoggingFactory:

public class ConsoleOutputLoggingFactory extends LoggingFactory {
    static {
        LoggingFactory.register("ConsoleOutputLoggingFactory", new ConsoleOutputLoggingFactory());
    }

    @Override
    public ILogger getLogger() {
        return new ConsoleOutputLogger();
    }
}

ConsoleOutputLogger:

public class ConsoleOutputLoggerimplements ILogger {
    @Override
    public void log(String content) {
        System.out.println("ConsoleOutputLogger " + content);
    }
}

FileOutputLoggingFactory:

public class FileOutputLoggingFactory extends LoggingFactory {
    static {
        LoggingFactory.register("FileOutputLoggingFactory", new FileOutputLoggingFactory());
    }

    @Override
    public ILogger getLogger() {
        return new FileOutputLogger();
    }
}

FileOutputLogger:

public class FileOutputLogger implements ILogger {
    @Override
    public void log(String content) {
        //write output to file
    }
}

ConsoleErrorLoggingFactory:

public class ConsoleErrorLoggingFactory extends LoggingFactory {
    static {
        LoggingFactory.register("ConsoleErrorLoggingFactory", new ConsoleErrorLoggingFactory());
    }

    @Override
    public ILogger getLogger() {
        return new ConsoleErrorLogger();
    }
}

ConsoleErrorLogger:

public class ConsoleErrorLogger implements ILogger {
    @Override
    public void log(String content) {
        System.out.println("ConsoleErrorLogger" + content);
    }
}

FileErrorLoggingFactory:

public class FileErrorLoggingFactory extends LoggingFactory {
    static {
        LoggingFactory.register("FileErrorLoggingFactory", new FileErrorLoggingFactory());
    }

    @Override
    public ILogger getLogger() {
        return new FileErrorLogger();
    }
}

FileErrorLogger:

public class FileErrorLogger implements ILogger {
    @Override
    public void log(String content) {
        //write error to file
    }
}

Factory Initializer:

public class LoggingInitializer {
    public LoggingInitializer() {
        LoggingFactory.register("ConsoleOutputLogger", new ConsoleOutputLoggingFactory());
        LoggingFactory.register("FileOutputLogger", new FileOutputLoggingFactory());
        LoggingFactory.register("ConsoleErrorLogger", new ConsoleErrorLoggingFactory());
        LoggingFactory.register("FileErrorLogger", new FileErrorLoggingFactory());
    }
}

Main:

public class AbstractFactoryExample {
    public static void main(String[] args) {
        new LoggingInitializer();

        ILogger outputLogger = LoggingFactory.getFactory("OutputLogger").getLogger();
        if (outputLogger != null)
            outputLogger.log("Hello World");
    }
}


texts: Translating status codes to messages in log files

texts:
I'm not overly efficient when it comes to coding, and I am looking for ways to improve the speed of a log translator I built.

Several files are uploaded to a folder on a server and log files are selected for processing. Speed here is fine. Each log file has 5000-10000 lines possible a bit higher. They are space delimited files that look like this:

20:04:13 + MA 00 61
20:04:17 - MA 00 61
20:04:18 + MA 00 61 optionaltxt
20:04:20 - MA 00 61

There are at least 5 fields for every entry but more can be added if optional text exists. Field 5 is a status code, and it has to be cross referenced with a language file then replaced with the corresponding message so the result is like this:

17:29:48 - MA 00 (061) Feed hold! X / Y axis is locked
17:29:48 + MA 00 (061) Feed hold! X / Y axis is locked
17:29:50 - MA 00 (061) Feed hold! X / Y axis is locked optionaltxt
17:29:51 + MA 00 (061) Feed hold! X / Y axis is locked

The translated logs are saved to a new folder for download later. At the moment translation takes about 7-10 seconds per log file. The problem is that it is common to have 90 files or more and the time starts adding up. I have no control over the log format or language file format.

This part of my code looks like this:

$shLANG = file_get_contents("ErrCODES/data_en.properties");
$SHarray = explode("\n", $shLANG);

$logARRAY = glob($uploadDIR.$filepath."/original/*.[lL][oO][gG]");
foreach ($logARRAY as $logFILEarray) {

$name = basename($logFILEarray);
$logFILE = file_get_contents($uploadDIR.$filepath."/original/".$name);
$logFILEarray = explode("\n", $logFILE);
foreach ($logFILEarray as $key => $line) {
        $newline="";
        $LINEarray = explode(" ", $line);

        if(isset($LINEarray[0])){$newline .= $LINEarray[0]." ";}
        if(isset($LINEarray[1])){$newline .= $LINEarray[1]." ";}
        if(isset($LINEarray[2])){$newline .= $LINEarray[2]." ";}
        if(isset($LINEarray[3])){$newline .= $LINEarray[3]." ";}
        if(isset($LINEarray[4])){
            foreach ($SHarray as $code) {
                if (strlen($LINEarray[4])>0){
                    if (substr($code, 0, strpos($code, '=')) == $LINEarray[4]) {
                        $newline .= trim(explode('null;', $code)[1]). " ";
                    }
                }
            }
        }

        if(isset($LINEarray[5])){$newline .= $LINEarray[5]." ";}
        if(isset($LINEarray[6])){$newline .= $LINEarray[6]." ";}
        if(isset($LINEarray[7])){$newline .= $LINEarray[7]." ";}
        if(isset($LINEarray[8])){$newline .= $LINEarray[8]." ";}
        $newline .= "\r";
        $logFILEarray[$key] = $newline;
        }

//var_dump($logFILEarray);
$info = implode("\n",$logFILEarray);
file_put_contents($uploadDIR.$filepath."/translation/".$name, $info);

}//for all glob .log

The code works fine, but how can I improve the speed (greatly)?