texts: Advent Of Code 2017 Day 4 (part 1) in Functional Programming (FP)

texts:
I wanted to practice functional programming (fp) without using any library but using vanilla JS only. So I took a problem from Advent of Code (the 1st part of Day 4):

  A new system policy has been put in place that requires all accounts
  to use a passphrase instead of simply a password. A passphrase
  consists of a series of words (lowercase letters) separated by spaces.

  To ensure security, a valid passphrase must contain no duplicate
  words.

  For example:

  aa bb cc dd ee is valid.

  aa bb cc dd aa is not valid - the word aa appears more than once.

  aa bb cc dd aaa is valid - aa and aaa count as different words.

  The system's full passphrase list is available as
  your puzzle input. How many passphrases are valid?

My solution in FP:

const INPUT =
`pphsv ojtou brvhsj cer ntfhlra udeh ccgtyzc zoyzmh jum lugbnk
vxjnf fzqitnj uyfck blnl impo kxoow nngd worcm bdesehw
...
caibh nfuk kfnu llfdbz uxjty yxjut jcea`;

const get = input => input.split('\n');
const countDuplicate = words => words.reduce((acc, word) => {
  return Object.assign(acc, {[word]: (acc[word] || 0) + 1});
}, {});
const onlyUniqueWords = phrases => {
  const words = phrases.split(' ');
  const duplicateWords = countDuplicate(words);
  return !Object.values(duplicateWords).some(w => w > 1);
};
const phrasesWithUniqueWords = get(INPUT).filter(onlyUniqueWords);
console.log("solution ", phrasesWithUniqueWords.length);

Is there a better way to write it in FP with pure JavaScript, i.e. no additional FP library? Any other improvement suggestions are welcomed.


texts: Is this the best message delay algorithm?

texts:
In my application I am attempting to queue outgoing messages by the following rules

By default they should be sent no less than messageDelay apart
Some messages can be sent immediately, completely bypassing all queued messages
Additionally, some immediately sent messages can reset the waiting time of queued messages

If this is confusing, this is what it would look like on the wire

|  Normal  |Immediate |Immediate-reset|
M----------M---M------M----M----------M ....

This was originally handled by a dedicated OutputThread and queue but for various reasons outside of the scope of this question I am attempting to implement this with ReentrantLock and Condition so when any of the methods finally returns, the message is known to of been sent (or an Exception is thrown meaning it didn't)

However this is my first time using ReentrantLock and while I think I've implemented it correctly I'm needing someone to verify that its correct since this is an extremely difficult thing to test

protected final ReentrantLock writeLock = new ReentrantLock(true);
protected final Condition writeNowCondition = writeLock.newCondition();

public void sendRawLine(String line) {
    //[Verify state code]
    writeLock.lock();
    try {
        sendRawLineToServer(line);
        //Block for messageDelay. If rawLineNow is called with resetDelay
        //the condition is tripped and we wait again
        while (writeNowCondition.await(getMessageDelay(), TimeUnit.MILLISECONDS)) {
        }
    } catch (Exception e) {
        throw new RuntimeException("Couldn't pause thread for message delay", e);
    } finally {
        writeLock.unlock();
    }
}

public void rawLineNow(String line, boolean resetDelay) {
    //[Verify state code]
    writeLock.lock();
    try {
        sendRawLineToServer(line);
        if (resetDelay)
            //Reset the
            writeNowCondition.signalAll();
    } finally {
        writeLock.unlock();
    }
}

Is this the correct way to implement ReentrantLock based on my requirements above? Is there a better way (NOT USING THREADS) to do this?


texts: Whitelist for textarea and save input to file

texts:
I'm working with product codes, so a user can only scan these type of product codes into a textarea element.
I don't know if my code is written the best way possible, or has good performance.
I'm using a whitelist solution for preventing bad entries.
It saves into file all product codes after 3 seconds of last keyup.
Is it good to use javascript and jQuery mixed?
And is it the correct way to use a php query to fetch product code and product code ending?
Why is "use strict" mode useless in my code?
"use strict";
toFile.focus();

var timeoutId;
toFile.addEventListener("keyup", event => {
    clearTimeout(timeoutId);
    timeoutId = setTimeout(function() {
        const data = toFile.value.split("\n");
        const result = data.unique();
        info.textContent = result.length !== data.length ? "Duplicate removed" : "";
        toFile.value = result.join('\n');
    }, 100);

    timeoutId = setTimeout(function() {
        onInput(document.getElementById('toFile'));
    }, 3000);
});

if (!Array.prototype.unique) {
    Object.defineProperty(Array.prototype, "unique", {
        configurable: false,  // these 3 properties default to false so not needed
        enumerable: false,    // But have added them just to show their availability.
        writable: false,
        value: function() {
            const existing = {};
            return this.filter(a => existing[a] = !existing[a] ? true : false);
        }
    });
} else {
    throw new Error("Array.prototype.unique already defined.");
}

const regexNounFilters = [<?php
    $data = $pdo->query("SELECT PRODUCT_CODE AS code, ENDING AS ec FROM test")->fetchAll(PDO::FETCH_OBJ);

    foreach ($data as $key) {
        $separator = ($key != end($data)) ? ", " : '';
        $std = "/^(" . $key->code . ")([a-zA-Z0-9]{" . $key->ec . "})$/";
        echo $std.$separator;
    }
?>];

// example: const regexNounFilters = [/^(AAAB)([a-zA-Z0-9]{7})$/, /^(BBBBBC)([a-zA-Z0-9]{9})$/];

const extractNouns = string =>
  string
  .split('\n')
  .filter(line =>
    regexNounFilters.some(re =>
      line.trim().toUpperCase().match(re)
    )
);

function onInput(target) {
    target.value = extractNouns(target.value).join('\n');
}

function saveToFile() {
    console.log('Saving to the db');
    toFile = $('#toFile').val().replace(/\n\r?/g, '<br />');
    $.ajax({
        url: "test2.php",
        type: "POST",
        data: {toFile:toFile}, // serializes the form's elements.
        beforeSend: function(xhr) {
            // Let them know we are saving
            $('#status').html('Saving...');
        },
        success: function(toFile) {
            // You can get data returned from your ajax call here. ex. jqObj.find('.returned-data').html()
            // Now show them we saved and when we did
            var d = new Date();
            $('#status').html('Saved! Last: ' + d.toLocaleTimeString());
        },
    });
}

$('.form').submit(function(e) {
    saveToFile();
    e.preventDefault();
});


texts:
I need an example on writing this code with range based loops:

int pListPet[2] = {34001, 34051};

int pListGlobal[] = {9001, 9002, 9003, 9004, 9005, 9006, 20011, 20091, 20092, 20093, 20094, 20095, 30000};

const char* strMapListGlobal[] = {"string1" , "string2"};

const char* strMapEventOx = "string3";

std::string stringName = CPythonBackground::Instance().GetWarpMapName();

for (int i = 0; i < _countof(strMapListGlobal); i++)
{
    #ifdef CHECK_STRING_3
    if (strMapEventOx == stringName)
    {
        if (0 <= rVictim.GetRace() && rVictim.GetRace() <= 7)
            return FALSE;
    }
    #endif

    if (strMapListGlobal[i] == stringName)
    {
        for (int i = 0; i < _countof(pListGlobal); i++)
        {
            if (rVictim.GetRace() == pListGlobal[i] || pListPet[0] <= rVictim.GetRace() && rVictim.GetRace() <= pListPet[1])
                return FALSE;
        }
    }
}

Do you think this correctly uses std::string? Can this be optimized more or maybe written shorter?

Should I use this?

if (!strcmp(strMapEventOx, stringName))


texts: Managing the state of multiple, similar, selects in React Form

texts:
I have a really simple form with 3 select boxes for days, hours, and minutes (in the full code I have a submit button attached to an ajax request). And I've got each of those select boxes attached to their own onChange function. But in light of simplicity and dryness, could I do better?

class FutureUpgradeForm extends React.Component {
  constructor(props) {
    super(props)
      days: this.props.days,
      hours: this.props.hours,
      minutes: this.props.minutes
    }
  }

  handleDaySelect(e) {
    this.setState({
      days: e.target.value
    })
  }

  handleHourSelect(e) {
    this.setState({
      hours: e.target.value
    })
  }

  handleMinuteSelect(e) {
    this.setState({
      minutes: e.target.value
    })
  }

  render() {
    return(
      <div>
        <select value={this.state.days} onChange={this.handleDaySelect.bind(this)}>
          <option value="0">0 Days</option>
          <option value="1">1 Day</option>
        </select>
        <select value={this.state.hours} onChange={this.handleHourSelect.bind(this)}>
          <option value="0">0 Hours</option>
          <option value="1">1 Hour</option>
        </select>
        <select value={this.state.minutes} onChange={this.handleMinuteSelect.bind(this)}>
          <option value="0">0 Minutes</option>
          <option value="1">1 Minute</option>
        </select>
      </div>
    )
  }
}


texts: Extension methods for translation engine

texts:
Our application uses some singletons for localized strings. These calls used to be long and dirty. Therefore some co-worker created extensions for the int and string type to ease the usage of translations.

What does the code do: Find a translation string based on the key (the int value). If it cannot be found it falls back to the first provided string. The latter is a params[] string used in underlying string.Format() method as parameters.

const var warrantyDescription = "14";
var text = 4426.Translate("Garantietermijn {0} mnd", warrantyDescription);

However, I think this approach is bad because:

Bad SoC. Int and string have nothing todo with translations
This will result in unreadable and unmaintainable code from the perspective of other developers, encountering a extended int with specific functionality. Imagine a case where not only translations but xx other functionalities extend the int as it is a unique identifier.

Trying to provide him with a better approach, all I could think of was a struct using implicit operators as below. However, I think this casting is bad practise as well..

const string warrantyDescription = "14";
var text = ((TranslateId) 4426).Translate( "Garantietermijn {0} mnd", warrantyDescription );

To make this question complete, here is my code for the struct type.

public struct TranslateId
{
    private readonly int m_Value;

    public TranslateId (int value)
    {
        m_Value = value;
    }

    public int Value
    {
        get { return m_Value; }
    }

    public string Translate(string defaultString, params object[] parameters)
    {
        // TODO: Use the actual singleton to retrieve translation
        string translationResult = defaultString;

        // Return the formatted retrieved value
        return string.Format(translationResult, parameters);
    }

    public static implicit operator TranslateId(int value)
    {
        return new TranslateId(value);
    }

    public static implicit operator int(TranslateId id)
    {
        return id.Value;
    }
}

Any thoughts?


texts:
I recently asked a question on Software Engineering and I had a comment:

  The updated version has some issues which are out of topic here. You may consider posting it at codereview.stackexchange.com to get some suggestions for improvements.

The code I had asked in that site:

public class GroupBillingPayment
{
    public void Save(IGroupBillingPayment model)
    {
        if (model == null || UserInfo.UserID == 0)
        {
            throw new Exception("GroupBillingPayment object or Current User Id is NULL , Please Contact Administrator.");
        }
        Data.GroupBillingPayment groupBillingPayment = RepositoryManager.GroupBillingPaymentRepository.GetById(model.GroupBillingPaymentID);
        Mapper.Map(model, groupBillingPayment);
        ServiceManager.GroupBilling.IsBillAlreadyCancelled(groupBillingPayment.GroupBillingID, THROW_ERROR);
        groupBillingPayment.UpdatedBy = UserInfo.UserID;
        groupBillingPayment.UpdatedOn = DateTime.Now;
        RepositoryManager.GroupBillingPaymentRepository.Update(groupBillingPayment, false);
        UpdateGroupBilling(groupBillingPayment.GroupBillingPaymentID, groupBillingPayment.GroupBillingID)
    }
}

