texts: Memoized solution to Count number of ways to climb n steps

texts:
Problem statement : A Child is climbing up n steps with either 1 , 2, 3 hops ..how many ways can the child climb up the stairs?

source: cracking the coding interview book.

I have two solutions the brute force and the memoized one.

Wanted to know how to improve my solution further, any issues you see with this code?

description of the code:
Brute force: basically count in a recursive manner, as there are 3 ways to reach the nth step so we count number of ways to reach each of n-1 , n-2 and n-3 steps and then sum them up to find the number of ways of reaching the nth step.

memoized solution:

we are now caching the solutions in a hashmap to reuse the solutions.

The Code:

public class CountNWaysforSteps {

    // running time O(3 ^ n) inefficient solution
    public static int numberOfWays(int n){

        if(n < 0 ) return 0;
        if(n == 0 ) return 1;

        return numberOfWays(n-1) + numberOfWays(n-2) + numberOfWays(n-3);
    }
    //memoized solution running time O(N)
    public static int numberOfWaysMemoized(int n, Map<Integer,Integer> cache){

        if(n < 0 ) return 0;
        if(n == 0 ) return 1;

        if(cache.containsKey(n)){
            return cache.get(n);
        }

        int calculatedNumberOFWays = numberOfWaysMemoized(n-1, cache) + numberOfWaysMemoized(n-2, cache) + numberOfWaysMemoized(n-3, cache);
        cache.put(n,calculatedNumberOFWays);
        return calculatedNumberOFWays;
    }

    public static void main(String...args){
        System.out.println(numberOfWays(5));
        System.out.println(numberOfWaysMemoized(5, new HashMap<>()));
    }
}


texts:
Today was my first day working with JavaScript and I tried to implement some Linq-like (C#) functions as extension methods.

I'm pretty new to JavaScript and wanted to ask you for checking my functions and tell me, whether they're good and really usable in productive applications. Or maybe, what I could do better. I don't know whether there is already a library for this; it's primarily for learning purposes.

Array.prototype.where = function * (predicate){
    for(var i=0; i < this.length; i++){
        if (predicate(this[i])) yield this[i];
    }
}

Array.prototype.all = function (predicate){
    for(var i=0; i < this.length; i++){
        if (!predicate(this[i])) return false;
    }
    return true;
}

Array.prototype.any = function (predicate){
    if (predicate == null) return this.length >0;
    for(var i=0; i < this.length; i++){
        if (predicate(this[i])) return true;
    }
    return false;
}

Array.prototype.select = function * (predicate){
    for(var i=0; i < this.length; i++){
        yield predicate(this[i], i);
    }
}

Array.prototype.groupby = function (predicate){
    var result = {};
    for(var i=0; i < this.length; i++){
        var key = predicate(this[i]);
        if (result[key] == null)
            result[key] = Array();
        result[key].push(this[i]);
    }
    return result;
}

Array.prototype.foreach = function (predicate){
    for(var i=0; i < this.length; i++){
        predicate(this[i]);
    }
}

Array.prototype.first_or_default = function (predicate){
    if (predicate == null){
        return this.length > 0 ? this[0] : null;
    }
    for(var i=0; i < this.length; i++){
        if (predicate(this[i]))
            return this[i];
    }
    return null;
}

Array.prototype.last_or_default = function (predicate){
    return this.reverse().first_or_default();
}

Array.prototype.count = function (predicate){
    var count = this.length;
    if (predicate == null) return count;
    for(var i=0; i < this.length; i++){
        if (!predicate(this[i])) count-=1;
    }
    return count;
}

Array.prototype.distinct = function * (predicate){
    var already_yielded = []
    var has_predicate = predicate != null;

    for(var i=0; i < this.length; i++){
        var current_element = this[i];

        if (!has_predicate){
            if (already_yielded.indexOf(current_element) >= 0) continue;
            already_yielded.push(current_element);
        }else{
            if (already_yielded.indexOf(predicate(current_element)) >= 0) continue;
            already_yielded.push(predicate(current_element));
        }
        yield current_element;
    }
}


texts: TypeScript Generics: Create a generic map

texts:
Exercise from a TypeScript course:

Create a map using TypeScript Generics. The key is always a string.
The value can be a string or a number. It has to work with both types.
It's got to have the following methods:

Creating a new key-value pair : setItem(key: string, item: T)

Retrieving values : getItem(key: string)

Remove all items : clear()

Display all key-value items : printMap() "

Here my uncompiled TypeScript code:

// -- THE ACTUAL IMPLEMENTATION --------------------------
class GenericMap < T extends number | string > {
  private items: object[]

  constructor() {
    this.items = [];
  }

  setItem(key: string, item: T): void {
    let tmp = {};
    tmp[key] = item;

    this.items.push(tmp);
  }

  getItem(key: string): T {
    let ret = null;

    for (let i = 0; i < this.items.length; i++) {
      let curr = this.items[i];

      if (curr[key]) {
        return curr[key];
      }
    }

    return null;
  }

  clear() {
    this.items.length = 0;
  }

  printMap() {
    this.items.forEach((item) => {
      let key: string[] = Object.keys(item);

      console.log('{ "' + key[0] + '": ', item[key[0]], '}');
    });
  }
}

// ------- JUST TESTING --------------------
const numb = new GenericMap < number > ();
numb.setItem('alpha', 7);
numb.setItem('beta', 14);
numb.printMap();
numb.clear();
numb.printMap();

const strMap = new GenericMap < string > ();
strMap.setItem('color', 'Blue');
strMap.setItem('height', '54 cm');
strMap.printMap();
// ---------------------------------------------

The compiled code as a live-demo:

class GenMap {
  constructor() {
    this.items = [];
  }
  setItem(key, item) {
    let tmp = {};
    tmp[key] = item;
    this.items.push(tmp);
  }
  getItem(key) {
    let ret = null;
    for (let i = 0; i < this.items.length; i++) {
      let curr = this.items[i];
      if (curr[key]) {
        return curr[key];
      }
    }
    return null;
  }
  clear() {
    this.items.length = 0;
  }
  printMap() {
    this.items.forEach((item) => {
      let key = Object.keys(item);
      console.log('{ "' + key[0] + '": ', item[key[0]], '}');
    });
  }
}
const numb = new GenMap();
numb.setItem('alpha', 7);
numb.setItem('beta', 14);
numb.printMap();
numb.clear();
numb.printMap();
const strMap = new GenMap();
strMap.setItem('color', 'Blue');
strMap.setItem('height', '54 cm');
strMap.printMap();

It works fine. But I guess there a better ways to solve these exercise.
Especially my getItem-method has become rather long.
Has someone an idea to solve the exercise in a more elegant way?
What do you think about my idea of storing objects within an array?
Looking forward to reading your answers and comments.


texts: Increment the neighbors of a cell in a matrix

texts:
Let's say you want to increment the cells around the position (x,y) in a square matrix m of size T.

I've written the same code in a few languages. Here's the JS syntax version :

if (x>0) {
    if (y>0) m[x-1][y-1]++;
    m[x-1][y]++;
    if (y<T-1) m[x-1][y+1]++;
}
if (y>0) m[x][y-1]++;
if (y<T-1) m[x][y+1]++;
if (x<T-1) {
    if (y>0) m[x+1][y-1]++;
    m[x+1][y]++;
    if (y<T-1) m[x+1][y+1]++;
}

I don't like this kind of code, those tests look redundant. What bothers me is less the clarity of the code than the fact it has to do so many times the same checks.

But I don't see anything better. Am I just missing an obvious solution or is there nothing better with the branching toolset we have ? A slower code isn't acceptable as an answer, performance is the essence of the question.


texts: Avoiding nested conditionals and comparing strings

texts:
I have a web page with a set of icons that are grey by default:

If a user clicks one, it toggles green (.toggleClass('green')):

If a user clicks a green one, it toggles back to grey (.removeClass('green')).
Only one icon can be green at any given time, so if you have a green icon and you click a grey icon, the icon that was originally green will change to grey while the one you clicked would change to green.

Each of these icons corresponds to a section on the page.

If all the icons are grey, all the sections on the page are visible.

If one icon is green, only that icon's section is visible, the sections represented by the grey icons are hidden.

Here is the code that controls that behavior:
$(document).on 'click', '.toggler', ->
  icons = [$('#toggle-videos'),$('#toggle-images'),$('#toggle-words')]
  sections = [$('#videos'),$('#images'),$('#words')]
  if $(this).hasClass('green')
    $(this).toggleClass('green')
    i = 0
    while i < sections.length
      sections[i].show()
      i++
  else
    $(this).addClass('green')
    i = 0
    while i < icons.length
      if icons[i][0] != $(this)[0]
        icons[i].removeClass('green')
        sections[i].hide()
      else
        j = 0
        while j < sections.length
          if $(this).attr('id').indexOf(sections[j].attr('id')) != -1
            sections[j].show()
          j++
      i++

I feel it is very hard to understand what this code does simply by reading it.  I feel I am missing a simpler way to accomplish the following:

Associate an icon with a specific section on the page
Keep the green state of an icon associated with the visibility of a specific section on the page

My primary concerns with the code:

I have several nested if statements with nested while statements
I am checking the relationship between a section and an icon by checking if the icon.id has part of the string (this is the icon element and j is a counter):
if $(this).attr('id').indexOf(sections[j].attr('id')) != -1

Is this acceptable for javascript or is there a better way?


texts: Making HttpWebRequest and not really interested in the response

texts:
I am working on top of existing application and I have this (original) method for making a HttpWebRequest:

        internal string SendWebRequest(string url, Guid Id)
        {
            string responseFromServer = string.Empty;
            Uri asyncUri;
            if (Uri.TryCreate(url, UriKind.Absolute, out asyncUri))
            {
                HttpWebRequest webRequest = (HttpWebRequest) WebRequest.Create(asyncUri);
                webRequest.Method = "POST";
                webRequest.KeepAlive = false;
                webRequest.ContentType = "...";

                StringBuilder sbPostedData = new StringBuilder();
                sbPostedData.Append("ID").Append("=").Append(Base64Encode(Id.ToString()));

                ASCIIEncoding encoding = new ASCIIEncoding();
                byte[] postedData = encoding.GetBytes(sbPostedData.ToString());
                webRequest.ContentLength = postedData.Length;
                Stream requestStream = webRequest.GetRequestStream();
                requestStream.Write(postedData, 0, postedData.Length);
                requestStream.Close();

                WebResponse response = webRequest.GetResponse();

                var dataStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(dataStream);
                responseFromServer = reader.ReadToEnd();
            }

            return responseFromServer;
        }

The things that got my attention here are that:

The method is used only within the class but yet the access modifier is internal. However I'm having the same method with the same logic. For the time being I've made it private but maybe it's a candidate for some utility or helper class?
We call WebResponse response = webRequest.GetResponse(); but actually when we get this - responseFromServer = reader.ReadToEnd(); and 3 lines below return it, the result is actually not used.

So my questions are, given the fact that the response is never used, first, is it fine to just get rid of this piece of code:

    WebResponse response = webRequest.GetResponse();

    var dataStream = response.GetResponseStream();
    StreamReader reader = new StreamReader(dataStream);
    responseFromServer = reader.ReadToEnd();

Just make the method void, and add this only when/if needed? What do you think is better, adding the same method in the new class and making it private or extracting it to some helper/utility class. And any suggestions on refactoring this code snippet?


texts:
On an IRC channel I'm a member of, one particular person mocks every piece of code I post, even if it's a rough draft or if I haven't coded in months. I am pretty well versed with computer theory and knowledge, but my skills with programming lack due to little practice.
 This particular code was from a project I was starting and I was putting down some ideas/structure on GitHub, when he pointed out this particular snippet.

So, after a couple months of trying to get the discipline to start coding again, I coded this and my question is: If indenting like this is bad, what other things did I do wrong here, assuming the logic is sound (I never tested it, I went on hiatus)?  Also, what would you do to avoid the indenting?

void parse_arguments(int count, char * arguments[]){//probably add file pointer here  to pass to load, change return type when an API is decided.
    int currentArg, paramIndex;

    for(currentArg = 1; currentArg <= count; currentArg++){
        if(arguments[currentArg][0] == '-'){
            for(paramIndex = 0; paramIndex < PARAMMAX; paramIndex++){
                if(strcmp(arguments[currentArg][1], arglist[paramIndex]) == 0){
                    switch(paramIndex){
                        case 0:
                            //load(currentArg + 1);
                            currentArg++;
                            break;
                        default:
                            break;
                    }
                    break;
                }
            }
        }
    }
}

Note: arglist is a statically declared array of chars.


texts: Find cycles in Graph using Union Find Algorithm on Adjacency List

texts:
My code detects if a cycle is present in a graph using Union find algorithm with Weighted tree analysis and path compression.

It works and I have included test cases.

# check cycles in graph using union find
# weighted tree balancing added

from collections import defaultdict
class graph:

  def __init__(self):

    self.graph = defaultdict(list)
    self.parents = {}
    self.size = {}

  def addnode(self, key1, key2, edge=0):
    if key2 not in self.graph:
      self.graph[key2] = []

    self.graph[key1].append(key2)

  def findcycle(self):

    for key in self.graph:
      self.parents[key] = -1
      self.size[key] = 1

    for vertex in self.graph:
      for neighbour in self.graph[vertex]:
        x_par = self.getparent(vertex)
        y_par = self.getparent(neighbour)
        if x_par == y_par:
          return True
        self.union(x_par, y_par)

  def getparent(self, vertex):

    while self.parents[vertex] != -1:
      vertex = self.parents[vertex]

    return vertex

  def union(self, vertex, neighbour):

    x_par = self.getparent(vertex)
    y_par = self.getparent(neighbour)
    if self.size[x_par] <= self.size[y_par]:
      self.parents[x_par] = y_par
      self.size[y_par] += self.size[x_par]
    else:
      self.parents[y_par] = x_par
      self.size[x_par] += self.size[y_par]

g = graph()
g.addnode(0, 1)
g.addnode(2, 3)
g.addnode(4,2)
g.addnode(4,1)

if g.findcycle():
    print ("Graph contains cycle")
else :
    print ("Graph does not contain cycle ")


texts:
I need a standalone client that consumes messages from the queue every time a message exists. I already have set-up an ActiveMQ queue named hello.world.

ConsumerClient.java

public class ConsumerClient {

    public static void main(String[] args){

        Thread thread = new Thread(new Consumer("tcp://localhost:61616", "hello.world"));
        thread.start();
    }
}

Consumer.java

public class Consumer implements Runnable{

    private String brokerUrl;
    private String queueName;
    private boolean isRunning;

    public Consumer(String brokerUrl, String queueName) {
        this.brokerUrl = brokerUrl;
        this.queueName = queueName;
        isRunning = true;
    }

    @Override
    public void run() {

        while (isRunning) {

            System.out.println("Listening for messages...");

            Connection connection = null;
            MessageConsumer consumer = null;
            Session session = null;

            try {

                //Create a connection factory
                ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerUrl);

                //Create a connection
                connection = connectionFactory.createConnection();
                connection.setExceptionListener(new ExceptionListener(){
                    @Override
                    public void onException(JMSException arg0) {
                        System.out.println("JMS Exception occurred...");
                    }
                });

                //Create a session
                session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);

                //Create destination
                Destination destination = session.createQueue(queueName);

                //Create consumer
                consumer = session.createConsumer(destination);

                //Start the connection
                connection.start();

                Message message = consumer.receive();
                if (message instanceof TextMessage) {
                    TextMessage txtMessage = (TextMessage) message;
                    try {
                        //Persist message to database
                        //...some code that persists message to database
                        session.commit();
                    } catch (Exception e) {
                        //Persistence failed
                        session.rollback();
                    }
                }

            } catch (JMSException e) {
                e.printStackTrace();
            } finally {
                if (consumer != null)
                    try {
                        consumer.close();
                    } catch (JMSException e) {
                        e.printStackTrace();
                    }

                if (session != null)
                    try {
                        session.close();
                    } catch (JMSException e) {
                        e.printStackTrace();
                    }

                if (connection != null)
                    try {
                        connection.close();
                    } catch (JMSException e) {
                        e.printStackTrace();
                    }
            }
        }
    }
}

