Flattening a HashMap of (String → ArrayList) to an ArrayList I have one HashMap<String, ArrayList<String>> and I want to concatenate a key with every item in its ArrayList.

Consider the following demo program:

HashMap<String, ArrayList<String>> map1 = new HashMap<String, ArrayList<String>>();

        ArrayList<String> map11 = new ArrayList<String>();
        map11.add("a");
        map11.add("b");
        map1.put("1", map11);

        ArrayList<String> map12 = new ArrayList<String>();
        map12.add("c");
        map12.add("d");
        map1.put("2", map12);

ArrayList<String> finalList = new ArrayList<String>();
        for (Entry<String, ArrayList<String>> ent : map1.entrySet()) {
            ArrayList<String> ls = ent.getValue();
            for (String string : ls) {
                finalList.add(ent.getKey()+" = "+string);
            }
        }

This is working fine and giving expected output. The value of finalList after executing program is:

  [1 = a, 1 = b, 2 = c, 2 = d]

If there is any better and faster way to do this, please let me know. In the actual project ArrayList, every HashMap entry contains thousands of items.

If it's really a list of entries that you want, then this is about as fast as it gets.

But it can be better:

Use interface types instead of implementations, for example List instead of ArrayList and Map instead of HashMap
Java 6 is no longer supported, and in Java 7 you should use the diamond operator to create new instances, for example: Map<String, List<String>> map1 = new HashMap<>();
Use better, more natural names, for example:

entry is better than ent
list is better than ls

Use unit tests to verify your implementation

Based on the above, here's an alternative implementation:

public static List<String> concatMapEntries(Map<String, List<String>> map) {
    List<String> finalList = new ArrayList<>();
    for (Map.Entry<String, List<String>> entry : map.entrySet()) {
        List<String> list = entry.getValue();
        for (String string : list) {
            finalList.add(entry.getKey() + " = " + string);
        }
    }
    return finalList;
}

And unit test:

@Test
public void testExample() {
    Map<String, List<String>> map1 = new HashMap<>();

    List<String> map11 = new ArrayList<>();
    map11.add("a");
    map11.add("b");
    map1.put("1", map11);

    List<String> map12 = new ArrayList<>();
    map12.add("c");
    map12.add("d");
    map1.put("2", map12);

    assertEquals("[1 = a, 1 = b, 2 = c, 2 = d]", concatMapEntries(map1).toString());
}


Android Gradle script in multi-module project I have a multi-module Android project built by Gradle with the new Android-plugin 0.10+.

All modules are mainly configured from the root project.

I've enable the new code coverage feature. The major problem is that only some modules contain test code and when I enable code coverage on modules that don't contain test code, it fails.

I would like to avoid enabling code-coverage in each module build.gradle file so that when someone add test code in a module that wasn't tested, there is no need to modify the build.gradle.

I ended up with the following code where code coverage is enabled only when the androidTest sourceSet contains at least one .java source file. It works as expected, but I'm wondering if there is a nicer way of doing this.

Here is the relevant portion of the root project build.gradle script:

