texts: Interface prototype that draw on windows forms chart

texts:
now I'm working on some code that will control external FPGA and draw some data on WF chart. It has complete functionality now, but I'm interested in better practices of doing interfaces.

I have UI thread that spawns control thread --> control thread in his order spawns computing thread (now it just calculates points on circle) and GUI_refresher thread that change text of label's and draw points on chart;

GUI_refresher have an option just draw one point or trace envelope; controls by bool flag

I'm interested in all that you can recommend from code arrangement and naming style to better ways of coding itself)

It hasn't any architecture, it's just prototype where I'm learning how to do interfaces. If u can help with it too, I will appreciate it.

the full solution for those who want to run it himself:

solution

the code sample in that improvement I'm most interested:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Concurrent;
using System.Threading;
using System.Timers;

namespace interfaceTest_SD1
{
class doComputingThread
{
    private AutoResetEvent autoevent = new AutoResetEvent(false);
    public delegate void Refresher(string text);
    public readonly ConcurrentQueue<coords> _queue = new ConcurrentQueue<coords>();
    public delegate void _dGuiRefresher(bool flag);
    public static System.Timers.Timer _timer;

    public int numOfCycle { get; set; }

    public struct coords{
         public int X;
         public int Y;
    }

    private coords Coordinate;

    Form1 myform;
    public doComputingThread(Form1 form)
    {
        myform = form;
    }

    public void MainThread()
    {
        bool flag;

        _timer = new System.Timers.Timer();
        _timer.Interval = 10;
        _timer.Elapsed += (sender, e) => { GuiRefresher(true); };
        _timer.Enabled = true;

        for (int i = 0; i < numOfCycle; i++)
        {
            Thread _ComputeThread = new Thread(new ThreadStart(ProducerThread));
            _ComputeThread.Start();
            flag =  i == 0 ?  true : false;
            _timer.Enabled = true;

            _ComputeThread.Join();

            autoevent.WaitOne();
            _timer.Stop();
            _timer.Elapsed -= (sender, e) => { GuiRefresher(true); };
            //_timer.Elapsed += (sender, e) => { GuiRefresher(false); };

        }

    }

    public void ProducerThread()
    {
        int veyerstrasse = 0;
        int statement = 100 ^ 2;

        //populate queue

        int X = 0;
        int Y = 0;

        for (double t = 0; t < 2 * 3.14; t = t + 0.1)
        {
            X = (int)(Math.Cos(t) * 5000);
            Y = (int)(Math.Sin(t) * 5000);

            Coordinate.X = X;
            Coordinate.Y = Y;
            _queue.Enqueue(Coordinate);

        }

    }

    public void GuiRefresher(object flag)
    {

        if (_queue.TryDequeue(out Coordinate))
        {

            Action action = () =>
            {
                myform.label1.Text = Coordinate.X.ToString();
                myform.label2.Text = Coordinate.Y.ToString();
            };
            myform.Invoke(action);

            if ((bool)flag == true)
            {

                action = () =>
                {
                    myform.chart1.Series["Series1"].Points.AddXY(Coordinate.X, Coordinate.Y);

                };
                myform.Invoke(action);

            }

            action = () =>
            {
                myform.chart1.Series["Series2"].Points.Clear();
                myform.chart1.Series["Series2"].Points.AddXY(Coordinate.X, Coordinate.Y);
            };
            myform.Invoke(action);

            Thread.Sleep(40);

        }
        else
        {
            autoevent.Set();
        }

    }

}
}


texts: Optimizing/Cleaning up Python Script for Zipf's Law

texts:
I have attached two scripts which calculate the most popular songs according to zipf's law from the following standard input:

6 3
100 one
50 two
10 three
30 four
5 five
10 six

The first line details the number of songs to be input followed by the number of songs to return. Each line that follows holds the play counts and respective song titles. In the case of a tie, the algorithm must give precedence to the song that appeared earlier on the album.

Both of my attempts to solve are currently not efficient enough. Any help in optimizing one or both of these would be greatly appreciated.

Attempt 1:

import sys
from operator import itemgetter

def main():

    line1 = sys.stdin.readline().split()
    x = int(line1[0])-1
    z = int(line1[1])

    data = ""
    for line in sys.stdin:
        data+=line
    data = data.split()

    while(x >= 0):
        data[2*x]= float(data[2*x]) * (x+1)
        x-=1

    answers = [(data[a+1], data[a]) for a in range(0,len(data),2)]
    answers.sort(key=itemgetter(1), reverse=True)
    p=0
    while(p<z):
        print answers[p][0]
        p+=1

main()

Attempt 2:

import sys
from operator import itemgetter

def main():

    line1 = sys.stdin.readline().split()
    x = int(line1[0])-1
    p = x
    z = int(line1[1])

    data = ""
    for line in sys.stdin:
        data+=line
    data = data.split()

    while(x >= 0):
        data[2*x]= float(data[2*x]) * (x+1)
        x-=1

    y=0
    list_of_lists = []
    while(y<=p):
        list_of_lists.append([data[2*y+1], data[2*y]])
        y+=1
    list_of_lists = sorted(list_of_lists, key=itemgetter(1), reverse=True)

    n=0
    while(n<z):
        print list_of_lists[n][0]
        n+=1

main()

Currently Cprofile reports are indicating that the first script is faster. Thanks!


texts:
I implemented a Trie in C++11, but I am not sure if my usage of struct and std::unique_ptr, and std::unordered_map are correct and idiomatic. Therefore, I hope someone could provide constructive criticism on my code.

My Trie implementation has three simple methods:

void Trie::Insert(const std::string&  word);
bool Trie::StartsWithPrefix(const std::string& prefix);
bool Trie::IsACompleteWord(const std::string& word);

Code

// tries.h
#ifndef TRIE_H_
#define TRIE_H_

#include <vector>
#include <string>
#include <unordered_map>

class Trie {
 public:
  // Trie Constructor
  Trie();
  explicit Trie(std::vector<std::string>);

  // Trie Operations
  void Insert(const std::string&  word);
  bool StartsWithPrefix(const std::string& prefix);
  bool IsACompleteWord(const std::string& word);

 private:
  struct Node {
    bool end_of_word;
    std::unordered_map<char, std::unique_ptr<Node>> children;
    Node(): end_of_word(false) {}
  };
  std::unique_ptr<Node> root;
};

/// ===========================
/// Tries Implementation
/// ===========================

Trie::Trie() {
  root = std::unique_ptr<Node>(new Node());
}

Trie::Trie(std::vector<std::string> words) {
  root = std::unique_ptr<Node>(new Node());
  for (auto w : words)
    Insert(w);
}

void Trie::Insert(const std::string&  word) {
  if (word.empty()) return;
  auto it = root.get();
  for (const char & key : word) {
    int found = it->children.count(key);
    if (!found)
      it->children.emplace(key, std::unique_ptr<Node>(new Node()));
    it = it->children[key].get();
  }
  it->end_of_word = true;
}

bool Trie::StartsWithPrefix(const std::string& prefix) {
  auto it = root.get();
  for (const char & key : prefix) {
    auto found = it->children.count(key);
    if (!found)
      return false;
    it = it->children[key].get();
  }
  return true;
}

bool Trie::IsACompleteWord(const std::string& word) {
  auto it = root.get();
  for (const char & key : word) {
    auto found = it->children.count(key);
    if (!found)
      return false;
    it = it->children[key].get();
  }
  return (it->end_of_word) ? true : false;
}

#endif // TRIE_H_

Test

#define CATCH_CONFIG_MAIN
#include "catch2/catch.hpp"
#include "trie.h"

SCENARIO("Trie operations", "[trie]") {
  GIVEN("A Trie Data Structure") {

    Trie trie({"hello", "test"});

    REQUIRE(trie.StartsWithPrefix("hell") == true);
    REQUIRE(trie.StartsWithPrefix("helli") == false);
    REQUIRE(trie.StartsWithPrefix("test") == true);
    REQUIRE(trie.StartsWithPrefix("ab") == false);

    trie.Insert("ab");
    REQUIRE(trie.StartsWithPrefix("ab") == true);

    REQUIRE(trie.IsACompleteWord("ab") == true);
    REQUIRE(trie.IsACompleteWord("hell") == false);
  }
}


texts: Safely order a list of objects by DateTime

texts:
I have several lists of various objects that need to be ordered by one of their properties that contains a string representation of DateTime, whose property also varies depending on the object.

The main issue with the lists is that the contained objects cannot be guaranteed to be valid DateTime representations and so simply ordering them (using LINQ) would throw exceptions if the property is null or an invalid format.

This means I have to use something like DateTime.TryParse. Here's the class method I wrote to do this:

static List<T> SafelyOrderListByDateTimeDescending<T>(List<T> list, string propertyName)
{
    DateTime value;
    PropertyInfo propInfo;
    string dateTimeString;
    foreach (T obj in list)
    {
        propInfo = obj.GetType().GetProperty(propertyName);
        dateTimeString = propInfo.GetValue(obj, null) as string;
        if (!DateTime.TryParse(dateTimeString, out value))
            propInfo.SetValue(obj, default(DateTime).ToString(), null);
    }

    return list.OrderByDescending(x => DateTime.Parse(x.GetType().GetProperty(propertyName).GetValue(x, null) as string)).ToList();
}

Considering there are many thousands of objects per list it's important that the method is not exceedingly inefficient. It seems to perform well in tests but I'd like some feedback on what could be improved or even if there's a better way.

I've tried some really neat ways of doing something similar but they all exclude the object if the value isn't successfully parsed whereas I still need the object.


texts: Counting all words from text and ordering it

texts:
I did it and it works as intended but it seemed very inefficient. I made the string a list, then I standardize it (removing accents, commas and points). After that, I count every word using a dictionary, then sort it in a loop.

Is there a better way to do it?

Edit - Thanks to you both, with your tips I managed to improve the code a lot, it's much faster and efficient now: https://pastebin.com/EN74daBG

import unidecode
from operator import itemgetter

def word_counter(text):
    counter = {}
    for word in text.lower().replace(",", "").replace(".", "").split():
        standardize = unidecode.unidecode(word)
        if standardize.isalnum():
            counter.setdefault(standardize, 0)
            counter[standardize] += 1
    for key, value in sorted(counter.items(), key=itemgetter(1), reverse=True):
        print("{} = {}".format(key, value))

word_counter('''text here''')


texts: Looking up the name and event for a ticket

texts:
This code is from my Kohana project. How can I make it more beautiful? Should I use try-catch instead? How could I avoid writing exit() twice?

public function action_index()
{
    $id = $this->request->query('id');
    if (!empty($id)) {
        $ticket = ORM::factory('ticket')
            ->where('id', '=', $id)
            ->find();
        if (!empty($ticket)) {
            $event = ORM::factory('event')
                ->where('id', '=', $ticket->event_id)
                ->find();
            if (!empty($event)) {
                $this->template->ticket = $ticket->id;
                $this->template->name = $ticket->name;
                $this->template->event = $event->title;
            } else {
                exit();
            }
        } else {
            exit();
        }
    }
}


texts: Loading animation refactoring to allow for more flexibility

