texts: Constructing an SQL expression from three parameters

texts:
I've been on stackoverflow looking for an alternative to the ugly if-elif-else structure shown below. The if-else structure takes three values as input and returns a pre-formatted string used for an SQL query. The code works right now, but it is difficult to explain, ugly, and I don't know a better way to do re-format it.

I need a solution that is more readable. I was thinking a dictionary object, but I can't wrap my mind how to implement one with the complex if/else structure I have below. An ideal solution will be more readable.

def _getQuery(activity,pollutant,feedstock):

   if feedstock.startswith('CG') and pollutant.startswith('VOC'):
        pass

    if feedstock == 'FR' and activity == 'Harvest':
        return 'FR'

    elif feedstock == 'FR':
        return 'No Activity'

    elif(activity == 'Fertilizer' and pollutant != 'NOx' and pollutant != 'NH3'):
        return 'No Activity'

    elif(activity == 'Chemical' and pollutant != 'VOC'):
        return 'No Activity'

    elif( (feedstock == 'CS' or feedstock == 'WS')
        and
        (activity == 'Non-Harvest' or
        activity == 'Chemical')):
        return 'No Activity'

    elif( ( ( pollutant.startswith('CO') or pollutant.startswith('SO') )
                and
            ( activity == 'Non-Harvest' or
            activity == 'Harvest' or
            activity == 'Transport' )
            )
            or
            ( pollutant.startswith('VOC')
                and not
            ( feedstock.startswith('CG') or feedstock.startswith('SG') )
            )
        ):

        rawTable = feedstock + '_raw'
        return """
    with
    activitySum as (select distinct fips, sum(%s) as x
        from %s where description ilike '%s' group by fips),

    totalSum as (select distinct r.fips, sum(r.%s) as x
        from %s r group by r.fips),

    ratios as (select t.fips, (a.x/t.x) as x from activitySum a, totalSum t
        where a.fips = t.fips),

    maxRatio as (select r.fips, r.x as x from ratios r
        group by r.fips, r.x order by x desc limit 1),

    minRatio as (select r.fips, r.x as x from ratios r
        group by r.fips, r.x order by x asc limit 1)

    select mx.x, mn.x from maxRatio mx, minRatio mn;
    """ % (pollutant, rawTable, '%'+activity+'%',
       pollutant, rawTable)

    elif( (pollutant[0:2] == 'PM')
            and
            (activity == 'Non-Harvest' or
            activity == 'Harvest' or
            activity == 'Transport')):

        rawTable = feedstock + '_raw'
        return """
    with
    activitySum as (select distinct fips, (sum(%s) + sum(fug_%s)) as x
        from %s where description ilike '%s' group by fips),

    totalSum as (select distinct r.fips, (sum(r.%s) + sum(r.fug_%s)) as x
        from %s r group by r.fips),

    ratios as (select t.fips, (a.x/t.x) as x from activitySum a, totalSum t
        where a.fips = t.fips),

    maxRatio as (select r.fips, r.x as x from ratios r
        group by r.fips, r.x order by x desc limit 1),

    minRatio as (select r.fips, r.x as x from ratios r
        group by r.fips, r.x order by x asc limit 1)

    select mx.x, mn.x from maxRatio mx, minRatio mn;
    """ % (pollutant, pollutant, rawTable, '%'+activity+'%',
       pollutant, pollutant, rawTable)

.
.
.
Lots more complex elif statements


texts:
I have recently written a program that reads information from a CSV file and puts it into lists. An encryption sub procedure converts the characters to ASCII, adds 2 then converts them back to characters.

import csv

ifile = open("messages.csv","rb")
reader= csv.reader(ifile)

plain_text= []
plain_ascii= []
encrypted_ascii= []
encrypted_text= []
latitude= []
longitude= []

for row in reader:
    latitude.append(row[0])
    longitude.append(row[1])
    plain_text.append(row[2])

encrypted_text=[''.join(chr(ord(ch)+2) for ch in string) for string in
plain_text]
print plain_text
print encrypted_text

count=0

print ""
while True:
    print ""
    response=int(raw_input("Enter 1 to see the next record, 2 to see the
previous record or 3 to end the program:"))
    print""
    if response > 3 or response < 1:
        print ("Please respond with one of the set responses.")
    elif response == 3:
        print "Process ended"
        break
    else:
        if response==1:
            count += 1
            if count >= len(plain_text):
                count = 0
        elif response==2:
            count -= 1
            if count < 0:
                count = len(plain_text) - 1
        print ""
        print("Record {0}:".format(count))
        print ""
        print("Latitude: {0}  Longitude: {1}  Unencrypted Text: {2}
Encrypted Text: {3}".format(latitude[count],longitude[count],
plain_text[count], encrypted_text[count]))

ifile.close()

Is there any way to make the entirety of this much more precise or simpler, and possibly any way to restructure this?


texts: Sorting dates in this format (DD-MM-YYYY)

texts:
Is this the correct and an elegant way to sort the dates in the format (DD-MM-YYYY) in ascending order?

let dates = ['01-02-2018', '02-01-2018', '01-01-2018', '31-12-2017'];

dates.sort((date1, date2) => {
    date1 = date1.split('-'), date2 = date2.split('-');
    let day1 = parseInt(date1[0]);
    let day2 = parseInt(date2[0]);
    let month1 = parseInt(date1[1]);
    let month2 = parseInt(date2[1]);
    let year1 = parseInt(date1[2]);
    let year2 = parseInt(date2[2]);
    if (year1 !== year2) {
        return year1 - year2;
    } else if (month1 !== month2) {
        return month1 - month2;
    } else {
        return day1 - day2;
    }
});

There is also this way, as Cris Luengo said in the comments, I can directly compare the strings.

let dates = ['01-02-2018', '02-01-2018', '01-01-2018', '31-12-2017'];

dates.sort((date1, date2) => {
    date1 = date1.split('-').reverse().join('-');
    date2 = date2.split('-').reverse().join('-');
    if (date1 < date2) {
        return -1;
    } else if (date1 > date2) {
        return 1;
    } else {
        return 0;
    }
});

Which way is more performant? How do I measure that?


texts:
The program takes census data from a text file in the format age, gender, marital status, district. It then gives a list of residents by district (there are 22) and by 5 age groups. Any suggestions for improvement?

The only topics we have covered so far in class are methods, decisions, looping, and arrays.

static void Main(string[] args)
    {
        FileStream fStream = new FileStream("test.txt", FileMode.Open, FileAccess.Read);
        StreamReader inFile = new StreamReader(fStream);

        string inputRecord = "";
        string[] fields;
        int[] ageData = new int[1000];
        string[] genderData = new string[1000];
        string[] maritalData = new string[1000];
        int[] districtData = new int[1000];
        int[] countDist = new int[23];
        int[] ageGroup = new int[5];

        inputRecord = inFile.ReadLine();

        int i = 0;
        while (inputRecord != null)
        {
            fields = inputRecord.Split(',');

            ageData[i] = int.Parse(fields[0]);
            genderData[i] = fields[1];
            maritalData[i] = fields[2];
            districtData[i] = int.Parse(fields[3]);

            if (ageData[i] > 0 && ageData[i] <= 18)
            {
                ageGroup[0] = ageGroup[0] + 1;
            }
            if (ageData[i] > 18 && ageData[i] <= 30)
            {
                ageGroup[1] = ageGroup[1] + 1;
            }
            if (ageData[i] > 30 && ageData[i] <= 45)
            {
                ageGroup[2] = ageGroup[2] + 1;
            }
            if (ageData[i] > 45 && ageData[i] <= 64)
            {
                ageGroup[3] = ageGroup[3] + 1;
            }
            if (ageData[i] >= 65)
            {
                ageGroup[4] = ageGroup[4] + 1;
            }

            i++;

            inputRecord = inFile.ReadLine();
        }
        Console.WriteLine("This Program takes census data from a file and lists residents by age group and district");
        Console.WriteLine("Please ensure the file is formatted correctly! age,gender (M/F),marital status   (M/S), and district");
        for (int x = 1; x <= 22; x++)
            for (int y = 0; y < districtData.Length; y++)
                if (districtData[y] == x)
                    countDist[x]++;

        for (int x = 1; x <= 22; x++)
            Console.WriteLine("District   " + x + "   has   " + countDist[x] + "   citizens");

        Console.WriteLine("------Amount of Residents per Age Group--------");
        Console.WriteLine("Age Group 18 & under = {0}", ageGroup[0]);
        Console.WriteLine("Age Group 18-30 = {0}", ageGroup[1]);
        Console.WriteLine("Age Group 31-45 = {0}", ageGroup[2]);
        Console.WriteLine("Age Group 46-64 = {0}", ageGroup[3]);
        Console.WriteLine("Age Group 65 & over = {0}", ageGroup[4]);
    }

EDIT

Per comments below, the instructor thinks I should get rid of the ageGroup and districtData. This is what he was suggesting I do - however, I have no idea how I'm going to change my logic that was using districtData.

   for (int districtCount = 1; districtCount <= 22; districtCount++)
   {
       for (int districtNumber = 0; districtNumber < districtData.Length; districtNumber++)
       {
           if (districtData[districtNumber] == districtCount)
           {
             countDist[districtCount]++;
           }
       }
   }


texts:
Here is the problem I've been trying to tackle:

  Design a thread-safe image caching server that can keep in memory only the ten most recently used images.

I chose to implement an LRU cache to solve this as follows:

'''
This module defines an LRUCache.

Constraints:

1. May only hold upto ten items at a time.
2. Must be able to synchronise multiple requests.
3. Must be able to update its cache.
'''

import random
import threading

class LRUCache(object):

    def __init__(self, cacheSize=10):

        # this variables maps image names to file locations
        self.Cache = {}

        # this variables maps image names to timestamp of last request
        # filed for them. Is volatile - requires local clock to never change,
        # or that there is a separate clock service that guarantees global
        # consistency.
        self.RequestTimestamps = {}

        self.CacheSize = cacheSize

    def insert(self, imageName, imageLocation):

        '''
        Insert a new image into our cache. If inserting would exceed our cache size,
        then we shall make room for it by removing the least recently used.
        '''

        with threading.Lock():

            if len(self.Cache) == self.CacheSize:
                self.removeLeastRecentlyUsed()

        self.Cache[imageName] = imageLocation
        self.RequestTimestamps[imageName] = time.time()

    def get(self, imageName):

        '''
        Retrieve an image from our cache, keeping note of the time we last
        retrieved it.
        '''

        with threading.Lock():

            if imageName in self.Cache:

                self.RequestTimestamps[imageName] = time.time()
                return self.Cache[imageName]

            else:

                raise KeyError("Not found!")

    def removeLeastRecentlyUsed(self):

        '''
        Should only be called in a threadsafe context.
        '''

        if self.RequestTimestamps:

            # scan through and find the least recently used key by finding
            # the lowest timestamp among all the keys.

            leastRecentlyUsedKey = min(self.RequestTimestamps, key=lambda imageName: self.RequestTimestaps[imageName])

            # now that the least recently used key has been found,
            # remove it from the cache and from our list of request timestamps.

            self.Cache.pop(leastRecentlyUsedKey, None)
            self.RequestTimestamps.pop(leastRecentlyUsedKey, None)
            return

        # only called in the event requestTimestamps does not exist - randomly
        # pick a key to erase.

        randomChoice = random.choice(self.Cache.keys())
        self.Cache.pop(randomChoice, None)

Questions:

Is this actually threadsafe?

I'm used to implementing locks on single resources, but not class
methods - would multiple threads, one accessing insert and the
other doing a get be forced to synchronised as well?

Or is my strategy of creating different locks in each method only able to prevent concurrent individual insert requests?

Ignore the GIL.
What would be a good strategy to test this code?

I have thought of the obvious strategy:

a) Insert ten items, make nine requests, attempt to insert a tenth
   and check if the item that was removed was the tenth.