configure(subprojects.findAll()) {
    if(it.name.equals('MyApp')){
        apply plugin: 'android'
    }else{
        apply plugin: 'android-library'
    }

    android {
        ...
        //my question is really about the follong 4 lines :
        //is there a simpler way to get java source file count in the androidTest sourceSet ?
        def androidTestJavaSourceFileCount = 0
        sourceSets.androidTest.allJava.each {
            androidTestJavaSourceFileCount ++
        }
        println("androidTestJavaSourceFileCount in androidTest for module $project.name : $androidTestJavaSourceFileCount")
        buildTypes {
            debug {
                testCoverageEnabled = androidTestJavaSourceFileCount > 0
            }
        }

        jacoco {
            version = '0.6.2.201302030002'
        }

        sourceSets {
            main {
                manifest.srcFile 'AndroidManifest.xml'
                res.srcDirs = ['res']
                assets.srcDirs = ['assets']
           }
       }
 }

Let's see what we have here:

sourceSets.androidTest.allJava.each {
    ...
}

sourceSets leads to SourceSetContainer
.androidTest leads to a single SourceSet
.allJava leads to the allJava property on a SourceSet which leads to a SourceDirectorySet

Now, while there is not a .size() method or anything like it in SourceDirectorySet, or any of the super interfaces of that, there is some Groovy magic that provides a .size() method for all Iterables.

This means that you should be able to do the following:

def androidTestJavaSourceFileCount = sourceSets.androidTest.allJava.size()

Don't be fooled by this one-liner though, the source of the Iterable.size() method is:

public static int size(Iterable self) {
    return size(self.iterator());
}

which calls:

public static int size(Iterator self) {
    int count = 0;
    while (self.hasNext()) {
        self.next();
        count++;
    }
    return count;
}

So it will do the same thing as your previous code did, but it will clean up your part of the code.


Thread-safe object cache, for read, high performance in .NET I want to realize an object cache which holds objects loaded from database, file or other slow access source, to make them available quickly with high performance. There will be a lot of accesses to get these objects from the dictionary!

It's not supposed to be Singleton/Static; multiple different object holders are possible, with multiple threads running on each.

Access is usually read-only, the objects themselves (in the dictionary) shall not be changed after being created.

For some special Admin functions, a Reload method exists, where thread safety can be ignored (not to be used by the main, multi-threaded program).

If I am right, the .NET Dictionary is thread-safe, as long as it is used only in read access, thus not requiring locks in reading methods. The dictionary will not be changed after being assigned to the _progObjects variable (do I need to make it volatile?). But time consuming volatile (alternatively Interlocked) access on the _areProgObjectsInitialized variable seems to be necessary, to make first accesses (with initialization) thread-safe.

Is the following a good and performant solution, can it still be improved, or are there safety risks?

/// <summary>
/// Not Singleton, but each instance multi-threaded!
/// Holds objects loaded from an external source (database, file, whatever...)
/// </summary>
public class ProgObjectsThreadSafeRead : IProgObjectsThreadSafeRead
{
    private IDictionary<ObjectKey, object> _progObjects = null;
    private object _progObjectsSyncObj = new object();
    private volatile bool _areProgObjectsInitialized = false;

    private void InitializeProgObjectsIfNeeded()
    {
        if (!_areProgObjectsInitialized)
        {
            lock(_progObjectsSyncObj)
            {
                if (!_areProgObjectsInitialized)
                {
                    IDictionary<ObjectKey, object> localProgObj = LoadProgObjects();
                    // do something more
                    _progObjects = localProgObj;
                    _areProgObjectsInitialized = true;
                }
            }
        }
    }

    public object GetProgObject(ObjectKey key)
    {
        InitializeProgObjectsIfNeeded();
        return _progObjects[key];
    }

    /// <summary>
    /// Not thread safe, don't use in multi-thread environments!
    /// Specific editing tools only! Not in interface.
    /// </summary>
    public void ReloadProgObjects()
    {
        lock (_progObjectsSyncObj)
        {
            _areProgObjectsInitialized = false;
            InitializeProgObjectsIfNeeded();
        }
    }

    // others removed
}

the .NET Dictionary is thread-safe, as long as it is used only in read access, thus not requiring locks in reading methods.

You are playing with fire here.  Your statement is only true iff no thread will modify the dictionary.  If any of them do, which is about impossible to avoid in a cache, then a lock on both the reading and the writing code is required.  A natural consequence of not being able to reliably read a data structure while it is in the process of being modified.

The ReaderWriterLock/Slim classes were designed to provide you this kind of locking.  You must acquire a reader lock when you read, a writer lock when you write.  Multiple threads can acquire a reader lock so you won't have any serious overhead as long as the cache is productive. The Slim version uses a low cost locking primitive which can reduce the lock cost somewhat.  But it is no cure when there's a lot of write contention, no magic formula exists for that.  If you do have a lot of write contention then double-check if the old ReaderWriterLock may give better throughput.

And be sure to checkout the .NET 4 MemoryCache class.


Print all subsets and permutations of a string The following code is designed to accept a string from the user that is at most 5 characters long. It will then find each possible substring of the string and print all possible permutations of each substring.

This is the first time I've done my utmost to prevent memory leaks by freeing every malloc. Even Valgrind seems to be happy, but I'm wondering if anyone here will be able to spot any bad practices, especially regarding memory allocation.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char * createString();
void genSubsets(char *);
char * genBetween(char *, int, int);
void swap(char *, char *);
void permute(char *, int, int);
int alreadyPermuted(char *);
char ** permutations;
unsigned int numPermuted = 0;

int main()
{
    permutations = malloc(sizeof(char*) * 120);
    memset(permutations, '\0', 120);
    int index = 0;
    for(index = 0; index < 120; index++)
    {
        permutations[index] = malloc(sizeof(char) * 6);
        memset(permutations[index], '\0', 6);
    }
    // 120 is 5!
    //So this will be the maximum space needed
    char * string  = createString();
    scanf("%5[^\n]s", string);
//  printf("%s\n", string);
    genSubsets(string);
    free(string);

    for(index = 0; index < 120; index++)
    {
        free(permutations[index]);
    }
    free(permutations);
    return 0;
}
char * createString()
{
    char * string = malloc(sizeof(char) * 6);
    if(string)
    {
        memset(string,'\0',6);
        return string;
    }
    else
    {
        puts("Malloc failed!");
        abort();
    }
}
void genSubsets(char * string)
{
    int counter1;
    int counter2;
    int size = strlen(string);
    //permute(string, 0, size);
    for(counter1 = 0; counter1 < size; counter1++)
    {
        for(counter2 = counter1; counter2 < size; counter2++)
        {
            char * generatedSubString = genBetween(string, counter1, counter2);
            if(generatedSubString[0] != '\0' && numPermuted < 120)
            {
        //      printf("Substring : %s\n", generatedSubString);
                permute(generatedSubString, 0 , strlen(generatedSubString));
            }
            free(generatedSubString);
        }
    }
}
char * genBetween(char * string, int begin, int end)
{
    char * subString = createString();
    int counter;
    if(!subString)
    {
        puts("Realloc failed.");
        abort();
    }
    for(counter = begin; counter <= end; counter++)
    {
        subString[counter-begin] = string[counter];
    }
    //free(string);
    return subString;
}
void swap(char * first, char * second)
{
    char temp = *first;
    *first = *second;
    *second = temp;
}
void permute(char * string, int begin, int end)
{
    int count = 0;
    if(begin == end && strlen(string) > 0 && !alreadyPermuted(string) && numPermuted < 120)
    {
        printf("%s\n", string);
        strcpy(permutations[numPermuted], string);
        numPermuted++;
        return;
    }
    for(count = begin; count <= end; count++)
    {
        swap(string+begin, string+count);
        permute(string, begin+1, end);
        swap(string+begin, string+count);
    }

}
int alreadyPermuted(char * string)
{
    int count = 0;
    for(count = 0;numPermuted < 120 &&  count <= numPermuted; count++)
    {
        if(strcmp(permutations[count], string)==0)
        {
            return 1;
        }
    }
    return 0;
}

Some (most of?) dynamic allocations seem unnecessary. A call to createString can be safely replaced with a local char string[6]; Similarly, a call to genBetween could be reduced essentially to

char generatedSubString[6];
strncpy(generatedSubString, string + counter1, counter2 - counter1);

Testing for numPermuted < 120 looks strange. You should trust your math. If you do not, don't fail the test silently, but rather assert(numPermuted < 120), and if it triggers, revisit your assumptions.
A search for already permuted (in fact, already visited) substring is suboptimal. Consider filling the permuted array in sorted order (to enable binary searching it).


Comparing random number of bits (followup) This is a follow-up of Comparing random number of bits.

I have a function that compares bits from a binary representation string and a chunk of memory and returns true if they are equal.

This is the function (which works as expected): I'll explain with comments what some external methods are supposed to do, accept that they work as expected for the sake of this question.

bool Bits::compareBinary(const char *string, size_t check_n_bits, size_t skip_n_bits){
    bool match = true;

    size_t bytes = (check_n_bits + 7) / 8;
    size_t bytes_with_skip = (check_n_bits + skip_n_bits + 7) / 8;

    if(this->canMoveForward(bytes_with_skip) == false) return false;

    Bits *data = this->readBits(check_n_bits, skip_n_bits);

    unsigned char *bin_string = Utils::removeSpaces(string);
    size_t len = strlen((const char *) bin_string);
    if(Utils::isValidBinString(bin_string) || len < check_n_bits) return false;

    char tmp_bin_repr[9];

    for(size_t i = 0; i < bytes; i++) {
        int chars_to_compare = i + 1 == bytes ? check_n_bits % 8 : 8;
        chars_to_compare = chars_to_compare == 0 ? 8 : chars_to_compare;

        uint8_t c = data->read_uint8();
        sprintf(tmp_bin_repr, BYTETOBINARYPATTERN, BYTETOBINARY(c));

        if(memcmp(tmp_bin_repr, &bin_string[i * 8], chars_to_compare) != 0) {
            match = false;
            break;
        }
    }
}

Helper macro:

#define BYTETOBINARYPATTERN "%d%d%d%d%d%d%d%d"
#define BYTETOBINARY(byte) \
    (byte & 0x80 ? 1 : 0), \
    (byte & 0x40 ? 1 : 0), \
    (byte & 0x20 ? 1 : 0), \
    (byte & 0x10 ? 1 : 0), \
    (byte & 0x08 ? 1 : 0), \
    (byte & 0x04 ? 1 : 0), \
    (byte & 0x02 ? 1 : 0), \
    (byte & 0x01 ? 1 : 0)

Let's call this function:

compareBinary(const char *string, size_t check_n_bits, size_t skip_b_bits)

As there are a couple of methods and a class I can't post here (for the sake of this question), you're free to explore the entire code here.
How is this function used? The function is a method of an object that contains some data. When this method is called, it compares as many bits from a binary string representation of data as we pass it with the data contained in the object.

Example:

mObj->compareBinary("01010101 11110100 00", 18, 0);

This will compare 18 bits in total with the data that is hold in the object. First 8 chars of the passed string will be taken and the first byte of the data hold by the object will be converted to a string (using the helper macro). Then both will be compared. Second 8 chars, the same. Then we'd get the last 00 chars, take the third byte, convert it to a 8 chars binary representation, but only the first two chars will be compared.

This lets me compare a random number of bits with a simple binary representation of the data.

While the function works as expected and it's passing multiple unit tests, I believe it can be refactored and optimized. Can you give me any tips how to do it?

The code looks fine, I can only mention a few things more related to code style, but before we get to that, this one line caught my attention:

if(Utils::isValidBinString(bin_string) || len < check_n_bits) return false;

Nothing really wrong with it, but it reads weird. It returns false if the binary string "is a valid bin string". Hum? Why would we want to fail if the string isValidBinString? This statement is either missing a ! at the beginning, or the function does the exact opposite of what its name advertises, unless you are actually wanting to check "invalid strings", which would also be weird...

As said before, I don't see anything particularly bad with the code, but one thing that I would refrain from doing is prefixing in-class member access with the this-> pointer. That has no practical purpose other than visual noise, but it can also hide variable names that shadow each other, which in turn is a thing you should really avoid.

You could make more thorough use of const on locals that are only assigned once, to ensure they are not changed later. That's a good habit to acquire. We can easily track the lifetime of a variable in a small scope, but sometimes scopes grow big, so it is nice to have a guarantee that a variable is not changed after its initialization. Languages like Rust make immutable the default, which is actually a neat idea, most of time you just want a single assignment.

Storing the return value here:

if(memcmp(tmp_bin_repr, &bin_string[i * 8], chars_to_compare) != 0) {
    match = false;
    break;
}

Is pointless since the function is over after the loop. Just return false directly.

A minor issue, but strict C++ mandates that C library functions and types be accessed from the std:: namespace, and imported from header files like <cstdio>, <cstdlib>, etc. To comply with that and ensure portability across different C++ compilers, you have to, for instance, use std::size_t, std::strlen(), etc.

I don't have problems with macros when they are well used, but you could have accomplished BYTETOBINARYPATTERN and BYTETOBINARY using a constant and inline function, respectively, gaining scoping and better error messages if misused.


Graham scan implementation in Haskell It looks like this is a really classical question, but I'd like to ask it one more time as all those solutions look quite long and complicated for me (maybe because I am dumb :) )

So this is a Graham convex hull algorithm from the very beginning of the "Real World Haskell" book. And a perimeter function just for the sake of implementing it.

Why did the authors define the Direction datatype when it only acts like
Ordering?

Is there any more elegant way to traverse through lists considering pairs?

Any suggestions would be appreciated

import Text.Printf
import Data.List (sortBy)
data Point = Point Int Int deriving (Show, Eq, Ord)

turn :: Point -> Point -> Point -> Ordering
turn (Point x2 y2) (Point x1 y1) (Point x3 y3) = compare ((x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1)) 0

polarSort :: [Point] -> [Point]
polarSort points = sorted where
    pivot = minimum points
    sorted = pivot:(sortBy (\a b -> turn b pivot a) . filter (pivot /=)) points

grahamScan :: [Point] -> [Point]
grahamScan points = grahamScan' rest [p2, p1] where
    (p1:p2:rest) = polarSort points
    grahamScan' [] l = l
    grahamScan' (p:pointsLeft) (s1:s2:selectedPoints) = case turn s2 s1 p of
                                        GT -> grahamScan' (p:pointsLeft) (s2:selectedPoints) -- the turn is right, so we discard last selected point
                                        _  -> grahamScan' pointsLeft (p:s1:s2:selectedPoints) -- the turn is left, so we add another point to the list of selected ones

dist :: Point -> Point -> Double
dist (Point x1 y1) (Point x2 y2) = sqrt (fromIntegral ((x2 - x1)^(2 :: Int) + (y2 - y1)^(2 :: Int)))

perimeter :: [Point] -> Double
perimeter points@(p0:_) = dist p0 pn + rest where
    walk :: [Point] -> (Double, Point)
    walk [a] = (0, a)
    walk (p1:p2:ps) = (dist p1 p2 + next, lastPoint) where
        (next, lastPoint) = walk (p2:ps)
    (rest, pn) = walk points

main = do
    _ <- getLine
    content <- getContents
    putStrLn $ printf "%.1f" ((perimeter . grahamScan . map ((\([x, y]) -> Point x y) . map read . words) . lines) content)

A good foundation to build on

One nice thing about your code is that it needs no [arccos,arctan,arg,atan2,phase]-like floating point approximations until you already have your convex hull and demand its Euclidean perimeter and another that you got the Graham scan's stack shuffle pretty concise and \$\mathcal O(n)\$ everywhere outside the one and only \$\mathcal O(n \log n)\$ sortBy.

with a glitch

The worst thing to say about your code is that it is only partially correct and easily confusable by collinear EQ triples: Just to give an example proof of failure, instead of the correct 4·4=16.0 perimeter it calculates incorrect 20.5 perimeter for input point sample sequence

0 0
4 0
1 0
2 0
4 4
0 4

and other inelegances calling for a rewrite:

Aesthetically, your lambdas

(\a b -> turn b pivot a)
(\([x, y]) -> Point x y)

make me want to flip your argument order and core data type so that we get

compareFrom o l r=compare((x l-x o)*(y r-y o))((y l-y o)*(x r-x o))

mnemonically returning LT iff from o l looks "lefter than" = "left to" = "less right than" r,  for example compareFrom (0,0) (1,2) (2,1) == LT [to answer your question about the need for data Direction=LEFT|STRAIGHT|RIGHT aliases] and together with a

distanceFrom from to = ((x to-x from)**2+(y to-y from)**2)**(1/2)

handy not only to reduce your over-individualized perimeter to a one-liner

totalDistance ps = sum(zipWith distanceFrom(last ps:init ps)ps)

but also to get your Graham preparation to lambda-free (but oververbosely pointed)

convexHull points =
    let{o = minimum points; -- maximum/By comparing sum would be equally fine
        presorted=sortBy(compareFrom o)(filter(/=o)points);
        collinears=groupBy(((EQ==).).compareFrom o)presorted;
        outmost=map(maximumBy(comparing(distanceFrom o)))collinears;
    }in dropConcavities[o]outmost

so that we can now easily catch all cases in the final convexity scan

dropConcavities (left:lefter) (right:righter:rightest) =
    case compareFrom left right righter of
    LT -> dropConcavities (right:left:lefter) (righter:rightest)
    EQ -> dropConcavities (left:lefter) (righter:rightest)
    GT -> dropConcavities lefter (left:righter:rightest)
dropConcavities output lastInput = lastInput++output

By hitherto restricting myself to abstract x and y accessor methods i can now take the postponed liberal pick of any representation for which i can provide such functions:

import Data.Ord;import Data.List;(x,y)=((!!0),(!!1));
compareFrom o l r=compare((x l-x o)*(y r-y o))((y l-y o)*(x r-x o));
distanceFrom from to = ((x to-x from)**2+(y to-y from)**2)**(1/2);
totalDistance ps = sum(zipWith distanceFrom(last ps:init ps)ps);
convexHull points =
    let{o = minimum points;
        presorted=sortBy(compareFrom o)(filter(/=o)points);
        collinears=groupBy(((EQ==).).compareFrom o)presorted;
        outmost=map(maximumBy(comparing(distanceFrom o)))collinears;
    }in dropConcavities[o]outmost;
dropConcavities (left:lefter) (right:righter:rightest) =
    case compareFrom left right righter of{
    LT -> dropConcavities (right:left:lefter) (righter:rightest);
    EQ -> dropConcavities (left:lefter) (righter:rightest);
    GT -> dropConcavities lefter (left:righter:rightest);}
dropConcavities output lastInput = lastInput++output;
main = interact(show.totalDistance.convexHull.map(map read.words).lines);


Python class initialize with dict how can I express this idea in Pythonic way?
The problem is that I want to create class with many attributes, but don't want to initialize all in __init__(self, arg1, arg2, ..., argN). Instead of this I want use some like this:

class DummyUpdater(object):

    def from_dict(self, d):
        self.__dict__.update(d)
        return self

    def __repr__(self):
        attributes = inspect.getmembers(self.__class__, lambda a: not(inspect.isroutine(a)))
        return '{}'.format({a[0]: getattr(self, a[0]) for a in attributes if not(a[0].startswith('__') and a[0].endswith('__'))})

class Passenger(DummyUpdater):
    """docstring for Passenger"""

    first_name = ''
    last_name = ''
    middle_name = ''
    title = ''
    maturity = ''
    document_number = ''
    document_type = ''

What is used now (with dummy updater):

passenger = Passenger().from_dict({'first_name' : 'Name', ...})

What I want to achive:

passenger = Passenger({'first_name' : 'Name', ...})
# and when I print passenger object I want see
print(passenger) # {'first_name' : 'Name', ...}

Python and the power of unpacking may help you in this one,

As it is unclear how your Class is used, I will give an example of how to initialize the dictionary with unpacking. This will work on any iterable.

True to it's name, what this does is pack all the arguments that this method call receives into one single variable, a tuple called *args. The same principle applies to **kwargs too, except that in this case it applies to keyword arguments. This most of the time corresponds to dictionaries

An example on how to use **kwargs to initialise your dictionary class

class DummyUpdater(object):
    def __init__(self, iterable=(), **kwargs):
        self.__dict__.update(iterable, **kwargs)

class Passenger(DummyUpdater):
    def __init__(self, iterable=(), **kwargs):
       super().__init__(iterable, **kwargs)

# See the power of unpacking!
# We can create a class like this just with variables
d = DummyUpdater(first_name="abc", last_name='sgkf', middle_name='something else', title='ook iets', maturity='', doc_num='4', doc_type='1900')

# Or with a dictionary
d2 = Passenger({'first_name' : 'abc', 'last_name' : 'def'}) # etc..

EDIT

I am unsure if this is in scope or not, since this question doesn't address this at all, but since @Peilonrayz attended me to the usage of __slots__ I learned something myself and am willing to share this with everyone who is interested.

Secondly setting up a class this way, will imrprove memory usage a lot!

class DummyUpdater(object):
    __slots__ = ['first_name', 'last_name']
    def __init__(self, **kwargs):
        for key, value in kwargs.items():
            setattr(self, key, value)

class Passenger(DummyUpdater):
    __slots__ = ['first_name', 'last_name']
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

# SLOTS MAGIC
# HOWTO init from variables
p = Passenger(first_name='abc', last_name='def')
print(p.first_name) # prints abs
print(p.last_name)  # prints def

# HOWTO init from a dict, notice how I unpack the dictionary to pass all the variables
p2 = Passenger(**{'first_name':'abc', 'last_name':'def'})
print(p2.first_name) # prints abc
print(p2.last_name)  # prints def


C# GDI+ rendering method performance I am looking for any performance insights or better coding style for my rendering method.

public Bitmap bmp;
public void render()
{
    Brush[] brushes = new SolidBrush[NumOfShapes];
    PointF[][] points = new PointF[NumOfShapes][];
    float[] tensions = new float[NumOfShapes];

    // We have multiply shapes, every shape has parameters for brush,points,tension
    for (int i = 0; i < NumOfShapes; i++)
    {
        int curT = i * ParameterNumber;
        brushes[i] = getBrush(curT); // get value and clip it if need
        points[i] = getPoints(curT);
        tensions[i] = getTension(curT);
    }

    using (Graphics graphics = Graphics.FromImage(bmp))
    {
        graphics.Clear(Color.Black);
        graphics.SmoothingMode = Globals.smoothingMode;
        for (int i = 0; i < NumOfShapes; i++)
        {
            graphics.FillPolygon(brushes[i], points[i]);
            // I also use shapes bellow
            //graphics.FillClosedCurve(brushes[i], points[i]);
            //graphics.DrawPolygon(new Pen(brushes[i],tensions[i]), points[i]);
            //graphics.FillClosedCurve(brushes[i], points[i], FillMode.Alternate, tensions[i]);
            //graphics.DrawCurve(new Pen(brushes[i], tensions[i]), points[i]);
        }

        graphics.Flush();
    }
}

With this, I am currently getting ~2000 bitmaps per second.

Generated bitmap is not displayed, I just get pixels with Marshal.Copy() and dispose bitmap. So is there a quicker way to render bitmap from shapes and get pixels.

private SolidBrush getBrush(int curT)
{
    parameters[curT] = clippingColor(parameters[curT]);
    parameters[curT + 1] = clippingColor(parameters[curT + 1]);
    parameters[curT + 2] = clippingColor(parameters[curT + 2]);
    parameters[curT + 3] = clippingColor(parameters[curT + 3]);
    Color color = Color.FromArgb(Convert.ToInt32(parameters[curT]), Convert.ToInt32(parameters[curT + 1]), Convert.ToInt32(parameters[curT + 2]), Convert.ToInt32(parameters[curT + 3]));
    return new SolidBrush(color);
}

private float getTension(int curT)
{
    parameters[curT + 4] = clippingTension(parameters[curT + 4]);
    return Convert.ToSingle(parameters[curT + 4]);
}

private PointF[] getPoints(int curT)
{
    PointF[] tempPoints = new PointF[(ParametersNum - 5) / 2];
    int pointsIndex = 0;
    int endSize = curT + ParametersNum;
    for (int j = curT + 5; j < endSize; j += 2)
    {
        parameters[j] = clippingPointsX(parameters[j]);
        parameters[j + 1] = clippingPointsY(parameters[j + 1]);
        tempPoints[pointsIndex] = new PointF(Convert.ToSingle(parameters[j]), Convert.ToSingle(parameters[j + 1]));
        pointsIndex++;
    }
    return tempPoints;
}

Update

I change bitmap pixel format to Format32bppPArgb and added rendering settings to graphics object as described in this guide: Rendering fast with GDI+ - What to do and what not to do! With this I am getting around ~2300 rendered bitmaps per second.

graphics.CompositingMode = CompositingMode.SourceOver;
graphics.CompositingQuality = CompositingQuality.HighSpeed;
graphics.PixelOffsetMode = PixelOffsetMode.None;
graphics.InterpolationMode = InterpolationMode.Default;

It looks like you may come from a Java or scripting background. For .NET, the standard recommends PascalCasing for method names. This would turn your methods into:

Render()
GetBrush(...)
GetPoints(...)
GetTension(...)

Variable names should be camelCasing, where the first letter is lower case and the remaining words are capitalized. I'm not sure if NumOfShapes or ParameterNumber are class properties (Pascal Cased) or class-level fields (camel Cased), but you should follow the standard.

As far as your method names, you should avoid GetXXX which suggests that they should be properties. I would recommend the following changes:

GetBrush(...) -> CreateBrush(...)
GetPoints(...) -> GeneratePoints(...)
GetTension(...) -> CalculateTension(...)

For your variable names, curT doesn't help me understand what the parameter is for. :

for (int i = 0; i < NumOfShapes; i++)
{
    int curT = i * ParameterNumber;
    brushes[i] = getBrush(curT); // get value and clip it if need
    points[i] = getPoints(curT);
    tensions[i] = getTension(curT);
}

The definition and modification must be elsewhere. I'd have to go searching for it. It would be much simpler if you named it something meaningful, even if it is long, like currentImageIteration.

As far as coding goes, always, always dispose of GDI objects when you are done using them. You can get some pretty strange errors (OutOfMemoryException for example) when hitting the GDI object limit. Because GDI uses unmanaged objects, you should always dispose of them when you are done, see this question, and this question. The object will be disposed at some time in the future, but because the GC doesn't clean up managed objects, this will cause a memory leak.

Other than that, everything seems OK.


Reversed Binary Numbers Here is my code for "Reverse Binary Numbers" in C.

  Problem

  Your task will be to write a program for reversing numbers in binary.
  For instance, the binary representation of 13 is 1101, and reversing
  it gives 1011, which corresponds to number 11.

  Input

  The input contains a single line with an integer \$N\$, (where
  \$1≤N≤1000000000\$ ).

  Output Output one line with one integer, the number we get by reversing the binary representation of \$N\$.

  Sample 1

  input 13
  output 11

  Sample 2

  input 47
  output 61

It's pretty efficient, but not the best. I wanted to improve the runtime. So I am looking for suggestions to improve the code.

Code

//Reverse Binary

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

#define DIVISOR 2
#define MAX 50

int main() {
  int r, q, n, m = 1, num = 0, i = 0, len;
  int res[MAX];
  char str[MAX];

  scanf("%d", &n);

  do {

    q = n / DIVISOR;
    r = n % DIVISOR;

    res[i] = r;

    n = q;

    i++;

  } while(n > 0);

  len = i;
  m = 1;

  for(; i > 0; i--) {
    if(res[i - 1] != 0) {
      num += res[i - 1] * m;
    }
    m *= DIVISOR;
  }

  fprintf(stdout, "%d", num);

  return 0;
}

Division and multiplication are relatively computationally costly operations compared to shifts and AND operations.  Try to reformulate your answer in terms of >> and & and you will likely find a performance gain.

Other things I noticed that may help you improve your program:

Eliminate unused variables

Both len and str are unused and may be eliminated from the program.

Try to put the loop exit condition at the top of the loop

Instead of using do {...} while, prefer to use while {...} or a for loop.  in your case this will also neatly solve the problem that occurs when the user types in a negative number.

return 0 is not needed at the end of main

Any modern compiler will automatically assume that getting to the end of main means that the program has executed successfully and will generate the equivalent of return 0 at the end.  It is not necessary to write it.

Check the return value of scanf

The call to scanf can fail, in which case it will return 0.  Your code should check for that.

If your input says you will only have positive numbers, use unsigned

Instead of "%d" it would make more sense to say scanf("%u", &n) since the inputs cannot be negative.  Note that this will not prevent negative numbers from being input, but it makes the expectation clear within the code.  Alternatively, check for negative number input to make the code more robust.

Consider verifying that your data type will hold the maximum value

The input specification says that the input numbers are all in the range of \$1 \le N \le 1,000,000,000\$ but an int isn't necessarily large enough on all platforms.  Since it takes 30 bits to express that number, and because it's unsigned, it would probably make more sense to use a uint_fast32_t type defined in <stdint.h> which is a fast integer type containing at least 32 bits.


Email a notification when detecting changes on a website The text of a website is checked in a given time period. If there are any changes a mail is sent. There is a option to show/mail the new parts in the website. What could be improved?

#!/usr/bin/env python3
import urllib.request, hashlib, time, html2text, smtplib, datetime, argparse

class urlchange:
    def __init__(self, url):
        self.url = url
        self.urlhash = self.createhash()
        self.content = self.getcontent()
        date = datetime.datetime.now().strftime( "%d.%m.%Y %H:%M:%S" )
        print(date+": Start Monitoring... hash: "+self.urlhash)

    def getcontent(self):
        #Try to get data
        try:
            urldata = urllib.request.urlopen(self.url).read().decode("utf-8","ignore")
            urldata = html2text.html2text(urldata)
        except:
            print("Can't open url: ", self.url)
        return urldata

    def createhash(self):
        #create hash
        urldata = self.getcontent().encode("utf-8")
        md5hash = hashlib.md5()
        md5hash.update(urldata)
        return md5hash.hexdigest()

    def comparehash(self):
        date = datetime.datetime.now().strftime( "%d.%m.%Y %H:%M:%S" )
        if(self.createhash() == self.urlhash):
            print(date+": Nothing has changed")
            return False
        else:
            print(date+": Something has changed")
            if(not args.nodiff):
                print(self.diff())
                if(not args.nomail):
                    try:
                        sendmail("Url has changed!","The Url "+self.url+" has changed at "+date+" .\n\nNew content:\n"+self.diff())
                    except:
                        sendmail("Url has changed!","The Url "+self.url+" has changed at "+date+" .")
            elif(not args.nomail):
                sendmail("Url has changed!","The Url "+self.url+" has changed at "+date+" .")
            return True

    def diff(self):
        #what has chaged
        start, end = 0, 0
        newcontent = self.getcontent()
        #start of changes
        for i,j in enumerate(self.content):
            if(i<len(newcontent) and j != newcontent[i]):
                start=i
                break
        #end of changes
        for i,j in enumerate(reversed(self.content)):
            if( (len(newcontent)-(i+1))>0 and j != newcontent[len(newcontent)-(i+1)]):
                end=len(newcontent)-i
                break
        return newcontent[start:end]

def sendmail(subject,message):
    try:
        server = smtplib.SMTP("smtp.server.com",587)
        server.set_debuglevel(0)
        server.ehlo()
        server.starttls()
        server.login("email@server.de","password")
    except:
        print("Can't connect to the SMTP server!")

    date = datetime.datetime.now().strftime( "%d.%m.%Y %H:%M:%S" )
    msg = "From: email@server.de\nSubject: %s\nDate: %s\n\n%s""" % (subject, date, message)

    server.sendmail("email@server.de","email2@server.de",msg)
    server.quit()
    print(date+": email was sent")

parser = argparse.ArgumentParser(description="Monitor if a website has changed.")
parser.add_argument("url",help="url that should be monitored")
parser.add_argument("-t","--time",help="seconds between checks (default: 600)",default=600,type=int)
parser.add_argument("-nd","--nodiff",help="show no difference",action="store_true")
parser.add_argument("-n","--nomail",help="no email is sent",action="store_true")
args = parser.parse_args()

url1 = urlchange(args.url)
time.sleep(args.time)
while(True):
    if(url1.comparehash()):
        break
    time.sleep(args.time)

Improved code at: Email a notification when detecting changes on a website - follow-up

You do not need to find the differences manually, you can use difflib.SequenceMatcher:

I think this is what you need:

>>> a, b = "foobxr", "foobar"
>>> diffs = difflib.SequenceMatcher(None, a, b).get_matching_blocks()
>>> diffs
[Match(a=0, b=0, size=4), Match(a=5, b=5, size=1), Match(a=6, b=6, size=0)]
>>> max((a, b), key=len)[diffs[0].size : diffs[1].a]
'x'

Specific except

Specify what you want to except exactly. Bare except catches even typos!

Use a logger

You print a lot of info, a logger is more flexible and can be very easily redirected to a file.

.format

You can make your messages more readable:

For example:

    print(date+": Start Monitoring... hash: "+self.urlhash)

Becomes:

print("{date}: Start Monitoring... Hash: {self.urlhash}".format(**locals()))

Or the more standard:

print("{}: Start Monitoring... Hash: {}".format(date, self.urlhash))

Thanks to @Jatimir for noticing that in python 3.6+ f-strings are a nice way to interpolate variables in strings with a clean sintax: for example:

print(f'{date}: Start Monitoring... Hash: {self.urlhash}')


Rate limit a method by generating particular load on demand in C# I am working on a project where I want to generate random throughput on a particular method so that I can do some performance testing on it. This way I can test my method by generating random throughputs and see how my method works under all those scenarios.

For example: I need to call my doIOStuff method at an approximate rate of x requests per second from multiple threads where x will be less than 2000 mostly but it really doesn't matter in this case. It doesn't have to be accurate so there is some room for an error but the overall idea is I need to make sure that my method doIOStuff is executed no more than x times in a sliding window of y seconds.

Assuming we start n threads and want a maximum of m calls per second. We can achieve this by having each thread generate a random number between 0 and 1, k times per second and call  doIOStuff method only if the generated number is less than m / n / k.

Below is the code I got which uses global variables and it does the job but I think it can be improved a lot where I can use some cancellation tokens as well and make it more efficient and clean.

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Threading;

namespace ConsoleApp
{
    class Program
    {
        const int m_threads = 100;
        const int n_throughput = 2000;
        const int k_toss_per_second = 2000; // Note that k_toss_per_second x  m_threads >= n_throughput

        static void Main(string[] args)
        {
            var tasks = new List<Task>();

            for (int i = 0; i < m_threads; i++)
                tasks.Add(Task.Factory.StartNew(() => doIOStuff()));

            Task.WaitAll(tasks.ToArray());
            Console.WriteLine("All threads complete");
        }

        static void callDoIOStuff()
        {
           int sleep_time = (int) (1000 * 1.0d / k_toss_per_second);
           double threshold = (double) n_throughput / m_threads / k_toss_per_second;
           Random random = new Random();
           while (true) {
                Thread.Sleep(sleep_time);
                if (random.NextDouble() < threshold)
                    doIOStuff();
            }
        }

        static void doIOStuff()
        {
            // do some IO work
        }
    }
}

I wanted to see what can we do here to make it more efficient and clean so that it can used in production testing for generating random throughput load. Also I should keep an eye to the completion of the tasks that are started otherwise number of pending tasks would keep increasing every second? Right.

Welcome to CR.  There is a lot of room for improvement with your code.  Let's start with the simple stuff and work our way up.

CR prefers use of braces always with if or for or foreach.

Hungarian Notation is heavily frowned upon, so get rid of names prefaced with m_.

Underscores internal to variable names are also frowned upon, so sleep_time would be better as sleepTime (or better altogether with a different name: see further below).

Names should meaningful and create clarity rather than confusion.  So m_threads isn't the number of threads running, but rather its the number of tasks you will start.  Thus that variable name should have "tasks" in it rather than "threads".

You should place your code into its own class.  For instance, class ThroughputTester.  There would be class level properties or fields named TaskCount, TargetThroughput, and TossesPerSecond.  I would not have these as constant fields, since it makes your code rigid and only applicable to the one set of values.  Rather these could be readonly properties set in the constructor, which allows for experimentation with different sets of values.

You could also have a CreateDefault method to create an instance with the same values as your constants.

public class ThroughputTester
{
    public ThroughputTester(int taskCount, int targetThroughput, int tossesPerSecond)
    {
        TaskCount = taskCount;
        TargetThroughput = targetThroughput;
        TossesPerSecond = tossesPerSecond;
    }

    public int TaskCount { get; }
    public int TargetThroughput { get; }
    public int TossesPerSecond { get; }

    public static ThroughputTester CreateDefault => new ThroughputTester(100, 2000, 2000);

}

Your use of Random is fragile and runs into issues, particularly since you have it in a method with a loop.  The better usage would be to define the Random instance at the class level instead of inside a method.

sleep_time leaves me wondering what it represents.  Is it seconds or milliseconds?  Can't tell from a vague name like time.  It could be renamed sleepMilliseconds.  Alternatively, you could use the Thread.Sleep(TimeSpan) signature (see link here) along with the TimeSpan.FromMilliseconds or TimeSpan.FromSeconds methods.

That said, even Thread.Sleep has some negativity associated with it.  You should read this answer for Why is Thread.Sleep so harmful.


Set Class Implementation ADT Is the following functions implemented correctly for the set class?
Please let me know if any changes need to be made.

# Implementation of the Set ADT using a Python list.
class Set :
    # Creates an empty set instance.
    def __init__( self, *initElements ):
        self._theElements = list()
        for i in range(len(initElements)) :
            self._theElements.add( initElements )

   # Returns the number of items in the set.
   def __len__( self ):
       return len( self._theElements )

   # Determines if an element is in the set.
   def __contains__( self, element ):
       return element in self._theElements

   # Determines if the set is empty.
   def isEmpty( self ):
       return len(self._theElements) == 0

   # Adds a new unique element to the set.
   def add( self, element ):
       if element not in self :
           self._theElements.append( element )

   # Removes an element from the set.
   def remove( self, element ):
       assert element in self, "The element must be in the set."
       self._theElements.remove( element )

   # Determines if this set is equal to setB.
   def __eq__( self, setB ):
       if len( self ) != len( setB ) :
           return False
       else :
           return self.isSubsetOf( setB )

   # Determines if this set is a subset of setB.
   def isSubsetOf( self, setB ):
    for element in self :
        if element not in setB :
            return False
    return True

  # Determines if this set is a proper subset of setB.
  def isProperSubset( self, setB ):
    for element in self :
        if self != setB :
            return True
    return False

   # Creates a new set from the union of this set and setB.
   def union( self, setB ):
    newSet = Set()
    newSet._theElements.extend( self._theElements )
    for element in setB :
        if element not in self :
            newSet._theElements.append( element )
    return newSet

   # Creates a new set from the intersection: self set and setB.
   def intersect( self, setB ):
    newSet = Set()
    for i in range(self._theElements) :
        for j in range(setB._theElements) :
            if self._theElements[i] == setB._theElements[j] :
                self.theElements.append(setB)
    return newSet

# Creates a new set from the difference: self set and setB.
def difference( self, setB ):
    newSet = Set()
    newSet._theElements.extend( self._theElements )
    for element in setB :
        if element in self :
            newSet._theElements.remove( element )
    return newSet

 # Creates the iterator for the self
def __iter__( self ):
    return iter(self._theElements)

Python already has built-in abstract base classes for sets and mutable sets — see the collections.abc module. There's even a recipe in the documentation for implementing an immutable set by inheriting from collections.abc.Set and storing the elements in a list.

Here's how you might extend the recipe to a mutable set:

from collections.abc import MutableSet

class ListBasedMutableSet(MutableSet):
    def __init__(self, iterable=()):
        self.elements = []
        for item in iterable:
            self.add(item)

    def __contains__(self, item):
        return item in self.elements

    def __iter__(self):
        return iter(self.elements)

    def __len__(self):
        return len(self.elements)

    def add(self, item):
        if item not in self.elements:
            self.elements.append(item)

    def discard(self, item):
        try:
            del self.elements[self.elements.index(item)]
        except ValueError:
            pass

It's also worth having a look at the implementation of the abstract base classes in _collections_abc.py.


How do I improve this logging mechanism? There are quite a few things I am considering:

I will have to check for null values
I will have serious trouble persisting it in the database.

How do I improve this or re-factor this for better results?

public class ErrorLog
    {
        public static void LogError(Exception e)
        {
            var exceptionLog = new Dictionary<String, String>
                                   {
                                       {"Inner Exception", e.InnerException.Message},
                                       {"Message", e.Message},
                                       {"Source", e.Source},
                                       {"StackTrace", e.StackTrace},
                                       {"MethodName",e.TargetSite.Name}
                                   };
            foreach (KeyValuePair<String, String> kvp in e.Data)
                exceptionLog.Add(kvp.Key, kvp.Value);
        }
    }

Update with the new and simpler class

  public class ErrorLog
    {
        public static void LogError(Exception e)
        {
            var innerExcpetionMessage = e.InnerException == null ? "Null" : e.InnerException.Message;
            using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
            {
                    var cmd = new SqlCommand("ApplicationErrorLog", connection) { CommandType = System.Data.CommandType.StoredProcedure };
                    cmd.Parameters.AddWithValue("@Type", e.GetType().Name);
                    cmd.Parameters.AddWithValue("@InnerException", innerExcpetionMessage);
                    cmd.Parameters.AddWithValue("@Message", e.Message);
                    cmd.Parameters.AddWithValue("@Source", e.Source);
                    cmd.Parameters.AddWithValue("@StackTrace", e.StackTrace);
                    cmd.Parameters.AddWithValue("@MethodName", e.TargetSite.Name);
                    connection.Open();
                    cmd.ExecuteNonQuery();
                    connection.Close();
                }

            }
        }
    }