Could anyone please highlight the issues in this method?

A bit more details about the above method:

ServiceManager and RepositoryManager are private properties, which have been injected into this class via constructor using Autofac.

The Service/Repository managers contain the reference to the classes in that particular layer. Hence ServiceManager can access the service GroupBilling which is in service layer and RepositoryManager can access GroupBillingPaymentRepository.

The classes in service and repository layers are resolved using Autofac and so is UserInfo.

[Parameters] are the necessary parameters required for that method to run. In this case, it is groupBillingPayment.GroupBillingPaymentID and groupBillingPayment.GroupBillingID (have updated the code to reflect this).


texts: Database "design" using Symfony2 and Doctrine2

texts:
I'm working on an application, which tracks expenses. I have users, and each user can create his categories for the expenses (like Food, Bills, Transport, Drinks, Clothes) and then create expenses - each expense has one category (OneToMany relationship).

For example, the expense Banana is in the category Food for the user Faery. I also have months for each user. For example, for March 2013, user Faery has a budget of 500.  For March 2013, user SuperCoolUser has a budget of 900 and so on.

Here are my entities:

User.php

/**
 * @ORM\Entity
 * @ORM\Table(name="fos_user")
 */
class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\OneToMany(targetEntity="Category", mappedBy="category")
     */
    protected $categories;

    /**
     * @ORM\OneToMany(targetEntity="Expense", mappedBy="expense")
     */
    protected $expenses;

    public function __construct()
    {
        parent::__construct();

        $this->categories = new ArrayCollection();
        $this->expenses = new ArrayCollection();
    }
    // ...
}

Category.php

/**
 * @ORM\Table(name="category")
 * @UniqueEntity(fields={"name", "user"})
 */
class Category
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\OneToMany(targetEntity="Expense", mappedBy="expense")
     */
    protected $expenses;

    public function __construct()
    {
        $this->expenses = new ArrayCollection();
    }

    /**
     * @ORM\Column(type="string", length=100)
    protected $name;

    /**
     * @ORM\ManyToOne(targetEntity="User", inversedBy="fos_user")
     * @ORM\JoinColumn(name="fos_user_id", referencedColumnName="id")
     */
    protected $user;
    // ...
}

Expense.php

/**
 * @ORM\Table(name="expense")
 */
class Expense
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity="User", inversedBy="fos_user")
     * @ORM\JoinColumn(name="fos_user_id", referencedColumnName="id")
     */
    protected $user;

    /**
     * @ORM\ManyToOne(targetEntity="Category", inversedBy="category")
     * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
     */
    protected $category;

    /**
     * @ORM\Column(type="string", length=100)
     */
    protected $product;

    /**
     * @ORM\Column(type="string")
     */
    protected $description;

    /**
     * @ORM\Column(type="float")
     */
    protected $price;

    /**
     * @ORM\Column(type="date")
     */
    protected $date;
    // ...
}

Month.php

/**
 * @ORM\Table(name="month")
 */
class Month
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string")
     */
    protected $name;

    /**
     * @ORM\ManyToOne(targetEntity="User", inversedBy="fos_user")
     * @ORM\JoinColumn(name="fos_user_id", referencedColumnName="id")
     */
    protected $user;

    /**
     * @ORM\Column(type="float")
     */
    protected $budget;
    /...

}

All of this looked fine to me, but I noticed that category, month, expense, all of these has a connection to User. I made this for the queries to be more convenient, but isn't it becoming too duplicated?

I have really little experience with databases and with programming, but I want the application to become nice and the code to be of good quality.

I want to know if this code is too problematic and connected and duplicated, or it's allowable. And if it's not please give me some advice or ideas on how to implement this better.


texts: Turning a flat nested set into a hierarchy

texts:
So I have categories in my database and I've implemented the Nested set model. I'm reading the categories as a flat structure and then I'm turning it into a hierarchy in memory, mapping each entity to a DTO.

Here's my code:

public async Task<IActionResult> GetCategories()
{
    var categories = (List<Category>)await _categoryService.GetAllAsync();

    var categoryDtos = GetCategoryDtosHierarchy(categories, null);

    categoryDtos.RemoveAll(x => x.Depth > 0); // Terrible

    return Ok(categoryDtos);
}

private List<CategoryDto> GetCategoryDtosHierarchy(List<Category> categories, Category parent)
{
    var categoryDtos = new List<CategoryDto>();
    var filteredCategories = categories;

    if (parent != null)
    {
        filteredCategories = categories.Where(x => x.Depth == (parent.Depth + 1) &&
                                                   x.Left > parent.Left &&
                                                   x.Right < parent.Right).ToList();
    }

    foreach (Category filteredCategory in filteredCategories)
    {
        var categoryDto = new CategoryDto
        {
            Id = filteredCategory.Id,
            Name = filteredCategory.Name,
            Depth = filteredCategory.Depth,
            SubCategories = GetCategoryDtosHierarchy(categories, filteredCategory)
        };

        categoryDtos.Add(categoryDto);
    }

    return categoryDtos;
}

So everything works but it feels like somewhat an awkward solution. When the GetCategoryDtosHierarchy(categories, null); line returns it not only returns the categories with their subcategories but every level below like so:

[
  {
    "name": "Clothing",
    "depth": 0,
    "subcategories": [
      {
        "name": "Men's",
        "depth": 1,
        "subcategories": [
          {
            "name": "Suits",
            "depth": 2,
            "subcategories": []
          }
        ]
      },
      {
        "name": "Women's",
        "depth": 1,
        "subcategories": []
      }
    ]
  },
  {
    "name": "Men's", // Not supposed to be here
    "depth": 1,
    "subcategories": [
      {
        "name": "Suits",
        "depth": 2,
        "subcategories": []
      }
    ]
  },
  {
    "name": "Women's", // Not supposed to be here
    "depth": 1,
    "subcategories": []
  },
  {
    "name": "Suits", // Not supposed to be here
    "depth": 2,
    "subcategories": []
  }
]

And it has forced me to run the categoryDtos.RemoveAll(x => x.Depth > 0); in order to get rid of the excess. I've thought about putting both calls in a new method but I feel like there might be a better solution than this hack.


texts: New Game - Am I going down the right path for DOM manipulation?

texts:
I find myself using a lot of .find functions within what I'm developing. The reason being that I need to run this same function on a TON of different characters eventually, and the easiest way I could find to do this was make appropriate sections classes and wrap each character in their respective ID.