but I am not sure if this is the only way or if there is a better way to implement it.


texts:
I wanted to try and use the Sieve of Eratosthenes to find all prime numbers between some arbitrary bounds 1 <= m <= n. The simple implementation of the sieve does not consider the lower bound at all, it always starts from 1 (or actually from 2). So for a big enough n, simply creating an array would be impossible.

The algorithm first finds all prime numbers from 1 to sqrt(n), then uses those numbers to find all primes in the given range.

I'd like to know if:

I'm using more memory than necessary
I'm unnecessarily repeating some operations
I can improve the style of this code

Note: I am not validating user input for simplicity sake.

import java.util.*;

public class PrimeLister {

    private static ArrayList<Integer> segmentSieve(int upperBound) {
        boolean[] primes = new boolean[upperBound + 1];
        Arrays.fill(primes, true);
        ArrayList<Integer> numbers = new ArrayList<>();

        for (int i = 2; i <= upperBound; i++) {
            if (!primes[i])
                continue;
            for (int j = i * i; j <= upperBound; j += i) {
                primes[j] = false;
            }

            if (primes[i])
                numbers.add(i);
        }

        return numbers;
    }

    private static int findOffset(int start, int prime) {
        for (int i = 0; i < prime; i++)
            if (start++ % prime == 0)
                return i;
        return -1;
    }

    public static void listPrimes(int lowerBound, int upperBound) {
        ArrayList<Integer> segmentPrimes = segmentSieve((int) Math.floor(Math.sqrt(upperBound)));
        int[] offsets = new int[segmentPrimes.size()];
        boolean[] primes = new boolean[1 + upperBound - lowerBound];
        Arrays.fill(primes, true);

        for (int i = 0; i < offsets.length; i++) {
            int tmp = segmentPrimes.get(i);
            offsets[i] = findOffset(lowerBound, tmp);

            for (int j = offsets[i]; j < primes.length; j += tmp) {
                if (!primes[j] || (j + lowerBound) == tmp)
                    continue;
                primes[j] = false;
            }
        }

        for (int i = 0; i < primes.length; i++) {
            if (primes[i] && (i + lowerBound) != 1)
                System.out.println(i + lowerBound);
        }

        System.out.println();
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int lowerBound = in.nextInt();
        int upperBound = in.nextInt();
        listPrimes(lowerBound, upperBound);
        in.close();
    }
}


texts: Efficiency of a recursive implementation of the Sieve of Erasthotenes in Python

texts:
I implemented the Sieve of Erasthotenes recursively using filter and I was wondering how efficient this implementation is and wether a non-recursive implementation or something without filter would be better. If someone wants to get into micro-optimization etc. - that would be fun too I suppose :)

This is my code:

def sieve (iterable, container):
    if len(iterable) != 1:
        container.append(iterable [0])
        iterable = [item for item in iterable if item % iterable [0] != 0]
        print("Filter call:", iterable, container, '\n')
        return sieve(iterable, container)
    else:
        container.append(iterable[0])
        print("Return container:", container)
        return container

An example I/O (with the print-Statements) would be:

#Input
lst = list(range(2, 20))
primes = []