Now what if there is an error in updating this log, how will I log that?

Why would you have serious trouble with persistance?

Just do null checks for InnerException and TargetSite, rest will be inserted as null or string empty depending on your db setup.

And why not just simplify it as:

var innerExceptionMessage = e.InnerException == null ? string.Empty : e.InnerException.Message;
var targetSiteMessage     = e.TargetSite == null ? string.Empty : e.TargetSite.Message;

exceptionLog.Add("Inner Exception", innerExceptionMessage);
exceptionLog.Add("Message", e.Message);
exceptionLog.Add("Source", e.Source);
exceptionLog.Add("StackTrace", e.StackTrace);
exceptionLog.Add("MethodName",targetSiteMessage);

Keep it simple :)


TypeWriter Javascript effect I have implemented a Javascript TypeWriter effect for my website. It types out code using the TypeWriter variable by adding each character on and setting a timeout. It then 'deletes' code using text slice and 're types.

I had big problems in setting a timeout for the individual functions because each time I did the speed would double.

I just tried to get the code working before making it more practical. At the moment my code is very linear and not very dynamic. Can anyone suggest some edits to make to make it more fluid? I'm not advanced, please go easy on me!

 /*
 typewriter.js
 Displays a typing effect on the home page.
 Tried to implement recursion instead of calling different functions but had trouble.
 **/
 var text1 = 'Hi, I\'m Ainsley I like';
 var text2 = ' love to code...';
 var id = 'typing-text1';
 var deleteUpTo = 17;
 var linebreak = 15;
 var speed = 150;
 var char = 0;
 var reType = 0;
 var textLength = text1.length;

 var typewrite = ' ';

 function typeWriter() {
    if (char < linebreak) {
       typewrite += text1[char];
       char++;
       setTimeout(typeWriter, speed);
       document.getElementById(id).innerHTML = typewrite;
    } else {
       typewrite += '<br />'
       printNewLine();
    }
 }

 function printNewLine() {
    if (char < text1.length) {
       typewrite += text1[char];
       char++;
       setTimeout(printNewLine, speed);
       document.getElementById(id).innerHTML = typewrite;
    } else {
       char = document.getElementById(id).innerHTML.length;
       deleteText();
    }
 }

 function deleteText() {
    if (char > 24) {
       typewrite = typewrite.slice(0, char);
       char--;
       document.getElementById(id).innerHTML = typewrite;
       setTimeout(deleteText, speed);
    } else {
       rePrintText();
    }
 }

 function rePrintText() {
    if (reType < text2.length) {
       typewrite += text2[reType];
       reType++;
       document.getElementById(id).innerHTML = typewrite;
       setTimeout(rePrintText, speed);
    }
 }