texts:
I finished part of a module this afternoon that is responsible for creating and showing a loading animation as an item is removed from a cart. However, it isn't the most efficient way and could use some refactoring. If anyone has any ideas on how, that'd be great. I also have another JavaScript file that is dependent on this file, so moving some of the parts over is an option. I am calling these functions in a removing button click event.

  cartGroupItem.prototype.showLoader = function () {
    var btnRemove = document.querySelector('.cartGroupItem__remove');
    this.showLoading(btnRemove);
  };

  cartGroupItem.prototype.hideLoader = function () {
    var btnRemove = document.querySelector('.cartGroupItem__remove');
    this.hideLoading(btnRemove);
  };

  cartGroupItem.prototype.showLoading = function (btnDisable) {
    var element = document.querySelector('.loading-widget__spinner');
    var spinnerSpecs = { top: '30px', left: '45%', color: 'white', radius: 12, width: 4 };
    var spinner = new Spinner(spinnerSpecs).spin(element);
    document.querySelector('.loading-widget').style.display = 'block';
    document.querySelector('.loading-widget-bg').style.display = 'block';
    if (btnDisable)
      btnDisable.removeAttribute('disabled', '');
  };

  cartGroupItem.prototype.hideLoading = function (btnEnable) {
    document.querySelector('.loading-widget').style.display = 'none';
    document.querySelector('.loading-widget-bg').style.display = 'none';
    if (btnEnable)
      btnEnable.removeAttribute('disabled');
  };

  return cartGroupItem;
});


texts: An array of values. looping many times to calculate mean, closest matching array value, and then finding its index

texts:
I have an array of values. (cols)

I want to find out which index of cols contains the value closest to the mean of cols.

Can I combine these functions so that I'm not repeating loops?

let mean = cols.reduce((prev, curr) => prev + curr) / cols.length;
let closest = cols.reduce((prev, curr) => (curr - mean) < (prev - mean) ? curr : prev);
let meanCol = cols.indexOf(closest);


texts: Two AngularJS controllers for camera actions

texts:
I have the same functionality in two controllers and I want to refactor to place it in a shared method that both controllers can access. I was thinking on creating a separate service called "Camera" and inject that service as a dependecy into both controllers as I'm currently doing with "Login". Is this a good approach?

  .controller('pictureOfTheDamageCtrl', function ($scope, $cordovaCamera, $ionicPopup, $rootScope, $timeout, Login, $ionicLoading) {
    $rootScope.width = document.getElementsByTagName('ion-content')[0].clientWidth;

    $scope.takePicture = function () {
      var options = {
        quality: 75,
        destinationType: Camera.DestinationType.DATA_URL,
        sourceType: Camera.PictureSourceType.CAMERA,
        allowEdit: false,
        targetWidth: 1024,
        targetHeight: 567,
        encodingType: Camera.EncodingType.JPEG,
        popoverOptions: CameraPopoverOptions,
        saveToPhotoAlbum: false
      };

      $cordovaCamera.getPicture(options).then(function (imageURI) {
        $rootScope.damage = "data:image/jpeg;base64," + imageURI;
      }, function (err) {
        $ionicPopup.alert({
          title: 'Camera',
          template: 'Something went wrong: ' + err
        });
      });
    }

  })
  .controller('pictureOfTheVehicleCtrl', function ($scope, $cordovaCamera, $ionicPopup, $rootScope, $timeout, Login, $ionicLoading) {
    $rootScope.width = document.getElementsByTagName('ion-content')[0].clientWidth;

    $scope.takePicture = function () {
      var options = {
        quality: 75,
        destinationType: Camera.DestinationType.DATA_URL,
        sourceType: Camera.PictureSourceType.CAMERA,
        allowEdit: false,
        targetWidth: 1024,
        targetHeight: 567,
        encodingType: Camera.EncodingType.JPEG,
        popoverOptions: CameraPopoverOptions,
        saveToPhotoAlbum: false
      };

      $cordovaCamera.getPicture(options).then(function (imageURI) {
        $rootScope.vehicle = "data:image/jpeg;base64," + imageURI;
      }, function (err) {
        $ionicPopup.alert({
          title: 'Camera',
          template: 'Something went wrong: ' + err
        });
      });
    }

  })


texts:
So I made a socket communication library. And a part of it is IConnection

public enum ConnectionState
{
    NotConnected, Connecting, Connected, Authenticated, Disconnecting, Disconnected
}

public interface IConnection
{
    ConnectionState State { get; }

    event Action Connected;
    event Action Disconnected;
    event Action Authenticated;
    event Action AuthenticationFailed;

    // this two methods are the core of my question
    void OnAuthenticated();
    void OnAuthenticationFailed();

    bool Send(byte[] data);
    void Connect();
    void Close();
}

Of course IConnection provides information about connection state and is able to fire Connected/Disconnected events as it holds Socket. There is no doubt.

Now, users of IConnection would also like to know when it becomes authenticated. For example, server might listen to that events, and once connection is authenticated - send client's initial configuration data. Or client might listen to that events and decide to start communication or retry authentication process.

But. The problem is, authentication process exists in the protocol layer. IConnection has no idea such layer even exists. Protocol layer actually uses IConnection to send serialized to byte[] message to the other party. And for this one particular message kind, protocol layer is able to send data upon connection in ConnectionState.Connected state.

So, for IConnection to be able to change it's state and inform subscribers on auth process i had to implement this two methods

void OnAuthenticated();
void OnAuthenticationFailed();

Which are, obviously called from protocol layer authentication process code.

I feel like I'm doing something wrong here. And since i work alone, any thoughts will be much appreciated.


texts: Replacing birth year column values with calculated age value

texts:
I have a stored procedure that is taking more than 2 hours to complete and I noticed the below SQL is taking close to 25 mins for updating a million+ values.  There are multiple steps like this and all added up takes around 2+ hours to finish.

All it is doing is taking a column value i.e. birth_year and replacing it with age i.e. getdate() - birth_year.

SELECT     id,
           birth_month,
           birth_year
INTO       #birth_date
FROM       table_name
WHERE      birth_year IS NOT NULL

UPDATE     table_name
SET        birth_year = ( YEAR(GETDATE()) - bd.birth_year )
FROM       #birth_date bd
INNER JOIN table_name ap ON ap.id= bd.id
WHERE      bd.birth_month <= MONTH(GETDATE())
           OR bd.birth_month IS NULL

UPDATE     table_name
SET        birth_year = ( YEAR(GETDATE()) - bd.birth_year - 1 )
FROM       #birth_date bd
INNER JOIN table_name ap ON ap.id= bd.id
WHERE      bd.birth_month > MONTH(GETDATE())

I replaced the above SQL with the below SQL and now instead of 25 mins, the below SQL takes less than a minute. I've verified the results and all seems good.  Is there anything that I am missing? Is there a better or alternate way?

UPDATE  table_name
SET     birth_year = case
                        when birth_month <= MONTH(GETDATE())  OR birth_month IS NULL then (YEAR(GETDATE()) - birth_year)
                        when birth_month > MONTH(GETDATE()) then ( YEAR(GETDATE()) - birth_year - 1 )
                        else null
                     end
FROM    table_name


texts:
Context
I was solving a task on CodeSignal here
You can view the task on the link I provided above.
I have also put the task at the bottom of the post.
Code
def check(matrix, num):
    for arr in matrix:
        for number in arr:
            if number != num:
                return True
    return False

def spiralNumbers(n):
    array = []
    array_dup = []
    for i in range(n):
        arr1 = []
        for j in range(n):
            arr1.append(j)
        array.append(arr1)

    for i in range(n):
        arr1 = []
        for j in range(n):
            arr1.append(j)
        array_dup.append(arr1)

    selected_row, selected_num_pos = 0, -1
    count = 1
    run = True
    while run:
        its_neg = False
        for i in range(selected_num_pos+1, n):
            if array_dup[selected_row][i] == -1:
                its_neg = True
                break
            array[selected_row][i] = count
            array_dup[selected_row][i] = -1
            count += 1
        if its_neg:
            selected_num_pos = i-1
        else:
            selected_num_pos = i

        its_neg = False
        for i in range(selected_row+1, n):
            if array_dup[i][selected_num_pos] == -1:
                its_neg = True
                break
            array[i][selected_num_pos] = count
            array_dup[i][selected_num_pos] = -1
            count += 1
        if its_neg:
            selected_row = i-1
        else:
            selected_row = i

        its_neg = False
        for i in range(selected_num_pos-1, -1, -1):
            if array_dup[selected_row][i] == -1:
                its_neg = True
                break
            array[selected_row][i] = count
            array_dup[selected_row][i] = -1
            count += 1
        if its_neg:
            selected_num_pos = i+1
        else:
            selected_num_pos = i

        its_neg = False
        for i in range(selected_row-1, -1, -1):
            if array_dup[i][selected_num_pos] == -1:
               its_neg = True
               break
            array[i][selected_num_pos] = count
            array_dup[i][selected_num_pos] = -1
            count += 1
        if its_neg:
            selected_row = i+1
        else:
            selected_row = i

        run = check(array_dup, -1)

    return array

Question
The Code I wrote works without any error and returns the expected output, but the code seems a bit long for this problem. I wanted to know how can I make this code shorter and more efficient?
The Task


texts: Interfacing with instruments using pyvisa

texts:
I'm new to Python, and after running through some introductory exercises I wrote a class definition for working with some hardware, specifically a Signal Recovery model 7230 lockin amplifier.  In the interests of learning good habits from the beginning, I've tried to write "good code" that is properly documented, follows good practices and so forth.

""" Module for  interfacing with Signal Recovery 7230 lock-in amplifier.

A substantial ammount is currently not implmented. It will not work with
a lockin which is configured for double demodulation. If you get a lot
of timeouts, that might be why.

It is also currently missing any way to configure the lockin. Use the
web interface. It does read and expose the status bytes, but at the moment
there is no checking of their content.

@author: Jack Barraclough
"""

import pyvisa

class WrongInstrumentError(Exception):
    """The wrong instrument is connected

    A connection was successfuly established, and the instrument responded
    to a request to identify itself, but the ID recieved was wrong.
    Probably the instrument at the given VISA identifier is not the one
    you wanted.
    """
    pass

class Instrument:
    """Represents a Signal Recovery Model 7230 lock-in amplifier

    Note that several instances can be created representing the same
    instrument. Be careful.
    Instance variables:
      pyvisa -- a pyvisa object for the connection, avoid using.
      status_byte -- status byte after the last read operation.
      overload_byte -- overload byte after the last read operation.
    Methods:
      read, write -- Read and write strings to/from the instrument.
      get_X, get_Y, get_R, get_T -- get measurements from the instrument.
      get_noise -- get the estimated measurement noise
    """

    def __init__(self,visaName):
        """ Connects to the lockin amplifier

        Connects to the lockin, and checks that the lockin is present and
        correct. An error will be raised if the lockin is not there, or
        if it fails to identify itself as a model 7230
        Arguments:
          visaName -- A Visa Resource ID, like 'TCPIP0::10.0.0.2::50000::SOCKET'
        """
        rm = pyvisa.ResourceManager()
        self.pyvisa = rm.open_resource(visaName)
        self.pyvisa.read_termination='\00'
        self.write('ID') # uses this not '*IDN?'
        resp = self.read()
        if resp != '7230':
            raise WrongInstrumentError(
                'Wrote "ID" Expected "7230" got "{}"'.format(resp))

    def write(self,string):
        """Write string to the instrument."""
        self.pyvisa.write(string)

    def read(self):
        """Read string from the instrument. Also sets status bytes"""
        # replies seem to be: response LF NUL then two status bytes
        resp = self.pyvisa.read().strip()
        status = self.pyvisa.visalib.read(self.pyvisa.session,2)[0]
        self.status_byte = status[0]
        self.overload_byte = status[1]
        return resp

    def get_X(self):
        """Get X value from the instrument, returns a float"""
        self.write('X.')
        return float(self.read())

    def get_Y(self):
        """Get Y value from the instrument, returns a float"""
        self.write('Y.')
        return float(self.read())

    def get_R(self):
        """Get R value from the instrument, returns a float"""
        self.write('MAG.')
        return float(self.read())

    def get_T(self):
        """Get theta value from the instrument, returns a float"""
        self.write('PHA.')
        return float(self.read())

    def get_noise(self):
        """Get Y noise from the instrument, in v/sqrtHz, returns a float"""
        self.write('NHZ.')
        return float(self.read())

