Change all pixels that lie in a given color range to a new color This is what I have so far but it's not perfect... Any input would be very helpful!

def picture_reset_pixels(filename, from_color, to_color, target_color):
    img = Image.open(filename)
    pixels = list(img.getdata())
    red_list=[]
    for i in pixels:
        R= i[0]
        G=i[1]
        B=i[2]

        if R >= from_color[0] and R<= to_color[0] and G >=from_color[1]
                and G<= to_color[1] and B >= from_color[2] and B<=
                to_color[2]:
                    red_list.append(target_color)
        else:
            red_list.append((round(R),round(G),round(B)))

    red_image = Image.new(img.mode,img.size)
    red_image.putdata(red_list)
    return red_image

Improving the general implementation

Generally we want to avoid appending to a list too many times if we can help it. This is because lists are dynamic arrays, so appends can be unnecessarily expensive (though the dynamic part helps with the efficiency). Instead, when we know the size of the input set (i.e. the number of pixels in the image), we can take advantage of that information. But in this case, that will be unnecessary because Image provides us with some handy helper methods to handle this very situation:

from PIL import Image

def picture_reset_pixels(filename, from_color, to_color, target_color):
    img = Image.open(filename)
    pixels = img.load()
    for i in range(img.size[0]):
        for j in range(img.size[1]):
            if all(from_color[k] <= pixels[i, j][k] <= to_color[k] for k in range(3)):
                pixels[i, j] = target_color + (255,)
    return img

Assigning pixels to img.load() allows us to take advantage of directly editing the image. Since copying the image into another list was unnecessary originally (to clarify, the img variable doesn't affect the file on disk that img originated from, i.e. the file at location filename), editing directly is the best way.

Edit 2: Looking at it again, I think @Reinderien's point about clarity has merit because range has an exclusive upper bound, which may be confusing because it is asymmetric relative to both of its arguments. I have edited to use the comparison operators <= and >= instead of the range check I mention ahead. The caveat only I have is that using the in range form is more similar to how I think a dedicated ColorRange object (mentioned ahead in the section Going beyond the current form) should handle a range check, since it's not necessary to expose the object's implementation details (though internally, it could still use comparison operators for the check).

Another general improvement I've taken advantage of in this implementation is using range to check if each of the RGB values of a pixel are between the two pixel boundaries of the input arguments. all() serves the purpose that the and statements in the original version served: it requires that all the pixels compared in the iterator pixels[i, j][k] in range(from_color[k], to_color[k] + 1) for k in range(3) are in the required range for the condition to be true.

Edit 1: the concern was raised in comments that testing for in range(from_color[k], to_color[k] + 1) would be extremely inefficient. However, this is mistaken and arises from a misunderstanding of how Python 3.x implement the range object. Checking if a number is in a range is a constant time operation. I would recommend reading this answer to understand Python 3.x's range implementation.

Going beyond the current form

While I've shown you a way to use your current parameter requirements in a more efficient implementation, I think there are some improvements you can make so picture_reset_pixels has more functionality. I think combining from_color, to_color, and target_color into a single dict parameter color_replacements could make this more versatile (where the key-value pairs would derive something like {(from_color, to_color): target_color}; you could even create a custom ColorRange class to encapsulate the idea of a range of colors).

There's also another issue I find with your current implementation: it can only support certain image types. I tried your algorithm on a GIF file and it failed because the pixels in a GIF file are ints, not four-tuples like PNGs. You also do not support the fourth alpha channel of PNG, because your pixel range bounds are only 3-tuples. You might want to consider investigating that further if you want to support multiple image types.


Extending Object.prototype in JavaScript I find the default Javascript extremely poor on useful functions. There are some nice libraries, but somehow I always need something I can't find. Currently, I'd need a method removing all matching properties from an object. And I really mean removing, not creating a filtered copy. I've found similar methods in both jquery and underscore, but no exact match.

That's a long introduction, why I want to give it a try. As both $ and _ are taken, I'm extending the Object.prototype which should be fine. So this is the code:

function removeMatching(predicate) {
    for (var key in this) {
        if (!Object.prototype.hasOwnProperty.call(this, key)) continue;
        if (predicate(key, this[key])) delete this[key];
    };
};

Object.defineProperty(Object.prototype, 'removeMatching', {
    value: removeMatching,
    writable: false,
    configurable: false,
    enumerable: false
});

It's all gets enclosed in a closure to prevent namespace pollution. I don't care much about speed as this can be improved later (but tell me). I don't care about style (it's fixed) not about compatibility with old crap. I do care about all problems possible in modern browsers.

It's to be used like

var o = {a: 1, b: 2, c: 3};
o.removeMatching(function(k, v) {return k === "a" || v === 2});
// now o is {c: 3}

There are extraneous semicolons (null statements) in your code. Semicolons do not belong after function declarations and for loops.

  I'm extending the Object.prototype which should be fine.

I would argue that it's not fine. While it's unlikely to cause undesired side effects, breaking encapsulation and polluting all objects just for the sake of being able to write foo.removeMatching(bar) instead of removeMatching(foo, bar) seems like bad form.

if (!Object.prototype.hasOwnProperty.call(this, key)) continue;

Since this is an Object by definition, because you are attaching this to Object.prototype, there is no need to write it like that. This would achieve the same thing (unless you are shadowing hasOwnProperty in which case you have bigger problems):

if (!this.hasOwnProperty(key)) continue;

However there's no need to write this line in the first place. If the object doesn't have a property as its own property, attempting to delete it will do nothing.

  It's all gets enclosed in a closure to prevent namespace pollution.

This also seems unnecessary, why not just write it like this:

Object.defineProperty(Object.prototype, 'removeMatching', {
    value: function (predicate) {
        for (var key in this) {
            if (predicate(key, this[key])) {
                delete this[key];
            }
        }
    },
    writable: false,
    configurable: false,
    enumerable: false
});


Basic cons cells in C So, I'm diving into C and decided to create cons cells. I'd like some thoughts on style and correctness. There are no warnings with -Wall -g and Valgrind doesn't complain about anything.

I'm really not sure what to do when the list is NULL in the first function.

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

struct Cell {
    int val;
    struct Cell *next;
};

struct Cell *cons(int val, struct Cell *rest)
{
    struct Cell *cell = malloc(sizeof(struct Cell));
    cell->val = val;
    cell->next = rest;
    return cell;
}

int first(struct Cell *list)
{
    if (list) {
        return list->val;
    }
    return 0;
}

struct Cell *rest(struct Cell *list)
{
    return list->next;
}

int length(struct Cell *list)
{
    if (list) {
        return 1 + length(rest(list));
    }
    return 0;
}

void destroy(struct Cell *list)
{
    if (list) {
        destroy(rest(list));
        free(list);
    }
}

void print(struct Cell *list)
{
    if (list) {
        printf("%d\n", first(list));
        print(rest(list));
    }
}

The main function.

int main(int argc, char *argv[])
{
    struct Cell *list = NULL;
    int i;
    for (i = 1; i < argc; ++i) {
        list = cons(atoi(argv[i]), list);
    }
    puts("Entire list:");
    print(list);
    puts("First:");
    printf("%d\n", first(list));
    puts("Rest:");
    print(rest(list));
    puts("Length:");
    printf("%d\n", length(list));
    destroy(list);
    return 0;
}

Running it.

./cons 1 2 3 4

A few comments:

typedef your structs:

typedef struct Cell
{
    int val;
    struct Cell *next;
} Cell;

typedef means you no longer have to write struct all over the place:

Cell *cons(int val, Cell *rest)
{
    Cell *cell = malloc(sizeof(Cell));
    cell->val = val;
    cell->next = rest;
    return cell;
}

That not only saves keystrokes, it also can make the code cleaner since it provides a bit more abstraction.

I would check if the pointer is NULL rather than if it isn't (related question):

int length(struct Cell *list)
{
    if (!list) return NULL; // or something else like -1
    return 1 + length(rest(list));
}

Set the pointer to NULL after freeing it.

void destroy(struct Cell *list)
{
    if (list)
    {
        destroy(rest(list));
        free(list);
        list = NULL;
    }
}

Re-using a freed pointer can be a subtle error. Your code keeps right on working, and then crashes for no clear reason because some seemingly unrelated code wrote in the memory that the re-used pointer happens to be pointing at.  This is to prevent that.


Use jQuery to force two checkboxes mirror each other's state RESTRICTION: jQuery 1.6.4 only, no other library, or version can be used.

My goal is to use jQuery to allow two input checkboxes behave the same.

For example: If 1st checkbox is checked, 2nd will be checked. If 1st is unchecked, 2nd will be unchecked. And vise versa. When either of the checkbox is checked, I will add a class to the HTML body tag, if it's unchecked, remove the class to the body tag.

HTML:

<body>
    <input type="checkbox" checked='true' id='1'/>
    <input type="checkbox" checked='true' id='2'/>
</body>

I can't modify the input tags, that means I can't give it a class name. I can only use what is there. Meantime, I try to make as less jQuery calls as possible, in order to increase performance. I try to write as less code as possible, so I came up this piece of code:

jQuery:

$('input:checkbox').live('change', function () {
  $('input:checkbox').prop('checked', $(this).is(":checked"));
  if ($('input:checkbox').is(":checked")) {
    $('body').addClass('checked');
  } else {
    $('body').removeClass('checked');
  }
});

The code works, but I am not happy with it. I think it can be improved. I especially do not like the part $('input:checkbox'), I am concerned about the speed when querying the DOM with $('input:checkbox'). How can I improve? Any other problems?

As said in other answer, cache the DOM elements references so you don't have to dive into DOM to get the elements again and again on each check/uncheck of the checkboxes.
Use $(document.body) to refer to <body> element, this is slight faster. See this post.
Use a common class on all the interested checkboxes. $('input:checkbox') is a general selector and it'll select all the checkboxes on the page.
You can use toggleClass() with second param which can be used to decide whether to add or remove class from elements. If the second param is true class is added and if it is false class is removed.
As the checkboxes are by default checked, add the class on the <body> by default.
this.checked is faster than $(this).is(':checked'). It just checks the property on the current elements. Whereas in jQuery, it has to call the is() function and check if the passed argument status on the element.
IF POSSIBLE, update the jQuery version to latest one. It has many performance improvements and bug-fixes.

Live Demo:

// Cache the elements
var $myCheckbox = $('.myCheckbox'),
  $body = $(document.body);1