I'm not sure if I need to use a MessageListener instead (honestly, I don't know where the MessageListener would fit in my code since if I remove MessageConsumer.receive() and added my own listener, my application hangs since there's no synchronous blocking). We're planning to have at least two of these consumers up just in case one fails.

Is this best practice? We don't use JNDI and I feel that Apache Camel would be too much for a simple process like this.

Should I be creating a deployable .war file instead?


texts: Very simple task scheduler (Task based, not Timer based)

texts:
My app has several periodic tasks. So I created this very simple generic task scheduler (got sick of copy-pasting code, which is based on Timers). It is very very basic and simple, can you find anything wrong with it?

//generic scheduling-job class
public static class PeriodicTask
{
    //keep a list of scheduled actions
    static Dictionary<Action, CancellationTokenSource> _scheduledActions = new Dictionary<Action, CancellationTokenSource>();

    //schedule an action
    public static async Task Run(Action action, TimeSpan period)
    {
        if (_scheduledActions.ContainsKey(action)) return; //this action is already scheduled, get out

        var cancellationTokenSrc = new CancellationTokenSource(); //create cancellation toket so we can abort later when we want

        _scheduledActions.Add(action, cancellationTokenSrc); //save it all into our disctionary

        //main scheduling loop
        Task task = null;
        while (!cancellationTokenSrc.IsCancellationRequested)
        {
            if (task == null || task.IsCompleted) //skip if previous invocation is still running (our requirements allow that)
                task = Task.Run(action);

            await Task.Delay(period, cancellationTokenSrc.Token);
        }
    }

    public static void Stop(Action action)
    {
        if (!_scheduledActions.ContainsKey(action)) return; //this method has not been scheduled, get out
        _scheduledActions[action].Cancel(); //stop the task
        _scheduledActions.Remove(action); //remove from the collection
    }
}

Usage

PeriodicTask.Run(MyStaticMethod, TimeSpan.FromHours(1));
PeriodicTask.Stop(MyStaticMethod);

private static void MyStaticMethod()
{
    //do something
}


texts: Using method which return a String or creating wrapper for String

texts:
Which approach is better and why?

First approach (inner class for wrapping):

public class OOP {
    private SomeService someService;

    private final class OrganizationPrimaryKeyString implements Serializable {
        private static final long serialVersionUID = -256615521081939636L;
        private final String pkString

        public OrganizationPrimaryKeyString(final OrganizationEnum organizationEnum) {
            final PK pk = someService.getValue(organizationEnum).getPk();

            if (pk == null) {
                throw new IllegalArgumentException("Can't be null");
            }

            pkString = pk.getLongValueAsString();
        }

        @Override
        public String toString() {
            return pkString;
        }
    }

    public List<Information> getInformation(final OrganizationEnum organizationEnum) {
        final OrganizationPrimaryKeyString organization = new OrganizationPrimaryKeyString(organizationEnum);

        try {
            final SessionContext ctx = createAndSetupLocalSessionContext(organization);

            (...)

            return (...);
        } catch (final FactoryException e) {
            throw new SystemException(e.getMessage(), e);
        } finally {
            closeLocalSessionContext();
        }
    }

    private SessionContext createAndSetupLocalSessionContext(final OrganizationPrimaryKeyString organization) {
        (...)
        sessionContext.setAttribute("organization", salesOrganization.toString());

        return sessionContext;
    }
}

Second approach (class method):

public class OOP {
    private SomeService someService;

    public List<Information> getInformation(final OrganizationEnum organizationEnum) {
        try {
            final SessionContext ctx = createAndSetupLocalSessionContext(organizationEnum);

            (...)

            return (...);
        } catch (final FactoryException e) {
        throw new SystemException(e.getMessage(), e);
        } finally {
            closeLocalSessionContext();
        }
    }

    private SessionContext createAndSetupLocalSessionContext(final OrganizationEnum organizationEnum) {
        (...)
        sessionContext.setAttribute("organization", getPkString(organizationEnum));

        return sessionContext;
    }

    private String getPkString(final OrganizationEnum organizationEnum) {
        final PK pk = someService.getValue(organizationEnum).getPk();

        if (pk == null) {
            throw new IllegalArgumentException("Can't be null");
        }

        return pk.getLongValueAsString();
    }
}


texts: Multiple similar event handlers for input validation

texts:
I want to reduce my repetitive code used to validate inputs values. For that I'm using:

a class associated to the input
a condition to validate the input value
an event handler

First I declare the classes I'm using:

var inputNotNull = '.js-mandatory-notnull',
    input2Words = '.js-mandatory-2words',
    inputNumb = '.js-mandatory-numb';

Then I create an event handler for each class and each of them has the same pattern: Call the function validateInput(event, thisInput, itsCondition)

$(document).on('keyup change focusout', inputNotNull, function(e){
    var $xthis = $(this),
        condition = $xthis.val().length>0;
    validateInput(e, $xthis, condition); //treats the visual part of the input
});

$(document).on('keyup change focusout', input2Words, function(e){
    var $xthis = $(this),
        condition = $xthis.val().match(/^[A-Za-zÀ-ú]+ [A-Za-zÀ-ú]+/i);
    validateInput(e, $xthis, condition);
});

$(document).on('keyup change focusout', inputNumb, function(e){
    var $xthis = $(this),
        condition = this.value.match(/^(?=.*\d)\d*[\.\,]?\d*$/);
    validateInput(e, $xthis, condition);
});

I was wondering how I could reduce this code. In this example I only show 3 classes, but on my code I have >20 classes. Is it possible to do something similar to "key:value" ("input:condition") and I would end up writing something like this:

var validations = {
    '.js-mandatory-notnull' : length > 0,
    '.js-mandatory-2words' : match(/^[A-Za-zÀ-ú]+ [A-Za-zÀ-ú]+/i),
    '.js-mandatory-numb' : match(/^(?=.*\d)\d*[\.\,]?\d*$/),
}

