Scheme: Math Kangaroo Contest 2013



It was really nice question at Math Kangaroo Contest 2013
Four cars enter a roundabout at the same time, each one from a different
direction, as shown in the diagram. Each of the cars drives less than a full
round of the roundabout, and no two cars leave the roundabout in the
same direction. How many different ways are there for the cars to leave
the roundabout? 

I would like to introduce elegant solution in scheme
(let ([src '(A B C D)])
 (length ( filter-not
           (lambda(x)
             (ormap eq? x src))
           (permutations src)) )
  
)

So you can find correct answer for four cars (9) , five cars (44) and more

Enjoy!

[SOLVED]: OTRS keeps to reopen tickets


when OTRS reopens consistently the tickets you have several options to resolve an issue

1. Disable state change with following options

PostMasterFollowUpState='open' # to keep ticket open
PostMasterFollowUpStateClosed='closed' # to keep ticket as closed if it was already closed




2. Disable followup mails individual per queue in Queue-Admin management using FollowUp combo box.

It exists also undocumented feature you can set default value for combo in Config.pm file
# 1-possible, 2-reject, 3-create new ticket
$Self->{'AdminDefaultFollowUpID'} = "3";


Enjoy!

ForEach LINQ Extension

Crosspost from codeproject.com

Introduction

There are many "missing" extensions of LINQ to Objects. You can look them up with LINQ Extensions Library or MoreLinq library, but with this article I would like to focus solely on ForEach extension. I believe that ForEach extension is the most missed feature from LINQ to Objects. While other extensions are nice-to-have and sometimes you use them and most of the time you don't, the ForEach extension is essential because foreach statements are intrinsic to C# code.

ForEach extension consists of just a simple foreach statement. The extension iterates over the elements of the collection and perform an action on each one of them. However, the benefit of this code being wrapped in an extension is that it can be written in a Lambda notation and can be chained with other LINQ expressions. If you just need the straightforward ForEach extension, you can NuGet it from the libraries I mentioned before. What I want to do in this article is to extend it a little bit further to accommodate more complex user-case scenarios.

ForEach

Our starting point is the basic and most simple version of ForEach extension. You can also take a look at the code in LINQ Extensions Library's ForEach.

// ForEach
public static IEnumerable ForEach(this IEnumerable source, 
    Action action)
{
    foreach (TSource item in source)
        action(item);

    return source;
}

// Foreach with index
public static IEnumerable ForEach(this IEnumerable source, 
    Action action)
{
    int index = 0;
    foreach (TSource item in source)
        action(item, index++);

    return source;
}

These are the obvious extensions. The first extension performs an action on each item. The second extension also passes the item's index in the source collection.

int[] arr = new int[] { 0, 1, 2, 3, 4, 5 };

arr.ForEach(x => { Console.WriteLine("item " + x); });

arr.ForEach((x, index) => { Console.WriteLine("item " + x + ", index " + index); });

ForEach with previous items

Consider a situation in which you would like to iterate over the collection but would also like to have a reference to the previous item or the 2 previous items. Of course you can generalize this situation to any number of previous items but I think a real-life scenarios would beg no more than 2 items at most.

// Foreach with index and previous item
public static IEnumerable ForEach(this IEnumerable source, 
    Action action)
{
    int index = 0;
    TSource previousItem = default(TSource);
    foreach (TSource item in source)
    {
        action(item, index++, previousItem);
        previousItem = item;
    }

    return source;
}

// Foreach with index and 2 previous items
public static IEnumerable ForEach(this IEnumerable source, 
    Action action)
{
    int index = 0;
    TSource previousItem1 = default(TSource);
    TSource previousItem2 = default(TSource);
    foreach (TSource item in source)
    {
        action(item, index++, previousItem1, previousItem2);
        previousItem2 = previousItem1;
        previousItem1 = item;
    }

    return source;
}

When there are no previous items (start of the collection), the extension method pass along the default value of the source type.

int[] arr = new int[] { 0, 1, 2, 3, 4, 5 };

// ForEach with index and previous item
arr.ForEach((x, index, previousItem) =>
{
    Console.WriteLine("previous item " + previousItem);
});

// ForEach with index and 2 previous items
arr.ForEach((x, index, previousItem1, previousItem2) =>
{
    Console.WriteLine(
        "previous item #1 " + previousItem1 + 
        ", previous item #2 " + previousItem2
    );
});

Breakable ForEach

A foreach statement is a loop which can be break out of by using the break statement. I wanted to mimic this behavior by letting the extension method to know when to continue and when to stop applying the action on the remaining items in the collection. The implementation is done by returning a boolean from the action. true means continue, false means break from the loop.

// Breakable ForEach
public static IEnumerable ForEach(this IEnumerable source, 
    Func action)
{
    foreach (TSource item in source)
    {
        if (!action(item))
            break;
    }

    return source;
}

This time the action argument is not an Action delegate but rather a Func delegate with boolean as its returning type. Breaking from the foreach loop doesn't impact the LINQ chaining because the source collection is returned as is. There are more overload versions of this Breakable ForEach, one with an index and 2 more with previous items.

int[] arr = new int[] { 0, 1, 2, 3, 4, 5 };

arr.ForEach(x => 
{ 
    Console.WriteLine("item " + x); 

    if (x == 2)
        return false; // break
    return true; // continue
});

arr.ForEach((x, index) => 
{ 
    Console.WriteLine("item " + x + ", index " + index); 

    if (index == 2)
        return false; // break
    return true; // continue
});

ForEach with previous results

We already saw that we can pass to the action one or two previous items from the collection. What if wanted to pass not the item but a value that is returned from the previous action? Consider our array of integers. A running sum for a given item in the array would be the current item + the result calculation of the running sum for the previous item.

// ForEach with index and previous result
public static IEnumerable ForEach(this IEnumerable source, 
    Func action)
{
    int index = 0;
    TResult previousResult = default(TResult);
    foreach (TSource item in source)
        previousResult = action(item, index++, previousResult);

    return source;
}

The extension method requires both the type of the source collection and the type of the return value of the action.

int[] arr = new int[] { 0, 1, 2, 3, 4, 5 };

// ForEach with index and previous result
arr.ForEach((x, index, previousResult) =>
{
    int result = 10 * x;
    Console.WriteLine("result " + result + ", previous result " + previousResult);
    return result;
});

// Running sum
arr.ForEach((x, index, previousResult) =>
{
    int runningSum = previousResult + x;
    Console.Write(runningSum + " ");
    return runningSum;
});

Conclusion

I integrated this code with all my work projects and it has been helping me immensely. It's working seamlessly with any LINQ expression that I write. I hope it will help you too.

HOWTO copy only newer files and preserve structure of directories

Often we need to copy only recently modified files and keep directories in its original states.
To display just modified files we can use command
# find . -type f -mtime 0
where . (dot) means start search from current directory
-type f means find and display only files otherwise directories appear also in the list
-mtime 0 means modify time was within last 24 hours
It looks good so far? Let’s continue.
# find . -type f -mtime 0 | xargs tar -cf /path_to/archive.tar
found filenames will be piped to tar command. Hence archive.tar contains all freshly changed files.
if any filename contains whitespaces it can be correctly handled with small changes
# find . -type f -mtime 0 –print0 | xargs -0 tar -cf /path_to/archive.tar
And what if we need only updated files in the last 10 minutes? It is not a problem at all.
Replace  -mtime 0 with –mmin -10
# find . -type f -mmin -10 –print0 | xargs -0 tar -cf /path_to/archive.tar

Enjoy!



HOWTO: removing spam referrer from your wordpress blog

I have recently discovered the plugin which helps me to remove completely all annoying referral sites in analytics.  Familiar pattern of best seo, social buttons, get free traffic and so on appears in your analytics?



The solution is simple - "Spam Referrer Block" Plugin. It's available from wordpress.org, All settings are clearly defined.



Enjoy!

Apache: _default_ virtualhost overlap on port 80

or Apache: _default_ virtualhost overlap on port 443

The size declaration place matters. Apache reads configuration settings from extras directory in alphabetic order. The error will appears if your virtual host was declared before httpd-vhost.conf 
The solution is quite simple. Just add to the httpd.conf before Listen 80 the line

NameVirtualHost *:80

If you use SSL virtual hosts add into httpd-ssl.conf file before Listen 443 the line

NameVirtualHost *:443


Enjoy!

C# IntelliSense plugin for Notepad++ (CSScriptNpp)

This plugin allows convenient editing and execution of the C# code (scripts). It also allows the usual C# IntelliSense and project management tasks to be performed in a way very similar to the MS Visual Studio.
In addition to this, it provides generic debugging functionality (with the integrated Managed Debugger) as well as the ability to prepare C# scripts for the deployment packages (script+engine or self-contained executable).
Typically user opens the C# file with Notepad++ and after presses 'Load' button on the CS-Script toolbar the all features can be accessed through two Notepad++ dockable panels Project and Output panel.



Source: csscriptnpp.codeplex.com

Asterisk AST_SORCERY function

 AST_SORCERY gets a field from a sorcery object. Sorcery is always created for PJSIP aors, endpoints and identifies in asterisk. It allows y...