One loop to rule all animation.
For almost all realtime animations the whole thing is handled from one timed loop running at as close to a constant rate as is possible.
Having many different timers call many different functions quickly gets complicated and very hard to follow.
For the browser requestAnimationFrame should be used for any content animations. It calls a special functions that holds any changes made to the DOM in a back buffer until the next display vertical refresh (60 times a second)
This ensures that the animation is in sync with the display and any other DOM rendered content.
You use that function as the main loop to control all the associated animation content.
Simulating the typist
You have over complicated the typer. All that is needed is the current character position and a way to move and clear lines when a newline char is found.
The function called by requestAnimationFrame is given the current time since the page loaded. You can use that time to calculate the character position and then iterate over the characters needing to be added, checking for new lines.
A variable is kept currentLine that holds the current typing line and you just append text to that as you go.
To reduce the load you can reuse existing elements, removing it from the top, and appending it to the bottom. See newLine function in example below
Example
Rather than change your code, I have started from scratch. There is one main loop function called typer and a function to setup a new line. The character position is calculated by dividing the time since start by the speed.
If you want to speed up the rate of characters just subtract from the start time. I have added an example that randomly speeds up every 1/20 character. To slow down just add to the start time.

requestAnimationFrame(typer);

const words = `
 Testing type writer.
 And now some more text.
 You can remove some<<<<<<<<<<<delete text as well.
 Displays a typing effect on the home page.
 If you want to speed up the rate of characters just subtract from the start time.
 I have added an example that randomly speeds up every 1/20 character.
 To slow down just add to the start time.`;