Is using .find a ton (to only target the classes within the #characterId) considered bad practice?

JS Fiddle Example: http://jsfiddle.net/hgwNU/6/

Example Script:

(x is the internal javascript object, where y is the DOM ID for what to target)

function updateDom(x,y){
    if(x.pc < 1 || x.speed >= 4){
        y.find('.addSpeed').attr('disabled', 'disabled');
    }
    if(x.speed <= 1){
        y.find('.remSpeed').attr('disabled', 'disabled');
    }
    if(x.speed > 1){
        y.find('.remSpeed').removeAttr('disabled');
    }
    if(x.speed < 4 && x.pc > 0){
        y.find('.addSpeed').removeAttr('disabled');
    }
    y.find(":checkbox").each(function() {
        if($(this).data('pc') > x.pc && $(this).prop('checked')==false){
            $(this).attr('disabled', 'disabled');
        } else if ($(this).data('pc') <= x.pc) {
            $(this).removeAttr('disabled');
        }
    });
    for (var key in x) {
        y.find('#' + key).text(x[key]);
    };
}

Called with:

updatePlayer(bulk,$('#bulkUi'));

JS Fiddle Example: http://jsfiddle.net/hgwNU/6/

I suppose I'm just curious if I'm off to a good start for how to manipulate both the GUI and the internal object effectively, easily, quickly and repeatedly. Looking for best practices if anybody has any feedback thus far.


texts: Printing a list to a file then reassembling said list

texts:
This is a GCSE practice problem, to practice for the actual coursework. Computing's my passion and I'd like to impress the examiners (note: any tips would be appreciated)

Essentially, this is simply splitting by lines into an array, printing said array into a file, then reassembling it by looping through one list by indexing the other.

uniqueword =[]
for i in range(0,len(sentencelist)):
    if not sentencelist[i] in uniqueword:#<--- creates list of unique words
        uniqueword.append(sentencelist[i])
    else:
        i+=1
print(uniqueword)
positionlist = []
for i in range(0,len(sentencelist)):
    positionlist.append(uniqueword.index(sentencelist[i]))#<-- finds the index of the unique words in the sentence and appends them to positionlist
print(positionlist)
wordvals = []
end=False
im = 0
while True:
    try:
        wordvals.append(uniqueword[im])
        im+=1
    except:
        break
print(wordvals)
text_file = open("sentencemanip.txt", "w")#prints by line on new lines
for item in positionlist:
  text_file.write("%s\n" % item)
text_file.close()
text_file = open("sentencemanip1.txt", "w")
for item in wordvals:
  text_file.write("%s\n" % item)#whee
text_file.close()
print('the sentence has been printed to the text files sentencemanip.txt and sentencemanip1.txt')

import sys
sentencelist = []
def splitsent(text):

    # split the text
    words = text.split()

    # for each word in the line:
    for word in words:
        sentencelist.append(word)

and

    import sys
    pl = []
    wv= []
    with open('sentencemanip.txt') as f:
        pl = f.read().splitlines()
    f.close()
    with open('sentencemanip1.txt') as f:
    wv=f.read().splitlines()
    f.close()
    print(pl)
    print(wv)
    for i in pl: print(wv[int(i)],'',end = '')#<- here

The question is, is there a better/more efficient/faster way to accomplish this task? Am I making any elementary errors in variable assignment? Would while or for loops be better?

Any help is appreciated on my continuing quest to become not awful at python!


texts:
Today I created my first simple AJAX application. It works fine, but I am a bit unsure if I did everything correct. I'd like to use the code as template for future projects.

function _solve(id) {
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function () {
        var DONE = 4; // readyState 4 means the request is done.
        var OK = 200; // status 200 is a successful return.
        if (xhr.readyState === DONE) {
            if (xhr.status === OK) {
                obj = JSON.parse(xhr.responseText);
                if (obj.success) {
                    if (document.getElementById('line'+obj.id).style.display != 'none') {
                        document.getElementById('line'+obj.id).style.display = 'none';
                        document.getElementById('count').innerHTML = document.getElementById('count').innerHTML - 1;
                    }
                } else {
                    alert('Json error: ' + obj.error);
                }
            } else {
                alert('Error: ' + xhr.status); // An error occurred during the request.
            }
        }
    };
    xhr.open('POST', 'ajax_cmd.php', true);
    xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    xhr.send('cmd=solve&id='+id);
}

Beside general improvement suggestions, I have following particular questions:

Do I need to somehow close the connection? It looks like send() automatically closes the connection because I cannot execute send() twice.
Is the error handling ok? When my internet connection was a bit unstable, I got the message "Error: 0". Zero sounds like a strange status code.

Note: For now, I'd like to stick with plain JavaScript, without jQuery or other frameworks.

Explanation/context of the code:

The HTML page has a table. Each table row  represents a dataset with an ID. The id of each row is "line" plus the dataset ID, e.g. "line1". Each table row has a link named "Solve" which calls the JavaScript function _solve(id).

  php/error.log
  Solve
  1113
  2017-09-03&nbsp;01:18:18
  Xyz...

When the user clicks at "Solve", a PHP script (ajax_cmd.php) is called with AJAX. The PHP script deletes the dataset and confirms the deletion with the JSON object success: true; id: (the ID) on success or success: false; error: (the error message) on failure.

After the PHP script confirmed the successful deletion, the AJAX script hides the table row and decreases a row counter.

A double-click prevention (which might result in a double-decrease of the counter) is realized with a check if the row is already hidden.


texts: Can this sampling code be vectorized or optimized?

texts:
I have some code that is slow because it loops over every row in an input matrix Y. Obviously, this code does not scale with the number of rows. I don't think it's critical to understand what the code does, but for the curious, it is a Gibbs sampling step for sampling the dispersion parameter r for the negative binomial distribution.

Is there anything I can do to make this faster? I've tried for y in Y_j[Y_j > 0] in the crt_sum function, but that's not actually faster according to a simple speed test. I suspect because in many cases, the code loops over every row in the Y_jth column of Y in C and then does it again in Python.

import numpy as np

def sample_r(Y, P, R, e0=1e-2, f0=1e-2):
    """Sample negative binomial dispersion parameter `r` based on
    (Zhou 2020). See:

    - http://people.ee.duke.edu/~mz1/Papers/Mingyuan_NBP_NIPS2012.pdf
    - https://mingyuanzhou.github.io/Softwares/LGNB_Regression_v0.zip
    """
    J = Y.shape[1]
    for j in range(J):
        L = crt_sum(Y, R, j)
        A = e0 + L
        # `maximum` is element-wise, while `max` is not.
        maxes = np.maximum(1 - P[:, j], -np.inf)
        B = 1. / (f0 - np.sum(np.log(maxes)))
        R[j] = np.random.gamma(A, B)
    # `R` cannot be zero.
    inds = np.isclose(R, 0)
    R[inds] = 0.0000001
    return R

def crt_sum(Y, R, j):
    """Sum independent Chinese restaurant table random variables.
    """
    Y_j = Y[:, j]
    r   = R[j]
    L   = 0
    tbl = r / (r + np.arange(Y_j.max()))
    for y in Y_j:
        if y > 0:
            u = np.random.uniform(0, 1, size=y)
            inds = np.arange(y)
            L += (u <= tbl[inds]).sum()
    return L

Synthetic data should be sufficient. Basically, Y is a matrix of count data (non-negative integers), P is a matrix of numbers in the range [0, 1], and R is a matrix of positive (nonzero) real numbers. This should be sufficient to generate realistic data:

def sigmoid(x):
    return 1 / (1 + np.exp(-x))

N = 100
J = 10
Y = np.arange(N*J).reshape(N, J)
P = sigmoid(np.random.random((N, J)))
R = np.arange(J, dtype=float)+1

sample_r(Y, P, R)

I currently don't have any tests (sorry, lonely researcher code), but assuming my implementation is correct, this should pass

assert(sample_r(Y, P, R) == sample_r_fast(Y, P, R))

where sample_r_fast a faster implementation. Thanks in advance.


texts: Reading Values From Enviromental Monitor XML Feed

texts:
I have written the below code to handle accessing and parsing the data presented in XML format from our server room environmental monitor.

The plan is to call this class from a controller/view handling the "dashboard" on our Intranet page.

Is there any way I could optimize the class? should the methods really be static or would it be better to create an instance of the class and call the methods that way?

public class EnviromentialMonitorInterface
{
    private static XmlDocument LoadInXML()
    {
        string environmentalMonitorUrl = "http://192.168.6.95/data.xml";
        //string environmentalMonitorUrl = ConfigurationManager.AppSettings["WhiteListedDomainNames"];

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(environmentalMonitorUrl);

        return xmlDoc;
    }

    public static double GetTemperature()
    {
        var extraction = LoadInXML().SelectSingleNode("//field[@key='TempC']").Attributes.GetNamedItem("value").Value;

        double value = Convert.ToDouble(extraction);

        return value;
    }

    public static int GetHumidity()
    {
        var extraction = LoadInXML().SelectSingleNode("//field[@key='Humidity']").Attributes.GetNamedItem("value").Value;

        int value = Convert.ToInt32(extraction);

        return value;
    }
}


texts: Are these set-uid scripts/binaries secure?

texts:
I have a system that needs to be able to reboot a different piece of hardware partway through a script that programs it. It used to wait for me to come and reboot the hardware halfway through, but I've since automated that. I use this python script to control a USB Net Power 8800.

I have two setups to show you: First is a system that I'm sure is secure, but is pretty annoying to maintain; I'm sure the second is insecure.

First:

/usr/bin/power (not set-uid)

#!/bin/sh
if [ -e /opt/usbnetpower/_powerwrapper_$1 ] ; then
        /opt/usbnetpower/_powerwrapper_$1
else
        /opt/usbnetpower/_powerwrapper_
fi

/opt/usbnetpower/_powerwrapper_reboot.c (The source code to one of six almost identical set-uid programs)

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
   setuid( 0 );
   system( "/usr/bin/python /opt/usbnetpower/usbnetpower8800.py reboot" );

   return 0;
}

The reason why there are six is that I didn't trust myself to write correct sanitization code in C that wasn't vulnerable to buffer overflows and the like.

/opt/usbnetpower/usbnetpower8800.py (not set-uid)

Essentially this, but modified to add a reboot command.

This setup is what I am currently using.

Second:

/opt/usbnetpower/usbnetpower8800.py (set-uid)

Much simpler, but I'm concerned that a vulnerability like this would apply. Also, suid python programs appear to be disabled on my system. (CentOS 6)


texts:
I'm learning to work in GAE. I'm so used to SQL, but transform my way of think the last 20 years to NoSQL is a little hard for me.

I have the next simple structure:

BOOKS than can have CHAPTERS
CHAPTERS that can have VOTES

In a traditional SQL I just make foreign keys from VOTES to CHAPTERS, and from it to BOOKS.

I do this for my models in Datastore:

class Book(db.Model):
    title = db.StringProperty(required=True)
    author = db.StringProperty(required=True)
    created = db.DateTimeProperty(auto_now_add=True)

    # Get all the Chapters for a book
    def getChapters(self):
        key = self.chapterMemkey()
        chapters = memcache.get(key)
        if chapters is None:
            logging.info('DB access for key %s.', key)
            chapters = Chapter.all().ancestor(self).order("number").fetch(100)
            if not memcache.set(key, chapters, 300):
                logging.error('Memcache set failed for Chapters.')
        else:
            logging.info('Memcache for key %s.', key)
        return chapters

class Chapter(db.Model):
    """ All chapters that a book have """
    title = db.StringProperty(required=True)
    number = db.IntegerProperty(default=1)
    created = db.DateTimeProperty(auto_now_add=True)

    book = db.ReferenceProperty(Book,
                                required=True,
                                collection_name='chapters')

    # Search by Book (parent)
    @classmethod
    def byBook(cls, book, limit=100):
        chapter = book.getChapters()
        return chapter

    # Search by id
    @classmethod
    def byId(cls, id, book):
        return Chapter.get_by_id(long(id), parent=book)

class Vote(db.Model):
    """ All votes that a book-chapter have """
    chapterNumber = db.IntegerProperty(required=True)
    option = db.IntegerProperty(default=1)
    value = db.IntegerProperty(default=1)

    book = db.ReferenceProperty(Book,
                                required=True,
                                collection_name='book_votes')
    chapter = db.ReferenceProperty(Chapter,
                                   required=True,
                                   collection_name='chapter_votes')

    # --------------------------
    # ClassMethods for the class
    # --------------------------

    # Search by Book (parent)
    @classmethod
    def byBook(cls, book, limit=100):
        vote = book.getVotes()
        return vote

    # Search by id
    @classmethod
    def byId(cls, id, book):
        return Vote.get_by_id(long(id), parent=book)

Well, my doubts are:

