Parsing Python arguments I'm trying to cover the following 4 scenarios:

No parameters
DB param
DB param and 1 option
No DB param and 1 option

So far I have the program working for all scenarios. However, I'm not happy with the way it's implemented. Is it possible to change this to a cleaner solution, rather than checking if there's only 1 parameter passed or catching an exception?

def do_work(database=None):
    if database:
        print 'Doing work for {0}'.format(database)
    else:
        print 'Doing work all database'

def do_other_work(database=None):
    if database:
        print 'Doing other work for {0}'.format(database)
    else:
        print 'Doing other work all database'

def create_parser():
    parser = ArgumentParser(description='Parser')
    parser.add_argument('--db', '-d', dest='database',
                        default=None, required=False, help='Database name')

    option_group = parser.add_mutually_exclusive_group(required=False)
    option_group.add_argument('-a', dest='cmd',
                              action='store_const',
                              const=lambda args: do_other_work(args.database)
    return parser

if len(sys.argv) == 1:
    do_work()

parser = create_parser()
parsed_args = parser.parse_args(sys.argv[1:])

try:
    parsed_args.cmd(parsed_args)
except TypeError:
    do_work(parsed_args.database)

No parameters passed: do_work() for all databases
db parameter passed and no option: do_work() for just that database
db parameter and an option: do_other_work() just for that database
No db parameter and an option: do_other_work() for all databases

You do not have to check len(sys.argv) yourself. A properly constructed ArgumentParser will sort that out by itself.
Likewise, you do not need to pass sys.argv[1:] to parse_args(), argparse will figure it out.
default=None and required=False are default values for an optional argument (those starting with '-'), you do not need to supply them.
You do not need to use a lambda to specify the function to call, just pass in the name of the function: it will simplify handling this parameters.
You can add do_work as a default value for '-a': no more TypeError as the content of parsed_args.cmd will alway be a callable.
There is no need in using a mutually exclusive group if you only ever put one option in it: remove it.
You should use if __name__ == '__main__': and put your top-level code under it.
In do_*work you should test database using is None or is not None.

def do_work(database=None):
    if database is not None:
        print 'Doing work for {0}'.format(database)
    else:
        print 'Doing work all database'

def do_other_work(database=None):
    if database is not None:
        print 'Doing other work for {0}'.format(database)
    else:
        print 'Doing other work all database'

def create_parser():
    parser = ArgumentParser(description='Parser')
    parser.add_argument('--db', '-d', dest='database', help='Database name')
    parser.add_argument('-a', dest='cmd', action='store_const',
                        const=do_other_work, default=do_work)
    return parser

if __name__ == '__main__':
    parsed_args = create_parser().parse_args()
    parserd_args.cmd(parsed_args.database)


Is this a good way of managing parallel go routines when I care about ordering of results? I have a process with a number of stages that need to be completed in sequence. Each stage is largely parallelisable, involving looping over a large data structure and processing each item independently, and collating the results into the data structure used by the next stage.

My approach is like the following:

var wg sync.WaitGroup
var control_routine sync.WaitGroup
stage_complete = make(chan bool)
stage_one_channel := make(chan map[typeFoo][]typeBar, 16)
stage_two_channel := make(chan map[typeBaz][]typeQux, 16)
stage_one_results := make(map[typeFoo][]typeBar)
stage_two_results := make(map[typeBaz][]typeQux)

// A routine to receive results as they arrive from each stage
go func() {
    for  {
        select {
        case new_stage_one_results := <- stage_one_channel:
            // merge new_stage_one_results into stage_one_results...
        case new_stage_two_results := <- stage_two_channel:
            // merge new_stage_two_results into stage_two_results...
        ...
        case stay_alive := <-stage_complete:
            control_routine.Done()
            if !stay_alive {
                return
            }
        }
    }
}()

// Begin stage One
control_routine.Add(1)
for i, some_data := range my_data {
    wg.Add(1)
    go func(i, datum){
        defer wg.Done()
        some_stage_one_results = make(map[typeFoo][]typeBar)
        // do some heavy lifting with some_data which populates some_stage_one_results...
        stage_one_channel <- some_stage_one_results
    }()
}
wg.Wait()
stage_complete <- true
control_routine.Wait()
// End stage one

// More stages much like the first...

// Finally send the control routine a terminal signal and wait for it to finish
stage_complete <- false
control_routine.Wait()

This basically seems to work, but I need to be sure of the following assumption (which some behaviour is causing me to doubt).

Can I be sure that all go routines in stage N will completely finish and the data sent through stage_N_channel will all be processed in the control routine before the stage_complete channel is read from?

What I'm seeing is that sometimes, it looks like one or two results from a stage go missing, as if the control routine were moving on before processing them. Adding a one second sleep to the main routine after wg.Wait(), seems to help but I'm not sure is this is right.

Is there anything else unusual or potentially dangerous about this pattern? Is there another pattern I should consider using?

I finally settled on a solution where each stage resembles the following.

for i, some_data := range my_data {
  wg.Add(1)
  go func(i, some_data){
    // stage logic
    ...
    stage_one_channel <- some_stage_one_results
  }(i, some_data)
}

go func() {
    wg.Wait()
    close(stage_one_channel)
}()
for new_stage_one_data := <- stage_one_channel {
  // integrate received data
  ...
  wg.Done()
}

The answer to "Can I be sure...?" is no, there is nothing in your code that ensures that.  That is why results sometimes go missing.

Missing results are a sign of a data race.  You can use the Go race detector to find and fix these problems.  Data races are categorically dangerous.  They can corrupt memory.

You might be able to find and fix the problem by adding more synchronization, but your code is too complex as it is.  Since you asked, a pattern you should probably be using is a particular producer-consumer pattern.  It has two parts, 1) a producer goroutine that sends data to a channel and closes the channel after sending the last value, and 2) a consumer goroutine with a for loop that ranges over the channel.  With this pattern you should be able to dispense with the select statement, the stage complete channel, and the control routine wait group.

Your code that begins for i, some_data := range my_data is what I'm calling a producer.  It produces data on the channel stage_one_channel.  Following wg.Wait(), you should replace the line stage_complete <- true with close(stage_one_channel).

Replace your for loop containing the select statement with a separate for loop for each stage, something like

for new_stage_one_results := range stage_one_channel {
    ...
}

When this for loop completes, you are sure that all data has been received from stage_one_channel and processed by the loop body.  This is the synchronization your code is missing.  Your current mechanism only waits for data to be sent, not received and processed.

When the last loop completes, your program can exit.  You could retain some of your mechanism for waiting for this, but simpler would be swap what you currently have in your main goroutine for the "consumer" code that you currently have in that first function literal.  That is, run your producers in one or more separate goroutines so you can have your consumers--the last one especially--in main.  When the last consumer has processed the last data, you know your program is done, with nothing else to wait for.

Finally, let me reinforce, if go run -race says you have a problem, you have a problem.


"Quick" save in electron with a conformation dialog I have implemented a "quick" save for my electron app. There has to be a better way to achieve what I am doing.

The MessageBox will always be triggered on a save as command. If the user selects save (Ctrl+S), I'm basically keeping a variable to hold if a file has been loaded from the file system or not. If a file has been loaded I take the array from fs and convert into a string and use that as the filename for the quick save.

If no file has been uploaded, this means my gantt area is clear and the SaveDialog box will be triggered since this isn't a loaded file. A little code to further explain. Before the quicksave function the filename is either undefined or a string from the fs array and stored in quickSaveFilename.

// dirty quicksave
const quickSave = () => {
  let saveData = gantt.serialize();
  saveData = JSON.stringify(saveData, null, '\t');
  if (quickSaveFileName === undefined) {
    dialog.showSaveDialog(
      {
        defaultPath: `C:\\Users\\${process.env.USERNAME}\\Documents\\`,
        filters: [
          {
            name: 'json',
            extensions: ['json'],
          },
        ],
      },
      (filename) => {
        if (filename === undefined) {
          return;
        }
        fs.writeFile(filename, content, (error) => {
          if (error) {
            dialog.showErrorBox(
              'Save Failed',
              `An error occured saving the file ${error.message}`,
            );
            return;
          }
          dialog.showMessageBox({
            type: 'none',
            title: 'Ganttron',
            message: 'The chart was successfully saved',
            buttons: ['OK'],
          });
        });
      });
  } else {
    fs.writeFile(quickSaveFileName, saveData, (error) => {
      if (error) {
        dialog.showErrorBox(
          'Save Failed',
          `An error occured saving the file ${error.message}`,
        );
      }
      dialog.showMessageBox({
        type: 'none',
        title: 'Ganttron',
        message: 'The chart was successfully saved',
        buttons: ['OK'],
      });
    });
  }
};

Is there a more efficient way of doing this in electron?

We can make the code simpler with a saveFile function

function saveFile(filename, data) {
    fs.writeFile(filename, content, (error) => {
        if (error) {
            dialog.showErrorBox(
                'Save Failed',
                `An error occured saving the file ${error.message}`,
            );
            return;
        }
        dialog.showMessageBox({
            type: 'none',
            title: 'Ganttron',
            message: 'The chart was successfully saved',
            buttons: ['OK'],
        });
    });
}

const quickSave = () => {
    let saveData = gantt.serialize();
    saveData = JSON.stringify(saveData, null, '\t');
    if (quickSaveFileName === undefined) {
        dialog.showSaveDialog({
                defaultPath: `C:\\Users\\${process.env.USERNAME}\\Documents\\`,
                filters: [{
                    name: 'json',
                    extensions: ['json'],
                }, ],
            },
            (filename) => {
                if (filename === undefined) {
                    return;
                }
                saveFile(quickSaveFileName, saveData);
            });
    } else {
        saveFile(quickSaveFileName, saveData);
    }
};


K&R exercise 1-19, reverse line I've just started to read K&R and currently, I'm dealing with problem 1-19:

  Write a function reverse(s) that reverses the character string s. Use it to write a program that reverses its input a line at a time.

Here is what I've implemented:

#include <stdio.h>

#define MAX_LINE_LENGTH 1000

void reverseLine(char reversedLine[], char line[], int lineSize);

int main(void) {

    int c;
    int i = 0;

    char line[MAX_LINE_LENGTH];
    while((c = getchar()) != EOF && c != '\n') {
        line[i++] = c;
    }
    if (c == '\n')
        line[i++] = '\0';

    int realSize = i;
    char reversedLine[realSize];

    reverseLine(reversedLine, line, realSize);
    printf("%s\n", reversedLine);

    return 0;
}