const leftChar = ":>";
const textContainer = document.getElementById('typingText');
const speed = 100;
var startTime;
var lastCharPos;
var currentLine;

for (let i = 0; i < 2; i++) {
  textContainer.appendChild(Object.assign(document.createElement("div"), {
    className: "typed--typed-text",
    textContent: ""
  }));
}
// removes top line, clears it and adds to bottom of container
function newLine() {
  currentLine = textContainer.querySelector(".typed--typed-text");
  textContainer.removeChild(currentLine);
  textContainer.appendChild(currentLine);
  currentLine.textContent = leftChar;
}

function typer(time) {
  var str = "";
  if (startTime === undefined) {
    newLine();
    startTime = time;
    lastCharPos = 0;
  }

  // 1 in 20 characters are 50ms faster
  if (Math.random() < 0.05) { startTime -= 50 }

  const charPos = (time - startTime) / speed | 0;
  while (lastCharPos <= charPos) {
    const char = words[(lastCharPos++) % words.length];
    if (char === "\n") {
      currentLine.textContent += str;
      str = "";
      newLine();
    } else if (char === "<") {
      const text = currentLine.textContent;
      currentLine.textContent = text.substring(0,text.length-1);
    } else {
      str += char;
    }
  }
  currentLine.textContent += str;
  requestAnimationFrame(typer);
}
.typed {}

.typed--typed-text {}
<div id="typingText" class="typed">
</div>


Simple factory retrieving object by name Despite the fact that this code works, I would like to know if there's any situation I did not anticipated, and/or if what I am trying to do is some what wrong.

-std=C++11 flag needed

#include <iostream>
#include <string>
#include <map>
#include <typeinfo>
#include <functional>

class object
{
    public:

    virtual ~object(){}

    virtual std::string to_string() = 0;
};

class SomeObject : public object
{
    public:

    SomeObject(){}
    virtual ~SomeObject(){}

    virtual std::string to_string()
    {
        return "I am a type of SomeObject";
    }
};

class SomeOtherObject : public object
{
    public:

    SomeOtherObject(){}
    virtual ~SomeOtherObject(){}

    virtual std::string to_string()
    {
        return "I am a type of SomeOtherObject";
    }
};

std::map<std::string, std::function<object*()> > types;

template<typename O>
inline
static void register_type(const std::string & name)
{
    types[name] = [](){ return new O; };
}

static object * get_object(const std::string & object_name )
{
    return types[object_name]();
}

int main()
{
    register_type<SomeObject>("SomeObject");
    register_type<SomeOtherObject>("SomeOtherObject");

    object * some = get_object("SomeObject");
    object * some_other = get_object("SomeOtherObject");

    std::cout << "::" << some->to_string() << std::endl;
    std::cout << "::" << some_other->to_string() << std::endl;

    delete some;
    delete some_other;

    std::cout << "exit" << std::endl;

    return 0;
}

In general your code looks fine, though there are some points to improve:

Memory management

Instead of using raw pointers and new delete, you should use a smart pointer to express transfer of ownership explicitly:

template<typename O>
void register_type(const std::string & name) {
    types[name] = [](){ return std::unique_ptr<O>(new O); };
}

std::unique_ptr<object> get_object(const std::string & object_name ) {
    return types[object_name]();
}

Thus it's clear, the caller will own the object instance, and doesn't need to struggle calling delete correctly if the obtained instance goes out of scope.

Template parameter naming

The more usual name for the template parameter is rather T than O

Use a factory class instead of global functions and global map

Consolidate the code of the register_type(), get_object() functions and the types map variable into a class Registry, and provide a single instance of that one.

This would allow to encapsulate the types map as a private class member.

You could consider making that class a Singleton.

Check if the type name is registered

You should check if the string passed to the get_object() function was already registered in the map, rather than blindly calling the default (NOP) function from the map.

Usage of static for a global function

You should be aware that static actually make the register_type() function only visible for the translation unit, it appears in. Actually you should not need it.

BTW, the technique you're using is often seen for unit testing frameworks (like e.g. google-test) as test case factories, also often coming along with stringizing macros for the registered classes.


Is my DSL good idea to replace builder pattern? I want to build a set of builders for generating test data. On one side I want my builders API to be natural to use for my teammates. On the other side I'm great fun of creating DSLs, because it eliminates writing the same boilerplate code and let you read the code as English sentences.

However, I found those requirements quite incompatible. I've shown my builders to my teammate and what was intuitive to me, was not for him. He would need to learn my DSL.

How would you improve my design decisions in Builder pattern?

Decision #1: Long domain names vs. new short names

I started with traditional withX() methods for setting field X. For X I used names that are well known in our domain and commonly used in the rest of implementation.

Flight.Builder.create()
   .withOrigin(Airport.LAX)
   .withDestination(Airport.JFK)
   .build();

Then I thought this takes too much space, so I used method names that are shorter but uncommon in the rest of the implementation.

Flight.Builder.create()
   .from(Airport.JFK)
   .to(Airport.LAX)
   .build();

Another programmer prefered traditional names from the domain: origin, destination.

Decision #2: Clear method names vs. duplicate methods with type-overloading

Another weak point I found was redundancy of some information. For instance, in the following code:

Flight.Builder.create()
  .withPassenger(Passenger.ADULT) // that's a template
  .withPassenger(Passenger.Builder.create().withName("KENNEDDY"))
  .withStatus(Status.CANCELLED)
  .build();

