texts: Solution to part 1 of Day 7 Advent of Code in Scala

texts:
Does someone want to review my solution to part 1 of Day 7 Advent of Code in Scala?
The assignment is here: http://adventofcode.com/day/7
Without repeating the whole text, in short:

Given a text file of the following form (simplified):

x AND y -> a
x -> 1
y -> 0

calculate the value of a. Here is an example of a full input file.

My solution:

import scala.io.Source

object Day7 {

  sealed trait Expr

  case class Variable(name: String) extends Expr

  case class Const(value: Int) extends Expr

  case class Not(expr: Expr) extends Expr

  case class And(left: Expr, right: Expr) extends Expr

  case class Or(left: Expr, right: Expr) extends Expr

  case class LShift(expr: Expr, by: Int) extends Expr

  case class RShift(expr: Expr, by: Int) extends Expr

  case class Assignment(v: Variable, expr: Expr) extends Expr

  case object Command {
    def parse(s: String): Assignment = {
      val assignmentRegex = """(.+) -> (.+)""".r
      def innerParse(s: String): Expr = {
        val rShiftRegex = """(\w+) RSHIFT (\d+)""".r
        val lShiftRegex = """(\w+) LSHIFT (\d+)""".r
        val orRegex = """(\w+) OR (\w+)""".r
        val andRegex = """(\w+) AND (\w+)""".r
        val notRegex = """NOT (\w+)""".r
        val constRegex = """(\d+)""".r
        val varRegex = """(\w+)""".r

        s match {
          case rShiftRegex(l, n) =>
            RShift(innerParse(l), n.toInt)
          case lShiftRegex(l, n) =>
            LShift(innerParse(l), n.toInt)
          case orRegex(l, r) =>
            Or(innerParse(l), innerParse(r))
          case andRegex(l, r) =>
            And(innerParse(l), innerParse(r))
          case notRegex(e) =>
            Not(innerParse(e))
          case constRegex(n) =>
            Const(n.toInt)
          case varRegex(n) =>
            Variable(n)
        }
      }
      s match {
        case assignmentRegex(l, r) => Assignment(Variable(r), innerParse(l))
        case _ => throw new Exception(s"Unrecognized command: $s")
      }
    }
  }

  class Environment(mappings: Map[String, Expr]) {

    val cache = new scala.collection.mutable.HashMap[Expr, Int]()

    private def eval(e: Expr): Int = {
      cache.get(e) match {
        case Some(i) => i
        case None => {
          val result = e match {
            case Variable(name) => {
              println(s"evaluating $name")
              eval(mappings(name))
            }
            case Const(value) => value
            case Not(expr) => ~eval(expr)
            case And(l, r) => eval(l) & eval(r)
            case Or(l, r) => eval(l) | eval(r)
            case LShift(expr, n) => eval(expr) << n
            case RShift(expr, n) => eval(expr) >> n
          }
          cache += (e -> result)
          result
        }
      }
    }

    def call(name: String): Int = {
      eval(mappings(name))
    }
  }

  def main(args: Array[String]) = {
    val lines = Source.fromFile("input-day7.txt").getLines()
    val x = lines.map(Command.parse).map((a) => a match {
      case Assignment(Variable(v), e) => (v -> e)
    })
    val m = x.toMap
    println(new Environment(m).call("a"))

  }

}


texts: Is there anything can be improved in this JavaScript module?

texts:
I am using RequireJS JavaScript library. I have the following JavaScript module define for dataservice:

define(['jquery'], function ($) {

    var callApi = function (url, type, dataType, data, callback) {
        $.ajax({
            url: url,
            type: type,
            data: data,
            dataType: dataType,
            contentType: "application/json",
            success: function (data) {
                callback(data);
            }
        });
    };

    var getData = function (url, dataType, data, callback) {
        callApi(url, 'GET', dataType, data, callback);
    };

    var postData = function (url, data, callback) {
        callApi(url, 'POST', 'json', data, callback)
    };

    return {
        getData: getData,
        postData: postData
    };

});

Is there anything which can be improved in this module?


texts: Optimization of Javascript Function swapping out images for gifs

texts:
Im working on a project where a grid of images are loaded all at once. when the mouse is on hover a certain image, a corresponding gif fades in and then fades out when the mouse moves away from the image.

I was able to get a working fiddle with the code i currently have. i have about 20 images that require this desired effect but my code is long and i feel like there is a better way to write my function where it can fade in and out as well as swapping out the images for gifs. Can someone help/point me in the right direction. still learning here!

code:

HTML

JAVASCRIPT
$(document).ready(function () {
    onHover();
    offHover();
    onHover1();
    offHover1();
    onHover2();
    offHover2();
    onHover3();
    offHover3();
});
function onHover()
{
    $(".img-ford").attr('src', 'http://reneinla.com/tasha/style/images/gifs/giphy.gif');

}
function offHover()
{
    $(".img-ford").attr('src', 'http://reneinla.com/tasha/style/images/stills/FORD-SUPER-BOWL.jpg');
}
function onHover1()
{
    $(".img-op").attr('src', 'http://reneinla.com/tasha/style/images/gifs/giphy.gif');
}
function offHover1()
{
    $(".img-op").attr('src', 'http://reneinla.com/tasha/style/images/stills/OPERATOR.jpg');
}
function onHover2()
{
    $(".img-op1").attr('src', 'http://reneinla.com/tasha/style/images/gifs/giphy.gif');

}
function offHover2()
{
    $(".img-op1").attr('src', 'http://reneinla.com/tasha/style/images/stills/FORD-SUPER-BOWL.jpg');
}
function onHover3()
{
    $(".img-ford1").attr('src', 'http://reneinla.com/tasha/style/images/gifs/giphy.gif');
}
function offHover3()
{
    $(".img-ford1").attr('src', 'http://reneinla.com/tasha/style/images/stills/OPERATOR.jpg');
}

UPDATE

Thanks for the help!
i was able to do some research and found something that works.
new fiddle


texts: Message Queue for inter-thread communication

texts:
I wrote this little piece of code, a while back. The intention behind it was to create a system to send messages between consumer and producer threads. I have no idea for what i wanted to use it. I just found it in an old snippet folder of mine. The type of the queue is FIFO.

The usage
Create a queue with new MessageQueue<MessageType>().

let's assume we will store an instance of the message queue in an private field inside the classes that will be using it.
This field is called messageQueue

Use messageQueue.push(new MessageType([...])) in a producer thread to add a
Message.
Take it out of the queue from a consumer thread with messageQueue.pull().
Check if there is a message in the queue with messageQueue.hasMessage().

I know that I thought about implementing a messageQueue.peek() but I never implementet it.

The code

/**
 * Holds a queue of messages for inter-thread communication
 * Producer and Consumer should not be the same thread!
 *
 * @param <T> type of the messages
 */
public class MessageQueue<T> {

private static final int MAX_SIZE = 255;

    private T[] messages;
    private int lastReadIndex;
    private int lastInsertIndex;

    private int adjustIndex(int index) {
        if (index >= MAX_SIZE) {
            return 0;
        }
        return index;
    }

    /**
     * Constructor of the {@link MessageQueue}
     */
    @SuppressWarnings("unchecked") // Java can't create an Array of a generic type...
    public MessageQueue() {
        messages = (T[]) new Object[MAX_SIZE];
    }

    /**
     * Pushes a new entry into the queue.
     * This function will block when the queue is full.
     *
     * @param msg The entry to push on the stack
     * @throws InterruptedException
     */
    public synchronized void push(T msg) throws InterruptedException {
        int newIndex = adjustIndex(lastInsertIndex + 1);

        // Check if we can push the message
        while (messages[newIndex] != null) {
            wait();
        }

        // Push the message
        messages[newIndex] = msg;
        lastInsertIndex = newIndex;
        notifyAll();
    }

    /**
     * Pulls an entry from the queue.
     * The entry will be returned and removed from the queue.
     *
     * @return The entry from the queue
     */
    public synchronized T pull() {
        int newIndex = adjustIndex(lastReadIndex + 1);

        T shouldReturn = messages[newIndex];
        messages[newIndex] = null;
        lastReadIndex = newIndex;
        notifyAll();
        return shouldReturn;
    }

    /**
     * Checks if there is an entry in the queue.
     *
     * @return true, when there is an entry in the queue
     */
    public synchronized Boolean hasMessage() {
        if (lastInsertIndex > -1) {
            int newIndex = adjustIndex(lastReadIndex + 1);

            if (messages[newIndex] != null) {
                return true;
            }
        }

        return false;
    }
}


texts: Ruby script to load .xlsx files into a MySQL database

texts:
I put together this Ruby script (1.9.3) to load data into MySQL from many .xlsx files that contain data entered by people. These files typically have many illegal characters and it has been difficult to handle the different errors. I have tried multiple rescue and character escaping approaches but now this method is not good and I have several more in the same class that are similar but load from different formats.

