texts: Filtering and processing computers listed in a CSV file

texts:
I am trying to make my script run a lot faster. I have a csv that has about 330k of lines on it that I plan on running my script with and it has been taking an extremely long time. I just did ran the script and it 50 mins to generate the new csv. What the code does is it adds 2 more fields and filters only the administrators and The windows operating systems. Below are before and after pictures of the output.

$csv = Import-Csv 'U:\Local Group Members.csv' |
Where-Object {($_.Name0 -eq "administrators") -and ($_.caption0 -match "Microsoft Windows 10 Enterprise|Microsoft Windows 7 Enterprise|Microsoft Windows 7 Professional|Microsoft Windows 8 Enterprise|Microsoft Windows 8 Pro|Microsoft Windows 8.1 Enterprise|Microsoft Windows 8.1 Pro")} |
Select-Object "Netbios_name0", "Name0", "Account0","category0","Domain0","Unique Account Name","Type0","caption0", "Excluded"
#Modify each line
Foreach ($row in $csv) {
If ($row.Type0 -eq 'Domain') {
    $row."Unique Account Name" = "$($row.Domain0) - $($row.Account0)"
    If ($row."Unique Account Name" -in @('ACCOUNTS - DODSCAN'.'ACCOUNTS - Domain Admins','ACCOUNTS - LADM_WS_Admins','ACCOUNTS - Tech Enterprise'))
         {$row."Excluded" = "True"}
    Else {$row."Excluded" = "False"}
}
Else {
    $row."Unique Account Name" = "$($row.Netbios_name0) - $($row.Account0)"
    If ($row."Account0" -in @('esrxadm1n_esi','#Update','medco_tech','medco_admin'))
         {$row."Excluded" = "True"}
    Else {$row."Excluded" = "False"}
}
    Write-Host $row."Unique Account Name"
    Write-Host $row."Excluded"
}

#Export CSV
$csv | Export-Csv U:\$(get-date -f yyyy-MM-dd-hh-mm)-TestOutput1.csv -NoTypeInformation

Can someone help me make my script faster?


texts: Synchronization of new data with previous data

texts:
I am working with synchronization for the first time. I wrote some code that I have to implement in my current work. I wanted to know if I am doing it right or am I making a mistake.

Whenever a new list of data is downloaded, NewDataArrived() is called. This method then calls the ListData() that returns the list of previously saved data. There is another class running in parallel that uses the ListData(). What I want to do is whenever the List is being updated, the Thread class should just wait until the list is updated.

public class ListData() {

/*
*
*

*/
public List < name_holder > getList() {
    synchronized(LOCK_LIST) {
        if (newDataArrived)
            LOCK_LIST.notifyAll();

        return full_list;
    }
}

void NewDataArrived() {

    newDataArrived = true;
    parallelDownload p = new parallelDownload();
    p.start();
    getList().addAll(my_list);
    newDataArrived = false;

}

class parallelDownload extends Thread {
    private Object o = new Object();