the redundancy is in passenger-passenger, and in status-status. So I removed redudant words from method names and used type-overloading.

Flight.Builder.create()
  .with(Passenger.ADULT)
  .with(Passenger.Builder.create().withName("KENNEDDY"))
  .with(Status.CANCELLED)
  .build();

The programmer who knows data model underlying this builder and was used to more traditional builder pattern, was confused with my approach.

Decision #3: Changing words order and delegating builder creation

Then I found builder creation kind of boilerplate code that makes it harder to read:

Flight.Builder.create()
   .withPassenger(Passenger.Builder.create().withName("KENNEDDY"))
   .build();

I shortened those constructs. I also found this incompatible with English words order in sentence, where you don't say "what.build()", but "build(what)". Here's the result.

build(flight()
  .withPassenger(passenger().withName("KENNEDDY")));

How would you improve my design decisions?

A few, rather subjective thoughts:

Decision #1:

Here you have a flight builder. Which group of sentences looks more natural from the following?

a flight builder with origin
a flight builder with destination

or

a flight builder from
a flight builder to

The first two looks better for me.

Furthermore, if origin and destination are in your domain you should use them. Using synonyms could be confusing.

Decision #2:

I may be worth to note that if you have more than one field with the same type (strings and ints are common) it would not work. Having builders in the same application and one half of them is using with() while the other half is using withX() is also confusing.

Decision #3:

The passenger().withName("KENNEDDY") statements make me think about where the passenger instance comes from. A database or maybe a cache? I think a create word somewhere would make it obvious that it's a new instance and it would be helpful for readers although renaming passenger() to createPassenger() does not seem natural in this code too.


Fetching data from a database I am fetching data from the database using Entity Framework and Linq queries. I am wondering about the performance of getting data. I just want to check whether or not a well exists in the database. The database table has 15 columns and the number of rows are unknown.

internal static bool WellExists(string name, DatabaseContext context)
{
    var name = context.Wells.Where(w => w.name == name).Select(w => w.name).FirstOrDefault();
    return !string.IsNullOrEmpty(databaseUwi);
}

Here my point of concern is the line in which I am using Linq queries. In this I am using a Where, then Select and then FirstOrDefault. Another solution is using FirstOrDefault only.

var well = context.Wells.FirstOrDefault(w => w.name == name);
return !(well == null);

The first query will get only selected data (Select) which is not required. Which solution's performance is better?

You can try the .Any function on IQueryable. It is made for checking if there are any records.

Remember that .Any() has an overload that takes an expression, making the solution

internal static bool WellExists(string name, DatabaseContext context)
{
    return context.Wells.Any(x => x.Name == name);
}

I'm going to add a comment about your code as well.
In the second example you have return !(well == null). This obfuscates the meaning of the code a bit, and would be clearer if you write return well != null;


Checking if a string is funny or not The idea is this: To determine whether a string is funny, create a copy of the string in reverse e.g. . Iterating through each string, compare the absolute difference in the ascii values of the characters at positions 0 and 1, 1 and 2 and so on to the end. If the list of absolute differences is the same for both strings, they are funny.

This is my Python code for the same:

def funnyString(s):
    temp = s[::-1]
    arr1,arr2 = [],[]
    for i in range(len(s)-1):
        arr1.append(abs(ord(s[i+1])-ord(s[i])))
        arr2.append(abs(ord(temp[i+1])-ord(temp[i])))
    if arr1 == arr2: return "Funny"
    else: return "Not Funny"

It works but I want to get some feedback if this is too verbose and should/can be shortened. One solution example:

acxz
bcxz

Funny
Not Funny

The same implementation in C++

string funnyString(string s) {
    string temp;
    temp = s;
    vector<int> arr1, arr2;
    reverse(temp.begin(), temp.end());
    for (int i = 0; i < s.length()-1; i++) {
        arr1.push_back(fabs(int(s[i + 1]) - int(s[i])));
        arr2.push_back(fabs(int(temp[i + 1]) - int(temp[i])));
}
    if (arr1 == arr2) return "Funny";
    else return "Not Funny";

}

There's no reason to make a reversed copy. Just index from the back.
Nor to delay comparing the differences. It just means you have to save them.
Following the above two points ensures the function never throws. So, in C++ mark it noexcept.
Next, there's no need to go over the string from beginning to end and from end to beginning. Just stop in the middle.
In line with that, you should use C++17's std::string_view so even the caller need not make a copy.
Also, return a boolean. Leave it to the caller to Interpret it as he likes, not necessarily by choosing some English words.
I have no idea why you are bringing floating-point into it with fabs(), instead of staying with integers using std::abs(). At least I'm reasonably sure the compiler will fix that inefficiency. Or will it?

Modified C++ code:

constexpr bool isFunnyString(std::string_view s) noexcept {
    std::size_t a = 0, b = size(s);
    if (b > 2)
        for (b -= 2; a < b; ++a, --b)
            if (std::abs(s[a] - s[a + 1]) != std::abs(s[b] - s[b + 1]))
                return false;
    return true;
}


Deleting rows from a spreadsheet where Column C is blank I have a script which I use to delete rows from a Excel Sheet, dependent on the Column C cell of that row being blank.

I believe deleting a range might be quicker than deleting individual rows, but I'm unsure.

Sub Shorter()

'47ms to remove blanks rows of 80 rows

'Searches the designated Column and checks for a Blank Cell
'It then Sets a Marker and then continues until it finds a cell with data in it.
'That blank range is then deleted.

    t = GetTickCount
    With ActiveSheet

        Firstrow = 20
        LastRow = .UsedRange.Rows(.UsedRange.Rows.Count).Row

        For i = LastRow To Firstrow Step -1

            If i = StartRow - 1 Then GoTo EndSubAutoDeleteRowsFromEndRow
            If .Cells(i, "C") = Empty Then

                DelRangeStartRow = i

                ii = 0

                Do While .Cells(i - ii, "C") = Empty
                    DelRangeEndRow = (i - ii)
                    ii = ii + 1
                Loop

                Range(.Rows(DelRangeStartRow), .Rows(DelRangeEndRow)).Delete Shift:=xlUp

            End If
        Next i
    End With
    'Come here if you are finished deleting

    EndSubAutoDeleteRowsFromEndRow:

    SecondsElapsed = GetTickCount - t

End Sub

Things I like:

Going last row to first, so as to avoid messiness with row numbers that occur when deleting whole rows.
Good use of With
Good function naming of GetTickCount (at least, assuming it does what it says it does)

And now that that's done:

Option Explicit

This should be at the top of every code module you ever create in VBA. Go to Tools --> Options --> Require Variable Declaration and it will automatically insert it for you from now on.

This is important because without it, VBA will interpret any new variable names (including mis-spellings) as entirely new variables, instead of what you intended.

It also forces you to declare your variables. So you must explicitly give them a type (Long, String, Variant etc.) and a scope (Procedure Dim var As, Module Private Var As, Project Public Var As).

This will then automatically catch all sorts of unintended situations (such as accidentally setting a number equal to an object) which would not be caught if VBA has to assume that all your variables are Variant because you never explicitly declared them.

Naming

Good Variable naming is one of the most important skills you can develop as a developer. Variable names (including values, objects, functions, even whole projects) should be Clear, Concise and, above all, Unambiguous.

Your variable names here are good. Your function name is not. Longer bears absolutely no relation to "Deleting Rows".

A function name like RemoveRowsWithEmptyCellsInRange would be far better.

Re-usability

Rather than hard-coding this function for column C of the active sheet, why not create a function to do it for any column at all?

Perhaps something like this?

Private Sub DescriptiveNameHere()

    Dim columnIndex As Long
    columnIndex = 3 '/ Column "C"

    Dim firstRow As Long, lastRow As Long
    firstRow = 20
    lastRow = Cells(Rows.Count, columnIndex).End(xlUp).row

    RemoveRowsWithEmptyCellsInColumn ActiveSheet, columnIndex, firstRow, lastRow

End Sub

Public Sub RemoveRowsWithEmptyCellsInColumn(ByRef targetSheet as Worksheet, ByVal columnIndex As Long, ByVal firstRow As Long, ByVal lastRow As Long)

    Dim ws as Worksheet
    Set ws = targetSheet
    ws.Activate

    Dim row As Long, col As Long
    Dim checkCell As Range

    col = columnIndex
    For row = lastRow To firstRow Step -1
        Set checkCell = ws.Cells(row, col)
        If IsEmpty(checkCell) Then Rows(row).Delete
        '/ You might also want to check for cells that appear empty but contain E.G. a formula that is currently displaying no value, like so:
        ' If checkCell.Text = "" Then Rows(row).Delete
    Next row

End Sub

Speed

Very low-hanging standard VBA fruit here:

Public Sub DisableApplicationSettings()

    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.Calculation = xlCalculationManual

End Sub

Public Sub ResetApplicationSettings()

    Application.ScreenUpdating = True
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic

End Sub

Put that at the start and end of any VBA program and it will run inordinantly faster.

However, it doesn't take into account how the spreadsheet was before your program started (what if your user already had calculation set to manual?).

So, these, coupled with some public variables and some error handling so premature ending of the code execution still restores the settings, is even better:

Public VarScreenUpdating    As Boolean
Public VarEnableEvents      As Boolean
Public VarCalculation       As XlCalculation

Public Sub StoreApplicationSettings()

    VarScreenUpdating = Application.ScreenUpdating
    VarEnableEvents = Application.EnableEvents
    VarCalculation = Application.Calculation

End Sub

Public Sub DisableApplicationSettings()

    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.Calculation = xlCalculationManual

End Sub

Public Sub RestoreApplicationSettings()

    Application.ScreenUpdating = VarScreenUpdating
    Application.EnableEvents = VarEnableEvents
    Application.Calculation = VarCalculation

End Sub


Find Start/Stop Indices Where Array Is True If I have a list that contains True and False values, how can I find the start and stop indices of where there are blocks of True values?

x = [True, False, False, True, True, True, False, True, False, False, False, True, True, False, True]

The output that I am looking for is:

[[0,  0],
 [3,  5],
 [7,  7],
 [11, 12],
 [14, 14]
]

Here, the value in the first column is the start index and the value in the second column is the stop index.

One solution that I have is:

start = []
stop = []
if y[0]:
    start.append(0)
for i in range(len(y)):
    if y[i] and not y[i-1]:
        start.append(i)
    if i > 0 and (not y[i] and y[i-1]):
        stop.append(i-1)
if y[len(y)-1]:
    stop.append(len(y)-1)

print(list(map(list, zip(start, stop))))

Enumerate

for i in range(len(y)): is an antipattern.  If you want indices (i) and the values at those indices (y[i]) the Pythonic way is using enumerate:

for i, yi in enumerate(y):

Start indices

Let's take a moment and split this task into two parts: the start indices and the stop indices.  First, the start indices:

start = []
if y[0]:
    start.append(0)
for i in range(len(y)):
    if y[i] and not y[i-1]:
        start.append(i)

That almost looks like it could be replaced by list comprehension ...

