Manhattan Distance + Linear Conflicts scoring function for sliding tile puzzle solver I have written yet another n-puzzle solver.  I used the A* algorithm to search with the Manhattan Distance heuristic.  It worked very well for the 9-puzzle (3x3) but was unusable for most instances of the 15-puzzle (4x4.)  In fact I would exhaust all RAM before I got anywhere near the result.

So I switched to IDA* and the Manhattan Distance + Linear Conflicts heuristic.  Now I can solve most puzzles reasonably fast.  (e.g. one with a 50-move solution takes about 8s.)  However more difficult puzzles (the worst case for the 15-puzzle is 80 moves.) are still much too slow.  Profiling my code with Callgrind I saw that by far the most expensive part of the algorithm is calculating the heuristic which has to be done once for every node.  Any win here would be a tremendous improvement to the solvers overall speed.  So I present to you the current state of my code.  Can it be improved?

One thing you need to know to understand this function is that Board is a struct
with three members.  _height and _width which are the dimensions of the board.  and _tiles which is one-dimensional C-style array of uint8_t which represents the state of the board with each cell containing a number from 1 to (_height * _width - 1) or 0 to represent the blank square.

int ManhattanLinearConflict(Board& b) {
    int md = 0;

    // Pre-compute goals.
    uint8_t goalRow[b._height * b._width];
    uint8_t goalCol[b._height * b._width];

    for (uint8_t i = 0, length = b._height * b._width; i < length; i++) {
        if (b._tiles[i] == 0) {
            continue;
        }
        goalRow[i] = (b._tiles[i] - 1) / b._height;
        goalCol[i] = (b._tiles[i] - 1) % b._width;
    }

    for (auto row = 0; row < b._height; row++) {
        for (auto col = 0; col < b._width; col++) {
            // This part is just Manhattan distance.
            auto i = row * b._width + col;
            if (b._tiles[i] == 0) {
                continue;
            }
            md += abs(long(row - goalRow[i]));
            md += abs(long(col - goalCol[i]));

            // Two tiles I and J are in a linear conflict if I and J are
            // in the same line, the goal positions of I and J are both in
            // that line, I is to the right of J and goal position of I is
            // to the left of the goal position of J.
            if (goalRow[i] != row) {
                continue;
            }
            for (uint8_t j = row * b._width, l = j + b._width; j < l; j++) {
                if (j == i || b._tiles[j] == 0) {
                    continue;
                }
                if (goalRow[j] == row && b._tiles[i] > b._tiles[j] &&
                goalRow[i] < goalRow[j]) {
                    md += 2;
                }
            }
        }
    }

    return md;
}

Bug: if statement always false

I'm confused about this if statement:

            if (goalRow[j] == row && b._tiles[i] > b._tiles[j] &&
                    goalRow[i] < goalRow[j]) {
                md += 2;
            }

and in particular the last condition goalRow[i] < goalRow[j].  When the last condition is checked, we already know that goalRow[i] == row (from above) and goalRow[j] == row (from the first condition), so that last condition will always be false.  I think that perhaps you meant to use goalCol instead:

            if (goalRow[j] == row && b._tiles[i] > b._tiles[j] &&
                    goalCol[i] < goalCol[j]) {
                md += 2;
            }

Bug 2: if statement still always false

After looking over the corrected if statement another time, I found that it is still always false.  The problem is that b._tiles[i] > b._tiles[j] implies that goalCol[i] > goalCol[j] because goalCol[x] is derived in a linear fashion from b._tiles[x].  I believe that you really meant this:

            if (goalRow[j] == row && i > j && goalCol[i] < goalCol[j]) {
                md += 2;
            }

But the simpler way to do this is to just make your loop run from the start of the row to i, like this:

        for (uint8_t j = row * b._width, l = j + col; j < l; j++) {
            if (b._tiles[j] == 0) {
                continue;
            }
            if (goalRow[j] == row && goalCol[i] < goalCol[j]) {
                md += 2;
            }
        }

I'm surprised that your program seemed to run faster after only changing goalRow to goalCol.  You may want to print something out if it ever reaches the md += 2 line to make sure you are ever getting there.  I ran a little test program to generate random boards and it never hit that statement until I fixed it with both fixes.


