texts: Linq version of the simulated annealing algorithm

texts:
I decided to try and implement (a version of) the simulated annealing algorithm using just LINQ, just to see if I could.

I'd love it if anybody could see any ways to improve it, or give advice on any cool tricks for doing this kind of thing.

var result = (from res in Enumerable.Range(0, 1)
                  let R = new Random()
                  let initial = Enumerable.Range(0, 10).Select(i => R.Next(-10, 10))
                  let iterations = Enumerable.Range(0, 100)

                  let Schedule = (Func<int, float>)
                                 (X => 4 + (float)Math.Sin(X))

                  from iteration in iterations
                      let temperature = Schedule(iteration)
                      let state = Enumerable.Range(0, 10).Select(i => R.Next(-10, 10))
                      let DeltaE = state.Sum() - initial.Sum()

                      where DeltaE > 0 ||
                            Math.Pow(Math.E, DeltaE / temperature) > R.NextDouble()
                      select state.ToList()
              ).OrderBy(S => S.Sum()).First();


texts: Generate short database identifier names from an integer

texts:
I wrote the following to generate short database identifier names (e.g. parameters). This isn't a terribly practical requirement and could be fulfilled nearly as well by using base-26, but what seemed like a simple problem became an interesting challenge.

My result doesn't feel very clean, and it has some collisions. I would appreciate performance or elegance suggestions.

Rules: First character must be alphabetic, subsequent must be alphanumeric.

public string intToDatabaseIdentifier(int number)
{
    if(number < 0 || number > 1000000)
        throw new ArgumentOutOfRangeException("number");
    if(number > 25 && number <= 25 + 10) // Skip 0-9 (modified base 36)
        number += 10;
    if(number > 971 && number <= 971 + 360) // Skip 0a-09 (modified base 36)
        number += 360;
    if(number > 35027 && number <= 35027 + 12960) // Skip 0aa-099 (modified base 36)
        number += 12960;
    var stack = new Stack<char>();
    // Base 36, but starting with letters rather than numbers
    const string characters = "abcdefghijklmnopqrstuvwxyz0123456789";
    while(number >= 0) {
        stack.Push(characters[number % 36]);
        number = number / 36 - 1;
    }
    return new string(stack.ToArray());
}

Additional characters (e.g. _, #) can be easily added, but they yield little and make the method less database-agnostic, so I'll stick with alphanumerics.

Results:

a b c d e f g h i j k l m n o p q r s t u v w x y z
aa ab ac ad ae af ag ah ai aj aa ab ac ad ae af ag ah ai aj ak al am an ao
ap aq ar as at au av aw ax ay az a0 a1...

Ideally the output will be as above (without the collisions). The accepted answer in SO works but produces output in an unusual order, e.g. aa ba ca


texts: TLE error: Remove element from LinkedList

texts:
I am working on an easy LinkedList basic operation problem on Remove Linked List Elements - LeetCode

  Remove Linked List Elements

  Remove all elements from a linked list of integers that have value val.

  Example:

Input:  1->2->6->3->4->5->6, val = 6
Output: 1->2->3->4->5

My solution:

  The 3 nodes: prev_node(prev), current_node(cur), next_node(nxt)

  change prev.next from current_node to next_node

  prev

  cur = prev.next

  nxt = prev.next.next

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def removeElements(self, head: ListNode, val: int) -> ListNode:
        #relation: prev.next = nxt to replace cur
        prev = head
        if prev == None: return head
        if prev.next == None: #cur == None:
            if prev.next.val == 6:
                prev.next = None
            return head

        while prev.next != None: #cur !=None
            if prev.next.val == 6: #if cur.val == 6
                nxt = prev.next.next
                prev.next = nxt
                prev = prev.next #advance to traverse
        return head

The solution:

class Solution:
    def removeElements(self, head: ListNode, val: int) -> ListNode:
        #relation: prev.next = nxt to replace cur
        dummy = prev = ListNode(0)
        prev.next = head

        while prev != None and prev.next != None: #cur !=None
            if prev.next.val == val: #if cur.val == val
                nxt = prev.next.next
                prev.next = nxt
            else:
                prev = prev.next #advance to traverse
        return dummy.next

However, it reported Time Limit ExceededError.

I assume that problem is not  performance but mistakes in my implementation which I don't find.

What's the problem?


texts: Implementation of a Singularly LinkedList

texts:
I'm new to C++ and trying to learn by implementing data structures in it.

Is the code below the most efficient way of implementing the data structure in C++? Does it conform to C++ standards, and how readable is it?

#include <iostream>

class Node
{
public:
    int data;
    Node *next;
    Node(int data)
    {
        this->data = data;
        this->next = nullptr;
    }
};

class LinkedList
{
public:
    Node *head;
    Node *tail;
    int length;
    LinkedList()
    {
        this->head = nullptr;
        this->tail = nullptr;
        this->length = 0;
    }

    void add(int data);
    void add(int data, int position);
    void delete_node(int position);
    int get_length();
    void print();
    int get_first();
};

void LinkedList::add(int data)
{
    Node *node = new Node(data);

    if (this->head == nullptr)
    {
        this->head = node;
    }
    else
    {
        this->tail->next = node;
    }
    this->tail = node;
    this->length = this->length + 1;
}

void LinkedList::add(int data, int position)
{
    Node *temp_head = this->head;
    Node *node = new Node(data);
    int i = 0;
    while (i < this->length)
    {
        if (i == position - 1)
        {
            node->next = this->head->next;
            this->head->next = node;
        }
        this->head = this->head->next;
        i++;
    }
    this->head = temp_head;
    this->length = this->length + 1;
}

void LinkedList::print()
{
    Node *temp_head = this->head;
    while (this->head != nullptr)
    {
        std::cout << this->head->data << '\n';
        this->head = this->head->next;
    }
    this->head = temp_head;
}

int LinkedList::get_length()
{
    return this->length;
}

int LinkedList::get_first()
{
    return this->head->data;
}

int main()
{
    LinkedList *llist = new LinkedList();
    llist->add(0);
    llist->add(1);
    llist->add(3);
    llist->add(4);
    llist->add(2, 2);
    llist->print();
    std::cout << "Length: " << llist->get_length() << '\n';
    std::cout << "First Element : " << llist->get_first() << '\n';
}


texts:
I'm not particularly Python savy and recently wrote a LocalizationEngine for Twisted.

The strategy I implemented was to make a dictionary from base.json and then merge with any existing files set in the Accept-Language set in the URL (or cookie). For instance the Accept-Language of en-US,en;q=0.8,es;q=0.6 would merge base.json, en.json, en-US.json in that order. The folder layout looks like this:

from twisted.web import resource, server, static, error as http_error
import json
import os

## load the JSON resource from the path and return the parse obj (dict in this case)
def getJSON(path):
  file = open(path)
  data = json.load(file)
  file.close()
  return data

class LocalizationEngine(resource.Resource):
  isLeaf = True
  localePath = "static/lang/"

  def __init__(self, prefix):
    pass

  def render_GET(self, request):
    return self.getLocale(request)

  def getLocale(self, request):
    """
    Request a localization and respond with json object of appropriate locale
    """
    setLocales = request.getCookie("i18n") #preferred locale (todo implement)?
    locales = []
    if setLocales:
      locales = json.load(setLocales)
    else:
      lang_header = request.headers.get("Accept-Language", "en") # for example en-US,en;q=0.8,es;q=0.6
      locales = [locale.split(';')[0] for locale in lang_header.split(',')]

    basePath = self.localePath + (request.args.get("path", [""])[0])
    if not basePath.endswith("/"):
      basePath += "/"

    if not os.path.exists(basePath):
      raise http_error.NoResource().render(request)

    lang = getJSON(basePath + "base.json")

    # reverse so specificity overrides
    for locale in reversed(locales):
      path = basePath + locale + ".json"
      if os.path.exists(path):
        lang.update(getJSON(path))

    cache(request)
    request.write(json.dumps(lang))
    request.finish()
    return True

cache.py

from wsgiref.handlers import format_date_time as format_date
from datetime import date, timedelta
from time import mktime

'''
   Sets the cache headers for a (static resource) request
'''
def cache(request, expires=30, public=True):
    #set expires header
    expiry = (date.today() + timedelta(expires)).timetuple()
    request.responseHeaders.setRawHeaders("expires" , [format_date(mktime(expiry))])

    #set cache control
    cache_control = "max-age=" + str(int(60*60*24*expires))
    if public:
        cache_control += ", public"
    else:
        cache_control += ", private"
    request.responseHeaders.setRawHeaders("cache-control", [cache_control])

    return request

A code review of the above implementation or suggesting an existing Twisted/Python Internationalization implementation (couldn't find one) would be great answers :)


texts:
Is there a way to write this code block cleaner and more readable?

 var shareNavigate = function () {
     scope.sharingActions.shareOnFacebook().done(function () {
             app.router.navigateToStories();
         });
 },
     sharePublishNavigate = function () {
         scope.sharingActions.shareOnFacebook().done(function () {
                 scope.model.publish().done(function () {
                         app.router.navigateToStories();
                     });
             });
     },
     loginSharePublishNavigate = function () {
         facebookActions.login().done(function () {
                 sharePublishNavigate();
             });
     };
 if (!noViews && hasEditPermissions) {
     shareNavigate();
 } else if (noViews) {
     if (hasEditPermissions) {
         sharePublishNavigate();
     } else if (!isLoggeedIn) {
         loginSharePublishNavigate();
     } else {
         shareNavigate();
     }
 }

First I define functions and then in each case I call the relevant function. I am using jQuery deferreds and since I need operation to done, I have many nested methods. Is there any way to beautify this code?


texts:
I am working on an application that is required to be constantly running, and as a requirement should make multiple attempts for particular processes such as web API/database calls, just in case network connectivity goes down for a moment, or something similar.
In this case, I'm constantly having to write a basic algorithm:
for (int attempt = 0; i < Config.AttemptCount; i++) {
    try {
        doSomething...;
        return true;
    } catch (Exception e) {
        Log(e);
    } finally {
        if (Config.WaitTimeForRetry > 0)
            Thread.Sleep(Config.WaitTimeForRetry);
    }
}

return false;

This of course is the most basic example, but there are others where I'm returning actual objects, performing heavy operations, etc. I recently got the idea to make a single method that could perform this algorithm for me so I can focus on the overall functionality with each implementation instead. With that said, I present to you, a method I need a better name for, and probably needs a much better implementation, or should be replaced by something pre-existing if such a thing exists because I re-invent the wheel a lot, but none-the-less, it's here .
public static TOut RunProcessWithMultipleAttempts<TIn, TOut>(int numberOfAttempts, int waitTimeInMilliseconds, TIn input, Func<TIn, TOut> process) {
    TOut result = default;
    while (numberOfAttempts-- > 0) {
        try {
            result = process.Invoke(input);
            break;
        } catch (Exception e) {
            Log(e);
        } finally {
            if (waitTimeInMilliseconds > 0)
                Thread.Sleep(waitTimeInMilliseconds);
        }
    }

    return result;
}

To use this, I would write a method that does the work I wish to be performed:
static int attempts = 0;
private static string TrySomething(int i) {
    for (int index = 0; index < i; index++)
        if (attempts++ < 2)
            throw new Exception("Your request was denied.");

    return "Your request was successful.";
}

I would then utilize the RunProcessWithMultipleAttempts method to execute it:
static void Main(string[] args) {
    Console.WriteLine(RunProcessWithMultipleAttempts(3, 1000, 10, TrySomething));

    // Prevent the console from closing.
    Console.ReadKey();
}

In this example, replace the Log(e) call with Console.WriteLine(e) to allow it to work on your local machine.

A few questions in my mind:

How hard is this to scale?
What are the downsides to this method?
What alternatives are available?
Is this code easy to understand?
Not only in final usage, but initial implementation?

Oh, and perhaps my biggest question if I were to keep this implementation around:

What would a more concise and clear name be for this method?


texts: Get all data from an external and paginated Swagger API using Java / Feign / Spring

texts:
I have written the following method that allows me to import from another API all the items stored there using Feign.
The only issue is that the external API provides a size limit of 2000 and therefore I need to take pagination into account to get all the records.
I think that my solution is not very clean and maintainable and I'd like to use streams and newer Java features to make it shorter (like I did in the final part) but I am still learning and I cannot think of good solutions for the main part of the code.
@Service
public class ClientService {

    @Autowired
    private Client Client;

    @Autowired
    private ItemRepository itemRepository;

    public List<Code> importItems() {

        int itemsPerPage = 2000;

        RestPage<Item> restPage = Client.getItems(itemsPerPage, 0);
        List<Item> items = restPage.getContent();
        int totalpages = restPage.getTotalPages();

        List<Item> result = new ArrayList<Item>();
        result.addAll(items);
        for (int pageNumber = 1; pageNumber < totalpages; pageNumber ++) {
            result.addAll(Client.getItems(itemsPerPage, pageNumber).getContent());
        }

        return result.stream()
                .filter(itemo -> !itemRepository.existsByCode(item.getCode()))
                .map(item -> itemRepository.save(new Code(item.getCodemsl)))
                .collect(Collectors.toList());

    }

}

Can you help me in improving this?


texts: Using ID's with a "scope" -like hierarchy

texts:
I currently have a ResourceManager class which is responsible for searching for resources with a given identifier, returning a reference counted pointer to the resource if a valid resource is found, and instantiating a new object if none has been found. Often a resource itself depends on other resources, which are requested from the resource manager.  For example, Window may depend on SdlMain (which initializes and shuts down SDL), and thus the constructor of Window would ask ResourceManager for the SdlMain object.

Example:

ResourceManager resources;
//the return type is std::shared_ptr<Window>, the first argument "MainWindow" is the
//..ID to search for, the rest of the arguments are simply forwarded to the constructor
//..of class Window if needed.
auto mainWindow = resources.make<Window>("MainWindow", "Hello World",
                    512, 512, SDL_WINDOW_SHOWN, SDL_RENDERER_ACCELERATED |
                    SDL_RENDERER_PRESENTVSYNC);
//The Window object contains the renderer, too, so the ID of the current window
//"MainWindow" must be passed to the texture's constructor
auto tex1 = resources.make<Texture>("tex/tex1.png", "tex/tex1.png", "MainWindow");
//This renders the texture to the screen.
tex1->renderToBuffer(0,0);
mainWindow->bufferToDisplay();

However, the requirement of knowing the actual ID of the Window when creating a texture becomes a problem when writing a sprite class, if multiple window objects are to be supported:

//Unlike Texture, a Sprite is not a resource but it's lifetime is bound to the  object
//..owning it (and there may be many sprites using the same Texture). Thanks to
//..how Texture is implemented, now everything that wishes to use a sprite must be
//..aware of the ID of the window "MainWindow" in order to initialize a Sprite.
Sprite sprite(resources, "tex/tex1.png", "MainWindow");

I recently came up a solution for this. I would give the identifiers a context, similar to the "scope" of programming languages. SdlMain would be found in global context, with it's ID "SdlMain" available and meaningful anywhere, while one window may reside in context Game, and a second window may reside in the context Editor. Both Window instances would have the name "Window", but there would be no conflict since they reside in a different contexts. The resulting system might look somewhat like this:

ResourceManager resources;
//Set the scope to "Game"
resources.setContext("Game");
auto mainWindow = resources.make<Window>("Window", "Hello World",
                    512, 512, SDL_WINDOW_SHOWN, SDL_RENDERER_ACCELERATED |
                    SDL_RENDERER_PRESENTVSYNC);
//Texture asks ResourceManager simply for a "Window", with no idea about the current
//context.
Sprite sprite(resources, "tex/tex1.png");
sprite.draw();
mainWindow->bufferToDisplay();
//Set the context back to global
resources.setContext();

The actual ID search would function as follows: First the ID is searched for in the current context, if there are no matches the preceding context is evaluated too, until the search arrives at the global context.

Note that the final version would use integer ID:s instead of strings. I do realize that this might be the best example of overengineering ever, but I'm mostly doing this for its learning value.

Before fully committing to such a system, I would like to know some opinions on the matter from more experienced programmers:

Is this a practicable solution?  Are there any blatant flaws in this concept?  Any suggestions?


texts: Angular controller scope inheritance vs service

texts:
On my site I have a navbar component that I want to customize for each and every ng-view I end up loading. Currently I'm doing this as follows. I have a NavCtrl for the navbar itself and my ng-view directive sits outside the scope of this controller. And I use a navbar service to change/override functionality in the navbar e.g each of my views need to override the click handler for a save button my navbar. The NavbarService has hooks to set the save function. And in the NavCtrl the $scope.save = NavbarService.save.

var app = angular.module('myApp', ['ngRoute', 'ngResource']);

app.config(function($routeProvider) {
  $routeProvider.when('/world', {templateUrl : 'world.html', controller : 'WorldCtrl'}).otherwise({templateUrl : 'hello.html', controller : 'HelloCtrl'});
});

app.service('NavbarService', function() {
  var message = 'Save button not clicked yet',
      saveFunction = function() {
    this.setMessage('Default save called');
  };

  this.save = function() {
    _saveFunction();
  };

  this.setSaveFunction = function(funct) {
    _saveFunction = funct;
  };

  this.setMessage = function(newMessage) {
    message = newMessage;
  };

  this.getMessage = function() {
    return message;
  }
});

app.controller('NavCtrl', function($scope, $location, NavbarService) {
  $scope.message = NavbarService.getMessage();
  $scope.save = NavbarService.save;
  $scope.world = function() {
    $location.path('/world');
  };

  $scope.hello = function() {
    $location.path('/hello');
  };

  $scope.$watch(NavbarService.getMessage, function(newValue) {
    $scope.message = newValue;
  });
});

app.controller('HelloCtrl', function($scope, NavbarService) {
  $scope.init = function() {
    NavbarService.setSaveFunction(function() {
      NavbarService.setMessage('Save method called from the HelloCtrl');
    });
  };
});

app.controller('WorldCtrl', function($scope, NavbarService) {
  $scope.init = function() {
    NavbarService.setSaveFunction(function() {
      NavbarService.setMessage('Save method called from the WorldCtrl');
    });
  };
});

  <title>My App</title>

    <button ng-click="save()">Save</button>
    <button ng-click="world()">Go to world</button>
    <button ng-click="hello()">Go to hello</button>

{{message}}

I'm wondering if I'm making this too complicated. I think the same thing can be achieved by just nesting the ng-view directive within the NavCtrl's scope. That way I can override $scope.save in each view controller.

But most the documentation states that services are the preferred way to share resources across controllers. Is one way better than the other? Why?


texts: Lock-free multiple producer single consumer message queue

texts:
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();
        }
    }
}