And then somehow treat this object to reproduce what I wrote before.

In this example I'm talking about validations, but I would like to apply this knowledge into another repetitive code scenarios.


texts:
I have written this custom hashset and though it isn't completed yet, I would like to know if there is anything I am overlooking in terms of clean code conventions. My aim was also to apply generics to it. So, would also want any input regarding that.

  public class CustomHashset<T> {

    private static final int SIZE = 100;
    private Entry<T>[] buckets;
    private int size;

    public CustomHashset() {
      this.buckets = new Entry[SIZE];
      this.size= 0;

    }

    private int hash(T element) {
        return element.hashCode() % buckets.length;
    }

    public Boolean add(T element) {
        int index = hash(element);

        Entry<T> current = buckets[index];
        while (current!=null) {
            if (current.key.equals(element)) return false;
            current = current.next;
        }
        Entry<T> entry = new Entry<T>();
        entry.setKey(element);
        entry.setNext(buckets[index]);
        buckets[index] = entry;
        size++;

        return true;
    }

    public int size() {
        return size;
    }

    private static class Entry<T> {
        private T key;
        private Entry next;

        public  T getKey() {
            return key;
        }

        public void setKey(T element) {
            this.key = element;
        }

        public Entry getNext () {
            return next;
        }

        public void setNext(Entry next) {
            this.next = next;
        }
    }
}


texts: Calculating travel statistics based on CSV data

texts:
I took a code test for acme transport company and was rejected. That's ok. I'm still learning how to code. I didn't get much feedback so that's a bit frustrating. Can anyone here provide tips on how to optimize this python code? I thought that I had done a pretty good job with it, had the correct answers, etc. But apparently there are some fatal flaws, at least according to acme transport co!

Here's the challenge:

Parse a text file so that we know the following:

how many cars were dropped in total?
which car had the longest single trip? How long was the trip?
what car had the longest cumulative distance? How long was that.

The text file is formatted like this:

timestamp, Integer, The time in seconds since the start of the simulation
id, String, The id of the associated vehicle, e.g. JK5T
event_type, String The type of the event is one of START_RIDE, END_RIDE, DROP
x, Double, The x coordinate of the location of where the event happened in the simulation
y, Double, The y coordinate of the location of where the event happened in the simulation
user_id, Integer, The id of the associated user or NULL if the event does not have an associated user

The first few lines of the file look like this:

302,OPN1,DROP,33.28280226855614,-87.88162047789511,NULL
305,OPN1,START_RIDE,33.28280226855614,-87.88162047789511,4204
419,G2I0,DROP,-28.367411419413685,23.567421582328606,NULL
426,OPN1,END_RIDE,7.563016232237587,91.97004351346015,4204
428,LQBF,DROP,-50.50579634246066,-54.53980771216895,NULL
441,5HQH,DROP,-70.05156581770649,36.8358644984674,NULL

For my code I import a library called shapely that helps determine distances:

import csv
from shapely.geometry import Point
from collections import defaultdict

def main():
        myDict = defaultdict(dict)
        aDict = {}
        totalDrops = 0
        maxCar = None
        with open('events.txt') as f:
            reader = csv.reader(f)
            for row in reader:
                if row[2] == 'DROP':
                    totalDrops += 1
                    myDict[row[1]]['drop'] = Point(float(row[3]), float(row[4]))
                if row[2] == 'START_RIDE':
                    myDict[row[1]]['start_ride'] = Point(float(row[3]), float(row[4]))
                if row[2] == 'END_RIDE':
                    singleTripDistance = myDict[row[1]]['drop'].distance(Point(float(row[3]), float(row[4])))
                    distanceTraveled = myDict[row[1]]['start_ride'].distance(Point(float(row[3]), float(row[4])))
                    aDict[row[1]] = singleTripDistance
                    if not maxCar:
                        maxCar = row[1]
                    if 'allDistances' not in myDict[row[1]]:
                        myDict[row[1]]['allDistances'] = distanceTraveled
                    else:
                        myDict[row[1]]['allDistances'] += distanceTraveled
                    if  myDict[row[1]]['allDistances'] > myDict[maxCar]['allDistances']:
                        maxCar = row[1]
            maxSingleTrip = max(zip(aDict.values(), aDict.keys()))
            print('There are ' + '' + str(totalDrops) + ' ' + 'total Drops.' + '\n' + 'The Car that ends up furthest away from its drop is:' + ' ' + str(maxSingleTrip[1]) + ' ' + 'and the distance is:' + ' ' + str(maxSingleTrip[0]) + '\n' + 'The Car with the highest total distance traveled is:' + ' ' + maxCar + ', ' + 'and the total distance is:' + ' ' +  str(myDict[maxCar]['allDistances']))
main()


texts:
I'm trying to create an efficient function for re-sampling time-series data.
Assumption: Both sets of time-series data have the same start and end time. (I do this in a separate step.)
Resample function (inefficient)
import numpy as np

def resample(desired_time_sequence, data_sequence):
    downsampling_indices = np.linspace(0, len(data_sequence)-1, len(desired_time_sequence)).round().astype(int)
    downsampled_array = [data_sequence[ind] for ind in downsampling_indices]
    return  downsampled_array

Speed testing
import timeit
def test_speed(): resample([1,2,3], [.5,1,1.5,2,2.5,3,3.5,4,4.5,5,5.5,6])
print(timeit.timeit(test_speed, number=100000))
# 1.5003695999998854

Interested to hear any suggestions.


texts: Feedback on Pitch Constellation/ Chromatic Circle in Python Project

texts:
Hello I'm doing a mini project that is modeled after pitch constellation.
--> https://en.wikipedia.org/wiki/Chromatic_circle
Basically, pitch constellation is modeled after a clock where each note is assigned a number. When the wheel goes clockwise it goes up the scale, and when it goes counterclockwise it goes down. I thought this might be a great idea for a midi project. (My project only goes up the scale.)
Here's how it works:

It prompts for an input of a music note and then shifts the elements so that the list will start with the root key. Then it will return the notes starting with the root note or the note selected.

Then it prompts to enter what mode to return the music note values. The way this done is by assigning the elements in the returned notes to the value selected in the dict variable scale. Which will then select the index number of the items and create a new list.

Lastly, it returns the scale with the key and mode.

That's all it does so far. Eventually, I'd like to combine it with a midi module I found in pythons open library and then have it send midi out. I want to get honest feedback on what areas can be improved in my program before I go any further.

notes = ['C', 'C#', 'D', 'D#',
             'E', 'F', 'F#', 'G', 'G#',
             'A', 'A#','B']

scale = {'major':[0, 2, 4, 5, 7, 9, 11],
    'minor':[0, 2, 3, 5, 7, 8, 10],
    'phrygian':[0, 1, 3, 5, 7, 8, 10],
    'lydian':[0, 2, 4, 6, 7, 9, 11],
    'mixolydian':[0, 2, 4, 5, 7, 9, 10],
    'aeolian':[0, 2, 3, 5, 7, 8, 10],
    'locrian':[0, 1, 3, 5, 6, 8, 10],
    'chromatic':[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
    'wholetone':[0, 2, 4, 6, 8, 10],
    'diminished7thchord': [0, 3, 6, 9],
    'augmentedchord':[0, 4, 8],
    'tritone':[0, 6]}

rootkey = input("Enter the root Note: ").upper()

class CircleFifths:
    """docstring for CircleFifths."""
    def __init__(self, key):
        self.rootkey = rootkey

    def shift(self, rootkey, index=0, s=0,):
        #shifts the leading item (rootkey) entered by the user down the list
        #removes the last term and puts it as the first term
        while index < 12 and s <12:
            s += 1
            index += 1
            new_notes = notes[-s:] + notes[:-s]
            if rootkey == new_notes[0]:
                print(new_notes)
                return new_notes

def main():
    a = CircleFifths(rootkey)
    key = a.shift(rootkey)

    keymode = []

    print('Select a scale and enter it below')
    for k in scale:
        print(k)

    mode = input("Enter the mode: ").lower()
    try:
        x = scale.get(mode)
        keymode = [key[i] for i in x]
        print(keymode)
    except:
        if mode not in scale:
            print("Scale not in list. Please start the program again.")
        return

main()


texts: Find the longest sub string of a word after concatenation of given words array

texts:
An Array of N words is given. Each word consists of small letters ('a'-'z'). Our goal is to concatenate the words in such a way as to obtain a single word with longest possible substring composed of one particular letter. Find the length of such substring.

Example 1: words = ["aaba", "aaaa", "bbab"] -> maxSubstring=6 of letter a, from the concatenated word = words[1] + words[0] + words[2] = "aaaaaababbab"

Example 2: words = ["xxbxx", "xbx", "x"] -> maxSubstring=4 of letter x, from the concatenated word = words[0] + words[2] + words[1] = "xxbxxxxbx"

My current implementation uses brute force:
find all combinations of words, and calculate the max sub-string.
Complexity is \$O(2^n)\$.

I'm looking for better ideas to reduce the time complexity.

class candiateCode{
    static Integer max = Integer.MIN_VALUE;

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        sc.nextLine();
        String[] arr = new String[N];

        for (int i = 0; i < N; i++) {
            arr[i] = sc.nextLine();

        }

        findCombi(arr, N, "", new HashMap<Integer, Boolean>());

        System.out.println(max);

    }

    private static void findCombi(String[] arr, int n, String prefix, Map<Integer, Boolean> map) {
        if (n == 0) {
            calculateMaxChar(prefix);
        }
        for (int i = 0; i < arr.length; i++) {
            if (!map.containsKey(i)) {
                Map<Integer, Boolean> map2 = new HashMap<>(map);
                map2.put(i, true);
                findCombi(arr, n - 1, prefix.concat(arr[i]), map2);
            }

        }
    }

    static void calculateMaxChar(String str) {
        Integer max1 = 1;
        Integer curr = 1;
        char ch = str.charAt(0);
        for (int i = 1; i < str.length(); i++) {
            if (str.charAt(i) == ch) {
                curr++;
            } else {
                ch = str.charAt(i);
                curr = 1;
            }
            if (max1 < curr)
                max1 = curr;

        }
        if (max1 > max)
            max = max1;

    }
}


texts: Finding legal knight moves in a chessboard in APL (follow-up)

texts:
This question is a follow-up to this previous question of mine. Assuming I understood correctly what is outlined in this meta post.

I wrote (and now, re-wrote) a function that takes as input a vector with two integers between 1 and 8 representing a position in a chessboard. This function should output a vector where each cell is a similar vector of integers, with the positions that a knight in the input position could reach.

E.g. for the input 1 1, my function should output (2 3)(3 2).

KnightMovesRevised ← {
  ⍝ Monadic function, expects a vector with 2 integers, e.g. (1 1)
  ⍝ Given a chessboard position, find the legal knight moves.
  ⍝ Returns vector of 2-integer vectors, e.g. (2 3)(3 2)

  ⍝ aux train to check if position is inside chessboard
  isInsideBoard ← ∧/(1∘≤∧≤∘8)

  signs ← ¯1 1 ∘., ¯1 1
  offsets ← (1 2)(2 1)
  moves ← , signs ∘.× offsets
  ⍝ list all the locations the knight could go to
  locations ← moves + ⊂⍵
  ⍝ and keep the valid ones
  valid ← isInsideBoard¨ locations
  valid/locations
}

