How do I write my multithreading code correctly so that it is faster than a single thread? I have the following code that I am trying to optimize. I am running it on a Linux machine with 24 cores. I thought I could use multithreading to make it faster, but it's somehow making it way slower than the single thread. I believe it is because of cache misses but I am not sure how to structure this code to avoid them.

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

#include "mex.h"
#include "string.h"
#include "pthread.h"

/* NOT sure why multi-threading is worse than single thread */
#define NTHREADS 1

/* An argument list structure */
struct argList{
    double *X;
    double *p;
    int cycles;
    int N1;
    int N2;
};
typedef struct argList aList;

long int bino_rand(double p, double n)
{
  long int    x = 0;
  int i;
  for (i=0; i<n; i++)
    if (((double)rand())/RAND_MAX < p) x++;

  return(x);
}

void *bino_process(void* arglist){
    double* X   = ((aList*)arglist)->X;
    double* p   = ((aList*)arglist)->p;
    int cycles  = ((aList*)arglist)->cycles;
    int N1      = ((aList*)arglist)->N1;
    int N2      = ((aList*)arglist)->N2;
    int i,j;

/* mexPrintf("N1=%d, N2=%d, cycles=%d\n", N1, N2, cycles); */

for(i = N1;i<N2;i++){
    double tmp = X[i];
    double tmp_p = p[i];
    for(j=0;j<cycles;j++){
        tmp = tmp + bino_rand(tmp_p,tmp);
    }
    X[i] = tmp;
}
}

void mexFunction( int nlhs, mxArray *plhs[],
              int nrhs, const mxArray *prhs[])
{
double* n;              /* input scalar */
double* p;
int cycles;
mwSize N;               /* size of matrix */
double *X;              /* output matrix */
int i;

pthread_t threadA[NTHREADS];
int iterA[NTHREADS];
aList arglistA[NTHREADS];

int N1[NTHREADS];
int N2[NTHREADS];

/* get the value of the scalar input  */
n = mxGetPr(prhs[0]);
p = mxGetPr(prhs[1]);
cycles = (int)mxGetScalar(prhs[2]);

N = mxGetM(prhs[0]);

/* create the output matrix */
plhs[0] = mxCreateDoubleMatrix(N,1,mxREAL);

/* get a pointer to the real data in the output matrix */
X = mxGetPr(plhs[0]);

memcpy(X,n,N*sizeof(double));

for(i=0;i<NTHREADS;i++){
    N1[i] = i*(N/NTHREADS);
    N2[i] = (i+1)*(N/NTHREADS);
    arglistA[i].X = X;
    arglistA[i].p = p;
    arglistA[i].cycles = cycles;

    arglistA[i].X = (double*)mxCalloc(N2[i]-N1[i],sizeof(double));
    memcpy(arglistA[i].X, &X[N1[i]], (N2[i]-N1[i])*sizeof(double));

    arglistA[i].N2 = N2[i]-N1[i];
    arglistA[i].N1 = 0;

}

/* Spawn the threads */
for(i=0;i<NTHREADS;i++){
    iterA[i] = pthread_create(&threadA[i],NULL,bino_process,(void*)&arglistA[i]);
}

/* Join back the threads */
for(i=0;i<NTHREADS;i++){
    pthread_join(threadA[i],NULL);
}

/* Copy back the data from the chunks into X */
for(i=0;i<NTHREADS;i++){
    memcpy(&X[N1[i]], arglistA[i].X, (N2[i]-N1[i])*sizeof(double));
}

for(i=0;i<NTHREADS;i++){
    mxFree(arglistA[i].X);
}

}

How did you time your program?
I ran a modified version of your program (I didn't have "mex.h" so I rewrote the parts that needed it).  When I ran your program with 1, 2, and 4 threads, I got the following results:
1 thread  = 9.38 sec
2 threads = 4.93 sec
4 threads = 2.81 sec

So it seems to be working for me.  How did you time your program?  You may have been using a function such as clock() that measures cpu time across all cores, instead of measuring wall clock time.  See this Stackoverflow question for more information on that.


Computing the sentiment score of a sentence I wrote a script to compute the polarity of sentences. The script is based on this:

The lexicon based approach assigns a sentiment tags to words in a text based on dictionaries of positive and negative words. A sentiment score is then calculated for each document as:
$$\frac{\text{number of positive words} - \text{number of negative words}}{\text{total number of words}}$$

Is the code below okay? Should I have to improve it to take into consideration the position of the word in the sentence?
fileDir = os.path.dirname(os.path.realpath('__file__'))

lexiconpos = open(os.path.join(fileDir, 'Ressources/positive.txt'), 'r', encoding='utf-8')
lexiconpos = set([line.rstrip() for line in lexiconpos])
lexiconneg = open(os.path.join(fileDir, 'Ressources/negative.txt'),'r', encoding='utf-8')
lexiconneg = set([line.rstrip() for line in lexiconneg])

path = os.path.join(fileDir, 'data/reference/freins.tsv')
with open(path, 'r', encoding='utf-8') as l_1:
    next(l_1)
    l_1 = [line.rstrip() for line in l_1]

tagger = treetaggerwrapper.TreeTagger(TAGLANG='fr')
def process_text(text):
    '''extract lemma and lowerize then removing stopwords.'''

    text_preprocess =[]
    text_without_stopwords= []

    text = tagger.tag_text(text)
    for word in text:
        parts = word.split('\t')
        try:
            if parts[2] == '':
                text_preprocess.append(parts[0])
            else:
                text_preprocess.append(parts[2])
        except:
            print(parts)

    text_without_stopwords= [word.lower() for word in text_preprocess if word.isalnum()] #if word not in stopWords
    #return text_without_stopwords
    return text_preprocess

def extract_info(texte, lexique1, lexique2):

    lemme_treetagger = []
    lemme_sent = []
    len_sent = []
    for elt in texte:
        texte_treetagger = process_text(elt.lower())
        lemme_treetagger.append(" ".join(texte_treetagger))
        len_sent.append(len(" ".join(texte_treetagger)))

    word_pos=[]
    word_neg=[]
    word_pos_len=[]
    word_neg_len=[]
    for elt in lemme_treetagger:
        word_pos.append([word for word in lexique1 if word in elt])
        word_neg.append([word for word in lexique2 if word in elt])

    for lst in word_pos:
        word_pos_len.append(len(lst))
    for lst in word_neg:
        word_neg_len.append(len(lst))

    print(len_sent)
    return texte, lemme_treetagger, len_sent, word_pos, word_neg, word_pos_len, word_neg_len

texte, lemme_treetagger, len_sent, word_pos, word_neg, word_pos_len, word_neg_len = extract_info(l_1, lexiconpos, lexiconneg)

def polarity_word(texte, *args):

    texte, lemme_treetagger, len_sent, word_pos, word_neg, word_pos_len, word_neg_len = extract_info(texte, lexiconpos, lexiconneg)

    for elt in lemme_treetagger:
        print(elt)
        sub_list = list(map(operator.sub, word_pos_len, word_neg_len))
        score = list(map(truediv, sub_list, len_sent))

    return score

TSV parsing
You should not need to do this yourself. The csv module accepts a delimiter that you can set to '\t'.
Context management
You use with in one out of three file opens - the other two would benefit.
Inner list
This:
set([line.rstrip() for line in lexiconpos])

can be
{line.rstrip() for line in lexiconpos}

The set literal and removal of the inner list will both express this better.


Refactoring multistep form rails I've implemented just as Ryan Bates suggested in his railscast episode, I've got everything working just right, so here is his approach in the controller :

def new
    session[:order_params] ||= {}
    @order = Order.new(session[:order_params])
    @order.current_step = session[:order_step]
  end

  def create
    session[:order_params].deep_merge!(params[:order]) if params[:order]
    @order = Order.new(session[:order_params])
    @order.current_step = session[:order_step]
    if @order.valid?
      if params[:back_button]
        @order.previous_step
      elsif @order.last_step?
        @order.save if @order.all_valid?
      else
        @order.next_step
      end
      session[:order_step] = @order.current_step
    end
    if @order.new_record?
      render "new"
    else
      session[:order_step] = session[:order_params] = nil
      flash[:notice] = "Order saved!"
      redirect_to @order
    end
  end

I'm ok with new method, but I wonder how can the create method be refactored. He said that he will do an episode how to refactor this in the future but he did not yet.

This was my take on he reactoring (more splitting than refactoring):

  def create
    session[:order_params].deep_merge!(params[:order]) if params[:order]
    @order = Order.new(session[:order_params])
    @order.current_step = session[:order_step]

    process_step

    if @order.new_record?
      render "new"
    else
      session[:order_step] = session[:order_params] = nil
      flash[:notice] = "Order saved!"
      redirect_to @order
    end
  end

def process_step
  if @order.valid?
    if params[:back_button]
      @order.previous_step
    elsif @order.last_step?
      @order.save if @order.all_valid?
    else
      @order.next_step
    end
    session[:order_step] = @order.current_step
  end
end

I would advise you to look at wicked gem: https://github.com/schneems/wicked.

Using this gem you could write:

include Wicked::Wizard
steps <list_of_your_steps>

def new
  session[:order_id] ||= Order.create.id
end

def show
  @order = Order.find(session[:order_id])
end

def update
  @order = Order.find(session[:order_id])
  @order.assign_attributes(params[:order_params])
  @order.current_step = step
  render_wizard @order
end

It will also separate your forms for each step. To add any  step-related action you can use step reader:

def update
  @order = Order.find(session[:order_id])
  @order.assign_attributes(params[:order_params])
  @order.current_step = step
  @order.do_sth if step == :my_step
  render_wizard @order
end

render_wizard automatically tries to save an object and redirects you to the next step or renders current form depending on a result.


String finder function This function finds a certain string within a string. I feel like my function is too messy.

I'm new to C memory functions. Are there any shortcuts I can take to make this function shorter? Is this implementation effective?

bool strfound(char * find, char * name) {
    int j = 0;
    int size = strlen(name) + 1;
    char * buffer;
    buffer = (char*)malloc(size + 1);
    for (int i = 0; i < size; i++) {
        std::cout << buffer << std::endl;
        if (i == 0 && name[i] != ' ') {
            j++;
            buffer[j - 1] = name[0];
        }
        if (i != 0 && name[i] != ' ') {
            buffer[j] = name[i];
            j++;
        }

        if (name[i] == ' ' || i == size - 1) {
            buffer[j] = '\0';
            if (strcmp(buffer, find) == 0) {
                std::cout << "String is found: " << buffer << std::endl;
                buffer[j] = '\0';
                std::cout << name << std::endl;
                free(buffer);
                return true;
            }
            strcpy(buffer, ""); // or null from 0
            buffer = (char*)malloc(size + 1);
            j = 0;
        }
    }
    return false;
}

Output: https://gyazo.com/2fc0fe88958dfeb2f8cf20eff1f2d113

The answer to your primary question is yes, methods already exist in libraries that can do this. Reference this StackOverflow post.

Your method can then be simplified to what's shown below:

#include <string>

bool strfound(const std::string &in, const std::string &substr) {
    return in.find(substr) != std::string::npos;
}


Java Cryptography AES/GCM/NoPadding without IV I'm implementing a PAKE-protocol as a university project and one step of the protocol involves sending encrypted data from a key created via a hash function (I'm using SHA-256). I want to use AES/GCM but do not have an IV so I'm using HKDF to expand the key into an IV and key with the following implementation:

import org.bouncycastle.crypto.digests.SHA256Digest;
import org.bouncycastle.crypto.generators.HKDFBytesGenerator;
import org.bouncycastle.crypto.params.HKDFParameters;
import org.bouncycastle.util.Arrays;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class AesGcmSymmetricCipher {

    public static final String CIPHER_ALGORITHM = "AES/GCM/NoPadding";

    public static final int GCM_TAG_BITS = 128;
    public static final int IV_BYTES = 12;
    public static final int KEY_BYTES = 32; // AES-256

    private final Cipher cipher;

    public AesGcmSymmetricCipher() throws NoSuchPaddingException, NoSuchAlgorithmException {
        this.cipher = Cipher.getInstance(CIPHER_ALGORITHM);
    }

    public byte[] encrypt(byte[] key, byte[] clearData) throws InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {

        var spec = expandKeys(key);
        cipher.init(Cipher.ENCRYPT_MODE, spec.getKeySpec(), spec.getParameterSpec());

        return cipher.doFinal(clearData);
    }

    public byte[] decrypt(byte[] key, byte[] cipherData) throws InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {

        var spec = expandKeys(key);
        cipher.init(Cipher.DECRYPT_MODE, spec.getKeySpec(), spec.getParameterSpec());

        return cipher.doFinal(cipherData);
    }

    public String getAlgorithm() {
        return "AES-256/GCM/NoPadding using HKDF";
    }

    private KeyedMaterial expandKeys(byte[] initialKey) {
        var iv = new byte[IV_BYTES];
        var key = new byte[KEY_BYTES];

        var digest = new SHA256Digest();
        var kdf = new HKDFBytesGenerator(digest);
        var parameters = new HKDFParameters(initialKey, null, null);

        kdf.init(parameters);
        kdf.generateBytes(iv, 0, IV_BYTES);
        kdf.generateBytes(key, 0, KEY_BYTES);

        var gcmParameters = new GCMParameterSpec(GCM_TAG_BITS, iv);
        var keySpec = new SecretKeySpec(key, CIPHER_ALGORITHM);

        Arrays.clear(iv);
        Arrays.clear(key);

        return new KeyedMaterial(gcmParameters, keySpec);
    }

    private static class KeyedMaterial {

        private final GCMParameterSpec parameterSpec;
        private final SecretKeySpec keySpec;

        public KeyedMaterial(GCMParameterSpec parameterSpec, SecretKeySpec keySpec) {
            this.parameterSpec = parameterSpec;
            this.keySpec = keySpec;
        }

        public GCMParameterSpec getParameterSpec() {
            return parameterSpec;
        }

        public SecretKeySpec getKeySpec() {
            return keySpec;
        }
    }
}

I'm interested in feedback especially from a security perspective, if relevant the initial key passed to encrypt/decrypt is ephemeral and will only be used once for encryption and once for decryption.

First of all, thank you for using my Bouncy Castle additions:

  Maarten Bodewes  initial implementation of HKDF and NIST SP 800-108 MAC based KDF functions.

Design review:

You are trying to emulate Java's Cipher class. However, I don't think this is such a good idea if your encrypt methods and is so much different. The Cipher class in Java is reusable for the same key (and, unfortunately, same IV) and this one is clearly not. I would not try this hard. Better make this class more specific to the use case and avoid generic wrapper classes.

In itself, you may wonder if combining the key derivation and cipher is a good idea. You're removing the update functionality, for instance. This is less of a problem if this is for a specific use case.

Code review:

public class AesGcmSymmetricCipher {

Better made final.

The name doesn't fully capture what the class is about. Symmetric is already captured by Aes. The key derivation part is missing on the other hand.

public static final int KEY_BYTES = 32; // AES-256

I generally use 256 / Byte.SIZE so it is clear where the 32 comes from, that way you don't need the comment.

public byte[] encrypt(byte[] key, byte[] clearData) throws InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {

clearData is generally called plaintext or simply message within the crypto community.

Other users will probably wonder what happened with the IV or nonce and where to insert it.

Arrays.clear(iv);
Arrays.clear(key);

Beware that Oracle made a huge mistakes by making the actual keys in software impossible to destroy. You can do this and fool yourself. It's great that you thought of this though - you're not in the wrong here.

return "AES-256/GCM/NoPadding using HKDF";

Nice but it doesn't capture all the little details - how is the IV calculated, for instance - so it doesn't seem to be of much use. You cannot specify it to anybody and they will know how to implement it.

Otherwise the class and design seems spot on to me, so well done.


Authentication system - AngularJS & Ionic Framework I am building a small mobile app using ionic framework and angularJS. It will consume a Restful Web Service that requires authentication (User token). This means I need to keep my users token by myself. Could anyone tell me if there is room for improvement in my code? Specifically regarding to my "Session" service which I feel kind of useless. I based my development in this article.

.run(function ($rootScope, EVENTS, AuthService) {
  $rootScope.$on('$stateChangeStart', function (event, next) {
    if (next.requiresAuth === true && !AuthService.isAuthenticated()) {
      event.preventDefault();
      $rootScope.$broadcast(EVENTS.notAuthenticated);
      console.log('You can not access here without login in.');
    }

  });
})
.constant('apiAddress', 'http://api.test.com')
.constant('EVENTS', {
  loginSuccess: 'auth-login-success',
  loginFailed: 'auth-login-failed',
  logoutSuccess: 'auth-logout-success',
  sessionTimeout: 'auth-session-timeout',
  notAuthenticated: 'auth-not-authenticated',
  signupSuccess: 'auth-signup-success'
})
.service('Session', function() {
  this.create = function(user) {
    this.user = user;
  };
  this.destroy = function() {
    this.user = null;
  }
  return this;
})
.factory('AuthService', function($http, $rootScope, $localstorage, Session, apiAddress, EVENTS) {
  var authService = {};
  authService.login = function(credentials){
    return $http.post(apiAddress + '/users/', credentials).then(function(res) {
      Session.create(res.data);
      $localstorage.setObject('user', res.data);
      return res.data;
    });
  };
  authService.signup = function(signForm){
    return $http.post(apiAddress + '/users/', signForm).then(function(res) {
      $rootScope.$broadcast(EVENTS.signupSuccess);
      return res.data;
    });
  };
  authService.logout = function(){
    Session.destroy();
    $localstorage.destroy('user');
    $rootScope.$broadcast(EVENTS.logoutSuccess);
  };
  authService.isAuthenticated = function() {
    return !!Session.user;
  };

  return authService;
})
controller('appCtrl', function($scope, $rootScope, $state, AuthService, EVENTS, $localstorage, Session) {
  $scope.currentUser = null;
  $scope.isAuthorized = AuthService.isAuthorized;
  $scope.setCurrentUser = function (user) {
    $scope.currentUser = user;
  };
  function logout() {
    AuthService.logout();
  }
  function redirectToLogin() {
    $state.go('login');
  }

  if ($localstorage.isSet('user') && Session.user == null) {
    Session.create($localstorage.getObject('user'));
    $scope.setCurrentUser($localstorage.getObject('user'))
    $state.go('cobranzas.clientes.index');
  }
  // We listen to auth related broadcasts.
  $scope.$on(EVENTS.notAuthenticated, logout);
  $scope.$on(EVENTS.sessionTimeout, logout);
  $scope.$on(EVENTS.signupSuccess, logout);
  $scope.$on(EVENTS.logoutSuccess, redirectToLogin);
})
.controller('loginCtrl', function($scope, $rootScope, $state, EVENTS, AuthService) {
  $scope.credentials = {
    username: '',
    password: ''
  };
  $scope.login = function (credentials) {
    AuthService.login(credentials).then(function (user) {
      $rootScope.$broadcast(EVENTS.loginSuccess);
      $scope.setCurrentUser(user);
      $state.go('index');
    }, function () {
      $rootScope.$broadcast(EVENTS.loginFailed);
    });
  };
})
.controller('signupCtrl', function($scope, $rootScope, $state, EVENTS, AuthService) {
  $scope.signForm = {
    FirstName: '',
    LastName: '',
    Password: '',
    Country: '',
    Phone: '',
    Gender: '',
    Position: ''
  };
  $scope.signup = function (signForm) {
    AuthService.signup(signForm).then(function (data) {
      $state.go('login');
    }, function (data) {
      console.log("Failed");
    });
  };
})

I don't feel right storing user in scope and in the service. Also I feel weird verifying localstorage in app controller. (App controller is a controller placed high at DOM so it's inherited by the others).

First thing that strikes me is the inconsistency of naming.
Why using different styles 'apiAddress', 'EVENTS' for constants?

Your .service does not need to return this, see here.

I would use something like signData instead of signForm as Form has another meaning.

As a personal preference I like to write my .factory as

.factory('AuthService', function (...) {

   // any initialization and private method go here
     ...

   return {

      // public methods
      login: function (...) {...},
         ...

   }
});

This keeps my code DRYer and I don't need to worry misprinting authService or forgetting to return it (your factory does need a return!)

You don't handle errors (but you knew this already ;-)

Your .controller has way too many dependencies and responsibilities. The best practice is to keep your controllers "thin" with the sole responsibility to glue your data with its scope. Changing routes (states) looks like one job too much. Say you want to keep another sing-up form elsewhere with its own controller - will you really want to copy over all the logic?

This is good for prototyping but is generally to be avoided in production:

console.log("Failed");

Also this way of using promise is not recommended - notice that your second (error) function will not catch any errors inside the first (success) function. A better way is:

AuthService.signup(signForm)
  .then(function (data) {

     // handle your success here

  })
  .error(function (error) {

     // handle your error here

  });

Now you will catch all errors.

Finally, I find your Session too small and AuthService too big with too many dependencies. It plays a role of a "Manager" that has many responsibilities. You can't always avoid it but you want to keep Manager's jobs at high level and as simple as possible. Imagine a stupid manager that knows nothing other than how to give basic commands :)

.factory('AuthService', function (...) {
   return {
      login: function (credentials) {
         Server
           .login(credentials)
           .then(function (user) {

              // we return the result promise that can be picked by next function
              return UserService.create(user);
           })
           .error(function (error) {
             ...
           })
      }
   }
})


using (IDisposable) in c# factory pattern How will this example affect the using implementation in GetSite?

public abstract class SPSiteBase
{
    protected abstract string Url { get; set; }

    public abstract SPSite GetSite(string url = null);
}

public class SPSiteFactory : SPSiteBase
{
    protected override sealed string Url { get; set; }

    public override SPSite GetSite(string url = null)
    {
        using (SPSite site = new SPSite(string.IsNullOrEmpty(url) ? Url : url))
        {
            return site;
        }
    }

