Computational verification of Collatz conjecture using OpenCL This code review request follows my previous request Computational verification of Collatz conjecture.
Unlike the previous program (which was designed for the CPU), this code should run on modern GPUs.
For this purpose, I chose OpenCL as a programming language.

Prerequisites

Since the verification program needs to deal with 128-bit arithmetic, the first thing I was forced to solve is the availability of 128-bit integer type.
So far, I rely on the __int128 compiler extension:

typedef unsigned __int128 uint128_t;

At this point my first question arises:
How efficient (in terms of performance) is this solution?
I also guess it may not be very portable, right?

For completeness, the

#define UINT128_MAX (~(uint128_t)0)

is maximum value for an object of type uint128_t.

I also needed to calculate the number of trailing zeros (ctz), which I solved as follows:

size_t ctzl(unsigned long n)
{
    return 63 - clz(n & -n);
}

size_t ctzu128(uint128_t n)
{
    size_t a = ctzl(n);

    if (a == ~0UL) {
        return 64 + ctzl(n>>64);
    }

    return a;
}

My concern here is that this could be implemented more simply.

The last building block I need is the n-th power of three:

uint128_t pow3(size_t n)
{
    uint128_t r = 1;
    uint128_t b = 3;

    while (n) {
        if (n & 1) {
            r *= b;
        }
        b *= b;
        n >>= 1;
    }

    return r;
}

All \$ 3^n \$ for \$ n < 81 \$ fit the uint128_t type.
Therefore I have the follow macro defined.

#define LUT_SIZE128 81

Code

My code verifies the convergence of the Collatz problem using this algorithm.
The range of the size 2^task_size numbers starting at task_id * 2^task_size is evenly divided among threads.
The threads do not communicate with each other.
When the calculation is complete, each thread stores their results (partial checksum, number of uint128_t overflows) into global memory.
My current solution is as follows:

__kernel void worker(
    __global unsigned long *overflow_counter,
    __global unsigned long *checksum_alpha,
    unsigned long task_id,
    unsigned long task_size,
    unsigned long task_units)
{
    unsigned long private_overflow_counter = 0;
    unsigned long private_checksum_alpha = 0;
    size_t id = get_global_id(0);

    uint128_t lut[LUT_SIZE128];

    unsigned long i;
    for (i = 0; i < LUT_SIZE128; ++i) {
        lut[i] = pow3(i);
    }

    uint128_t n_     = ((uint128_t)task_id << task_size) + ((uint128_t)(id + 0) << (task_size - task_units)) + 3;
    uint128_t n_sup_ = ((uint128_t)task_id << task_size) + ((uint128_t)(id + 1) << (task_size - task_units)) + 3;

    for (; n_ < n_sup_; n_ += 4) {
        uint128_t n = n_, n0 = n_;

        do {
            n++;
            size_t alpha = ctzu128(n);
            private_checksum_alpha += alpha;
            n >>= alpha;

            if (n > UINT128_MAX >> 2*alpha || alpha >= LUT_SIZE128) {
                private_overflow_counter++;
                break;
            }

            n *= lut[alpha];

            n--;
            n >>= ctzu128(n);

            if (n < n0) {
                break;
            }
        } while (1);
    }

    overflow_counter[id] = private_overflow_counter;
    checksum_alpha[id] = private_checksum_alpha;
}

I see an acceleration of more than two orders of magnitude compared to the CPU implementation.
My main concern (about the performance) here is that the adjacent threads do not go the same code path (the control flow is not coherent for the threads of a processor).
However, I am not sure how big this problem actually is.

The lookup table lut should be hard-coded in the source code and defined outside the kernel function as a __constant space global variable. As it is now, every thread would have to recalculate the entire table. Also they take too much space for thread's private memory space. Alternately, maybe pre-calculate it on the host and pass it to the kernel in a __constant space argument.
The outer for loop should be reformulated so that every thread has the same values for the counter variables, and the number of iterations is a compile-time constant. Then applying #pragma unroll may accelerate it.
When code diverges (on NVIDIA), it can become as slow as if the diverging section (i.e. the inner do loop) on threads within each warp (= 32 adjacent threads) were executed sequentially. Maybe somehow change it so that there is no divergence inside warps.

Edit:

Maybe hardcoding the LUT and putting it into __global memory instead. The GPU would put it into global memory anyways because it is too large for the per-thread private memory. And like that the values don't have to be calculated inside the kernel.
It that does not accelerate it, maybe make a copy of it in __local memory (per work group). And copy it from global to local inside the kernel. Then use the shared lut.
Do this copy in a coalesced manner, using multiple work items. For example on NVIDIA, each n'th work item of 32 must access the n'th item of 32 from the global memory table.
Take the first (always-executed) iteration of the inner do loop outside the loop, and make it execute the next iterations (in a loop), only if it is needed for at least one work item the subgroup, using the subgroup functions.
Use an OpenCL profiler (one exists for AMD) to see where the performance losses are. Or port it to CUDA, and use the NVidia Visual Profiler (from the CUDA SDK).
Pre-calculate if 128 bit integers are needed for each work item, or if 64 bit (or 32 bit) are sufficient. Then only use 128 bit integers when any work item in the subgroup needs it.


The 101 Pictures Gallery I am working on a website for a group of artists and want to know of a way to make my code cleaner and my website run faster. I am working on the gallery page, and there are over 100 pictures I want to show. I have been entering them in as shown below, but I know there must be a better way to try and do this, but I just need some help.

/*****************************
    PIC GALLERY
*****************************/

.gallerySize {
  /*height: 25%;*/
  /* overflow-x:30%;*/
}
.galleryContainer {
  margin-top: 5%;
  width: 90%;
  overflow-x: scroll;
  overflow-y: hidden;
  /*height: 15%;*/
  /*margin: 2%;*/
  /*height: 10%;*/
}
. .galleryContainer img {
  height: 171px;
  width: 180px;
}
.profile {
  margin-top: 5%;
}
.galleryContainer img {
  height: 300px;
  width: 400px;
}
.fullDrawing img {
  height: 117.273px;
  width: 1042.273px;
}
.topImg {
  margin: 5% 2% 0% 5%;
}
.list-inline {
  white-space: nowrap;
}
.galleryContainer > .row [class*="col-lg"],
.galleryContainer > .row [class*="col-md"],
.galleryContainer > .row [class*="col-md"] {
  float: none;
  display: inline-block;
  white-space: normal;
  vertical-align: top;
}
#main-content > .row {
  overflow-x: scroll;
  overflow-y: hidden;
  white-space: nowrap;
}

Image gallery.
To be completely honest, if you're wanting to make an image gallery, you would save yourself a lot of time and trouble by using Wordpress.org
Example: http://psychoproductions.net/psychographics/

That said...
Assuming your images are stored server-side, and you really want to code this yourself, unless you want to type every image URL one by one, you may want to look into a server side language that can scan through your image folder(s), parse the paths and wrap it into HTML tags. It really varies what you can do depending on your server environment...

A few other things

There's no point declaring class is there is not one to assign to it.
Another example:

alt="..." may as well be left out, if an ellipsis will be its content. Why not class="something meaningful"?


Recursive java.util.Properties references Background

A subclass of java.util.Properties attempts to recursively resolve configured properties:

prop_a=Prop A is ${prop_b}
prop_b=Prop B is ${prop_c}
prop_c=Prop C.

After reading the file, prop_a should be Prop A is Prop B is Prop C.

Property Reference Semantics

Nest properties are not allowed, making the following invalid:

${property_name_a ${property_name_b}}

A valid property is:

name=Text for ${property_a}, followed by ${property_b}.

Source Code

Code that handles parsing:

/**
 * This will throw an IllegalArgumentException if there are mismatched
 * braces.
 *
 * @param s - The string with zero or more ${} references to resolve.
 * @return The value of the string, with all substituted values made.
 */
public String resolve( String s ) {
  StringBuffer sb = new StringBuffer( 256 );
  Stack<StringBuffer> stack = new Stack<StringBuffer>();
  int len = s.length();

  for( int i = 0; i < len; i++ ) {
    char c = s.charAt( i );

    switch( c ) {
      case '$': {
        if( i + 1 < len && s.charAt( i + 1 ) == '{' ) {
          stack.push( sb );
          sb = new StringBuffer( 256 );
          i++;
        }
        break;
      }

      case '}': {
        if( stack.isEmpty() ) {
          throw new IllegalArgumentException( "unexpected '}'" );
        }

        String name = sb.toString();

        sb = stack.pop();
        sb.append( super.getProperty( name, null ) );
        break;
      }

      default: {
        sb.append( c );
        break;
      }
    }
  }

  if( !stack.isEmpty() ) {
    throw new IllegalArgumentException( "missing '}'" );
  }

  return sb.toString();
}

Question

How would you simplify the code and detect/avoid infinite recursion?

You say "recursively resolve", but in fact resolve is not recursive. It returns Prop A is Prop B is ${prop_c} instead of Prop A is Prop B is Prop C, if you call it with Prop A is ${prop_b}. If you call it with prop_a, as advertised, it returns prop_a.
You allow nested ${} in you syntax. What is your semantics of it?
Do you have any test cases for this. The bug and Unclear semantics of nested parens for instance, strongly indicate you should have.
Since you access the super class through its public interface, namely the the singe call to the super.getProperty you could instead pass the properties as a parameter. It will improve usability. Then you can make the static, if you wish. Since the method does not refer to any instance members otherwise.
StringBuffer and Stack have built in synchronization, whose overhead you can avoid by using StringBuilder and ArrayList.

Here is the test code to save others some effort:

import java.util.Properties;
import java.util.Stack;

public class Main {
    public static String resolve(Properties props,  String s ) {
          StringBuffer sb = new StringBuffer( 256 );
          Stack<StringBuffer> stack = new Stack<StringBuffer>();
          int len = s.length();

          for( int i = 0; i < len; i++ ) {
            char c = s.charAt( i );

            switch( c ) {
              case '$': {
                if( i + 1 < len && s.charAt( i + 1 ) == '{' ) {
                  stack.push( sb );
                  sb = new StringBuffer( 256 );
                  i++;
                }
                break;
              }

              case '}': {
                if( stack.isEmpty() ) {
                  throw new IllegalArgumentException( "unexpected '}'" );
                }

                String name = sb.toString();

                sb = stack.pop();
                sb.append( props.get(name) );
                break;
              }

              default: {
                sb.append( c );
                break;
              }
            }
          }

          if( !stack.isEmpty() ) {
            throw new IllegalArgumentException( "missing '}'" );
          }

          return sb.toString();
        }

    public static void main(String[] args) {
        Properties props = new Properties();

        props.setProperty("prop_a", "Prop A is ${prop_b}");
        props.setProperty("prop_b", "Prop B is ${prop_c}");
        props.setProperty("prop_c", "Prop C.");

        System.out.println(resolve(props, "Prop A is ${prop_b}"));
        System.out.println(resolve(props, "prop_a"));
        System.out.println(resolve(props, "${prop_a}"));
        System.out.println(resolve(props, "${${prop_b}}"));
    }
}


Database abstraction class in C# (version 2) Below is my revised version of this code after I acted on the suggestions:

Database abstraction class in C#