Changes

From the previous version to this one, I

Reformatted code a bit with a suggested naming convention, by naming an auxiliary train and adding a couple more comments;
Removed the train used to write offsets, which I had used just to give a go at tacit programming. This is such a small vector that I think it makes more sense to hardcode it;
Rewrote signs by writing twice the ¯1 1 and removing ,, ⍨. This made it slightly easier to digest and not much more annoying to type;

These changes were motivated by the two great reviews I got (here and here) and I was hoping I could get reviews on those changes, because I tried to adhere to their suggestions but I didn't necessarily agree with all of them.

Questions

(paired with the above)

Are the extra comments ok or are they too much?
Are signs and offsets defined in an acceptable way? I like the trade-off between hardcoding too much and using too many functions just to create a couple of constants.
What is the standard spacing notation around ¨? Should I write f¨ arg, f ¨ arg, f ¨arg or f¨arg?


texts: Add an HTML body class depending on specific conditions if a block region is present on the web page or not

texts:
I wrote this code and I am wondering if there is a better way to phrase this. I was considering switch case but I was not sure how to translate this code into that.

The purpose of this code is to add a css body class to the webpage depending on if certain block regions show up on the page or not. $varssignifies a path to an array available within the function. So for example preface_first is a block region within the page and I am testing to see if it is present or not. If it is, then we add an HTML body class i.e. $vars['attributes']['class'][] = for example.

/**
 * Override or insert variables into the page template.
 */
function template_preprocess_html(&$vars) {

     // Preface columns.
      if (!empty($vars['page']['preface_first']) && !empty($vars['page']['preface_second']) && !empty($vars['page']['preface_third'])) {
        $vars['attributes']['class'][] = 'preface-three';
      }
      elseif (!empty($vars['page']['preface_first']) && !empty($vars['page']['preface_second'])) {
        $vars['attributes']['class'][] = 'preface-two';
      }
      elseif (!empty($vars['page']['preface_first']) && !empty($vars['page']['preface_third'])) {
        $vars['attributes']['class'][] = 'preface-two';
      }
      elseif (!empty($vars['page']['preface_second']) && !empty($vars['page']['preface_third'])) {
        $vars['attributes']['class'][] = 'preface-two';
      }
      else {
        $vars['attributes']['class'][] = 'preface-one';
      }
}

The code works as is but I somehow think it can be improved to be shorter and more efficient.


texts: Calculate the sum of a series with a given accuracy

texts:
#include <iostream>
#include <cstring>
using namespace std;
int main() {
    system("chcp 1251>nul");
    int i = 1;
    int x;
    cin >> x;
    if (abs(x) >= 1) { cout << "The series is divergent"; }
    else {
    double eps = 0.000001;
    double sum = 0;
    double lastElementOfSequence=1;
    do {
        sum += lastElementOfSequence;
        lastElementOfSequence *= -1 * x * (2 * i + 3) / (2 * i);
         i++;
    } while (fabs(lastElementOfSequence) > eps);
    cout.precision(17);
    cout << "sum=" << sum << endl;
    }
    system("pause>nul");
    return 0;
}

$$S=(1+x)^{-\frac{5}{2}}=1-\frac{5}{2}x+\frac{5\cdot7}{2\cdot4}x^2-\frac{5\cdot7\cdot9}{2\cdot4\cdot6}x^3+\frac{5\cdot7\cdot9\cdot11}{2\cdot4\cdot6\cdot8}x^4$$
$$where\ |x|<1$$
The task in the image, and the code from above I will ask to check up whether the correctly solved task I will be grateful, but if an error that specify please or correct.


texts: Translating Python to Rust: reading a Salesforce ID from a string

texts:
As a first project in Rust, I'm translating an existing, working piece of Python code. This code's purpose is to convert a 15-character Salesforce Id (which is guaranteed to be ASCII, exactly 15 bytes of base-62 data) to its 18-character equivalent, which includes a 3-character suffix to discriminate between values differing from one another only in case.

The Rust code works. I am looking for guidance on how to improve its idiomaticity in Rust, particularly around processing this type of data. It's a String conceptually, but doesn't need the overhead of UTF-8 handling, and is likely to be read from (e.g.) CSV files or Web services that do return UTF-8 encoded data.

I also suspect that the way I handle the "accumulator" values suffix and base_two is not proper Rust and welcome education there.

Python

class SalesforceId(object):
    def __init__(self, idstr):
        if isinstance(idstr, SalesforceId):
            self.id = idstr.id
        else:
            idstr = idstr.strip()
            if len(idstr) == 15:
                suffix = ''
                for i in range(0, 3):
                    baseTwo = 0
                    for j in range (0, 5):
                        character = idstr[i*5+j]
                        if character >= 'A' and character <= 'Z':
                            baseTwo += 1 << j
                    suffix += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ012345'[baseTwo]
                self.id = idstr + suffix
            elif len(idstr) == 18:
                self.id = idstr
            else:
                raise ValueError('Salesforce Ids must be 15 or 18 characters.')

Rust

#[derive(Debug)]
struct SalesforceId(String);

impl SalesforceId {
    fn from_string(s: &str) -> SalesforceId {
        let b = s.as_bytes();
        match b.len() {
            18 => { SalesforceId(s.to_string()) }
            15 => {
                let mut suffix: [u8; 3] = [b'A', b'A', b'A'];
                for i in 0..3 {
                    let mut base_two = 0;
                    for j in 0..5 {
                        let character = b[i*5+j];
                        if character >= b'A' && character <= b'Z' {
                            base_two += 1 << j;
                        }
                    }

                    suffix[i] = b'A' + base_two
                }
                SalesforceId(format!("{}{}", s, std::str::from_utf8(&suffix).unwrap()))
            }
            _ => panic!("invalid Salesforce Id")
        }
    }
}

fn main() {
    println!("{:?}", SalesforceId::from_string("0063600000aqQ1h"));
}


texts:
The following code is intended to infer the alignment of double.
My questions include:

Is there a better to do this? Either by refining the code or using an entirely different approach.

Under what (reasonable) circumstances could this not work?

Under what (plausible) circumstances could this not work? See below.

What is the actual or practical complete list of types I would need to do this with to determine universal alignment?
I think it's short,int,long,long long,float,double, long double, void*.
Specifically char can't be have a alignment constraint (or c-strings are broken!), unsigned can't be different from signed (or type-aliasing is broken).
Is there any reason why void* can't stand in for all pointer alignments?

#include <stdio.h>

// *********************************************************************** //
//WARNING: Do not allow this translation unit to be compiled under the
//#pragma pack directive or similar unless whole application is compiled the same way.
// *********************************************************************** //

typedef struct {
    char mWedge;//Wedges the member off alignment with the start of the struct.
    double mMember;//Member with type we're going to get alignment of.
} DoubleAligned;

const size_t getDoubleAlignment(void){
//A bit of pointer shennanigans to count the padding between mWedge & mMember.
//See text.
    const DoubleAligned lSpecimen;
    const size_t lOffset= (char*)&(lSpecimen.mMember)-(char*)&(lSpecimen.mWedge);
    return lOffset;
}

int main(void) {
    printf("Alignment : %zu\n",getDoubleAlignment());
    return 0;
}

Expected output on platform where double has 4-byte alignment:

Alignment : 4