// On change of the checked state of checkbox
$myCheckbox.live('change', function() {
  // Update the checked property of all other checkboxes
  $myCheckbox.prop('checked', this.checked);

  // Add/Remove the class on body
  $body.toggleClass('checked', this.checked);
});
/* For DEMO purpose */
body.checked {
  background: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>

<body class="checked">
  <input type="checkbox" checked='true' class="myCheckbox" id='1' />
  <input type="checkbox" checked='true' class="myCheckbox" id='2' />
</body>


Maximum number of unique integers I'm given N integers. I need to find the maximum number of unique integers among all the possible contiguous subarrays of size M.

Scanner in = new Scanner(System.in);
Deque deque = new ArrayDeque<Integer>();
int n = in.nextInt();
int m = in.nextInt();
in.nextLine();
String s = in.nextLine();
in.close();

int maxUnique = 0;
List<Integer> numbers = Stream.of(s.split(" "))
    .map(Integer::parseInt)
    .collect(Collectors.toList());
for (int i = 0; i <= n-m; i++) {
    HashSet<Integer> unique = new HashSet<>(numbers.subList(i, i+m));
    if (maxUnique < unique.size())
        maxUnique = unique.size();
    if (maxUnique == m) {
        System.out.println(maxUnique);
        return;
    }
}
System.out.println(maxUnique);

It works fine, but fails on big arrays due to timeout. For example N=100000, M=98777 times out.

How can I improve it?

You're almost there. You rebuild the entire HashSet for each of the i-m sub arrays resulting in (i-m)*m  worrk.

Change the HashSet to a HashMap and then imagine the sub array as a sliding window. When the window moves one step to the right, count down number at the far left of the old window position in the HashMap and if it becomes zero, remove it. And then add the number to the far right of the new window position to the count of that number in the HashMap (or add the number with count one if it didn't exist), and at each window position take the size of the HashMap. This is (i-m)+m work which is much faster.


What is the better way to retrieve value from this cache implementaion? I have the following methods in CacheUtil class

public void addCacheEntry(String key, Serializable entry) {
    //add to cache
}

public void removeCacheEntry(String key) {
   //remove from cache
}

public Serializable getCacheEntry(String key) {
   Serializable entry = // get from cache
   return entry;
}

And in the class where I use the cache, I have the following methods,

public List<String> fetchList() {
   List<String> aList = (List<String>) CacheUtil.getInstance().getCacheEntry("aList"); //IDE warns of unchecked casting
   if(aList == null) {
     aList = fetchFromDB():
     CacheUtil.getInstance().addCacheEntry("aList", new ArrayList<String>(aList));
   }
}

How could I have avoided the warning of unchecked cast? I can not change the methods in cache to store lists explicitly, as I want to store anything that is serializable in cache and that is just a library.

I believe there is a better way to do it.

You could change your CacheUtil class to the following:

public class CacheUtil {
    ...
    public void addCacheEntry(String key, Serializable entry) {
        //add to cache
    }

    public void removeCacheEntry(String key) {
       //remove from cache
    }

    public <T extends Serializable> T getCacheEntry(String key) {
       T entry = // get from cache (probably needs a cast to T)
       return entry;
    }
}

which would allow you to get cached entries by type T without a cast:

    public List<String> fetchList() {
       ArrayList<String> aList = CacheUtil.getInstance().getCacheEntry("aList");
       if(aList == null) {
         aList = fetchFromDB():
         CacheUtil.getInstance().addCacheEntry("aList", new ArrayList<String>(aList));
       }
    }

Notice that I was required to use ArrayList, because List is not Serializable on its own.  You could take this one step farther and have getCacheEntry() return T without extends:

    public <T> T getCacheEntry(String key) {
       T entry = // get from cache (may need a cast)
       return entry;
    }

But Im not sure I'd take that leap.


Naïve Prime Factorization in Clojure Pt. 2 After my last review request, I decided to try and make a "tree" visualization, and thought that I could benefit from making find-prime-factors lazily produce prime factors so the visualization can be updated in real time as factors are found.

This is what I ended up with. I was actually surprised how easy it was to adapt the loop to a recursive lazy-seq solution. The non-return accumulators were added as parameters to a recursive function, and the lazy return list was created via the typical (lazy-seq (cons ... ...)). It actually performs identically to the previous strict version too when evaluation is forced using vec.

If anyone sees anything here that can be commented on, I'd like to know.

(doseq [p (lazily-find-prime-factors 99930610001)]
  (println p
           (int (/ (System/currentTimeMillis) 1000))))
163 1544998649
191 1544998692
3209797 1544998692

Same as in the last review:

(ns irrelevant.prime-factorization.prime-factorization)

(defn is-factor-of? [n multiple]
  (zero? (rem n multiple)))

(defn prime? [n]
  (or (= n 2) ; TODO: Eww
      (->> (range 2 (inc (Math/sqrt ^double n)))
           (some #(is-factor-of? n %))
           (not))))

(defn primes []
  (->> (range)
       (drop 2) ; Lower bound of 2 for the range
       (filter prime?)))

(defn smallest-factor-of? [n possible-multiples]
  (some #(when (is-factor-of? n %) %)
        possible-multiples))

Updated:

(defn lazily-find-prime-factors [n]
  (letfn [(rec [remaining remaining-primes]
            (when-let [small-prime (and (> remaining 1)
                                        (smallest-factor-of? remaining remaining-primes))]
              (lazy-seq
                (cons small-prime
                      (rec (long (/ remaining small-prime))
                           (drop-while #(not= % small-prime) remaining-primes))))))]
    (rec n (primes))))

All that the lazily-find-prime-factors function needs is the (primes) sequence. You just keep trying each in turn. Your smallest-factor-of drops the failed factors, but forgets what it has done, so you have to do it again for the recursive call.

And I'd rename your is-factor-of? function:

=> (is-factor-of? 4 2)
true
=> (is-factor-of? 2 4)
false

... reads wrongly. I've called it divides-by?.

I end up with ...

(defn lazily-find-prime-factors [n]
  (letfn [(rec [remaining remaining-primes]
            (when (> remaining 1)
              (let [rp (drop-while #(not (divides-by? remaining %)) remaining-primes)
                    small-prime (first rp)]
                (lazy-seq (cons small-prime
                            (rec (quot remaining small-prime) rp))))))]
   (rec n (primes))))

Factoring the problem this way, you can easily plug in an efficient way of generating primes.


Guessing game - Is it a 40? User has number in head, the program must guess the number.

public class GuessingGame extends JFrame{

    JButton newGameButton, highButton, lowButton, correctButton, exitButton;
    JLabel initialTextLabel, enterLabel;
    JTextArea commentTextArea;
    JScrollPane scroll;
    private int guess, high = 101, low = 0, tries = 1;

    public guessingGame()
    {
        title("Guessing Game");
        newGameButton = new JButton("Start Game");
        exitButton = new JButton("Exit Game");
        highButton = new JButton("Too High");
        lowButton = new JButton("Too Low");
        correctButton = new JButton("Correct!");

        commentTextArea = new JTextArea(null,10,30);
        scroll = new JScrollPane(commentTextArea);
        commentTextArea.setEditable(false);
        initialTextLabel = new JLabel("Think of a number between 0 & 100 can the computer guess it");
        enterLabel = new JLabel("Is your number: ");

        //add components

        highButton.setVisible(false);
        lowButton.setVisible(false);
        correctButton.setVisible(false);

        //set default jframe size

        //create and register the button event handlers

    }//end of GuessGame constructor

    //highButtonHandler class
    class highButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
                high = guess;
                guess = low + (guess - low) / 2;
                tries++;
                commentTextArea.append("Is the number " + guess + " too small, too high or correct.\n");
        }
    }//end of high class

    //lowButtonHandler class
    class lowButtonHandler implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                low = guess;
                guess = guess + (high - guess) / 2;
                tries++;
                commentTextArea.append("Is the number " + guess + " too small, too high or correct.\n");
            }
        }//end of low class

    //correctButtonHandler class
    class correctButtonHandler implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                commentTextArea.append("The computer took " + tries + " tries to guess your number");
            }
        }//end of correct class

    //newGame ButtonHandler class
    class newGameButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            commentTextArea.setText(null); //clear textArea
            //reset values
            guess = 50;
            tries = 1;
            low = 0;
            high = 101;

            commentTextArea.append("Is the number " + guess +" too small, too high or correct.\n");
            newGameButton.setText("Start New Game"); //set button text
        }
    }//end of newGame class

    //exitButtonHandler class
    class exitButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            System.exit(0);
        }
    }

    public static void main(String[] args)
    {
        guessingGame myGuessGame = new guessingGame(); //instantiate a GUI object
        myGuessGame.setVisible(true);
    }//end main
}

Pros

dedicated classes for the ActionListener
calls JFramw.setVisible() from outside any constructor

cons

Naming conventions: class names always start with upper case letter
Inheritance: never extend a class unless you want to change behavior, you only configure JFrame, so no need to extend it.
comments: comments in code should explain why the code is like it is and only if it is somewhat unusual or unexpected. Your comments only repeat what the code expresses already.
System.exit(0);: This kills the JVM. There will be no change to i.e. close database connections correctly. Use it only in a catch block inside main.


AutoreleasePool NSString Automatic Reference Counting I have some doubt about how NSString is released in ARC Mode.

I would like to know if have something i can do to release a nsstring when i want in arc mode.

Take a look at this code:

__block NSString *strImage = nil;
    dispatch_sync(backGround, ^{
        // Convert NSData To NSString.
        strImage = [UIImagePNGRepresentation([UIImage imageWithData:reg.photoData]) base64Encoding];
    });

    // Prepare Request Operation.
    NSString * strPost = @"myUrl";

    strPost = [strPost stringByAppendingFormat:@"}&image="];
    strPost = [strPost stringByAppendingFormat:@"%@",strImage];
    //NSLog(@"POST: %@",strPost);

    // setting up the URL to post to
    NSString *urlString = @"myURLAPI";

    // setting up the request object now
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];

    /*
     add some header info now
     we always need a boundary when we post a file
     also we need to set the content type

     You might want to generate a random boundary.. this is just the same
     as my output from wireshark on a valid html post
     */
    NSString *contentType = [NSString stringWithFormat:@"application/x-www-form-urlencoded"];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

    /*
     now lets create the body of the post
     */
    NSMutableData *body = [NSMutableData data];

    [body appendData:[NSData dataWithData:[strPost dataUsingEncoding:NSUTF8StringEncoding]]];
    // setting the body of the post to the reqeust
    [request setHTTPBody:body];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];

    //MY DOUBT ABOUT RELEASE THIS STR
    @autoreleasepool {
        strImage = nil;
    }

is it correct or have a best way to do it? Thank you

Also @autoreleasepool does not look are the objects create outside of it scope, just the object used within. Also if using are there is not need for the @autoreleasepool at all, since the scope variables will be released and nilled at the end of the scope.