I would love some code review to help make this more functional. This is a standalone Ruby script without Rails or activerecord. I am using the logger, rubyXL, and MySQL2 gems.

  def paid_adv_load_ppc
    begin
      Dir["#{@paid_adv_work_path}*.xlsx"].each do |file|
        if file =~ /(PPC)/ && file !~ /(~)/
          begin
            @file = file
            begin
              workbook = RubyXL::Parser.parse(file)
              data = workbook.worksheets[0]
              sheet1 = data.extract_data
              begin
                sheet1.each_with_index do |row,index|
                  if index > 0 && row[1] != nil
                    service = row[3].to_s.gsub(/['"]/, '')
                    impressions = row[9].to_i
                    clicks = row[10].to_i
                    cost = row[11].to_f
                    total = row[12].to_f
                    puts "index: #{index} row: #{row}"
                    @db.query("INSERT INTO ppc
                     (service,impressions,clicks,cost,total)
                     VALUES  ('#{service}',#{impressions},#{clicks},#{cost},#{total})  ")
                  end
                end
              rescue Exception => e
                @log.info("There is a problem loading row for index #{index} :#{row} because of #{e.message}")
                next
              end
            rescue Exception => e
              @log.info("There is a problem loading #{@file}: #{e.message}")
              next
            end
          end
          @log.info("Loaded file #{@file} into database")
          FileUtils.rm(@file)
        end
      end
    rescue Exception => e
      @log.warn("Unable to process the data feed: #{@file} because #{e.message}")
    end
  end


texts: Generating HTML for prerolled d20 RPG initiative tables

texts:
A fairly simple program that I tried to write in Common Lisp in order to practice/learn. I'm sure this can be done more elegantly but Lisp's string handling seems to be very werd.

(defun d20 () (+ 1 (random 19)))

(defun initlist (specs)
  (let* ((ilist (map 'list #'(lambda (n) `(,(first n) ,(+ (second n) (d20)))) specs))
         (sortedilist (sort ilist #'> :key #'second)))
    sortedilist))

(defun htmlinitlist (specs)
  (apply #'uiop:strcat `("

&nbsp;	&nbsp;
"
   ,@(loop for (a b) in (initlist specs) collecting
        (format nil "
~A	~A

&nbsp;	&nbsp;
"
                a b))
   "
")))

(defun htmlinitlists (specs count)
  (format nil "~{ ~A~}" (loop for i from 0 to count collecting (htmlinitlist specs))))

Sample usage from REPL:

(htmlinitlists '(("Elvish" 5) ("Wizgit" 2) ("Alaric" 8) ("Pounder" 6)) 17)


texts: Random bits with a specified density (Bernoulli Trials)

texts:
I'm sure this is a fully 'solved' problem and expecting cross references but I can't find this on Google. I'm probably missing a keyword.

Here is a naive function for populating a bool array (b[i]) of length N in a Bernoulli distribution.
P(b[i]==true)==p and P(b[i]==false)==(1-p) for each independent random variable b[i].

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

void randomize(bool*const bits,const size_t len,double p){

    for(size_t i=0;i<len;++i){
        if(p>=1.0||rand()<p*RAND_MAX){//Need to force the hand for p>=1.0 as 0 and RAND_MAX possible.
            bits[i]=true;
        }else{
            bits[i]=false;
        }
    }
}

int main(void) {

    const size_t len=100;//N in the text.
    bool* bits=malloc(len*sizeof(*bits));//b[i] in the text.
    if(bits==NULL){
        return EXIT_FAILURE;
    }

    srand(1234);//Fixed for Reproducibility.

    randomize(bits,len,0.3);

    int count=0;
    for(size_t i=0;i<len;++i){
        printf("%c" , (bits[i]?'1':'0'));
        if(bits[i]){
            ++count;
        }
    }
    printf("\ncount=%f\n",((double)count)/((double)len));

    free(bits);

    return EXIT_SUCCESS;
}

Obviously it's linear \$\mathcal{O}(N)\$ for 'seeks' in the array.

However I could start with an array full of (0) and calculate a random variable (\$B\$) in the binomial distribution to get the number of bits to set then select them at random.

If \$p\$ is small that will involve far fewer seeks. It will still be (expected) asymptotically linear but with a far reduced constant.

If I naively pick the \$B\$ elements to set I might pick the same one twice (Birthday Paradox anyone?) and will need to pick again. If I just 'scan' forwards to the first free slot I will introduce a 'clumping' bias and violate the independence of the random variables P(b[i+1]==1|b[i]==1)>p. Clumping is a no-no for my application.

Therein lies the rub. If \$p\$ is near to 1.0 it will take an increasingly massive amount of time to fill the array. When it's nearly full randomly picking a 0 will typically take a lot of retries.

I can be 'clever' and if \$B \gt \frac{N}{2}\$ I can block set to 1 and set \$N-B\$ back to 0. That means only counting 'seeks' I will always beat the full scan above.
That's because the average retries at each step is \$\le 1 \$so expected seek count is \$ \lt N\$.

Does anyone have such a piece of code readily on hand?

PS: The data structure I'm working with isn't an array; it's a particular kind of tree. That's not important but might explain why I'm counting 'seeks' as the overriding parameter. However I am able to easily conjure up a 'tree of false' and a 'tree of true' to implement that 'block fill' requirement.


texts: Specifying that one set of HTML elements controls a corresponding set

texts:
How could the following code be made more concise?

HTML structure

jQuery

$('#main-nav li:eq(0)').on({
    mouseenter: function () {
        $('body section:eq(0)').find('.secondState').stop().fadeIn(600);
    },
    mouseleave: function () {
        if ($('body section:eq(0) .toggle').hasClass('current')) {
            return false;
        } else {
            $('body section:eq(0)').find('.secondState').stop().fadeOut(600);
        }
    }
});
$('#main-nav li:eq(1)').on({
    mouseenter: function () {
        $('body section:eq(1)').find('.secondState').stop().fadeIn(600);
    },
    mouseleave: function () {
        if ($('body section:eq(1) .toggle').hasClass('current')) {
            return false;
        } else {
            $('body section:eq(1)').find('.secondState').stop().fadeOut(600);
        }
    }
});
$('#main-nav li:eq(2)').on({
    mouseenter: function () {
        $('body section:eq(2)').find('.secondState').stop().fadeIn(600);
    },
    mouseleave: function () {
        if ($('body section:eq(2) .toggle').hasClass('current')) {
            return false;
        } else {
            $('body section:eq(2)').find('.secondState').stop().fadeOut(600);
        }
    }
});


texts: Checking for missing values in several columns of a spreadsheet

texts:
Basically I had 3 columns of Data

Adjudicated Cases
Not Adjudicated Cases
All Cases

and I wanted to get a list of all the cases that were missing from Column A and Column B, with a little help from @RubberDucky and his really cool Enumerable.cls (Found on GitHub in His VBX Repository) I used the Merge function to first merge Column A and Column B together in an Enumerable and then using the Contains function I checked to see if the Values in the Enumerable were contained in Column C, if they weren't there I added them to a second Enumerable and then printed that one out into another column.

How can I further hone my VBA skills?

Sub FindUnAssigned()
    With Sheets("FY13")
        Dim e As New Enumerable
        Dim lastARow As Integer
        lastARow = .Range("A" & .Rows.Count).End(xlUp).Row
        Dim lastBRow As Integer
        lastBRow = .Range("B" & .Rows.Count).End(xlUp).Row

        Set e.Collection = Range("A3:A" & lastARow)
        e.Merge (Range("B3:B" & lastBRow))

        Dim cell As Range
        Dim columnD As New Enumerable
        Dim lastCRow As Integer
        lastCRow = .Range("C" & .Rows.Count).End(xlUp).Row

        For Each cell In Range("C3:C" & lastCRow)
            If Not (e.Contains(cell.value, True)) Then
                columnD.Add (cell.value)
            End If
        Next cell

        Dim i As Integer
        For i = 1 To columnD.Count
            Dim j As Integer
            j = i + 2
            Range(("D" & j), ("D" & columnD.Count + 2)) = columnD.item(i)
        Next i
    End With
End Sub


texts:
I'm new to Clojure, and I'm writing some code that processes HTML. I'm parsing the HTML using clj-tagsoup and then trying to pull out the relevant bits with this function:

(defn find-nodes [tag-prototype base-node]
  (letfn [(node-matches [tag-prototype node]
            (let [[tag attrs] tag-prototype]
              (and (= (nth node 0) tag)
                   (every? (fn [[key val]]
                             (= (key (nth node 1)) val))
                           (seq attrs)))))]
    (if (node-matches tag-prototype base-node)
      [base-node]
      (loop [nodes base-node matches []]
        (if (seq nodes)
          (recur (next nodes)
                 (let [node (first nodes)]
                   (if (vector? node)
                     (if (node-matches tag-prototype node)
                       (conj matches node)
                       (concat matches (find-nodes tag-prototype node)))
                     matches)))
          matches)))))

Used something like this:

(->> (tagsoup/parse file)
     (find-nodes [:div {:class "magic"}])
     (find-nodes [:p]))

The code works (finally), but the style is probably horrible. What would you suggest to improve it?

Sample input:

<!DOCTYPE html>

    <title>This is a test page</title>

        ...

        Hello World.

Sample query:

(pprint (find-nodes [:div {:id "something-important"}]
                    (tagsoup/parse "test.html")))

Sample output:

([:div
  {:id "something-important"}
  [:p {:class "alert"} "\n        ...\n      "]])

Sample query:

(pprint (find-nodes [:p]
                    (tagsoup/parse "test.html")))

Sample output:

([:p {:class "alert"} "\n        ...\n      "]
 [:p {:class "message"} "\n        Hello World.\n      "])


texts: Automate the Boring Stuff with Python - The Collatz sequence project

texts:
I am new to programming and started learning with Automate the Boring Stuff with Python.  I just completed chapter 3's "Collatz sequence" project.

I was wondering what would you do differently and why? Trying to learn how to think like a programmer and become more efficient.

def collatz(number):
    global nextNumber
    if number % 2 == 0:
        nextNumber = (number//2)
        print(nextNumber)
    else:
        nextNumber = (3*number+1)
        print(nextNumber)

print('Type in an integer.')
integer=input()

if integer.isdigit() ==True:
    collatz(int(integer))
    while nextNumber !=1:
        collatz(nextNumber)

while integer.isdigit() != True :
        print('Please try again.')
        integer=input()
        collatz(int(integer))
        while nextNumber !=1:
            collatz(nextNumber)

print('Thank you!')


texts:
I have to apply two deals on the cart.

Buy 3 (equal) items and pay for 2
Buy 3 (in a set of items) and the cheapest is free

Items in cart:

----
|Car|
----
{'itemcode': 4, 'cost': 20.5, 'type': 'car', 'name': 'volvo'}
{'itemcode': 5, 'cost': 25, 'type': 'car', 'name': 'tesla'}
{'itemcode': 6, 'cost': 15, 'type': 'car', 'name': 'Ford'}
-----
|Tractor|
--------
{'itemcode': 7, 'cost': 10, 'type': 'tractor', 'name': 'mahindra'}
{'itemcode': 8, 'cost': 10, 'type': 'tractor', 'name': 'hmt'}
{'itemcode': 9, 'cost': 5, 'type': 'tractor', 'name': 'farmtrac'}

Below is the code I have written.

def print_receipts(types):
    """Prints the receipts with deals applied.

    Args
        types (collection.defaultdict) : items purchased with cost.
    """
    print "\n\t-: Payment Receipt :-\n"
    grand_total = []
    new_total = 0
    deal_one_msg = ""
    types_two = copy.deepcopy(types)
    for item, costs in types.items():
        if len(costs) >= 3:
            saving = costs.pop(costs.index(min(costs)))
            total = sum(costs)
            grand_total.append(total)
            print u"\tSaving: \xa3{} on {}".format(saving, item)
            deal_one_msg = "Deal Buy 3 for price of 2: "
            print u"\tType: {} Total: \xa3{}".format(item, total)
        else:
            new_total -= min(costs) * -1
            new_total = new_total if len(costs) > 1 else costs[0]
            grand_total.append(new_total)
            print u"\tType-: {} Total: \xa3{}".format(item, new_total)
    print u"\t{}Grand Total: \xa3{}".format(deal_one_msg, sum(grand_total))
    sets_cost = []
    if len(types_two.items()) < 3:
        return
    deal_two = True
    deal_two_msg = "3 sets: "
    saved = 0
    for item, _costs in types_two.items():
        sets_cost.append(sum(_costs))
        if len(_costs) != 3:
            deal_two = False
    if deal_two:
        deal_two_msg = "Deal get 3rd set free: "
        saved = sets_cost.pop(sets_cost.index(min(sets_cost)))
    grand_total_sets = sum(sets_cost)
    if grand_total_sets > 0 and saved > 0:
        print u"\tSaved \xa3{} of cheapest set.".format(saved)
        print u"\t{}Grand Total: \xa3{}".format(deal_two_msg, grand_total_sets)
    print "\t", "~" * 40

An example for testing:

import collections

types = collections.defaultdict(
    list,
    {u'car': [20.5, 25, 15], u'train': [5, 5, 5], u'tractor': [10, 10, 5]},
)


texts: Python for HPC: Parse file, generate statistics, record output

texts:
I have a program which is designed to be highly parallelizable. I suspect that some processors are finishing this Python script sooner then other processors, which would explain behavior I observe upstream of this code. Is it possible that this code allows some mpi processes to finish sooner than others?

comm = MPI.COMM_WORLD
rank = comm.Get_rank()
nam ='lcoe.coe'
csize = 10000
with open(dacout) as f:
    for i,l in enumerate(f):
        pass
numlines = i
dakchunks = pd.read_csv(dacout,  skiprows=0, chunksize = csize, sep='there_are_no_seperators')
linespassed = 0
vals = {}
for dchunk in dakchunks:
    for line in dchunk.values:
        linespassed += 1
        if linespassed < 49 or linespassed > numlines - 50: continue
        else:
            split_line = ''.join(str(s) for s in line).split()
        if len(split_line)==2:
              if split_line[0] == 'nan' or split_line[0] == '-nan':
                  continue

              if split_line[1] != nam:
                  continue
              if split_line[1] not in vals:
                  try:
                      vals[split_line[1]] = [float(split_line[0])]
                  except NameError:
                      continue
              else:
                  vals[split_line[1]].append(float(split_line[0]))
# Calculate mean and x s.t. Percentile_x(coe_dat)<threshold_coe
self.coe_vals = sorted(vals[nam])
self.mean_coe = np.mean(self.coe_vals)
self.p90 = np.percentile(self.coe_vals, 90)
self.p95 = np.percentile(self.coe_vals, 95)

count_vals = 0.00
for i in self.coe_vals:
    count_vals += 1
    if i > coe_threshold:
        break
self.perc = 100 * (count_vals/len(self.coe_vals))
if rank==0:
    print>>logf, self.rp, self.rd, self.hh, self.mean_coe
    print self.rp, self.rd, self.hh, self.mean_coe, self.p90, self.perc


texts:
I have written a tool for encrypting string using the AesCryptoServiceProvider in .NET. The folllowing parameters are used:

Block Cipher Mode: CBC
Initialization Vector: 16 bytes (randomized
per encryption operation), stored in last 16 bytes of final message
Key/Block size: 128 bit Padding: PKCS #7

Overview of code
I have added two functions below. The first prepares a message for encryption, takes the result and appends the Initialization Vector to the cipher message. The second function encrypts the provided message using the specified key and initialization vector.

Obviously, i'm interested in any security holes that exist with this code. Also any optimizations are very much appreciated.

Side questions. In development I always store bytes as strings in their hex representation, seen below. Is this bad? The only other alternative I see is  base64 encoding? Which is better?

public void cryptoExample {

    AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
    aes.KeySize = 128;
    aes.BlockSize = 128;

    aes.Key = Helper.StringToByteArray("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"); // Sample key
    aes.GenerateIV();

    String plainMessage = "Hello AES Crypto!!";

    String encryptedMessage = Helper.ByteArrayToString(CryptoHelper.EncryptStringToBytes_Aes(plainMessage, aes.Key, aes.IV));

    // Must always use a random IV, meaning it needs to be stored with the cipher message.
    // Append it to the end. IV is a constant 16 bytes so we just extract and remove it before decryption.
    String strIv = Helper.ByteArrayToString(aes.IV);

    String messageToTransmit = encryptedMessage += strIv;
}
public static byte[] EncryptStringToBytes_Aes(string plainText, byte[] Key, byte[] IV)
{
    // Check arguments.
    if (plainText == null || plainText.Length <= 0)
        throw new ArgumentNullException("plainText");
    if (Key == null || Key.Length <= 0)
        throw new ArgumentNullException("Key");
    if (IV == null || IV.Length <= 0)
        throw new ArgumentNullException("IV");
    byte[] encrypted;
    // Create an AesCryptoServiceProvider object
    // with the specified key and IV.
    using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
    {
        aesAlg.Key = Key;
        aesAlg.IV = IV;
        aesAlg.Mode = CipherMode.CBC;
        aesAlg.Padding = PaddingMode.PKCS7;
        // Create a decrytor to perform the stream transform.
        ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

        // Create the streams used for encryption.
        using (MemoryStream msEncrypt = new MemoryStream())
        {
            using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
            {
                using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                {

                    //Write all data to the stream.
                    swEncrypt.Write(plainText);
                }
                encrypted = msEncrypt.ToArray();
            }
        }
    }
}


texts: Wikipedia indexer and shortest link finder

texts:
I have the following code, how can I make it more efficient? Also, it doesn't always find the shortest route. (See Cat -> Tree)

import urllib.request, re, json

try:
    file = open("urls.json", "r")
    urls = json.loads(file.read())
    file.close()

    file = open("links.json", "r")
    links = json.loads(file.read())
    file.close()
except:
    urls = ["http://en.wikipedia.org/wiki/Cat"]
    links = [[-1, 0]]

for url in urls:
    try:
        print("Trying: " + url)
        request = urllib.request.Request(url, headers = {"User-Agent" : "DanBot/2.0"})
        file = urllib.request.urlopen(request)
        source = file.read().decode("utf-8")
    except Exception as e:
        print("Failed: " + url)
        while url in urls:
            urls.remove(url)

    suburls = re.findall("href=[\"']?([^\"' >]+)", source)

    for suburl in suburls:
        if len(suburl) > 6 and suburl[:7] == "http://" or suburl[:8] == "https://":
            fullurl = suburl
        elif len(suburl) > 1 and suburl[:2] == "//":
            fullurl = "http:" + suburl
        elif len(suburl) > 0 and suburl[0] == "/":
            fullurl = "/".join(url.split("/")[:3]) + suburl
        else:
            fullurl = "/".join(url.split("/")[:-1]) + "/" + suburl

        if fullurl[:29].lower() == "http://en.wikipedia.org/wiki/" and "#" not in fullurl[29:] and ":" not in fullurl[29:]:
            if fullurl not in urls:
                urls.append(fullurl)
            if [urls.index(url), urls.index(fullurl)] not in links:
                links.append([urls.index(url), urls.index(fullurl)])
        else:
            continue

    file = open("urls.json", "w")
    file.write(json.dumps(urls))
    file.close()

    file = open("links.json", "w")
    file.write(json.dumps(links))
    file.close()

input()

That program creates the index, and this one finds the links:

import urllib.request, re, json

try:
    file = open("urls.json", "r")
    urls = json.loads(file.read())
    file.close()

    file = open("links.json", "r")
    links = json.loads(file.read())
    file.close()
except:
    print("Error: No link or url file found")
    input()
    exit()

starturl = "http://en.wikipedia.org/wiki/" + input("Start at: http://en.wikipedia.org/wiki/")
endurl = "http://en.wikipedia.org/wiki/" + input("End at: http://en.wikipedia.org/wiki/")

if starturl not in urls or endurl not in urls:
    print("Error: One or more urls not found in index")
    input()
    exit()

print("")

route = []
selection = [[-1, urls.index(starturl)]]

done = False
for link in selection:
    for link2 in links:
        if link[1] == link2[0]:
            selection.append(link2)
        if link2[1] == urls.index(endurl) and link2 in selection:
            done = True
            break
    if done:
        break

if not done:
    print("Error: No link found between pages")
    input()
    exit()

selection = list(reversed(selection))
route = [selection[0]]
for link in selection:
    if link[1] == route[-1][0]:
        route.append(link)
route = list(reversed(route))

print("Route:")
for link in route:
    print(urls[link[1]])

input()


texts: Follow up - Decide whether a triangle is isosceles or not

texts:
Previous question: Decide whether a triangle is isosceles or not

Using the comments on the previous question (from people such as Carcigenicate, Luke Sawczak, dgnuff, alecxe and 200_success) I have revised the code and made an improved version but I'm sure that there is still more that I can do to improve it. (And while the calls to sleep and the 'print("")' may be unnecessary, I prefer to write my code with them to make the output look better).

import time

def askSideLength(sideMessage):
    while True:
        try:
            length = float(input("How long is the " + sideMessage + " side of the triangle? "))
            if length <= 0.0:
                print("This is an impossible triangle!")
                time.sleep(0.5)
            else:
                return length
        except ValueError:
                print("That's not a number...")
                time.sleep(0.5)

def findTriangleType():
        if side1 == side2:
            print("This is an isosceles triangle!")
        elif side1 == side3:
            print("This is an isosceles triangle!")
        elif side2 == side3:
            print("This is an isosceles triangle!")
        elif side1 != side2 and side1 != side3:
            print("This is not an isosceles triangle!")
        elif side2 != side1 and side2 != side3:
            print("This is not an isosceles triangle!")
        elif side3 != side1 and side3 != side2:
            print("This is not an isosceles triangle!")

print("""I am going to ask you for three numbers.
These numbers can be integers or decimals.
They will be the sides of a triangle,
and I will tell you if it is an isosceles triangle or not.
""")
time.sleep(2.5)
side1 = askSideLength("first")
time.sleep(0.25)
side2 = askSideLength("second")
time.sleep(0.25)
side3 = askSideLength("third")
print("")
time.sleep(1)

semiSum = (side1 + side2 + side3) / 2.0
areaSquared = semiSum * (semiSum - side1) * (semiSum - side2) * (semiSum - side3)

if side1 == side2 == side3:
    print("""While this triangle is technically an isosceles triangle,
it is much more commonly known as an equilateral triangle.""")
elif areaSquared <= 0.0:
    print("This is an impossible triangle!")
elif areaSquared > 0.0:
    findTriangleType()
time.sleep(2.5)


texts: Matlab code demonstrating use of fft (Fast Fourier Transform)

texts:
I posted the following Matlab script in response to a question on Signal Processing. Here is the questions with my answer.

I am looking for comments on how to make this code more instructive for the questioner. Any other code review comments are also welcomed.

fs = 2^10;        %sample frequency in Hz
T  = 1/fs;        %sample period in s
L  = 2^20;        %signal length
t  = (0:L-1) * T; %time vector

A1 = 0.2; %amplitude of x1 (first signal)
A2 = 1.0; %amplitude of x2 (second signal)
f1 = 1;   %frequency of x1
f2 = 50;  %frequency of x2

x1 = A1*sin(2*pi*f1 * t); %sinusoid 1
x2 = A2*sin(2*pi*f2 * t); %sinusoid 2
y  = x1 + x2;

%Plot signal
figure;
set(gcf,'Color','w'); %Make the figure background white
plot(fs*t(1:100), y(1:100));
set(gca,'Box','off'); %Axes on left and bottom only
str = sprintf('Signal with %dHz and %dHz components',f1,f2);
title(str);
xlabel('time (milliseconds)');
ylabel('Amplitude');

%Calculate spectrum
Y = fft(y)/L;
ampY = 2*abs(Y(1:L/2+1));
f = fs/2*linspace(0,1,L/2+1);
i = L/fs * (max(f1,f2)) + 1; %show only part of the spectrum

%Plot spectrum.
figure;
set(gcf,'Color','w');  %Make the figure background white
plot(f(1:i), ampY(1:i));
set(gca,'Box','off');  %Axes on left and bottom only
title('Single-Sided Amplitude Spectrum of y(t)');
xlabel('Frequency (Hz)');
ylabel('|Y(f)|');


texts:
I try to intercept some OpenGL calls for testing my rendering classes. Reflection is used for replacing the OpenGL backend.

I feel this class is badly written and I need advices for refactoring it.

package fr.meuns.opengl;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.FloatBuffer;

public class GL
{
  private static class MethodSignature
  {
    String name;
    Class< ? >[] parameters;

    public MethodSignature( String name, Class< ? >... parameters )
    {
      this.name = name;
      this.parameters = parameters;
    }
  }

  private final static MethodSignature[] METHOD_SIGNATURES = new MethodSignature[] {
    new MethodSignature( "glGenBuffers" ),
    new MethodSignature( "glDeleteBuffers", int.class ),
    new MethodSignature( "glBindBuffer", int.class, int.class ),
    new MethodSignature( "glBufferData", int.class, FloatBuffer.class, int.class ),
    new MethodSignature( "glGetBufferSubData", int.class, long.class, FloatBuffer.class )
    ...
  };

  private final static String[] CONSTANT_NAMES = new String[] {
    "GL_ARRAY_BUFFER",
    "GL_ELEMENT_ARRAY_BUFFER",
    "GL_STATIC_DRAW"
    ...
  };

  public static int GL_ARRAY_BUFFER;
  public static int GL_ELEMENT_ARRAY_BUFFER;
  public static int GL_STATIC_DRAW;
  ...

  public static Method _glGenBuffers;
  public static Method _glDeleteBuffers;
  public static Method _glBindBuffer;
  public static Method _glBufferData;
  public static Method _glGetBufferSubData;
  ...

  public static int glGenBuffers()
  {
    try
    {
      return (int)_glGenBuffers.invoke( null );
    }
    catch( IllegalAccessException | IllegalArgumentException | InvocationTargetException e )
    {
      e.printStackTrace();
      return 0;
    }
  }

  public static void glDeleteBuffers( int buffer )
  {
    try
    {
      _glDeleteBuffers.invoke( null, buffer );
    }
    catch( IllegalAccessException | IllegalArgumentException | InvocationTargetException e )
    {
      e.printStackTrace();
    }
  }

  public static void glBindBuffer( int target, int buffer )
  {
    try
    {
      _glBindBuffer.invoke( null, target, buffer );
    }
    catch( IllegalAccessException | IllegalArgumentException | InvocationTargetException e )
    {
      e.printStackTrace();
    }
  }

  public static void glBufferData( int target, FloatBuffer data, int usage )
  {
    try
    {
      _glBufferData.invoke( null, target, data, usage );
    }
    catch( IllegalAccessException | IllegalArgumentException | InvocationTargetException e )
    {
      e.printStackTrace();
    }
  }

  public static void glGetBufferSubData( int target, long offset, FloatBuffer data )
  {
    try
    {
      _glGetBufferSubData.invoke( null, target, offset, data );
    }
    catch( IllegalAccessException | IllegalArgumentException | InvocationTargetException e )
    {
      e.printStackTrace();
    }
  }

  public ...

  public static void useClass( Class< ? > glClass )
  {
    for( String name : CONSTANT_NAMES )
      try
      {
        GL.class
          .getField( name )
          .set( null, glClass.getField( name ).get( null ) );
      }
      catch( IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException e1 )
      {
        e1.printStackTrace();
      }

    for( MethodSignature signature : METHOD_SIGNATURES )
      try
      {
        GL.class
          .getField( "_" + signature.name )
          .set( null, glClass.getDeclaredMethod( signature.name, signature.parameters ) );
      }
      catch( IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException | NoSuchMethodException e )
      {
        e.printStackTrace();
      }
  }
}

Initialization for rendering using LWJGL :

import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL20;
...

GL.useClass( GL11.class );
GL.useClass( GL15.class );
GL.useClass( GL20.class );
...

Initialization for testing :

import fr.meuns.opengl.GLMock;
...

GL.useClass( GLMock.class );
...


texts: Evaluate/Refactor my ASP.NET XSS Security Helper Class

texts:
I am maintaining a site with significant security concerns and I wrote a helper class for validating potential XSS attacks.  The ValidateRequest method is meant to evaluate the HttpRequest for any potential issues, and if issues are found, redirect the user to the same page, but in a away that is not threatening to the application.

The application is being evaluated by HP WebInspect.  Their initial review included two regular expressions:

The "Regex for a simple XSS attack"

/((\%3C) <)((\%2F) \/)*[a-z0-9\%]+((\%3E) >)/ix
The "Paranoid regex for XSS attacks"

/((\%3C) <)[^\n]+((\%3E) >)/I

The regular expression I am using is a more stringent C# adaptation of the paranoid regex.  I prefer to err on the side of caution, so I would rather risk denying valid requests than allowing an invalid request to be processed.

Thanks in advance for the help.

using System;
using System.Text.RegularExpressions;
using System.Web;

public static class XssSecurity
{

public const string PotentialXssAttackExpression = "(http(s)*(%3a|:))|(ftp(s)*(%3a|:))|(javascript)|(alert)|(((\\%3C) <)[^\n]+((\\%3E) >))";

private static readonly Regex PotentialXssAttackRegex = new Regex(PotentialXssAttackExpression, RegexOptions.IgnoreCase);

public static bool IsPotentialXssAttack(this HttpRequest request)
{
    if(request != null)
    {
        string query = request.QueryString.ToString();
        if(!string.IsNullOrEmpty(query) && PotentialXssAttackRegex.IsMatch(query))
            return true;
        if(request.HttpMethod.Equals("post", StringComparison.InvariantCultureIgnoreCase))
        {
            string form = request.Form.ToString();
            if (!string.IsNullOrEmpty(form) && PotentialXssAttackRegex.IsMatch(form))
                return true;
        }
        if(request.Cookies.Count > 0)
        {
            foreach(HttpCookie cookie in request.Cookies)
            {
                if(PotentialXssAttackRegex.IsMatch(cookie.Value))
                {
                    return true;
                }
            }
        }
    }
    return false;
}

   public static void ValidateRequest(this HttpContext context, string redirectToPath = null)
   {
       if(context == null || !context.Request.IsPotentialXssAttack()) return;

       // expire all cookies
       foreach(HttpCookie cookie in context.Request.Cookies)
       {
           cookie.Expires = DateTime.Now.Subtract(TimeSpan.FromDays(1));
           context.Response.Cookies.Set(cookie);
       }

       // redirect to safe path
       bool redirected = false;
       if(redirectToPath != null)
       {
           try
           {
               context.Response.Redirect(redirectToPath,true);
               redirected = true;
           }
           catch
           {
               redirected = false;
           }
       }
       if (redirected)
           return;

       string safeUrl = context.Request.Url.AbsolutePath.Replace(context.Request.Url.Query, string.Empty);
       context.Response.Redirect(safeUrl,true);
   }
}


texts: Find palindromes in circular rotated string

texts:
I'm trying to tackle a programming challenge to determine how many palindromes exist in a given string that is being rotated character by character. My solution works, but it's too slow, and I'm struggling on where to optimize it.

For example, the string "cacbbba" would rotate 6 times, and would have 3 palindromes in each string.

cacbbba (3 palindromes)
acbbbac (3 palindromes)
cbbbaca (3 palindromes)
bbbacac (3 palindromes)
bbacacb (3 palindromes)
bacacbb (3 palindromes)
acacbbb (3 palindromes)

function circularPalindromes(s) {
    const k = s.length;
    const result = [];
    const isPalindrome = (str) => {
        const len = str.length;

        for (let i = 0; i < len / 2; i++) {
            if (str[i] !== str[len - 1 - i]) {
                return false;
            }
        }

        return true;
    };
    const subs = (s) => {
        let max = 0;

        for (let i = 0; i < s.length; i++) {
            for (let j = 1; j <= s.length - i; j++) {
                const sub = s.substring(i, i + j);

                if (sub.length < 2) continue;

                if (isPalindrome(sub) && sub.length > max) {
                    max = sub.length;
                }
            }
        }

        return max;
    };

    for (let i = 0; i < k; i++) {
        result.push(subs(s.substring(i, k) + s.substring(0, i)));
    }

    return result;
}
console.log(circularPalindromes('cacbbba'));


texts: Plotting with interpolation using Matplotlib

texts:
Program description:

You are given a set of two functions: $$f=x^3-6x^2+x+5; g=(x-2)^2-6$$
Plot them using Matprolib on a user input segment [a; b].

My solution:
import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import interpolate # for smoothing

def func(x):
    return [pow(x, 3) - 6 * pow(x, 2) + x + 5, pow((x-2), 2) - 6]
# plt.plot([1,5, -3, 0.5], [1, 25, 9, 0.5])
# plt.plot(1, 7, "r+")
# plt.plot(-1, 7, "bo")
f = []
f_1 = []
x = []
interval = [int(x) for x in input("Define segment (i.e. a b): ").split(' ')]
for val in range(interval[0], interval[1] + 1):
    x.append(val)
    f.append(func(val)[0])
    f_1.append(func(val)[1])

linear_space = np.linspace(interval[0], interval[1], 300)
a_BSpline = interpolate.make_interp_spline(x, f)
b_BSpline = interpolate.make_interp_spline(x, f_1)

f_new = a_BSpline(linear_space)
f_1_new = b_BSpline(linear_space)

plt.plot(linear_space, f_new, color="#47c984",
linestyle="solid", linewidth=1)

plt.plot(linear_space, f_1_new, color="#fc6703",
linestyle="solid", linewidth=1)

plt.gca().spines["left"].set_position("zero")
plt.gca().spines["bottom"].set_position("zero")
plt.show()

Input: -10 10
Output:

Question:
Is there any way to make this code more concise?
Thank you in advance.


texts: Encrypted text - C# SHA256, implementation

texts:
I have a C# application which needs to encrypt string and save the key as a hash. The key is saved as a hash to check the right key has been input before trying to decrypt the message.
First, I have these method to generate the hash string - hopefully "off the shelf" and ready to go
        public static byte[] GetHash(string inputString)
        {
            using (HashAlgorithm algorithm = SHA256.Create())
                return algorithm.ComputeHash(Encoding.UTF8.GetBytes(inputString));
        }

        public static string GetHashString(string inputString)
        {
            StringBuilder sb = new StringBuilder();
            foreach (byte b in GetHash(inputString))
                sb.Append(b.ToString("X2"));

            return sb.ToString();
        }

Then my encryption extension method:
private const int Keysize = 256;
private const int DerivationIterations = 1000;

public static string Encrypt(this string plainText, string passPhrase)
        {
            // Salt and IV is randomly generated each time, but is preprended to encrypted cipher text
            // so that the same Salt and IV values can be used when decrypting.
            var saltStringBytes = Generate256BitsOfRandomEntropy();
            var ivStringBytes = Generate256BitsOfRandomEntropy();
            var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
            using (var password = new Rfc2898DeriveBytes(passPhrase, saltStringBytes, DerivationIterations))
            {
                var keyBytes = password.GetBytes(Keysize / 8);
                var engine = new RijndaelEngine(256);
                var blockCipher = new CbcBlockCipher(engine);
                var cipher = new PaddedBufferedBlockCipher(blockCipher, new Pkcs7Padding());
                var keyParam = new KeyParameter(keyBytes);
                var keyParamWithIV = new ParametersWithIV(keyParam, ivStringBytes, 0, 32);

                cipher.Init(true, keyParamWithIV);
                var comparisonBytes = new byte[cipher.GetOutputSize(plainTextBytes.Length)];
                var length = cipher.ProcessBytes(plainTextBytes, comparisonBytes, 0);

                cipher.DoFinal(comparisonBytes, length);
                //                return Convert.ToBase64String(comparisonBytes);
                return Convert.ToBase64String(saltStringBytes.Concat(ivStringBytes).Concat(comparisonBytes).ToArray());
            }
        }

private static byte[] Generate256BitsOfRandomEntropy()
        {
            var randomBytes = new byte[32]; // 32 Bytes will give us 256 bits.
            using (var rngCsp = new RNGCryptoServiceProvider())
            {
                // Fill the array with cryptographically secure random bytes.
                rngCsp.GetBytes(randomBytes);
            }
            return randomBytes;
        }

Which is then called to a input string:
var encryptedString = inputString.Encrypt(_appSettings.EncryptionSecret + passPhrase);
The passphrase is a random set of characters, prepended with an application string.
My concern is that the passphrase is 4 sets of "diceware" words (like here: https://diceware.dmuth.org/) and therefore could be brute forced. Would adding more to the passphrase, such as as hash of DateTime.UtcNow.ToString("yyyyMMdd") or GetHashString(random-string) for example add any additional security or is it just theatre and the "diceware" passphrase is practicably unbreakable?


texts:
Anonymous functions are one of the best features of C++11. They make everything so beautiful!

However, one can get carried away and start overusing them.

This code calls a function that reads through a file and invokes a callback everytime that something matches a regex. Since I read two different files but I want callbacks only to differ for a constant, I chose this "lambda-returning-lambda" factory pattern.

auto callback_factory =
    [&] (IPMarkerType start, IPMarkerType stop) -> std::function<void(IPNode<T>&)>
{
    return [&,start,stop](IPNode<T> & node){
        markers.push_back(IPMarker<T>(node.ip, start));
        markers.push_back(IPMarker<T>(node.ip.network_ones(node.prefix), stop));
    };
};

read_regexp<T>("C:\\path\\to\\file1.txt", regex, callback_factory(ipm_a_open,ipm_a_close));
read_regexp<T>("C:\\path\\to\\file2.txt", regex, callback_factory(ipm_b_open,ipm_b_close));

Callbacks are really local just to the calling function, so I think that external functor would not be justified. On the other hand, this code smells a lot to me, so I would like to hear your comments.


texts: What would be the best way to update data from associative array?

texts:
I recieve this array in my controller:

"sort" => array:6 [
    0 => array:2 [
        "id" => "153"
        "sort" => "2"
    ]
    1 => array:2 [
        "id" => "152"
        "sort" => "1"
    ]
    2 => array:2 [
        "id" => "154"
        "sort" => "3"
    ]
    3 => array:2 [
        "id" => "166"
        "sort" => "4"
    ]
    4 => array:2 [
        "id" => "155"
        "sort" => "5"
    ]
    5 => array:2 [
        "id" => "156"
        "sort" => "6"
    ]
]

I have made this and it works:

foreach ($request['sort'] as $model) {
    Model::where('id', $model['id'])->update(['sort' => $model['sort']]);
}

There is probably a better way to do this....
This will make so many requests on DB how many items there are inside that array....
Anyone has better ideas? Maybe there is a way to make that update all at once so there is only one request on DB?


texts: Report a status based on a list of results

texts:
In one of my services, I have the function as presented below:

private def convertCreateResultsToCreateAllReasult(results: List[CreateResult.Result]): CreateAllResult.Result = {
    val firstConflictResult = results.collectFirst {
      case r: CreateResult.Conflict => r
    } match {
      case Some(cr: CreateResult.Conflict) => Some(CreateAllResult.Conflict(cr.message))

      case None => None
    }

    val firstNotFoundResult = results.collectFirst {
      case r: CreateResult.NotFound => r
    } match {
      case Some(nfr: CreateResult.NotFound) => Some(CreateAllResult.NotFound(nfr.message))

      case None => None
    }

    val firstFailedResult = results.collectFirst {
      case r: CreateResult.Failed => r
    } match {
      case Some(fr: CreateResult.Failed) => Some(CreateAllResult.Failed(fr.message))

      case None => None
    }

    val failedResults = List(firstConflictResult, firstNotFoundResult, firstFailedResult).flatten

    if (failedResults.isEmpty) {

      val createdDefinitions = results.collect {
        case r: CreateResult.Ok => r.definition
      }

      CreateAllResult.Ok(createdDefinitions)

    } else {

      failedResults.head

    }
  }

The argument of this function is a list of result objects, where the result object looks like this:

object CreateResult {

    sealed trait Result

    case class Ok(definition: ChecklistRuleDefinition) extends Result
    case class Conflict(message: String) extends Result
    case class NotFound(message: String) extends Result
    case class Failed(message: String) extends Result

  }

As you can see the result can be Ok (which is considered successful) and everything else (that considered as failed for various reasons).

The function should:

Return first fails result converted into a different type in case there is one such result in the list.
or
Return all successful results converted into CreateAllResult.Ok (if all of them successful).

Here is the CreateAllResult object:

object CreateAllResult {

    sealed trait Result

    case class Ok(definitions: List[ChecklistRuleDefinition]) extends Result
    case class Conflict(message: String) extends Result
    case class NotFound(message: String) extends Result
    case class Failed(message: String) extends Result

  }

The thing is, whatever I posted here, it seems to be working as expected. But, I hate the code. I know know that scala has all the bells and whistles to make the code nicer and more readable. I just didn't find the way to do so.

How can this code be written in a nicer, scala like, way?


texts: How are my javascript module pattern and promise pattern?

texts:
I'm working on an app that downloads some SVG paths from a server and draws them.

Anything in here is open to critique, but specifically I'd like to review my use of a module pattern and my implementation and use of promise objects.

The "modules" are each a file, and the aim is to limit and explicitly declare dependencies.  My use of promises is to simplify the use of ajax.

My 3 files are:

init.js - the main application.
promise.js - my implementation of promises
ajax.js - ajax functionality

init.js:

(function (window, document, bigmap, ajax) {
    "use strict";

    var attr = {
        fill: "#fafafa",
        stroke: "#505050",
        "stroke-width": 1,
        "stroke-linejoin": "round"
    };

    var drawings, svg_canvas;

    svg_canvas = bigmap.initialize("map", window.innerWidth - 21, window.innerHeight - 21);

    ajax.post("php/paths.php", null)
        .then(function (value) { drawings = JSON.parse(value); })
        .then(function () { bigmap.draw(svg_canvas, drawings.walls, null); })
        .then(function () { bigmap.draw(svg_canvas, drawings.depts, attr); });

}(this, this.document, parent.bigmap, parent.ajax));

promise.js:

var promise = function () {
    "use strict";

    var pending = [],
        ready = false,
        result;

    var then = function (callback) {
        if (typeof callback === "function") {
            if (ready === false) {
                pending.push(callback);
            } else {
                callback(result);
            }
        }
        return this;
    };

    var keep = function (response) {
        if (ready === false) {
            ready = true;
            pending.forEach(function (value, index, ar) {
                result = response;
                value(result);
            });
            pending = undefined;
        }
    };

    var isReady = function () {
        return ready;
    };

    return {
        keep: keep,
        share: {
            then: then,
            isReady: isReady
        }
    };

};

ajax.js:

var ajax = (function (XMLHttpRequest, promise) {
    "use strict";

    var done = 4, ok = 200;

    function post(url, parameters) {

        var XHR = new XMLHttpRequest();
        var p = promise();

        if (parameters === false || parameters === null || parameters === undefined) {
            parameters = "";
        }

        XHR.open("post", url, true);
        XHR.setRequestHeader("content-type", "application/x-www-form-urlencoded");
        XHR.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
        XHR.onreadystatechange = function () {
            if (XHR.readyState === done && XHR.status === ok) {
                p.keep(XHR.responseText);
            }
        };
        XHR.send(parameters);

        return p.share;
    }

    return {
        post: post
    };

}(parent.XMLHttpRequest, parent.promise));

Thanks.


texts:
I am rewriting a VB.NET application in C#. I will not subject you to the original code because it's pretty messy. Below is the converted C# code:

   public IEnumerable<WebQuery> GetQueries(Request request)
    {
        var queries = new List<WebQuery>();
        foreach (var report in request.Reports)
        {
            queries.AddRange((from query in _context.WebQueries
                join qg in _context.WebQueryGroups on query.QueryKey equals qg.QueryKey
                join wp in _context.WebPermissions on qg.QueryGroupNameKey equals wp.TaskGroupNameKey
                join wugn in _context.WebUserGroupNames on wp.UserGroupNameKey equals wugn.UserGroupNameKey
                join wug in _context.WebUserGroups on wugn.UserGroupNameKey equals wug.UserGroupNameKey
                join wt in _context.WebTasks on query.QueryCategory equals wt.TaskReportCategory
                where wt.TaskKey == request.Key && wp.ResourceKey == 4 && wt.TaskKey == request.Key && wug.UserKey == report.UserKey
                select query).OrderBy(x => x.QueryTitle));
        }
        return queries;
    }

As you can see, I am committing a grievous error in making a call to the database many times.

I am not sure how to append the loop to my query so that I only call the context one time.


texts:
I have written my own expendable list in Java.
LinkedList in Java
 Now I tried to write that thing in Python. Did I have used the elements of the python language properly?

class Node:
    def __init__(self):
        self.object = None
        self.nextNode = None

    def hasObject(self):
        return self.object != None

    def hasNextNode(self):
        return nextNode != None

class LinkedList:
    def __init__(self):
        self.startNode = Node()

    def getEmptyNode(self):
        currentNode = self.startNode
        while currentNode.hasObject():
            currentNode = currentNode.nextNode
        return currentNode

    def insert(self, obj):
        emptyNode = self.getEmptyNode()
        emptyNode.object = obj
        emptyNode.nextNode = Node()

    def getNumberOfElements(self):
        currentNode = self.startNode
        numberOfElements = 0
        while currentNode.hasObject():
            numberOfElements += 1
            currentNode = currentNode.nextNode
        return numberOfElements

    def getNodeByIndex(self, index):
        currentNode = self.startNode
        for i in range(index):
            currentNode = currentNode.nextNode
        return currentNode

    def get(self, index):
        node = self.getNodeByIndex(index)
        return node.object

    def delete(self, index):
        if index == 0:
            startNode = self.getNodeByIndex(1)
            return

        # if index equals last available index
        if index == (self.getNumberOfElements) - 1:
            nodeBefore = self.getNodeByIndex(index - 1)
            nodeBefore.nextNode = None

        nodeBefore = self.getNodeByIndex(index - 1)
        nodeBehind = self.getNodeByIndex(index + 1)
        nodeBefore.nextNode = nodeBehind

linkedList = LinkedList()
linkedList.insert("Hello")
linkedList.insert("How are you")
linkedList.insert(3)
linkedList.insert("I feel fine")
print(linkedList.getNumberOfElements())
print(linkedList.get(2))

for i in range(linkedList.getNumberOfElements()):
    print(linkedList.get(i))


texts:
What would be the most efficent way to calculate this?

var reportLimit = 96*1024;

IEnumerable<int> memoryInUse =
  things
    .Where(sample => sample.IsOn)
    .Select(sample => sample.MemoryInMb)
    .ToArray();

int totalUnderReportLimit = memoryInUse.Where(ram => ram <= memoryCalcFactor).Sum();
int totalOverEqualReportLimit = memoryInUse.Count(ram => ram > memoryCalcFactor) * memoryCalcFactor;

return totalUnderReportLimit  + totalOverEqualReportLimit;


texts:
Inspired by the learn you a haskell exercise to express map with fold I wrote an implementation of scanl by means of a fold.

scanl2 :: (b -> a -> b) -> b -> [a] -> [b]
scanl2 f z xs = reverse $ foldl (\a x -> (f (head a) x): a) [z] xs

I have the idea that in terms of performance it kinda is up to par with standard scanl but I am wondering whether I could improve anything both in terms of performance, idiomacy, style, etc. I am a Haskell novice with modest functional programming (Scala, some Clojure) background.


texts:
I decided to dive into the world of JS/JQuery by trying to build a simple web app to display all of my various meet results in an easy-to-view manner. For this stage of the review, all it does is generate a table of all the meets and log some stuff to the console when you click on it.

The app gets its data as JSON in a form like:

[{"Date": "MM/DD/YYYY", "Name": "Some Event", "Sport": "Run", "Events": [...]},
 {"Date": "MM/DD/YYYY", "Name": "Some Other Event", "Sport": "Swim", "Events": [...]},
 ...
 ]

and displays them in a table like:

Additionally, I'm storing the "Events" part in each cell so that when I click on a cell, I can eventually do something interesting with the data.

Is there anything I'm doing that violates some key language principles? Is what I'm doing with events too hacky? Is there a simpler way to build up the table? All comments welcome.

Date	Run	Swim


texts: Generate random unit vectors around circle

texts:
I'm trying to generate a bunch of uniformly distributed unit vectors around the unit circle. Here's my code, which is working, but unfortunately I have a for-loop. How do I get rid of this for-loop?

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

def gen_rand_vecs(dims, number):
    vecs = np.random.uniform(low=-1, size=(number,dims))
    mags = np.sqrt((vecs*vecs).sum(axis=-1))
    # How to distribute the magnitude to the vectors
    for i in range(number):
        vecs[i,:] = vecs[i, :] / mags[i]
    return vecs

theta = np.linspace(0, 2*np.pi, 100)
circle = np.array([np.cos(theta), np.sin(theta)])

fig = plt.figure()
plt.plot(circle[0], circle[1])
rand_vecs = gen_rand_vecs(2, 100)
for e in rand_vecs:
    plt.plot([0,e[0]], [0,e[1]], 'r')
plt.show()


texts: Project Euler problem 92 (sequences formed by the sum of squares of digits) in Haskell

texts:
I made the following program in Haskell to solve Project Euler's problem 92. The program works (i.e. the solution it yields is correct), but it's not exactly the fastest in the world.

The problem is:

  A number chain is created by continuously adding the square of the
  digits in a number to form a new number until it has been seen before.

  For example,

  44 → 32 → 13 → 10 → 1 → 1
  85 → 89 → 145 → 42 → 20 → 4 → 16 → 37 → 58 → 89

  Therefore any chain that arrives at 1 or 89 will become stuck in an
  endless loop. What is most amazing is that EVERY starting number will
  eventually arrive at 1 or 89.

  How many starting numbers below ten million will arrive at 89?

My code:

digits :: Int -> [Int]
digits 0 = []
digits x = digits (x `div` 10) ++ [x `mod` 10]

squareDigits :: [Int] -> [Int]
squareDigits x =  map (^2) x

sumSquareDigits :: Int -> Int
sumSquareDigits x = sum (squareDigits (digits x))

chainTo89 :: Int -> Bool
chainTo89 89 = True
chainTo89 1 = False
chainTo89 x = chainTo89 (sumSquareDigits x)

main :: IO()
main = print $ length $ filter (chainTo89) $ [1..9999999]

Any feedback on how this can improved, ranging from advice on good practices to very concrete algorithmic improvements, preferably without using too complicated Haskell concepts (I've only been programming in Haskell for three days) is more than welcome. If you are going to propose rather complex improvements (e.g. state monads?), then that's fine, so long as it's understandable to a beginner. :)

Note: I've implemented this in Python too, where I mapped known outcomes to a dictionary, which sped up the process considerably (i.e. the moment the chain function encounters a number already checked, it goes straight to the outcome without chaining further). However, I don't know how to do that in Haskell, if that is good practice in Haskell in the first place. This is exactly one of the reasons I asked this question in the first place; it feels as if it would be good to have some 'global' variable that can be used to map known values to, but that seems to go against Haskell's spirit.


texts: Error handling for function calls to parse a GIF file

texts:
I'm debating which version would be more suitable in parameters of code length, readability and maintenance. This is a portion of a set of functions which together parse a Gif file.

Version 1:

if ((errno = GifLoadHeaders(g, &bufTemp)) != GIF_SUCCESS) {
    GifDispose(g);
    *gPtr = NULL;

    return errno;
}

if ((errno = GifLoadGCT(g, &bufTemp)) != GIF_SUCCESS) {
    GifDispose(g);
    *gPtr = NULL;

    return errno;
}

if ((errno = GifLoadImages(g, &bufTemp)) != GIF_SUCCESS) {
    GifDispose(g);
    *gPtr = NULL;

    return errno;
}

Version 2:

if ((errno = GifLoadHeaders(g, &bufTemp)) != GIF_SUCCESS
    || (errno = GifLoadGCT(g, &bufTemp)) != GIF_SUCCESS
    || (errno = GifLoadImages(g, &bufTemp)) != GIF_SUCCESS) {

    GifDispose(g);
    *gPtr = NULL;

    return errno;
}

I generally prefer the latter version. Maybe there's a more optimal third solution to my issue. Any help?

EDIT:
I'm sorry I did not include the entire routine. The code I've included is used in the function GifParse which initializes a given pointer to a Gif structure, which is named gPtr.


texts: Return whether a string is a palindrome if you can delete at most k elements

texts:
The task:

  Given a string which we can delete at most k, return whether you can
  make a palindrome.

  For example, given 'waterrfetawx' and a k of 2, you could delete f and
  x to get 'waterretaw'.

The following solution is partially inspired by @Oh My Goodness's answer.

const s = "waterrfetawx";
const del = 2;

function palindromePossible(str, deleteAtMost) {
  const set = new Set();
  for (let i = 0, len = str.length; i < len; i++) {
    const char = str[i];
    void(set.delete(char) || set.add(char)); // <--is this good code?
  }

  let iStart = 0, iEnd = str.length - 1, possibleDeletion = 0;

  function isSame() {
    do {
      if (str[iStart] === str[iEnd]) {
        iStart++;
        iEnd--;
        return true;
      }
      if (++possibleDeletion > deleteAtMost) { return false; }
      if (set.has(str[iStart])) { iStart++; }
      if (set.has(str[iEnd])) { iEnd--; }
      if (iStart > iEnd) { return false; }
    } while(str[iStart] !== str[iEnd]);

    return true;
  }

  if (set.size <= deleteAtMost + 1) {
    for (let i = 0, len = Math.floor(str.length/2); i < len && iStart <= iEnd; i++) {
      if (!isSame()) { return false; }
    }
    return true;
  }
  return false;
}

console.log(palindromePossible(s, del));


texts:
I have inherited a site which uses the Google API to create a map on my webpage. I don't know much about JavaScript but from the research I've done, I dont think this code is optimal as it is not loading asynchronously. Any help or pointers would be appreciated.

function initialize() {
var myLatlng = new google.maps.LatLng(<?php echo get_post_meta($post->ID,'school_latitude',true); ?>, <?php echo get_post_meta($post->ID,'school_longitude',true); ?>);

var myOptions = {
  zoom: 11,
  center: myLatlng,
  mapTypeId: google.maps.MapTypeId.ROADMAP
 }

var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

var marker = new google.maps.Marker({
  position: myLatlng,
  map: map,
  title: 'School'
});
}

function loadScript() {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=initialize";
document.body.appendChild(script);
}

window.onload = loadScript;
</script>


texts: Creating a grid in HTML from a database search

texts:
I have a little piece of code that is a little bit unoptimized and need some help with optimization in this case.

The procedure is to search in a database using "Conditions/Condicao" and create a grid using tables in HTML with content.  In debug, the following part is taking a long time to complete with 300 results or more.

for (x = 0; x < Campos.Length; x++)
{
    string concat = "";
    for (i = 0; i < 2; i++)
    {
        if (concat != "")
        {
            concat += "," + oDrc[y].ItemArray[1].ToString().Trim().Replace("'", "");
        }
        else
        {
            concat += oDrc[y].ItemArray[0].ToString().Trim().Replace("'", "");
        }
    }
    if (!pkunica)
    {
        concat = "";
        for (i = 0; i < Pks.Length; i++)
        {
            if (concat != "")
            {
                concat += "," + oDrc[y].ItemArray[i].ToString().Trim().Replace("'", "");
            }
            else
            {
                concat += oDrc[y].ItemArray[i].ToString().Trim().Replace("'", "");
            }
        }
    }

I've uploaded the full code to Pastebin.  And about some words, they're in PT-BR.


texts:
I made some crude code using RamdaJS to essentially group orders by their states. There are some nested structures. Currently, I have a working code, but I wondering if Ramda can perhaps help to clean that up more? I've added TypeScript annotations to present (simplified) structures more clearly.

I've also prepared codesandbox with example data: https://codesandbox.io/s/yqwm2p65zz

import * as R from 'ramda'

export default function(orders: Order[], orderListing: OrderListing[]) {
  const pluckStateEnum = (orderState: OrderState) => orderState.enum

  const ordersByState = (states: OrderStateEnum[]) =>
    orders.filter(
      (order: Order) =>
        !R.isEmpty(
          R.intersection(R.map(pluckStateEnum, order.orderStates), states),
        ),
    )

  return orderListing.map(listing => ({
    id: listing.id,
    orders: ordersByState(listing.orderStateEnums).map(order => order.id),
  }))
}

interface Order {
  id: string
  orderStates: OrderState[]
}

interface OrderState {
  enum: OrderStateEnum
}

interface OrderListing {
  id: string
  orderStateEnums: OrderStateEnum[]
}

type OrderStateEnum =
  | 'RECEIVED'
  | 'PAID'
  | 'UNPAID'
  | 'CUSTOMER_NOTIFIED'
  | 'FINISHED'


texts: Determine local maximum from given points

texts:
How could I make this code more effective?  I'm just experimenting and learning the different ways of how Python runs.

list=[]
original=mkPoint3D(x,y)
z1=mkPoint3D(x-d,y)
z2=mkPoint3D(x+d,y)
z3=mkPoint3D(x,y-d)
z4=mkPoint3D(x,y+d)
list.append(original)
list.append(z1)
list.append(z2)
list.append(z3)
list.append(z4)

if(maxPoint(list).z==original.z):
    print("The local maximum is at " + "( " + str(original.x) + " , "  + str(original.y) + " )" + " = " + str(original.z))
elif maxPoint(list).z==z1.z:
    hillClimb(z1.x,z1.y,d)
elif maxPoint(list).z==z2.z:
    hillClimb(z2.x,z2.y,d)
elif maxPoint(list).z==z3.z:
    hillClimb(z3.x,z3.y,d)
elif maxPoint(list).z==z4.z:
    hillClimb(z4.x,z4.y,d)


texts: Stripping leading tab of each line from a file

texts:
I've got this Python code:

import os, re

def clnFile(file):
    inputFile = open(file, 'r')
    filename = file + '.tmp'
    outputFile = open(filename, 'w')
    code = inputFile.read()
    pattern = re.compile('\n\t')
    code = pattern.sub('\n', code)
    outputFile.write(code)
    inputFile.close()
    outputFile.close()
    os.remove(file)
    os.rename(filename, file)

It supposed to change content of the file.
My algorithm is:

open original file
open original temporary file
read original file content to a string
change the string
write string to original temporary file
close original file
close temporary file
remove old original file
rename temporary file

I am sure there should be a way to simplify this code. Because now it's rather bulky.
Could anyone help me to adjust it?


texts: Create nested named object using variables as key

texts:
I am trying to create a new object with nested value pairs. My initial challenge is that I have a huge object containing lots of junk data, and I'd like to strip away the value pairs that I do not need as well as renaming certain values. Performance is vital as this will iterate over thousands of items. I assume that creating a new object gives better performance than deleting and replacing values within the original object.

I've also put the following code in this JSFiddle: http://jsfiddle.net/mk3cH/2/

var data = [{
  "ID": 20,
  "Title": "Test item",
  "Category": "1;#Local",
  "ContentType": "Item",
  "FileLeafRef": "20;#20_.000",
  "FileRef": "20;#km/Lists/Meny/20_.000",
  "_ModerationStatus": "0",
  "_Level": "1",
  "UniqueId": "20;#{447FEC10-E7E2-4561-A7B3-0433C8C90D31}",
  "FSObjType": "20;#0",
  "PermMask": "0x400001f07fff1bff",
  "Modified": "2013-01-10 11:56:05",
  "owshiddenversion": "8",
  "Created": "2012-04-12 11:53:07"
}, {
  "ID": 27,
  "Title": "Test item 2",
  "ContentType": "Item",
  "FileLeafRef": "27;#27_.000",
  "FileRef": "27;#km/Lists/Meny/27_.000",
  "_ModerationStatus": "0",
  "_Level": "1",
  "UniqueId": "27;#{F7965302-AB39-45AE-9D9E-99BCF39F03B9}",
  "FSObjType": "27;#0",
  "PermMask": "0x400001f07fff1bff",
  "Modified": "2013-04-14 14:00:30",
  "owshiddenversion": "18",
  "Created": "2012-04-20 14:39:02"
}]
var new_object = {}
var keysToIgnore = ['FileLeafRef', 'UniqueId', 'PermMask', 'owshiddenversion', 'Created', '_Level', '_ModerationStatus'].reduce(function (res, key) {
  res[key] = true;
  return res;
}, {});
var object_initiated = false
document.writeln("Desired content of object:
");
for (var i in data) {
  for (var key in data[i]) {
    if (key in keysToIgnore) continue;
    if (key == "ID") {
      new_object[i] = {
        "ID": keydata
      }
    } else {
      var keyname = key;
      var keydata = data[i][key]
      document.writeln(keyname + ": " + keydata + "
");
      new_object[i][keyname] = keydata
    }
  }
}
document.writeln("
Actual new object content
");
document.writeln(JSON.stringify(new_object));


texts:
Is there a way to write this code more efficiently so that it is easier to read, maybe using loops and lists?

import random
total = 0
sixes = 0
fives = 0
fours = 0
threes = 0
twos = 0
ones = 0
limit = 50
throws = 0
Total = 0

play = input("Do you want to play, 'Y' or 'N'")
if play.upper() == "Y" :
    play = True
else :
    play = False

while total < limit and play :
    roll = random.randint(1,6)
    throws +=1
    total += roll
    if roll == 6:
        sixes +=1
    elif roll == 5:
        fives +=1
    elif roll == 4:
        fours +=1
    elif roll == 3:
        threes +=1
    elif roll == 2:
        twos +=1
    elif roll == 1:
        ones +=1
    else :
        print(" try again ")

if play :
    print("Total throws : ",throws)
    print("Total : ",total)
    print("Total 6's : ",sixes)
    print("Total 5's : ",fives)
    print("Total 4's : ",fours)
    print("Total 3's : ",threes)
    print("Total 2's : ",twos)
    print("Total 1's : ",ones)
else :
    print("Yr los")


texts: Encapsulation preserving operator= overloading in C++

texts:
I have made a facility to perform operator= dispatching to a member setter function. The purpose is to enable accessing members (like using object.member = value;) while at the same time respecting encapsulation.

My implementation looks like this:

#include <iostream>

#define OPEQ_SETTER_GETTER( OwnerType, Type, Function, Name ) \
    accessor< Type, OwnerType, decltype(&OwnerType::Function) > Name = \
    accessor< Type, OwnerType, decltype(&OwnerType::Function) >(this, &OwnerType::Function); \
    friend class accessor< Type, OwnerType, decltype(&OwnerType::Function) >;

// A generic accesor class
template <typename T, class Owner, class Setter> struct accessor {
    accessor ( Owner* o, Setter &&f ): owner( o ), setter( f ) { }
    accessor &operator=( const T &value ) { setter( owner, data, value ); return *this; }
    operator T() const { return data; }
private:
    Owner *owner;
    Setter setter;
    T      data;
};

// An example of applying the idiom
class example
{
    private:
    static void setx( example *this_handle, double &data, const double &value )
    {
        std::cout << "Setting data to: " << value << '\n';
        this_handle->another_member = 85656;
        std::cout << "another_member is: " << this_handle->another_member << '\n';
        data = value;
    }

    std::size_t another_member = 0;

    public:
    example( )  {}

    OPEQ_SETTER_GETTER( example, double, setx, x );
    // Will expand to:
    //accessor< double, example, decltype(&example::setx) > x =
    //      accessor< double, example, decltype(&example::setx) >(this, &example::setx);
    //friend class accessor< double, example, decltype(&example::setx) >;

};

int main()
{
    example e;
    e.x = 2*3.14159;
    std::cout << "New value: " << e.x << '\n';

    return 0;
}

Demo

The questions I have questions are:

Does anybody see any issues with the above?
Does anybody have any suggestions in implementing this without using the preprocessor and still keep the interface similarly compact?


texts: Preserve formatting for mail merge Excel & Word 2016

texts:
I inherited some code and have discovered that it seems incredibly inefficient. The code works by applying text formatting to the cells in an Excel file cell by cell, via this loop:

Public Sub RemoveNumberFormat(ByVal columnName As String, ByVal lastColumn As Integer)
    For i = 2 To lastColumn
        textval = Range(columnName & CStr(i)).Text
        Range(columnName & CStr(i)).NumberFormat = "@"
        Range(columnName & CStr(i)).Value = textval
    Next i
End Sub

This will preserve numbers such as 6120 as 6,120 and currency values such as 7568910 as $7,568,910. These numbers are brought in from the Excel workbook into mail merge fields in a Word document. Currently, this function is called on every column in the workbook like so:

    RemoveNumberFormat "AM", lastColumn
    RemoveNumberFormat "AU", lastColumn
    RemoveNumberFormat "AV", lastColumn
    RemoveNumberFormat "BA", lastColumn
    ... etc.

Now this does work, but there's 145 columns. The macro is called on open and it takes several seconds to run. This seems incredibly inefficient.

I had tried simply updating the NumberFormat of the UsedRange as so:

Sheets("MailMerge").UsedRange.NumberFormat = "@"

But that still removes the commas and currency formatting from the columns.

I had also tried copying the values, updating the NumberFormat, then pasting the values in (essentially re-creating that loop as a set operation rather than cell by cell) but that also did not result in the formatting being preserved.

Is there a better way to do this than loop through each cell?


texts:
I've been learning Python for about 1 - 1 1/2 weeks now and just finished my first small project to help solidify what I've learnt.  It's a very basic (bad) login / create account system that I run in PowerShell but just wanted someone feedback and thoughts as to how I'm getting on in regards to how long I've been learning.

import os.path

def app(user_name):
    print(user_name)

def create():

    exists = os.path.exists("C:/Users/Tom/Desktop/test/login_project/account_info.txt")

    if not exists:
        f = open("account_info.txt", "w")
        f.close()

    creating_username = True
    creating_password = True

    while creating_username:
        user_name = input("\nEnter a username: ")

        f = open("account_info.txt", "r")
        file_contents = f.read()

        if f"'{user_name}'" in file_contents:
            print("\nUsername already in use.")
        else:
            creating_username = False

    email = input("Enter an e-mail address: ")

    while creating_password:

        password = input("Enter a password: ")
        password_confirm = input("Confirm password: ")

        if password == password_confirm:

            info_list = [user_name, password, email]

            f = open("account_info.txt", "a+")
            f.write(f"{info_list}\r\n")
            f.close()

            print("\nAccount Created.\n")

            creating_password = False

            choosing()

        else:
            print("\nPasswords did not match.\n")

def login():

    exists = os.path.exists("C:/Users/Tom/Desktop/test/login_project/account_info.txt")
    entering_info = True

    while entering_info:

        user_name = input("\nEnter username: ")
        password = input("Enter password: ")

        if not exists:
            entering_info = False
            print("\nError: [0] account(s) found in database\n")
            choosing()

        else:
            f = open("account_info.txt", "r")
            file_contents = f.read()

            f = open("account_info.txt", "r")
            list_ = [char for char in f.readlines() if char != "\n"]

            counter_one = 1
            counter_two = 1

            for i in list_:
                if f"'{user_name}'" in i:
                    break
                counter_one += 1

            for y in list_:
                if f"'{password}'" in y:
                    break
                counter_two += 1

            f.close()

            if f"'{user_name}'" in file_contents:
                if f"'{password}'" in file_contents and counter_one == counter_two:
                    print("Login Successful")
                    entering_info = False
                    app(user_name)
                else:
                    print("\nPassword incorrect.")
            else:
                print("\nUsername incorrect.")

def choosing():

    main_choosing = True
    main_choice = ""

    print("Please continue to login or create an account.")
    print("(Type 'login' to login, or 'create' to create an account).")
    print("('exit' to close program)\n")

    while main_choosing:

        main_choice = input("- ")

        if main_choice == "login" or main_choice == "create" or main_choice == "exit":

            if main_choice == "login":
                main_choosing = False
                login()
            elif main_choice == "create":
                main_choosing = False
                create()
            else:
                print("\nClosing...\nClosed.")
                break

        else:
            print("\nEnter a valid command.\n")

print("\nWelcome.\n")
choosing()


texts:
I'm building an autosave for a data driven form on Angular.

Here is our code snippet:

public ngOnInit(): void {
    this.form = this.formBuilder.group({
        name: ['', Validators.required],
        enabled: [false],
    });

    this.form.valueChanges.pipe(
        debounceTime(500),
        withLatestFrom(this.form.statusChanges.pipe(filter(status => status === 'VALID')))
    ).subscribe((value) => console.log('We could autosave!', value[0], 'status', value[1]));

}

It works but I wonder if it's optimal to use the statusChanges.

Indeed we could use the valid property instead:

this.form.valueChanges.pipe(
      debounceTime(500)
).subscribe((value) => {
    if (this.form.valid) {
        console.log('We could autosave!', value);
    }
});

What would be the most reliable/optimal method?


texts: Country data archiving script using Google Spreadsheet

texts:
I've just created a script in Google Spreadsheet in order to archive some things. I have data from four countries.  If something is marked done, then that line could be archived into that country's archive.

My code is pretty ugly. Is there a clearer way to do this?

function CroatianArchive()
{
  var source_sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('original');
  var target_sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('croatian_backup');
  var lastRow = source_sheet.getLastRow();
  var source_range = source_sheet.getDataRange();
  var target_range = target_sheet.getDataRange();
  var rowsToBeDeleted = [];

  var i = 2;
  while (i <= lastRow) {
  if (source_sheet.getRange("A"+i).getValue() == "CRO" && source_sheet.getRange("M"+i).getValue() == "DONE" ) {
    var office = source_sheet.getRange("A"+i).getValue();
    var title = source_sheet.getRange("B"+i).getValue();
    var imdbId = source_sheet.getRange("C"+i).getValue();
    var channel = source_sheet.getRange("D"+i).getValue();
    var type = source_sheet.getRange("E"+i).getValue();
    var added = source_sheet.getRange("F"+i).getValue();
    var deadline = source_sheet.getRange("G"+i).getValue();
    var airing = source_sheet.getRange("H"+i).getValue();
    var link = source_sheet.getRange("I"+i).getValue();
    var picture = source_sheet.getRange("J"+i).getValue();
    var comment = source_sheet.getRange("K"+i).getValue();
    var portId = source_sheet.getRange("L"+i).getValue();
    var status = source_sheet.getRange("M"+i).getValue();
    var data = [office,title,imdbId,channel,type,added,deadline,airing,link,picture,comment,portId,status];
    target_sheet.appendRow(data);
    rowsToBeDeleted.push(i);
    i++;
    } else {
      i++;
      }
    }
    rowsToBeDeleted.reverse();
    for (var j = 0; j < rowsToBeDeleted.length; j++)
    {
        source_sheet.deleteRow(rowsToBeDeleted[j]);
    }
    SpreadsheetApp.getActiveSpreadsheet().toast('Archivation finished.', 'Status');
  }
function SerbianArchive()
{
  var source_sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('original');
  var target_sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('serbian_backup');
  var lastRow = source_sheet.getLastRow();
  var source_range = source_sheet.getDataRange();
  var target_range = target_sheet.getDataRange();
  var rowsToBeDeleted = [];

  var i = 2;
  while (i <= lastRow) {
  if (source_sheet.getRange("A"+i).getValue() == "SER" && source_sheet.getRange("M"+i).getValue() == "DONE" ) {
    var office = source_sheet.getRange("A"+i).getValue();
    var title = source_sheet.getRange("B"+i).getValue();
    var imdbId = source_sheet.getRange("C"+i).getValue();
    var channel = source_sheet.getRange("D"+i).getValue();
    var type = source_sheet.getRange("E"+i).getValue();
    var added = source_sheet.getRange("F"+i).getValue();
    var deadline = source_sheet.getRange("G"+i).getValue();
    var airing = source_sheet.getRange("H"+i).getValue();
    var link = source_sheet.getRange("I"+i).getValue();
    var picture = source_sheet.getRange("J"+i).getValue();
    var comment = source_sheet.getRange("K"+i).getValue();
    var portId = source_sheet.getRange("L"+i).getValue();
    var status = source_sheet.getRange("M"+i).getValue();
    var data = [office,title,imdbId,channel,type,added,deadline,airing,link,picture,comment,portId,status];
    target_sheet.appendRow(data);
    rowsToBeDeleted.push(i);
    i++;
    } else {
      i++;
      }
    }
    rowsToBeDeleted.reverse();
    for (var j = 0; j < rowsToBeDeleted.length; j++)
    {
        source_sheet.deleteRow(rowsToBeDeleted[j]);
    }
    SpreadsheetApp.getActiveSpreadsheet().toast('Archivation finished.', 'Status');
  }


texts:
I have these queries written in Entity Framework where I try to get:

Name of the customer
Most Recent Invoice Ref
Most Recent Invoice Amount (£)
Number of outstanding invoices (#)
Total of all outstanding invoices (£)

I use these two tables:

CREATE TABLE Customers
(
    CustomerId INT,
    Name NVARCHAR(100),
    Address1 NVARCHAR(100),
    Address2 NVARCHAR(100),
    Postcode NVARCHAR(100),
    Telephone NVARCHAR(15),
    CONSTRAINT Customers_PK PRIMARY KEY (CustomerId)
)

CREATE TABLE Invoices
(
    InvoiceId INT,
    CustomerId INT, -- FK
    Ref NVARCHAR(10),
    InvoiceDate DATETIME,
    IsPaid BIT,

    Value DECIMAL,
    CONSTRAINT Invoices_PK PRIMARY KEY (InvoiceId)
)

And the queries I make are:

public PaginatedList<CustomerListEntity> GetCustomers(int pageIndex, int pageSize, out int totalRecords)
        {
            var customers = _customerRepository.AllIncluding(x => x.Invoices).OrderBy(x => x.CustomerId).Select(x => new
              CustomerListEntity {
                Name = x.Name,
                CustomerID=x.CustomerId,
                RecentInvoiceRef = x.Invoices.OrderByDescending(t => t.InvoiceDate).FirstOrDefault() != null ? x.Invoices.OrderByDescending(t => t.InvoiceDate).FirstOrDefault().Ref : string.Empty,
                RecentInvoiceAmount = x.Invoices.OrderByDescending(t=>t.InvoiceDate).FirstOrDefault()!=null? x.Invoices.OrderByDescending(t => t.InvoiceDate).FirstOrDefault().Value:null,
                UnpaidInvoicesNumber=x.Invoices.Where(t=>t.IsPaid==false).Count(),
                UnpaidInvoicesTotalAmount = x.Invoices.Where(t => t.IsPaid == false).Sum(k=>k.Value)
            });
            totalRecords = customers.Count();
            return customers.ToPaginatedList(pageIndex, pageSize);
        }

Here is the implementation of AllINcluding:

public virtual IQueryable<T> AllIncluding(params Expression<Func<T, object>>[] includeProperties)
    {
        IQueryable<T> query = _entitiesContext.Set<T>();

        foreach (var includeProperty in includeProperties)
        {
            query = query.Include(includeProperty);
        }
        return query;
    }


texts: Calculate the sum of interior angles of a polygon

texts:
Of the languages that I use, I have the most experience with C++. I am looking to sharpen my skills with C, so I wrote this small calculator program.

What I have here is a sample of how I would probably write larger programs.

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

int getSumInteriorAngles(const unsigned int numSides)
{
    return 180 * (numSides - 2);
}

int main(int argc, char** argv)
{
    /* Keep string constants of the questions. */

    const char* howManySides =
    "How many sides does the shape have? ";

    const char* askAnotherShape =
    "Would you like to find interior angles for another polygon? (1 or 0) ";

    /* Table of polygon names from 3 to 12 sides */
    const char* names[] = {
        "triangle",
        "quadrilateral",
        "pentagon",
        "hexagon",
        "heptagon",
        "octagon",
        "nonagon",
        "decagon",
        "hendecagon",
        "dodecagon"
    };

    /* Input variable */
    int numSides = 0;

    /* Assume that the input is invalid. */
    int sidesValid = 0;

    /* Input variable */
    int askAgain = 0;

    do {
        do {
            sidesValid = 0;

            /* Ask for the number of sides. */
            printf(howManySides);

            /* Get the user input. */
            scanf("%i", &numSides);

            /* Handle invalid input. */
            if(numSides < 0) {
                printf("The number of sides cannot be negative...\n");
            }
            else if(numSides == 0) {
                printf("The number of sides cannot be zero...\n");
            }
            else if(numSides < 3) {
                printf("The shape must have at least 3 sides...\n");
            }
            else sidesValid = 1;
        }
        while(sidesValid == 0);

        /* If we have a name for this polygon, then get it from the table. */
        if(numSides <= 12) {
            char shapeName[128];
            /* Don't forget to copy the string terminator, so add 1 to the
               copy length. */

            memcpy(shapeName, names[numSides - 3],
                strlen(names[numSides - 3]) + 1);

            printf("The sum of the interior angles of a %s is %i\n", shapeName,
                getSumInteriorAngles(numSides));
        }

        /* If we don't have a special name for this polygon, then just refer
           to it non-specifically. */

        else if(numSides > 12) {
            printf("The sum of the interior angles of this polygon is %i\n",
                getSumInteriorAngles(numSides));
        }

        printf(askAnotherShape);
        scanf("%i", &askAgain);

    } while(askAgain);

    return 0;
}


texts:
Would the following be considered idiomatic Go code?

My main concerns are:

The use of maps for this purpose.  I basically want the data structure to be completely dynamic in size and have easy access to elements by id.  This is because I will want to retrieve specific items on demand lots of times, so I don't want to loop each time.
The check for the map element
My use of pointers
General flow and structure

package main

    import (
        "fmt"
    )

    type BoxItem struct {
        Id  int
        Qty int
    }

    func NewBox() *Box {
        return &Box{make(map[int]*BoxItem), 0}
    }

    type Box struct {
        BoxItems map[int]*BoxItem
    }

    func (this *Box) AddBoxItem(id int, qty int) *Box {

        i, ok := this.BoxItems[id]

        if ok {
            i.Qty += qty
        } else {
            boxItem := &BoxItem{id, qty}
            this.BoxItems[id] = boxItem
        }

        return this
    }

    func main() {

        box := NewBox()
        box.AddBoxItem(1, 1)

        fmt.Printf("There are %d items in the box", len(box.BoxItems))
    }


texts: Replace each whitespace in a string with "%20"

texts:
My implementation:

string ReplaceAllSpaces(string input)
{
    StringBuilder builder = new StringBuilder();
    using(StringReader reader = new StringReader(input))
    {
        while(reader.Peek() != -1)
        {
            char c = (char)reader.Peek();
            if(char.IsWhiteSpace(c))
            {
                while(char.IsWhiteSpace(c))
                {
                    reader.Read();
                    c = (char)reader.Peek();
                }
                builder.Append("%20");
            }
            else builder.Append((char)reader.Read());
        }
    }
    return builder.ToString();
}

Input:

"My      Name Is  John"

Output:

"My%20Name%20Is%20John"

How could this be improved?  Only ASCII characters are permitted.


texts: Reading entire contents of file at a given java.no.file.Path into a String

texts:
What I'm wanting to do is create a String readFile(Path filePath, Charset encoding) method, that returns the entire contents of a given file.

I'm wanting the equivalent of Guava's Files.toString(file, encoding), but without calling the Path.getFile() method (as this method only works with the default filesystem provider, and in this particular case I'm wanting to write tests that use JimFS).

I've written the following, but I'm wondering if it can be improved:

import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import java.nio.file.Files;
⋮
public String readFile(Path path, Charset encoding) throws IOException {
    Preconditions.checkState(Files.exists(path), "File does not exist: %s", path);
    Preconditions.checkState(Files.isRegularFile(path), "File is not regular file: %s", path);

    return Joiner.on(System.lineSeparator()).join(Files.readAllLines(path, encoding));
}

Is there a better way to read the file in (such as without using Files.readAllLines() which results in a slightly awkward List<String>). I'd previously joined this result together by streaming the lines of the file, and then immediately collecting them with Collectors.joining(System.lineSeparator()), but since the project has a dependency on Guava for other purposes, it seems preferable to use the Joiner available.

I'm happy to restrict this to Java 8 and onwards if needed, and as previously mentioned I already have a dependency on Guava, but I'd rather not add too many dependencies if possible.


texts:
Here's the code I'm having trouble shortening. I am currently a beginner in Python 3.x.
from os import system

def fibonacci(terms):
    count = 0
    x = 0
    y = 1
    z = x + y
    if terms <= 0:
        print("Enter a positive integer.")
    elif terms == 1:
        print("Fibonacci sequence in " + str(terms) + " terms.")
        print(x)
    else:
        print("Printing " + str(terms) + " terms.")
        while count < terms:
            print(x)
            z = x + y
            x = y
            y = z
            count += 1

while True:
    user_input = int(input("How many terms? "))
    fibonacci(user_input)
    loop = input("Again? (Y/n) ")
    if loop == "Y" or loop == "":
        system("cls")
    else:
        exit()

I would appreciate if there will be someone who will be able to make this as efficient as possible.


texts: Creating a struct-of-arrays database with encapsulating interface c++

texts:
I want to create a struct-of-arrays (SOA) database for better cache use but also have a separate interface that gives encapsulation. I am close to the design I think but would like more experienced coding eyes to have a look to critique my design. Here is what I have so far:
struct OrbitalDataBase {
  OrbitalDataBase(std::shared_ptr<FEMDVR> a_radial_grid, std::shared_ptr<AngularGrid> a_angular_grid)
      : nbas(a_radial_grid->getNbas()), number_of_lm_pairs_per_orbital(), m_orbital_coefs() {
    m_orbitals_coefs.reserve(a_radial_grid->getNbas() * a_angular_grid->getNumChannels());
}

  inline void StoreData(const uint32_t &a_ith_orbital,
                        std::shared_ptr<MOPartialWaveRepresentation> a_MO) {

    this->number_of_lm_pairs_per_orbital.push_back(a_MO->getNumChannels());

    this->m_orbital_coefs.reserve(a_ith_orbital * nbas *
                                  a_MO->getNumChannels());

    for (uint32_t i = 0; i < nbas * a_MO->getNumChannels();
         ++i) {
      this->m_orbital_coefs.push_back(a_MO->getPartialWaveRep(i));
    }
  }

  /* private: */
  uint32_t nbas;
  std::vector<uint32_t> number_of_lm_pairs_per_orbital;
  std::vector<std::complex<double>> m_orbital_coefs;
};

struct OrbitalAccessor {
  OrbitalAccessor(const OrbitalDataBase &a_orbital_data)
      : orbital_data(a_orbital_data){};

  inline std::complex<double> coef(uint32_t ith_orbital, uint32_t i) const {
    return this->orbital_data.m_orbital_coefs
        [ith_orbital * this->orbital_data.nbas *
             this->orbital_data.number_of_lm_pairs_per_orbital[ith_orbital] +
         i];
  }

private:
  const OrbitalDataBase orbital_data;
};

How this would work is construct the database with the number of orbitals and then in a for-loop, save all the ith_orbital's coefficients in one large array. Something like this
uint32_t number_of_orbitals = 5;
OrbitalDataBase orb_data_base(number_of_orbitals);
for (uint32_t ith_orbital = 0; ith_orbital < number_of_orbitals < ++ith_orbital)
  {
    Orbital();
    orb_data_base.StoreData(ith_orbital, femdvr_grid, orbital);
  }

The Orbital() function just creates the orbital I want to store. Then the OrbitalAccessor should be able to access these large arrays index with stride ith_orbital. I think I should pass the accessor the created database I want to access in a constructor like
  int which_orbital = 1, int index 4; // example looks at 2nd orbital's 5th coefficient
  OrbitalAccessor orb_accessor(orb_data_base);
  orb_accessor.coef(which_orbital, index);

I tried to have the ObitalDataBase member data private but I get

'm_orbitals_coefs' is a private member of 'OrbitalDataBase'

so I commented the private: keyword so it will compile. I think they should be private for encapsulation so I'd prefer that if I am correct.
I think a better way would be to have OrbitalAccessor derive the OrbitalDataBase rather than pass it in to a constructor.
UPDATE: I changed the code a little so now the m_orbital_coef has a base vector capacity and resizes if it becomes larger because a_MO->getNumChannels() can be larger than a_angular_grid->getNumChannels().