Expected output on platform where double has no alignment (AKA 1-byte alignment:

Alignment : 1

NB: If the compiler 'packs' structures by default and just 'takes the hit' on slicing and shuffling to get them into hardware alignment, I don't mind getting an output of 'Alignment : 1'. I'm interested in how the platform (programming environment presented to me) is aligned not trying to infer underlying hardware design.
Reasoning
The C specification (C99 to be specific) makes the following commitments:

malloc(.) returns a universally aligned address.

Structures can pad between members and at the end but specifically not the beginning.

Arrays of adjacent structures must be aligned in a way compatible with their members.

Members will be laid out in the order they are declared.

So the compiler has to place mMember in an aligned position after mWedge itself aligned to the start of DoubleAligned. The C specification doesn't further constrain why or how structures are padded but there appears to be no normal reason to pad them other than minimally to satisfy 2, 3 & 4 above.
Hence my question about 'plausible' circumstances. Formally the compiler can do what it likes including adding arbitrary space into structures (but not at the beginning) for reasons that have nothing to do with alignment - maybe debug info or overwrite detection. Does that actually happen?
I'm not entirely sure about void* standing in for all pointer types. void* must have the lowest-common-factor alignment of all the pointer types (not the types pointed to!).
Bonus Note:
On many platforms I can get away with:
const size_t getDoubleAlignment(void){
//A bit of pointer shennanigans to count the padding between mWedge & mMember.
//See text.
    const DoubleAligned* lSpecimen=NULL;
    const size_t lOffset= (char*)&(lSpecimen->mMember)-(char*)&(lSpecimen->mWedge);
    return lOffset;
}

But I believe this is formally undefined because it performs arithmetic on NULL.
Final Note: Why do you need to know? (someone will ask) Suppose I need a portable alternative malloc(.) implementation that uses pre- and post- 'overhead' areas. I could implement it around the native malloc(.) but would need to know what "universal alignment" meant to comply with the C standard for malloc(.) to carve out a universally aligned return value.


texts: Query to get distinct items at the top of results and duplicates following

texts:
I need to query the table below and list the distinct items at the top of my result set and the duplicates will follow, the order of the duplicates does not matter.

I am working with Sql Server 2016 if that offers any nice tricks for improvement.

Table design

create table Items(Id INT,Item VARCHAR(5))

insert into Items values(1,'Cat')
insert into Items values(2,'Dog')
insert into Items values(3,'Dog')
insert into Items values(4,'Cat')
insert into Items values(5,'Fish')
insert into Items values(6,'Cat')
insert into Items values(7,'Dog')

Here is my current query. It does function correctly but seems very poor and brute force'ish.

--Select all the items but have the distinct ones appear at the top and repeats following
SELECT Id= (SELECT TOP 1 Id FROM Items it2 WHERE it2.Item = it1.Item ORDER BY Id ASC), Item
FROM Items it1
GROUP BY Item

UNION ALL

SELECT *
FROM Items WHERE Items.Id NOT IN
(
    SELECT Id= (SELECT TOP 1 Id FROM Items it2 WHERE it2.Item = it1.Item ORDER BY Id ASC)
    FROM Items it1
    GROUP BY Item
)

Results

1, Cat
2, Dog
5, Fish
3, Dog
4, Cat
6, Cat
7, Dog


texts: UserScript to Highlight tab for queue item

texts:
It may seem a bit obsessive and/or pedantic, but the following script allows me to see when posts are ready for reviewing.
I developed this UserScript for use with TamperMonkey to allow me to have a tab open for a review queue, and then when an item is ready for review, the tab will be highlighted with an asterisk on the far left. Until an item is ready, an animation effect is applied to the title to show that a timer is currently running before refreshing the page. I have considered inspecting the number of action buttons to discern if an item is ready to be reviewed instead of inspecting the URL but this technique seems to work fine.
Questions:

How does the ecmascript-6 usage look?
What would you change and/or code differently?

// ==UserScript==
// @name         Highlight active review
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Bring attention to the user when a post is up for reviewing
// @author       Sam Onela
// @match        https://codereview.stackexchange.com/review/*
// ==/UserScript==

;(function() {
    'use strict';
    const INITIAL_CHECK_TIMEOUT = 1500;
    const INTERVAL_DELAY = 400;
    const RELOAD_DELAY = 6500;
    const CLICK_CHECK_DELAY = 6000;
    const ticks = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
    let timeout = window.setTimeout(check, INITIAL_CHECK_TIMEOUT);
    let interval;
    function updateTitle(showAsterisk = false) {
        var titleMatches = document.title.match(/^([\W]{1})Review/);
        if (titleMatches && titleMatches.length > 1) {
            const index = ticks.indexOf(titleMatches[1]);
            document.title =  document.title.replace(titleMatches[1], ticks[(index+1)%ticks.length]);
        }
        else {
            document.title =  (showAsterisk?'*': ticks[0]) + document.title;
        }
    }
    function check(clickHandlerAdded = false){
        if (timeout) {
            window.clearTimeout(timeout);
            timeout = 0;
        }
        if (window.location.pathname.match(/\d+/)) { // drilled in to a review item
            updateTitle(true);
            if (!clickHandlerAdded) {
                document.addEventListener('click', (event) => timeout = window.setTimeout(check, CLICK_CHECK_DELAY, true));
            }
        }
        else { //add spinner animation and then reload
            updateTitle();
            if (interval === undefined) {
                interval = window.setInterval(updateTitle, INTERVAL_DELAY);
            }
            window.setTimeout(_=> window.location = window.location, RELOAD_DELAY);
        }
    }
})();

Follow-up (meta) question
Perhaps this is too meta (and could be asked in a question on CR meta), and might not be seen by target user, but I'll ask anyway: What is the reason for the downvote? If something is unclear, please ask for clarification.
I understand votes are supposed to be anonymous and downvotes could also be used if the user thinks the OP didn't research "enough" or the code question is not useful...


texts: Improving Twitter JSON parser for big data set

texts:
I have the following fully working code that

imports JSON files,
parses the tweets contained in JSONs,
records them in a table in a data frame.

Considering that per run I currently analyze 1,400 JSONs (about 1.5Gb), the code takes quite some time to run. Please suggest if there is a plausible way to optimize the code in order to increase its speed. Thanks!

import os
import json
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import os

tweets = []

for dirs, subdirs, files in os.walk('/Users/mymac/Documents/Dir'):
    for file in files:
        if file.endswith('.json'):
            print(file)
            for line in open(file) :
                try:
                    tweet = json.loads(line)
                    tweets.append(tweet)
                except:
                    continue

tweet = tweets[0]

ids = [tweet['id_str'] for tweet in tweets if 'id_str' in tweet]
text = [tweet['text'] for tweet in tweets if 'text' in tweet]
lang = [tweet['lang'] for tweet in tweets if 'lang' in tweet]
geo = [tweet['geo'] for tweet in tweets if 'geo' in tweet]
place = [tweet['place'] for tweet in tweets if 'place' in tweet]

df=pd.DataFrame({'Ids':pd.Index(ids),
                 'Text':pd.Index(text),
                 'Lang':pd.Index(lang),
                 'Geo':pd.Index(geo),
                 'Place':pd.Index(place)})
df


texts:
I'm newbie in Python but I want to know some tricks to make my code look cute. I know that syntax of Python allows to do this, but I don't know how.
Suppose I have file with lines of format "x y z" and I want to parse it and make some objects. Now code looks like this:

class Point(object):
    def __init__(self, x, y, z):
        self.x = x;
        self.y = y;
        self.z = z;

...

def load_file(file_path):
    points = [];
    with open(file_path, "r") as input_file:
        for line in input_file:
            str_point = line.split();
            x = float(str_point[0]);
            y = float(str_point[1]);
            z = float(str_point[2]);
            points.append(Point(x, y, z));
    return points;

Is there any way to make this code simpler?


texts: Returning a pseudo-random Gaussian number

texts:
Is there any ways I can write redundant-less code without this infinite loop? I have also tried putting the conditions and the generating number, but since it has two conditions, the output number will be different.

private static final int PERCENT_97 = 3;
/**
 * Returns returns a pseudo-random Gaussium number. Forumla:
 * {@code (RanGassium * 3) + mean }
 *
 * @param mean
 *            mean of returning value
 * @param min
 *            min bound
 * @param max
 *            max bound
 * @return a pseduo-random Gaussium that is close to the mean.
 */
public static double getRandomGaussium(double mean, double min, double max) {
    double returningNumber = 0;
    double rangeMean=(max-min)/2;
    while (true) {
        returningNumber = (generator.nextGaussian() * rangeMean/PERCENT_97) + mean;
        if (returningNumber > min && returningNumber < max) {
            break;
        }
    }
    return returningNumber;
}


texts: Pandas nested loop which checks if values from one series are in another series

texts:
I have two different series in pandas that I have created a nested for loop which checks if the values of the first series are in the other series. But this is time consuming in pandas and I cannot work out how to change it to a pandas method. I thought to use the apply function but it did not work with method chaining. My original nested for loops look like so and they work;

for x in df_one['ser_one']:
    print(x)
    for y in df_two['ser_two']:
        if 'MBTS' not in y and x in y:
            if 'L' in y:
                print(y)

Is there a way to make this less time consuming?

Here is what I attempted using apply methods;

df_two['ser_two'].apply(lambda x: x if 'MBTS' not in df_one['ser_one'].apply(lambda y:y) and x in df_one['ser_one'].apply(lambda y:y))

Example input:

df_one.head()
Out[136]:
   type   ser_one
0  MBTS  VUMX1234
1  MBTS  VUMX6436
2  MBTS  VUMX5745
3  MBTS  VUMX5802
4  MBTS  VUMX8091

df_two.head()
Out[137]:
     ser_two
0  VUMX8091
1  VUMX8091L
2  VUMX1234
3  VUMX1234L
4  VUMX5838


texts: localStorage should expire after 24 hours

texts:
The following code is checking if the user's browser is IE and if this is the case and there's no localStorage yet, it sets a localStorage, which should be valid for only 24 hours.

(function ieAlert () {
  var isIE = document.documentMode
  var lastClear = window.localStorage.getItem('myLocalStorage')
  var timeNow = (new Date()).getTime()

  if (!isIE && lastClear === null) {
    window.alert(ieWarningText)
    // Setting a localStorage with 1 day expiry date
    if ((timeNow - lastClear) > 1000 * 60 * 60 * 24) {
      window.localStorage.clear()
      window.localStorage.setItem('myLocalStorage', timeNow)
    }
  }
})()

How could the script be improved?


texts: Grouping positive & negative reviews and counting the words

texts:
Can somebody help me make the code below smarter and free of redundancy? The code does exactly what I want, but I believe there are many redundant loops and arrays.

from collections import Counter
import csv
import re

with open("/Users/max/train.csv", 'r') as file:
  reviews = list(csv.reader(file))

def get_texts(reviews, score):
  texts = []
  texts.append([r[0].lower() for r in reviews if r[1] == str(score)]) ;
  return texts

def getWordListAndCounts(text):
  words = []
  for t in text:
      for tt in t:
        for ttt in (re.split("\s+", str(tt))):
            words.append(str(ttt))
  return Counter(words)

negative_text_list = get_texts(reviews, -1)
positive_text_list = get_texts(reviews, 1)

negative_wordlist_counts = getWordListAndCounts(negative_text_list)

positive_wordlist_counts = getWordListAndCounts(positive_text_list)

print("Negative text sample: {0}".format(negative_text_list))
print("Negative word counts: {0}".format(negative_wordlist_counts))
print("Positive text sample: {0}".format(positive_text_list))
print("Positive word counts: {0}".format(positive_wordlist_counts))

Sample contents in train.csv are as below.

i like google,1
google is evil,-1
Apple is cute,1
Microsoft booo,-1


texts:
I am burdened with the requirement of interacting with a C based library, which has a bunch of constant sized arrays (e.g. char[17]).

When trying to assign or read those properties from swift, they are represented as a Tuple type of that size..

As far as I know, you can't access tuples with subscripts, and it is not trivial to convert an array to a tuple .. so I am left with this ugly code to interact with the library:

Note for the curious: The C library uses the eatLength to determine which values of the tuple are valid, and which shouldn't be considered. That is why I don't care about what the padded value is.

typedef struct _DMMove
{
    uint8_t steps[17];
    uint8_t eats[16];
    uint8_t eatLength;
} DMMove;

extension DMMove {

    init(steps: [Int], eats: [Int]) {

        var paddedSteps = Array(0..<17) as [UInt8]
        var paddedEats = Array(0..<16) as [UInt8]

        for (i, v) in enumerate(steps) {
            paddedSteps[i] = Int8(v)
        }

        for (i, v) in enumerate(eats) {
            paddedEats[i] = Int8(v)
        }

        var stepsTuple = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0) as (Int8,Int8,Int8,Int8,Int8,Int8,Int8,Int8,Int8,Int8,Int8,Int8,Int8,Int8,Int8,Int8,Int8)
        var eatsTuple = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0) as (Int8,Int8,Int8,Int8,Int8,Int8,Int8,Int8,Int8,Int8,Int8,Int8,Int8,Int8,Int8,Int8)

        stepsTuple.0 = paddedSteps[0]
        stepsTuple.1 = paddedSteps[1]
        stepsTuple.2 = paddedSteps[2]
        stepsTuple.3 = paddedSteps[3]
        stepsTuple.4 = paddedSteps[4]
        stepsTuple.5 = paddedSteps[5]
        stepsTuple.6 = paddedSteps[6]
        stepsTuple.7 = paddedSteps[7]
        stepsTuple.8 = paddedSteps[8]
        stepsTuple.9 = paddedSteps[9]
        stepsTuple.10 = paddedSteps[10]
        stepsTuple.11 = paddedSteps[11]
        stepsTuple.12 = paddedSteps[12]
        stepsTuple.13 = paddedSteps[13]
        stepsTuple.14 = paddedSteps[14]
        stepsTuple.15 = paddedSteps[15]
        stepsTuple.16 = paddedSteps[16]

        eatsTuple.0 = paddedEats[0]
        eatsTuple.1 = paddedEats[1]
        eatsTuple.2 = paddedEats[2]
        eatsTuple.3 = paddedEats[3]
        eatsTuple.4 = paddedEats[4]
        eatsTuple.5 = paddedEats[5]
        eatsTuple.6 = paddedEats[6]
        eatsTuple.7 = paddedEats[7]
        eatsTuple.8 = paddedEats[8]
        eatsTuple.9 = paddedEats[9]
        eatsTuple.10 = paddedEats[10]
        eatsTuple.11 = paddedEats[11]
        eatsTuple.12 = paddedEats[12]
        eatsTuple.13 = paddedEats[13]
        eatsTuple.14 = paddedEats[14]
        eatsTuple.15 = paddedEats[15]

        self.init(
            steps: stepsTuple,
            eats: eatsTuple,
            eatLength: Int16(eats.count)
        )
    }

}