    public SPSiteFactory() { }

    public SPSiteFactory(string url)
    {
        Url = url;
    }
}

I call it like this

SPSiteFactory siteFactory = new SPSiteFactory("http://portalurl/");
SPSite site = siteFactory.GetSite();

I've noticed that the code steps out of the using after I run the siteFactory.GetSite() method but will the site ever be disposed?

No, you can't use a factory to dispose your objects, your code is creating and immediately disposing the SPSite  as soon as it steps out of your method.

public override SPSite GetSite(string url = null)
{
    return new SPSite(string.IsNullOrEmpty(url) ? Url : url))
}

Have the disposing be handled by the calling code. You can't handle the disposing from a factory method.

If you want to check it out, build your own disposable type, put a breakpoint in the Dispose method and check for yourself.


Handling many networking exceptions in Ruby What would be a prettier/faster way to do handling of multiple errors in Ruby? Here is the code I am working with:

begin
  response = session.get url
rescue Patron::HostResolutionError
  puts "Error resolving remote host."
  exit 1
rescue Patron::PartialFileError
  puts "File size mismatch. (Host reported a file size, but the actual file is of different size)"
  exit 1
rescue Patron::TimeoutError
  puts "Operation timed out."
  exit 1
rescue Patron::TooManyRedirects
  puts "Tried redirecting too many times."
  exit 1
rescue Patron::URLFormatError
  puts "Error with the URL format"
  exit 1
rescue Patron::UnsupportedProtocol
  puts "This URL is using a protocol that we cannot handle."
  exit 1
rescue Patron::ConnectionFailed
  puts "Error connecting to host. Check your internet connection."
  exit 1
end

Since you're rescuing all 7 subclasses of Patron::Error, it would make sense to directly rescue Patron::Error rather than rescuing them one by one.

You're also duplicating work that has already been done for you, by formulating your own error message for each exception: the exceptions will already contain a useful error message, which even contains more information than yours do (for example if the host could not be resolved, the exception's message will contain the hostname that could not be resolved instead of just saying "Error resolving remote host").

Lastly I would print error messages to stderr, not stdout, as that's where they're supposed to go.

So I would write the code like this:

begin
  response = session.get url
rescue Patron::Error => e
  $stderr.puts e.message
  exit 1
end


this code returns environment varibles or passed enverionment variable with values #!/usr/bin/env python

import os, sys

variable =  sys.argv

def enVar(variable):
        """
        This function returns all the environment variable set on the machine or in active project.
        if environment variable name is passed to the enVar function it returns its values.
        """
        nVar = len(sys.argv)-1
        if len(variable) == 1: # if user entered no environment variable name
                for index, each in enumerate(sorted(os.environ.iteritems())):
                        print index, each
        else: # if user entered one or more than one environment variable name
                for x in range(nVar):
                        x+=1
                        if os.environ.get(variable[x].upper()): # convertes to upper if user mistakenly enters lowecase
                                print "%s : %s" %  (variable[x].upper(), os.environ.get(variable[x].upper()))
                    else: print 'Make sure the Environment variable "%s" exists or spelled correctly.' % variable[x]

enVar(variable)

i run the above file as show.py is their a better way to do it ?

Some notes:

Indent with 4 spaces (PEP8)
Use underscore_names for function and variables.
Use meaningful names for functions (specially) and variables.
Be concise in docstrings.
Don't write for index in range(iterable): but for element in iterable:.
Don't write variable = sys.argv in the middle of the code, use it directly as an argument.
Use an import for each module.

I'd write (Python 3.0):

import sys
import os

def print_environment_variables(variables):
    """Print environment variables (all if variables list is empty)."""
    if not variables:
        for index, (variable, value) in enumerate(sorted(os.environ.items())):
            print("{0}: {1}".format(variable, value))
    else:
        for variable in variables:
          value = os.environ.get(variable)
          if value:
              print("{0}: {1}".format(variable, value))
          else:
            print("Variable does not exist: {0}".format(variable))

print_environment_variables(sys.argv[1:])

But if you asked me, I would simplify the function without second thoughts, functions are more useful when they are homogeneous (that's it, they return similar outputs for the different scenarios):

def print_environment_variables(variables):
    """Print environment variables (all if variables list is empty)."""
    variables_to_show = variables or sorted(os.environ.keys())
    for index, variable in enumerate(variables_to_show):
        value = os.environ.get(variable) or "[variable does not exist]"
        print("{0}: {1}={2}".format(index, variable, value))


Hiding all list items except the current and next element Only current and next element (cNum) is to be shown; in case it is the last element in the structure - the first element should be shown.

var childNum = $('#h').roundabout("getChildInFocus");
var cNum = (Number(childNum)) + 2;
if (cNum == 5) cNum = 1;

$("#h li").hide();
$("#h li:nth-child(" + cNum + ")").show();

if (cNum == 4) {
    $("#h li:nth-child(1)").show();
} else {
    $("#h li:nth-child(" + (Number(cNum) + 1).toString() + ")").show();
}

Here are a few tips.

1)

childNum is not needed since it's only used once.

Old Code:

var childNum = $('#h').roundabout("getChildInFocus");
var cNum = (Number(childNum)) + 2;

New Code:

var cNum = $('#h').roundabout("getChildInFocus") + 2;

2)

In general, adding a object to a string results in a string.
And adding a object to a number results in a number.
Use parentheses to make the conversion clear.

Old Code:

$("#h li:nth-child(" + (Number(cNum) + 1).toString() + ")").show();

New Code:

$("#h li:nth-child(" + (1 + cNum) + ")").show();

3)

Cache commonly referenced elements.
Old Code:

$("#h li:nth-child(" + cNum + ")").show();

New Code:

var $lis = $("#h li");
$lis.find(":nth-child(" + cNum + ")").show();

4)

Combine operations on selectors.
Old Code:

$("#h li").hide();
$("#h li:nth-child(" + cNum + ")").show();

New Code:

$("#h li").hide().find(":nth-child(" + cNum + ")").show();

5)

Resetting the value of cNum to get rid of the else condition gives the following.

Old Code:

if (cNum == 4) {
    $("#h li:nth-child(1)").show();
} else {
    $("#h li:nth-child(" + (1+cNum) + ")").show();
}

New Code 1:

if (cNum == 4) {
    cNum = 0;
}
$("#h li:nth-child(" + (1+cNum) + ")").show();

New Code 2:

if (cNum == 5) {
    cNum = 1;
}
$("#h li:nth-child(" + cNum + ")").show();

As you can see New Code 2 is the same from the new code in tip 4.
So you can delete this redundant code.

Final Code:

var cNum = 2 + $('#h').roundabout("getChildInFocus");
if (cNum == 5){
    cNum = 1;
}
$("#h li").hide().find(":nth-child(" + cNum + ")").show();


Creating a List of objects of a particular type only I'm using an interface IloadPattern on deriving 3 classes from it LinearLoadingPattern , JointsLoadingPattern , AreaLoadingPattern. At some point, I'm creating a list all the objects created from those classes.

At a later point, I want to retrieve list of a particular object (JointsLoadingPattern) from the above list, I'm wondering if there is a better way to do this.

Below is the way I'm doing it.

public List<JointsLoadingPattern> GetJointLoads()
{
    List<JointsLoadingPattern> jointLoads =  new List<JointsLoadingPattern>();

    foreach(ILoadPattern loadCase in AllLoadCases)
    {
        if(loadCase.GetType() ==typeof(JointsLoadingPattern))
        {
            jointLoads.Add((JointsLoadingPattern)loadCase);
        }
    }

    return jointLoads;
}

You can use the LINQ extension method IEnumerable.OfType<TResult>(), it returns any elements matching the type of the specified generic type argument:

var jointLoads = AllLoadCases.OfType<JointsLoadingPattern>();

This will return an IEnumerable<JointsLoadingPattern>, you can always call .ToList() to convert it.

Update

As noted in the comments, since OfType<TResult> uses the is operator internally, it will also return any sub-types of TResult type. If you want to avoid this you can write your own extension method:

public static class Extensions
{
    public static IEnumerable<TResult> OfExactType<T, TResult>(this IEnumerable<T> source)
        where TResult : T
    {
        return OfExactType<TResult>(source);
    }

    public static IEnumerable<TResult> OfExactType<TResult>(this IEnumerable source)
    {
        if (source == null) throw new ArgumentNullException(nameof(source));
        return OfExactTypeImpl<TResult>(source);
    }

    private static IEnumerable<TResult> OfExactTypeImpl<TResult>(IEnumerable source)
    {
        var type = typeof(TResult);
        foreach (var t in source)
        {
            if (t.GetType() == type)
            {
                yield return (TResult)t;
            }
        }
    }
}

Example usage:

AllLoadCases.OfExactType<ILoadPattern, JointsLoadingPattern>();
AllLoadCases.OfExactType<JointsLoadingPattern>();

The main difference between the 2 being, that in 1 case you will get a compile-time error when you attempt to provide invalid types, while in the other case this, will be ignored and it will simply return nothing.


Find a missing element in a sorted list I've made a program to find the missing element (if there's one) in a sequential list:

Program criteria:
Returns -1 if a list is sequential, otherwise the missing element in a non-sequential list is returned.
To perform binary search, we need a key to half the problem size. Here the key is to find the distance between the index (low, high) and their corresponding values.
Compilation procedure:
gcc -Wall -g testList.c -o testList

./testList

#include<stdio.h>
int bSearch(int array[], int low, int high){

  int mid = (low + high) / 2;

  if(high == low + 1){
    if(array[high] > array[low] +1){
      return array[low] +1;
    }else{
      return -1;
    }
  }

  if(array[mid] - array[low] == mid - low){
    return bSearch(array, mid, high);
  }else if(array[mid] -  array[low] > mid - low){
    return bSearch(array, low, mid);
  }else{
    return -1;
  }
}

int main(void){

  int array [] = {22, 23, 24, 25, 26, 27, 28, 30};
  int upperBound = (sizeof(array)/sizeof(array[0]))-1;
  printf("%d\n", bSearch(array, 0,  upperBound));
  return 0;
}

My questions:

Why does removing the else{} case cause the compiler to give a warning for it?

Can the base case look more elegant?

For code elegance and optimization, can this code be improved?

Why does removing the else{} case cause the compiler to give a warning for it?

The compiler will indeed issue a warning if a value-returning function doesn't return a value for every path.  Ignoring this warning could then cause undefined behavior.

If you intend for bSearch() to return -1 on failure, then you can put just one return -1; at the very end of the function and remove the elses that return -1.


Getting the square of the difference between many points in a NumPy array I have an array with 40000 numbers that are floats or ints. I need to perform some calculation.
To do this I have used nested for loop, but the code is really slow. Can I use something instead of nested the for loop? Is there any other way to reduce the execution time?
When I changed to a list comprehension the execution time reduce slightly.
Why did the list comprehension take less time?
import numpy as np
import time as t

