texts: Using JavaScript callback to conditionally send email

texts:
I have an app where users at some point select the amount of tasks they have completed. If they have only completed 3 or less then 3 tasks then I send an email to the system admin to inform them.

But at the same time as the email is being sent the I want the data to be saved into the database. To achieve this I implemented a callback (or what I think is a callback.)

Is the below implementation okay?

if(req.body.sessionNo == 3){
                newDoc.sessionThreeMileStones = req.body.sessionThreeMileStones;
                // CALLBACK!!!
                checkMileStones(req.body,req.body.sessionThreeMileStones, sendMail)
    }

//SAVING THE DATA INTO MONGODB
newDoc.save(function(err, data) {
                if (err) {
                    sendJsonResponse(res, 400, err)
                }
                else {
                    incrementSessionTypesTotal(req, res);
                    sendJsonResponse(res, 200, data);
                }
            });

  // PASSING THE CALLBACK
  function checkMileStones(body,milestones,callback){
    if(milestones.length <= 3){
        //CALLING THE CALLBACK
        callback(body,milestones)
    } else {
        console.log("Not required");
    }
};

 // ACTUAL CALLBACK FUNCTION
 function sendMail(data,milestones){
    var data = {
                    from: 'App Team',
                    to: '',
                    subject: 'Milestones Alert - Session ' + data.sessionNo,
                    text: 'Hi ' + '\n\n' + data.fullName + ' from ' + data.company + ' has submitted the data for 3rd session with ' + data.clientName +'.\n' + 'They have only completed ' + milestones.length + ' milestones so far.'
                        };

                        mailgun.messages().send(data, function(error, body) {
                            console.log("sending email " + body)
                       });
}


texts:
I have a set of rules as follows:

Product One:

If either part is acceptable, update both parts

Product Two:

If either part is acceptable, and one age is > 55, update both parts
If either part is acceptable, and neither age is > 55, then update whichever part needs updating (or both)

I know it's not the most nested if statements you'll see, but it feels like I'm repeating code unnecessary and there should be a simpler version of what I've attempted.

The Code:

 if (this.isAcceptablePartOne || this.isAcceptablePartTwo) {
    if (this.productName.equals(PRODUCT_ONE)) {
        updatePartOne();
        updatePartTwo();
    }
    if (this.productName.equals(PRODUCT_TWO)) {
        if (Math.max(this.ageLifeOne, this.ageLifeTwo) > 55) {
            updatePartOne();
            updatePartTwo();
        } else {
            if (this.isAcceptablePartOne) {
                updatePartOne();
            }
            if (this.isAcceptablePartTwo) {
                updatePartTwo();
            }
        }
     }
 }


texts: Emulating super() in Python 3.x using Python 2.7