Pass logger dependency to PostSharp aspect I'm trying to pass my logger to a Logging Aspect that I've created with PostSharp. I have one logger per job run (because I need to send each job's logs to different folders), each job runs in different threads using  BackgroundWorker. This is the code that I have:

[Serializable]
public class LogExecutionTimeAttribute : OnMethodBoundaryAspect
{
    [NonSerialized]
    public static Func<ILog> GetLogger;

    private string methodName;

    public override void CompileTimeInitialize(MethodBase method, AspectInfo aspectInfo)
    {
        this.methodName = method.Name;
    }

    public override void OnEntry(MethodExecutionArgs args)
    {
        args.MethodExecutionTag = Stopwatch.StartNew();
        base.OnEntry(args);
    }

    public override void OnExit(MethodExecutionArgs args)
    {
        Stopwatch sw = (Stopwatch)args.MethodExecutionTag;
        sw.Stop();
        var logger = GetLogger();
        logger.DebugFormat(
            "{0} executed in {1} seconds.",
            this.methodName,
            sw.ElapsedMilliseconds / 1000.0);
        base.OnExit(args);
    }
}

Each thread would do:

var log = CreateLoggerForJobId(jobId);
LogExecutionTimeAttribute.GetLogger = () => log;

I'm doubtful about the Func<ILog> GetLogger being static - How will it work when called from multiple threads? My manual tests seem to suggest that this is working, but I'm not sure if this is thread safe. I am also not sure if using NonSerialized attribute on GetLogger helps.

Entire working code is here:

https://gist.github.com/rnarayana/5518327f9c7c94f8c2fde4ae7fee7733

You're reading and writing the GetLogger static field on multiple threads simultaneously, so the shown code is not thread-safe. You can mark the field with the [ThreadStatic] attribute to have a unique value on each thread.

Also, you don't need to mark the field as [NonSerialized] because static fields are not serialized anyway (https://stackoverflow.com/questions/3903079/does-default-serialization-in-c-sharp-serialize-static-fields).


Standard delegate methods I'm aware standard delegate method declaration should include the object that triggers the method, usually of the class that declared the protocol.

That's fine when the delegate method includes further arguments down the line, but what if it doesn't?

Example:

@protocol MyObjectDelegate <NSObject>

- (void)myObject:(id)object didPerformActionWithResult:(NSObject*)resul;
- (void)myObject:(id)object didPerformActionWithoutResult;
@end

I know the second method isn't accepted, its missing an argument, but what do I put there? A BOOL that I know will always be true?

Do I change |didPerformActionWithoutResult| to performedAction:(BOOL)yesOrNo?

Or move the object reference to the end?

Have a look at TableView's datasource protocol:

– tableView:cellForRowAtIndexPath:
– numberOfSectionsInTableView:
– tableView:numberOfRowsInSection:
– sectionIndexTitlesForTableView:

I find it quite hard to come up with nice names for such sidle parameter method signature and code completion would be easier, if the form, you propose would exists, but if a method accepts parameters, it name must terminate with one of therm — if it just have one, it must be at the end.

Your idea of adding a dead parameter is IMO very bad design. Another parameter should lead possibly to another behavior. Also it would work against objective-c strength of self explaining method name. countOfSubscribersForNewsEmitter:(MyNewsEmitter *)emitter make sense, while newsEmitter:(MyNewsEmitter *)emitter countOfSubscribers:(BOOL) aBool does not make much sense, as the bool disallows us to form a correct mental model of the purpose of this method.


Generate random letter by frequency Which method below should I use to generate a "random letter by frequency" using Javascript?

From https://gist.github.com/furf/2413792

var randomAtoZ = (function (lookup) {
    return function () {
        var random = Math.random() * 100000,
                letter;
        for (letter in lookup) {
            if (random < lookup[letter]) {
                return letter;
            }
        }
    }
})({
    // Ranges calculated from data found at
    // http://en.wikipedia.org/wiki/Letter_frequency
    a: 8167,  b: 9659,  c: 12441, d: 16694,
    e: 29396, f: 31624, g: 33639, h: 39733,
    i: 46699, j: 46852, k: 47624, l: 51649,
    m: 54055, n: 60804, o: 68311, p: 70240,
    q: 70335, r: 76322, s: 82649, t: 91705,
    u: 94463, v: 95441, w: 97801, x: 97951,
    y: 99925, z: 100000
});

My re-write

function randomAtoZ() {
    // Ranges calculated from data found at
    // http://en.wikipedia.org/wiki/Letter_frequency
    var lookup = {
        a: 8167,  b: 9659,  c: 12441, d: 16694,
        e: 29396, f: 31624, g: 33639, h: 39733,
        i: 46699, j: 46852, k: 47624, l: 51649,
        m: 54055, n: 60804, o: 68311, p: 70240,
        q: 70335, r: 76322, s: 82649, t: 91705,
        u: 94463, v: 95441, w: 97801, x: 97951,
        y: 99925, z: 100000
    };

    var random = Math.random() * 100000;

    for (letter in lookup) {
        if (lookup[letter] > random) {
            return letter;
        }
    }
}

Focusing on performance, I recommend to split the lookup table into an array of letters and an array of cumulative relative frequencies.

Iterating and accessing compact array elements is much faster compared to accessing object properties.

Also, in contrast to array iteration, the for..in iteration order of object properties is not guaranteed and thus less robust (as pointed out by Mike Brant in the comments).

Moving the lookup table out of the function body into either a closure or a module level constant gives you a speed advantage at least for non-optimizing JavaScript compilers.

A binary search strategy would find the letter corresponding to a random frequency value in O(log n) steps compared to O(n) steps for the current linear search. However, the constant binary search overhead is too big and the array small enough that the linear search performs better in practice.

I get the following performance test results in Firefox 53:

Test:                 Ops/sec:
Original               1,787,627   94% slower
Linear array search   31,399,828   fastest
Binary array search   28,691,914   9% slower

See also the performance test given by @hjpotter92 in the comments.

Here is the winning linear array search module implementation:

/** English language alphabet. */
export const letters = [
    'a', 'b', 'c', 'd',
    'e', 'f', 'g', 'h',
    'i', 'j', 'k', 'l',
    'm', 'n', 'o', 'p',
    'q', 'r', 's', 't',
    'u', 'v', 'w', 'x',
    'y', 'z'
];

/**
 * English language cumulative letter frequencies × 100000.
 * @see http://en.wikipedia.org/wiki/Letter_frequency
 */
export const frequencies = [
    8167,  9659,  12441, 16694,
    29396, 31624, 33639, 39733,
    46699, 46852, 47624, 51649,
    54055, 60804, 68311, 70240,
    70335, 76322, 82649, 91705,
    94463, 95441, 97801, 97951,
    99925, 100000
];

/**
 * Return a random letter from a - z according to their relative
 * frequencies in the English language.
 *
 * @return {string} random letter from a - z.
 */
export default function randomAtoZ() {
  let random = Math.random() * 100000;
  for (let i = 0, length = letters.length; i < length; i++) {
    if (random < frequencies[i]) {
      return letters[i];
    }
  }
}


EF Core Complex Query Method The code is returning reviews based on users like status order. It seems it executes a lot of SQL queries, so do you think it can be simplified and can it be a headache to server performance?

ret = _dbEntitySet.AsNoTracking()
           .Where(p => p.PostId == postId && p.IsPublished)
           .Include(p => p.UserLikes)
           .OrderByDescending(p => p.UserLikes.Select(f => f.LikeStatus == LikeStatus.Like).Count() - p.UserLikes.Select(f => f.LikeStatus == LikeStatus.Dislike).Count())
           .Skip((reviewPageIndex - 1) * reviewPageSize)
           .Take(reviewPageSize)
           .Select(p => new PostModalReviewDisplay()
           {
               CommentCount = p.Comments.Count(),
               DislikeCount = p.UserLikes.Where(q => q.LikeStatus == LikeStatus.Dislike).Count(),
               LikeCount = p.UserLikes.Where(q => q.LikeStatus == LikeStatus.Like).Count(),
               IsCurrentUserDisliked = p.UserLikes.Any(w => w.LikeStatus == LikeStatus.Dislike && w.UserInfoId == currUserId),
               IsCurrentUserLiked = p.UserLikes.Any(w => w.LikeStatus == LikeStatus.Like && w.UserInfoId == currUserId),

               Content = p.Content,
               Id = p.Id,
               UserInfo = new UserInfoExtraSmall()
               {
                   AppUserId=p.UserInfo.AppUserId,
                   Name=p.UserInfo.Name,
                   Surname=p.UserInfo.Surname,
                   ProfileImage=p.UserInfo.ProfilePicture.SmallPath
               }
           })

As mentioned in a comment of mine, it's possible that there's a bug in the code. The OrderByDescending(p => p.UserLikes.Select(f => f.LikeStatus == LikeStatus.Like).Count() - p.UserLikes.Select(f => f.LikeStatus == LikeStatus.Dislike).Count()) call should always sort on a single value, 0. The call should be equal to OrderByDescending(p => p.UserLikes.Count() - p.UserLikes.Count()) because the Select method does not filter values.

That being said, I'll consider it as OrderByDescending(p => p.UserLikes.Where(f => f.LikeStatus == LikeStatus.Like).Count() - p.UserLikes.Where(f => f.LikeStatus == LikeStatus.Dislike).Count()) from here on out. If this is not the case let me know and I'll delete this answer.

I see that you count likes and dislikes a couple of times in your queries, in the OrderByDescending method and in the LikeCount and DislikeCount properties in the final Select. Keeping in mind this consideration I'd change the query in the following:

ret = _dbEntitySet.AsNoTracking()
       .Where(p => p.PostId == postId && p.IsPublished)
       .Include(p => p.UserLikes)
       .Select(p => new { DbEntity = p, LikeCount = p.UserLikes.Where(f => f.LikeStatus == LikeStatus.Like).Count(), DislikeCount = p.UserLikes.Where(f => f.LikeStatus == LikeStatus.Dislike).Count() })
       .OrderByDescending(p => p.LikeCount - p.DislikeCount)
       .Skip((reviewPageIndex - 1) * reviewPageSize)
       .Take(reviewPageSize)
       .Select(p => new PostModalReviewDisplay()
       {
           CommentCount = p.DbEntity.Comments.Count(),
           DislikeCount = p.DislikeCount,
           LikeCount = p.LikeCount,
           IsCurrentUserDisliked = p.DbEntity.UserLikes.Any(w => w.LikeStatus == LikeStatus.Dislike && w.UserInfoId == currUserId),
           IsCurrentUserLiked = p.DbEntity.UserLikes.Any(w => w.LikeStatus == LikeStatus.Like && w.UserInfoId == currUserId),

           Content = p.DbEntity.Content,
           Id = p.DbEntity.Id,
           UserInfo = new UserInfoExtraSmall()
           {
               AppUserId=p.DbEntity.UserInfo.AppUserId,
               Name=p.DbEntity.UserInfo.Name,
               Surname=p.DbEntity.UserInfo.Surname,
               ProfileImage=p.DbEntity.UserInfo.ProfilePicture.SmallPath
           }
       })

One more thing that comes to mind is to switch the comparison clauses in the IsCurrentUserDisliked and IsCurrentUserLiked properties. Note that the two versions could be the same in terms of performance, depending on the execution plan that the query optimizer generates, but personally I prefer to put the most selective comparison clause first.

So, the final version would be the following:

ret = _dbEntitySet.AsNoTracking()
       .Where(p => p.PostId == postId && p.IsPublished)
       .Include(p => p.UserLikes)
       .Select(p => new { DbEntity = p, LikeCount = p.UserLikes.Where(f => f.LikeStatus == LikeStatus.Like).Count(), DislikeCount = p.UserLikes.Where(f => f.LikeStatus == LikeStatus.Dislike).Count() })
       .OrderByDescending(p => p.LikeCount - p.DislikeCount)
       .Skip((reviewPageIndex - 1) * reviewPageSize)
       .Take(reviewPageSize)
       .Select(p => new PostModalReviewDisplay()
       {
           CommentCount = p.DbEntity.Comments.Count(),
           DislikeCount = p.DislikeCount,
           LikeCount = p.LikeCount,
           IsCurrentUserDisliked = p.DbEntity.UserLikes.Any(w => w.UserInfoId == currUserId && w.LikeStatus == LikeStatus.Dislike),
           IsCurrentUserLiked = p.DbEntity.UserLikes.Any(w => w.UserInfoId == currUserId && w.LikeStatus == LikeStatus.Like),

           Content = p.DbEntity.Content,
           Id = p.DbEntity.Id,
           UserInfo = new UserInfoExtraSmall()
           {
               AppUserId=p.DbEntity.UserInfo.AppUserId,
               Name=p.DbEntity.UserInfo.Name,
               Surname=p.DbEntity.UserInfo.Surname,
               ProfileImage=p.DbEntity.UserInfo.ProfilePicture.SmallPath
           }
       })

As a final thought, I agree with @AdrianoRepetti's comment:

  Nothing better than to check generated SQL then they query plan and then measure...

Check the generated query plan for different versions and use the query that best suits your needs.


An Update on my TileMap Class I'm making a old-school Final Fantasy type game and I made a tile map class a while back and posted it on here (Is this a good way to implement a tile map from a text file?) But, when I got to collision, it was a nightmare. So I've updated it to make it more collision friendly and I was wondering what the community's thoughts were on this code.

Tile.java

import java.awt.image.BufferedImage;

public class Tile
{
    public int texKey;  //number to represent texture
    public BufferedImage texture;
    public boolean collidable;

    public Tile(int texKey, BufferedImage texture, boolean collidable)
    {
        this.texKey = texKey;
        this.texture = texture;
        this.collidable = collidable;
    }
}

TileMaker.java

import com.stardust.rpgtest2.gfx.Assets;

public class TileMaker
{
    public Tile[] tileArray= new Tile[28];

    public TileMaker()
    {

    }

    public void setTile()
    {
        for(int i = 0; i <= tileArray.length; i++)
        {
            switch(i)
            {
                case 0:
                {
                    tileArray[i] = new Tile(i, Assets.grass, false);
                    break;
                }
                case 1:
                {
                    tileArray[i] = new Tile(i, Assets.dirt, false);
                    break;
                }
                case 2:
                {
                    tileArray[i] = new Tile(i, Assets.water, false);
                    break;
                }
                //this goes up to 27 at the moment
                default:
                {
                    break;
                }
            }

        }
    }

    public Tile[] getTileArray()
    {
        return tileArray;
    }
}

TileMap.java

import com.stardust.rpgtest2.Game;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class TileMap
{
    int posX, posY, x, y, sx, sy, mapSize;
    public String filename;

    public int[][] tilemap;

    public TileMap(int posX, int posY, int mapSize, String filename)
    {
        this.mapSize = mapSize;
        tilemap = new int[mapSize][mapSize];
        this.posX = posX;
        this.posY = posY;
        this.sx = mapSize;
        this.sy = mapSize;
        this.filename = filename;
    }

    public void render(Graphics g)
    {
        for(int y = 0; y < tilemap.length; y++)
        {
            for(int x = 0; x < tilemap.length; x++)
            {
                int textureType = tilemap[x][y];
                BufferedImage texture = Game.tileMaker.getTileArray()[textureType].texture;
                g.drawImage(texture, posX, posY, null);
                posY += 32;
            }
            posX += 32;
            posY = Game.mapY;
        }
        posX = Game.mapX;
        posY = Game.mapY;
    }

    public void fileParser() throws IOException
    {
        BufferedReader in = new BufferedReader(new FileReader(filename));

        String line;

        while((line = in.readLine()) != null)
        {
            String[] values = line.split(",");

            for(String str : values)
            {
                int str_int = Integer.parseInt(str);
                tilemap[x][y] = str_int;
                ++y;
            }
            ++x;
            y = 0;
        }
        x = 0;

        in.close();
    }
}

I call the TileMaker.setTile() and TileMap.fileParser() methods both in the initialization method in the main game class, and everything behaves as needed so far.

The for-case paradigm is an anti-pattern, you shouldn't use it. One alternative would be to initialize the array in a static block (without for-switch), another would be to use an enum. I would prefer the second approach. Both approaches would save you the call of TileMaker.setTile().
Your indent style is not customary in Java, and it's always best to go with what most programmers of a language are used to (opening curly bracket on the same line).
Your variable names are not very clear. What is the difference between posX and x? And what is sx?
Get rid of unused fields. If you don't need mapSize, don't save it.
Move magic numbers out of your code into static fields.
I would create a separate class for the parsing of the file.


Test whether two strings are anagrams of each other What is the time/space complexity of this algorithm which checks if two strings are anagrams? My guess is that time is \$O(n)\$ and space \$O(1)\$ but I am not sure.  Can it be improved?

def anagram(str1, str2):
    s1 = list(str1)
    s2 = list(str2)
    freq1 = {}
    freq2 = {}

    for pos in s1:
        freq1[pos] = s1.count(pos)
    for pos in s2:
        freq2[pos] = s2.count(pos)
    if freq1 == freq2:
        return True
    else:
        return False

You take a string, and instantly make a list from it, and so space is at least \$O(n)\$. s1.count(pos) is an \$O(n)\$ operation, and is in a loop that loops \$n\$ times, and so the loop is \$O(n^2)\$. To make the loops \$O(n)\$ you just need to add 1 to the amount, and use a defaultdict to default everything to 0.

from collections import defaultdict

def anagram(str1, str2):
    freq1 = defaultdict(int)
    freq2 = defaultdict(int)

    for char in str1:
        freq1[char] += 1
    for char in str2:
        freq2[char] += 1

    return freq1 == freq2

This can however be simplified to use collections.Counter. And so you can use:

from collections import Counter

def anagram(str1, str2):
    return Counter(str1) == Counter(str2)

And so both the above have \$O(n)\$ time and space complexity.


Custom JavaScript function queue For fun, I made a function queue in JavaScript.  I named it tinyq.  You can add functions to it, and each function is passed the next function in the queue.  You can also pass parameters to the functions, and they can return values.

function q(){
    var Q = [], t = this;
    this.length = 0;
    this.add = function (f){
        Q.push(f);
        this.length++;
    };
    this.run = function(){
        var n = Q.shift();
        this.length = Q.length;
        if(typeof n === 'function'){
            var a = Array.prototype.slice;
            return n.apply(window,[function(){
                return t.run.apply(t, a.call(arguments));
            }].concat(a.call(arguments)));
        }
    };
}

Here's a simple example of its use.

var Q = new q;
Q.add(function(next, A, B){
    return A + ": "+ next(B);
});
Q.add(function(next, C){
    return C + "123";
});
console.log(Q.length); // 2
console.log(Q.run("Test", "ABC")); // "Test: ABC123"

So, what do you think of my function queue?  Any improvements, or anything I'm doing wrong?

P.S. I was also trying to make this small, any suggestions on making it smaller?

I'm still trying to wrap my head around what exactly is happening in that apply/call mass :-)

But that may be a hint, that those three lines aren't really well readable. I'm aware you want to keep it small, but unless the code needs to be really, really fast (which I doubt it does) I would prefer readability over shortness.

In that line, I would prefer to have long, more expressive variable names. It be better to use complete words instead of Q, t, n, a etc.

If in the end, shortness is still important, then use a JavaScript compressor.

BTW, you should keep JavaScript naming conventions in mind, too. Constructor functions ("classes") should be named with capital letters (Q instead of q) and fields should use small letters (q instead of Q).

Just out of interest: Can you give a concrete use case for this kind of queue?


Project Euler problem 12: highly divisible triangular number I am just a beginner in C++ and I tried solving this problem:

  What is the first number of the form 1 + 2 + 3 + … + n that has over five hundred factors?

But it seems my code is not that efficient because its taking ages to find the answer:

#include<iostream>
using namespace std;

void triNum(int);

int main()
{
    int res=0;
    for (int i=1;i<100000000;i++){
        res+=i;
        triNum(res);
    }
    cout<<endl;
}

void triNum(int n)
{
    int counter=0;
    for(int i=1;i<=n;i++){
        if(n%i==0){
            counter+=1;
        }
    }
    if(counter==500){
        cout<<n;
    }
}

The program is slow for several reasons:

The triNum function finds all the divisors. It doesn't need to. After you find 500 divisors, you could print the number and return from the function

The function doesn't do what the problem description asked: it prints a number only when it has exactly 500 divisors, but the problem asks for the first number with over 500 divisors. If the first number has 502 divisors
that would be a valid answer but your program will never print that.

Even after finding a number with over 500 divisors, your program would continue running until it checks 100000000 numbers. That's completely unnecessary, it should exit after finding the first appropriate number.
The program doesn't reuse the counts it already calculated. For example, after counting the divisors of 100, that could be reused when counting the divisors of 200. You could greatly benefit from using a map to cache already calculated counts.

The program has several other violations of good practices:

using namespace std is considered bad practice
The expression int i=1;i<100000000;i++ is too compact, it's recommended to put spaces around operators and after semicolons, like this: int i = 1; i < 100000000; i++
triNum is a poor name: it fails to describe what the function does
100000000 and 500 are magic numbers. It would be better to put these values in constants with descriptive names


Return an object but with one property different In the following code I want to return all the same keys and values except the property color. If the name is default, return the color: '#1F2532', if it's different than default, it will call a function that gives me random colors color: pickColor([]). But I think there is a better way to write this instead of repeating code and just changing the property color. How can I do this?

class Project {

  static createProject(data) {
    if (data.name !== 'default') {
      return {
        owner: {
          name: data.owner,
          avatar: data.ownerAvatar
        },
        users: [
          {
            name: data.owner,
            avatar: data.ownerAvatar,
            active: true,
          }],
        name: data.name,
        retrospectives: [],
        color: pickColor([])
      }
    } else {
      return {
        owner: {
          name: data.owner,
          avatar: data.ownerAvatar
        },
        users: [
          {
            name: data.owner,
            avatar: data.ownerAvatar,
            active: true,
          }],
        name: data.name,
        retrospectives: [],
        color: '#1F2532'
      }
    }
  }
}

As said in a comment above, using a conditional assignment would be your best bet:

class Project {

  static createProject(data) {
    return {
      owner: {
        name: data.owner,
        avatar: data.ownerAvatar
      },
      users: [
        {
          name: data.owner,
          avatar: data.ownerAvatar,
          active: true,
        }],
      name: data.name,
      retrospectives: [],
      color: data.name === 'default' ? '#1F2532' : pickColor([])
    }
  }
}


Find the pairs of integers that sum to N Can it be made faster, and I am a newbie to complexity but I'm pretty sure the average case for this would be O(n). Am I wrong?
Output for the first list should be: (1,9), (2,8), (5,5), (-11,21)

The program finds pairs of an array whose sum is equal to ten.

#find pairs of an array whose sum is equal to ten
#bonus do it im linear time

list1 = [1,1,2,1,9,9,5,5,2,3,745,8,1,-11,21]
list2 = [1,1,1,1,9,9,9,9,2,8,8,8,2,2,0,0,0]
list3 = ["dog", "cat", "penguin", 9, 1]
list4 = []

def find_pairs_sum_n(list1, n):
    set1 = set(list1)
    history = set()

    if not list1:
        print("list is empty")
        raise StopIteration
    if not all(isinstance(n, int) for n in list1):
        print("list must contain only integers")
        raise StopIteration

    for x in set1:
        history.add(x)
        if (n - x) in set1 and (n-x) not in history:
            yield (x, n - x)

        elif x == n/2 and list1.count(n/2) > 1:
            yield (int(n/2), int(n/2))

x = find_pairs_sum_n(list1, 10)
for i in x:
    print(i)

y = find_pairs_sum_n(list2, 10)
for i in y:
    print(i)

z = find_pairs_sum_n(list3, 10)
print(next(z))

w = find_pairs_sum_n(list4, 10)
print(next(w))

In no particular order, a few suggestions to make the code more Pythonic.

return from generator

You do not need to raise StopIteration in a generator, you can simply return. So this:

if not list1:
    print("list is empty")
    raise StopIteration
if ...

Could have been a bit simpler as:

if not list1:
    print("list is empty")
elif ...

Use an Exception for exceptions

It is generally bad practice to put prints into your computation routines.  Instead consider using exceptions.

if not list1:
    raise ValueError("list is empty")

Then catch the exception like:

try:
    for j in find_pairs_sum_n(l, 10):
        print(j)
except ValueError as exc:
    print(exc)

You don't need history

Since the values you are evaluating are already unique by putting them into a set(), you don't need history.  Instead, only do the evaluation for numbers which are < 1/2 of n.

Mid value check only once

For the mid-value check I suggest you do it only once (ie, not in the loop).  Additionally if you use collections.Counter instead of the set() you don't need to do the list.count() because you already have the counts.

Consider using a generator expression

Now that we have simplified the loop somewhat, it can be pretty cleanly written as a generator expression:

yield from ((x, n-x) for x in values if x <= threshold and (n - x) in values)

Full code listing

from collections import Counter

def find_pairs_sum_n(values, n):
    values = Counter(values)
    non_ints = {x for x in values if not isinstance(x, int)}

    if non_ints:
        raise ValueError(
            "Found non-integers in values: {}".format(non_ints))

    threshold, n_odd = divmod(n, 2)
    if not n_odd:
        if values[threshold] > 1:
            yield threshold, threshold
        threshold -= 1

    yield from (
        (x, n-x) for x in values if x <= threshold and (n - x) in values)

list1 = [1, 1, 2, 1, 9, 9, 5, 5, 2, 3, 745, 8, 1, -11, 21]
list2 = [1, 1, 1, 1, 9, 9, 9, 9, 2, 8, 8, 8, 2, 2, 0, 0, 0, 4, 5, 6]
list3 = ["dog", "cat", "penguin", 9, 1]
list4 = []

for l in (list1, list2, list3, list4):
    try:
        for j in find_pairs_sum_n(l, 10):
            print(j)
    except ValueError as exc:
        print(exc)
    print('---')

Results:

(5, 5)
(1, 9)
(2, 8)
(-11, 21)
---
(1, 9)
(2, 8)
(4, 6)
---
Found non-integers in values: {'dog', 'penguin', 'cat'}
---
---


Count numbers with atleast one common factor, excluding one (updated) Below is an updated version of  the question I asked prior:

Count numbers with atleast one common factor, excluding one

I have been trying to teach myself some python 3 by working through problems at geeksforgeeks.org. A common issue I am having is that my code runs on my machine, and in their test environment, but my code ultimately exceeds their time limits upon submission. I was wondering if anyone had any general advice on making my code more efficient so I could continue to make use of the practice problems.

As for my update, I have tried:

(1) Utilizing list comprehension in python. While cleaner, I don't think this made any dramatic improvements for me.
(2) I only populate the list of possibilities once now as per the suggestion of Gareth Rees. I originally knew this was inefficient, but I thought this was bad form since it makes my code less flexible for the most general case. I realize now that the constraints should be used in a way to make the code faster for these types of time-limited problems.
(3) I have traded a while loop for a for loop since I read this was faster.

That said, I am still no where near the time limits they are imposing in the problem. Some thoughts I had:

(1) My guess is the main culprit is the nested for loop inside a for loop which is likely very time consuming, but I don't know how to get around using nested for loops in this.

(2) Also, I am wondering if Python is even suitable for severely time limited challenges of this sort? I have read that Python is generally slower than C++ and from what I can tell very few people use Python on time-limited sites like this.

The problem I have been working on is:

http://practice.geeksforgeeks.org/problems/mathematical-manipulation/0

  Given a number \$N\$, find the total numbers, less than or equal to \$N\$ which have at-least one common factor with \$N\$ other than \$1\$.

  Input:
  The first line of input contains an integer \$T\$ denoting the no of test cases. Then \$T\$ test cases follow. Each test case consist of an integer \$N\$.

  Output:
  For each test case output an integer denoting the count of numbers, less than or equal to \$N\$ having at-least one common factor between the number and \$N\$ except \$1\$.

  Constraints:
  \$1 \le T \le 100000\$
  \$1 \le N \le 1000000\$

  Example:
  Input:

3
1
6
15

  Output:

0
4
7

And my (updated) code:

def myfun(num_in):

    int_list = []
    some_list = []
    final_list = []

    if num_in == 1:
        print(0)
    elif num_in > 1:

        # check remainder from division
        test = [num_in % pos[j] for j in range(0, (num_in + 1) // 2)]  # only need .5 the number or smaller

        # record if remainder is 0, if it is, keep that because it will be common denominator
        for i in range(0, len(test)):
            if test[i] == 0:
                int_list.append(pos[i])

        # multiply factor by integers until it exceeds input
        # this seems like the best candidate to make more efficient
        for i in range(0, len(int_list)):
            temp = int_list[i]
            # for j in range(1, int(num_in / temp)):
            #     final_list.append(temp * j)

            ttempp = [j for j in range(1, int(num_in / temp))]
            some_list = list(map(lambda x: x * temp, ttempp))
            final_list = final_list + some_list

        #print(final_list)
        #print(set(final_list))
        # add 1 for the number itself since we don't check that (only checked up to .5 * num)
        print(len(set(final_list)) + 1)

# list all possibilities once
pos = [i for i in range(2, 1000000)]

# input code
# how many to run
tot_num = int(input())

# get full list of inputs
master_list = [int(input()) for i in range(tot_num)]

# run code on list of inputs
[myfun(master_list[q]) for q in range(tot_num)]

Your algorithm is very far from optimal. In the worst case (if all numbers are equal to N in the input), it does O(N * T) operations, which clearly way too much (not just for python, for any programming language).

What they're asking to compute is n - phi(n), where phi is the Euler's totient function. Using the fact that the function is multiplicative one can derive a formula phi(n) = n * {product of (1 - 1 / p) over all prime p dividing n. Using this fact, we can compute it efficiently in O(N log log N) time for all 1 <= i <= N using something that looks like the sieve of Eratosthenes. After that, we can answer each query in O(1).

A C++ implementation is ridiculously concise and easily fits in the time limit:

#include <cstdio>
#include <vector>
#include <algorithm>

int main() {
    const int max_n = 1000 * 1000;
    std::vector<int> ans(max_n + 1);
    std::iota(ans.begin(), ans.end(), 0);
    for (int i = 2; i <= max_n; i++)
        if (ans[i] == i)
            for (int j = i; j <= max_n; j += i)
                ans[j] = ans[j] / i * (i - 1);
    int t;
    scanf("%d", &t);
    while (t--) {
        int x;
        scanf("%d", &x);
        printf("%d\n", x - ans[x]);
    }
}

Unfortunately, the same solution in python is too slow.

def calc_phi(n):
    res = [i for i in range(n + 1)]
    is_prime = [True] * (n + 1)
    for i in range(2, n + 1):
        if is_prime[i]:
            for j in range(2 * i, n, i):
                is_prime[j] = False
                res[j] = res[j] // i * (i - 1)
    return res

def solve():
    max_n = 10 ** 6
    phi = calc_phi(max_n)
    cnt = int(input())
    res = []
    for _ in range(cnt):
        n = int(input())
        res.append(n - phi[n])
    print('\n'.join(map(str, res)))

if __name__ == '__main__':
    solve()

With some optimizations (like more efficient input reading and handling small prime divisors separately), which made the code less readable and nice, I managed to make it complete under 1 second in the worst case on my machine, which is pretty close. It's still too slow for the online judge. So I'm not sure if it's actually solvable in python. That's the main reason why python is rarely used to solve problems with a tight time limit: it's much slower than C++ or Java.


jQuery "on('click')" to toggle webpage content I have made a successful test page that satisfies my goals of using JavaScript ( jQuery specifically ) to add and remove content from a webpage.

<!doctype html>

<title>test</title>

For this simple example where only two possible outcomes exist the code isn't too bad to look at, however I know that if this were inflated to what a full website can entail (e.g. dozens of links per page, each one putting in different content) the .on(click) chain would get very complicated.

How else could I handle this situation?

I know that I could just make two different .html files ( foo and bar ) and setup simple anchor links (i.e. ) between them and obtain the same effect but my goal is to have an external source generate the content that is displayed.

I am getting used to JavaScript programming in general so please do point out other ways I can improve what I am doing.

The code pattern:

$("#main").html("");
$("#main").append(bar);

can be simplified to:

$("#main").html(bar);

In general, the code is, clear, easy to read, and well organized. All the decisions among alternatives are reasonable and straight forward. Doing the simplest thing that might work is a good approach because it avoids adding unneeded complexity to the necessary complexity.

The use of template literals rather than strings might be unnecessary complexity because the templating features are not used. Support for template literals is not universal among browsers. The page may break for no reason for some people. To put it another way, using template literals rather than strings creates potential broken pages for no gain in browsers that support template literals.


Show that sum and product are both examples of accumulation Given this task:

  Exercise 1.32

  a. Show that sum and
  product (exercise 1.31) are both
  special cases of a still more general
  notion called accumulate that combines
  a collection of terms, using some
  general accumulation function:

  (accumulate combiner null-value term a
  next b)

  Accumulate takes as arguments the same
  term and range specifications as sum
  and product, together with a combiner
  procedure (of two arguments) that
  specifies how the current term is to
  be combined with the accumulation of
  the preceding terms and a null-value
  that specifies what base value to use
  when the terms run out. Write
  accumulate and show how sum and
  product can both be defined as simple
  calls to accumulate.

  b. If your accumulate procedure
  generates a recursive process, write
  one that generates an iterative
  process. If it generates an iterative
  process, write one that generates a
  recursive process.

I wrote the following solution:

Recursive:

(define (accumulate combiner null-value term a next b)
  (if (> a b) null-value
      (combiner (term a)
                (accumulate combiner
                            null-value
                            term
                            (next a)
                            next
                            b))))

Iterative:

(define (i-accumulate combiner null-value term a next b)
  (define (iter a result)
    (if (> a b) result
        (iter (next a) (combiner (term a) result))))
  (iter a null-value))

Sum/Product using iterative accumulate:

(define (sum term a next b) (i-accumulate + 0 term a next b))
(define (product term a next b) (i-accumulate * 1 term a next b))

What do you think?

Since the only parameter that changes in your recursive definition is a, you can write an inner definition like so:

(define (accumulate combiner null-value term a next b)
  (define (rec a)
    (if (> a b)
        null-value
        (combiner (term a) (rec (next a)))))
  (rec a))


Error-testing be with cucumber/capybaraweb I'm working on writing tests for a rails application using cucumber and capybara. I have a scenario for a user editing a post, and making it invalid.

The scenario looks like this:

Scenario: I edit a post and make the data invalid.
  Given I have 1 post
  And I view my 1st post
  Then I click "Edit"
  And I fill in the edit post form with bad data
  When I click the "Update post" button
  Then I should see "Title is too short (minimum is 3 characters)"
  And I should see "Body is too short (minimum is 30 characters)"

Should I leave the scenario as-is, or create a scenario for entering the title incorrectly, and another scenario for entering the body incorrectly, like this:

Scenario: I edit a post and make the title too short.
  Given I have 1 post
  And I view my 1st post
  Then I click "Edit"
  And I fill in the edit post form with a title that is too short
  When I click the "Update post" button
  Then I should see "Title is too short (minimum is 3 characters)"

Scenario: I edit a post and make the body too short.
  Given I have 1 post
  And I view my 1st post
  Then I click "Edit"
  And I fill in the edit post form with a body that is too short
  When I click the "Update post" button
  Then I should see "Body is too short (minimum is 30 characters)"

If the post has several more fields, should I still create a scenario for each one, or would it be more DRY to use one scenario?

Personally, I only write feature tests for critical and very common cases, and outsource more specific tests (i.e is a certain field on a model validated) to model specs.

I wouldn't consider it a feature that a certain validation error is shown to the user, but I consider it a feature that the user gets informed that something went wrong and his post couldn't be created.

TL;DR Test the user experience in a feature spec and data integrity in model / controller specs.

Use different thinking hats for different tests:

Feature tests: Think in the user's perspective, how does the user interact with your app? what is the user expecting from your app?
Model tests: Think compulsive orderliness, how can you ensure data integrity?
Controller tests: Think paranoid, who can access what? does your controller comply with your API specs?

I would test in a feature spec for a "post":

That the user is able to create a post with an expected set of information (i.e the minimum that is required to create a post) and is redirected to the new post which shows the entered information.
That the user is unable to create a post by just hitting the "Submit" button without filling anything in and is shown a validation error. After correcting the user's mistake he should be able to create the post and be redirecred to the new post which shows the entered information.

This ensures the following "features":

Posts can be created
The user is informed about mistakes
Mistakes can be fixed
The user is assured that his post has been created.

I would test in a model spec:

Spec.describe Post, type: :model do
  subject { create(:post) }

  context 'validations' do
    it { should validate_presence_of(:title) }
    # for my taste, it's good enough to test that we validate the length
    it { should validate_length_of(:title) }

    it { should validate_presence_of(:body) }
    # if you want to be super-strict, you can check the length
    it { should validate_length_of(:body).is_at_least(100) }
  end
end


Rock, Paper, Scissors in C++ I have a fair few years experience with programming, and am currently working in a software development role, writing internal web applications in C#.

This is the first C++ code I have ever written, so I therefore don't know the conventions, code smells, do's/don't's of the language.

I decided to write an extremely basic Rock, Paper, Scissors implementation using the command line.

Please let me know if I can improve anything?

main.cpp

#include <iostream>
#include <stdlib.h>

using namespace std;

enum Outcome { user, bot, draw };
string outcomeMap[3] = {"You win!", "Bot wins!", "It was a draw!"};
enum Choice { rock, paper, scissors };
string choiceMap[3] = {"rock", "paper", "scissors"};

Choice getUserChoice() {
    cout << "Rock, paper or scissors? " << endl;

    string input;
    getline(cin, input);

    if(input == "rock" || input == "ROCK" || input == "1" || input == "r" || input == "R") {
        return rock;
    } else if(input == "paper" || input == "PAPER" || input == "2" || input == "p" || input == "P") {
        return paper;
    } else if(input == "scissors" || input == "SCISSORS" || input == "3" || input == "s" || input == "S") {
        return scissors;
    } else {
        throw invalid_argument("You must choose rock, paper or scissors.");
    }
}

Choice getBotChoice() {
    auto randomNumber = rand() % 3 + 1;

    switch(randomNumber) {
        case 1:
            return rock;
        case 2:
            return paper;
        case 3:
            return scissors;
        default:
            throw invalid_argument("Random number was generated outside of the given range.");
    }
}

Outcome decideOutcomeOfGame(Choice userChoice, Choice botChoice) {
    if (userChoice == botChoice) {
        return draw;
    } else if (userChoice == rock && botChoice == paper) {
        return bot;
    } else if (userChoice == rock && botChoice == scissors) {
        return user;
    } else if (userChoice == paper && botChoice == rock){
        return user;
    } else if (userChoice == paper && botChoice == scissors){
        return bot;
    } else if (userChoice == scissors && botChoice == rock){
        return bot;
    } else if (userChoice == scissors && botChoice == paper) {
        return user;
    }
}

bool shouldGameExit() {
    cout << "Quit?" << "\r\n";
    string input;
    getline(cin, input);
    if(input == "no" || input == "NO" || input == "n" || input == "N" || input == "0") {
        return false;
    } else {
        return true;
    }
}

void gameLoop() {
    auto quit = false;
    while(!quit) {
        auto userChoice = getUserChoice();
        auto botChoice = getBotChoice();

        cout << "You chose " << choiceMap[userChoice] << "\r\n";
        cout << "The bot chose " << choiceMap[botChoice] << "\r\n";
        cout << outcomeMap[decideOutcomeOfGame(userChoice, botChoice)] << "\r\n";

        quit = shouldGameExit();
    }
}

int main() {
    gameLoop();
    return 0;
}

It's a good start!  Here are some things that may help you improve your program.

Don't abuse using namespace std

Putting using namespace std at the top of every program is a bad habit that you'd do well to avoid.  Know when to use it and when not to (as when writing include headers).  In this particular case, I happen to think it's perfectly appropriate because it's a single short program that and not a header. Some people seem to think it should never be used under any circumstance, but my view is that it can be used as long as it is done responsibly and with full knowledge of the consequences.

Use <cstdlib> instead of <stdlib.h>

The difference between the two forms is that the former defines things within the std:: namespace versus into the global namespace.  Language lawyers have lots of fun with this, but for daily use I'd recommend using <cstdlib>.  See this SO question for details.

Use for loops rather than while loops where practical

Your gameloop routine can be simplified a bit by using a for loop rather than a while loop.  here is the current code:

void gameLoop() {
    auto quit = false;
    while(!quit) {
        // other stuff
        quit = shouldGameExit();
    }
}

I'd recommend writing it as a for loop to do several things.  First, it changes the scope of the quit variable to solely within the loop.  Second, it makes it clear how the exit condition is set:

void gameLoop() {
    for (auto quit = false; !quit; quit = shouldGameExit()) {
        // other stuff
    }
}

Don't use std::endl if you don't really need it

The difference betweeen std::endl and '\n' is that '\n' just emits a newline character, while std::endl actually flushes the stream.  This can be time-consuming in a program with a lot of I/O and is rarely actually needed.  It's best to only use std::endl when you have some good reason to flush the stream and it's not very often needed for simple programs such as this one.  Avoiding the habit of using std::endl when '\n' will do will pay dividends in the future as you write more complex programs with more I/O and where performance needs to be maximized.

Check return values and handle errors

The code calls getline but never checks for error return values.  If getline encounters a problem, it sets the failbit.  It's easy to check for this in C++, because of operator overloading.  That is, one could do this:

getline(cin, input)
if (cin) {  // if getline was OK
    ///
}

Think of the user

Generally speaking, it's not unusual for users to type in faulty input.  For that reason, I think I would not throw an exception from with getUserChoice.  Instead of abruptly aborting the user out of the program, a friendlier way to do it would be to give the user a chance to correct the input.  Here's one way to rewrite that function:

Choice getUserChoice() {
    Choice userchoice;
    for (auto valid{false}; !valid; ) {
        cout << "Rock, paper or scissors? \n";
        string input;
        getline(cin, input);
        if (cin) {
            if(input == "rock" || input == "ROCK" || input == "1" || input == "r" || input == "R") {
                valid = true;
                userchoice = rock;

            } else if(input == "paper" || input == "PAPER" || input == "2" || input == "p" || input == "P") {
                valid = true;
                userchoice = paper;
            } else if(input == "scissors" || input == "SCISSORS" || input == "3" || input == "s" || input == "S") {
                valid = true;
                userchoice = scissors;
            } else {
                cout << "Sorry, I didn't understand \"" << input << "\"\n";
            }
        }
    }
    return userchoice;
}

Note that one could code a return instead of using the userchoice and valid variables, but I prefer having an easier-to-read program flow without having to hunt for return statements.  You can decide for yourself which flavor you prefer.

Make sure all paths return a value

The decideOutcomeOfGame routine returns the outcome of the game.  It's probable that all combinations are enumerated, but I'd prefer to make sure the function returns something every time.  Here's how I'd write it.

Outcome decideOutcomeOfGame(Choice userChoice, Choice botChoice) {
    if (userChoice == botChoice) {
        return draw;
    } else if ((userChoice == rock && botChoice == paper)
              || (userChoice == paper && botChoice == scissors)
              || (userChoice == scissors && botChoice == rock)) {
        return bot;
    }
    return user;
}

Consider using a better random number generator

You are currently using

auto randomNumber = rand() % 3 + 1;

Normally I recommend the use of the C++11 std::uniform_int_distribution to replace the old-style rand() but since you're using C++17, you could use std::sample.  This reduces the getBotChoice() function to this:

Choice getBotChoice() {
    constexpr static std::array<Choice,3> choices{ rock, paper, scissors };
    static auto rnd{std::mt19937{std::random_device{}()}};
    std::vector<Choice> botChoice;
    std::sample(std::begin(choices), std::end(choices), std::back_inserter(botChoice), 1, rnd);
    return botChoice.front();
}

There is now no need for the throw because the code will never generate an invalid return value.

Use std::string_view where practical

Rather than mutable strings, the global variables outcomeMap and choiceMap would be better expressed as a std::array of std::string_view.  In other words, instead of this:

string outcomeMap[3] = {"You win!", "Bot wins!", "It was a draw!"};

Write this:

constexpr array<string_view, 3> outcomeMap{"You win!", "Bot wins!", "It was a draw!"};

Consider encapsulating into a namespace

It doesn't seem as though these functions are going to be useful without each other.  Because they're so closely related (and related as well to the data), I'd recommend consolidating everything into a namespace.

Understand return 0 in main

When a C or C++ program reaches the end of main the compiler will automatically generate code to return 0, so there is no need to put return 0; explicitly at the end of main.  I advocate omitting it to reduce the clutter and let the compiler generate code; others prefer to write it explicitly.  It's up to you to choose which style you prefer, but either way you should know about this compiler behavior.

Simplify your code

The code currently contains this construct:

if(input == "no" || input == "NO" || input == "n" || input == "N" || input == "0") {
        return false;
    } else {
        return true;
    }

As @Deduplicator correctly notes in a comment, better would be to simply return the value of the expression which is already bool:

return !(input == "no" || input == "NO" || input == "n" || input == "N" || input == "0");


jQuery dropdown plugin I'm looking for OOP tips or advice.

    ;(function ( $, window, document, undefined ) {

  if ( typeof Object.create !== 'function' ) {
    Object.create = function (o) {
        function F() {}
        F.prototype = o;
        return new F();
    };
  }

  if (!Function.prototype.bind) { // check if native implementation available
    Function.prototype.bind = function(){
      var fn = this, args = Array.prototype.slice.call(arguments),
          object = args.shift();
      return function(){
        return fn.apply(object,
          args.concat(Array.prototype.slice.call(arguments)));
      };
    };
  }

  $.component = function( name, object ) {
    $.fn[name] = function( options ) {
      return this.each(function() {
        if ( !$.data( this, name ) ) {
          $.data( this, name, Object.create(object).init(
            $.extend(true, {}, $.fn[name].opts, options), this )
          );
        }
      });
    };
  };

})( jQuery, window , document );

(function ( $, window, document, undefined ) {

  var dropdown = {
    opts: {},
    init: function( options, el ) {
      this.$el = $(el).on('click.dropdown',
        (function(evt){
          this.toggle(evt);
        }).bind(this)
      );
      this.opts = $.extend(true, this.opts, options);
      this.isShowing = false;
      return this;
    },
    show: function (evt) {
      if(evt) evt.preventDefault(); evt.stopPropagation();
      $(this.opts.header).addClass('show-mobile-nav');
      this.isShowing = true;
      this.bindClose();
    },
    hide: function (evt) {
      if(evt) evt.preventDefault();
      $(this.opts.header).removeClass('show-mobile-nav');
      this.isShowing = false;
      return this;
    },
    bindClose: function () {
      $('html').on('click.dropdown',
        (function(evt){
          var $targ = $(event.target || window.event.srcElement),
          targetIsNotDropdown = !$targ.is(this.opts.menu),
          targetsNotAChildOfDropdown = $(this.opts.menu).has( $targ ).length == 0;
          if( targetIsNotDropdown && targetsNotAChildOfDropdown) {
            $('html').off('.dropdown');
            this.hide();
          }
        }).bind(this)
      );
      return this;
    },
    toggle: function (evt) {
      if(evt) evt.preventDefault();
      if ( !this.isShowing ) {
        this.show(evt);
      } else {
        this.hide(evt);
      }
    }
  };

  $.component('dropdown',dropdown);

})( jQuery, window , document );

From a once over:

Your code is not consistently indented, especially the first dozen lines need some TLC
$.component = function( name, object ) { needs commenting, it is not clear what this is trying to achieve.
evt is an unfortunate parameter name, I would go for the classic e or the readable event
$targ is needlessly short, just use $target
You could probably use helper functions for if(evt) evt.preventDefault(); and if(evt) evt.preventDefault(); evt.stopPropagation();
Note that if(evt) evt.preventDefault(); evt.stopPropagation(); is exactly the kind of bug that makes people enforce {} around if blocks!
toggle() could use a ternary condition with !this.isShowing
'show-mobile-nav' should probably not be hardcoded since you use it more than once, instead you should use a properly named variable

All in all, some pretty cool code.


Swift extension: Dictionary to JSON string for URL parameter Here's a quick extension for [String: AnyObject] dictionaries in Swift. The output needs to be in the following format (order and spacing don't matter):

{"key": "val", "keywdict": {"anotherKey": 100, "Key2": "Val2"}, "strings": ["string", "another"]}

I am wondering if there's a better way, or if this can be improved.

extension Dictionary //...

func toJSONString() -> String {
  var pcs = [String]()
  for (key, val) in self {
    var valStr = ""
    if let val = val as? String {
      valStr = "\"\(val)\""
    } else if let val = val as? Dictionary<String, AnyObject> {
      valStr = val.toJSONString() // recursion for nested dictionaries
    } else if let val = val as? Array<String> {
      let tmpStr = "\",\"".join(val)
      valStr = "[\"\(tmpStr)\"]"
    } else if let val = val as? NSNumber {
      valStr = "\(val)"
    }
    pcs.append("\"\(key)\":\(valStr)")
  }
  return "{" + ",".join(pcs) + "}"
}

Your dictionary to JSON converter has some serious issues:

The special characters " and \in strings are not properly escaped:

[ "a\"b" : "c\\d"].toJSONString()

produces "{"a"b":"c\d"}", correct would be {"a\"b":"c\\d"}
(see for example http://json.org).
Only arrays of strings are implemented, but not arrays of numbers
or arrays of arrays or dictionaries.
Value types which are not covered by your code just print as an
empty string instead of causing an error.
Boolean values are simply treated as numbers:

[ "key" : true].toJSONString()

gives {"key":1}, correct would be {"key":true}

While this could all be fixed, it probably isn't worth the effort
because there is a Foundation class NSJSONSerialization which already
does exactly this:

var dict : [String: AnyObject] = [:]
dict["a\"b"] = "c\\d"
dict["strings"] = ["string", "another"]
dict["keywdict"] = [ "anotherKey": 100, "Key2": "Val2"]
dict["numbers"] = [ 1, 2, 3]
dict["bools"] = [ true, false]

var error : NSError?
if let jsonData = NSJSONSerialization.dataWithJSONObject(dict, options: nil, error: &error) {
    let jsonString = NSString(data: jsonData, encoding: NSUTF8StringEncoding)! as String
    println(jsonString)
} else {
    println("Error in JSON conversion: \(error!.localizedDescription)")
}

Output:

{"keywdict":{"anotherKey":100,"Key2":"Val2"},"bools":[true,false],"a\"b":"c\\d","strings":["string","another"],"numbers":[1,2,3]}


Recursively find files of certain types and log their paths (C++) I've written a function that takes a list of extensions and recursively finds files of those types, and logs their paths to a text file.

Usage example (finding image files in a home directory):

// set up filestream for unicode
const std::locale utf8_locale = std::locale(std::locale(), new std::codecvt_utf8<wchar_t>());
std::wofstream log("image_paths.txt", std::ios::app); // append mode
log.imbue(utf8_locale);

const std::set<std::wstring> image_extensions = {L".jpeg", L".jpg", L".tiff", L".gif", L".bmp", L".png"};
get_files(L"C:\\Users\\username", image_extensions, log);

Output to image_paths.txt:

C:\Users\username\image.jpg
C:\Users\username\image.png
C:\Users\username\directory\ajdsk.bmp
C:\Users\username\directory\subdirectory\other file.tiff
C:\Users\username\directory with spaces\file with spaces.jpeg

Function Code:

// return 0 -- all good
// return 1 -- root doesn't exist
// return 2 -- root isn't a directory
// return 3 -- no matching files found or error opening first file
// return 4 -- hit recursion limit
auto get_files(_In_ const std::wstring root, // root dir of search
        _In_  const std::set<std::wstring> &ext, // extensions to search for
        _Out_ std::wofstream &log, // file to write paths to
        _In_  unsigned limit = 10 /* default recursion limit */) -> int
{
    if(limit == 0) return 4;

    // check root path
    {
        DWORD root_attrib = GetFileAttributesW(root.c_str());

        if(root_attrib == INVALID_FILE_ATTRIBUTES) return 1;    // root doesn't exist
        if(!(root_attrib & FILE_ATTRIBUTE_DIRECTORY)) return 2; // root isn't a directory
    }

    LPCWSTR dir; // root directory + "\*"
    HANDLE file = INVALID_HANDLE_VALUE; // handle to found file
    WIN32_FIND_DATAW file_info; // attributes of found file

    // prepare path for use with FindFile functions
    std::wstring root_slash = root;
    root_slash.append(L"\\*");
    dir = root_slash.c_str();

    file = FindFirstFileW(dir, &file_info);

    if(file == INVALID_HANDLE_VALUE) return 3; // no matching files found or error opening first file

    do { // for each file in directory

        // for some reason
        // file_info != L"." && file_info != L".."
        // won't work unless file_info.cFileName is assigned to a var
        std::wstring name = file_info.cFileName;

        std::wstring path = root; // full path to current file
        path.append(L"\\").append(name);

        if(!(file_info.dwFileAttributes & FILE_ATTRIBUTE_READONLY)        // not read-only
                && !(file_info.dwFileAttributes & FILE_ATTRIBUTE_OFFLINE) // not physically moved to offline storage
                && !(file_info.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)  // not a system file
                && file_info.dwFileAttributes != INVALID_FILE_ATTRIBUTES  // not invalid
                && (name != L"." && name != L"..")) { // not "." or ".."
            if(file_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) // file is a directory
                get_files(path, ext, log, --limit);
            else // file is not a directory
                if(ext.find(PathFindExtensionW(path.c_str())) != ext.end()) // extension matches
                    log << path << '\n' << std::flush; // log path to file
        }
    } while(FindNextFileW(file, &file_info) != 0);

    FindClose(file);

    return 0;
}

The declarations for dir and file can be moved lower, where they are first assigned values.  However, since dir is only used in one place, it can be eliminated and the value used directly.

HANDLE file = FindFirstFileW(root_slash.c_str(), &file_info);

When testing file attributes, the check for invalid attributes should be first, and you can combine several individual tests into one:

if (file_info.dwFileAttributes != INVALID_FILE_ATTRIBUTES &&
    !(file_info.dwFileAttributes & (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_OFFLINE | FILE_ATTRIBUTE_SYSTEM) &&
    (name != L"." && name != L".."))

Since there are three attributes you want to ignore, you can define a constant to hold them rather than list them out in your if statement.

Incidentally, since cFileName is a C array, you need to assign it to a string variable to be able to use the equality comparisons with it.  Or you could leave it in cFileName and use basic comparisons like strcmp.  Since there are two similar strings that are very short, you could also do direct character comparison but that makes the code larger and harder to understand and should only be done when absolutely necessary.

Since you decrement limit with each recursive call to get_files, you reduce the limit for the current directory as well.  If your initial directory has 16 subdirectories, your search will skip the 10th, may skip some of the subdirectories of the first 9, and will search all of the directories under the 11th and later subdirectories.  You should use

get_files(path, ext, log, limit - 1);

instead.  If any of the recursive calls fail in some way (return nonzero) you ignore the error and keep going.  This is reasonable in this instance, but does make the "recursion limit reached" error somewhat pointless since it will never be returned to the caller.  This one value should probably be handled differently, so that if any search reaches the recursion limit, this value is returned to the original caller to indicate that the results are incomplete.

Potentially more serious is that your extension comparison is case sensitive.  A file called "IMAGE.JPG" will not be listed, because the extension ins in uppercase and you're looking for a lowercase one.

Unless there's an absolute need for it, you should omit the std::flush from the log outputs.  This will reduce the performance as every filename will be written one at a time, instead of in larger chunks.


Reading analog moisture signal from Arduino over USB to RPi3 and publishing via paho MQTT This program is meant to read a moisture sensor attached to an Arduino UNO board via USB to a Raspberry Pi3 which broadcasts it over MQTT.

I am looking for any constructive criticism.

import serial
import time
import RPi.GPIO as GPIO
import paho.mqtt.client as mqtt

def main():
    try:
        while 1:
            sendMQTT(read_sensor("clean"),"Planty/moisture_readings")
            alerts()
    except KeyboardInterrupt:
        print "\n\nExiting via Keyboard Interrupt"

def debugger():
    # This is just to test each reading
    moisture_clean = read_sensor("clean")
    output = "Soil Moisture is " + str(moisture_clean) + "%"
    moisture_raw_adjusted = read_sensor("raw_adjusted")
    output += "\nRaw_adjusted is " + str(moisture_raw_adjusted)
    moisture_raw = read_sensor("raw")
    output += "\nRaw is " + str(moisture_raw)
    sendMQTT(output, "Planty/debug")

def alerts():
    # Generates alerts that are humanly readable for certain moisture thresholds
    moisture = read_sensor("clean")
    alert_low = 10
    alert_high = 90
    alert_threshold = 30
    if moisture <= alert_low:
        output = "**Moisture is below 10%**"
    if moisture <= alert_threshold and moisture > alert_low:
        output = "***Moisture is between 10%` and 30%, watering***"
        relay(14)
        time.sleep(60*1)
    if moisture >= alert_high:
        output = "***Moisture is above 90%***"
    sendMQTT(output, "Planty/alerts")

def read_sensor(choice):
    # Read analog moisture data from moisture probe via Arduino UNO plugged into
    # the RPi via USB
    min = 330
    max = 970
    ser = serial.Serial('/dev/ttyACM0', 9600)
    #Line below handles the problem of the Arduino returning more than 1 reading
    raw_data = int(str.split(ser.readline())[0])
    if choice == "raw":
        moisture = raw_data
    #Stop readings going over or under 0 or 100%
    if raw_data < min:
        raw_data = min
    if raw_data > max:
        raw_data = max
    if choice == "raw_adjusted":
        moisture = raw_data
    if choice == "clean":
        #Get moisture as  % reading
        moisture = int(round(100-(100/float(max-min))*(raw_data-min), 0))
    return moisture

def relay(pin):
    # Turn on relay via PIN.HIGH
    try:
        GPIO.setmode(GPIO.BCM)
        GPIO.setwarnings(False)
        GPIO.setup(pin,GPIO.OUT,initial=1)
        sendMQTT("Turning Relay1 ON", "Planty/relay")
        GPIO.output(pin,0)
        time.sleep(5)
        sendMQTT("Turning Relay1 OFF", "Planty/relay")
        GPIO.output(pin,1)
    finally:
        GPIO.cleanup()

def sendMQTT(output, topic):
    # Publish to MQTT for given topic
    host = "MQTTBroker.local"
    user = "user"
    password = "password"
    mqttc = mqtt.Client()
    mqttc.username_pw_set(user, password)
    mqttc.connect(host, 1883)
    mqttc.publish(topic, output)

if __name__ == '__main__':
    main()

Here're some changes that I'd personally do:
PEP8 Guidlines

Be consistent when writing code! You used both camelCase and snake_case conventions when you declared your functions. Chose one of them and keep it that way. PEP8 recommends the second one.
Talking about consistency, you should also have a space after each #. More, when you want to write what your function is doing, you should use the following:

def read_sensor(choice):
    """
    Read analog moisture data from moisture probe via Arduino UNO plugged into
    the RPi via USB
    """
    # ...

After each comma (or any other operator) you should have a space
This: if moisture <= alert_threshold and moisture > alert_low: can be simplified using a chained expression:

if alert_low < moisture <= alert_threshold:
    ....

Use string format() instead of using the old + operator.
Constants should be UPPER_CASED (but not inside functions). I just took them out.

Personal anecdote:

It feels a bit weird to have the main() function at the beginning and everything within that function after. I'd just move it to the end as it will look more naturally.
You have several magic numbers which should be declared at the beginning. Doing this, you'll be able to modify their value faster than going through all the code and trying to remember what does what.
Don't store variables if you use them only once. It will make you're code harder to follow and it will speed it down (don't think of it as a big speed down but even so...)

The above will lead our code to the following:
import serial
import time
import RPi.GPIO as gpio
import paho.mqtt.client as mqtt

ALERT_LOW = 10
ALERT_HIGH = 90
ALERT_TRESHOLD = 30
RELAY_TIME = 14
TIME_TO_SLEEP = 60 * 1
MIN = 330
MAX = 970
RATE = 9600

def read_sensor(choice):
    """
    Read analog moisture data from moisture probe via Arduino UNO plugged into
    the RPi via USB
    """
    ser = serial.Serial('/dev/ttyACM0', RATE)

    # Line below handles the problem of the Arduino returning more than 1 reading
    raw_data = int(str.split(ser.readline())[0])
    if choice == "raw":
        moisture = raw_data

    # Stop readings going over or under 0 or 100%
    if raw_data < MIN:
        raw_data = MIN
    if raw_data > MAX:
        raw_data = MAX
    if choice is "raw_adjusted":
        moisture = raw_data
    if choice == "clean":
        # Get moisture as  % reading
        moisture = int(round(100 - (100 / float(MAX - MIN)) * (raw_data - MIN), 0))

    return moisture

def send_mqtt(output, topic):
    """Publish to MQTT for given topic"""
    host, user, password, port = "MQTTBroker.local", "user", "password", 1883

    mqttc = mqtt.Client()
    mqttc.username_pw_set(user, password)
    mqttc.connect(host, port)
    mqttc.publish(topic, output)

def alerts():
    """ Generates alerts that are humanly readable for certain moisture thresholds"""
    moisture, output = read_sensor("clean"), ''

    if moisture <= ALERT_LOW:
        output = "**Moisture is below 10%**"
    if ALERT_LOW < moisture <= ALERT_TRESHOLD:
        output = "***Moisture is between 10%` and 30%, watering***"
        relay(RELAY_TIME)
        time.sleep(TIME_TO_SLEEP)
    if moisture >= ALERT_HIGH:
        output = "***Moisture is above 90%***"
    send_mqtt(output, "Planty/alerts")

def debugger():
    """This is just to test each reading"""
    output = "Soil Moisture is: {}%\n" \
             "Raw_adjusted is: {}\n" \
             "Raw is: {}".format(read_sensor("clean"), read_sensor("raw_adjusted"), read_sensor("raw"))

    send_mqtt(output, "Planty/debug")

def relay(pin):
    """Turn on relay via PIN.HIGH"""
    try:
        gpio.setmode(gpio.BCM)
        gpio.setwarnings(False)
        gpio.setup(pin, gpio.OUT, initial=1)
        send_mqtt("Turning Relay1 ON", "Planty/relay")
        gpio.output(pin, 0)
        time.sleep(5)
        send_mqtt("Turning Relay1 OFF", "Planty/relay")
        gpio.output(pin, 1)
    finally:
        gpio.cleanup()

def main():
    try:
        while 1:
            send_mqtt(read_sensor("clean"), "Planty/moisture_readings")
            alerts()
    except KeyboardInterrupt:
        print("\n\nExiting via Keyboard Interrupt")

if __name__ == '__main__':
    main()

Since you're using the try/except in main only for KeyboardInterrupt I'd completely remove it as it seems redundant.
def main():
    while 1:
        send_mqtt(read_sensor("clean"), "Planty/moisture_readings")
        alerts()


Finding hidden field values in a web page before submitting a login form In order to submit a login form using wget, I need to find the values for the hidden field that were added to the form recently.  Grepping for the names gives the following:

$ grep csrf signin.php

  <input type="hidden" name="csrf1" value="56a0ecec3f15c" />
  <input type="hidden" name="csrf2" value="4a8cb442fc85a745c9cf92edd44d71cc3ac7225e99acf57d67276ea0481f9ae8" />
  <input type="hidden" name="csrf1" value="56a0ecec3f236" />
  <input type="hidden" name="csrf2" value="292f226a5048c3d75dcfc28c8e05df49089d55160ff37e5572ecb51e208a1e52" />
  <input type="hidden" name="csrf1" value="56a0ecec3f399" />
  <input type="hidden" name="csrf2" value="a99e22c2361505931a8e26f40d26de156b928b9d218fbe4f02a98f15e2364269" />

I need to extract the two middle values for csrf1 and csrf2, and concatenate them into something that looks like the following, all on one line:

  csrf1=56a0ecec3f236&csrf2=292f226a5048c3d75dcfc28c8e05df49089d55160ff37e5572ecb51e208a1e52

What I came up with, using bash, is the following:

CSRF=`grep csrf signin.php | tail -4 | head -2 | awk -F "\"" '{print $4 "=" $6}'`
CSRF=`echo $CSRF | sed -e 's/ /\&/'`

How would you improve these two lines?

Whenever chaining awk together with grep, sed, head, tail or other text processor tools, consider the possibility of doing it all with just awk, as it's very powerful. Reducing the number of processes in the pipeline makes sense, and in some examples can drastically improve performance.

Here's one way to write the two lines of pipelines as a single line (expanded to multiple lines for readability):

awk -F '"' '
    /csrf1/ {csrf1 = $4 "=" $6}
    /csrf2/ {
      csrf2 = $4 "=" $6;
      if (count++ > 0) {
        print csrf1 "&" csrf2;
        exit;
      }
    }' signup.php

That is:

When you see a line matching "csrf1", save it
When you see a line matching "csrf2", save it, and:

If this is the first time, increment the count and do nothing else
If this is the second time, print csrf1 and csrf2 and stop processing

Note that the algorithm overwrites the first csrf1 and csrf2 values that were seen, and print only the second time.

I also used -F '"' instead of -F "\"" because it's easier to both write and read.

And if you want to save the result of this in a variable, then instead of:

CSRF=`awk ...`

Use the modern $(...) syntax:

CSRF=$(awk ...)


Knight moves in Chess game I am trying to learn Java by doing some (easy) ACM ICPC problems. The problem consist to check if the knight in a chess game can move from a point A(r1, c1) to B(r2, c2) with one move only.

package dalia;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class Dalia {
  static int[] moves = { -1, 2, -1, -2, 1, 2, 1, -2, -2, 1, -2, -1, 2, 1, 2, -1 };

  public static void main(String[] args) throws IOException {
    File fin = new File("./dalia.in");
    knight(fin);
  }

  private static void knight(File fin) throws IOException {
    try (BufferedReader br = new BufferedReader(new FileReader(fin))) {
      String line;
      // Read the number of cases.
      int numberOfCases = Integer.parseInt(br.readLine());

      int[] cord = new int[5];
      String[] parts;
      int i = 0;
      // Solve each case
      while ((line = br.readLine()) != null && i < numberOfCases) {
        // Split the line to array of integer
        parts = line.split(" ");
        for (int j = 0; j < 5; j++) {
          cord[j] = Integer.parseInt(parts[j]);
        }
        // Print case number
        i++;
        System.out.print("Case " + i + ": ");
        // Check if the knight can move
        if (validMove(cord[0], cord[1], cord[2], cord[3], cord[4]))
          System.out.println("YES");
        else
          System.out.println("NO");
      }
      br.close();

    }
  }

  private static boolean validMove(int n, int r1, int c1, int r2, int c2) {
    // Return True if the knight can move from (r1, c1) to (r2, c2) in one
    // move
    for (int i = 0; i < moves.length; i = i + 2) {
      if (r1 == r2 + moves[i] && c1 == c2 + moves[i + 1] && stillInTheBoard(n, r2, moves[i])
          && stillInTheBoard(n, c2, moves[i + 1]))
        return true;

    }
    return false;
  }

  private static boolean stillInTheBoard(int n, int x1, int x2) {
    // Check if the knight still in the board after making the move
    return (1 <= x1 + x2 && x1 + x2 <= n);
  }

}

Example:

Input:

2
4 1 2 2 4
5 1 1 3 3

Output:

Case 1: YES
Case 2: NO

Where the first number is n, the number of block in the Chess board (in the normal Chess board n = 8).

I'm looking for a review in terms of best practices, things I should or shouldn't do, or things I should do in another way.

I feel @spyr03's answer is great (+1 it), but I would like to extend it a but further in a number of ways.
Algorithm
The suggestion that you can validate the move by checking one axis moves 2, and the other moves 1, is an interesting, but not ambitious enough solution. A knight moves in a right-angled pattern, with 2 steps on one side, and 1 on the other.
Pythagoras indicates that the square on the hypoteneuse is the same as the sum of the other two squares.
Putting those together, there is a really neat trick....
public boolean isValid(int r1, int c1, int r2, int c2) {
    // use pythagoras to ensure that a move makes a right-angled
    // triangle move with sides of 1 and 2. 1-squared + 2 squared is 5.
    int deltaR = r2 - r1;
    int deltaC = c2 - c1;
    return 5 == deltaR * deltaR + deltaC * deltaC;
}

You can avoid the conditional checks on 1 or 2 steps, and you can also remove the Math.abs() calls because the square of negative numbers are always positive.
Try-With-Resources
I really like that you have used a try-with-resources to open the buffered reader (though again, @spyr03's suggestion to use a Scanner is a good one.
My special point here, though, is that one of the main reasons that try-with-resources was introduced, is to ensure the resources are always closed in a sane order.
There is no need to explicitly close the buffered reader at all... the try block is designed to do that for you.


Insert and update SQL entries for posts to be accessed by up to 3 other users At first, I have this HTML I use here:

body {
  text-align: center;
}
<form method='post' action=''>
  <input type='text' placeholder='post' name='post'>

    <input type='text' placeholder='ref1' name='ref1'>
    <input type='text' placeholder='ref1id' name='ref1id'>

    <input type='text' placeholder='ref2' name='ref2'>
    <input type='text' placeholder='ref2id' name='ref2id'>

    <input type='text' placeholder='ref3' name='ref3'>
    <input type='text' placeholder='ref3id' name='ref3id'>

  <input type='submit' name='submit'>
</form>

I receive the data using PHP and send it like this:

<?php
if (isset($_POST['submit'])) {
  $post = $_POST['post'];

  $ref1 = $_POST['ref1'];
  $ref1id = $_POST['ref1id'];

  $ref2 = $_POST['ref2'];
  $ref2id = $_POST['ref2id'];

  $ref3 = $_POST['ref3'];
  $ref3id = $_POST['ref3id'];

  $stmt = $conn->prepare($Query)
  $stmt->execute([
    'post' => $post,
    'ref1' => $ref1,
    'ref1id' => $ref1id,
    'ref2' => $ref2,
    'ref2id' => $ref2id,
    'ref3' => $ref3,
    'ref3id' => $ref3id
  ])

}
?>

Then I apply this SQL Query to INSERT the data:

INSERT INTO `posts` (`post`, `ref1`, `ref1id`, `ref2`, `ref2id`, `ref3`, `ref3id`) VALUES (:post, :ref1, :ref1id, :ref2, :ref2id, :ref3, :ref3id)

And this one to UPDATE the data:

UPDATE `posts` SET `post` = :post, `ref1` = :ref1, `ref1id` = :ref1id, `ref2` = :ref2, `ref2id` = :ref2id, `ref3` = :ref3, `ref3id` = :ref3id

in/of this MYSQL Table: http://sqlfiddle.com/#!9/7d22d8/1/0

The idea is that the user can make a private post for 3 people only, He writes their name in ref[x] and their passcode which is ref[x]id

Consider the following table:

CREATE TABLE IF NOT EXISTS `posts` (
  `id` int(6) unsigned NOT NULL,
  `post` varchar(24) NOT NULL,
  PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;

We've stripped out the ref columns.  Now we add a new table:

CREATE TABLE IF NOT EXISTS `post_refs` (
  `posts_id` int(6) unsigned NOT NULL,
  `ref` varchar(24) NOT NULL,
  `ref_id` varchar(24) NOT NULL,
  PRIMARY KEY (`posts_id`, `ref`)
) DEFAULT CHARSET=utf8;

It's possible that the index should contain ref_id rather than ref.  You don't really provide enough context to tell.

This is basic normalization.  Database tables should not contain columns of duplicate information (e.g. numbered columns).  Such columns should be moved into their own table so that you don't have to modify the database structure every time you change the business rule.

To make this work, you need at least two SQL queries, one for each table.  You have to get the posts_id after the first one.  For updates, you would do so with a SELECT (possibly embedded in the query).  For inserts, you could do $conn->lastInsertId() after inserting the post.  For updates, you might want to delete from the post_refs table and then do an insert.  E.g. something like

SELECT id AS posts_id FROM posts WHERE post = :post

If no rows, do

INSERT INTO posts (post) VALUES (:post)

If there are rows, do

DELETE FROM post_refs WHERE posts_id = :posts_id

Either way, then do

INSERT INTO post_refs (posts_id, ref, ref_id) VALUES (:posts_id, :ref, :ref_id)

or the multi-row equivalent.

That logic becomes simpler if you know beforehand whether you need to do an update or an insert.  Your question implies that you do, but it doesn't show how.

Also consider

CREATE TABLE IF NOT EXISTS `post_read_users` (
  `posts_id` int(6) unsigned NOT NULL,
  `users_id` int(6) unsigned NOT NULL,
  PRIMARY KEY (`posts_id`, `users_id`)
) DEFAULT CHARSET=utf8;

Because it's not normally necessary to describe the people that you want to give access.  You usually just select from a list, connecting existing users to the post.  You seem to be creating the users as you go.  So either the person creating has to remember to give the same information each time, or the person reading has to remember multiple passcodes.  Either would be an uncommon and overly difficult implementation.


Generating random HTML color grid This is a Python script that generates a grid of random HTML colors.  It works by reading random data from /dev/urandom and then encoding it as a hexadecimal string.  This string is spit into individual values that will be represented as a color.

I am wanting to know if this is the best way to generate random values for this task.  Would it be better to use the random module?  Or is my method more beneficial for this task?

WIDTH = 64
HIGHT = 5

def chunks(l, n):
   return [l[i:i+n] for i in range(0, len(l), n)]
data = file("/dev/urandom").read(HIGHT*WIDTH*3).encode("hex")
colors = ""
count = 0
for byte in chunks(data, 6):
   if count == WIDTH:
       colors+="
"
       count=0
   count+=1
   colors+="""
&#x25a0;
""" % (byte)
file("colors.html", "w").write(colors)

This is the output:

An implementation that uses the random module has the benefit of being platform independent. This code isn't going to run so well if you try it on Windows.

There is also another alternative. os.urandom() will use the underlying os to provide the random values.

  On a UNIX-like system this will query /dev/urandom, and on Windows it
  will use CryptGenRandom().

You can also use random.SystemRandom() if you still want the OS source, but want to use the interface provided by random.

This answer provides an implementation of chunks() that iterates instead of constructing a list of all the chunks.

I would also lean towards an implementation that opens the output file first, then writes to it as it iterates over the chunks. This way, you won't need to build a huge string in memory before writing it to disk. Similarly, I would generate the random values as needed.


Partition TPL Tasks in ASP.NET 4.0 for WCF Service I would like to know if using partition code like this is ok to do to partition tasks in ASP.NET 4.0 inside of a WCF service.  My goal with this code is to try to do non-blocking calls without spinning up too many concurrent tasks on the server.

public static async Task<IEnumerable<string>> ExecuteTasks(IEnumerable<string> files)
    {
        ConcurrentBag<string> bag = new ConcurrentBag<string>();

        await Task.Factory.ContinueWhenAll(Partitioner.Create(files).GetPartitions(5).Select(partition => Task.Factory.StartNew(() =>
       {
           using (partition)
           {
               while (partition.MoveNext())
               {
                    string test = string.Format("File {2} is executing in thread {0} and {3} in background at {1}",
                                               Thread.CurrentThread.ManagedThreadId,
                                                DateTime.Now, partition.Current,
                                                Thread.CurrentThread.IsBackground);
                   Console.WriteLine(test);
                   bag.Add(test);
               }
           }
        }, TaskCreationOptions.LongRunning)).ToArray(), _ => { });

        return bag.ToList();
    }

Specification

There are a couple of strategies available to you depending on what exactly you mean with

  "not spinning up too many concurrent tasks"

Possible Strategies

If you want a maximum allowed number of tasks to be processed concurrently on the server, regardless of this method, assuming you are using the default task scheduler, you can set the maximum number of threads of the .NET ThreadPool.
If you want a maximum allowed number of tasks to be processed concurrently for this given scenario, leaving the other task scheduling on the server alone, you can:

implement a custom TaskScheduler with its own dedicated thread pool. In .NET Framework you might also implement a custom SynchronizationContext instead. It's a lower level than the scheduler and you can always create a TaskScheduler.FromCurrentSynchronizationContext();.
use a SemaphoreSlim to limit the number of tasks that actually perform work concurrently. Note that this could still lead to more threads than your threshold to be activated, however, only the specified threshold amount of threads would actually be performing work.
Use JanDotNet's Parallel with MaxDegreeOfParallelism approach within a single Task. Note that this changes the affinity from threads to processors. Not sure whether this is what you are looking for. But at least it's a possibility, and non-blocking because wrapped inside a Task.


Reviewing the Dockerfile and docker-compose.yml files for a basic nodejs mysql project I'm new to docker and after much research and stufy I've created a sample application and would like it to be reviewed in case if I'm missing any corner or if there's redundant information that can be simplified.

Dockerfile

FROM node

WORKDIR /src
COPY . .
RUN npm install

CMD ["npm", "start"]

docker-compose.yml

MySQL is getting initialized through schema.sql

version: '3'

services:
  database:
    image: "mysql:5.7"
    container_name: "mysql"
    ports:
      - "6603:3306"
    volumes:
      - ./schema.sql:/docker-entrypoint-initdb.d/init.sql
    environment:
      DATABASE_HOST: database
      MYSQL_ROOT_PASSWORD: rootpass
      MYSQL_DATABASE: test
      MYSQL_USER: mysql
      MYSQL_PASSWORD: password

  service:
    build: .
    image: "node"
    container_name: "nodejs"
    ports:
      - "8080:8080"
    depends_on:
      - database
    links:
      - database
    environment:
      DATABASE_HOST: database
      MYSQL_PORT: 3306
      MYSQL_DATABASE: test
      MYSQL_USER: mysql
      MYSQL_PASSWORD: password
    restart: on-failure

index.js

It has some delay, that basically waits for the mysql to become fully alive.

setTimeout(function(){
    var mysql = require('mysql');

    var con = mysql.createConnection({
        host: process.env.DATABASE_HOST,
        user: process.env.MYSQL_USER,
        password: process.env.MYSQL_PASSWORD,
        database: process.env.MYSQL_DATABASE
    });

    con.connect(function(err) {
        if (err) throw err;
        console.log("Database Connected!");

        var http = require('http');

        //create a server object:
        http.createServer(function (req, res) {
            res.write('Hello World!'); //write a response to the client
            res.end(); //end the response
        }).listen(8080); //the server object listens on port 8080

        console.log("Listening at 8080");
    });
}, 10 * 1000);

I had one file in docker-compose.yml which I removed assuming it's not required.

#    volumes:
#      - .:/src

Probably it's better to you concrete node version.

Also, you can try expressjs framework.

I have got simple example of Dockerfile here in this sub-directory of my repository.
But that is without Docker Compose and connection to any database.


Merging K Sorted Linked Lists Specifically, how can I improve the time complexity of my algorithm (currently it is O(listLength * numberOfLists))? It only beats 5% of accepted LeetCode solutions, which surprised me.

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    private void advance(final ListNode[] listNodes, final int index) {
        listNodes[index] = listNodes[index].next;
    }

    public ListNode mergeKLists(final ListNode[] listNodes) {
        ListNode sortedListHead = null;
        ListNode sortedListNode = null;

        int associatedIndex;

        do {
            int minValue = Integer.MAX_VALUE;
            associatedIndex = -1;

            for (int listIndex = 0; listIndex < listNodes.length; listIndex++) {
                final ListNode listNode = listNodes[listIndex];

                if (listNode != null && listNode.val < minValue) {
                    minValue = listNode.val;
                    associatedIndex = listIndex;
                }
            }

            // An associated index of -1 indicates no more values left in any of the given lists
            if (associatedIndex != -1) {
                if (sortedListNode == null) {
                    sortedListNode = new ListNode(minValue);
                    sortedListHead = sortedListNode;
                }
                else {
                    sortedListNode.next = new ListNode(minValue);
                    sortedListNode = sortedListNode.next;
                }

                advance(listNodes, associatedIndex);
            }
        }
        while (associatedIndex != -1);

        return sortedListHead;
    }
}

Note that the Solution class in addition to ListNode is already provided, the only code that I wrote was inside mergeKLists.

The time complexity is \$O(\text{listLength} * \text{numberOfLists}^2)\$ because the check of which node is the smallest? is looking at one element of each list every iteration (so each iteration's complexity is \$O(\text{numberOfLists})\$, and there are \$\text{listLength} * \text{numberOfLists}\$ iterations.

You can get to \$O(\text{listLength} * \text{numberOfLists} * \log(\text{numberOfLists}))\$ by using a sorted list of the ListNode elements that you are checking in each iteration, instead of the unsorted array listNodes. Let's call this list sortedNodes. You can avoid checking each element of sortedNodes every iteration because you know the first one is the smallest, and once you take this first value into the merged list and advance the node - do a binary search to decide where to move the first element after its value has changed. (Or remove it if it got to a null.)