start = [i for i, yi in enumerate(y) if yi and not y[i-1]]

... except for that y[i-1] part, which may wrap around and grab a value from the end of the array.  What you really want to do is zip the y[i] and y[i-1] sequences together.  We just need to start the y[i-1] sequence off with a leading False value:

start = [i for i, (y1, y2) in enumerate(zip([False] + y[:-1], y)) if y2 > y1]

Stop indices

The stop indices are similar, zipping y[i] with y[i+1] with a trailing False:

stop = [i for i, (y1, y2) in enumerate(zip(y, y[1:] + [False])) if y2 < y1]

Groupby

And now for something completely different.

itertools.groupby() takes sequential items with matching keys, groups them together, and emits them together with their key.  We just need the True and False values to be the key, and attach the index numbers to the True/False values.

>>> [list(group) for key, group in groupby(enumerate(x), key=lambda ix: ix[1])]
[[(0, True)],
 [(1, False), (2, False)],
 [(3, True), (4, True), (5, True)],
 [(6, False)],
 [(7, True)],
 [(8, False), (9, False), (10, False)],
 [(11, True), (12, True)],
 [(13, False)],
 [(14, True)]]

(output reformatted to show one inner list per line, for clarity)

But we only want the groups where the key is True:

>>> [list(group)
...  for key, group in groupby(enumerate(x), key=lambda ix: ix[1])
...  if key]
[[(0, True)],
 [(3, True), (4, True), (5, True)],
 [(7, True)],
 [(11, True), (12, True)],
 [(14, True)]]

For each group, we want the first and the last items in the group:

>>> [[group[0], group[-1]]
...  for group in (list(group)
...                for key, group in groupby(enumerate(x),
...                                          key=lambda ix: ix[1])
...                if key)]
[[(0, True), (0, True)],
 [(3, True), (5, True)],
 [(7, True), (7, True)],
 [(11, True), (12, True)],
 [(14, True), (14, True)]]

Actually, we only want the indices stored in those items:

start_stop = [[group[0][0], group[-1][0]]
              for group in (list(group)
                            for key, group in groupby(enumerate(x),
                                                      key=lambda ix: ix[1])
                            if key)]

Which gives:

>>> start_stop
[[0, 0], [3, 5], [7, 7], [11, 12], [14, 14]]

In the comments, Graipher suggests:

  Personally, I would make getting the groups a generator expression assigned to a variable and then iterator over that in a list comprehension ...

from itertools import groupby
from operator import itemgetter

groups = (list(group) for key, group in groupby(enumerate(x), key=itemgetter(1))
                      if key)
start_stop = [[group[0][0], group[-1][0]] for group in groups]

  ... using operator.itemgetter (instead of the lambda ix: ix[1]) for some more readability.

In the comments, RootTwo suggests:

  This could be simplified by grouping the indices directly instead of using enumerate:

[(group[0], group[-1]) for group in (
 list(group) for key, group in groupby(range(len(x)), key=x.__getitem__) if key)]

Timings, for a list of 15,000,000 entries (the original x list, replicated one million times):

Original:   7.3 seconds
AJNeufeld:  6.4 seconds
Graipher:   5.5 seconds
RootTwo:    3.1 seconds


Run several Python programs and check for errors For a post-graduate subject that I present, I maintain a repository of examples (on GitHub) from the textbook which is contributed and edited by my students, who are not very Python literate. I have written this code to run all the programs associated with figures, examples and exercises in the book and use it to alert me if a pull request has broken anything via Travis. Each program is a simple Python script which does not use any special stuff to do what it does. I don't check for correctness, just if the program actually runs. Many of the programs produce figures, so I've had to do some dirty things with matplotlib's show command so that these programs don't halt execution. This works but it all feels very hacky to me, and I'd be interested in suggestions to improve this checking.

You can check the repo for all the example files, but the main running script is here:

#!/usr/bin/env python

from __future__ import print_function
from past.builtins import execfile
import os
import traceback
import re
from collections import Counter
import sys

# disable show in figures
import matplotlib.pyplot as plt
plt.show = lambda: None

statuscounter = Counter()

itemparser = re.compile('(?P<kind>.*) (?P<chapter>.*)\.(?P<number>.*)')
allitems = open('allitems.txt').read().splitlines()

kinds = ['Figure', 'Example', 'Exercise']

faillist = []

if __name__ == "__main__":
    for item in allitems:
        kind, chapter_c, number_c = itemparser.match(item).groups()

        number = int(number_c)

        if chapter_c.isdigit():
            chapter = int(chapter_c)
            mask = '{}_{:02d}_{:02d}.py'
        else:
            chapter = chapter_c
            mask = '{}_{}_{}.py'

        filename = mask.format(kind, chapter, number)
        if os.path.exists(filename):
            try:
                execfile(filename)
                status = 'Success'
            except Exception as err:
                status = 'Failed'
                message = traceback.format_exc()
        else:
            status = 'Not implemented'

        statuscounter[status] += 1

        if status != 'Not implemented':
            print(kind, chapter, number, status)

        if status == 'Failed':
            faillist.append([kind, chapter, number])
            print(message)

    for items in statuscounter.items():
        print("{}: {}".format(*items))
    print("Failed items:")
    for items in faillist:
        print("  {} {} {}".format(*items))

    sys.exit(statuscounter['Failed'])

allitems = open('allitems.txt').read().splitlines()

You're putting all of those lines into a list, but you don't need them as a list.  All that you do with them later is iterate, so you should just say allitems = open('allitems.txt').  That will mean that the newline characters will be at the end of each line, but you can fix that by adding item = item.strip() in your loop. You should also always use a with statement when opening files.

kind, chapter_c, number_c = itemparser.match(item).groups()

What if the item doesn't match?  You'll have a very ugly error that won't make sense to whoever uses your program.  You should have some error handling added:

item = item.strip()
match = itemparser.match(item):
if not match or len(match.groups()) != 3:
    print("Invalid item: {}".format(item), file=sys.stderr)
    continue

If you want, you could throw an error instead, but your error would hopefully be more helpful to the user.

number = int(number_c)

Again, no error handling.

status = 'Success'
...
status = 'Failed'
...
status = 'Not implemented'

Those are hard-coded values.  What would happen if you had just one slight typo?  It could throw you off with something completely unexpected and it would be very difficult to debug.  You should define constants at the beginning of the file for the different status codes:

SUCCESS = 'Success'
FAILED = 'Failed'

Well, you don't really need to define one for 'Not implemented' because NotImplemented already exists as a built-in constant.  You could even use True, False, and NotImplemented.  In that case, the variable might be success instead of status.

sys.exit(statuscounter['Failed'])

I like that.  You exit with success if nothing failed, but the error exit codes make sense.


Algorithm to replace this duplicated code I've got a simple thing that I inherited from another developer and think it could be replaced with something a bit more elegant. What this is doing is comparing the length of a string to a set value, and setting the font size accordingly:

if(maxLen<50){$('.fc-cards').css('font-size',40);}
if(maxLen<100){$('.fc-cards').css('font-size',25);}
if(maxLen<150){$('.fc-cards').css('font-size',22);}
if(maxLen<200){$('.fc-cards').css('font-size',20);}

If the min font size is 18, and the max 40, what a nice way to dynamically figure a font size with a range of <50 chars being 40px to >200 being 18px... it doesn't have to match the exact values mentioned above, a sliding scale would be perfect.

So, a linear mapping from 50-200 onto 40-20 is (40-20)/(200-50) = 0.1333 font size points per length. So we can can compute this as

$('.fc-cards').css('font-size', 20 + 0.1333 * (200 - maxLen));

for a sliding linear falloff in font size. If you want to clamp the sizes to change in exactly the same way, you could index them into an array of sizes, computed by the modulus of the length, i.e. something like...

 $('.fc-cards').css('font-size', [40, 25, 22, 20][Math.floor(maxLen / 50)]);

But you'll need suitable checks on the length to ensure you remain in bounds.

These solutions are debatably more elegant though. They take less code, but they're not as easy to read as definitely solving the same problem your original if tests did. I'd probably prefer a loop over a dictionary of range/size possibilities as the best solution:

sizes = [50, 100, 150, 200];
fonts = [40, 25, 22, 20];

for (var i = 0; i < sizes.length; i++)
{
  if (maxLen < sizes[i])
  {
    $('.fc-cards').css('font-size', fonts[i]);
    break;
  }
}

Edit: Changed based on comment that for/in doesn't ensure enumeration order.

I wouldn't rage against the original solution in a code review, either. Elegance is found in the clearest expression of the goal, in tension with the solution efficiency and maintainability (where, in my view, clear expression wins 99/100 times). Efficiency is not a worry here, and adding/removing sizes from the range in the original code is trivial. Hence, pick whatever your team finds easiest to read a solving the "Change the font size based on length in these windows" problem is.


HTML website practice exercise Here is a problem I solved. Please review this.

<!DOCTYPE HTML>

            <title>BLA| Welcome </title>

Meet Jack

 He is Jack.He is an odd little boy that just got an empty box for his birthday. He may look  happy but is a little  disappointed. Now he will have to put a little things in the box.

            Jack would really like...

Roller blades

Magic tricks

Anything Pokemon

A motorized Lego set

                You can get all this (and more) at an online toy store. Two very popular ones are toysrus.com and eToys.

Okay, I am not sure exactly in what limits your exercise is, but I'll answer to what I think is good web standard. (I've been a web developer for ~4 years)

Don't use the align attribute. HTML is supposed to be a markup language. Use CSS to control the appearance of your html elements. In this case something like h1 { text-align: center; } should do just fine.
Don't use the
 tags for other things than actual line-breaks (that is, to break text). I would remove all
 tags, since you don't need them in this exercise. Elements like ul, li and p are by default block elements, which flows vertically. If you need more space between elements, use margin/padding instead. However, img is not a block element by default, so you can either wrap it in a div or make it a block element using CSS like img { display: block; }.
Do not put a ul inside a p tag. Close the p tag and then add another p tag after the ul instead.
Fine detail: insert line breaks in text to increase readability. A typical practice is to keep lines less than 80 characters.

Btw, you do know that there are validator tools for HTML, right?

EDIT: Full code, in a better way: (Note that I chose to preserve one br tag. Maybe it can be an exercise to figure out why)

<!DOCTYPE HTML>

        <title>BLA| Welcome </title>

Meet Jack

He is Jack. He is an odd little boy that just got an empty box for his
               birthday. He may look happy but is a little
               disappointed. Now he will have to put a little things in the box.

               Jack would really like...

Roller blades

Magic tricks

Anything Pokemon

A motorized Lego set

You can get all this (and more) at an online toy store. Two very popular
               ones are toysrus.com and
               eToys.


Continually refresh game data using AJAX I've been working with JavaScript and AJAX a lot in the past, and now I'm moving towards the backend and working with databases more. I want to update my game data in as close to real time as possible in JavaScript.

Here is what I have been doing to update the data as frequently as possible (I simplified it):