pox1=  np.random.randint(1000, size= 40000)
time = np.arange(40000)

y=np.zeros(len(pox1))
w=np.zeros(len(pox1))

start = t.time()
for num in range (len(time)-1):
    z= [((pox1[i] - pox1[i-num]) ** 2) for i in range(num, len(pox1))]
    k=np.mean(z)
    y[num]=k

  # for i in range(num, len(pox1)):
  #    z.append((pox1[i] - pox1[i-num]) ** 2)

endtime = (t.time()-start)

print(y,endtime)

Python slow, C fast
By having the loops in Python you're basically cutting one of your legs off. It should come as no surprise that the one legged human will lose to the two legged human.
The question we should be asking is how can we slice pox1 to get what we want.

We should split the for loop into two comprehensions so we can see how we'd do it in numpy.
a = [pox1[i]       for i in range(num, len(pox1))]
b = [pox1[i - num] for i in range(num, len(pox1))]
z = (a - b) ** 2

a is very simple we just slice with the values of the range. But b is a bit harder, we can just move the - num into the range and we'll be able to do the same.
a = [pox1[i] for i in range(num, len(pox1))]
b = [pox1[i] for i in range(0, len(pox1) - num)]
z = (a - b) ** 2

Convert it to slices
a = pox1[num : len(pox1)]
b = pox1[0 : len(pox1) - num]
z = (a - b) ** 2

Use some slice sugar
a = pox1[num :]
b = pox1[: len(pox1) - num]
z = (a - b) ** 2

Put back in your code
for num in range(len(time) - 1):
    y[num] = np.mean((pox1[num :] - pox1[: len(pox1) - num]) ** 2)

I changed the size to 4000 and this cut it from 5.04s to 0.07s. To get 40000 changes with the updated code takes 3.30s.
import numpy as np
import time as t

SIZE = 4000
pox1 = np.random.randint(1000, size=SIZE)
time = np.arange(SIZE)

y=np.zeros(len(pox1))
w=np.zeros(len(pox1))

start = t.time()

for num in range(len(time)-1):
    y[num] = np.mean((pox1[num :] - pox1[:len(pox1) - num]) ** 2)

endtime = t.time() - start
print(y, endtime)


List of binary numbers: How many positions have a one and zero I have a list of integers, e.g. i=[1,7,3,1,5] which I first transform to a list of the respective binary representations of length L, e.g. b=["001","111","011","001","101"] with L=3.
Now I want to compute at how many of the L positions in the binary representation there is a 1 as well as a zero 0. In my example the result would be return=2 since there is always a 1 in the third (last) position for these entries. I want to compute this inside a function with a numba decorator.
Currently my code is:
@nb.njit
def count_mixed_bits_v2(lst):
    andnumber = lst[0] & lst[1]
    ornumber = lst[0] | lst[1]
    for i in range(1, len(lst)-1):
        andnumber = andnumber & lst[i+1]
        ornumber = ornumber | lst[i+1]
    xornumber = andnumber ^ ornumber
    result = 0
    while xornumber > 0:
       result += xornumber & 1
       xornumber = xornumber >> 1
    return result

First I take the AND of all numbers, ans also the OR of all numbers, then the XOR of those two results will have a 1 where the condition is fulfilled. In the end I count the number of 1's in the binary representation. My code seems a bit lengthy and I'm wondering if it could be more efficient as well. Thanks for any comment!
Edit: Without the numba decorator the following function works:
def count_mixed_bits(lst):
    xor = reduce(and_, lst) ^ reduce(or_, lst)
    return bin(xor).count("1")

(Credit to trincot)

I only see a few micro optimisations:

Iterate the list instead of a range, so that you don't have to do another lookup with list[i+1]
Use more assignment operators, such as &=, |= and >>=
It is not needed to use lst[1] in andnumber = lst[0] & lst[1]. It can be just andnumber = lst[0]

So:
def count_mixed_bits_v2(lst):
    andnumber = ornumber = lst[0]
    for value in lst:
        andnumber &= value
        ornumber |= value
    xornumber = andnumber ^ ornumber
    result = 0
    while xornumber > 0:
       result += xornumber & 1
       xornumber >>= 1
    return result

This visits the first list value again (in the first iteration), even though it is not necessary. But that probably does not really hurt performance, and keeps the code simple.


Swapping two integer numbers with no temporary variable I tried to swap 2 integer numbers without using an additional variable as a traditional swap.

Is it legal in C++? My VC compiler doesn't complain nor gives any warning about it. If so, how can I improve this script?

#include <iostream>

int main()
{
    int a = 20;
    int b = 66;

    // before swapping
    std::cout << a  << ' ' << b  << '\n';

    // swap
    a ^= b ^= a ^= b;

    // after swapping
    std::cout << a << ' ' << b << '\n';
}

For this code:

int a = 20;
int b = 66;
a ^= b ^= a ^= b;

Assembler output for VC++ 2013:

_b$ = -20                     ; size = 4
_a$ = -8                      ; size = 4

mov   DWORD PTR _a$[ebp], 20          ; 00000014H
mov   DWORD PTR _b$[ebp], 66          ; 00000042H

mov   eax, DWORD PTR _a$[ebp]
xor   eax, DWORD PTR _b$[ebp]
mov   DWORD PTR _a$[ebp], eax

mov   ecx, DWORD PTR _b$[ebp]
xor   ecx, DWORD PTR _a$[ebp]
mov   DWORD PTR _b$[ebp], ecx

mov   edx, DWORD PTR _a$[ebp]
xor   edx, DWORD PTR _b$[ebp]
mov   DWORD PTR _a$[ebp], edx

For this code:

int a = 20;
int b = 66;

int t = a;
a = b;
b = t;

Assembler output for VC++ 2013:

_t$ = -32                     ; size = 4
_b$ = -20                     ; size = 4
_a$ = -8                      ; size = 4

mov   DWORD PTR _a$[ebp], 20          ; 00000014H
mov   DWORD PTR _b$[ebp], 66          ; 00000042H

mov   eax, DWORD PTR _a$[ebp]
mov   DWORD PTR _t$[ebp], eax

mov   eax, DWORD PTR _b$[ebp]
mov   DWORD PTR _a$[ebp], eax

mov   eax, DWORD PTR _t$[ebp]
mov   DWORD PTR _b$[ebp], eax

You make assumptions which may not be true. Why do you believe that
int tmp = a;
a = b;
b = tmp;

actually is compiled down to using an actual variable? It is likely just a register used on the CPU.
Have you inspected it?
Further, why do you assume that:
a ^= b ^= a ^= b;

uses fewer registers than a swap?
Really, what you should do is:
#include <algorithm>
#include <iostream>

int main() {

    int a = 20;
    int b = 66;

    // before swapping
    std::cout << a  << ' ' << b  << '\n';

    // swap
    std::swap(a,b);

    // after swapping
    std::cout << a << ' ' << b << '\n';

}

Which is also a reminder that having the a ^= b ^= a ^= b; 'naked' in your code is not good practice. Something like that should be embedded in a function, not directly in the main method.
Update - assembler output
For the code:
int a = 20;
int b = 66;

int t = a;
a = b;
b = t;

return a;

you get the assembler output:
movl    $20, -12(%rbp)
movl    $66, -8(%rbp)
movl    -12(%rbp), %eax
movl    %eax, -4(%rbp)
movl    -8(%rbp), %eax
movl    %eax, -12(%rbp)
movl    -4(%rbp), %eax
movl    %eax, -8(%rbp)
movl    -12(%rbp), %eax
popq    %rbp

For the code:
int a = 20;
int b = 66;

a ^= b ^= a ^= b;

return a;

you get
movl    $20, -8(%rbp)
movl    $66, -4(%rbp)
movl    -4(%rbp), %eax
xorl    %eax, -8(%rbp)
movl    -8(%rbp), %eax
xorl    %eax, -4(%rbp)
movl    -4(%rbp), %eax
xorl    %eax, -8(%rbp)
movl    -8(%rbp), %eax
popq    %rbp

What does that show?