More Notes

If you don't know how a C struct is exposed to Swift, well it's as simple as though there is an actual Swift struct with an init that takes all the struct's attributes as parameters.

I am sure someone will suggest using Tuple initializers ... Here ya go:

  Expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions


texts: Randomly generate a Hex Colour code and its contrasting colour using JavaScript

texts:
I am attempting to generate a new colour and its contrasting colour with this javascript code.

I am making a web app that is different every time it is loaded (i.e. background and text colour)

I would love some critique on the quality and efficiency of my code.

How can it be done better?

Here is my code:

function startcolour() {
 var values = Array('1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
 var color = "#";
 var item;

 for (i = 0; i < 6; i++) {
   item = values[Math.floor(Math.random()*values.length)];
   color = color + item;
 }

 return color;
}

function contrastcolour(colour) {
 var r;
 var g;
 var b;
 var comp = [];
 var contrast = '#';
 var arrayLength;

 if (colour.charAt(0) == '#') {
  colour = colour.slice(1);
 }

 r = (255 - parseInt(colour.substring(0,2), 16)).toString(16);
 g = (255 - parseInt(colour.substring(2,4), 16)).toString(16);
 b = (255 - parseInt(colour.substring(4,6), 16)).toString(16);

 contrast = contrast + padZero(r) + padZero(g) + padZero(b);
 return contrast
}

function padZero(str) {
  if (str.length < 2) {
    return '0' + str;
  }
  else {
    return str;
  }
}

function colourRun() {
  var colours = [];
  var base = startcolour();

  colours.push(base);
  var contrast = contrastcolour(base);
  colours.push(contrast);
  return colours;
}

Sample output (aka colours):

['#123abc','#edc543']

I then do the following with jQuery to set CSS properties of HTML components:

  $(document).ready(function() {
    var colours = colourRun();
    $("body").css({"background-color": colours[0], "color": colours[1]});
    $("nav").css({"background-color": colours[1], "color": colours[0]});
  });

All constructive criticism is appreciated.


texts: Return fake jqXHR from custom Ajax method

texts:
I have several methods that wrap the $.ajax() method and return its jqXHR, but I need to force fail before even calling the original $.ajax() and still allow to use success/error methods of the result.

//just a very simple example, actuall code is more complicated
$.rpc = function(method, args) {

    if (!method || 'string' === typeof method ||) {
        return false; //PROBLEM - cannot use success/error handlers!
    }

    return $.ajax({
        type: 'POST',
        url: '/rpc',
        data: { method: method, params: args },
    }); //AJAX()
};

//usage:
$.rpc(loginMethod, credentials).success(gotoProfile).fail(gotoLogin);
//if loginMethod is for any reason undefined it crash instead of calling fail handler

So I use this helper method, since the jqXHR is created directly inside the Ajax method:

$.ajax.getFailed = function(message) {
    var
        jqDef = new $.Deferred(),
        jqXHR = jqDef.promise();

    jqXHR.success = jqXHR.done; //deprecated  $.ajax methods
    jqXHR.error = jqXHR.fail;

    jqDef.reject.defer(1, jqXHR, [jqXHR, message]);
               //defer is from extJS, alternative to setTimeout

    return jqXHR;
};

$.rpc = function(method, args) {

    if (!method || 'string' === typeof method ||) {
        return $.ajax.getFailed('Missing method');
    }

    return $.ajax({
        type: 'POST',
        url: '/rpc',
        data: { method: method, params: args },
    }); //AJAX()
};

Is this code OK or is there anything else I should add?


texts:
I am trying to solve this question. The essence of the problem is this:

  Given a list of school classes with start and end times, find the total number of non-overlapping subsets.

I've written up a DP solution that I think is optimal in terms of time complexity - \$n * \log(n)\$.

'''
https://www.spoj.com/problems/ACTIV/
'''

from collections import defaultdict

def bin_search(classes, target):
    hi = len(classes)-1
    lo = 0
    while lo <= hi:
        mid = (hi + lo)//2
        if classes[mid][1] > target:
            hi = mid-1
        else:
            lo = mid+1
    return hi

def num_nonoverlapping(classes):
    classes.sort(key=lambda x: x[1])
    dp = [0] * len(classes)
    prefix_sum = defaultdict(int)
    dp[0] = 1
    prefix_sum[0] = 1

    for i in range(1, len(classes)):
        start = classes[i][0]
        best = bin_search(classes, start)
        dp[i] = 1 + prefix_sum[best]
        prefix_sum[i] = dp[i] + prefix_sum[i-1]
    return sum(dp)

def format_ans(ans):
    s = str(ans)
    if len(s) >= 8:
        return s[len(s)-8:]
    else:
        return (8-len(s)) * "0" + s

while True:
    n = int(input())
    classes = []
    if n == - 1:
        break

    for _ in range(n):
        start, end = map(int, input().split())
        classes.append((start, end))

    ans = num_nonoverlapping(classes)
    print(format_ans(ans))

I’m getting a Time Limit Exceeded Error. I'd love some pointers on how I could make this faster.


texts:
I need roomId (an auto_increment primary key) to locate a specific room to update chat info. My way of doing it right now is dumb.

In createRoom.js, I use roomName from a form that a user has entered to create a room model and find its roomId, then make roomId a param of a URL:

var Room = require('../models/database').Room

exports.create = function (req, res) {

Room
    .create({
        room_name: req.body.roomName
    })
    .complete(function () {

        Room
            .find({where: {room_name: req.body.roomName}})
            .success(function (room) {

                res.redirect('rooms/videochat/' + req.body.roomName + '/' + room.room_id);

        })

    })

};

in routes.js, I grab roomId with req.params.roomId and pass it to a view file jade so it can be displayed on a video chat page:

app.get('/rooms/videochat/:roomName/:roomId', function (req, res) {
    res.render('videochat.jade', {roomName: req.params.roomName, roomId: req.params.roomId});
});

In the client side JavaScript, I use

var roomId = $('#roomId span').text();

to get the roomId, then use socket.emit to pass it back to server side:

socket.emit('updateStartTime', roomId);


texts: Mixing chrome.i18n (internationalization method) into templates

texts:
I'm mixing chrome.i18n into my templates as templateHelpers with Backbone.Marionette.

templateHelpers: function () {
    return {
        //  Mix in chrome to reference internationalize.
        'chrome.i18n': chrome.i18n,
        instant: this.instant
    };
}

and here's my template:

<% if( instant ) { %>
    <img class="item-thumb" src="http://img.youtube.com/vi/<%= video.get('id') %>/default.jpg" />
<% } else { %>
    <img class="item-thumb" data-original="http://img.youtube.com/vi/<%= video.get('id') %>/default.jpg" />
<% } %>

<span class="item-title" title="<%= video.get('title') %>"><%= video.get('title') %>

    <% if( video.get('highDefinition') ) { %>
        <%= chrome.i18n.getMessage('hd') %> ·
    <% } %>

        <%= video.get('prettyDuration') %>

    <button class="button-icon playInStream" title="<%= chrome.i18n.getMessage('play') %>">

    </button>

    <button class="button-icon addToStream" title="<%= chrome.i18n.getMessage('enqueue') %>">

    </button>

    <button class="button-icon save" title="<%= chrome.i18n.getMessage('save') %>">

    </button>

The number of templates being rendered could well be in the thousands. Would it be greatly more efficient to pass the strings themselves into the template, i.e. call getMessage inside of templateHelpers, or does it not make much of a difference at all? My thought is that since I don't use the rest of chrome.i18n, I shouldn't be serializing it, but I'm not sure if that's the case.


texts: Finding average word length in a given year

texts:
I have written a function which takes the year in question and words as a data which is a dictionary that maps words to the list of year/count. Now I am wondering how I can improve the code that I have or how to make it more simpler or make it better performance-wise.

def avgWordLen(year, words):
    totLen = 0
    totword = 0
    for word in words:
        for nary in words[word]:
            if nary.year == year:
                totLen += len(word) * nary.count
                totword += nary.count
    if totword != 0:
        return totLen / totword
    else:
        return 0


texts: Extract changesets between last two tags in Mercurial

texts:
I have written the following script that I'd like to have reviewed:

#!/usr/bin/python

import os

os.system('hg tags > tags.txt')
file = 'tags.txt'
path = os.path.join(os.getcwd(), file)
fp = open(path)
for i, line in enumerate(fp):
    if i == 1:
        latestTagLine = line
    elif i == 2:
        previousTagLine = line
    elif i > 4:
        break
fp.close()
revLatestTag = latestTagLine.split(':')
l = revLatestTag[0].split(' ')
revPreviousTag = previousTagLine.split(':')
p = revPreviousTag[0].split(' ')
command = 'hg log -r {}:{} > diff.txt'.format(l[-1],p[-1])
os.system(command)

Output of hg tags command:

tip                         523:e317b6828206
TOOL_1.4                    522:5bb1197f2e36
TOOL_1.3                    515:7362c0effe40
TOOL_1.1                    406:33379f244971


texts:
I'm trying to find the first 100 million primes and am trying to find the most efficient way to do it. Can I get some advice on how I could improve this?

public class Main {

    public static void main(String[] args) {

        int counter = 0;
        int num = 3;
        int index = 1;
        int[] primes = new int[100000000];
        primes[0] = 2;

        while(counter < 99999999){
            if(isPrime(num)) {
                primes[index] = num;
                index++;
                counter++;
            }
            num+=2;
        }
        index = 1;
        for (int i : primes) {
            System.out.println(String.format("%d: %d", index, i));
            index++;
        }
    }

    private static boolean isPrime(int num){
        if (num < 2) return false;
        else if (num == 2) return true;
        for (int i = 2; i < num; i++) {
            if (num % i == 0) return false;
        }
        return true;
    }
}


texts:
I have the following class:

public class IssueWithComments : Issue
{
    public IReadOnlyList<IssueComment> FullComments { get; set; }

    public IssueWithComments()
        : this(new List<IssueComment>())
    {
    }

    public IssueWithComments(IEnumerable<IssueComment> comments)
    {
        FullComments = comments.ToList();

    }

    public static IssueWithComments FromIssue(Issue issue, IEnumerable<IssueComment> comments)
    {
        return new IssueWithComments(comments)
        {
            Assignee = issue.Assignee,
            Body = issue.Body,
            ClosedAt = issue.ClosedAt,
            Comments = issue.Comments,
            CreatedAt = issue.CreatedAt,
            HtmlUrl = issue.HtmlUrl,
            Labels = issue.Labels,
            Milestone = issue.Milestone,
            Number = issue.Number,
            PullRequest = issue.PullRequest,
            State = issue.State,
            Title = issue.Title,
            UpdatedAt = issue.UpdatedAt,
            Url = issue.Url,
            User = issue.User
        };
    }
}

where Issue is a type defined in the Octokit.Net namespace.

I populate my objects by calling the following code:

public static async Task<IReadOnlyList<IssueWithComments>> GetAllForRepositoryWithComments(this IIssuesClient client, string owner, string name)
{
     var issues = await client.GetAllForRepository(owner, name);
     List<IssueWithComments> all = new List<IssueWithComments>();

     foreach (var i in issues)
     {
         all.Add(IssueWithComments.FromIssue(i, await client.Comment.GetAllForIssue(owner, name, i.Number)));
     }

     return all;
 }

This works, but just takes 7.640 seconds on average according to System.Diagnostics.Stopwatch (plus that I can see it taking a long time to load).

Task.WhenAll could work, but the way to find the Issue it is referring to is as follows:

x.FullComments.First().HtmlUrl.Segments[4]

Is there a better way to either run the loop or should I go with Task.WhenAll and drill into the URL?


texts:
In my view, I have a CSS class in ruby variable which I'm using in style tag like below:

<% style_var = '.myclass {color:red}' %>

<div class="myclass">This text will be Red </div>

<style type="text/css">
  <%= style_var %>
</style>

Though it is working flawlessly, my question is: "Is it a good practice to use ruby variables in style tag?"


texts: Underscore and general app design review

texts:
I'm working on my larger project to date, and on top of that I'm using underscore for the first time.
All is working perfectly but I'd like some criticism on the use of underscore templating and underscore after function.

The goal of the app is to generate on the dom a messaging interface starting from an empty  div. It gets initialized by passing the jQuery selector of that empty div.

I took out most of the code and left what I'd like to be reviewed the most, hope the flow of the app is quite clear anyway.

(function($) {
$(document).ready(function() {

var mssg_tmpl = {
        msg_container : ' \
    <div class="row-fluid msg-container well" id="msg-<% id %>"> \
        some html \

',

        msg_reply : ' \

 \
        some html \

'

};

mssg = {
    init : function(selector) {
        this.$container = $(selector);
        this.tmpl = {};         // List of compiled templates functions
        this.compileTemplates(mssg_tmpl);
        this.messages = [{      // THIS IS HARDCODE TO ILLUSTRATE BASIC STRUCTURE
            id: 1,              // Thread opener
            children: [2,3,4]   // Replies
        }, {
            id: 5,
            children: [6,7,8]
        }];

        for ( var i in this.messages) {
            this.$container.append(this.tmpl.msg_container(this.messages[i])); // fn msg_container is a compiled template
            this.messages[i].$container = $('#msg-' + this.messages[i].id);
            this.fetchChildren(this.messages[i]);
        }

        this.bindEvents();
        this.collapseMessages();
        return this;
    },

    compileTemplates : function(templates) {
      for ( var k in templates) {
        this.tmpl[k] = _.template(templates[k]);
      }
    },

    fetchChildren : function(message) {

        var renderChildren = _.after(message.children.length, function() {
            children = _.sortBy(children, 'id'); // Sort replies (oldest first)
            for ( var i in children) {
                message.$container.find('div.msg-reply-container').before(mssg.tmpl.msg_reply(children[i])); // fn msg_reply is a compiled template
            }
        });

        var children = [];

        for ( var i in message.children) {
            $.ajax({
                url : 'getmessage.htm?messageId=' + message.children[i]
            }).done(function(response) {
                children.push(response);
                renderChildren();
            });
        }

    },
};

mssg = mssg.init('#messaging');

});

})(jQuery);


texts:
I'm implementing a search for a deep array. It applies the search/filter to the deepest level of children below, i.e. { name: 'foo' }, { name: 'bar' }.

If all children are filtered from the parent object, the parent is removed from the list too.

Since I want to leave the original object intact, I copy each container object and add a new children property to it.

I think this has a lot of room for improvement. It feels like it can be simplified. Also, this doesn't feel DRY.

var data = [
    {
    name: 'a1', children: [
        {
        name: 'b1', children: [
            { name: 'foo' }, { name: 'bar' }
        ]
      }
    ]
  },
  {
    name: 'a2', children: [
        {
        name: 'b2', children: [
            { name: 'baz' }, { name: 'bah' }
        ]
      }
    ]
  }
];

function filterData(data, filter){
    var result = [];
    _.forEach(data, function(firstLevelItem){
    var newFirstLevelItem = _.extend({}, firstLevelItem, { children: [] });

    _.forEach(firstLevelItem.children, function(secondLevelItem){
        var newSecondLevelItem = _.extend({}, secondLevelItem, {
        children: _.filter(secondLevelItem.children, filter)
      });

      if(newSecondLevelItem.children.length > 0) {
        newFirstLevelItem.children.push(newSecondLevelItem);
      }
    });
    if(newFirstLevelItem.children.length > 0) {
        result.push(newFirstLevelItem);
    }
  });
  return result;
}

var result = filterData(data, function(item){
    return item.name === 'foo';
});

console.log(result);

https://jsfiddle.net/b2xsna5k/


texts:
I'm looking for code review on the following basic log/error middleware. The logging middleware uses logger.js which has a bunch of methods like log.error() (which makes a request out to error tracking service like Sentry).

The handleErrors middleware is intended to handle any errors that occur in any of the api routes. So, for example, I have a route /api/users and if an error occurs in that request, the catch will execute and pass along the error to the handleErrors middleware.

const getUsers = (req, res, next) => {
  return apiHttpClient.get(req, '/api/users')
    .then(users => res.status(HttpStatus.OK).json(users))
    .catch(err => next(err));
};

The error middleware should create an error context for the client without exposing the specific error. So for example, if a 400 error occurs, the message sent to the browser would look like:

{
  "message": "Bad Request",
  "status": 400
}

But depending on if the NODE_ENV is production, the logging middleware should log out the specific error:

{
  "name":"Example",
  "hostname":"",
  "pid":18322,
  "level":40,
  "payload":{
    "name":"BadRequest",
    "message":"A specific error message",
    "statusCode":400,
    "errorCode":400
  },
  "msg":"400: A specific error message",
  "time":"2017-10-22T15:26:06.824Z",
  "v":0
}

Usage

express.js

const { logErrors, handleErrors } = require('./errorMiddleware');

// other middleware
// api routes

app.use(logErrors())
app.use(handleErrors());

errorMiddleware.js

const log = require('../../logger');
const HttpStatus = require('http-status-codes');

const isServerError = (statusCode) => {
  return statusCode >= HttpStatus.INTERNAL_SERVER_ERROR;
};

const isProdEnv = (env) => {
  return env === 'production';
};

const getStatusCode = (statusCode) => {
  return statusCode || HttpStatus.INTERNAL_SERVER_ERROR;
};

const createErrorContext = (statusCode) => {
  return {
    message: HttpStatus.getStatusText(statusCode),
    status: statusCode
  };
};

const createErrorMessage = (err) => {
  const statusCode = getStatusCode(err.statusCode);
  return `${statusCode}: ${err.message}`;
};

const deleteErrorStack = (env, err) => {
  if (isProdEnv(env)) delete err.stack;
};

const getLogLevel = (statusCode) => {
  return isServerError(statusCode) ? 'error' : 'warn';
};

const logErrors = () => {
  return (err, req, res, next) => {
    const statusCode = getStatusCode(err.statusCode);
    const logLevel = getLogLevel(statusCode);
    const errMessage = createErrorMessage(err);
    const extraData = req.path;

    log[logLevel](errMessage, err, extraData);

    next(err);
  };
};

const handleErrors = () => {
  return (err, req, res, next) => {
    const statusCode = getStatusCode(err.statusCode);
    const errorContext = createErrorContext(statusCode);

    deleteErrorStack(req.app.get('env'), err);
    res.status(statusCode).json(errorContext);
  };
};

module.exports = {
  logErrors: logErrors,
  handleErrors: handleErrors
};


texts: Passing many configuration options to Java code formatter

texts:
I use the DefaultCodeFormatter of the core Java package to prettify my code. Unfortunately I want to look my code very different from the default settings. That why, I have to explicitly define a lot of values:

public static String makeSourceCodeLookingMoreSexy(String source) {
        Map<String, String> options = DefaultCodeFormatterConstants.getEclipseDefaultSettings();

        options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_6);
        options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_6);
        options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_6);
        options.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ENUM_CONSTANTS, DefaultCodeFormatterConstants.createAlignmentValue(true, DefaultCodeFormatterConstants.WRAP_ONE_PER_LINE, DefaultCodeFormatterConstants.INDENT_ON_COLUMN));
        options.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_PARAMETERS_IN_METHOD_DECLARATION, DefaultCodeFormatterConstants.createAlignmentValue(false, DefaultCodeFormatterConstants.WRAP_NO_SPLIT, DefaultCodeFormatterConstants.INDENT_DEFAULT));
        options.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_PARAMETERS_IN_METHOD_DECLARATION, DefaultCodeFormatterConstants.createAlignmentValue(false, DefaultCodeFormatterConstants.WRAP_NO_SPLIT, DefaultCodeFormatterConstants.INDENT_DEFAULT));
        options.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_THROWS_CLAUSE_IN_METHOD_DECLARATION, DefaultCodeFormatterConstants.createAlignmentValue(false, DefaultCodeFormatterConstants.WRAP_NO_SPLIT, DefaultCodeFormatterConstants.INDENT_DEFAULT));

        final CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(options);

        final TextEdit edit = codeFormatter.format(CodeFormatter.K_COMPILATION_UNIT, source, 0, source.length(), 0, System.getProperty("line.separator"));

        IDocument document = new Document(source);
        try {
            edit.apply(document);
        } catch (MalformedTreeException e) {
            e.printStackTrace();
        } catch (BadLocationException e) {
            e.printStackTrace();
        } catch (NullPointerException e) {
            e.printStackTrace();
        }

        return document.get();
    }