I'm interested in general feedback but also have some specific questions:

Am I using exceptions correctly?
The pyvisa resource manager has a rm.close() method. By not calling it, am I leaking something? Will the garbage collector deal with it? Is it bad form to leave it to the garbage collector? Should I clear it up with a destructor somehow?


texts: Configuring Constructor Parameters and Unit Testing

texts:
Currently working on writing out some test for an iOS enterprise application.  My concern is in this set up of my overloaded constructor.

- (id)init
{
    CRMHttpClient *client = [CRMHttpClient sharedClient];
    NSOperationQueue *queue = [NSOperationQueue new];
    UIApplication *application = [UIApplication sharedApplication];
    return [self initWithCRMHttpClient:client operationQueue:queue application:application];
}

- (id) initWithCRMHttpClient:(CRMHttpClient *)client operationQueue:(NSOperationQueue *)operationQueue application:(UIApplication *)application
{
    self = [super init];
    if(self)
    {
        self.client = client;
        self.queue = operationQueue;
        self.queue.name = [[NSBundle.mainBundle bundleIdentifier] stringByAppendingString:@".CRMDataProcessingQueue"];
        self.queue.maxConcurrentOperationCount = 2;
        self.application = application;
        self.backgroundTask = UIBackgroundTaskInvalid;
    }

    return self;
}

-(id)init is called by the other classes in the project. -(id)initWithCRMHttpClient:... is called in my test class so I can inject my mocks to unit test my class. (I know I could use partial mocks for the singleton classes, but I prefer to explicitly inject the class)
What bothers me about this approach is the few lines of code where I'm configuring my NSOperationQueue's name and operation count.  This requires me to write this up in my -(void)setup method of my testing class.

- (void)setUp
{
    [super setUp];
    self.mockCRMHttpClient = [OCMockObject mockForClass:[CRMHttpClient class]];
    self.mockOperationQueue = [OCMockObject mockForClass:[NSOperationQueue class]];
    [[self.mockOperationQueue stub] setName:OCMOCK_ANY];
    [[self.mockOperationQueue stub] setMaxConcurrentOperationCount:2];
    self.mockApplication = [OCMockObject mockForClass:[UIApplication class]];
}

I need to stub out the calls for those set up methods so that I don't get failed calls.  I my options are:

Leave it as is, stubbing in the -setup
Configure the operation queue in the -init method instead.
Create a nice mock for mockOperationQueue
Am I forgetting something?

I'm unsure of which approach to take.


texts:
I'm using bluebird for promises in my Node/Express application and wrote an API call in which the user passes in a JSON Web Token that contains their user information, and then I decode the token, and pull up the events that user should see based off their userId.

If anyone could give me advice on how I can clean this code up while also using the proper promise practices, that would be great.

/routes.js

app.post('/api/events/', require('./views/api/index').events);

/views/api/index.js

var B_Promise = require('bluebird');
var jwt = require('jsonwebtoken');

exports.events = function(req, res) {
  var results = {};
  var errors = [];

  var validateEmptyFields = function() {
    return new B_Promise(function(resolve, reject) {
      var token = req.body.token || req.param('token') || req.headers['x-access-token'];
      if (!token) {
        return reject('Please provide the token parameter');
      }

      resolve(token);
    });
  };

  var getUser = function(token) {
    return new B_Promise(function(resolve, reject) {
      jwt.verify(token, req.app.config.api.secret, function(err, decoded) {
        if (err) {
          return reject(err);
        }

        resolve(decoded);
      });
    });
  };

  var getEvents = function(user) {
    return new B_Promise(function(resolve, reject) {
      req.app.db.getConnection(function(err, connection){
        if (err) {
          return reject(err);
        }

        /* jshint multistr: true */
        connection.query('SELECT e.* FROM events e \
          INNER JOIN event_to_groups etg ON e.id=etg.event_id \
          INNER JOIN user_to_groups utg ON utg.group_id=etg.group_id \
          WHERE utg.user_id=?', user.id, function(err, events) {
            if (err) {
              return reject(err);
            }

            results.events = events;
            resolve();
        });

        connection.release();
      });
    });
  };

  validateEmptyFields()
    .then(getUser)
    .then(getEvents)
    .catch(function(err) {
      errors.push(err);
    })
    .finally(function() {
      res.json({results: results, errors: errors});
    });
};


texts: Download Fasta protein sequences as a file from the selected categories

texts:
I have the following function in Django to download the Fasta sequences (protein) through the selected categories as a file. This works well. I would like to improve the following script.

import textwrap
from io import StringIO
from django.shortcuts import render
from django.http import HttpResponse
from database.models import PesticidalProteinDatabase

def download_data(request):
    """List the categories for download."""

    categories = \
        PesticidalProteinDatabase.objects.order_by(
            'name').values_list('name').distinct()
    category_prefixes = []
    for category in categories:
        prefix = category[0][:3]
        if prefix not in category_prefixes:
            category_prefixes.append(prefix)

    context = {
        'proteins': PesticidalProteinDatabase.objects.all(),
        'category_prefixes': category_prefixes
    }
    return render(request, 'database/download_category.html', context)

def download_category(request, category=None):
    """Download the fasta sequences for the category."""

    context = {
        'proteins': PesticidalProteinDatabase.objects.all()
    }
    category = category.title()
    file = StringIO()
    data = list(context.get('proteins'))

    for item in data:
        if category in item.name:
            fasta = textwrap.fill(item.fastasequence, 80)
            str_to_write = f">{item.name}\n{fasta}\n"
            file.write(str_to_write)
        else:
            pass

    if 'All' in category:
        for item in data:
            fasta = textwrap.fill(item.fastasequence, 80)
            str_to_write = f">{item.name}\n{fasta}\n"
            file.write(str_to_write)

    response = HttpResponse(file.getvalue(), content_type="text/plain")
    download_file = f"{category}_fasta_sequences.txt"
    response['Content-Disposition'] = 'attachment;filename='+download_file
    response['Content-Length'] = file.tell()
    return response

The template data

{% extends "database/base.html" %}
{% load static %}
{% block content %}
<!doctype html>

  Download the fasta sequences by categories

      {% for prefix in category_prefixes %}

              {{ prefix }}
        {% endfor %}

           All

{% endblock content %}

The url data

from django.urls import path
from database import views

path('download_data/', views.download_data, name='download_data'),
path('download_category_<str:category>', views.download_category,
 name='download_category'),


texts: Shift one value of array values to the beginning

texts:
Given an integer $value in the range of 1, 2, 3, I want to get an array that starts with $value and proceeds with the remaining two integers from above in arbitrary order. The best I could think of was doing something like this:

switch ($value) {
      case '1':
        $array = [1,2,3];
        break;

      case '2':
        $array = [2,1,3];
        break;

      case '3':
        $array = [3,1,2];
        break;
    }

Is there a shorter and more beautiful way of doing that? Something like that:

$array = push_to_top_of_array($value,[1,2,3]);


texts:
I am a self taught coder taking a Programming Fundamentals class to work towards a degree. It's based on Python, which I'm not as familiar with as other languages. I added error handling like I would in javascript or php to be an overachiever. Professor gave online feedback (online course) not to use try-except, that it is ahead of where we are in the semester, that even when we do get there, she doesn't want it in our code, and that this code is "not well formed" because I used try-except. She gave no feedback as to what she meant and I don't think that sounds right but don't now enough to stand my ground. Googling "well formed python" just give me a bunch of xml.

# main function
def main():

    # @input dist : distance traveled in miles

    # @input guse : gas used traveling dist

    # @output mpg : dist/guse

    # set dist from input "Enter miles traveled: "
    dist = input('Enter distance traveled(in miles): ')
    # make sure dist is a number, and if not print error and restart
    try:
        dist = float(dist)
    except ValueError:
        print('Distance traveled must be a number! You did not travel "' + \
              dist + '" miles!')
        main()
    else:
        # if dist is 0.0, print error and restart
        try:
            x = 1 / dist
        except ZeroDivisionError:
            print('Miles traveled cannot be 0!')
            main()
        else:
            # set guse from input "Enter gas used in gallons: "
            gcon = input('Enter gas consumed traveling(in gallons): ')

            # make sure guse is a number, and if not print error and restart
            try:
                gcon = float(gcon)
            except ValueError:
                print('Gas consumed must be a number. You did not use "' + \
                          gcon + '"" gallons of gas!')
                main()
            else:

                # set mpg to dist divided by guse
                # if guse is 0.0, print error and restart
                try:
                    mpg = dist / gcon
                except ZeroDivisionError:
                    print('Gas consumed can not be 0!')
                    main()
                else:

                    # return(print) mpg
                    print('Average miles per gallon(mpg):', format(mpg, '5.2f'))

                    # print request for enter keypress to end
                    input('press enter to continue')

# Call main function
main()


texts:
I've got a simple single page web app that's build with React. It fetches data from two different APIs, when they have been successfully retrieved, and the promise is resolved, it does some simple calculations to work out the GBP/Bitcoin exchange rate.

It's been a while since I've build anything with React, and it would be great if you would be willing to give it a run through to see if I'm doing anything silly or outdated.

I'd also like to be able to flip the exchange comparisons. By default it compares how many bitcoin you could buy for one GBP, but I would like to be able to toggle a button to then show how many GBP could be bought with one bitcoin. I can't think of a clean way to do it?

app.js

import React, { Component } from 'react';
import Conversion from './Conversion';

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {};
  }

  render() {
    return (

          <Conversion />

    );
  }
}

export default App;

conversion.js

import React, { Component } from 'react';
import axios from 'axios';
var NumberFormat = require('react-number-format');

class Conversion extends Component {
  constructor(props) {
    super(props);

    this.state = {
        isReversed: false
    };

    this.requestData = this.requestData.bind(this);
    this.handleClick = this.handleClick.bind(this);
  }

  componentDidMount() {
    this.requestData();
  }

  requestData() {
    axios.all([this.getX(), this.getY()])
      .then(axios.spread( (xVal, yVal) => {
        const x = xVal;
        const y = yVal.data.bpi.USD.rate_float;
        const xOfy = this.xOfy(x, y);
        const yOfX = this.yOfX(y, x);
        const timeStamp = yVal.data.time.updateduk;
        this.setState({timeStamp, x, y, xOfy, yOfX});
      }));
  }