class DatabaseAccess : IDisposable
{
    private SqlConnection connection;
    private SqlCommand alreadyDownloadedCommand;
    private SqlCommand markedDownloadedCommand;
    // *FieldSize variables match the size of the field in the database.
    private const int FileNameFieldSize = 80;
    private const int AppTypeFieldSize = 2;
    private const int UserNameFieldSize = 8;

    public DatabaseAccess(string dbconnectionstring)
    {
        connection = new SqlConnection(dbconnectionstring);

        connection.Open();
        alreadyDownloadedCommand = getAlreadyDownloadedCommand(connection);
        markedDownloadedCommand = getMarkedDownloadedCommand(connection);
    }

    public void Dispose()
    {
        alreadyDownloadedCommand.Dispose();
        markedDownloadedCommand.Dispose();
        connection.Dispose();
    }
    private void ValidateFieldSize(string fielddata, int fieldsize)
    {
        if (fielddata.Length > fieldsize)
        {
            throw new ArgumentOutOfRangeException("Parameter size too large");
        }
    }
    private SqlCommand getMarkedDownloadedCommand(SqlConnection connection)
    {
        const string sql =
        @"insert into dbo.InspectionsDownloaded(FileName)
          values (@FileName)
        ";
        SqlCommand command = new SqlCommand(sql, connection);
        command.Parameters.Add(new SqlParameter("@FileName", System.Data.SqlDbType.VarChar, FileNameFieldSize));
        command.Prepare();
        return command;
    }

    private SqlCommand getAlreadyDownloadedCommand(SqlConnection connection)
    {
        const string sql =
        @"select count(*) as numDownloaded
            from dbo.InspectionsDownloaded id
           where id.FileName = @FileName
        ";
        SqlCommand command = new SqlCommand(sql, connection);
        command.Parameters.Add(new SqlParameter("@FileName", System.Data.SqlDbType.VarChar, FileNameFieldSize));
        command.Prepare();
        return command;
    }
    public bool IsAlreadyDownloaded(string filename)
    {
        int rowCount;
        ValidateFieldSize(filename, FileNameFieldSize);
        alreadyDownloadedCommand.Parameters[0].Value = filename;
        rowCount = (Int32)alreadyDownloadedCommand.ExecuteScalar();
        return (rowCount != 0);
    }
    public int MarkDownloaded(string filename)
    {
        ValidateFieldSize(filename, FileNameFieldSize);
        markedDownloadedCommand.Parameters[0].Value = filename;
        return(markedDownloadedCommand.ExecuteNonQuery());
    }
}

Some organization nitpicks first :

Your class file should start with the const
Even your private functions should be PascalCased meaning getAlreadyDownloadedCommand should be GetAlreadyDownloadedCommand
Your public methods should appear before your private methods IMO

You don't implement the Dispose pattern correctly. You should check this article but basically, to properly dispose your object, the pattern is the following :

public void Dispose()
{
    Dispose(true);
    GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
    if (disposing){
        if (connection != null) connection.Dispose();
        //etc.
    }
}

I must say I'm not 100% why we need to do it this way, but this is how it is supposed to be implemented according to Microsoft's documentation and developer tools like Resharper.

When you have a public method/constructor/etc, you should always check your input for null. Don't trust the user of your code, even if it's yourself! If you check your inputs for null, you'll know very fast if your input is null, instead of having a weird exception that tells you little to nothing.

public DatabaseAccess(string dbconnectionstring)
{
    if(String.IsNullOrWhitespace(dbconnectionstring) throw new ArgumentNullException(nameof(dbconnectionstring));

    connection = new SqlConnection(dbconnectionstring);

    connection.Open();
    alreadyDownloadedCommand = getAlreadyDownloadedCommand(connection);
    markedDownloadedCommand = getMarkedDownloadedCommand(connection);
}


Very basic Hangman game in Python I'm trying to learn Python, so I made this project. I think there are problems with the code, but I don't know how to improve it.

import os
import random
clear = lambda: os.system('cls')
def game():
    def printOutCorrectGuess():
        print('Correct! There is/are ', lettersInWord, 'of this letter in this word.')
    def printOutStars():
        print('Currently revealed of the word: ', ''.join(stars))
    def printOutVictory():
        print('You won!')
        print('The word was ', theWord)
        print('Press enter to quit...')
    def printOutWords():
        print('You guessed these letters : ', words)
    def printOutLifes():
        print('You have ', lives, ' more lives.')
    print("Let's start guessing!")
    correctLetters = 0
    lives = 10
    words = []
    stars = list('*' * len(theWord))
    while True:
        guess = str(input('Please give me a letter: '))
        if len(guess) > 1 or len(guess) < 1:
            clear()
            print('I need 1 and letter, thanks.')
            printOutStars()
        elif guess in words:
            clear()
            print('You already wrote this letter.')
            printOutStars()
        elif guess in theWord:
            theStart = 0
            theEnd = 1
            lettersInWord = 0
            letterPosition = []
            for i in range(len(theWord)):
                tempWords = theWord.find(guess, theStart, theEnd)
                theStart = theStart + 1
                theEnd = theEnd + 1
                if tempWords >= 0:
                    lettersInWord = lettersInWord + 1
                    letterPosition.append(i + 1)
                    stars[i] = guess
            correctLetters = correctLetters + lettersInWord
            if correctLetters == len(theWord):
                clear()
                printOutVictory()
                if input() == '':
                    break
                else:
                    break
            clear()
            printOutCorrectGuess()
            printOutStars()
        else:
            lives = lives - 1
            if lives == 0:
                clear()
                print('You lost.')
                print('The word was: ', theWord)
                print('Press enter to quit the game')
                if input() == '':
                    break
                else:
                    break
            clear()
            printOutLifes()
            printOutStars()
        if guess not in words and len(guess) == 1:
            words.append(guess)
        printOutWords()
def welcome():
    print('Now we will play the classic Hangman game, but for this time without drawing it.')
    print('1 or 2 player game mode? Write 1 or 2 please.')
welcome()
while True:
    try:
        gameMode = int(input('INPUT: '))
    except ValueError:
        print('Write 1 or 2 please.')
        continue
    if gameMode == 1:
        words = ['letters', 'sloppy', 'bedroom', 'jazzy', 'discovery', 'wistful', 'unadvised', 'help', 'line', 'shake', 'mend', 'time', 'attempt', 'dare', 'straw', 'destroy', 'health', 'shiny']
        theWord = random.choice(words)
        clear()
        game()
        break
    elif gameMode == 2:
        theWord = str(input('Please write here the word: '))
        clear()
        game()
        break
    elif gameMode < 1 or gameMode > 2:
        print('Write 1 or 2 please.')

Brand new to programming myself, but here's what I've got for you.

This is just different formatting to not call print so many times:

def printOutVictory():
    print('You won!')
    print('The word was ', theWord)
    print('Press enter to quit...')

Alternate format:

# snake_case over camelCase in python
def print_out_victory():
    message = "You won!\nThe word was " + theWord
    message += "\nPress enter to quit..."
    print(message)

You could clean up your main body by creating game_mode_1 and game_mode_2 outside of main and calling them for your if and elif and then just reduce your last elif to an else statement:

def game_mode_1():
    words = ['letters', 'sloppy', 'bedroom', 'jazzy', 'discovery', 'wistful', 'unadvised', 'help', 'line', 'shake', 'mend', 'time', 'attempt', 'dare', 'straw', 'destroy', 'health', 'shiny']
    theWord = random.choice(words)
    clear()
    game()
    break

def game_mode_2():
    theWord = str(input('Please write here the word: '))
    clear()
    game()
    break

You would then have:

#using game_mode in place of gameMode, snake_cases again
def main():
    welcome()
    while true:
        try:
            game_mode = int(input('INPUT: '))
        except ValueError:
            print('Write 1 or 2 please.')
            continue

        if game_mode == 1:
            game_mode_1()

        elif game_mode == 2:
            game_mode_2()

        else:
            print("Write 1 or 2 please.")

Furthermore you could reduce main by creating user_selection

def user_selection():
    while true:
        try:
            game_mode = int(input('INPUT: '))
        except ValueError:
            print('Write 1 or 2 please.')
            continue

        if game_mode == 1:
            game_mode_1()

        elif game_mode == 2:
            game_mode_2()

        else:
            print("Write 1 or 2 please.")

And then your main would look like this

def main():
    welcome()
    user_selection()

Then run main adding if __name__ == '__main__': guard, this will prevent main from running if you import this script into another script

if __name__ == '__main__':
    main()


Project Euler 1 (sum of multiples of 3 or 5 under 1000) I solved this a while ago. In the moment I solved it I was learning Scheme and, well, I still am. I'm not looking at the best solution (I searched for it and coded it already, in Python), what I want is to improve this code, maintaining the same (brute) approach, so I can learn some best practices and tips in Scheme. Maybe there are some suggestions on how I wrote the routines, for example.

#lang racket
(define (check x) (or (= (modulo x 3) 0)
                      (= (modulo x 5) 0)))

(define (count-check x) (if (check x)
                            x
                            0))

(define (sum-multiples-rec actual limit acc) (if (< actual limit)
                                                 (sum-multiples-rec (+ actual 1) limit (+ (count-check actual) acc))
                                                 acc))

(define (sum-multiples lower-limit upper-limit) (sum-multiples-rec lower-limit upper-limit 0))

(sum-multiples 0 1000)

So, any time you want to write a "helper" recursive function, the standard way to write that is to use a named let. So here's how I might restructure your program (but keeping the same algorithm):

(define (sum-multiples start end)
  (let loop ((sum 0)
             (i start))
    (cond ((>= i end) sum)
          ((or (zero? (modulo i 3))
               (zero? (modulo i 5)))
           (loop (+ sum i) (add1 i)))
          (else (loop sum (add1 i))))))

Named let is a macro, which in this case expands to the same expression as:

(define (sum-multiples start end)
  ((letrec ((loop (lambda (sum i)
              (cond ((>= i end) sum)
                    ((or (zero? (modulo i 3))
                         (zero? (modulo i 5)))
                     (loop (+ sum i) (add1 i)))
                    (else (loop sum (add1 i)))))))
     loop)
   0 start))

(I've taken the liberty of using (add1 i) instead of (+ i 1) since you're using Racket.)

Here's a version that's even more Rackety, using for/sum to accumulate the sum rather than using a manual loop:

(define (sum-multiples start end)
  (for/sum ((i (range start end)))
    (if (or (zero? (modulo i 3))
            (zero? (modulo i 5)))
        i 0)))


Django view to display weather measurements for selected location and dates I'm a crop scientist and am currently building a web app that collects weather data then generates and displays modeled agriculturally relevant output. My question centers around an increasingly complex Django view and a desire to learn about a potentially superior implementation if one exists.

I have a Django view that allows the user to select a location from a Google maps layer when the page in initially rendered. Once a selection is made the view will check for location in URL request and render the template for an individual location. Once the individual location page is rendered the user has further options, like selecting a date range for desired data. The selection is made and submitted at which point the view will check for selectstartdate and selectenddate in the URL request and, if present, will render the single location view with data for the desired range. As I plan on adding several other interactive features, I'm beginning to realize that this view is going to get complicated quickly.

Is an intricate, conditionally driven Django view a reasonable implementation or is there a better way to do something like this?

Code

import time
from django.shortcuts import render
from locations.models import Location
from metload.models import Obsset
from datetime import datetime
from ETo_py.eto import EToEstimator

def single_location(**kwargs):

    """

    Process a bunch of weather parameters and return modeled output.
    This function is not a view, just a helper function for processing data.
    I have left out the lengthy inner workings of this function.

    """

    return context

def index(request):

    print('hello')

    # Single location without date range selected.
    if 'location' in request.GET and \
            'selectstartdate' not in request.GET and \
            'selectenddate' not in request.GET:

        location = request.GET['location']
        selectstartdate = None
        selectenddate = None

        params = {
            'location': location,
            'selectstartdate': selectstartdate,
            'selectenddate': selectenddate,
        }

        context = single_location(**params)

        return render(request, 'locations/location.html', context)

    # Single location with a date range selected.
    elif 'location' in request.GET and  \
            'selectstartdate' in request.GET and \
            'selectenddate' in request.GET:

        location = request.GET['location']
        selectstartdate = request.GET['selectstartdate']
        selectenddate = request.GET['selectenddate']

        print(selectstartdate, selectenddate)

        params = {
            'location': location,
            'selectstartdate': selectstartdate,
            'selectenddate': selectenddate,
        }

        context = single_location(**params)

        return render(request, 'locations/location.html', context)

    # Initial page.
    else:
        # Get locations from db.
        locations = Location.objects.all()

        # Prepare locations dictionary for google map layer js.
        locs_ls = []
        for loc in locations:
            lat = loc.latitude
            lon = loc.longitude
            name = loc.name
            site_id = loc.site_id

            locs_ls.append({'site_id': site_id, 'name': name, 'lat': lat, 'lon': lon})

        context = {
            'locs_ls': locs_ls,
        }

        return render(request, 'locations/index.html', context)

Not sure this will solve all your problems, but you can give those GET parameters default values in case they don't exist. In my example below, they are given the default value of None. This should simplify things a bit.

def index(request):

    print('hello')

    location = request.GET.get('location', None)
    selectstartdate = request.GET.get('selectstartdate', None)
    selectenddate = request.GET.get('selectenddate', None)

    if location is not None or selectstartdate is not None and selectenddate is not None:

        # Get locations from db.
        locations = Location.objects.all()

        # Prepare locations dictionary for google map layer js.
        locs_ls = []
        for loc in locations:
            lat = loc.latitude
            lon = loc.longitude
            name = loc.name
            site_id = loc.site_id

            locs_ls.append({'site_id': site_id, 'name': name, 'lat': lat, 'lon': lon})

        context = {
            'locs_ls': locs_ls,
        }

        return render(request, 'locations/index.html', context)

    else:

        params = {
            'location': location,
            'selectstartdate': selectstartdate,
            'selectenddate': selectenddate,
        }

        context = single_location(**params)

        return render(request, 'locations/location.html', context)


Can I use IDisposable by treating abstract concepts such as colors as resources? There are the following class:

class ConsoleForegroundColor : IDisposable
{
    public ConsoleForegroundColor(ConsoleColor color)
    {
        OriginalColor = Console.ForegroundColor;
        Console.ForegroundColor = color;
    }

    public ConsoleColor OriginalColor { get; set; }

    public void Dispose()
    {
        Console.ForegroundColor = OriginalColor;
    }
}

This has the following uses:

Console.WriteLine("display with default color");
using (new ConsoleForegroundColor(ConsoleColor.Green))
{
    Console.WriteLine("display with green");
}
Console.WriteLine("display with default color");

This source code changes the console font color to the specified color when creating an instance and restores it when discarded.

I think it's possible to think of the colors drawn on the console as resources, so I don't think it's so different from the original use of IDisposable. Is the use of IDisposable like this source code a source that is difficult to understand and maintain? If you have a better think, please tell me.

Readability-wise, this is fine, though I prefer ConsoleForegroundBrush, because it keeps the theme of Drawing library, which is well known, and in the real world, you can definitely destroy/dispose brushes / put them down, but disposing an actual colour would be like... changing the color of the text back to black?

The main issue is there is a performance impact that comes with using, as this is really a try/finally block under the hood, and so you are adding try/finally around your code which may slow things down unnecessarily, particularly if there's other non-console logic in the using block.

The using block can also sometimes hide other errors, because it's a try/finally block. If you put a using block inside a try() block and your dispose method of the ConsoleForegroundColour class throws an exception (such as if the Console became null somehow), you will probably only see the Dispose() exception, and not the other exception. This situation is highly unlikely to happen in your class though.


all appointment based on interval distance Currently with a given Date and an Interval in Day, I can calculate all of the appointment in the future. for example if you are supposed to have an appointment each 30 day starting '2020-09-24', all the future appointments would be calculated like this.
--Source Table Creation
DROP TABLE IF EXISTS #DimDate

CREATE TABLE #DimDate ( id INT PRIMARY KEY IDENTITY, GregorianDate DATE)
--Sample Data
INSERT INTO #DimDate(GregorianDate)
SELECT  TOP 2000 DATEADD(DAY, ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) , GETDATE())
FROM    sys.all_objects