void reverseLine(char reversedLine[], char line[], int lineSize){
    int j, k;
    for(j = lineSize - 2, k = 0; j >= 0; j--, k++){
        reversedLine[k] = line[j];
    }
    reversedLine[k] = '\0';
}

I'm having problems on grasping the spirit exactly as I am supposed to be. So, any help to improve my solution will be appreciated!

Good initial code.

No protection against buffer overflow.

char line[MAX_LINE_LENGTH];
while((c = getchar()) != EOF && c != '\n') {
  if (i < MAX_LINE_LENGTH) {  // add
    line[i++] = c;
  }
}

No check for "too long a line"

// add test
if (i  == MAX_LINE_LENGTH) {
  fprintf(stderr, "Input too long.\n");
  return EXIT_FAILURE;
}

Always add the null character termination

// if (c == '\n')  // comment out line
line[i++] = '\0';

Code looks wrong with the - 2, yet it appears code functions correctly.  The 2-ness needs explaining.  Below is a size_t version.  Note use of const.

void reverseLine(char reversedLine[], const char line[], size_t lineSize){
  if (lineSize > 0) {
    size_t lineLen = lineSize - 1;
    size_t i;
    for (i = 0; i < lineLen; i++) {
      reversedLine[i] = line[lineLen - i];
    }
    reversedLine[i] = '\0';
  }
}

Minor: for array sizing and indexing size_t is the best type.  int may be too narrow in extreme cases.  Yet keep in mind that size_t is some unsigned type.
Minor: "length" vs "size":  When these two are applied to strings, using "size" to refer to the array size of the string and "length" as the value return from strlen() adds clarity.  IMO, MAX_LINE_LENGTH should be LINE_SIZE or stay with MAX_LINE_LENGTH and use char line[MAX_LINE_LENGTH + 1]; ...  if (i < sizeof line) { ... if (i == sizeof line) {.

Advanced issue: Overlap

void reverseLine(char reversedLine[], char line[], int lineSize);

Strictly speaking, the function, reverseLine() does not, by its declaration, indicate that reversedLine[] and lineSize[] should not overlap.  It could fail in bizarre ways should they overlap.

C provides restrict to 1) indicate pointers should not point to overlapped data and 2) allow the compiler to perform additional optimizations that require that assumption.

// [] form also shifted to * form
void reverseLine(char * restrict reversedLine, const char * restrict line, int lineSize);

Alternatively code could be re-written to cope with overlapped buffers.  I'll leave that as an exercise.

Design idea:  In C, many of the basic string functions assume the destination array is big enough.  As the standard library functions are crafted for performance, checking size allocation is left as a responsibility to the caller.

reverseLine() could be re-designed to do that test.  Instead of leaving the size test to the caller, consider that reverseLine() is not a basic building block and it could check for sufficient memory.  As with such things, error handling is an important, but for now, TBD consideration.

// rLineSize is the space available at `rLine`
// The length of the string pointed to by `line` can use `strlen()`
void reverseLine2(char * restrict rLine, size_t rLineSize, const char * restrict line) {
  size_t lineSize = strlen(line) + 1;
  if (lineSize > rLineSize) Handle_Error();
  ...


Updating properties and states I am rewriting a VB.NET application in C#. This code was originally done in 50 lines and I am trying to clean it up. As you can see I have gotten it down to 10 but I still feel its ugly. I want to convert the call and assignments into one Linq statement or method chain. See Comments:

public void Update(IEnumerable<MyObjectproperties> properties)
    {
        var propertiesToBeUpdated = new List<MyObjectproperties>();
        //I am convinced I can do this in one linq statement
        foreach (var output in properties)
        {
            var outputToBeUpdated = _context.MyObjectProperties.FirstOrDefault(x => x.Key == output.Key);
            outputToBeUpdated.propertiesName = output.propertiesName;
            outputToBeUpdated.propertiesValue = output.propertiesValue;
            propertiesToBeUpdated.Add(outputToBeUpdated);
        }

        _context.Entry(propertiesToBeUpdated).State = EntityState.Modified;
        _context.SaveChanges();
    }

What I am hoping this method looks like when I am done is:

public void Update(IEnumerable<MyObjectProperties> properties)
{
    var propertiesToBeUpdated = //My new linq statement here
    _context.Entry(propertiesToBeUpdated).State = EntityState.Modified;
    _context.SaveChanges();
}

I am "okay" at writing complex linq statements but this one is a bit more than I can handle (assuming its possible).

var propertiesToBeUpdated = properties.Select(p => {
    var prop = _context.MyObjectProperties.FirstOrDefault(x => x.Key == output.Key);

    return prop == null ? null :
        new MyObjectProperties {
            Key = prop.propertiesKey,
            Name = prop.propertiesName,
            Value = prop.propertiesValue
        };
}).Where(p => p != null);

The extra set of {} inside your LINQ statement allow you to do multiple lines. If for some reason, you're going to be absolutely POSITIVE that prop would never be null, it would simplify the LINQ a lot more by not having to return nulls (since you have to return SOMETHING for each item in the collection you're iterating). You also then wouldn't need the Where() at the end as well.

UPDATE

If it will never be null, then it is even simpler:

var propertiesToBeUpdated = properties.Select(p => {
    var prop = _context.MyObjectProperties.First(x => x.Key == output.Key);

    return new MyObjectProperties {
            Key = prop.propertiesKey,
            Name = prop.propertiesName,
            Value = prop.propertiesValue
        };
});


Simple evolutionary string matching This is based on a very similar question I asked, found here.

The intent of this program is to start with a set number of random strings of zeros and ones, and eventually evolve them to find one specific string ('111111').

It fulfils its purpose, but what do you think, and how can I improve it?

import random as r
import operator
import time
import os
target = '111111'

class String(object):
    def __init__(self, chromo):
        self.chromo = chromo
        self.fitness = 5
        self.age = 0
def Fitness(string):
    for char in string.chromo:
        if char == '1':
            string.fitness += 1
        else:
            pass

def SelectParents(strings):
    parents = []
    parents.append(max(strings, key=operator.attrgetter('fitness')))
    strings.remove(parents[0])
    parents.append(max(strings, key=operator.attrgetter('fitness')))
    strings.append(parents[0])
    return parents

def PartStringRand():
    string_complete = ''
    for _ in range(4):
        rand = r.randint(0, 1)
        string_element = str(rand)
        string_complete = ''.join([string_complete, string_element])
    return string_complete

def CreateChild(strings, parents):
    for children in range(2):
        string_complete = PartStringRand()
        string_complete += (parents[0].chromo[r.randint(0, 5)] + parents[1].chromo[r.randint(0, 5)])
        child = String(string_complete)
        strings.append(child)

def KillOld(strings):
    for _ in strings:
        if _.age > 2:
            strings.remove(_)

#First strings
strings = []

for _ in range(5):
    string_complete = ''
    for _ in range(6):
        rand = r.randint(0, 1)
        string_element = str(rand)
        string_complete = ''.join([string_complete, string_element])
    string = String(string_complete)
    strings.append(string)
    if string.chromo == "111111":
        strings.remove(string)
for string in strings:
    print string.chromo
raw_input()

#Start incremental mutation
c = 0
for _ in range(5000000):
    #os.system("cls")
    for string in strings:
        Fitness(string)
    parents = SelectParents(strings)
    CreateChild(strings, parents)
    for organism in strings:
        organism.age += 1
        print organism.chromo
        if organism.chromo == '111111':
            print "::...Successfully evolved specifc string '111111' from random strings...::"
            time.sleep(4)
            sys.exit()
    KillOld(strings)
    c += 1
    print "Generation: %s" %c
    #time.sleep(0.3)

One thing that immediately jumped out:

def KillOld(strings):
    for _ in strings:
        if _.age > 2:
            strings.remove(_)

_ is used by convention to indicate "I won't be using this variable", e.g.:

for _ in range(5):
    do_something()

If you are using the variable, give it a better name (even just s). Also, functions' names should be lowercase_with_underscores per PEP-0008.

Next, your mutation seems odd, as it generates four new random characters then picks two from the parents. Instead, I would have done:

def create_child(parents):
    return String("".join(r.choice(c) for c in zip(*(p.chromo for p in parents))))

i.e. for each position, pick randomly between the two parents.

Note that the function returns the new String, rather than adding it to strings. I would say that:

strings.append(create_child(parents))

is clearer than adding to strings as a side-effect. Similarly, I would probably rewrite KillOld to return a new list, rather than mutate its argument (and call it kill_old).

You can simplify

string_complete = ''
for _ in range(6):
    rand = r.randint(0, 1)
    string_element = str(rand)
    string_complete = ''.join([string_complete, string_element])
string = String(string_complete)

to:

s = String("".join(map(str, (r.randint(0, 1) for _ in range(6)))))

(don't use string as a variable name; you may want to use that module later). And rather than append then remove it, check first:

if s.chromo != target:
    strings.append(s)

(Note that you define target then don't use it - replace all '111111' with target and you can change it much more easily in future!)

Rather than calculate the s.fitness for each s, then have a complex key:

parents.append(max(strings, key=operator.attrgetter('fitness')))

make the function fitness that takes a single String and returns the fitness score:

def fitness(s):
    return sum(c == t for c, t in zip(s.chromo, target))

Your key is then e.g.:

parents.append(max(strings, key=fitness))

This also decouples the fitness from the String object itself; you no longer need the s.fitness attribute.

You could also generally improve SelectParents; remove and append again is not efficient.

def select_parents(strings):
    return sorted(strings, key=fitness)[-2:]

This sorts strings in ascending fitness, then returns a sliced list of the last (up to) two items.


Consecutive words match in a dataset I have written an algorithm to do consecutive word match in a dataset

The input is a dataset (list of textlines) and I want to scan that and return repeated word(s) in the dataset along with some metadata like the strings (lines) they matched at.

I am having hard time to wrap my mind around a better approach to hand the consecutive words match. What I have so far is:

hashtable datastructure
I compute the permutations of each sentence's words. So complexity is n+ (n*avg words count). e.g: abc def gg ww will produce the following permutations

-- abc def
-- abc def gg
-- abc def gg ww
-- def gg
-- def gg ww
-- gg ww
I store every possible word combination from each sentence in a dictionary with value of 1
if a word combination exists in the dictionary, I increase the value by 1
using stringBuilder to avoid string immutability
I refine my dictionary at the end to remove any items that has value of 1

I am curios if:
1) this can be improved
2) are there any other datastructures/algo I can use to do the consecutive word matching on a text blob