texts:
Write a program to determine the mean, median, and standard deviation of a list of numbers
  Count the duplicate numbers in the code, listing any that occur more than once with the number of times it occurs.

My implementation:

import math

def mean(input):
  return sum(input) / float(len(input))

def median(input):
  sorted_input = sorted(input)
  length = len(input)
  if length < 1:
    return None
  if length % 2 == 0:
    return (sorted_input[length / 2] + sorted_input[length / 2 - 1]) / float(2)
  else:
    return sorted_input[length / 2]

def duplicate_counts(input):
  distinct_input = set(input)
  for distinct in set(input):
    count = input.count(distinct)
    if (count > 1):
      print("{} occurs {} times".format(distinct, count))

def standard_deviation(input):
  return math.sqrt(mean([(x - mean(input)) ** 2 for x in input]))

def main():
  #sample run/test
  the_list = [3, 6, 7, 2, 8, 9, 2, 3, 7, 4, 5, 9, 2, 1, 6, 9, 6]
  print("The mean is: {}".format(mean(the_list)))
  print("The median is: {}".format(median(the_list)))
  print("The standard deviation is: {}".format(standard_deviation(the_list)))
  duplicate_counts(the_list)

if __name__ == "__main__":
  main()

Sample output:

The mean is: 5.23529411765
The median is: 6
The standard deviation is: 2.64640514805
2 occurs 3 times
3 occurs 2 times
6 occurs 3 times
7 occurs 2 times
9 occurs 3 times

Simple exercise I decided to do with Python for practice and to test/try all the excellent feedback I received on my previous two questions. There may be built in functions for these, and feel free to mention them, but for the sake of practice I implemented them this way.


texts:
General advice for a change. This is an implementation of message queues that I'm going to use for some work on an actors model library.

(defclass message-queue ()
  ((messages :accessor messages :initarg :messages :initform nil)
   (last-cons :accessor last-cons :initarg :last-cons :initform nil
    :documentation "Cached end of the list")
   (len :accessor len :initarg :len :initform 0
    :documentation "Cached message queue length. Modified by enqueue and dequeue")
   (lock :initform (bt:make-lock) :accessor lock
    :documentation "Lock for this message queue")
   (max-len :accessor max-len :initarg :max-len :initform nil
    :documentation "If present, queue maintains at most this many elements")
   (flag :initform (bt:make-condition-variable) :accessor flag
    :documentation "Condition variable used to notify that a message was enqueued")))