It shows that both systems run in 12 instructions, including the copy to the stack (%rbp)
that both systems use the single register %eax
both systems use the stack as a temp store for the result (the XOR reuses -8 and -4 offsets in the stack, the tmp uses -12(%rbp)

Net result? Both systems use less than 16 bytes of the stack, they both use 1 register in addition to the stack, and they both have the same number of instructions.
I know which one is more readable....
Of course, with the above code, if I add -O2 to the optimization, I get the assembler:
movl    $66, %eax
ret

which, as you can imagine, is fast.


Place a button after an input and show it's password in plain text I have managed to achieve this action through this:

function togglePasswordVisibility($pw, on, id) {
  $pw.attr('type', on ? 'password' : 'text');
  $('[data-id=' + id + '] > i').toggleClass('fa-eye-slash fa-eye');
 }

 // $("#pass-on").after('

');

 $('[data-id]').on('click', function() {
  var id = $(this).data('id'),
   $pw = $('#' + id);

  togglePasswordVisibility($pw, false, id);

  setTimeout(function() {
   togglePasswordVisibility($pw, true, id);
  }, 800);
 });
<link href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" rel="stylesheet"/>

       <input type="password" id="pass-on" title="<?php echo $user->valid_password_error; ?>" class="form-control" name="txt_upass" placeholder="Enter Password" autocomplete="off" value="" oninvalid="setCustomValidity('<?php echo $user->password_error; ?>')" oninput="setCustomValidity('')" required />
     Password

      <input type="password" id="pass-verify-on" title="<?php echo $user->valid_password_error; ?>" class="form-control" name="txt_upass_ok" placeholder="Retype Password" autocomplete="off" value="" oninvalid="setCustomValidity('<?php echo $user->password_error; ?>')" oninput="setCustomValidity('')" required />
      Retype Password

But I would like to place the password button from within the function using the commented line with the after callback and making it apply the same effect. Right now the button correlates with the input through the association of data-id and id. If I would to place it inside the function I would have to get rid of the data-id and still make it act individually on each input, I just can't figure how. Thank you for your time.

You can first create the i element and add a click event handler to it dynamically while you wrap it inside the div and span and add it to the document.

As you use jQuery, I would suggest using it for creating each individual element, providing it with attributes using jQuery methods.

Here is how you could do it:

function togglePasswordVisibility($pw, on, $eye) {
    $pw.attr('type', on ? 'password' : 'text');
    $eye.toggleClass('fa-eye-slash fa-eye');
}

$("[type=password]").each(function () {
    var $pw = $(this);
    var $eye = $("").addClass("fas fa-eye-slash").click(function () {
        togglePasswordVisibility($pw, false, $eye);
        setTimeout(function() {
            togglePasswordVisibility($pw, true, $eye);
        }, 800);
    });
    $pw.parent().append(
        $("
").addClass("input-group-append").append(
            $("").addClass("password-button password-button-main").append($eye)
        )
    );
});
<link href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" rel="stylesheet"/>

<link href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" rel="stylesheet"/>

    <input type="password" id="pass-on" title="<?php echo $user->valid_password_error; ?>" class="form-control" name="txt_upass" placeholder="Enter Password" autocomplete="off" value="" oninvalid="setCustomValidity('<?php echo $user->password_error; ?>')" oninput="setCustomValidity('')" required />
    Password

    <input type="password" id="pass-verify-on" title="<?php echo $user->valid_password_error; ?>" class="form-control" name="txt_upass_ok" placeholder="Retype Password" autocomplete="off" value="" oninvalid="setCustomValidity('<?php echo $user->password_error; ?>')" oninput="setCustomValidity('')" required />
    Retype Password


Any problems in using hibernate's session.connection() to run native sql In my project, they use Hibernate's session the below mentioned way and then save entity objects with in a transaction.

Session session = HibernateUtil.getCurrentSession();
session.beginTransaction();

Employee employee = new Employee() ;

employee.setName("someName") ;
employee.setEmailId ("someId")
employee.setID (100000) ;

session.saveOrUpdate(employee) ;

session.getTransaction().commit();

Now for few functionality I decided to run native SQL. Below is the way i used to run native sql. I wanted to run the queries with-in a transaction and so i decided to write the code the below way

String query = "select name from master_employee where id=?"

Session session = HibernateUtil.getCurrentSession();
session.beginTransaction();

Connection connection = session.connection();
PreparedStatement psStmt = connection.prepareStatement(query);
psStmt.setInt(1,id) ;

ResultSet resultSet = psStmt.executeQuery();
// here i will take data from this result set
psStmt.close();
resultSet.close() ;

// I am not closing the connection object since am taking it from hibernate session
// rather i commit hibernate's transaction
session.getTransaction().commit();

Is this the right way ?? will the transaction's still be managed ?? taking the connection object from session cannot be managed in to the transaction ????

Please tell if there any problems in using this way ??? thanks

Since Hibernate session provides APIs to run native SQL queries, I would avoid extracting the connection from the session and leave the life cycle management of the Statement, ResultSet used for running native queries, to Hibernate Session itself. There must be some reason why they discourage the use of connection like this, directly and so it is being deprecated.


Gathering tokens from ASP.NET routes This is one of the (many) utilitarian classes I have that I use with my ASP.NET sites.

The basis of this one is to make gathering certain tokens that I use periodically, to make URL's more friendly.

It's pretty simple, and should be quite obvious. I use it to make URL's a little more friendly. (/Id/SomeKey/SomeValue/OtherKey/OtherValue/) It returns Id and creates a dictionary for the remaining pairs of Key and Value.

public class RouteUtilities
{
    public static Dictionary<string, string> GetRouteKeyValuePairs(RouteValueDictionary dataTokens, int startIndex)
    {
        Dictionary<string, string> routeItems = new Dictionary<string, string>();
        List<string> segments = GetRouteSegments(dataTokens);

        for (int i = startIndex; i < segments.Count; i += 2)
        {
            routeItems.Add(segments[i], segments[i + 1]);
        }

        return routeItems;
    }

    public static List<string> GetRouteSegments(RouteValueDictionary dataTokens) => (List<string>)dataTokens["FriendlyUrlSegments"];

    public static bool GetIdAndKeys(RouteValueDictionary dataTokens, out int id, out Dictionary<string, string> keys)
    {
        if (dataTokens.Count > 0)
        {
            Dictionary<string, string> routeItems = GetRouteKeyValuePairs(dataTokens, 1);
            List<string> routeSegments = GetRouteSegments(dataTokens);

            if (routeSegments.Count > 0)
            {
                id = Convert.ToInt32(routeSegments[0]);
                keys = GetRouteKeyValuePairs(dataTokens, 1);
                return true;
            }
        }

        id = 0;
        keys = null;
        return false;
    }
}

In GetIdAndKeys(), you do the default initialization of the out variables just before the default return.  I find it is usually better to initialize these variables at the top of the method because many methods will have multiple early return conditions, as well as a default return value.  Doing the default initialization at the beginning of the method prevents needing to do it in multiple places, and aids in consistency if the specific method does not have this problem.

public static bool GetIdAndKeys(RouteValueDictionary dataTokens, out int id, out Dictionary<string, string> keys)
{
    id = 0;
    keys = null;

    if (dataTokens.Count > 0)
    {
        // re-assign and return
    }

    return false;  // use default values
}


Copy a directory from source to destination I wrote a program that copies a directory from a source to a destination, similar to the cp utility.

For this, I used the essential functions:

open(), write(), mkdir(), readdir().

This is the source code (ignore the main() function, this is not the final form):

#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>

int isDirectory(char*);
int copyFile(char*, char*);
int copyDirectory(char*, char*);

int main(int argc, char **argv) {

  copyDirectory(*(argv + 1), *(argv + 2));

  return 0;
}

int isDirectory(char *path) {
   struct stat statbuf;

   if (stat(path, &statbuf) == -1) {
      return -1;
   }
   else {
      return S_ISDIR(statbuf.st_mode);
   }
}

int copyFile(char *source, char *destination) {
  if((source == NULL) || (destination == NULL)) {
    return -1;
  }

  struct stat statbuf;
  int fdr, fdw, rv;
  char buff[256];

  stat(source, &statbuf);
  if(!S_ISREG(statbuf.st_mode)) {
    return 1;
  }

  fdr = open(source, O_RDONLY);
  fdw = open(destination, O_CREAT|O_WRONLY, S_IREAD|S_IWRITE);

  while((rv = read(fdr, &buff, 128))) {
    write(fdw, &buff, rv);
  }

  return 0;
}

int copyDirectory(char *source, char *destination) {
  if((source == NULL) || (destination == NULL)) {
    return -1;
  }

  struct dirent *entry;
  DIR *dr;
  char sourcePatch[512], destinationPatch[512];

  strcpy(sourcePatch, source);
  strcpy(destinationPatch, destination);
  strcat(destinationPatch, "/");
  strcat(sourcePatch, "/");

  if(!isDirectory(source)) {
    return -1;
  }

  if(opendir(destination) == NULL) {
    mkdir(destination, 0700);
  }

  dr = opendir(source);
  while((entry = readdir(dr))) {
    strcat(sourcePatch, entry->d_name);
    strcat(destinationPatch, entry->d_name); //printf("%s\n", sourcePatch);

    if(isDirectory(sourcePatch) && strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")) {
      copyDirectory(sourcePatch, destinationPatch);
    }
    else {
      copyFile(sourcePatch, destinationPatch);
    }

    *sourcePatch = '\0'; *destinationPatch = '\0';
    strcpy(sourcePatch, source);
    strcpy(destinationPatch, destination);
    strcat(destinationPatch, "/");
    strcat(sourcePatch, "/");
  }

  return 0;
}

Is there a better way for doing this?

Comparing my program, to the GNU cp program, I get this results:

[marius@arch labs]$ gcc -Wall cp.c -g -O3 -o cp
[marius@arch labs]$ time ./cp ~/Desktop/management ~/Desktop/testtest

real    0m0.139s
user    0m0.003s
sys     0m0.133s
[marius@arch labs]$ time cp -r ~/Desktop/management ~/Desktop/testtest2

real    0m0.018s
user    0m0.000s
sys     0m0.017s

And what about this code, can I make some improvements?

Overall, it is good, but I would apply the following changes:

Use const for read only function parameters. All the char* pointers you are passing to the functions are actually only read from, so you should use const char* to document that.
Personally, I'm not a big fan of returning -1, 0, 1, etc. This kind of error codes are popular with UNIX system calls, but I find them rather error prone. It is easy to make a mistake with the return value of a function if you don't carefully read the documentation. If the function has only two possible outcomes, success and failure, then include <stdbool.h> and return true or false. There is no chance of misunderstanding it this way.
A few magic numbers that could be named constants. For example, why did you choose 512 and 256 for buffer sizes? That is not obvious. Named constants would be better for these cases. Also, even though the 0700 constant on mkdir is well known for UNIX users, it might not be so for others less familiar with the architecture. I would also make that a named constant.
I suggest keeping the parameter names on function prototypes. In int copyFile(char*, char*); which one is the source file and which is the dest? Can't tell for sure just by reading the function prototype. Parameter names would make that clear without having to look at the implementation.
I suspect you have too many #includes for such a tiny program. <time.h> is certainly not used here. Make sure to only import the dependencies that your program actually requires.
A few system calls are not being checked for failure, so your program is not as robust as it could be.


Class for Conway's Game of Life in C# I have a class which takes a string representing a grid from Conway's game of life (along with its size, only accepting square grids) and returns the next grid in string format. Can it be improved?

Please note that this purely does the computation, and none of the displaying or editing. Also, it is internal so it will not have detection or prevention for errors.

The code:

public class GameOfLifeComputer
{
    #region Fields

    private int _up;
    private int _down;
    private int _left;
    private int _right;
    private int _currentIndex;
    private int _cellCount;
    private string _outputGrid;

    #endregion

    #region Methods

    /// <summary>
    /// Returns a string for the next grid state.
    /// </summary>
    /// <param name="inputGrid">The current grid state.</param>
    /// <param name="gridSize">Side length of the grid in cells.</param>
    /// <returns>The next grid state.</returns>
    public string CalculateNextGrid(string inputGrid, int gridSize)
    {
        _outputGrid = "";
        _currentIndex = 0;

        foreach (char currentCell in inputGrid)
        {
            _cellCount = CountCells(_currentIndex, inputGrid, gridSize);

            if (currentCell == '1')
            {
                if (_cellCount < 2 || _cellCount > 3)
                {
                    _outputGrid += "0";
                }
                else
                {
                    _outputGrid += "1";
                }
            }
            else
            {
                if (_cellCount == 3)
                {
                    _outputGrid += "1";
                }
                else
                {
                    _outputGrid += "0";
                }
            }
        }

        return _outputGrid;
    }

    /// <summary>
    /// Returns how many adjacent cells are alive.
    /// </summary>
    /// <param name="currentIndex">Position of the cell.</param>
    /// <param name="inputGrid">State of the grid as a string.</param>
    /// <param name="gridSize">Side length of the grid in cells.</param>
    /// <returns></returns>
    private int CountCells(int currentIndex, string inputGrid, int gridSize)
    {
        var count = 0;

        // Define where the adjacent cells are in terms of relative index position.
        _up = -gridSize;
        _down = gridSize;

        // Up and down must cycle to the top or bottom of the grid if the cell is on the edge.
        if (currentIndex < gridSize)
        {
            _up = inputGrid.Length - gridSize;
        }
        else if (currentIndex > inputGrid.Length - gridSize)
        {
            _down = gridSize - inputGrid.Length;
        }

        // Left and right must cycle to the opposite side on the same row if the cell is on the edge.
        if (currentIndex % gridSize == 0)
        {
            _left = gridSize - 1;
            _right = 1;
        }
        else
        {
            _left = -1;
            _right = 1 - gridSize;
        }

        // Count how many adjacent cells are alive.
        if (inputGrid[currentIndex + _up    + _right]   == '1') { count += 1; }
        if (inputGrid[currentIndex + _up            ]   == '1') { count += 1; }
        if (inputGrid[currentIndex + _up    + _left ]   == '1') { count += 1; }
        if (inputGrid[currentIndex + _left          ]   == '1') { count += 1; }
        if (inputGrid[currentIndex + _down  + _left ]   == '1') { count += 1; }
        if (inputGrid[currentIndex + _down          ]   == '1') { count += 1; }
        if (inputGrid[currentIndex + _down  + _right]   == '1') { count += 1; }
        if (inputGrid[currentIndex + _right         ]   == '1') { count += 1; }

        return count;
    }

    #endregion
}

The first bad thing you do is a lot of string concatenations like

_outputGrid += "1";

which cause many memory reallocations. Since during one game grid size is a constant number you can save it at the constructor, and then just create array for result and set its elements by index.

_outputGrid = new char[_gridSize * _gridSize];
...
_outputGrid[currentIndex] = '0';

At the end of the CalculateNextGrid method convert this array to string and return it:

return new string(_outputGrid);

I suggest to use array of bool's as internal storage for result instead of a string. In my opinion true and false represent the state of a cell (alive or dead) more clearly. You can even use array of enum values like:

public enum CellState
{
    Alive,
    Dead
}

_outputGrid[currentIndex] = CellState.Alive;

You can go further and create method like:

private void ReviveCell(int index) => _outputGrid[index] = CellState.Alive;

It would be better to extract magic conditions to some methods like:

private static bool ShouldBeDeadByUnderpopulation(int count) => count < 2;

private static bool ShouldBeDeadByOverpopulation(int count) => count > 3;

and so on.

And instead of condition like

if (inputGrid[currentIndex + _up    + _right]   == '1')

use methods too

if (IsAlive(value))

With all these changes your code will look like a story where all is clear :)