  getX() {
    /* Currency/USD */
    return axios.get('https://openexchangerates.org/api/latest.json?app_id=#APP_KEU#');
    return 0.7
  }

  getY() {
    /* Bitcoin/USD*/
    return axios.get('https://api.coindesk.com/v1/bpi/currentprice/XBT.json');
  }

  /* 1 unit of X: #Y can be bought */
    xOfy(unit, value) {
        return 1 / (unit * value);
    }

    /* 1 unit of Y: #X can be bought */
    yOfX(unit, value) {
      return unit * value;
    }

    handleClick () {
        this.setState(prevState => ({
      isReversed: !prevState.isReversed
    }));
    }

  render() {
    /* Format Values */
    let xOfy = <NumberFormat value={this.state.xOfy} displayType={'text'} decimalPrecision={5} thousandSeparator={true}  />
    let yOfX = <NumberFormat value={this.state.yOfX} displayType={'text'} decimalPrecision={2} thousandSeparator={true}  />

    return (

{this.state.timeStamp} refresh

GBP/XBT:Cur

1 GBP is worth:

{xOfy} BTC

@ £{yOfX} per BTC

            <button onClick={this.handleClick}>
                    {this.state.isReversed ? true : false}
                    Reverse
                  </button>

    );
  }
};

export default Conversion;


texts: Checking for balanced brackets in JavaScript

texts:
I'm wrote a simple function isBalanced which takes some code and returns true if the brackets in the code are balanced and false otherwise:

function isBalanced(code) {
    var length = code.length;
    var delimiter = '';
    var bracket = [];
    var matching = {
        ')': '(',
        ']': '[',
        '}': '{'
    };

    for (var i = 0; i < length; i++) {
        var char = code.charAt(i);

        switch (char) {
        case '"':
        case "'":
            if (delimiter)
                if (char === delimiter)
                    delimiter = '';
            else delimiter = char;
            break;
        case '/':
            var lookahead = code.charAt(++i);
            switch (lookahead) {
            case '/':
            case '*':
                delimiter = lookahead;
            }
            break;
        case '*':
            if (delimiter === '*' && code.charAt(++i) === '/') delimiter = '';
            break;
        case '\n':
            if (delimiter === '/') delimiter = '';
            break;
        case '\\':
            switch (delimiter) {
            case '"':
            case "'":
                i++;
            }
            break;
        case '(':
        case '[':
        case '{':
            if (!delimiter) bracket.push(char);
            break;
        case ')':
        case ']':
        case '}':
            if (!delimiter && bracket.length && matching[char] !== bracket.pop())
                return false;
        }
    }

    return bracket.length ? false : true;
}

The function must not operate on brackets inside strings and comments. I wanted to know if my current implementation will work correctly for all test cases. I also wanted to know whether brackets may be used in any other context beside strings and comments in a language like JavaScript (AFAIK this is not the case).


texts:
The subject is this small project - PHP/MySQL Database wrapper

The three main goals of this projects are:

Easy way to map database columns to object properties
Simple solution to insert or update data
Query builder class which generate sql query (something like ActiveRecord in CodeIgniter)

Some examples:

class User extends stdClass {
    function getName() {
        return $this->first_name . " ". $this->last_name;
    }
}

class Post extends stdClass {

    /**
     * @var User
     */
    public $author;
}

DB::getInstance()->setFetchTableNames(1);
$sql = DB::getInstance()
    ->select("p.*, u.*")
    ->from("posts p")
    ->join("INNER JOIN users u USING(user_id)")
    ->where("u.user_id = ?", $user_id)
    ->orderBy("p.title");

$stmt = $sql->execute();

/* @var Post[] $post_collection  */
$post_collection = array();

while($post = $stmt->fetchInto(new Post, "p")) {
    $post->author = $stmt->fetchIntoFromLastRow(new User, "u");
    $post_collection[] = $post;
}

// Usage
foreach($post_collection as $post) {
    echo $post->author->getName();
}

More examples are available in readme file at GitHub - https://github.com/salebab/database

So, please review this classes and I will be very pleased if you can give me some comment.


texts:
I have the following code for my coupon system. It should work but I'm sure I can optimize it.

Any suggestions would be welcome.

@price_to_pay = @booking_request.guests * @table_offer.price_cents / 100
@remaining = @coupon.current_amount - @price_to_pay
if @remaining > 0
  @coupon.current_amount = @remaining
  @price_to_pay = 0
elsif @remaining = 0
  @coupon.current_amount = 0
  @price_to_pay = 0
elsif @remaining < 0
  @coupon.current_amount = @remaining
  @price_to_pay = @remaining * -1
end
coupon.save


texts: Using mocking to test that a Django view renders as expected

texts:
I have a Django view that ends with this...

return render(request, 'define/see_all.html', context)

I have mocked the render function and

@patch('define.views_func.render')
def test_get_define_see_all_complete(self, mock_render):
    """Testing that the queryset returned properly"""
    things = AllThings(group="Word Editors")
    things.model['definition'].is_complete = True
    things.model['definition'].save()
    t_url = reverse(
        'define:get',
        kwargs={'language': things.model['language']}) \
        + "?action=seeall&complete=True"
    req = self.rf.post(t_url, {"nada": "nada"})
    see_all(req, {'language': things.model['language']})
    self.assertTrue(len(mock_render.call_args[0][2]['words'].object_list) == 1)

I am basically just trying to see that the context that was passed to render contains the appropriate output from the view, but yuck the last line looks like garbage to me and I know I will not understand it if I come back to look at it later. Is there a better way to do this?


texts: php script to retrieve enum values from sql table

texts:
I have an sql table with multiple fields and 4 of them are enums. I wrote a script that runs thought the table and retrieve the enums and put them in a 2 dimension array.

Unfortunately this script is extreamly slow and I can't fix it.

<?php

require_once('mySQL_Connect.php');

$con = ConnectToDataBase();
if ($con == false)
{
    //data returned will be null
    exit;
}

$db = 'courses_db';
$table = 'courses';

$fields = array(
'training_field',
'speciality_field',
'type',
'language');

$enums = array();

foreach ($fields as $colomn) {
$sq1 = "SELECT
            column_type
        FROM
            information_schema.columns
        WHERE
            table_schema = '$db'
        AND
            table_name = '$table'
        AND
            column_name = '$colomn'";
$query =  mysqli_query($con,$sq1);

$stack = array();
$i = 0;
$stack[$i]=$colomn;
if ($fetch = mysqli_fetch_assoc($query) )
{
    $enum = $fetch['column_type'];
    $off = strpos($enum,"(");
    $enum = substr($enum, $off+1, strlen($enum)-$off-2);
    $values = explode(",",$enum);

    // For each value in the array, remove the leading and trailing
    // single quotes, convert two single quotes to one. Put the result
    // back in the array in the same form as CodeCharge needs.

    for( $n = 0; $n < Count($values); $n++) {
    $val = substr( $values[$n], 1,strlen($values[$n])-2);
    $val = str_replace("''","'",$val);
    $stack[$i+1]=$val;
    $i++;
    }
}
    // return the values array to the caller
    //echo json_encode( $stack);
    array_push($enums,$stack);
    reset($stack);
}
echo json_encode($enums);
?>


texts:
Is there any way to make this code run faster. I have to compare values in a text file which is written in the format:

1 1.12345

2 0.12345

3 3.45678...

The values are in billions and the code takes weeks to run. As far as I can see, the calculation (subtraction in my case) is the heavy step.

a=open("output.txt","w")

for i in range(1,num_lines+1): #num_lines are the number of lines in the file
    read=linecache.getline('file.txt',i).rstrip('\n') #reads line by line from a text file
    splitted=read.split()   #splits the string "read"

    for k in range(i+1,num_lines+1):
        read1=linecache.getline('just_nodes.txt',k).rstrip('\n')
        splitted1=read1.split()

        alpha_temp=abs(float(splitted[1])-float(splitted1[1])) #heavy step

        alpha=round(alpha_temp,3)

        if (alpha==1.000):
            a.write(str(splitted1[0]) +' '+ str(splitted[0]) + "\n")
a.close()


texts:
I have implemented a solution to flatten an array of multiple levels in Javascript. I am just a newbie so forgive any mistakes. Could someone give me any tips to improve the code?

function streamrollArray(arr) {
    var copyArr = arr;
    var to_return = [];

    while (true) {
        if (copyArr.length <= 0) { return to_return; }
        var head = copyArr[0];
        to_return = to_return.concat(getValues(head));
        //Chop head off since we have got all the values from it.
        copyArr = copyArr.splice(1);
    }
}

function getValues(arr) {
    var copyArr = arr;
    var to_return = [];

    if (!Array.isArray(arr)) {
        return arr;
    }

    while (true) {
        //A head can contain multiples arrays inside it.
        if (copyArr.length <= 0) { return to_return; }
        var head = copyArr[0];
        to_return = to_return.concat(getHead(head));
        copyArr = copyArr.slice(1);
    }
}

function getHead(head) {
    //If its an array we break into it getting the head and keep checking.
    while (Array.isArray(head)) {
        head = head[0];
    }
    return head;
}


texts: Method to return the lowest filename number not being used

texts:
I have a Django site where users upload and have the option to delete files. There will be a lot of files that will have the same logical name so I append numbers to them:

foo/bar/file.mp3 foo/bar/file2.mp3...

I needed a method that would return the lowest file number not currently being used. I have a working example here, but it feels wrong and ugly and I don't know if there is an easier way of doing it. If I ran the method on the examples above, it would return "1" so I know I can make the next file foo/bar/file1.mp3.

def get_num(self, files):
        """function to extract the lowest file number to save as, returns string. Takes a list as input"""
        nums = []
        for f in files:
            # Call the unicode function which should return the string path
            match = re.findall("([\d]+)\.[\w]+", f.__unicode__())
            if match:
                nums.append(int(match[0]))
            else:
                nums.append(0)

        for i in range(len(nums) + 1):
            if i not in nums:
                if i == 0:
                    return ""
                else:
                    return str(i)


texts: How to refactor code to abstract data from a complex object into a single object without using foreach statements

texts:
I am trying to abstract data from a complex and create a EventDto.
And I was able to do it using foreach but the syntax is dreadful.
Is there a better way of writing this code?

 public class EventDtO
 {
    public string Id { get; set; }
    public string Title { get; set; }
    public string CategoryTitle { get; set; }
    public DateTime DateTime { get; set; }
  }

This is the complex object that I am trying to get the data from:

public class RootObject
{
    public List<Event> Events { get; set; }
}

public class Event
{
    public string Id { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public string Link { get; set; }
    public List<Category> Categories { get; set; }
    public List<Geometry> Geometries { get; set; }
}

public class Geometry
{
    public DateTime Date { get; set; }
    public string Type { get; set; }
    public List<object> Coordinates { get; set; }
}