--Calculation Code

;WITH cte_name --30.4375
AS ( SELECT GregorianDate,DATEADD(DAY,30,GregorianDate) next30, ((ROW_NUMBER() OVER (ORDER BY GregorianDate ASC)-1) + 30) % 30 AS r
     FROM   #DimDate
     WHERE GregorianDate >='2020-09-24'
)
SELECT  *
FROM    cte_name
WHERE r=0

Calculation is based on residual of this part the code ((ROW_NUMBER() OVER (ORDER BY GregorianDate ASC)-1) + 30) % 30 AS r, if residual is 0 then we are at the next interval.
What I am looking for

Is there any problem not yet considered?
Is there any other way? ( some playful way like window aggregate!)

Thank you

Update:
Some clarification:
I have a Date Dimension table in which all the related Calendar Conversion is stored like HijriCalendar, PersianCalendar and other needed data.
That is why even though generally speaking calculating nth appointment using (n * 30 (interval distance) ) + start is faster but overall because of other data needed from Dimension it'd be the same. (Greatly appreciated)

You can greatly simplify things as well as wildly improve performance by using something like the following...
DECLARE
    @start_date date = '2020-09-24',
    @interval_in_days int = 30,
    @appt_count int = 100;

WITH
    cte_n1 (n) AS (SELECT 1 FROM (VALUES (1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) n (n)),   -- 10
    cte_n2 (n) AS (SELECT 1 FROM cte_n1 a CROSS JOIN cte_n1 b),                             -- 100
    cte_Calendar (dt) AS (
        SELECT TOP (@appt_count)
            DATEADD(DAY, (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) -1) * @interval_in_days, @start_date)
        FROM
            cte_n2 a CROSS JOIN cte_n2 b                                                    -- 10,000
        )
SELECT
    c.dt
FROM
    cte_Calendar c;

You can even even create an inline table function to make life a little easier...
SET QUOTED_IDENTIFIER ON;
GO
SET ANSI_NULLS ON;
GO

CREATE FUNCTION dbo.tfn_CalculateAppointmentDates
/* ===================================================================
09/22/2020, Created: Calculates all future appointment dates based
                on a start date, specified interval and number of
                requested appointments.
=================================================================== */
--===== Define I/O parameters
(
    @start_date date,
    @interval_in_days int,
    @appt_count int
)
RETURNS TABLE WITH SCHEMABINDING AS
RETURN
    WITH
        cte_n1 (n) AS (SELECT 1 FROM (VALUES (1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) n (n)),   -- 10
        cte_n2 (n) AS (SELECT 1 FROM cte_n1 a CROSS JOIN cte_n1 b),                             -- 100
        cte_Calendar (dt) AS (
            SELECT TOP (@appt_count)
                DATEADD(DAY, (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) -1) * @interval_in_days, @start_date)
            FROM
                cte_n2 a CROSS JOIN cte_n2 b                                                    -- 10,000
            )
    SELECT
        c.dt
    FROM
        cte_Calendar c;
GO


Loading visible images I am using this code to get images to load only if they are visible. However, it seems to be slow with thousands of images, even if they are not rendered.

function getViewportHeight() {
    if(window.innerHeight) {
        return window.innerHeight;
    }
    else if(document.body && document.body.offsetHeight) {
        return document.body.offsetHeight;
    }
    else {
        return 0;
    }
}
function inView(elem, nearThreshold) {
    var viewportHeight = getViewportHeight();
    var scrollTop = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
    var elemTop = elem.offset().top;
    var elemHeight = elem.height();
    nearThreshold = nearThreshold || 0;
    if((scrollTop + viewportHeight + nearThreshold) > (elemTop + elemHeight)) {
        return true;
    }
    return false;
}

function loadVisibleImages() {
    jQuery('img[data-src]').each(function() {
        if(jQuery(this).is(':visible')) {
            if(inView(jQuery(this), 100)) {
                this.src = jQuery(this).attr('data-src');
            }
        }
    });
}

jQuery(window).scroll(function() {
    loadVisibleImages();
});

I am using this code to render images:

<img src="" data-src="http://placehold.it/400x400" alt="" width="400" height="400">

Everything works, but how can I optimize it?

Here are a few callouts/suggestions:

You should throttle your code on the scroll event.  That way the event doesn't fire thousands of times.  You can read up this article for more information.  A basic example would be:

//which makes the `scroll` event fire every 250ms.
$(window).scroll( $.throttle( 250, loadVisibleImages) );

You probably don't need to get the viewport size every time through the loop.  This should be called once at the start and then only on the resize event.  Otherwise, it never changes so there is no need to re-calculate it.

//which makes the `resize` event fire every 250ms.
$(window).resize( $.throttle( 250, getViewportHeight) );

You can add a class to all the images that have already been processed and exclude them from your selector.  This way you are only dealing with new images.
For example:

function loadVisibleImages() {
  jQuery('img[data-src]').not('.processed').each(function() {
    if(jQuery(this).is(':visible')) {
        if(inView(jQuery(this), 100)) {
            this.src = jQuery(this).attr('data-src').addClass('processed');
        }
    }
  });
}

If you haven't already you should wrap your code in an IIFE so you can create a private scope.  Just take the code below and put it around your existing code.  This would also allow you to use $ for jQuery without worry.

(function( $ ) {
  //your code here
}) ( jQuery );

You could optimize the loop in your loadVisibleImages functions.  See your other question.

Hope that helps!


My attempt at a weighted search in JavaScript I tried to make a weighted search function in JavaScript. I've been improving my JS lately but still not sure about some best practices, wondering if there's any improvements I could make, or if I've ended up doing it completely wrong.

The intention is to keep it quite flexible so it can take an array of filter functions, each with its own weight, it also takes a property on which to sort items with equal weights.

The search function is

function weightedSearch(array, weightedTests, sortProperty) {
        return array.map(function (e) {
            return {
                element: e, weight: weightedTests.map(function (weightedTest) {
                    var testResult = weightedTest.test(e);
                    return testResult * weightedTest.weight;
                }).reduce(function (previousValue, currentValue) { return previousValue + currentValue; }, 0)
            };
        }).filter(function (element) {
            return element.weight > 0;
        }).sort(function (obj1, obj2) {
            //sort first by weight
            if (obj1.weight > obj2.weight) {
                return -1;
            } else if (obj1.weight < obj2.weight) {
                return 1;
            }

            // Else by chosen property
            if (obj1.element[sortProperty] < obj2.element[sortProperty]) {
                return -1;
            } else if (obj1.element[sortProperty] > obj2.element[sortProperty]) {
                return 1;
            } else {
                return 0;
            }
        }).map(function (e) {
            return e.element;
        });
    }