if (IsAlive(currentCell))
{
    if (ShouldBeDeadByUnderpopulation(_cellCount ) ||
        ShouldBeDeadByOverpopulation(_cellCount))
    {
        KillCell(currentIndex);
    }
    else
    {
        // Note that this code is redundant since cell is alive at the moment
        ReviveCell(currentIndex);
    }
}


Loading content from external HTML files I'm using jQuery's .get() method to load content from external HTML files into my main index file. I created 25 different functions, function videoLoad1(), function videoLoad2() etc, for the 25 videos that I'm loading separately when its corresponding link is clicked. The content that is being swapped out in my HTML index file is the video src and video details. I'm new to jQuery and have been trying to find a more practical way of writing the code.

HTML - links for the onclick function:

cars.com: be honest

cars.com: tag

HTML for video inclusion:

External HTML file that is being loaded via $.get() (file name is cars_bh.html):

Video

Now Playing

cars.com: be honest

<video id="kz-player" width="100%" height="100%" controls preload>
    <source src="vid/CarscomBeHonest.mp4"  type='video/mp4;'>
    <source src="vid/CarscomBeHonest.webmhd.webm" type='video/webm;'>
    <source src="vid/CarscomBeHonest.oggtheora.ogv"  type='video/ogg;'>
</video>

jQuery function:

function videoLoad2() {

$("a#cars_hb").click(function(e) {
e.preventDefault();
e.stopPropagation();
$.get('cars_hb.html', function( data ) {
  $('#kz-video').html( data );
  });
});

//close overlay/hide content
$('.close').click(function (e) {
e.stopPropagation();
$('#kz-player')[0].pause();
$('#kz-video').hide();
$('.close').fadeOut(800);
$('#video_overlay').fadeOut(800);
});
}

When you say

  I created 25 different functions for the 25 videos that I'm loading separately when its corresponding link is clicked.

Does that mean you have multiple function videoLoad#() { ?

I think this will help:

$("div#movie_list a").click(function (e) {
    e.preventDefault();
    e.stopPropagation();
    id = e.target.id;
    loadVideo(id);
});

function loadVideo(id) {
    file = id + ".html";
    $.get(file, function (data) {
        $('#kz-video').html(data);
    });
});


Ajax for RESTful web service I wrote an Ajax function, which should be used in a framework to interact with a webserver.

The (self-written) Java-Webserver can be used to fetch files and folder-contents, create files and folders, and delete them.

/**
 * Performs an Ajax-Request
 * @param  {string}   method            HTTP-Method
 * @param  {string}   url
 * @param  {string}   postData          optional; HTTP-body
 * @param  {string}   contentType       optional; default: "multipart/form-data"
 * @param  {object}   authentication    optional; loginCredentials for basicAuth; {"user": "userName", "password": "password"}
 * @param  {function} callback          optional; called, if the operation succeeded;
 *                                              Parameter: (string) response-body
 * @param  {function} errorCallback     optional; called, if the operation failed;
 *                                              Parameter: 1 = (number) statusCode; (==0, if the given parameters were invalid)
 *                                                         2 = (string) statusText; (or errorMessage, if statusCode == 0)
 */
function ajax(method, url, postData, contentType, authentication, callback, errorCallback) {
    if(typeof(errorCallback) != 'function'){
        errorCallback = function(){};                           //Dummy-function to reduce code
    }

    //Validate the input parameters
        if(["GET", "HEAD", "POST", "PUT", "DELETE"].indexOf(method) < 0){
            return errorCallback(0, "Invalid method");
        }
        if(postData && ["HEAD", "POST"].indexOf(method) < 0){          //Other methods don't support postData
            return errorCallback(0, "Invalid method for postData");
        }
    //Get a cross-browser XML-HTTP-Object:
        var req = createXMLHTTPObject();
        if (!req) {
            return errorCallback(0, "Ajax is not supported");   //Oops! Something bad happened
        }
        req.open(method, url, true);    //async = true
    //Manipulate header-fields:
        //req.setRequestHeader('User-Agent','XMLHTTP/1.0');
        if(postData){
            if(contentType){
                req.setRequestHeader('Content-type', contentType);
            }else{
                req.setRequestHeader('Content-type', 'multipart/form-data');
            }
            //key-value pairs with binary data or big payloads:
            //    multipart/form-data
            //key-value pairs with few non-alphanumerical symbols:
            //    application/x-www-form-urlencoded
            //binary data:
            //    application/octet-stream
        }
        if(authentication){
            req.setRequestHeader("Authorization", "Basic " + btoa(authentication.user + ":" + authentication.password));    //btoa() = encode string to base64
        }
        req.overrideMimeType("text/plain; charset=utf-8");      //The browser must not execute javascript-files, if they're loaded with this method
    //Prepare execution:
        req.onreadystatechange = function () {
            if (req.readyState != 4) {
                return;
            }
            if (req.status != 200 && req.status != 304) {
                return errorCallback(req.status, req.statusText);
            }
            if(typeof(callback) == 'function'){
                callback(req.response);
            }
        }
        if (req.readyState == 4){
            return;
        }
        req.send(postData);
}

The createXMLHTTPObject()-Method returns either an XMLHttpRequest-Object, or an ActiveXObject-Object to reach cross-browser compatibility.

The whole function should be cross-browser compatible, but doesn't have to support older browsers, as the Framework itself depends upon HTML5.

The code is based on this example

As I'm not 100% familiar with the HTTP-Protocol, I have the following questions:

Did I forget an important part of HTTP/RESTful services that might be needed in a framework?
Can there be efficiency-improvements?
Are there any other bugs/inconsistencies/possible improvements?

Support for JSONP and CORS is a must for any modern web framework.

APIwise, I think you'd want to reduce the arguments to your method to a single object, similar to how jQuery does it. It gives an opportunity to give defaults, and simplifies it from the API perspective.  As it stands, it will be painful to use, in that you need to remember a lot of arguments.  A simple GET ajax with your's:

ajax('GET', 'index.html', null, null null function(evt) {
    console.log("Success!")
})

vs jQUery or similar:

ajax({method:'GET', url:'index.html'}, success:function(evt) {
    console.log("Success!")
})


Convert a Binary Tree to Doubly Linked List Challenge

Link on geeksforgeeks: Binary Tree to DLL

  Given a Binary Tree (BT), convert it to a Doubly Linked List(DLL)
  In-Place. The left and right pointers in nodes are to be used as
  previous and next pointers respectively in converted DLL. The order of
  nodes in DLL must be same as Inorder of the given Binary Tree. The
  first node of Inorder traversal (left most node in BT) must be head
  node of the DLL.

  Your Task:

  You don't have to take input. Complete the function bToDLL() that
  takes node and head as parameter. The driver code prints the DLL both
  ways.

The following code is given. Class Node may not be changed. Method bToDLL must be implemented, but its signature may not be changed.

class Node
{
    Node left, right;
    int data;

    Node(int d)
    {
        data = d;
        left = right = null;
    }

}

// This function should convert a given Binary tree to Doubly Linked List
class GfG
{
    Node head;
    Node bToDLL(Node root)
    {
        // TODO .. convert tree and store the result as head
    }
}

Solution

As pointed out in the comments, a solution is required to be in-place. This is what I came up with. Any type of feedback is invited.

class GfG
{
    Node head;
    Node bToDLL(Node root)
    {
        head = first(reduce(root));
        return head;
    }

    void append(final Node source, final Node node) {
        source.right = node;
        node.left = source;
    }

    Node reduce(final Node node) {
        if (node == null) return node;
        Node prev = reduce(node.left);
        Node next = reduce(node.right);
        if (prev != null) append(last(prev), node);
        if (next != null) append(node, first(next));
        return node;
    }

    Node first(Node node) {
        while (node.left != null) node = node.left;
        return node;
    }

    Node last(Node node) {
        while (node.right != null) node = node.right;
        return node;
    }
}

Algorithm: because of the recursion, it still doesn't qualify as in-place. Consider a variation on a Morris traversal theme.

The calls to first() and last() are detrimental to the performance. Meanwhile, the callee already computed them. Consider returning a (fake) node pointing the beginning and the end of the flattened subtree, along the lines

    left = flatten(root.left);
    right = flatten(root.right);

    left.right.next = root;
    root.left = left.right;

    right.left.left = root;
    root.right = right.left;

    return Node(left.left, right.right);

There is only \$O(h)\$ Nodes to exist at any given moment, so the space complexity is not compromised.


If Elif replacement for expandability I am having the following problem. My code works, but it seems like it can improve a lot. I am fairly new to python and can't seem to get this block any better. The logic behind it goes like this.

I have a dictionary that acts like a tree, it looks like this:

SampleTree[10000000] = ['ACC','ARRAY']
SampleTree[10010000] = ['ACC_ABO','ABO']
SampleTree[10020000] = ['ACC_CHILDLOCK','OPTION']
SampleTree[10030000] = ['ACC_RECO','ARRAY']
SampleTree[10030100] = ['ACC_RECO_ACTIVATE']
SampleTree[10030200] = ['ACC_RECO_ACTIVATE']
SampleTree[10030201] = ['ACC_RECO_ACTIVATE']
SampleTree[10030202] = ['ACC_RECO_ACTIVATE']
SampleTree[10040000] = ['ACC_ADDUSER','OPTION']
SampleTree[10050000] = ['ACC_SETTINGS','ARRAY']