    public class Category
{
    public int Id { get; set; }
    public string Title { get; set; }
}

The mapping relationship I want is

EventDto.Id->Event.Id
EventDto.Title->Event.Title
Event.CategoryTitle->Category.Title
Event.DateTime->Geometry.Date

The Category class will only contain one value, but the geometry.Date can have multiple values.

So the output I want is:

Title           Categories          Date
"Iceberg B42"   Sea and Lake Ice    2020-04-23T14:24:00Z
"Iceberg B42"   Sea and Lake Ice    2017-09-15T00:00:00Z

I am able to get the correct information if I use the following code:

var Event = new List<EventDTO>();
foreach (var con in content.Events)
{
    var data = new EventDTO
    {
        Title = con.Title,
        Id = con.Id
    };

    foreach (var cat in con.Categories)
    {
        data.CategoriesTitle = cat.Title;
    }

    foreach (var geo in con.Geometries)
    {
        data.DateTime = geo.Date;
        Event.Add(data);
    }
}

An example of the JSON:

   {
        "id": "EONET_2881",
        "title": "Iceberg B42",
             "description": "",
        "categories": [
            {
                "id": 15,
                "title": "Sea and Lake Ice"
            }
        ]
        "geometries": [
            {
                "date": "2017-04-21T00:00:00Z",
                "type": "Point",
                "coordinates": [ -107.19, -74.63 ]
            },
            {
                "date": "2017-09-15T00:00:00Z",
                "type": "Point",
                "coordinates": [ -107.11, -74.08 ]
            }
        ]
    }


texts:
I wrote this simple HTML parser in Ruby.

Basically I have a folder that has files. Every file has crawled web pages. The only addition within these files is before the HTML content there is a page metadata, #URL is one of them.

What I am aiming is to have two files:

1)Will contain: currentURL, destinationURL, description

2)Will contain: currentURL, destinationURL, word, count

So if I did that for this current page and assuming that href that I am looking at is:

Stack Overflow

1st file would have

codereview,stackexchange.com, http://stackoverflow.com, Stack Overflow

2nd file would have

codereview,stackexchange.com, http://stackoverflow.com, Stack, 1

codereview,stackexchange.com, http://stackoverflow.com, Overflow, 2

However my code works really slow. It has been longer than an hour and still parsing a file with size of 31MB.

My code is:

beginning_time = Time.now

folder = "allParts/"
extension = "*.dat"
urlMet = "#URL:"
newLine = "\n"
files = Dir[folder+extension]
hrefTag = ""
hrefEnd = ""
firstOutput = "firstOut.txt"
secondOutput = "secondOut.txt"

File.open(firstOutput, 'w') {}
File.open(secondOutput, 'w') {}

countFile = 1