A weighted test is an object of the form:

{test: function(element){}, weight: relativeWeightOfTheTest}

And would be used in a manner such as:

var search = "test";
var results = weightedSearch(arrayToSearch, [
        { test: function (testElement) { return testElement.title.toLowerCase().indexOf(search) >= 0; }, weight: 2 },
        { test: function (testElement) { return testElement.description.toLowerCase().indexOf(search) >= 0; }, weight: 0.5 }
    ], "title")

This would return an array where either the title or description contain "test" and ordered by:

Appearing in title and description
Appearing in title only
Appearing in description only

Any results with the same weighting are then ordered by their title. Obviously this is a simple test case, you could easily have a test that adds up instances of the search string, the test could check it against a list of synonyms before counting etc.

Here's a JSbin with some test data and a quick Knockout usecase.

http://jsbin.com/figay/1/edit?html,js,output

I like it, from a once over:

map, filter, reduce etc. are not the most efficient functions, they make for readable code, not for speedy sorting/searching. See this : http://jsperf.com/arraymap, a simply for loop beats everything else every time, this also goes for filter,reduce etc.
I would change around the first block, there is too much happening horizontally:

return array.map(function (e) {
    return {
        element: e,
        weight: weightedTests.map(function (weightedTest) {
            return weightedTest.test(e) * weightedTest.weight;
        }).reduce(function (previousValue, currentValue) {
            return previousValue + currentValue; }, 0)
     };
 })

The sorting by weight I would do like this:

//sort first by weight if possible
var weightDifference = obj2.weight- obj1.weight;
if (weightDifference) {
  return weightDifference;
}

The last return 0 does not need to be in an else block
Other than that the code runs fine on JsHint, is well commented and easy to follow.


Lock-range code for Google Scripts/Sheets This code locks a range and adds a timestamp. More details below. I'd like to learn how to make this code more efficient (minimize code/variables and reduce redundancy). Any thoughts on the areas I can improve?

function lockEdits(e) {

  // delcare initial col variable
  var colCheck = e.range.getLastColumn();

  // exit function if the col edited was not 11, 17, 20
      if (colCheck != 11 && colCheck != 17 && colCheck != 20) {
        return;
      }

  //  // delcare remaining variables
  var ss = e.source.getActiveSheet();
  var thisRow = e.range.getRow();
  var rngHeight = e.range.getHeight();
  var email = Session.getActiveUser().getEmail();
  var owners = ["owner1@test.com", "owner2@test.com"];
  var checkEmpty = ss.getRange(e.range.getRow(), colCheck).getValue();
  var rejectCheck = ss.getRange(e.range.getRow(), 17).getValue();

  // if change in col 11, enter user email and timestamp, then protect range
  if (colCheck == 11 && checkEmpty !== '') {
    var protection = ss.getRange(thisRow, 2, rngHeight, 10).protect().setDescription('Lock Range:');
    var nEmail = ss.getRange(thisRow, 21, rngHeight, 1);
    var nStamp = ss.getRange(thisRow, 22, rngHeight, 1);
    nEmail.setValue(email); // print email
    nStamp.setValue(new Date()); // print timestamp
    SpreadsheetApp.flush();
    protection.removeEditors(protection.getEditors()); // protect range
    if (protection.canDomainEdit()) {
      protection.setDomainEdit(false);
    }
    protection.addEditors(owners);
    SpreadsheetApp.flush();

    // if change in col 20, enter email and timestamp, then protect range
  } else if (colCheck == 20 && checkEmpty !== '') {
    var protection = ss.getRange(thisRow, 17, rngHeight, 4).protect().setDescription('Lock Range:');
    var vEmail = ss.getRange(thisRow, 23, rngHeight, 1);
    var vStamp = ss.getRange(thisRow, 24, rngHeight, 1);
    vEmail.setValue(email); // print email
    vStamp.setValue(new Date()); // print timestamp
    SpreadsheetApp.flush();
    protection.removeEditors(protection.getEditors()); // protect range
    if (protection.canDomainEdit()) {
      protection.setDomainEdit(false);
    }
    protection.addEditors(owners);
    SpreadsheetApp.flush();

    // if rejection in col 17, enter email and timestamp
  } else if (colCheck == 17 && rejectCheck == "Rejected") {
    var vEmail = ss.getRange(thisRow, 23, rngHeight, 1);
    var vStamp = ss.getRange(thisRow, 24, rngHeight, 1);
    vEmail.setValue(email); // print email
    vStamp.setValue(new Date()); // print timestamp
    SpreadsheetApp.flush();
  }
}

So a few notes on what this does:

This script is set up as an onedit trigger and this sheet is shared with multiple users.

If a user edits a cell in column K (11), then lock that row/range from columns B-K. Then also add the users email in column U and a timestamp in column V.
If a user edits a cell in column T (20), then lock that row/range from columns Q-T. Then also add the users email in column W and a timestamp in column X.
If a user edits a cell in column Q (17) to "Rejected", then just add the users email in column W and a timestamp in column X.

This works as-is, I'm just not sure this is the most efficient way to do it and I'm hoping those of you with more knowledge can help me fine-tune this a bit.

Let me know if any other info would be helpful in sorting this out!

It's not necessary to include SpreadsheetApp.flush() as the last command of the if blocks because there aren't any command to be executed after those blocks and Google Apps Script submits all the changes to the spreadsheet when the script execution ends. If you remove them, you will be saving three code lines.
On some scenarios using one sheet.getDataRange().getValues() is faster than having several sheet.getRange(...).getValue().
Using e.range.columnStart is faster than e.range.getLastColumn();


"Edit distance" UVA programming challenge I'm trying to solve a programming challenge for fun. The programming challenge is here and my solution for the same can be found here.

It currently causes an exceeded time limit.

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;

int a[25001];

bool is_edit_distance_one(string one, string two) {
  if ( one.size() == two.size() ) {
    bool found_diff = false;
    for ( int i = 0 ; i < one.size(); i++ ) {
      if ( found_diff && one[i] != two[i] ) return false;
      else if ( one[i] != two[i] ) found_diff = true;
    }
    return true;
  }

  else if ( (one.size() - two.size()) == 1 && one.find(two) != string::npos )
    return true;
  else if ( (two.size() - one.size()) == 1 && two.find(one) != string::npos )
    return true;

  return false;
}

map<string, bool> generate(string one) {
  map<string, bool> edits;
  string all_chars = "abcdefghijklmnopqrstuvwxyz";

  // Generate all strings by adding another letter
  for ( int i = 0; i <= one.size(); i++ ) {
    for ( int j = 0; j < all_chars.size(); j++ ) {
      // split the string at i
      string temp = one.substr(0, i);
      temp += all_chars[j];
      temp += one.substr(i, one.size()-i);
      edits[temp] = true;
    }
  }

  // Generate all strings by deleting a letter
  for ( int i = 0; i < one.size(); i++ ) {
    string temp = one.substr(0, i);
    temp += one.substr(i+1, one.size()-i);
    edits[temp] = true;
  }

  // Generate all strings by changing a letter
  for ( int i = 0; i < one.size(); i++ ) {
    for ( int j = 0; j < all_chars.size(); j++ ) {
      string temp = one.substr(0, i);
      temp += all_chars[j];
      temp += one.substr(i+1, one.size()-i);
      edits[temp] = true;
    }
  }
  return edits;
}

int main() {
  string s;
  int i = 0;
  int l;
  vector<string> all;
  while ( cin >> s ) {
    a[i] = 0;
    all.push_back(s);

    l = all.size();
    for ( int i = 0; i < l-1; i++ ) {
      map<string, bool> edits = generate(all[l-1]);
      if ( edits[all[i]] ) {
        a[l-1] = max(a[i]+1, a[l-1]);
      }
    }
  }
  sort(a, a+l);
  cout << a[l-1] + 1 << endl;
}

Instead of calculating the edit distance between strings (using is_edit_distance_one), I'm generating a map with all possible edit strings of a string and checking if previous strings are present in it (using the function "generate").

Are there any implementation level mistakes that I'm making that's slowing it down?
Any particular data structure I should/should not be using?
Any other algorithm I could use to speed things up?

Generating all possible single-edit variations of a word results in a huge number of possibilities!  With a 10-letter word, for example, you could add 26 letters in each of 11 positions (before, after, or between letters), remove any of the 10 letters, or modify any of the 10 letters.  That's 26 × 11 + 10 + 25 × 10 = 546 possibilities.  On average, you could trim that in half if you only consider words that come lexicographically later than the input word.  Still, that's a huge inefficiency — enough to rule out that approach altogether.

Your is_edit_distance_one() should take const string & arguments to avoid copying.  The body would be improved if you break down the cases as:

switch (one.size() - two.size()) {
  case -1: // Is one is a subsequence of two (not necessarily consecutive)?
  case +1: // Is two is a subsequence of one (not necessarily consecutive)?
  case 0:  // Check for single-letter difference
  default: return false;
}

The challenge with the is_edit_distance_one() approach is that if you apply it naïvely (comparing every pair of words), your running time will be O(n2), where n is the number of words.

One technique to consider is to take the hint that no word exceeds 16 letters.  Create a char[25001][16] matrix and fill it with '\0' NULs.  You can then treat the NULs just like any other char for the purpose of determining edit distances.

Once you have determined which words are one edit distance apart, the search for the longest stepladder should be similar to the Longest Increasing Subsequence problem.


Using VBA to "apply names" to named range references So there's a known bug in excel where applying named ranges via ribbon doesn't work properly (for 2010 and 2013 and possibly 2016). There was a question about it on superuser - Apply names in excel bugged?. I took a shot at it.

This code will find all of the named ranges in the book and then search a specified range for any cells that have formulas containing a reference tied to the named range's address and replace that with the named range.

This does that, but only works for absolute references when searching as I couldn't figure out a simple way to get the named range's relative reference.