It is C# now, but we are considering python or Go as well.

public Dictionary<string, List<string>> ConsecutiveMatches { get; set; }

public void Load(List<string> dataset)
{
    for (int i = 0; i < dataset.Count; i++)
    {
        var sentence = dataset[i];
        var words = sentence.Split(" ", StringSplitOptions.RemoveEmptyEntries);

        BuildConsecutiveMatches(words, sentence);
    }

    RefineConsecutiveMatches();
}

private void BuildConsecutiveMatches(string[] words, string sentence)
{
    BuildConsecutiveMatchesRecursive(words, 0, sentence);
}

private void BuildConsecutiveMatchesRecursive(string[] words, int index, string sentence)
{
    if (index >= words.Length - 1)
    {
        return;
    }

    var builder = new StringBuilder(words[index]);
    builder.Append(' ');

    for (int i = index + 1; i < words.Length; i++)
    {
        builder.Append(words[i]);
        var permutation = builder.ToString();
        if (ConsecutiveMatches.TryGetValue(permutation, out var matches))
        {
            ConsecutiveMatches[permutation].Add(sentence);
        }
        else
        {
            ConsecutiveMatches.Add(permutation, new List<string> { sentence });
        }
        builder.Append(' ');
    }

    BuildConsecutiveMatchesRecursive(words, ++index, sentence);
}

private void RefineConsecutiveMatches()
{
    ConsecutiveMatches = ConsecutiveMatches.Where(pair => pair.Value.Count >= 2)
                            .ToDictionary(pair => pair.Key,
                                        pair => pair.Value);
}

The data structure you want is called a trie.  It's often used with letters in the nodes, for doing word search (finding anagrams and so on); in your case the keys would be words and the values would be sub-tries and arrays of line numbers or other metadata.

Given this input:

cat dog pig
dog pig cow

You'd make a structure like:

{
    cat: {
      dog: {
        pig: { "#": [ 1 ] },
        "#": [ 1 ]
      },
      "#": [ 1 ]
    },
    dog: {
      pig: {
        cow: { "#": [ 2 ] },
        "#": [ 1, 2 ]
      },
      "#": [ 1, 2 ]
    },
    pig: {
      cow: { "#": [ 2 ] },
      "#": [ 1, 2 ]
    },
    cow: { "#": [ 2 ] },
}


Printing multiplication table to 12 × 12 Now, a while ago, I said in chat:

  I'm done with BF programming.

Except I guess I was lying. Here's a Brainfuck program I wrote, that prints out the multiplication tables up to 12. Fairly simple; what it does:

Loops through 1-12; let's say the current value we are at is x

Loop 12 times, adding x each time, and prints the result
Resets the number

The code:

Cell 0 is space
Cell 1 is newline
Cell 2 is outer counter
Cell 3 is outer number
Cell 4 is inner counter
Cell 5 is inner number
Cell 6 is copy workspace

>++++[<+++++ +++>-]                                 set cell 0 to space
+++++ +++++                                         set cell 1 to new line

>+++++ +++++ ++                                     set counter to 12
[                                                   while counter is not 0
    >+                                              increment number
    >+++++ +++++ ++                                 set inner counter to 12
    [                                               while inner counter is not 0
        <[>>+>+<<<-]>>>[<<<+>>>-]                   increment inner number

        >++++++++++<<[->+>-[>+>>]>[+[-<+>]>+>>]
        <<<<<<]>>[-]>>>++++++++++<[->-[>+>>]>
        [+[-<+>]>+>>]<<<<<]>[-]>>[>++++++
        [-<++++++++>]<.<<+>+>[-]]<[<[->-<]++++++
        [->++++++++<]>.[-]]<<++++++[-<++++++++>]
        <.[-]<<[-<+>]                               print number gotten off SO

        <<<<<<.                                     print space
        >>>>-                                       go to inner counter
    ]                                               end inner loop
    >[-]<                                           reset number
    <<<.                                            print new line
    >-                                              go to counter
]                                                   end outer loop

Compressed:

>++++[<++++++++>-]++++++++++>++++++++++++[>+>++++++++++++[<[>>+>+<<<-]>>>[<<<+>>>-]>+++++++
+++<<[->+>-[>+>>]>[+[-<+>]>+>>]<<<<<<]>>[-]>>>++++++++++<[->-[>+>>]>[+[-<+>]>+>>]<<<<<]>[-]
>>[>++++++[-<++++++++>]<.<<+>+>[-]]<[<[->-<]++++++[->++++++++<]>.[-]]<<++++++[-<++++++++>]<
.[-]<<[-<+>]<<<<<<.>>>>-]>[-]<<<<.>-]

Output:

1 2 3 4 5 6 7 8 9 10 11 12
2 4 6 8 10 12 14 16 18 20 22 24
3 6 9 12 15 18 21 24 27 30 33 36
4 8 12 16 20 24 28 32 36 40 44 48
5 10 15 20 25 30 35 40 45 50 55 60
6 12 18 24 30 36 42 48 54 60 66 72
7 14 21 28 35 42 49 56 63 70 77 84
8 16 24 32 40 48 56 64 72 80 88 96
9 18 27 36 45 54 63 72 81 90 99 108
10 20 30 40 50 60 70 80 90 100 110 120
11 22 33 44 55 66 77 88 99 110 121 132
12 24 36 48 60 72 84 96 108 120 132 144

Some notes:

The multiplication table is missing formatting. I realise that, and will add it some time later, if I can figure out how to compare two values.
I realise it doesn't work for multiplication tables of 16 or higher, due to BF cell sizes being only 8 bits, or up to 255. The value 16 * 16 would result in 256, setting the value of the cell to be 0. Is this unavoidable? Or is there a trick to it?

Concerns:

Does it make sense? (Considering it's Brainfuck, it shouldn't make sense...)
Do my "comments" make sense?
Are there redundant commands that I can remove/reduce?

The multiplication table is missing formatting. I realise that, and will add it some time later, if I can figure out how to compare two values.

Comparing two values isn't that complicated, once you know how to do it. It is however extremely inefficient if you do it over and over again. Your best bet would be to hook into the printing of the number and print spaces if there are no hundreds or tens.

I managed to do a little proof-of-concept for this, which I am sure can be optimized further. The idea is the following:

Before the loop that prints the number of 100's, set a temporary flag on another cell
If the loop is entered, reset the temporary flag at the end
Outside the loop, go to temporary flag and if it is set, print a space. Clear the flag and go back to the cell.

The unformatted and unoptimized version of this enhanced "print the number" code is as follows:

        >++++++++++<<[->+>-[>+>>]>[+[-<+>]>+>>]
        <<<<<<]>>[-]>>>++++++++++<[->-[>+>>]>
        [+[-<+>]>+>>]<<<<<]>[-]>>

>>>+<<<[

    [x>++++++[-<++++++++>]<.<<+>+>[-]]

>>>-<<<]>>>[++++++++++ ++++++++++ ++++++++++ + . [-]]<<<

<

>>>+<<<[
    [y<[->-<]++++++[->++++++++<]>.[-]]
>>>-<<<]>>>[++++++++++ ++++++++++ ++++++++++ + . [-]]<<<

<<++++++[-<++++++++>]
        <.[-]<<[-<+>]                               print number gotten off SO

I have marked the loop where the 100's are printed with x and the loop for the 10's with y, to make it easier for you to see the changes I did.

  I realise it doesn't work for multiplication tables of 16 or higher, due to BF cell sizes being only 8 bits, or up to 255. The value 16 * 16 would result in 256, setting the value of the cell to be 0. Is this unavoidable? Or is there a trick to it?

There are some different possible solutions to this:

Use a Brainfuck interpreter that supports higher values (Disclaimer: That's my project)
Print value of cell as number for any sized cell
Use bit-width conversions
Perform the multiplication differently. This would involve a completely different approach of storing each individual digit in its own cell and doing the multiplication on a more detailed level, by looping over the digits and multiplying. I won't go into much more detail about this as it would require a lot of work.

I am unfortunately only experienced with using a Brainfuck interpreter that supports higher values. I don't know at all how the Bit-width conversions work, or how you are gonna do the number printing then.

  Does it make sense? (Considering it's Brainfuck, it shouldn't make sense...)

  Do my "comments" make sense?

Surprisingly much sense, yes. For a Brainfuck developer your code is quite easy to follow and your comments are helpful.

  Are there redundant commands that I can remove/reduce?

There are two places in your code where I am, without doing over-analysis, able to find optimization possibilities: How you structure your memory, and how you print the numbers.

When structuring the memory, think about which places you need to go, and in which order. Currently you have positioned the space (value 32) at index 0 on the tape, and yet it is one of the most accessed values. Positioning that closer to the "inner number", will allow you to go to it more easily.

The printing of the number is a common algorithm found at Stack Overflow, which is fine. But I have realized that it is a bit inefficient for printing multiple numbers, as it goes from value 0 to value 48 (ASCII value for '0') multiple times, and resets to 0 every time. It would be better to initialize a '0' into a cell and re-use that cell multiple times, without resetting it.


Merge adjacent occurrences of identical elements in data collection There is a small programming exercise to merge adjacent identical elements in a collection.

Here are two solutions (passing the tests from the exercise).
I am asking for review of the first version using standard algorithms.
The second is just for reference of the expected behavior.

template <typename ForwardIterator, typename OutputIterator, typename Equal, typename Merge>
void merge_adjacent(ForwardIterator first, ForwardIterator last, OutputIterator out, Equal equal, Merge merge)
{
    while(first != last){
    auto start = first;
    auto stop = std::find_if_not(start,last,[&](auto const &t){return equal(*start,t);});
    if(start != stop){
        out = std::accumulate(start+1,stop,*start,merge);
    }
    else{
        out = *start;
    }
    first = stop;
    }

    /*
    while(first != last)
    {
        auto next = *first;
        first++;
        while((first != last) && (equal(next,*first)))
        {
            next = merge(next,*first);
            first++;
        }
        out = next;
    }
    */
}

Generally looks okay (though I would prefer shorter line lengths, and the indentation needs cleaning up).

It seems that we've focused on using std::back_insert_iterator as the output iterator; this leads to a couple of problems that are visible when we switch to a different iterator, such as a collection iterator or raw pointer:

The caller doesn't know where the output finished; we should return the final value of out, just as std::unique_copy() does.
The assignment out = neither indirects nor advances the iterator; it should be *out++ =.

We don't need a separate start copied from first, and we can use std::adjacent_find to determine the range of equal elements:

#include <algorithm>
#include <functional>
#include <numeric>

template <typename ForwardIterator, typename OutputIterator,
          typename Equal, typename Merge>
OutputIterator merge_adjacent(ForwardIterator first, ForwardIterator last,
                              OutputIterator out,
                              Equal equal, Merge merge)
{
    while (first != last) {
        auto stop = std::adjacent_find(first, last, std::not_fn(equal));
        if (stop != last) {
            // advance to include the first of the pair
            ++stop;
        }
        *out++ = std::accumulate(first+1, stop, *first, merge);
        first = stop;
    }

    return out;
}

This version still performs two passes (one for the search, and one for the accumulate).  The commented-out, lower-level version of the function should be able to accept input iterators, as a single-pass algorithm.

Looking at that version, there's only a little to improve.  I'd probably post-increment the iterators while dereferencing, rather than as a separate statement, but that's just a style preference.  One thing I would change would be to use std::move() when finishing with next, to minimise unnecessary copying:

#include <algorithm>
#include <numeric>
#include <utility>

template <typename InputIterator, typename OutputIterator,
          typename Equal, typename Merge>
OutputIterator merge_adjacent(InputIterator first, InputIterator last,
                              OutputIterator out,
                              Equal equal, Merge merge)
{
    while (first != last) {
        auto next = *first++;
        while (first != last && equal(next, *first)) {
            next = merge(std::move(next), *first++);
        }
        *out++ = std::move(next);
    }

    return out;
}


General primality test in C++ I have this code, which I think can be optimized even more, but I can't think of a way of optimizing the main loop to make it faster and more comprehesive. Here's the code:

#include <iostream>
#include <cstdlib>
#include <cmath>

template <class Integral>
bool IsPrime (const Integral &n){
   // Get rid of base cases
   if (n==2 || n==3) return true;

   // Initialize the boolean to an intuitive correct value
   bool is_prime = n%2 != 0 && n%3 != 0;
   unsigned long top = (unsigned long) sqrt(n)+1;

   // This is the loop I want to optimize
   for (unsigned long i=3; i<top && is_prime; i+=6){
      is_prime = (n%(i+2) != 0) && (n%(i+4) != 0);  // kind of loop unrolling...
   }

   return is_prime;
}

// Main program
int main (int argc, char* argv[]){
   // Check passed arguments
   if (argc != 2){
      std::cerr << "Format: " << argv[0] << " <integer>" << std::endl;
      return -1;
   }

   if (IsPrime(strtoul(argv[1], NULL, 0))) return 0;
   else return 1;
}

As you can see, I could reduce the amount of iterations by six times, but going further was worthless.

For the biggest 64-bit unsigned prime number, 18446744073709551557, it spends about 15-17 seconds in an i5, 1.70 GHz processor (which I think can be optimized more).

Simple errors
This code includes <cmath> and <cstdlib>, but then calls unqualified sqrt and strtoul.  Implementations are not required to import these names to the global namespace, so they should be qualified with std:: (or include <math.h> and <stdlib.h> instead).
An unsigned long may not be big enough to hold the integer part of sqrt(n) (for example, if Integer is a 128-bit type, and unsigned long is only 32 bits).  The only safe way (short of heavy template metaprogramming) is to use Integer for the counter.
It's probably worth explicitly returning false for 0 and 1, and for any negative values that crazy people feel compelled to test.
Style
We usually use snake_case (or in some projects, camelCase) for functions, and reserve PascalCase for composite types.  But go along with the conventions of your collaborators where necessary.
Performance
I did manage to get a small improvement (from 9½ seconds down to about 9 here, compiled with gcc -march=native -03) by splitting the condition into two (I think this might be allowing the compiler to execute both tests together).
The best results were obtained by simply parallelizing the algorithm (std::atomic<bool> is quite cheap on my hardware):
#include <atomic>
#include <cmath>
#include <cstdlib>

template <class Integral>
bool is_prime(const Integral &n){
    // Get rid of base cases
    if (n < 2) return false;
    if (n==2 || n==3) return true;

    if (n%2 == 0 || n%3 == 0) return false;

    auto const top = (Integral)std::sqrt(n) + 1;
    std::atomic<bool> is_prime = true;
#pragma omp parallel for
    for (Integral i = 3;  i < top;  i += 6) {
        if (!is_prime) continue;
        if (n%(i+2) == 0) is_prime = false;
        if (n%(i+4) == 0) is_prime = false;
    }
    return is_prime;
}

#include <iostream>
int main(int argc, char* argv[]){
    // Check passed arguments
    if (argc != 2){
        std::cerr << "Format: " << argv[0] << " <integer>" << std::endl;
        return -1;
    }

    if (is_prime(std::strtoul(argv[1], NULL, 0))) return 0;
    else return 1;
}

This got my time down to under 1½ seconds elapsed time, making full use of 8 cores.
I don't know how I can break instead of continue in an OpenMP parallel loop, but I found it didn't hurt too much with a simple composite number such as 625, which evaluated in around 20ms.
Loop unrolling
I did gain a speedup of a further 20% or so by unrolling the loop to consider 30 factors at a time:
#include <atomic>
#include <cmath>
#include <cstdlib>
#include <initializer_list>

template<class Integral>
bool is_prime(const Integral &n)
{
    // Values under 30
    if (n < 2) return false;
    for (Integral i: { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 })
        if (n%i == 0) return n==i;

    auto const top = (Integral)std::sqrt(n) + 1;
    std::atomic<bool> is_prime = true;
#pragma omp parallel for
    for (Integral i = 30;  i < top;  i += 30) {
        if (!is_prime) continue;
        if (n%(i+1) == 0) is_prime = false;
        if (n%(i+7) == 0) is_prime = false;
        if (n%(i+11) == 0) is_prime = false;
        if (n%(i+13) == 0) is_prime = false;
        if (n%(i+17) == 0) is_prime = false;
        if (n%(i+19) == 0) is_prime = false;
        if (n%(i+23) == 0) is_prime = false;
        if (n%(i+29) == 0) is_prime = false;
    }
    return is_prime;
}

int main()
{
    return is_prime(0)
        || is_prime(1)
        || !is_prime(2)
        || is_prime(49)
        || is_prime(961)
        || !is_prime(18446744073709551557u);
}

(I've also simplified main() here, to just run auto tests rather than requiring an argument).


Positioning a container next to its menu I'm a beginner in CSS and need help with a suggestion.

I'm working on a project, in which I managed to position container_main right next to container_menu giving the remaining dimension of the screen, giving it relative position and floating it to the right. container_menu has a dimension and a fixed position specified.

body {
  font-size: 16px;
  font-family: 'Open Sans', sans-serif;
  margin: 0;
  padding: 0;
}

.wrapper {
  width: 100%;
  height: 100%;
  position: relative;
}

/*Contenedor de la barra de navegacion: Columna*/
.container_menu {
  height: 100%;
  width: 18rem;
  /*display: inline-block;*/
  position: fixed;
  background-color: gray;
}

/*Barra de navelación: Elemento
*/
.container_menu .menu {
  width: 100%;
  padding: 0;
}

.container_menu ul {
  list-style-type: none;
}

.container_menu .menu li a{
  color: white;
  display: block;
  padding: 1rem 1.5rem;
  background-color: gray;
}

.container_menu .menu li a:hover {
  color: white;
  background-color: black;
}

.container_main {
  height: 100%;
  width: 66.31rem;
  background-color: black;
  position: relative;
  float: right;
}

p {
  color: white;
  background-color: blue;
  width: 30rem;
  height: 20rem;
  border: .7rem solid white;;
  padding: 5rem;
  margin: 10rem;
}
<!DOCTYPE html>

  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" href="css/style.css" type="text/css">
  <link href="https://fonts.googleapis.com/css?family=Open+Sans:300i,400" rel="stylesheet">
  <title>MDN - Mockup</title>

Teaching Activities

Web Literacy

Leadership Opportunities

Tools

Comunity

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
      ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
      laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in
      voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat
      non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
       ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
       laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in
       voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat
       non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
       ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
       laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in
       voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat
       non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

  <

I would like to know if the characteristics I gave the containers are ideal, or if there is a better way to get the same result using other properties.

I have trouble to realize what were your initial whishes, since the current choices lead to some inconveniences (as it already appears in the reduced "snippet box").

Here is what I can|can't understand:

You want to keep menu always visible and located at the same place regardless how user scrolls.
So using container_menu {position: fixed;} is ok.
You want to have main container occuping the rightmost place, in front of container_menu and starting at the top of the window.
So using container_main {float: right;} is ok.
But why did you also set its postion to relative? Without adding some of top|left|right|bottom properties it changes nothing to its own location. In the other hand, it might be useful to constraint its
 childs if they had for example fixed position but they don't.
So using container_main {position: relative;} is useless.
Since you set fixed width values for container_main and its
 children, depending on the window width it may result in a (likely) unwanted layout: as soon as the window is small enough, container_main partially or totall overwrites container_menu.
So regarding only this point (but see also below) you should add container_menu {z-index: 1;} (or any other
convenient value, depending on other elements not currently showed).
For the same reason, if the window width is smaller than the fixed
 children, their content becomes cropped.
So you had better using a totally different technique, such as setting container_menu postion to fixed, with a left margin equal to container_menuwidth, and using percentage for container_main width

Here is an example using the above technique:

body {
  font-size: 16px;
  font-family: 'Open Sans', sans-serif;
  margin: 0;
  padding: 0;
}

.wrapper {
  width: 100%;
  height: 100%;
  position: relative;
}

/*Contenedor de la barra de navegacion: Columna*/
.container_menu {
  height: 100%;
  width: 18rem;
  /*display: inline-block;*/
  position: fixed;
  background-color: gray;
}

/*Barra de navelación: Elemento
*/
.container_menu .menu {
  width: 100%;
  padding: 0;
}

.container_menu ul {
  list-style-type: none;
}

.container_menu .menu li a{
  color: white;
  display: block;
  padding: 1rem 1.5rem;
  background-color: gray;
}

.container_menu .menu li a:hover {
  color: white;
  background-color: black;
}

.container_main {
  height: 100%;
  margin-left: 18rem;
  background-color: black;
  position: fixed;
  overflow-y: scroll;
}

p {
  color: white;
  background-color: blue;
  width: 60%;
  border: .7rem solid white;;
  padding: 5rem;
  margin: 10rem auto;
}
<!DOCTYPE html>

  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" href="css/style.css" type="text/css">
  <link href="https://fonts.googleapis.com/css?family=Open+Sans:300i,400" rel="stylesheet">
  <title>MDN - Mockup</title>

Teaching Activities

Web Literacy

Leadership Opportunities

Tools

Comunity

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
      ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
      laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in
      voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat
      non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
       ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
       laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in
       voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat
       non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
       ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
       laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in
       voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat
       non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

  <


Approximately matching strings to elements in an array I have two datasets:

A list containing about 4200 string elements
A list containing about 119,000 string elements

I'm taking each element from list 1 and looping through each element in list 2 using the jaro-winkler algorithm to find a "good" match based on a desired confidence threshold. I know that this process will work, but it will take way too long. Does anyone have any suggestions on a better way to do this to speed the process up?

def get_jw_match(data, target):
    """Uses the jaro-winkler algorithm to match strings(names) from the   target(the set from which you want to find matches) to the data(the set for which you are trying to find matches."""
    temp_list = []
    for i, item_data in enumerate(target):
        max = 0
        temp_list.append(0)
        for j, item_target in enumerate(data):
            jw = jellyfish.jaro_winkler(item_data, item_target)
            if (jw > confidence):
                temp_list[i] = jw
    return temp_list

Code Style Review:

Use of Redundant variables: I cannot find any use for max in your
script. Remove it
Better naming: jw could be better described as jw_score. Max is
built_in name, don't use it.
Unnecessary enumerate. You are not using index in inner loop. Remove
enumerate.
I am assuming confidence is global scope. Bad practice. Pass
confidence value as argument. If you must use confidence as global
constant, declare it as CONFIDENCE (caps indicate constant)
Since you are inserting zero as initial value for temp_list there are better ways to achieve that instead of appending zero in loop.
Use item_data while iterating data, and vice-versa. Your naming
convention right now is confusing

Code Design Review

I am having hard time understanding what you wanted to return. Right now, if any of strings for data matches target, your temp_list will have a value for it. But it will have value for "last_matched" only.

If you only care about any good match, then as soon as any value match, break out from inner loop, and your average running time should reduce drastically:

def get_jw_match(data, target, confidence):
    """Uses the jaro-winkler algorithm to match strings(names) from the   target(the set from which you want to find matches) to the data(the set for which you are trying to find matches."""
    score_list = [0] * len(target)
    for i, item_target in enumerate(target):
        for item_data in data:
            jw_score = jellyfish.jaro_winkler(item_data, item_target)
            if (jw_score > confidence):
                score_list[i] = jw_score
                break
    return score_list

Now you would know for which elements in target you have good match, but you don't know any element in data for which you have good match. For that, instead of assigning score, you can assign index for data.
Also, you can then ditch list, and use dictionaries:

def get_jw_match(data, target, confidence):
    """Uses the jaro-winkler algorithm to match strings(names) from the   target(the set from which you want to find matches) to the data(the set for which you are trying to find matches."""
    score_dict = dict()
    for i, item_target in enumerate(target):
        for j, item_data in enumerate(data):
            jw_score = jellyfish.jaro_winkler(item_data, item_target)
            if (jw_score > confidence):
                score_dict[i] = j
                break
    return score_dict

Advantage of this is that you can create reverse dict also.
Which also means, that you should be able to swap inner-outer loops reducing time further. (In general, use inner loop for bigger data, when it short circuit, you save much more time)

If I think, there might be further time-reduction optimizations, but for practical purposes, this would shorten your average running time by magnitude.


C++ for_each_adjacent I was watching a C++ Talk and he showed some code for a basic for_each_adjacent.
I took the base concept and improved it a little bit, I am wondering if i could have any feedback. It automatically deduces the amount of parameters.

My main concern is the remainder. I am not sure to pass in a epilogue function for the remaining variables, or a pointer to remaining variables. If i use an epilogue function, I can foresee some issues with a vectors, because its size can change a run time. Ultimately that would force the epilogue to have the same parameter count as the lambda, and which some of the values would be null, but I would have to set them to 0 and it could be a headache to tell if they are really 0 or supposed to be null? Hopefully that makes sense. Anyways, here is the video which started the whole thing if your curious.

#include <iostream>
#include <numeric>
#include <array>
#include <cstring>
#include <optional>

namespace detail
{
    template<class Function, class Iterator, unsigned... IntegerSequence>
    auto apply(Function f, Iterator first, std::integer_sequence<unsigned, IntegerSequence...>)
    {
        return f(*std::next(first, IntegerSequence)...);
    }
    template<typename F>
    struct arg_count : arg_count<decltype(&F::operator())> {};

    template<typename F,typename Return,typename... Args>
    struct arg_count<Return(F::*)(Args...)const>
    {
        static const unsigned value = sizeof...(Args);
    };

    template<typename F>
    constexpr const unsigned arg_count_v = arg_count<F>::value;
}

//Returns a pointer to the remainder values or the end or nullptr
template<class Iterator,class Function>
auto for_each_adjacent(Iterator begin,Iterator end,Function func)
    -> std::optional<typename std::iterator_traits<Iterator>::pointer>
{
    using value_type = typename std::iterator_traits<Iterator>::value_type;
    const value_type length = std::distance(begin,end);
    const std::ptrdiff_t remainder = length % detail::arg_count_v<Function>;

    for (auto d = length; d >= detail::arg_count_v<Function>; d -= detail::arg_count_v<Function>)
        detail::apply(func,(end - d),std::make_integer_sequence<unsigned,detail::arg_count_v<Function>>());

    return (remainder) ? std::make_optional(end - remainder) : std::nullopt;
}

int main()
{
    std::array<int,256> test_array{};
    std::iota(test_array.begin(),test_array.end(),0);

    std::optional<int*> leftover_values = for_each_adjacent(test_array.begin(),test_array.end(),[](int a,int b,int c) {
        std::cout << a << "," << b << "," << c << "\n";
    });

    std::cout << leftover_values.value()[0]; // 255;
}

template<class Function, class Iterator, unsigned... IntegerSequence>
    auto apply(Function f, Iterator first, std::integer_sequence<unsigned, IntegerSequence...>)
    {
        return f(*std::next(first, IntegerSequence)...);
    }

The Iterator has the requirements of a forward iterator (dereferenceable, forward traversal, multipass).  At the very least, document the iterator type. Also, you should check to make sure the iterator meets the requirements.

The sequence type is too narrow and doesn't support a commonly used sequence like std::index_sequence.

The sequence helpers are defined in <utility>. Don't rely on latent dependencies from other includes, if you reference it, you should include it.

#include <cstddef>       // std::ptrdiff_t
#include <iterator>      // std::iterator_traits, std::next, std::distance
#include <type_traits>   // std::is_base_of
#include <optional>
#include <utility>       // std::make_integer_sequence
/* ... */

    template<class Function, class ForwardIterator, class Index, Index... Indices>
    auto apply(Function f, ForwardIterator first, std::integer_sequence<Index, Indices...>)
    {
        static_assert(
            std::is_base_of<std::forward_iterator_tag,
                typename std::iterator_traits<ForwardIterator>::iterator_category
            >::value, "");
        return f(*std::next(first, Indices)...);
    }

    template<typename F>
    constexpr const unsigned arg_count_v = arg_count<F>::value;

Programmers typically refer to the number of arguments or operands a function or operation takes as the Arity.

What is the arity of [](auto...){}? What is the arity of [](){} and how does that interact with any subsequent divisions?

Try to be consistent with your naming.  While F is commonly Function, you kinda bounce between the two.  Pick one and be consistent.

//Returns a pointer to the remainder values or the end or nullptr
template<class Iterator,class Function>
auto for_each_adjacent(Iterator begin,Iterator end,Function func)
-> std::optional<typename std::iterator_traits<Iterator>::pointer>

Adjacents work like a sliding window, where each subsequent window starts at an offset of 1 from the first element in the previous window, e.g.

$$
[1, 2], [2, 3], [3, 4], ...
$$

The behavior you have implemented is a window over chunk sizes, incrementing from the last element in the previous window instead of the first, e.g.

$$
[1, 2], [3, 4], [5, 6], ...
$$

and some remainder less than the the length.  Consider renaming this function to a name that better represents its action, (for_each_chunked?).

Reading your comment, why not just return std::optional<Iterator> or the Iterator itself? After the calls, begin will always point to the first element in the remaining chunk or end.  The user can check if the returned iterator is at end.

    using value_type = typename std::iterator_traits<Iterator>::value_type;
    const value_type length = std::distance(begin,end);

This doesn't make sense. Iterator::value_type is the type of the values that can be obtained by dereferencing the iterator.  std::list<std::string>::iterator::value_type is std::string.  Just use auto and let std::distance provide the correct type (iterator::difference_type, or std::ptrdiff_t).

    const auto length = std::distance(begin, end);

    const std::ptrdiff_t remainder = length % detail::arg_count_v<Function>;
    for (auto d = length; d >= detail::arg_count_v<Function>; d -= detail::arg_count_v<Function>)
        detail::apply(func,(end - d),std::make_integer_sequence<unsigned,detail::arg_count_v<Function>>());

return (remainder) ? std::make_optional(end - remainder) : std::nullopt;

Instead of trying to figure out if their are enough elements remaining to call a function by arity, just calculate the number of chunks you are going to call f with.  Integral division has the nice property of truncating the remainder.

    auto chunks = length / detail::arg_count_v<Function>;

If you really want the remainder

    auto [chunks, remainder] = std::div(length, detail::arg_count_v<Function>);

Then you just loop on that count.

    constexpr auto indices = std::make_index_sequence<detail::arg_count_v<Function>{};

    while (chunks--) {
        detail::apply(func, begin, indices);
        std::advance(begin, detail::arg_count_v<Function>);
    }

    return begin;

An added benefit it that you can keep supporting forward iterators (std::advance) instead of elevating the requirement to random access iterators (end - d, end-remainder).

  I am not sure to pass in a epilogue function for the remaining variables

Does your use-case require you to have the entire range consumed by functions?  Let the caller decide what to do and just return an iterator.


A fractal picture - John Zells graphics.py This code generates a fractal picture. I'm experimenting with using long variable names to see if the code is still readable without comments. I'm a slow reader and loose track a lot and thought to myself why read twice (variable names and comments).

(When) is it better to replace comments with longer descriptive variable and function names?
Does this code make sense to you?
Would it be better off with comments?

from math import *
from graphics import *

def brushes(win, centerx, centery, brushmaximumradius):
    for brush in range(brushmaximumradius, 0, -1):
        spiralrevolutions = 10 * pi
        spiralanglevector = ((brushmaximumradius - brush) / brushmaximumradius * spiralrevolutions) ** 1.6
        spiralradius= (brushmaximumradius - brush) ** 0.8
        xaxistremble = (spiralradius / 10) * cos(spiralanglevector * 3)
        yaxistremble = (spiralradius / 10) * sin(spiralanglevector * 3)
        xkoordinat = int(spiralradius * cos(spiralanglevector) + xaxistremble) + centerx
        ykoordinat = int(spiralradius * sin(spiralanglevector) + xaxistremble) + centery
        c = Circle(Point(xkoordinat, ykoordinat), brush)
        c.setWidth(0)

        period = 1.3
        perioddisplacement = 0.15
        maximumlightintensity = 223
        lowestlightintensity = 22
        lightintensityatangle = int(sin(brush/brushmaximumradius*pi/2 * period + perioddisplacement) * maximumlightintensity + lowestlightintensity)

        amplitude = amplitudedisplacement = 0.5
        cirkelRGBred = int(lightintensityatangle * (sin(spiralanglevector) * amplitude + amplitudedisplacement))
        cirkelRGBgreen = int(lightintensityatangle * (sin(spiralanglevector + pi/2) * amplitude + amplitudedisplacement))
        cirkelRGBblue = int(lightintensityatangle * (sin(spiralanglevector + pi) * amplitude + amplitudedisplacement))
        c.setFill(color_rgb(cirkelRGBred, cirkelRGBgreen, cirkelRGBblue))
        c.draw(win)

windowwidth = 1350
windowheight = 730
win = GraphWin("My Circle", windowwidth, windowheight)
win.setBackground(color_rgb(55, 55, 55))

centerx = int(windowwidth / 2)
centery = int(windowheight / 2)
windowcornerfitting = 1.04
brushmaximumradius = int(windowcornerfitting * sqrt(centerx**2 + centery**2))

brushes(win, centerx, centery, brushmaximumradius)

win.getMouse()
win.close()

(When) is it better to replace comments with longer descriptive variable and function names?

That's one of the finer arts of programming, and varies by language. Much of the time, code should be clear enough and variable names sensible enough that comments are rarely needed, except for in the important places like method docstrings (which should exist regardless).

Since Python is not a strongly-typed language, it needs a little bit of encouragement to add type information, but it can still be (kind of) done via PEP484. Doing this will further enhance your ability to understand the code without needing to rely on explanatory comments. For example (these are only educated guesses):

def brushes(win: GraphWin, center_x: int, center_y: int, brush_max_radius: float):

Note the underscore convention for Python variable names.

  Does this code make sense to you?

I mean... not really? But that's just because I don't understand the fractal algorithm.

Some of the variable names are not English - koordinat? Since most of your other names are English, you should make this English, too. Some people argue that English is the universal language of development. I'm less convinced, but at least you should be internally consistent.

brushes is a confusing name. A method name should be an action - perhaps draw_brushes.

Another trick to get shorter, but still understandable, variable names - drop redundant information. windowwidth is the only width in your code, and it's used right next to the place where it's called, so call it simply WIDTH (capitals because it's a constant).

  Would it be better off with comments?

Yes, definitely - but only comments that are useful.

A comment like this:

# spiral revolutions
spiral_revolutions = 10*pi

is useless. However, a comment like this is very important and useful:

"""
This is an implementation of the fractal algorithm xxx from chapter
yyy of Zells' Python Programming.

:param win: The window object to which the fractal will be drawn.
:param center_x: The x-coordinate of the fractal's origin, in window space.
:param center_y: The y-coordinate of the fractal's origin, in window space.
:param brush_max_radius: The maximum radius of the circles constituting the fractal.
"""


Proper cellForRowAtIndexPath cell return in Swift So I have started creating a UITableView with multiple prototype cells. I currently have the below code setup, which works fine

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell: UITableViewCell?

    if indexPath.row == tableView.numberOfRowsInSection(0) - 1 {
        cell = tableView.dequeueReusableCellWithIdentifier("AddSetCell")
    } else {
        cell = tableView.dequeueReusableCellWithIdentifier("SetCell") as! SetTableViewCell
    }

    return cell!
}

I have learned to avoid using ! as much as possible when dealing with optionals. However, I also don't like creating an if let statement a thousand times. In theory, cell should never be nil because
my code only allows cell to be one of the identifiers above. However it still makes me nervous in terms of bad practice.

If I do var cell = UITableViewCell, I have to unwrap both of the calls to dequeueReusableCellWithIdentifier or XCode gets mad at me.

Which is the best approach for this case?

First note that

func dequeueReusableCellWithIdentifier(identifier: String, forIndexPath indexPath: NSIndexPath) -> UITableViewCell

is preferred over

func dequeueReusableCellWithIdentifier(identifier: String) -> UITableViewCell?

because – according to the documentation –

  This method uses the index path to perform additional configuration based on the cell’s position in the table view.

and because it returns a non-optional UITableViewCell.

If you just want to return one of the two possible prototype cells
then you don't need to cast at all.

But there is no need to make cell an optional. You can even
declare it as a constant, this allows the compiler to verify that it is assigned a value exactly once
on each code path.

let cell: UITableViewCell
if condition {
    cell = tableView.dequeueReusableCellWithIdentifier("AddSetCell", forIndexPath: indexPath)
} else {
    cell = tableView.dequeueReusableCellWithIdentifier("SetCell", forIndexPath: indexPath)
}
// ... set up common properties ...
return cell

If you want to assign specific properties to the different cell types
then you need to cast to the respective type.
Since  you define the  custom class SetTableViewCell for the prototype cell with identifier "SetCell" in the Storyboard,

tableView.dequeueReusableCellWithIdentifier("SetCell", forIndexPath: indexPath)

is guaranteed to return an instance of SetTableViewCell, either by
re-using a cell or by instantiating a new one from the prototype.
This is one of the legitimate use-cases of the
forced cast operator as!. Or to put it differently: The cast can only
fail if you did not define the prototype cell correctly. That would be
a programming error and should be detected early.

let cell: UITableViewCell
if condition {
    let aCell = tableView.dequeueReusableCellWithIdentifier("AddSetCell") as! AddTableViewCell
    // ... set up specific properties ...
    cell = aCell
} else {
    let sCell = tableView.dequeueReusableCellWithIdentifier("SetCell") as! SetTableViewCell
    // ... set up specific properties ...
    cell = sCell
}
// ... set up common properties ...
return cell

or alternatively with "early return":

if condition {
    let aCell = tableView.dequeueReusableCellWithIdentifier("AddSetCell") as! AddTableViewCell
    // ... set up properties ...
    return aCell
} else {
    let sCell = tableView.dequeueReusableCellWithIdentifier("SetCell") as! SetTableViewCell
    // ... set up properties ...
    return sCell
}

that is a matter of personal choice.

Finally, if you write your condition as

indexPath.row == tableView.numberOfRowsInSection(indexPath.section) - 1

then it will work for all sections, not only the first one.


Efficient implementation of a Dynamic Programming challenge I wrote the following code for a Dynamic Programming problem but the online judge gives me a 'Time Limit Exceeded' error.  How can I optimize this code to make it more efficient, especially the part of initializing the DP array to 0.0 (I am using memset() right now)?

Problem:

There are N values in an array. Two players take turn by turn to play the game.
Player1, with 50% probability can pick the left most value or the right most value.
3.If there is only one value left in the array, the player, having no more choice, will just pick up that value.
Question is to calculate the expected sum of values that First player can obtain.
\$T <= 500\$ and \$N <= 2000\$

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstring>
using namespace std;
#define max  2000
int main(){
    int T;
    double **dp = new double*[max];
    for(int i=0; i<max; ++i)
            dp[i] = new double[max];
    cin >> T;
    while(T--){
            int N;
            cin >> N;
            int V[N];
            for(int i=0; i<N; ++i)
                    cin >> V[i] ;
            for(int i=0; i<N; ++i)
                    memset(dp[i], 0.0, sizeof(dp[0][0])*N);
            for(int i=0; i<N; ++i)
                    dp[i][i] = V[i];
            for(int j=1; j<N; ++j)
                    for(int i=j-1; i>=0; --i){
                            dp[j][i] = 0.5 * ( dp[i][i] + dp[j][j] + dp[j-1][i+1] );
                            if( (i+2)<N )
                                    dp[j][i] += (0.250 * dp[j][i+2]);
                            if( (j-2)>=0 )
                                    dp[j][i] += (0.250 * dp[j-2][i]);
                    }
            cout << setprecision (5) << dp[N-1][0] << endl;
    }
    for(int i=0; i<max; ++i)
            delete[] dp[i];
    delete dp;
    return 0;
}

It looks like the nested loops of your algorithm are using only three rows of the dp matrix:

dp[j]
dp[j-1]
dp[j-2]

You can use this observation to transform your solution from one requiring O(N^2) space to one requiring O(N) space by changing

double **dp = new double*[max];

to

double dp[3][max]; // Now your matrix has a chance of fitting on the stack

and adding %3 to all operations that touch the first index of the dp matrix.

This may or may not eliminate the timeout, but it will make your program better by reducing the amount of resources that it needs to operate.


Finding overlapping time intervals for two games I am trying to learn Java by doing some (easy) ACM TCPC problems. The problem is finding number of overlapping times intervals. If there a one, a fight will happen between the two games connected to those intervals.

Example

Input

1    // this is the number of cases.
3    // the number of time intervals
1 5  // The first intervals is [1, 5]
1 2  // ...
2 4

The comment are mine.

Output:

  Case 1: 3

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

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Compo {
  public static void main(String[] args) throws IOException {
    File fin = new File("./compo.in");
    numberOfFight(fin);
  }

  private static int numberOfFight(File fin) throws IOException {
    try (BufferedReader br = new BufferedReader(new FileReader(fin))) {
      int numberOfCases = Integer.parseInt(br.readLine());
      int i = 0;
      int numberOfGame = 0;
      String line;

      while (i < numberOfCases) {
        numberOfGame = Integer.parseInt(br.readLine());
        List<int[]> games = new ArrayList<int[]>(numberOfGame);
        for (int j = 0; j < numberOfGame; j++) {
          line = br.readLine();

          int[] game = new int[2];
          game[0] = Integer.parseInt(line.split(" ")[0]);
          game[1] = Integer.parseInt(line.split(" ")[1]);
          games.add(game);
        }

        int fights = 0;
        for (int j = 0; j < numberOfGame - 1; j++) {
          for (int k = j + 1; k < numberOfGame; k++) {
            if (fight(games.get(j), games.get(k))) {
              fights++;
            }
          }
        }
        i++;
        System.out.println("Case "+i+": "+fights);
      }
    }
    return 0;
  }

  private static boolean fight(int[] game1, int[] game2) {
    // Check if the games overlap
    return ((game1[0] <= game2[0]) && (game2[0] <= game1[1])) || ((game1[0] <= game2[1]) && (game2[1] <= game1[1]));

  }
}

Depends on what you want. Typically programming challenges are not validated on clean code. But on correctness and efficiency.

  Use libraries:

You are having a lot of duplicate code, parsing numbers. Check out the Scanner class which has these methods builtin.

https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html

Remark: When efficiency is important, Scanner may be a lot slower than BufferedReader. In this case I still recommend putting the one time effort of creating your own custom read methods.

  Efficiency:

You are looping over all intervals. This means you have to check all intervals with each other. Hence complexity: O(N²)

This is a well known problem, you could find a lot of reading about this online if you want.

  General:

Write your overlap with less comparissons.

// overlaps when A starts before B ends AND B starts before A ends
private static boolean fight(int[] game1, int[] game2) {
    return game1[0] < game2[1] && game2[0] < game1[1];
}

For readability I would recommend create a class instead of working with int[2]. Typos are sometimes difficult to track.

class Game
{
    private int start;
    private int end;

    public Game(int start, int end)
    {
        this.start = start;
        this.end = end;
    }

    public bool overlaps(Game other)
    {
        return this.start < other.end && other.start < this.end;
    }
}


Updating generated file With this algorithm I update an automatically generated verilog-file. The update within this file is done by commenting the assigned wires to specific ports of module instances.

Is there a better, more elegant or more optimized way to do this than with this algorithm?

file_name = "test.v"    # name of the verilog file
test = ".test"          # port name 1
tezt = ".tezt"          # port name 2
dummy = []              # buffer for the updated string

with open(file_name, "r+") as f:
    lines = f.readlines()
    f.seek(0)
    f.truncate()                                 # clear the file
    for line in lines:
        if test in line or tezt in line:         # check if one of the ports is in the line
            if line[line.index('(')+1] != '/':   # check if the assigned wire is already is commented
                 for c in line:                  # update the line and comment the wire name within the brackets
                    if c == ')':
                        dummy.append("*/")
                    dummy.append(c)
                    if c == '(':
                        dummy.append("/*")
                 line = line.replace(line, "".join(dummy))    # replace the old line with the new string
                 dummy.clear()
        f.write(line)
    f.close()

You don't need to manually call f.close(), that's what the with is for.
It looks like line = line.replace(line, "".join(dummy)) can just be line = "".join(dummy).
It's clearer to define dummy in the if statement:

This means that it's in the correct scope, meaning we don't have to look out for it being used elsewhere.
It also means that you can remove dummy.clear().

Personally I would merge your two if statements together, to reduce the arrow anti-pattern.
It looks like dummy and your for loop can be replaced with str.maketrans and str.translate.

untested

file_name = "test.v"
test = ".test"
tezt = ".tezt"
trans_table = str.maketrans({'(': '(/*', ')': '*/)'})

with open(file_name, "r+") as f:
    lines = f.readlines()
    f.seek(0)
    f.truncate()
    for line in lines:
        if ((test in line or tezt in line)
            and line[line.index('(') + 1] != '/'
        ):
            line = line.translate(trans_table)
        f.write(line)


Palindrome-testing Java program for an interview In an interview I was asked to write a function that would take an input word and return true if the word is a palindrome. At first I used an approach using StringBuilder but the interviewer said that wasn't allowed and to use a for loop instead. I scored 7/9 so I'm guessing it's possible to improve this:

public static boolean isPalinedrome(String word) {

    for(int i = 0; i < word.length(); i++) {
        if(word.charAt(i) != word.charAt(word.length() - 1 - i)) {
            return false;
        }
    }
    return true;
}

I added the following to the main() section to test

String input = "racecar";
if(isPalinedrome(input)) {
    System.out.println(input + " is a palinedrome");
} else {
    System.out.println(input + " is not a palinedrome");
}

The way I test it seems to be ugly. Is there a standard way to test a new function? I guess if you're in an advanced enough environment you would have test cases for JUnit to run against it, but anything simpler that could be done in an interview with a cloud IDE?

It's enough to loop until word.length() / 2, as this will compare the first half with the second half, so no need to go until the end.

As you use word.length() multiple times, you could extract it to a helper variable.

There is a typo in the method name.

As for testing, yes, JUnit is the way to go. In a cloud IDE, you could create a helper method that takes a single string, calls isPalindrome and prints the result. That way you can test multiple cases easily, by adding one line per case. It's important to try to cover corner cases and potentially interesting cases, not just the "happy path". For example:

palindrome with even length
palindrome with odd length
single letter
empty string
non-palindromes

During an interview, it might also be worth mentioning the trade-off between comparing characters using .charAt, or using the array of characters returned by .toCharArray.


Inserting users using PDO prepared statements I made a small script to update fields in a database. I'm using PDO to connect to MySQL. All the business logic of PHP is in the top half of the file, and the form is at the bottom.

Here is the full code. If it's a POST request, it opens the PDO connection and inserts the values from the form.

<?php

/**
 * Use an HTML form to update the users table
 * with new entries.
 *
 */

if ($_POST)
{

    require_once "config.php";
    require_once "common.php";

    try
    {
        $connection = new PDO($dsn, $username, $password, $options);

        $sql = "INSERT INTO users (firstname, lastname, email, age, location)
                VALUES (:firstname, :lastname, :email, :age, :location)";

        $statement = $connection->prepare($sql);

        $statement->execute(array(
            "firstname" => $_POST['firstname'],
            "lastname"  => $_POST['lastname'],
            "email"     => $_POST['email'],
            "age"       => $_POST['age'],
            "location"  => $_POST['location']
        ));

    }

    catch(PDOException $error)
    {
        echo $sql . "
" . $error->getMessage();
    }
}
?>
<!doctype html>

    <meta charset="utf-8">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel="stylesheet" href="css/primitive.css">
    <title>Update Users</title>

Add a user

            <form method="post">
                First Name
                <input type="text" name="firstname" id="firstname">
                Last Name
                <input type="text" name="lastname" id="lastname">
                Email Address
                <input type="text" name="email" id="email">
                Age
                <input type="text" name="age" id="age">
                Location
                <input type="text" name="location" id="location">
                <input type="submit" value="Submit">
            </form>

I know I can sanitize HTML input by doing something like:

function escape($html)
{
    return htmlspecialchars($html, ENT_QUOTES | ENT_SUBSTITUTE, "UTF-8");
}

// example use
escape($_POST['firstname']);

I'm wondering how necessary it is when I'm using a prepared statement. I'm still a bit confused about sanitization and filtering, and when it's necessary. I know it's necessary when printing data out to HTML, but not sure how much when receiving input to PDO.

Please forgive any and all ignorance. I'm writing this from scratch and doing my best to teach myself PHP without frameworks.

Do not escape SQL query parameters when using PDO. PDO will do the right thing. If you call unnecessary escaping functions "just in case", then you end up mangling the data. That's how you get junk like "this &amp; that" appearing in some broken web pages.

When would you want to escape text using htmlspecialchars()? When outputting text to be embedded in HTML. For example,

 <input type="text" name="firstname" id="firstname" value="<?php echo escape($_POST['firstname']); ?>">

If you didn't call htmlspecialchars() on $_POST['firstname'], then a malicious user could break the page by inputting a name that contained double-quotes.


nonstd:: non standard classes I am looking for people's opinions on my use of a set of non standard "standard classes". Basically I am working on a 'modern' C++ Lexical Analyser and Parser Generator duo to replace flex and GNU Bison which will work as a library and tool combination.

I have a number of non standard extensions of std library classes. The reason I am needing these nonstandard classes is to be able to express algorithms more succinctly and nearer the mathematical equivalents.

Here's a vector class that allows extension via operator[], which also has a generic Base parameter, allowing the vector to be based at 1 rather than zero, for example. And I return indices as size_t's rather than iterators as indexes are more useful in a traditional mathematical sense.

//
//  vector.h - auto sizing 'vector' class
//
//  Copyright (c) 2015-2016 Aaron Nathaniel Gray under the MIT License
//

#include <vector>
#include <iostream>

template<typename T, size_t Base = 0>
class vector : public std::vector<T> {
public:
    vector() : std::vector<T>() {}
    vector(const std::initializer_list<T> il) : std::vector<T>(il) {}
    ~vector() {}

    iterator last() { return end() - 1; }
    const_iterator clast() const { return end - 1; }

    size_t size() const { return std::vector<T>::size(); }
    void resize(size_t _size) { std::vector<T>::resize(_size); }
    T& operator [] (size_t index) {
        if (index + 1 - Base > size())
            resize(index + 1 - Base);
        return std::vector<T>::operator[](index - Base);
    }
    const T operator [] (size_t index) const {
        if (index + 1 - Base <= size())
            return std::vector<T>::operator[](index - Base);
        else
            return T();
    }
    signed find(const T& value) {
        auto i = std::find(cbegin(), cend(), value);
        if (i != cend())
            return i - cbegin() + Base;
        else
            return -1;
    }
    size_t add(const T value = T()) {
        push_back(value);
        return size() - 1 + Base;
    }
    std::string toString() const;
};

template<typename T, size_t Base = 0>
std::string vector<T, Base>::toString() const {
    std::stringstream ss;

    for (size_t element = Base; element < size() + Base; ++element) {
        ss << (*this)[element];
        if (element < size() + Base)
            ss << ",";
        ss << std::endl;
    }

    return ss.str();
}

I am wondering whether to use the namespace nonstd::vector.

I also have non-standard set, deque, and a very extensive bitset class.

Example:

nonstd::vector<size_t> accept;

for(auto state : states) {

    .... accept = ....

    this->accept[state->index] = accept;
}

Here, state->index may not be sequential or contiguous and maximum value not known, so push_back will not work.

And a find example:

nonstd::vector<Production*> productions;

...

size_t productionIndex = production.find(aProduction);

Standard classes and derivation

If a class does not have a virtual destructor it is not supposed to be derived from. Before we got the finally key word this was the indicator you were supposed to use before deriving from a class.

But its safe unless you start taking pointers to your class. So to make sure it is not used incorrectly you should put it in a private context (so nobody else can use it apart from your library).

Inheriting functions.

You don't need to copy functions if they have the same name (that's the point of inheriting).

size_t size() const { return std::vector<T>::size(); }
void resize(size_t _size) { std::vector<T>::resize(_size); }

Don't need these. You automatically inherit them.

Inheriting Constructors.

You can use the using clause to inherit all the constructors.

vector() : std::vector<T>() {}
vector(const std::initializer_list<T> il) : std::vector<T>(il) {}
~vector() {}

Can be replaced with:

using std::vector::vector;

Identifiers with leading underscore.

Don't use them.

void resize(size_t _size)
                   ^^^^^

DO you know the rules about a leading underscore. I do (you got it correct). But can you assume that all the people reading your code know (you can't and most get it wrong). So prefer not to use a leading underscore on your identifiers.

Operator[] is supposed to be fast

The operator[] is an unchecked access into the data. You use this when you know that your index is guaranteed correct. We also have a checked access at() which is used when you can't guarantee that your index is in range.

T& operator [] (size_t index) {
    if (index + 1 - Base > size())
        resize(index + 1 - Base);
    return std::vector<T>::operator[](index - Base);
}
const T operator [] (size_t index) const {
    if (index + 1 - Base <= size())
        return std::vector<T>::operator[](index - Base);
    else
        return T();
}

Your operator[] is severely restricting the performance of your code for people that know that their access is good.

for(int loop=0;loop < v.size(); ++loop)
{
    doStuff(v[loop]);  // Why do I need to check loop is in bounds
                       // each time? It is guaranteed to be in bounds
                       // by the context.
}

When people use operator[] they have already checked (or by context guranteed) that the index is in bounds. You should not be checking. You should provide an alternative function that checks and resizes.

find does not return an iterator

It seems strange that find does not return an iterator.

signed find(const T& value) {
    auto i = std::find(cbegin(), cend(), value);
    if (i != cend())
        return i - cbegin() + Base;
    else
        return -1;
}

What's all this pointer maths.

        return i - cbegin() + Base;

Use the standard functions.

    return std::distance(cbegin(), i) + Base;

Constructing a string is easy

Your toString() function is bad as all that work can be done using the std::string constructors (also the standard library uses to_string() you should follow the same convention).

std::string to_string() const
{
    return std::string(cbegin(), cend());
}

Note: this does nothing.

    ss << std::endl;

You should probably never use std::endl. It is actually very rare that you want to flush a stream (they flush themselves at the best times anyway being a human you will just make the code in-efficient by trying to overcompensate).

Namespace

You should definitely put your code into its own namespace. The name vector is just too common.

Sure nonstd seems like a fine name.

Personally I use an initial uppercase letter for all user defined types and namespaces. This allows me to quickly spot an object from type.

namespace ThorsAnvil
{
    class BlaBla
    {
        public:
            BlaBla(int x);
    };
    int bla(int y);
}

int main()
{
    namespace TA = ThorsAnvil;
    int data1 = 15 + TA::BlaBla(15); // I constructed an object here.
    int data2 = 16 + TA::bla(15);    // I called a function here.
}

So I would call your stuff.

namespace NonStd
{
    template<typename T>
    class Vector { /* STUFF */};
};

template<typename T>
#if SomeNormalSituation
using Vec = NonStd::Vector<T>;
#else
using Vec = std::vector<T>;
#endif;

int main()
{
    Vec<char>   data;
}

Idea

Overall find.

  A vector class that allows extension via operator [] which also has a generic Base parameter allowing the vector to be based at 1 rather than zero for example.

Your crazy this is going to cause you so many issues. CS people are so used to their arrays being zero based that any other number is just going to cause errors.

Thoughts

  Basically I am working on a 'modern' C++ Lexical Analyser and Parser Generator duo to replace flex and GNU Bison

Hmm. I love flex/bison. Use them all the time. It will be hard to replace something that is already near perfection. There is a reason why nobody has tried to improve them in years (and all attempts to replace them have failed).


Fast execution of function which resembles matrix multiplication I am trying to make the following function as fast as possible on large matrices. The matrices can have dimensions in the range of 10K-100K. The matrix values are always between 0 and 1. The function resembles matrix multiplication, but with log operations in the inner loop. These log operations appear to be a bottleneck.

I tried various ways of using Numba and Cython. I also tried writing as much as I could with Numpy. I also experimented with doing fewer memory lookups, but this did not seem to give much advantage. The fastest version is below. High precision is greatly preferred, but if there is a way to increase speed at its expense, that would also be appreciated. Thank you for your feedback.

import numpy as np
import numba
from numba import njit, prange

@numba.jit(nopython=True, fastmath=True, parallel=True)
def f(A, B):

    len_A = A.shape[0]
    len_B = B.shape[0]
    num_factors = B.shape[1]
    C = np.zeros((len_A, len_B))

    for i in prange(len_A):
        for j in prange(len_B):
            for a in prange(num_factors):

                A_elem = A[i,a]
                B_elem = B[j,a]
                AB_elem = (A_elem + B_elem)/2

                C[i,j] += A_elem * np.log(A_elem/AB_elem) + \
                          B_elem * np.log(B_elem/AB_elem) + \
                          (1-A_elem) * np.log((1-A_elem)/(1-AB_elem)) + \
                          (1-B_elem) * np.log((1-B_elem)/(1-AB_elem))

    C = (np.maximum(C, 0)/2*num_factors)**0.5
    return C

#A = np.random.rand(10000, 10000)
#B = np.random.rand(10000, 10000)
#f(A, B)

I think you can rearrange your equation from

\$
AB_{ija} = \frac{1}{2}(A_{ia} + B_{ja})
\$

\$
\begin{align}
C_{ij} = \sum_a & A_{ia}\cdot \log\left(\frac{A_{ia}}{AB_{ija}}\right) + B_{ja}\cdot \log\left(\frac{B_{ja}}{AB_{ija}}\right) + \\
&+ (1-A_{ia})\cdot \log\left(\frac{1-A_{ia}}{1-AB_{ija}}\right) + (1-B_{ja})\cdot \log\left(\frac{1-B_{ja}}{1-AB_{ija}}\right)
\end{align}
\$

to

\$
\begin{align}
C_{ij} = \sum_a & A_{ia} \cdot \log A_{ia} + B_{ja}\cdot \log B_{ja} - 2AB_{ija}\cdot \log AB_{ija}\\
&+ (1-A_{ia}) \cdot \log (1-A_{ia}) + (1-B_{ja})\cdot \log (1-B_{ja}) - 2(1-AB_{ija})\cdot \log (1-AB_{ija})\\
= \sum_a & A_{ia}\cdot \log A_{ia} + \sum_a B_{ja}\cdot \log B_{ja} - 2\sum_a AB_{ija}\cdot \log AB_{ija} + \dots
\end{align}
\$

This will at least save you some divisions, which can be a bit slower. But the crucial part is to see that you can compute the sums separately. The sums with only \$A\$ or \$B\$ are a lot less computation, which you would otherwise repeat, and only the combined terms are (computationally) hard.

It also means that there is probably a  numpy way to eliminate at least some of your for loops, which might give you a bit of a speedup, although numba will negate some of that (by already having sped up the for loops). The hard part is then to use the numpy broadcasting correctly to achieve this, which is left as an exercise for now :).


WiFi adapter settings Yesterday I have found that I can't use the wireless network at some spots in my house. I used another modem as a WiFi booster and I managed to cover these spots.

The problem is that when I go to these dead spots I need to use static IP and change my primary DNS servers, or I get limited connection. Also, I still want to use DHCP when I'm not in these spots.

I have written two Batch files and a Python script to define the wireless adapter settings. I would like someone to take a look and suggest how to improve it.

Batch Files (I'm using shortcuts because of the option to run them as administrator)

DeadSpots.bat.ink

netsh interface ip set address "Wi-Fi" static 192.168.x.x 255.255.255.0 192.168.x.x
netsh interface ip set dns "Wi-Fi" static 192.168.x.x primary # This is the second modem
netsh interface ip add dns "Wi-Fi" ISP.dns.IP index=2

Regular.bat.ink

netsh interface ip set address "Wi-Fi" dhcp
netsh interface ip set dnsservers "Wi-Fi" source=dhcp

Python code

import subprocess as sub

def WiFi():
    filepath1 = Path_To_DeadSpots.bat.ink
    filepath2 = Path_To_Regular.bat.ink
    loc = input("Please choose your location: 1-Rooms, 2-Rest \n")
    while(loc != "1" and loc != "2"):
        print("Wrong input, please choose again")
        loc = input("Please choose your location: 1-Rooms, 2-Rest \n")
    if loc == "1":
        p = sub.Popen(filepath1,shell=True,stdout=sub.PIPE)
    else:
        p = sub.Popen(filepath2,shell=True,stdout=sub.PIPE)
WiFi()

This seems pretty straightforward, so I don't have a ton of critiques.

Naming

PEP8 conventions say your function should probably be named wifi (which is not a great name)

The names filepath1 and filepath2 aren't very helpful.

Input

You're repeating yourself unnecessarily while getting user input.  Furthermore, try to keep your functions pure and handle user input separately.

Magic numbers

It might be cleaner to make "1" and "2" constants.  Even cleaner is to use a dictionary, like I did.

Unused variables

You never use p, so I don't see a reason to assign to it.

Here is my full rewrite of your Python program.  I don't know enough about the batch scripting to comment there.

import subprocess as sub

room_to_batchfile = {
    "1": "Path/To/DeadSpots.bat.ink",
    "2": "Path/To/Regular.bat.ink"
}

def setup_wifi(room):
    sub.Popen(room_to_batchfile[room], shell=True, stdout=sub.PIPE)

if __name__ == '__main__':
    loc = ""
    while loc not in room_to_batchfile:
        loc = input("Please choose your location: 1-Rooms, 2-Rest \n")

    setup_wifi(loc)

Honestly, given the small nature of this it might not even make sense to make it a function.  If you plan on expanding this then it might be worthwhile to implement these changes.