Shell agnostic startup flow - env, profile, rc, login The goal of these files is to have some common behavior between Bash and Zsh
when starting them. As is known, when Bash starts as a login shell it will
source the first found of the following in your home director: .bash_profile,
.bash_login, or .profile. If Bash is a nonlogin interactive it will source
.bashrc. Meanwhile, Zsh behaves pretty differently. First, it always sources
.zshev. Next, if it's a login shell it will source .zprofile followed by
.zshrc (it sources this even if it's just interactive) and finally .zlogin.
I wanted there to be some common behavior here, so I made it so the flow for
both are:

Common environment
Shell specific environment
Common profile (if login)
Shell specific profile (if login)
Common "rc" (if interactive)
Shell specific "rc" (if interactive)
Common login (if login)
Shell specific login (if login)

The new files I have had to add are .bash_env, .common_env, .common_login,
and .common_rc.

A lot of these files only have some debug statements at the top (which all of
them do), so I won't be posting those. In addition, all of the Zsh files are the
same pattern so I'll only be posting one. I'm not including any of the "actual"
things these do (like set aliases or environment variables) because they're not
relevant to what I want reviewed.

.bash_profile

[ "$MY_STARTUP_DEBUG" = "true" ] && echo "* In .bash_profile"

[ "$MY_STARTUP_DEBUG" = "true" ] && echo "\t* Sourcing ${HOME}/.common_env if it exists"
[ -f "${HOME}/.common_env" ] && source ${HOME}/.common_env

[ "$MY_STARTUP_DEBUG" = "true" ] && echo "\t* Sourcing ${HOME}/.bash_env if it exists"
[ -f "${HOME}/.bash_env" ] && source ${HOME}/.bash_env

[ "$MY_STARTUP_DEBUG" = "true" ] && echo "\t* Sourcing ${HOME}/.profile if it exists"
[ -f "${HOME}/.profile" ] && source ${HOME}/.profile

# Start of "actual content"

# End of "acutal content"

[ "$MY_STARTUP_DEBUG" = "true" ] && echo "\t* Sourcing ${HOME}/.common_rc if it exists"
[ -f "${HOME}/.common_rc" ] && source ${HOME}/.common_rc

[ "$MY_STARTUP_DEBUG" = "true" ] && echo "\t* Sourcing ${HOME}/.bashrc if it exists"
[ -f "${HOME}/.bashrc" ] && source ${HOME}/.bashrc

[ "$MY_STARTUP_DEBUG" = "true" ] && echo "\t* Sourcing ${HOME}/.common_login if it exists"
[ -f "${HOME}/.common_login" ] && source ${HOME}/.common_login

[ "$MY_STARTUP_DEBUG" = "true" ] && echo "\t* Sourcing ${HOME}/.bash_login if it exists"
[ -f "${HOME}/.bash_login" ] && source ${HOME}/.bash_login

.bashrc

This one checks to make sure it's not a login shell before sourcing to avoid sourcing twice.

[ "$MY_STARTUP_DEBUG" = "true" ] && echo "* In .bashrc"

shopt -q login_shell || ([ "$MY_STARTUP_DEBUG" = "true" ] && echo "\t* Sourcing ${HOME}/.common_env if it exists")
shopt -q login_shell || ([ -f "${HOME}/.common_env" ] && source ${HOME}/.common_env)

shopt -q login_shell || ([ "$MY_STARTUP_DEBUG" = "true" ] && echo "\t* Sourcing ${HOME}/.bash_env if it exists")
shopt -q login_shell || ([ -f "${HOME}/.bash_env" ] && source ${HOME}/.bash_env)

[[ "$-" != *i* ]] && return

shopt -q login_shell || ([ "$MY_STARTUP_DEBUG" = "true" ] && echo "\t* Sourcing ${HOME}/.common_rc if it exists")
shopt -q login_shell || ([ -f "${HOME}/.common_rc" ] && source ${HOME}/.common_rc)

# Start of "actual content"

.zshenv

[ "$MY_STARTUP_DEBUG" = "true" ] && echo "* In .zshenv"

[ "$MY_STARTUP_DEBUG" = "true" ] && echo "\t* Sourcing ${HOME}/.common_env if it exists"
[ -f "${HOME}/.common_env" ] && source ${HOME}/.common_env

# Start of "actual content"

These repeated statements are really ugly:

[ "$MY_STARTUP_DEBUG" = "true" ] && echo "* In .bash_profile"
[ "$MY_STARTUP_DEBUG" = "true" ] && echo "\t* Sourcing ${HOME}/.common_env if it exists"

You can do better by using a function, for example:

msg() {
    [ "$MY_STARTUP_DEBUG" = true ] || return
    echo "$@"
}

msg "* In .bash_profile"
msg "\t* Sourcing ${HOME}/.common_env if it exists"

Extra to: I removed the double-quotes around true, as that's unnecessary.

Whenever you see repeated logic, try to use a function.
Here's another helper function that can simplify much of the code:

source_if_exists() {
    msg "\t* Sourcing $1 if it exists"
    [ -f "$1" ] && source "$1"
}

source_if_exists "${HOME}/.common_env"
source_if_exists "${HOME}/.bash_env"

Note that in the original code, you forgot to double-quote the parameters of the source commands.


Android loading playlists function public static ArrayList<Playlist> getAllPlaylists( Context context ){
    ArrayList<Playlist> playlists = new ArrayList<Playlist>();
    ArrayList<String> fileList = new ArrayList<>();

    //this will get me all playlist files available for my application
    for( String file : context.fileList() )
        if( file.endsWith(".playlist") )
            fileList.add(file);

    for( String file : fileList ){
        Playlist newPlaylist = loadPlaylist( context, file );

        if( !newPlaylist.isEmpty() ){
            playlists.add(newPlaylist);
        }
    }

    return playlists;
}

private static Playlist loadPlaylist( Context context, String file ){
    Playlist playlist = new Playlist();

        try {
            FileInputStream inputStream = context.openFileInput(file);
            byte[] inputBuffer = new byte[ inputStream.available() ];
            inputStream.read(inputBuffer);
            String content = new String(inputBuffer);
            //each line holds a path so we split the content
            String[] files = content.split("\n");

            playlist.setName( files[0] );
            for( int i = 1; i < files.length; i++ ){
                //This will just load the Song (artist, title etc.)
                //if somehow the path leads to nowhere or it is just not a
                //music file, getSong() will return null
                Song song = MediaManager.getSong( context, files[i] );

                if( song != null )
                    playlist.addSong( song );
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    return playlist;
}

The structure I chose for my playlist files is pretty simple. The first line is the name of that playlist and the following lines will contain a filepath which I can use later on to load a file and play it / display its data.

Here is an example of what they might look like :

My first Playlist
/sdcard/.../.../whatever.mp3
/sdcard/.../.../noway.mp3
/sdcard/.../.../seriously.mp3

How can I improve the speed of this process? It takes about 1 second for 1 file...

About the slowness...

I don't see obvious bottlenecks here. When you say "It takes about 1 second for 1 file..." do you mean:

A single song? In this case the MediaManager.getSong is a prime suspect. Add logging before and after to measure the difference.
An entire playlist file?

Are there many songs in a list?
Does it make a difference if all the files in the list are valid or invalid? Test with 100 valid files and 100 invalid files. If invalid files somehow cause MediaManager.getSong to be slow, you can try to add a check if the file exists before passing it to MediaManager.

These are mostly shots in the dark.

There are some other problems with your code that won't make your program faster but you should fix anyway.

Reading from streams

The documentation says this about inputStream.available():

  Returns an estimated number of bytes that can be read or skipped
  without blocking for more input.

  Note that this method provides such a weak guarantee that it is not
  very useful in practice.

Don't use it this way. Read like this instead:

FileInputStream inputStream = context.openFileInput(file);
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String line;
List<String> files = new ArrayList<String>();
while ((line = bufferedReader.readLine()) != null) {
    files.add(line);
}
bufferedReader.close();

Iterator<String> iter = files.iterator();
playlist.setName(iter.next());
while (iter.hasNext()) {
    String path = iter.next();
    Song song = MediaManager.getSong(context, path);
    if (song != null) {
        playlist.addSong(song);
    }
}

An important mistake you did there that you didn't close the input stream. Remember to close resources that you opened.

Use interface types instead of implementations

Instead of this:

ArrayList<Playlist> playlists = new ArrayList<Playlist>();

Use the interface type in declarations, like this:

List<Playlist> playlists = new ArrayList<Playlist>();

Style issues

This code doesn't follow the standard spacing rules, and the convention of using braces even for one-line statements:

for( String file : context.fileList() )
    if( file.endsWith(".playlist") )
        fileList.add(file);

This is the recommended style:

for (String file : context.fileList()) {
    if (file.endsWith(".playlist")) {
        fileList.add(file);
    }
}


Alternatives for checking if something is not None in Python I recently bumped into dry-python/returns, which seems like a very useful project. In the project description, the authors describe a snippet which can be written with their Maybe container in a better way (according to the authors):
The original code is:
if user is not None:
     balance = user.get_balance()
     if balance is not None:
         credit = balance.credit_amount()
         if credit is not None and credit > 0:
             discount_program = choose_discount(credit)

And the improved version is:
discount_program: Maybe['DiscountProgram'] = Maybe.from_optional(
    user,
).bind_optional(
    lambda real_user: real_user.get_balance(),
).bind_optional(
    lambda balance: balance.credit_amount(),
).bind_optional(
    lambda credit: choose_discount(credit) if credit > 0 else None,
)

This is a legitimate solution. However, I would write the first code as:
if user:
   balance = user.get_balance()
   if balance:
      credit = balance.credit_amount()
      if credit > 0:
          discount_program = choose_discount(credit)

Being a long time pylint user that the style that is recommended.
Honestly, I've never given it much thought, and now that I saw the first
code, I started asking myself if I should give it some thought.
The most dangerous place IMHO, in my version is:
if credit > 0:

Since None will raise here a TypeError. However, the original line can be written as:
if credit and credit > 0:
   discount_program = choose_discount(credit)

My favorite style for re-writing this block would be very LISPy. I use getattr with a default value which is callable (a lambda which returns None). This very similar to what the example in return does, but without a 3rd party library:
choose_discount(
   getattr(
      getattr(user, 'get_balance', lambda: None)(),
      'credit_amount', lambda: 0)())

Which I think would not be like by many because it's very unusual. Also, for this to work properly, I would write choose_discount as:
def  choose_discount(credit):
    if credit:
        return "Your discount choosing ..."
    else:
        return "No discount"

If allowed to use a newer version of Python, I would probably use the walrus operator here:
if (credit := getattr(getattr(user, 'get_balance', lambda: None)(), 'credit_amount', lambda: 0)()) > 0:
    discount_program = choose_discount(credit)

This style strongly assume that credit_amount always returns a type which can be numerically compared ... Hence, the caveat I mentioned earlier, with credit being a None is implicitly handled.
I have a few questions regarding this.

Can you point out possible caveats in my style?
Could I be missing out something terrible, and I should look for is not None
explicitly?
Is the functional style legitimate here or should I absolutely avoid it?

I thought the readme on dry-python/return did a good job of explaining the usage of the Maybe type.
The key to understanding Maybe is that it represents computations which might "go wrong" by not returning a value.
The bind method (bind_optional) in this library allows you to write code without having to worry about the result of the computation at any intermediate step, it handles the case where one of the computations returns None.
In the code you use to demonstrate the conditionals, you still have to handle the cases of None via an if statement, the use of if not None vs if Some isn't relevant. As a hypothetical consider a computation with many level of optional results from computation, you'd still have to implement the same level of nesting.
In the example from the documentation
user: Optional[User]

# Type hint here is optional, it only helps the reader here:
discount_program: Maybe['DiscountProgram'] = Maybe.from_optional(
    user,
).bind_optional(  # This won't be called if `user is None`
    lambda real_user: real_user.get_balance(),
).bind_optional(  # This won't be called if `real_user.get_balance()` is None
    lambda balance: balance.credit_amount(),
).bind_optional(  # And so on!
    lambda credit: choose_discount(credit) if credit > 0 else None,
)

the comments in the code say the same. You can imagine a library using a symbolic function name >>= with infix operators allowing the above code to be rewritten as

credit = user >>= (lambda real_user: real_user.get_balance())
              >>= (lambda balance: balance.credit_amount())
              >>= (lambda credit: choose_discount(credit) if credit > 0 else None)

IMO, you approach to utilizing getattr is more akin to programming in a "duck typing" style than functional.
I used the haskell wiki's description of the Maybe monad if you want to dig in further.


Show image when in viewpoint of browser window I am working on a web project and am trying to create a way to detect when an image is in viewpoint of the browser window then load/show the image. Since there are some rather large image that are being resize to use a thumbnails, I am trying to take into consideration those that might view it one a mobile device and reduce page loading time. I notice that many site with galleries are doing the same thing.

I create a little script using JavaScript to detect when an  tag is in view and then take its data attribute value (the URL of the image) and set it's  attribute to it.

Is this is an efficient way of doing it or should I just use PHP the create thumbnails?

I have included snippets of the HTML and JavaScript I am using.

HTML of the image

JavaScript with jQuery

//Show images when in viewpoint
$(window).on('scroll load',function(){
    //Window width & height
    var viewpointWidth = $(window).width(),
    viewpointHeight = $(window).height(),
    //Document Top pos & Left pos
    documentScrollTop = $(document).scrollTop(),
    documentScrollLeft = $(document).scrollLeft(),
    //Document Positions
    minTop = documentScrollTop,
    maxTop = documentScrollTop + viewpointHeight,
    minLeft = documentScrollLeft,
    maxLeft = documentScrollLeft + viewpointWidth;
    //Loop for each image
    $('.page-content img').each(function(){
        $eleImage = $(this),
        elementOffset = $eleImage.offset();
        if((elementOffset.top > minTop && elementOffset.top < maxTop) && (elementOffset.left > minLeft &&elementOffset.left < maxLeft)) {
            $(this).attr('src',$(this).data('image-src'));
        }
    });
});

Correctness

You need to handle resize events also. Yes, you can add it like : $(window).on('scroll load resize', ...
You are only checking for the visibility of the top/left corner, if you load a large image, scroll right, and then scroll down, you will see the right edges of the ALT text of the images, but because their top/left corner is not in viewport, those images will not load. You might want to check if any point of the img box is visible.

Browser Behavior

Make sure you have <!DOCTYPE HTML> on your document, this will load all images if you miss doctype declaration. (for Firefox, Chrome, don't know about others)
You might want to mark images processed, by adding a class or another data; and do not fiddle with the src attributes of the images once they are loaded.

Javascript related

Because you miss a var before $eleImage and elementOffset, they pollute global namespace.
You copy the value of $(this) here : $eleImage = $(this), but you keep using $(this) below.


Performance for, foreach and for key I have a question about of the performance of using for loops (normal for, for each and for key in object) in JavaScript.

Just now I created this code to test:

var stopwatch = function(){
    var start;

    this.Start =function(){
        this.start = new Date().getTime();
    }

    this.getDifference= function(){
        return new Date().getTime() - this.start;
    }
}

var stopwatch = new stopwatch();
var length = 1000000;
var array = new Array(length);
for(var i = 0; i<length; i++){
    array[i]=1;
};

stopwatch.Start();
for(var i = 0; i<length; i++){
    array[i]+1;
};
var time = stopwatch.getDifference();
console.log("Time of normal for: "+time);
stopwatch.Start();
array.forEach(function(entry){
    entry+1;
});
time = stopwatch.getDifference();
console.log("Time of for each: "+time);
stopwatch.Start();
for(key in array){
    array[key]+1;
}
time = stopwatch.getDifference();
console.log("Time of for key in array: "+time);

The result is the following

Time of normal for: 29
Time of for each: 151
Time of for key in array: 803

It seems like the normal for is faster than the others implementations.  Is it right?

Well, here's some tips:

Never use for-in for arrays. for-in runs through the array properties.

var a = [1,2,3]
a.foo = 'bar'; // arrays are still objects
for(var i in a){console.log(i)} // will run over foo

forEach though native, but AFAIK is still slow compared to while and for. Some say it suffers the same overhead of calling a function (activation records, stack, scope lookup etc.)
Consider async instead. By the looks of it, you want to loop through an array as fast as possible to avoid freezing the browser. You can go with WebWorkers, or Timers. Here's an example using setInterval. It's not as fast as loops, but you won't be seeing those "Unresponsive script" and "He's dead Jim!" warnings:

var i = 1000000;
var looper = setInterval(function(){
  /* do something */

  if(!--i) clearInterval(looper);
},0);


Find all pairs in an array that sum to a given number Assume you have an array of random integers and a sum value. Find all
  pairs of numbers from the array that sum up to the given sum in O(n)
  time. Find all distinct pairs. (1,2) and (2,1) are not distinct.

import java.util.HashSet;

public class PairsSummingToElement {

  public static void main(String[] args) {
    PairsSummingToElement e = new PairsSummingToElement();
    int[] input = new int[] { 2, 5, 3, 7, 9, 8 };
    int sum = 11;
    HashSet<Pair> result = e.findAllPairs(input, sum);
    for (Pair p : result) {
      System.out.println("(" + p.getElement1() + "," + p.getElement2() + ")");
    }

  }

  public HashSet<Pair> findAllPairs(int[] inputList, int sum) {
    HashSet<Integer> allElements = new HashSet<Integer>();
    HashSet<Integer> substracted = new HashSet<Integer>();
    HashSet<Pair> result = new HashSet<Pair>();

    for (int i : inputList) {
      allElements.add(i);
      substracted.add(i - sum);
    }

    for (int i : substracted) {
      if (allElements.contains(-1 * i)) {
        addToSet(result, new Pair(-i, i + sum));
      }
    }

    return result;

  }

  public void addToSet(HashSet<Pair> original, Pair toAdd) {
    if (!original.contains(toAdd) && !original.contains(reversePair(toAdd))) {
      original.add(toAdd);
    }
  }

  public Pair reversePair(Pair original) {
    return new Pair(original.getElement2(), original.getElement1());
  }

}

class Pair {
  private int element1;

  private int element2;

  public Pair(int e1, int e2) {
    element1 = e1;
    element2 = e2;
  }

  public int getElement1() {
    return element1;
  }

  public int getElement2() {
    return element2;
  }

  public int hashCode() {
    return (element1 + element2) * element2 + element1;
  }

  public boolean equals(Object other) {
    if (other instanceof Pair) {
      Pair otherPair = (Pair) other;
      return ((this.element1 == otherPair.element1) && (this.element2 == otherPair.element2));
    }
    return false;
  }

}

No comments on the time complexity, I do have a bunch of other comments:

Code to interfaces, not implementations

It's better to use Set<Integer> allElements = new HashSet<Integer>() (side note: guess you're not on >= Java 7, since you can use diamond operators otherwise) so that users of allElements do not need to know that it actually is a HashSet. This is just good practice. The lesson here is that it allows the programmer to replace the implementation with another in the future when required.

Does not contain, then add to a Set

In the following code block:

if (!original.contains(toAdd) && !original.contains(reversePair(toAdd))) {
  original.add(toAdd);
}

I think the extra check !contains() is not really required because Set.add()'s Javadoc says:

  If this set already contains the element, the call leaves the set unchanged and returns false.

So, even if original did contain toAdd, calling add() will leave the set unchanged anyways. I guess this is useful when you're running on a large set of inputs so that there is some short-circuiting done, hence this is strictly just 'food-for-thought' and not something to frown upon.

hashCode()

I'm guess there's an implied range of inputs such as all must be positive right? Because your calculation (element1 + element2) * element2 + element1 will yield the same hash codes when element1 + element2 equals to 1, and having a bunch of negative and positive integers (or just 0, 1) will 'break' this easily. You may want to update your question with any assumptions on the range of inputs.

How to reverse a Pair

I think that you can move your method reversePair(Pair) into the Pair class itself as such:

public final class Pair { // also declared as final

    private final int element1; // also declared as final
    private final int element2; // also declared as final

    public Pair(int element1, int element2) {
        this.element1 = element1;
        this.element2 = element2;
    }
    ...

    public Pair getReverse() {
        return new Pair(element2, element1);
    }
}

Using it then becomes slightly easier:

if (!original.contains(toAdd.getReverse())) {
  original.add(toAdd);
}

Override toString()

Oh yeah, given how you want to print the contents of Pair objects in the end, why not just override toString() too? :)


Request for help to tidy up code involving arrays and loops I posted the following code on here a few days ago:

public class Practical4_Assessed
{
    public static void main(String[] args)
    {

        Random numberGenerator = new Random();
        int[] arrayOfGenerator = new int[100];
        int[] countOfArray = new int[10];
        int count;

        for (int countOfGenerator = 0; countOfGenerator < 100; countOfGenerator++)
        {
            count = numberGenerator.nextInt(10);
            countOfArray[count]++;
            arrayOfGenerator[countOfGenerator] = count + 1;
        }

        int countOfNumbersOnLine = 0;
        for (int countOfOutput = 0; countOfOutput < 100; countOfOutput++)
        {
            if (countOfNumbersOnLine == 10)
            {
                System.out.println("");
                countOfNumbersOnLine = 0;
                countOfOutput--;
            }
            else
            {
                if (arrayOfGenerator[countOfOutput] == 10)
                {
                    System.out.print(arrayOfGenerator[countOfOutput] + "  ");
                    countOfNumbersOnLine++;
                }
                else
                {
                    System.out.print(arrayOfGenerator[countOfOutput] + "   ");
                    countOfNumbersOnLine++;
                }
            }
        }

        System.out.println("");
        System.out.println("");

        for (int countOfNumbers = 0; countOfNumbers < countOfArray.length; countOfNumbers++)
            System.out.println("The number " + (countOfNumbers + 1)
                    + " occurs " + countOfArray[countOfNumbers] + " times.");

        System.out.println("");

        for (int countOfNumbers = 0; countOfNumbers < countOfArray.length; countOfNumbers++)
        {
            if (countOfNumbers != 9)
                System.out.print((countOfNumbers + 1) + "   ");
            else
                System.out.print((countOfNumbers + 1) + "  ");

            for (int a = 0; a < countOfArray[countOfNumbers]; a++)
            {
                System.out.print("*");
            }
            System.out.println("");
        }

        System.out.println("");

        int max = 0;
        int test = 0;
        for (int counter = 0; counter < countOfArray.length; counter++)
        {
            if (countOfArray[counter] > max)
            {
                max = countOfArray[counter];
                test = counter + 1;
            }
        }

        System.out.println("The number that appears the most is " + test);

    }
}

I posted it for help on how to tidy it up - the suggestion I got was to add methods to break the code down into more manageable, testable sections.
However, despite spending the best part of two days trying, I can't get my head around how to do this. Everytime I try, I either generate compile-time or run-time errors.

Would anyone mind showing me even one or two of the methods I could use, so I can work from that?

After stressing about it for about two hours, I got the methods sorted, so I'll answer my own question. Please note - I'm just a beginner and I'm sure there are plenty of things that could be done even more efficiently!

import java.util.Random;
// Imports Java random number generator.

public class Practical4_Assessed_with_Methods
{
static int[] randomNumbers = new int[100];
static int[] countNumbers = new int[10];
// Two arrays declared, one to hold 100 elements, the other 10.

public static void main(String[] args)
{
    programExplanation();
    generateNumbers(randomNumbers, countNumbers);
    printNumbers(randomNumbers);
    generateNumberOfOccurences(countNumbers);
    generateAsteriskPattern(countNumbers);
    findMaxValues(countNumbers);
    // Code is broken down into five method calls (to make each section more manageable and testable).
}

static void programExplanation()
{
    System.out.println("This program will generate 100 random integers between 1 and 10. " +
            "It will then output them in rows of 10.");
    System.out.println("Following this, it will output how often each integer occured in " +
            "the array.");
    System.out.println("It will also output this information in a horizontal bar chart of " +
            "asterisks.");
    System.out.println("Finally, it will output which number, or numbers appeared most often");
    System.out.println("");
}

static void generateNumbers(int[] arrayOfGenerator, int[] countArray)
{
    Random numberGenerator = new Random ();
    for (int countOfGenerator = 0; countOfGenerator < 100; countOfGenerator++) // For loop continues 100 times.
    {
        int count = numberGenerator.nextInt(10);
        countArray[count]++;
        arrayOfGenerator[countOfGenerator] = count + 1;
    }
    /*  A random number (from 0 and 9) is generated and stored in count.
     *  Whatever number is generated, that element number of countArray is incremented by 1.
     *  E.g. If random number generated = 4, fourth element of countArray (i.e. element 3) is increased by 1.
     *  The element of arrayOfGenerator (determined by the loop) is given the value of count + 1.
     *  This is because the numberGenerator creates integers from 0 and 9 - we want from 1 and 10.

     *  This coding is beneficial as both arrays are filled at the same time, which saves processing time.
     */
}

static void printNumbers(int[] arrayOfGenerator)
{
    int countOfNumbersOnLine = 0;
    for (int countOfOutput = 0; countOfOutput < 100; countOfOutput++)
    {
        if (countOfNumbersOnLine % 10 == 0)
            System.out.println("");
        //  Takes a new line when 10 integers have been printed on a line.

            if (arrayOfGenerator[countOfOutput] == 10)
            {
                System.out.print(arrayOfGenerator[countOfOutput] + "  ");
                countOfNumbersOnLine++;
            }
            else
            {
                System.out.print(arrayOfGenerator[countOfOutput] + "   ");
                countOfNumbersOnLine++;
            }

    }
    /*  Used to calculate the amount of integers on a line and the spacing between them.
     *  If the arrayOfGenerator is 10, it takes two spaces, else it takes 3 spaces (to ensure proper spacing).
     *  This is needed because the numeral 10 has two characters, as opposed to 1-9.
     */

    System.out.println("");
    System.out.println("");
}

static void generateNumberOfOccurences(int[] countOfArray)
{
    for (int countOfNumbers = 0; countOfNumbers < countOfArray.length; countOfNumbers++)
        System.out.println("The number " + (countOfNumbers + 1) + " occurs " + countOfArray[countOfNumbers] + " times.");
    System.out.println("");
    /*  For loop continues for entire length of countOfArray (i.e. 10 times)
     *  Prints out how often each random number occured.
     */
}

static void generateAsteriskPattern(int[] countOfArray)
{
    for (int countOfNumbers = 0; countOfNumbers < countOfArray.length; countOfNumbers++)
    {
        if (countOfNumbers != 9)
            System.out.print((countOfNumbers + 1) + "   ");
        else
            System.out.print((countOfNumbers + 1) + "  ");

        for (int asteriskCount = 0; asteriskCount < countOfArray[countOfNumbers]; asteriskCount++)
            System.out.print("*");
        System.out.println("");
    }
    System.out.println("");
    /*  For loop continues for entire length of countOfArray (i.e. 10 times)
     *  First if/else statement determines how many spaces are taken after numeral and before asterisks.
     *  This is used to ensure all asterisks are vertically aligned when output.
     *  Second for statement runs on each element of countOfArray, outputting asterisks for as long as array lasts.
     *  For example, if element 0 has the value 3, 3 asterisks are output.
     */
}

static void findMaxValues(int[] countOfArray)
{
    int max = 0;
    for (int counter = 0; counter < countOfArray.length; counter++)
    {
        if (countOfArray[counter] > max)
        {
            max = countOfArray[counter];
        }
    }

    System.out.print("Number(s) that appears the most:");
    /*  For loop continues for entire length of countOfArray (i.e. 10 times) and compares each value to max.
     *  If value compared is larger than the max value, it becomes new max value.
     *  By the end of this loop, largest value in the array is stored in max.
     */

    for(int checkValue = 0; checkValue < countOfArray.length; checkValue++)
        if(countOfArray[checkValue] == max)
            System.out.print(" " + (checkValue + 1));
    System.out.print(".");
    /*  For loop continues for entire length of countOfArray (i.e. 10 times)
     *  If value of element scanned is equal to the value in max, it is output.
     */
}
}

Andrew


Given an ASCII string, replace all ASCII chars with their Cyrillic equivalents The objective of the following PHP function is simple: given an ASCII string, replace all ASCII chars with their Cyrillic equivalents.

As can be seen, certain ASCII chars have two possible equivalents.

For example: if the function is fed the string "dvigatel" it should get you "двигател".

The problem: if I pass "dvigat", the function will finish its execution in a fairly reasonable amount of time.

But, if I pass "dvigatel", which is only a couple letters longer, the execution time exceeds my 30 second PHP execution time limit.

Could anybody give me a few pointers here, what's wrong with this function, why does it run so slow with an 8-char string?

function transliterate($query) {
        $map=array(
            "a" => array("а", "ъ"),
            "b" => "б",
            "c" => array("с", "ц"),
            "d" => "д",
            "e" => "е",
            "f" => "ф",
            "g" => array("г", "ж"),
            "h" => "х",
            "i" => array("и", "й"),
            "j" => array("дж", "й"),
            "k" => "к",
            "l" => "л",
            "m" => "м",
            "n" => "н",
            "o" => "о",
            "p" => "п",
            "q" => "я",
            "r" => "р",
            "s" => "с",
            "t" => "т",
            "u" => array("ъ", "ю", "у"),
            "v" => array("в", "ж"),
            "w" => "в",
            "x" => array("кс", "х"),
            "y" => array("ъ", "у"),
            "z" => "з"
        );
        $query_array = preg_split('/(?<!^)(?!$)/u', $query );

        $en_letters=array_keys($map);

        $this->processed[]=$query;

        foreach ($query_array as $letter) {
            if (in_array($letter, $en_letters)) {

                if (!is_array($map[$letter])) {
                    $query_translit=str_replace($letter, $map[$letter], $query);
                } else {
                        foreach ($map[$letter] as $bg_letter) {
                            $query_translit=str_replace($letter, $bg_letter, $query);

                            if (!in_array($query_translit, $this->transliterations)) $this->transliterations[]=$query_translit;
                        }
                    }

            } else {
                $query_translit=$query;
            }

            if (!in_array($query_translit, $this->transliterations)) $this->transliterations[]=$query_translit;
        }

        foreach ($this->transliterations as $transliteration) {
            if (!in_array($transliteration, $this->processed)) {
                if (!preg_match("/[a-zA-Z]/", $transliteration)) {
                    return;
                } else {
                    $this->transliterate($transliteration);
                }
            }
        }
    }

Why a recursive function for such a simple task?

$in = 'This is your input';
$map = 'your char translation array here';
$out = '';
for($i = 0; $i < strlen($in); $i++) {
   $char = $in[$i];
   if (isset($map[$char])) {
       if (is_array($map[$char])) {
           $newchar = $map[$char][0]; // whatever your multi-char selection logic is...
       } else
           $newchar = $map[$char];
       }
       $out .= $newchar;
   }
}


Storing a Ruby app's settings I have used code which looks a little like this in an app I'm working on:

module App
  module Settings

    class << self
      attr_accessor :console, :file
    end

    self.console ||= true
    self.file    ||= []

  end
end

I decided to use this method because I like the interface, I had the idea after reading the Singleton examples from Design Patterns in Ruby.

I can then require 'app/settings' from any file in my app and read/write the settings

p App::Settings.console
App::Settings.file << "afile.rb"
p App::Settings.file

etc...

Is this a reasonable way to keep my settings in a single file? I feel I am unnecessarily repeating myself. Any ideas for DRYing?

There is nothing really wrong with this but using a simple hash may be easier:

module App
    @@settings = {
        :console => true,
        :file => []
    }
    def self.settings
        @@settings
    end
end

Your following code would then become:

p App.settings[:console]
App.settings[:file] << "afile.rb"
p App.settings[:file]

There are several advantages to this approach:

Hashes are easy to serialise and store in files using yaml/json/etc.

require 'yaml'
p App.settings.to_yaml

You can reference App.settings in a local variable to avoid writing it out:

s = App.settings
#do stuff with s

Hash has many convenience methods for iterating over its values.

App.settings.each do |key, value|
    #do something
end

You can add to/update it succinctly using the merge! method.

App.settings.merge! :console => false, :newsetting => 'value', :etc => '...'

To get functionality like this in your method would require you to write lots of methods yourself. It's better to just use the built in functionality that Ruby has instead of reinventing the wheel.


Removing space and ChrW from cells in VBA This code is working when a paste is done it will force the formatting to be the same as in the destination sheet and will remove space and ChrW from the cells as soon as the paste event is detected. Is there any way to improve the code to work better?

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

Dim cell As Range

On Error Resume Next

    With Application.CommandBars("Standard").Controls("&Undo")
        If .ListCount < 1 Then Exit Sub '<--- this line creates an error if on error resume next is commented when reset filter is pressed
        If .List(1) <> "Paste" Then Exit Sub
    End With

    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Selection.Replace ChrW(160), " ", xlPart

    For Each cell In Selection
        cell.Value2 = WorksheetFunction.Trim(WorksheetFunction.Clean(cell.Value2))
    Next
    Application.CutCopyMode = False
    Application.EnableEvents = True
    Application.ScreenUpdating = True

End Sub

And this is the code for the reset filter:

Sub resetFilters()

Dim sht As Worksheet
Dim LastRow As Range

Application.ScreenUpdating = False

   On Error Resume Next
        If ActiveSheet.FilterMode Then
    ActiveSheet.ShowAllData
  End If

Range("A3:T3").ClearContents
Application.ScreenUpdating = True
Call GetLastRow

End Sub

On Error Resume Next is not some kind of magic that makes bugs go away. It simply hides them and makes your code completely unpredictable.

Let me repeat that, because it's of the utmost importance....

On Error Resume Next makes your code completely unreliable, unpredictable, and unsafe. Once an error occurs, your program is left in an unknown and invalid state, but continues to chug along pretending everything is fine. In the meantime, totally wrecking all your data.

So, remove that evil line of code, take the time to recreate the error, figure out why it's happening, then fix it. Once you've done that, come back and we'd be happy to help you turn your "good enough" code into Good Code™.


Efficient human readable timedelta I wanted to write a human readable datetime.timedelta that can be used in log files.

Eg, "Report issued 1 hour, 44 minutes, 20 seconds ago"

I noticed that casting a timedelta to str() generates something almost like what I want, but not quite.

To this end I wrote this:

def verbose_timedelta(delta):
    hours, remainder = divmod(delta.seconds, 3600)
    minutes, seconds = divmod(remainder, 60)
    dstr = "%s day%s" % (delta.days, "s"[delta.days==1:])
    hstr = "%s hour%s" % (hours, "s"[hours==1:])
    mstr = "%s minute%s" % (minutes, "s"[minutes==1:])
    sstr = "%s second%s" % (seconds, "s"[seconds==1:])
    dhms = [dstr, hstr, mstr, sstr]
    for x in range(len(dhms)):
        if not dhms[x].startswith('0'):
            dhms = dhms[x:]
            break
    dhms.reverse()
    for x in range(len(dhms)):
        if not dhms[x].startswith('0'):
            dhms = dhms[x:]
            break
    dhms.reverse()
    return ', '.join(dhms)

Essentially, it's shaving off both ends of a list to make the results more meaningful.

The code above feels clunky though. Is there a more "Pythonic" way to do it? I'm using Python 2.7.3.

Use a ternary operator in "s"[seconds==1:].
Use a generator expression to replace the xstr = "%s... lines.
The two for loops should use enumerate(), i.e. they could be for s, i in range(...).
The two for loops should be moved into a for _ in range(2):.
i is preferred over x when using an incrementing index counter.
The filtering of the redundant strings which the for-loop does could be done earlier so that the number to string code can be modified but the filtering code will not require adjustments.

PS: I have implemented a similar function here:

    days, rem = divmod(seconds, 86400)
    hours, rem = divmod(rem, 3600)
    minutes, seconds = divmod(rem, 60)
    if seconds < 1:seconds = 1
    locals_ = locals()
    magnitudes_str = ("{n} {magnitude}".format(n=int(locals_[magnitude]), magnitude=magnitude)
                      for magnitude in ("days", "hours", "minutes", "seconds") if locals_[magnitude])
    eta_str = ", ".join(magnitudes_str)


Randomly pick key from dictionary weighted by the keys value I was trying to write a function that would get a random card from a dictionary of cards in a players deck weighted by the number of cards of each type you have.
I don't know if the solution I came up with is the fates way of doing something like this and this is something I'm going to be doing a lot per turn because of certain mechanics in the games design. Please let me know if there is something i missed that could help the performance of this code.

For some contexts _deck._card is a dictionary with the key being a string and the value being a int. Dictionary<string, int> _cards

Also Nez.Random is a singleton of my random class for the project. Random.nextInt(int max) takes in a int for the max value of the random number.

static public int nextInt( int max )
{
    return random.Next( max );
}

Code:

public string GetRandomCard()
{
    string result = "";
    var totalWeight = 0;

    foreach (var cardNumber in _deck._cards)
        totalWeight += cardNumber.Value;

    var randNumber = Nez.Random.nextInt(totalWeight);

    foreach (var cardNumber in _deck._cards)
    {
        var value = cardNumber.Value;

        if (randNumber >= value)
        {
            randNumber -= value;
        }
        else
        {
            result = cardNumber.Key;
            break;
        }
    }
    return result;
}

Time complexity O(N^2)
I dont think there is a way to decrease the time complexity but there might be other ways to optimize this code.

After all this good discussion It looks like this function is as optimize as its going to get for its intended purpose.
I want to note that I was incorrect in its time complexity. The time complexity is actually O(N) not O(N^2) luckily.

Considering this algorithm can perform 1,000 random selections in 0.00194s on a mid range workstation It shouldn't have any performance impact if used many times.

Here are the results of the unit test:

Running random 100 times: 0.0013367s
Key: 30%, Value: 35
Key: 60%, Value: 60
Key: 10%, Value: 5

Running random 1000 times: 0.0019477s
Key: 30%, Value: 378
Key: 60%, Value: 618
Key: 10%, Value: 104

Running random 10000 times: 0.0202942s
Key: 30%, Value: 3391
Key: 60%, Value: 6635
Key: 10%, Value: 1074

Running random 100000 times: 0.191678s
Key: 30%, Value: 33669
Key: 60%, Value: 66427
Key: 10%, Value: 11004

Running random 1000000 times: 1.77491s
Key: 30%, Value: 334075
Key: 60%, Value: 666087
Key: 10%, Value: 110938

Running random 10000000 times: 18.43691s
Key: 30%, Value: 3332173
Key: 60%, Value: 6667785
Key: 10%, Value: 1111142


CakePHP image upload component I would like to know how I can improve this CakePHP 3.0 Component (inside folder controller)

1st: to use external libs (stored on vendor folder) I'm using the require keyword and include the class using use keyword, like this:

require_once(ROOT . DS . 'vendor' . DS . 'CakePHP-ImageTool-Component' . DS . 'ImageTool.php');

and

use ImageTool;

2nd: in method saveFileLFS I'm using true or false to flag OK.

<?php
namespace App\Controller\Component;

require_once(ROOT . DS . 'vendor' . DS . 'CakePHP-ImageTool-Component' . DS . 'ImageTool.php');

use Burzum\FileStorage\Lib\StorageManager;
use Cake\Controller\Component;
use ImageTool;

class UploadFileComponent extends Component
{
    function resizeImage($settings)
    {
        $status = ImageTool::resize([
            'input' => $settings['input'],
            'output' => $settings['output'],
            'width' => $settings['width'],
            'height' => $settings['height'],
            'mode' => $settings['mode']
        ]);
        return $status;
    }

    public function saveFileLFS($stringSeparator, $storeName, $productName)
    {
        $key = $storeName . $stringSeparator . $productName . $stringSeparator .
            $this->request->data['Media']['file']['name'];
        if(StorageManager::adapter('Local')->write($key,
            file_get_contents($this->request->data['Media']['file']['tmp_name']))){
            return true;
        }else
       {
            return false;
        }
    }
}

If you have another point to improve, please comment or answer, I will change the accepted answer

To change the require I use the composer.json classmap like this (follow this answer: Import Class without autoload and repository):

"autoload": {
    "classmap": [
        "./vendor/CakePHP-ImageTool-Component"
    ]
}

I don't use require now.

In saveFileLFS method now I'm using Exception if something don't work.

public function saveFileLFS($stringSeparator, $storeName, $productName)
{
    $key = $storeName . $stringSeparator . $productName . $stringSeparator .
        $this->request->data['Media']['file']['name'];
    if(!StorageManager::adapter('Local')->write($key,
        file_get_contents($this->request->data['Media']['file']['tmp_name']))){
        throw new MissingHelperException();
        //Or other Exception
    }
}


Get an array of strings separating a string on N character incrementally So I asked a question over here, and even though I placed it in the wrong place (it should have been here), Redu was nice enough to give me a nice answer to my question.

Here is the original question for reference:

given the following input:

"/bob/ross/is/awesome/squirrels"

I would like to end up with:

[
    "/bob/ross/is/awesome/squirrels",
    "/bob/ross/is/awesome",
    "/bob/ross/is",
    "/bob/ross",
    "/bob"
]

The answer that Redu was nice enough to post was as follows:

res = s => s.length ? [s].concat(res(s.match(/.*(?=\/.+$)/)[0])) : [];

However he also commented the following:

  I honestly wouldn't advise recursion in JS, however since you have asked for it, i have provided a recursive answer down below.

Which makes me wonder, is there a better way to do this without recursion, but just as succinctly?

My original non-recursive function is as follows:

getWords = function( string ){

    // Get an array of all words in the provided string separating on "/"
    let words = string.split("/");

    // Filter out empty strings from leading/trailing "/" characters
    words = words.filter( function(a){return a !== ""} );

    // Create an array to store results in
    let results = [];

    // Create an iteration for each word in the array
    for (var i=0, j=words.length; i<j; i++){

        // Create a string to concatenate to
        let result = "";

        // Loop over each word in the array minus the current iteration of "i"
        for (var k=0, l=words.length - i; k<l; k++){

            // Contatenate using the special character plus the current word
            result += "/" + words[k];
        }

        // Push the resulting string to the results array
        results.push(result);
    }

    // Set the results to display
    resultsDisplay.innerHTML = results.toString().replace(/,/g, " -- ");

    // Return the results array
    return results;
}

input = document.getElementsByTagName("input")[0];
resultsDisplay = document.getElementsByClassName("results")[0];
input.addEventListener("input", function(){
getWords(input.value)
});
getWords(input.value);
<input value="bob/ross/is/awesome/squirrels" />

This code is, admittedly, awful, hence my original post.

Which makes me wonder, is there a better way to do this without
  recursion, but just as succinctly?

Recursion can be an issue if your input size is huge and you recurse often and your JavaScript engine can't perform tail call optimization. Due to limited call stack size, you might then encounter a Maximum call stack size exceeded error. You might also want to avoid recursion if performance is a concern, as the function call overhead is not marginal in JavaScript.

So, how do you eliminate the recursion?

First of all, in order to gain a clear understanding of Redu's minified one-liner:

res = s => s.length ? [s].concat(res(s.match(/.*(?=\/.+$)/)[0])) : [];

I recommend to rewrite it into a self-documenting but otherwise equivalent function:

function expand(path) {
  if (path.length == 0) return [];
  let result = [path];

  path = path.match(/.*(?=\/.+$)/)[0];
  result = result.concat(expand(path));

  return result;
}

Now, we can analyze how that function works:

It appends the current path to the result
It replaces the current path with the parent path
It recurses to find a solution for the parent path
It appends the complete path to the solution for the parent path

It is always possible to turn a recursive function into a non-recursive function. We just need to manually keep track of already found paths and the current parent path:

function expand(path) {
  let result = [];

  while (path.length > 0) {
    result.push(path);
    path = path.match(/.*(?=\/.+$)/)[0];
  }
  return result;
}

console.log(expand("/bob/ross/is/awesome/squirrels"));

This non-recursive solution works as-is for empty strings without having to check for this base case.

We could even minify it again, which I don't recommend:

const res=(s,r=[s])=>{while(s.length)r.push(s=s.match(/.*(?=\/.+$)/)[0]);return r};

In a practical setting, I would recommend a more generative and more robust approach which, in a first step, extracts all valid path segments from the input. In a second step, you would then build the cumulative paths from those segments.

Using Array.map():

function expand(path, prefix = '') {
  return path.match(/[^\/]+/g).map((segment) =>
    prefix += '/' + segment
  ).reverse();
}

console.log(expand("/bob/ross/is/awesome/squirrels"));

Using Array.reduce():

function expand(path, prefix = '') {
  return path.match(/[^\/]+/g).reduce((path, segment) => {
    prefix += '/' + segment;
    path.unshift(prefix);
    return path;
  }, []);
}

console.log(expand("/bob/ross/is/awesome/squirrels"));


Reading a certain pixels color from a window I'm currently reading a certain pixels color from a process window like this:

[DllImport("gdi32.dll")]
private static extern int BitBlt(IntPtr srchDc, int srcX, int srcY, int srcW, int srcH,
                                 IntPtr desthDc, int destX, int destY, int op);

public static Color GetPixel(IntPtr hwnd, int x, int y)
{
    var screenPixel = new Bitmap(1, 1);

    using (Graphics gdest = Graphics.FromImage(screenPixel))
    {
        using (Graphics gsrc = Graphics.FromHwnd(MemoryHandler.GetMainWindowHandle()))
        {
            IntPtr hsrcdc = gsrc.GetHdc();
            IntPtr hdc = gdest.GetHdc();
            BitBlt(hdc, 0, 0, 1, 1, hsrcdc, x, y, (int)CopyPixelOperation.SourceCopy);
            gdest.ReleaseHdc();
            gsrc.ReleaseHdc();
        }
    }

    return screenPixel.GetPixel(0, 0);
}

It seems to be working.

I would like to know if I'm disposing everything that I use correctly to avoid memory leaks. If I inspect my app in the task manager I notice that the memory use is increasing a bit for each time i fetch a pixel, but I suppose the garbage collector will kick in when it's supposed to?

Also, is there any obvious "better" way of doing this?

You'll actually need to wrap the Bitmap in a using as well because its base class, Image, implements IDisposable:

public static Color GetPixel(IntPtr hwnd, int x, int y)
{
    using (Bitmap screenPixel = new Bitmap(1, 1);
    {
        using (Graphics gdest = Graphics.FromImage(screenPixel))
        {
            using (Graphics gsrc = Graphics.FromHwnd(MemoryHandler.GetMainWindowHandle()))
            {
                IntPtr hsrcdc = gsrc.GetHdc();
                IntPtr hdc = gdest.GetHdc();
                BitBlt(hdc, 0, 0, 1, 1, hsrcdc, x, y, (int)CopyPixelOperation.SourceCopy);
                gdest.ReleaseHdc();
                gsrc.ReleaseHdc();
            }
        }

        return screenPixel.GetPixel(0, 0);
    }
}

Further, the garbage collector works almost immediately. Or more simply put, if you use a memory allocation tool like MemProfiler, the memory will get cleaned up by GC faster than you can collect it with a snapshot. The Bitmap is likely where you are seeing the little tick.

It may also be worth wrapping the API call in a try ... finally:

IntPtr hsrcdc = gsrc.GetHdc();
IntPtr hdc = gdest.GetHdc();

try
{
    using (Graphics gsrc = Graphics.FromHwnd(MemoryHandler.GetMainWindowHandle()))
    {
        BitBlt(hdc, 0, 0, 1, 1, hsrcdc, x, y, (int)CopyPixelOperation.SourceCopy);
    }
}
finally
{
    gdest.ReleaseHdc();
    gsrc.ReleaseHdc();
}

to ensure the ReleaseHdc gets called.


Comparing columns from two CSV files This question has a second part here:

Comparing columns from two CSV files - follow-up

I'm currently trying to compare two CSV files column by column but only when their indexes match.

Here is what I've done:

import sys

def get_column(columns, name):
    count = 0
    columns = columns.split(';')
    for column in columns:
        array = list(column)
        if(array[0] == '"'):
            array[0] = ''
        if(array[len(column) - 1] == '"'):
            array[len(column) - 1] = ''
        column = ''.join(array)
        if column != name:
            count += 1
        else:
            return(count)

def set_up_file(file, variable):
    columns = next(file)
    siren_pos = get_column(columns, 'INDEX1')
    nic_pos = get_column(columns, 'INDEX2')
    variable_pos = get_column(columns, variable)
    return(siren_pos, nic_pos, variable_pos)

def test_variable(variable):
    with open('source.csv', 'r') as source:
        sir_s, nic_s, comp_s = set_up_file(source, variable)
        with open('tested.csv', 'r') as tested:
            sir_t, nic_t, comp_t = set_up_file(tested, variable)
            correct = 0
            memory = 0
            for row_s in source:
                row_s = row_s.split(';')
                tested.seek(0, 0)
                count = 0
                for row_t in tested:
                    count += 1
                    if(count >= memory):
                        row_t = row_t.split(';')
                        if(row_s[sir_s] == row_t[sir_t] and row_s[nic_s] == row_t[nic_t]):
                            if(row_s[comp_s] == row_t[comp_t]):
                                correct += 1
                                memory = count
                            break
            tested.seek(0, 0)
            print(correct / sum(1 for line in tested) * 100)

def main():
    test_variable('VARIABLE_TO_COMPARE')

if(__name__ == "__main__"):
    main()

This script is running into some performance issue and I would like to know how to make it run faster / make it more readable.

First I'd like to call out the good things that you've done:

Writing functions, including a standard main().
Having more or less sensible function and variable names.
Proper use of with.

An improvement here is to stop parsing the file yourself, and to start parsing it with Python's native csv library. Even though your format is not technically CSV (it's separated by semicolon), you can still configure csv to use a different delimiter.

I recommend the use of the DictReader class. Given that you only pay attention to one variable, just use csv.reader. During initialization, get the index of the column you want, and then use that on every record that the reader gives back.

The critical performance issue here is that you have nested loops for row comparison. Given that you are comparing two series expected to be in the same order, but with edits (insertions and deletions), effectively you're doing a diff. Read this for a nice walkthrough.

http://www.xmailserver.org/diff2.pdf


Large table mousehover effects HTML

Where data-parent points to the data-id of another row.

JQUERY/JS

$("tr[data-parent='" + id + "']").css("background-color", "#1ba1e2").each(function () {
    $("tr[data-parent='" + $(this).attr("data-id") + "']").css("background-color", "#75F7FF").each(function () {
        $("tr[data-parent='" + $(this).attr("data-id") + "']").css("background-color", "#98E8E3").each(function () {
            $("tr[data-parent='" + $(this).attr("data-id") + "']").css("background-color", "#B6DCE8")
    });});});

(I know that I could have written a recursive function as well, that's not the point right now)

When someone mouse-overs a TR, I want that row to be one color, all the rows that has the hovered row as parent, another color, the grandchildren another color etc (for several levels deep).

Now, for 100 rows, this isn't a problem, but my data contains around 30 000 rows, and that's where it starts to go wrong, several-seconds-of-freezing-wrong.

How can I optimise this code?

jsFiddle

The problem with the current approach, as I'm sure you already know, is the very high number of DOM elements that the JavaScript engine is navigating through and assessing before the end of the process (bearing the 30,000 row scenario in mind).

And the jQuery selector function has a fair bit of work to do, checking every row element to see if it has the data attribute value or not, before it can even begin the highlighting. A taxing process. DOM manipulation is notoriously hard on performance.

My answer would be that the mapping between row and parent, which is what essentially needs to be calculated for each row when hovered, needs to be taken out of the DOM entirely so this calculation doesn't require any work with the DOM at all.

See my solution below, and this JSFiddle: https://jsfiddle.net/dawdg4sj/4

Ultimately, you'll see that on each row hover, the 'calculateHighlighting' function is called which performs the calculation "off-line" so to speak, away from the DOM (data attributes withdrawn) and instead uses the local 'map' object (row IDs on the left, parent row IDs on the right), and returned is a straight-forward array of objects each describing an element by ID and a class to add for highlighting purposes.

Bottom line, you need to be able to generate the row id/parent mapping outside the DOM and inside JavaScript instead. As long as you have this mapping information inside the DOM and wish to process 30,000 rows, it'll be a sluggish affair.

(Note: My solution uses function calls from the Lodash library for convenience, i.e. '.keys(.pick(...'. They are optional, keep or swap.)

Finally, and separately, I would express my surprise at such a large number of rows. Working with so many elements in a single view of an app via JavaScript is always going to lead to slow processing to one extent or another. But obviously so many rows could not be visible within the viewport all at once, so I would further suggest that part of the solution is to limit the highlighting to those within view and just outside only, and update on scroll, or even paginate the rows. It depends on the context of the project of course, but those are my supplementary thoughts.)

// ---
// New-look HTML
// ---

        test

        tes

        tes

        test

        tes

        tes

        test

// ---
// New-look JS
// ---

var map = { // row id -> parent row id
    1: 0,
    2: 1,
    3: 1,
    4: 3,
    5: 3,
    6: 1,
    7: 0
};
var highlightColours = 4;

// calculateHighlighting
// Description: Establish a straight-forward array of highlighting rules (classes to add to elements)
// e.g. [ {element: '#row-1', class: 'row-colour-1'}, {element: '#row-2', class: 'row-colour-4'} ... ]

function calculateHighlighting(rootElementID) {
    var result = [];
    var elementIDPrefix = '#row-';
    var elementColourClassPrefix = 'row-colour-';

    var parentRows = [];
    var id = rootElementID.split('-')[1]; // (e.g. '#row-1' -> 1)
    parentRows.push(id); // Start with the root (hovered) element

    var colourIndex = 1; // Keep track of highlight colours used

    while(parentRows.length > 0 || colourIndex <= highlightColours) { // While there are parent rows to highlight (or we run out of highlight colours)
        var childRows;
        for(var i = 0; i < parentRows.length; i++) {
            // Fetch related rows based on id/parent mapping (using lodash function, _.keys, for convenience)
            childRows = _.keys(_.pick(map, function(parentID) {
                return parentID == parentRows[i];
            }));
            if(childRows.length > 0) { // Highlight found parent rows
                for(var j = 0; j < childRows.length; j++) {
                    result.push({
                        element: elementIDPrefix + childRows[j],
                        class: elementColourClassPrefix + colourIndex
                    });
                }
            }
        }
        parentRows = childRows; // Just highlighted parent rows, become base for next parent search
        colourIndex++; // Next highlight colour
    }

    return result;
}

$('tr').on('mouseover', function () {
    var rowID = $(this).attr('id');
    var rowsToHighlight = calculateHighlighting(rowID);
    for(var i = 0; i < rowsToHighlight.length; i++) {
        $(rowsToHighlight[i].element).addClass(rowsToHighlight[i].class);
    }
});

$('tr').on('mouseleave', function () {
    $('tr').attr('class', '');
});

// ---
// New-look CSS
// ---
tr:hover {
    background-color: red;
}
.row-colour-1 {
    background-color: #1ba1e2;
}
.row-colour-2 {
    background-color: #75F7FF;
}
.row-colour-3 {
    background-color: #98E8E3;
}
.row-colour-4 {
    background-color: #B6DCE8;
}


Dependency Injection tool for c++ I'm trying to create some tool for easier dependency injection in c++14, here is one of my concepts using std::tuple.

template <class... Types>
class injector
{
public:
    injector(Types... i) { injected_ = std::tuple<Types...>(i...); }

protected:
    template <class T>
    T *injected()
    {
        return std::get<T *>(injected_);
    }

private:
    std::tuple<Types...> injected_;
};

using component_injector = injector<scene *, entity *, foo *>;
class component : component_injector
{
public:
    component(component_injector injected) : component_injector(injected) {};
    virtual ~component(){};

    void method()
    {
        injected<scene>()->method();
        injected<foo>()->method();
    }
};

int main(int argc, char *argv[])
{
    scene s;
    entity e;
    foo f;

    component c({&s, &e, &f});
    c.method();

    return 0;
}

Is it good idea to create dependence this way? It has some limitations, for example I cannot have two dependencies of the same type etc. but code of useful part (component class) seems to be much clearer.
The injected types should be replaced by interfaces but the real types are used directly here for simplicity.

I don't think that makes it easier (or cleaner)
You are injecting pointers (which begs to ask ownership)
You should be preferring encapsulation over inheritance.

If I was doing it normally:

class component
{
    Scene&   scene;
    Entity&  entity;
    Foo&     foo;
    public:
    component(Scene& s, Entity& e, Foo& f)
        : scene(s)
        , entity(e)
        , foo(f)
    {}
    virtual ~component(){};

    void method()
    {
        scene->method();
        foo->method();
    }
};

int main()
{
    scene s;
    entity e;
    foo f;
    component c(s, e, f});

    c.method();
}

This seems much cleaner than your method (where you introduce another level of inheritance).


Concisely, robustly, temporarily stashing c++ iostream formats I got tired of the boilerplate and tedium restoring formatting context, so I made an RAII stasher that relies on the destroy-at-end-of-full-statement temporary semantics. With C++17 I can get it down to a single type, no helpers:

#include <iostream>
template< template<typename, class> class streamtmp, typename charT, class traits >
struct fmtstash {
    typedef streamtmp<charT,traits> streamtype;
    streamtype &from;
    std::ios_base::fmtflags flags;
    std::streamsize width;
    std::streamsize precision;
    charT fill;
    fmtstash(streamtype &str)
        : from(str), flags(str.flags()), width(str.width())
        , precision(str.precision()), fill(str.fill())  {}
    ~fmtstash() { from.flags(flags), from.width(width), from.precision(precision), from.fill(fill); }
    template<typename T> streamtype  &operator<<(const T &rhs) { return from << rhs; }
    template<typename T> streamtype  &operator>>(      T &rhs) { return from >> rhs; }
};

int main(int c, char **v)
{
    using namespace std;
    fmtstash(cout) << hex << 51901 <<'\n';
    cout << 51901 <<'\n';

    return 0;
}

which produces about the easiest-to-write-and-read formatting I can manage.

Am I missing any bets here? Shorter or more robust is what I'm after.

The biggest improvement I found since I first wrote it is to use from << rhs instead of the from.operator<<(rhs) I had, since user-defined stream insert/extractors have to be free functions and of course an explicit member callout breaks those.

I could make the template args simpler, just template<class streamtmp>, the full breakout is a holdover from when I had the internal reference as ios_base<charT,traits> &from; instead of the full original type, but I ran across a broken inserter that wanted to be first or only and took an ostream reference as its left operand.

And of course the boost savers don't supply the inserter and extractor templates at all.  Is this an oversight or is there some argument against using the destroy-at-end-of-full-expression semantics this way?

One tiny improvement I'd suggest is that the stored state can all be declared const:

const std::ios_base::fmtflags flags;
const std::streamsize width;
const std::streamsize precision;
const charT fill;

There are no implications for assignability of fmtstash objects, as we already had a reference member.


Code Smell in Unit Testing Serialized Classes I can feel that something is wrong with my code. Here's an excerpt where I think the code smell is...

    internal Hazen(IRawDataReceiver rawDataReceiver) : base(rawDataReceiver)
    {
        SerialPortConnection = rawDataReceiver as SerialPortConnection;
    }
    private SerialPortConnection _serialPortConnection;
    [DataMember] private SerialPortConnection SerialPortConnection
    {
        get
        {
            return _serialPortConnection;
        }
        set
        {
            if(value != null)
            {
                _serialPortConnection = value;
                RawDataReceiver = value;
            }
        }
    }

So basically, Hazen inherits from an abstract class. Notice how I pass the rawDataReceiver to its base class when Hazen's constructor is called. But during deserialization, the constructor is bypassed, thus I included the RawDataReceiver = value to the set accessor of the DataMember property. That is where the code smell is. Is there a better way to do it and am I violating any good practices (such as DRY)?

As regards to unit testing. As you can see, Hazen has a property of type SerialPortConnection as opposed to having it as IRawDataReceiver since you can't serialize an interface. Now, I can still test this class since the base class exposes everything I need for testing. But going back to the previous paragraph, I feel like I'm doing something wrong here.

EDIT: Here's a snippet of the base class

    internal TideDevice(IRawDataReceiver rawDataReceiver)
    {
        RawDataReceiver = rawDataReceiver;
    }
    private IRawDataReceiver _rawDataReceiver;
    protected internal IRawDataReceiver RawDataReceiver
    {
        get
        {
            return _rawDataReceiver;
        }
        protected set
        {
            _rawDataReceiver = value;
            RawDataReceiver.RawDataReceived += RawDataReceiver_RawDataReceived;
        }
    }

EDIT: I have extracted the Task to another method to remove the responsibility of the set accessor. But I'm still in a roadblock on fixing the code smell I initially stated. I editted the code to reflect what I have now.

EDIT2: This code is working really fine whether in unit test and integration test, so I'll leave it as is until someone points out that there is something wrong here or until a problem arises. Thank you very much!

I think the bigger issue is that you have an operation with side-effect in the property setter SerialPortConnection,  which means that if your unit test fails, it will not be clear whether it is due to a problem with deserialisation or with the operation.

I would suggest moving the operation (i.e. writing "R" to the port) in a separate Initialize method that you can test individually.


Searching for log entries in SQLite that involve a particular IP subnet This reads a huge database (SQLite) and extract any IP address similar to ip_base:

for num_db=1:10
 i=1;

%     connection to different db
db_name='C:\Users\galotibi\Documents\MATLAB\user';
obj_name='jdbc:SQLite:C:\Users\galotibi\Documents\MATLAB\user';

databases=strcat(db_name,num2str(num_db),'.db');
objects=strcat(obj_name,num2str(num_db),'.db');
conn=database(databases,'','','org.sqlite.JDBC',objects);
% ----------------------------------------------------------------------

num_row='select count(*)from IP_logs';
results=exec(conn,num_row);
results=fetch(results);
Fcount=results.Data;
count=cell2mat(Fcount);
start_a=0;
start_b=10000;
% 10000 to know how many round do we need, data is huge
row=count/start_b;
row=ceil(row);
% rounding the value
query_base='select * from IP_logs where rowid>';
wiki=cell(1,7);
for ii=1:row

        % to take 10000 record every time
        query_1=strcat(query_base, num2str(start_a+(ii-1)*start_b));
        query_2=strcat(query_1, ' and rowid<=');
        query_3=strcat(query_2, num2str(start_b*ii));

        results= exec(conn,query_3);
        results=fetch(results);

        ip_base='91.198.174.';

            for n=1:10000
                if n>size(results.Data,1)
                break;
                end;
  if strncmp(results.Data(n,2), ip_base,length(ip_base)) ||     strncmp(results.Data(n,4), ip_base,length(ip_base))

                    wiki(i,:)=results.Data(n,:);
                    i=i+1;

                end

            end;

        clear results;

Databases are meant for efficient searching: you formulate a query to specify exactly what data you want to retrieve.  What you are doing is retrieving the entire contents of the table, to be filtered in MATLAB.  That defeats the purpose of the database — you might as well have stored the data in a CSV file.

Just write a query that looks like

SELECT *
    FROM IP_logs
    WHERE src_addr LIKE '91.198.174.%' OR dest_addr LIKE '91.198.174.%';

Depending on what your database schema looks like, you might be able to do even better.  Indexes on the relevant columns would help.  Storing IPv4 addresses as 32-bit integers would result in even faster searches.