Where the dict key acts like a unique key, which I use to recognize in my program. I use a for loop to iterate over the full dict like this:

KEYLIST = SampleTree.keys()
KEYLIST.sort()

for KEY in KEYLIST :
    if (KEY % 1000000) == 0 :
        BACKS = 0
    elif (KEY % 1000000) == 10000 and (LAST % 1000000) == 0 :
        BACKS = 1
    elif (KEY % 10000) == 0 and (LAST % 1000000) == 10000 :
        BACKS = 2
    elif (KEY % 10000) == 100 and (LAST % 10000) == 0 :
        BACKS = 1
    elif (KEY % 10000) == 0 and (LAST % 10000) == 0 :
        BACKS = 2
    elif (KEY % 10000) == 0 and ((LAST % 10000) % 100) == 0 :
        BACKS = 3
    elif (KEY % 10000) == 0 and (LAST % 100) != 0 :
        BACKS = 4
    elif (KEY % 100) == 1 and ((LAST % 10000) % 100) == 0 :
        BACKS = 1
    elif (KEY % 100) == 0 and (LAST % 100) == 0 :
        BACKS = 2
    elif (KEY % 100) == 0 and (LAST % 100) != 0 :
        BACKS = 3
    elif (KEY % 100) != 0 and (LAST % 100) != 0 :
        BACKS = 2

    LAST=KEY

As you can see in the code, it uses a bunch of if elif tests to see in which branch the Key currently is. And accordingly it sets the BACKS variable how it should be. After this code there are a bunch of operation based on the KEY value and on the BACKS variable.

The BACKS variable depends on the transition of each KEY. Some examples:

from : 10 00 00 00 00 to : 10 01 00 00 00 you need to go BACK 1 time

from : 10 01 00 00 00 to : 10 01 01 00 00 you need to go BACK 1 time

from : 10 01 01 00 00 to : 10 01 02 00 00 you need to go BACK 2 times

from : 10 01 02 00 00 to : 10 02 00 00 00 you need to go BACK 3 times

So what I am trying to say is that there doesn't seem to be a particular logic behind all this. Can you please help me?

My whole problem with the code is that it is so ugly and look like it can improve a lot. But I just can't see a better way. Another big issue is expansion. If I for example add another layer to the tree (add two more zeros to the KEY), the whole logic falls apart.

You've got tree paths encoded as integers such as: 10030201. Your code would much easier to write if instead they tuples like: (10, 03, 02, 01). If at all possible, you should change your dictionary to always use tuples. If not possible, have a function to convert the integers to tuples when you need them. If you do that, you'll find its fairly easy to devise a general implementation of your BACKS logic.

On the stylistic front: We typically use ALL_CAPS only for constants. We use lowercase_with_underscores for local variables which all of your stuff appears to be.

My solution:

def calculate_backs(source, destination):
    """
    Return how many "resets" are required to transform source into destination
    """

    def as_tuple(id_number):
        """
        Converts the id_number into a path sequence:
        10010200 becomes (10, 1, 2)
        """
        pieces = []
        while id_number:
          pieces.append(id_number % 100)
          id_number /= 100
        pieces.reverse()

        # position 0 denotes a root node in the tree, so remove any trailing
        # zeros.
        while pieces[-1] == 0:
          pieces.pop()
        return tuple(pieces)

    def calculate(source, destination):
        if source and destination and source[0] == destination[0]:
            # If both source and destination agree for the first piece of the
            # path, drop that piece and determine the number of resets required
            # for the rest.
            return calculate(source[1:], destination[1:])
        else:
            # If the path disagrees on the first piece, we have to reset
            # everything remaining in both paths.
            return len(source) + len(destination)

    return calculate(as_tuple(source), as_tuple(destination))


Write a lock free singleton factory using generics in c# I am writing a generic CacheProvider factory which contains a concurrent dictionary to keep the "Named" cache objects (a custom implementation of memory cache based on concurrent dictionary) to be shared among different state machines/pipelines.

I found this article and came up with below implementation

public class CacheProvider<TKey, TType>
{
    private Eventing.Tracer tracer;
    public delegate TType CreateObjectDelegate();
    private readonly ConcurrentDictionary<TKey, CreateObjectDelegate> FactoryMap;
    private ConcurrentDictionary<TKey, TType> cachePool;
    private static object _lock = new object();

    /// <summary>
    /// This class is a singleton wrapper on custom memory cache class to be shared with different state machines
    /// </summary>
    /// <param name="tracer"></param>
    public CacheProvider(Eventing.Tracer tracer)
    {
        Contract.Requires<ArgumentException>(tracer != null, "tracer cannot be null");
        this.tracer = tracer;
        cachePool = new ConcurrentDictionary<TKey, TType>();
        FactoryMap = new ConcurrentDictionary<TKey, CreateObjectDelegate>();
    }

    public void Register(TKey key, CreateObjectDelegate ctor)
    {
     // This method registers a delegate to the method used to create the
       object so that it can be created later.
       //
      FactoryMap.TryAdd(key, ctor);
    }

    public TType CreateOrGetMemoryCache(TKey key)
    {
            if (cachePool.TryGetValue(key, out TType memoryCache))
            {
                tracer.Trace(SplitterTraceCodes.CacheProvider, $"MemoryCache with cache key: {key} already created.");
                return memoryCache;
            }
            else
            {
                tracer.Trace(SplitterTraceCodes.CacheProvider, $"MemoryCache with cache key: {key} not found.");

                // Create a new one
                CreateObjectDelegate constructor = null;
                if (FactoryMap.TryGetValue(key, out constructor))
                {
                    cachePool.TryAdd(key,constructor());
                    cachePool.TryGetValue(key, out TType memoryCache);
                    return memoryCache;
                }

                throw new ArgumentException("Error in Creating MemoryCache. No Type registered for this Cache Key");
            }
    }
}

TType is as follows:

MyBase
{
  string BaseProperty1;
}
MyChild1 : MyBase
{
  string ChildProperty1;
}
MyChild2 : MyBase
{
 string ChildProperty2;
}

I am looking to setup this at service startup as follows:

CacheProvider<string, object> cacheProvider= new CacheProvider<string, object>();
CacheProvider<string, object>.CreateObjectDelegate createCacheForMyChild1 = new CacheProvider<string, object>.CreateObjectDelegate(CustomConcurrentCache<string,MyChild1>());
CacheProvider<string, object>.CreateObjectDelegate createCacheForMyChild2 = new CacheProvider<string, object>.CreateObjectDelegate(CustomConcurrentCache<string,MyChild2>());
cacheProvider.Register("MyChild1Cache", createCacheForMyChild1 );
cacheProvider.Register("MyChild2Cache", createCacheForMyChild2 );

and use it wherever I need the named cache instance:

CustomConcurrentCache<string,MyChild1> cache =  (MyChild1)cacheProvider.CreateOrGetMemoryCache<object>("MyChild1Cache");

Can this be done without using the delegates? Is there any other better way to achieve this?

A delegate can be seen as method signature contract. You can define what sort of methods are you willing to work with. For example

The method should accept type x, type y and type z as parameters
and should return with type w

Any method, which satisfy this contract, is a valid method from the delegate perspective.

public delegate TType CreateObjectDelegate();

This delegate says that any method that has no parameter and will return with TType is a good one.

You can describe the same by using the Func class:

Func<TType> CreateObjectDelegate

Whenever you want to call the Register method you just have to pass a method which satisfies this contract. For example (let's suppose TType is int):

cacheProvider.Register("MyChild1Cache", () => 0);

Func<int> alternative1 = () => 1;
cacheProvider.Register("MyChild2Cache", alternative1.Invoke);

Func<int, Func<int>> alternative2 = i => () => i;
cacheProvider.Register("MyChild3Cache", alternative2.Invoke(2).Invoke);


Badger - The tumbleweed detector I have written a small program to query the Stack Exchange API, specifically for badges. The main idea of the program is to return the new list of Tumbleweed posts on Stack Overflow, every 10 minutes. The motivation for this bot was the Weed Eater hat.

The program queries the API every 10 mins. If there are new tumbleweed posts, Then it'll print the number of new posts with a link to the tumbleweed page. If there are no new ones, It waits for another 10 minutes.

The code for the same is

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import java.io.IOException;
import java.time.Instant;

public class RunBadger {

    private static final String apiKey = "my api key";
    public static Instant previousBadgeTimestamp = Instant.now();
    public static void main(String[] args) {
        try {
            while (true) {
                JsonArray weeds = getBadges().get("items").getAsJsonArray();
                int numberOfWeeds = weeds.size();
                if(numberOfWeeds!=0) {
                    System.out.println("[ [Badger](https://www.youtube.com/watch?v=dQw4w9WgXcQ) ] " + numberOfWeeds + " new [Tumbleweed posts](//stackoverflow.com/help/badges/63/tumbleweed)");
                }
                previousBadgeTimestamp = Instant.now();
                Thread.sleep(600000);
            }
        }
        catch (Exception e){
            e.printStackTrace();
        }

    }
    public static JsonObject getBadges() throws IOException{
        String badgeIdUrl = "https://api.stackexchange.com/2.2/badges/63/recipients";
        JsonObject badgeJson = get(badgeIdUrl,"site","stackoverflow","pagesize","100","fromdate",String.valueOf(previousBadgeTimestamp.minusSeconds(1).getEpochSecond()),"key",apiKey);
        return badgeJson;
    }
    public static JsonObject get(String url, String... data) throws IOException {
        Connection.Response response = Jsoup.connect(url).data(data).method(Connection.Method.GET).ignoreContentType(true).ignoreHttpErrors(true).execute();
        String json = response.body();
        if (response.statusCode() != 200) {
            throw new IOException("HTTP " + response.statusCode() + " fetching URL " + (url) + ". Body is: " + response.body());
        }
        JsonObject root = new JsonParser().parse(json).getAsJsonObject();
        return root;
    }
}

I'd like to have an improved version of it. Particularly regarding the while(true) code block.

Conceptually, when you want to perform an action at a fixed rate, you want to use an ScheduledExecutorService. This class is designed to handle the use-case of running code at a fixed rate, possibly with an initial delay. The advantage is that you can schedule more than one task, and it also lets you handle all the tasks, eventually cancelling some of them. It's not so much that the while(true) is an issue per se, but doing all those operations with that approach would be a lot more complicated. Furthermore, it wouldn't really let you perform other unrelated actions at the same time as the code getting the badges. Additionally, don't use the Timer class, and always prefer a ScheduleExecutorService, when you can use one.

Rewriting the code to use the service would actually make it more clear. First of all, you want a method that represents the action to perform:

private static void printBadges() {
    JsonArray weeds;
    try {
        weeds = getBadges().get("items").getAsJsonArray();
    } catch (IOException e) {
        e.printStackTrace(); // this should be replaced with a real logger
        return;
    }
    int numberOfWeeds = weeds.size();
    if(numberOfWeeds!=0) {
        System.out.println("[ [Badger](https://www.youtube.com/watch?v=dQw4w9WgXcQ) ] " + numberOfWeeds + " new [Tumbleweed posts](//stackoverflow.com/help/badges/63/tumbleweed)");
    }
    previousBadgeTimestamp = Instant.now();
}

With that method in place, scheduling the task is as easy as

ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
executorService.scheduleAtFixedRate(RunBadger::printBadges, 0, 10, TimeUnit.MINUTES);

This documents clearly what is happening, mainly that something scheduled at a fixed rate, and the delay is obvious: 10 minutes. The second parameter is a potential initial delay before performing the action the first time, which I set to 0 here.

A note about the exception handling. The current code catches the exception outside of the while loop, which means that any exception will stop the program from running; generally, this isn't want you want. An exception from getting the badges could occurs for multiple reasons and it shouldn't stop the rest of the code from running (although in this case, there is no other part of the code, but imagine this in a more complex scenario, with other tasks running concurrently). The scheduledAtFixedRate method will suppress the execution of future tasks, thereby cancelling the schedule, if there is an exception thrown by the task: this means that if an exception is thrown, nothing will get executed anymore. Catching and handling the exceptions inside the task, like done in the example above, lets you manage this.

The question of what should happen after scheduling the task remains. If you just want to run it for fun until you kill the process manually, you can just keep this in the main method:

public static void main(String[] args) {
    ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
    executorService.scheduleAtFixedRate(Main::printBadges, 0, 1, TimeUnit.SECONDS);
}

The executor service won't be garbage collected and the action will run every 10 minutes, until you kill the JVM yourself. A cleaner approach would be to invoke executorService.shutdown(); when you want the process to stop, as it will correctly stop the executor, and let the main thread finish properly. (Note that it won't block until the tasks are finished; awaitTermination can be used for that). You just need to find the stopping condition, like a specific exception thrown in the task. It could be added as a runtime hook as well, but note that those hooks don't run when killing with SIGKILL.