for file in files do
    puts file + "#: "+countFile.to_s
    countFile++
    File.open(file, "r") do |f|
        source = ""
        f.each_line do |line|
            dest = ""
            desc = ""
            if line.include? urlMet
                source = line[/#{urlMet}(.*?)#{newLine}/m, 1]
            end
            if line.include? hrefTag
                dest = line[/#{hrefTag}(.*?)#{qtMark}/m, 1]
                descStIn = line.rindex(descStart)
                descEndIn = line.rindex(hrefEnd)
                desc = line[descStIn+2..descEndIn-1]
            end
            if (source != "" && dest != "")
                occur = Hash.new(0)
                mainEntry = "original-url=\"" + source +
                "\", dest-url=\"" + dest + "\""
                descEntry = ""
                if (desc != nil && desc != "")
                    descEntry = ", desc=\"" + desc + "\""
                    words = desc.split(' ')
                    words.each { |word| occur[word] += 1 }
                end
                firstEntry = mainEntry+descEntry+"\n\n"
                File.open(firstOutput, 'a') { |file|
                    file.write(firstEntry)
                }
                occur.each { |word, occurrance|
                    wordEntry = ", word=\"" + word +
                    "\", count=" + occurrance.to_s
                    secondEntry = mainEntry+wordEntry+"\n\n"
                    File.open(secondOutput, 'a') { |file|
                        file.write(secondEntry)
                    }
                }
            end
      end
    end
end

end_time = Time.now
puts "Time elapsed #{(end_time - beginning_time)} seconds"


texts:
I wrote perfectly working code that reads an image, and then does a kind of threshold on it by zone. If the zone has too much white then it turns all the zone into white, otherwise it turns into black.

The problem is that the code was originally from a C++ source with pointers and all instead of GetPixel() etc. Speed was just instant, nothing more. Now that I rewrote "safely" (no pointers thingy), it take like 20 seconds per images, so instant versus 20 seconds = insane difference.

Could any of you help me improve that small code, even if you use back unsafe pointers code etc? I don't really care about that. The part I absolutely need is speed, and it has to be quick as possible because I will deal with huge images library. Instead of taking 30 minutes, it will literally takes days.

    private Bitmap doti(Bitmap imag)
    {
        int threshold = 7;
        int distance = 9;
        int rows, cols, row, col, x, y, count;
        int dhalf = (distance / 2) + 1;
        int Sqdistance = SQ(distance);
        rows = imag.Height;
        cols = imag.Width;
        Bitmap bitmap0 = (Bitmap)imag.Clone();
        Bitmap bitmap = new Bitmap(cols, rows);
        Bitmap outmap = new Bitmap(cols, rows);

        //convert to grayscale of a single byte
        for (row = 0; row < rows; row++)
            for (col = 0; col < cols; col++)
            {
                Color pixel = bitmap0.GetPixel(col, row);
                byte grayish = (byte)Math.Floor((decimal)(pixel.R + pixel.G + pixel.B) / 3);
                bitmap.SetPixel(col, row, Color.FromArgb(grayish, grayish, grayish));
            }

        //check our threshold to set black or white by checking each pixels in a square defined by distance.
        for (row = 0; row < rows; row++)
            for (col = 0; col < cols; col++)
            {
                count = 0;
                //could optimize here heavily maybe, by only checking inside a circle rather than square+dist.
                for (x = Math.Max(col - dhalf, 0); x < Math.Min(col + dhalf, cols); x++)
                    for (y = Math.Max(row - dhalf, 0); y < Math.Min(row + dhalf, rows); y++)
                    {
                      //if inside the square and pixel color is not black count one more not black pixel
                        if ((Sqdistance > DISTANCE(col, row, x, y)) && ((bitmap.GetPixel(x, y).R) > 0)) //this second condition is killing a lot the speed to begin.
                            count++;
                    }

                //if too much count of not black pixel, set it white.
                if (count >= threshold)
                    outmap.SetPixel(col, row, Color.FromArgb(255, 255, 255));
                else
                    outmap.SetPixel(col, row, Color.FromArgb(0, 0, 0));

            }

        return outmap;
    }

SQ() and DISTANCE():

private int SQ(int a) { return ((a) * (a)); }
private int DISTANCE(int a, int b, int c, int d) { return (SQ(a - c) + SQ(b - d)); }


texts:
In this revision (follow-up of this post) I've fixed reading from stdin, and also tried to minimise the damage caused by typing additional characters. You might want to see my original post on this.

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

/*
 * A very SIMPLE pager that does not require curses. If your curses library
 * is broken, this is practically the only portable one you can easily get.
 */

int
main(
    int argc,
    char *argv[])
{
    FILE *fp;
    FILE *tty;
    int ch;

    if (argc < 2) {
        fp = stdin;
    } else if ((fp = fopen(argv[1], "r")) == NULL) {
        perror(argv[1]);
        return EXIT_FAILURE;
    }

    if ((tty = fopen("/dev/tty", "r")) == NULL) {
        perror("/dev/tty");
        return EXIT_FAILURE;
    }

    while ((ch = fgetc(fp)) != EOF) {
        if (ch == '\n') {
            while (fgetc(tty) != '\n');
        } else {
            putchar(ch);
        }
    }

    if (ferror(fp)) {
        perror(argv[1]);
        return EXIT_FAILURE;
    }

    fclose(fp);
    return EXIT_SUCCESS;
}

Some questions:

How can I avoid having to read from /dev/tty? I know that the device file is available on practically all Unixes, but it won't work on Windows.
Also, I want to avoid dependency on any library except standard C.


texts:
I wrote this program for an assignment, thus I had some requirements as far as the format of the cfg.txt file and some basic other classes that we had to use. Other than that, I am curious if there is a way to drastically simplify this code. Although the program runs fine and produces the desired output, is there a better way to go about writing this? (What initially spurred my concern was the iterateData method. I did not like the code repetition and thought it may be convenient to "reuse" some of it. However, in doing so, I then introduced numerous class variables to prevent from having to pass in 7 instance variables to the method. I can't say that I'm all too thrilled about this...) Regardless, advice and suggestions appreciated.

Thank you.

import java.util.Scanner;
import java.io.*;
import java.util.Random;

public class ContextFreeGrammar {

    private static String nt, useExp = "";
    private static int totalWeight = 0, randomNum;
    private static ArrayUnsortedList<Production> data = new ArrayUnsortedList<Production>();
    private static Production pObj;

    public static void main(String[] args) throws IOException {

        String result = "<S>";
        File myFile = new File("cfg.txt");
        Scanner fileScan = new Scanner(myFile);

        // Read and process each line of the file
        while (fileScan.hasNext()) {
            Production p = new Production(fileScan.nextLine());
            data.add(p);
        }

        // If the result string still contains non-terminals
        while (result.contains("<")) {

            // Get the non-terminal
            nt = result.substring(result.indexOf('<')+1, result.indexOf('>'));

            // Cannot condense because we have an ambiguous
            // totalWeight. Hence, we need to iterate twice:
            // 1. To generate appropriate random number from totalWeight
            // 2. To determine when the totalWeight exceeds that random number.

            iterateData(false);

            Random rand = new Random(); // Generate a random number object
            randomNum = rand.nextInt(totalWeight - 1) + 1; // Generate actual random number

            // Resets
            totalWeight = 0;
            data.reset();

            iterateData(true);

            // Replace the non-terminal with the expression to use
            result = result.replaceFirst("<" + nt + ">", useExp);
            totalWeight = 0; // Reset totalWeight;
        }

        System.out.println(result);
    }

    public static void iterateData(boolean GETEXP) {
        // Iterate through list again
        for (int i = 0; i < data.size(); i++) {

            // Get the current value
            pObj = data.getNext();

            // Keep track of weight
            if (nt.equalsIgnoreCase(pObj.getNonTerminal())) {
                totalWeight += pObj.getWeight();

                if (GETEXP) {
                    if (totalWeight > randomNum) {
                        useExp = pObj.getExpression(); // This is expression to use
                        break;
                    }
                }
            }
        }
    }
}

My cfg.txt file is as such:

80 <S> = My homework is late because <reason>.
20 <S> = I want to <action> instead of writing this program.
40 <reason> = <who> ate it
40 <reason> = I set it on fire because I was cold
20 <reason> = I didn't feel like doing it
30 <who> = Deep Shah
70 <who> = my pet <animal>
10 <animal> = dog
20 <animal> = narwhal
50 <animal> = python
20 <animal> = velociraptor
30 <action> = go on a vacation
30 <action> = indulge in a giant feast
40 <action> = frolic in the snow


texts: Get Worksheet Data Array (Standard Methods)

texts:
I'm re-writing my module of Standard Methods. Virtually every project I do begins with grabbing some number of Data Tables and putting them in arrays. So, this is my general "Get Worksheet Data" method(s).

As always, particularly interested in maintainability, but all feedback is welcome.

Public Function GetWsDataArray(ByRef wbTarget As Workbook, ByRef wsTarget As Worksheet, ByVal topLeftCellText As String, ByVal useCurrentRegion As Boolean _
                            , Optional ByVal searchStartRow As Long = 1, Optional ByVal searchStartColumn As Long = 1 _
                            , Optional ByVal searchEndRow As Long = 10, Optional ByVal searchEndColumn As Long = 10) As Variant
                            '/ 10x10 is arbitrary search range that should cover almost all typical worksheets

    Dim dataArray As Variant
        dataArray = Array()
        dataArray = GetWsDataRange(wbTarget, wsTarget, topLeftCellText, useCurrentRegion, searchStartRow, searchStartColumn, searchEndRow, searchEndColumn)

        GetWsDataArray = dataArray

End Function

Public Function GetWsDataRange(ByRef wbTarget As Workbook, ByRef wsTarget As Worksheet, ByVal topLeftCellText As String, ByVal useCurrentRegion As Boolean _
                            , ByVal searchStartRow As Long, ByVal searchStartColumn As Long _
                            , ByVal searchEndRow As Long, ByVal searchEndColumn As Long) As Range

    Dim wbSource As Workbook, wsSource As Worksheet
    Set wbSource = ActiveWorkbook
    Set wsSource = ActiveSheet

    wbTarget.Activate
    wsTarget.Activate
    UnhideWsCellsAndRemoveFilters wsTarget

    Dim topLeftCell As Range, searchRange As Range, dataRange As Range
    Set searchRange = wsTarget.Range(Cells(searchStartRow, searchStartColumn), Cells(searchEndRow, searchEndColumn))
    Set topLeftCell = CellContainingStringInRange(searchRange, topLeftCellText)

    Dim lastRow As Long, lastCol As Long
    If useCurrentRegion Then
        Set dataRange = topLeftCell.CurrentRegion
    Else
        lastRow = Cells(Rows.Count, topLeftCell.Column).End(xlUp).row
        lastCol = Cells(topLeftCell.row, Columns.Count).End(xlToLeft).Column
        Set dataRange = wsTarget.Range(topLeftCell, Cells(lastRow, lastCol))
    End If

    Set GetWsDataRange = dataRange
    wbSource.Activate
    wsSource.Activate

End Function

Public Function CellContainingStringInRange(ByRef rngSearch As Range, ByVal strSearch As String) As Range

    Dim errorMessage As String

    Set CellContainingStringInRange = rngSearch.Find(strSearch, LookIn:=xlValues)
    If CellContainingStringInRange Is Nothing _
        Then
            errorMessage = "Couldn't find cell """ & strSearch & """ in " & rngSearch.Worksheet.name
            PrintErrorMessage errorMessage, stopExecution:=True
    End If

End Function

Public Sub UnhideWsCellsAndRemoveFilters(ByRef ws As Worksheet)

    ws.Rows.Hidden = False
    ws.Columns.Hidden = False
    ws.AutoFilterMode = False

End Sub


texts:
This is my revision from another post

union' :: (Eq a) => [a] -> [a] -> [a]
union' [] list = list
union' (x:xs) list
    | not $ x `elem` list = x : union' xs list
    | otherwise = union' xs list

Any suggestions welcome.


texts: Simple string hashing algorithm implementation

texts:
I thought of a simple way to hash a string. By taking the ASCII decimal value of each character, multiplying it by 10, and adding all of the values computed together for each character in a string. Is there a name for this algorithm? I highly doubt I was the first one to think of this.

Compiled with gcc -Wall -Wextra -Werror -std=c99 string.c -o string

#include <stdio.h>
#include <stddef.h>

size_t stringLength(const char* source)
{
    if(source == NULL) { return 0; }

    size_t length = 0;
    while(*source != '\0') {
        length++;
        source++;
    }
    return length;
}

static size_t getHash(const char* source)
{
    size_t length = stringLength(source);
    size_t hash = 0;
    for(size_t i = 0; i < length; i++) {
        char c = source[i];
        int a = c - '0';
        hash = (hash * 10) + a;
    }

    return hash;
}

static const char *const testCases[] = {
    "this",
    "is",
    "a",
    "test",
    "but",
    "i",
    "should",
    "use",
    "real",
    "dictonary"
};

#define TABLE_SIZE (16)

int main()
{
    size_t name = getHash("Ryan McCullagh");
    printf("%zu\n", name);

    for(size_t i = 0; i < (sizeof(testCases) / sizeof(testCases[0])); i++) {
        const char* source = testCases[i];
        size_t hash = getHash(source);
        printf("%s <==> %zu\n", source, (hash % TABLE_SIZE));

    }

    return 0;
}


texts:
Method 1:

public void removeFirst() {
    if (first == null) throw new NoSuchElementException();
    if (first == last) first = last = null;
    else {
        var second = first.next;
        first.next = null;
        first = second;
    }
}

Method 2:

public void removeFirst() {
    if (first == null) throw new NoSuchElementException();
    if (first == last) first = last = null;
    else               first = first.next;
}

Which method is preferable, speed and memory-wise? Stepping through the debugger, I see no differences in first, next, and last objects or their value attributes - so 'correctness' appears the same, though I ponder about garbage collection.

Base class excerpt:

public class LinkedList {
    private class Node {
        private int value;
        private Node next;

        public Node(int value) {
            this.value = value;
        }
    }
    private Node first;
    private Node last;
}

public void addLast(int value){
    var new_node = new Node(value);

    if (first == null){
        first = last = new_node;
    } else {
        last.next = new_node;
        last = new_node;
    }
}


texts: Create a pump object from either one of pump model number or pump product id

texts:
Consider this code:

class AssemblyFactory
{
    public function makeAssembly(array $params)
    {
        if (array_key_exists('modelNumber', $params))
            $pump = $pumpFactory->fromModelNumber($params['modelNumber']);
        else if (array_key_exists('productId', $params))
            $pump = $pumpFactory->fromProductId($params['productId']);
        else
            throw new \InvalidArgumentException("FAIL");
    }
}

Is there a way to write this code better using PHP facilities? For example, I considered instead of $params to include $modelNumber and $productId directly, but I am not sure that it will be appropriate, since there will only be one legally valid parameter used at any one time.

Namely I need a way to create a pump using EITHER model number OR product id.

to call:

$factory = new AssemblyFactory();
$pump = $factory->makeAssembly(array(
    'productId' => 33
));
$pump = $factory->makeAssembly(array(
    'modelNumber' => 'AAA-50'
));


texts:
I have a program that displays colorful shapes to the user. It is designed so that it is easy to add new shapes, and add new kinds of views.

Currently, I have only two shapes, and a single text-based view. In the near future, I'm going to implement a Triangle shape and a BezierCurve shape.

I'm also going to implement these views:

GraphicalView - uses a graphics library to render shapes on screen.
OscilloscopeView - draws the shapes on an oscilloscope.
DioramaView - a sophisticated AI directs robotic arms to construct the scene using construction paper, string, and a shoebox.

The MVC pattern is essential here, because otherwise I'd have a big intermixed tangle of oscilloscope and AI and Graphics library code. I want to keep these things as separate as possible.

#model code

class Shape:
    def __init__(self, color, x, y):
        self.color = color
        self.x = x
        self.y = y

class Circle(Shape):
    def __init__(self, color, x, y, radius):
        Shape.__init__(self, color, x, y)
        self.radius = radius

class Rectangle(Shape):
    def __init__(self, color, x, y, width, height):
        Shape.__init__(self, color, x, y)
        self.width = width
        self.height = height

class Model:
    def __init__(self):
        self.shapes = []
    def addShape(self, shape):
        self.shapes.append(shape)

#end of model code

#view code

class TickerTapeView:
    def __init__(self, model):
        self.model = model
    def render(self):
        for shape in self.model.shapes:
            if isinstance(shape, Circle):
                self.showCircle(shape)
            if isinstance(shape, Rectangle):
                self.showRectangle(shape)
    def showCircle(self, circle):
        print "There is a {0} circle with radius {1} at ({2}, {3})".format(circle.color, circle.radius, circle.x, circle.y)
    def showRectangle(self, rectangle):
        print "There is a {0} rectangle with width {1} and height {2} at ({3}, {4})".format(rectangle.color, rectangle.width, rectangle.height, rectangle.x, rectangle.y)

#end of view code

#set up

model = Model()
view = TickerTapeView(model)

model.addShape(Circle   ("red",    4,   8,   15))
model.addShape(Circle   ("orange", 16,  23,  42))
model.addShape(Circle   ("yellow", 1,   1,   2))
model.addShape(Rectangle("blue",   3,   5,   8,   13))
model.addShape(Rectangle("indigo", 21,  34,  55,  89))
model.addShape(Rectangle("violet", 144, 233, 377, 610))

view.render()

I'm very concerned about the render method of TickerTapeView. In my experience, whenever you see code with a bunch of isinstance calls in a big if-elseif block, it signals that the author should have used polymorphism. But in this case, defining a Shape.renderToTickerTape method is forbidden, since I have resolved to keep the implementation details of the view separate from the model.

render is also smelly because it will grow without limit as I add new shapes. If I have 1000 shapes, it will be 2000 lines long.

Is it appropriate to use isinstance in this way? Is there a better solution that doesn't violate model-view separation and doesn't require 2000-line if blocks?


texts:
Note: This question has a follow up question here.

I have an Excel workbook with two sheets: "Raw data" and "Summary". In the raw data sheet there are several blocks with data structured like the image below:

I want to create a summary of the data below, in the sheet "Summary", looking like this:

The way I've done this is:

Sub TransferAndSumElements()
    Dim wb As Workbook
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet

    Set wb = ActiveWorkbook
    Set ws1 = Sheets("Raw data")
    Set ws2 = Sheets("Summary")

    '##############
    ' Copy / Paste first row
    '##############
    ws1.Select
    Rows("1:1").Activate
    Selection.Copy
    ws2.Select
    Range("A1").Select
    ws2.Paste

    '##############
    ' Set Item names in summary
    '##############

    ws2.Select
    Range("A2").Select
    ActiveCell.FormulaR1C1 = "Knife"
    Range("A3").Select
    ActiveCell.FormulaR1C1 = "Fork"
    Range("A4").Select
    ActiveCell.FormulaR1C1 = "Spoon"
    Range("A5").Select
    ActiveCell.FormulaR1C1 = "Spork"
    Range("A6").Select
    ActiveCell.FormulaR1C1 = "Bowl"
    Range("A7").Select

    '##############
    ' Find Elements and Sum
    '##############

    Dim s As Long           ' Sum of elements for each Item
    Dim str As String       ' String in first column in "Summary" (Knife, Fork ...)
    Dim cellstr As String   ' String in first column in "Raw data" (To be searched through)
    Dim DataRange As Range  ' All rows in sheet 1 ("Raw data")

    Dim cellA As Range      ' Used to find cellstr = cellA.Value
    Dim cellB As Range     ' Used to increment s = s + cellB.Value

    ws1.Select              ' Select first worksheet ("This worksheet")
    LastRow = ws1.Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
    Set DataRange = Range("A1:A" & LastRow)

    For i = 1 To 5          ' Iterate through Knife, Fork .. in "Summary"...

        str = ws2.Cells(i + 1, 1).Value     ' Extract names
        s = 0                               ' Initialize sum
        ws1.Activate                        ' Activate Worksheet 1 ("Raw data")

        For r = 1 To LastRow
            Set cellA = Range("A" & r, "A" & r)
            cellstr = cellA.Value
            If InStr(cellstr, str) > 0 Then
                Set cellB = Range("B" & r, "B" & r)
                s = s + cellB.Value
            End If
        Next r

        ws2.Cells(i + 1, 2).FormulaR1C1 = s

    Next i

ws2.Activate

End Sub

Some stuff that I want to improve, but haven't been able to:

Automatically fetch the item names from "Raw data". I've tried creating a Collection, but I end up with an empty row in between the others (Knife, Fork, Spoon, Spork, Blank, Bowl).

I can loop through this list after creating it, and delete the blank row, but I think there should be a way to do this in one go.

I want to create a list of words ("Knife", "Fork" .. ) and write it to Excel in one command. The way I'm doing it now is to manually write each item name.
Do the loops make sense, or should I do it in some other way?

And last but not least:

What about the big picture? Does it make sense to do it like this, or should I do it completely different?

Note: The summary can be sorted alphabetically, by number of elements, or not sorted at all. That doesn't matter.

Tab delimited data dump:

Item    Number
Knife   2
Fork    2
Spoon   1
Spork   2

Item    Number
Spork   2
Fork    3

Item    Number
Bowl    3
Knife   5


texts: Extracting original values from cumulative sum values

texts:
I was writing a function in python to extract original values given a list containing cumulative sum values. For eg -

Given : cumulative_sum = [1, 3, 6, 10, 15, 21, 28, 36, 45]
Output : [1, 2, 3, 4, 5, 6, 7, 8, 9]

I came up with two functions A & B and am looking for feedback on them, especially using xrange() vs enumerate().

import numpy as np
cumulative = np.cumsum(range(1,10))

def A(cumulative):
    output = [0] * len(cumulative)
    for i in xrange(len(cumulative)-1):
        output[-(i+1)] = cumulative[-(i+1)] - cumulative[-(i+2)]
    output[0] = cumulative[0]
return output

def B(cumulative):
    output = [0] * len(cumulative)
    for i,e in reversed(list(enumerate(cumulative))):
        output[i]=cumulative[i] - cumulative[i-1]
    output[0]=cumulative[0]
return output


texts: C++ implementation of a Stack with dynamic C-style array

texts:
I implemented a Stack in C++ using a dynamic C-style array. I tried sticking to the most important functions a Stack has to have to be usable. This is meant to only be for integers. I appreciate any suggestions on how to improve this. Here is my code:
Stack.h
#ifndef STACK_H
#define STACK_H
#include <cstdint>
#include <algorithm>

class Stack
{
public:
    Stack(int32_t u_size = 10) : m_current_ind{ -1 }, m_size{ 0 }, m_capacity{ u_size }{  // constructor
        m_stack_arr = new int32_t[u_size];
    };
    ~Stack() {                                                                     // destructor
        delete[] m_stack_arr;
    };
    Stack(const Stack& other) : m_current_ind{ -1 }, m_size{ 0 }, m_capacity{ 0 }{        // copy constructor
        for (int32_t i = 0; i < other.size(); i++)
        {
            push(other.m_stack_arr[i]);
        }
    }
    Stack(Stack&& other) : m_current_ind{ -1 }, m_size{ 0 }, m_capacity{ 0 }{             // move constructor
        m_stack_arr = std::exchange(other.m_stack_arr, nullptr);
    }
    Stack& operator=(const Stack& other) {                                         // copy assignment operator
        if (this != &other)
        {
            clear();
            for (int32_t i = 0; i < other.size(); i++)
            {
                push(other.m_stack_arr[i]);
            }
        }
        return *this;
    }
    Stack& operator=(Stack&& other) {                                              // move assignment operator
        if (this != &other)
        {
            m_stack_arr = std::exchange(other.m_stack_arr, nullptr);
            m_size = other.m_size;
            m_capacity = other.m_capacity;
            m_current_ind = other.m_current_ind;
        }
        return *this;
    }
    void clear(int32_t new_capacity = 10); // clear stack and initialise new empty array
    bool is_empty() const; // check if stack is empty
    int32_t pop(); // delete value on top and return it
    int32_t peek() const; // return value on top of the stack
    void push(int32_t u_value); // add value to stack
    int32_t size() const; // return size

private:
    void extend(); // double capacity

    int32_t m_current_ind;
    int32_t m_size;
    int32_t m_capacity;
    int32_t* m_stack_arr;
};

#endif

Stack.cpp
#include "Stack.h"
#include <stdexcept>

void Stack::clear(int32_t new_capacity)
{
    delete[] m_stack_arr;
    m_stack_arr = new int32_t[new_capacity];
    m_capacity = new_capacity;
    m_current_ind = -1;
    m_size = 0;
}

bool Stack::is_empty() const
{
    return !m_size;
}

int32_t Stack::pop()
{
    if (!is_empty())
    {
        m_size--;
        return m_stack_arr[m_current_ind--];
    }
    throw std::out_of_range("Stack is already empty.");
}

int32_t Stack::peek() const
{
    if (!is_empty())
    {
        return m_stack_arr[m_current_ind];
    }
    throw std::out_of_range("Stack is empty.");
}

void Stack::push(int32_t u_value)
{
    m_size++;
    m_current_ind++;
    if (m_size > m_capacity)
    {
        extend();
    }
    m_stack_arr[m_current_ind] = u_value;
}

int32_t Stack::size() const
{
    return m_size;
}

void Stack::extend()
{
    int32_t* temp = new int32_t[m_capacity * 2];
    m_capacity *= 2;
    std::copy(m_stack_arr, m_stack_arr + m_size, temp);
    delete[] m_stack_arr;
    m_stack_arr = temp;
}
```


texts: how to write generic extensions for standard collection types in java?

texts:
I am a Java beginner and I am looking for an idiomatic way of writing a function that involves generics. I wrote this helper class (below) that pushes items into a sorted generic collection and I wanted to ask for your feedback. Should I perhaps extends some base class of some collection? Or maybe there is some better approach that follows the Java philosophy?

import java.util.Comparator;
import java.util.List;

public abstract class ListExtensions {
    public static <T> void addOnCompare(List<T> collection, T item,
                                        Comparator<T> comparator) {
        synchronized(collection) {
            int i = 0;
            int size = collection.size();
            if (size == 1) {
                int diff = comparator.compare(item, collection.get(0));
                switch(diff) {
                case 1: i++; break;
                default: break;
                }
            } else {
                int range = size - 1;
                i = size / 2;
                int left = 0;
                int right = range;
                while(true) {
                    if (i <= 0) { i = 0; break; }
                    if (i > range) { i = range; break; }
                    int diff = comparator.compare(item, collection.get(i));
                    if (diff == 0) break;
                    else {
                        if (diff == -1) right = i;
                        if (diff == 1) left = i;
                        int near = i + diff;
                        if (near < 0) { i = 0; break; }
                        if (near > range) { i = range + 1; break; }
                        int diff_near = comparator.compare(item, collection.get(near));
                        if (diff_near == 0) { i = diff_near; break; }
                        if (diff_near == diff) {
                            int step = (right-left)/2;
                            if (step == 0) step = 1;
                            switch(diff){
                            case -1:
                                right = i;
                                i = i - step; break;
                            case 1:
                                left = i;
                                i = i + step; break;
                            }
                        } else if (diff > diff_near) {
                            i = near; break;
                        } else { break; }
                    }
                }
            }
            collection.add(i, item);
        }
    }
}


texts:
I did this exercise yesterday mostly as practice, but it has some utility as well in day to day. I was basically attempting to take a string that looked like the following:
src=bing_adid=8312488564_kw=swiftcapital com_kwid=35030235383_mt=e_qs=swiftcapital.com_device=c'

And create a series of columns based on the parameters within the string. Here you can see src, kw, kwid, mt, etc.
I was given a set of rules for the string, and used those in my assumptions; as follows:

Exercise 1: Parsing out variables

Any of the ten variables might appear in a string (src, adid, kw, kwid, mt, dist, qs, adpos, device, placement).

The value for that parameter will always be after the equal sign, i.e. src=bing (bing is the value being parsed).

At this point, the length is inconsistent for any given parameter.

Not every parameter will appear in every string.

Not every parameter will appear in the same position in every string.

The parameters will always be delimited with an underscore.

Exercise 2: Absence of parameters (catch all)

If none of that ten parameters appear, then it should all be captured into a catch-all column (i.e., Site Link).

In addition, I used the assumption that an _ and = character both could occur in a string which is why I elected to use regular expressions (this was as much for practical utility as it was just to teach myself some more regex.)
Here's my solution using pandas, csv, and re, please let me know if you can think of an easier/smarter way in python. (I have a feeling I could have hard-coded this using things like FIND() in excel but I want to avoid Excel whenever possible.
# we use pandas and regular expression libraries
import pandas as pd
import re

# dropping all but the 'input' column; dropping all rows in that column that are NaN
df = pd.read_csv('Parsing.csv')
df = df[df['input'].notnull() == True]
df = df[['input']]

# defining params to search for in our keywords
parameters = ['src', 'adid', 'kw', 'kwid', 'mt', 'dist', 'qs', 'adpos', 'device', 'placement']
new_params = [params+'=' for params in parameters]

#split apart parsed data will be stored here in a mock_dataframe which we will reinject to pandas
mock_df = {'comb_str=': [],
        'src=': [],
         'adid=': [],
         'kw=': [],
         'kwid=': [],
         'mt=': [],
         'dist=': [],
         'qs=' : [],
         'adpos=' : [],
         'device=' : [],
         'placement=' : [],
          'catchall' : []}

# nested for loop that looks for the conditions using regular expressions and appends to our mock_df
a = [x for x in df['input']]
for entry in a:
    mock_df['comb_str='].append(entry)
    if any(params in entry for params in new_params):
        mock_df['catchall'].append('')
        for params in new_params:
            if params in entry:
                mock_df[params].append(re.search(params+'(.*?)(?=_[^_=\n]+=|$)',entry).group(1))
            else:
                mock_df[params].append('')
    else:
        for params in new_params:
            mock_df[params].append('')
        mock_df['catchall'].append(entry)

# to get an idea what the new df looks like
df_upd = pd.DataFrame(data=mock_df)
df_upd

# port new DF to a csv for excel use.
df_upd.to_csv(path_or_buf='parsed.csv')

I'm hoping to get help on removing complexity in my code, and if possible making the code more efficient. I would imagine an input might be 1mil or so of the above strings, with 1mil*#of params defined columns as an output. So it can get messy in a hurry.
comb_str is just re-importing the string itself and then adding it back to the DF in case it's needed to be used in an index or something later.


texts:
I'm trying to write a Python module to handle matrices. (I know about numpy, this is just for fun)

So far I have written a few classes, Matrix, Dim, and Vec. Matrix and Vec are both subclasses of Dim. When creating a matrix, one would first start out with a list of lists and they would create a matrix like:

startingList = [[1,2,3],[4,5,6],[7,8,9]]
myMatrix = matrix.Matrix(startingList)

This should create a Matrix. The created Matrix should contain multiple Dims all of the same length. Each of these Dims should contain multiple Dims all of the same length, etc. The last Dim, the one that contains numbers, should contain only numbers and should be a Vec instead of a Dim.

So far this works as I want it to. Here is what I have:

from numbers import Number

test2DMat = [[1,2,3],[4,5,6],[7,8,9]]
test3DMat = [[[1,2,3],[4,5,6],[7,8,9]],[[2,3,4],[5,6,7],[8,9,0]],[[9,8,7],[6,5,4],[3,2,1]]]

class Dim(list):
    def __new__(cls,inDim):
        # Make sure inDim is iterable
        iter(inDim)

        # If every item in inDim is a number create a Vec
        if all(isinstance(item,Number) for item in inDim):
            #return Vec(inDim)
            return Vec.__new__(cls,inDim)

        # Make sure every item in inDim is iterable
        try:
            for item in inDim: iter(item)
        except TypeError:
            raise TypeError('All items in a Dim must be iterable')

        # Make sure every item in inDim has the same length
        # or that there are zero items in the list
        if len(set(len(item) for item in inDim)) > 1:
            raise ValueError('All lists in a Dim must be the same length')

        # Actually create the Dim because it passed all the tests
        return list.__new__(cls,inDim)

    def __init__(self,inDim):
        inDim = map(Dim,inDim)
        list.__init__(self,inDim)

class Vec(Dim):
    def __new__(cls,inDim):
        if cls.__name__ not in [Vec.__name__,Dim.__name__]:
            newMat = list.__new__(Vec,inDim)
            newMat.__init__(inDim)
            return newMat
        return list.__new__(Vec,inDim)

    def __init__(self,inDim):
        list.__init__(self,inDim)

class Matrix(Dim):
    def __new__(cls,inMat):
        return Dim.__new__(cls,inMat)

    def __init__(self,inMat):
        super(Matrix,self).__init__(inMat)

This works, but as I have little experience overriding __new__() I'm fairly certain that this could be better. How can I improve this? Answers need not be specific to use of __new__ and __init__, but that is what I care most about.


texts: Obtaining all repeated characters' position in a string using Java Stream API

texts:
Input is a string, and the method should return repeated characters in that string and the position of these characters.

I'm currently using Java 8 Stream. The problem is I have to do it by calling 2 times collectors. I am trying to find a way to combine all these operations in 1 single collector operations if possible.

Furthermore, the operation of mapping of characters and their position in a string is still performed in another method since I have not found a way to perform that.

Please give suggestion of improvement for the code:

public static void main(String args[]) {
    String str = "acbdaghfb";

    Map<Character,List<Integer>> map=charMaps(str);
    map.forEach((k,v)->System.out.println(k+"_"+v));
}

public static Map<Character,List<Integer>> charMaps(String str) {

    Map<Character, List<Integer>> maps = encodeString(str)
            .stream()
            .collect(Collectors.groupingBy(Pair::getKey, Collectors.mapping(Pair::getValue, Collectors.toList())));

    maps = maps.entrySet()
                .stream()
                .filter(e -> e.getValue().size() > 1)
                .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));

    return maps;
}

public static List<Pair<Character, Integer>> encodeString(String str) {
    List<Pair<Character, Integer>> pairs = new LinkedList<Pair<Character, Integer>>();
    char chars[] = str.toCharArray();

    for (int a = 0; a < chars.length; a++) {
        pairs.add(new Pair<Character, Integer>(chars[a], a));
    }
    return pairs;
}


texts:
I am using this basic extension for a Swift project. It works well, but I want to make sure it is the absolute fastest way to get a substring.

extension String {
    func substring(start: Int, end: Int) -> String {
        if (start == end || self.strlen() == 0) {
            return ""
        }
        let startIndex = self.index(self.startIndex, offsetBy: start)
        let endIndex = self.index(self.startIndex, offsetBy: end)
        return self[startIndex..<endIndex]
    }

    func strlen() -> Int {
        return self.characters.count
    }
}

You can see I am checking for two situations that don't make since in the if then statement. Those are the only two things I need to check for. For example, I don't need to check for a situation where start is greater than end.

Should this condition be rewritten as a guard statement? Remember I'm not concerned about following best practices or writing pretty code, I just want to have an efficient substring function.


texts:
I extended the example in 04-random.elm to support N dice and to draw the results using SVG.

import Html exposing (..)
import Html.Events exposing (..)
import Random
import List exposing (length, repeat, range)
import Svg exposing (Svg, svg, circle)
import Svg.Attributes exposing (..)

main =
  Html.program {
    init = init,
    view = view,
    update = update,
    subscriptions = subscriptions
  }

-- MODEL

type alias Model = {
  dieFaces : List Int
}

init : (Model, Cmd Msg)
init =
  let numDice = 5
  in
    (Model (repeat numDice 1), Cmd.none)

-- UPDATE

type Msg =
  Roll |
  NewFaces (List Int)

update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
  case msg of
    Roll -> (
        model,
        Random.generate NewFaces (
          Random.list (length model.dieFaces) (Random.int 1 6)
        )
      )

    NewFaces newFaces ->
      (Model newFaces, Cmd.none)

-- SUBSCRIPTIONS

subscriptions : Model -> Sub Msg
subscriptions model =
  Sub.none

-- VIEW

view : Model -> Html Msg
view model =
  div [] (
    List.map drawFace model.dieFaces
    ++ [ button [ onClick Roll ] [ text "Roll" ] ]
  )

drawFace : Int -> Html Msg
drawFace numDots =
  div [ style "width: 66px; height: 66px; padding: 10px" ] [
    svg
      [ version "1.1", width "66", height "66" ]
      (drawDice ::
      (List.map (\i -> drawDot i numDots) (range 0 (numDots - 1))))
  ]

drawDice: Svg Msg
drawDice =
  circle [ fill "gold", stroke "orange", strokeWidth "3", cx "33", cy "33", r "30" ] []

drawDot: Int -> Int -> Svg Msg
drawDot i n =
  circle [ fill "black", cx (toString (posX i n)), cy (toString (posY i n)), r "5" ] []

posX: Int -> Int -> Int
posX i n =
  round (33 + (15 * cos (6.28 * (toFloat(i) / toFloat(n)))))

posY: Int -> Int -> Int
posY i n =
  round (33 + (15 * sin (6.28 * (toFloat(i) / toFloat(n)))))


texts: Work out largest prime factor of a number

texts:
I am trying to get better at problem solving, so I am going through challenges on Project Euler.
Problem:

The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?

My solution:
My approach to this was the following process:

Get all factors of the number

Identify prime factors by looping from 1 up to each factor. If the factor MOD the numbers going up is equal to 0 and the number is not 1 or the number itself, then it is not a prime. Then another for loop goes through each factor again and checks if it is not in the not primes list, hence making it a prime.

The last item in the primes list gives the biggest prime number

Code:
def get_factors(n):
    factors = []
    for i in range(1,n+1):
        if n % i == 0:
            factors.append(i)

    return factors

def get_prime_factors(factors):
    print(factors)
    primes = []
    not_primes = [1]
    for i in range(len(factors)):
        for k in range(1,factors[i]+1):
            if factors[i] % k == 0 and factors[i] !=1 and k!=1 and k!=factors[i]:
                not_primes.append(factors[i])
    for i in factors:
        if i not in not_primes:
            primes.append(i)
    return primes

def biggest_prime(primes):
    biggest = primes[-1]
    print(biggest)

factors = get_factors(600851475143)
primes = get_prime_factors(factors)
biggest_prime(primes)

Problems with my solution:
It seems like it is too overcomplicated, which is why it can only handle small numbers. The program did not finish executing for the number 600851475143.
What is specifically making my solution so inefficient, is it because I am using too many loops? How can I make it efficient?


texts: Counting all substrings with exactly k distinct characters

texts:
Problem Statement : Given a string of lowercase alphabets, count all possible substrings (not necessarily distinct) that has exactly k distinct characters. Example:

Input: abc, k = 2 Output: 2 Possible substrings are {"ab", "bc"}

I have written the solution with a two pointer approach. I am not sure how to calculate the time complexity of the program?

According to me complexity should be O(n*k)

public static void main(String[] args)
    {
    String s = "abacuusttlvbnc";
    int k=3;
    char[] sArr = s.toCharArray();
    int strLen = sArr.length;
    Set<Character> set = new LinkedHashSet<Character>();
    int l=0;
    int r=0;

    while(l<=strLen-k){  // will run (arrayLength - k) times
        for(int i=0;i<k;i++){   // will run k times for every while iteration
            set.add(sArr[l]);
            l++;
        }
        if(set.size()==k){
            System.out.println("substring : " + set);
        }
        set.clear(); // O(k) for every while iteration
        r++;
        l= r;
     }
  }
}

Output :

substring : [b, a, c]
substring : [a, c, u]
substring : [u, s, t]
substring : [t, l, v]
substring : [l, v, b]
substring : [v, b, n]
substring : [b, n, c]


texts: Throwing a custom error in a stored procedure

texts:
I'm wanting to throw an error when reference keys are missing, however instead of failing through a integrity check, I want to list out the missing keys. I've created the below which works. However I'm hoping there is a way to optimise it and reduce the number of lines of code.

DECLARE @NonRefKeys INT

SELECT @NonRefKeys = SUM(1)
FROM staging.Sale sa
WHERE NOT EXISTS (
    SELECT cu.Customer_Shipping_ID
    FROM staging.Customer cu
    WHERE LTRIM(RTRIM(sa.Customer_Shipping_ID)) = LTRIM(RTRIM(cu.Customer_Shipping_ID)))

IF @NonRefKeys IS NOT NULL
BEGIN

IF OBJECT_ID('tempdb..#Missing_Ref') IS NOT NULL
DROP TABLE #Missing_Ref;

SELECT sa.Customer_Shipping_ID AS ID
INTO #Missing_Ref
FROM staging.Sale sa
WHERE NOT EXISTS (
        SELECT cu.Customer_Shipping_ID
        FROM staging.Customer cu
        WHERE LTRIM(RTRIM(sa.Customer_Shipping_ID)) = LTRIM(RTRIM(cu.Customer_Shipping_ID)))

DECLARE @Current_ID VARCHAR(50);
DECLARE @Missing_ID VARCHAR(MAX) = '';
DECLARE @Output_Error VARCHAR(MAX);

DECLARE id_cursor CURSOR FOR
SELECT ID
FROM #Missing_Ref;

OPEN id_cursor
FETCH NEXT FROM id_cursor INTO @Current_ID


texts: How do I refactor lines of Ruby code that run too long due to method chaining or object instantiation?

texts:
Here are a couple of examples of one-liners that go way beyond 80 characters:

scope = DepartmentRepository.includes(:location).by_account(@request.account_id).find(approved_department_ids)
departments = ActiveModel::ArraySerializer.new(scope, each_serializer: DepartmentWithLocationSerializer).as_json

I usually simply break these kinds of chained method calls before the . and indent the next line. My IDE (Emacs) runs rubocop on my source files as I edit them and doesn't complain about this practice. It does complain about lines longer than 80 characters so I feel somewhat compelled to fix them. How would I refactor these? The first line is calling ActiveRecord finders and scopes. Scopes can be compbined but then they have ackward names that seldom see reuse (e.g. by_account_with_locations).