I guess I'm wondering if there's a better way to do this (maybe with a dictionary so I don't need two loops?) and also capture the relative references. Or if there are any properties I didn't use that will do what I want.

Option Explicit
Sub FixNames()

Dim ClctNames As Variant
Set ClctNames = ActiveWorkbook.Names

Dim rngName As String
Dim rngNameLoc As String
Dim strFrmla As String

Dim c As Range
Dim n As Integer

'Define as needed
Dim srchRng As Range
Set srchRng = ActiveSheet.UsedRange

'For each name (n) in the collection
For n = 1 To ClctNames.Count

    'I'm storing the Named Range's name and address as strings to use below
    rngName = ClctNames(n).Name
    rngNameLoc = ClctNames(n).RefersToRange.Address

    '--Should I break this out into a function? If so, at what point?
    For Each c In srchRng
        'We only want to test cells with formulas
        If c.HasFormula = True Then
           'We have to check if the cell contains the current named range's address
           If InStr(1, c.Formula, rngNameLoc, vbTextCompare) <> 0 Then
              'Since these are perfect matches, no need to look for length or location, just replace
              strFrmla = Replace(c.Formula, rngNameLoc, rngName)
              c.Formula = strFrmla
           End If
        End If
    Next
Next

'No error handling should be needed

End Sub

'No error handling should be needed

Except, you never really know what assumptions you're making without realizing. Better safe than sorry.

For example, here's where your procedure might blow up:

Set srchRng = ActiveSheet.UsedRange

You're assuming that the ActiveSheet will be a Worksheet object, but ActiveSheet can also be a Chart object, which doesn't have a UsedRange method, so that line raises error 438 "Object doesn't support this property or method".

You could verify that ActiveSheet is indeed a Worksheet object before accessing its UsedRange, but that wouldn't be all.

For n = 1 To ClctNames.Count

You're also assuming ClctNames.Count will be equal to or greater than 1. However in a new workbook, ActiveWorkbook.Names.Count returns 0, so that For loop goes from 1 to 0... lucky for you, VBA won't enter that loop - but it's not obvious behavior and would be better off explicitly handled at the start of the procedure:

If ClctNames.Count = 0 Then Exit Sub

When evaluating a Boolean value in an If condition:

If c.HasFormula = True Then

You never need to make a Boolean expression out of the Boolean value. This is equivalent, and reads better:

If c.HasFormula Then

As for the forced-absolute references, you can pass parameters to Range.Address:

rngNameLoc = ClctNames(n).RefersToRange.Address(RowAbsolute:=False, ColumnAbsolute:=False)

This removes the dollar signs from the RefersToRange address, but then this:

strFrmla = Replace(c.Formula, rngNameLoc, rngName)

Needs to actually look for the 4 variants of the range address:

$A$1
$A1
A$1
A1

Things get more "fun" when the address refers to a range of cells:

$A$1:$A$10
$A1:$A$10
A$1:$A$10
A1:$A$10

Wait, did you notice?

Replacing $A$1 with Foobar in =SUM($A$10:$A$25) will break something - it won't raise a VBA runtime error, but I'm pretty sure =SUM(Foobar0:$A$25) won't evaluate properly.

This definitely looks like a job for a regex replace, more than a simple string comparison and replacement.


Decoding a telephone number that was masked with letters from the respective keys with APL Legend says phones used to have keyscitation needed and in those times, a person could "encode" a phone number by creating a word from the letters in the keys with the given numbers.

Below is the usual layout of the keys (that matter for my question) of a phone:

     | abc | def |
  1  |  2  |  3  |
------------------
 ghi | jkl | mno |
  4  |  5  |  6  |
------------------
pqrs | tuv | wxyz|
  7  |  8  |  9  |
------------------

I have to write a function that, given a vector of characters with uppercase letters/digits, recovers the original phone number.

For example,

'HELLO' -> 4 3 5 5 6
'IAMYY4U' -> 4 2 6 9 9 4 8
'' -> ⍬

This is the code I have written:

Telephone ← {
  ⍝ Monadic function taking an uppercase string as input.
  ⍝ Decodes the number expressed with uppercase letters,
  ⍝ e.g. '1ABCZ' gives '12229'

  ⍝ Split letters and digits according to their keys (each digit goes to itself)
  keyStarts ← 'ADGJMPTW'
  splits ← ⎕D, keyStarts
  classes ← (1 -⍨ ⍳10), 1 + ⍳8

  classes[splits ⍸ ⍵]
}

I am particularly interested in a better way to create the classes variable, that looks like this 0 1 2 3 4 5 6 7 8 9 2 3 4 5 6 7 8 9 and this ordering depends obviously on the order of splits.

This comes from a problem from an APL competition.

I am particularly interested in a better way to create the classes variable, [...] this ordering depends obviously on the order of splits.

Because ASCII codes of A..Z come later than those of 0..9, we can't shorten the classes array itself. Instead, we can lengthen it, so that classes becomes simply two copies of 0..9. You can create unused intervals by copying the boundary item that comes after them:

    'BBB' ⍸ 'ABCD'
0 3 3 3

Incorporating it in Telephone:

  splits ← ⎕D, 'AAADGJMPTW'
  classes ← ,⍨ 1 -⍨ ⍳10

Nitpicking: When I need to subtract 1 from an expression, I use ¯1+expr instead of 1-⍨expr because the former reads better to me. This is a personal choice though. Or, if the "subtract 1" appears only for generating 0..n-1 instead of 1..n with ⍳, consider using ⎕IO←0.

Now, splits is creating the interval indices 0..20, and except the first 0, each index is mapped to 0..9,0..9.

interval index: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
class         : 0 1 2 3 4 5 6 7 8 9  0  1  2  3  4  5  6  7  8  9

Do you see the pattern? If you discard the unused interval and take modulo 10, you get the right class! Then you can entirely discard the variable classes and extra indexing:

Telephone ← {
  splits ← 1↓ ⎕D, 'AAADGJMPTW'
  10| splits ⍸ ⍵
}


Function that multiplies all numbers between "a" and "b" with do loop I'm making a function that multiplies all numbers between an "a" input and a "b" input with do loop. If you please, check my function and say what's wrong since I don't know loops very well in Scheme.

(define (pi-function x y)
  (let ((result y))
    (do ((limI x (+ x 1)))
        ((= limI y) result)
      (set! result (* result limI)))))

You can use a do loop without using set!.

(define (product-of-range x y)
  (do ((result 1 (* result i))
       (i x (+ i 1)))
      ((> i y) result)))


Javascript MVC Design Feedback I would like to share my design of mvc via js.

What do you think about it? I tried to not use any mvc framework but i want to get clear structure and a decoupled organisation.

Next step for me is evaluating something like require.js to get rid of the ordering within the index file

The code:

XX.namespace.ContactListController = (function() {

var _initListener = function() {
    $(document).ready(function() {
        $("#addContact").click(function() {
            var contactName = $("#contactName").attr('value');
            var contact = new  XX.namespace.ContactModel(contactName);
            $.publish("add#contact", contact);
        });

        $("#removeContact").click(function() {
            var contactName = $("#contactName").attr('value');
            var contact = new  XX.namespace.ContactModel(contactName);
            $.publish("remove#contact", contact);
        });
    });
};

var interface = {
    init: function() {
        _initListener();
    }
};
return interface;

})(XX.namespace.ContactListModel, XX.namespace.ContactListView);

XX.namespace.ContactListModel = (function() {

var contactList = [];

/**
* @private
*/
var _addContact = function(channel, contact) {
    contactList.push(contact);
};

var _removeContact = function(channel, contact) {
    var index = contactList.indexOf(contact);
    contactList.splice(index, 1);
};

/**
* API
*/
$(document).ready(function() {
    $.subscribe("add#contact", _addContact);
    $.subscribe("remove#contact", _removeContact);
})
 })();

XX.namespace.ContactModel = function x(name) {
this.name = name;
};

   XX.namespace.ContactListView = (function(){

var uiComponents = {
    contactList:  "#contactList"
};

var _addContact = function(channel, contact) {
    $(uiComponents.contactList).append("
" + contact.name +"
");
};

var _removeContact = function(channel, contact) {
    $(uiComponents.contactList+" li[contact_name='" + contact.name+ "']").remove();
};

/**
* API
*/
$(document).ready(function() {
    $.subscribe("add#contact", _addContact);
    $.subscribe("remove#contact", _removeContact);
})
})();

     (function() {
      XX.namespace.ContactListController.init();
 })();

<!DOCTYPE html>

     <meta charset="utf-8">

ContactList1

Chat

            <input type="text" id="contactName"/>
            <input type="button" id="addContact" value="hinzufügen">
            <input type="button" id="removeContact" value="entfernen">

        Message: <input id="msg"  type="text" />
        <textarea id="msgContainer"></textarea>

Did you post the right code?

What is TC.messenger?

You pass two arguments in

XX.namespace.ContactListController = (function(contactListModel, view)

and you save them as members, but never use them.


Get collection of objects from CSV string and vise-versa A generic class to get object collection from csv string. I would like to know if there's any better way?

Converter class

public class CsvConverter<T>:List<T> where T:class
{
    private readonly string _csvText;
    private readonly string _separetor;

    /// <summary>
    /// Get object collection from CSV string
    /// </summary>
    /// <param name="csvText">CSV string with Title</param>
    /// <param name="separator">CSV line separator</param>
    public CsvConverter(string csvText, string separator= "\r\n")
    {
        this._csvText = csvText;
        _separetor = separator;
        BuildObject();
    }

    /// <summary>
    /// Get CSV string from collection of objects
    /// </summary>
    /// <param name="collection">Pass collection of objects</param>
    public CsvConverter(IEnumerable<T> collection)
    {
        foreach (var item in collection)
        {
            this.Add(item);
        }
    }

    /// <summary>
    /// Create new Instance of T object
    /// </summary>
    /// <returns></returns>
    protected T GetObject()
    {
        return (T)Activator.CreateInstance(typeof(T),true);
    }

    /// <summary>
    /// Build object from CSV string to collection
    /// </summary>
    private void BuildObject()
    {
        if (!string.IsNullOrEmpty(_csvText))
        {
            //Get first line from CSV string, than split by comma to get column names
            var _csvLines = _csvText.Split(_separetor);
            var _columnName =_csvLines[0].Split(',');

            for (int i = 1; i < _csvLines.Length; i++)
            {
                var line = _csvLines[i].Split(',');

                T obj = GetObject();
                Type t = obj.GetType();
                PropertyInfo[] props = t.GetProperties();

                int colIndx = 0;
                foreach (string str in line)
                {
                    PropertyInfo find = props.First(x => x.Name == _columnName[colIndx]);
                    find.SetValue(obj, Convert.ChangeType(str,find.PropertyType),null);

                    //propertyInfo.SetValue(propertyInfo.PropertyType, line[0]);
                    colIndx++;
                }
                this.Add(obj);
            }
        }
        else
        {
            throw new System.NullReferenceException("No CSV string has passed in the parameter");
        }
    }

    /// <summary>
    /// Returns CSV string from collection of T object
    /// </summary>
    /// <returns>string</returns>
    public string GetCsvString()
    {
        StringBuilder sb = new StringBuilder();
        foreach (var item in this)
        {
            Type t = item.GetType();
            PropertyInfo[] props = t.GetProperties();
            string ln = "";
            foreach (PropertyInfo prp in props)
            {
                ln= ln+($"{prp.GetValue(item)},");
            }
            sb.AppendLine($"{ln.TrimEnd(',')}");
        }
        return sb.ToString();
    }
}

get collection of object from csv string

        var csv = await System.IO.File.ReadAllTextAsync("test.csv");
        List<TestC> csvBarcode = new CsvConverter<TestC>(csv.TrimEnd());

get csv string from collection of object

        CsvConverter<TestC> b = new CsvConverter<TestC>(list);
        return Content(b.GetCsvString());

I would suggest using the new() generic type constraint. Using Activator means you can match a private constructor which you would no longer be the case with the generic constraint but you would be able to catch incorrect use at compile time. Currently, if I tried to use a class like:

 public class Foo
 {
     public Foo(string bar, double baz) { /* ... */ }
 }

Your class would throw at runtime (a MethodMissingException I believe). If you added where T : class, new() instead of just where T : class, trying to use your class with the Foo type would produce a compile time error. An error I can fix before having to run the code is the most helpful kind.

Doing reflection in a loop when you don't need to is not a good thing. You do this for every line in the input file:

 T obj = GetObject();
 Type t = obj.GetType();
 PropertyInfo[] props = t.GetProperties();

You can create this all before processing the file and you don't need to create an instance to get the type information:

PropertyInfo[] props = typeof(T).GetProperties();

for (int i = 1; i < _csvLines.Length; i++)
{
    T obj = GetObject(); // or T obj = new T(); with the constraint added.

You probably want the PropertyInfo[] to be an IDictionary<string, PropertyInfo>. to simplify the lookup by column name.

As for whether this should be a class or not... If you do the reflection in the constructor and keep the instance around to process multiple files then I think it is okay to be a class to save on having to do the reflection over and over again.

If you are going to create an instance to process a file and then throw it away, or if you don't move the reflection to the constructor, I think it would be better as a static class with 2 methods as the other review suggests.

It's spelt: "separator" not "separetor". You've used both spellings. It's a word that I almost always spell wrong too :)


Pairing unit fractions (I was suggested to post here from stack overflow so hey guys!)

I have been given a problem where fractions between 1/2 - 1/1000 have to be added to create the longest sequence of all unique unit fractions.

The rules on constructing these fractions:

Let create a set: D , D is only to hold unique unit fractions , sub-fractions can add up to the same fraction for example:

       1/10
        /      \
1/110 + 1/11    1/35 + 1/14

All sub-fractions can be held within the set as long as they themselves are not the same fractions once we are adding them together it is ok for them to total up to the same root.

The goal:

The fractions have to be added in a way to sum up to exactly 1. Any sub-fractions are not allowed to be over 1000 it is explicitly between 2 and 1000 hence the fractions which make up 1/1000 would not be applicable ( 1/1004 + 1/251000 ).

What currently I found to be the most effective:

Find the two lowest multiples which make-up the current fraction that I am looking at so for e.g 1/6 = A = 3 , B = 2. And now we complete the following equation: C = (A+B)*A , D = (A+B)*B. Now C & D are the sub-fractions which add up to my initial fraction

      1/6
   /       \
1/15       1/10

In code:

public static int[] provideFactorsSmallest(int n) {
    int k[] = new int[2];

    for(int i = 2; i <= n - 1; i++) {
        if(n % i == 0) {
            k[0] = i;
            break;
        }
    }

    for(int i = k[0] + 1; i <= n - 1 && k[0] != 0; i++) {
        //System.out.println("I HAVE BEEN ENTERED");
        if(k[0] * i == n) {
            k[1] = i;
            int firstTerm =  k[0];
            int secondTerm = k[1];
            k[0] = (firstTerm + secondTerm) * firstTerm;
            k[1] = (firstTerm + secondTerm) * secondTerm;
            return k;
        }
    }
    return null;
}

My question:

What would be the most effective way to pair and group the numbers to achieve possible longest fraction sequence?

In first for loop we can stop when i*i>=n. As we need two different divisors, smallest must be less than Math.sqrt(n)
Second for loop looks like simple integer division.

Whole code is equivalent to this:

    private static int[] provideFactorsSmallest_v(int n) {
        for (int firstTerm = 2; firstTerm*firstTerm < n; firstTerm++) {
            if (n % firstTerm == 0) {
                int secondTerm = n / firstTerm;
                return new int[]{
                        (firstTerm + secondTerm) * firstTerm,
                        (firstTerm + secondTerm) * secondTerm
                };
            }
        }
        return null;
    }


Merging totals from rows with duplicate IDs I have the following query which updates the quantity when there are duplicate items and users (there should only ever be 2 duplicates), then deletes the row which doesn't get updated...

UPDATE cart_items t
  JOIN (
    SELECT MinID, b.itemQuantity LatestName
    FROM cart_items b
    JOIN (
      SELECT MIN(id) MinID, MAX(id) MaxID
      FROM cart_items
      GROUP BY account_id, itemNumber
      HAVING COUNT(*) > 1
    ) g ON b.id = g.MaxID
  ) rs ON t.id = rs.MinID
SET t.itemQuantity = t.itemQuantity+LatestName;

DELETE `cart_items`
FROM `cart_items`
LEFT OUTER JOIN (
   SELECT MIN(`id`) as `id`, `account_id`, `itemNumber`
   FROM `cart_items`
   GROUP BY `account_id`, `itemNumber`
) as `KeepRows` ON
   `cart_items`.`id` = `KeepRows`.`id`
WHERE
   `KeepRows`.`id` IS NULL;

However I'm pretty sure this is not the best way to achieve this.

Any suggestions?

Here's the schema for the table:

CREATE TABLE `cart_items` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `account_id` int(11) unsigned NOT NULL,
  `itemNumber` int(11) NOT NULL,
  `itemQuantity` int(11) NOT NULL DEFAULT '1',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=56 DEFAULT CHARSET=utf8;

INSERT INTO `cart_items` (`id`, `account_id`, `itemNumber`, `itemQuantity`)
VALUES
    (54, 10, 49, 1),
    (55, 10, 49, 2);

A safer way than this would be to do something like

CREATE TABLE `cart_items_nodupes` (
  `id` int(11) NOT NULL,
  `account_id` int(11) unsigned NOT NULL,
  `itemNumber` int(11) NOT NULL,
  `itemQuantity` int(11) NOT NULL DEFAULT '1',
  PRIMARY KEY (account_id, itemNumber)
) ENGINE=InnoDB CHARSET=utf8;

This creates a new table that maintains a unique index on account_id and itemNumber, so you'll never have a duplicate.

Then

INSERT INTO `cart_items_nodupes`
SELECT * FROM `cart_items`
ON DUPLICATE KEY UPDATE itemQuantity = itemQuantity + VALUES(itemQuantity)

Or

INSERT INTO `cart_items_nodupes`
SELECT MIN(id) AS id, account_id, itemNumber, SUM(itemQuantity) AS itemQuantity
  FROM `cart_items`
  GROUP BY account_id, itemNumber

Either of these will insert all the values from the old table into the new table, combining those with the same account_id and itemNumber.

And

ALTER TABLE cart_items_nodupes
  DROP PRIMARY KEY,
  MODIFY id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  ADD UNIQUE INDEX uc_account_item (account_id, itemNumber)
ALTER TABLE cart_items_nodupes AUTO_INCREMENT = 56
RENAME cart_items TO cart_items_original, cart_items_nodupes TO cart_items

This creates a new table that ensures there are no duplicates and then copies all the values from the original table.  Then it sets up the primary key so as to continue from the original table.  Finally, it renames the tables so that the new table takes the place of the old table.  The 56 was taken from your table definition -- modify as necessary.  Then you can just continue using the table as normal (after you check the table definition and rows).

Note that you need to stop updating the table while doing this.  You can sort of make the INSERT ON DUPLICATE KEY UPDATE work by saying WHERE id < 56 (again, 56 is based on the table definition -- adjust as necessary).  If new rows are added, you can get them with WHERE id >= 56.  But you should stop updates before altering the table indexes and renaming the tables.

An advantage of doing things this way is that if the data is wrong after the inserts, you can always drop the table and try again.  Your original data is still there, untouched.  And you can test it before doing it for real.


Passing Array into Unmanaged Code for Modification I have an array allocated in C# which I am passing into unmanaged code to be modified. The following code works, but I am not sure if there are any other more 'correct' or 'efficient' methods that I am not aware of.

Code.c

extern "C" {
    class MyObject
    {
    public:
        int32_t X;
    };

    __declspec(dllexport) void ArrayTest(MyObject* a, int length);

    void ArrayTest(MyObject* a, int length)
    {
        for (int i = 0; i < length; i++)
        {
            a[i].X = i + 1;
        }
    }
}

Code.cs

class Program
{
    [StructLayout(LayoutKind.Sequential)]
    public struct MyObject
    {
        public Int32 X;
    }

    [DllImport(@"MyDll.dll", CallingConvention = CallingConvention.Cdecl)]
    public extern static void ArrayTest(IntPtr objects, int length);

    static void Main(string[] args)
    {
        var objects = new MyObject[3];

        var handle = GCHandle.Alloc(objects, GCHandleType.Pinned);
        var ptr = GCHandle.ToIntPtr(handle);

        ArrayTest(ptr, objects.Length);

        for (int i = 0; i < objects.Length; i++)
        {
            var offset = Marshal.SizeOf<MyObject>() * i;

            objects[i] = Marshal.PtrToStructure<MyObject>(IntPtr.Add(ptr, offset));
        }
    }
}

You must definitely call handle.Free() when finished using it so GC can do the cleaning.

Note that GCHandle.Alloc(objects, GCHandleType.Pinned); only works for structs with pure primitive or to be more precise: blittable types. String fields etc. must be handled differently.

A little optimization:

You repeatedly calculate this

  var offset = Marshal.SizeOf<MyObject>() * i;

Instead you can calculate offset once:

var offset = Marshal.Sizeof<MyObject>();

and in the loop do:

ptr = IntPtr.Add(ptr, offset);

If you are willing/allowed to run in unsafe mode you can do:

unsafe public void RunUnsafe()
{
  var objects = new MyObject[3];

  var handle = GCHandle.Alloc(objects, GCHandleType.Pinned);
  var ptr = GCHandle.ToIntPtr(handle);

  ArrayTest(ptr, objects.Length);
  MyObject* pobj = (MyObject*)ptr;

  for (int i = 0; i < objects.Length; i++, pobj++)
  {
    Console.WriteLine((*pobj).X);
  }

  handle.Free();
}

If you are entitled to modify the signature of ArrayTest, you can changed it to:

    public extern static void ArrayTest([In, Out] MyObject[] objects, int length);

The [In, Out] attributes determines that the array should be marshaled both ways so that ArrayTest can work on the provided instances in the array [In], and changes made in ArrayTest are reflected in the objects when the function returns [Out].

Then your method can be simplified to:

  var objects = new MyObject[3];

  ArrayTest(objects, objects.Length);

  foreach (var obj in objects)
  {
    Console.WriteLine(obj.X);
  }


Random uniqueID for each ID SQL How can I improve this code? I wanted to know if there's a possibility to remove the for loop, and UPDATE a different uniqueID for each ID in my database.

<?php

  $reponse = $bdd->prepare("SELECT MAX(ID) FROM tuto WHERE title=''");
  $reponse->execute();

  $IDnow = $reponse->fetch(PDO::FETCH_ASSOC);
  $IDnow = $IDnow['MAX(ID)'];

  for ($i=2 ; $i <= $IDnow ; $i++){

     $reponse = $bdd->prepare("UPDATE tuto SET uniqueID=:uniqueID WHERE uniqueID='' AND title='' AND ID=:i");

     $uniqueID = $donnees['uniqueID'];

     $int = rand(0,51);
     $a_z = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
     $uniqueID = time().$a_z[$int];

     $reponse->execute([':uniqueID' => $uniqueID, 'i' => $i]);
  }
?>

Finally, I used RAND(), for generate an "UniqueID", and we can add some letters after or else :

Thanks to NerdyDev and Bart Friederichs for solutions.

<?php

$reponse = $bdd->prepare("UPDATE tuto SET uniqueID=ROUND(RAND() * :tp) WHERE uniqueID='' AND title=''");

$uniqueID = $donnees['uniqueID'];

$tp = time();
$reponse->execute([':tp' => $tp]);

?>


Array even and odd indexes sorting and printing its values I'm looking for a more elegant solutions of this task.

Task:
Given a string, print its even-indexed and odd-indexed characters as space-separated strings on a single line.

Input Format:
The first line contains an integer (the number of test cases).
Each line of the subsequent lines contain a string.

puts "Input a number of test cases"
t = gets.to_i
t.times do
  puts "Input a string please"
  s = gets.strip
  z = s.split(//)
  b = z.each_with_index.sort_by { |i, x| [x.even? ? 0 : 1, x] }
  b.map { |i, x| print i if x.even? }
  print " "
  b.map { |i, x| print i if x.odd? }
  puts
end

It's a good idea to become familiar with all the methods on Array and Enumerable. It just so happens that Enumerable has a method called partition that has a purpose of dividing an array into .two parts (e.g. even and odd). It'll help tidy up your code.

So let's use partition on the array of chars and partition based on index.

parts = s.chars.partition.with_index{|_,i| i.even?}

Note since we are only using the index, the actual character is unimportant so I used _ instead of a variable name in the partition block.
Now you have two arrays of characters--let's join each array so that you have a single array with two strings:

strings = parts.map(&:join)

If you don't know about &:join it is a shortcut for {|a| a.join} (using Symbol#to_proc if you want to read up on it).

Then, finally print it. Note that in your code, you intermingle printing with manipulation. This is best avoided if you can; print only once you have things in final form.

print strings.join(' ')

The above could all be put together in one line if you'd rather:

print s.chars.partition.with_index{|_,i| i.even?}.map(&:join).join(' ')


Initial public offering auction Image transcription:
A company registers for an IPO. All shares are available on the website for bidding during a time frame called the bidding window. At the end of the bidding window an auction logic is used to decide how many available shares go to which bidder until all shares have been allocated, or all the bidders have received the shares they bid for, whichever comes first.
The bids arrive from users in the form of [userid, # shares, bidding price, timestamp] until the bidding window is closed.
The auction logic assigns shares as follows:

The bidder with the highest price gets the # of shares they bid for

If multiple bidders have bid at the same price, the bidders are assigned shares in the order in which they places their bids (earliest bids first)

List the userids of all the users who didn't get even 1 share after all shares have been allocated.
Input

bids
list of lists of ints representing [userid, # shares, $bid, timestamp]
totalShares
total # of shares to be distributed.

Todo
distribute shares amongst bidders and return userids of bidders that got 0 shares.
Share distribution logic:

bidder with highest offer gets all the shares they bid for, and then
if there are ties in $ bid price, assign shares to earlier bidder.

I feel like the solution I came up with is relatively simple. It seems to pass all edge cases I can think of.
Question
I have found a questionable situation:
Bid price and times are the same and there aren't enough shares for all bidders ie: bids is [[0,2,10,0], [1,2,10,0]] and totalShares is 2. It's unclear if 1 share should be given to each, or if userid 0 just gets both.
Code
Can my solution be optimized in anyway?
def getUnallocatesUsers(bids, totalShares):
  s = 0
  for b in bids:
      s += b[1]
  if totalShares >= s: return []  # no losers because enough shares to go around

  bids.sort(key = lambda x: (-x[2],x[3]))  # sort by highest bid, then timestamp for ties
  losers = []
  for b in bids:
    if totalShares <= 0: losers.append(b[0])
    else:
      totalShares -= b[1]
  return losers

Use the function name given in the problem:
def getUnallottedUsers(bids, totalShares):

The problem doesn't provide any information about the likelihood of there being enough shares for all bidders, so IMO the first for-loop is an example of premature optimization.
Use constants instead of "magic numbers".  Use meaningful names.  Take a look at PEP8 about common formatting conventions.  These things go a long way in making code readable.
USERID = 0
SHARES = 1
PRICE = 2
TIME = 3

bids.sort(key=lambda bid:(-bid[PRICE], bid[TIME]))

for index, bid in enumerate(bids):
    totalShares -= bid[SHARES]

    if totalShares <= 0:
        break

Answer the question that was asked: "The function must return a list of integers, each an id for those bidders who receive no shares, sorted ascending"
return sorted(bid[USERID] for bid in bids[index + 1:])

All together:
USERID = 0
SHARES = 1
PRICE = 2
TIME = 3

def getUnallottedUsers(bids, totalShares):
    bids.sort(key=lambda bid:(-bid[PRICE], bid[TIME]))

    for index, bid in enumerate(bids):
        totalShares -= bid[SHARES]

        if totalShares <= 0:
            break

    return sorted(bid[USERID] for bid in bids[index + 1:])

Or use an iterator:
    bids = iter(bids)
    while totalShares > 0:
        price = next(bid)[PRICE]
        totalShares -= price

    return sorted(bid[USERID] for bid in bids)


VB.net SQL match against Excel data to datagridview I am importing an Excel sheet where the first column will contain product numbers. I match those numbers to my SQL database and display the product number and description of the matching products to a datagridview.
The code works, but if there are a lot of products it can take a long time.  What I'd really like to do is have it read the Excel sheet and add the description directly into the Excel sheet after the last column with data. Or at very least optimize the code to run quicker.
    Dim v As New FormRec
    Dim x As New FormRec
    Dim MyConnection As OleDb.OleDbConnection
    Dim DtSet As DataSet
    Dim MyCommand As OleDb.OleDbDataAdapter
    MyConnection = New OleDb.OleDbConnection(String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 8.0", workbook))
    MyCommand = New OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection)
    MyCommand.TableMappings.Add("Table", "match")
    DtSet = New DataSet
    MyCommand.Fill(DtSet)
    v.dgv.DataSource = DtSet.Tables(0)
    MyConnection.Close()

    Dim col As New DataGridViewTextBoxColumn
    col.HeaderText = "Product Number"
    col.Name = "prodnmbr"
    col.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
    col.DefaultCellStyle.Font = New Font("Arial", 14)
    x.dgv.Columns.Add(col)

    Dim co2 As New DataGridViewTextBoxColumn
    co2.HeaderText = "Description"
    co2.Name = "descrip"
    co2.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
    co2.DefaultCellStyle.Font = New Font("Arial", 14)
    x.dgv.Columns.Add(co2)

    For index As Integer = 0 To v.dgv.RowCount - 1
        Using conn As New SqlConnection(myconnection)
            Dim commandText As String = "select productnmbr,descr from database where productnmbr = @prodnmbr"
            Using command As SqlCommand = New SqlCommand(commandText, conn)
                conn.Open()
                With command.Parameters
                    .AddWithValue("@prodnmbr", v.dgv.Rows(index).Cells(0).Value.ToString)
                End With
                Using sqlread As SqlDataReader = command.ExecuteReader
                    If sqlread.Read Then
                        Dim match As String = sqlread.GetString(0)
                        If match = v.dgv.Rows(index).Cells(0).Value Then
                            x.dgv.Rows.Add(v.dgv.Rows(index).Cells(0).Value.ToString, sqlread.GetString(1))
                            x.dgv.Rows(index).DefaultCellStyle.BackColor = Color.LightGreen
                        End If
                    Else
                        x.dgv.Rows.Add(v.dgv.Rows(index).Cells(0).Value.ToString, "N/A")
                        x.dgv.Rows(index).DefaultCellStyle.BackColor = Color.Red
                    End If
                End Using
            End Using
        End Using
    Next
    x.ShowDialog()

You are repeatedly opening the DB connection in a loop:
For index As Integer = 0 To v.dgv.RowCount - 1
    Using conn As New SqlConnection(myconnection)
        Dim commandText As String = "select productnmbr,descr from database where productnmbr = @prodnmbr"
        Using command As SqlCommand = New SqlCommand(commandText, conn)
            conn.Open()
            With command.Parameters
                .AddWithValue("@prodnmbr", v.dgv.Rows(index).Cells(0).Value.ToString)
            End With
            ...
        End Using
    End Using
Next

There is overhead involved when opening a connection. You could just open it once,  leave it open, do your stuff, then dispose of it:
Using conn As New SqlConnection(myconnection)
    For index As Integer = 0 To v.dgv.RowCount - 1
        ' do something
    Next
End Using

Just moving code a bit should improve performance.
Something else: no need to read values from the DGV. This could even freeze your UI. Read the rows from the dataset instead, that is the one table it contains (match).
I see that you are also adding rows to the DGV but the better way would be to feed the underlying source, that is your datatable, possibly a bindingsource. Then let the DGV refresh itself.
I am not familiar with your SQL table but if the product table is not too large, I might be tempted to load it to a second datatable, then compare both datatable with LINQ for example (you will find code samples on Stack Overflow). The benefit is that all your data will be preloaded to memory and comparison should be quite fast if you're not doing it row by row. It depends on how much data there is.
Or do it differently: load your Excel file to a temporary table in your SQL server. Then compare the tables by doing a join, a stored procedure if you like.


Javascript recursive object is defined test I wrote the method below, and was wondering whether a better way exists/best practice tips

_objectIsDefined = function (obj, path) {
  if (typeof path !== 'undefined') {
    // if we have been given a specific object path then recursively search it
    var pathSplit = path.split("."),
      scope = undefined;
    scope = obj;
    for (i = 0; i < pathSplit.length; i++) {
      if (typeof scope[pathSplit[i]] === 'undefined') {
        return false;
      } else if (i == pathSplit.length - 1 && typeof scope[pathSplit[i]] !== 'undefined') {
        return true;
      }
      scope = obj[pathSplit[i]];
    }
    return false;
  } else if (typeof obj !== 'undefined') {
    // if we've just been given an object and no path then return whether
    // its defined or not...
    return true;
  } else {
    return false;
  }
}
 // Usage
var isDefined = _objectIsDefined(myObject);
var isDefinedDeep = _objectIsDefined(myObject, "somenested.child.property.name");

First of all, what you are doing is not recursive (self-calling). It's iterative (looping).

And here's a shorter version of it:

_objectIsDefined = function (obj, path) {

  if (!obj) return false;           // no object, return false
  if (obj && !path) return true;    // has object, no path, return true

  // I suggest var per variable for clarity
  var props = path.split(".");
  var currentObject = obj;

  for (var i = 0; i < props.length; ++i) {
    //store the next property, evaluate and break out if it's undefined
    currentObject = currentObject[props[i]];
    if (!currentObject) return false;
  }

  // If the loop did not break until the last path, then the path exists
  return true;
}


Pathfinding in turn-based strategy game I am using Python with libtcodpy to build a turn-based strategy game.

In this game each unit has a maximum range which varies from 4-10. This range determines how many "steps" the unit can take and therefore the distance it can move on the game map.

I wish to output all the squares which the unit can move to, based upon its range as well as variable terrain, which increases the number of steps necessary to move over it.

This function is what I have to generate these squares and it works but is intolerably slow (takes about 1-2 seconds for the squares to highlight on the map):

#Gets a list of all the squares the unit can move to (so that they
#can be highlighted later)
def get_total_squares(max_range, playerx, playery, team):

    global coord_in_range   #List where the final coords will be stored
    global T_Map    #Instance of the Map class, i.e the map

    #This iterates through an area of the game map defined by the square
    #created by the player's maximum range (where the player is in the center
    #of the square)
    for x in range((playerx-max_range), (playerx+max_range+1)):
        for y in range ((playery-max_range), (playery+max_range+1)):
            #This creates a path for every square in the above area
            path = libtcod.path_new_using_map(new_map, 0)
            #This computes a path to every square in the above area
            libtcod.path_compute(path, playerx, playery, x, y)
            #This gives the number of steps the unit takes to walk one specific path
            num_steps = libtcod.path_size(path)
            #This is a blank list which will be populated with the coordinates of
            #the tiles of one specific path
            coord_of_path = []

            #This populates the above list
            for i in range(libtcod.path_size(path)):
                coord_of_path.append(libtcod.path_get(path, i))

            #This is a list of all the tiles in the map which can hinder movement
            #henceforth called "terrain_tiles"
            terrain_tiles = [tile for tile in T_Map.tile_array
                                if tile.terrain_name in ('Tall Grass', 'Hills', 'Forest', 'Water')]

            #This iterates through all the "terrain tiles" and
            #if the tile is in the path, adds that tiles movement penalty
            #to the total number of steps to take that path
            for tile in terrain_tiles:
                if (tile.x, tile.y) in coord_of_path:
                    num_steps += tile.move_cost

            #This is what actually determines whether the path is added to the
            #list of walkable paths; if the path's step count, taking into account
            #modifications from terrain, is greater than the unit's range that path is not added
            if num_steps <=max_range:
                for i in range(libtcod.path_size(path)):
                    coord_in_range.append(libtcod.path_get(path, i))

    return coord_in_range

I'm quite certain something in here is acting as a bottleneck and very certain it has something to do with adjusting the paths due to terrain, since if I do it without considering terrain, it runs very fast.

If the full code is necessary (I don't think it should be as, stated above, the problem must lie here) I will post it.

What you're trying to compute here is the cost of the unit to travel to each accessible square in the region. You do it by iterating over all squares in the region, computing a path to the destination square, and adding up the cost of getting there.

Suppose that this figure shows the cost of traversing each square, and you have a unit in the square marked A:

┌───┬───┬───┬───┐
│ 1 │ 2 │ 1 │ 1 │
├───┼───┼───┼───┤
│ 2 │ 2 │ 2 │ 1 │
├───┼───┼───┼───┤
│ 1 │ 3 │ 1 │ 1 │
├───┼───┼───┼───┤
│ 1 │ 2 │ 1 │ A │
└───┴───┴───┴───┘

Then your algorithm goes like this:

┌───┬───┬───┬───┐    ┌───┬───┬───┬───┐    ┌───┬───┬───┬───┐    ┌───┬───┬───┬───┐
│ * │ . │ . │ . │    │ . │ * │ . │ . │    │ . │ . │ * │ . │    │ . │ . │ . │ * │
├───┼───┼───┼───┤    ├───┼───┼───┼───┤    ├───┼───┼───┼───┤    ├───┼───┼───┼───┤
│ . │ * │ . │ . │    │ . │ * │ . │ . │    │ . │ . │ * │ . │    │ . │ . │ . │ * │
├───┼───┼───┼───┤    ├───┼───┼───┼───┤    ├───┼───┼───┼───┤    ├───┼───┼───┼───┤
│ . │ . │ * │ . │    │ . │ . │ * │ . │    │ . │ . │ * │ . │    │ . │ . │ . │ * │
├───┼───┼───┼───┤    ├───┼───┼───┼───┤    ├───┼───┼───┼───┤    ├───┼───┼───┼───┤
│ . │ . │ . │ A │    │ . │ . │ . │ A │    │ . │ . │ . │ A │    │ . │ . │ . │ A │
└───┴───┴───┴───┘    └───┴───┴───┴───┘    └───┴───┴───┴───┘    └───┴───┴───┴───┘
1 + 2 + 1 = 4        1 + 2 + 2 = 5        1 + 2 + 1 = 4        1 + 1 + 1 = 3

and so on. There is wasted work here, because each path starts again from scratch instead of making use of previously computed information. The point is that after computing the first path above, we already know the following costs:

┌───┬───┬───┬───┐
│ 4 │ . │ . │ . │
├───┼───┼───┼───┤
│ . │ 3 │ . │ . │
├───┼───┼───┼───┤
│ . │ . │ 1 │ . │
├───┼───┼───┼───┤
│ . │ . │ . │ A │
└───┴───┴───┴───┘

and so to compute the second path we can start from the square marked "3" instead of having to start all the way back at A.

If you make this idea into an algorithm then you get Dijkstra's algorithm.

But you may be able to do better than that. If the cost of moving over a square of terrain never changes during the game, then the cost of moving along a path never changes either. So you could pre-compute the cost of all paths (using the Floyd–Warshall algorithm) and remember them.


Optimizing implementation of Dijkstra I am using a std::priority_queue to implement Dijkstra's algorithm:

while(!Q.empty())
{
    u = Q.top().first;
    Q.pop();
    int size=G[u].size();
    for(int i=0; i<size; i++)//tle
    {
        v = G[u][i].first;
        w = G[u][i].second;
        //cout<<u<<" "<<v<<" "<<w<<endl;
        if(d[v] > d[u] + w)
        {
            d[v] = d[u] + w;
            Q.push(pp(v,d[v]));

        }
    }
}

But I think it is a bit slow for competitive programming (\$O(e + v*v)\$).  How can I improve my implementation?

Handling vertices multiple times
There are two places where you are doing extra work because you are handling vertices multiple times:

When you find a shorter path to a vertex, you currently push that vertex with the new path length into your priority queue.  While this works, you still have the old vertex in the priority queue as well.  You will eventually pop the vertex off twice (and handle it twice), which is not ideal.  If you kept track of which vertices you've already handled, you could quickly skip duplicate vertices when you come across them.

When you handle a new vertex, you iterate over all of its edges.  But if you see an edge to a vertex that has already been handled, you don't need to deal with it.  Right now, you could be pushing vertices back onto your priority queue that you've already dealt with.


SQL: Search for a keyword in several columns of a table I want to perform a search in several columns of a table. I use the following query:

select *
from Tabela t
inner join "TabelaPai" tp on tp."ID" = t."RefTabelaPai" and tp."RefProject" = 'projectid'
where not t."Deleted"
 and (t.Col1 ~ '.*__param1__.*' or t.Col2 ~ '.*__param1__.*' or t.Col3 ~ '.*__param1__.*'
   or t.Col4 ~ '.*__param1__.*' or t.Col5 ~ '.*__param1__.*' or t.Col6 ~ '.*__param1__.*'
   or t.Col7 ~ '.*__param1__.*' or t.Col8 ~ '.*__param1__.*' or t.Col9 ~ '.*__param1__.*');

This will search for the keyword __param1__ in any of the columns and it's working fine.

But I don't like the way the query looks like. Any suggestion on how to refactor the query so it can look 'prettier' (without those ~ '.*__param1__.*' repetitions, for example)?

Edit: A little of context about the query:

What leads to this usage is that I can parameterize the data in the table. For example, I have a column in a table where scripts are saved. My application allows the users to parametrize the script using something like __param1__. If the user wants to rename the parameter I'll have to search for the usage of the parameter in every column that is parameterizable, and this is the query that finds where the parameter is used.

Something "prettier"? And so many repetitions call for generalization?

SELECT *
FROM   tabela AS t
JOIN   "TabelaPai" tp ON tp."ID" = t."RefTabelaPai"
WHERE  NOT t."Deleted"
AND    tp."RefProject" = 'projectid'
AND    t::text LIKE E'%\\_\\_param1\\_\\_%';

Major points:

You can reference the composite type of any relation in the SELECT list. I quote the manual:

  Whenever you create a table, a composite type is also automatically
  created, with the same name as the table, to represent the table's row type.

You can cast the whole row (the composite type) to its text representation in one fell swoop, which is a very convenient syntactical shorthand. The resulting filter in my query is guaranteed to find every occurrence in the whole row.
The LIKE operator is generally faster than regular expression pattern matching (~). Regular expressions are far more powerful, but whenever LIKE can do the job use it. Its syntax is simpler, too.
Underscores (_) have a special meaning for the LIKE operator, so you need to escape literal _. Default escape character is \, which also has a special meaning in escape string constants, so you have to double them when you use the E'' prefix: E'\\_'.

As of version 9.1, the setting standard_conforming_strings is on by default. With this setting \ has no special meaning in plain string literals (without the E prefix) and you don't need to double them: '\_'.
There is a minor flaw / corner case: If the separator in text representation (, by default, and some values are enclosed in double quotes "), can be part of the search pattern, there can be false positives.

Minor cleanup / simplification:

JOIN is shorthand for INNER JOIN.
It is cleaner to write AND tp."RefProject" = 'projectid' as WHERE clause, as it has no connection to tabela. Else you could put NOT t."Deleted" into the JOIN condition as well. Either way, you get the same result here.
No point for mixed case in Tabela (without double quotes). By default (and without double-quotes), all identifiers are folded to lower case automatically. My standing advice is to use legal. lower case identifiers exclusively in PostgreSQL and avoid avoid double-quoting and possible confusion.


Dynamic module import in a class hierarchy I have two python libraries, let's call them lib and extended_lib, that provide a very similar public API. To be more specific, extended_lib enriches the API of lib with new features and functionality; it also modifies the behavior of some functions that are also provided in lib (e.g. get_version()).

I have a pair of classes LibWrapper and ExtendedLibWrapper, the latter being a sub-class of the former, that dynamically load the corresponding module and save its reference within an instance variable (i.e. self._lib).

I have a class Base that inherits from LibWrapper and a class Derived that inherits from both Base and ExtendedLibWrapper. The class Base exposes the functionalities provided by lib. The class Derived inherits all these functions and exposes also the functionalities provided by extended_lib.

This setup is meant to allow Derived to inherit, without code duplication, the functionality provided by Base.

I designed the LibWrapper and ExtendedLibWrapper as an independent hierarchy of classes because there are multiple Base/Derived pairs.

Example:

#!/usr/bin/env python3

import importlib

class LibWrapper():
    def __init__(self, *args, **kwargs):
        self._lib = self.get_lib()

    def get_lib(self):
        return importlib.import_module("lib")

class ExtendedLibWrapper(LibWrapper):

    def __init__(self, *args, **kwargs):
        super(ExtendedLibWrapper, self).__init__(*args, **kwargs)

    def get_lib(self):
        return importlib.import_module("extended_lib")

class Base(LibWrapper):
    def __init__(self, *args, **kwargs):
        super(Base, self).__init__(*args, **kwargs)

    def get_version(self):
        return self._lib.get_version()

    def print_version(self):
        print("Base class:\n\t" + self.get_version())

class Derived(Base, ExtendedLibWrapper):

    def __init__(self, *args, **kwargs):
        super(Derived, self).__init__(*args, **kwargs)

    def print_version(self):
        print("Derived class:\n\t" + self.get_version())

if __name__ == '__main__':

    base = Base()
    base.print_version()

    derived = Derived()
    derived.print_version()

The output is:

~$ ./test.py
Base class:
    Lib Version
Derived class:
    Extended Lib Version

Question(s):

coming from a different software development experience (c++), I would like to ask: is this pythonic?
is there a better, more robust or easily extensible, setup?

Yes, this is definitely pythonic: implementing lib wrappers allows one to easily include additional features in wrapper classes, as a complement to pure import. So it is both concise and versatile.

As you are working with Python 3 (according to the shebang), you may use the simplified form for super():

super().__init__(*args, **kwargs)

instead of

super(ExtendedLibWrapper, self).__init__(*args, **kwargs)

It works even with multiple inheritance.