That are not even all options that I want to set. Actually it is way more, maybe about 50. As you can imagine the code gets very long. So is there a better way to do this than I'm actually doing it?


texts: Deserialize with LINQ query code in PageModel or PageView

texts:
Today I do something like this, where I get, select and sort some data and pass it on to the view (which loads a partial view) as a list of custom objects

Page model

public PagesSettings PageSettings { get; set; }
public List<Reportage> itemListReportages { get; set; }

public void OnGet()
{
    PageSettings = (PagesSettings)HttpContext.Items["pagesettings"];

    if (PageSettings == null)
    {
        return;
    }

    var fileContent = _fileService.GetFileContentCached(PageSettings.DataFile);

    if (fileContent?.Length > 0)
    {
        itemListReportages = (JsonConvert.DeserializeObject<List<Reportage>>(fileContent))
            .Where(l => l.Published)
            .OrderBy(l => l.Name)
            .ToList();
    }
}

Page view

@page
@model ListReportagesModel

@if (Model.PageSettings?.Data?.Count > 1 && Model.PageSettings.Data[1].Html?.Length > 0)
{

            @if (Model.PageSettings.Data[1].Values?.Length > 0)
                {
                    @Html.Raw(String.Format(Model.PageSettings.Data[1].Html, Model.PageSettings.Data[1].Values))
                }
                else
                {
                    @Html.Raw(@Model.PageSettings.Data[1].Html)
                }

}

        <partial name="/Pages/Partial/@Model.PageSettings.PartialView" />