    @Override
    public void run() {
        synchronized(o) {
            for (name_holder n: getList()) {
                while (newDataArrived) {
                    try {
                        o.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
}


texts:
I wrote this MySQL stored procedure to emulate DROP USER IF EXISTS (not available in MySQL). Any comments or identified bugs?

DROP PROCEDURE IF EXISTS DropUserIfExists;
DELIMITER $$
    CREATE PROCEDURE DropUserIfExists(MyUserName VARCHAR(100))
    BEGIN
      DECLARE foo BIGINT DEFAULT 0 ;
      SELECT COUNT(*)
      INTO foo
        FROM mysql.user
          WHERE User = MyUserName ;
       IF foo > 0 THEN
             SET @A = (SELECT Result FROM (SELECT GROUP_CONCAT("DROP USER"," ",MyUserName,"@'%'") AS Result) AS Q LIMIT 1);
             PREPARE STMT FROM @A;
             EXECUTE STMT;
             FLUSH PRIVILEGES;
       END IF;
    END ;$$
DELIMITER ;


texts: Loading site configuration from ini file

texts:
I load in static setting via an ini file. Ini file have the benefits

of NOT being server by most web servers by default.
easier to edit than XML
even simplier than JSON

Setting get loaded in Application.cfc

void function setupApplication() output="false" {
...
application.stAdminSetting  = application.loadini("admin\standard.ini");
...
}

Application.cfc has this as a function...

<cffunction name="loadini" output="false" returnType="struct">
<cfargument name="configfile" required="true" type="string" hint="reads from currentTemplate">

<cfscript>
var stResult = {};

var iniFilePath = "#GetDirectoryFromPath(GetBaseTemplatePath())##arguments.configfile#";
var stSection = getProfileSections(iniFilePath);

for(var section in stSection)   {

    var CurrentSection = evaluate("stSection.#section#");

    var stData = {};

    for(var i = 1; i <= ListLen(CurrentSection); i++)   {
        var key = ListGetAt(CurrentSection, i);

        stData[key] = getProfileString(iniFilePath, section, key);

        }

    setvariable("stResult.#section#", stData);
    }

return stResult;
</cfscript>
</cffunction>

The ini file can have any number of setting. In particular my ini file has:

[ws]
comment=http://xxxxx.com/resource/comment.cfc?wsdl
ior=http://xxxxx.com/resource/ior.cfc?wsdl
node=http://xxxxx.com/resource/node.cfc?wsdl
pref=http://xxxxx.com/resource/pref.cfc?wsdl
traffic=http://xxxxx.com/resource/traffic.cfc?wsdl

What kinds of things can I do to improve this?


texts: Copy certain lines from one text file to another

texts:
I need to open a file, read a line and if that line respect some conditions write it to another file, the line that I'm reading is a normal ASCII string representig HEX values and I need to paste it into a new file as HEX values and not as ASCII string.

What I have is this:

private void Button_Click(object sender, RoutedEventArgs e)
{
    byte[] arrayByte = { 0x00 };
    var linesToKeep = File.ReadLines(fileName).Where(l => l.Contains(":10"));
    foreach (string line in linesToKeep)
    {

        string partialA = line.Substring(9);
        string partialB = partialA.Remove(partialA.Length - 2);
        arrayByte = ToByteArray(partialB);
        using (var stream = new FileStream(fileName+"_gugggu", FileMode.OpenOrCreate))
        {
            FileInfo file = null;
            file = new FileInfo(fileName + "_gugggu");
            stream.Position = file.Length;
            stream.Write(arrayByte, 0, arrayByte.Length);
        }
    }
}
public static byte[] ToByteArray(String HexString)
{
    int NumberChars = HexString.Length;
    byte[] bytes = new byte[NumberChars / 2];
    for (int i = 0; i < NumberChars; i += 2)
    {
        bytes[i / 2] = Convert.ToByte(HexString.Substring(i, 2), 16);
    }
    return bytes;
}

This method is doing what I need but it takes ages to finish, the original files have roughly 70000 lines... Is there a better way to do that in order to increase speed?

EDIT
SOURCE FILE EXAMPLE:

:106AD00000000000000000000000000000000000B6
:106AE00000000000000000000000000000000000A6
:106AF0000000000000000000000000000000000096
:106B00000000000000000000000000000000000085
:106B10000000000000000000000000000000000075
:106B20000000000000000000000000000000000065
:106B30000000000000000000000000000000000055
:106B40000000000000000000000000000000000045
:106B50000000000000000000000000000000000035
:106B60000000000000000000000000000000000025


texts: Download NASA images to use as Windows wallpaper

texts:
I made myself a simple script (Batch/JScript) that download a pic from a blog (APOD) & set it as wallpaper.

@if (@a==@b) @end /*

Set BaseUrl=https://apod.nasa.gov/apod/
set yr=%date:~8,2%
set mn=%date:~3,2%
set dy=%date:~0,2%
set dte=%yr%%mn%%dy%
set SaveDest=C:\Users\ME\Desktop\Wallpaper\%dte%.jpg
Set Url=%baseUrl%ap%dte%.html

@echo off
setlocal
for /f "delims=" %%I in ('cscript /nologo /e:jscript "%~f0" "%Url%"') do (
    rem
    echo %%I
    set LastImg=%%I
)

set ImgUrl=%BaseUrl%%LastImg:~6%
bitsadmin.exe /transfer GETWALLPAPER %ImgUrl% %SaveDest%
timeout /t 01
reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /v Wallpaper /t REG_SZ /d %savedest% /f
timeout /t 01
for /l %%x in (1,1,80) do  (
RUNDLL32.EXE USER32.DLL,UpdatePerUserSystemParameters
reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /v Wallpaper /t REG_SZ /d %savedest% /f
timeout /t 01
)

goto :EOF

JScript */
function fetch(url) {
    var xhr=WSH.CreateObject("Microsoft.XMLHTTP");
    var dom=WSH.CreateObject('htmlfile');
    xhr.open("GET",WSH.Arguments(0),true);
    xhr.setRequestHeader('User-Agent','XMLHTTP/1.0');
    xhr.send('');
    while (xhr.readyState!=4) {WSH.Sleep(25)};
    dom.write('<meta http-equiv="x-ua-compatible" content="IE=9" />');
    dom.write(xhr.responseText);
    return dom;
}
var dom=fetch(WSH.Arguments(0));
var link=dom.getElementsByTagName('IMG');

WSH.Echo(link[0].src);

It's working. However, I am unhappy about the for /l %%x in (1,1,80) d loop: it seems not to work without the loop. Any Idea ?


texts:
public static List<Integer> storePrimes(){
        int limit = Integer.MAX_VALUE / 10;
        boolean primes[] = new boolean[limit];
        List<Integer> primeList = new ArrayList<Integer>();
        int i = 2;
        while(primeList.size() <= 10001){
            while(true){
                if(primes[i] == false){
                    primeList.add(i);
                    break;
                }
                i++;
            }

            for(int j = i; j < limit; j += i){
                primes[j] = true;
            }
            i++;
        }
        return primeList;
    }

I want to find Nth primes. I am using this function to store all N primes. Later I can easily get Nth prime from list instead of doing everytime. N here is 10000.

How could I improve this code? What is the best upperlimit of an array?


texts: Read integers from text file and write to CSV file

texts:
Background:

I've recently started learning bash due to my new job in a VFX company. We backup all of our media to LTO tapes (one master and one clone). I was tasked with writing a script that split the tape list of master and clone to a CSV file. I feel I've done so in a crude manner as this was my first ever script and would love some feedback as to how I could improve the efficiency / syntax / code in general so I can learn from this experience.

Here is the text file: https://1drv.ms/t/s!AkWewdosAYGuhiaBbehlLBF64Qsg

I've been executing the script by calling it like this:
$ sh /scriptname.sh filename.txt

Code:

#!/bin/bash
# This script will split the presstore list of tapes into a .CSV file with two seperate coloumns.
file="$1"
echo "Splitting tape list....."
touch tempsplit.csv #creates temporary file for use later in script
while IFs= read line
do
    lastchar=$(echo $line | tail -c 2)
        if [ "$lastchar" == : ]     #Ommits any lines that end with : else error
            then
                echo -ne
        elif [ "$lastchar" -ge 0 -a "$lastchar" -le 9 ]     #Selects lines that end in number
            then
                breakdown=$(echo "$line" | cut -d':' -f2,6) #selects fileds 2 & 6 containing tape numbers
                master=$(echo "$breakdown" | cut -d'a' -f1) #cuts first number
                clone=$(echo "$breakdown" | cut -d':' -f2) #cuts second  number
                final=$(echo -e "$master,$clone" | tr -d ' ' >> tempsplit.txt) #outputs to a temp csv file
        fi
done < $file
touch tapelist_split.csv
awk 'NR % 2 == 0' tempsplit.txt | sort -n >> tapelist_split.csv #removes every 2nd line, sorts numerically, converts to a .CSV file
rm -rf tempsplit.txt #removes tempfile
echo "Complete"

Summary:

I'm not sure why the while read statement doesn't work if I don't call file="$1" as in line 3 and again output to that varaible in line 20 done < $file- an explanation on this would be amazing.

I realise I shouldn't have to call my first if statement, as I only care about the numbers but if I don't I get an error: "integer expected" when I run the script - does anyone know why this might be.

The reason I create the file "tempslipt.txt" is because my code to remove every 2nd line and sort the file wasn't working within the wile read statement so I figured this was a clean way of doing it.

My code may not be very efficient or good which is why I'm asking for tips on how I can refine and correct it so in future I can write much cleaner scripts.


texts:
I tried to implement an immutable list.

public class ImmutableList<T> : IEnumerable<T>
{
    public T Head { get; set; }
    public ImmutableList<T> Tail { get; set; }

    protected ImmutableList()
    {

    }

    public static ImmutableList<T> WithItems(IEnumerable<T> items)
    {
        if (items.Count() == 1)
        {
            return new ImmutableList<T>()
            {
                Head = items.Single(),
                Tail = new EmptyList<T>()
            };
        }

        return new ImmutableList<T>()
        {
            Head = items.First(),
            Tail = WithItems(items.Skip(1))
        };
    }

    public ImmutableList<T> Add(T item)
    {
        if (this is EmptyList<T>)
        {
            return new ImmutableList<T>() {Head = item, Tail = new EmptyList<T>()};
        }

        return new ImmutableList<T>()
        {
            Head = Head,
            Tail = Tail.Add(item)
        };
    }

    public IEnumerator<T> GetEnumerator()
    {
        if (this is EmptyList<T>) yield break;
        else
        {
            yield return Head;
            foreach (var elem in Tail)
                yield return elem;
        }
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

public sealed class EmptyList<T> : ImmutableList<T>
{

}

public static class ImmutableListExtensions
{
    public static ImmutableList<T> ToImmutableList<T>(this IEnumerable<T> sequence)
    {
        return ImmutableList<T>.WithItems(sequence);
    }
}

Simple example of how to use:

private static void Main(string[] args)
{
    ImmutableList<int> list = new[] {1, 2, 3}.ToImmutableList();

    ImmutableList<int> list2 = new EmptyList<int>().Add(1).Add(2).Add(3).Add(4);

    foreach (var n in list)
    {
        Console.WriteLine(n);
    }
}

Is there a better and/or more efficient way to do this?


texts: particle swarm optimization algorithm and 2D array partitioning

texts:
if I have a large 2D array let's call it a dataset and I want to divide it into a number of 2D array of equal size that is smaller and easy to processing it(let's treat it as chunks), by using PSO algorithm I take the best position values, utilize it as an index of the dataset then putting these value in the chunks..
this code gives me a number of repeated chunks that has the same value but I want different values for each chunk from all the dataset
// partitioning the dataset
        int k = 0;//chunks counter
        double[][] ch = new double[125][14];
        while(k!=dataset.length){
        best = swarm.getBestPosition();
        ch = DatasetChunks(best, dataset);
        ChunksPrint(ch, k);

        k++;
        }
 private static double[][] DatasetChunks(double[] best, double[][] dataset) {
        dataset = LoadData();
        int row = 125, col = 14;
        double[][] ch1 = new double[row][col];
        int[] s2 = new int[best.length];
        //treat the negative and repeated value of best position
        Set<Integer> st = new HashSet<>();
        for (int i = 0; i < s2.length - 1; i++) {
            s2[i] = (int) best[i];
            if (s2[i] < 0) {
                s2[i] *= -1;
            }
        }

        for (int i = 0; i < s2.length; i++) {
            // check whether the element is
            // repeated or not
            if (!st.contains(s2[i])) {
                st.add(s2[i]);
            } else {
                // find the next greatest element
                for (int j = (int)(s2[i] + Math.random()); j < Integer.MAX_VALUE; j++) {
                    if (!st.contains(j) ) {
                        s2[i] = j;
                        st.add(j);
                        break;
                    }
                }
            }
        }
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                ch1[i][j] = dataset[s2[i]][j];
            }
        }
        return ch1;
    }


texts:
I was inspired by this Reddit /r/dailyprogrammer challenge:

  Contrary to popular belief, the tetromino pieces you are given in a game of Tetris are not randomly selected. Instead, all seven pieces are placed into a "bag." A piece is randomly removed from the bag and presented to the player until the bag is empty. When the bag is empty, it is refilled and the process is repeated for any additional pieces that are needed.

  The pieces are as follows: O, I, S, Z, L, J, T

I was just wanting some general feedback on the code regarding readability, structure, and efficiency.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;

/**
 * Created on 6/23/2016.
 *
 * Inspired by r/dailyprogrammer
 * https://www.reddit.com/r/dailyprogrammer/comments/3ofsyb/20151012_challenge_236_easy_random_bag_system/
 */

public class RandomBag {

    /**
     * This is the main method. The main acquires input from the user
     * then generates a combination of pieces in a particular order.
     * @param args Unused.
     */
    public static void main(String args[]){
        Scanner scanner = new Scanner(System.in); // Used to obtain user input.
        loop : while (true) {
            System.out.print(">>> ");
            String input = scanner.nextLine().replace(" ", "");

            // Checks for special cases.
            switch (input) {
                case "exit" : break loop;
                case "" : continue loop;
            }

            // The generator class will return a string of its results.
            System.out.println(new Generator(Integer.parseInt(input)));
        }
    }

    /**
     * Created on 6/23/2016.
     * Used to create a bag that generates a set amount of pieces in a combination
     * that will never have the same piece displayed 3 times in a row. This is
     * accomplished by placing all pieces in a list and removing them one by one.
     * Once the list has been depleted the list is renewed with the same pieces.
     */

    private static class Generator{
        private int pieceNumber; // Number of pieces that will be generated.
        private String generation = ""; // Used to store the piece generation.
        private final String[] pieces = {"O", "I", "J", "L", "S", "Z", "T"}; // Pieces used in the generation.

        /**
         * Constructor used to store the number of pieces that need to be
         * generated and execute the piece generation method.
         * @param pieceNumber Used to determine how long the generation should be.
         */
        Generator(int pieceNumber){
            this.pieceNumber = pieceNumber; // Initializes number of pieces.
            generatePieces(); // Generates piece combination.
        }

        /**
         * Used to generate the pieces in the specific order. Manipulates class instanced variables.
         */
        private void generatePieces(){
            ArrayList<String> temp = new ArrayList<>(Arrays.asList(pieces)); // Used to store all pieces.
            while(generation.length() <= pieceNumber){ // Generates string until it is the specified size.
                if(temp.isEmpty()) // Checks if list is empty.
                    temp = new ArrayList<>(Arrays.asList(pieces)); // Replaces the pieces in the list.
                Collections.shuffle(temp); // Shuffles the list
                generation += temp.get(0); // Places the first element of the list onto the string.
                temp.remove(0); // Removes the element that was added from the list.
            }
        }

        /**
         * Returns the generation constructed by the generatePieces method.
         * @return The string generated.
         */
        @Override
        public String toString(){
            return generation; // Returns piece generation once generated.
        }
    }
}


texts:
I would like to know if my current jQuery code could be improved? If yes, can you give me improvements?

$(document).ready(function($)
{
    $("#bannerPickerToggleIcon").click(function(e)
    {
        e.preventDefault();
        $("#bannerPicker").slideToggle();
        $("this").css({"display": "block"});
        $("#bannerPickerToggleIcon").toggleClass("active");
    });
    $("#bannerPicker span").click(function()
    {
        var bannerid = $(this).attr("id");
        $.cookie('bannerimg', bannerid,
        {
            expires: 365,
            path: '/'
        });
        $("body").removeClass().addClass(bannerid);
    });
    if (($.cookie('bannerimg') !== null))
    {
        $("body").addClass($.cookie('bannerimg'));
    }
    else
    {
        $("body").addClass("bannerDefaultImage");
    }
});
</script>

The code allows to open a selection of banners allowing the user to select a banner from the selection. Once a user has selected a banner, this one is saved via a cookie to save the selection. I created a cookie js file. It's the default cookie file from jQuery. Currently the code works, but I would like to know if it could be improved.


texts: Adding groups to ListView depending on the data in a list

texts:
I have written simple code which loops through a list object and create groups in ListView depending on the data in the list. I have a list object of type List<Assignment> where Assignment is a class as follows.

public class Assignment
{
    public TimeSpan Time { get; set; }
    public string Type { get; set; }
    public string Description { get; set; }
    public int ImageIndex { get; set; }
}

Now, the list object have several assignments with different TimeSpan. My job is to group together all the Assignments with similar TimeSpan into one group and add this group to the ListView. The following code works perfectly fine for me. Still, I would love to know if there is better way of doing this.

The list passed into the function is sorted by TimeSpan.

private void FeedDataToPane(List<Assignment> assignmentList)
{
    ListViewGroup _listViewGrp;
    ListViewItem _listViewItem;

    MonitorPane.BeginUpdate();
    MonitorPane.Groups.Clear();
    MonitorPane.SmallImageList = imageList;

    for (int i = 0; i < assignmentList.Count; i++)
    {
        if (i == 0)
        {
            _listViewGrp = new ListViewGroup(assignmentList[i].Time.ToString());
            MonitorPane.Groups.Add(_listViewGrp);

            _listViewItem = new ListViewItem(assignmentList[i].Type, assignmentList[i].ImageIndex, _listViewGrp);
            _listViewItem.SubItems.Add(assignmentList[i].Description);
            MonitorPane.Items.Add(_listViewItem);
        }
        else
        {
            if (assignmentList[i].Time.Equals(assignmentList[i - 1].Time))
            {
                _listViewItem = new ListViewItem(assignmentList[i].Type, assignmentList[i].ImageIndex, _listViewGrp);
                _listViewItem.SubItems.Add(assignmentList[i].Description);
                MonitorPane.Items.Add(_listViewItem);
            }
            else
            {
                _listViewGrp = new ListViewGroup(assignmentList[i].Time.ToString());
                MonitorPane.Groups.Add(_listViewGrp);

                _listViewItem = new ListViewItem(assignmentList[i].Type, assignmentList[i].ImageIndex, _listViewGrp);
                _listViewItem.SubItems.Add(assignmentList[i].Description);
                MonitorPane.Items.Add(_listViewItem);
            }
        }
    }

    MonitorPane.EndUpdate();
}


texts:
I am reading lot of articles regarding memory leaks. But i am not sure about below code. I usually do coding in this way. Can anyone tell me, will they leak memory?

I have doubts in following 3 scenarios (marked them in code).

I got these suspects after reading this post

var orderDetails = $("<root/>");

function GetOrderDetais()
{
    $.ajax(
    {
     url: url,
     dataType: 'xml',
     data: someThing,
     dataType: 'text',
     type: 'POST',
     contentType: "text/xml; charset=utf-8",
     success:function(value) //1 - closure function refrencing global variable
     {
        orderDetails = $(value);

        orderDetails.find("header").each(function() //2 - closure function refrencing global variable
        {
            //loop through order header details and do some DOM appends here.
        });

        ShowOrderDetails();
     },
     error:function(jqXHR, textStatus, errorThrown)
     {
        alert("Error" + textStatus + " ==> " + errorThrown);
     },
    });

}

function ShowOrderDetails()
{
    var orderNum = orderDetails.find("header oNum").text();

    $(orderDetails).find("item").each(function() //3 - closure function refrencing outer variable
    {
        //loop through item details and do some DOM appends here to form table.

        //one intresting statement.
        $("div").append($(this).find("itemNum").text()  + " - " + orderNum);
    });
}

  Update:

  See this Video to learn about memory
  leake.


texts: Speed performance of picking unique value from binary matrix file

texts:
I wrote this code to read a square matrix saved as binary file, that it can be int, uint, float etc... to pick a value from the matrix at given row y and given column x.
Could this code be faster to pick this value, better than 20 seconds.
The maximum row and column number in matrix is 3601 for each one.

import struct

#convert x,y indexes of 2D array into index i of 1D array
#x: index for cols in the 2D
#y: index for rows in the 2D
def to1D(y,x,width):
    i = x + width*y
    return i

def readBinary_as(filename,x,y,file_type,width=3601):
    with open(filename,'rb') as file_data:

        #initialize, how many bytes to be read
        nbByte = 0
        #data type of the file, uint, int, float, etc...
        coding = ''
        if file_type == "signed int":
            nbByte = 2
            coding = '>h'  #2B Signed Int - BE

        if file_type == "unsigned int":
            nbByte = 2
            coding = '>H' #2B Unsigned Int - BE

        if file_type == "unsigned byte":
            nbByte = 1
            coding = '>B' #1B Unsigned Byte - BE

        if file_type == "float":
            nbByte = 4
            coding = '>f' #4B float32 - BE

        #index of my value in 1D array
        i = to1D(y,x,width)
        for each_cell in range(0,i):
            file_data.read(nbByte)

        #read and save the picked value
        my_value_pos = file_data.read(nbByte)
        val = struct.unpack(coding,my_value_pos)[0]
    return val


texts: Entity Framework C# updating an entity list generic solution

texts:
I have an entity which has a list of other entities in it.

for example:

class Library
{
   public virtual List<Book> Books{get;set;}
}

And I would like to update the entity and also the list of entities inside.

I came up with this solution:

private void UpdateChild<T>(DbSet<T> dbset, ICollection<T> existingItems, ICollection<T> newItems, Func<T, T, bool> isEqual) where T : class
{
    dbset.RemoveRange(existingItems);

    foreach (var newItem in newItems)
    {
        T oldItem = existingItems.FirstOrDefault(x => isEqual(newItem, x));

        if (null != oldItem)
        {
            oldItem = newItem;

            dbset.Update(oldItem);
        }
        else
        {
            dbset.Add(newItem);
        }
    }
}

But I am not sure if this solution is a good approach.


texts: Making Math vector implementation using java

texts:
Below is the Code.

package Calculation;
public class Vector
{
    double x;
    double y;
    double z;
   /**Constructor to create a null vector i.e. 0i+0j+0k*/
    public Vector()
    {
        this.x=0;
        this.y=0;
        this.z=0;
    }
    /**Constructor to create a vector with the given values of i,j and k*/
    public Vector(double x,double y,double z)
    {
        this.x=x;
        this.y=y;
        this.z=z;
    }
    /**Return the string notation of the Vector */
    public String getCompleteStringNotation()
    {
        String not=this.x+"i^+"+this.y+"j^+"+this.z+"k^";
        return not;
    }
 /**Return the string notation of the Vector.Reduced it if any of x,y and z are integers */
    public String getReducedStringNotation()
    {
        String not="";
        if(this.x!=0)
        {
            if(this.x==(int)this.x)
                not=not+(int)this.x+"i^+";
            else
                not=not+this.x+"i^+";
        }
        if(this.y!=0)
        {
            if(this.y==(int)this.y)
                not=not+(int)this.y+"j^+";
            else
                not=not+this.y+"j^+";
        }

        if(this.z!=0)
        {
            if(this.z==(int)this.z)
                not=not+(int)this.z+"k^";
            else
                not=not+this.z+"k^";
        }

            if(not.equals(""))
            return null;

            if(not.charAt(not.length()-1)=='+')
            not=not+"\b";

        return not;
    }
  /**Gives the length of the vector*/
    public double modulus()
    {
        double mod=Math.sqrt(Math.hypot(x,Math.hypot(y,z)));
        return mod;
    }
/**Check if two vectors are parallel or not*/
    public boolean isParallel(Vector v)
    {
        double xr=this.x/v.x;
        double yr=this.y/v.y;
        double zr=this.z/v.z;
        if(xr==yr&&yr==zr&&zr==xr)
            return true;
        else
            return false;
    }
  /**Returns the unit vector in the direction of the Calling vector object*/
    public Vector getUnitVector()
    {
        double mod=this.modulus();
        Vector unit=new Vector(this.x/mod,this.y/mod,this.z/mod);
        return unit;
    }
   /**Computes and return the dot product between two vector*/
    public double dotProduct(Vector v)
    {
        double res=this.x*v.x+this.y*v.y+this.z*v.z;
        return res;
    }
  /**Compute and returns the angle between two vectors in radians*/
    public double angleBetween(Vector v)
    {
        double dotPro=this.dotProduct(v);
        double modObj=this.modulus();
        double modV=v.modulus();
        double angle=Math.acos(dotPro/(modObj*modV));
        return angle;
    }
   /**Computes and returns the cross product of the two vector*/
    public Vector crossProduct(Vector v)
    {
        double resx=(this.y*v.z)-(v.y*this.z);
        double resy=-((this.x*v.z)-(v.x*this.z));
        double resz=(this.x*v.y)-(v.x*this.y);
        Vector res=new Vector(resx,resy,resz);
        return res;
    }

}

And now some questions.

  1. In my stringNotation methods i could not write i cap.Mean to say i(and at the top of it ^) similarly with j and k.

  2. How to implement hashcode and equals function.

  3. Any other guide will also be entertained.


texts:
I have a simple localStorage cache class written in TypeScript, would love to have some insights on any way/aspect you think it can be improved (making it more readable, maintainable, usable, etc.):
export interface CacheItem<T> {
  data: T;
  expiration: number;
}

class SimpleLocalStorageCache<T> {
  constructor(private key: string, private durationInSeconds: number) {}

  get(): CacheItem<T> | null {
    const cache = localStorage.getItem(this.key);

    if (!cache) {
      return null;
    }

    const parsedCache = JSON.parse(cache);

    if (Date.now() >= parsedCache.expiration) {
      return null;
    }

    return parsedCache;
  }

  update(data: T): void {
    const durationInMilliseconds = this.durationInSeconds * 1000;

    localStorage.setItem(
      this.key,
      JSON.stringify({
        data,
        expiration: Date.now() + durationInMilliseconds,
      })
    );
  }
}

export default CacheSimpleLocalStorageCache;

Really appreciate any feedback on how to improve.


texts:
Is there a way in Python 2.7 to improve reading/writing speed (or memory consumption of the file) compared to this version?

import gzip
import cPickle
import io

# save zipped and pickled file
def save_zipped_pickle(obj, filename):
    # disable garbage collector (hack for faster reading/writing)
    gc.disable()
    with gzip.open(filename, 'wb') as f:
        cPickle.dump(obj, io.BufferedWriter(f), -1)
        # enable garbage collector again
        gc.enable()

# load zipped and pickled file
def load_zipped_pickle(filename):
    # disable garbage collector (hack for faster reading/writing)
    gc.disable()
    with gzip.open(filename, 'rb') as f:
        loaded_object = cPickle.load(io.BufferedReader(f))
        # enable garbage collector again
        gc.enable()
        return loaded_object


texts:
I am currently done with this little Rock, Paper, Scissors, Lizard, Spock game in JavaScript. The game is working fine, but I am not pleased with the code. Is there a way to refactor these if statements? I was thinking about ternary operators and I tried doing so but failed. This is basically my function based on which I get the results.

function result(userChoice, cpuChoice) {
    var result = '';

     if(userChoice == 'rock' ) {
        if (cpuChoice == 'rock') {
            result = 'Tie';
            ties++;
        } else if ( cpuChoice == 'spock') {
            result = 'Spock vaporizes rock';
            loses++;
        } else if ( cpuChoice == 'lizard' ) {
            result = 'Rock crushes lizard';
            wins++;
        }  else if ( cpuChoice == 'paper' ) {
            result = 'Paper covers rock';
            loses++;
        }  else if ( cpuChoice == 'scissors' ) {
            result = 'Rock crushes scissors';
            wins++;
        };
    } else if(userChoice == 'paper') {
        if (cpuChoice == 'paper') {
            result = 'Tie';
            ties++;
        } else if ( cpuChoice == 'spock') {
            result = 'Paper disproves Spock';
            wins++;
        } else if ( cpuChoice == 'lizard' ) {
            result = 'Lizard eats paper';
            loses++;
        }  else if ( cpuChoice == 'rock' ) {
            result = 'Paper covers rock';
            wins++;
        }  else if ( cpuChoice == 'scissors' ) {
            result = 'Scissors cuts paper';
            loses++;
        };
    } else if(userChoice == 'scissors') {
        if (cpuChoice == 'scissors') {
            result = 'Tie';
            ties++;
        } else if ( cpuChoice == 'spock') {
            result = 'Spock distroys scissors';
            loses++;
        } else if ( cpuChoice == 'lizard' ) {
            result = 'Scissors beheads lizard';
            wins++;
        }  else if ( cpuChoice == 'rock' ) {
            result = 'Rock crushes scissors';
            loses++;
        }  else if ( cpuChoice == 'paper' ) {
            result = 'Scissors cuts paper';
            wins++;
        };
    } else if(userChoice == 'lizard') {
        if (cpuChoice == 'lizard') {
            result = 'Tie';
            ties++;
        } else if ( cpuChoice == 'spock') {
            result = 'Lizard poisons Spock';
            wins++;
        } else if ( cpuChoice == 'scissors' ) {
            result = 'Scissors beheads lizard' ;
            loses++;
        }  else if ( cpuChoice == 'rock' ) {
            result = 'Rock crushes lizard';
            loses++;
        }  else if ( cpuChoice == 'paper' ) {
            result = 'Lizard eats paper';
            wins++;
        };
    } else if(userChoice == 'spock') {
        if (cpuChoice == 'spock') {
            result = 'Tie';
            ties++;
        } else if ( cpuChoice == 'lizard') {
            result = 'Lizard poisons Spock';
            loses++;
        } else if ( cpuChoice == 'scissors' ) {
            result = 'Spock distroys scissors';
            wins++;
        }  else if ( cpuChoice == 'rock' ) {
            result = 'Spock vaporizes rock';
            wins++;
        }  else if ( cpuChoice == 'paper' ) {
            result = 'Paper disproves Spock';
            loses++;
        };
    } else {
        return false;
    };

    return result;
};


texts:
I'm trying to do a simple implementation of a scp wrapper in Elixir.

The code is working as intended, but I got a feeling that the error conditions can be simplified. Besides error handling, I don't the best way to append the -r flag if the source_path is a directory with a cond keyword.

defmodule Scp do
  def scp(user, ip, source_path, destination_path, timeout \\ 30) do
    scp_flags = [
      "-oStrictHostKeyChecking=no",
      "-oConnectTimeout=#{timeout}",
      "-oPasswordAuthentication=no",
      source_path,
      "#{user}@#{ip}:#{destination_path}"
    ]

    scp_flags =
      case File.dir?(source_path) do
        true -> ["-r" | scp_flags]
        _ -> scp_flags
      end

    if File.exists?(source_path) do
      case System.cmd("scp", scp_flags, stderr_to_stdout: true) do
        {resp, 0} ->
          {:ok, resp, 0}

        {resp, exit_code} ->
          cond do
            String.contains?(resp, "Permission denied") ->
              {:error, :permdenied, exit_code}

            String.contains?(resp, "Connection timed out") ->
              {:error, :conntimeout, exit_code}

            true ->
              {:error, :errunkown, exit_code}
          end
      end
    else
      {:error, :enoent, -1}
    end
  end
end


texts: Simple Python Console Game With Inefficient Code Flow

texts:
I made a simple program as an exercise in improving my code flow in Python, which does the following:

asks user to input their name
presents them with a choice to either print their name in reverse, or print the number of characters
after the output is printed, the user is asked if they want to "play again".  if not, exits

Have I done anything especially goofy?  Any low hanging fruit?  Best practice I'm unaware of?
from sys import exit

def userinput():
    print('Enter your name:')
    n = input()
    print('Hello, ' + n + ', would you rather (R)everse or (C)ount the letters of your name?')
    a = input()
    return (a,n)

def choosef(choice,myname):
    while choice not in ['R','C']:
        print("Only 'R' or 'C' are valid. Try again:")
        choice = input()
    if choice == 'R':
        print(''.join(reversed(myname)))
    else:
        spaces = 0
        for c in list(myname):
            if c == ' ':
                spaces = spaces + 1
        print('Total # Characters: ' + str(len(myname) - spaces))
    playagain()

def playagain():
    print("Would you like to play again, (Y)es or any other character to exit?")
    yn = input()
    if yn == 'Y':
        ui = userinput()
        choosef(ui[0],ui[1])
    else:
        exit()

ui = userinput()
choosef(ui[0],ui[1])


texts: Managing invalid cookie when 2 users use the same computer and therefore share the same cookie

texts:
I have asked this question on stackoverflow... here, I am asking for some feedback on the solution that I have implemented.

I am using ASP.NET MVC default template to logout user:
// POST: /Account/LogOff
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LogOff()
{
    AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
    return RedirectToAction("index", "home");
}

The above code is part of MVC web template, I have not changed anything. There is a edge case scenario that I get the following error:

The provided anti-forgery token was meant for a different claims-based
user than the current user

How to reproduce the error:
User1 and User2 are both using the same computer, they both use Chrome (i.e. share the cookie). User1 logs in to my-website.com and does not log out. 10 mins later, Users2 uses the same computer, opens a new chrome tab and logs in to my-website.com. This would invalidate User1's cookie... 10 min later User1 comes back to the computer and opens the original tab... User1 is unaware that his cookie is no longer valid and the website still shows him as the logged in user (if he refreshes the page, he would notice that User2 is logged in, but if he does not refresh the tab, it appears that he is still logged in). Now when User1 clicks logout he gets the above error, because his AntiForgeryToken is no longer valid... and he keeps getting the error until he refreshes the page.
This is edge case scenario but I have got this error several times, because I have 2 different users using the same computer.
My solution
// POST: /Account/LogOff
[HttpPost]
// [ValidateAntiForgeryToken] <-- removed
public ActionResult LogOff()
{
    AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
    return RedirectToAction("index", "home");
}

I have decided to remove the validation of anitforgery token from this action method... this way, user can always logoff... I think the worst thing an attacker can do is to logoff a user and I think that should be OK...
Other Issues
Given the same scenario, when User1 comes back to the website, unaware that his token is invalidated, he may perform any other post action and he would get the same Invalid AntiForgery error...


texts:
I'm coding a very simple reverse shell in python, and I want to encrypt the communication between the server and the client. The idea is to exchange an AES key using RSA then use it to encrypt everything sent on the network.

Right now I'm trying to use PyCrypto's Crypto.Cipher.PKCS1_v1_5 module for RSA and it seems to work, but since I'm no expert in cryptography (I know the maths behind RSA but thats about it) I have no idea whether this is secure or not. The test code:

from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5

msg = 'attack at dawn'
key = RSA.generate(4096)    # I know this is a huge overkill
pubkey = RSA.importKey(key.publickey().exportKey('DER'))
privkey = RSA.importKey(key.exportKey('DER'))
cipher = PKCS1_v1_5.new(pubkey)
ciphertext = cipher.encrypt(msg)
print ciphertext

dcipher = PKCS1_v1_5.new(privkey)
secret = dcipher.decrypt(ciphertext, 'thisIsForVerificationIfIAmRight')
print secret

The code works as expected. My questions:

Is this a correct way to use this module?
Is this secure?


texts:
I'm trying to generate the sum of a collection of strings by calcuating each character's byte value into a sum:

$sum = 0;

foreach( $array as $item ) {
    $bytes = unpack( 'C*', $item );

    for( $i = 1; $i <= count( $bytes ); $i++ ) {
        if( isset( $bytes[$i+1] ) ) {
            $sum += $bytes[$i] - $bytes[$i+1];
        } else {
            $sum -= $bytes[$i];
        }
    }
}

return $sum;

The goal here is to compare past sums with newly generated sums (that is to say, check if there has been a new addition to the collection, suppose Item4) and perform actions if yes.

As such, it's very important that:

The algorithm can compute the sum irrespective of the order of the items.
The algorithm doesn't get confused by a case where let's say Item3 now becomes temI3 (and therefore the sum value is still the same, even though it's clearly not the same Item).

The whole secondary loop is to check against exactly that: loop through each byte  (character) from each Item$i and to the final summ, add the differene between the first and second bytes. If there isn't a next one and we are at the end of the string, simply subtract if from the whole sum.

As such, the following:

Input(s): ['Item1', 'Item2', 'Item3'] / ['Item1', 'Item2', 'Item3'] , output: the same int. Where as ['Item1', 'Item2', 'Ite3m'] outputs a different int.

The performance as of now is as follows:

100000 items across 100 runs: 0.18s / 1000000 items across 100 runs: 2.05s

And although I understand that to parse and do these calculations for a million items in 2s is rather fast for PHP, I still think that, if you look at what the thing does, it's still slow.

Any way to speed this up?


texts:
I am a relatively new programmer and am currently working on something with the egit GitHub client library which requires me to iterate over a bunch of values and if a condition is met, add a bunch of stuff to a HashMap then add it to a List.

Example:

List<Map<String, Object>> files = new ArrayList<>();

for (TreeEntry file : fileList) {
  if ("blob".equals(file.getType())) {
    files.add(new HashMap<String, Object>() {
      {
        put("sha", file.getSha());
        put("path", file.getPath());
        put("name", GitHubHelper.getNameFromPath(file.getPath()));
        put("size", Objects.toString(file.getSize()));
        put("commits", GitHubHelper.getCommits(config, repo, file.getPath()));
      }
    });
  }
}

As far as I understand it, this is almost the same as

Map<String, Object> map = new HashMap<>();
map.put(key, value)

with the only exception that the class is anonymous, which in this case doesn't bother me since I'm adding it to a list and returning it anyway.

Questions:

Is the Double-Brace Initialization example different in performance and, in this case in particular, is there any reason why I might choose to do it the regular way? As far as I understand DBI, this should be about the same in terms of performance or am I wrong?
Same as Question #1, however, what if in addition to all of this, in this end, I want to return two things as opposed to one. Does that change anything? In my case, I also want to return the repository object these files belong to.

return new HashMap<Repository, List<Map<String, Object>>>() {
    {
        put(repo, files);
    }
};

To me, the code looks kind of nice like this. But it kind of feels as if this isn't really the right way. Is there any way I can do this better (tidier/more efficient performance-wise)?


texts:
I wanted to study c++ so i took up algorithms.I have implemented a Binary search tree with insert,delete,inorder and minimum element functions.I would like to have some feedback and some pointers to do things better.Anything is appreciated.

code:

#include <iostream>
using namespace std;

//This represents my tree node
struct node{
    int value;
    node *left,*right;
};

//minimum value of bst
node* minValue(node* tnode){
    if(tnode->left!=NULL){
        return minValue(tnode->left);
    }
    else{
        return tnode;
    }

}

//Inorder traversal
void inorder(node *tnode){
    if(tnode->left!=NULL){
        inorder(tnode->left);
    }
    cout<<tnode->value;
    if(tnode->right!=NULL){
        inorder(tnode->right);
    }

}

//insert function
node* insert(int val,node *tnode){
    node *temp=tnode;
    if(temp==NULL){
        temp= new node;
        temp->value=val;
        temp->left=NULL;
        temp->right=NULL;
    }
    else if(val<temp->value){
        temp->left=insert(val,temp->left);
    }
    else if(val>temp->value){
        temp->right=insert(val,temp->right);
    }

    return temp;
}

//delete function
node* deleteNode(node* tnode,int val){

    if(tnode==NULL)
        return tnode;
    if(tnode->value>val){
        tnode->left=deleteNode(tnode->left,val);
    }
    else if(tnode->value<val){
        tnode->right=deleteNode(tnode->right,val);
    }
    else{
        if(tnode->left==NULL){
            node* temp=tnode->right;
            delete tnode;
            return temp;
        }
        else if(tnode->right==NULL){
            node* temp=tnode->left;
            delete tnode;
            return temp;
        }
        node* temp=minValue(tnode->right);
        tnode->value=temp->value;
        tnode->right=deleteNode(tnode->right,temp->value);
    }
    return tnode;

}
int main(){
cout<<"Binary Tree";
node *ROOT=NULL;
ROOT=insert(3,NULL);
insert(2,ROOT);
insert(5,ROOT);
insert(4,ROOT);
insert(1,ROOT);
insert(7,ROOT);
insert(8,ROOT);
deleteNode(ROOT,3);
inorder(ROOT);
return 0;
}


texts:
I have tried to simulate a cart (only partially completed). I just wanted to see how the code upto this point can be enhanced. I am trying to learn about the usage of directives.

Here I have used 2 directives:

msgpallette- to show the messages when something is added to the cart
item-container - to show each item in menu

A demo of the same is provided at plunkr

Do tell me whether I have used directives properly. If not, do suggest code changes with explanations.

HTML

    <msgpallete msg="message" ng-show='show'>{{message}}
    </msgpallete>

    <item-container
        startcounter = 1
        resetter     = 'reset'
        name         = {{item.name}} >
        {{item.name}}
    </item-container>

    <order-container >
        {{order.name}}
    </order-container>

JS

angular.module('myApp',[])
.controller('MainCtrl', function($scope, $timeout) {
  $scope.items = [
  {'name':'kabab'},
  {'name':'noodles'},
  {'name':'chicken'},
  {'name':'egg'}
  ]
  $scope.resettrigger = function () {
  $scope.reset = true;
  $timeout(function() {
  $scope.reset = false;
},100)
}
  $scope.show=0;
  $scope.addMsg = function (qty,item) {
  $timeout.cancel($scope.promise);
  $scope.show = true;
  msg = "Added "+qty+" "+item+" to the cart";
  $scope.message = msg;
  $scope.promise = $timeout(function() {
  $scope.show = false;
},3000)

}})
.directive('msgpallete',function(){
 return{

restrict:"E",
transclude:true,
scope:{},
template:"

"
}
})

.directive('itemContainer',function(){
return {
controller: function() {return {}},
    restrict:'E',
    scope:{

  resetter:"="
    },
transclude:true,
    link:function(scope,elem,attr){

        scope.qty = attr.startcounter
        scope.add = function(){

            scope.qty++;
        }
        scope.remove = function(){
            scope.qty--;
        }
        scope.reset = function(){
    console.log("attr.item:"+attr.name);
     scope.$parent.addOrder(attr.name)
    scope.$parent.addMsg(scope.qty,attr.name)
    console.log("value when submitted:" + scope.qty + "name:"+ attr.name);
            scope.qty = attr.startcounter;
            scope.$parent.resettrigger();
        }

        scope.$watch(function(attr){
            return attr.resetter
        },
        function(newValue){
            if(newValue === true){
                scope.qty = attr.startcounter;
            }
        });

    },
    template:"<button ng-click='reset();'>Add to cart</button>&nbsp&nbsp"+
         "<button ng-click='remove();' >-</button>&nbsp"+
                 "{{qty}}&nbsp" +
                 "<button ng-click='add();'>+</button>&nbsp&nbsp"+
         " "

    }
});


texts: Insert Model object effectively using entity framwork

texts:
I have a WPF MVVM Application.  I have a Customer Model which is generated by an entity framework (Database first approach) and one CustomerViewModel where I created an instance of a Customer Model and used Model members inside the ViewModel property get and set.

Here is my Customer Model:

public partial class TblCustomer
{
    public TblCustomer()
    {
        this.TblSalesInvoices = new HashSet<TblSalesInvoice>();
        this.TblSalesOrders = new HashSet<TblSalesOrder>();
    }

    public int CustomerId { get; set; }  //Primary key Identity
    public string CustomerName { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Country { get; set; }
    public string Phone { get; set; }
    public string Email { get; set; }

    public virtual TblUser TblUser { get; set; }
    public virtual ICollection<TblSalesInvoice> TblSalesInvoices { get; set; }
    public virtual ICollection<TblSalesOrder> TblSalesOrders { get; set; }
}

Here is my CustomerViewModel:

    private TblCustomer_customerModel;
    public TblCustomer CustomerModel
    {
        get
        {
            return _customerModel?? (_customerModel= new TblCustomer());
        }
        set { _customerModel= value; }
    }

    #region Bindable Properties

    public string CustomerName
    {
        get
        {
            return CustomerModel.CustomerName;
        }
        set
        {
            CustomerModel.CustomerName= value;
            RaisePropertyChanged("CustomerName");
        }
    }

    public string Email
    {
        get
        {
            return CustomerModel.Email;
        }
        set
        {
            CustomerModel.Email = value;
            RaisePropertyChanged("Email");
        }
    }

    private ICommand _saveCommand;
    public ICommand SaveCommand
    {
        get
        {
            if (_saveCommand == null)
            {
                _saveCommand = new RelayCommand(() => Save());
            }
            return _saveCommand;
        }
    }
   #Endregion Bindable Properties

    private void SaveCustomer()
    {
        using(var context=new R_MaizeEntities())
        {
            context.TblCustomers.Add(CustomerModel);
            context.SaveChanges();
        }
    }

Is this a correct way create a Model class instance in ViewModel class and set and get model class members inside the ViewModel Property get and set?  By doing so, does the ViewModel class have a dependency on the Model class?  Is there any other efficient way to handle this situation?


texts: Counting ambiguous Morse Code encodings, with and without Java Streams

texts:
Unique Morse Code Words

  International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: "a" maps to ".-", "b" maps to "-...", "c" maps to "-.-.", and so on.

  For convenience, the full table for the 26 letters of the English alphabet is given below:

[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]

  Now, given a list of words, each word can be written as a concatenation of the Morse code of each letter. For example, "cab" can be written as "-.-.-....-", (which is the concatenation "-.-." + "-..." + ".-"). We'll call such a concatenation, the transformation of a word.

  Return the number of different transformations among all words we have.

  Example:

  Input: words = ["gin", "zen", "gig", "msg"]
  Output: 2
  Explanation:
  The transformation of each word is:

  "gin" → "--...-."
  "zen" → "--...-."
  "gig" → "--...--."
  "msg" → "--...--."

  There are 2 different transformations, "--...-." and "--...--.".

  Note:

  The length of words will be at most 100.
  Each words[i] will have length in range [1, 12].
  words[i] will only consist of lowercase letters.

My approach:

import java.util.*;

    class Solution {
        public int uniqueMorseRepresentations(String[] words) {

            HashMap<String,String> morseMap = new HashMap<>(26);
                       String [] morseWords = new String[] {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};

        HashSet<String> uniqCode = new HashSet<>();
        String morse = "";
        int ind;

        for( int i = 0; i < words.length; i++ )
        {
            morse = "";
            for( int j = 0; j < words[i].length(); j++ )
            {
                ind = ((int)words[i].charAt(j)) - 97;
                morse += morseWords[ind];
            }
            uniqCode.add(morse);
        }

            return uniqCode.size();

        }
    }

A 2nd approach using Java Stream:

public int uniqueMorseRepresentations(String[] words) {
        Set<String> res = Arrays.stream(words).map(i -> getMC(i)).collect(Collectors.toSet());
        return res.size();
    }
    private String getMC(String str) {
        String[] mCode = {".-","-...","-.-.","-..",".","..-.","--.","....","..",
                          ".---","-.-",".-..","--","-.","---",".--.","--.-",".-.",
                          "...","-","..-","...-",".--","-..-","-.--","--.."};
        return str.chars().mapToObj(ch -> mCode[(char)ch % 97]).reduce("", String::concat);
    }

With regards to the above code snippets, I have the following questions:

Which approach is better according to an interviewer?
What can be improved in the 1st approach?
Does the 2nd approach sacrifice readability for succintness?
Is there a better method, eg StringBuilder in place of string that can speed up the solution of the first approach?


texts:
This class should function as a simple insert into table. What do you think about it? How can I make my code better?

class addProfile{
private $profileName;
private $profileEmail;
private $profileFromName;
private $profileReplaytToEmail;

public function __construct($profileName,$profileEmail,$profileFromName,$profileReplayToEmail){
    $this->profileEmail = $profileEmail;
    $this->profileFromName = $profileFromName;
    $this->profileName = $profileName;
    $this->profileReplaytToEmail = $profileReplayToEmail;
}

public function postProfile($profileName,$profileEmail,$profileFromName,$profileReplayToEmail){
    $stmt = $this->mysqli->prepare("INSERT INTO SendingProfiles (fromemail,Fromname,Profilename,replaytoemail)
    VALUES ('$profileName','$profileEmail','$profileFromName','$profileReplayToEmail')");
    $stmt->bind_param($profileName,$profileEmail,$profileFromName,$profileReplayToEmail);

    if ($stmt->execute()){
        echo "the record added";
    }else{
        echo "Failed";
    }

    $stmt->close();

}

public function __destruct(){
    $this->mysqli->close();
}

}

$profileName = $_POST['profilename'];
$profileEmail = $_POST['fromemail'];
$profileFromName = $_POST['fromname'];
$profileReplayToEmail = $_POST['replaytoemail'];

$var = new addProfile();
$var->postProfile($profileName, $profileEmail, $profileFromName, $profileReplayToEmail)


texts: Simple countdown programs to print "3, 2, 1, Action!" using while loop

texts:
My program is a simple while loop making a countdown starting from 3 and shouts "Action!" upon reaching zero and then breaks. I would like to know which of the following ways is better in means of clarity and functionality. I am aware that there are other ways to achieve the same thing. I would like to hear your thoughts and suggestions.

countDown = 3
while (countDown >= 0):
    print(countDown)
    countDown = countDown - 1
    if countDown == 0:
        print("Action!")
        break

or

countDown = 3
while (countDown >= 0):
    if countDown != 0:
        print(countDown)
        countDown = countDown - 1
    else:
        print("Action!")
        break


texts: Python3 - Determing if a PDF is scanned or "searchable"

texts:
I am in a situation where I need to determine if a PDF file is either:

Scanned
Searchable (contain text)

To do this, I am simply running the command pdffonts on the PDF file:

# Check if the resource definition indicates this is a scanned PDF
cmd = ['pdffonts', pdf_file]
proc = subprocess.Popen(
    cmd, stdout=subprocess.PIPE, bufsize=0, text=True, shell=False)
out, err = proc.communicate()

scanned = True

for idx, line in enumerate(out.splitlines()):
    if idx == 2:
        scanned = False

Imagine I have two PDF files:

scanned.pdf
notscanned.pdf

The results of above:

scanned.pdf:

name                                 type              encoding         emb sub uni object ID
------------------------------------ ----------------- ---------------- --- --- --- ---------

notscanned.pdf

name                                 type              encoding         emb sub uni object ID
------------------------------------ ----------------- ---------------- --- --- --- ---------
TNNLVN+Calibri                       TrueType          WinAnsi          yes yes yes      9  0

As you can see, the notscanned.pdf file contains font information.

In my Python script, I iterate over each line in the command line output:

for idx, line in enumerate(out.splitlines()):
    if idx == 2:
        scanned = False

And if there is a line 2, then it contains font information.

I am wondering if this approach is viable? I've been searching for a solution for quite a bit, and it seems there is no way to know 100%.


texts:
It looks like it should be shortened but I can't see how.

I have my methods <= 5 lines but that's as far as I got:

def check_lines
  if horizontal?(@player) || vertical?(@player) || diagonal?(@player)
    @win=@player
  end
end

def horizontal?(player)
  ((@squares[0] == player) && (@squares[1] == player) && (@squares[2] == player)) ||
  ((@squares[3] == player) && (@squares[4] == player) && (@squares[5] == player)) ||
  ((@squares[6] == player) && (@squares[7] == player) && (@squares[8] == player))
end

def vertical?(player)
  ((@squares[0] == player) && (@squares[3] == player) && (@squares[6] == player)) ||
  ((@squares[1] == player) && (@squares[4] == player) && (@squares[7] == player)) ||
  ((@squares[2] == player) && (@squares[5] == player) && (@squares[8] == player))
end

def diagonal?(player)
  ((@squares[0] == player) && (@squares[4] == player) && (@squares[8] == player)) ||
  ((@squares[2] == player) && (@squares[4] == player) && (@squares[6] == player))
end


texts: Create a hierarchical tree with ordering field with entiry framework core

texts:
This is my model:

public class CaseType {
    public int CaseTypeID { get; set; }
    public int? ParentID { get; set; }
    public string CaseTypeText { get; set; }
    public int? OrderBy { get; set; }
}

I'm trying to create a hierarchical tree structure from this model with the following values:

CaseTypeID  ParentID   CaseTypeText   OrderBy
    1          Null       CT A         1
    2          Null       CT B         2
    3          Null       CT C         3
    4             1       CT A 1       1
    5             1       CT A 2       2
    6             3       CT C 1       1
    7             3       CT C 2       2

Which should output as:

CT A
   CT A 1
   CT A 2
CT B
CT C
   CT C 1
   CT C 2

This link helped me create T-SQL which nearly did the job. It just wouldn't let me use the OrderBy column. It used the order of the records in the table.   I decided I'm probably best doing it with EF and LINQ anyway but hopefully that should let you see what I'm trying to achieve.

This is the Index method from my controller:

public async Task<IActionResult> Index() {
    var TopLevelCaseTypes = await _context.CaseTypes.Where(ps => ps.ParentID.HasValue == false).OrderBy(x => x.OrderBy).ToListAsync();
    List<CaseType> outputView =  new List<CaseType>();
    foreach (CaseType IndividualTopLevelCaseType in TopLevelCaseTypes) {
        outputView.Add(IndividualTopLevelCaseType);
        var AllSubCaseTypes = await _context.CaseTypes.Where(ps => ps.ParentID == IndividualTopLevelCaseType.CaseTypeID).OrderBy(x => x.OrderBy).ToListAsync();
        foreach (CaseType IndividualSubCaseTypes in AllSubCaseTypes) {
            outputView.Add(IndividualSubCaseTypes);
        }
    }
    return View(outputView);
}

This achieves what I need it to but is inefficient as it gets the top level CaseTypes (where ParentID is null) then for each one, see if there are any subCaseTypes, adding each CaseType and subCaseType as it arrives to an outputView which is pushed to the View. Is there a better way of writing this in my controller making one DB call but letting me use the OrderBy field to pick the order?


texts: Faster way to replace strings in pandas series using dictionary in Python

texts:
I'm working with a dictionary with 11830 keys/values, that have to loop over 13905 strings, that can have up to 50 words each. My series looks like this:

  strings
"hello foo helloo"
"bye bar byer"

And my dictionary like this:

dic={'hello':'hi','bye':'byebye'}

So I want my final Series to look like this:

  strings
"hi foo helloo"
"byebye bar byer"

I achieved that like this:

for k,v in dicc.items():
    b=b.apply(lambda x: re.sub(r'\b'+k+r'\b',r'\b'+v+r'\b',x))

But it's quite slow. Is there a faster way to do it?

Edit: noticed splitting the string didn't solve the issue of replacing substrings that matched the dictionary.


texts:
I retrieved all the books in pdf from my library with a recursive function.

import os

def walk(dirname):
    for name in os.listdir(dirname):
        path = os.path.join(dirname, name)

        if os.path.isfile(path) and path.endswith('pdf'):
            books.append(path)
        if os.path.isdir(path):
            walk(path)
def main():
    dir = '~/Documents/MyLibrary/'
    global books
    books = []
    walk(dir)
    print(books)

if __name__ == "__main__":
    main()


texts: Calculate the amount of savings I will have in n months

texts:
I have the following problem and I am new in Java:
I have an initial capital of 10 000. I invest this initial capital of 10 000 in the bank for a interest rate per month of 0.04/12. My net income is 3000 per month and my expenses per month is 2000. I invest also my monthysavings = net income - expenses = 3000 - 2000 = 1000 in the bank for the interest rate of 0.04/12.
I would like to calculate the amount of savings I will have in 300 months.
I did the following code:

public class RetCalcJava {

public double futureCapital(
      double interestRate
    , int nbOfMonths
    , int netIncome
    , int currentExpenses
    , double initialCapital) {

double  monthlySavings = netIncome - currentExpenses;
double iniVal=0;
double finalVal =0;
double intermediate = 0;

for (int i=0; i< nbOfMonths; i++){
    System.out.println("Boucle" + i);

    if (i ==0) {
        iniVal = initialCapital * (1 + interestRate) + monthlySavings;
        intermediate = iniVal;

        System.out.println("intival" + iniVal);
    }
    else {
        finalVal =  intermediate * (1 + interestRate) + monthlySavings;
        intermediate = finalVal;
        System.out.println("finalVal" + finalVal);
    } }    return finalVal;

}
}

The main class is :
public class RetCalcMain {

public static void main(String[] args) {
    RetCalcJava retcal = new RetCalcJava();
    retcal.futureCapital(0.04/12, 300, 3000,2000,10000);

    }
}

This code works. What do you think about my code? Is it optimized? What should I change?


texts:
It works, but it seems a bit long winded. I'd like some critiquing.

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

struct node
{
  int data;
  struct node * previous;
  struct node * next;
};

struct linked_list
{
  struct node * first;
  struct node * last;
};

struct linked_list * init_list(struct node * node)
{
  struct linked_list * ret = malloc(sizeof(struct linked_list));
  ret->first = node;
  ret->last = node;
  return ret;
}

struct node * create_node(int data)
{
  struct node * ret = malloc(sizeof(struct node));
  ret->data = data;
  ret->previous = NULL;
  ret->next = NULL;
  return ret;
}

void destroy_list(struct linked_list * list)
{
  struct node * node = list->first, *next = NULL;
  while(node != NULL)
  {
    next = node->next;
    free(node);
    node = next;
  }
  free(list);
}

void push_front(struct linked_list * list, struct node * node)
{
  if(list->first != NULL)
  {
    node->next = list->first;
    list->first->previous = node;
  }
  else list->last = node;
  list->first = node;
}

void push_back(struct linked_list * list, struct node * node)
{
  if(list->last != NULL)
  {
    node->previous = list->last;
    list->last->next = node;
    list->last = node;
  }
  else push_front(list, node);
}

void insert_after(struct node * node, struct node * to_add)
{
  to_add->previous = node;
  to_add->next = node->next;
  node->next = to_add;
}

void remove_node(struct node * node)
{
  node->previous->next = node->next;
  node->next->previous = node->previous;
  free(node);
}


texts: Kaitai Struct: shift rotate >1 byte groups

texts:
This is an implementation of rotation (cyclic shift) of many-byte groups to the left. Rotate right is supported as negative amount. I would like to request, if someone would be willing, to review this code for correctness and performance. Both Python 2 and 3 need to be supported.

This code is part of Kaitai Struct framework. Current implementation can be found here. Code below is in PR #28, unmerged.

There is a test case for this feature here.

Support code:

if PY2:
    range = xrange
    def integers2bytes(ints):
        return bytes(bytearray(ints))
    def bytes2integers(data):
        return bytearray(data)
else:
    def integers2bytes(ints):
        return bytes(ints)
    def bytes2integers(data):
        return data

Main code:

class KaitaiStream(object):

    # formula taken from: http://stackoverflow.com/a/812039
    precomputed_single_rotations = {amount: [(i << amount) & 0xff | (i >> (8-amount)) for i in range(256)] for amount in range(1,8)}

    @staticmethod
    def process_rotate_left(data, amount, group_size):
        amount = amount % (group_size * 8)
        if amount == 0:
            return data
        data_ints = bytes2integers(data)

        if group_size == 1:
            translate = KaitaiStream.precomputed_single_rotations[amount]
            return integers2bytes(translate[a] for a in data_ints)

        if len(data) % group_size != 0:
            raise Exception("data length must be a multiple of group size")

        if amount % 8 == 0:
            indices = [(i + amount//8) % group_size for i in range(group_size)]
            return integers2bytes(data_ints[i+k] for i in range(0,len(data),group_size) for k in indices)

        amount1 = amount % 8
        amount2 = 8 - amount1
        indices_pairs = [ ((i+amount//8) % group_size, (i+1+amount//8) % group_size) for i in range(group_size)]
        return integers2bytes((data_ints[i+k1] << amount1) & 0xff | (data_ints[i+k2] >> amount2) for i in range(0,len(data),group_size) for k1,k2 in indices_pairs)


texts:
I've solved The trip from Programming Challenges.  My solution suffers from lack of floating-point precision, though.

The problem:

  Your job is to compute, from a list of expenses, the minimum amount of
  money that must change hands in order to equalize (within a cent) all
  the students' costs.
  Input Standard input will contain the information for several trips.
  The information for each trip consists of a line containing a positive
  integer, n , the number of students on the trip, followed by n lines
  of input, each containing the amount, in dollars and cents, spent by a
  student. There are no more than 1000 students and no student spent
  more than $10,000.00. A single line containing 0 follows the
  information for the last trip. Output For each trip, output a line
  stating the total amount of money, in dollars and cents, that must be
  exchanged to equalize the students' costs.

For example, if you have:

4
15.00
15.01
3.00
3.01

The output should be:

$11.99

My code:

#include <iostream>
#include <iomanip>

using namespace std;

float two_precision(float n){
    return n - float((n*100.0-(int)(n*100))/100.0);
}

int main(){

  int n; cin >> n;
  float *v, sum, avg, dif;

  while(n){

    sum = dif = 0.0;
    v = new float[n];
    for(int i = n-1; i >= 0; i--){
      cin >> v[i];
      sum += v[i];
    }

    avg = (1.0*sum)/n;

    for(int i = n-1; i >= 0; i--)
      if(two_precision(v[i]-avg > 0))
        dif = two_precision(dif + two_precision(v[i]-avg));

    cout << '$';
    cout << fixed << setprecision(2) << dif << endl;

  delete v;
  cin >> n;
  }

return 0;
}

I have one case to show the error:

5
15.04
15.17
30.32
39.99
78.99
0

The output should be $47.17 and my code returns $47.16.


texts: Number of Integer Pairs with a Specific Difference

texts:
Problem

Adapted from this HackerRank Problem.

  Given N integers, count the number of pairs of integers whose difference is K.

Approach

Given the initial int array, sort it.
Now iterate through the indices in the array using a lower index (starting at 0) and an upper index (starting at 1)`.
Calculate the difference between the value at the upper index value and the value at the lower index.

If the difference is equal to the target difference, increment the count and increment the upper index.
If the difference is less than the target difference, increment the upper index. A difference less than the target
difference implies that the value at the upper index is not large enough.
If the difference is greater than the target difference, increment the lower index. A difference greater than the
target implies that the value at the lower index is not large enough.

Return the count

Implementation

public class PairDifferenceCounter {
    public static int countPairsWithDifference(int[] values, int difference) {
        Arrays.sort(values);

        int count = 0;
        int lowerIndex = 0;
        int upperIndex = 1;

        while (upperIndex < values.length) {
            int pairDifference = values[upperIndex] - values[lowerIndex];

            if (pairDifference == difference) {
                count++;
                upperIndex++;
            } else if (pairDifference > difference) {
                lowerIndex++;
            } else {
                upperIndex++;
            }
        }

        return count;
    }
}


texts: Command to measure CPU time and RAM usage of process while making sure it does not go over certain ressource limitation

texts:
I found some old code of mine and am trying to see if it can be improved. The goal is to write a command that measures a process's CPU time and RAM peak usage and kills the process if it exceeds a specified amount of CPU time and RAM usage.

This command is meant to be used on both MAC OSX and Linux.

# Get arguments
MaxMemory="$1"
MaxTime="$2"
Command="$3"
for (( i=4 ; i<="$#"; i++)); do
    Command="${Command} ${!i}"
done

echo -e "MaxMemory = ${MaxMemory}\nMaxTime = ${MaxTime}\nCommand = ${Command}"

#### run the command in the background
${Command} &

#### Get pid
pid=$!
echo "pid = ${pid}"

#### Monitor resources
MemoryPeak=0
timeBefore=$(date +"%s")
while true;do
    # Get memory
    mem=$(ps -o rss= -p $pid)

    # Break if the process has stopped running
    if [[ ${mem} == "" ]]; then
        break
    fi

    # Set the MemoryPeak of memory
    if [ "${mem}" -gt "${MemoryPeak}" ]; then
        MemoryPeak=$mem
    fi

    # If it consumed too much memory, then kill
    if [ "${MemoryPeak}" -gt "${MaxMemory}" ];then
        #echo "process consumed too much memory"
        kill ${pid}
        break
    fi

    # If it consumed too much CPU time, then kill
    timeAfter=$(date +"%s")
    timeUsage=$((timeAfter - timeBefore))
    if [ "${timeUsage}" -gt "${MaxTime}" ];then
        #echo "process consumed too much time"
        kill ${pid}
        break
    fi

    # sleep
    sleep 0.1
done

timeAfter=$(date +"%s")
timeUsage=$((timeAfter - timeBefore))

echo "MEM ${MemoryPeak} TIME ${timeUsage}"

How can this be improved?


texts: Converting between different units of temperature

texts:
The task is to convert a temperature in a list of Fahrenheit, Celsius, or Kelvin to the two other temperatures in the list.

Here is my code:

/*
 * #define LANG C
 */

#include <stdio.h> /** standard io **/
#include <stdlib.h> /** exit(), atof and the lot **/
#include <unistd.h> /** needed for getopt() **/

/*** SYMBOIC CONSTANTS ***/
#define VERSION "1.0" /** version string array **/
#define HELP help(argv[0]) /** will insert this instead of help(argv[0]) every time **/
#define FLOATARG atof(optarg) /** atof is kind of vague, so a #define is better **/
#define TOVERH "FAHR\tCELSIUS\tKELVIN\n" /** just so I don't have to type it out ever time **/
#define OPTS "vk:f:c:h" /** I prefer a #define to this -> b:ull:s::hi:t: **/
/** For reference, an option followed by a double colon **/
/**        signifies that it takes no arguments         **/
/**  while an option followed by one colon signifies    **/
/**           that it takes one argument.               **/

static const char *ver = VERSION;

void help(char *s);

int check(float N) { /** check if kelvin temperature is celsius 0 **/
    if(N > 273.1499 && N < 273.15001) { /** I had to set this limit. There is no other way. **/
        return 1;
    }
    else {
        return 0;
    }
}

/*** Fahrenheit ***/
float FK(float F) {
    return(((5.0 / 9.0) * (F - 32.0)) + 273.15);
}
float FC(float F) {
    return((5.0 / 9.0) * (F - 32.0));
}
void printfahr(float F) {
    printf(TOVERH);
    printf("%4.3f\t%5.3f\t%5.3f\n", F, FC(F), FK(F));
}

/** Celsius **/
float CK(float C) {
    return((C + 273.15));
}
float CF(float C) {
    return((C * (9.0 / 5.0) - 32.0));
}
void printc(float C) {
    printf(TOVERH);
    printf("%4.3f\t%5.3f\t%5.3f\n", CF(C), C, CK(C));
}

/*** Kelvin ***/
float KC(float K) {
    return(K - 273.15);
}
float KF(float K) {
    return((K - 273.15) * (9.0 / 5.0) - 32.0);
}
void printk(float K) {
    printf(TOVERH);
    printf("%4.3f\t%5.3f\t%5.3f\n", KF(K), KC(K), K);
}

int main(int argc, char *argv[]) {
    if(argc == 1) {
        HELP;
        exit(argc);
    }
    else {
        char arg = '\0';
        while((arg = getopt(argc, argv, OPTS)) != -1)
        {
            switch(arg) {
                case 'c':
                    if(optarg == NULL)
                    {
                        HELP;
                        exit(1);
                    }
                    else {
                        printc(FLOATARG);
                    }
                break;

                case 'f':
                    if(optarg == NULL)
                    {
                        HELP;
                        exit(1);
                    }
                    else {
                        printfahr(FLOATARG);
                    }
                break;

                case 'h':
                    HELP;
                break;

                case 'k':
                    if(optarg == NULL)
                    {
                        HELP;
                        exit(1);
                    }
                    else {
                        if(check(FLOATARG) != 0)
                        {
                            printf(TOVERH);
                            printf("32.0\t0\t273.15\n");
                        }
                        else {
                            printk(FLOATARG);
                        }
                    }
                break;

                case 'v':
                    printf("%s\n", ver);
                break;

                default:
                    HELP;
                break;
            }
        }
    }
    return 0;
}

void help(char *s) {
    printf("Usage:\t%s [-c Celsius] [-f Fahrenheit] [-h] [-k Kelvin] [-v]\n", s);
}


texts: SEDE query calculating user reputation averages

texts:
I've created the following SEDE query which calculates the reputation averages of users on a particular site. It also optionally excludes "new" users (users with rep of 1 or 101 so note 100% accurate).
DECLARE @exclude_new_users INT
SET @exclude_new_users = ##ExcludeNewUsers:int?0##

DECLARE @excluded_rep_table TABLE (Rep INT)
INSERT INTO @excluded_rep_table VALUES (1),(101)

DECLARE @rep_table TABLE (Reputation INT)
INSERT INTO @rep_table (Reputation)
    SELECT Reputation
    FROM USERS
    WHERE Reputation <>
        CASE WHEN @exclude_new_users = 0
        THEN -1
        ELSE (SELECT IIF (EXISTS (SELECT Rep FROM @excluded_rep_table WHERE Rep = Reputation), Reputation, -1))
        END

SELECT COUNT(*) AS [Total Users],
       MIN(Reputation) AS [Minimum],
       MAX(Reputation) AS [Maximum],
       SUM(Reputation) / COUNT(*) AS [Mean],
       (
           SELECT TOP 1 Reputation
           FROM @rep_table
           GROUP BY Reputation
           ORDER BY COUNT(Reputation) DESC
       ) AS [Mode],
       (
           SELECT
           (
               (
                   SELECT MAX(Reputation)
                   FROM
                   (
                       SELECT TOP 50 PERCENT Reputation
                       FROM @rep_table
                       ORDER BY Reputation
                   ) AS [Bottom Half]
               )
               +
               (
                   SELECT MIN(Reputation)
                   FROM
                   (
                       SELECT TOP 50 PERCENT Reputation
                       FROM @rep_table
                       ORDER BY Reputation DESC
                   ) AS [Top Half]
               )
           ) / 2
       ) AS [Median],
       STDEVP(Reputation) AS [Standard Deviation]
FROM @rep_table

I'm quite the SQL novice and mainly did this for some practice so I'm really after general feedback: ways to improve/optimise the code but also to improve the layout, it looks ugly and I can't imagine it fits with style guidelines.


texts:
NOTE
The script relies on ruamel-yaml and requests packages
available on pypi.

I wrote a script which goes over the pack file details as specified by the emojipacks project, and downloads (and saves) to the local drive. It downloads the files as threads, and saves to disk with the shutil's copyfileobj method.
Feel free to nitpick each and everything which could be improved.
from argparse import ArgumentParser
from pathlib import Path
from shutil import copyfileobj
from threading import Thread

import requests
import ruamel.yaml as YAML

yaml = YAML.YAML(typ="safe")
DOWNLOAD_DIR: str = "emojis"

def download_file(url: str, filename: str, parent: Path):
    """
    Downloads the file from `url` and saves to `filename` inside the `parent` directory.
    """
    suffix = url.split(".")[-1]
    if suffix in ("png", "jpg", "gif", "jpeg"):
        filename = f"{filename}.{suffix}"
    path = Path(parent / filename)
    with requests.get(url, stream=True) as response:
        with path.open(mode="wb") as f:
            copyfileobj(response.raw, f)

def create_download_thread(url: str, name: str, path: Path) -> Thread:
    thread = Thread(target=download_file, args=(url, name, path))
    thread.start()
    return thread

def main(dir_path: str, download_dir: str):
    path = Path(dir_path)
    threads = []
    if not path.is_dir():
        return
    for yaml_file in path.glob("*.yaml"):
        data = yaml.load(yaml_file)
        emoji_dir = Path(download_dir) / data["title"]
        emoji_dir.mkdir(parents=True, exist_ok=True)
        for emoji in data["emojis"]:
            names = [emoji.get("name"), *emoji.get("aliases", [])]
            src = emoji.get("src")
            threads += [create_download_thread(src, name, emoji_dir) for name in names]
    for thread in threads:
        thread.join()

if __name__ == "__main__":
    parser = ArgumentParser(description="Download emojipacks to specified directory!")
    parser.add_argument(
        "directory",
        nargs="+",
        help="Directory(ies) with yaml for the emojipack specifications.",
    )
    parser.add_argument(
        "-p",
        "--download-path",
        help="Directory where emojis will be downloaded.",
        default=DOWNLOAD_DIR,
    )
    args = parser.parse_args()
    for path in args.directory:
        main(path, args.download_path)

The script was created because the project emojipacks itself is no longer maintained and the authentication system has been updated from slack. The downloaded emojis are then bulk uploaded to slack using another utility slack-emojinator.

EDIT
Since this point was raised in the answer(s), the yaml specification is:

Also note that the yaml file must be indented properly and formatted
as such:
title: food emojis:
  - name: apple
    src: http://i.imgur.com/Rw0Vlda.png
  - name: applepie
    src: http://i.imgur.com/g4RU1fM.png

..with the src pointing to an image file. According to Slack:
[...]
It is possible to give multiple names to a single emoji using yaml
such as:
title: octicons emojis:
  - name: pr
    aliases:
      - pullrequest
      - mergerequest
    src: https://i.imgur.com/rhwNxfc.png

emphasis mine


texts: Display element in a grid and conditionally add to redux or remove if not selected

texts:
I have some logic on my JSX for an element in a grid, if element is selected, then add to redux, if element is not selected then remove from redux...
is working ok, but I sense a bad smell...
<GridSelector
          items={items}
          onChange={(categoryId, isSelected) => {
            favorites[categoryId] = isSelected;
            const favsArraya = Object.values(onboarding.favoriteCategories);
            let index;

            if(isSelected){
              if (favsArraya.indexOf(categoryId) === -1){
                //doesnt exist, add it
                 favsArraya.push(categoryId)
              }
            }else{
              if (favsArraya.indexOf(categoryId) !== -1){
                // exists, delete it
                index = favsArraya.indexOf(categoryId);
                favsArraya.splice(index, 1);
              }
            }

            dispatchData({ [DATA_KEY.FAVORITE_CATEGORIES]: favsArraya})

          }}
        />


texts:
I built a simple Blackjack game in Ruby on Rails, and am looking for feedback, because I believe I may have overcomplicated things. This was a homework assignment, where I was told to make 1 be an Ace card.

$number_array = []

def pull_push_random()
    number = Random.rand(1..10)
    $number_array.push(number)
    $array_sum = $number_array.inject(0){|sum,number_array| sum + number_array }
end

pull_push_random()
pull_push_random()
puts "This is an array #{$number_array}"

puts "Would you like to draw another card (random number)? y/n"
answer = gets.chomp
if answer == 'y'
    pull_push_random()
    puts "This is the array: #{$number_array}"
    if $array_sum < 16
        pull_push_random()
        puts "This is the array #{$number_array}"
    elsif $array_sum > 21
        puts "Done"
    else
        puts "Invalid option"
    end
elsif answer == 'n'
    puts "Done"
else
    puts "You did not enter a valid option."
end


texts: Drawing a regression line with a timeseries

texts:
My code is using seaborn to draw a regression line with a timeseries data as the x axis, and whatever you want the y-axis to be. Since seaborn does not support this function directly, a dummy column has to be made first based on the timeseries (i.e. instead of using 2016-05-01, use 1,2,3,4,5... to represent the date. Then, plot the regression line using 1,2,3... as the x-axis and replace the 1,2,3 label with 2016-05-01, 2016-05-02...

def regplot_timeseries(df:pd.DataFrame, time_col:str, data_col:str, figsize=(14, 6), xlabel = '', ylabel = ''):
    # make a copy of the incoming data (think of the incoming data as an excel sheet visually)
    dfc = df.copy()

    # if it is a index, treat it slightly differently
    # add a column to the copy of the excel sheet for plotting purpose
    if time_col == 'index':
        dfc = dfc.sort_index()
        dfc['date_f'] = pd.factorize(dfc.index)[0] + 1
        mapping = dict(zip(dfc['date_f'], dfc.index.date))
    else:
        dfc = dfc.sort_values(time_col_str)
        dfc['date_f'] = pd.factorize(dfc[time_col])[0] + 1
        mapping = dict(zip(dfc['date_f'], dfc[time_col].dt.date))

    # plotting
    fig, ax = plt.subplots()
    fig.set_size_inches(figsize[0], figsize[1])
    sns.regplot('date_f', data_col, data=dfc, ax=ax)
    labels = pd.Series(ax.get_xticks()).map(mapping).fillna('')
    ax.set_xticklabels(labels)
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)

    # delete the copy to free up memory
    del dfc

I am new to functional programming, and from what I read, I shouldn't alter the data being passed in. In this case, I shouldn't alter df, so I made a copy instead and deleted the copy at the end of the function. Is this the right to do it?


texts:
I have a list of authors, where seeking duplicity. The indexes of these duplicates are stored in a list of lists named duplicity. For example, at index 0 in the list duplicity is nested list, which refer to indexes of authors list with duplicates author A, index 1 author duplicity B, etc.
I also have list edge, where indexes of nodes(authors) are stored that are connected by an edge. Program passes through and removes duplicate authors by list duplicity.

For example I have stored in duplicity list indexes 1, 2, 3. These refers to authors list, where are stored names of authors. It also refers to edge list, where are stored start and end point of edges. For example if I have authors stored on indexes 1,2,3 and they are connected altogether, than the edge list is 1,2, 1,3, 2,3. Because authors on indexes 1,2,3 are same, I need make them one. So I pass edge list and replace all 2 and 3 to 1.

The problem is that progress of code below is very slow. I'm sure that in the code is something useless which raise complexity, but I can not figure it out. I think, the main failure in my code is the first part with the for loops. It many times passes authors list and if there are tens of thousands items, it's really slow. Can you help me optimize this code?

 for (int i = 0; i < authors.GetDuplicates().Count; i++)
        {
            for (int j = 0; j < edge.Count; j++)
            {
                for (int k = 0; k < authors.GetDuplicates()[i].Count - 1; k++)
                {
                    if (edge[j] == authors.GetDuplicates()[i].ElementAt(k))
                    {
                        edge[j] = authors.GetDuplicates()[i].ElementAt(k + 1);
                    }
                }
            }
        }

 public static List<List<int>> GetDuplicates(this IList<string> source)
    {
        HashSet<string> itemsSeen = new HashSet<string>();
        HashSet<string> itemsYielded = new HashSet<string>();
        List<List<int>> duplicates = new List<List<int>>();
        List<int> dupLow = new List<int>();
        HashSet<int> temp = new HashSet<int>();

        int c = 0;

        foreach (string item in source)
        {
            if (!itemsSeen.Add(item))
            {
                if (itemsYielded.Add(item))
                {
                    if (item != "-")
                    {
                        int w = 0;

                        for (int j = 0; j < authors.Count; j++)
                        {
                            if (authors[j] == authors[c])
                            {
                                temp.Add(w);
                            }
                            w++;
                        }

                        dupLow = new List<int>(temp);
                        duplicates.Add(dupLow);
                        temp.Clear();
                    }
                }
            }
            c++;
        }
        return duplicates;
    }

EDIT:
We have four authors, A and B are authors of one book and C and A are authors of different book. Authors within one book are connected by edge. We can see, there are two authors "A". So we replace in edges index of author A (index 0) to 3. At the end books are connected by common author.

authors | edge | remove dup. | bad result
[0] A    [0] 0   [0] 3    [0] 4
[1] B    [1] 1   [1] 1    [1] 1

[2] C    [2] 2   [2] 2    [2] 2
[3] A    [3] 3   [3] 3    [3] 3

The result should look like this:


texts: Simplifying decision tree when working with map which has two keys and value as a slice

texts:
Can this logic not be simplified more, the code works, but it has become too much of a spaghetti. The requirements are that there should always be one connection if possible in available state, but I need to take all the available states down and re-create them.
these paths I have to cover:

if both vifs down, call create
if both vifs available, take them down and up one-by-one
if one down and one available, bring first up the down one, then take down and bring up the available one
if only one down, get it up, then create the second one
if only one up, create the second one, then delete and re-create the first one

var vifsMap = make(map[string][]*directconnect.VirtualInterface)
var vifsStateAvailable, vifsStateDown []*directconnect.VirtualInterface
for _, item := range convertToDirectconnect(lagPair, amz.dcLags) {
    vis, err := amz.dcRead.DescribeVirtualInterfaces(&directconnect.DescribeVirtualInterfacesInput{ConnectionId: item.LagId})
    if err != nil {
        return err
    }

    for _, vi := range vis.VirtualInterfaces {
        if *vi.OwnerAccount == cfg.RemoteAccountID {
            if aws.StringValue(vi.VirtualInterfaceState) == directconnect.VirtualInterfaceStateAvailable {
                vifsStateAvailable = append(vifsStateAvailable, vi)
            } else if aws.StringValue(vi.VirtualInterfaceState) == directconnect.VirtualInterfaceStateDown {
                vifsStateDown = append(vifsStateDown, vi)
            }
        }
    }
}
vifsMap["available"] = vifsStateAvailable
vifsMap["down"] = vifsStateDown

if len(vifsStateDown) > 1 {
    fmt.Println("contains 2 down elements")
} else if len(vifsStateAvailable) > 1 {
    fmt.Println("contains 2 up elements")
} else if len(vifsStateDown) == 1 && len(vifsStateAvailable) == 1 {
    fmt.Println("contains 1 vif down and 1 up")
} else if len(vifsStateAvailable) == 1 && len(vifsStateDown) == 1 {
    fmt.Println("contains 1 vif up and 1 down")
} else if len(vifsStateDown) == 1 && len(vifsStateAvailable) == 0 {
    fmt.Println("contains 1 down only")
} else if len(vifsStateAvailable) == 1 && len(vifsStateDown) == 0 {
    fmt.Println("contains 1 up only")
}
```


texts: Python / Numpy running 15x slower than MATLAB - am I using Numpy effeciently?

texts:
Here is some code I wrote in Python / Numpy that I pretty much directly translated from MATLAB code. When I run the code in MATLAB on my machine, it takes roughly 17 seconds. When I run the code in Python / Numpy on my machine, it takes roughly 233 seconds. Am I not using Numpy effectively? Please look over my Python code to see if I'm using Numpy in a non effective manner. All this code is doing is fitting the parameter D (diffusion coefficient) in the heat equation to some synthetically generated data using MCMC method.

import numpy as np
from numpy import *
import pylab as py
from pylab import *
import math
import time

def heat(D,u0,q,tdim):
    xdim = np.size(u0)
    Z = np.zeros([xdim,tdim])
    Z[:,0]=u0;
    for i in range(1,tdim):
        for j in range (1,xdim-1):
            Z[j,i]=Z[j,i-1]+ D*q*(Z[j-1,i-1]-2*Z[j,i-1]+Z[j+1,i-1])
    return Z

start_time = time.clock()
L = 10
D = 0.5

s = 0.03  # magnitude of noise

Tmax = 0.2
xdim = 25
tdim = 75

x = np.linspace(0,L,xdim)
t = np.linspace(0,Tmax,tdim)

dt = t[1]-t[0]
dx = x[1]-x[0]

q = dt/(dx**2)
r1 = 0.75*L
r2 = 0.8*L

################################################
## check the stability criterion dt/(dx^2)<.5 ##
################################################

# Define the actual initial temperature distribution
u0 = np.zeros(xdim)
for i in range(0,xdim):
    if(x[i]>=r1 and x[i]<=r2):
        u0[i] = 1
xDat = range(1,xdim-1)
tDat = np.array([tdim])
nxDat = len(xDat)
ntDat = 1
tfinal = tdim-1

# synthesize data
Z = heat(D,u0,q,tdim)
u = Z[xDat,tfinal] # matrix
uDat = u + s*randn(nxDat)

# MATLAB PLOTTING
#figure(1);surf(x,t,Z); hold on;
#if ntDat>1, mesh(x(xDat),t(tDat),uDat);
#else set(plot3(x(xDat),t(tDat)*ones(1,nxDat),uDat,'r-o'),'LineWidth',3);
#end; hold off; drawnow

#MCMC run
N = 10000
m = 100
XD = 1.0
X = np.zeros(N)
X[0] = XD
Z = heat(XD,u0,q,tdim)
u = Z[xDat,tfinal]
oLLkd = sum(sum(-(u-uDat)**2))/(2*s**2)
LL = np.zeros(N)
LL[0] = oLLkd

# random walk step size
w = 0.1
for n in range (1,N):
    XDp = XD+w*(2*rand(1)-1)
    if XDp > 0:
        Z = heat(XDp,u0,q,tdim)
        u = Z[xDat,tfinal]
        nLLkd = sum(sum( -(u-uDat)**2))/(2*s**2)
        alpha = exp((nLLkd-oLLkd))
        if random() < alpha:
            XD = XDp
            oLLkd = nLLkd
            CZ = Z
    X[n] = XD;
    LL[n] = oLLkd;

print time.clock() - start_time, "seconds"


texts:
Below is my Python 3.7 code for a Tic-Tac-Toe game I made as a programming challenge. I want to know if I could make this code less janky, more readable or improve it in any way.

# Defining Main Game Functions, variables, etc.
board = [0,1,2,
         3,4,5,
         6,7,8]
win_con = [[0,1,2],[3,4,5],[6,7,8],
            [0,3,6],[1,4,7],[2,5,8],
            [0,4,8],[2,4,6]] # possible 3-in-a-rows
def show():
    print(board[0],'|',board[1],'|',board[2])
    print('----------')
    print(board[3],'|',board[4],'|',board[5])
    print('----------')
    print(board[6],'|',board[7],'|',board[8])
def x_move(i):
    if board[i] == 'X' or board[i] == 'O':
        return print('Already taken!')
    else:
        del board[i]
        board.insert(i,'X')
def o_move(i):
    if board[i] == 'X' or board[i] == 'O':
        return print('Already taken!')
    else:
        del board[i]
        board.insert(i,'O')
# Main Game Loop
while True:
    turn_num = 1
    board = [0,1,2,3,4,5,6,7,8]
    print('Welcome to Tic-Tac-Toe!')
    print('AI not implemented yet.')
    while True:
        for list in win_con: # check if someone's won
            xnum = 0
            onum = 0
            for num in list:
                if board[num] == 'X':
                    xnum += 1
                elif board[num] == 'O':
                    onum += 1
                else:
                    pass
            if xnum == 3 or onum == 3:
                break
        if xnum == 3 or onum == 3: # break out of all loops
            break
        if turn_num > 9: # check if board is full
            break
        show()
        if turn_num % 2 == 1:
            print('X\'s turn.')
        else:
            print('O\'s turn.')
        move = int(input('Choose a space. '))
        if turn_num % 2 == 1:
            x_move(move)
        else:
            o_move(move)
        turn_num += 1
    if xnum == 3: # After game is over
        print('X Won!')
    elif onum == 3:
        print('O Won!')
    else:
        print('Draw!')
    play_again = input('Play again? Y or N ')
    if play_again == 'Y' or play_again == 'y':
        continue
    else:
        break


texts: Reading attachment data from a PostgreSQL table with python

texts:
I have developed this script to find the counts of attachment records stored in a table in PostgreSQL for different other tables. I check if the res_id field of ir_attachment table is existed in it's corresponding table (res_model) and if it's not existed, I increase the counter. This script works fine and I get the desired result but it is slow with big number of records (9000000 -10000000). How can I improve this script's performance to optimize it? Should I use any SQL technic or bring changes in python code?

def delete_attachment(self):
        self.apps_cursor.execute("""
                                SELECT id,res_id,res_model FROM ir_attachment WHERE res_model IS NOT NULL AND res_id IS NOT NULL limit 5;
                                """)
        counter = 0
        attachment_records = self.apps_cursor.fetchall()
        print '=========================================Checking attachment records started========================================='
        self.apps_cursor.execute("""
                SELECT model FROM ir_model WHERE model IS NOT NULL;
        """)
        apps_models = self.apps_cursor.fetchall()
        apps_models = [model.get('model') for model in apps_models]
        for record in attachment_records:
            if record['res_model'] in apps_models:
                model_name = str(record['res_model'].replace('.', '_'))
                self.apps_cursor.execute("""
                        SELECT id FROM %s WHERE id=%s
                """ % (model_name, record['res_id']))
                model_record = self.apps_cursor.fetchall()

                if not model_record:
                    counter += 1
        print 'Total attachment records with out res_id field existed in corresponding model:', counter