texts:
Depending on the name of the first argument, self.__sup or cls.__sup behaves like super() in Python 3, while this code is written in Python 2.7. It works for both ordinary methods and class methods(static methods don't use super()). See my code for detailed examples:

class mySuper(object):
    def __init__(self, cls):
        self.cls = cls
    def __get__(self, obj, cls):
        if obj is None:
            return super(self.cls, cls)
        else:
            return super(self.cls, obj)

class Meta(type):
    def __init__(cls, name, bases, clsdict):
        setattr(cls, '_' + name + '__sup', mySuper(cls))
        super(Meta, cls).__init__(name, bases, clsdict)

# Usage examples:

class A(object):
    __metaclass__ = Meta
    def meth(self):
        print 'A.meth(%s)' % self
    @classmethod
    def clsmeth(cls):
        print 'A.clsmeth(%s)' % cls

class B(A):
    def meth(self):
        self.__sup.meth()                # super().meth()
        print 'B.meth(%s)' % self
    @classmethod
    def clsmeth(cls):
        cls.__sup.clsmeth()              # super().clsmeth()
        print 'B.clsmeth(%s)' % cls

class C(A):
    def meth(self):
        self.__sup.meth()
        print 'C.meth(%s)' % self
    @classmethod
    def clsmeth(cls):
        cls.__sup.clsmeth()
        print 'C.clsmeth(%s)' % cls

class D(B, C):
    def meth(self):
        self.__sup.meth()
        print 'D.meth(%s)' % self
    @classmethod
    def clsmeth(cls):
        cls.__sup.clsmeth()
        print 'D.clsmeth(%s)' % cls

d = D()
d.meth()
d.clsmeth()

Here is the output:

A.meth(<__main__.D object at 0x103c8aed0>)
C.meth(<__main__.D object at 0x103c8aed0>)
B.meth(<__main__.D object at 0x103c8aed0>)
D.meth(<__main__.D object at 0x103c8aed0>)
A.clsmeth(<class '__main__.D'>)
C.clsmeth(<class '__main__.D'>)
B.clsmeth(<class '__main__.D'>)
D.clsmeth(<class '__main__.D'>)

I'm sorry if the examples are not very meaningful, but I'm just a beginner. (Maybe emulating super() itself is pointless.)

Apart from the "Usage examples", it's just a little snippet, so please tell me any problem you can spot in this piece of code.


texts:
I need to find the end of a loop or the length of the object. Some context on the code:

I obtain the txt var from the DOM (that's the only way I've got by now and can't change it) and it's a formatted JSON.  Here's an example of the expected JSON output:

  {"SE DIO AVISO A CENTRAL":true,"OTRO":false,"SE DIO AVISO A SEGURIDAD
  CIUDADANA":true,"SIN ACCIÓN":false,"SE DIO AVISO A
  AMBULANCIA":false,"SE DIO AVISO A CARABINEROS":true,"SE DIO AVISO A
  BOMBEROS":false}

My idea here is to only show the ones with value trueone after the other followed by a comma, but changing that at the end of the loop for a dot. For the moment I've got the commas down but not the dot, which is why I need to know either the length of obj or the end of the $.each() loop. Any improvements on overall code are appreciated.

try{
    var obj=jQuery.parseJSON(txt);
    $(self).text(" ");
    var i = 0;
    jQuery.each(obj, function(text, val){
        if(val){
            if(i == 0){
                $(self).append(text);
            }else{
                $(self).append(", "+text);
            }
        }
        i++;
    });
}
catch(e){
}


texts:
I have a function where I validate emails via a regex. I was wondering if this is the best regex to use or if there's a better way of doing it.

Also is there a difference between this method and just setting the type="email" in the HTML form?

<form name="form" action="process_registration.jsp" onSubmit="return validateEmail()" method="POST">
        <script>
        function validateEmail(){
            var email = document.forms["form"]["Email"].value;
            var regex = /^([0-9a-zA-Z]([-_\\.]*[0-9a-zA-Z]+)*)@([0-9a-zA-Z]([-_\\.]*[0-9a-zA-Z]+)*)[\\.]([a-zA-Z]{2,9})$/;

            if(!regex.test(email)){
                alert("Enter a valid email");
                return false;
                }
        }


texts: What would be a more optimal way of filtering a list into sub-lists

texts:
In the start of one of my methods I have a section of code that needs to get three sub-lists of grouped dictionaries from a list of grouped dictionaries. This is a big list and I'm optimizing it a little by only looking at a smaller sub-list generated from a communal attribute.

Is there a faster a way to generate these three lists.

Code

 private List<List<CellData>> FormatOfficeData(List<Dictionary<References.ColHeaders, CellData>> dataOffice)
    {
        List<Dictionary<References.ColHeaders, CellData>> dataDownTown = new List<Dictionary<References.ColHeaders, CellData>>();
        List<Dictionary<References.ColHeaders, CellData>> dataSuburban = new List<Dictionary<References.ColHeaders, CellData>>();
        List<Dictionary<References.ColHeaders, CellData>> dataTotal = new List<Dictionary<References.ColHeaders, CellData>>();

        dataOffice = dataOffice.Where(x => x[References.ColHeaders.AssetClass].val == "All Classes" && (
        x[References.ColHeaders.Submarket].val == "Downtown" ||
        x[References.ColHeaders.Submarket].val == "Suburban" ||
        x[References.ColHeaders.Submarket].val == "Total")).ToList();
        dataDownTown = dataOffice.Where(x => x[References.ColHeaders.Submarket].val == "Downtown").ToList();
        dataSuburban = dataOffice.Where(x => x[References.ColHeaders.Submarket].val == "Suburban").ToList();
        dataTotal = dataOffice.Where(x => x[References.ColHeaders.Submarket].val == "Total").ToList();

... Code continues


texts: Click handlers that display the status of a submission

texts:
It's been years since I've touched javascript and the strange scoping trips me up each time.  Please could you point me into the right direction of how to both tidy this up and prevent the duplication?

It is simply two click events, with the only difference being the color updated on success.

$(function () {

    $(".manualSubmitComplete")
        .click(function () {

            $(".spinner").show();

            var $this = $(this);

            $.get(this.href)
                .done(function () {

                    var $parent = $this.parent();

                    $parent.children("spinner").show();

                    $parent.children("a").remove();
                    $parent.children("span").remove();
                    $parent.attr("class", "");
                    $parent.css("background-color", "#B0FFB0");

                    $(".spinner").hide();
                });

            return false;

        });

    $(".manualSubmitReject")
            .click(function () {

                $(".spinner").show();

                var $this = $(this);

                $.get(this.href)
                    .done(function () {

                        var $parent = $this.parent();

                        $parent.children("spinner").show();

                        $parent.children("a").remove();
                        $parent.children("span").remove();
                        $parent.attr("class", "");
                        $parent.css("background-color", "#FF0000");

                        $(".spinner").hide();
                    });

                return false;

            });

})


texts: Using std::future to manipulate objects in C++11

texts:
I'm experimenting with std::future, but haven't found any materials that deal with the usecase I have in mind and was hoping someone could either confirm or deny the validity of the approach excerpted below:

class Thing {
private:

    std::string mValue;

public:
    Thing() : mValue( "" ) {}

    void set(const std::string& s)
    {
        mValue = s;
        reverse( mValue.begin(), mValue.end() );
    }

    const std::string& getValue() const
    {
        return mValue;
    }
};

int main(int argc, const char * argv[])
{
    Thing* tA = new Thing();
    Thing* tB = new Thing();
    Thing* tC = new Thing();

    auto tLambda = [](const string& iStr, Thing* oThing) -> Thing* {
        oThing->set( iStr );
        return oThing;
    };

    future<Thing*> tA1 = async( launch::async, tLambda, "This is a test...A", tA );
    future<Thing*> tB1 = async( launch::async, tLambda, "This is a test...B", tB );
    future<Thing*> tC1 = async( launch::async, tLambda, "This is a test...C", tC );

    future<Thing*> tA2 = async( launch::async, tLambda, tA1.get()->getValue(), tA );
    future<Thing*> tB2 = async( launch::async, tLambda, tB1.get()->getValue(), tB );
    future<Thing*> tC2 = async( launch::async, tLambda, tC1.get()->getValue(), tC );

    cout << tA2.get()->getValue() << endl;
    cout << tB2.get()->getValue() << endl;
    cout << tC2.get()->getValue() << endl;

    return 0;
}

In essence, I'd like to use std::future to update a pre-existing object's member variables rather than initializing and returning a new object (as is shown in most std::future examples I've seen). In the example above, the model seems a little suspect. I would love to hear any thoughts on the best way to achieve this sort of functionality. Ultimately, I would like to embed this in a thread-pool sort of context.

(Note: On another user's suggestion, this is reposted from Stack Overflow).


texts:
This is part of my first OCaml program.

Its job is to replace a set of placeholder characters with umlauts. Taking the German word Ruebe as an example, the program turns it into Rübe. Other examples are Moewe -> Möwe and aendern -> ändern

Generally, everytime the program encounters the characters ae, oe, and ue it turns them into umlauts. There is an exception though: If the preceding character is a vowel, it does not change the word. This ensures words like Treue remain as they are (since there is no word Treü).

I've used the library Re2 for regular expressions. Re2 doesn't implement lookarounds, which was my first thought for checking for preceding vowels. This is the reason for the function replace_if_not_after_vowel.

I am happy for all suggestions, especially if they help make the code more idiomatic or simpler.

To compile the program, I used the command ocamlbuild -use-ocamlfind -package re2 -package core -tag thread myCompiledFile.byte

open Core.Std
open Re2.Std
open Re2.Infix

(* If the word contains an umlaut placeholder like "ue" we replace that with the proper umlaut "ü". Except if there is a vowel directly before the placeholder like in "Treue" *)
let placeholders_to_umlauts = [("ue", "ü"); ("oe","ö"); ("ae","ä"); ("Ue", "Ü"); ("Oe","Ö"); ("Ae","Ä")]

(* A regex that matches a vowel *)
let vowel = ~/"[aeiou]"

(* Applies a list of changes to a word *)
let rec apply_changes word changes =
  match changes with
  | [] -> word
  | change :: rest -> apply_changes (change word) rest

(* Replaces replace_this with replacement inside the text if the preceding character not a vowel. Since Re2 doesn't implement lookarounds we can't use a negative lookbehind *)
let replace_if_not_after_vowel replace_this replacement text =

  Re2.replace_exn ~/replace_this text ~f:(fun regex_match ->
   (* Returns true if there is a vowel at the given position in the text *)
   let is_vowel text pos =
     if pos >= 0 && pos < String.length text then
       let maybe_vowel = String.get text pos in
       Re2.matches vowel (Char.to_string maybe_vowel)
     else false
   in

   (* Get the position in the text where the regex matched *)
   let match_pos, _ = Re2.Match.get_pos_exn ~sub:(`Index 0) regex_match in

   (* Replace the placeholder if doesn't follow a vowel *)
   if is_vowel text (match_pos -1 ) then replace_this else replacement
   )

let change_word word =
  (* Those are the changes that we will apply to the word *)
  let changes = List.map placeholders_to_umlauts ~f:(
    fun (placeholder, umlaut) -> replace_if_not_after_vowel placeholder umlaut
  )
  in

  apply_changes word changes

  let () =
    (* We want to change this word into "Übergrößenträgertreue" *)
    let word = "Uebergroeßentraegertreue" in
    let new_word = change_word word in

    printf "new word: %s\n" new_word


texts:
Security threats in mind:

SQL Injections!!! --- Solutions: Prepared Statements (PDO) and including
$bpdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); in the
database connection

XSS Attacks!!! --- Solutions: htmlspecialchars(), Content-Security
Policy (placed in htaccess).

OS Command Attacks!!! --- Solutions: Striping whitespace.

DOS Attacks!!! --- Solution: None implemented. I'm unsure if any additional precaution is necessary, since there are no login possibilities on my website.

Is the following script sufficient enough to sanitize user input?
//sanitize user input
$Tmessage = trim(preg_replace('/\s\s+/', ' ', htmlspecialchars($_POST["message"])));
$sanitizedMessage = filter_var($Tmessage, FILTER_SANITIZE_STRING);

//prepare info and submit into table
$stmnt = $pdo->prepare("INSERT INTO submissionsTable (message) VALUES (:message)");
$stmnt->execute(['message' => $sanitizedMessage]);
header("Location: success.html");
exit (0);


texts: KQuery SPOJ problem, using merge sort segment tree

texts:
Problem : KQUERY

Summary: For each query consisting of \$(i, j, k)\$, return the number of elements greater than \$k\$ in the subsequence \$a_i, a_{i+1}, …, a_j\$.

My code has complexity of \$O(n\log^2{n})\$. I am only interested in segment tree based solution of Kquery. Please let me know why am I getting TLE using merge sort segment tree. Is it possible to do some optimization to get my code accepted or is this approach totally wrong. Here I have used merge sort segment tree which has sorted vector as its node and to query particular range it creates the sorted vector of elements for the given query.

#include <bits/stdc++.h>
using namespace std;

const int N=30005;
int arr[N];
vector<int> seg[4*N];

void build(int low,int high,int node)
{
    if(low>high)
    return;
    if(low == high)
    {
        seg[node].push_back(arr[low]);
        return;
    }
    int mid=low+high>>1;
    build(low,mid,2*node+1);
    build(mid+1,high,2*node+2);
    merge(seg[2*node+1].begin(),seg[2*node+1].end(),seg[2*node+2].begin(),seg[2*node+2].end(),back_inserter(seg[node]));
}

vector<int> query(int low,int high,int lq,int hq,int k,int node)
{
    vector<int> ans;
    if(low>high || low>hq || high<lq)
    return ans;
    if(lq<=low && high<=hq)
    {
        return seg[node];
    }
    vector<int> left,right;
    int mid=low+high>>1;
    left = query(low,mid,lq,hq,k,2*node+1);
    right = query(mid+1,high,lq,hq,k,2*node+2);
    merge(left.begin(),left.end(),right.begin(),right.end(),back_inserter(ans));
    return ans;
}
int main(){
    int n;
    scanf("%d",&n);
    register int i;
    for(i=0;i<n;i++)
    scanf("%d",&arr[i]);
    register int q;
    scanf("%d",&q);
    int x,y,k;
    build(0,n-1,0);
    while(q--)
    {
        scanf("%d %d %d",&x,&y,&k);
        vector<int> ans=query(0,n-1,x-1,y-1,k,0);
        int point=upper_bound(ans.begin(),ans.end(),k)-ans.begin();
        printf("%d\n",ans.size()-point);
    }
    return 0;
}


texts: Is the usage of SQL Cursor appropriate here?

texts:
I'm looking for following kind of feedback:

Firstly, is the use of a cursor an overkill here?
Is there a simpler way to do what I'm doing in SQL?
Could the script go terribly wrong?
I have limited SQL experience, how does it look? Any standards that I'm not following?

Bear in mind that this is a one-time script to be run, so performance is not absolutely crucial. Nevertheless, I would like to have some feedback on whether it could still be done more efficiently.

A few words about what I'm trying to do: There are two tables, tblEmployment & tblUserCodeList. The first table (tblEmployment) has a column of type int named RepaymentType. Due to a requirements change, this has to be abandoned and converted to a specific GUID/uniqueidentifier to be stored in another column RepaymentTypeIdentifier in the same tblEmployment. That is the whole point of the SQL script. I'll leave out further business logic.

So for each row in tblEmployment I want to read its RepaymentType and depending on its value (can only be 1-12) I want to store a specific GUID from tblUserCodeList (column:UserCodelistIdentifier) in the column RepaymentTypeIdentifier.

The column RepaymentTypeIdentifier is thus a Foreign Key referencing UserCodelistIdentifier in tblUserCodelist.

(I have shortened the CASE statement a little bit.)

USE db_name_left_out...
GO

DECLARE @EmploymentId as NVARCHAR(50);
DECLARE @UserCodeList TABLE
(
    Title NVARCHAR(100),
    UserCodeListIdentifier NVARCHAR(50)
)

INSERT INTO @UserCodeList (Title, UserCodeListIdentifier)
    SELECT Title, UserCodeListIdentifier
    FROM tblUserCodeList;

Declare @EmpCursor as CURSOR

SET @EmpCursor = CURSOR FOR
    SELECT EmploymentIdentifier FROM dbo.tblEmployment;

OPEN @EmpCursor;

FETCH NEXT FROM @EmpCursor INTO @EmploymentId;

WHILE @@FETCH_STATUS = 0
BEGIN
    UPDATE dbo.tblEmployment SET RepaymentTypeIdentifier = CASE
            when RepaymentType = 1 then (SELECT TOP 1 UserCodeListIdentifier FROM @UserCodeList WHERE title like '%some_text_1%')
            when RepaymentType = 2 then (SELECT TOP 1 UserCodeListIdentifier FROM @UserCodeList WHERE title like '%some_text_2%')
            when RepaymentType = 3 then (SELECT TOP 1 UserCodeListIdentifier FROM @UserCodeList WHERE title like '%some_text_3%')
            .
            .
            .
            when RepaymentType = 12 then (SELECT TOP 1 UserCodeListIdentifier FROM @UserCodeList WHERE title like '%some_text_12%')
        END
    WHERE EmploymentIdentifier = @EmploymentId

    FETCH NEXT FROM @EmpCursor INTO @EmploymentId;
END

CLOSE @EmpCursor

DEALLOCATE @EmpCursor;


texts: Computation for finding the largest prime factor of 600851475143 is slow

texts:
I implemented the following for Project 3 in Project Euler:

--Problem 3. The prime factors of 13195 are 5, 7, 13 and 29. What is the
--largest prime factor of the number 600851475143?

prime :: Integer -> Bool
prime x = prime' x (x `div` 2) where
          prime' _ 1 = True
          prime' x y = if x `mod` y == 0 then False
                       else prime' x (y - 1)

largestPrimeFactor :: Integer => Maybe Integer
largestPrimeFactor x = largestPrimeFactor' x (x `div` 2)
             where largestPrimeFactor' _ 1 = Nothing
                   largestPrimeFactor' x y = if x `mod` y == 0 && prime y then Just y
                                             else largestPrimeFactor' x (y - 1)

I was able to get Just 29 per the introduction to this problem from Project Euler:

*Main> largestPrimeFactor 13195
Just 29
*Main> largestPrimeFactor 600851475143 -- running for (as of now) 15 hours!

My implementation succeeded for getting 29 from 13195, but the computation for 600851475143 has been running for 15 hours so far.

Please review my implementation for correctness and idiomatic Haskell.


texts:
I have created a basic checkbox custom meta box in order to set a post as "featured".

//create the boxes html markup
function custom_meta_box_markup($object)
{
    wp_nonce_field(basename(__FILE__), "meta-box-nonce");

    ?>
        <div>

            <label for="meta-box-checkbox">Featured Article </label>
            <?php
                $checkbox_value = get_post_meta($object->ID, "meta-box-checkbox", true);

                if($checkbox_value == "")
                {
                    ?>
                        <input name="meta-box-checkbox" type="checkbox" value="true">
                    <?php
                }
                else if($checkbox_value == "true")
                {
                    ?>
                        <input name="meta-box-checkbox" type="checkbox" value="true" checked>
                    <?php
                }
            ?>
        </div>
    <?php
}

function add_custom_meta_box()
{
    add_meta_box("demo-meta-box", "Featured Article", "custom_meta_box_markup", "post", "side", "high", null);
}

add_action("add_meta_boxes", "add_custom_meta_box");
// save checkbox meta
function save_custom_meta_box($post_id, $post, $update)
{
    if (!isset($_POST["meta-box-nonce"]) || !wp_verify_nonce($_POST["meta-box-nonce"], basename(__FILE__)))
        return $post_id;

    if(!current_user_can("edit_post", $post_id))
        return $post_id;

    if(defined("DOING_AUTOSAVE") && DOING_AUTOSAVE)
        return $post_id;

    $slug = "post";
    if($slug != $post->post_type)
        return $post_id;

    $meta_box_checkbox_value = "";

    if(isset($_POST["meta-box-checkbox"]))
    {
        $meta_box_checkbox_value = $_POST["meta-box-checkbox"];
    }
    update_post_meta($post_id, "meta-box-checkbox", $meta_box_checkbox_value);
}

add_action("save_post", "save_custom_meta_box", 10, 3);

Now this code does work rendering a checkbox in the post editor, and saving the meta information for use within the theme. The issue I have is this creates several rows in the DB (seems like it could be a performance issue down the road) and more importantly, there can be several "featured" posts, when there should be only one. What is the most efficient way to maybe consolidate this meta field into only 1 db row AND check and erase any existing featured posts during the save of the current featured post?


texts:
I'm going to be working on a much larger version of this program and I just wanted to see if there was anything I should change in my style of coding before I made anything larger.

If it wasn't obvious, this code goes through each comic on A Softer World and downloads each comic.

Oh and with larger projects, should I try and fit my main() function into a class or is having a large main() function normal with programs?

from BeautifulSoup import BeautifulSoup as bs
from urllib import urlretrieve
from urllib2 import urlopen
from os import path, mkdir
from threading import Thread
from Queue import Queue

class Worker(Thread):

    def __init__(self, queue):
        self.queue = queue
        self.url = 'http://asofterworld.com/index.php?id='
        Thread.__init__(self)

    def run(self):
        while 'running':
            number = self.queue.get()
            soup = bs(urlopen(self.url + number))
            match = soup.find('p', {'id' : 'thecomic'}).find('img')

            filename = match['src'].split('/')[-1]
            urlretrieve(match['src'], 'Comic/' + filename)
            self.queue.task_done()

def main():

    queue = Queue()

    if not path.isdir('Comic'):
        mkdir('Comic')

    for number in xrange(1, 977):
        queue.put(str(number))

    for threads in xrange(20):
        thread = Worker(queue)
        thread.setDaemon(True)
        thread.start()

    queue.join()

main()


texts:
What I basically want to do is this:

int main()
{
    const int n = 100;

    #pragma omp parallel for
    for (int i=0; i<n; i++)
    {
        int thread_ID = omp_get_thread_num();
        printf("%d) work 1 %d\n", thread_ID, i);
    #pragma omp barrier
        printf("    %d) work 2 %d\n", thread_ID, i);
    #pragma omp barrier
    }
    return 0;
}

except we cannot put a barrier into a parallel for in OpenMP; it just cannot be done. However, it's possible to explicitly do the separation between the threads in order to make it work:

#include <stdlib.h>
#include <stdio.h>
#include <omp.h>

int main()
{
    const int n = 100;
    int nthreads;
    #pragma omp parallel
    {
        // get the number of threads
        #pragma omp single
        {
            nthreads = omp_get_num_threads();
        }

        int thread_ID = omp_get_thread_num();

        // calculate which threads have to do one more iteration
        int one_more = thread_ID<(n%nthreads);
        int step = n/nthreads;
        int start, end;

        if(one_more){
            start = step*thread_ID + thread_ID;
            end = start + step + 1;
        }
        else{
            start = step*thread_ID + n%nthreads;
            end = start + step;
        }

        // the real work is here
        for (int i=start; i<start+step+1; i++)
        {
            if(i<end)
                printf("%d) work 1 %d\n", thread_ID, i);
        #pragma omp barrier
            if(i<end)
                printf("    %d) work 2 %d\n", thread_ID, i);
        #pragma omp barrier
        }

    }
    return 0;
}

However, I find this a bit dirty and I would have preferred something cleaner. Does somebody have a better idea?


texts:
This is a problem from CodeWar.

  Write a function that will return the count of distinct
  case-insensitive alphabetic characters and numeric digits that occur
  more than once in the input string. The input string can be assumed to
  contain only alphabets (both uppercase and lowercase) and numeric
  digits.

  EX: "abba" -> 2, "aabBcde" -> 2.

Here is my solution in Java:

public class CountingDuplicates {
      public static int duplicateCount(String text) {
      String textLower = text.toLowerCase();
      char[] charArray = textLower.toCharArray();
      String uniqueRepeats = ""; //Will keep track of unique repeats.
      int count = 0;

      for(int i = 0; i < charArray.length - 1; i ++) {
      String restOfString = textLower.substring(i + 1);

      //Convert single char to String to be used in method.
      String character = Character.toString(charArray[i]);

      //If not in uniqueRepeats, check if it is a repeat.
      if(!uniqueRepeats.contains(character)) {
          if(restOfString.indexOf(character) != -1) {
     //If it is a repeat, increase count and concat it to uniqueRepeats
              count++;
              uniqueRepeats += character;
     }
    }
   }
  return count;
  }
}

Some improvements that I can think of is using a HashMap to store the repeats so that I do not have to create a new string object for every repeat
If you guys have any suggestions on code readability, code structure, or better implementation for this problem, please leave me a comment. Thank you!
EDIT - Here is the link to the problem: https://www.codewars.com/kata/counting-duplicates/train/java


texts: Rapid color replacement for in-memory BMP

texts:
A function that relatively quick replaces one color with another, in a memory-buffered BMP.
I am trying to improve the performance as much as it is possible. The previous implementation of this function was even faster.. but then I ran into some padding complications.

union
{
    unsigned long ulColor;
    unsigned char byteColor[4];
} oldColor;
union
{
    unsigned long ulColor;
    unsigned char byteColor[4];
} newColor;

typedef unsigned char BYTE;
typedef unsigned short int WORD;
typedef unsigned long int DWORD;
typedef unsigned long long int DDWORD;

DWORD
bitfox_find_xy
(DWORD WIDTH, DWORD xp, DWORD yp)
{
    DWORD w = WIDTH;
    DWORD channels = 3;
    DWORD bpp = 8;
    DWORD single = (channels*bpp)/8;
    DWORD offset = (2+sizeof(BMP)+sizeof(DIB));
    DWORD rowsize = w*single;

    DWORD pixAddress;

    if (rowsize % 4 != 0) rowsize += 4 - (rowsize % 4); // account for padding
    pixAddress = offset + yp*rowsize + xp*single; // finding the address
    return pixAddress;
}
DDWORD
bitfox_color_replace_data
(BYTE *buff, BYTE old_r, BYTE old_g, BYTE old_b, BYTE new_r, BYTE new_g, BYTE new_b)
{
    #define OFFSET_OF_SIZE      0x2
    #define OFFSET_OF_PIXELS    0xA
    #define OFFSET_OF_WIDTH     0x12
    #define OFFSET_OF_HEIGHT    0x16

    DWORD* buffSize     = (DWORD*)&buff[OFFSET_OF_SIZE];
    DWORD* buffPixels   = (DWORD*)&buff[OFFSET_OF_PIXELS];
    DWORD* buffWidth    = (DWORD*)&buff[OFFSET_OF_WIDTH];
    DWORD*  buffHeight  = (DWORD*)&buff[OFFSET_OF_HEIGHT];

    DDWORD pixels_replaced = 0;
    DDWORD ln, col;

    oldColor.byteColor[0] = old_b;  newColor.byteColor[0] = new_b;
    oldColor.byteColor[1] = old_g;  newColor.byteColor[1] = new_g;
    oldColor.byteColor[2] = old_r;  newColor.byteColor[2] = new_r;

    for(ln = 0; ln < *buffHeight; ln++)
    {
        for(col = 0; col <*buffWidth; col++)
        {
            DDWORD offset = bitfox_find_xy(*buffWidth, col, ln);

            if(!memcmp(buff + offset, oldColor.byteColor, 3))
            {
                memcpy(buff + offset, newColor.byteColor, 3);

                pixels_replaced++;
            }
        }
    }
    return pixels_replaced;
}


texts: Find the longest path in a matrix where each step has entries that differ by 1

texts:
Given an N*N matrix that all numbers are distinct in it, the function should find the maximum length path (starting from any cell) such that all the cells along the path are in increasing order with a difference of 1.

It can move in 4 directions from the given cell (i, j), It can move to (i-1, j) or (i, j-1) or (i+1, j) or (i, j+1) with the condition that the adjacent cells have a difference of 1.

I wrote the following code that works in all the cases that I have checked.
I know that this is not optimal writing, with your permission, I would love to hear insights and ways to shorten the code. The program must be pure recursion without loops at all.

I'm not satisfied with the form and structure of my code because I went through the recursion step by step over all the options. I'm trying to write this in the form of backtracking with markings cells and stop conditions.

In the original question, the complexity of place, time, and the code are irrelevant.
At the moment, the discussion deals with the complexity of the code. I want to write it in a shorter and more elegant way while implementing backtracking.

public static int longestWorm(int[][] mat){
    return longestWorm(mat, 0,0,0);
}

private static int longestWorm(int[][] mat,int i, int j, int max){
    if (i == mat.length) return max;
    if (j == mat[i].length-1)
        return longestWorm(mat, i + 1, 0, max);
    if (wormCount(mat,i,j,0) > max)
        max = wormCount(mat,i,j,0);
    return longestWorm(mat, i, j + 1, max);
}
private static int wormCount(int[][] mat, int i, int j,int count){
    if(i < mat.length-1 && mat[i][j] == mat[i+1][j]+1)
        return 1+ wormCount(mat, i+1, j, count+1);
    if(j < mat[i].length-1 && mat[i][j] == mat[i][j+1]+1)
        return 1+ wormCount(mat, i, j+1, count+1);
    if(i > 0 && mat[i][j] == mat[i-1][j]+1)
        return 1+ wormCount(mat, i-1, j, count+1);
    if(j > 0 && mat[i][j] == mat[i][j-1]+1)
        return 1+ wormCount(mat, i, j-1, count+1);
    return count;
}


texts:
I'm currently filtering a list based on several conditions, which may or may not be present:

    def requests = Request
        .list()
        .findAll { if (exportFrom) { it.tsCreated.millis >= exportFrom.millis } else { it } }
        .findAll { if (exportTo) {  it.tsCreated.millis <= exportTo.millis } else { it } }
        .findAll { if (clientId) { it.resolvedClient?.clientId == clientId } else { it } }
        .findAll { if (params.exportType) { it.typeName == params.exportType } else { it } }
        .findAll { if (params.exportStatus) { it.status.toString() == params.exportStatus } else { it } }

For instance, exportFrom is a date coming from the user interface. If it's present, we only want to export requests that occurred on that date or later.  If not, we can disregard that test.

it will never be 0 or "", as it from a list of objects that have been loaded from the database and so have already been validated.

While this is readable and works, it seems like a poor solution and probably won't be very performant on a large dataset.  Is there a Groovy idiom for this kind of series of selective filters?


texts: Saving settings to Windows registry as a tuple

texts:
Regarding an application written in C++ using the Windows API. I'd like to store a user setting like the window position on program exit, to the Windows registry and retrieve settings like these on the next start of the program.

I currently have 3 settings to be stored/retrieved to/from the registry.

typedef struct tagWINPOS
{
  DWORD dwWindowStyle;
  int iWindowX;
  int iWindowY;
} winpos_t;

HKEY hKeyApp = (HKEY)INVALID_HANDLE_VALUE;
const char szWindowStyle[] = "WindowStyle";
const char szWindowX[] = "WindowX";
const char szWindowY[] = "WindowY";

Later on in the code I'm calling functions like RegSetValueEx and RegQueryValueEx always three times. Like so:

int SaveSettings( HWND hWnd )
{
  winpos_t wpos;
  // window dimensions from hWnd are stored to wpos, not shown

  RegSetValueEx( hKeyApp, szWindowStyle, 0,
    REG_DWORD,
    (const BYTE*)&wpos.dwWindowStyle,
    sizeof(wpos.dwWindowStyle) );
  RegSetValueEx( hKeyApp, szWindowX, 0,
    REG_DWORD,
    (const BYTE*)&wpos.iWindowX,
    sizeof(wpos.iWindowX) );
  RegSetValueEx( hKeyApp, szWindowY, 0,
    REG_DWORD,
    (const BYTE*)&wpos.iWindowY,
    sizeof(wpos.iWindowY) );

  // further irrelevant code removed
}

Things start to look a bit tedious. So I was thinking to put all the parameters for one function call (say RegSetValueEx) into an std::array and then make a for loop iterating over all the array elements.

Because the Windows registry can store multiple data types, like DWORD (32-bit), QWORD (64-bit) and strings. I thought about std::variant to list all of these for this data field.

I'm started with a little test program like this:

#include <cstddef>
#include <cstdio>
#include <cstdint>
#include <tuple>
#include <variant>

typedef std::variant< int32_t, int64_t, char* > data_t;

typedef std::tuple<
  data_t,       // setting data
  const char*,  // setting name
  std::size_t   // size of setting [bytes]
> setting_t;

typedef std::array< setting_t, 2 > settings_t;

constexpr int reg_dword = 0;
constexpr int reg_qword = 1;

constexpr settings_t settings{
  std::make_tuple( (std::in_place_index<reg_dword>, 33), "Mydata", 4 ),
  std::make_tuple( (std::in_place_index<reg_qword>, 34), "Mydata2", sizeof(std::in_place_index_t<reg_qword>) ) };

int main()
{
  printf( "name: %s, value %ld\n", std::get<1>( settings[0] ), std::get<0>( std::get<0>( settings[0] ) ));

  getchar();
  return EXIT_SUCCESS;
}

Anyway, do people consider this approach a good one to get to my goal of having to spell out a single function call of RegSetValueEx inside a for loop?

The initialization of the array has much boilerplate as well as the effort to obtain values. What is a good approach to improve the readability of this? Maybe a constexpr function like std::make_tuple but then more specific for my case?


texts: Custom function for serialization of form inputs (and selects)

texts:
Just asking to if following code is ok since i needed didn't find equivalent of .serialize() for vanilla JS. So i invented my own function for plain ol' JS.

    function uzim_vred(phpdoc,parent_id){

    var name = "";
    var val = "";
    var str = phpdoc+"?,";
    var len = ($("#"+parent_id+" [name]").length);
    var i = 0;

    $("#"+parent_id+" [name]").each(function(){

        i++;
        name = $(this).attr('name');
        val = $(this).val();

        str += name+"="+val;

        if(i<len){
            str += "&,"
        }

    });

    i = 0;
    var elems = str.split(",");

    str = "";
    while(i<=len){
        str += elems[i];
        i++
    }

        var xhttp = new XMLHttpRequest();

        xhttp.onreadystatechange = function() {

            if((document.getElementById("raport")!=undefined || document.getElementById("raport")!=null) && this.readyState == 4 && this.status == 200){
                document.getElementById("raport").innerHTML = this.responseText;
            }

        };

        xhttp.open("GET", str, false);
        xhttp.send();

}

How to envoke it:

<button type="button" onclick="uzim_vred('yourPHPscript.php',$(this).parent().attr('id'))">Send</button>

First argument is a path to and name of php script, second is used for id of form element from whom you want to "serialize" values.


texts: Asynchronous Web Crawler with Pyppeteer - Python

texts:
This weekend I've been working on a small asynchronous web crawler built on top of asyncio. The webpages that I'm crawling from have Javascript that needs to be executed in order for me to grab the information I want. Hence, I'm using pyppeteer as the main driver for my crawler.

I'm looking for some feedback on what I've coded up so far, as async, asyncio, and pyppeteer are all still fairly new to me and I feel I'm making some pretty big, naive mistakes in my approach. I'm also using this as an opportunity to learn. Any feedback is greatly appreciated! Thanks.

import asyncio
import lxml.html as html
from pyppeteer import launch

seed = 'https://example.com' # Base seed URL

def get_links(url, text):
    """"Returns a list of valid URLs from a given document.

    Args:
        url: the url of the page
        text: the HTML document

    Returns:
        l: a list of valid urls to crawl
    """
    h = html.fromstring(text)
    l = [a.attrib['href'] for a in h.xpath('//a')]
    l = [seed+a for a in filter(lambda a: 'http' not in a, l)]  # Removes external URLs.
    return l

async def _crawl(browser, que):
    """Crawls a webpage.

    Args:
        browser: a pyppeteer browser object
        que: the main task queue
    """
    page = await browser.newPage() # Creates a new page
    seen = set()
    while not que.empty():
        url = await que.get() # Retrieves a url from the task queue

        if url in seen: # If the url has already been crawled, complete the task and continue
            que.task_done()
            continue

        seen.add(url)

        await page.goto(url, timeout=10000) # go to the current url
        text = await page.content() # get the page content
        links = get_links(page.url, text) # retrieve valid links to be crawled

        for link in links:
            if link in seen:
                continue

            que.put_nowait(link) # put an unseen link into the task queue

        print('put %3d links from %s into queue' % (len(links), url))
        que.task_done() # complete the task

class CrawlerPool:
    """A worker pool for managing crawlers."""
    def __init__(self, loop, coro, max_crawlers):
        self.loop = loop
        self.q = asyncio.Queue(loop=self.loop)
        self.coro = coro
        self.max_crawlers = max_crawlers

    async def run(self):
        browser = await launch()
        crawlers = []

        for _ in range(self.max_crawlers):
            crawlers.append(asyncio.create_task(self.coro(browser, self.q)))

        await self.q.join()

        for c in crawlers:
            c.cancel()

        await browser.close()
        print('done.')

loop = asyncio.get_event_loop()
cp = CrawlerPool(loop, _crawl, 5)

cp.q.put_nowait(seed)

loop.run_until_complete(cp.run())

Edit Added additional comments to code and concerns.

The concerns I have are mostly a result of my ignorance to asynchronous functions in Python and the pyppeteer library. I'm ideally just looking for feedback if what I've written makes sense in an asynchronous model.

Additional Edit

After looking over and reviewing it with a coworker, this snippet seems to work fine. I believe that the for loop containing all the c.cancel() calls can even be removed as they are redundant.


texts:
I wonder what is better code practice or just what looks better in Java:

Version 1:

protected Boolean doSomething(int amount) {
    if (amount < 1) return false;

    return insertToDb(fillImages(fetchInfo(REQUEST_URL, amount)));
}

Version 2:

protected Boolean doSomething(int amount) {
    if (amount < 1) return false;

    Data[] data = fetchInfo(REQUEST_URL, amount);

    fillImages(data);
    // or even
    // data = fillImages(data);

    return insertToDb(data);
}

This could be a problem if we had more functions chained like that. I would like to make my code nicer, for anyone else who might have to edit it, and just to know that I am not writing anything that looks stupid.


texts: sudoedit_enhanced written POSIX-ly (readlink possible problem?)

texts:
sudoedit_enhanced written POSIX-ly challenge

While creating sudoedit_enhanced POSIX (apart from readlink I have been pointed out) shell script snippet, I face two challenges:

check if all the files exist, it does not make sense to create a new file this way
sudoedit does not work with symlinks, so translate symlinks to normal file paths

The core part (carved out) checking the existence of files and translates symlinks to normal files follows:

for file in "$@"; do
    if ! [ -f "$file" ]; then
        printf '%s\n' >&2 "sudoedit_enhanced_run(): This file ('$file') does not exist or it is not a regular file."
        return 1
    fi
    [ -L "$file" ] && file=$(readlink -f "$file")
    set -- "$@" "$file"
    shift
done

It should work, though not much testing has been done so far, I am quite positive there is space to improve.


texts: Validate particular id's depending on what process type is coming

texts:
I have code where I validate deviceId and clientId basis on what ProcessType is coming:

  public static boolean isValid(ProcessType processType, Record record) {
    String deviceId = DataUtils.extract(record, "deviceId");
    String clientId =DataUtils.extract(record, "clientId");

    if(processType == ProcessType.ABC || processType == ProcessType.DEF) {
        if (Strings.isNullOrEmpty(clientId) || Strings.isNullOrEmpty(deviceId)) {
        LogMetric.getInstance().increment(processType.name(), Metric.ID);
        logger.logError("invalid clientId and deviceId provided. ProcessType= ", processType,
            ", Record= ", record);
          return false;
        }
    } else {
        if (Strings.isNullOrEmpty(clientId) && Strings.isNullOrEmpty(deviceId)) {
        LogMetric.getInstance().increment(processType.name(), Metric.ID);
        logger.logError("invalid clientId and deviceId provided. ProcessType= ", processType,
            ", Record= ", record);
          return false;
        }
    }
    return true;
  }

If ProcessType is coming as ABC or DEF, then I will return false if clientId or deviceId is missing
Otherwise I will return false if both clientId and deviceId are missing.
If all the conditions are not met, then I will return true.

I wanted to see if there is any better way to write this. I'm working with Java 7.


texts: Ten thousand and first prime checker takes a very long time

texts:
I am attempting problem 7 on Project Euler.  I have come up with this solution which works fine for finding smaller nth prime numbers, but really lags when it comes to higher nth prime numbers.  I am not quite sure where to start to make it more efficient.

public class TenThousandFirstPrime {

    public static boolean isPrime(int num) {
        if(num % 2 == 0) return false;
        for(int i = 3; i < Math.floor(Math.sqrt(num)); i += 2) {
            if(num % i == 0) return false;
        }

        return true;
    }

    public static void main(String[] args) {
        int count = 2;
        int i = 3;
        while(count <= 10001) {
            if(isPrime(i)) {
                i += 2;
                count++;
            }
        }
        System.out.println(i);
    }
}

The solution only took a couple of minutes and I didn't expect it to be as slow as it is...


texts:
There are two functions, sportsRunning and sportsWalking, that will render different text. For this, I am using an if condition at the bottom to display corresponding div:

{sportsDescription.sportsDate && sportsDescription.sportsDate.length > 0 && sportsDescription.sportsDate !== '2199-01-01' ?
                this.sportsWalking() : this.sportsRunning()}`

Can you tell me how I can refactor the code?

sportsRunning() {
    const sportsFire = this.props.profile.firstName;
    return (

{`Want some advice${sportsFire && sportsFire.length > 0 ? `, ${sportsFire}` : ''}?`}

Stay focused by setting a target date for completion of your sports.

                Set Target Date

    );
}

sportsWalking() {
    const sportsFire = this.props.profile.firstName;
    return (

{`Want a helpful tip${sportsFire && sportsFire.length > 0 ? `, ${sportsFire}` : ''}?`}

Add some momentum to your account by setting up automatic recurring deposits.

                Set Up Automatic Deposits

    );
}

render() {
    const {sportsDescription, data} = this.props;
    const sportsAmount = parseFloat(sportsDescription.sportsAmount);
    const accumulatedAmount = data.summary.total || 0;

    return (

                {
                    sportsDescription.error ?
                        this.renderProgressError() :
                        this.renderProgress({
                            accumulatedAmount,
                            sportsAmount
                        })
                }

Your Goal Progress

You have accumulated = 0 ? null : 'negative'}>
                        {moneyFormat(accumulatedAmount)} towards your sports of
                        {moneyFormat(sportsAmount)}.

                    {
                        sportsDescription.sportsDate && sportsDescription.sportsDate.length > 0 &&
                        sportsDescription.sportsDate !== '2199-01-01' && this.renderGoalDate(sportsDescription.sportsDate)
                    }

                        Edit Goal Details

            {sportsDescription.sportsDate && sportsDescription.sportsDate.length > 0 && sportsDescription.sportsDate !== '2199-01-01' ?
                this.sportsWalking() : this.sportsRunning()}

    );


texts: MSSQL query to look for duplicate record

texts:
This query took 6 seconds to complete. How can I optimize it?
Total records in table is 166803.

SELECT ltrim(rtrim(CAST(cageID as nvarchar(max))))+ltrim(rtrim(CAST(trayNo as nvarchar(max)))) as _unique,*
from lf_transit_cage
where ltrim(rtrim(CAST(cageID as nvarchar(max))))+ltrim(rtrim(CAST(trayNo as nvarchar(max)))) in
(
    SELECT dt._unique FROM
    (
        SELECT ltrim(rtrim(CAST(cageID as nvarchar(max))))+ltrim(rtrim(CAST(trayNo as nvarchar(max)))) as _unique
        from lf_transit_cage
    ) as dt
    group by dt._unique
    HAVING COUNT(dt._unique)>1
)
order by cageID,trayNo


texts: Simple palindrome function for single words

texts:
I have created a simple palindrome function that works only on single words. It is working as intended, but I feel that it definitely needs improvement.

def is_palindrome(w):

    if not isinstance(w, str):
        return "ERROR: input is not a string."

    elif len(w) > 2:
        if w[0] == w[-1]:
            while len(w) > 2:
                w = w[1:-1]
                is_palindrome(w)

        if len(w) == 1:
            return True

        else:
            return False

    else:
        return "ERROR: input contains two or less characters."

print is_palindrome('aibohphobia')
print is_palindrome('definitely')

Can you please review my function and post your suggestions?

Also, is it better to do w[:] = w[1:-1] instead of w = w[1:-1]?


texts: Data structure (Map-like implementation) in C++

texts:
I wrote this code to implement a data structure in C++. It works perfectly, however, I am looking to improve its efficiency. Also, I'm just getting started in C++ so using code to explain your suggestions would be incredibly helpful.

Here is the class that implements the data structure:

class key_value_sequences {
public:
// YOU SHOULD USE C++ CONTAINERS TO AVOID RAW POINTERS
// IF YOU DECIDE TO USE POINTERS, MAKE SURE THAT YOU MANAGE MEMORY PROPERLY

// IMPLEMENT ME: SHOULD RETURN SIZE OF A SEQUENCE FOR GIVEN KEY
// IF NO SEQUENCE EXISTS FOR A GIVEN KEY RETURN -1
int size(int key) const
{
    for( int i=0; i<pairs.size(); i++)
    {
        if( pairs[i][0] == key )
        {
            return pairs[i].size() - 1;
        }
    }
    return -1;
}

// IMPLEMENT ME: SHOULD RETURN POINTER TO A SEQUENCE FOR GIVEN KEY
// IF NO SEQUENCE EXISTS FOR A GIVEN KEY RETURN nullptr
const int* data(int key) const
{
    int length = size(key);

    if( length == -1 )
    return nullptr;

    for( int i=0; i<pairs.size(); i++)
    {
        if( pairs[i][0] == key )
        {
        return &pairs[i][1];
        }
    }
    return nullptr;
}

// IMPLEMENT ME: INSERT VALUE INTO A SEQUENCE IDENTIFIED BY GIVEN KEY
void insert(int key, int value)
{
    bool found = false;
    int pos = -1;

    for( int i=0; i<pairs.size(); i++)
    {
        if( pairs[i][0] == key )
        {
            found = true;
            pos = i;
        }
    }

    // insert if found in old vector,
    // else make a new vector

    if( found )
    {
        pairs[pos].push_back(value);
    }
    else
    {
        // insert new value set for the key
        std::vector<int> v;
        v.push_back(key);
        v.push_back(value);
        pairs.push_back(v);
    }
}
private:

// list is stored as a vector and
std::vector< std::vector<int> > pairs;

}; // class key_value_sequences


texts: Calculating a unique count within a rolling time window

texts:
I have a Pandas DataFrame that contains a row per member per day, expressing member interaction with a website. Members interact only on some days, each member is identified with an ID. Here is a simulated example:
import pandas as pd
import numpy as np

# Generate data.

ids = np.repeat(np.arange(100), np.random.randint(100, size = 100))

test = (
    pd.Series(
        ids,
        index = pd.Series(pd.date_range('2020-01-01', '2020-02-01').values).sample(ids.shape[0], replace = True)
    )
    .sort_index()
)

print(test.head())

Gives:
2020-01-01     4
2020-01-01    65
2020-01-01    95
2020-01-01    40
2020-01-01    88
dtype: int32

I'd like to calculate a unique count of members within a 7 day rolling window. After some experimentation and research on Stack Overflow (including Pandas rolling / groupby function), I arrived at an explicit loop and slicing.
# Calculate rolling 7 day unique count.

unique_count = {}

for k in test.index.unique():

    unique_count[k] = test.loc[k - pd.Timedelta('7 days'):k].nunique()

# Store as a dataframe and truncate to a minimum period.

unique_count = pd.DataFrame.from_dict(unique_count, orient = 'index', columns = ['7_day_unique_count']).iloc[7:]

print(unique_count.tail())

Gives:
            7_day_unique_count
2020-01-28                  98
2020-01-29                  98
2020-01-30                  98
2020-01-31                  97
2020-02-01                  97

This seems to work correctly and performs OK. But is it possible to do this without the explicit loop, such as with resample / groupby / rolling functions? If so, is that more efficient?


texts: Instance variables and do loops in ruby on rails

texts:
I find my self often doing things like this in Ruby on Rails:

@count = 0

LineItem.all.each do |item|
     if (item.cart_id == @cart.id)
       @count += item.quantity
     end
end

So many things look wrong with this, but I want to initialize my variable (@count) before the loop, and then be able to use it after I'm out of the loop.

Is there a better way to do this?


texts:
I have Tornado chat with export to Redis. Other scripts will export messages from Redis to MongoDB in the time. It works, but how is it written?

def tornadoredis_client():
    client = tornadoredis.Client(*configuration.REDIS_CONFIG)
    client.connect()
    return client

class PlansqTornadoChat(SockJSConnection):
    users = dict()
    timezones = dict()

    def on_open(self, info):
        self.authenticated = False
        self.timezone = None
        self.client = tornadoredis_client()

    @tornado.gen.engine
    def on_message(self, msg):
        data = proto.json_decode(msg)

        if data['type'] == 'auth':
            if bool(data.get('sid', None)):
                try:
                    int(data['sid'])  # check that sid is number, not word
                    self.user_sid = data['sid']
                    self.users[self.user_sid] = self
                    self.user_timezone = yield tornado.gen.Task(self.client.hget, 'timezones', self.user_sid)
                    self.timezones[self.user_sid] = self.user_timezone
                    self.authenticated = True
                    self.send(proto.json_encode({"body": "You was authorized", 'type': 'alert'}))
                except Exception, e:
                    logging.info("bad user's sid: " + str(data['sid']))
            else:
                self.send(proto.json_encode({"body": "You did not authorize", 'type': 'alert'}))
                self.close()

        elif data['type'] == 'message':
            if all([x in data for x in ['type', 'recipient', 'body', 'sender']]) \
                    and data['sender'] == self.user_sid:
                recipient = data['recipient']

                try:
                    int(recipient)  # check that sid is number, not word
                except Exception, e:
                    logging.info('sid of recipient is not number')
                    return

                self.thread = "_".join([str(x) for x in sorted([int(self.user_sid), int(recipient)])])

                # Save to Redis
                data_to_send = {
                    'thread_id': self.thread,
                    'sender': self.user_sid,
                    'recipient': recipient,
                    'body': data['body'],
                    'datetime': (datetime.now() - datetime(1970, 1, 1)).total_seconds()
                }

                yield tornado.gen.Task(self.client.sadd, "thread:" + self.thread, proto.json_encode(data_to_send))
                yield tornado.gen.Task(self.client.sadd, 'new_threads:all', "thread:" + self.thread)

                # Send to Socket
                data_to_send['type'] = 'message'

                # if user is online
                if recipient in self.users:
                    data_to_send['datetime'] = utc_to_localtime(data_to_send['datetime'], self.timezones[recipient])
                    self.users[recipient].send(proto.json_encode(data_to_send))

                data_to_send['datetime'] = utc_to_localtime(data_to_send['datetime'], self.user_timezone)
                self.send(proto.json_encode(data_to_send))

            else:
                logging.info('Bad request: ' + str(data))
        else:
            logging.info('Bad request: ' + str(data))

    def on_close(self):
        del self.users[self.user_sid]
        del self.timezones[self.user_sid]
        self.client.disconnect()

My configuration in configuration of main application (Flask + MongoDB). There only Redis configuration (host and port).

The rest of the code:

if __name__ == '__main__':
    logging.getLogger().setLevel(logging.DEBUG)
    logging.getLogger().setLevel(logging.INFO)

    EchoRouter = SockJSRouter(PlansqTornadoChat, '/socket')

    app = web.Application(EchoRouter.urls)
    app.listen(configuration.TORNADO_PORT)
    ioloop.IOLoop.instance().start()


texts:
I wrote a simple class in Python, which controls code invocation, in a multi-threaded environment, with the following logic:

The class' main method, named try_to_do, takes two function pointers as arguments: yes_we_can_fn and no_we_cannot_fn.

In any point in time, only a single thread can invoke code the function passed as yes_we_can_fn argument.

If a specific thread tries to invoke its code, but some other thread is already invoking its code, then the no_we_cannot_fn is invoked instead of yes_we_can_fn.

If there's an exception in the code being executed, it should raise to the calling context.

The code:

from threading import Lock

class MyController():
    def __init__(self):
        self.locker = Lock()
        self.is_active = False

    def try_to_do(self, yes_we_can_fn, no_we_cannot_fn):
        with self.locker:
            if self.is_active:
                we_can_do_it = False
            else:
                we_can_do_it = True
                self.is_active = True

        try:
            if we_can_do_it:
                yes_we_can_fn()
            else:
                no_we_cannot_fn()
        finally:
            if we_can_do_it:
                with self.locker:
                    self.is_active = False

Usage:

ctl = MyController()

def p1():
    from time import sleep
    print 'before sleep'
    sleep(2)
    print 'done'

def p2():
    print 'too busy, will try some other time'

ctl.try_to_do(p1, p2)

I'd like to get some reviews: thread safety (maybe I'm missing something?), coding style, etc.


texts: Can I further secure this blog post loading script?

texts:
The following code is developed around a mysql database where the posts table has 3 columns: post_ID, date and title.

config.php holds the values for $dsn, $username, $password, $options.

The content for each post is held in a directory format:

year/month/day/(database column)title/content.txt

By querying the database, I retrieve the date, which lets me order the posts (newest first).

$new_date_dir formats the date to serve as the directory for the include link $link.

$new_date_vis formats the date to serve as the date for the post.

The file is called using include within another page which uses exterior formatting.

 <?php include $_SERVER['DOCUMENT_ROOT']."/resources/config.php";

try {

    $dbh = new PDO($dsn, $username, $password, $options);

    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

} catch (PDOException $e) {

    echo 'Connection failed: ' . $e->getMessage();

}

function orderBlogPost($dbh) {
$statement = $dbh->prepare("
SELECT date, title
FROM post
ORDER BY UNIX_TIMESTAMP(date) DESC
LIMIT 0,5
");

$statement->execute();

    $statement->bindColumn('date', $date);

    $statement->bindColumn('title', $title);

    while ($row = $statement->fetch(PDO::FETCH_BOUND)) {

        $new_date_dir = date("Y-m-d",strtotime($date));

        $new_date_vis = date("d-m-Y",strtotime($date));

        $new_row = preg_replace("[-]", "/", $new_date_dir);

        $link = $_SERVER['DOCUMENT_ROOT']."/content/".$new_row."/".$title."/content.txt";

       echo "
".$title."".$new_date_vis."

";
       include "$link";
    }

}
orderBlogPost($dbh);

$dbh = null

?>

Whilst, this is outputting my desired result, I would like to know the following:

Is this procedural code basically secure/usable?
Can you suggest further reading on refactoring?
How would I benefit from using OOP in this scenario?


texts: Animated scrolling when intra-page links are clicked

texts:
I have the following code to get the ID of a   and go to their respective div
:

$('a[href=#certificados]').click(function(e){
    e.preventDefault();
    $("html, body").animate({ scrollTop: $('#certificados').offset().top }, 1000);
});

$('a[href=#team]').click(function(e){
    e.preventDefault();
    $("html, body").animate({ scrollTop: $('#team').offset().top }, 1000);
});

$('a[href=#house]').click(function(e){
    e.preventDefault();
    $("html, body").animate({ scrollTop: $('#house').offset().top }, 1000);
});

$('a[href=#contact]').click(function(e){
    e.preventDefault();
    $("html, body").animate({ scrollTop: $('#contact').offset().top }, 1000);
});

How could I optimize the code?


texts: Is there a better way to loop through records and call an api?

texts:
I have a CakePHP function here that gets info for a twitter screen_name from the twitter api and writes it to a mysql db. I'm just looking for feedback about other ways to accomplish this that might be more efficient with fewer loops maybe. The challenge is, there are many screen_names that need to be processed for a $reportid before the "report" is considered done. Any feedback is very much appreciated. thanks.

    function admin_wtfTwitter($reportid, $jobid) {

    $this->autoRender = false;

    //use if the api lags
    //set_time_limit(18000);

    //notify the log
    $this->admin_saveLog($reportid, $jobid, 'parsecrawlresults', 'Twitter wce started.');

    //get the twitter channels from reportdetails
    $channels = $this->Reportdetail->getChannel($reportid, 4);  //channel 4 = twitter
    foreach ($channels as $c) {

        //open reportdetail channel for writting
        $this->Reportdetail->create();
        $this->Reportdetail->set(array('id' => $c['Reportdetail']['id']));

        /*
        *   small_tag is null --> continue to next reportdetail record
        *   small_tag is bad
        *   small_tag is good but no data returned
        */
        if ($c['Reportdetail']['small_tag'] == null) { $this->Reportdetail->set(array('done' => -1, 'note' => 'small_tag null')); $this->Reportdetail->save(); continue; }

        $uri = 'http://api.twitter.com/1/users/show.json?screen_name=' . $c['Reportdetail']['small_tag'];
        $data = @file_get_contents($uri);
        if($data === false) { unset($data); $this->Reportdetail->set(array('done' => -1, 'note' => 'small_tag bad')); $this->Reportdetail->save(); continue; }
        $content = json_decode($data, true);

        //set fans, title username
        $this->Reportdetail->set(array(
            'fans' => $content['followers_count'],
            'title' => $content['description'],
            'username' => strtolower($content['screen_name'])
        ));

        //proceed with second api call****************************
        unset($uri);
        unset($data);
        unset($content);
        $i = 0;
        $e = 0;
        $postmonths = array();
        //LIMIT SET TO 1000 - CAN BE 3200 - https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline
        $uri = 'https://api.twitter.com/1/statuses/user_timeline.json?trim_user=true&exclude_replied=true&include_entities=true&include_rts=true&contributor_details=false&count=1000&screen_name=' . $c['Reportdetail']['small_tag'];
        $data = @file_get_contents($uri);
        if($data === false) { unset($data); $this->Reportdetail->set(array('done' => -1, 'note' => 'small_tag bad')); $this->Reportdetail->save(); continue; }
        $content = json_decode($data, true);

        //set engagement, posts
        foreach ($content as $t) {
            if (!empty($t['id_str'])) {
                $i++;
                $e = $e + $t['retweet_count'];
                array_push($postmonths, substr($t['created_at'], 4, 3)); //API resutls are: "created_at":"Fri Nov 30 00:38:34 +0000 2012
            }
        }
        $this->Reportdetail->set(array(
            'posts' => $i,
            'engagements' => $e
        ));

        //get and set frequency
        $freq = $this->admin_calcFrequency($i, count(array_unique($postmonths)));
        $this->Reportdetail->set(array('frequency' => $freq));

        //********************************************************

        //we're done with this one
        $this->Reportdetail->set(array('done' => 1));

        //commit the content to reportdetail record
        $this->Reportdetail->save();

    }

    //notify the log
    $this->admin_saveLog($reportid, $jobid, 'parsecrawlresults', 'Twitter wce completed.');
}


texts:
Selection Sort

  The selection sort algorithm sorts a list by finding the minimum
  element from the right unsorted part of the list and putting it at the
  left sorted part of the list. The algorithm maintains two sub-lists in
  a given input list.

  1) The sub-list which is already sorted.
  2) Remaining sub-list which
  is unsorted.

  In every iteration of selection sort, the minimum element from the
  unsorted sub-list is picked and moved to the sorted sub-list.

I've been trying to implement the Selection Sort algorithm using Python magic functions such as __iter__ and I'd appreciate it if you'd review the code for changes/improvements.

Code

"""
This class returns an ascending sorted integer list
for an input integer list using Selection Sort method.

Sorting:
- In-Place (space complexity O(1))
- Efficiency (time complexity O(N^2))
- Unstable Sort (Order of equal elements might change)

"""
class SelectionSort(object):
    """
    """
    def __init__(self, input_list:list)->list:
        self.input_list = input_list
        self.__iter__()

    def __iter__(self)->list:
        """
        Iterates through the list and swaps the min from the right side
        to sorted elements from the left side of the list.
        """

        # Is the length of the list
        input_length = len(self.input_list)

        # Iterates through the list to do the swapping
        for element_index in range(input_length - 1):

            min_index = element_index

            # Iterates through the list to find the min index
            for finder_index in range(element_index+1, input_length):
                if self.input_list[min_index] > self.input_list[finder_index]:
                    min_index = finder_index

            # Swaps the min value with the pointer value
            if element_index is not min_index:
                self.input_list[element_index], self.input_list[min_index] = self.input_list[min_index], self.input_list[element_index]

        print(self.input_list)
        return self.input_list

SelectionSort([10, 4, 82, 9, 23, -30, -45, -93, 23, 23, 23, 0, -1])

Solution from Geeks by Geeks

I'm not so sure about Sorting Stability, it says the following implementation is not stable. However, Selection Sort can be made stable:

import sys
A = [64, 25, 12, 22, 11]

for i in range(len(A)):

    min_index = i
    for j in range(i+1, len(A)):
        if A[min_index] > A[j]:
            min_index = j

    A[i], A[min_index] = A[min_index], A[i]

for i in range(len(A)):
    print("%d" %A[i])

Reference

Selection Sort (Geeks for Geeks)
Selection Sort (Wiki)


texts:
I wrote this function today that shift the values of a dictionnary:

def shift_dict(dic, shift):
    # Save the dic length
    dic_len = len(dic)

    # Reduce the shift to the dic length
    shift = shift % dic_len

    # convert the dict to a tupple to reference by index
    list_dic = [(k,v) for k, v in dic.iteritems()]

    # Create the new shifted dict
    shifted = {
        list_dic[x][0]: list_dic[ (x - shift) % dic_len ][1]
        for x in xrange(dic_len)
    }

    return shifted

The idea is to keep the keys but to assign the nth - shift value to it.

A few examples:

dic = {
    'a':10,
    'b':2,
    'c':30,
    'd':4,
    'e':50,
    'f':6,
}
print dic
print shift_dict(dic, 1)
print shift_dict(dic, 4)
print shift_dict(dic, -2)

Result:

{'a': 10, 'c': 30, 'b': 2, 'e': 50, 'd': 4, 'f': 6}
{'a': 6, 'c': 10, 'b': 30, 'e': 2, 'd': 50, 'f': 4}
{'a': 2, 'c': 50, 'b': 4, 'e': 6, 'd': 10, 'f': 30}
{'a': 30, 'c': 2, 'b': 50, 'e': 4, 'd': 6, 'f': 10}

I find that the "create a list and then another dictionnary" is not very effective, but I couldn't think of another way to do it.

Any thoughts?


texts:
How can I write this PHP code better? It puts together an SQL query string from user input. The task is to return search results based on one or more text fields. The user can determine if partial matches are allowed.

$compop = $allowpartial ? "LIKE" : "="; // use LIKE for wildcard matching
$any = $allowpartial ? "%" : ""; // SQL uses % instead of * as wildcard

$namecondstr    = ($name === "")    ? "TRUE" : ("Name    $compop '$any$name$any'");
$citycondstr    = ($city === "")    ? "TRUE" : ("City    $compop '$any$city$any'");
$itemnocondstr  = ($itemno === "")  ? "TRUE" : ("ItemNo  $compop '$any$itemno$any'");
$ordernocondstr = ($orderno === "") ? "TRUE" : ("OrderNo $compop '$any$orderno$any'");
$serialcondstr  = ($serial === "")  ? "TRUE" : ("Serial  $compop '$any$serial$any'");

$sortstr = ($name !== "") ? "Name" :
           (($city !== "") ? "City" :
           (($itemno !== "") ? "ItemNo" :
           (($orderno !== "") ? "OrderNo" :
                                    "Serial")));

$query = "SELECT * From Licenses
          LEFT JOIN Items
            ON Licenses.LicenseID = Items.LicenseID
          WHERE $namecondstr
            AND $citycondstr
            AND $itemnocondstr
            AND $ordernocondstr
            AND $serialcondstr
          ORDER BY $sortstr, Licenses.LicenseID";


texts:
I saw How to wait current thread execution and execute another thread completedly? on StackOverflow, and I answered it there, but would like more eyes on the code I posted.
The requirement is to pause one thread whilst a different thread is running.
Could you please review and comment on this implementation?
public class App {

    public static void main(String[] args) {
        Thread1 thread1 = new Thread1();
        Thread2 thread2 = new Thread2();

        try {
            thread1.start();
            Thread.sleep(500);
            synchronized (thread1) {
                thread1.waiting = true;
                thread2.start();
                thread2.join();
                thread1.waiting = false;
                thread1.notify();
            }
        } catch (Exception e) {
            //TODO actually handle exception
        }
    }
}

Class Thread1.java
public class Thread1 extends Thread {
    boolean waiting = false;

    public void run() {
        testFun1();
    }

    public void testFun1() {
        for (int i = 1; i < 10; i++) {
            synchronized (this) {
                while (waiting) {
                    try {
                        wait();
                    } catch (Exception e) {
                        //TODO Handle exception
                    }
                }
            }
            try {
                Thread.sleep(100);
                System.out.println("From testFun1() = " + i);
            } catch (Exception e) {
                //TODO Handle exception

            }
        }
    }
}

Class Thread2.java
public class Thread2 extends Thread {

    public void run() {
        testFun2();
    }

    public void testFun2() {
        try {
            for (int i = 20; i <= 25; i++) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    //catch
                }
                System.out.println("From testFun2() = " + i);
            }

        } catch (Exception e) {
            //TODO do something
        }

    }
}

Output:
C:\Java\jdk1.6.0_25\bin\java -Didea.launcher.port=7543 "-Didea.launcher.bin.path=C:\Program Files\JetBrains\IntelliJ IDEA 10.5.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Java\jdk1.6.0_25\jre\lib\alt-rt.jar;C:\Java\jdk1.6.0_25\jre\lib\alt-string.jar;C:\Java\jdk1.6.0_25\jre\lib\charsets.jar;C:\Java\jdk1.6.0_25\jre\lib\deploy.jar;C:\Java\jdk1.6.0_25\jre\lib\javaws.jar;C:\Java\jdk1.6.0_25\jre\lib\jce.jar;C:\Java\jdk1.6.0_25\jre\lib\jsse.jar;C:\Java\jdk1.6.0_25\jre\lib\management-agent.jar;C:\Java\jdk1.6.0_25\jre\lib\plugin.jar;C:\Java\jdk1.6.0_25\jre\lib\resources.jar;C:\Java\jdk1.6.0_25\jre\lib\rt.jar;C:\Java\jdk1.6.0_25\jre\lib\ext\dnsns.jar;C:\Java\jdk1.6.0_25\jre\lib\ext\localedata.jar;C:\Java\jdk1.6.0_25\jre\lib\ext\sunjce_provider.jar;C:\Java\jdk1.6.0_25\jre\lib\ext\sunmscapi.jar;C:\Java\jdk1.6.0_25\jre\lib\ext\sunpkcs11.jar;C:\IdeaProjects\PracticeModule\target\classes;C:\Program Files\JetBrains\IntelliJ IDEA 10.5.1\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain jmhp.core.App
From testFun1() = 1
From testFun1() = 2
From testFun1() = 3
From testFun1() = 4
From testFun1() = 5
From testFun2() = 20
From testFun2() = 21
From testFun2() = 22
From testFun2() = 23
From testFun2() = 24
From testFun2() = 25
From testFun1() = 6
From testFun1() = 7
From testFun1() = 8
From testFun1() = 9

Thanks!


texts:
I have written a program to print Brown numbers from Brocard's problem.
I want to improve the code.

  Brocard's problem is a problem in mathematics that asks to find
  integer values of n and m for which

n ! + 1 = m^2

  where n! is the factorial.

  Pairs of the numbers (n, m) that solve Brocard's problem are called
  Brown numbers. There are only three known pairs of Brown numbers:

(4,5), (5,11), and (7,71).

#include <iostream>

unsigned int factorial(unsigned int num)
{
  unsigned int result = 1;
  for(unsigned int i = 1; i <= num; i++)
  {
    result = result*i;
  }
  return result;
}

void print_brown_numbers(int limit)
{
  for(unsigned int i = 2; i <= limit; i++)
  {
    for(unsigned int j = 1; j < i; j++)
    {
      if((i * i) == (factorial(j) + 1))
      {
        std::cout << i << " " << j << " are brown numbers\n";
      }
    }
  }
}

int main()
{
  unsigned int max_limit;
  std::cout << "Enter the maximum limit for you want to test \n";
  std::cin >> max_limit;
  print_brown_numbers(max_limit);
}


texts:
I am building a classifieds website here in Portugal, and I'm now in the security phase. So until now, I already made what I think is a good measure against SQL injection:

$firstname = chunk_split(mysql_real_escape_string($_POST[firstname]),1,'.');
$lastname = chunk_split(mysql_real_escape_string($_POST[lastname]),1,'.');
$email = chunk_split(mysql_real_escape_string($_POST[email]),1,'.');

etc...

This will save valid and invalid emails, for example, like this:

good email: u.s.e.r.@.e.m.a.i.l.h.o.s.t...c.o.m.
bad email:

Example of text in the email field:

Y';
 UPDATE table
  SET email = 'hacker@ymail.com'
  WHERE email = 'joe@ymail.com';

and after stored in the database:

Y.\.'.;. .U.P.D.A.T.E. .t.a.b.l.e. .S.E.T. .e.m.a.i.l. .=. .\.'.h.a.c.k.e.r.@.y.m.a.i.l...c.o.m.\'. .W.H.E.R.E. .e.m.a.i.l. .=. .\.'.j.o.e.@.y.m.a.i.l...c.o.m.\.'.;.

So, I don't care if the size of the stored string is bigger because I think this is a solid approach and a very fast one. And if I had to use other ways, the scripts would probably consume the same time that is here exchanged by size.

But now I have the serious problem of XSS attacks. I'm trying to prevent only attacks based on text, not images or JavaScript because I will not have untrusted data there.

The solution I find is based on the last example, and looks like this:

$str = "";
$ad_title = chunk_split($str,1,".");

So when the page loads the users will see the inserted text and the alert will not work:

This is the source code:

<.s.c.r.i.p.t.>.a.l.e.r.t.(.'.f.a.b.i.o.'.).;.<./.s.c.r.i.p.t.>.

Without being concerned about all the code it produces, my question is: is this enough or actually safe?


texts:
I'm posting a solution for LeetCode's "Non-decreasing Array". If you'd like to review, please do. Thank you!
Problem
Given an array nums with n integers, your task is to check if it could become non-decreasing by modifying at most 1 element.
We define an array is non-decreasing if nums[i] <= nums[i + 1] holds for every i (0-based) such that (0 <= i <= n - 2).
Example 1:

Input: nums = [4,2,3]
Output: true
Explanation: You could modify the first 4 to 1 to get a non-decreasing array.

Example 2:

Input: nums = [4,2,1]
Output: false
Explanation: You can't get a non-decreasing array by modify at most one element.

Constraints:

1 <= n <= 10 ^ 4
-10 ^ 5 <= nums[i] <= 10 ^ 5

Code
// Most of headers are already included;
// Can be removed;
#include <iostream>
#include <cstdint>
#include <vector>

// The following block might slightly improve the execution time;
// Can be removed;
static const auto __optimize__ = []() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    return 0;
}();

struct Solution {
    using ValueType = std::int_fast32_t;
    static const bool checkPossibility(
        std::vector<int>& nums
    ) {

        if (std::size(nums) < 3) {
            return true;
        }

        ValueType max_changes = 0;

        for (ValueType index = 1; max_changes < 2 && index < std::size(nums); ++index) {
            if (nums[index - 1] > nums[index]) {
                ++max_changes;

                if (index - 2 < 0 || nums[index - 2] <= nums[index]) {
                    nums[index - 1] = nums[index];

                } else {
                    nums[index] = nums[index - 1];
                }
            }
        }

        return max_changes < 2;
    }
};

int main() {
    std::vector<int> nums = {3, 4, 2, 3};
    std::cout << std::to_string(Solution().checkPossibility(nums) == false) << "\n";
    return 0;
}


texts: Java 8 unit test for third-party data feeds

texts:
Use Case

I have a service that does processing of records from multiple third-party feeds. The steps are generally identical for each feed, but each feed is populated in a different location on an aggregate feed. I decided to write a generic test that would allow testing of all this processing to keep things DRY.

Test

The test basically tests whether the processor method in our service properly adapts and adds the incoming record to the appropriate aggregate list.

@Mock AggregateFeed dataFeed; // contains lists of POJOs
@Mock Exchange exchange; // camel mock.. not really important

@Mock SportsAdapter sportsAdapter; // used to convert string to POJO
@Mock BusinessAdapter businessAdapter; // used to convert string to POJO

FeedService fixture = new FeedService();

...

private <T1, T2> void shouldProcessRecord(BiConsumer<T1, Exchange> processor,
        T1 incomingRecord, T2 fakeAdaptee, Function<T1, T2> mockAdapter,
        Function<AggregateFeed, List<T2>> list) {
    List<T2> fakeList = Lists.newArrayList();

    when(mockAdapter.apply(incomingRecord)).thenReturn(fakeAdaptee);
    when(list.apply(dataFeed)).thenReturn(fakeList);

    processor.accept(incomingRecord, exchange);

    assertEquals(1, fakeList.size());
    assertSame(fakeAdaptee, fakeList.get(0));
}

@Test
public void shouldProcessSports() {
    String testRecordFeed = constructSportsRecordFeed();
    SportsRecord fakeSportsRecord = new SportsRecord();
    shouldProcessRecord(fixture::addSportsRecord, testRecordFeed,
        fakeSportsRecord, sportsAdapter::fromFeed, AggregateFeed::getSports);
}

@Test
public void shouldProcessBusiness() {
    String testRecordFeed = constructBusinessRecordFeed();
    BusinessRecord fakeBusinessRecord = new BusinessRecord();
    shouldProcessRecord(fixture::addBusinessRecord, testRecordFeed,
        fakeBusinessRecord, businessAdapter::fromFeed, AggregateFeed::getBusiness);
}

// builds up a csv delimited string that represents a sports article
private String constructSportsRecordFeed() {
    return new StringBuilder().append( ... ).toString();
}

// builds up a csv delimited string that represents a business article
private String constructBusinessRecordFeed() {
    return new StringBuilder().append( ... ).toString();
}

Concerns

The main problem I have doing things this way is that the signature just doesn't look natural. If another developer comes to create a test, they'll have to dig through the code to figure out how to use this test. On the other hand, it's much easier to do a copy and paste of the test and change around the variables, but that doesn't feel right either because it's not DRY.

Questions

How can this generic test be improved to be easier to use?
Since this is a test, should a copy-and-paste methodology be better since it's more direct on what the test does?
Any other advice/comments would be greatly appreciated.


texts:
I recently wrote a python script to convert multiple files EOL's from Unix to DOS and vice versa.

I am looking for tips to improve my code or if there is a better way of doing something that I have missed.

#!/usr/bin/env python3

import sys

def main():
    command, *filenames = sys.argv[1:]
    valid_commands = ['-d', '-u']
    sys.tracebacklimit = None

    if not command in valid_commands:
        error = """'{command}'
        Provide the following arguments -u|d file [file2] [file3] ...
        flags:
            -u : converts DOS to UNIX
            -d : converts UNIX to DOS
        example command:
            ./eol -u foo.py bar.py""".format(command=command)

        raise ValueError(error)
        sys.exit(1)

    if filenames:
        convert(filenames, command)
    else:
        print("> no files to convert")

def convert(files, command):
    for file in files:
        text = open(file, 'r').read()

        with open(file, 'w') as current:
            if command == '-u':
                format = 'UNIX'
                current.write(text.replace('\r\n', '\n'))
            elif command == '-d':
                format = 'DOS'
                current.write(text.replace('\n', '\r\n'))

        print("> converting file {filename} to {format} ...".format(
              filename=file, format=format))

if __name__ == "__main__":
    main()


texts:
Okay... here's the beast:

SELECT
     SUBSTRING(DischDate, 7, 4) + SUBSTRING(DischDate, 1, 2) as YYYYMM
    ,Type
    ,SubType
    ,Diags
    ,Count(*)        as Count
    ,SUM(Charges)    as Charges
    ,SUM(Payments)    as Payments
FROM
    (
        SELECT DISTINCT
             ID
            ,Diags
        FROM
           (SELECT VisitID as ID
                    ,DX01 ,DX11 ,DX21, DX31
                    ,DX02 ,DX12 ,DX22, DX32
                    ,DX03 ,DX13 ,DX23, DX33
                    ,DX04 ,DX14 ,DX24, DX34
                    ,DX05 ,DX15 ,DX25, DX35
                    ,DX06 ,DX16 ,DX26, DX36
                    ,DX07 ,DX17 ,DX27, DX37
                    ,DX08 ,DX18 ,DX28, DX38
                    ,DX09 ,DX19 ,DX29, DX39
                    ,DX10 ,DX20 ,DX30, DX40
               FROM [AGH00]...[20110128 - AGH00#TXT]) p
            UNPIVOT
               (Diags FOR DX IN
                    (DX01 ,DX11  ,DX21, DX31
                    ,DX02 ,DX12  ,DX22, DX32
                    ,DX03 ,DX13  ,DX23, DX33
                    ,DX04 ,DX14  ,DX24, DX34
                    ,DX05 ,DX15  ,DX25, DX35
                    ,DX06 ,DX16  ,DX26, DX36
                    ,DX07 ,DX17  ,DX27, DX37
                    ,DX08 ,DX18  ,DX28, DX38
                    ,DX09 ,DX19  ,DX29, DX39
                    ,DX10 ,DX20  ,DX30, DX40)
            )AS unpvt
        ) as DIAGS
    LEFT JOIN [AGH00]...[20110128 - AGH00#TXT] as A0 on DIAGS.ID = A0.VisitID
    LEFT JOIN ( SELECT VisitID, Sum(ChargsAmt) as Charges
                FROM [AGH00]...[20110128 - AGH00Chg#TXT]
                Group By VisitID ) as AC on A0.VisitID = AC.VisitID
    LEFT JOIN ( SELECT VisitID, Sum(Pmt) as Payments
                FROM [AGH00]...[20110128 - AGH00Pmt#TXT]
                Group By VisitID ) as AP on A0.VisitID = AP.VisitID
Group By
     SUBSTRING(DischDate, 7, 4) + SUBSTRING(DischDate, 1, 2)
    ,Type
    ,SubType
    ,Diags
Order By
     SUBSTRING(DischDate, 7, 4) + SUBSTRING(DischDate, 1, 2)
    ,Type
    ,SubType
    ,Diags

Sample output:

YYYYMM Type SubType Diags # Charges    Payments
201004 I    SPEC    5185  3 224086.60  222986.60
200910 I    MONT    73390 2 7573.42    6984.13
200910 O    AHCW    463   4 578.00     578.00
200910 I    MEDI    2766  2 67444.39   67444.39

Notes:

SQL2005 LinkedServer to a TextFile with a backing ini for field descriptions.
DischDate is "mm/dd/yyyy" text field. I want YYYYMM for grouping (as well as other fields)

First "table" is a list generated via Unpivot that basically generates a list of VisitID's and unique Diags
Left Join main customer data by "VisitID"
Left Join Charges Data (Grouped to get individual charges grouped by "customer" aka VisitID)
Left Join Payment Data (same as last)
Group by/Order By/Select Date YYYYMMDD, Type, Subtype, Diags... and grouped Count/Sums

More information is in this SO question where someone pointed me to the unpivot option. Including a semi-confused explanation of what I want to accomplish (basically grouping information for a couple similar reports).

Just wondering if there is a more graceful way, and if anyone see potential errors.


texts:
This is yet another take in Python for the infamous Sieve of Eratosthenes. I am a beginner in Python so any criticism is highly appreciated.

# Find primes until this integer.. Modify to your taste..
upper_bound = 1000

# Pass a list of integers and a prime number,
# will return you a list where numbers divisible by the prime you passed are removed.
def remove_non_primes(list_of_ints, prime_to_check_against):
    multiplier = 1
    while prime_to_check_against * multiplier < list_of_ints[-1]:
        multiplier += 1
        # Should be quite fast since list is sorted.
        if prime_to_check_against * multiplier in list_of_ints:
            list_of_ints.remove(prime_to_check_against * multiplier)
    return list_of_ints

ints = list(range(2, upper_bound)) # Generate initial list of integers

# Initial prime for bootstrap
prime = 2
# Keep track of iteration count, so we know the next prime to work with in each iteration.
iteration_count = 0

while True:
    # Use length of list to determine we removed everything we could by
    # checking this length against returning lists length.
    original_list_length = len(ints)
    # Do the magic, remove all non primes found by parameter prime.
    ints = remove_non_primes(ints, prime)
    # List was not modified.. Nothing to remove.
    if original_list_length == len(ints):
        break
    iteration_count += 1
    prime = ints[iteration_count]

print(ints)

Are there any modifications that will dramatically increase the performance?


texts: Stack implementation using a linked list

texts:
To understand the concept, I implemented the stack operations using a linked list. Please review the code and tell me your suggestions.

Node.java

public class Node {

public int data;
public Node next;

public Node(int data) {
    this.data = data;
}

public void displayNode() {
    System.out.print(data);
    System.out.print("  ");

 }
}

LinkList.java

public class LinkList {

private Node first = null;

public void insertFirst(int data) {
    Node n = new Node(data);
    n.next = first;
    first = n;
}

public Node deleteFirst() {
    Node temp = first;
    first = first.next;
    return temp;
}

public void displayList() {
    Node current = first;
    while (current != null) {
        current.displayNode();
        current = current.next;
    }
}

public boolean isEmpty() {
    return (first == null);
  }
}

LinkListStack.java

public class LinkListStack {

LinkList li = new LinkList();

public void push(int data) {
    li.insertFirst(data);
}

public void pop() {
    while(!li.isEmpty()){
    li.deleteFirst();
    }
}

public void displayStack() {
    System.out.println("  ");
    li.displayList();
  }
}

LinkListStackDemo.java

public class LinkListStackDemo {

public static void main(String[] args) {
    LinkListStack st = new LinkListStack();

    st.push(50);
    st.push(70);
    st.push(190);
    st.displayStack();
    st.pop();
    st.displayStack();

  }
 }


texts: How can we optimizing Java BigInteger operations?

texts:
Problem
Reduce the number to 1 in a minimal number of steps, following the rules:

If the number is even, divide it by 2.
Otherwise, increment or decrement it by 1.

Limit: s can be up to 309 digits long.
Solution
public static int solution(String s) {
    BigInteger n = new BigInteger(s);
    BigInteger one = BigInteger.ONE;
    BigInteger two = BigInteger.TWO;
    BigInteger four = new BigInteger("4");
    int count = 0;

    while (n.compareTo(one) == 1) {
        count++;

        if( n.and(one).intValue() == 0 )
            n = n.divide(two);
        else if ( n.intValue() == 3 || n.mod(four).intValue() == 1)
            n = n.subtract(one);
        else
            n = n.add(one);
    }

    return count;
}

Is there a better way to do things binary-operations-wise for this problem?
At the moment the choice I am making among these functions:

use n.byteValue() to compare with the integer,
or use function n.equals().

What more can we improve?

Follow-up question posted here


texts: Program to check if a date is valid or not

texts:
I am a novice programmer and am currently learning Python programming by myself through a book. I stumbled upon a question in a chapter and it took me 2 days to figure out a solution for it. Although, it may not be an efficient code for the problem, I would be grateful if you guys could review it. Just to let everyone know of my Knowledge level on programming, I just have basic knowledge of functions, sequences and decision structure.

  Write a program to check if a date is valid or not? The date should be
  in the mm/dd/yyyy format.

  Pseudocode:

  Define 3 functions to check the validity of month,day and year, if valid return True for each function.
  input a string date separated by a "/" and assign it to a date variable.
  split the date variable to 3 separate variables.
  check if the 3 variables are True or not.
  Print if the date is valid or not.

I didn't take into account leap years where February has 29 days and also assumed the year to start at 1 A.D. to current years.

def monthcheck(month):
        if month > 0 and month <= 12: ## If month is between 1 and 12, return True.
            return True
        else:
            return False

def daycheck(month,day):
    monthlist1 = [1,3,5,7,8,10,12] ## monthlist for months with 31 days.
    monthlist2 = [4,6,9,11] ## monthlist for months with 30 days.
    monthlist3 = 2 ## month with month with 28 days.

    for mon in monthlist1: ## iterate through monthlist1.
        if month == mon: ## Check if the parameter month equals to any month with 31 days.
            if day >=1 and day <= 31: ## If the parameter day is between 1 and 31, return True.
                return True
            else:
                return False

    for mon in monthlist2: ## iterate through the monthlist with 30 days.
        if month == mon: ## check if the parameter month equals to any month with 30 days.
            if day >= 1 and day <= 30: ## if the parameter day is between 1 and 30,return True.
                return True
            else:
                return False

    if month == monthlist3: ## check if parameter month equals month with 28 days.
        if day >=1 and day <= 28: ## if the parameter day is between 1 and 28,return True.
            return True
        else:
            return False

def yearcheck(year):
    if len(year) >= 1 and len(year) <= 4: ## Check if year has between 1 to 4 numbers and return True.
        return True
    else:
        return False

def main():
    date = str(input("Enter the date in mm/dd/yyyy format: ")) ## Input date in the given format.

    month,day,year = date.split("/") ## split the date into 3 separate variables.

    monthvalidity = monthcheck(int(month))

    dayvalidity = daycheck(int(month),int(day))

    yearvalidity = yearcheck(year)

    if monthvalidity == True and dayvalidity == True and yearvalidity == True: ## check if all 3 variables are valid or True
        print("The date {0} is valid.".format(date))

    else:
        print("The date {0} is invalid.".format(date))

main()


texts:
I'm a beginner in Python but I have tried to implement K-means algorithm in python and it's working... but it's too slow... Instead of few seconds I can spend hours to finish it and I don't know why... something it's wrong... Could anyone of you give me some advice please?
Code:
import math
from random import randint
from copy import deepcopy

from chart import chart_centroids, save_image

centroid = dict(x=0, y=0, points_x=[], points_y=[])

list_centroids = []
x = []
y = []
k = 0

def distance(x1, y1, x2, y2):
    x = (int(x1) - int(x2)) ** 2
    y = (int(y1) - int(y2)) ** 2
    sum = x + y
    sqr = math.sqrt(sum)
    return sqr

def generate_centroids(range_x, range_y):
    list_centroids.clear()
    k = 3
    for i in range(0, k):
        centroid["x"] = randint(1, range_x)
        centroid["y"] = randint(1, range_y)
        list_centroids.append(deepcopy(centroid))

def choose_points_for_centroids(x, y):
    for i in range(len(list_centroids)):
        list_centroids[i]["points_x"].clear()
        list_centroids[i]["points_y"].clear()

    distances = []
    for j in range(len(x)):
        for i in range(len(list_centroids)):
            dist = distance(x[j], y[j], list_centroids[i]["x"], list_centroids[i]["y"])
            distances.append(dist)

        minim = min(float(s) for s in distances)
        index = distances.index(minim)
        list_centroids[index]["points_x"].append(x[j])
        list_centroids[index]["points_y"].append(y[j])
        distances.clear()

def move_centroids():
    sum_x = 0
    sum_y = 0

    for cent in list_centroids:
        for j in range(len(cent["points_x"])):
            sum_x += cent["points_x"][j]
            sum_y += cent["points_y"][j]

        if len(cent["points_x"]) > 0 and len(cent["points_y"]) > 0:
            avg_x = sum_x / len(cent["points_x"])
            avg_y = sum_y / len(cent["points_y"])
            cent["x"] = avg_x
            cent["y"] = avg_y

def run():
    generate_centroids(300, 300)
    read_file("input.txt")

    tmp_x = []
    tmp_y = []

    checkers = []

    while_end = False

    while True:
        if not while_end:
            choose_points_for_centroids(x, y)
            move_centroids()

            for cent in list_centroids:
                tmp_x.append(cent["x"])
                tmp_y.append(cent["y"])

            choose_points_for_centroids(x, y)
            move_centroids()

            for i in range(len(list_centroids)):
                if tmp_x[i] == list_centroids[i]["x"] and tmp_y[i] == list_centroids[i]["y"]:
                    checkers.append(True)
                else:
                    checkers.append(False)

            for checker in checkers:
                if not checker:
                    tmp_x.clear()
                    tmp_y.clear()
                    break
                else:
                    while_end = True
        else:
            break

        for i in range(len(list_centroids)):
            chart_centroids(list_centroids[i], i)
        save_image()

def read_file(name):
    lines = [line.rstrip('\n') for line in open('../generate_file/' + name)]

    global x
    global y
    zone = []

    for index in range(5):
        x.append(int(lines[index].split()[0]))
        y.append(int(lines[index].split()[1]))
        zone.append(int(lines[index].split()[2]))

run()

The input file it's looks something like...
100 52 2
440 100 3
10 200 1
...

Note: The code works in both Python 2 and Python 3.


texts: Get persons full name from their First and Surname

texts:
My Person class has two properties; name and surname, and a function that returns the first name and the surname together.

Is there anything that can be done better? Anything that is done poorly?

public class Person
{
    private string Firstname { get; set; }
    private string Surname { get; set; }

    public Person(string firstname, string surname)
    {
        this.Firstname = firstname;
        this.Surname = surname;

    }
    public string GetFullName()
    {
        string fullname = String.Format("Firstname is: {0}, Surname is {1}", Firstname, Surname);
        return fullname;
    }
}


texts: Implementing the stochastic gradient descent algorithm of the softmax regression with only NumPy

texts:
I am implementing the stochastic gradient descent algorithm. I think there is plenty of room for improvement.

def array2onehot(X_shape, array, start=1):
    """
    transfer a column to a matrix with each row being a onehot
    note that the array index defaults to start with 1 rather than 0
    """
    array += 1 - start if start != 1 else 0
    onehot = np.zeros(X_shape)
    onehot[np.arange(X_shape[0]), array-1] = 1
    return onehot

def stochastic_gradient_descent(self, batch, Y):
    """
    the batch contains both the X's and targets
    Y is the output produced by the forward process using the X's in the batch
    """
    ones = np.ones(batch.shape[0])
    # add the bias to the X's
    X = np.column_stack(ones, batch[:, :-1])
    T = array2onehot(X.shape, batch[:, -1])
    m = self._batch_size
    # batch size is the size of the mini batch

    count = np.zeros(self._class_size)
    # class size is the amount of classes to predict
    delta = np.zeros(self._coefficients.shape)
    for i in range(m):
        k = batch[i, -1] - 1
        count[k] += 1
        delta[:, k] += X[i] * sum(np.multiply(T[i], Y[i]))
    tmp = self._coefficients
    for i in count:
        if i == 0:
            tmp[:, i] = 0
    delta = np.nan_to_num(delta / count) - 2 * self._penalty_rate * tmp
    self._coefficients -= delta


texts: Check if we can form a word across words in a sentence

texts:
I've come across a simple challenge: given a phrase, check if we can form the given word vertically by stacking words in rows. For example, we can form the word "boom" from this phrase:

   every breath
            you take
every move
      you make

My Python code:

def can_spell(s, w):
    words = s.split()
    words_iter = iter(words)

    for char in w:
        try:
            next_word = next(words_iter)
            while char not in next_word:
                next_word = next(words_iter)
        except StopIteration:
            return False
    return True

print(can_spell('every breath you take every move you make', 'boom'))  # True

Is this the most efficient way to do it?