Partial view

@for (var i = 0; i < Model.itemListReportages.Count; i++)
{

            @Model.itemListReportages[i].Text

}

It now start to become a lot of PageModel's, so I started to wonder where the proper location to run C# code is. In above the data loading/select/sort is done in the PageModel and the rest in the view's.

I was planning to split it up like below instead, moving the select/sort from Pagemodel to the partial view, and with that reduce the amount of Pagemodel's and make it simpler to add data by simply create a new class + data file + partial view.

The question, which way is recommended (if any), perform select/sort in PageModel or in View?

Page model

public PagesSettings PageSettings { get; set; }
public string fileContent { get; set; }

public void OnGet()
{
    PageSettings = (PagesSettings)HttpContext.Items["pagesettings"];

    if (PageSettings == null)
    {
        return;
    }

    fileContent = _fileService.GetFileContentCached(PageSettings.DataFile);
}

Partial view

if (fileContent?.Length > 0)
{
    var itemListReportages = (JsonConvert.DeserializeObject<List<Reportage>>(fileContent))
        .Where(l => l.Published)
        .OrderBy(l => l.Name)
        .ToList();

    @for (var i = 0; i < Model.itemListReportages.Count; i++)
    {

                @Model.itemListReportages[i].Text

    }
}


texts: Is there a more efficient way of doing function 'which' in R?

texts:
The data file is here.

Here are my codes:

> ftable=read.table(file.choose())
> start.time=Sys.time()
> 1-length(which(ftable==1))/sum(ftable)
[1] 0.12
> end.time=Sys.time()
> end.time-start.time
Time difference of 0.004880905 secs

I understand that 0.00488 secs are not a lot. But I have to repeatedly do this calculation over different and larger tables. I am wondering if the function 'which' can be replaced by a more efficient one.

Thanks in advance!

Note: This piece of codes is to calculate the percentage of singletons in ftable. If there is a more efficient way, please let me know. Thank you!


texts:
I have a JSON response from the Facebook API, which contains the app requests of certain Facebook apps via FB.API('/me/apprequests') (using Graph API 2.0, Facebook SDK for Unity 6.0).

There are 3 things I want to collect from the requests:

The response data (a JSON string)
A texture (in Texture2D class)
Status of loading texture (in bool)

Here is how I achieve it:

private Dictionary<int, JsonData> requestData;
private Dictionary<int, bool> requestLoadStatus;
private Dictionary<int, Texture2D> requestTexture;

void Start() {
    loaded = false;
    requests = new ArrayList();
    requestData = new Dictionary<int, JsonData>();
    requestLoadStatus = new Dictionary<int, bool>();
    requestTexture = new Dictionary<int, Texture2D>();
    GetFriendRequests();
}

protected void GetFriendRequests() {
    if(FB.IsInitialized) {
        FB.API("/me/apprequests", Facebook.HttpMethod.GET, GetFriendRequestsCallback);
    } else {
        InitFacebook();
    }
}

private void GetFriendRequestsCallback(FBResult result) {
    if(result != null) {
        if (!String.IsNullOrEmpty(result.Error)) {
            // Failed Case
            lastResponse = "Get Friend Request Callback: Error Response:\n" + result.Error;
        } else if (!String.IsNullOrEmpty(result.Text)) {
            // Success Case
            lastResponse = "Get Friend Request Callback: Success Response:\n" + result.Text;
            JsonData datas = JsonMapper.ToObject(result.Text);
            Debug.Log(datas["data"]);
            for(int i = 0; i < datas["data"].Count; i++) {
                requestData.Add(i, JsonMapper.ToObject((string)datas["data"][i]));
                requestLoadStatus.Add(i, false);
                requestTexture.Add(i, new Texture2D(50, 50));
                StartCoroutine(LoadUserProfilePic(i));
            }
        }
        Debug.Log(lastResponse);
    } else {
        Debug.Log("Get Friend Request Callback: Response is empty");
    }
}

Where InitFacebook() is simply calling FB.Init(). Note that I used LitJson as a JSON parser.

How can I simplify this code? Using 3 Dictionarys seems to be clumsy. Will there be a benefit if I use a custom class to wrap them?


texts: Replacing thrown exception with more descriptive exception during file I/O

texts:
My code needs to open a specific file. Under certain (expected, but rare) conditions, the code will get handed a file path to a file that does not exist -- the path is expected to be otherwise valid though.

If I get handed a path to a file that does not exist, this is an explicit failure scenario that could be reported to the logfile differently, with a more explicit exception message, thereby probably increasing the chance of analysing the error appropriately.

I have solved this by catching the thrown IOException and checking its type. If it is a FileNotFoundException or DirectoryNotFoundException I throw an InvalidOperationException with a message relating to how my code runs.

Here are the relevant parts from my code:

    public MyUpdater(...)
    {
        _reader = ...;
        try {
            // Open immediately to ensure we can read the file:
            _reader.AccessForReading();
        } catch (IOException ioex) {
            var extype = ioex.GetType();
            if (extype == typeof(FileNotFoundException) || extype == typeof(DirectoryNotFoundException)) {
                // This is an explicit failure scenario in which case we do not want the raw IOException to escape
                throw new InvalidOperationException("File to update does not exist! It may have been moved because of foobar reason xyz ...", ioex);
            } else {
                // re-throw all other
                throw;
            }
        }

Essentially, I'd like to replace the exception message with the likely reason for the error, so that support personnel looking at the logfile can quickly identify the problem.

Does this solution make sense?

Note:

This is not example code. It's actually copied verbatim out of the source, that's the very reason for the ellipsis, because that stuff there isn't relevant for the question.
By "injecting" I do mean what you see. The exception message (the one above is made up for posting it here) just gives a hint as to what may be the likely cause. This message should end up being the first related entry in the log file, thereby giving support personnel a first hint as to what may have gone wrong.)
One point that I'm not sure about specifically is using InvalidOperationException -- Since it's only for logging, the exception type is really not all that important, but should one create a separate exception type for such a scenario?


texts:
I want to be able to find a solution to run the following code in a much faster fashion (ideally something like dataframe.apply(func) which has the fastest speed, just behind iterating  rows/cols- and there, there is already a 3x speed decrease). The problem is twofold: how to set this up AND save stuff in other places (an embedded function might do that). I know the pandas function for ROLLING window regression is already optimized to its limit but I was wondering how to get rid of the loop cycle and other \$O(N^k)\$ I might have missed.

Any help is greatly appreciated

import pandas as pd
import numpy as np
periods = 1000
alt_pan_fondi_prices = pd.DataFrame(np.random.randn(periods ,4),index=pd.date_range('2011-1-1', periods=peridos), columns = list('ABCD'))
indu = pd.DataFrame(np.random.randn(periods ,4),index=pd.date_range('2011-1-1', periods=peridos), columns = list('ABCD'))
indu.columns = list('ABCD')

# some names to be used later
cols = ['fund'] + [("bench_" + str(i)) for i in list('ABCD')]
for item in alt_pan_fondi_prices.columns.values:

    to_infer = alt_pan_fondi_prices[item].dropna()
    indu = indu.loc[to_infer.index[0]:, :].dropna()
    dfBothPrices = pd.concat([to_infer, indu], axis=1)
    dfBothPrices = dfBothPrices.fillna(method='bfill')
    dfBothReturns = dfBothPrices.pct_change()
    dfBothReturns.columns = cols
    mask = cols[1:]

    # execute the OLS model
    model = pd.ols(y=dfBothReturns['fund'], x=dfBothReturns[mask], window=20)

    # I then need to store a whole bunch of stuff (alphas / betas / rsquared / etc) but I have this part safely taken care of


texts:
I have a task to write a concurrent version of quicksort algorithm, to speed it up. This is what I wrote:

template <class T>
void quicksort::sort(T *data, int len) {
    thread_count = 0;
    int p = 0;
    int r = len - 1;
    _sort(data, p, r);
}

template <class T>
void quicksort::_swap(T *data, int first, int second) {
    auto temp = data[first];
    data[first] = data[second];
    data[second] = temp;
}

template <class T>
void quicksort::_sort(T *data, int p, int r) {
    thread_count++;
    if (p < r) {
        auto q = _partition(data, p, r);
        if (thread_count >= thread_limit) {
            _sort(data, p, q - 1);
            _sort(data, q + 1, r);
        } else {
            std::thread lower(&quicksort::_sort<T>, this, data, p, q - 1);
            std::thread upper(&quicksort::_sort<T>, this, data, q + 1, r);
            lower.join();
            upper.join();
        }
    }
    thread_count--;
}

template <class T>
int quicksort::_partition(T *data, int p, int r) {
    auto first = data[p];
    auto last = data[r];
    T pivot = 0;
    // Median of three
    auto middle = data[(p + 2) / 2];
    if (first > middle) {
        if (middle > last) {
            pivot = middle;
        } else {
            pivot = last;
        }
    } else {
        if (middle > last) {
            pivot = std::max(first, last);
        } else {
            pivot = middle;
        }
    }
    auto i = p - 1;
    for (auto j = p; j < r; ++j) {
        if (data[j] <= pivot) {
            _swap(data, ++i, j);
        }
    }
    _swap(data, i + 1, r);
    return i + 1;
}

Those are methods from class quicksort, which also has std::atomic<int> thread_limit and std::atomic<int> thread_count.

So f.e I cap the thread limit to f.e maximum 1000 threads.

The part I am particularly not sure of is the part in _sort() function. Basically, I don't know if my way of starting those threads and then joining them is the right way to do it.

I have a benchmark method, and I benchmarked my quicksort implementation like this: I took the average of 100 benchmarks for every thread_limit from 200 to 2000, stepping by 100 (so 200, 300, 400) and for single-threaded quicksort of mine. And this was for 1.000.000-sized arrays (every time I was creating a new one - this overhead was not measured). The result was shocking for me. It occurred, that of course, even with 2000 threads it was faster than with only 1 thread, but still, the fastest one was with thread_limit = 200. I thought that the optimum would be somewhere nearer the middle.

Sometimes f.e 700 threads were faster than 600 threads, but still, the general rule was: the more threads, the slower.

Do you guys see any flaws in my code or in my understanding of the benchmark I made?


texts:
The code below is my shot at trying to do monte carlo integration of the function sin(x) from 0 to pi in Python 3.
I sample randomly 10000 times a floating number from 0 to pi and do the standard procedure of the monte carlo integration and also take the average of ten results for an even better approximation,i was wondering if there is anything i can do to optimize my code from a time or accuracy standpoint.
import random
import math

def func(n):
    return math.sin(n)

integralres=0

for t in range(10):
    resf=0
    for i in range(10000):
        u=random.uniform(0,math.pi)
        resf+=func(u)
    integralres+=(math.pi)*resf/10000

print(integralres/10)