function updateData() {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.ontimeout = function (e) {
            // XMLHttpRequest timed out. Try sending another request
            updateData();
        };
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                // only keep the data after the leading { (in case errors are outputted) (SHOULD NOT HAPPEN)
                var data = this.responseText.slice(this.responseText.indexOf('{'));
                try {
                    data = JSON.parse(data); // we're expecting JSON data -- make this an object for us
                } catch (error) {
                    console.error("Error parsing data");
                }
                // handle data...
                // and repeat:
                updateData();
            }
        };
        xmlhttp.open("GET", "data_getter.php", true); // "?t=" + getTime() to endure that the data is not cached
        xmlhttp.send();
}
updateData();

I call the function updateData() once and then each time the previous request is received, the function gets ran again.

I am wondering, is there a better way to continually refresh data (or make the data on the game website as close to realtime as possible) than to send an AJAX request every time the previous one is received? This method means that there will be a delay of the time it takes for the server to load, but that isn't too much.

Is this the best practice? Or can you somehow open a connection to a PHP script that communicates with the server in realtime and not close the connection for the duration of the game?

Reviewing your function

Looking at updateData() shows:

(reading signature only) there's no return value. So it does something inside, potentially modifying state (outside your function scope)
(reading body) it has two responsibilities: (1) retrieve data from server via http query; (2) actual update of variable data by assigning retrieved data to it.
it calls itself in two conditional branches. In fact it's a recursive function.

Improve:

(1) The function could return retrieved data. This would make it a pure function representing query part (of CQRS).

(2) This violates CQRS. You could improve by separating query (http data retrieval) from command (updating state: assigning a new value to a variable).

(3) recursive calls of same HTTP GET request in order to keep state up-to-date  are conceptually called long-polling. This polling is usually implemented as consecutive GET requests between intervals using a timer. To reduce unnecessary updates, the polling makes advantage of HEAD requests using modifiedSince header and E-tags (both telling if server has updated data or no change since last fetch).
A resource-friendly alternative to long-polling is the use of web-socket protocol.

Web-sockets

An event-based communication between server and client via a dedicated socket-connection. This connection is opened once allowing bi-directional asynchronous text-messages. Thus the server can send an update-notification (either including updated data or to allow the client to retrieve data in a separate GET request).

There are web-socket libraries for both JavaScipt (client) and PHP (server).


Using strtol() follow up Link to original question: Successful use of strtol() in C

After getting excellent feedback from the code review community I attempted to redo my string to long conversion program! I believe I enhanced it significantly(Most ideas from answers on my last post) but I did add my own 'style' to it and am looking to see if I missed anything or need to change the way I implemented the solution.

#include "stdafx.h"
#include <stdio.h>
#include <stdbool.h>
#include <limits.h>
#include <stdlib.h>

#define MAXLENGTH 1024

bool readline(char** buf, const size_t buf_len);
char* str_to_long(const char* const buf, long* out);
void str_cmp(char* str);

int main() {
    char* str = malloc(sizeof(char) * MAXLENGTH);
    long cnvt_numb = 0;
    printf("Enter a number: ");
    while (!readline(&str, MAXLENGTH)) {
        printf("Try again...\n");
    }
    char* err = str_to_long(str, &cnvt_numb);
    if (err == NULL) {
        if (cnvt_numb == LONG_MAX) {
            fprintf(stderr, "INPUT OUT OF RANGE...\n");
        }
        else {
            printf("Your number was: %ld\n", cnvt_numb);
        }
    }
    else {
        fprintf(stderr, "That number '%s' was not convertable...\n", err);
    }

    free(str);

    printf("Press any key to continue...\n");
    getch();
    return EXIT_SUCCESS;
}

bool readline(char** buf, const size_t buf_len) {
    if ((fgets(*buf, buf_len, stdin)) != NULL) {
        str_cmp(*buf);
        return true;
    }
    return false;
}

void str_cmp(char* str) {
    if ((strchr(str, '\n')) != NULL) {
        str[strcspn(str, "\n")] = 0;
    }
}

//if returns anything other than NULL, conversion failed
char* str_to_long(const char* const str, long* out) {
    char* end = NULL;
    long number = strtol(str, &end, 10);
    if ((!*end) && (end != str)) {
        *out = number;
        end = NULL;
    }
    return end;
}

#include
You didn't supply the contents of "stdafx.h", but I was able to compile simply by removing that include, so you probably don't need it.
You do seem to be missing a necessary include of <string.h>.
malloc()
In main() you allocate a fixed length buffer on the heap.  There's no advantage over stack allocation (unless you're compiling for a system with a tiny stack), and no need to multiply by 1: sizeof (char) is necessarily 1, by definition:
char str[MAXLENGTH];

Also, I note that there aren't many platforms that support 3400-bit longs, so you could probably make the input buffer a bit smaller.
Having done this, your call to readline() can be
readline(&str, sizeof str)

readline()
You take a pointer to pointer, but don't ever change the pointed-to value, so you could introduce a const between the stars, or (better) remove a level of indirection:
bool readline(char *buf, const size_t buf_len) {
    if (fgets(buf, buf_len, stdin)) {
        str_cmp(buf);
        return true;
    }
    return false;
}

Also, it's not clear whether you want to return true for a partial-line read; it's probably worth a comment to clarify this.
str_cmp()
You should remember the value returned by strchr, rather than recomputing it:
void str_cmp(char* str) {
    char *nl = strchr(str, '\n');
    if (nl)
        *nl = '\0';
}

I also prefer to write '\0' in preference to 0 to show we're dealing with string characters here.
Since it's only used from one place, we can inline this function.  It's a judgement call, but I don't see enough benefit from having this separate.
back to main():
This code doesn't do what it claims:
char* err = str_to_long(str, &cnvt_numb);
/* ... */
fprintf(stderr, "That number '%s' was not convertible...\n", err);

The value you get back as err isn't the string - it's only the portion after the parseable digits.  So for input such as 10x, you'll just print x; it's not obvious that's what you want.  I'd show the whole string to the user:
fprintf(stderr, "That number '%s' was not convertible...\n", str);

You probably don't want to return EXIT_SUCCESS in that case, either.
You test for output of LONG_MAX, but not LONG_MIN.  Actually, both of those are legitimate outputs, so by not testing errno, you are unnecessarily reducing the range you accept.
There's a call to getch() but that's not declared in any of the headers.  Perhaps you meant getchar()?  Either way, asking for extra input just to exit the program is user-hostile, and I'd just excise that part.

Putting it all together
Here's what I ended up with:
#include <errno.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

#define MAXLENGTH 40            /* supports 128-bit long */

bool readline(char* buf, const size_t buf_len) {
    if (!fgets(buf, buf_len, stdin))
        return false;
    char *nl = strchr(buf, '\n');
    if (nl)
        *nl = '\0';
    return true;
}

// returns NULL on success, or the unparseable portion of input on failure
char* str_to_long(const char* const str, long* out) {
    char* end;
    errno = 0;
    *out = strtol(str, &end, 10);
    if (!*end && end != str)
        end = NULL;
    return end;
}

int main() {
    char str[MAXLENGTH];
    long cnvt_numb = 0;

    printf("Enter a number: ");
    while (!readline(str, sizeof str)) {
        printf("Try again...\n");
    }

    char* err = str_to_long(str, &cnvt_numb);
    if (err) {
        fprintf(stderr, "That number '%s' was not convertible...\n", str);
        return EXIT_FAILURE;
    } else if (errno == ERANGE) {
        fprintf(stderr, "INPUT OUT OF RANGE...\n");
        return EXIT_FAILURE;
    } else {
        printf("Your number was: %ld\n", cnvt_numb);
        return EXIT_SUCCESS;
    }
}

It accepts input from -9223372036854775808 to 9223372036854775807 (on this system, where long is 64 bits), and correctly reports a range error for -9223372036854775809 and for 9223372036854775808.
12345aoeu is rejected with

That number '12345aoeu' was not convertible...


C code to reset all usb devices (because ubuntu laptop disables usb mouse after sleep) I don't program much in c, but cobbled together some code from different places in google. I'm sure this is backwards especially in how it works with strings and directory files. Please let me know what the best practices would have been.
/* usbresetall -- reset all of the usb devices on bus 001 */

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>

#include <dirent.h>
#include <string.h>

#include <linux/usbdevice_fs.h>

int main(int argc, char **argv)
{
    int fd;
    int rc;

    DIR *d;
    char filename[256];
    struct dirent *dir;

    d = opendir("/dev/bus/usb/001/");
    strcpy(filename,"/dev/bus/usb/001/");

    if (d) {
        while ((dir = readdir(d)) != NULL) {
            strcat(filename,dir->d_name);
            printf("%s\n", filename);

        fd = open(filename, O_WRONLY);
        if (fd < 0) {
        perror("Error opening output file");
        } else {

        printf("Resetting USB device %s\n", filename);
        rc = ioctl(fd, USBDEVFS_RESET, 0);
        if (rc < 0) {
            perror("Error in ioctl");
        } else printf("Reset successful\n");

        close(fd);
            }

            strcpy(filename,"/dev/bus/usb/001/");
        }
        closedir(d);
    }

    return 0;
}

Use consistent indentation
I don't know if the indentation style of the code you pasted here is as you wrote it, but if it looks the same in your code editor, you should definitely try to fix it and make it more consistent. I would not bother trying to fix this manually, instead use either code formatting functions of your editor, or use an external tool such as GNU indent, ClangFormat or Artistic Style to reformat your source code.
Return a non-zero exit code on error
When you encounter an error you call perror(), which is great, but then you let the program continue to run, and eventually you will always return 0. It would be better if your program would return a non-zero exit code when it has encountered an error, in particular return EXIT_FAILURE, which is declared in <stdlib.h>.
Don't hardcode the USB bus number
You only open USB bus 1. What if you attach your mouse to a different USB port, which might be on a different bus? I would make it so that the USB bus number has to be passed as a command line option. This makes the program a lot more flexible.
Beware of buffer overflows
While arguably the /dev/bus/usb pathnames can never be longer than 255 bytes, it is bad practice to make any assumptions. At the very least, make sure you never overflow this buffer, by using strncpy() and strncat() instead of strcpy() and strcat() (read the documentation of these functions carefully and be aware what happens if the destination buffer is not large enough to hold the input), or use snprintf(). However, there is another way to sidestep this issue entirely:
Avoid the need for concatenating paths
Instead of manipulating filename all the time, you can avoid all this by calling chdir() to go into the USB bus's directory, like so:
if (chdir("/dev/bus/usb/001/") == -1) {
    perror("Error accessing USB bus directory");
    return EXIT_FAILURE;
}

DIR *d = opendir(".");

if (!d) {
    perror("Error reading USB bus directory");
    return EXIT_FAILURE;
}

struct dirent *ent;

while ((ent = readdir(d)) != NULL) {
    int fd = open(ent->d_name, O_WRONLY);
    ...

Declare variables as close as possible to where they are used
In C99 and later, you can declare a variable anywhere. It is considered good practice to declare a variable close to where it is used, and ideally you also declare and initialize it in one go. I've used this in the example above.