(defun make-queue (&optional max-len)
  (make-instance 'message-queue :max-len max-len))

(defmethod full-p ((queue sized-queue))
  (with-slots (len max-len)
      (and max-len (>= len max-len))))

(defmethod empty-p ((queue message-queue))
  (= (len queue) 0))

(defmethod enqueue (object (queue message-queue))
  "Adds an element to the back of the given queue in a thread-safe way."
  (with-slots (lock messages max-len len flag last-cons) queue
    (with-lock-held (lock)
      (let ((o (list object)))
    (cond ((empty-p queue)
           (setf messages o
             last-cons messages
             len 1))
          ((full-p queue)
           (pop messages)
           (setf (cdr last-cons) o
             last-cons o))
          (t (setf (cdr last-cons) o
               last-cons o)
         (incf len)))))
    (condition-notify flag)
    messages))

(defmethod dequeue ((queue message-queue) &optional (timeout 0))
  "Pops a message from the given queue in a thread-safe way.
If the target queue is empty, blocks until a message arrives.
If timeout is not zero, errors after timeout."
  (with-slots (messages lock flag len) queue
    (with-timeout (timeout)
      (with-lock-held (lock)
    (unless messages (condition-wait flag lock))
    (decf len)
    (pop messages)))))

(defmethod dequeue-no-hang ((queue message-queue))
  "Pops a message from the given queue in a thread-safe way.
If the target queue is empty, returns NIL.
The second value specifies whether an item was found in queue (this is meant
to disambiguate the situation where a queue contains the message NIL)"
  (with-slots (messages lock flag len) queue
    (with-lock-held (lock)
      (if messages
      (progn
        (decf len)
        (values (pop messages) t))
      (values nil nil)))))


texts:
I'm learning concurrency and asynchronous programming in C#. For a better grasp on the matter I'm trying to create an OLX Crawler. My issue here is the performance: It takes roughly 159 minutes to crawl 17.901 links.

The method which crawls each item is this:

public async Task FetchItemsData()
    {
        var itemsTask = new List<Task<string>>();
        string itemUrl = string.Empty;
        var semaphore = new SemaphoreSlim(150);

        while (this.queue.TryDequeue(out itemUrl))
        {
            await semaphore.WaitAsync();
            try
            {
                var source =  Task.Run( () => this.GetSource(itemUrl) );

                itemsTask.Add(source);

                await Task.Delay(1);
            }
            finally
            {
                if (itemsTask.Count == 150)
                {
                    await Task.WhenAll(itemsTask);
                    ItemSourceParser itemParser = new ItemSourceParser();

                    foreach (var task in itemsTask)
                    {
                        await task.ContinueWith(async _ =>
                        {
                            itemParser.InitializeNewItem(_.Result);
                            itemParser.TryToParse();
                            Console.WriteLine(itemParser.data["Id"]);
                            this.database.InsertItem(itemParser.data);
                            itemParser.RefreshData();
                            await Task.Delay(1);
                        });
                    }

                    itemsTask.Clear();
                }
                semaphore.Release();
            }
        }

       /* FURTHER PROCESSING */
    }

The queue is a ConcurrentQueue which holds the item string. In the finally block I'm trying to await 150 concurrent tasks to not overload the server.

The GetSource method is here:

public async Task<string> GetSource(string url)
    {
        HttpResponseMessage responseMessage = new HttpResponseMessage();
        try
        {
            this.ProgressUpdater.ProgressBarUpdater.Report(1);
            responseMessage.Version = HttpVersion.Version10;
            responseMessage = await this.client.GetAsync(url);
        }
        catch (Exception e)
        {
            this.ProgressUpdater.LogUpdater.Report("Connection timeout on: " + url + ". Retrying after 0.5 seconds");
            await Task.Delay(1500);
            this.ProgressUpdater.ProgressBarUpdater.Report(-1);
            return await this.GetSource(url);
        }
        return await responseMessage.Content.ReadAsStringAsync();
    }

I'm guessing that something is not right. Is there any redundant code that needs to be removed or something that I should have done so that the crawler has a better performance?


texts: Compare and match results in two two-dimensional arrays in Excel VBA

texts:
I have four loops to populate and compare two two-dimensional arrays then add the results to the first array before writing back to the active worksheet.
Wondering if anyone has a cleaner alternate or more elegant method?
It works as is but haven't tested it on large arrays yet!

Function lastRow(x As Range, y As Worksheet)

lastRow = y.Cells(Rows.Count, x.Column).End(xlUp).Row

End Function

Sub arrayMatch()

Dim arr1() As Variant
Dim arr2() As Variant
Dim rowX As Byte, colX As Byte, aX As Byte

Dim arr1x As Long, arr2y As Long

arr1x = lastRow(Range("A1"), ActiveSheet) 'returns the last row in the column for cell A1
arr2y = lastRow(Range("A1"), Sheets(2)) 'returns the last row in the column for cell A1 on sheet2

ReDim arr1(1 To arr1x, 1 To 4) As Variant 'dynamically sizes arr1 array
ReDim arr2(1 To arr2y, 1 To 2) As Variant 'dynamically sizes arr2 array

For rowX = 1 To arr1x 'arr1x is the last row in the active sheet and the end of array arr1
    For colX = 1 To 3 'only fills array up to 3rd dimension as 4th is reserved for the match results

        arr1(rowX, colX) = Cells(rowX, colX).Value 'set the Cells range to whatever your array is

    Next colX
Next rowX

For rowX = 1 To arr2y 'arr2y is the last row in sheet 2 and the end of array arr2
    For colX = 1 To 2

        arr2(rowX, colX) = Sheets(2).Cells(rowX, colX).Value

    Next colX
Next rowX

For aX = 1 To arr1x
    For rowX = 1 To arr2y
        If arr1(aX, 3) = arr2(rowX, 1) Then
            arr1(aX, 4) = arr2(rowX, 2)
            rowX = arr2y 'helps to exit array earlier when a match is found
        End If
    Next rowX
Next aX

For rowX = 1 To arr1x
    Cells(rowX, 4).Value = arr1(rowX, 4)
Next rowX

End Sub


texts: Kivy language code of my Vehicle Details app

texts:
So I've written my first android app in kivy.

This app takes a Vehicle License Number as input from the user and submits it to a sites and shows the results from the site.

The complete Source Code of the app is at https://github.com/Logmytech/Andhra-pradesh-vehicle-lookup

Here is the Kivy language code:

<RootWidget>:

#This is the root widget's kv definition

    search_input: ap
    roosvelt:roosvelt
    Screen:

        name: 'Screen 1'

        BoxLayout:
            orientation: 'vertical'

            TextInput:
                id: ap
                hint_text: 'Enter the license plate number here'
                size_hint: (1,0.1)
                muliline: False

            Button:
                text: 'Search number'
                on_press:root.postbug()
                size_hint: (1,0.1)

            Label:
                text: "Works for AP numbers only"
                id:roosvelt

Here is some Python code from the app that is related:

class RootWidget(ScreenManager):

The RootWidget which contains all the logic of the app inherits from ScreenManager.

self.roosvelt.text= ".........."

So roosvelt is a text label which I use to show data to the user.

Here is a screenshot of the app in action.


texts: Fetching Python news headlines for Alexa

texts:
I've recently added a custom Alexa Flask Briefing skill that calls an API endpoint which scrapes top news headlines from the Planet Python RSS feed.

It works - whenever you ask Alexa about the news, it's gonna read top 5 (flash briefing API's limitation) topics from the Planet Python section. But, I'd like to see if there are things to improve in the code:

import logging

from bs4 import BeautifulSoup
from lxml import etree
from dateutil.parser import parse
from flask import Flask, jsonify
from nltk import sent_tokenize
import requests

PLANET_PYTHON_FEED_URL = "http://planetpython.org/rss20.xml"

logger = logging.getLogger()
logger.setLevel(logging.INFO)

app = Flask(__name__)

@app.route("/")
def main():
    with requests.Session() as session:
        session.headers = {
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36"
        }

        response = session.get(PLANET_PYTHON_FEED_URL)
        data = response.content

        root = etree.fromstring(data)

        return jsonify([
            {
                "uid": topic.findtext("guid"),
                "updateDate": parse(topic.findtext("pubDate")).strftime("%Y-%m-%dT%H:%M:%S.%fZ"),  # "2016-04-10T00:00:00.0Z"
                "titleText": topic.findtext("title"),
                "mainText": sent_tokenize(BeautifulSoup(topic.findtext("description"), "lxml").text)[0],
                "redirectionUrl": topic.findtext("link")
            }
            for topic in root.xpath("/rss/channel/item")
        ])

if __name__ == '__main__':
    app.run(debug=True, use_reloader=True)

Note that, since there is a news description length limitation, I had to tokenize the descriptions and cut the first sentences only for the Alexa to read - for that, I've used sent_tokenize from nltk - not sure, if this is an overkill for such a straightforward problem.

Project is also available on github, hosted on Heroku.


texts: Function that assigns names to a struct using strtok

texts:
I worked around using strtok function so to assign first name and last name to a struct PERSONNE from a char *names[] array. It was not possible to directly modify the *names[] using strtok since it was throwing an exception.

This code works fine but the last loop shows a lot of '\n' until it reaches 10. I am unable to set the unused data in p1 to NULL. Is it bad practice to not set unused structure data to NULL?

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

#define u_short             unsigned short
#define u_int               unsigned int

#define bool                int
#define true                1
#define false               0

#define MAX                 10

typedef struct PERSONNE
{
    int id;
    char nom[MAX];
    char prenom[MAX];
}personne, *p_personne;

char *names[]={
                "HARRY JANDU",
                "LIBNI MACTAVISH",
                "DIMPLE CHOWDARI",
                "PRIYA MOHAN",
                NULL
            };

int main(void)
{
    int i, j;
    personne p1[10];
    memset(&p1, 0, sizeof(p1));       // doesn't set to 0
    for( i=0; i<sizeof(names); i++ )
    {
        if( names[i]==NULL )break;
        char *temp=NULL, *pch=NULL;
        temp=(char*)malloc(strlen(names[i]));
        strcpy(temp, names[i]);
        pch=strtok(temp, " ");
        strcpy(p1[i].prenom, pch);
        while( pch!=NULL )
        {
            strcpy(p1[i].nom, pch);
            pch=strtok(NULL, " ");
        }
    }

    printf("FIRST NAME\tLastName\n");
//  printf("%d\n", p1[0]);
    for( i=0; i<10; i++ )
    {
        if( p1[i].nom==0 )break;
        printf("%s\t\t%s\n", p1[i].prenom, p1[i].nom);
    }

    return 0;
}


texts:
I have a program that gives the player a random U.S. state, asks for its capital, and keeps score. This keeps going until the player decides to stop playing. At the end, it prints out both the amount of correct answers and amount of incorrect answers.
I took some of the advice into account that I got on my other posts and put the dictionary within the main function (I had it in its own function before), but was wondering if there was a more efficient way to do the while loop that is the basis of the game, as well as if me converting the dict to a list is an okay way to solve this problem.
MIN = 0
MAX = 49

import random

def main():

    stateDict = {'Alabama': 'Montgomery',
              'Alaska': 'Juneau',
              'Arizona': 'Phoenix',
              'Arkansas': 'Little Rock',
              'California': 'Sacramento',
              'Colorado': 'Denver',
              'Connecticut': 'Hartford',
              'Delaware': 'Dover',
              'Florida': 'Tallahassee',
              'Georgia': 'Atlanta',
              'Hawaii': 'Honolulu',
              'Idaho': 'Boise',
              'Illinois': 'Springfield',
              'Indiana': 'Indianapolis',
              'Iowa': 'Des Moines',
              'Kansas': 'Topeka',
              'Kentucky': 'Frankfort',
              'Louisiana': 'Baton Rouge',
              'Maine': 'Augusta',
              'Maryland': 'Annapolis',
              'Massachusetts': 'Boston',
              'Michigan': 'Lansing',
              'Minnesota': 'Saint Paul',
              'Mississippi': 'Jackson',
              'Missouri': 'Jefferson City',
              'Montana': 'Helena',
              'Nebraska': 'Lincoln',
              'Nevada': 'Carson City',
              'New Hampshire': 'Concord',
              'New Jersey': 'Trenton',
              'New Mexico': 'Santa Fe',
              'New York': 'Albany',
              'North Carolina': 'Raleigh',
              'North Dakota': 'Bismarck',
              'Ohio': 'Columbus',
              'Oklahoma': 'Oklahoma City',
              'Oregon': 'Salem',
              'Pennsylvania': 'Harrisburg',
              'Rhode Island': 'Providence',
              'South Carolina': 'Columbia',
              'South Dakota': 'Pierre',
              'Tennessee': 'Nashville',
              'Texas': 'Austin',
              'Utah': 'Salt Lake City',
              'Vermont': 'Montpelier',
              'Virginia': 'Richmond',
              'Washington': 'Olympia',
              'West Virginia': 'Charleston',
              'Wisconsin': 'Madison',
              'Wyoming': 'Cheyenne'}

    statesList = list(stateDict)

    correct = 0
    incorrect = 0

    again = 'y'

    while (again == 'y'):
        num = random.randint(MIN,MAX)
        state = statesList[num]
        print('\n' + state)
        ans = input("\nEnter the capital of this state: ")
        if (ans == stateDict[state]):
            correct += 1
            print("Correct")
            again = input("Type y to play again, anything else to stop: ")
        else:
            incorrect += 1
            print("Incorrect")
            again = input("Type y to play again, anything else to stop: ")

    print("\nYou have",correct,"correct answers")
    print("\nYou have",incorrect,"incorrect answers")

main()


texts:
Below is my implementation of a stack in C. Is it correct? And/or is there anything that I should fix to make it better in any way?

stack.h

#ifndef STACK_H
#define STACK_H

#define EMPTY_STACK -1
typedef struct stack
{
  char ch;
  struct stack* prev;
} Stack;

extern Stack* init_stack(void);
extern char pop(Stack*);
extern void push(Stack*, char);

#endif

stack.c

#include "stack.h"
#include "stdlib.h"

Stack* init_stack()
{
  Stack* ret = (struct stack*)malloc(sizeof(struct stack));
  ret->prev = NULL;
  return ret;
}

char pop (Stack* stck)
{
  char ret;
  if (stck->prev)
  {
    ret = stck->ch;
    Stack* prv = stck->prev;
    stck->prev = prv->prev;
    stck->ch = prv->ch;
    free(prv);
    return ret;
  }
  else
    return EMPTY_STACK;
}

void push (Stack* stck, char ch)
{
  Stack* new_nd = (struct stack*)malloc(sizeof(struct stack));

  new_nd->prev = stck->prev;
  new_nd->ch = stck->ch;

  stck->prev = new_nd;
  stck->ch = ch;
}

Thanks!


texts: Why is this Phaser 3 app munching resources?

texts:
So, I've got a game in the pipeline.

There's a problem, tho. After a couple of weeks of lunch hours incrementally adding stuff, it's regularly using up 100% of my CPU and slowing down. I don't really know how to go about debugging what part of the application is using all those resources.

I've particularly noticed that if you restart the map, (by pressing r or falling in water) a few times, the resource issue gets much worse.

Possible issues:

It's loading quite a lot of assets in /js/loader.js

The collision method on the platforms group (interaction.landOnPlatform) is being called many times a second. Although that steps over most functionality when 3 flags are false.

  landOnPlatform: function (player, platform) {
    if (platform.breakable) {
      interaction.breakPlatform(player, platform);
    }
    if (player.slamming) {
      player.slamming = false;
      action.shakeCamera();
    }
    if (player.jumping) {
      if (player.body.onFloor()) {
        audio.play_land();
        player.jumping = false;
      } else if (player.body.onCeiling()) {
        audio.play_sfx('head_block')
      }
    }
  },

There's also quite a lot of objects being created by map.draw (reading a file from /maps/

Because of the repeated reloading exacerbating the issue, could it be that this block in map.draw is not removing all the objects and their interactions from memory?

  platforms.clear(true, true);
  doors.clear(true, true);
  water.clear(true, true);
  keys.clear(true, true);
  springs.clear(true, true);
  sliders.clear(true, true);
  slider_tracks.clear(true, true);

The full code base is here:

https://github.com/AJFaraday/night_and_day

I'm really looking for advice on how to find this issue, or more information on exactly what and where the issue is.

Please be kind, I'm a ruby developer playing in JavaScript, so this isn't my first language.


texts: Check if each element of my list param contains all data string passed as a parameter

texts:
My code below is working but I am searching for performance and improvement.
It consists of looping my PartList  elements and check if my string parameter orginalDataStringList contains the elements on label string for each Part element.

private boolean excludeAllInformationVariation(List<String> orginalDataStringList, PartList parts) {
    for (Part part : parts) {
        List<String> listTemp = new ArrayList<String>();
        listTemp.addAll(orginalDataStringList);
        if (!part.getOptLogic().isLabelNullOrEmpty()) {
            String[] dataArrays = part.getOptLogic().getLabel().split("( )");
            for (String data : dataArrays) {
                listTemp.remove(data);
            }
        } else {
            return false;
        }
        if (!listTemp.isEmpty()) {
            return false;
        }
    }
    return true;
}

public class PartList extends ArrayList<Part> {
    public PartList() {
        //Empty constructor
    }
}

public class Part {
    private Logic optLogic;
    public Logic getOptLogic() { return optLogic; }
    public void setOptLogic(Logic optLogic) { this.optLogic = optLogic; }
}

public class Logic {
    private String label;
    public String getLabel() { return label; }
    public void setLabel(String label) { this.label = label; }
}
public boolean isLabelNullOrEmpty() {
    return (this.getLabel() == null || this.getLabel().isEmpty()) ? true : false;
}


texts:
get_item_count calls to an API and gets a count.  There are several pages to get this count from until there are no more results.

How do I improve this code to remove all of the extra variables and make it easier to read?

final_count = 0
page = 1
final_count = individual_count = get_item_count(config, page)
while individual_count > 20
  page = page + 1
  individual_count = get_item_count(config,page)
  final_count = individual_count + final_count
end


texts: A recursive_print Function For Various Type Arbitrary Nested Iterable Implementation in C++

texts:
This is a follow-up question for A recursive_transform for std::vector with various return type and A recursive_transform for std::array with various return type. Based on these discussion about the recursive things, the another idea comes up in my mind that trying to implement a print function for arbitrary nested iterable things. Normally, the multiple for loops may be used for printing nested iterables. Maybe like this one.
std::vector<double> testVector1;
testVector1.push_back(1);
testVector1.push_back(20);
testVector1.push_back(-100);

decltype(testVector1) testVector2;
testVector2.push_back(10);
testVector2.push_back(90);
testVector2.push_back(-30);

std::vector<decltype(testVector1)> testVector3;
testVector3.push_back(testVector1);
testVector3.push_back(testVector2);

for (auto& outer_element : testVector3)
{
    for (auto& inner_element : outer_element)
    {
        std::cout << inner_element << std::endl;
    }
}

However, the multiple nested for loop shows up when it comes to the case like std::vector<std::vector<std::vector<std::vector<...>>>>.
If the structure similar to recursive_transform is used in this task, that's the recursive_print function here.
template<typename T>
concept is_iterable = requires(T x)
{
    *std::begin(x);
    std::end(x);
};

template<typename T>
concept is_elements_iterable = requires(T x)
{
    std::begin(x)->begin();
    std::end(x)->end();
};

template<class T> requires is_iterable<T>
T recursive_print(const T& input, const int level = 0)
{
    T output = input;
    std::cout << std::string(level, ' ') << "Level " << level << ":" << std::endl;
    std::transform(input.cbegin(), input.cend(), output.begin(),
        [level](auto& x)
        {
            std::cout << std::string(level, ' ') << x << std::endl;
            return x;
        }
    );
    return output;
}

template<class T> requires is_iterable<T> && is_elements_iterable<T>
T recursive_print(const T& input, const int level = 0)
{
    T output = input;
    std::cout << std::string(level, ' ') << "Level " << level << ":" << std::endl;
    std::transform(input.cbegin(), input.cend(), output.begin(),
        [level](auto& element)
        {
            return recursive_print(element, level + 1);
        }
    );
    return output;
}

Here's a Godbolt link. The output of the example is as below. The nested level is also displaying with this recursive_print function.
Level 0:
 Level 1:
  Level 2:
  1
  20
  -100
  Level 2:
  10
  90
  -30
 Level 1:
  Level 2:
  1
  20
  -100
  Level 2:
  10
  90
  -30

All suggestions are welcome.

Which question it is a follow-up to?
A recursive_transform for std::vector with various return type and
A recursive_transform for std::array with various return type

What changes has been made in the code since last question?
The previous questions focus on the input/output content and type, but the print out operation is the main idea here.

Why a new review is being asked for?
If there is any possible improvement about this code, please let me know.


texts:
I'm using a function to determine if resources can be used again or not.
This is the numpy array I'm using.
 'Resource_id', 'Start_date', 'end_date', 'start_time', 'end_time', 'overload'
   [548, '2019-05-16', '2019-05-16', '08:45:00', '17:40:00',2],
   [546, '2019-05-16', '2019-05-16', '08:45:00', '17:40:00',2],
   [546, '2019-05-16', '2019-05-16', '08:45:00', '17:40:00',2],
   [543, '2019-05-16', '2019-05-16', '08:45:00', '17:40:00',1],

First step is to find all resource available on a date, for example (2019-05-16 from 8:30 to 17:30). To achieve this I used np.where like the example below:
 av_resource_np = resource_availability_np[np.where(
 (resource_availability_np[:,1] <= '2019-05-16')
 & (resource_availability_np[:,2] >= '2019-05-16')
 & (resource_availability_np[:,3] <= '17:30:00')
 & (resource_availability_np[:,4] >= '08:30:00'))]

Here I try to find unique resource ids and the sum of their overload factor using np.unique():
 unique_id, count_nb = np.unique(av_resource_np[:,(0,5)], axis=0, return_counts=True)
 availability_mat = np.column_stack((unique_id, count_nb ))

Which yields the following results:
'Resource_id' 'overload' 'Count'
548           2           1
546           2           2
543           1           1

A simple filtering is done to select which resource hat can't be used in this date. If a resource is used in the same date more or equal (>=) to itsoverload, then we can't use it again.
  rejected_resources = availability_mat [np.where(availability_mat [:, 2] >= availability_mat [:, 1])]

Result here should be both resource 543 and 546 which can't be used again.
So this is main idea behind my function, the problem here is that it takes more than 60% of the whole program runtime and I would appreciate any advice about how to make it more efficient/faster. Thank you.
Full code:
def get_available_rooms_on_date_x(date, start_time, end_time, resource_availability_np):

    av_resource_np = resource_availability_np[np.where(
    (resource_availability_np[:,1] <= date)
    & (resource_availability_np[:,2] >= date)
    & (resource_availability_np[:,3] <= end_time)
    & (resource_availability_np[:,4] >= start_time))]

    unique_id, count_nb = np.unique(av_resource_np[:,(0,5)], axis=0, return_counts=True)

    availability_mat = np.column_stack((unique_id, count_nb ))

    rejected_resources = availability_mat [np.where(availability_mat [:, 2] >= availability_mat [:, 1])]

    return rejected_resources


texts: Creating user accounts while avoid SQL injection attacks

texts:
I have a class that creates user accounts and to avoid SQL injection attacks, I am using the following functions to sanitize both the user name and password inputs:

    $admUsername = filter_var($_POST['admUsername'], FILTER_SANITIZE_STRING);
    $admPassword = filter_var($_POST['admPassword'], FILTER_SANITIZE_STRING);

    $s->bindParam(':admUsername', $admUsername, PDO::PARAM_STR);
    $s->bindParam(':admPassword', $admPassword, PDO::PARAM_STR);

But since I am using filter_var(), when a user creates an account or tries to login, all the special characters like (?,/.!() that s/he inputs are removed. For that reason only alphanumeric characters can be used for the passwords, which might not always be good for the user's security. So my question is, if I use bindParam, is it necessary to also use filter_var or will bindParam do the work that filter_var does?


texts:
I needed a function that would:

Decode, and return, the first character in an UTF8 encoded strings
Return the length of encoding with the special case that lenght of '\0' must be 0
Perfomance are important

I had no special requirement on what to do with invalid sequences so I opted for the following behaviour:

The first byte of an invalid sequences is considered as a single "character" (e.g. for the sequence "\xFF\x2F" it would return '\xFF' as value and 1 as length).
Overlong encoding are accepted

I wrote the following function:

static uint8_t LEN[] = {1,1,1,1,2,2,3,0};
static uint8_t MSK[] = {0,0,3,4,5,0,0,0};

static int utf8_cp(char *txt, int32_t *ch)
{
  int len = 0;
  int32_t val = 0;
  uint8_t first = (uint8_t)(*txt);

  len = (first > 0) * (1 + ((first & 0xC0) == 0xC0) * LEN[(first >> 3) & 7]);
  val = first & (0xFF >> MSK[len]);

  for (int k=len; k>1; k--) {
    if ((*++txt & 0xC0) != 0x80) {
      val = first;
      len = 1;
      break;
    }
    val = (val << 6) | (*txt & 0x3F);
  }

  *ch = val;
  return len;
}

So that a code like:

char *t; int l; int32_t c;

t = "aàも";
while(1) {
  l = utf8_cp(t, &c);
  printf("'%s' len:%d cp:0x%05x\n", t, l, c);
  if (*t == 0) break;
  t += l;
}

Produces:

'aàも' len:1 cp:0x00061
'àも' len:2 cp:0x000e0
'も' len:3 cp:0x03082
'' len:4 cp:0x2b014
'' len:0 cp:0x00000

To make it faster I thought about unrolling the for loop (but I wonder how much could I gain) and introducing, at the beginning, some if to handle ASCII character (but I fear that branching could be more costly that just making a bunch of operation).

I will appreciate any comment you may have and any suggestion for improvement.


texts: Conditional Create: must be the only statement in the batch

texts:
I only want to create this SQL function if the dependent Assembly exists.
I can do it using dynamic SQL, but it seems messy and I lose syntax checking (in
management studio). This function's dependency (for various reasons) may or may
not be available on an individual developers machine, and I don't want to
interrupt our local "get latest" process with an error if this is the case.

Is there a better way?

--Attempt 1 (FAILS)
IF  EXISTS (SELECT * FROM sys.assemblies WHERE name = 'SQL_CLR_Functions')
BEGIN
    CREATE FUNCTION dbo.CLR_CharList_Split(@list nvarchar(MAX), @delim nchar(1) = N',')
    RETURNS TABLE (str nvarchar(4000)) AS EXTERNAL NAME SQL_CLR_Functions.[Library.SQL.CLR.Functions].CLR_CharList_Split
END

--Attempt 2 (FAILS)
BEGIN TRY
    CREATE FUNCTION dbo.CLR_CharList_Split(@list nvarchar(MAX), @delim nchar(1) = N',')
    RETURNS TABLE (str nvarchar(4000)) AS EXTERNAL NAME SQL_CLR_Functions.[Library.SQL.CLR.Functions].CLR_CharList_Split
END TRY
BEGIN CATCH
END CATCH

--Attempt 3 (FAILS)
IF NOT EXISTS (SELECT * FROM sys.assemblies WHERE name = 'SQL_CLR_Functions')
BEGIN
 GOTO END_CLR;
END
GO
CREATE FUNCTION dbo.CLR_CharList_Split(@list nvarchar(MAX), @delim nchar(1) = N',')
RETURNS TABLE (str nvarchar(4000)) AS EXTERNAL NAME SQL_CLR_Functions.[Library.SQL.CLR.Functions].CLR_CharList_Split
GO
END_CLR:

--Attempt 4 (WORKS!!!)
IF EXISTS (SELECT * FROM sys.assemblies WHERE name = 'SQL_CLR_Functions')
BEGIN
    EXEC (N'CREATE FUNCTION dbo.CLR_CharList_Split(@list nvarchar(MAX), @delim nchar(1) = N'','')
            RETURNS TABLE (str nvarchar(4000)) AS EXTERNAL NAME SQL_CLR_Functions.[Library.SQL.CLR.Functions].CLR_CharList_Split')
END


texts:
The following method is used in a call center application to display an approximation of remaining time. The call center telephone operator would inform the caller that they could perform their desired action (like, for example, place another order) after N time has elapsed. You'll note some arbitrary rules revolving around the interpretation of time embedded within the method, e.g., an interval greater than 50 minutes is considered to be "1 hour"; these business rules need to remain as is.

With that in mind, are there any suggestions on how this routine might be enhanced and/or improved (i.e., made more efficient)?

N.B. Obviously, the millisecond case (the "else") is not practical, but only included now for the sake of completeness. It may, in the end, be converted to something like 'less than 3 seconds' equals "now". For the sake of this question, however, let's go with the routine as written.

public static string LargestIntervalWithUnits(TimeSpan interval)
{
    if (interval < TimeSpan.Zero)
    {
        throw new ArgumentOutOfRangeException("interval");
    }

    if (interval == TimeSpan.Zero)
    {
        return "now";
    }

    int timeValue;
    string timeUnits;

    if (interval.TotalHours > 22.0)
    {
        timeValue = (int)Math.Ceiling(interval.TotalDays);
        timeUnits = " day";
    }
    else if (interval.TotalMinutes > 50.0)
    {
        timeValue = (int)Math.Ceiling(interval.TotalHours);
        timeUnits = " hour";
    }
    else if (interval.TotalSeconds > 40.0)
    {
        timeValue = (int)Math.Ceiling(interval.TotalMinutes);
        timeUnits = " minute";
    }
    else if (interval.TotalMilliseconds > 500.0)
    {
        timeValue = (int)Math.Ceiling(interval.TotalSeconds);
        timeUnits = " second";
    }
    else
    {
        timeValue = (int)Math.Ceiling(interval.TotalMilliseconds);
        timeUnits = " millisecond";
    }

    return string.Format("{0:#,##0}{1}{2}",
                         timeValue,
                         timeUnits,
                         (timeValue == 1 ? string.Empty : "s"));
}

Edit: Subsequent refactored code block, that was added after answers were posted, has been removed per users' suggestions. (This note has been added to clarify comments posted by users.)

Edit 2: The code listed above has been refactored and reposted for further review: Displaying TimeSpan as largest interval (with units) [Part II]


texts:
We're given a char[] containing characters A-Z. (For more characters this can easily be extended). Now we're asked to remove all duplicates in the char[].

For example, in: \$['B','A','A']\$, out \$['B','A']\$.

The algorithm:

public void removeDuplicates(final char[] s) {
  if (s == null || s.length < 2) {
    return;
  }

  int checker = 0;
  int j = 0;
  for (int i = 0; i < s.length; i++) {
    if ((checker & 1 << s[i] - 'A') == 0) {
      s[j] = s[i];
      j++;
      checker |= 1 << s[i] - 'A';
    }
  }

  while (j < s.length) {
    s[j] = 0;
    j++;
  }
}

Here, checker is an int whose bits represent whether we've seen a character before. For example, if we've seen A and C, checker would be 5 (101).

i is our next character to check, and for \$j > 0\$, \$[s[0], s[1], ... , s[j-1]]\$ contains no duplicate characters.

Worst case: \$2n-1 = O(n)\$

Test cases:

null -> null
[] -> []
['A'] -> ['A']
['A','B'] -> ['A','B']
['A','A'] -> ['A']
['B','A','A'] -> ['B','A']
['A','B','C','D'] -> ['A','B','C','D']
['A','B','A','D'] -> ['A','B','D']
['A','B','A','D','D','A'] -> ['A','B','D']

I am looking for any flaws, mistakes and opinions in general.


texts: Find the number of substrings of a numerical string greater than a given num string

texts:
Question:
http://qa.geeksforgeeks.org/9744/vinay-queried-interesting-problem-optimization

  Input

  The first line contains N denoting the length of Target String. This
  line is followed by the Target String. The Target String is followed
  by an integer Q denoting the number of queries asked. After this Q
  lines follow each line containing a Query String.

  Output

  For each query output the total number of distinct Cruel Pairs that
  satisfies the given conditions.

  Constraints

  1 ≤ N ≤ 5000
  1 ≤ Q ≤ 104
  Sum of Lengths of all Query Strings ≤ 106
  All strings are composed only of '0' - '9'

  Sample Input

5
75201
5
78945884875
22
00048
77
501

  Sample Output

0
8
8
6
5

  Explanation

  Query String 1 : No such Pair exist.

  Query String 2 : Pairs are (1,2) , (1,3) , (1,4) , (1,5) , (2,3) ,
  (2,4) , (2,5) , (3,5)

  Query String 5 : Pairs are (1,3) , (1,4) , (1,5) , (2,4) , (2,5)

  Time Limit:1.0 sec(s) for each input file. 5s for python

  Memory Limit:256 MB

  Source Limit:1024 KB

Please review the code and help me with optimization

N = int(raw_input())

target = str(int(raw_input()))

N = len(target)

Q_N = int(raw_input())

queries_list = []
output = []

for x in xrange(Q_N):
    queries_list.append(raw_input())

for x in queries_list:
    d = int(str(int(x))[0])
    x = str(int(x))
    q_l = len(x)
    if q_l > N:
        print 0
        continue
    count = 0
    for i, t in enumerate(target):
        t = int(t)
        temp = 0
        if t > d:
            temp = (N - i) - q_l + 1
        elif t > 0 and t < d:
            temp = (N - i) - q_l
        elif t == d:
            temp = (N - i) - q_l
            if int(target[i:i + q_l]) > int(x):
                temp = temp + 1
        if temp > 0:
            count = count + temp

    print count


texts: Existential quantification versus explicit types

texts:
I developed a card game engine for a german three player card game called Skat. The operating code lives in a StateT SkatEnv (WriterT [Trick] IO) Monad.

data SkatEnv = SkatEnv { piles :: Piles
                       , turnColour :: Maybe TurnColour
                       , skatGame :: Game
                       , players :: Players
                       , currentHand :: Hand
                       , skatSinglePlayer :: Hand }
               deriving Show

type Skat = StateT SkatEnv (WriterT [Trick] IO)

To force the three player character on the typelevel, I decided to use a special Players datatype instead of a list, that uses an existentially quantified type PL that can hold any kind of player, that implements the Player typeclass. To make match specific information available to the Player, the chooseCard etc. functions live in a MonadPlayer Monad.

class (Monad m, MonadIO m) => MonadPlayer m where
    trump :: m Trump
    turnColour :: m (Maybe TurnColour)
    singlePlayer :: m Hand
    game :: m Game

class (Monad m, MonadIO m, MonadPlayer m) => MonadPlayerOpen m where
    showPiles :: m (Piles)

class Player p where
    team :: p -> Team
    hand :: p -> Hand
    chooseCard :: (HasCard d, HasCard c, MonadPlayer m)
               => p
               -> [CardS Played]
               -> [CardS Played]
               -> Maybe [d]
               -> [c]
               -> m (Card, p)
    onCardPlayed :: MonadPlayer m
                 => p
                 -> CardS Played
                 -> m p
    chooseCardOpen :: MonadPlayerOpen m
                   => p
                   -> m Card

data PL = forall p. (Show p, Player p) => PL p

data Players = Players PL PL PL
               deriving Show

By using the MonadPlayer interface I can use my Skat monad for calling the Player's functions without exposing unallowed information, e.g. the other players' cards.

instance MonadPlayer Skat where
    trump = getTrump <$> P.game
    turnColour = gets turnColour
    singlePlayer = gets skatSinglePlayer
    game = gets skatGame

I used the PL wrapper, to be able to implement different players in seperate modules, to keep the code clean and extendible, since I have many different players, e.g. an online player that communicates via a socket or a bot, that heuristically decides which card to play.

Another concern for me is the need of the MonadIO restriction for the
MonadPlayer typeclass. This is needed, because my online player for example needs to send messages via a socket, which obviously needs to live in IO, although the bot player could perfectly deal without it. But since I have one common Player interface, I have to make IO available to every player.

Is it better style, not to use the PL wrapper but implement the Players type as

data Players a b c = Players a b c

This would lead to type variables all over the place, especially for everything SkatEnv related.

I left out many implementation details, e.g. many small type definitions. Please let me know, if you need more details about a specific type.


texts: Delete a linked list using recursion in Python

texts:
Given a Singly Linked-List, write a function to delete the linked list recursively.
Below is my attempt.
I am confused if I should use del keyword to delete the nodes or below method is better. I don't know if it is memory efficient this way.I'd appreciate any critique of what I could do better.

class SLLNode:
    def __init__(self, data):
        self.data = data
        self.next = None

class SLL:
    def __init__(self):
        self.head = None

    def append(self, data):
        temp = SLLNode(data)
        if self.head is None:
            self.head = temp
        else:
            current = self.head
            while current.next is not None:
                current = current.next
            current.next = temp

def delete_a_linked_list_using_recursion(head):
    if head is None:
        return head
    head = head.next
    return delete_a_linked_list_using_recursion(head)

"""driver's code"""
sll = SLL()
sll.append(1)
sll.append(2)
sll.append(3)
sll.append(4)

head = delete_a_linked_list_using_recursion(sll.head)


texts: Django Custom Decorator for user group check

texts:
I wrote a custom decorator to check whether user is in particular group of not. Please provide the feedback on optimizing code. I currently moved from Java to Django-Python so I wasn't able to optimize the logic.

Check whether student is in labsheet_collector, assignment_collector or fund_collector.

def labsheet_collector(view_func):
    def _wrapped_view_func(request, *args, **kwargs):
        print request.user
        if request.user.is_anonymous:
            return redirect('/admin/') #?next=%s' % request.path)
        if not (request.user.groups.filter(name='labsheet_collector').exists()) or request.user.is_superuser:
            raise Http404
        return view_func(request, *args, **kwargs)

    return _wrapped_view_func

def assignment_collector(view_func):
    def _wrapped_view_func(request, *args, **kwargs):
        if request.user.is_anonymous:
            return redirect('/admin/')
        if not (request.user.groups.filter(name='assignment_collector').exists()) \
                or request.user.is_superuser:
            raise Http404
        return view_func(request, *args, **kwargs)

    return _wrapped_view_func

def fund_collector(view_func):
    def _wrapped_view_func(request, *args, **kwargs):
        if request.user.is_anonymous:
            return redirect('/admin/')
        if not (request.user.groups.filter(name='fund_collector').exists()) \
                or request.user.is_superuser:
            raise Http404
        return view_func(request, *args, **kwargs)

    return _wrapped_view_func


texts:
I'm pretty new to python and have created a simple hangman. Since doing so I realise this appears to be a fairy common theme.
It uses /usr/share/dict/words on the local system, resulting in some tricky words to guess. I guess I should look at maybe curl'ing a URL to get random words or an API. I've also not defined a main(), not sure if that's bad practice. Feedback appreciated.

#!/usr/local/bin/python3

import os
import random
import subprocess

def draw_man_and_show_word():
   global word
   showword=''.join(word)
   os.system('clear')
   for y in range(0,7):
      for x in range(0,7):
         print (chr(array[y][x]),end='')
      print()
   print ()
   print (showword)
   print ()

def add_limb_to_man():
   global size
   y=man[size][0]
   x=man[size][1]
   c=man[size][2]
   array[x][y]=c
   size=size+1

def find_random_word():
   global theword
   lines=int(subprocess.getoutput("wc -l /usr/share/dict/words | awk \'{print $1}\'"))
   line=random.randint(1,lines)
   theword=subprocess.getoutput("head -"+str(line)+" /usr/share/dict/words |tail -1")
   theword=str.lower(theword)

# 2D array of character (ASCII) values as a matrix for the hangman pic
array=[[124,45,45,45,45,32,32,32],[124,32,32,32,124,32,32,32],[124,32,32,32,32,32,32,32],[124,32,32,32,32,32,32,32],[124,32,32,32,32,32,32,32],[124,32,32,32,32,32,32,32],[124,32,32,32,32,32,32,32]]

# 2D array with the (single) character position to change (x,y,newvalue) for each additional limb
man=[[4,2,79],[4,3,43],[3,3,47],[5,3,92],[4,4,124],[3,5,47],[5,5,92]]

size=tries=0
limit=6
letters_tried=""

find_random_word()

# Array to represent word shown on screen (using array as string immutable)
word=['-' for x in range(0,len(theword))]

while ((tries <= limit)):
   draw_man_and_show_word()

   letter=""
   while (len(letter) != 1 or not letter.islower() or letters_tried.find(letter) >= 0):
      letter=input("Enter your choice of (single lowercase) letter:")

   letters_tried=letters_tried+letter

   pos=theword.find(letter)
   if (pos >= 0):
      tmpword=theword
      while (pos >= 0):
         word[pos]=letter
         tmpword=tmpword.replace(letter,'#',1)
         pos=tmpword.find(letter)

   else:
      add_limb_to_man()
      tries=tries+1

   if (''.join(word) == theword):
      draw_man_and_show_word()
      print()
      print("you got it!")
      exit()

draw_man_and_show_word()

print ("you lost. It was "+theword)


texts:
I need a function to determine whether or not a string has HTML in it or not so that I can know whether I'm dealing with a plain-text format or HTML format.

It seems simple enough in C#, using HTMLAgilityPack.  Recursively go through the tree of nodes, and if any are an element node (or comment too) then we say "Yes, it's HTML"

public static class HTMLUtility
{
    public static bool ContainsHTMLElements(string text)
    {
        HtmlDocument doc = new HtmlDocument();
        doc.LoadHtml(text);

        bool foundHTML =  NodeContainsHTML(doc.DocumentNode);
        return foundHTML;
    }

    private static bool NodeContainsHTML(HtmlNode node)
    {
        return node.NodeType == HtmlNodeType.Element
            || node.NodeType == HtmlNodeType.Comment
            || node.ChildNodes.Any(n => NodeContainsHTML(n));
    }
}

Am I missing anything?  Thanks!


texts: Converting JSONP to JSON: Is this Regex correct?

texts:
I'm fetching a string that is JSONP, and looking to convert it to JSON.

I'm using the following regular expression for matching (for removal) the padding on the JSON.

([a-zA-Z_0-9\.]*\()|(\);?$)

In python, here's the line I'm using for converting the JSONP to JSON:

apijson = re.sub(r'([a-zA-Z_0-9\.]*\()|(\);?$)','',jsonp)

Is there anything I should worry about using this regular expression? Any chance I could end up mangling the JSON?


texts:
I wrote some code which compares numbers of all datatypes with each other.
The standard Comparer which is implemented by every number Type only compares numbers of the same type.

I cast to decimal because this is the largest possible number type and should cover everything.

What do you think about the code?
Is there any room for improvement?
How can I handle NaN the best way?
Do you think BigInt and Real numbers should be a part of it?

public class UniversalNumberComparer: IComparer
{
    public int Compare(object x, object y)
    {
        if(!IsNumber(x) || !IsNumber(y))
            throw new InvalidOperationException();

        if(x == null)
        {
            return y == null
                ? 0
                : -1;
        }
        else
        {
            if(y == null)
                return 1;
        }

        return Decimal.Compare(Convert.ToDecimal(x), Convert.ToDecimal(y));
    }

    private bool IsNumber(object value)
    {
        if(value is sbyte || value is sbyte?)
            return true;
        if(value is byte || value is byte?)
            return true;
        if(value is short || value is short?)
            return true;
        if(value is ushort || value is ushort?)
            return true;
        if(value is int || value is int?)
            return true;
        if(value is uint || value is uint?)
            return true;
        if(value is long || value is long?)
            return true;
        if(value is ulong || value is ulong?)
            return true;
        if(value is float || value is float?)
            return true;
        if(value is double || value is double?)
            return true;
        if(value is decimal || value is decimal?)
            return true;
        return false;
    }
}


texts:
I've written this code in an attempt to create the Monty Hall problem and check the ratios. I would like someone to check the logic on it. I know it isn't organized well at all; I just need to see if I'm performing correctly.

If you don't know what the Monty Hall problem is.

private static int losses = 0;
private static int wins = 0;
public static void main(String[] args) throws Exception {
    for (int x = 0; x < 100; x++) {
        boolean doors[] = getDoors();
        run(getGuess(), doors);
    }
    System.out.println("Wins: " + wins);
    System.out.println("Losses: " + losses);
    double ratio = wins / wins + losses;
    System.out.println("Ratio: " + ratio);
}

public static boolean[] getDoors() {
    //door range
    int min = 1;
    int max = 3;
    //generate random correct door
    Random r = new Random();
    int i = r.nextInt(max - min) + min;
    //assign doors to boolean
    boolean[] doors = new boolean[3];
    for (int x = 0; x < 3; x++) {
        if (x == i) {
            doors[x] = true;
        } else {
            doors[x] = false;
        }
    }
    return doors;
}

public static int getGuess() {
    //guess range
    int min = 1;
    int max = 3;
    //generate random guess
    Random r = new Random();
    int guess = r.nextInt(max - min) + min;
    return guess;
}

public static void run(int guess, boolean[] doors) {
    //if guess is valid before switch, declare loss
    if (doors[guess]) {
        losses++;
        return;
    }
    //find other false door
    int wrongDoor = 0;
    for (int x = 0; x < 3; x++) {
        if (doors[x] == false && guess != x) {
            wrongDoor = x;
        }
    }
    //switch doors
    int newGuess = 0;
    for (int x = 0; x < 3; x++) {
        if (x != wrongDoor && x!= guess) {
            newGuess = x;
        }
    }
    //checkguess
    if (doors[newGuess] == true) {
        wins++;
    } else {
        losses++;
    }
}


texts:
I use this code to make sure that the user didn't leave the combo box empty before pressing OK

            int option=-1;
            while(cboFruits.getSelectedItem() == null){
             option = JOptionPane.showConfirmDialog(null, message, "Fruits", JOptionPane.OK_CANCEL_OPTION);
             if (!(option == JOptionPane.OK_OPTION))
                 break;
             if(cboFruits.getSelectedItem() == null)
                 JOptionPane.showMessageDialog(null, "Select a fruit");
            }

Is there a better alternative to this that doesn't involve creating my own Dialog? The combo box gets filled with data when another combobox changes its value, so putting a value by default is not an option.


texts:
I'm trying to speed up this code that loops over an entire spectrum range. It's using a Raspberry Pi, and a rtl-sdr dongle that covers up to around 1.7 GHz. My goal is to be sweep over that spectrum and gather data from it in a timely manner.

#!bin/usr/python

#gathers from the rtl library needed for this program
from rtlsdr import RtlSdr
import math
from pylab import *
import time
from scipy.fftpack import fft, ifft
import pyqtgraph as pg
from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import os
import sys

os.system("sudo rmmod dvb_usb_rtl28xxu rtl2832")

#RtlSdr set up
##sdr = RtlSdr()
##
##
##num_samples = 4096
##
##freq_range_lo = 50e6     #experimental center freq for now
##freq_range_hi = 600e6  #1750e6
##freq_step = 1e6

##sample_rate = 2e6
##
###Set initial parameters for RTL-SDR Dongle
##sdr.sample_rate = sample_rate
##freq = 512e6
##sdr.center_freq = freq
##
##
##sdr.freq_correction = 102
##sdr.gain = 0

class Spec_Scan(object):

    samples = np.array([])
    samples_temp = np.array([])
    fft_data = np.array([])
    fft_data_temp = np.array([])

    app = QtGui.QApplication([])
    win = pg.GraphicsWindow(title="Basic plotting examples")
    win.resize(1000,600)
    win.setWindowTitle('pyqtgraph example: Plotting')

    def __init__(self, lo, hi, freq, step, samps):
        self.freq_range_lo = lo
        self.freq_range_hi = hi
        self.freq_step = step
        self.freq = freq
        self.num_samples = samps
        self.sample_rate = 2e6

        self.sdr = RtlSdr()
        self.sdr_default()

    def sdr_default(self):
        self.sdr.center_freq = self.freq
        self.sdr.freq_correction = 101
        self.sdr.gain = 0
        self.sdr.sample_rate = self.sample_rate

    def pyqt(self):
        global curve, p6
        # Enable antialiasing for prettier plots
        pg.setConfigOptions(antialias=True)

        self.win.nextRow()
        p6 = self.win.addPlot(title="Freq plot")

        self.curve = p6.plot(pen='y', clear=True)
        #p6.setRange(xRange = [0, (self.num_samples/2)*(1700e6/self.freq_step)], yRange = [-100,100])
        p6.setRange(xRange = [0, (self.num_samples/2)-48], yRange = [-100,100])

        print 'here'
        timer = pg.QtCore.QTimer()
        print'should begin update'
        timer.timeout.connect(self.update)
        timer.timeout.emit()
        print 'update should have finished'
        timer.start(2)

        if __name__ == '__main__':

            if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
                QtGui.QApplication.instance().exec_()

    def update(self):
        global curve, p6

        for self.freq in range(int(self.freq_range_lo), int(self.freq_range_hi), int(self.freq_step)):

            self.sdr.center_freq = self.freq
            self.samples = self.sdr.read_samples(self.num_samples)
            self.fft_data_temp = 20*np.log10(np.abs(np.fft.rfft(self.samples)))
            #self.fft_data_temp = self.fft_data_temp[0:2049]
##            self.fft_data_temp = self.fft_data_temp[1:(self.num_samples/2)]
##            self.fft_data_temp = 2*self.fft_data_temp[2::]
            self.fft_data = np.append(self.fft_data, self.fft_data_temp)
            self.fft_data_temp = 0

        #self.curve.setData(self.fft_data[1::], clear=True)
        self.fft_data = 0

        print 'refresh'

def main():
    num_samples = 4096

    lo = 50e6     #experimental center freq for now
    hi = 1750e6
    step = 1e6
    freq = 425e6

    test = Spec_Scan(lo, hi, freq, step, num_samples)
##    test.pyqt()
    start = time.time()
    test.update()
    end = time.time()
    print str(end-start)

## Start Qt event loop unless running in interactive mode or using pyside.

main()

When the scan itself was timed, the result yielded around 197 seconds which is entirely too long. Is there a way I can speed up the spectrum scan?


texts:
I am new to Haskell and, coming from a OO/procedural programming background, I want to make sure my code follows the functional way of doing things. I built this short module to test if a given string is a palindrome (same backwards and forwards, case-sensitive) and I am simply wondering if this is the most "Haskell" way to do it. Any criticism is greatly appreciated.

module PalindromeTest (isPalindrome) where

isPalindrome :: String -> Bool

isPalindrome w
 | nChars <= 1           = nChars == 1
 | nChars == 2           = firstElem == lastElem
 | firstElem /= lastElem = False
 | firstElem == lastElem = isPalindrome (take (nChars - 2) (tail w))
 where firstElem = head w
       lastElem  = last w
       nChars    = length w


texts:
I've been working on improving my C coding. I wanted to write two string algorithms:

to trim white space from the beginning and end of a string
to compact a string

I have a few problems with this code that I was hoping to get some advice on:

The casts from const char * to char *. Is there a clean and idiomatic way of cleaning those up?
The string compaction algorithm requires extra storage and I couldn't think of a clean way to do the compaction in place. Is there a clean way of doing it in place?

I'm looking for a review on algorithmic correctness, code style, design, and any other improvements you can think of. My main concern is performance!

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
#include <assert.h>

static const char TERMINATOR = '\0';

/* skips space in @param[str] depending on the provided direction
   @param[in] -- str -- the string
   @param[in] -- direction -- the direction to skip space in
                 1 -- forward direction
                 0 -- back direction
   @return -- pointer to first non-space character in the string if
              @param[direction] and @param[str] are valid or the original
              string otherwise
*/
const char *skip_space(const char *str, int direction)
{
    if (direction == 1) {
        while (isspace(*str)) { ++str; }
    } else if (direction == 0) {
        while (isspace(*str)) { --str; }
    }
    return str;
}

const char *ltrim_white(const char *str, size_t len)
{
    char *end = (char *)&str[len - 1];
    end = (char *)skip_space(end, 0);
    *++end = TERMINATOR;
    return skip_space(str, 1);
}

const char *trim_white(const char *str)
{
    return ltrim_white(str, strlen(str));
}

char *compact_with_mem(char *str)
{
    str = (char *)skip_space(str, 1);

    size_t len = strlen(str);
    if (len == 0) {
        char *c = malloc(1);
        if (c) {
            *c = TERMINATOR;
        }
        return c;
    }

    char *back = (char *)skip_space(&str[len - 1], 0);
    *++back = TERMINATOR;

    char *buf = malloc((back - str) + 1);
    printf("Allocated %u memory\n", (back - str) + 1);
    char *copy = buf;

    while (*str) {
        if (isspace(*str) && isspace(*(str + 1))) {
            *copy++ = *str;
            str = (char *)skip_space(str, 1);
        } else {
            *copy++ = *str++;
        }
    }
    *copy = TERMINATOR;
    return buf;
}

void print(const char *s)
{
    const size_t len = strlen(s);
    printf("|%s", s);
    if (*(s + len) == TERMINATOR) { // redundant but just for testing
        putchar('N');
    }
    puts("|");
}

void test(char *s, char *cmp_str)
{
    const size_t blen = strlen(s);
    printf("Base string: |%s|\n", s);
    print(s);

    char *ret = (char *)compact_with_mem(s);
    const size_t len = strlen(ret);
    printf("Length before compaction: %u\nLength after compaction: %u\n", blen, len);

    print(ret);
    puts("");
    assert(strcmp(ret, cmp_str) == 0);
    free(ret);
}

int main()
{
    char s1[] = "   hello!";
    char s2[] = "   world.   ";
    char s3[] = "lol..";
    char s4[] = "";
    char s5[] = "               ";
    char s6[] = "lmfao  ";
    char s7[] = "         x";
    char s8[] = "ab   ";
    char s9[] = "   i am cthulhu   ";
    char s10[] = "  all    your    base are belong    to us      ";
    char s11[] = "  random    chars \\29289892 *&@ @)!  !*  # $ _ ! & # * !   @!  ^ *#^ | {][";
    char s12[] = "omfgwhyaretherenospaceshere!";

    test(s1, "hello!");
    test(s2, "world.");
    test(s3, "lol..");
    test(s4, "");
    test(s5, "");
    test(s6, "lmfao");
    test(s7, "x");
    test(s8, "ab");
    test(s9, "i am cthulhu");
    test(s10, "all your base are belong to us");
    test(s11, "random chars \\29289892 *&@ @)! !* # $ _ ! & # * ! @! ^ *#^ | {][");
    test(s12, "omfgwhyaretherenospaceshere!");
}


texts: Unique elements of a list and corresponding indices in Haskell

texts:
Given a list vs, I want to get the list vs' of the unique elements of vs, as well as the indices of the elements of vs in vs'. For example, if vs = [7, 8, 7, 8, 9] I want to get [7,8,9] (the unique elements) and [0,1,0,1,2] (the indices).

An easy implementation is:

import           Data.List
import           Data.Maybe

unique :: Eq a => [a] -> ([a], [Int])
unique vs = (vsnub, indices)
  where
  vsnub = nub vs
  indices = map (\v -> fromJust $ elemIndex v vsnub) vs

However I think this is not efficient. I've done an implementation using mutable vectors. The pseudo-code is (saying that vs is a list of numbers):

n = length of vs
idx = list of n integers (to store the indices)
visited = [false, false, ..., false] (n elements)
nvs = list of n numbers (to store the unique elements)
count = 0
for(i = 0; i < n; ++i)
{
  if(not visited[i])
  {
    nvs[count] = vs[i]
    idx[i] = count
    visited[i] = true
    for(j = i+1; j < n; ++j)
    {
      if(vs[j] = vs[i])
      {
        visited[j] = true
        idx[j] = count
      }
    }
    count ++
  }
}
nvs = first 'count' elements of nvs

And here is my Haskell code:

{-# LANGUAGE ScopedTypeVariables #-}
import           Control.Monad               ((>>=))
import           Data.Vector.Unboxed         (Unbox, Vector, freeze, (!))
import           Data.Vector.Unboxed.Mutable (IOVector, new, write)
import qualified Data.Vector.Unboxed.Mutable as VM

unique' :: forall a . (Unbox a, Eq a) => [a] -> IO (Vector a, Vector Int)
unique' vs = do
  let n = length vs
  idx <- VM.replicate n 0 :: IO (IOVector Int)
  visited <- VM.replicate n False :: IO (IOVector Bool)
  nvs <- new n :: IO (IOVector a)
  let inner :: Int -> Int -> Int -> IO ()
      inner i j count | j == n = return ()
                      | otherwise =
                        if vs !! i == vs !! j
                          then do
                            write visited j True
                            write idx j count
                            inner i (j+1) count
                          else inner i (j+1) count
  let go :: Int -> Int -> IO (IOVector a)
      go i count | i == n = return $ VM.take count nvs
                 | otherwise = do
                   vst <- VM.read visited i
                   if not vst
                     then do
                       write nvs count (vs !! i)
                       write idx i count
                       write visited i True
                       _ <- inner i (i+1) count
                       go (i+1) (count + 1)
                     else go (i+1) count
  nvs' <- go 0 0 >>= freeze
  idx' <- freeze idx
  return (nvs', idx')

Is it nice? Can it be improved? Is there a solution that does not resort to IO?


texts: Display the n terms of even natural numbers(between 1 and 100) and their sum

texts:
This is a C# console application that displays the n terms of even natural numbers between (1 and 100) and their sum

Is this good? Or is it bug riddled ?

int numberOfEvenNumbers;
int currentNumber;
currentNumber = 0;
int counter;
counter = 1;
int sum;
sum = 0;

Console.Write("How many even numbers :");

numberOfEvenNumbers = Convert.ToInt32(Console.ReadLine());

while (counter <= 100)
{

    if (counter % 2 == 0)
    {
        Console.WriteLine(counter);
        sum += counter;
        currentNumber++;
    }

    if (currentNumber == numberOfEvenNumbers)
    {
        break;
    }

    counter++;
}

Console.Write("Sum : ");
Console.WriteLine(sum);

Console.ReadLine();


texts:
I am trying to figure out how to DRY up this code:

nino = "QQ"
3.times { nino += Random.rand(10..99).to_s}
nino += ("A".."D").to_a.sample

I am trying to generate a sample National Insurance number starting with QQ, six digits and then ending with A, B, C or D.


texts: Come around limitation of CSingleLock don't accept NULL

texts:
The CSyncObject object in CSingleLock constructor can't be NULL but I want it to work with NULL because I need to use this nest.

BOOL CADODatabase::Open(LPCTSTR lpstrConnection, LPCTSTR lpstrUserID, LPCTSTR lpstrPassword)
{
    CString database = GetSourceDatabase( lpstrConnection, NULL );

    // get the appropriate critical section based on database
    g_dbCriticalSection = GetDbCriticalSection( database );

    if( g_dbCriticalSection) // it can be NULL
    {
        g_dbCriticalSection->Lock();
    }

    // more code,  have to unlock() manually in multiple exits!
}

As can be seen, I need to lock() and then unlock() manually at all exit points because its in a nest.

i wrote a class to make CSingleLock work with NULL, my new code looks like this:

BOOL CADODatabase::Open(LPCTSTR lpstrConnection, LPCTSTR lpstrUserID, LPCTSTR lpstrPassword)
{
    CString database = GetSourceDatabase( lpstrConnection, NULL );

    // get the appropriate critical section based on database
    g_dbCriticalSection = GetDbCriticalSection( database );

    CSingleLockEx lock( g_dbCriticalSection, TRUE );

    // eliminates the need to unlock() manually, works like CSingleLock
}

My new class is here:

class CSingleLockEx
{
public:
    CSingleLock* _singleLock;

    CSingleLockEx(CSyncObject* pObject, BOOL bInitialLock = FALSE):
    _singleLock(NULL)
    {
        if (pObject)
            _singleLock = new CSingleLock(pObject, bInitialLock);

    }
    ~CSingleLockEx()
    {
        if (_singleLock)
            delete _singleLock;
    }
};

Since it has to do with critical section, I thought I will put it for review if I have missed anything.


texts: Spring configuration without XML is this reality?

texts:
We're doing a website for Cardshifter, and now I've converted the plain html files and css into a Spring MVC web application. Here is the typical configuration classes that I used in most of the project I work with.

package com.cardshifter.io.web.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.cardshifter.io.web" })
public class ApplicationConfig extends WebMvcConfigurerAdapter {

    @Bean
    ServletContextTemplateResolver templateResolver() {
        ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
        templateResolver.setPrefix("/WEB-INF/");
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode("HTML5");
        templateResolver.setOrder(10);
        return templateResolver;
    }

    @Bean
    SpringTemplateEngine templateEngine() {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(templateResolver());
        return templateEngine;
    }

    @Bean
    ThymeleafViewResolver viewResolver() {
        ThymeleafViewResolver resolver = new ThymeleafViewResolver();
        resolver.setOrder(1);
        resolver.setTemplateEngine(templateEngine());
        resolver.setViewNames(new String[] { "*" });
        return resolver;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
            .addResourceHandler("/css/**","/images/**")
            .addResourceLocations("/css/", "/images/");
    }

}

This is the class that define the view engines and a bunch of other stuff.

package com.cardshifter.io.web.config;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.util.Log4jConfigListener;

import com.cardshifter.io.web.config.ApplicationConfig;

public class AppInitializer  implements WebApplicationInitializer {

    public void onStartup(ServletContext servletContext) throws ServletException {

        // Create the 'root' Spring application context
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.register(ApplicationConfig.class);

        // pour log4j
        servletContext.addListener(new Log4jConfigListener());

        // Register and map the dispatcher servlet
        Dynamic dispatcherSpring = servletContext.addServlet("io", new DispatcherServlet(
                rootContext));
        dispatcherSpring.setLoadOnStartup(1);
        dispatcherSpring.addMapping("/");

        servletContext.addListener(new ContextLoaderListener(rootContext));
    }

}

That class is the one responsible to expose the application to the servlet without a good old web.xml.

What I'm wondering is, is there something more I would need to add in order to make the configuration easier? I always have difficulty to set the resources (css, js, etc.) in the webapp, is what I'm doing the "right" way?


texts:
Below is my entire program. You can read what it does thanks to the comments and specifications in particular.

My question is: can it be improved? Would it be possible, for example, to avoid writing a fwrite() inside each if? Is there a good pattern that can be implemented somewhere in this code?

The entire program is base on this UTF8 model and also studies the case in which a bit occurs in the 32nd position.

#include <stdio.h>
#include <math.h>
#include <stdint.h>

double log(double a);

/*
* This program reads 4 byte codepoints (in BIG ENDIAN) from a file strictly called "input.data" and creates another file called "ENCODED.data" with the relative encoding in UTF8.
*
* In order to compile this file, in Unix, you need to add the -lm clause because the library math.h function log() requires it.
* For example: gcc encoding.c -o encoding -lm
*/
int main() {

    unsigned char bufferCP[4]; //Buffer used to store the codepoints
    unsigned char bufferOut[6]; //Buffer used to store the UTF8-encoded codepoints

    FILE *ptr, *out;
    ptr = fopen("input.data", "rb"); //r for read, b for bynary
    out = fopen("ENCODED.data", "wb");

    int elem = 0, bytesRead = 0;
    unsigned char mask = 0x3F; //Mask used to keep bits interesting for analysis
    uint32_t codepoint = 0; //A codepoint must be an unsigned 32 bit integer

    //--------------------File-Reading--------------------
    while ((elem = fgetc(ptr)) != EOF) {
        //Stores the character in the buffer
        bufferCP[bytesRead++] = (unsigned char) elem;

        if (bytesRead == 4) { //A codepoint is ready to be managed

            //Builds a codepoint from the buffer. Reads it in BIG ENDIAN.
            for(int j=3; j>=0; j--) {
                    codepoint <<= 8;
                    codepoint |= bufferCP[j];
            }
            //Searches the position of the most significant bit
            double logRes = (log(codepoint)/log(2)) + 1;
            int bitPos = (int) logRes;

            //--------------------UTF8-Encoding--------------------
            if (bitPos <= 7) {
                bufferOut[0] = (unsigned char) codepoint; //No need to manage this codepoint
                fwrite(bufferOut, 1, 1, out);

            } else if (bitPos <= 11) {
                bufferOut[0] = (codepoint >> 6) | 0xC0;
                bufferOut[1] = (codepoint & mask) | 0x80;
                fwrite(bufferOut, 1, 2, out);

            } else if (bitPos <= 16) {
                bufferOut[0] = (codepoint >> 12) | 0xE0;
                for(int i=1; i<3; i++)
                    bufferOut[i] = ((codepoint >> 6*(2-i)) & mask) | 0x80;
                fwrite(bufferOut, 1, 3, out);

            } else if (bitPos <= 21) {
                bufferOut[0] = (codepoint >> 18) | 0xF0;
                for(int i=1; i<4; i++)
                    bufferOut[i] = ((codepoint >> 6*(3-i)) & mask) | 0x80;
                fwrite(bufferOut, 1, 4, out);

            } else if (bitPos <= 26) {
                bufferOut[0] = (codepoint >> 24) | 0xF8;
                for(int i=1; i<5; i++)
                    bufferOut[i] = ((codepoint >> 6*(4-i)) & mask) | 0x80;
                fwrite(bufferOut, 1, 5, out);

            } else if (bitPos <= 32) {
                if (bitPos == 32)
                    bufferOut[0] = (codepoint >> 30) | 0xFE; //UTF8-encoding first byte would be: 11111111?
                else
                    bufferOut[0] = (codepoint >> 30) | 0xFC;

                for(int i=1; i<6; i++)
                    bufferOut[i] = ((codepoint >> 6*(5-i)) & mask) | 0x80;
                fwrite(bufferOut, 1, 6, out);
            }

            bytesRead = 0; //Variable reset
        }
    }

}


texts:
I have a macro that makes comparisons and then this macro exports all of the changes based on if the information doesn't match. I have it so that each column gets their own worksheet in the new workbook. I am using 7 different counting integers and it takes a very long time because I am exporting over 60k rows.

Question: is there a faster way to execute this code? Can a UDF be used? if so how?

Dim ws As Worksheet
Dim wb2 As Workbook
Set wb = Application.Workbooks("Total Database Update_WORKING.xlsm")
Set ws = wb.Worksheets("Results")
Set wb2 = Application.Workbooks.Open("C:\Import Update.xlsx")
    i = 2
    ii = 2
    iii = 2
    iiii = 2
    iiiii = 2
    iiiiii = 2
    iiiiii = 2
    k = 2
    wb2.Activate
    Do While ws.Cells(k, 1) <> ""
        If ws.Cells(k, 4) = "No Match" Then
            wb2.Worksheets("AD UPDATE").Cells(i, 1) = ws.Cells(k, 1)
            wb2.Worksheets("AD UPDATE").Cells(i, 2) = ws.Cells(k, 2)
            i = i + 1
        End If
        If ws.Cells(k, 7) = "No Match" Then
            wb2.Worksheets("SENIOR UPDATE").Cells(ii, 1) = ws.Cells(k, 1)
            wb2.Worksheets("SENIOR UPDATE").Cells(ii, 2) = ws.Cells(k, 5)
            ii = ii + 1
        End If
        If ws.Cells(k, 10) = "No Match" Then
            wb2.Worksheets("ID UPDATE").Cells(iii, 1) = ws.Cells(k, 1)
            wb2.Worksheets("ID UPDATE").Cells(iii, 2) = ws.Cells(k, 8)
            iii = iii + 1
        End If
        If ws.Cells(k, 13) = "No Match" Then
            wb2.Worksheets("MINOR UPDATE").Cells(iiii, 1) = ws.Cells(k, 1)
            wb2.Worksheets("MINOR UPDATE").Cells(iiii, 2) = ws.Cells(k, 11)
        End If
        If ws.Cells(k, 16) = "No Match" Then
            wb2.Worksheets("MAJOR UPDATE").Cells(iiii, 1) = ws.Cells(k, 1)
            wb2.Worksheets("MAJOR UPDATE").Cells(iiii, 2) = ws.Cells(k, 14)
            iiii = iiii + 1
        End If
        If ws.Cells(k, 19) = "No Match" Then
            wb2.Worksheets("CAP UPDATE").Cells(iiiii, 1) = ws.Cells(k, 1)
            wb2.Worksheets("CAP UPDATE").Cells(iiiii, 2) = ws.Cells(k, 17)
            iiiii = iiiii + 1
        End If
        If ws.Cells(k, 22) = "No Match" Then
            wb2.Worksheets("PL UPDATE").Cells(iiiiii, 1) = ws.Cells(k, 1)
            wb2.Worksheets("PL UPDATE").Cells(iiiiii, 2) = ws.Cells(k, 20)
            iiiiii = iiiiii + 1
        End If
        k = k + 1
    Loop

    wb2.Save
    Sleep (1000)
    wb2.Close SaveChanges:=True
    wb.Activate
End Sub