Is this approach correct?
Must I define in the Vote class a reference for a book and for a chapter, as it was a foreign keys (just like I think I've done)?
Is well defined the way to retrieve the chapters from a book? I mean, in the Chapter class the function byBook uses a function from the Book class.
In the Vote class, I also define the byBook function but, must I define another byChapter function with similar behaviour than the myBook function? I mean something like that:

@classmethod
def byChapter(cls, chapter, limit=100):
    vote = chapter.getVotes()
    return vote

Are the parents well-defined for Votes (book) or must Chapter instead be a Book? The votes are for the chapters, but a chapter belongs to book.
Which are the right ways to get the sum of all the votes for a specific chapter and for specific book?


texts: Efficient smart pointer implementation in C++

texts:
The idea behind this is mainly educational but I might even consider using it in reality if turns out to be good. Here's my first try at implementing smart pointers:

template<typename T>
class smart_pointer{
    T* pointer;
    std::size_t *refs;

    void clear(){
        if (!--*refs){
            delete pointer;
            delete refs;
        }
    }

public:
    smart_pointer(T* p = NULL)
        : pointer(p), refs(new std::size_t(1))
    {}
    smart_pointer(const smart_pointer<T>& other)
        : pointer(other.pointer), refs(other.refs)
    {
        ++*refs;
    }
    ~smart_pointer(){
        clear();
    }

    smart_pointer<T>& operator=(const smart_pointer<T>& other){
        if (this != &other){
            clear();

            pointer = other.pointer;
            refs    = other.refs;
            ++*refs;
        }
        return *this;
    }

    smart_pointer<T>& operator=(T* p){
        if (pointer != p){
            pointer = p;
            *refs = 1;
        }
        return *this;
    }

    T& operator*(){
        return *pointer;
    }

    const T& operator*() const{
        return *pointer;
    }

    T* operator->(){
        return pointer;
    }

    const T* operator->() const{
        return pointer;
    }

    std::size_t getCounts(){
        return *refs;
    }
};

I have tested this under valrind and its clean. Also, to see how much the "smartness" makes things slower, I did the following test:

struct foo{
    int a;
};

template<typename pointer_t>
class bar{
    pointer_t f_;

public:
    bar(foo *f)
        :f_(f)
    {}

    void set(int a){
        f_->a = a;
    }
};

int main()
{
    foo *f = new foo;

    typedef smart_pointer<foo> ptr_t;
//    typedef boost::shared_ptr<foo> ptr_t;
//    typedef foo* ptr_t;

    bar<ptr_t> b(f);
    for (unsigned int i = 0; i<300000000; ++i)
        b.set(i);

//    delete f;
    return 0;
}

Here is some timing between my implementation, boost, and raw pointers: (code compiled with clang++ -O3)

typedef smart_pointer<foo> ptr_t;

real   0m0.006s
user   0m0.001s
sys    0m0.002s

typedef boost::shared_ptr<foo> ptr_t;

real    0m0.336s
user    0m0.332s
sys     0m0.002s

typedef foo* ptr_t;

real    0m0.006s
user    0m0.002s
sys     0m0.003s

My implementation seems to be running almost as fast as raw pointers, which I think is a good sign.  What worries me here is why Boost is running slower.  Have I missed something in my implementation that is important and I might get into trouble for later?


texts:
I would value your opinion on the following piece of code. I am rather new to both Python and Monte Carlo analysis, so I was wondering whether the routine makes sense to more experienced and knowledgeable users.

import numpy as np
import scipy.optimize
from scipy.optimize import curve_fit
from scipy.stats import kde

def MC_analysis_a():
    x = (0.001, 0.002, 0.004, 0.008, 0.016, 0.032, 0.064, 0.128)
    y_signal_a = (1.0, 0.83, 0.71, 0.62, 0.55, 0.49, 0.45, 0.42)
    x = np.array(x, dtype = float)
    y_signal_a = np.array(y_signal_a, dtype = float)
    e_signal_a = (0.06, 0.05, 0.04, 0.05, 0.04, 0.02, 0.04, 0.03)

    def func(x, a, b):
        return a * np.exp(-b * x)

    initial_guess = [1.0, 1.0]
    fitting_parameters, covariance_matrix = curve_fit(func, x, y_signal_a, initial_guess)
    print(round(fitting_parameters[1], 2))

    # ---> PRODUCING PARAMETERS ESTIMATES

    total_iterations = 5000
    MC_pars = np.array([])

    for iTrial in range(total_iterations):
        xTrial = x
        yTrial = y_signal_a + np.random.normal(loc = y_signal_a, scale = e_signal_a, size = np.size(y_signal_a))
        try:
            iteration_identifiers, covariance_matrix = curve_fit(func, xTrial, yTrial, initial_guess)
        except:
            dumdum = 1
            continue

    # ---> STACKING RESULTS

        if np.size(MC_pars) < 1:
            MC_pars = np.copy(iteration_identifiers)
        else:
            MC_pars = np.vstack((MC_pars, iteration_identifiers))

    # ---> SLICING THE ARRAY

    print(np.shape(MC_pars))
    print(np.median(MC_pars[:,1]))
    print(np.std(MC_pars[:,1]))

The output I get is apparently satisfactory and plausible.


texts:
I want to be able to construct and fill a vector of bytes using a stream (binary shift) operators. So I overloaded operator << (...).

Here is the code I want to write :

using ByteVector = std::vector<uint8_t>;

void send_message(MessageId id, std::vector<ByteVector> parameters);

void foo()
{
    std::string str = "A string object";
    int bar = 42;

    ByteVector baz = {0x42, 0x13, 0x37};

    send_message(MessageId::COMMAND, {
        ByteVector() << str,
        ByteVector() << bar,
        ByteVector() << baz
    });
}

I started by adding the following operator overloads :

ByteVector & operator << (ByteVector & bv, const std::string & str);
ByteVector & operator << (ByteVector & bv, uint8_t byte);
ByteVector & operator << (ByteVector & bv, const ByteVector & bv2);
ByteVector & operator << (ByteVector & bv, int value);

They were working well for following use case:

ByteVector bv;
std::string str = "Never gonna give you up";
bv << str;

But not the for previous example where my compiler (clang) shows the following error:

error: invalid operands to binary expression ('ByteVector' (aka 'vector<unsigned char>') and 'const std::string' (aka 'const basic_string<char, char_traits<char>, allocator<char> >'))

note: candidate function not viable: expects an l-value for 1st argument
ByteVector & operator << (ByteVector & bv, const std::string & str);
             ^
note: candidate function not viable: expects an l-value for 1st argument
ByteVector & operator << (ByteVector & bv, uint8_t byte);
             ^

note: candidate function not viable: expects an l-value for 1st argument
ByteVector & operator << (ByteVector & bv, const ByteVector & bv2);
             ^

note: candidate function not viable: expects an l-value for 1st argument
ByteVector & operator << (ByteVector & bv, int value);
             ^

I didn't really understand the error. I think the compiler want's to use the ByteVector() in ByteVector() << something  as a rvalue (ByteVector &&).

So I added the following operator overload:

template<typename T>
ByteVector && operator << (ByteVector && bv, T data)
{
    return std::move(bv << data);
}

This overload solve the compiler error. But I would like to know if my implementation is good and optimal in terms of good practice, memory usage and efficiency.


texts:
I writing a function in Python to substitute word tokens in a sentence with IDs.

The sentence is a list of tokens (list_of_tokens).
The IDs are provided in a dictionary mapping tokens with an IDs (tokens2IDs_dict).

However, the functions I've written is not very efficient because of the for-loop. I am wondering if there is any more pythonic and efficient way to perform this mapping.

def tokens2IDs(list_of_tokens, tokens2IDs_dict):
    new_sentence = []
    for token in list_of_tokens:
        if len(token) == 0 or token == " ":
               continue
        new_sentence.append(tokens2IDs_dict.get(token, tokens2IDs_dict.get('UNK')))
    return new_sentence


texts: Simple Grid Builder for Future Maze Applications

texts:
I'm new to Clojure and decided to read the book, "Mazes for Programmers". My first task, without reading any of the book, was to think of how I would create a maze. This is my first result and I'd like good, honest and even scathing critiques:

(defrecord Cell [visited? valid-dirs open-dirs])
(defrecord Maze [cells])

(defn create-cell
      "Creates an unvisited cell with no valid directions."
      ([] (Cell. false #{} #{}))
      ([valid-dirs] (Cell. false valid-dirs #{})))

(defn create-corner-cell [corner]
      "Creates a corner cell which has two valid directions."
      (case corner
            (:top-left) (create-cell #{:east :south})
            (:top-right) (create-cell #{:west :south})
            (:bottom-left) (create-cell #{:east :north})
            (:bottom-right) (create-cell #{:west :north})))

(defn create-wall-cell [side]
      "Creates a wall cell which has three valid directions."
      (case side
            (:top) (create-cell #{:west :south :east})
            (:left) (create-cell #{:north :south :east})
            (:bottom) (create-cell #{:west :north :east})
            (:right) (create-cell #{:north :south :west})))

(defn create-unbound-cell []
      "Creates a cell that has no invalid directions."
      (create-cell #{:north :east :south :west}))

(defn visited? [cell]
      "Determines whether or not a cell has been visisted."
      (true? (:visited? cell)))

(defn corner-cell? [index size]
  "Determines whether the index is a corner cell of #{top-left top-right bottom-left bottom-right} in a grid
  of size*size dimensions. Returns both whether or not the index is a corner cell, but
  also which corner of the grid it's on."
      (assert (not= 0 size))
      (cond
            (= index 0) [true :top-left]
            (= index (dec size)) [true :top-right]
            (= index (- (* size size) size)) [true :bottom-left]
            (= index (dec (* size size))) [true :bottom-right]
            :else [false]))

(defn wall-cell? [index size]
  "Determines whether the index is a wall cell of #{top left right bottom} in a grid
   of size*size dimensions. Returns both whether or not the index is a wall cell, but
   also which side of the grid it's on."
      (assert (not= 0 size))
  (let [contains-index (partial seq-contains index)
        top-row (range 0 size)
        bottom-row (range (- (* size size) size) (* size size))
        left-col (take-while (partial > (* size size)) (iterate #(+ % size) 0))
        right-col (take-while (partial > (* size size)) (iterate #(+ % size) (dec size)))]
    (cond
      (contains-index top-row) [true :top]
      (contains-index bottom-row) [true :bottom]
      (contains-index left-col) [true :left]
      (contains-index right-col) [true :right])))

(def m-corner-cell? (memoize corner-cell?))
(def m-wall-cell? (memoize wall-cell?))

(defn create-maze-cell [index size]
      "Creates a cell depending on its location in the maze."
      (let [[is-corner-cell corner] (m-corner-cell? index size)
            [is-wall-cell side] (m-wall-cell? index size)]
            (cond
                  (true? is-corner-cell) (create-corner-cell corner)
                  (true? is-wall-cell) (create-wall-cell side)
                  :else (create-unbound-cell))))

(defn create-maze [size]
      "Create a maze of size*size dimensions."
      (if (= 1 size) (create-cell)
        (let [total-cells (* size size)]
              (map-indexed
                    (fn [idx _] (create-maze-cell idx size))
                    (range total-cells)))))


texts: Filter nested array of objects with object property

texts:
I am filtering array of objects with object properties provided by the user. The goal is to return an object property 'label' by matching the property 'value'. 'input' provided to the function can be either array of one element or many elements or not an array. I'm using lot of loops to accomplish this. Any suggestions to use less loops will be appreciated.

function filterFunc(input, key, filterData){

  var label = [];
  filterData.forEach(function(fv){
      if(fv.key == key && fv.values) {
          fv.values.forEach(function(v) {
             if(_.isArray(input)){
                 if(input.length > 1 ){
                   input.forEach(function(i){
                      if(v.value == i) {
                        label.push(v.label);
                      }
                   })
                  }else {
                   if(v.value == input) {
                      label = v.label;
                   }
               }
              } else {
                 if(v.value == input) {
                      label = v.label;
                   }
              }
             })
           }
        })
     return label.toString();
  }

filterData = [{'key':'data1','values':[{'value':'1','label':'one'}]},
               {'key':'data2','values':[{'value':'2','label':'two'}]}]

Note: Values are not always present. Input to the function is key and value. The function to should return the label for the input value.


texts: Cleaning up file resources in a DRY manner

texts:
I've written a function that generates a report, and then sends it as an attachment to an email.  I don't want to clutter the function with a lot of calls to close up the filesystem, if there was an error along the way.  What do you all think?

// attempt to generate the report, any error will be wrapped in Failure(ex)
val op1 = Try {
  DB.withConnection{ implicit c =>
    generateReport(command.programId, token.claims)(output, c)
  }
}

// use (abuse?) pattern matching to send the report if it generated succesfully
val op2 = op1 match {
  case Success(_) => Try(sendReport(Address(user.email, user.first), file, command.created))

  // if the op1 value is of type Failure(ex), carry it forward until after the temp files have been removed
  case Failure(ex) => Failure(ex)
}

// the operations have either succeeded or failed, so clean up the filesystem
cleanUp(output, file)

// throw any exception that occurred along the way
op2.get

Assume that throwing exceptions is a legitimate design choice for this function, the calling process expects exceptions to be thrown and handles them gracefully.


texts:
The code runs fine and the algorithm is much faster than insertion sort. I built a modified bucket sort around the parameters given in the HW to sort a vector of ints with 50 iterations and 40000 items.

Being new and not familiar with bucket sorts I haven't found any code examples quite like this one. So my question is about my style and possible room for optimization. How can I streamline this algorithm?

void intVectSort2(vector<int> &V, int M)
   {
     // declare size for U and V
     int uSize = ((M * 2) + 1);
     int vectSize = V.size();

     // allocate memory for U array and initialize to zeros
     int *U = new int[uSize];
     for (int i = 0; i < uSize; i++)
       U[i] = 0;

     // step through M values and increment U when V is equal to M
     for (int j = 0; j < vectSize; j++)
       {
         int val = 0;
         int count = 0;
         int element = 0;
         int iterator = M;
         val = V[j];
         while (val != iterator)
           iterator--;
         element = iterator + M;
         count = U[element];
         count++;
         U[element] = count;
       }

     //load confirmed values in order back into V
     int index = 0;
     int currentSize = 0;
     for (int k = 0; k < uSize; k++)
       {
         int value = 0;
         int counter = 0;
         value = U[k];
         if (value != 0)
           {
             while (counter < value)
             counter++;
             currentSize += counter;
             for (index; index < currentSize; index++)
               V[index] = k - M;
           }
       }

     // free dynamic memory
     delete [] U;
   }


texts:
My first delve into vimscript was writing a plugin for folding markdown lists.

I reckon there are a few idioms I'm missing here, so I would greatly appreciate any comments to improve the plugin.

I've noticed something akin to "header guards" in C family languages among popular plugins:

if exists('g:touchdown__loaded')
 finish
endif
" ... plugin body goes here ..."
let g:touchdown__loaded=1

Logic for folding markdown lists is handled in the next chunk:

function! IndentLevel(lnum)
  return indent(a:lnum) / &shiftwidth
endfunction

function! NextNonBlankLine(lnum)
  let numlines = line('$')
  let current = a:lnum + 1

  while current <= numlines
    if getline(current) =~? '\v\S'
      return current
    endif

    let current += 1
  endwhile

  return -2
endfunction

function! GetListFold(lnum)
  if getline(a:lnum) =~? '\v^\s*$'
    return '-1'
  endif

  let this_indent = IndentLevel(a:lnum)
  let next_indent = IndentLevel(NextNonBlankLine(a:lnum))

  if next_indent == this_indent
    return this_indent
  elseif next_indent < this_indent
    return this_indent
  elseif next_indent > this_indent
    return '>' . next_indent
  endif
endfunction

I've also set up a custom text for the lists, but I want to make sure my logic doesn't apply to folds that aren't markdown lists. Calling foldtext when the regex doesn't match is the closest thing I know to do:

function! GetFoldText()
  if (match(getline(v:foldstart), "[\s\t]*[-\*][\s\t]*.*") != -1)
    let nl = v:foldend - v:foldstart
    let linetext = substitute(getline(v:foldstart),"-","+",1)
    let txt =  linetext . "\t (" . nl . ' lines hidden)'
  else
    let txt = foldtext()
  endif

  return txt
endfunction

Finally I apply the fold strategy here. One thing to note is I want this to only apply to markdown files, but I'm not sure how to accomplish that idiomatically:

 setlocal foldtext=GetFoldText()
 setlocal foldmethod=expr
 setlocal foldexpr=GetListFold(v:lnum)
 setlocal fillchars=fold:\


texts: R: Optimize algorithm speed that estimates hourly temperature and relative humidity

texts:
I built an algorithm to estimate real-time temperature and humidity values based on daily values of the same variables. The algorithm works, however the speed with which it processes the information is too slow and this causes me problems when the data is of many years or decades.

I need to increase the processing speed. I attach the algorithm:

names(hrTable) <- c("date","tmin","tminnext","tmax","tdew","tdewnext", "dayIndex")

hrTable is a dataframe that contains 8 columns, the first is "date" that contains the date in format "%Y-%m-%d". The rest are numerical variables.

Ejemplo : 1995-01-20, 12, 13, 25, 15, 16, 360

CalculateHumidHoursAndHumidTemp1 <-
function(hrTable,yearLength,lat,incli,rhThreshold,verbose) {

nrowTab = nrow(hrTable);
lines2 = split(hrTable,rep(1:nrowTab,each=1));
hrResult = c("hrvalues");
HumHr = 0;
AccumHumHr = 0;
HumTm = 0;
AccumHumTm = 0;
day0 = 1;
dayn = nrowTab;

for(k in day0:dayn) {
  line2 = lines2[[k]];
  HumHr = 0;

  tempArray2 = line2;
  date2 = tempArray2[1];
  tMin = tempArray2[2];
  tMinNext = tempArray2[3]; #ENTERO PARA PODER COMPARAR EN LA TABLA
  tMax = tempArray2[4];
  tDew = tempArray2[5];
  tDewNext = tempArray2[6];
  dayIndex = tempArray2[7];

  alpha = tMax - tMin;
  t0 = tMax - 0.39*(tMax - tMinNext);
  r = tMax - t0;
  sindec = -1.0*incli*cos(6.283185307179586*((dayIndex + 10)/yearLength));
  cosdec = sqrt(abs(1.0 - (sindec * sindec)));
  b = cos(lat*(3.141592653589793/180.0))*cosdec;
  a = sin(lat*(3.141592653589793/180.0))*sindec;
  dayLength = 12.0*(1.0 + (0.6366197723675814*sin(a/b)));
  ho = 12.0 + (dayLength/2.0);
  hn = 12.0 - (dayLength/2.0);
  hp = hn + 24.0;
  beta = (tMinNext - t0)/(sqrt(abs(hp - ho)));
  hx = ho - 4.0;
  h = as.numeric(round(hn));

  if (verbose) {
    print(paste(hn,ho));
  }

  for(n in 1:24) {
    tAux1 = 0;
    tAux2 = 0;
    tAux3 = 0;
    hour = h + n;

if (hour >= hn && hour < hx) {
  tAux1 = tMin + alpha*sin(((hour - hn)/(hx - hn))*(3.141592653589793/2.0));

}

if (hour >= hx && hour <= ho) {
  tAux2 = t0 + r*sin(1.5707963267948966 + ((hour - hx)/4.0)*(3.141592653589793/2.0));

}

if (hour > ho && hour <= hp) {
  tAux3 = t0 + beta*sqrt(hour - ho);

}

tSum = tAux1 + tAux2 + tAux3;

if (verbose) {
  print(paste("Indice=",n,"\tHour", hour,"\ttSum=",tSum ,"\tTaux1=",tAux1,"\ttAux2=",tAux2,"\ntAux3=",tAux3," dayLength=",dayLength," a=",a," b=",b));
}

es = 611.0*exp(17.27*(tSum/(237.3+tSum)));

if (hour <= 24) {
  e = 611.0*exp(17.27*(tDew/(237.3+tDew)));
  hr = (e/es)*100.0;

  if (hr > rhThreshold) {
    HumHr = HumHr + 1;
    HumTm = tSum + HumTm;
  }

  if (verbose) {
    print(paste("Indice=",n,"\tHour",hour,"\te=",e,"\tes=",es));
  }
}

if (hour > 24) {
  e = 611.0*exp(17.27*(tDewNext/(237.3+tDewNext)));
  hr = (e/es)*100;

  if (hr > rhThreshold) {
    AccumHumHr = AccumHumHr + 1;
    AccumHumTm = tSum + AccumHumTm;
  }

  if (verbose) {
    print(paste("Indice=",n,"\tHour",hour,"\te=",e,"\tes=",es));

  }

}
hrResult = paste(hrResult,hr,tSum)
}

if (verbose) {
    print(paste("Horas Humedas= ",HumHr,"Temp Humedad= ",HumTm));
  }

}
return(hrResult)
}

HhrandHt <- CalculateHumidHoursAndHumidTemp1(hrTable,365,-12,12,90,T)


texts: Partition numbers as polynomial, each number in a thread

texts:
I'm beginner at multithreading programming and I wanna write a piece of my code using it. I'm trying to split different numbers into multiple parts and store them as a polynomial. I wanna do this using multithreading as each number being splitted in one thread, all simultaneously. For example, 5 separate threads split 5 different numbers.

Suppose I have a class named Polynomial. Assume that there's just two numbers that I wanna create polynomials out of them. They are initialized and stored already and I wanna do the partition task on both of them simultaneously.

Number is a class that stores numbers. n_Polynomial and m_Polynomial store results. I created a method named partitionNumbers().

Class Polynomial{
Number n,m,n_Polynomial[],m_Polynomial[],

private void partitionNumbers() {

        class Utility extends Thread {

            Number number;
            Number parts[];
            boolean threadHasDoneTask = false;

            Utility(Number number) {
                this.number = number;
            }

            Number[] getParts() {
                return this.parts;
            }

            @Override
            public void run() {
                int partNumbers = (number.getReal().getIntegerPartLength() + number.getReal().getFractionalPartLength()) / base;
                if (partNumbers <= 1) {
                    partNumbers = 1;
                } else {
                    partNumbers++;
                }
                this.parts = new Number[partNumbers];
                String temp = this.number.toString();
                int index = temp.indexOf('.');
                if (index != -1) {
                    temp = new StringBuilder(temp).delete(index, index + 1).toString();
                }
                int length = temp.length();
                for (int i = partNumbers - 1; i >= 0; i--) {
                    try {
                        parts[i] = new Number(temp.substring(length - base, length));
                        length -= base;
                    } catch (IndexOutOfBoundsException e) {
                        parts[i] = new Number(temp.substring(0, length));
                    }
                }
                threadHasDoneTask = true;
                notify();
            }
        }

        Utility thread1 = new Utility(m);
        Utility thread2 = new Utility(n);
        thread1.start();
        thread2.start();

        try {
            while (!thread1.threadHasDoneTask) {
                wait();
            }
        } catch (InterruptedException e) {
        }
        m_Polynomial = thread1.getParts();
        thread1.interrupt();
        try {
            while (!thread2.threadHasDoneTask) {
                wait();
            }
        } catch (InterruptedException e) {
        }
        n_Polynomial = thread2.getParts();
        thread2.interrupt();
    }
}

I know using wait/notify idiom is wrong here, but I just can't find any other way to tell main thread that thread1 and thread2 are done!

What are my solution problems and is it generally reliable?


texts: Customizable Blockchain's block structure

texts:
I'm creating a new open-source blockchain framework for .NET Core and .NET Standard. But I have some questions about how to make block structure to be customizable and look beautiful.

Let's begin from basic block which I created:

public class Block
{
  //header part
  public string Hash;
  public long Index;
  public long TimeStamp;
  public string PreviousHash;
  //data part
  public string Data;
}

I want the user to be able to store anything in Block, so I've created generic version of Block:

public class Block<TData>
{
  //header part
  public string Hash;
  public long Index;
  public long TimeStamp;
  public string PreviousHash;
  //data part
  public TData Data;
}

It is a good solution, but if I want the user to be able to create any good cryptocurrencies and, which is more necessary, blockchains for business, I need to support some limitations. What about hashing data? Or storing it in some file, not in a memory? Or what if the user wants to modify header parts and/or add more fields? We compute the block hash from this fields and data hash(if data is complex), so I've created interfaces and split them:

public interface IHashable
{
  string Hash { get; set;}
  byte[] GetDataForHash();
}
public interface IBlock: IHashable
{
  long Index { get; set;}
  long TimeStamp { get; set;}
  string PreviousHash { get; set;}
}
public interface IWithData<TData>
{
  TData Data { get; set;}
}
public interface IWithStoredData<TData>: IWithData<TData>
{
  string DataHash {get;set;}
  string FileName {get;set;}
}
public interface IFullBlock<TData>: IBlock, IWithData<TData> {}

And there will be generic Block class, which implements IFullBlock and basic interfaces. If the user wants to modify the data type, he can modify the type parameter.

If the user wants to modify header fields, he can create a new ICustomBlock: IBlock, and add more fields, then derive from Block class and implement the new interface:

public class Block<TData>: IFullBlock<TData>
{
  //..implementation
}
// for example
public interface IBitcoinBlock: IBlock
{
   long Nonce {get;set;}
   long Bits {get;set;}
   int Version {get;set;}
}
public class MyBitcoinBlock: Block<List<Transaction>>, IBitcoinBlock
{
   //..implementation
}

And if the user wants to use more fields in hashing the block, he just overrides the GetDataForHash method.

But is this solution good? Can it be simpler? And is there any possibility to make block deconstructable into the header part and data part ( via c# 7 Deconstruct method) ?


texts: Creating a generic filter system to pass into an API request

texts:
I am building an application that will interact with an API.
My service layer will call the repository which will request data from the API.
I want to be able to pass filters to the repository and came up with the following. I am just curious if there are better ways of doing this?

First, I created an interface called IFilter:

public interface IFilter
{
    string GetFilterName();
    string GetFilterValue();
}

I then have a bunch of filters, for example:

public class CreatedAfterFilter : IFilter
{
    readonly string _name = "CreatedAfter";
    DateTime _value;

    public CreatedAfterFilter(DateTime value)
    {
        this._value = value;
    }

    public string GetFilterValue()
    {
        return _value.ToString("yyyy-MM-ddTHH:mm:ss+00:00");
    }

    public string GetFilterName()
    {
        return _name; ;
    }
}

...or

public class OrdersFilter : IFilter
{
    readonly string _name = "OrderIdList";
    List<Order> _value;

    public OrdersFilter(List<Order> value)
    {
        this._value = value;
    }

    public string GetFilterValue()
    {
        return '[' + String.Join(",", _value.Select(x => x.OrderId)) + ']';
    }

    public string GetFilterName()
    {
        return _name; ;
    }
}

When I call my repository, I do something like this:

var filters = new List<IFilter>();
filters.Add(new CreatedAfterFilter(DateTime.UtcNow.AddDays(-110)));
filters.Add(new OrderFilter(1709359));
var orders = orderRepository.GetSingleOrder(filters);

Then in my code that calls the API, I do this to create the query string:

foreach (var filter in filters)
{
   getParams.Add(new KeyValuePair<string, string>(filter.GetFilterName(), filter.GetFilterValue()));
}

Am I on the right track here?
This is my first real time in writing C# an using Interfaces.


texts:
I am working on a rewrite project, where I'm supposed to change the
enhanced for-loop code used in java6 to the newer forEach loop.
Similarly, we have to use Streams API as heavily as possible.
Below is the complete scenario followed by code sample

Iterate over each element of List<String> list.
If Map<String, A> has each as key, take the corresponding A value from the Map.
A has an instance variable, List<B> which is a list of class B objects.
If List<B> is null initialize new List<B> and put some value inside it. Else just add a new value to Existing List<B>.

The complete code for the problem is below:
    class A{
        List<B> values;

    }

    class B{

    }

    //Assume some values
    List<String> list = ["val1","val2"];
    //Assume some values
    Map<String, A> map = { "val1" : [A object] , "val2" : [A object] };
    B b = new B();

    //Iterate over every value in list
    for(String each: list){
        //if map has a key with the value inside the list
        A a = map.get(each);

        //A has a list called "values" of B class objects. If that list is not null or empty add a new B object (b )in our case to the a List else initialze a new List and add the value there.
        if(a.values != null && !a.values.isEmpty()){
            a.values.add(b);
        }else{
            List<B> bList = new ArrayList<>();
            bList.add(b);
            a.values = bList;
        }
    }


texts: Performance of WordPress Plugin is currently very bad and needs to be improved

texts:
I have written the following Plugin, which adds fields to the Media/Attachment Entries and returns all information in one Shortcode. This solution works, however, is pretty slow the more entries available.

I know that the problem lies in the while loop. The 20000 simply is a high number in order for all posts to be searched through.
However, I included the entire code in order to see what I'm trying to achieve:

defined( 'ABSPATH' ) or die( 'Are you ok?' );
/** ----- ADD FIELDS IN MEDIA LIBRARY ----- **/
function additionalMediaFields( $form_fields, $post ) {
    $form_fields['copyright_author'] = array(
        'label' => '&copy; Autor',
        'input' => 'text',
        'value' => get_post_meta( $post->ID, 'copyright_author', true ),
    );
    $form_fields['copyright_description'] = array(
        'label' => '&copy; Beschreibung',
        'input' => 'text',
        'value' => get_post_meta( $post->ID, 'copyright_description', true ),
    );
    $form_fields['copyright_website'] = array(
        'label' => '&copy; Website',
        'input' => 'text',
        'value' => get_post_meta( $post->ID, 'copyright_website', true ),
    );
    return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'additionalMediaFields', 10, 2 );

function storeAdditionalMediaFields( $post, $attachment ) {
    if( isset( $attachment['copyright_author'] ) )
        update_post_meta( $post['ID'], 'copyright_author', $attachment['copyright_author'] );
    if( isset( $attachment['copyright_description'] ) )
        update_post_meta( $post['ID'], 'copyright_description', $attachment['copyright_description'] );
    if( isset( $attachment['copyright_website'] ) )
        update_post_meta( $post['ID'], 'copyright_website', $attachment['copyright_website'] );
    return $post;
}
add_filter( 'attachment_fields_to_save', 'storeAdditionalMediaFields', 10, 2 );

/** ----- SHOW THEM IN ONE SHORTCODE ----- **/
function listAllCopyrightInformation ($atts, $content = null) {

    $i = 1;
    while ( $i<=20000 ) {
        $copyrightAuthor = get_post_meta($i, 'copyright_author', true );
        $copyrightDescription = get_post_meta($i, 'copyright_description', true );
        $copyrightWebsite = get_post_meta($i, 'copyright_website', true );

        if ( !empty($copyrightAuthor)) {
            if( !empty($copyrightDescription)) {
                $CopyrightEntries .= $copyrightDescription . ': ';
            }
            $CopyrightEntries .= '&copy; ';
            $CopyrightEntries .= $copyrightAuthor;
            if( !empty($copyrightWebsite)) {
                $CopyrightEntries .= ' / ' . get_post_meta($i, 'copyright_website', true) . ' || ';
            }
        }
        $i++;
    }
    $CopyrightReturn = substr($CopyrightEntries, 0, -8);
    return $CopyrightReturn;
}

add_shortcode("show-all-copyrights", "listAllCopyrightInformation");

Thanks in advance. I appreciate your effort!


texts: Code review for style, clarity and efficiency. Removing duplicates from linkedlist

texts:
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

class RemoveDuplicates {
    public static void main(String[] args) {
        LinkedList<Integer> list = new LinkedList<Integer>();
        list.add(1);
        list.add(1);
        list.add(1);
        list.add(4);
        System.out.println(RemoveDuplicates.rmDuplicates(list));
    }

    public static List<Integer> rmDuplicates(List<Integer> list) {
        Collections.sort(list);
        Iterator<Integer> it = list.iterator();
        int index = 0;
        while (it.hasNext() && index < list.size() - 1) {
            int current = it.next();
            index++;
            if (current == list.get(index)) {
                it.remove();
                index--;
            }
        }
        return list;
    }
}

Update: I did not want a sorted list. Just removing duplicates is enough and I wanted it in place. I thought without sorting it would be tricky to remove duplicates.


texts:
I want to let you look through my NetHandler which is basically the component which gets notified when a full packet arrived (not like tcp-packet but my own type of packets) and handles it.

First of all, the handle method which gets called from the networking

void NetHandler::handle(int id, char* data, int len) {
#define P(ID, TYPE) case ID: try_handle<TYPE>(data, len); break
    switch (id) {
        P(0, game::networking::packets::Hello);
        P(1, game::networking::packets::Disconnect);
        P(2, game::networking::packets::Chat);
    default:
        unhandled(id);
    }
#undef P
}

Yeah... As you can see, a little define to create a case for me. unhandled just prints out an error.

Next, the try_handle<>()

template<typename T>
void try_handle(char* data, int len) {
    T packet;
    if (!packet.ParseFromArray(data, len)) {
        bad_packet(packet.GetTypeName(), len);
        return;
    }
    handle(packet);
}

It creates the actual corresponding Packet and parses it (I use google protobuf). bad_packet, again, just prints an error. Now the handle

#define STUB(TYPE) virtual void handle(TYPE& type) { unhandled(TYPE::default_instance().GetTypeName()); }
    STUB(game::networking::packets::Hello)
    STUB(game::networking::packets::Disconnect)
    STUB(game::networking::packets::Chat)
#undef STUB

I create a virtual method for every packet-type which calls unhandled. In my application I can now subclass this NetHandler and override the handler-methods for all packets I expect.

Now the other direction, send packets. I actually wanted a single send-method which takes every possible packet and just sends it. But it needs to send the id along with it, so I created a PacketRegistry

class PacketRegistry {
public:
    PacketRegistry() {
        for (int i = 0; i < 256; i++)
            messages[i] = nullptr;
        reg<game::networking::packets::Hello>(0);
        reg<game::networking::packets::Disconnect>(1);
        reg<game::networking::packets::Chat>(2);
    }

    template<typename T>
    void reg(int id) {
        messages[id] = T::default_instance().GetDescriptor();
    }

    int getId(const google::protobuf::Message& message) {
        const google::protobuf::Descriptor* ptr = message.GetDescriptor();
        for (int i = 0; i < 256; i++) {
            if (messages[i] == ptr)
                return i;
        }
        return -1;
    }

    const google::protobuf::Descriptor* messages[256];
};

As you can see, this is the third time I need to add every packet manually.

I tried to come up with some template-magic, but I'm not that experienced yet.
Something like

//in NetHandler
template<typename T>
void handle(T& packet) { unhandled(T::default_instance().GetTypeName()); }

//In a subclass
template<>
void handle<game::networking::packets::Chat>(game::networking::packets::Chat& chat) {   }

unfortunately doesn't work because... well... templates and virtual don't really like each other.

Any ideas how I can let the template-magic do more of the dirty work instead of defining methods, cases, and so on for every existing packet?


texts: Algorithm Improvement - element differences in list

texts:
The task is, you have an array of n numbers called nums, and an array of m numbers called maxes.

For each element in maxes, find the number of elements for which m is bigger than or equal to n.

nums = [4, 2, 5, 10, 8]
maxes = [3, 1, 7, 8]

output = [1, 0, 3, 4]

Eg, The first element, 3, in maxes, is only higher than 2 in nums, hence the first element in output is 1

The initial solution I could think of was trivial, and its O(n^2)

def sol_naive(nums,maxes):
    output = []
    for entry in maxes:
        higher_numbers = len([x for x in nums if x <= entry])
        output.append(higher_numbers)
    return output

Wondering if there's a better approach to solve this problem?


texts: Reading/writing null-terminated strings from socket

texts:
I need to handle C style ('\0' delimited) strings from/to a socket and came up with this as a first attempt:

#lang racket
(require racket/tcp)

(define (read-c-str inp)
  (define (read-to-zero acc)
    (define b (read-byte inp))
    (cond
      [(equal? b 0) (bytes->string/utf-8 (apply bytes (reverse acc)))]
      [else         (read-to-zero (cons b acc))]))
 (read-to-zero null))

(define (write-c-str val p)
  (write-bytes (bytes-append (string->bytes/utf-8 val) (bytes 0)) p)
  (flush-output p))

Writing is pretty simple, but I have the feeling I'm missing a more simple way to handle the accumulator there, and mainly looking for feedback on read-c-str. I'm suspicious of having to reverse, making me think there is a better way.

Here was my second attempt which I'm not convinced is a better way:

(define (blist->string blist)
  (bytes->string/utf-8 (foldr (lambda (inb l) (bytes-append l (bytes inb))) #"" blist)))

(define (read-c-str inp)
  (define (read-to-zero acc)
    (define b (read-byte inp))
    (if (equal? b 0)
        (blist->string acc)
        (read-to-zero (cons b acc))))
  (read-to-zero null))


texts:
My code is too slow, it does the desired, but not in \$O(\log n)\$ or faster.

Explanation of the auction system:

For simplicity we will assume that all orders are for a single unit of the traded item. The
double action system operates as follows. When a new buy order of $x arrives it is simply
stored for future use if none of the stored sell orders has an asking price lower than or equal
to $x. Otherwise, the new buying bid is matched with the sell order with lowest asking price
(earliest such order if there are ties). The two matched orders are removed from the system,
and the buyer receives the item in exchange for paying a clearing price equal to the matched
sell order asking price, i.e., the lowest asking price available at the time the new buy order
was received.

It's supposed to be a simple program that takes orders per line and sells or buys. The program must read from the standard input a list of buy and sell orders in the format given in the samples below, with one order per line. The program must print to the standard output the number of units exchanged and the total clearing price, using the format given in the examples below, and printing a new line at the end. Note that after processing the input there can be orders left uncleared.

Sample Input

order 1 sell at 30
order 2 buy at 20

Output for Sample Input

units exchanged 0 total clearing price 0

public static void main(String args[]) {
  // Create an array list for order to read data from the file
  ArrayList<String> orders = new ArrayList<String>();

  // Create sell and buys array list to get the list of
  // buyers and sellers
  ArrayList<String> sells = new ArrayList<String>();
  ArrayList<String> buys = new ArrayList<String>();

  // declare the required variables
  int price = 0;
  int exchange = 0;

  // since we are using files put the execution code in try..catch
  // block
  try {
      // create a file object by passing it to scanner class
      // to read the file data
      Scanner input = new Scanner(System.in);

      // read the lines from the text file
      while (input.hasNextLine()) {
           orders.add(input.nextLine());
      }

      // separate the orders
      for (int i = 0; i < orders.size(); i++) {
           System.out.println("" + orders.get(i));

           if (orders.get(i).contains("sell")) {
                sells.add(orders.get(i));
           }

           if (orders.get(i).contains("buy")) {
                buys.add(orders.get(i));
           }

      }

      // now get the price values from each line from the
      // respective orders and compare the values
      int buy = 0;
      int sell = 0;

      for (int i = 0; i < buys.size(); i++) {
           buy = getPrice(buys.get(i));
           for (int j = 0; j < sells.size(); j++) {
                sell = getPrice(sells.get(j));

                // if the values are same, then compute the clear price
                // values
                // and increment the exchange value
                if (buy == sell) {
                     price += 2 * buy;
                     exchange += 2;
                }
           }
      }

      // print the final statement.
      System.out.println("\nUnits of exchanged " + exchange + " total clearing price "+ price);
  } catch (Exception e) {
      e.printStackTrace();
  }
}

// getPrice method that retrieves the integer values from
// each line
 public static int getPrice(String s) {
  // use the scanner object to access the line values
  Scanner in = new Scanner(s).useDelimiter("[^0-9]+");

  int i = 0;
  int j = 0;

  // get the integer values
  while (in.hasNext()) {
      i = in.nextInt();
      j = in.nextInt();
  }

  // return the required price value
  return j;
}


texts: C++11 Blocking connection pool with auto release

texts:
Relatively new to C++. Please help me understand the potential issues with the following blocking object pool.

#pragma once
#include <vector>
#include <queue>
#include <memory>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <future>
#include <functional>
#include <stdexcept>

template <typename T>
struct DefaultDeleter {
    DefaultDeleter() {};
    void operator()(T* p) const {
        delete p;
    }
};

/// \brief A blocking object pool containing objects of type T.
/// Objects burrowed from the pool are automatically returned to the pool when they
/// are out of scope.
/// \n
/// When the pool is empty calls to borrow objects blocks until a resource is returned to the pool.
template <typename T, typename Deleter = DefaultDeleter<T>>
class ObjectPool {
public:

    /// \brief Construct an object pool of size n.
    ///
    /// \param n The size of the pool.
    /// \param factory A factory method that returns a pointer to a resource.
    ObjectPool(size_t n, std::function<T*()> factory) {
        for(size_t i = 0; i<n; ++i) {
            resources.emplace_back(factory());
        }
    }

    /// \brief destruct the object pool and delete all resources using the provided deleter.
    ~ObjectPool() {
        Deleter d;
        for (auto it = resources.begin(); it != resources.end(); ++it) {
            d(*it);
        }
    };

    /// \typedef Alias for a function which returns a resource to the pool.
    using ObjectReturner = std::function<void(T*)>

    /// \brief burrow an object from the pool.
    /// When the borrowed pointer is out of scope the resource is returned to the pool.
    std::unique_ptr<T, ObjectReturner> borrow() {
        std::unique_lock<std::mutex> lock(mu);
        if (resources.empty()) {
            this->condition.wait(lock, [this]{return !this->resources.empty();});
        }
        auto resource = std::unique_ptr<T, ObjectReturner>(this->resources.front(), [this](T* t){
            std::unique_lock<std::mutex> lock(mu);
            resources.emplace_back(t);
            condition.notify_one();
        });
        this->resources.pop_front();
        return std::move(resource);
    }

private:
    std::deque<T*> resources;

    // synchronization
    std::mutex mu;
    std::condition_variable condition;
};

EDIT: Maybe ObjectPool is a bad term. The intention in here is that this is a ConnectionPool. A common use case looks like this

    struct CloseDb
    {
        void operator()(sqlite3* db) const
        {
            sqlite3_close(db);
        }
    };

    struct Resource {
        std::shared_ptr<ObjectPool<sqlite3, CloseDb>> sqlitePool;
         Resource() {
                std::function<sqlite3*()> factory = [=] ()
                {
                   sqlite3* db;

                    auto rc = sqlite3_open(sqlite3File.c_str(), &db);
                    if (rc)
                     {
                        sqlite3_close(db);
                        std::string err = sqlite3_errmsg(db);
                        sqlite3_close(db);
                        throw std::runtime_error(err);
                     }
                    return db;
              };

            sqlitePool = std::make_shared<ObjectPool<sqlite3, CloseDb>>(std::thread::hardware_concurrency(), factory);
    }


texts: Generate random numbers without repetitions

texts:
I want to generate a list of N different random numbers:

public static List<int> GetRandomNumbers(int count)
{
    List<int> randomNumbers = new List<int>();

    for (int i=0; i<count; i++)
    {
        int number;

        do number = random.Next();
        while (randomNumbers.Contains(number));

        randomNumbers.Add(number);
    }

    return randomNumbers;
}

But I feel there is a better way.  This do...while loop especially makes this ugly. Any suggestions on how to improve this?


texts: My solution for Advent of Code 2020, Day 4, in C

texts:
Here is my solution for Advent of Code 2020, Day 4, Part 1  in C. I tried using fscanf or fgets, but couldn't find a nice of doing it, so I ended up using getc. Is there a way of using fscanf or fgets that makes this code better? Also, how does the code look in general?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct keyValuePair
{
    char key[4];
    char value[20];
} KeyValuePair;

typedef struct passport
{
    KeyValuePair fields[8];
    int numberOfFields;
} Passport;

int main()
{
    FILE* f = fopen("../input.txt", "r");
    if (f == NULL)
    {
        printf("Fail opening file\n");
        return -1;
    }

    Passport passports[1000];
    int currentPassport = 0;
    int currentField = 0;

    char key[4];
    char value[20];

    int c;
    while (!feof(f))
    {
        c = getc(f);
        if (c == '\n')
        {
            ++currentPassport;
            currentField = 0;
            continue;
        }
        else
        {
            // read key
            key[0] = (char)c;
            key[1] = (char)getc(f);
            key[2] = (char)getc(f);
            key[3] = '\0'; // place null terminator at the end

            // discard ':'
            c = getc(f);

            // read value
            int index = 0;
            while (c != ' ' && c != '\n')
            {
                c = getc(f);
                if (c == EOF) { break; } // at the last line, instead of a new line we have EOF
                else if (c != ' ' && c != '\n') { value[index++] = (char)c; }
            }
            value[index] = '\0';  // place null terminator at the end

            strcpy(passports[currentPassport].fields[currentField].key, key);
            strcpy(passports[currentPassport].fields[currentField].value, value);
            ++currentField;

            ++passports[currentPassport].numberOfFields;
        }
    }

    int numberOfValidPassports = 0;
    for (int i = 0; i <= currentPassport; ++i)
    {
        if (passports[i].numberOfFields == 8) { ++numberOfValidPassports; }
        else if (passports[i].numberOfFields == 7)
        {
            int cidFound = 0;
            for (int j = 0; j < passports[i].numberOfFields; ++j)
            {
                if (strcmp(passports[i].fields[j].key, "cid") == 0)
                {
                    // passport contains cid and has 7 fields, therefore invalid
                    cidFound = 1;
                    break;
                }
            }
            if (!cidFound) { ++numberOfValidPassports; }
        }
    }

    printf("Number of valid passports: %d", numberOfValidPassports);

    fclose(f);
    return 0;
}


texts: Creating a List of objects of a particular type only

texts:
I'm using an interface IloadPattern on deriving 3 classes from it LinearLoadingPattern , JointsLoadingPattern , AreaLoadingPattern. At some point, I'm creating a list all the objects created from those classes.

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

Below is the way I'm doing it.

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

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

    return jointLoads;
}


texts: Extended exception for network errors in Python

texts:
I need a custom exception for network errors that holds a related exception and more than one URL (the first one as the used service has its URL but it indirectly calls other URLs), so I wrote this:

class ExtendedNetworkError(Exception):

    def __init__(self, reason, *args, **kwargs):
        kwargs_copy = dict(kwargs)
        main_url, related_exception = self._move_kwargs(kwargs_copy)
        Exception.__init__(self, reason, **kwargs_copy)
        self.reason = reason
        self.main_url = main_url
        self.related_exception = related_exception

    def _move_kwargs(self, kwargs):
        moved = []
        for kwarg in ['main_url', 'related_exception']:
            moved.append(kwargs.get(kwarg))
            if kwarg in kwargs:
                del kwargs[kwarg]
        return tuple(moved)

    def as_dict(self):
        d = OrderedDict()
        for attr in ['reason', 'main_url', 'related_exception']:
            d[attr] = self.__dict__.get(attr)
        return d

Problems? Improvements?


texts: Removing space and ChrW from cells in VBA

texts:
This code is working when a paste is done it will force the formatting to be the same as in the destination sheet and will remove space and ChrW from the cells as soon as the paste event is detected. Is there any way to improve the code to work better?

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

Dim cell As Range

On Error Resume Next

    With Application.CommandBars("Standard").Controls("&Undo")
        If .ListCount < 1 Then Exit Sub '<--- this line creates an error if on error resume next is commented when reset filter is pressed
        If .List(1) <> "Paste" Then Exit Sub
    End With

    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Selection.Replace ChrW(160), " ", xlPart

    For Each cell In Selection
        cell.Value2 = WorksheetFunction.Trim(WorksheetFunction.Clean(cell.Value2))
    Next
    Application.CutCopyMode = False
    Application.EnableEvents = True
    Application.ScreenUpdating = True

End Sub

And this is the code for the reset filter:

Sub resetFilters()

Dim sht As Worksheet
Dim LastRow As Range

Application.ScreenUpdating = False

   On Error Resume Next
        If ActiveSheet.FilterMode Then
    ActiveSheet.ShowAllData
  End If

Range("A3:T3").ClearContents
Application.ScreenUpdating = True
Call GetLastRow

End Sub


texts:
I have a method in my rails project that initializes a new user by copying some properties of another. The method should also retain entered parameters if saving the new user is rejected. Rubocop complains that Assignment Branch Condition Size for this method is 42.02/15. How can I improve this?

def setup_user_clone
    member_id = (params[:id] || params[:member_id])
    @membership = Membership.find(member_id)
    accounts = @membership.user.accounts.map(&:id) | [@account.id]
    cloned_user = User.new @membership.user.attributes.slice('permissions')
    cloned_user.game_plan_admin = @membership.user.game_plan_admin
    if user_edit_params
      cloned_user.email = invite_email
      cloned_user.permissions = user_edit_params[:permissions]
      cloned_user.accounts = Account.find(invite_app_access.map(&:to_i))
      cloned_user.level = user_edit_params[:user_level]
    end
    profile = SettingsProfile.new @account, cloned_user
    accounts |= [*cloned_user.accounts.map(&:id).to_set]
    @user_edit = UserEdit.new current_user, profile, @membership.user.admin?, accounts, @membership.user.shared_credentials.map(&:id)
    @method = :post
  end


texts:
I'm a beginner at Lua and programming in general and I just made a simple calculator and I was wondering where I could make improvements.

print("Choose numbers:")
print("Num1:")
local num1 = io.read()
print("num2:")
local num2 = io.read()
print("Choose operator: + / - / * / /")
local operator = io.read()
if operator == "+" then
   local add = num1 + num2
   print(add)
end
if operator == "-" then
   local subtract = num1 - num2
   print(subtract)
end
if operator == "*" then
   local multiply = num1 * num2
   print(multiply)
end
if operator == "/" then
   local devide = num1 / num2
   print(devide)
end
io.read()


texts: Summer camp cost calculation using Google Sheets

texts:
I am making a sheet that adds up the total cost for a summer camp after registration. Each parent can register up to five children. I have already made my code and it works, but there must be a simpler way.

The code looks at the cell, and determines the cost of the program depending on whether the parent chooses after school, before school or both. The prices varies if it is a 4 day week or 5 day week (week 5 of 7 is a short week). The prices are called from another sheet. This process is repeated 35 times (the program is 7 weeks, plus 5 children can be registered).

Here is the code for one child:

=SUM(IF(AA2=Frais!$B$17;Frais!$C$9;IF(AA2=Frais!$B$18;Frais!$C$10;IF(AA2=Frais!$B$19;Frais!$C$11;0)));IF(AB2=Frais!$B$17;Frais!$C$9;IF(AB2=Frais!$B$18;Frais!$C$10;IF(AB2=Frais!$B$19;Frais!$C$11;0)));IF(AC2=Frais!$B$17;Frais!$C$9;IF(AC2=Frais!$B$18;Frais!$C$10;IF(AC2=Frais!$B$19;Frais!$C$11;0)));IF(AD2=Frais!$B$17;Frais!$C$9;IF(AD2=Frais!$B$18;Frais!$C$10;IF(AD2=Frais!$B$19;Frais!$C$11;0)));IF(AE2=Frais!$B$17;Frais!$D$9;IF(AE2=Frais!$B$18;Frais!$D$10;IF(AE2=Frais!$B$19;Frais!$D$11;0)));IF(AF2=Frais!$B$17;Frais!$C$9;IF(AF2=Frais!$B$18;Frais!$C$10;IF(AF2=Frais!$B$19;Frais!$C$11;0)));IF(AG2=Frais!$B$17;Frais!$C$9;IF(AG2=Frais!$B$18;Frais!$C$10;IF(AG2=Frais!$B$19;Frais!$C$11;0))))

This process is repeated 5 times.

Here is a link to the sheet (the cell with the formula is the last one).

Here are some pictures of the sheet:

The data prices are on a separate sheet:


texts:
I made the following class to simpify texture loading in LWJGL 3, and modeled it after the slick-util API:

public class Texture {

    private ByteBuffer data;
    private int id;
    private int width;
    private int height;

    private ByteBuffer resizeBuffer(ByteBuffer buffer, int size) {
        ByteBuffer newBuffer = BufferUtils.createByteBuffer(size);
        buffer.flip();
        newBuffer.put(buffer);
        return newBuffer;
    }

    private ByteBuffer imageToByteBuffer(String path, int size) throws IOException {
        ByteBuffer buffer;

        URL url = Thread.currentThread().getContextClassLoader().getResource(path);
        File file = new File(url.getFile());

        if (file.isFile()) {
            FileInputStream stream = new FileInputStream(file);
            FileChannel channel = stream.getChannel();
            buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());

            channel.close();
            stream.close();
        } else {
            buffer = BufferUtils.createByteBuffer(size);
            InputStream source = url.openStream();

            if (source == null) {
                throw new FileNotFoundException(path);
            }

            try {
                ReadableByteChannel channel = Channels.newChannel(source);

                try {
                    while (true) {
                        int bytes = channel.read(buffer);

                        if (bytes == -1) {
                            break;
                        }

                        if (buffer.remaining() == 0) {
                            buffer = resizeBuffer(buffer, buffer.capacity() * 2);
                        }
                    }

                    buffer.flip();
                } finally {
                    channel.close();
                }
            } finally {
                source.close();
            }
        }

        return buffer;
    }

    public Texture(String path) throws IOException {
        IntBuffer width = BufferUtils.createIntBuffer(1);
        IntBuffer height = BufferUtils.createIntBuffer(1);
        IntBuffer components = BufferUtils.createIntBuffer(1);
        data = STBImage.stbi_load_from_memory(imageToByteBuffer(path, 1024), width, height, components, 4);
        id = GL11.glGenTextures();

        GL11.glBindTexture(GL11.GL_TEXTURE_2D, id);
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR_MIPMAP_LINEAR);
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL14.GL_GENERATE_MIPMAP, GL11.GL_TRUE);

        this.width = width.get(0);
        this.height = height.get(0);

        GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA8, this.width, this.height, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, data);
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
        STBImage.stbi_image_free(data);
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    public int getID() {
        return id;
    }

    public ByteBuffer getByteBuffer() {
        return data;
    }

    public byte[] getByteArray() {
        return data.array();
    }

    public void bind() {
        GL11.glEnable(GL11.GL_TEXTURE_2D);
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, id);
    }

    public void release() {
        GL11.glDisable(GL11.GL_TEXTURE_2D);
    }
}

I use it as follows:

bg.bind();
GL11.glBegin(GL11.GL_QUADS);

GL11.glTexCoord2f(0F, 0F);
GL11.glVertex2f(-1F, 1F);

GL11.glTexCoord2f(1F, 0F);
GL11.glVertex2f(1F, 1F);

GL11.glTexCoord2f(1F, 1F);
GL11.glVertex2f(1F, -1F);

GL11.glTexCoord2f(0F, 1F);
GL11.glVertex2f(-1F, -1F);

GL11.glEnd();
bg.release();

where bg is a declared Texture that throws no errors.


texts: Find the largest element in a sorted array with increasing consecutive differences, smaller than x, in O( log(logmax_value)

texts:
I want to find the largest element in a sorted array, where the differences between the consecutive elements  of the array are increasing, that is smaller than x.
For example:

[1, 3, 10, 17, 50, 1000] is a valid input , because the consecutive differences (2,7,7,33,950) are in increasing order.
[1, 3, 4, 10, 18] is not a valid input, as the consecutive differences (2, 1, 6, 8) are not in increasing order.

The optimum solution is O(log(log max_value)), so I am looking for better solutions than upperbound/ binary search. Actually ,I am looking to optimize binary search from O(log N) to O(log(log max_value).
#include <bits/stdc++.h>

using namespace std;

const int n=50000001;
int arr[n];

int binarysearch_lowerbound(int begin, int end, int x){
    int ans=end;
    while(begin<end){
    int mid=(begin+end)/2;
        if(arr[mid] >= x){
            ans = mid;
            end = mid;
        } else {
            begin = mid + 1;
        }
    }return ans;
}
int main() {
    int N;
    cin>>N;
    for (int i=0; i<N; i++){
        cin>> arr[i];
    }
    int q;
    cin>>q;
    for (int i=0; i<q; i++){
        int x;
        cin>>x;
        int k;
        int begin = x/(arr[N-1]-arr[N-2]);
        int end = x/(arr[1]-arr[0]);
        if(end>N-1){
            end=N;
        }
        k=binarysearch_lowerbound(begin,end,x);
        cout<<arr[k-1]<<endl;
    }
    return 0;
}


texts: Optimize a generic foreach method that converts Datatable to my object using Reflection

texts:
I need to optimize this code block below, this method converts a Datatable object to the Object that I am passing by parameter, in another words, this serializes the object, however I have to run this block 1 million times when reading data from the database, so this is the bottleneck of my application.

  Obs: All my objects that I use in this method have a property "Id"
  which is the PrimaryKey of the table, but the name of the primary key
  in the database is different, so that's why I have that "if command"
  asking the name of the primary key.

How can I optimize this code below?

public static List<T> BindDataList<T>(DataTable dt, string primaryKey)
{
    // Get all columns' name
    var columns = (from DataColumn dc in dt.Columns select dc.ColumnName);

    var properties = typeof(T).GetProperties().ToList();

    var lst = new List<T>();

    foreach (DataRow dr in dt.Rows)
    {
        // Create object
        var ob = Activator.CreateInstance<T>();

        // Get all properties
        properties.ForEach(propertyInfo =>
        {
            if (columns.Any(s => s.Equals(propertyInfo.Name, StringComparison.OrdinalIgnoreCase)))
            {
                // Fill the data into the property
                if (!(dr[propertyInfo.Name] is DBNull))
                    propertyInfo.SetValue(ob, dr[propertyInfo.Name]);
            }
            else
            {
                if (propertyInfo.Name.ToUpper().Equals("ID"))
                {
                    // Fill the data into the property
                    if (!(dr[primaryKey] is DBNull))
                    {
                        propertyInfo.SetValue(ob, dr[primaryKey].SafeToInt());
                    }
                }
            }
        });

        lst.Add(ob);
    }
    return lst;
}


texts:
I am trying to get a signal name to print when getting a signal number.
I actually have :
char* getsig(int sig){
    switch (sig)
    {
    case SIGKILL:
        return "SIGKILL";
    case SIGSTOP:
        return "SIGSTOP";
    case SIGTERM:
        return "SIGTERM";
    case SIGTRAP:
        return "SIGTRAP";
    case SIGABRT:
        return "SIGABRT";
    case SIGALRM:
        return "SIGALARM";
    case SIGSEGV:
        return "SIGSEGV";
    case SIGQUIT:
        return "SIGQUIT";
    case SIGINT:
        return "SIGINT";
    case SIGCHLD:
        return "SIGCHLD";
    case SIGCONT:
        return "SIGCONT";
    case SIGPIPE:
        return "SIGPIPE";
    case SIGFPE:
        return "SIGFPE";
    case SIGILL:
        return "SIGILL";
    case 0:
        return "[NO SIGNAL TO DELIVER]";
    default:
        return "UNKN";
    }
}

I can see that this is wrong : I have no certitude that those char* names will be stored in data section on every compiler.
I am certain that there is a clean way of doing such a thing, but I cannot find it on the internet. Do anyone has an idea of how I could do this task the beautiful way?
Edit
The final aim is to print a potential signal delivered by a ptrace(PTRACE_CONT, ...); call, e.g:
printf("ptrace(PTRACE_CONT); [delivered %s]\n", getsig(data));