Other thoughts:

JsonObject root = new JsonParser().parse(json).getAsJsonObject();
return root;

You don't need to store this in a temporary variable just before returning. Returning directly is just as clear, and doesn't introduce a variable.

return new JsonParser().parse(json).getAsJsonObject();

public static JsonObject getBadges() throws IOException

This may tie a bit the code fetching the badges to the code printing the result. In fact, you're only interested in the number of new items, so make that method return the count directly:

public static int getBadgesCount() throws IOException

This way, the rest of the code is hidden from the internal JSON representation of what the API returns.

public static Instant previousBadgeTimestamp

That's very good use of Instant: it represents a point in the timeline in UTC and isn't tied to any specific timezone. When you want to model at what time a system event took place, Instant is the perfect class.

private static final String apiKey = "my api key";

If the API key deserves its constant, why not all the other hard-coded Strings like badgeIdUrl or the rick-rolling very interesting link badger links to? You could even make all of those properties dynamic, by reading them from a file.

if(numberOfWeeds!=0) {

Since this number will never be negative, it is clearer to write

if(numberOfWeeds > 0) {

as it doesn't let the reader wonder whether accepting a negative value was intended or not.

Final point: keep in mind that the StackExchange API has a rate limit in place, and if the API returns a backoff, you have to wait that amount of seconds before making another call.


NullPointer Exception handling in the code import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

public class FileOperations<T> implements Operations<T> {

    private ObjectMapper objectMapper;
    //this is going to come from the property file.
    private String filePath = "C:\\Users\\phyadavi\\workspace\\ConfigurationService\\src\\main\\resources\\seed.txt" ;
    private File file;

    public FileOperations() {
        this.objectMapper = new ObjectMapper();
        this.file = new File(filePath);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.getMessage();
            }
        }
    }

    private void saveImpl(Object serializableObject) throws IOException {
        String jsonData = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(serializableObject);
        BufferedWriter br = new BufferedWriter(new FileWriter(file));
        br.write(jsonData);
        br.close();
    }

    @Override
    public void save(List<T> serializableObjects) throws IOException {
        saveImpl(serializableObjects);
    }

    @Override
    public void save(T serializableObject) throws IOException {
        saveImpl(serializableObject);
    }

    @Override
    public List<T> get() throws IOException {
        List<T> list = objectMapper.readValue(file, new TypeReference<List<T>>() {
        });

        return list;
    }

}

import java.io.IOException;
import java.util.List;

import com.cisco.daas.configservice.entities.User;
import com.cisco.daas.configservice.utilities.FileOperations;
import com.cisco.daas.configservice.utilities.Operations;

public class Data {

    private static final Operations<User> operations = new FileOperations<User>();

    private static List<User> users;

    public static List<User> getUsers() {
        try {
            users = operations.get();
        } catch (IOException e) {
            e.getMessage();
        }
        return users;
    }
}

The above code reads json data from a text file using the Jacksons ObjectMapper API.

The public List<T> get() method reads the data from the files and converts the data into a list of objects of type T.

The public void save(T serializableObject) and public void save(List<T> serializableObjects) both saves the json data to the file.

The Data creates a static list objects which is meant for performing operations in-memory.

Now, I would like someone to review the exception handling in the code. I need some idea on how to handle exceptions like NullPointer and FileNotFound exceptions in the above code.

The general rule is:
never return null except it is a valid member of the result set.
Usually it is not as in your example.
Instead of returning a null after the IOException occurred you should either return an empty List or throw your own (Runtime-) Exception. Whch option to choose depends if havin no users is an expected use case whithin your application (e.g. resulting in an empty list presented to the user)  or if it is an error condition with the user gets presented as an explicit error message.
Your class Data should look like this:
public class Data {

    private static final Operations<User> operations = new FileOperations<User>();

    private static final List<User> users = new ArrList<>();

    public static List<User> getUsers() {
        users.clear();
        try {
            users.addAll(operations.get());
        } catch (IOException e) {
            e.getMessage();
        }
        return users;
    }
}

BTW:
avoid the use of static
declaring your methods static has a lot of Problems.
It makes your code inflexible, had to change and to maintain. It also throws away polymorphism which is the most powerful feature of OOP.
If you refure to work with objects you should ask yourself why you chose an OO language...


Finding the maximum number of id's among all weeks available this is a really short code, but I'm trying to improve my Python skills. I've got a DataFrame, with a week variable, for every week I want to know the number of different id's in that week, and then get the maximum number of id's among all weeks. I achieve that doing this:

listt=[]
for i in range(df.week.max()):
    listt.append(df[df.week==i].id.nunique())
np.array(listt).max()

It feels to me that this can probably be done in a one-liner. How would you do it? Thanks

Instead of iterating through each possible week and filtering your dataframe, you should use df.groupby and work from there using convenience methods on the results:

df.groupby('week').id.nunique().max()


Unique Paths II, dynamic programming problem, O(n^2) time, O(n^2) space This is based on this leetcode question.  I get the correct answer, but I know it could be much, much cleaner.  I also know there is supposed to be an O(n) space solution, but I'm not sure how to implement cleanly.
It is a dynamic programming problem, I tried to add some helpful code comments, I know it's pretty messy so thank you for your review.
class Solution:
    def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:
        #initialize memoization array
        memo = [[-1 for i in range(len(obstacleGrid[0]))] for j in range(len(obstacleGrid))]

        #add known values before any calculation (last row and last column)
        memo[len(memo)-1][len(memo[0])-1] = 1
        for i in range(len(memo[0])-2,-1,-1):
            if obstacleGrid[len(memo)-1][i] != 1:
                memo[len(memo)-1][i] = memo[len(memo)-1][i+1]
            else:
                memo[len(memo)-1][i] = 0

        for j in range(len(memo)-2,-1,-1):
            if obstacleGrid[j][len(memo[0])-1] != 1:
                memo[j][len(memo[0])-1] = memo[j+1][len(memo[0])-1]
            else:
                memo[j][len(memo[0])-1] = 0

        if obstacleGrid[len(memo)-1][len(memo[0])-1] == 1:
            return 0

        #does calculations
        def helper(row,col):
            nonlocal memo
            if obstacleGrid[row][col] == 1:
                return 0
            if memo[row][col] == -1:
                memo[row][col] = helper(row+1,col) + helper(row,col+1)
            return memo[row][col]

        return helper(0,0)

Nice solution, few suggestions:

Duplicated code: the functions len(memo) and len(memo[0]) are called several times. When working with a matrix is common to call m the number of rows (len(memo)) and n the number of columns (len(memo[0])). This can help to reduce the duplicated code. As @Manuel mentioned in the comments, m and n are also defined in the problem description.

Last element of a list: instead of memo[len(memo)-1] you can use memo[-1].

Input validation: this input validation:
if obstacleGrid[len(memo)-1][len(memo[0])-1] == 1:
    return 0

is done too late in the code, after the memo matrix is fully built. Better to move it up at the beginning of the function. BTW, with the previous suggestion, it can be shortened to:
if obstacleGrid[-1][-1] == 1:
    return 0

Naming: the row is called row in the helper function and j in the rest of the code. Same for the column. Use the same name to be consistent.

Throwaway variables: in the initialization of memo:
memo = [[-1 for i in range(len(obstacleGrid[0]))] for j in range(len(obstacleGrid))]

the variables i and j are not used. They can be replaced with _.

Type hints: the helper function is missing the type hints.

LRU cache: there is a handy annotation that does the memoization automatically, @lru_cache. Or the unbounded @cache in Python 3.9.

An example using @lru_cache:
def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:
    m = len(obstacleGrid)
    n = len(obstacleGrid[0])

    if obstacleGrid[-1][-1] == 1:
        return 0

    @lru_cache(maxsize=None)
    def helper(row: int, col: int) -> int:
        if row == m - 1 and col == n - 1:
            return 1
        if row < m and col < n:
            if obstacleGrid[row][col] == 1:
                return 0
            return helper(row + 1, col) + helper(row, col + 1)
        return 0

    return helper(0, 0)

For dynamic programming solutions, you can have a look at the "Solution" section or "Discussion" section using tags python and dynamic programming.


Getting last or return a default value in a Seq[Either[A, B]] val y: Seq[Either[String, Int]]

case class Response(value: String)

val notFound = Response("Not Found")
val found    = Response("Found")

val response: Response = y.lastOption
    .map(_.fold(_ => notFound, _ => found))
    .getOrElse(notFound)

I am getting a list of Eithers. I want to get the last Either in the list. I then want to get Rightvalue. If the list is empty or the Either is  Left, I want to return a default value. The code does this but can this be done more elegantly and clearer in Scala with perhaps cats? I don't use scalaz.

Instead of map() - fold() - getOrElse(), you could just use two fold() calls.

val response = Response(y.lastOption
                         .fold("Not Found")(_.fold(_=>"Not Found", _=>"Found")))

Or you could turn it into a single Option with just one fold().

val response = Response(y.lastOption
                         .flatMap(_.toOption)
                         .fold("Not Found")(_ => "Found"))