Calculating the Average Minimum Length of Random Numbers From 0 - 1 that Adds up to Greater Than 1 My Computer Science teacher gave us a task to calculate the average after one billion trials.

His exact assignment was this:

  Consider generating a sequence of random numbers on the interval [0, 1) perhaps using Math.random() in a loop and adding them together.  How many numbers would you expect to generate in such a sequence before the sum exceeds 1.0 ? (i.e. probability)

  Write a program that simulates these trials a billion times and calculates the average length of the sequences.
  This is a neat exercise in nested loops.

  Examples:

  0.449   0.814

  length of sequence: 2

  0.167 0.138 0.028 0.934

  length of sequence: 4

  0.640 0.258 0.417

  length of sequence: 3

  0.911 0.212

  length of sequence: 2

  Average of the four lengths is 11/4 ≈ 2.75

  What is the average of one billion random sequences?

My code was this:

import random

def genSequence():
  storenums = 0
  numTrials = 1000000000
  for x in range(0,numTrials):
    numberOfAttempts = 0
    getToOne = 0
    while (getToOne < 1): #keeps on generating random numbers and adding it to getToOne until it reaches 1 or is over 1
      getToOne += random.random()
      numberOfAttempts += 1
    storenums = storenums + numberOfAttempts
    #print (x)
    #print(storenums)
  calculateAverage(storenums,numTrials)

def calculateAverage(num,den):
  average = num/den
  print(average)

genSequence()

*Note: I am using repl.it to run my code so there is no main.

The problem with my code is that it can't reach 1 billion trials and stops working at around 227,035. I'm pretty sure this is a memory issue but I don't know how to fix this. What can I do so that it actually completes the billion trials and preferably not in an egregiously long amount of time.

EDIT: My teacher the result should be e, but that isn't the point as I just need to write the code. Getting e just means I did it right.

If I rewrite genSequence so that it takes numTrials as an argument, then I get the following timing in CPython:

Python 3.6.4 (default, Dec 21 2017, 20:33:21)
>>> from timeit import timeit
>>> timeit(lambda:genSequence(10**8), number=1)
2.71825759
62.77562193598715

Based on this, it would take about 10 minutes to compute genSequence(10**9). Possibly you just didn't wait long enough.

This kind of loop-heavy numerical code generally runs much faster if you use PyPy, which has a "just-in-time" compiler. I get more than ten times speedup with PyPy:

[PyPy 5.10.0 with GCC 4.2.1 Compatible Apple LLVM 9.0] on darwin
>>>> from timeit import timeit
>>>> timeit(lambda:genSequence(10**8), number=1)
2.71816679
5.389536142349243

On PyPy you should be able to carry out \$10^9\$ trials in under a minute (on my computer it takes 51 seconds).

Some review points:

The number 1000000000 is hard to read — it could easily be confused with 100000000 or 10000000000. I would write 10**9 to make it clear.
There's no need for the variable numberOfAttempts; you could just add one to storenums on each loop.
The name storenums is a bit vague. This is the total length of the random sequences generated so far, so a name like total_length would be clearer.
Similarly, the name genSequence is vague. This calculates the mean length of a random sequence, so a name like mean_sequence_length would be clearer.
The meaning of the constant 1 is not altogether clear. I would give it a name like target_sum.
When a loop variable like x is not used, it's conventional to name it _.
range(0,numTrials) can be written range(numTrials).

Revised code:

import random

def mean_sequence_length(trials, target_sum=1.0):
    """Return mean length of random sequences adding up to at least
    target_sum (carrying out the given number of trials).

    """
    total_length = 0
    for _ in range(trials):
        current_sum = 0.0
        while current_sum < target_sum:
            current_sum += random.random()
            total_length += 1
    return total_length / trials


Extract data labels from a chart in PowerPoint to Excel Would like to check if anyone here is able to simplify the following code.
What it does: Extract data labels value from a chart in powerpoint to excel.

Sub Extract_Datalabels3()
'Goal: To extract datalabels of Chart's series collection and write to excel
'Working

    Dim sh As Shape
    Dim sld As slide
    Dim chtnow As Chart

    Dim x As Integer
    Dim z As Integer

    Dim xlApp As New Excel.Application
    Dim xlWorkbook As Excel.Workbook
    Dim xlworksheet As Excel.Worksheet

    Set xlWorkbook = xlApp.Workbooks.Add
    Set xlworksheet = xlWorkbook.Worksheets.Add
    xlApp.Visible = True

Set chtnow = ActiveWindow.Selection.ShapeRange(1).Chart

z = ActiveWindow.Selection.ShapeRange(1).Chart.SeriesCollection(1).DataLabels.Count

For x = 1 To z
xlWorkbook.Sheets(1).Range("A" & x).Value = chtnow.SeriesCollection(1).DataLabels(x).Text
Next

For x = 1 To z
xlWorkbook.Sheets(1).Range("B" & x).Value = chtnow.SeriesCollection(2).DataLabels(x).Text
Next

For x = 1 To z
xlWorkbook.Sheets(1).Range("C" & x).Value = chtnow.SeriesCollection(3).DataLabels(x).Text
Next

For x = 1 To z
xlWorkbook.Sheets(1).Range("D" & x).Value = chtnow.SeriesCollection(4).DataLabels(x).Text
Next

For x = 1 To z
xlWorkbook.Sheets(1).Range("E" & x).Value = chtnow.SeriesCollection(5).DataLabels(x).Text
Next

For x = 1 To z
xlWorkbook.Sheets(1).Range("F" & x).Value = chtnow.SeriesCollection(6).DataLabels(x).Text
Next

For x = 1 To z
xlWorkbook.Sheets(1).Range("G" & x).Value = chtnow.SeriesCollection(7).DataLabels(x).Text
Next

For x = 1 To z
xlWorkbook.Sheets(1).Range("H" & x).Value = chtnow.SeriesCollection(8).DataLabels(x).Text
Next

For x = 1 To z
xlWorkbook.Sheets(1).Range("I" & x).Value = chtnow.SeriesCollection(9).DataLabels(x).Text
Next

For x = 1 To z
xlWorkbook.Sheets(1).Range("J" & x).Value = chtnow.SeriesCollection(10).DataLabels(x).Text
Next

End Sub

Would this work instead of all the loops:

For a = 1 To 10
    For x = 1 To z
        xlWorkbook.Sheets(1).Cells(x, a) = chtnow.SeriesCollection(a).DataLabels(x).Text
    Next
Next a

do not use Interger in VBA - https://stackoverflow.com/questions/26409117/why-use-integer-instead-of-long
do not use variable names like a, z, x, but give them some meaningful names. E.g. x can be row or rows, thus the code would be easier to get.


C# static class holding list of member class instances I was wondering if such a design was a bad idea in C#. Here I have a static class EventLog which holds a list of instances of Event class. This is a simple event log of actions players can do in a game. What can I be doing in a better way?

public class EventLog
{
    private const int maxEventsInLog = 50;
    private static List<Event> events = new List<Event>();
    private static string[] eventDescriptions = new string[8]
    {
        "<fromUnit> walked",
        "<fromUnit> waited",
        "<fromUnit> inquired",
        "<fromUnit> allured <toUnit>",
        "<fromUnit> persuaded <toUnit>",
        "<fromUnit> bribed <toUnit>",
        "<fromUnit> hired <toUnit>",
        "<fromUnit> attacked <toUnit>",
    };

public static void AddEvent(GameObject _fromUnit, GameObject _toUnit, Utils.ActionType _actionType)
{
    events.Add(new Event(_fromUnit, _toUnit, _actionType));

    if (events.Count > maxEventsInLog)
    {
        while (events.Count > maxEventsInLog)
        {
            events.RemoveAt(0);
        }
    }
}

public static int NumEventsInLog()
{
    return events.Count;
}

public static string GetEventDescription(int eventIndex)
{
    if (eventIndex < 0 || eventIndex > events.Count - 1)
        return null;

    return events[eventIndex].description;
}

private class Event
{
    private GameObject fromUnit;
    private GameObject toUnit;
    private Utils.ActionType actionType;
    public string description;

    public Event(GameObject _fromUnit, GameObject _toUnit, Utils.ActionType _actionType)
    {
        fromUnit = _fromUnit;
        toUnit = _toUnit;
        actionType = _actionType;

        description = eventDescriptions[(int)_actionType];
        description = description.Replace("<fromUnit>", _fromUnit.GetComponent<UnitData>().firstName + " " + _fromUnit.GetComponent<UnitData>().lastName);
        description = description.Replace("<toUnit>", _toUnit.GetComponent<UnitData>().firstName + " " + _toUnit.GetComponent<UnitData>().lastName);
    }
}

I think your tokens <fromUnit> and <toUnit> should be public static readonly string fields, instead of being hard-coded the way they are. This turns eventDescriptions into something like this:

private static string[] eventDescriptions = new string[8]
{
    FromUnitToken + " walked",
    FromUnitToken + " waited",
    FromUnitToken + " inquired",
    FromUnitToken + " allured " + ToUnitToken,
    FromUnitToken + " persuaded " + ToUnitToken,
    FromUnitToken + " bribed " + ToUnitToken,
    FromUnitToken + " hired " + ToUnitToken,
    FromUnitToken + " attacked " + ToUnitToken,
};

And then the Event constructor can do something like this:

    description = description.Replace(EventLog.FromUnitToken, _fromUnit.GetComponent<UnitData>().firstName + " " + _fromUnit.GetComponent<UnitData>().lastName);
    description = description.Replace(EventLog.ToUnitToken, _toUnit.GetComponent<UnitData>().firstName + " " + _toUnit.GetComponent<UnitData>().lastName);

The description field is problematic though:

public string description;

Instance fields shouldn't be public - that's breaking encapsulation, anyone that can use an Event instance can do whatever they want with that value. Better encapsulate it:

private readonly string description;
public string Description { get { return description; } }

Fields initialized from constructor should be marked readonly if that's the intent - it makes the intent clearer.

In AddEvents, you're adding events, and then removing whatever exceeds your max capacity:

events.Add(new Event(_fromUnit, _toUnit, _actionType));

if (events.Count > maxEventsInLog)
{
    while (events.Count > maxEventsInLog)
    {
        events.RemoveAt(0);
    }
}

Looks like you want some First In, First Out data structure - a List just isn't practical for that - consider using a Queue<Event> instead.


LeetCode: LRU cache implementation C# I have implemented an LRU cache, the question is taken from: leetcode: 146. LRU Cache

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put.
get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
put(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.

Tests:
[TestClass]
public class LruCacheUnitTest
{
    [TestMethod]
    public void LruCacheTest()
    {
        LRUCache cache = new LRUCache(2 /* capacity */ );

        cache.put(1, 1);
        cache.put(2, 2);
        Assert.AreEqual(1, cache.get(1));       // returns 1
        cache.put(3, 3);    // evicts key 2
        Assert.AreEqual(-1, cache.get(2));       // returns -1 (not found)
        cache.put(4, 4);    // evicts key 1
        Assert.AreEqual(-1, cache.get(1));       // returns -1 (not found)
        Assert.AreEqual(3, cache.get(3));       // returns 3
        Assert.AreEqual(4, cache.get(4));       // returns 4
    }
}

Implementation:
public class LRUCache
{
    private int _numOfCells;
    private Dictionary<int, int> _cache;
    private List<KeyValuePair<int, int>> _orderList;

    public LRUCache(int numberOfCacheCells)
    {
        this._numOfCells = numberOfCacheCells;
        _cache = new Dictionary<int, int>(_numOfCells);
        _orderList = new List<KeyValuePair<int, int>>(_numOfCells);
    }

    public void put(int key, int value)
    {
        if (_cache.Count == _numOfCells) // the cache is full we need to remove 1
        {
            var toRemove = _orderList[0];
            _cache.Remove(toRemove.Key);
            _orderList.Remove(toRemove);
        }
        _orderList.Add(new KeyValuePair<int, int>(key, value));
        _cache[key] = value;
    }

    public int get(int key)
    {
        if (!_cache.ContainsKey(key))
        {
            return -1;
        }

        //put the key and value to the back of the ordered list
        var tempCacheCell = _orderList.FirstOrDefault(x=>x.Key == key);
        _orderList.Remove(tempCacheCell);
        _orderList.Add(tempCacheCell);
        return _cache[key];
    }
}

The indentation is inconsistent: the test class has its {} indented the same as the class declaration, but the main class has them indented one level more.

It's good that you've included a unit test, but it's only testing half of the functionality. What about the getter?

put and get as method names don't follow C# conventions, which would be Put and Get -- although it would be even more idiomatic here to make them an indexer. The original task is phrased in terms which are as language-agnostic as possible, but in an interview you should aim to show language knowledge where you have it as well as general knowledge and skill.

Is there any reason for hard-coding int as the type of the key and value rather than making them generic?

private List<KeyValuePair<int, int>> _orderList;

Two questions: firstly, why KeyValuePair<int, int>? I don't see anything which uses the value. Secondly, why include List in the name? The type says that already.

                _orderList.Remove(toRemove);

            var tempCacheCell = _orderList.FirstOrDefault(x=>x.Key == key);
            _orderList.Remove(tempCacheCell);
            _orderList.Add(tempCacheCell);

What's the asymptotic complexity of adding or fetching an element? It can certainly be improved. Hint: one approach would be to manually implement a different type of list. Alternative hint: there's some quite powerful collections in .Net Framework (I haven't checked which of them made it into .Net Standard).


Count character replacements needed to make string a palindrome Problem

Count the number of character replacements necessary to convert an input String to a palindrome.
Each character replacement must replace the given character with the character directly before it in the alphabet (sorted A-Z).
In other words, D can only be replaced with C.
For simplicity, only consider Strings that are comprised of alphabetic characters.

Inspired by this HackerRank problem.

Example

A => already palindromic, so 0 character replacements are necessary
ACBD => ABBD => ABBC => ABBB => ABBA, 4 character replacements

Implementation

The solution is pretty basic, so maybe getting feedback is unnecessary, but is there a better implementation?

public class PalindromeConversionCounter {
  public static int count(String s) {
    int count = 0;
    char[] chars = s.toCharArray();
    for (int i = 0; i < Math.ceil(chars.length / 2); i++) {
      char currentChar = Character.toLowerCase(chars[i]);
      char oppositeChar = Character.toLowerCase(chars[chars.length - 1 - i]);
      if (!Character.isAlphabetic(currentChar) || !Character.isAlphabetic(oppositeChar)) {
        throw new IllegalArgumentException("String should contain only alphabetic characters");
      }
      count += Math.abs(currentChar - oppositeChar);
    }
    return count;
  }
}

for (int i = 0; i < Math.ceil(chars.length / 2); i++) {
      char currentChar = Character.toLowerCase(chars[i]);
      char oppositeChar = Character.toLowerCase(chars[chars.length - 1 - i]);

You'll get the identical answer from Math.floor

    for (int i = 0; i < Math.floor(chars.length / 2.0); i++) {
      char currentChar = Character.toLowerCase(chars[i]);
      char oppositeChar = Character.toLowerCase(chars[chars.length - 1 - i]);

For one thing, you would get the integer result from the original since you were dividing two integers.  But even if you fix that, realize that both return the same answer.  Because when i == chars.length - 1 - i, the absolute value of the difference will always be zero.  And that case is the one that the second version does not do.

    for (int i = 0; i < chars.length / 2; i++) {
      char currentChar = Character.toLowerCase(chars[i]);
      char oppositeChar = Character.toLowerCase(chars[chars.length - 1 - i]);

This does the same thing through the magic of integer math which implicitly takes the floor for you.

You can save some math by using two variables.

    for (int i = 0, j = chars.length - 1; i < j; i++, j--) {
      char currentChar = Character.toLowerCase(chars[i]);
      char oppositeChar = Character.toLowerCase(chars[j]);


Refactoring this legacy application to a cleaner state (continued) This is continued from here:

Refactor a legacy application to a less-unwieldy state

I am refactoring a legacy application. I DO NOT have the ability to change the Permissions, CustomerReport or the ListItems classes. They are in external dlls that are given to me. I realize that the ref usage is dumb and it makes NO SENSE. I am going to work with my peers to follow better practices.

This whole class looks ugly and mind you I have refactored some of it out into methods but it still is too large and unwieldy.

The code is used to return a TreeViewItem in VB. We are rewriting the front end into C# and I am using an API call to get the data from this service. So what I need is to hand the front end something reusable for every call to this report service.

I am mainly focused on cleaning up the AssembleNodes method, but any and all help is great.

public class ReportService : IReportService
{
    private readonly Permissions _permissions;

    public ReportService()
    {
        _permissions = new Permissions();
    }

    public IList<Node> GetFor(string userGroup)
    {
        var listItems = new List<ListItem>();
        //Yes this is silly since statusReturn is never used
        var statusReturn = _permissions.GetReportsForUserGroupName(userGroup, ref listItems);
        return AssembleNodes(listItems);
    }

    public IList<Node> GetFor(int userKey)
    {
        var listItems = new List<ListItem>();
        //Yes this is silly since statusReturn is never used
        var statusReturn = _permissions.GetReportsForUser(userKey, ref listItems);
        return AssembleNodes(listItems);
    }

    //I want to refactor this method down more
    private static List<Node> AssembleNodes(IList<ListItem> listItems)
    {
        var nodes = new List<Node>();
        if (!listItems.Any()) return nodes;
        var initialReportType = listItems[0].value;
        var initialCrs = listItems[0].key;
        var parent = AssembleNode(initialReportType);
        var child = AssembleNode(initialCrs, parent);
        foreach (var item in listItems)
        {
            var customerReport = (CustomerReport) item.dataObject;
            var grandChild = AssembleGrandChild(child, customerReport);
            if (item.value.Equals(initialReportType))
            {
                if (!item.key.Equals(initialCrs))
                {
                    initialCrs = item.key;
                    parent.Children.Add(child);
                    child = AssembleNode(item.key, parent);
                    child.Children.Add(grandChild);
                }
                    child.Children.Add(grandChild);
            }
            else
            {
                initialReportType = item.value;
                initialCrs = item.key;
                parent.Children.Add(child);
                nodes.Add(parent);
                parent = AssembleNode(item.value);
                child = AssembleNode(item.key, parent);
                grandChild = AssembleGrandChild(child, customerReport);
                child.Children.Add(grandChild);
            }
        }
        parent.Children.Add(child);
        nodes.Add(parent);
        return nodes;
    }

    private static Node AssembleNode(string value, Node parent = null)
    {
        return parent.IsNotNull() ?
            new Node(parent) { Text = value, Value = value } :
            new Node { Text = value, Value = value };
    }

    private static Node AssembleGrandChild(Node child, CustomerReport customerReport)
    {
        return new Node(child)
        {
            Text = customerReport.FileName,
            Value = customerReport.ReportKey.ToString(),
            Title = customerReport.Description
        };
    }
}

I really want to refactor this to a better state; any suggestions?

you should move

child.Children.Add(grandChild);

outside of your if statements like so:

    foreach (var item in gmcListItems)
    {
        var customerReport = (CustomerReport) item.dataObject;
        var grandChild = AssembleGrandChild(child, customerReport);
        if (item.value.Equals(initialReportType))
        {
            if (!item.key.Equals(initialCrs))
            {
                initialCrs = item.key;
                parent.Children.Add(child);
                child = AssembleNode(item.key, parent);
            }
        }
        else
        {
            initialReportType = item.value;
            initialCrs = item.key;
            parent.Children.Add(child);
            nodes.Add(parent);
            parent = AssembleNode(item.value);
            child = AssembleNode(item.key, parent);
            grandChild = AssembleGrandChild(child, customerReport);
        }
        child.Children.Add(grandChild); // <--
    }
    parent.Children.Add(child);
    nodes.Add(parent);

Otherwise you are going to have a whole lot of duplicates.


Creating a slider to adjust the width and two click events I have some JavaScript that was put together for functionality but I wanted to know if there was a better way to write it. As it stands it's a little hard to read and follow.

$(document).ready(function(){
var loop = null;

$("li").click(function(){
    $("li").css("box-shadow", "none");
    $(".selector").css("visibility","hidden");
    var offSet = 7;
    if($(this).hasClass('Selected')){
        $(this).removeClass('Selected');
        $(this).children("div").css("visibility","hidden");
        $(this).css("box-shadow", "none");
    } else{
        $("li").removeClass('Selected');
        $(this).addClass('Selected');
        $(this).children("div").css("visibility","visible");
        $(this).css("box-shadow", "inset 0 0 " + offSet + "px #333");
    }
});

$("#sideNav > ul > li").click(function(){
    var element = $("#content");
    var offsetElement = $("#subSideNav");
    var width = 0;
    var offSet = parseInt(offsetElement.css("width")) + 2;

    if($(this).attr('id') === "Selected" && !element.hasClass('large')){
        $(this).attr('id', '');
        width = parseInt(element.css("width")) + 2;
        slider(element, offSet, 'hidden');
        $(element).addClass('large');
    } else if($(this).attr('id') !== 'Selected' && element.hasClass('large')){
        $("#subSideNav").css('display', 'block');
        $(this).attr('id', 'Selected');
        width = parseInt(element.css("width")) - 2;
        slider(element, offSet, 'visible');
        $(element).removeClass('large');
    }else if($(this).attr('id') !== 'Selected' && !element.hasClass('large')){
        $("#sideNav > ul > li").attr('id', '');
        $(this).attr('id', 'Selected');
    }
});

function slider(element, offSet, visibility){
    var width = parseInt(element.css("width"));
    if('visible' === visibility){
        width -= 1;
    } else{
        width += 1;
    }
    offSet -= 1;
    if(0 < offSet){
        element.css("width", width + "px");
        loop = setTimeout(function(){slider(element, offSet, visibility)}, 5);
    } else{
        clearTimeout(loop);
    }
}
})

The functions are basically supposed to create a slider to adjust the width and two click events. One event make a cover selector div visible to let you know it has been clicked, and the other does the same plus triggering the slider function. The reason there are two different clicks is because there are two separate menus, and only one needs the slider function.

I was able to take out 5 lines worth after noticing that some lines weren't being used or used twice in the click events.

$("li").click(function(){
    $(".selector").css("visibility","hidden");
    var offSet = 7;
    if($(this).hasClass('Selected')){
        $(this).removeClass('Selected');
        $(this).css("box-shadow", "none");
        $(this).children("div").css("visibility","hidden");
    } else{
        $("li").css("box-shadow", "none");
        $(this).addClass('Selected').siblings().removeClass('Selected');
        $(this).css("box-shadow", "inset 0 0 " + offSet + "px #333");
        $(this).children("div").css("visibility","visible");
    }
});

$("#sideNav > ul > li").click(function(){
    var element = $("#content");
    var offsetElement = $("#subSideNav");
    var offSet = parseInt(offsetElement.css("width")) + 2;

    if($(this).attr('id') === "Selected" && !element.hasClass('large')){
        $(this).attr('id', '');
        slider(element, offSet, 'hidden');
        element.addClass('large');
    } else if($(this).attr('id') !== 'Selected' && element.hasClass('large')){
        offsetElement.css('display', 'block');
        $(this).attr('id', 'Selected');
        slider(element, offSet, 'visible');
        element.removeClass('large');
    } else if($(this).attr('id') !== 'Selected' && !element.hasClass('large')){
        $(this).attr('id', 'Selected').siblings().attr('id', '');
    }
});


Run a timer while waiting for user input I am making a C# console maths test where the user answers maths questions.

I am trying to add a timer to the test, I have managed to make a timer but when I run my code it becomes a mess!

Here is some example code:

class Program
{
    public static OtherCode()
    {
        \\*other code for test
    }
    public class Timer
    {
        public static int Timers(int timeLeft)
        {
            do
            {
                Console.Write("\rtimeLeft: {0} ", timeLeft);
                timeLeft--;
                Thread.Sleep(1000);
            } while (timeLeft > 0);
            Console.Write(Environment.NewLine);
            return timeLeft;
        }
    }

    public static void Main(string[] args)
    {
        int numberOfSeconds = 30;
        Thread thread = new Thread(new ThreadStart(() => {
                TimerClass.Timers(numberOfSeconds);
            }));
        thread.Start();

        \\other code
        OtherCode();
    }
}

Here is my full code:
https://github.com/CrazyDanyal1414/mathstester

This is what it looks like when I run the code and try to type an answer to a question:

As you can see my timer code is overlapping my question code.

If I use Console.SetCursorPosition(), my timer doesn't move but When I try to type an answer to a maths question my code makes me write it on the same line as the timer like so:

Any help appreciated!

Several things:

When doing multi-processing and sharing a resource (in this case, the console window), you should use locks to prevent two threads using the same resource at once. This is the source of your problems. When updating the counter you probably need to hold the lock while moving the cursor to the timer, change the timer string, and hold it until you set the cursor back to the bottom of the console. On the other thread you'd have to maintain the lock while reading user input.
You don't need to roll-your-own timers. You can use .NET's Timer class. Notice that Thread.Sleep() is not accurate and you're not really counting seconds (even if you roll-your-own than you should probably do it with time deltas and not with thread sleeps).
Don't use the Thread class, use the Task Parallel Library class. It'll be much easier. You will need to use Task.Delay instead of Thread.Sleep
Notice that you're line-breaking outside of the loop and not inside. Also WriteLine will add a line-break at the end automatically

If you're merely learning the language (I noticed the "beginner" tag) than I suggest you learn the points I've mentioned. They're more important than learning a specific (and somewhat advanced) use case of C# console applications where you change text while accepting user input. I believe it would be easier for you if it would be a simple WPF application instead of a console application.


Truly private JavaScript variables I created a function which has, I believe, truly private variables.  It's not quite production ready for several reasons:

It's using Object.defineProperty -- I can deal with this
It is re-defining every function in the prototype by doing eval( fn.toString() ) -- I do not like this
I'm re-defining the prototypes, hence doing-away with the advantage of prototypes! -- deal breaker

Anyway, I'm putting this out there for critique / suggestions / recommendations for how to fix the above points.  I'm not sure I'll ever really do anything with it, but I find it to be an interesting problem:

function foo(){
    this.definePrivateProperties('foo', 'bar');
}

foo.prototype.setFoo = function(x){
    this.foo = x;
};

foo.prototype.getFoo = function(){
    return this.foo;
};

foo.prototype.definePrivateProperties = function(){
    var   self = this,
          args = arguments,
      validFns = {},
         proto = this.__proto__;

    // replace all functions in prototype with new ones!
    // set a random key on each new function
    //   (not 100% secure, but close enough for now)
    //   this ensures that the function is unique to -this- object
    for(var fn in proto){
        this[ fn ] = eval( '(' + proto[fn] + ')' );
        validFns[ this[ fn ].key = Math.random() ] = 1;
    }

    for(var i=0, l=args.length; i<l; i++){
        (function(){
            var val;

            Object.defineProperty(self, args[i], {
                get          : function getter(){
                    if(validFns[ arguments.callee.caller.key ])
                        return val;
                },
                set          : function setter(v){
                    if(validFns[ arguments.callee.caller.key ])
                        val = v;
                },
                configurable : true,
                enumerable   : false
            });
        })();
    }
};

> var x = new foo();
> x.setFoo(42);
> x.foo = 20;
20
> x.foo;
undefined
> x.getFoo();
42

As for the code:

The property getter and setter should probably throw an exception if the check against the caller failed.  If you attempt to set it from any function besides one defined on the prototype, you don't want it to silently fail -- if it does, you'll be wasting lots of time trying to figure out why your changes aren't sticking.  (Particularly in light of other issues below.)
If you're going to assign a different key to each member function, you might consider making validFns[key] be the function with that key, rather than just '1', so you can check it against the caller.  (Ensures the key doesn't ever accidentally match.)
You're setting keys on each thing you copy from the prototype, regardless of whether it's a function.  Only functions need keys; other stuff (well, anything really, but particularly non-function members) might have its own purpose for key.
The way the access checks are now, only member functions can use them.  Though that may be the intent, it is way more crippling than it sounds.

Observe:

foo.prototype.doStuff = function() {
    var self = this;
    $('input.some_selector').each(function() {
        self.foo += parseInt(this.value);
    });
};

We're in a private function, which should have access to foo.  But since the actual access is within an anonymous function, and not doStuff, it's not allowed.  In order to fix this (and fix it you should), your access check would need to go down the call stack (check the caller, then the caller's caller, and so on) til it finds a function with a key.  Recursive functions seem to cause such a search massive amounts of trouble, though; a depth limit might help with that.

definePrivateProperties can be run a second time.  And a third, fourth, etc.  And each time you call it, it obliterates the previous property's value in order to add the new one.  Read: external code can mess up your private variables.

Say:

foo.prototype.mangleFoo = function() { this.foo = 100; };
x.definePrivateProperties('foo');
delete foo.prototype.mangleFoo;
x.mangleFoo();

(BTW, see what we just did there?  We can add a function and give it access to the "private" properties pretty much at will.)  In order to prevent that...i dunno.  redefine definePrivateProperties after it's run once?

In contrast, the standard method (hiding the variable away in a closure) is not prone to such hackery.


Double-Ended Queue - Have I over-engineered it again? I'm working on an implementation of the Double-Ended Queue as a Doubly-Linked List (for personal enrichment), and I was wondering if anyone minded taking a look at my Constructor/Destructor and Empty functions to see if I'm on the right track.  It should be self-explanatory enough on it's own (I hope).

    DeQueue::DeQueue() {
        // set up the sentinels
        head = new QueueItem();
        tail = new QueueItem();

        head->next = tail;
        tail->last = head;

        head->data = NULL;
        tail->data = NULL;
        tail->next = head->last = NULL;
    }

   DeQueue::~DeQueue() {
    QueueItem* current = head;
    QueueItem* next;
    current = current->next;
    for (;current != tail; current = current->next){
        delete current->last;
    }
    delete current;
    }

    bool DeQueue::Empty() {
        return tail->last == head;
    }

The idea is that the two head and tail sentinels will always remain at the ends, so the easiest way to check if it is empty is to determine if head points to tail or vice versa.  Also, in my destructor, I start at the tail and delete everything from there back, sentinels included.  Any suggestions?

EDIT:  The destructor now moves forward one node, deletes what's behind it, then moves on.  Sound like a better approach?

I would make it so that you only have one sentanal.

Then the list is empty when head and tail point at the same object.

DeQueue::DeQueue()
    // set up the sentinels
    : head(new QueueItem())   // prefer to use initialize list.
    , tail(head)              // Make sure your declarations are in the correct order.
{

    head->next = head;        // Circular linked list.
    head->last = head;

    head->data = NULL;        // No data in the fake node.
}

Your delete loop does not actually move:

while(current->next != head)
{
    delete current->next;  // your deleting the element
                           // But not moving the the current item
                           // So the loop will not exit.
}

DeQueue::~DeQueue()
{
    // Delete from the head.
    // Just slowly loop over the nodes deleting them as you go
    while(head != tail)
    {
         QueueItem* old = head;
         head           = head->next;

         delete old;
    }
    // Delete the sentanal object created in the constructor.
    delete tail;
}

// Simplify empty.
bool DeQueue::Empty()
{
    return tail == head;
}


delegate click event on document I'm adding an onclick listener to the whole document then checking the source of the click. If the source of the click contains the attribute data-widget I want to go to the appropriate action. I did it but I'm hoping for a more efficient way of doing things and if I can do without the self invoking function.

<div data-widget="myaction"> Click here </div>

document.addEventListener("click", function(e) {
    var widgets = new Widgets();
    var w = e.target.getAttribute("data-widget");
    if(w){
        e.preventDefault();
        widgets[w];
    }
});

function Widgets(){
    var myaction = (function myaction(){
        console.log('indeed');
    })();
}

The way constructors work is that the engine creates an object, and calls the constructor passing the object as context. Essentially, the constructor is being run like a function.

console.log('indeed'); runs because you created an instance of Widget, which calls the constructor and runs your iife. The following code alone works in the same way.

document.addEventListener("click", function(e) {
  var widgets = new Widgets();
});

function Widgets(){
  console.log('indeed');
}

Now your code creates a Widgets instance regardless of the presence of data-widget. If you only deal business when it has data-widget, I suggest you create the instance within the if block.

I'm not sure what your goal with widgets[w] is, but if you mean storing w's value as a property of an instance of Widgets, I it's best if you define the object's properties in the constructor in advance. While this sacrifices dynamicity, it eliminates guesswork as to what properties an instance of Widgets contain.

Additionally, delegating all the way up to document is slow. That's one reason jQuery deprecated live and provided you on instead. live bound all handlers to document, while on gives you flexibility to where you want the delegate target bound.


Finding pairs of Amicable Numbers, up to 10000 I have this program to find pairs of amicable numbers in C:

#include<stdio.h>

int main()
{
    printf("|---------- PROGRAM FOR AMICABLE NUMBERS.----------|");
    int num1,num2,sum=0;
    for(num1=1; num1<=10000; num1++)
    {
        for(num2=1; num2<=10000; num2++)
            {
                if ((num1==sum_of_divisors(num2)) && (num2==sum_of_divisors(num1)) && num1!=num2)
                {
                    printf("\n%d\t\t%d", num1,num2);
                }
            }
    }
    return 0;
}

int sum_of_divisors(int n)
{
    int sum=0,i;
    for(i=1; i<n; i++)
    {
         if(n%i==0)
         {
              sum=sum+i;
         }
    }
    return sum;
}

I'd like to improve the performance - it's taking around 10 minutes to find even the first 4 pairs.

printf("|---------- PROGRAM FOR AMICABLE NUMBERS.----------|");

This kind of banner message makes it harder to use the output of your program in a pipeline.  I'd suggest removing it (the user has chosen to run it; trust them to know what they're doing!).

    int num1,num2,sum=0;

What's sum for?  It doesn't seem to be used.

    for(num1=1; num1<=10000; num1++)

Where does the magic value 10000 come from?  It should be a named constant (or, better, specifiable as a command-line argument).

        for(num2=1; num2<=10000; num2++)

Do we really need to consider every pair in both directions?  We could simply iterate while num2 < num1 (which also saves ourselves a comparison later).  But see below why this loop isn't needed at all.

                if ((num1==sum_of_divisors(num2))

There's no prototype visible for sum_of_divisors() - enable the relevant compiler warnings and provide the prototype (e.g. by moving the definition ahead of main()).

        for(num2=1; num2<=10000; num2++)
            {
                if ((num1==sum_of_divisors(num2))
                    && (num2==sum_of_divisors(num1))
                    && num1!=num2)

In the inner loop, sum_of_divisors(num1) is a constant, so we could save it in the outer loop, and make that the first comparison (so that short-circuit && will then evaluate sum_of_divisors(num2) just once).  Then observe that we have a loop which tests whether its index is equal to a particular value - that means that we replace the inner loop with a single test:
    for (num1 = 1;  num1 <= 10000;  num1++) {
         num2 = sum_of_divisors(num1);
         if (num1 < num2 && sum_of_divisors(num2) == num1) {
             /* we found a pair */
         }
    }

The num2 < num1 test there saves us re-finding pairs we've already seen.  num2 > num1 would also work, of course - I chose this version so that we print each pair lowest-first, as that seems to be the convention.

                    printf("\n%d\t\t%d", num1,num2);

It's better to consistently end your output with newline, rather than beginning with newline.  This works better in programs that might emit errors or other diagnostics, and plays nicely with line-buffered output such as interactive terminals (where the newline causes a flush).

    for(i=1; i<n; i++) if(n%i==0) sum += i;

This is a really inefficient way to accumulate factors.  You can reduce it to the range (1, √n) with sum += i + n/i (adjusted a little to avoid double-counting √n when n is square); it may be better instead to generate all the prime factors and use those to generate the composite factors.

After making the above improvements, I found the runtime was below my measurement threshold.  Increasing the limit to 1 million gives a runtime of about 2 seconds - that might be acceptable; if not, the problem parallelizes well (other than outputting the results).

Modified version
#include <stdio.h>

#if USE_LONG_TYPE
typedef unsigned long Number;
#define FMT "%lu"
#else
typedef unsigned int Number;
#define FMT "%u"
#endif

static const Number MAX_VALUE = 1000000;

Number sum_of_factors(Number n)
{
    Number sum = 1;
    Number i = 2;
    for (;  i < n/i;  ++i) {
        if (n/i*i == n) {
            sum += i + n/i;
        }
    }
    if (i*i == n)
        sum += i; /* add square root only once */
    return sum;
}

int main()
{
    //#pragma omp parallel for
    for (Number num1 = 1;  num1 <= MAX_VALUE;  ++num1) {
        Number num2 = sum_of_factors(num1);
        if (num2 > num1 && num1 == sum_of_factors(num2)) {
            printf(FMT "\t\t" FMT "\n", num1, num2);
        }
    }
}


Depth-first search algorithm in clojure Context

As an exercise for myself (I'm learning clojure). I wanted to implement the  Depth-first search algorithm.

How I did it

Using recursion

(def graph
  {:s {:a 3 :d 4}
   :a {:s 3 :d 5 :b 4}
   :b {:a 4 :e 5 :c 4}
   :c {:b 4}
   :d {:s 4 :a 5 :e 2}
   :e {:d 2 :b 5 :f 4}
   :f {:e 4 :g 1}})

(def stack [[:s]])

(def goal :g)

(defn cost [Graph start goal]
  (goal (start Graph)))

(defn hasloop? [path]
  (not (= (count path) (count (set path)))))

(defn atgoal? [path]
  (= goal (last path)))

(defn solved? [stack]
  (some true? (map atgoal? stack)))

(defn addtopath [path node]
    (conj path node))

(defn pop* [stack]
    (last stack))

(defn findpath [stack]
    (if (not (solved? stack))
        (let [first* (pop* stack) l (last first*) ]
                (findpath (drop-last
                    (remove hasloop?  (lazy-cat
                                            (map #(addtopath first* %)
                                            (keys (l graph))) stack)))))
        [(first stack)]))

How to use

(findpath stack)

Question

I'm really really interested in how this code can be improved. Both in readability, efficiency and performance.

I wrote the modified version of your findpath function using recursion:

(defn- dfs
  [graph goal]
  (fn search
    [path visited]
    (let [current (peek path)]
      (if (= goal current)
        [path]
        (->> current graph keys
             (remove visited)
             (mapcat #(search (conj path %) (conj visited %))))))))

(defn findpath
  "Returns a lazy sequence of all directed paths from start to goal
  within graph."
  [graph start goal]
  ((dfs graph goal) [start] #{start}))

Instead of using your hasloop? function,
my search function keeps track of visited nodes
in order to avoid visiting the same node twice.
It seems to work for your settings:

user> (def graph
  {:s {:a 3 :d 4}
   :a {:s 3 :d 5 :b 4}
   :b {:a 4 :e 5 :c 4}
   :c {:b 4}
   :d {:s 4 :a 5 :e 2}
   :e {:d 2 :b 5 :f 4}
   :f {:e 4 :g 1}})
user> (findpath graph :s :g)
([:s :a :b :e :f :g] [:s :a :d :e :f :g] [:s :d :a :b :e :f :g] [:s :d :e :f :g])


Calculating risk levels Let's consider following function that return some risk levels enum type:

func calculateRiskLevel()-> ResultFirstLevels
{
    let result = self.result
    if result < 1
        {
            return .Level1
        }
    else if result >= 1 && result < 5
        {
            return .Level2
        }
    else if result >= 5 && result < 10
        {

            return .Level3
        }
    else
        {
            return .Level4
        }

}

I'm pretty sure there is a more clever way to achieve that.

I was thinking about a dictionary with some key-value coding, but how can I incorporate conditional statements within keys?

Could you suggest some "more professional" way to deal with such parsing with conditional statements?

If you need risk level to be an enum (which sounds like good idea anyway), it would be nice to tightly couple the meaning of, say, Risk.Level2 with the range of raw values it represents, such as 1..<5. In other words, the enum itself should define all the applicable ranges. Depending on your specific needs, one of the following two approaches may be along the lines of what you're looking for:

enum Risk {

    case Level1, Level2, Level3, Level4

    var range: Range<Int> {
        switch self {
        case Level1: return 0..<1
        case Level2: return 1..<5
        case Level3: return 5..<10
        case Level4: return 10..<Int.max
        }
    }

    init(_ risk: Int) {
        switch risk {
        case Level2.range: self = Level2
        case Level3.range: self = Level3
        case Level4.range: self = Level4
        default: self = Level1
        }
    }
}

Risk(5) //--> Risk.Level3

Or:

enum Risk: Int {

    case Level1 = 0
    case Level2 = 1
    case Level3 = 5
    case Level4 = 10

    var range: Range<Int> {
        switch self {
        case Level1: return Level1.rawValue..<Level2.rawValue
        case Level2: return Level2.rawValue..<Level3.rawValue
        case Level3: return Level3.rawValue..<Level4.rawValue
        case Level4: return Level4.rawValue..<Int.max
        }
    }

    init(_ risk: Int) {
        switch risk {
        case Level2.range: self = Level2
        case Level3.range: self = Level3
        case Level4.range: self = Level4
        default: self = Level1
        }
    }
}

Risk(5) //--> Risk.Level3

This would all be much nicer if we could have enum Risk: Range<Int>. Unfortunately, Range<Int> cannot be expressed as a literal, so cases could not be assigned rawValues...


Subprocess CSV validator and multiprocessing on Python This Python script takes a directory of CSV files and calls a Scala script which tests whether a file's contents match a given regular expression. The link to this Scala script can be found in the 3rd line.

That script takes a single file as argument, hence my creation of this Python script to feed it an entire directory worth of files, while also utilizing the maximum possible amount of CPU power.

Do you see any issues or potential improvements in my code?

"""
Command line API to CSV validator using Scala implementation from:
http://digital-preservation.github.io/csv-validator/#toc7
"""

PATH_TO_VALIDATOR = r"C:\prog\csv\csv-validator-cmd-1.2-RC2\bin\validate.bat"
PATH_TO_CSV_FOLDER = r"C:\prog\csv\CSVFiles"
PATH_TO_CSV_SCHEMA = r"C:\prog\csv\ocr-schema.csvs"

# Set defaults
CSV_ENCODING = "windows-1252"
CSV_SCHEMA_ENCODING = "UTF-8"

def open_csv(CSV_LIST):
    import subprocess

    # To be used to display a simple progress indicator
    TOTAL_FILE_COUNT = len(CSV_LIST)
    current_file_count = 1

    with open("output.txt", 'w') as output:
        for filename in CSV_LIST:
            print("Processing file " + str(current_file_count) + "/" + str(TOTAL_FILE_COUNT))

            output.write(filename + ': ')
            validator = subprocess.Popen(
                [PATH_TO_VALIDATOR, PATH_TO_CSV_FOLDER + "/" + filename, PATH_TO_CSV_SCHEMA, "--csv-encoding",
                 CSV_ENCODING, "--csv-schema-encoding", CSV_SCHEMA_ENCODING, '--fail-fast', 'true'], stdout=subprocess.PIPE)
            result = validator.stdout.read()
            output.write(result.decode('windows-1252'))

            current_file_count += 1

# Split a list into n sublists of roughly equal size
def split_list(alist, wanted_parts=1):
    length = len(alist)
    return [alist[i * length // wanted_parts: (i + 1) * length // wanted_parts]
            for i in range(wanted_parts)]

if __name__ == '__main__':
    import argparse
    import multiprocessing
    import os

    parser = argparse.ArgumentParser(description="Command line API to Scala CSV validator")
    parser.add_argument('-pv', '--PATH_TO_VALIDATOR', help="Specify the path to csv-validator-cmd/bin/validator.bat",
                        required=True)
    parser.add_argument('-pf', '--PATH_TO_CSV_FOLDER', help="Specify the path to the folder containing the csv files "
                                                            "you want to validate", required=True)
    parser.add_argument('-ps', '--PATH_TO_CSV_SCHEMA', help="Specify the path to CSV schema you want to use to "
                                                            "validate the given files", required=True)

    parser.add_argument('-cenc', '--CSV_ENCODING', help="Optional parameter to specify the encoding used by the CSV "
                                                        "files. Choose UTF-8 or windows-1252. Default windows-1252")
    parser.add_argument('-csenc', '--CSV_SCHEMA_ENCODING', help="Optional parameter to specify the encoding used by "
                                                                "the CSV Schema. Choose UTF-8 or windows-1252. "
                                                                "Default UTF-8")

    args = vars(parser.parse_args())

    if args['CSV_ENCODING'] is not None:
        CSV_ENCODING = args['CSV_ENCODING']

    if args['CSV_SCHEMA_ENCODING'] is not None:
        CSV_SCHEMA_ENCODING = args['CSV_SCHEMA_ENCODING']

    PATH_TO_VALIDATOR = args["PATH_TO_VALIDATOR"]
    PATH_TO_CSV_SCHEMA = args["PATH_TO_CSV_SCHEMA"]
    PATH_TO_CSV_FOLDER = args["PATH_TO_CSV_FOLDER"]

    CPU_COUNT = multiprocessing.cpu_count()

    split_csv_directory = split_list(os.listdir(args["PATH_TO_CSV_FOLDER"]), wanted_parts=CPU_COUNT)

    # Spawn a Process for each CPU on the system
    for csv_list in split_csv_directory:
        p = multiprocessing.Process(target=open_csv, args=(csv_list,))
        p.start()

Only a few small things I would suggest changing.
Pep8
You should consider formatting your code in accordance with pep8.  This is important when sharing code, as the consistent style makes it much easier for other programmers to read your code.  There are various tools available to assist in making the code pep8 compliant.  I use the PyCharm IDE which will show pep8 violations right in the editor.
ALL_CAPS_IS_FOR_CONSTS
So generally ALL_CAPS is reserved for constant variables. So this sort of thing:
TOTAL_FILE_COUNT = len(CSV_LIST)

is more Pythonic as:
total_file_count = len(CSV_LIST)

And even better is getting rid of this intermediate assignment altogether with:
print("Processing file {}/{}".format(current_file_count, len(CSV_LIST)))

Testing for presence in dicts
Often when you find code checking for a key presence in a dict before performing some work:
if args['CSV_ENCODING'] is not None:
    CSV_ENCODING = args['CSV_ENCODING']

Basically the same thing can be done without the explicit check:
CSV_ENCODING = args.get('CSV_ENCODING', CSV_ENCODING)


Best way to get the best performance of a large CSV upload in PHP I am trying to upload a CSV file to a database which has around 800,000 rows. I am able to do this on my server with the following specs:

RAM: 4GB
Rows: 800,000
Time taken: 305 seconds
Usage: 15% RAM
Database: Postgres (Amazon RDS)

My upload code looks like this:

   public function handle()
   {
      set_time_limit (0);
      // try {

            $time_start = microtime(true);
            $counter=0;
            $data = array();
            $file = flag_tables::where('imported','=','0')
                     ->orderBy('created_at', 'DESC')
                     ->first();
              $file_path = Config::get('filesystems.disks.local.root') . '/exceluploads/' .$file->file_name;

              $header = null;
              $totalRows=0;
              $in = fopen($file_path,"r");
              if($in){
                  while(!feof($in)){
                        $content = fgets($in);
                    if($content)    $totalRows++;
                  }
              }

              $totalRows = --$totalRows;
              $file->total_rows = $totalRows;
              $file->save();
              fclose($in);
              $in = fopen($file_path,"r");
              $c=0;

              while ($row = fgetcsv($in)){
                  if ($header === null) { $header = $row;continue;}
                  $data[] = array('data'=> json_encode(array_combine($header, $row)),'created_at'=>date('Y-m-d H:i:s'),'updated_at'=> date('Y-m-d H:i:s'));
                  $counter++;

                   if(count($data) == 1400)
                   {
                      DB::table('price_results')->insert($data);
                      $file = $file->fresh(); //reload from the database
                      $file->rows_imported = (int) $file->rows_imported + $counter;
                      $counter= 0;
                      $file->save();
                      $data = array();
                    }
                  }

              $time_end = microtime(true);
              $time = $time_end - $time_start;
              DB::table('price_results')->insert($data);
              $file = $file->fresh(); //reload from the database
              $file->rows_imported = (int) $file->rows_imported + $counter;
              $file->imported =1;
              $file->total_execution_time=$time;
              $file->save();
              fclose($in);
      // }
      //   catch(\Exception $e)
      //   {
      //     dd($e->getMessage());
      //   }
   }

I would like to make this upload time better, if possible.

I would say that when you are dealing with bulk load operations, you might want to forego usage of your framework's ORM (data model) or query builder functionalities, in favor of more straightforward raw query or even command line functionality in order to leverage the DB's bulk load capabilities.

In this case, if your file and DB are on same server you might consider Postgres COPY query to bulk load the CSV.

For example:

COPY {table_name} (column1, column2, ...) FROM '$file_name' DELIMITER ',' CSV

Here you specify the table columns that match your columns in the CSV (in order given in CSV).  If you have timestamps properly defined on your table, you eliminate the need for you to "enrich" your inserted data with timestamps like you are currently doing (i.e. they could behave much like an autoincrement field upon row insert). In most cases, you should probably let the database be the authoritative source for generating timestamps anyway (unless you are trying to capture some client-specific timestamp value).

If your application and DB are on separate servers, you might consider forming a command-line statement using \copy to satisfy your requirement. This is pretty much the same syntax-wise as using COPY. This is the case that would be applicable to you since you are using RDS.

I think this would allow you to greatly simplify your code here such that you really only need to do your update check, your file system validation, then the bulk load command.

You would move away from the concept of interactively loading and refreshing data between the client and database.

You probably should also read through AWS documentation on bulk-loading into RDS for other considerations.

The bottom line here is that when you start thinking about bulk loading, you should probably understand that there are specific tools for this sort of work that go beyond the capabilities of your framework/ORM.

Other code review thoughts:

Should you have a more meaningful name for this method?  handle() seems kind of generic.
You should consider refactoring this method, as there is a lot of code complexity within this single method.  Perhaps individual (protected/private) methods for:

checking to see table(s) needing upload
file path generation and file system validation as readable
changing PHP ini settings at runtime - i.e. set_time_limit() - and resetting after execution which you fail to do here
actually executing bulk load.

You have some problems with style consistency. You should consider settling on a style and sticking to it (prefereably PSR-1/PSR-2 if you want to stick to best existing PHP-wide standards). Examples:

inconsistent indents
inconsistent spacing around and placement of flow control structures
mixing of camelCase and snake_case in your user-defined variables
inconsistent spacing around assignment operators

You are using exact and loose comparisons inconsistently.  I didn;t mention this as style problem, as this is more significant to code quality than those other items.  I would suggest getting in the habit of using strict comparisons as default and using loose comparisons only where you have specific reason to do so.  Codebases that are littered with loose comparisons are much more likely to have bugs in them due to unexpected truthy/falsey behaviors.


Volatile boolean to prevent deadlock I wrote this code to overcome the deadlock condition described in this Oracle tutorial.

Summary of Problem:

  Alphonse and Gary are friends, and great believers in courtesy. A strict rule of courtesy is that when you bow to a friend, you must remain bowed until your friend has a chance to return the bow. Unfortunately, this rule does not account for the possibility that two friends might bow to each other at the same time.

I used a volatile boolean CanBow() to check if either of the friends are bowing so that the condition when both of them bow and wait for the other to bow back will not happen.

Is this a valid way to handle deadlocks as opposed to using locks?

public class NewThread extends Thread {
    Friend bower;
    Friend bowBack;
    NewThread(Friend bower,Friend bowBack){
       this.bower = bower;
       this.bowBack = bowBack;
    }
    @Override
    public void run(){
       if(bowBack.canBow())
             bower.bow(bowBack);
    }
}

public class ApplicationThread {
    public static void main(String[] args) {
        final Friend alphonse = new Friend("Alphonse");
        final Friend gary = new Friend("Gary");
        NewThread t1 = new NewThread(alphonse,gary);
        NewThread t2 = new NewThread(gary,alphonse);
        t1.start();
        t2.start();
    }
    static class Friend {
        private final String name;
        private volatile boolean canBow;
        public Friend(String name) {
            this.name = name;
            canBow = true;
        }
        public String getName() {
            return this.name;
        }
        public boolean canBow(){
            return canBow;
        }
        public synchronized void bow(Friend bower) {
                canBow = false;
                System.out.format("%s: %s"
                    + "  has bowed to me!%n",
                    this.name, bower.getName());
                bower.bowBack(this);
                canBow = true;
        }
        public synchronized void bowBack(Friend bower) {
            System.out.format("%s: %s"
                + " has bowed back to me!%n",
                this.name, bower.getName());
        }
    }
}

Welcome to the wonderful world of deadlocks. In a nutshell: No, your “solution” is not adequate. Here is some pseudocode detailing what the two threads are doing. Note that a synchronized method aquires a lock on the object it is called on.

THREAD 1                             |     THREAD 2
IF:                                  |     IF:
synchronized (gary) {                |     synchronized (alphonse) {
  // gary.canBow();                  |       // alphonse.canBow();
  gary.canBow;                       |       alphonse.canBow;
}                                    |     }
THEN:                                |     THEN:
synchronized (alphonse) {            |     synchronized (gary) {
  // alphonse.bow(gary);             |       // gary.bow(alphonse);
  alphonse.canBow = false;           |       gary.canBow = false;
  ...                                |       ...
  // gary.bowBack(alphonse)          |       // alphonse.bowBack(gary)
  synchronized (gary) {              |       synchronized (alphonse) {
    ...                              |         ...
  }                                  |       }
  alphonse.canBow = true;            |       gary.canBow = true;
}                                    |     }

We can see that gary and alphonse can be locked at the same time. Especially, the bowBack.canBow() can return true in both threads. Then, even when the canBow boolean is volatile, both objects can wait for each other in a deadlock (in Thread 1, alphonse is locked and waits for gary. In Thread 2, the other way round).

You can make this deadlock more likely by increasing the time between the bowBack.canBow() and bower.bow() calls:

public void run(){
   if(bowBack.canBow()) {
       Thread.sleep(100);  // exception handling omitted
       bower.bow(bowBack);
   }
}

To solve these issues, you could impose an order or hierarchy on the friends, so that locks are aquired in exactly the same order in all threads (e.g. always synchronize on alphonse before gary). You could also decide that only one friend may initiate a bow at any time, e.g. via a semaphore.

But your implementation doesn't work.


Python palindrome checker This is my palindrome-checker that is doing what it should do. If you have any additional suggestions, please let me know.

# -*- coding: utf-8 -*-

def palindrome_verif():
    Mot = raw_input("Tapez votre mot \n")

    Comptage = len(Mot)
    Lett_Sep = list(Mot)
    Decompte = 0
    Der_Lett = Comptage

    if ((Comptage % 2) == 1):
        ImmPair = (Comptage / 2)+1
        #print(str(Decompte)," et ",str(Der_Lett))
        #print(str(Decompte)," et ",str(ImmPair))
        while Decompte < ImmPair:
            print(str(Decompte)," et ",str(Der_Lett))
            if Lett_Sep[Decompte] == Lett_Sep[Der_Lett-1]:
                print("Même lettre")
                Decompte += 1
                Der_Lett -= 1
            else:
                print("Ceci n'est pas un palindrome")
                return 0000
                #break
    elif ((Comptage % 2) == 0):
        Pair = Comptage / 2
        while Decompte < Pair:
            print(str(Decompte)," et ",str(Der_Lett))
            if Lett_Sep[Decompte] == Lett_Sep[Der_Lett-1]:
                print("Même lettre")
                Decompte += 1
                Der_Lett -= 1
            else:
                print("Ceci n'est pas un palindrome")
                return 0000
                #break

Writing functions

A palindrome-checking function should accept a string parameter and return a result.  Your function takes its input from the keyboard (or stdin), which makes it not reusable.  If you want to accept keyboard input, it should be done outside the function.

Even more unusual is the fact that you return None if it's a palindrome, and return 0000 if it's not.  Why so many zeroes?  Why are both return values falsy?  The convention would be to return True or False.

Implementation

You're doing a lot of unnecessary things.

You can index and iterate over strings directly.  There is no need to convert it into a list.

There is no need to handle odd and even lengths as separate cases.

Der_Lett (a cryptic abbreviation for "dernière lettre"?) actually isn't the index to the last letter — it's off by one.  (Your code is correct, but weird.)

Style

PEP 8 naming conventions apply to your variable names, même en français.  That is, they should be lowercase.

Counting loops are nearly always better done using of range(), xrange(), enumerate(), or something from itertools.

Suggested solutions

Here is a simple loop that does the job.

def verifier_palindrome(mot):
    centre = len(mot) // 2
    for decompte in range(centre + 1):
        if mot[decompte] != mot[-decompte - 1]:
            return False
    return True

Here is a more compact way to write it, using a generator expression:

def verifier_palindrome(mot):
    return all(mot[i] == mot[-i - 1] for i in range(len(mot) // 2 + 1))


Uni-directional singly linked list in JavaScript I've been studying data structures and have taken a stab at implementing a singly LL in JavaScript. Please review and advise on any additional improvements and optimizations:

function Node(val){
  this.data = val;
  this.next = null;
};

function SinglyList(){
  this._length = 0;
  this.head = null;
};

//Add
SinglyList.prototype.add = function(val){
  var node = new Node(val),
      currentNode = this.head;

  //if list is empty
  if(!currentNode){
    this.head = node;
    this._length++;
    return;
  }

  //locate tail
  while(currentNode.next){
    currentNode = currentNode.next;
  }
  //Add to the tail
  currentNode.next = node;
  this._length++;

  return node;
}
//isEmpty
SinglyList.prototype.isEmpty = function(){
  return this._length === 0;
}

//Contains
SinglyList.prototype.contains = function(val){
  var currentNode = this.head;

  if(this.isEmpty()) return;

  while(currentNode){
    if(currentNode.data === val) return true;
    currentNode = currentNode.next;
  }

  return false;
}
//Reversed
SinglyList.prototype.reverse = function(){
  var currentNode = this.head,
      prev = null;

  while(currentNode){
    var temp = currentNode.next;
    currentNode.next = prev;
    prev = currentNode;
    currentNode = temp;
  }
  this.head = prev;
  return this;
}
//RemoveAt
SinglyList.prototype.removeAt = function(index){

  if(index<0 || index > this._length)
    return;

  var currentNode = this.head, i=0, prev;

  //if 1st in the list
  if(index === 0){
    this.head = currentNode.next;
    this._length--;
    return this;
  }
  //Subsequent items in the list
  while(i++ < index-1){
    prev = currentNode;
    currentNode = currentNode.next;
  }
  prev.next = currentNode.next;
  this.head = prev;
  this._length--;

  return this;
}
//Remove by value
SinglyList.prototype.removeByVal = function(val){
  if(this.isEmpty()) return;
  if(!this.contains(val)) return;

  var currentNode = this.head;

  if(currentNode.data===val){
    this.head = currentNode.next;
    this._length--;
    return this;
  }

  while(currentNode.next){
    if(currentNode.next.data===val){
      prev = currentNode;
    }
    currentNode = currentNode.next;
  }

  prev.next = prev.next.next;
  this._length--;

  return this;

}
//Append
SinglyList.prototype.append = function(val){
  this.add(val);
  return this;
}
//Prepend
SinglyList.prototype.prepend = function(val){
  var node = new Node(val),
      temp = this.head;

  node.next = temp;
  this.head = node;
  return this;
}

var singlyList = new SinglyList();

singlyList.add(5);
singlyList.add(10);
singlyList.add(15);
singlyList.add(20);
singlyList.add(25);
singlyList.add(30);
singlyList.add(35);
singlyList.add(40);

console.log('Singly LL:\n'+JSON.stringify(singlyList));
console.log('isEmpty method:\n'+singlyList.isEmpty());
console.log('Contains:\n'+singlyList.contains(5));

console.log('Reversed:\n'+JSON.stringify(singlyList.reverse()));
console.log('Remove:\n'+JSON.stringify(singlyList.removeAt(2)));
console.log('Reversed:\n'+JSON.stringify(singlyList.reverse()));

console.log('Append:\n'+JSON.stringify(singlyList.append(45)));
console.log('Prepend:\n'+JSON.stringify(singlyList.prepend(50)));
console.log('Remove by value:\n'+JSON.stringify(singlyList.removeByVal(50)));

Just to be sure, this will always be slower than a plain Array() in JavaScript, no matter the optimizations we might find.

After perusing your code I came to:

I like your old school prototyping approach with prototype
You should have a function to retrieve the last element
I would call the root tail or root, as the last element tends to be the head
I would not declare null values in your constructors, it makes no difference:

function Node(val){
  this.data = val;
};

function SinglyList(){
  this._length = 0;
};

You almost never use (read) ._length, I would not keep track of it and just have a function that calculates the length, your code would be so much neater
I would let removeByVal() find the index that needs to be removed and then call removeAt()
I like reverse(), I had to scratch my head at first, it's quite clever for me
JS people would expect unshift instead of prepend
JS people would expect push instead of add
I like that you provided a chainable version of push


Convert a string like 4h53m12s to a total number of seconds in JavaScript At the moment I have this:

function getValue(str)
{
 let result = 0;

 var regex = /(\d+[a-z]+)/g;
 match = regex.exec(str);

 while (match != null)
 {
  var match_str  = match[0];
  var last_char  = match_str[match_str.length-1];

  if ( last_char == 'h' )
   result += parseInt(match_str) * 3600;
  if ( last_char == 'm' )
   result += parseInt(match_str) * 60;
  if ( last_char == 's' )
   result += parseInt(match_str);

  match = regex.exec(str);
 }

 return result;
}

console.log( getValue("4h12m32s") );

It feels quite clumsy though. Also checking invalid values like 4hs feels difficult.

Is there any clever trick for something similar?

getValue(str) is such a vague name for the function and its parameter, it could mean anything!  Furthermore, "get" implies that this is a getter function that retrieves something, which is not the case.
Your regex is ineffective.  Capturing parentheses could be useful, but you didn't actually use them right, such that you ended up having to pass a dirty string to parseInt() and extract the last character the harder way.
You neglected to scope match, such that it acts as a global variable.  The regex-matching statement is written twice; the assignment could be done within the loop condition instead.
The if statements should be an if-else chain, since the conditions are mutually exclusive.  However, since the branches are all so similar, a lookup table would be more elegant.

function durationSeconds(timeExpr)
{
 var units = {'h': 3600, 'm': 60, 's': 1};
 var regex = /(\d+)([hms])/g;

 let seconds = 0;
 var match;
 while ((match = regex.exec(timeExpr)))
 {
  seconds += parseInt(match[1]) * units[match[2]];
 }

 return seconds;
}

console.log( durationSeconds("4h12m32s") );

Alternatively, if you expect that the units will be in the conventional order, you don't have to loop at all.

function durationSeconds(timeExpr)
{
 var match = /^(?:(\d+)h)?(?:(\d+)m)?(?:(\d+)s)?$/.exec(timeExpr);
 return 3600 * (parseInt(match[1]) || 0)
        + 60 * (parseInt(match[2]) || 0)
        +      (parseInt(match[3]) || 0);
}

console.log( durationSeconds("4h32s") );


Progress report for a long-running process using 'yield' I have a function that processes a large amount of data. I call this function as part of a wider process via a command line script in which many similar but shorter jobs are conducted in sequence.

I have something like this:

import time

def initialise_foo():
    """do something quickly"""
    time.sleep(0.1)

def initialise_bar():
    """do something else quickly"""
    time.sleep(0.1)

def load_data(n_items):
    """do another thing quickly"""
    return range(n_items)

def process_datum(datum, sleep):
    """"do something with datum that takes a while"""
    time.sleep(sleep)

def cleanup():
    """do a final thing quickly"""
    time.sleep(0.1)

My problem is that I want my script to provide feedback on progress and I was considering converting my data processor into an iterator and using yield to emit progress information.

I can do this but there is a long wait with no feedback...

def process_data1(data, sleep):
    """loop over a lot of data"""
    for item in data:
        process_datum(item, sleep)

def main1(n_items, sleep):
    print("initialising foo")
    initialise_foo()
    print("initialising bar")
    initialise_bar()
    print("loading data")
    mydata = load_data(n_items)
    print("processing data")
    process_data1(mydata, sleep)
    print("cleaning up")
    cleanup()
    print("done")

So what about this?

def process_data2(data, sleep, report_frequency):
    """loop over a lot of data"""
    for i, item in enumerate(data):
        process_datum(item, sleep)
        if not (i % report_frequency):
            yield i

def main2(n_items, sleep, report_frequency):
    print("initialising foo")
    initialise_foo()
    print("initialising bar")
    initialise_bar()
    print("loading data")
    mydata = load_data(n_items)
    print("processing data")
    for i in process_data2(mydata, sleep, report_frequency):
        print("%i items processed" % i)
    print("cleaning up")
    cleanup()
    print("done")

run it like this.

if __name__ == "__main__":
    print ("\nwithout progress")
    main1(100, 0.01)
    print ("\nwith progress")
    main2(100, 0.01, 10)

Does this look like a reasonable approach? Any comments on how I have coded this?

I see three basic options you have, to structure progress feedback:

yield
a callback function
inline, hard-coded feedback

Using yield, as you've shown, is one approach.  Ensure you are familiar with Python's implementation of yield and iterables and generators, since several languages share the yield keyword superficially but have subtle implementation differences that might surprise you if, for example, you're used to C#'s yield. Here's a reference on that -- it's subtle but worth reading. Essentially, in Python when your function yields, it returns a generator which can be usefully assigned to a variable, capturing iteration to that point, but you may or may not want to do that.

So there is fundamentally nothing wrong with using yield for this job.  It carries the big advantage of separating pure computing from progress IO.

A decent alternative to yield which is easy to understand is a callback function.  If you passed a callable as a final parameter to your process_data function then a block of code can be executed, instead of a yield or hardcoded inline feedback, also separating processing from feedback.

One advantage a callback approach carries is it allows communication back to the process_data function which could be used to pause or halt processing.  For example, if your callback function is aware of the console or UI, then while updating progress it can also monitor buttons or keys and return a value to the process_data function.  This could indicate, for example, that the user wants to abort processing and this doesn't pollute the process_data function with awareness of specific UI or IO.  So a callback would also work well and allow you to pass a lambda block or full function in Python.  (Languages without a yield facility may have no choice but to use callbacks.)

Example code:

def process_data(data, sleep, report_frequency, report_callback=None):
    for ...
        ...
        if not (i % report_frequency):  # or whatever criteria
            if report_callback:
                if report_callback(i) == USER_ABORT:  # or other codes, by convention
                  return

Please let me also address what I see as a big, very practical, issue with the code defining report_frequency in terms of data elements or loop iterations.

I have see two problems with this pattern of progress tracking:

It's difficult to know in advance what a good value of report_frequency is, when it's expressed in loop iterations.  Trial and error can quickly get you into the ballpark, but that leaves problem #2.
Depending on the nature of your computation, the process_datum function may not run in constant time.  For example, it may take longer as you get deeper into the dataset, if numbers are getting larger and depending on what you're calculating.  What I'm getting at is there may not be a constant number of loop iterations per second.

So I'll propose a better way for you to think of report_frequency. Use seconds, not iterations or data elements.  Time is likely what you had in mind, anyways. It's natural to want to see an update every X seconds, regardless of how many loop iterations that is.  This eliminates both problems and is especially useful when your process_datum function does not run in constant time.

Your code:

def process_data2(data, sleep, report_frequency):
    """loop over a lot of data"""
    for i, item in enumerate(data):
        process_datum(item, sleep)
        if not (i % report_frequency):
            yield i

Can become this:  (I'm putting the yield vs inline vs callback issue aside for the moment, to illustrate the concept)

def process_data3(data, sleep, report_frequency):
    """loop over a lot of data"""
    t_updlast = datetime.now()
    t_update = t_updlast + timedelta(seconds=report_frequency)
    for i, item in enumerate(data):
        if datetime.now() >= t_update:
            print("%6.2f%%\n" % (i*100.0/len(data)))
            t_update = datetime.now() + timedelta(seconds=report_frequency)
        process_datum(item, sleep)

Now you'll see a report every report_frequency seconds that looks like:

56.12%
56.83%
...

And this simplifies how you call process_data3 from main. By dropping the yield, you simply call the following from main, without wrapping it in a for loop:

process_data3(mydata, sleep, report_frequency)

And now that you're in the domain of measuring time, if your process_datum function runs in constant time, you can add a fairly simple calculation for "estimated time to completion".  I'll leave that as an exercise for you. :-)

Remember to add this at the top of your module:

from datetime import datetime, timedelta

Sorry for the long answer and timedelta diversion. I hope this is helpful. Good luck!


Caching image in memory or on disk I am caching an image locally..That same image will be used in few screens. Steps for doing this is as follows:

Get image from cache
If not present in NSCache, get image from Cache directory
If not present in Cache Directory, download from Internet
Save downloaded image to dir
Insert the image in cache

//Helper class is as

     class ImageCacheHelper:NSObject{

        static var cache = NSCache()
        static var isNotRunningDispatch:Bool = true

      class func setObjectForKey(imageData:NSData,imageKey:String){

            ImageCacheHelper.cache.setObject(imageData, forKey: imageKey)

        }

      class func getObjectForKey(imageKey:String)->NSData?{

            return ImageCacheHelper.cache.objectForKey(imageKey) as? NSData

        }

      class func getImage(imageUrl:String,completionHandler:(NSData)->()){
            if ImageCacheHelper.isNotRunningDispatch{

                ImageCacheHelper.isNotRunningDispatch = false
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), {
                    let imgUrl = NSURL(string:imageUrl)
                    let imageData = NSData(contentsOfURL: imgUrl!)!
                    ImageCacheHelper.setObjectForKey(imageData, imageKey: "\(imageUrl.hashValue)")
                    ImageCacheHelper.isNotRunningDispatch = true
                    completionHandler(imageData)

                })
            }else{
               print("alerady started loading image")
           }
        }
      }

//How i call this from ViewController class is as

let userImageUrl = "\(appBaseUrl)\(imageUrlString)"
let fileManager = NSFileManager.defaultManager()
let diskPaths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true)
let cacheDirectory = diskPaths[0] as NSString
let diskPath = cacheDirectory.stringByAppendingPathComponent("\(userImageUrl.hashValue)")
if let data = ImageCacheHelper.getObjectForKey("\(userImageUrl.hashValue)"){
          let userImage =  UIImage(data: data)
          self.userImgView.image = userImage

}else if fileManager.fileExistsAtPath(diskPath){

          let image =  UIImage(contentsOfFile: diskPath)
          self.userImgView.image = image
          ImageCacheHelper.setObjectForKey(UIImageJPEGRepresentation(image, 1.0)!, imageKey: "\(userImageUrl.hashValue)")

}else   {
          ImageCacheHelper.getImage(userImageUrl){ imageData in

            imageData.writeToFile(diskPath, atomically: true)
                    dispatch_async(dispatch_get_main_queue()){

                        let userImage =  UIImage(data: imageData)
                        self.userImgView.image = userImage

                    }
        }
}

Flaws That I see in my code are as follows:

Creating a static variable for NSCache.
Would be great if settingObject in NSCache, getting would be as of NSUserDefaults.

How to optimize this?

Here is a snippet of project that i have tried to do
https://drive.google.com/file/d/0B6dTvD1JbkgBS1k3Ry1xNmZ1VHc/view?usp=sharing

Modify Caching Strategy

It seems every image that is subject to caching is ending up both in memory and on disk. Depending on the number of images and caching aggressiveness, this could be more expensive (in terms of resource usage) than the network request used to retrieve the image.

From my perspective, using both NSCache and the Cache directory together doesn’t add any value. They are both volatile and unpredictable (to different extents). The system could reclaim space from either when it deems it necessary.

From Apples File System Programming Guide:

  "Note that the system may delete the Caches/ directory to free up disk space, so your app must be able to re-create or download these files as needed.”

Therefore its either a choice between caching in memory and caching on disk or an emphasis of one over the other. This inevitably leads to additional questions about how your app will be used and the what kind of images you are working with.

Some basic guidelines:

You don’t want to store a large number of full resolution images in memory
You don’t want to have too many file system I/O operations (spread over time)
You don’t want to have images in memory that are not going to be seen by the user
You don’t want to go to disk every time for frequently seen images

What is a large number? How much is too many? You will have to test and measure.

Test & Measure (and then measure again)

I would definitely recommend testing and measuring your caching strategy to see how much of an impact it has. Xcode instruments provides tools to accomplish this.

One way to go about this is:

Test with no cache (this will be your baseline)
Test with a simple caching strategy (maybe just using NSCache or the Cache directory)
Compare the results of step 2 to your baseline obtained from step 1
Not satisfied, then apply a different caching strategy. Repeat step 3. Otherwise, if you are satisfied with the resource usage and responsiveness of the app then you can stop here until there are significant changes to either your codebase, data or the iOS frameworks.


Given a sorted dictionary of an alien language, find order of characters Given a sorted dictionary (array of words) of an alien language, find
  order of characters in the language.

Link:
The idea is to create a graph of characters and then find topological sorting of the created graph. Following are the detailed steps.

1) Create a graph g with number of vertices equal to the size of alphabet in the given alien language. For example, if the alphabet size is 5, then there can be 5 characters in words. Initially there are no edges in graph.

2) Do following for every pair of adjacent words in given sorted array.

a) Let the current pair of words be word1 and word2. One by one compare
   characters of both words and find the first mismatching characters.

b) Create an edge in g from mismatching character of word1 to that of
   word2.

3) Print topological sorting of the above created graph.

import collections

def topological_sort(nodes_map, nodes):
    def recursion(nodes_map, node, result, visited):
        if node in visited:
            return
        visited.append(node)
        if node in nodes_map:
            for c in nodes_map[node]:
                recursion(nodes_map, c, result, visited)
        result.append(node)

    visited, result = [], []
    for node in nodes.keys()[::-1]:
        recursion(nodes_map, node, result, visited)
    return reversed(result)

words = ["baa", "abcd", "abca", "cab", "cad"]
chars = collections.OrderedDict()
for word in words:
    for c in word:
        chars[c] = True

nodes_map = collections.defaultdict(list)
for i, word in enumerate(words[:-1]):
    nxt = words[i+1]
    for a, b in zip(word, nxt):
        if a != b:
            nodes_map[a] += [b]
            break
for i in topological_sort(nodes_map, chars):
    print i,

words = ["baa", "abcd", "abca", "cab", "cad"]

I don't believe you ever mutate words so you should make it constant by making it a tuple:

words = ("baa", "abcd", "abca", "cab", "cad")

This:

words = ["baa", "abcd", "abca", "cab", "cad"]
chars = collections.OrderedDict()
for word in words:
    for c in word:
        chars[c] = True

    nodes_map = collections.defaultdict(list)
    for i, word in enumerate(words[:-1]):
        nxt = words[i+1]
        for a, b in zip(word, nxt):
            if a != b:
                nodes_map[a] += [b]
                break
    for i in topological_sort(nodes_map, chars):
        print i,

Should probably wrapped into a "main function". Furthermore, to keep stuff in the "main scope" I highly recomend you actually make a main function. So instead of just:

if __name__ == '__main__':
    words = ("baa", "abcd", "abca", "cab", "cad")
    chars = collections.OrderedDict()
    ...

You should do:

def main():
    words = ("baa", "abcd", "abca", "cab", "cad")
    nodes_map = collections.defaultdict(list)
    for i, word in enumerate(words[:-1]):
        nxt = words[i+1]
        for a, b in zip(word, nxt):
            if a != b:
                nodes_map[a] += [b]
                break
    for i in topological_sort(nodes_map, chars):
         print i,

if __name__ == '__main__':
    main()

Instead of using a list to store results change it to a dequeue by making it results = collection.dequeue(). This gives you the appendleft method which adds stuff to the beginning of the "list". This means you don't need to return reversed(results), just return results.


Least common ancestor in binary tree This code finds a common ancestor. If one of the input does not exist in the tree then throws an exception. This does not use extra storage. It does not even traverse more than what it should be. It would also account for duplicate node values in binary tree.

I want you to pick my code apart and give me some feedback on how I could make it better or more simple.

public class LeastCommonAncestor {

    private TreeNode root;

    private static class TreeNode {
        TreeNode left;
        TreeNode right;
        int item;

        TreeNode (TreeNode left, TreeNode right, int item) {
            this.left = left;
            this.right = right;
            this.item = item;
        }
    }

    public void createBinaryTree (Integer[] arr) {
        if (arr == null)  {
            throw new NullPointerException("The input array is null.");
        }

        root = new TreeNode(null, null, arr[0]);

        final Queue<TreeNode> queue = new LinkedList<TreeNode>();
        queue.add(root);

        final int half = arr.length / 2;

        for (int i = 0; i < half; i++) {
            if (arr[i] != null) {
                final TreeNode current = queue.poll();
                final int left = 2 * i + 1;
                final int right = 2 * i + 2;

                if (arr[left] != null) {
                    current.left = new TreeNode(null, null, arr[left]);
                    queue.add(current.left);
                }
                if (right < arr.length && arr[right] != null) {
                    current.right = new TreeNode(null, null, arr[right]);
                    queue.add(current.right);
                }
            }
        }
    }

    private static class LCAData {
        TreeNode lca;
        int count;

        public LCAData(TreeNode parent, int count) {
            this.lca = parent;
            this.count = count;
        }
    }

    public int leastCommonAncestor(int n1, int n2) {
        if (root == null) {
            throw new NoSuchElementException("The tree is empty.");
        }

        LCAData lcaData = new LCAData(null, 0);

        foundMatchAndDuplicate (root, lcaData, n1,  n2, new HashSet<Integer>());

        if (lcaData.lca != null) {
            return lcaData.lca.item;
        } else {

            throw new IllegalArgumentException("The tree does not contain either one or more of input data. ");
        }
    }

    private boolean foundMatchAndDuplicate (TreeNode node, LCAData lcaData, int n1, int n2, Set<Integer> set) {
        if (node == null) {
            return false;
        }

        // when both were found
        if (lcaData.count == 2) {
            return false;
        }

        // when only one of them is found
        if ((node.item == n1 || node.item == n2) && lcaData.count == 1) {
            if (!set.contains(node.item)) {
                lcaData.count++;
                return true;
            }
        }

        boolean foundInCurrent = false;
        // when nothing was found (count == 0), or a duplicate was found (count == 1)
        if (node.item == n1 || node.item == n2) {
            if (!set.contains(node.item)) {
                set.add(node.item);
                lcaData.count++;
            }
            foundInCurrent = true;
        }

        boolean foundInLeft = foundMatchAndDuplicate(node.left, lcaData, n1, n2, set);
        boolean foundInRight = foundMatchAndDuplicate(node.right, lcaData, n1, n2, set);

        if (((foundInLeft && foundInRight) ||
                (foundInCurrent && foundInRight) ||
                    (foundInCurrent && foundInLeft)) &&
                        lcaData.lca == null) {
            lcaData.lca = node;
            return true;
        }
        return foundInCurrent || (foundInLeft || foundInRight);
    }

}

There are a number of issues in your code that I can see:

Input validation.

public void createBinaryTree (Integer[] arr) {
    if (arr == null)  {
        throw new NullPointerException("The input array is null.");
    }

    root = new TreeNode(null, null, arr[0]);

You check the arr is not null, but you do not check that its arr.length > 0. An empty array will cause an ArrayIndexOutOfBounds exception.
AutoBoxing

You have the method:

public void createBinaryTree (Integer[] arr) { ... }

which takes an array of Integer[], which you then have to check for nulls in, and then you store the values away as:

current.left = new TreeNode(null, null, arr[left]);

where the constructor for TreeNode is an int value.

Why do you take an Integer[] array at all? it should all be int[]...

As a consequence, you have null-value issues in your code which make your code harder to read.
The inner iteration of your code is unnecessarily complicated. This code here:

    for (int i = 0; i < half; i++) {
        if (arr[i] != null) {
            final TreeNode current = queue.poll();
            final int left = 2 * i + 1;
            final int right = 2 * i + 2;

should not start with i = 0 because arr[0] was already included as the root. This is not obvious. You should have code that looks more like (note the + 1 and + 2 have been changed to  and + 1 respectively):

    // start at 1 because arr[0] is already processed.
    for (int i = 1; i < half; i++) {
        if (arr[i] != null) {
            final TreeNode current = queue.poll();
            final int left = 2 * i;
            final int right = 2 * i + 1;

In your recursion method, set is a really, really bad variable name, it should be dupcheck or something.
BUG your code does not find the least common ancestor (for whatever definition of Least you choose). This code here:

boolean foundInLeft = foundMatchAndDuplicate(node.left, lcaData, n1, n2, set);
boolean foundInRight = foundMatchAndDuplicate(node.right, lcaData, n1, n2, set);

means that if there is a (duplicate) match in both the left, and the right, that whatever happened in the 'left' side is kept, and whatever happens in the right side is 'forgotten' (because you later have the check && lcaData.lca == null ). As a result, you only identify the 'left-most' common ancestor.


Lock-free multiple producer single consumer message queue For a while now I've been after a lock-free, simple and scalable implementation of a multiple producer, single consumer queue for delegates in C#. I think I finally have it. I've run basic tests on it showing it works, and the design is so simple that I've managed to convince myself it is rock-solid.
This relies on a compare-and-swap approach to update the queue, similar to the new lock-free pattern used to generate event field accessors in C# 4.0 (see here), combined with an Interlocked.Exchange to read-and-set the queue to null.
Essentially, this derived from the realization that message-queues are really one-shot multicast delegates that reset their invocation lists after message execution!
However, parallel code is very hard to get right so I would like confirmation that this pattern is indeed correct. I've found my intuitions can be surprisingly misleading when it comes to parallelism, and there's always some crazy edge-case driving me off...
So, to the question: Can anyone confirm to me that the below message queue design pattern is thread-safe?
public class MessageQueue
{
    Action queue;

    public void Enqueue(Action message)
    {
        Action currentQueue;
        var previousQueue = queue;
        do
        {
            currentQueue = previousQueue;
            var newQueue = currentQueue + message;
            previousQueue = Interlocked.CompareExchange(ref queue, newQueue, currentQueue);
        }
        while (previousQueue != currentQueue);
    }

    public void Process()
    {
        var current = Interlocked.Exchange(ref queue, null);
        if (current != null)
        {
            current();
        }
    }
}

As far as thread safety goes, your code is fine. But there's a couple of things I want to point out:

The meaning of your variables (previousQueue, newQueue and currentQueue) isn't very clear, at least not to me. And when writing multi-threaded code, readability becomes extremely important.

Also, for CAS-loops, I always find a "while(true) - break" loop a lot easier on the eyes, but that's my personal opinion.

Here's my suggestion for improving readability:

while(true)
{
    var expectedOldQueue = queue;
    var newQueue = expectedOldQueue + message;
    var actualOldQueue = Interlocked.CompareExchange(ref queue, newQueue, expectedOldQueue);

    if(expectedOldQueue == actualOldQueue)
        break;
}

Also, I'm a bit concerned about the design you chose to achieve a multiple producer/single consumer queue, or maybe I'm just not understanding something...

If I understood correctly, producers will enqueue actions, instead of items that need to be consumed, and producers will simply call Process to trigger those actions, correct?

So, instead of this:

//producer
queue.EnqueueItem(item);

//consumer
var item = queue.Dequeue();
Console.WriteLine(item);

You're proposing this:

//producer
queue.Enqueue(() => Console.WriteLine(item));

//consumer
queue.Process();

If so, this worries me because it goes against the main vein of a consumer/producer architecture, where the consumers are detached from the producers, and have no idea how items will be consumed.

With your proposal, producers will be in charge of producing work items and define how they are going to be consumed.