print(sieve(lst, primes)

#Output
Filter call: [3, 5, 7, 9, 11, 13, 15, 17, 19] [2]

Filter call: [5, 7, 11, 13, 17, 19] [2, 3]

Filter call: [7, 11, 13, 17, 19] [2, 3, 5]

Filter call: [11, 13, 17, 19] [2, 3, 5, 7]

Filter call: [13, 17, 19] [2, 3, 5, 7, 11]

Filter call: [17, 19] [2, 3, 5, 7, 11, 13]

Filter call: [19] [2, 3, 5, 7, 11, 13, 17]

Return container: [2, 3, 5, 7, 11, 13, 17, 19]

#Return
Out: [2, 3, 5, 7, 11, 13, 17, 19]


texts: Can someone review my implementation of a Queue?

texts:
So here is my Queue interface:

public interface Queue<E> {
    /**
     * Add an item to the rear of the queue.
     *
     * @param item
     *            Item to be added.
     */
    public void enqueue(E item);

    /**
     * Remove and return the item at the front of the queue.
     *
     * @return The item that was removed.
     */
    public E dequeue();

    /**
     * @return The item at the front of the queue.
     */
    public E frontValue();

    /**
     * @return How many elements are in the queue.
     */
    public int length();

    /**
     * Remove all the items in the queue.
     */
    public void clear();
}

And here is my actual implementation:

public class LinkedQueue<E> implements Queue<E> {
    private SinglyLinkedListNode<E> front;
    private SinglyLinkedListNode<E> rear;
    private int size;

    /**
     * Create a new LinkedQueue object.
     */
    public LinkedQueue() {
    this.init();
    }

    private void init() {
    // only one object is constructed
    this.front = this.rear = new SinglyLinkedListNode<E>(null, null);
    this.size = 0;
    }

    @Override
    public void enqueue(E value) {
    this.rear.setNextNode(new SinglyLinkedListNode<E>(value, null));
    this.rear = this.rear.getNextNode();
    this.size++;
    }

    @Override
    public E dequeue() {
    if (this.size == 0) {
        throw new IllegalStateException("In method dequeue of class "
            + "LinkedQueue the linked queue is empty can cannot"
            + " be dequeued");
    }

    E item = this.front.getNextNode().getValue();
    this.front.setNextNode(this.front.getNextNode().getNextNode());

    if (this.front.getNextNode() == null) {
        this.rear = this.front;
    }
    this.size--;
    return item;
    }

    @Override
    public E frontValue() {
    if (this.size == 0) {
        throw new IllegalStateException("In method frontValue of class "
            + "LinkedQueue the linked queue is empty");
    }
    return this.front.getNextNode().getValue();
    }

    @Override
    public int length() {
    return this.size;
    }

    @Override
    public void clear() {
    this.init();
    }

    /**
     * Creates a easy to read String representation of the linked queue's
     * contents.
     *
     * Example: < 1 2 3 4 5 6 >
     */
    public String toString() {
    StringBuilder linkedQueueAsString = new StringBuilder();
    linkedQueueAsString.append("< ");
    System.out.println("front.getNextNode: " + this.front.getNextNode());
    for (SinglyLinkedListNode<E> node = this.front.getNextNode(); node != null; node = node.getNextNode()) {
        linkedQueueAsString.append(node.getValue());
        System.out.println(node.getValue() + ",");
        linkedQueueAsString.append(" ");
    }
    linkedQueueAsString.append(">");
    return linkedQueueAsString.toString();
    }
}


texts: Parse a specified arithmetic type from string

texts:
This class has the responsibility of parsing a string to one of the primitive types: int, double, float, decimal...

While creating it, I have noticed that it'll be terribly ugly.

It basically should perform a TryParse of each primitive type and return the first that returns true, along with the parsed value.

public static class PrimitiveParser
{
    public static bool TryParse(Type targetType, string sourceValue, out object result)
    {
        result = null;

        if (targetType == typeof(int))
        {
            int intResult;
            var parseResult = int.TryParse(sourceValue, out intResult);
            {
                result = intResult;
                return parseResult;
            }
        }
        if (targetType == typeof(double))
        {
            double doubleResult;
            var parseResult = double.TryParse(sourceValue, out doubleResult);
            {
                result = doubleResult;
                return parseResult;
            }
        }
        if (targetType == typeof(float))
        {
            float floatResult;
            var parseResult = float.TryParse(sourceValue, out floatResult);
            {
                result = floatResult;
                return parseResult;
            }
        }

        /// ...

        return false;
    }
}


texts:
I am working on a program that checks for the browser support of a CSS file. Below you can find my working code, so far it supports CSS At-Rules & Properties. In the next few weeks, I will need to expand this code so that I can slowly support the entire language.

I am trying to come up with a readable and efficient system, keep in mind that some CSS files can get huge (10.000 + Lines), so I want to keep the iteration count as low as possible.

There are a couple of things bothering me at the moment, for once, I don't like that I have different functions to check the browser support ('getAtruleSupportData' & 'getPropertySupportData'). I am quite sure I can write this in a single function but I have had no success so far. The other thing that really bothers me is the 'checkBrowserSupport' function itself. I am not sure, what to think about it, but the more I expand it, the more 'reduces' and 'filters' I will need to use. I am not sure this is going to scale at all.

In general, I want to improve the scalability of my code, it's readability and general performance. Any help is more than welcome.

Example Input:

{ atrules:
   [ { type: 'Atrule',
       loc: [Object],
       name: 'media',
       prelude: [Object],
       block: [Object] } ],
  declarations:
   [ { type: 'Declaration',
       loc: [Object],
       important: false,
       property: 'margin',
       value: [Object] },
     { type: 'Declaration',
       loc: [Object],
       important: false,
       property: 'margin',
       value: [Object] } ],
  mediaFeatures:
   [ { type: 'MediaFeature',
       loc: [Object],
       name: 'min-width',
       value: [Object] } ] }

Example Browserscope:

const browserscope = {
  chrome: 60,
  edge: 15,
  firefox: 60,
  ie: 10,
  safari: 10,
};

Working Code:

const bcd = require('mdn-browser-compat-data');

const checkBrowserSupport = ({atrules, declarations}, browserscope) => {
  return {
    atrules: atrules
        .reduce((acc, {name, loc}) => {
          const supportData = getAtruleSupportData(name);

          supportData &&
          acc.push(formatData(browserscope, name, loc, supportData));

          return acc;
        }, [])
        .filter((atrule) => atrule.notSupported.length),
    declarations: declarations
        .reduce((acc, {property, loc}) => {
          const supportData = getPropertySupportData(property);

          supportData &&
          acc.push(formatData(browserscope, property, loc, supportData));

          return acc;
        }, [])
        .filter((property) => property.notSupported.length),
  };
};

const formatData = (browserscope, name, loc, supportData) => {
  const {line, column} = loc.start;

  return Object.entries(browserscope).reduce((acc, [browser, version]) => {
    acc.name = name;
    acc.location = {line, column};
    acc.supported = acc.supported || [];
    acc.notSupported = acc.notSupported || [];

    const browserSupportData = Array.isArray(supportData[browser])
      ? supportData[browser][0].version_added
      : supportData[browser].version_added;

    browserSupportData && browserSupportData <= version
      ? acc.supported.push(browser)
      : acc.notSupported.push(browser);

    return acc;
  }, {});
};

const getAtruleSupportData = (atrule) => {
  try {
    return bcd.css['at-rules'][atrule].__compat.support;
  } catch (error) {
    return false;
  }
};

const getPropertySupportData = (property) => {
  try {
    return bcd.css.properties[property].__compat.support;
  } catch (error) {
    return false;
  }
};

module.exports = {
  checkBrowserSupport,
};


texts: Is this `img` creator decently secure from XSS?

texts:
This is being used now, seems decent to me but I'm curious.

    if (m=s.match(/^\s*(https?:\/\/[^\s<>"]+)\.(bmp|png|webp|gif|jpg|jpeg|svg)\s*$/)) {
         // example s: http://mustachify.me/?src=http://www.someImage.jpg
        return '';
    }
    if (m=s.match(/^\s*(https?:\/\/[^\s<>?"]+)\.(bmp|png|webp|gif|jpg|jpeg|svg)(\?[^\s<>?"]*)?\s*$/)) {
        // example s: http://place.com/photo/something.jpg?height=600&modified_at=1384796271&ratio_x=03&ratio_y=02&width=900
        return '';
    }

I've been trying to get around it but haven't yet managed it. So I'm thinking of just using this.


texts: Generate word list based on Spanish text file

texts:
I'm a beginner and wrote a program that takes a text file and writes a downcased vocabulary list to another text file. I intend to use it mainly to work with text in Spanish, so I added a line to downcase capitalized-accented words. I'm wondering if there is a more efficient way of reading from the original file, as well as removing non-letters and sorting for unique items.

f = File.open("/.../quijote.txt")
words = f.read.split.map(&:downcase)
f.close

#remove numbers and non-letters
words = words.map {|item| item.tr('0-9.,;:¿¡?!«»\‘\“\”\–\]\[\-\(\)\'\"', '')}

#downcase capitalized accented words
words = words.map {|item| item.tr('ÁÉÍÓÚÑ', 'áéíóúñ')}

words = words.uniq.sort

# write each word on a separate line in the file...
File.open("/.../quijotewords.txt", "w+") do |f|
  words.each { |element| f.puts(element) }
end


texts: Generic methods for serializing and deserialzing xml files using streams

texts:
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace Common.Extensions
{
    /// <summary>
    /// Contains the logic for streaming extensions.
    /// </summary>
    public static class StreamExtensions
    {
        /// <summary>
        /// Serialize an object.
        /// </summary>
        /// <typeparam name="T">The type of the object that gets serialized.</typeparam>
        /// <param name="stream">The stream to which the bytes will be written.</param>
        /// <param name="serializableObject">The object that gets serialized.</param>
        public static void SerializeObject<T>(this Stream stream, T serializableObject) where T : IXmlSerializable
        {
            var writerSettings = new XmlWriterSettings();

            writerSettings.Indent = true;
            writerSettings.IndentChars = "    "; // 4 spaces
            writerSettings.Encoding = Encoding.UTF8;

            using (var xmlWriter = XmlWriter.Create(stream, writerSettings))
            {
                serializableObject.WriteXml(xmlWriter);
            }

            stream.Close();
        }

        /// <summary>
        /// Deserialize a stream and return the object.
        /// </summary>
        /// <typeparam name="T">The type of the object that returns from the deserialization.</typeparam>
        /// <param name="stream">The stream which contains the bytes to deserialize.</param>
        /// <returns>The object recovered.</returns>
        public static T DeserializeObject<T>(this Stream stream) where T : IXmlSerializable
        {
            using (var xmlTextReader = XmlReader.Create(stream))
            {
                var serializer = new XmlSerializer(typeof(T));
                T result = (T)serializer.Deserialize(xmlTextReader);
                stream.Close();
                return result;
            }
        }
    }
}

I have made a class which can serialize a generic object which implements the interface IXmlSerializable and can deserialize an xml file to that same object. The idea behind this is as follows: we are using a lot of xml files to (for example) write configurations to. Since the configuration classes are often different we needed a generic function to create and read the xml files.

The stream we use (atm) is a file stream since it is currently only used for reading and writing the xml files to and from disk. Later we might use other kind of streams as well.

I am wondering if what I have written is the correct way (obviously it works but can it be made better? )


texts: First SO chat message in Haskell followup

texts:
I have wrote an application which asks for the user id, and returns a link to the first available chat message:

Output

  Enter user id:
  322395
  Fetching data...
  http://chat.stackoverflow.com/transcript/17?m=231895#231895

Source code

module Main where

import Network.HTTP (simpleHTTP, getRequest, getResponseBody)
import Text.HTML.TagSoup
import Data.List (isInfixOf, isPrefixOf)

type Link   = String
type UserID = String

hostname = "http://chat.stackoverflow.com"

getDivText :: String -> [Tag String] -> String
getDivText m = innerText
             . takeWhile (~/= "
")
             . dropWhile (~/= m)

getURLContent :: Link -> IO String
getURLContent lnk = simpleHTTP (getRequest lnk) >>= getResponseBody

getMessages :: UserID -> IO Integer
getMessages uid = do

    contents <- getURLContent $ concat [ hostname
                                       , "/users/"
                                       , uid ]

    let count = getDivText "
"
              $ (parseTags contents)

    return (read count)

getFirstMessagePage :: UserID -> IO String
getFirstMessagePage uid = searchLast "" . ceiling . (/100)
                      =<< (fmap fromIntegral $ getMessages uid) where

    searchLast body pg = do

        let lnk = concat [ hostname
                         , "/users/"
                         , uid
                         , "/?tab=recent&pagesize=100&page="
                         , show pg ]

        body' <- getURLContent lnk

        if "monologue" `isInfixOf` body'
            then return body'
            else searchLast body (pg - 1)

getFirstMessage :: UserID -> IO Link
getFirstMessage ui = do

    page <- getFirstMessagePage ui

    let tags = filter step $ parseTags  page
        lnk  = fromAttrib "href" $ last tags

    return $ concat [ hostname, lnk ]

    where step t = isTagOpen t
                 && "/transcript" `isPrefixOf` fromAttrib "href" t

main :: IO ()
main = do

    putStrLn "Enter user id:"
    uid <- getLine

    putStrLn "Fetching data..."
    lnk <- getFirstMessage uid

    putStrLn lnk

I tried to keep the HTTP requests to a minimum.

If the user has removed messages, than there will be a difference between the pages available and the number of messages in the profile.

To solve this, the app recursively lowers the page number till it finds the last message.


texts: Dependency injection with Factory pattern sample code

texts:
I'm trying to understand and learn about the SOLID principle and especially Dependency Injection (DI) and Factory pattern.

Some background:

I have a production application that uses some "helpers" classes but those are all-in-one classes that I just copy into each new project. One of the (most used) functions is serialization. Our customer has strict demands about history/revision tracing so all important requests are serialized to XML and inserted in Sql server.

With SOLID, DI and Factory in mind I decided to rewrite ye old helpers into a fresh C# project and do things properly.

Below is sample code for Serializers (I have three, XML, JSON and Binary), shown is only the Json class but they are all pretty much the same.

My questions are:

Have I correctly implemented the Factory pattern?
Is the Dependency Injection the way it is supposed to be?

I'm not exactly sure if I even covered both patterns, a mix or perhaps just one?

I hope I made things clear, if not let me know, and thanks for helping me understand DI/Factory better.

Interface

namespace nUtility.Serializers
{
    public interface ISerializer
    {
        string InputString { get; set; }
        object InputObject { get; set; }

        string SerializeToString<T>(); // Need <T> because of the Xml serializer.
        object DeserializeFromString<T>();

    }

}

Factory

namespace nUtility.Serializers
{
    public class SerializersFactory
    {
        readonly ISerializer _iserializer;

        public SerializersFactory(ISerializer iserializer)
        {
            _iserializer = iserializer;
        }

        public string Serialize<T>() => _iserializer.SerializeToString<T>();

        public object Deserialize<T>() => _iserializer.DeserializeFromString<T>();
    }
}

Serializers (Json in this example)

using Newtonsoft.Json;
using System;

namespace nUtility.Serializers
{
    public class nSerializerJson : ISerializer
    {
        public string InputString { get; set; }
        public object InputObject { get; set; }

        public nSerializerJson(string inputString, object inputObject)
        {
            if (inputString == null)
                throw new ArgumentNullException(nameof(inputString));

            if (inputObject == null)
                throw new ArgumentNullException(nameof(inputObject));

            InputObject = inputObject;
            InputString = inputString;

        }

        public string SerializeToString<T>()
        {
            return JsonConvert.SerializeObject(InputObject);
        }

        public object DeserializeFromString<T>()
        {
            return JsonConvert.DeserializeObject<T>(InputString);
        }

    }
}

Program.cs

(SampleValues.InputObjectList is a simple test class which populates a List with 3 items)

var serializersFactory = new SerializersFactory(new nSerializerJson(expectedString, SampleValues.InputObjectList));
expectedString = serializersFactory.Serialize<List<SerializeTestObject>>();

Two more things:

Why not static class for serializers - I kinda agree with this answer on SO, however on a second thought changing my class (the parameter creep/constructor point in SO answer) would violate SOLID (the SRP) so I'm not sure if this is correct decision?

I know I'm breaking the Type and Namespace naming convetion by putting "nSerializers" etc. but this "n" prefix has been in the company for 9 years now and we're not prepared to let go yet :).


texts: Using arecord through subprocess to record in Raspberry Pi

texts:
I am using arecord through subprocess to record in raspberry pi : press joystick button to start recording and press button again to stop recording. I am really a noob in programming.

while True:
    for event in pygame.event.get(): # User did something
        if event.type == pygame.QUIT: # If user clicked close
            done=True # Flag that we are done so we exit this loop
    joystick_count = pygame.joystick.get_count()

    if(button_pressed == False and joystick.get_button( 0 ) == 1):
            button_pressed = True
            button_released = False

        if not is_recording:
        #detect joystick up
        if joystick.get_numaxes() >= 1 and joystick.get_axis(1) < 0:
            time.sleep(0.4)
            current_index = current_index + 1
            screen.blit(background, (0,0))
            screen.blit(images[current_index%len(images)], position1)
            pygame.display.update()
            print('JOYSTICK DOWN')

        #detect joystick down
        if joystick.get_numaxes() >= 1 and joystick.get_axis(1) > 0:
            time.sleep(0.4)
            current_index = current_index - 1
            screen.blit(background, (0,0))
            screen.blit(images[current_index%len(images)], position1)
            pygame.display.update()
            print('JOYSTICK UP')

    #detect button press
    if joystick.get_numbuttons() >= 1 and joystick.get_button( 0 ) == 0 and     button_pressed:
        button_pressed = False
        is_recording = not is_recording
        print(is_recording)

        #start recording
        if is_recording:
            dotting = dotting + 1
            #recording code
            filepath = "%s.wav" % datetime.now().strftime("%Y%m%d_%H%M%S")
            p=subprocess.Popen(['arecord', '--device=hw:1,0', '--format', 'S16_LE', '--rate', '44100-c1', filepath], shell=False)

            #change background
            background = pygame.image.load(background_image2).convert()
            #start timer
            stopFlag = Event()
            timer = MyTimer(stopFlag,time.clock())
            #timer.start()
            text2 = font.render('.', True, BLACK)

            screen.blit(background, (0,0))

            stopFlag.set()

            #record, remaining here

        #stop recording
        else:
            #stop recording, save record

            #back to previous question
            timer.do_run = False
            backToPreviousQuestion()
            background = pygame.image.load(background_image).convert()
            p.terminate()
    pygame.display.update()


texts:
I am trying to optimize an object filter function. Given an array of keys, I need to filter an object. I feel that creating a new object each time may degrade performance. Is there a way to mock the Array.prototype.filter function for objects?

function main() {
    var Types = {
        INTEGER:   { value: 'int'    },
        CHARACTER: { value: 'char'   },
        FLOAT:     { value: 'float'  },
        DOUBLE :   { value: 'double' },
        STRING:    { value: 'str'    },
        BOOLEAN:   { value: 'bool'   }
    };

    var Numbers = filterByKeys(Types, [ 'INTEGER', 'FLOAT', 'DOUBLE' ]);

    console.log(Numbers);
};

function listToSet(list) {
    var _set = {};
    if (Ext.isArray(list)) {
        for (var i = 0; i < list.length; i++) {
            _set[list[i]] = true;
        }
    }
    return _set;
};

function filterByKeys(obj, keep) {
    var result = {},
        unfiltered = keep === undefined,
        keys = listToSet(keep);
    if (Ext.isObject(obj)) {
        Ext.Array.each(Object.keys(obj), function(key) {
            if (unfiltered ||  (!unfiltered && keys[key])) {
                result[key] = obj[key];
            }
        });
    }
    return result;
};

Ext.onReady(function () {
    main();
});
.as-console-wrapper { top: 0; max-height: 100% !important; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/extjs/4.2.1/ext-all.js"></script>

Without ExtJS Dependency

function main() {
    var Types = {
        INTEGER:   { value: 'int'    },
        CHARACTER: { value: 'char'   },
        FLOAT:     { value: 'float'  },
        DOUBLE :   { value: 'double' },
        STRING:    { value: 'str'    },
        BOOLEAN:   { value: 'bool'   }
    };

    var Numbers = filterByKeys(Types, [ 'INTEGER', 'FLOAT', 'DOUBLE' ]);

    console.log(Numbers);
};

function listToSet(list) {
    var _set = {};
    if (isArr(list)) {
        for (var i = 0; i < list.length; i++) {
            _set[list[i]] = true;
        }
    }
    return _set;
};

function filterByKeys(obj, keep) {
    var result = {},
        unfiltered = keep === undefined,
        keys = listToSet(keep);
    if (isObj(obj)) {
        for (var key in obj) {
            if (unfiltered ||  (!unfiltered && keys[key])) {
                result[key] = obj[key];
            }
        };
    }
    return result;
};

var tObj='[object Object]',tArr='[object Array]',str=Object.prototype.toString;
var isArr='isArray'in Array?Array.isArray:function(v){return str.call(v)===tArr};
var isObj=str.call(null)===tObj?function(v){return v!==null&&v!==void 0&&str.call(v)===tObj&&v.ownerDocument===void 0}:function(v){return str.call(v)===tObj
};

$(document).ready(function () {
    main();
});
.as-console-wrapper { top: 0; max-height: 100% !important; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


texts: Selector targets overly obtuse - recommendations?

texts:
This presumably simple task resulted in much more scripting logic than I thought would be necessary.

Goal: when the user clicks a button inside one of the divs, it affects said div.

Issues: I did not wish to have my function target specific elements in my DOM. (ex. targeting <a> by ID). Also, because my function is traversing the DOM and being called in multiple instances, I had to use $(this) format so as not to be specific to one ID.

Anyways, here is my code:

$(".button").toggle(function(){
    $(this).parent().children(".innerBox").fadeIn();
}, function(){
    $(this).parent().children(".innerBox").fadeOut();
});

It works fine. However, can someone with a little more jQuery savvy please let me how I could execute this function in the future, without having to parse through siblings and making it more convoluted than need be?


texts:
I wrote a solution to the Coding Challenge SPOJ Prime in C++ using the Sieve of Eratosthenes. How much Ever I tried I could not get over the Time Limit on SPOJ which is 6s for Primes upto 1000000000.

Here is the Problem StateMent

  Input

  The input begins with the number t of test cases in a single line
  (t<=10). In each of the next t lines there are two numbers m and n (1
  <= m <= n <= 1000000000, n-m<=100000) separated by a space.

  Output

  For every test case print all prime numbers p such that m <= p <= n,
  one number per line, test cases separated by an empty line.

  Example

  Input:
    2
    1 10
    3 5

  Output:
    2 3 5 7

  3 5

  Warning: large Input/Output data, be careful with certain
  languages (though most should be OK if the algorithm is well designed)

How can I optimise this Code ?

/*
  * Generate Prime Numbers Between two given numbers.

  * Url : http://www.spoj.com/problems/PRIME1/
  * Dated : 16-Jun-2016

  * Performance Oriented , that's why I'm using C++. The AlgoRythm is the Famous Seive of Euractus
*/

#include <iostream>
#include <stdlib.h>
#include <vector>
#include <math.h>

using namespace std;

typedef long int bignum;
struct input {
    bignum start;
    bignum end;
};

// Functions
std::vector<input> parseInput();
void printPrimes(std::vector<bool> primes , bignum start , bignum end);
std::vector<bool> findPrimes(bignum start , bignum end);

int main(){
    system("clear");

    std::vector<bool> primes;
    std::vector<input> programInput;

    programInput = parseInput();

    for (int i = 0; i < programInput.size(); ++i){
        primes = findPrimes(programInput[i].start, programInput[i].end);
        printPrimes(primes , programInput[i].start, programInput[i].end);
    }
}

// Parse Input as an Array
std::vector<input> parseInput(){
    int testCases;
    bignum start;
    bignum end;

    cin >> testCases;

    std::vector<input> parsedInput(testCases);
    input currentInput;

    for (int i = 0; i < testCases; ++i){
        cin >> start >> end;
        currentInput = {start , end};
        parsedInput.push_back(currentInput);
    }

    return parsedInput;
}

// Print all the Primes
void printPrimes(std::vector<bool> primes , bignum start , bignum end){
    for (int i = start; i <= end; ++i){
        if (primes[i] == true){
            cout << i << '\n';
        }
    }
}

// Find all Prime Numbers between two numbers
std::vector<bool> findPrimes( bignum start , bignum end){

    // Define an array of Bits and Set all Bits to 1
    std::vector<bool> primeSeive(end);

    // Assign all Numbers upto end as True
    primeSeive.assign(end + 1, true);

    // 0 and 1 is not a prime
    primeSeive[0] = false;
    primeSeive[1] = false;

    for (int i = 2; i <= end ; ++i ){
        for (int j = 2; j <= (end / i); ++j){
            primeSeive[i*j] = false;
        }
    }

    return primeSeive;
}


texts:
This code implements quick sort and takes advantage of passing the list by reference. I'm looking for performance and general best practices.

import random

def quicksort(array, l=0, r=-1):
    # array is list to sort, array is going to be passed by reference, this is new to me, try not to suck
    # l is the left bound of the array to be acte on
    # r is the right bound of the array to act on

    if r == -1:
        r = len(array)

    # base case
    if r-l <= 1:
        return

    if r == -1:
        r = len(array)

    piviot = int((r-l)*random.random()) + l

    i = l+1 # Barrier between below and above piviot, first higher element
    swap(array, l, piviot) #piviot is now in index l

    for j in range(l+1,r):
        if array[j] < array[l]:
            swap(array, i, j)
            i = i+1

    swap(array, l, i-1) # i-1 is now the piviot

    quicksort(array, l, i-1)
    quicksort(array, i, r)

    return array

def swap(array, a, b):
    # a and b are the indicies of the array to be swapped

    a2 = array[a]
    array[a] = array[b]
    array[b] = a2

    return


texts:
I am relatively new to Perl and would like to check that the code I am writing docent have any major flaws in it or any bad practices.

This is a fairly simple sub but one that gets used often. Seems to work OK. Simply reads a text file that has parameters in it set by the user.

Config.txt

#cucm username and password of user associated with phones
username  = bob
password  = bobPass
#subnet details. Use /30 to start with to test 2 phones before entire subnet
subnet = 10.64.97.216/32
#HFS web server details IP and folder
webserver   = http://10.64.164.230/Desktop/
#image file names. Full and Thumbnail size
imgFullSize = screen_F.png
imgThumSize = screen_S.png

Perl sub that reads the file:

sub readConfigFile
{
print $lfh "Reading Config.txt file\n";
#path to the config file
my $configF = "config.txt";
if($DEBUG == 1){print "opening config File\n";}
open(my $configFhandle, '<', $configF) or die "Unable to open file, $!";
my @arrFileData=<$configFhandle>; #Slurp!

#go through each of the lines in the file
for (my $i = 0; $i <= $#arrFileData; ++$i)
{
local $_ = $arrFileData[$i];
if($DEBUG == 1){print "Reading line:$_";}
#check to see if the line is a comment... if it is skip it
if($_ =~ /^#/)
{
if($DEBUG == 1){print "Line is comment. Skiping:$_";}
next;
}

#split the line into the key and value
my @dataKV = split /=/, $_;
#check to see that Key and Value exist ... if not skip it
my $arrSize = @dataKV;
if($arrSize <= 1)
{
if($DEBUG == 1){print "Read Config: Error: No Key AND Value found:$_";}
next;
}
#remove all leading and end white spaces around elements
s{^\s+|\s+$}{}g foreach @dataKV;

for (my $x = 0; $x <= $#dataKV; $x++)
{
if($DEBUG == 1){print "Key:$dataKV[$x]\n";}
given($dataKV[$x])
{
when (/^username/)
{
$usr = $dataKV[++$x];
if($DEBUG == 1){print "Value Username:$dataKV[$x]\n";}
next;
}
when (/^password/)
{
$pass = $dataKV[++$x];
if($DEBUG == 1){print "Value password:$dataKV[$x]\n";}
next;
}
when (/^subnet/)
{
@arrSubnets = $dataKV[++$x];
if($DEBUG == 1){print "Value Subnet:$dataKV[$x]\n";}
next;
}
when (/^webserver/)
{
$webserver = $dataKV[++$x];
if($DEBUG == 1){print "Value webserver:$dataKV[$x]\n";}
next;
}
when (/^imgFullSize/)
{
$imgFull = $dataKV[++$x];
if($DEBUG == 1){print "Value imgFullSize:$dataKV[$x]\n";}
next
}
when (/^imgThumSize/)
{
$imgThum = $dataKV[++$x];
if($DEBUG == 1){print "Value imgThumSize:$dataKV[$x]\n";}
next
}
}#end of switch statement
}#end of for loop going through the key and value line
}#end of For loop going though the lines in the config

print "*********************************\n";
print "*Config imported\n";
print "*********************************\n";
print "*username    :$usr\n";
print "*password    :$pass\n";
print "*subnet      :@arrSubnets\n";
print "*webserver   :$webserver\n";
print "*imgFullSize :$imgFull\n";
print "*imgThumSize :$imgThum\n";
print "*********************************\n";
print "Is this above correct?[Y]:";
my $confirm = <>;
if($confirm !~ /^Y/)
{
print $lfh "Config file needs to be changed. Closing script down.\n";
close($lfh) or warn "Unable to close the file handle: $!";
close($configFhandle) or warn "Unable to close the file handle: $!";
print "Script will now terminate. Please amend \"Config.txt\" file and re-run script. Press     any key to terminate";
<>;
exit;
}
#close the file
close($configFhandle) or warn "Unable to close the file handle: $!";
print $lfh "Y pressed by User. Config all good.\n";
}

Any advise on how to improve this would be great.


texts:
I'm parsing (specifically tokenizing) a file, line-by-line. I've a method tokenize, that takes a string (one line of code, it can't take the whole file at once), breaks it into parts, and returns a generator that yields those parts until it reaches the end of line. This is all given. I implemented some methods, that will give me all of the file parts (i.e. feeding each line to tokenize, and sending each line parts). Here's what I did, I feel it's very cumbersome and ugly.

class tokenizer:
    def __init__(self,read_file):
        self.file = read_file
        self.line = self.file.readline()
        self.gen = tokenize(self.line)
        self.token = Token("","","","")

    def advance(self):
        return self.token

    def hasMoreTokens(self):
        try:
            self.token = self.gen.__next__()
        except StopIteration:
            self.line = self.file.readline()
            if self.line == '':
                return False
            self.gen = tokenize(self.line)
            self.token = self.gen.__next__()
        return True

with open(sys.argv[1],'r') as file:
    tknzr = tokenizer(file)
    while tknzr.hasMoreTokens():
        print(tknzr.advance())

Could you advise me on how to make a more logical and sensible implementation?


texts: Multi-replacement in a string, independently and prioritized

texts:
I need a shorter way to do this.

input: an ordered list of string pairs L; and a string s

task: for each pair (a,b), replace each occurrence a in s by b

note 1: the replacements have to be independent. The result of one replacement can not be taken as a trigger for a subsequent replacement
Example: L=(("a","b"),("b","c")); s="ab"; desired output: "bc", and not "cc".

note 2: L is ordered; the beginning of the list has higher priority, so if L is (("xyz", "P"), ("xy", Q)) and s is "wxyzabc" then the result should be "wPabc". If the order of L were reversed, the result would be "wQzabc".

bonus: it's a great thing if it can also handle regex patterns as replacement pairs

My idea:

iterate over L, and replace each source to a sequence of joker characters of equal length (the joker does not appear anywhere in the string), while noting the position where we found it.
then split the string to a list of strings of length 1, replace the first joker of each sequence with the desired target replacement, join the list back together and remove jokers.

Here is my implementation in Python:
def multireplace(text, changes):
    # find a possible joker character
    for num in range(256):
        joker = chr(num)
        if text.find(joker) != -1:
            continue

        jokerUsable = True
        for old,new in changes:
            if old.find(joker) != -1 or new.find(joker) != -1:
                jokerUsable = False
                break

        if jokerUsable:
            break

    # list of lists, each list is about one replacement pair,
    # and its elements will be the indices where they have to be done
    index_lists=[]

    for old, new in changes:
        indices = []
        pattern = re.escape(old)
        for m in re.finditer(pattern, text):
            indices.append(m.start())

        text = text.replace(old, joker*len(old))
        index_lists.append(indices)

    character_list = list(text)
    for i in range(len(index_lists)):
        old, new = changes[i]
        for place in index_lists[i]:
            character_list[place] = new

    # remove jokers
    i=0
    while i < len(character_list):
        if character_list[i] == joker:
            del character_list[i]
        else:
            i+=1

    return "".join(character_list)

This is very cumbersome, and seems to be a standard problem, so I was wondering if there is a standard way of doing this.


texts: Recursive component JSON configuration builder

texts:
My goal here is to make data driven nested forms in React. I've created a function that points to a form configuration JSON, which can contain both components and references to other configuration files, with their own components. I'm splitting it to separate files to keep things DRY and to get an easier overview (hierarchy can be about 5 levels deep, one JSON file would be rather hard to overview).

I've introduced $ref to be able to point to another JSON configuration and $extends for inheritance (configurations can differ dependings on context but have common properties). args will be spread out as props to the component.

config-builder.js

function createConfiguration(configuration) {
  return _.reduce(configuration.components, (result, data) => {
    // if it's a reference to another configuration - fetch it - otherwise use as is
    const component = data.$ref ? getComponentByReference(data) : data
    return addResult(result, component.key, component.components ? { ...component, components: createConfiguration(component) } : component)
  }, {})
}

function getComponentByReference(data) {
  const component = _.merge({}, configMap[data.$ref], data)
  const result = component.$extends ? _.merge({}, configMap[component.$extends], component) : component
  // clear internal values like $ref, $extends
  return _.omitBy(result, (value, key) => _.startsWith(key, '$'))
}

// use an array to wrap results to handle multiple keys of the same type on same level
function addResult(result, key, data) {
  if(!result[key]) {
    result[key] = [data]
  } else {
    result[key].push(data)
  }
  return result
}

foo.json

{
  "key": "foo",
  "type": "DefaultContainer",
  "components": [
    { "key": "name", "type": "Text" },
    { "$ref": "Bar" }
  ]
}

bar.json

{
  "key": "bar",
  "type": "DefaultContainer",
  "components": [
    { "key": "id", "type": "DropDown", "args": { options: [] } }
  ]
}

https://jsfiddle.net/wkmrp4gc/


texts:
I was wondering about the best way to return the most common items in a list.  So far the best way I could do it was:

//@param string $this->matched_ids_list = 1,11,12,11,12,
$this->matched_ids_list = "," . $this->matched_ids_list;
$this->ids_count = explode(",", $this->matched_ids_list);
$this->ids_count = array_unique($this->ids_count); //See below why this is done
foreach ($this->ids_count as $this->id) {
    $this->appearences[$this->id] = substr_count($this->matched_ids_list, ",{$this->id},");
    //[1] => 1, [11] => 2, [12] => 2
}
$this->most_appearences = max($this->appearences); //2
foreach ($this->appearences as $this->id => $this->times) {
    if ($this->times == $this->most_appearences) {
        $this->top_ids .= $this->id . ",";
    }
}
$this->top_ids = rtrim($this->top_ids, ",");
echo "tops = " . $this->top_ids;

It basically just works for 1,11,12,11,12, and returns 11,12, but it seems a bit overcomplicated. I've been at it for some time now.  Am I doing more steps than necessary, missing a built-in function or something?

I might be dealing with hundreds of ids, so performance is important.

Would this step:

foreach ($this->ids_count as $this->id) {
    $this->appearences[$this->id] = substr_count($this->matched_ids_list, ",{$this->id},");
    //[1] => 1, [11] => 2, [12] => 2
}

do more looping than necessary if array_unique wasn't used?

For instance, if ids_count was

[0] => 1, [1] => 11, [2] => 12, [3] => 11, [4] => 12

it would count how many times 11 and 12 appear on matched_ids_list twice. Instead, I use array_unique so ids_count is

[0] =>, [1] => 1, [2] => 11, [3] => 12


texts: Union find using unordered map and templates in C++

texts:
I tried to implement union find (disjoint set data structure) algorithm in C++. There I used unordered_map data structure. Is there a better way of doing this using any other data structure.

While finding the root I have used the following way.

int root(int i){
        if(i != parent[i]) parent[i] = root(parent[i]);
        return parent[i];
    }

Also, I can implement it as

int root(int i){
        while (i != parent[i]){
            parent[i] = parent[parent[i]]);
            i = parent[i];
        }
        return i;
    }

It seems like both are same other than the (recursive and iterative) approach for me. Which is better here!

The following is my implementation.

#include<iostream>
#include<unordered_map>
#include<vector>
using namespace std;

class Disjoinset{

    private:

    unordered_map<int, int> parent, rank;
    // find the root
    int root(int i){
        if(i != parent[i]) parent[i] = root(parent[i]);
        return parent[i];
    }

    public:

    // initialize the parent map
    void makeSet(int i){
        parent[i] = i;
    }
    // check for the connectivity
    bool is_connected(int p, int q){
        return root(p) == root(q);
    }
    // make union of two sets
    void Union(int p, int q){

        int proot = root(p);
        int qroot = root(q);

        if(proot == qroot) return;

        if(rank[proot] > rank[qroot]) {
            parent[qroot] = proot;
        }
        else{
            parent[proot] = qroot;
            if(rank[proot] == rank[qroot]) rank[qroot]++;
        }
    }
};

Driver code

int main(){
    vector<int> arr = {1,2,9,8,6,5,7};
    Disjoinset dis; // create a disjoint set object
    for(int x: arr){
        dis.makeSet(x); // make the set
    }
    dis.Union(1,9); // create connections
    dis.Union(6,5);
    dis.Union(7,9);
    cout <<  dis.is_connected(1,7) << endl; // check the connectivity.
    cout <<  dis.is_connected(1,6) << endl;
    return 0;
}

Thereafter I tried to use generics and I came up with following. Can someone help me on this as I am new to C++. Is my implementation is correct or are there any better approach.

#include<iostream>
#include<unordered_map>
#include<vector>
#include<string>
using namespace std;

template<class T>
class Disjoinset{

    private:

    unordered_map< T, T> parent;
    unordered_map< T, int>rank;
    // find the root
    T root(T i){
        if(i != parent[i]) parent[i] = root(parent[i]);
        return parent[i];
    }

    public:

    // initialize the parent map
    void makeSet(T i){
        parent[i] = i;
    }
    // check for the connectivity
    bool is_connected(T p, T q){
        return root(p) == root(q);
    }
    // make union of two sets
    void Union(T p, T q){

        T proot = root(p);
        T qroot = root(q);

        if(proot == qroot) return;

        if(rank[proot] > rank[qroot]) {
            parent[qroot] = proot;
        }
        else{
            parent[proot] = qroot;
            if(rank[proot] == rank[qroot]) rank[qroot]++;
        }
    }
};

int main(){
    vector<string> arr = {"amal", "nimal", "kamal", "bimal", "saman"};
    Disjoinset <string>dis; // create a disjoint set object
    for(string x: arr){
        dis.makeSet(x); // make the set
    }
    dis.Union("amal", "kamal"); // create connections
    dis.Union("kamal", "nimal");
    cout <<  dis.is_connected("amal", "nimal") << endl; // check the connectivity.
    cout <<  dis.is_connected("bimal", "amal") << endl;
    return 0;
}

Thank you for every answer.


texts: Efficiently swapping the first and last five bits in an unsigned 16-bit integer in C

texts:
I'm programming on an 8-bit Z80 embedded system, and encountered a problem. The program needs to render some pixels to the screen. The color is stored as RGB565 format as a 16-bit unsigned integer, but the system color is encoded as BGR565, so I've devised the following code to swap the first and last 5 bits in a uint16_t.

Note that flash is uint8_t *, pointed to the flash memory, and each cell is seen as uint8_t.

/* read lower 8-bit */
uint16_t color = flash[idx];

/* read higher 8 bits */
color |= ((flash[idx + 1]) << 8);

/*
 * This converts RGB565 to BGR565 by swapping the first/last five bits.
 * For the last five bits, we extract and bitshift it to the beginning,
 * and for the first five bits, we extract and bitstift it to the end.
 * Then, we clear the original first and last five bits, and apply the
 * bitshifted version on it to swap them.
 */
uint16_t tmp = (color & 0x001F) << 11;
tmp |= (color & 0xF800) >> 11;
color &= 0xFFE0 & 0x07FF; /* at least the compiler can optimize this as 0x07e0 */
color |= tmp;

If I'm programming with a standard compiler, like GCC or Clang, I would be just happy about my solution. But this embedded platform only supports relatively primitive compilers without advanced optimization techniques to remove duplicate computations.

In fact, the best code optimization to this problem is "no code", just make the stored format to be consistent with the system format. But using it as a chance of learning, I'm curious to know if there is a faster way to do the computation above in pure C, especially when we're dealing with a particularly slow machine, like a Z80.


texts: VBA script that copies data from one workbook to another

texts:
I've written a perl script that creates a csv file consolidated of values from multiple files. This file that I've created is then used to update certain rows in a main excel spreadsheet my company uses. I've created a VBA script that scans the main file for values that match up with those from my perl generate file and it all works fine.

The problem is that it takes about four minutes to run. The main file is only about 1000 or so rows long, and the file I'm generating only comes out to about 100 rows top. My logic in my script is to scan through every row in the main file for each row in the generated file:

Sub TransferDataToForecast()
  Dim strPath As String  'File path
  Dim foreb As Workbook  'Workbook that the file path points to
  Dim copys As Worksheet 'Worksheet that contains the data to be copied
  Dim dests As Worksheet 'Worksheet that is being copied to
  Dim copysLastRow As Long 'Last row of data worksheet
  Dim copysLastCol As Long 'Last column of data worksheet
  Dim destsLastRow As Long 'Last row of target worksheet
  Dim counter As Long      'Counter for loop control

  Application.ScreenUpdating = False
  Set copys = ActiveWorkbook.ActiveSheet

  'Get user to select target workbook
  strPath = BrowseForFile("Select the DM Forecast file you want to update")
  If strPath = vbNullString Then Exit Sub

  'Debug.Print strPath
  Set foreb = Workbooks.Open(strPath)
  Set dests = foreb.Worksheets("Material_Usage_Prim")

  'Get last row of each worksheet and last column of data sheet in order to help with loop control
  copysLastRow = copys.Cells(Rows.Count, 3).End(xlUp).Row
  copysLastCol = copys.Cells(2, Columns.Count).End(xlToLeft).Column
  destsLastRow = dests.Cells(Rows.Count, 1).End(xlUp).Row
  counter = 0

  'Loop through each row of the data sheet
  For i = 2 To copysLastRow
  Debug.Print "i is "; i
  'For each row in the data sheet, loop through every row in the destination sheet in order
  'to find matching id's between each sheet
    For j = 2 To destsLastRow
      Debug.Print "j is "; j
      If Trim(copys.Cells(i, 3).Value2) = vbNullString Then Exit For   'This exits the loop if the data sheet has a blank row
      If dests.Cells(j, 4) <> copys.Cells(i, 4) Then GoTo NextDestLoop 'This skips the rest of the current j if values in col4 of each sheet don't match up

      'Have to use replace due to differing naming conventins among people i.e. _ vs - (this row can be changed for specific peoples needs)
      'This checks that columns 3 and 4 of each sheet match
      If Replace(copys.Cells(i, 3).Value2, "_", "") = Replace(dests.Cells(j, 3).Value2, "_", "") And copys.Cells(i, 4).Value2 = dests.Cells(j, 4).Value2 Then
        counter = 0
        For k = 5 To copysLastCol
            Debug.Print "k is "; k
            'Go to 14, because we want to keep the first 14 weeks (columns)
            If counter < 14 Then
                counter = counter + 1
                GoTo NextIteration
            End If
            counter = counter + 1
            dests.Cells(j, k).Value2 = copys.Cells(i, k).Value2
     NextIteration:
            Next k
      End If
      If counter = copysLastRow Then GoTo NextLoop
   NextDestLoop:
    Next j
NextLoop:
  Next i

  Application.ScreenUpdating = True
  MsgBox ("Data has been transferred.")
End Sub

'This function brings up a window for the user to pick an excel file they want to update
Private Function BrowseForFile(Optional strTitle As String) As String
  Dim fDialog As FileDialog
    On Error GoTo Err_handler
    Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
    With fDialog
        .Title = strTitle
        .Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb", 1
        .AllowMultiSelect = False
        .InitialView = msoFileDialogViewList
        If .Show <> -1 Then GoTo Err_handler:
        BrowseForFile = fDialog.SelectedItems.Item(1)
    End With

lbl_Exit:
    Exit Function
Err_handler:
    BrowseForFile = vbNullString
    Resume lbl_Exit
End Function

If anyone can help me to make this more efficient I would be grateful!


texts: Transpose of a very large matrix with fewer than 1021 rows with Python

texts:
I have a matrix of fewer than 1021 rows. I'd like to take its transpose, but it is too big for GNU datamash and awk solutions to fit into memory.

To keep memory overhead low, my thought is that I keep a list of read-only file pointers to the start of each row. I then read out bytes from each pointer until I hit a delimiter (tab or newline).

Because I have fewer than 1021 rows, I won't hit the usual 1024 OS-based file pointer limit.

Once I have read out a field from all file pointers, I write a newline character and start over, reading a field from all file pointers, and again, until no more bytes are available:

#!/usr/bin/env python

import sys
import os

try:
  in_fn = sys.argv[1]
except ValueError as ve:
  sys.exit(-1)

def get_size(fn):
  st = os.stat(fn)
  return st.st_size

#
# 1) Read in file offsets to start of new line
# 2) Open up a file pointer to that offset
# 3) Process each file pointer to get a value until a delimiter is hit, then write it as a row of output
#

size = get_size(in_fn)

fps = []
new_fp = open(in_fn, 'r')
new_fp.seek(0, 0)
fps.append(new_fp)

with open(in_fn, 'r') as f:
  byte = f.read(1)
  while byte:
    if byte == '\n':
      new_offset = f.tell()
      if new_offset < size:
        new_fp = open(in_fn, 'r')
        new_fp.seek(new_offset, 0)
        fps.append(new_fp)
      else:
        break
    byte = f.read(1)

while size > 0:
  for fi, f in enumerate(fps):
    byte = f.read(1)
    size -= 1
    while byte:
      if byte == '\t' or byte == '\n':
        if fi != len(fps) - 1: sys.stdout.write('\t')
        break
      sys.stdout.write('%s' % (byte))
      byte = f.read(1)
      size -= 1
  sys.stdout.write('\n')

for f in fps:
  f.close()

Is there anything I can do to improve the performance of this? Reading and processing a set of file pointers one byte at a time seems quite expensive. However, I need to find newline characters to build offsets and file pointers. Is there a cleverer/faster way (in Python) to find the byte offsets of newlines?


texts: Nth Prime via Sieve of Erastothenes as iterator

texts:
I did the Nth Prime problem on exercism.io. Now I need feedback what can be done better / improved to make the most of rust. The code calculates the nth prime. I chose to provide the sieve of Eratosthenes as iterator in Rust. The code can be seen here: https://exercism.io/tracks/rust/exercises/nth-prime/solutions/22ef08ba47d0401b9ef506ee426de5cd
///implements a sieve of Eratosthenes as iterator
///https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
///The iterator skips non-primes and does the necessary
///calculations for the next.
struct Sieve {
    ///current index from where the next prime is searched
    index: usize,
    ///the sieve
    marked_numbers: Vec<bool>,
}

///largest number to be checked to be prime
///could be optimized to actually point to
///a prime number.
const MAX_SIEVE_SIZE: usize = 1_000_000;

impl Sieve {
    fn new() -> Self {
        let mut numbers = vec![true; MAX_SIEVE_SIZE];
        numbers[0] = false; //no prime
        numbers[1] = false; //no prime - 2 is the first prime
        Sieve {
            index: 0,
            marked_numbers: numbers,
        }
    }

    ///marks all multiples of value to be non-prime
    ///to be used by next
    fn mark_multiples(&mut self, value: usize) {
        self.marked_numbers[self.index..]
            .iter_mut()
            .step_by(value)
            .for_each(|x| *x = false);
    }
}

impl std::iter::Iterator for Sieve {
    type Item = usize;

    fn next(&mut self) -> Option<Self::Item> {
        let slice = &self.marked_numbers[self.index + 1..];
        let position = slice.iter().position(|x| *x == true);
        match position {
            Some(x) => {
                self.index = x + self.index + 1;
                self.mark_multiples(self.index);
                Some(self.index)
            }
            None => None,
        }
    }
}

pub fn nth(n: usize) -> usize {
    let mut sieve = Sieve::new();
    sieve.nth(n).unwrap() //not nice but the API works this way
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn first_prime_is_2() {
        let mut sieve = Sieve::new();
        assert_eq!(sieve.next().unwrap(), 2);
    }

    #[test]
    fn first_few_primes_are() {
        let sieve = Sieve::new();
        let expected = [2, 3, 5, 7];
        sieve
            .zip(expected.iter())
            .for_each(|x| assert_eq!(x.0, *x.1 as usize));
    }
}
```


texts:
I am starting to learn Ruby.

This is pretty much the first thing I have coded. It's very simple; it's a prime number calculator. Since this is my first Ruby code, I would like some review on the following:

Adherence to Ruby standards
Is it done in the Ruby way (the way a Rubyist would have coded it)?
Adherence to Ruby naming conventions

What I am not looking for is a review on the prime number algorithm. I know there are more efficient ones out there.

File: prime_numbers.rb

class PrimeNumber
    def is_prime_kata(number)
        if number == 1 then return false end

        max = Math.sqrt(number)

        (2..max).any? do |i|
            if number % i == 0 then return false end
        end

        true
    end
end

File: prime_numbers_test.rb

require 'test/unit'
require 'set'
require_relative 'prime_numbers.rb'

class PrimeNumberTest < Test::Unit::TestCase
    def test_is_prime
        primeNumbers = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

        primeNumber = PrimeNumber.new

        for i in primeNumbers
            result = primeNumber.is_prime_kata(i)
            assert_equal(true, result)
        end

        primeNumbersSet = Set.new(primeNumbers)
        allNumbersSet = Set.new(1..100)

        nonPrimeNumbersSet = allNumbersSet - primeNumbersSet

        for i in nonPrimeNumbersSet
            result = primeNumber.is_prime_kata(i)
            assert_equal(false, result)
        end
    end
end


texts:
My goal was to click every single hyperlink under the "Shortcuts" header on here.

The code looks like it works to me, but if you have ideas that could help me improve/change I would love to hear them! Please do not be to critical of my variable naming.

#   Author: Brandon Finley
#   Date: 5/17/2017
#   Purpose: Clicks on every link of the selenium2library shortcuts by counting how many
#            elements it has in the table.
*** Settings ***
Library     Selenium2Library
Suite Setup   Open Browser   http://robotframework.org/Selenium2Library/Selenium2Library.html   chrome

*** Variables ***
${SHORTCUTS} =  dom=document.getElementsByClassName("shortcuts")[0].childNodes

*** Test Cases ***
Testing
    #   First half of the shortcuts table has shortcuts-container xpath name
    ${Cuts-Container} =  get matching xpath count  //*[@id="shortcuts-container"]/div/a
    #   Second half of the shortcuts table has keywords-container xpath name
    ${Key-Container} =  get matching xpath count  //*[@id="keywords-container"]/table/tbody/tr
    #   Count is the addition of both xpath names integer
    ${COUNT} =  evaluate  ${Cuts-Container}+${Key-Container}

    #   Simple for loop that will only click on every odd element because every even
    #   element is a non-clickable symbol used to make the shortcuts table look pretty
    : FOR   ${INPUT}    IN RANGE    0   ${COUNT}
    \   ${ODD?} =    evaluate  ${INPUT}%2
    \   Only Click Odd  ${ODD?}   ${INPUT}
    \   sleep   .2s

*** Keywords ***
Only Click Odd
    [Arguments]  ${EvenOrOdd}    ${Counter}
    run keyword if  ${EvenOrOdd}==1  click element  ${SHORTCUTS}[${Counter}]


texts:
I am working on a Web Scraper for the first time using Test Driven Development, however I have caught myself into a huge CRAP (Change Risk Anti-Patterns) index (56) and I can not seem to find a solution about this.

<?php

/**
 * Code Snippet MIT licensed taken from proprietary source.
 */

namespace WebScraper;
use WebScraper\Contracts\Scraper;
use WebScraper\Collections\BaseCollection;
use WebScraper\Exceptions\EngineIgnitionException;

class Engine
{
    protected $targetAdAge;

    protected $currentAdAge = 1;

    protected $targetPage  = 999;

    protected $currentPage = 1;

    public function __construct(Scraper $scraper, BaseCollection $collection)
    {
        $this->scraper    = $scraper;
        $this->collection = $collection;
    }

    public function start($targetAdAge = null)
    {
        if ($this->collection->count() >= 1)
        {
            throw new EngineIgnitionException("Engine can not be ran twice in the same instance.");
        }

        if (array_key_exists('adage', $requestData = $scraper->queryBuilder()->buildArray()))
        {
            $this->targetAdAge = $requestData['adage'];
        }

        if (! $this->targetAdAge)
        {
            throw new EngineIgnitionException("targetAdAge value is a core part of the Engine and it is missing.");
        }

        for ($this->currentAdAge = 1; $this->currentAdAge <= $this->targetAdAge; ++$this->currentAdAge)
        {
            for ($this->currentPage = 1; $this->currentPage <= $this->targetPage; ++$this->currentPage)
            {
                $this->scraper->queryBuilder()->adage($this->currentAdAge)->page($this->currentPage);
                $parser = $this->scraper->extract()->parse();
                $items = $parser->items();
                $this->targetPage = $parser->maxPages();
                foreach ($items as &$item)
                {
                    $item->age = (new DateTime('now'))->modify("-{$this->currentAdAge} day");
                    $this->collection->push($item);
                }

                unset($item);
            }
        }

        return $this->collection;
    }
}

The main issue is in the start() function.

Basically, what this class does is building an Engine to web scrape a website that uses pagination to show their data, and because of so I am forced to use iterators and scrape every single page to gather all the data. Line 48 is the function that does the dirty job and scrapes the website.

The thing is, this function is likely to take even minutes to complete if there's a lot of data to extract or if scraping filters are very broad. This is the only function where I really do not know how to refactor it in a better way. The good thing, however, is that every other single class has a low CRAP index (< 5) and Code Coverage is above 85%.

How do I refactor the iterator of page and adAge?


texts: Converting a numerical string to the equivalent multipled by 100

texts:
Method:

Input: a string which represents an integer or a non-integer value.
Output: a string which represents the same value multiplied by 100.

Requirements:

No floating-point unless needed.
No leading 0s in the part left of the floating-point.
No trailing 0s in the part right of the floating-point.

I cannot use the Double.parseDouble method, because it sometimes yields an "ugly" result.

For example:

String.valueOf(Double.parseDouble("0.0071")*100)); // yields 0.7100000000000001
String.valueOf(Double.parseDouble("0.072" )*100)); // yields 7.199999999999999

Here is my code, and I would like to know if there are any cavities in it:

String MultiplyBy100(String number)
{
    StringBuilder sb = new StringBuilder();

    String[] parts = number.split("\\.");
    if (parts.length == 1)
        sb.append(parts[0]+"00");
    else if (parts[1].length() == 1)
        sb.append(parts[0]+parts[1]+"0");
    else if (parts[1].length() == 2)
        sb.append(parts[0]+parts[1]);
    else
        sb.append(parts[0]+parts[1].substring(0,2)+"."+parts[1].substring(2));

    if (sb.charAt(0) == '-')
    {
        while (sb.length() > 2 && sb.charAt(1) == '0' && sb.charAt(2) != '.')
            sb.deleteCharAt(1);
    }
    else
    {
        while (sb.length() > 1 && sb.charAt(0) == '0' && sb.charAt(1) != '.')
            sb.deleteCharAt(0);
    }

    return sb.toString();
}


texts: Divide all elements in the column of a sparse matrix

texts:
Using Python Scipy, I am trying to divide all numbers in all columns of a sparse matrix (400K × 500K, density 0.0005), by the sum of the squares of all numbers in a column.

If a column is [ [ 0 ] , [ 2 ] , [ 4 ] ], the sum of the squares is 20, so after computation the column should be [ [ 0 ] , [ 0.1 ] , [ 0.2 ] ].

This was my first attempt:

# Loading the sparse matrix
csc = np.load('sparse_matrix.npz')
csc = sp.csc_matrix((csc['data'], csc['indices'], csc['indptr']), shape = csc['shape'], dtype=np.float)

# Computing sum of squares, per column
maxv = np.zeros((csc.shape[1]))
for i in xrange(csc.shape[1]) :
    maxv[i] = sum(np.square(csc[:,i].data))

# Division of non-zero elements by the corresponding sum
for i in xrange(csc.shape[1]) :
    x,y = csc[:,i].nonzero()
    del y
    if x.shape[0] > 0 :
        csc[x,i] = np.array(csc[x,i].todense()) / maxv[i]

However, this seemed to take forever.  I improved the second part (using SciPy sparse: optimize computation on non-zero elements of a sparse matrix (for tf-idf)):

csc = np.load('sparse_matrix.npz')
csc = sp.csc_matrix((csc['data'], csc['indices'], csc['indptr']), shape = csc['shape'], dtype=np.float)

# THIS PART is slow
# Computing sum of squares, per column
maxv = np.zeros((csc.shape[1]))
for i in xrange(csc.shape[1]) :
    maxv[i] = sum(np.square(csc[:,i].data))

# Division of non-zero elements by the corresponding sum
csc = sp.csr_matrix(csc)
xs,ys = csc.nonzero()
csc.data /= maxv[ys]
csc = sp.csc_matrix(csc)

… but I wonder if the computation of squares part can be improved further.


texts:
Given an array of integers arr where each element is at most k places
  away from its sorted position, code an efficient function
  sortKMessedArray that sorts arr. For instance, for an input array of
  size 10 and k = 2, an element belonging to index 6 in the sorted array
  will be located at either index 4, 5, 6, 7 or 8 in the input array.

Analyze the time and space complexities of your solution.

  Example:

  input:  arr = [1, 4, 5, 2, 3, 7, 8, 6, 10, 9], k = 2

  output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Constraints:

[time limit] 5000ms

[input] array.integer arr

1 ≤ arr.length ≤ 100
[input] integer k

1 ≤ k ≤ 20
[output] array.integer

My approach improved :

import java.io.*;
import java.util.*;

class Solution {

  static int[] sortKMessedArray(int[] arr, int k) {

   int arrLen = arr.length;

   PriorityQueue<Integer> queue = new PriorityQueue<Integer>();

   //Store the first k+1 elements in the heap
   for( int i = 0; i <= k; i++ )
      queue.add(arr[i]);

   for( int i = k+1; i < arrLen; i++ )
   {
     //Extract the minimum element in the priority queue
     arr[i - (k + 1)] = queue.poll();

     //Add the next element to the queue
     queue.add(arr[i]);
   }

   //Extract the remaining elements from the queue and put it in the next index available
    for( int i = 0; i <= k; i++ )
      arr[ arrLen - k - 1 + i] = queue.poll();

   return arr;
  }

  public static void main(String[] args) {
   int [] arr = sortKMessedArray(new int[]{1,0},1);
   for( int m : arr )
      System.out.println(m);
  }

}

I have the following questions regarding my approach:

1) How can I further improve my approach?

2) Is there a better way to solve this question?

3) Are there any grave code violations that I have committed?

4) Can space and time complexity be further improved?

Reference


texts: A 'flexible' VBA approach to lookups using arrays, scripting dictionary and user input

texts:
In my previous post Optimise compare and match method using scripting.dictionary in VBA I wanted to address optimising the scripting.dictionary approach I was using and I feel I have achieved that (feel free to correct me though!). I am now implementing user input boxes which have also increased the complexity and now have duplicated code. I'm wondering if there might be a better way of doing this?

This is all one sub procedure and it is working well as far as I have been able to test it, I have just broken it up with comments.

Public Sub arrayMatchv3()

    Dim workingArray As Variant
    Dim workingCell As Range
    Dim workingLastRow As Long
    Set workingCell = Application.InputBox("Select the first cell of your comparison column.", Type:=8)
    workingLastRow = workingCell.End(xlDown).Row
    With workingCell
        workingArray = Range(.Address, Left(.Address, Len(.Address) - 1) & workingLastRow)
    End With

    Dim targetArray As Variant
    Dim targetCell As Range
    Set targetCell = Application.InputBox("Select the first cell of your target column.", Type:=8)
    With targetCell
        targetArray = Range(.Address, Left(.Address, Len(.Address) - 1) & workingLastRow)
    End With

    Dim i As Long

With the below ReDim section, I am unsure of any method to write a single column from the worksheet to a single column within a 2d array, so have resorted to looping to populate the second column. Perhaps it isn't really needed to add them both into a single array?

    ReDim Preserve workingArray(1 To UBound(workingArray, 1), 1 To UBound(workingArray, 2) + 1)
    For i = LBound(workingArray, 1) To UBound(workingArray, 1)
        workingArray(i, UBound(workingArray, 2)) = targetArray(i, 1)
    Next i

The below three code sections are duplicates of the three above but using different variables, not sure how to address this?

    Dim comparisonArray As Variant
    Dim comparisonCell As Range
    Dim comparisonLastRow As Long
    Set comparisonCell = Application.InputBox("Select the first cell of the column to match values with.", Type:=8)
    comparisonLastRow = comparisonCell.End(xlDown).Row
    With comparisonCell
        comparisonArray = Range(.Address, Left(.Address, Len(.Address) - 1) & workingLastRow)
    End With

    Dim returnArray As Variant
    Dim returnCell As Range
    Set returnCell = Application.InputBox("Select the first cell from the column you wish to return values from.", Type:=8)
    With returnCell
        returnArray = Range(.Address, Left(.Address, Len(.Address) - 1) & comparisonLastRow)
    End With

    ReDim Preserve comparisonArray(1 To UBound(comparisonArray, 1), 1 To UBound(comparisonArray, 2) + 1)
    For i = LBound(comparisonArray, 1) To UBound(comparisonArray, 1)
        comparisonArray(i, UBound(comparisonArray, 2)) = returnArray(i, 1)
    Next i

I'm quite happy with the remaining code which includes a lot of feedback from my previous posts, but critique is still welcome, especially if it can address any potential performance issues.

    With New Scripting.Dictionary
        .CompareMode = vbTextCompare

        For i = LBound(comparisonArray, 1) To UBound(comparisonArray, 1)
            If Not .Exists(comparisonArray(i, 1)) Then
                .Add comparisonArray(i, 1), comparisonArray(i, 2)
            End If
        Next i

        Dim arrayToPaste As Variant
        ReDim arrayToPaste(1 To UBound(workingArray, 1), 1 To 1)
        For i = LBound(workingArray, 1) To UBound(workingArray, 1)
            If Len(workingArray(i, 2)) = 0 Then
                If .Exists(workingArray(i, 1)) Then
                    arrayToPaste(i, 1) = .Item(workingArray(i, 1))
                End If
            Else
                arrayToPaste(i, 1) = workingArray(i, 2)
            End If
        Next i
    End With

    With targetCell
        Range(.Address, Left(.Address, Len(.Address) - 1) & workingLastRow) = arrayToPaste
    End With

End Sub


texts:
Following is my code to check if string has unique chars.

I am assuming string would have only ascii chars.

#include <iostream>
#include <array>
using namespace std;

int main()
{
    array<char,255> arr;

    char *str = "ABCADEFGHIJKL";

    for (int i = 0; i < 255; i++)
        arr[i] = 0;

    int i = 0;

    while (str[i])
    {
        if (arr[str[i]] == 1)
        {
            cout << "Not unique";
            return 0;
        }

        arr[str[i]] = 1;

        i++;
    }

    cout << "Unique";

    return 0;
}


texts: Parallel foreach with configurable level of concurrency

texts:
The purpose of this code is to let me loop over 100 items (up to MAX_CONCURRENT at a time), performing some action on them, and then return only once all items have been processed:

/// <summary>Generic method to perform an action or set of actions
/// in parallel on each item in a collection of items, returning
/// only when all actions have been completed.</summary>
/// <typeparam name="T">The element type</typeparam>
/// <param name="elements">A collection of elements, each of which to
/// perform the action on.</param>
/// <param name="action">The action to perform on each element. The
/// action should of course be thread safe.</param>
/// <param name="MaxConcurrent">The maximum number of concurrent actions.</param>
public static void PerformActionsInParallel<T>(IEnumerable<T> elements, Action<T> action)
{
    // Semaphore limiting the number of parallel requests
    Semaphore limit = new Semaphore(MAX_CONCURRENT, MAX_CONCURRENT);
    // Count of the number of remaining threads to be completed
    int remaining = 0;
    // Signal to notify the main thread when a worker is done
    AutoResetEvent onComplete = new AutoResetEvent(false);

    foreach (T element in elements)
    {
        Interlocked.Increment(ref remaining);
        limit.WaitOne();
        new Thread(() =>
        {
            try
            {
                action(element);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error performing concurrent action: " + ex);
            }
            finally
            {
                Interlocked.Decrement(ref remaining);
                limit.Release();
                onComplete.Set();
            }
        }).Start();
    }
    // Wait for all requests to complete
    while (remaining > 0)
        onComplete.WaitOne(10); // Slightly better than Thread.Sleep(10)
}

I include a timeout on the WaitOne() before checking remaining again to protect against the rare case where the last outstanding thread decrements 'remaining' and then signals completion between the main thread checking 'remaining' and waiting for the next completion signal, which would otherwise result in the main thread missing the last signal and locking forever. This is faster than just using Thread.Sleep(10) because it has a chance to return immediately after the last thread completes.

Goals:

Ensure thread safety - I want to be sure I won't accidentally return too early (before all elements have been acted on), and be sure that I don't become deadlocked or otherwise stuck.
Add as little overhead as possible - minimizing amount of time that fewer than MAX_CONCURRENT threads are executing action, and returning as soon as possible after the final action has been performed.


texts:
public bool CheckPlace(string State, string City)
{
    TestDataContext data = new TestDataContext();
    bool IsKochi = new bool();
    if (!string.IsNullOrEmpty(State) && !string.IsNullOrEmpty(City))
    {
        var Place = (from a in data.Places where a.State == State || a.City == City select a.Place).ToList();
        if (Place != null)
        {
            foreach (var placeName in Place)
            {
                if (placeName.ToString().ToLower() == "kochi")
                {
                    IsKochi = true;
                    break;
                }
                else
                {
                    IsKochi = false;
                }
            }
        }
    }

    return IsKochi;
}


texts:
I'm very new to Erlang and I'm specifically interested in making my Erlang idiomatic (in other words, can you tell I don't know Erlang from this code?) and removing code duplication. I am not as interested in performance or the UI of the "game."
This is a simple interactive Rock, Paper, Scissors game and I've been using erlc rps.erl && ./rps-main.erl to run it.
rps.erl
-module(rps).
-export([play_game/1, get_random_symbol/0]).

get_symbol(1) -> rock;
get_symbol(2) -> paper;
get_symbol(3) -> scissors.

get_random_symbol() ->
    get_symbol(rand:uniform(3)).

play_game(InputFunctions) ->
    Score = [{win, 0}, {loss, 0}, {tie, 0}],
    play_game(Score, InputFunctions).

play_game(ok, _) ->
    ok;
play_game(Score, {Player1Input, Player2Input}) ->
    display_score(Score),
    Input1 = Player1Input(),
    Continue = Input1 =/= quit,
    Input2 = Player2Input(Continue),
    NewScore = compare_results(Input1, Input2, Score),
    play_game(NewScore, {Player1Input, Player2Input}).

compare_results(quit, _, _) ->
    ok;
compare_results(_, quit, _) ->
    ok;
compare_results(Same, Same, [Wins, Losses, {tie, Ties}]) ->
    [Wins, Losses, {tie, Ties+1}];
compare_results(rock, paper, [Wins, {loss, Losses}, Ties]) ->
    [Wins, {loss, Losses+1}, Ties];
compare_results(paper, scissors, [Wins, {loss, Losses}, Ties]) ->
    [Wins, {loss, Losses+1}, Ties];
compare_results(scissors, rock, [Wins, {loss, Losses}, Ties]) ->
    [Wins, {loss, Losses+1}, Ties];
compare_results(rock, scissors, [{win, Wins}, Losses, Ties]) ->
    [{win, Wins+1}, Losses, Ties];
compare_results(scissors, paper, [{win, Wins}, Losses, Ties]) ->
    [{win, Wins+1}, Losses, Ties];
compare_results(paper, rock, [{win, Wins}, Losses, Ties]) ->
    [{win, Wins+1}, Losses, Ties].

display_score([{win, Wins}, {loss, Losses}, {tie, Ties}]) ->
    io:format("Player1 Wins: ~B~nPlayer2 Wins: ~B~nTies: ~B~n", [Wins, Losses, Ties]).

rpsgame.erl
#!/usr/bin/env escript
%% -*- erlang -*-
%%! -pa 'ebin/'
-import(rps, [play_game/1, get_random_symbol/0]).

main(_) ->
    GetPlayerInput = fun() ->
        io:format("Enter rock, paper, scissors, or quit~n"),
        case io:get_line("> ") of
            "rock\n" -> rock;
            "paper\n" -> paper;
            "scissors\n" -> scissors;
            _ -> quit
        end
    end,
    GetCompInput = fun(Continue) when Continue =:= false ->
                        ok;
                    (_) ->
                        Input = rps:get_random_symbol(),
                        io:format("Computer threw ~p~n", [Input]),
                        Input
                    end,
    play_game({GetPlayerInput, GetCompInput}).


texts:
I want to properly test the strong exception guarantee of a method.

The method in question is SetFiles. It does not have side-effects (it only modifies the object it is called upon). It is expected to change some object fields and to modify the elements of FileList field - a collection. In case the argument is invalid then it throws ArgumentException and it is expected to not modify the object.

This is what I have:

[TestClass]
public class UnitTest_Files
{
    [TestMethod]
    public void TestFiles1()
    {
        var fs = new Files();

        // 1st call to SetFiles. valid

        fs.SetFiles(new string[]
            {
                @"C:\work\file1.txt",
                @"C:\work\file2.avi",
                @"C:\work\file3",
            });
        Assert.AreEqual(@"C:\work", fs.ParentFolder);
        Assert.AreEqual(3, fs.FileList.Count);

        Assert.AreEqual(@"C:\work\file1.txt", fs.FileList[0].Path);
        Assert.AreEqual(@"C:\work\file2.avi", fs.FileList[1].Path);
        Assert.AreEqual(@"C:\work\file3", fs.FileList[2].Path);

        // 2nd call to SetFiles. valid

        fs.SetFiles(new string[]
            {
                @"C:\muzică\Diabulus in Musica\poze\membri\Zuberoa Aznárez.jpg",
                @"C:\muzică\Diabulus in Musica\poze\membri\Gorka Elso.jpg",
            });

        Assert.AreEqual(@"C:\muzică\Diabulus in Musica\poze\membri", fs.ParentFolder);
        Assert.AreEqual(2, fs.FileList.Count);

        Assert.AreEqual(@"C:\muzică\Diabulus in Musica\poze\membri\Zuberoa Aznárez.jpg", fs.FileList[0].Path);
        Assert.AreEqual(@"C:\muzică\Diabulus in Musica\poze\membri\Gorka Elso.jpg", fs.FileList[1].Path);

        // 3rd call to SetFiles. Invalid

        // set to invalid structure (files not in same folder)
        // and expect throw
        // and no change
        try
        {
            fs.SetFiles(new string[]
                {
                    @"C:\muzică\folder1\file2.txt",
                    @"C:\muzică\folder2\file2.txt"
                });

            Assert.Fail("expected exception");
        }
        catch
        {
        }

        // test that fs was not modified

        Assert.AreEqual(@"C:\muzică\Diabulus in Musica\poze\membri", fs.ParentFolder);
        Assert.AreEqual(2, fs.FileList.Count);

        Assert.AreEqual(@"C:\muzică\Diabulus in Musica\poze\membri\Zuberoa Aznárez.jpg", fs.FileList[0].Path);
        Assert.AreEqual(@"C:\muzică\Diabulus in Musica\poze\membri\Gorka Elso.jpg", fs.FileList[1].Path);
    }
}

This is my first dive into C# unit testing and the code I wrote seems very cumbersome. I particularly don't like the copy-pasted last tests. The object is not clonable/copyable and I don't have any other reason to implement that. So I don't know how other way to test that object after is the same as before. Also the entire test doesn't seem very scalable.


texts: Linked object for creating chains without lists

texts:
I need a LinkedList but unfortunatelly the one that the .NET provides didn't have the functionality I was looking for (not working with linq and the LinkedListNode is sealed). I created a replacement that works similar but is linq-compatible.

The main class is a LinkedObject<T> that might be used on it's own but is better suited to be used by composition and should provide all the functionality of a chain:

[DebuggerDisplay("Value = {Value?.ToString()}")]
class LinkedObject<T>
{
    private LinkedObject<T> _previous;
    private LinkedObject<T> _next;

    public LinkedObject() {}

    public LinkedObject(T value)
    {
        Value = value;
    }

    public LinkedObject<T> Previous
    {
        get { return _previous; }
        set
        {
            _previous = value;
            value._next = this;
        }
    }

    public LinkedObject<T> Next
    {
        get { return _next; }
        set
        {
            _next = value;
            value._previous = this;
        }
    }

    public T Value { get; set; }

    // or maybe precessors?
    public IEnumerable<LinkedObject<T>> Before
    {
        get
        {
            var item = this;
            while (item != null)
            {
                yield return item;
                item = item.Previous;
            }
        }
    }

    // or maybe successors?
    public IEnumerable<LinkedObject<T>> After
    {
        get
        {
            var item = this;
            while (item != null)
            {
                yield return item;
                item = item.Next;
            }
        }
    }

    public void Remove()
    {
        _previous.Next = _next;
        _next.Previous = _previous;
        _previous = null;
        _next = null;
    }
}

Some custom object would use it like this (my real linked-object has a few more additional properties then this simple example):

class Word
{
    private readonly LinkedObject<Sentence> _linkedObject;

    public Word()
    {
        _linkedObject = new LinkedObject<Sentence>(this);
    }

    public string Value
    {
        get { return _linkedObject.Value; }
        set { _linkedObject.Value = value; }
    }

    public WordPrevious
    {
        get { return _linkedObject.Previous.Value; }
        set { _linkedObject.Previous = value._linkedObject; }
    }

    public Word Next
    {
        get { return _linkedObject.Next.Value; }
        set { _linkedObject.Next = value._linkedObject; }
    }

    public IEnumerable<Word> Before
    {
        get { return _linkedObject.Before.Select(x => x.Value); }
    }

    public IEnumerable<Word> After
    {
        get { return _linkedObject.After.Select(x => x.Value); }
    }
}

Example (I use a loop to create the real chain):

var foo = new LinkedObject<string>
{
    Value = "foo",
    Next = new LinkedObject<string>
    {
        Value = "bar",
        Next = new LinkedObject<string>() { Value = "baz" }
    }
};

// foo, bar, baz

foo.Next.Remove(); // foo, baz