texts: Improving function when used from another class

texts:
The title could probably be better, but in any case here goes.

I am having a bit of a problem understanding how to move methods/functions to other classes to organize things.

So, I have moved a method of sorts, but as I am skeptical of it, I would like to get it reviewed and hear opinions on what can be improved.

Here is the method ran from the default class:

private void GetAudioDevices()
{
    foreach (MMDevice device in Enumerator.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active))
    {
        comboBox1.Items.Add(device.FriendlyName);
        Console.WriteLine("{0}, {1}", device.FriendlyName, device.State);
    }
    PlayBackDevices = Enumerator.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active);
    for (waveInDevice = 0; waveInDevice < NAudio.Wave.WaveIn.DeviceCount; waveInDevice++)
    {
        DeviceInfo = WaveIn.GetCapabilities(waveInDevice);
        AudioDevices.Items.Add(DeviceInfo.ProductName);
    }
    if (AudioDevices.Items.Count > 0)
        AudioDevices.SelectedIndex = 0;

    AudioDevices.Items.Add("Wasapi Loopback");
    ListenAudioAgain = false;
    AudioDevice = AudioDevices.SelectedItem.ToString();
    comboBox1.SelectedItem = VoiceChatClass.Enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia).ToString();
}

Here it is when ran from a separate class:

public static void GetAudioDevices(ComboBox comboBox1, ComboBox AudioDevices)
{
    foreach (MMDevice device in VoiceChatClass.Enumerator.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active))
    {
        comboBox1.Items.Add(device.FriendlyName);
        Console.WriteLine("{0}, {1}", device.FriendlyName, device.State);
    }
    VoiceChatClass.PlayBackDevices = VoiceChatClass.Enumerator.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active);
    for (VoiceChatClass.waveInDevice = 0; VoiceChatClass.waveInDevice < NAudio.Wave.WaveIn.DeviceCount; VoiceChatClass.waveInDevice++)
    {
        VoiceChatClass.DeviceInfo = NAudio.Wave.WaveIn.GetCapabilities(VoiceChatClass.waveInDevice);
        AudioDevices.Items.Add(VoiceChatClass.DeviceInfo.ProductName);
    }
    if (AudioDevices.Items.Count > 0)
        AudioDevices.SelectedIndex = 0;

    AudioDevices.Items.Add("Wasapi Loopback");
    VoiceChatClass.ListenAudioAgain = false;
    VoiceChatClass.AudioDevice = AudioDevices.SelectedItem.ToString();
    comboBox1.SelectedItem = VoiceChatClass.Enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia).ToString();
        Console.WriteLine(VoiceChatClass.Enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia));
}

I am having difficulties understanding how to use static and non-static variables.

I just want to move it to a separate class for organization reasons. I know you can do it with partial classes, but sadly they are a bit "bugged" when used with a form in Visual Studio, as the design form will go away and stuff.  It just gets unnecessarily complicated so to speak.

So anyway, what are your opinions upon viewing this?  Is there a way to make it simpler?  Are there any issues that have come up?


texts:
This program is used to get names and numbers of N person, print out the numbers of a person. Please help me improve this code.

Additional Question:

To avoid a very long name that exceeds char's size, I limit it to 255. Is there a better way?

To avoid buffer, I included a space before % sign when using scanf and not use getchar(). Is there a better way?

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

// Limiting string input
const unsigned int STRINGCONSTRAINT = 255;

// Person struct to store name and numbers
typedef struct {
  char name[STRINGCONSTRAINT];
  char numbers[STRINGCONSTRAINT];
} person;

void getNameNumber(person people[], int size);
void printNumber(person people[], int size, char personFind[]);

// Asked for array's size and a person's name to find
int main(void) {
  unsigned int size = 0;
  printf("Enter the amount of people: ");
  scanf(" %d", &size);

  person people[size];
  getNameNumber(people, size);

  char personFind[STRINGCONSTRAINT];
  puts("Who do you want to find?");
  printf("Person to find: ");
  scanf(" %[^\n]s", personFind);

  printNumber(people, size, personFind);

  return 0;
}

// Get name and numbers for the array
void getNameNumber(person people[], int size) {
  unsigned int i;
  for (i = 0; i < size; i++) {
    printf("Name: ");
    scanf(" %[^\n]s", people[i].name);
    printf("Numbers: ");
    scanf(" %[^\n]s", people[i].numbers);
  }
}

// Find the person and print the numbers
void printNumber(person people[], int size, char personFind[]) {
  unsigned int i;
  int flag = 1;

  for (i = 0; i < size; i++) {
    flag = strcmp(people[i].name, personFind);
    if (flag == 0) {
      puts("Found!");
      printf("You can call %s at %s.\n", personFind, people[i].numbers);
    }
  }
}


texts:
I have many unrelated tasks to execute and I have to get the result of the task so I use the FutureTask.

I submit all the task to the thread pool and use a map to keep track of the task and the FutureTask to get the result. And two for loops are used:

ExecutorService es = Executors.newCachedThreadPool();

Map<Task, FutureTask<Boolean>> taskResultMap = new HashMap<Task, FutureTask<Boolean>>();

for (int i = 0 ; i < callerList.size(); i++)
{
    Caller caller = callerList.get(i);
    FutureTask<Boolean> futureTask = new FutureTask<Boolean>(caller);
    es.submit(futureTask);
    taskResultMap.put(caller, futureTask);

}

for (int i = 0 ; i < callerList.size(); i++)
{
    Caller caller = callerList.get(i);
    FutureTask<Boolean> task = taskResultMap.get(caller);
    System.out.println(caller.getName() + " result is " + task.get());
}

Is this a good practice about ExecutorService and FutureTask? I'm new in multi-thread programming.

Another two questions:

How can I get the execution time for each thread? print in caller iteself or outside caller?
When I invoke the FutureTask.get() method in for loop, I think the get() method may be blocked, If a time-consuming task is running, I can't get the result of the next task but to wait. How could I do this?


texts: Compare performance of Concurrent Dictionary with Dictionary+Locks

texts:
I wanted to measure the performance of Concurrent Dictionary vs Dictionary+Locks in a multithreaded environment. So I created my own SyncDict class of type<int,int[]>. Whenever there is a key match, it adds the int[] array value to itself, it also locks the whole dictionary with ReaderWriterLockSlim while updating the value.

I replicated the whole code through Concurrent Dictionary and I am using AddOrUpdate() method.
Whole console app code can be found here https://dotnetfiddle.net/1kFbGy Just copy paste the code in console app to run. It will not run fiddle

After running both codes with the same inputs I see a considerable amount of difference in running time. For example for one particular run on my machine Concurrent dictionary took 4.5 seconds vs SyncDict took less than 1 second.

I would like to know any thoughts / suggestions explaining the above running time. Is there anything wrong am I doing here?

 class SyncDict<TKey>
    {
        private ReaderWriterLockSlim cacheLock;
        private Dictionary<TKey, int[]> dictionary;
        public SyncDict()
        {
            cacheLock = new ReaderWriterLockSlim();
            dictionary = new Dictionary<TKey, int[]>();
        }

        public Dictionary<TKey, int[]> Dictionary
        {
            get { return dictionary; }
        }

        public int[] Read(TKey key)
        {
            cacheLock.EnterReadLock();
            try
            {
                return dictionary[key];
            }
            finally
            {
                cacheLock.ExitReadLock();
            }
        }

        public void Add(TKey key, int[] value)
        {
            cacheLock.EnterWriteLock();
            try
            {
                dictionary.Add(key, value);
            }
            finally
            {
                cacheLock.ExitWriteLock();
            }
        }

        public AddOrUpdateStatus AddOrUpdate(TKey key, int[] value)
        {
            cacheLock.EnterUpgradeableReadLock();
            try
            {
                int[] result = null;
                if (dictionary.TryGetValue(key, out result))
                {
                    if (result == value)
                        return AddOrUpdateStatus.Unchanged;
                    else
                    {
                        cacheLock.EnterWriteLock();
                        try
                        {
                            Parallel.For(0, value.Length,
                            (i, state) =>
                            {
                                result[i] = result[i] + value[i];
                            });
                        }
                        finally
                        {
                            cacheLock.ExitWriteLock();
                        }
                        return AddOrUpdateStatus.Updated;
                    }
                }
                else
                {
                    Add(key, value);
                    return AddOrUpdateStatus.Added;
                }
            }
            finally
            {
                cacheLock.ExitUpgradeableReadLock();
            }
        }

        public void Delete(TKey key)
        {
            cacheLock.EnterWriteLock();
            try
            {
                dictionary.Remove(key);
            }
            finally
            {
                cacheLock.ExitWriteLock();
            }
        }

        public enum AddOrUpdateStatus
        {
            Added,
            Updated,
            Unchanged
        };
    }


texts:
I am developing a web-application and I am encountering some trouble :
I have some tabs and each one contains input forms with different values. I need to switch beetween these tabs and see the different contents of each one.
So I thought I could create objects for every tab , but I have to name them , I use something like this :

Object

var step = function(params){
this.id = randID();
this.titolo = null;
this.descrizione = null;
this.dmax = null;
$.extend(this,params);
// this.render();
}

When the button is clicked , it instance a new object

$("#btn").on('click',function(){
stepA = new step({
"titolo" : $("#titolo").val(),
"descrizione" : $("#descrizione").val()
})
var id = stepA.id;
var nome = "step"+id;
console.log(nome);
nome = stepA;
console.log(nome);
});

As you can see , I used this solution to name every step object with his ID

var id = stepA.id;
var nome = "step"+id;
console.log(nome);
nome = stepA;
console.log(nome);

otherwise I think is a really bad solution. Notice that I will call every object by his id clicking on the tabs.

Hope you can help me improve it, thank you guys.


texts: C++ implementation of recursive_shared_mutex

texts:
I know it's bad to even use recursive_mutex because of its poor performance, let alone a recursive shared mutex. However, I'm doing this just to practice. Any suggestion will be appreciated!

recursive_shared_mutex.hpp

#pragma once

#include <optional>
#include <shared_mutex>
#include <thread>

class recursive_shared_mutex : private std::shared_mutex {
public:
    recursive_shared_mutex();
    ~recursive_shared_mutex() = default;

    void lock();
    bool try_lock();
    void unlock();

    /*
    * std::shared_mutex's shared locking is recursive by default.
    */
    void lock_shared() {
        std::shared_mutex::lock_shared();
    }

    bool try_lock_shared() {
        return std::shared_mutex::try_lock_shared();
    }

    void unlock_shared() {
        std::shared_mutex::unlock_shared();
    }

private:
    std::mutex mtx_;
    std::condition_variable cv_;
    std::optional<std::thread::id> writer_id_;
    std::size_t writer_cnt_;
};

recursive_shared_mutex.cpp

#include "recursive_shared_mutex.hpp"

recursive_shared_mutex::recursive_shared_mutex()
    : std::shared_mutex(),
      mtx_(),
      cv_(),
      writer_id_(),
      writer_cnt_(0)
{
}

void recursive_shared_mutex::lock() {
    std::thread::id this_id = std::this_thread::get_id();
    std::unique_lock<std::mutex> ulock(mtx_);
    if (this_id == writer_id_) {
        // Same thread/writer trying to acquire the shared_mutex again.
        // Simply increase writer count.
        ++writer_cnt_;
    }
    else {
        // Another writer or no writer is holding the shared_mutex.
        // It's also likely that some readers are holding
        // the shared_mutex.
        if (writer_id_.has_value()) {
            // If another writer is holding the mutex,
            // waiting for it to release.
            cv_.wait(ulock, [this] { return writer_cnt_ == 0; });
        }
        std::shared_mutex::lock();
        writer_id_ = this_id;
        writer_cnt_ = 1;
    }
}

bool recursive_shared_mutex::try_lock() {
    // TODO
    return false;
}

void recursive_shared_mutex::unlock() {
    std::unique_lock<std::mutex> ulock(mtx_);
    if (!writer_id_.has_value()) {
        // No writer is holding the shared_mutex.
        // Call unlock() anyway. UB expected.
        std::shared_mutex::unlock();
    }
    else {
        // At this point, the shared_mutex must be held by a writer
        // and writer_cnt_ must be greater than 0.
        --writer_cnt_;
        if (writer_cnt_ == 0) {
            // If writer count becomes 0, release the
            // underlying shared_mutex.
            // It's likely that we are unlocking in a
            // different thread from the one where the shared_mutex
            // has been acquired.
            // Call unlock() anyway. UB expected.
            std::shared_mutex::unlock();
            writer_id_.reset();
            // Manually unlock to let cv notify.
            ulock.unlock();
            cv_.notify_one();
        }
    }
}


texts: Implementation of Prim's Algorithm to find minimum spanning tree

texts:
What is the time complexity of the following code? I'm implementing Prim's Algorithm with adjacency matrix representation of the graph and priority queue.

from graph import adj_mtx, AP
import heapq as hq

lv, visited, h = float('inf'), {}, []  # O(n), lv stands for 'large_value' used to determine infinite distance, h is the heap

def prims_mst(adj_matrix, src):
    hq.heappush(h, (0, (src, None)))  # O(logn)
    curr_dist = {item.value: lv if item.value != src else 0 for item in AP}  # curr_dist stands for current distances from the source

    while len(h) != 0:
        # print h[0]
        curr_nd = hq.heappop(h)[1][0]  # first element of the tuple is the value, second is the node  # O(1)
        visited[curr_nd] = True  # O(1)
        for nd, dst in enumerate(adj_matrix[src]):  # O(n) -> n is the number of nodes
            if nd not in visited and curr_dist[nd] > curr_dist[curr_nd] + adj_matrix[curr_nd][nd]:
                curr_dist[nd] = curr_dist[curr_nd] + adj_matrix[curr_nd][nd]
                hq.heappush(h, (curr_dist[nd], (nd, curr_nd)))  # O(logn)
        print h


texts: Find and replace consecutive string values in a list

texts:
Suppose I have a list that holds a list of column names:

columns = [
        'Village Trustee V Belleville', 'Unnamed: 74', 'Unnamed: 75',
        'Village President V Black Earth', 'Village Trustee V Black Earth',
        'Unnamed: 78', 'Unnamed: 79', 'Village President V Blue Mounds',
        'Village Trustee V Blue Mounds', 'Unnamed: 82',
        'Village Trustee V Cottage Grove', 'Unnamed: 94', 'Unnamed: 95',
        'Village President V Cross Plains', 'Village Trustee V Cross Plains',
        'Unnamed: 98', 'Unnamed: 99'
        ]

And I want to find all Unnamed: XX values and replace them consecutively with the first valid string. For example:

Input:
'Village Trustee V Belleville', 'Unnamed: 74', 'Unnamed: 75'

Output:
'Village Trustee V Belleville', 'Village Trustee V Belleville', 'Village Trustee V Belleville'

For this I have a function:

def rename_unnamed(columns):
    current_value = None
    pattern = re.compile(r'^Unnamed:\s\d+$')
    for column_index, column in enumerate(columns):
        if re.match(pattern, column):
            columns[column_index] = current_value
        else:
            current_value = column
    return columns

Which does the job pretty fine, however, I was wondering if this could be improved and/or simplified, as this looks like a mouthful for rather a simple task.


texts: A ValueService is used to share data among objects

texts:
I have C# code that makes some calculations for physical models. I have this project class:

public class Project
{
    public IEnumerable<GenericComponent> components;
    private ValueService valueService = new ValueService(Guid.NewGuid().ToString());

    public Project(string norm)
    {
        components = FactoryGenerator.GetFactory(norm).GetComponents();
    }

    public void SetValues(IEnumerable<ValueViewModel> values)
    {
        values.ToList().ForEach(t => valueService.SetValue(t.Key, t.Value));
    }

    public bool Valid(string componentName)
    {
        return components.Where(s => s.Title == componentName).Single().Valid();
    }
}

That creates smaller components that do some calculations. E.g.

public class CrossSectionComponent: GenericComponent
{
    public CrossSectionComponent(ValueService valueService): base(valueService)
    {

    }

    ICrossSectionViewModel crossSection;

    double valuex;

    override
    public bool Valid()
    {
        return valuex > valueService.GetValue<double>("valuey") ? true : false;
    }
}

Each component has a list of values with keys that are unique over the entire model. At the moment I use a ValueService, where each component registers its values with the respective keys. The ValueService can be used to get values from other components in order to do validation calculations.

    private string projectUUID;
    private List<ValueViewModel> values = new List<ValueViewModel>();

    public ValueService(string projectUUID)
    {
        this.projectUUID = projectUUID;
    }

    public void RegisterValue(string key)
    {
        if (!this.hasKey(key))
        {
            values.Add(new ValueViewModel()
            {
                Key = key,
            });
        }
    }

    public void SetValue(string key, object value)
    {
        if (this.hasKey(key))
            this.values.Find(t => t.Key == key).Value = value;
    }

    public T GetValue<T>(string key)
    {
        return (T)values.Find(t => t.Key == key)?.Value;
    }

    private bool hasKey(string key)
    {
        return values.Any(t => t.Key == key);
    }
}

This is part of a stateless API, i.e. for each HTTP request (at the moment) a new Project object is generated and used to make validations.

My API controller looks like this:

    public IActionResult postData([FromRoute] string norm, [FromRoute] string component, [FromBody] ValueViewModel[] data)
    {
        Project project = new Project(norm);
        project.SetValues(data);
        return Ok(project.Valid(component));
    }

The validity of one component does depend in most cases on values of other components. If one wants to check the validity of a components, the use sends a list of values (ValueViewModel[] data), the server creates the project where all available values are stored. Then IF AND ONLY IF all required values are set, the validity can actually be calculated. If not all values are provided inside the "data list", then the validity of said component is false. So there are two cases where component.Valid() => false

The calculations yield false
Not all values have been submitted

My concerns are

When the project continues, the number of components will get quite large, I fear there will be a performance issue if for each request the whole set of components needs to be generated --> should I use LazyLoading?
Is the ValueService approach good to share data between the components? Is there a better solution?


texts:
Here's my method used for parsing a ByteArray into multiple frames of CAN interface. It works now correctly but it's somewhat ugly in my opinion.

It's written in Kotlin with use of RxJava2

private fun parseCanFrameBytes(frame: ByteArray): Observable<CanFrame> {
    return Observable.create { emitter ->
        var index = 0
        while (index < frame.size) {
            val byte = frame[index]

            if(byte xor IFM_FRAME_HEADER == BYTE_ZERO) {
                try {
                    val ifmIndex = index
                    val length = frame[ifmIndex + 1].toInt()
                    val dlc1 = length - 3
                    val dlc2 = length - 5
                    val command = frame[ifmIndex + 2].toInt()
                    val data = frame.copyOfRange(ifmIndex + 3, ifmIndex + 2 + length)
                    val isChecksumValid = frame[ifmIndex + 2 + length].toInt() == frame.copyOfRange(ifmIndex, ifmIndex + 2 + length).calculate8XorChecksum()
                    if (!isChecksumValid) {
                        index++
                        continue
                    }

                    val canFrame = when (command) {
                        0 -> {
                            val pgn = data.copyOfRange(0, 2).toHexString()
                            CanFrame(
                                    type = "DLC1",
                                    pgn = pgn,
                                    id = pgn,
                                    value = data.copyOfRange(2, 2 + dlc1).toHexString(),
                                    ts = timestamp(),
                                    data = frame.copyOfRange(ifmIndex, ifmIndex + 4 + length).toHexString()
                            )
                        }
                        2 -> {
                            val pgn = data.copyOfRange(0, 4).toHexString()
                            CanFrame(
                                    type = "DLC2",
                                    pgn = pgn,
                                    id = pgn,
                                    value = data.copyOfRange(4, 4 + dlc2).toHexString(),
                                    ts = timestamp(),
                                    data = frame.copyOfRange(ifmIndex, ifmIndex + 4 + length).toHexString()
                            )
                        }
                        else -> null
                    } ?: continue

                    emitter.onNext(canFrame)
                    index = ifmIndex + length + 4
                } catch (ex: IllegalArgumentException) {
                    index++
                }
            } else {
                index++
            }
        }
    }
}


texts: poh-tay-toh poh-tah-toh, does writing the same code a different way affect readability?

texts:
I have a particular if statement that could be written in different ways, and I'm curious as to whether there's any significant difference in readability that I should prefer one over the other:

The flow can be boiled down to

if string does not begin with '_'
    do stuff
else
    throw error

Some different ways of writing the if condition:

str[0] !== '_'
str.indexOf('_')
!/^_/.test(str)
/^[^_]/.test(str)


texts:
I'm learning Python and Gtk so I decided to write a simple application that will take some image let user annotate with a pen and send results through email or save to a file.

So far I have the skeleton of the interface:

from gi.repository import Gtk
import os, sys

class GUI:

image = 0;

def __init__(self):

    self.window = Gtk.Window()
    self.window.set_title ("Imagine")
    self.window.connect_after('destroy', self.destroy)
    self.window.set_border_width(10)
    self.window.set_default_size(400, 200)

    self.window.vbox = Gtk.Box()
    self.window.vbox.set_spacing (5)
    self.window.vbox.set_orientation (Gtk.Orientation.VERTICAL)
    self.window.add (self.window.vbox)

    self.window.hbox = Gtk.ButtonBox()
    self.window.vbox.add(self.window.hbox)

    self.window.new_button = Gtk.Button(label="New")
    self.window.new_button.connect("clicked", self.on_new_clicked)
    self.window.hbox.pack_start(self.window.new_button, False, False, 0)

    self.window.save_button = Gtk.Button(label="Save")
    self.window.save_button.connect("clicked", self.on_save_clicked)
    self.window.hbox.pack_start(self.window.save_button, False, False, 0)

    self.window.email_button = Gtk.Button(label="eMail")
    self.window.email_button.connect("clicked", self.on_email_clicked)
    self.window.hbox.pack_start(self.window.email_button, False, False, 0)

    self.window.pen_button = Gtk.Button(label="Pen")
    self.window.pen_button.connect("clicked", self.on_pen_clicked)
    self.window.hbox.pack_start(self.window.pen_button, False, False, 0)

    self.image = Gtk.Image()
    self.window.vbox.pack_start (self.image, False, False, 0)

    self.window.show_all()

def on_save_clicked(self, widget):
    print("fixme: Save Clicked")

def on_email_clicked(self, widget):
    print("fixme: eMail Clicked")

def on_pen_clicked(self, widget):
    print("fixme: Pen Clicked")

def on_new_clicked (self, button):
    print("fixme: New Clicked")#code below is just to test the image panel works
    dialog = Gtk.FileChooserDialog ("Open Image", button.get_toplevel(), Gtk.FileChooserAction.OPEN);
    dialog.add_button ("Cancel", 0)
    dialog.add_button ("Ok", 1)
    dialog.set_default_response(1)

    filefilter = Gtk.FileFilter ()
    filefilter.add_pixbuf_formats ()
    dialog.set_filter(filefilter)

    if dialog.run() == 1:
        self.image.set_from_file(dialog.get_filename())

    dialog.destroy()

def destroy(self, window):
    Gtk.main_quit()

class Controls:

    def send_email (self):
        print("fixme: Send Email")

    def pen_controls (self):
        print("fixme: Pen Controls")

    def on_save_clicked(self, widget):
        print("fixme: Save Clicked")

def main () :
    win = GUI()
    Gtk.main()

if __name__ == "__main__":
    sys.exit(main())

Can you please review my code and let me know what could have been done better? Any documentation along will be highly appreciated.

Also, my main doubts are:

Is my code compliant with the object oriented Python?
Will vbox and hbox exist in two places in my window because I created them in self.window and then added to the window again with self.window.add?
Should I have created vbox and hbox outside of window and then used the window.add command?
The idea is to use Gtk3 PyGObject and Python 3


texts:
The following code is used to convert a base-10 number to a base-N number, where N is a length of a given alphabet that contains characters of which a base-N number can consist.

The number to convert is always increasing, like from 1 to 45789, not from 536 to 13. The resulting number may not contain zero as first digit, so I need to carry the one.

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

typedef unsigned long long ull;

void conv(ull num, const char *alpha, char *word, int base){
    while (num) {
       *(word++)=alpha[(num-1)%base];
       num=(num-1)/base;
    }
}

int main(){
   ull nu;
   const char alpha[]="abcdefghijklmnopqrstuvwxyzOSR34";

   /* "OSR43" was added to show that the letters of alpha
      are NOT in alphabetical order */

   char *word=calloc(30,sizeof(char));
   // word always contains null-terminator
   int base=(int)strlen(alpha);
   for (nu=1;nu<=1e8;++nu) {
       conv(nu,alpha,word,base);
       printf("%s\n",word);
   }
   return 0;
}

This code's working fine but I need to speed it up as much as possible. How do I do it?


texts:
I'm receiving a certain string value over the network and I need to call a function based on that value.

So this is more or less what I have at the moment:

String methodName = soapObj.getMethodName();

switch(methodName) {
    case "getTemperature":
        getDeviceTemp();
        break;
    case "getBrightness":
        getScreenBrightness();
        break;
    ...
}

And.. there are about 60 to 70 methods supported like that.

What's the most elegant way of handling above situation without using reflection? (The app is moderately constrained performance-wise).


texts:
https://github.com/BingXiong1995/react-flv-player/blob/master/lib/wrapper/ReactFlvPlayer.js
import React, { Component } from 'react';
import flvjs from './flv.min';
import PropTypes from 'prop-types';

class ReactFlvPlayer extends Component {
  constructor(props) {
    super(props);
    this.myRef = React.createRef();
    this.flvPlayerRef = element => {
      this.flvPlayerRef = element;
    };
  }

  componentDidMount() {

    const {type , url, isLive, enableStashBuffer, stashInitialSize, hasAudio, hasVideo, handleError, enableWarning, enableError} = this.props;

    // 组件挂载后，拿到Ref进行操作
    if (flvjs.isSupported()) {
      const flvPlayer = flvjs.createPlayer({
        type,
        isLive,
        url,
        hasAudio,
        hasVideo
      },{
        enableStashBuffer,
        stashInitialSize
      });

      flvjs.LoggingControl.enableError = false;
      flvjs.LoggingControl.enableWarn = enableWarning;

      flvPlayer.attachMediaElement(this.myRef.current); // 将这个DOM付给第三方库
      flvPlayer.load();
      flvPlayer.play();
      flvPlayer.on('error', (err)=>{
        // console.log(err);
        handleError(err);
      });
    }
  }

  render() {
    const { height, width, isMuted,showControls } = this.props;
    return (
      <div>
        <video
          controls={showControls}
          muted={{isMuted}}
          ref={this.myRef}
          style={{height, width}}
        />
      </div>
    );
  }
}

ReactFlvPlayer.propTypes = {
  type: PropTypes.string,
  url: PropTypes.string.isRequired,
  isLive: PropTypes.bool,
  showControls: PropTypes.bool,
  hasAudio: PropTypes.bool,
  hasVideo: PropTypes.bool,
  enableStashBuffer: PropTypes.bool,
  stashInitialSize: PropTypes.number,
  height: PropTypes.string,
  width: PropTypes.string,
  isMuted: PropTypes.bool,
  enableWarning: PropTypes.bool,
  enableError: PropTypes.bool,
  handleError: PropTypes.func
};

ReactFlvPlayer.defaultProps = {
  type: 'flv',
  isLive: true,
  hasAudio: true,
  hasVideo: true,
  showControls: true,
  enableStashBuffer: true,
  stashInitialSize: 128,
  height: '100%',
  width: '100%',
  isMuted: false,
  handleError: (err)=>{console.log(err)},
  enableWarning: false,
  enableError: false
};

export default ReactFlvPlayer;

Wrote some wrapper a long time ago. I am wondering if I could have done it in a better way. What are some of the improvements I could make or issues with the code. Thanks.


texts: Access an associative array value given an array of keys in PHP

texts:
I have an object which stores an app configuration in an associative array, as this:

[
  'Database' =>
    [
      'DB_SERVER' => 'localhost'
      'DB_NAME' => 'adbname'
      'DB_USER' => 'theuser'
      'DB_PASS' => 'thepass'
    ]
]

It could have more than two levels.

And a function, get(), which receives a variable number of arguments and returns the configuration value for this parameters. For example, Config::get('Database','DB_PASS') will return 'thepass'.

This is an excerpt of the code:

class Config
{
    protected static $values;

    ...

    public static function get()
    {
        $val = &self::$values;
        $argList = func_get_args();
        for ($i = 0; $i < count($argList); $i++) {
            $val = &$val[$argList[$i]];
            if(empty($val)) break;
        }
        return (is_null($val)?"":$val);
    }
}

Is there a more elegant / efficient way of accessing the value?

(note: I posted the question here but I was notified this web was a proper place to ask)


texts: Failing fast by raising an exception with a custom message

texts:
This is a script that utilizes lazy exception throwing. What I do is decorate a function that might throw an exception to throw an exception with a formatted string.

from functools import wraps

def on_fail(message):
    def on_fail_dec(func):
        @wraps(func)
        def on_fail_wrapper(*args, **kw):
            ret = None
            success = True
            message_addon = ""
            try:
                ret = func(*args, **kw)
            except Exception as e:
                success = False
                message_addon = str(e)
            # raise Exception if failed
            if not success:
                raise Exception("{0}\nBecause: {1}".format(message, message_addon))

            return ret
        return on_fail_wrapper
    return on_fail_dec

# Usage :

@on_fail("Division Failed")
def divide(a, b):
    return a / b

test_cases = [(1, 2), (20, 5), (3, 0)]

for a,b in test_cases:
    print("Trying to divide {0} from {1}".format(a, b))
    print(divide(a, b))

Output:

Trying to divide 1 from 2
0.5
Trying to divide 20 from 5
4.0
Trying to divide 3 from 0
Traceback (most recent call last):
  File "<string>", line 30, in <module>
  File "<string>", line 16, in on_fail_wrapper
Exception: Division Failed
Because: division by zero


texts: Neural Network Written in Python is Extremely Slow

texts:
I coded a basic feedforward neural network with all pure python with the exception of numpy in order to better understand how neural networks work. It works, but the only problem is it is extremely slow, and I have no idea how to fix it. The neural network looks like this:
import numpy as np

from digits import x_train

np.random.seed(0)

def leaky_relu(inputs):
    return np.maximum(0.1*inputs, inputs)

class Layer:
    def __init__(self, n_inputs, n_neurons):
        self.weights = 0.1*np.random.randn(n_inputs, n_neurons)
        self.biases = np.zeros((1, n_neurons))
        self.updated_weights = self.weights
        self.updated_biases = self.biases
        self.dc_dz = []

    def forward(self, inputs):
        self.output = leaky_relu(np.dot(inputs, self.weights) +
self.biases[0]

l1 = Layer(784, 8)
l2 = Layer(8, 128)
l3 = Layer(128, 128)
l4 = Layer(128, 64)
l5 = Layer(64, 10)

l1.forward(x_train[0].flatten())
l2.forward(l1.output)
l3.forward(l2.output)
l4.forward(l3.output)
l5.forward(l4.output)

layers = [l1, l2, l3, l4, l5]

def leaky_relu_derivative(output):
    if output > 0:
        return 1
    else:
        return 0.1

def calculate_bias(output, actual=None, dc_dcn=None):
    if dc_dcn is None:
        return leaky_relu_derivative(output) * 2 * (output - actual)
    else:
        return leaky_relu_derivative(output) * dc_dcn

def calculate_weight(output, input, actual=None, dc_dcn=None):
    if dc_dcn is None:
        return input * leaky_relu_derivative(output) * 2 * (output - actual)
    else:
        return input * leaky_relu_derivative(output) * dc_dcn

def calculate_dc_dcn(weights, dc_dz):
    #find the derivative of the cost function in respect to the current node
    return np.sum(np.multiply(weights, dc_dz))

def train(learning_rate, actual):
    prev = None
    next = None
    x = len(layers) - 1

    while x != 0:
        layer = layers[x]
        next = layers[x-1]

        if x == len(layers) - 1:
            for i in range(len(layer.output)):
                #for every node in the layer
                new_bias = calculate_bias(layer.output[i], actual[i])
                layer.dc_dz.append(new_bias)
                layer.updated_biases[0][i] -= learning_rate * new_bias
                for j in range(len(next.output)):
                    #for every weight of the current node
                    new_weight = calculate_weight(layer.output[i], next.output[j], actual[i])
                    layer.updated_weights[j][i] -= learning_rate * new_weight
            prev = layer
        else:
            for i in range(len(layer.output)):
                #for every node in the layer
                dc_dcn = calculate_dc_dcn(prev.weights[i], prev.dc_dz[:(len(prev.output))])
                new_bias = calculate_bias(layer.output[i], dc_dcn)
                layer.dc_dz.append(new_bias)
                layer.updated_biases[0][i] -= learning_rate * new_bias
                for j in range(len(next.output)):
                    #for every weight of the current node
                    new_weight = calculate_weight(layer.output[i], next.output[j], dc_dcn)
                    layer.updated_weights[j][i] -= learning_rate * new_weight
            prev = layer

        for layer in layers:
            layer.weights = layer.updated_weights
            layer.biases = layer.updated_biases

        x -= 1

I'm assuming the code isn't very efficient and probably written poorly so any constructive criticism along with how to make it faster would help.


texts: Calculate Levenshtein distance between two strings in Python

texts:
I need a function that checks how different are two different strings. I chose the Levenshtein distance as a quick approach, and implemented this function:

from difflib import ndiff

def calculate_levenshtein_distance(str_1, str_2):
    """
        The Levenshtein distance is a string metric for measuring the difference between two sequences.
        It is calculated as the minimum number of single-character edits necessary to transform one string into another
    """
    distance = 0
    buffer_removed = buffer_added = 0
    for x in ndiff(str_1, str_2):
        code = x[0]
        # Code ? is ignored as it does not translate to any modification
        if code == ' ':
            distance += max(buffer_removed, buffer_added)
            buffer_removed = buffer_added = 0
        elif code == '-':
            buffer_removed += 1
        elif code == '+':
            buffer_added += 1
    distance += max(buffer_removed, buffer_added)
    return distance

Then calling it as:

similarity = 1 - calculate_levenshtein_distance(str_1, str_2) / max(len(str_1), len(str_2))

How sloppy/prone to errors is this code? How can it be improved?


texts: BestApproximationDiv2 problem in Haskell

texts:
The task is to write a function findFraction maxDen number to find a short but good rational approximation to a decimal representation of a number — problem statement on TopCoder:

  Given a fraction F = A/B, where 0 <= A < B, its quality of approximation with respect to number is calculated as follows:

  Let S be the decimal fraction (infinite or finite) representation of F.
  Let N be the number of digits after the decimal point in number. If number has trailing zeros, all of them are considered to be significant and are counted towards N.
  If S is infinite or the number of digits after the decimal point in S is greater than N, only consider the first N decimals after the decimal point in S. Truncate the rest of the digits without performing any kind of rounding.
  If the number of digits after the decimal point in S is less than N, append trailing zeroes to the right side until there are exactly N digits after the decimal point.
  The quality of approximation is the number of digits in the longest common prefix of S and number. The longest common prefix of two numbers is the longest string which is a prefix of the decimal representations of both numbers with no extra leading zeroes. For example, "3.14" is the longest common prefix of 3.1428 and 3.1415.

  […] You are only allowed to use fractions where the denominator is less than or equal to maxDen. Find an approximation F = A/B of number such that 1 <= B <= maxDen, 0 <= A < B, and the quality of approximation is maximized. Return a String formatted "A/B has X exact digits" (quotes for clarity) where A/B is the approximation you have found and X is its quality. If there are several such approximations, choose the one with the smallest denominator among all of them. If there is still a tie, choose the one among those with the smallest numerator.

import Data.Char
import Data.List
import Data.Maybe

showResult :: (Int, Int, Int) -> String
showResult (a, b, x) = show a ++ "/" ++ show b ++ " has "
                    ++ show x ++ " exact digits"

compareDigits :: [Int] -> [Int] -> Ordering
compareDigits xs [] = EQ
compareDigits (x:xs) (y:ys) = case compare x y of
                                  EQ -> compareDigits xs ys
                                  order -> order

toDigit :: Char -> Int
toDigit c = ord c - ord '0'

preciseDivision :: Int -> Int -> [Int]
preciseDivision a b = div a' b : preciseDivision (mod a' b) b
           where a' = 10 * a

estimate :: [Int] -> Double
estimate = sum . zipWith (flip (/)) (iterate (*10) 10) .
           map fromIntegral

bestNumerator :: [Int] -> Double -> Int -> Maybe Int
bestNumerator ds p b =
    case find atLeast . map (pair ds b) $ [pivot .. ] of
        Just (a, EQ) -> Just a
        otherwise -> Nothing
    where pivot = truncate $ p * fromIntegral b
          pair ds b a = (a, compareDigits (preciseDivision a b) ds)
          atLeast (a, order) = order /= LT

bestResult :: Int -> [Int] -> Maybe (Int, Int, Int)
bestResult n ds = fromMaybe Nothing . find isJust .
                  map (toResult ds $ estimate ds) $ [1..n]
    where toResult ds p b = case bestNumerator ds p b of
                                Just a -> Just (a, b, 1 + length ds)
                                Nothing -> Nothing

findFraction' :: Int -> [Int] -> (Int, Int, Int)
findFraction' n = fromJust . last . takeWhile isJust .
                  map (bestResult n) . inits

findFraction :: Int -> String -> String
findFraction n = showResult . findFraction' n .
                 map toDigit . tail . tail

I'm most worried by bestNumerator and bestResult here. I'm using a find idiom that I invented myself, and I wonder if cleaner, more Haskelly alternatives exist.


texts:
I have a 4 list of posts. I am trying to load more posts (clones of the "original" ones) when I reach the bottom of the container.

class InfiniteScroll {
  constructor() {
    this.observer = null;
    this.isLoading = false;
    this.postsContainer = document.querySelector('#postsContainer');
    this.postsArary = postsContainer.querySelectorAll('.post');
    this.iterationCount = Number(this.postsContainer.dataset.currentPage);
    this.perPage = 5;
    this.maxCount = this.postsContainer?.dataset?.maxCount;
    this.numberOfPages = Math.ceil(this.maxCount / this.perPage);
    this.hasNextPage = this.iterationCount < this.numberOfPages;
  }

  loadMorePosts() {
    if (this.hasNextPage) {
      this.postsArary.forEach(item => {
        let postClone = item.cloneNode(true);
        this.postsContainer.appendChild(postClone);
      });
    } else {
      if (this.observer) this.observer.disconnect();
    }
  }

  getLastPost() {
    const allPosts = postsContainer.querySelectorAll('.post');
    return allPosts[allPosts.length - 1];
  }

  counter() {
    if (this.hasNextPage) {
      this.iterationCount = this.iterationCount + 1;
      this.postsContainer.dataset.currentPage = this.iterationCount;
    }

    this.postsContainer.dataset.hasNextPage = this.hasNextPage;
    this.hasNextPage = this.iterationCount < this.numberOfPages - 1;
  }

  bindLoadMoreObserver() {
    if (this.postsContainer) {
      this.observer = new IntersectionObserver((entries, observer) => {
        entries.forEach(entry => {
          if (entry && entry.isIntersecting) {
            observer.unobserve(entry.target);
            this.loadMorePosts();
            this.counter();
            if (this.hasNextPage) {
              observer.observe(this.getLastPost());
            }
          }
        });
      });

      this.observer.observe(this.getLastPost());
    }
  }

  init() {
    this.bindLoadMoreObserver();
  }
}

const infiniteScroll = new InfiniteScroll();
infiniteScroll.init();
body, body * {
  margin: 0;
  padding: 0;
}

body {
  font-family: Arial, Helvetica, sans-serif;
}

.post {
  margin: 20px;
  padding: 15px;
  border: 1px solid #ccc;
  border-radius: 5px;
}

p {
  line-height: 1.5;
}

Title 1

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Neque dolorum consequatur nostrum sapiente ipsa! Veniam, laudantium accusantium, odio maxime quo adipisci possimus enim quam, voluptate quidem animi perferendis delectus aliquam?

Title 2

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Neque dolorum consequatur nostrum sapiente ipsa! Veniam, laudantium accusantium, odio maxime quo adipisci possimus enim quam, voluptate quidem animi perferendis delectus aliquam?

Title 3

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Neque dolorum consequatur nostrum sapiente ipsa! Veniam, laudantium accusantium, odio maxime quo adipisci possimus enim quam, voluptate quidem animi perferendis delectus aliquam?

Title 4

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Neque dolorum consequatur nostrum sapiente ipsa! Veniam, laudantium accusantium, odio maxime quo adipisci possimus enim quam, voluptate quidem animi perferendis delectus aliquam?


texts:
Follow Up to my earlier question.

This is an RSS Parser intended only to parse the feed Wordpress provides (and even then, only what I actually want to display on my website). System.Linq.Xml and System.ServiceModel.Syndication are unavailable because I'm targeting .Net Core instead of the full framework.

I think I've achieved a more idiomatic implementation this time. Where can I improve? Are there opportunities to compose higher order functions that I'm missing?

Rss.fs

module Rss
    open System

    type Item = {
        Title: string
        Link: Uri
        Comments: Uri
        PublishDate: DateTimeOffset
        Description: string
    }

    type Channel = {
        Title: string
        Link: Uri
        Description: string
        LastBuildDate: DateTimeOffset
        Items: seq<Item>
    }

    type RssFeed = {
        Version: double
        Channel: Channel
    }

    open System.Xml
    let parse input =

        // Helper Functions
        let innerText (elementName: string) (node: XmlNode) =
            let element = node.Item elementName
            element.InnerText

        let titleText node =
            node |> innerText "title"

        let descriptionText node =
            node |> innerText "description"

        let elementAsDateTimeOffset elementName node =
            node |> innerText elementName |> DateTimeOffset.Parse

        let elementAsUri elementName node =
            node |> innerText elementName |> Uri

        let linkUri node =
            node |> elementAsUri "link"

        let itemNodeToItem node = {
            Title = node |> titleText
            Link =  node |> linkUri
            Comments = node |> elementAsUri "comments"
            PublishDate = node |> elementAsDateTimeOffset "pubDate"
            Description = node |> descriptionText
         }

        // Load the document and begin parsing
        let document = XmlDocument()
        document.LoadXml input

        let rss = document.DocumentElement
        let channelNode = rss.FirstChild

        let items =
            channelNode.ChildNodes
            |> Seq.cast<XmlNode>
            |> Seq.filter (fun node -> node.Name.Equals "item")
            |> Seq.map itemNodeToItem

        let channel = {
            Title = channelNode |> titleText
            Link = channelNode |> linkUri
            Description = channelNode |> descriptionText
            LastBuildDate = channelNode |> elementAsDateTimeOffset "lastBuildDate"
            Items = items
        }

        let version =
            (rss.Attributes.ItemOf "version").Value
            |> Double.Parse

        let rssFeed = {Version = version; Channel = channel}
        rssFeed

Controllers/BlogController.fs

namespace theupsyde.Controllers

open System
open System.Collections.Generic
open System.Linq
open System.Threading.Tasks
open Microsoft.AspNetCore.Mvc
open Rss
open System.Net

type BlogController () =
    inherit Controller()

    let fetchUrl url = async {
        let req = WebRequest.Create(Uri(url))
        use! resp = req.GetResponseAsync() |> Async.AwaitTask
        use stream = resp.GetResponseStream()
        use reader = new IO.StreamReader(stream)
        return reader.ReadToEnd()
    }

    let rssFeed =
        fetchUrl "https://christopherjmcclellan.wordpress.com/feed/"
        |> Async.RunSynchronously
        |> Rss.parse

    let recent =
        rssFeed.Channel.Items

    member this.Index () =
        this.View(rssFeed.Channel)

    member this.Recent() =
        this.PartialView("BlogPosts", recent)

    member this.Latest () =
        let latest =
            recent
            |> Seq.head

        this.PartialView("BlogPost", latest)


texts:
I'm writing wrapper for REST API of a service. In my wrapper I have quite simple method:

public List<string> GetAvailableSources()
{
    var request = new RestRequest($"session/{Id}");
    var sessionProto = _client.Execute<SessionPrototype>(request);
    return sessionProto.Data.InstalledSources;
}

It's really simple and just sends request to service and returns result. However, unit test for that method is huge:

[Test]
public void GetAvailableSources_MustReturnItemsFromResponse()
{
    const long sessionId = 123;

    var clientMock = new Mock<IRestClient>();

    clientMock.Setup(x => x.Execute<SessionPrototype>(It.IsAny<IRestRequest>()))
        .Returns(new RestResponse<SessionPrototype>()
        {
            Data = new SessionPrototype
            {
                Id = sessionId,
                InstalledSources = new List<string>
                {
                    "Scanner1",
                    "Scanner2"
                }
            },
            StatusCode = HttpStatusCode.OK
        }).Verifiable();

    var session = new Session(sessionId, clientMock.Object);
    var sources = session.GetAvailableSources();
    Assert.That(sources, Has.Count.EqualTo(2));
    Assert.That(sources[0], Is.EqualTo("Scanner1"));
    Assert.That(sources[1], Is.EqualTo("Scanner2"));
    clientMock.Verify(
        m => m.Execute<SessionPrototype>(
            It.Is<IRestRequest>(
                r => r.Resource == $"session/{sessionId}" && r.Method == Method.GET)));
}

Is there a way for simplifying tests for REST API?


texts: Many nested session verification conditions

texts:
Can someone give me advice on cleaning this code? It's more messy then I expected. I got like 10-15 more ifs to be added.

I've thought of adding the error messsages in methods and then just check if method returns true, or false it'd throw error..

    if( isset($_SESSION["access_token"], $_SESSION["user_email"], $_SESSION["username"]) ){
    if($GoogleAccess->does_account_exist){
        if(!$GoogleAccess->is_g_acc_banned()){
            if(!$GoogleAccess->is_ip_banned()){
                if(!$GoogleAccess->is_ip_blacklisted($blacklist_ips)){
                    if($GoogleAccess->is_user_acc_verified()){
                        header("location: ../member.php");
                    }else{
                        $error = "Please verify your account";
                    }
                }else{
                    $error = "This IP's blacklisted.";
                }
            }else{
                $error = "Your IP's been banned";
            }

        }else{
            $error = "This account has been banned";
        }
    }else{
        //mean doesn't have account. Register user and send verification to email
    }


texts: Break an array into n equal or almost-equal sized sublists

texts:
I want to break a larger list into $num-lists smaller lists. I thought that there might be an option for rotor that would already do it, but I haven't found one.

#!/bin/env perl6

my @a = 1 .. 13;

my @lists = fair-lists(@a, 3);

say @$_ for @lists;

sub fair-lists ( @array, $num-lists )
{
    my $num-elements = @array.elems;
    my $num-large    = $num-elements % $num-lists; # remainder
    my $num-small    = $num-lists - $num-large;

    my $small-size = floor($num-elements/$num-lists);
    my $large-size = $small-size + 1; # same as ceiling($num-element/$num-lists);

    my $first-large = ($num-small * $small-size);
    my $last-small  = $first-large - 1;

    my @first-sublist  = @array[ 0 .. $last-small];
    my @second-sublist = @array[ $first-large .. $num-elements - 1 ];

    my @small-lists = @first-sublist.rotor($small-size);
    my @large-lists = @second-sublist.rotor($large-size);
    my @all-lists   = (@small-lists, @large-lists).flat;
    return @all-lists;
}

Is there a more concise and more clear way to do this in Perl 6?


texts:
Could someone help me shorten this code, please?

while cd == "monday":
    if cp == "0":
        for v in mon.values():
            print(v)
            break
        break
    elif cp == "1":
        print(mon[1])
        break
    elif cp == "2":
        print(mon[2])
        break
    elif cp == "3":
        print(mon[3])
        break
    elif cp == "4":
        print(mon[4])
        break
    elif cp == "5":
        print(mon[5])
        break
    cd = False

while cd == "tuesday":
    if cp == "0":
        for v in tues.values():
            print(v)
            break
        break
    elif cp == "1":
        print(tues[1])
        break
    elif cp == "2":
        print(tues[2])
        break
    elif cp == "3":
        print(tues[3])
        break
    elif cp == "4":
        print(tues[4])
        break
    elif cp == "5":
        print(tues[5])
        break
    cd = False


texts:
Uuencoding is historically used to encode emails. Instructions for creating a Uuencoder are:

Start with 3 bytes from the source, 24 bits in total.
Split into 4 6-bit groupings, each representing a value in the range 0 to 63: bits (00-05), (06-11), (12-17) and (18-23).
Add 32 to each of the values. With the addition of 32 this means that the possible results can be between 32 (" " space) and 95 ("_" underline). 96 ("`" grave accent) as the "special character" is a logical extension of this range.
Output the ASCII equivalent of these numbers.

import platform

def dec_to_bin(int):
    return bin(int).replace('0b', '')

def uuencode(string, filename):
    if platform.system() == 'Windows':
        mode = '644'
    elif platform.system() == 'Linux':
        mode = '744'

    trail = 'begin ' + mode + ' ' + filename + '\n'
    string_values = []
    char_list = list(string)
    bytes = ''

    for char in char_list:
        string_values.append(ord(char))

    for ascii in string_values:
        bytes += dec_to_bin(ascii)

    three_byte = [bytes[p:p+24] for p in range(0, len(bytes), 24)]
    if len(three_byte[-1]) < 24:
        three_byte[-1] += (24 - len(three_byte[-1])) * '0'

    four_six_bits = [three_byte[n][m:m+6] for n in range(len(three_byte)) for m in range(0, 24, 6)]
    four_six_bits = [four_six_bits[k:k+4] for k in range(0, len(four_six_bits), 4)]

    decimal_list = []
    for x in range(len(four_six_bits)):
        for z in range(4):
            decimal_list.append(int(four_six_bits[x][z], 2))

    for index, decimal in enumerate(decimal_list):
        decimal_list[index] += 32

    encoded_chars = [chr(decimal_list[o]) for o in range(len(decimal_list))]

    length_char = chr(len(encoded_chars) + 32)
    trail += length_char

    for newchar in encoded_chars:
        trail += newchar

    trail += '\n' + '`' + '\n' + 'end' + '\n'

    return trail


texts: Binary search for random access iterators in C++

texts:
I wrote a generic binary search routine in C++ for ranges that are specified by random access iterators. In case the user inputs a range with non-random access iterators, an exception will be thrown. My code is below:

binary_search.h

#ifndef CODERODDE_BINARY_SEARCH_H
#define CODERODDE_BINARY_SEARCH_H

#include <iterator>

namespace coderodde {

    template<typename RandomIt, typename T>
    RandomIt search(RandomIt begin,
                    RandomIt end,
                    T value,
                    std::random_access_iterator_tag)
    {
        RandomIt save_end = end;

        while (begin != end)
        {
            RandomIt middle = begin + ((end - begin) >> 1);

            if (value < *middle)
            {
                end = middle;
            }
            else if (value > *middle)
            {
                begin = middle + 1;
            }
            else
            {
                return middle;
            }
        }

        return save_end;
    }

    template<typename NonRandomIt, typename T>
    NonRandomIt search(NonRandomIt begin,
                       NonRandomIt end,
                       T value,
                       std::input_iterator_tag)
    {
        throw std::runtime_error(
                   "coderodde::search requires random access iterators.");
    }

    template<typename It, typename T>
    It search(It begin, It end, T value)
    {
        return search(begin, end, value, typename std::iterator_traits<It>::iterator_category());
    }
}

#endif /* CODERODDE_BINARY_SEARCH_H */

And the demonstration code is:

#include <iostream>
#include <iterator>
#include <list>
#include <stdexcept>
#include <vector>
#include "binary_search.h"

using coderodde::search;
using std::boolalpha;
using std::cerr;
using std::cout;
using std::end;
using std::endl;
using std::list;
using std::runtime_error;
using std::vector;

int main() {
    cout << "int array:" << endl;
    int arr[] = { 1, 3, 4, 4, 5, 7, 10, 15, 23 };
    size_t length = sizeof(arr) / sizeof(arr[0]);
    int* p = search(arr, arr + length, 1);
    cout << "The 1st iterator points to: " << *p << endl;
    p = search(arr, arr + length, 7);
    cout << "The 2nd iterator points to: " << *p << endl;
    p = search(arr, arr + length, 23);
    cout << "The 3rd iterator points to: " << *p << endl;

    p = search(arr, arr + length, 8);

    cout << "8 not found: " << boolalpha << (p == end(arr)) << endl;

    cout << "int vector:" << endl;
    vector<int> vec = { 1, 3, 4, 4, 5, 7, 10, 15, 23 };
    vector<int>::iterator it = search(vec.begin(), vec.end(), 1);
    cout << "The 1st iterator points to: " << *it << endl;
    it = search(vec.begin(), vec.end(), 7);
    cout << "The 2nd iterator points to: " << *it << endl;
    it = search(vec.begin(), vec.end(), 23);
    cout << "The 3rd iterator points to: " << *it << endl;

    p = search(arr, arr + length, 8);

    cout << "8 not found: " << boolalpha << (p == end(arr)) << endl;

    try {
        list<int> lst = { 1, 3, 4, 4, 5, 7, 10, 15, 23 };
        search(lst.begin(), lst.end(), 4);
    } catch (runtime_error& err) {
        cerr << err.what() << endl;
    }
}

The output is:

int array:
The 1st iterator points to: 1
The 2nd iterator points to: 7
The 3rd iterator points to: 23
8 not found: true
int vector:
The 1st iterator points to: 1
The 2nd iterator points to: 7
The 3rd iterator points to: 23
8 not found: true
coderodde::search requires random access iterators.

As always, any critique is much appreciated.


texts:
I made this to track my variable changes while I experiment in Python. I tend to use the same variable names and fit different values to them so it's nice to be able to see what I've tried, especially if I'm constructing a huge code block.

I copy text onto my clipboard before starting this code.The code asks for the variables to track, then creates a Regex fitting my usual variable definition for each variable:

' variable_name = '

It then searches text pasted by Pyperclip with re.findall to create a list of bindings for each name, which is then written to an Excel spreadsheet for easier reading of the changes to definitions over time.

import re, pyperclip, openpyxl
wb= openpyxl.Workbook()
ws = wb.active

Setting up - importing modules, creating a new workbook

print("Please input definitions/name bindings you want to track, separated"
      " by ', '")
names = input().split(', ')

Creating a list of names to track

regexlist = []
for name in names:
    regexlist.append(re.compile(r'(?<=\s' + name + r'\s=\s).*'))

Making a list of Regexes that will be used to search for the text following variable definitions. Note each Regex in the list should share the same index as
the name in the list 'names'.

text = pyperclip.paste()
c = 1          # column number, changes with iteration
for i, regex in enumerate(regexlist):
    changelist = regex.findall(text)
    ws.cell(row=1, column=c, value=names[i]) # Header is variable name
    for i, binding in enumerate(changelist):
        ws.cell(row=i+2, column=c, value=binding)
    c += 1     # move to next column for next variable
print('variable tables created, input filename as filename.xlsx')
file = input()
wb.save(file)

The eventual goal is to move towards having it read from the files (sys.stdout? for example) but at this stage I don't have the capability to do it, nor the need so I'm just looking to fine tune this in case I've missed out any possibilities.


texts:
What do you think of my auto clicker script? Even though it's just for my own personal use, I'm thinking of using PyInstaller to turn it into an exe, so I know how to do that in the future.

import pyHook, threading
import tkinter as tk
from pyautogui import click

t1token = 0
threadsRun = 1
ac = 0

def simplegui():

    def sunken():
        """keep startButton lowered and disabled.
            hook keyboard, start clicks function"""
        global threadsRun
        startButton.config(relief=tk.SUNKEN, state=tk.DISABLED,
                           text="r=stop listening")
        hm.HookKeyboard()
        threadsRun = 1
        t1 = threading.Thread(target=clicks, args=(startButton,),
                              name='clicks{}'.format(t1token))
        t1.start()

    def destroy():
        """exit program"""
        global threadsRun, ac
        ac = 0
        threadsRun = 0
        # Not sure if keyboard unhooks if press quit after press start
        root.destroy()

    startButton = tk.Button(root, text="Start", command=sunken,
                            height=10, width=20)
    startButton.grid(row=1, column=0)

    quitButton = tk.Button(root, text="Quit", command=destroy,
                           height=10, width=20)
    quitButton.grid(row=1, column=1)

def clicks(startButton):
    """right clicks the mouse at 1k clicks per minute
        also puts button back to normal state once done"""
    global t1token
    while threadsRun:
        while ac:
            click(clicks=17, interval=0.06, button='right')
    t1token += 1
    startButton.config(relief=tk.RAISED, state=tk.NORMAL, text="Start")

root = tk.Tk()

def OnKeyboardEvent(event):
    """A key was pressed"""
    global threadsRun, ac
    if event.Key == "R":
        ac = 0
        threadsRun = 0
        hm.UnhookKeyboard()
        return False
    if event.Key == "T":
        ac = not ac
        return False
    return True

hm = pyHook.HookManager()
hm.KeyDown = OnKeyboardEvent
simplegui()
root.mainloop()


texts:
Goal:

Extract all Appendix Titles
If Appendix Title is Answer Key call custom solutions template
If Not Answer Key call default appendix template

XML:

<appendix>
  <title>Appendix A</title>

data

</appendix>

<appendix>
  <title>Answer Key</title>

data

</appendix>

XSLT:

<xsl:template match="appendix/title">
  <xsl:choose>
    <xsl:when test="self::node()[text()='Answer Key']">

       <xsl:call-template name="solutions"/>

    </xsl:when>
    <xsl:otherwise>
      <xsl:apply-templates select="appendix"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

This template works correctly; however I am interested in gaining some ground when it comes to writing solid XSL templates. Particularly the XPATH expressions. There seem to be various ways to achieve the same results when using XPath. Is the following XPath expression best?

test="self::node()[text()='Answer Key']"


texts:
I am going through coding challenges and I would like to get feedback on one of them.
I am not sure if i can post the link to the challenge but here it is.  CodeEval Challenge

  Outside of your window, there's a wire between two
  buildings. Bats love to hang there but you notice they never hang
  closer than "d" centimeters from each other. They also don't hang
  closer than 6 centimeters from any of the buildings.

  Your goal is to determine the maximum number of additional bats that
  can fit on that wire assuming they have zero width.

  INPUT SAMPLE:

  Each line of input contains three space separated integers: the length of
  the wire "l", distance "d" and number of bats "n" already hanging on
  the wire.

  "n" numbers contain the positions of the bats in any order. All number
  are integers. You can assume that the bats already hanging on the wire
  are at least 6 cm from the poles and at least "d" centimeters apart
  from each other.:

  22 2 2 9 11
  33 5 0
  16 3 2 6 10
  835 125 1 113
  47 5 0

  Output
  3
  5
  0
  5
  8

Here is my solution.  I commented it a bit.  I had to convert the method to C to get it passed through the automated system.  Any suggestions to improve it would be greatly appreciated.

#define CENTIMETERS_FROM_WALL 6

int howManyBatsBetween(int pointOne, int pointTwo, int distanceBetween)
{
    //calculates how many bats you can put between two bats already on the wire
    //Have to make sure that it would be proper distance between the first bat and second bat
    int nextBatPosition = pointOne+distanceBetween;
    int batCount = 0;
    while ((nextBatPosition+distanceBetween) <= pointTwo) {
        batCount++;
        nextBatPosition+=distanceBetween;
    }
    return batCount;
}

-(void)batCount
{
    NSString *line = @"47 5 0"; //input
    //extract input
    NSArray *inputArray = [line componentsSeparatedByString:@" "];
    int lengthWire = [[inputArray objectAtIndex:0] intValue];
    int distanceFromEachOther = [[inputArray objectAtIndex:1] intValue];
    int batsOnWire = [[inputArray objectAtIndex:2] intValue];

    //create an array with bats already on the wire
    NSMutableArray *batsOnWireArray = [[NSMutableArray alloc]init];
    for (int indx = 1; indx <= batsOnWire; indx ++){
        [batsOnWireArray addObject:[inputArray objectAtIndex:indx+2]];
    }
    [batsOnWireArray sortedArrayUsingSelector:@selector(compare:)];
    int firstLocation = 0;
    int secondLocation = 0;
    int additionalBats = 0;

    if ([batsOnWireArray count] == 0) {
        //if there are no bats on the wire
        additionalBats = floor((lengthWire-(CENTIMETERS_FROM_WALL*2))/distanceFromEachOther)+1;
    }else{
        //calculate how many bats can be added between the beginig of the wire + 6cm and the first bat
        additionalBats = floor(([[batsOnWireArray objectAtIndex:0] integerValue]-CENTIMETERS_FROM_WALL)/distanceFromEachOther);

        for(int indx = 0; indx < [batsOnWireArray count]; indx++){
            firstLocation =[[batsOnWireArray objectAtIndex:indx] intValue];
            //calculate how many bats you can add between bats already on the wire
            if (indx < [batsOnWireArray count]-1) {
                secondLocation = [[batsOnWireArray objectAtIndex:indx+1] intValue];
                additionalBats += howManyBatsBetween(firstLocation, secondLocation, distanceFromEachOther);
            }else{
                //calculate how bats you can fit between the last bat and the end of the wire - 6cm
                additionalBats += floor(((lengthWire - CENTIMETERS_FROM_WALL) - firstLocation) / distanceFromEachOther);
            }

    }

    }

    NSLog(@"Result %d", additionalBats);
}


texts:
This app is a simple taxi emulator. The only feedback I've got on it was "bad internal architecture", that's why I would be grateful to receive more adequate and humiliating review.

Simply-speaking, it should be a static lib, all the functionality must execute in the same thread (but! thread-safe). There should be a taxi station with cash desk, drivers and cabs. Each driver has its own car and own cash desk. One can re-assign the car to another driver. Car breaks after each 50 000 km.  The DoMainProcessing function should wake every 2 seconds from outside, check if driver already took passenger to his place, fill in cash desk and possibly fix cars.

Full description

The weakest place in my code, I think, is DoMainProcessing. It just goes through all drivers and cars checking their condition. But I just had no better idea to do this in one thread.

Main class header:

#pragma once
#include "CashBox.h"
#include "Driver.h"
#include "Car.h"
#include <unordered_map>
#include <unordered_set>
#include <memory>

static const std::string DRIVER_CAR_LOCK = "DriverCar";

static const int NO_ERR = 0;
static const int CAR_IS_BUSY = 1;
static const int DRIVER_IS_BUSY = 2;
static const int NO_DRIVER_FOUND = 3;
static const int CAR_IS_BROKEN = 4;

class TaxiPark
{
public:
    //Basic function. Checks if which trips are finished
    // and repairs broken cars
    void DoMainProcessing();

    ~TaxiPark(void){};
    TaxiPark(void){};

    //Cars and drivers management
    int AddDriver(DWORD ID, std::shared_ptr<Car> car);
    int AddCar(DWORD ID);
    int ChangeCar(DWORD driverID, std::shared_ptr<Car> newCar);

    //Get driver/car info by ID
    const std::shared_ptr<Driver> GetDriverById(DWORD ID);
    const std::shared_ptr<Car> GetCarById(DWORD ID);

    //Get all ID's
    const std::vector<DWORD> GetAllCarID();
    const std::vector<DWORD> GetAllDriverID();

    //Try to set driver status as sick/on vacation if driver is not on trip
    int SetDriverStatus(DWORD driverID, bool onVacation, bool isSick);

    //Try to start trip for chosen passenger with chosen driver if it possible
    //(driver isn't already busy/on vacation/sick, car isn't broken)
    int TakeClients(Passenger client, DWORD driverID, DWORD distance, DWORD speed );

private:
    CashBox cashBox;

    std::unordered_map<DWORD, std::shared_ptr<Driver>> Drivers;
    std::unordered_map<DWORD, std::shared_ptr<Car>> Cars;
};

DoMainProcessing function:

void TaxiPark::DoMainProcessing()
{
    auto mut = CreateMutex(NULL, FALSE, DRIVER_CAR_LOCK.c_str());

    WaitForSingleObject(mut, INFINITE);
    {
        std::for_each(Drivers.begin(), Drivers.end(), [this] (std::pair<DWORD, std::shared_ptr<Driver>> it)
        {
            std::shared_ptr<Driver> driver(it.second);
            auto driverID = it.first;
            if (driver->IsTripFinished())
            {
                Drivers.erase(driverID);
                driver->FinishTrip();
                Drivers.emplace(std::make_pair<DWORD,std::shared_ptr<Driver>>(driverID, driver));
            }
        });
        std::for_each(Cars.begin(), Cars.end(), [this] (std::pair<DWORD, std::shared_ptr<Car>> it)
        {
            std::shared_ptr<Car> car(it.second);
            auto carID = it.first;
            if (car->IsBroken())
            {
                Cars.erase(carID);
                if (cashBox.GetBalance() >= CAR_REPAIR_COST)
                {
                    cashBox.TakeCash(CAR_REPAIR_COST);
                    car->Repair();
                }
                Cars.emplace(std::make_pair<DWORD,std::shared_ptr<Car>>(carID, car));
            }
        });
    }
}

Here's the full code listing.

P.S.: I know that unordered_map is a bad idea in this case. It gives no advantages in terms of speed, but requires exceed work in need of changing elements.


texts:
Project Euler, problem #50:

  The prime 41, can be written as the sum of six consecutive primes:

  41 = 2 + 3 + 5 + 7 + 11 + 13 This is the longest sum of consecutive
  primes that adds to a prime below one-hundred.

  The longest sum of consecutive primes below one-thousand that adds to
  a prime, contains 21 terms, and is equal to 953.

  Which prime, below one-million, can be written as the sum of the most
  consecutive primes?

I came up with this code, but it only works decently for primes below ten thousand.

Any ideas on how to optimize it?

from pyprimes import *

def sequence_exists(l,ls,limit = 100):
    for x in range(0,len(ls)-l):
        if x+l > len(ls): return False
        if any (ls[i] > limit/6 for i in range(x,x+l,1)) :
            return False
        test_sum = sum(ls[x:x+l:1])
        if (test_sum <limit) and is_prime(test_sum) :
            return True
    return False

def main():
    n = prime_count(10000)
    prime_list = list(nprimes(n))
    l = 6
    for x in range(6,len(prime_list)):
        if sequence_exists(x,prime_list,10000):
            l=x
    print l

if __name__ == '__main__':
    main()


texts:
We have about 14G files and each is about 150k.

I'm writing a script to upload them to Azure Blob storage and would like to run an upload() function (each with own file to upload) in 5 threads.

Here is how I realized it and it looks like it works - but I still have doubts about how correct this code is.

...
class Loader:

    def __init__(self):

        self.account = 'accname'
        #self.container = 'userdata'
        self.container = 'bar1'
        key = 'DQ4***A=='

        self.base_url = 'http://' + self.account + '.blob.core.windows.net'
        self.blob = BlobService(account_name=self.account, account_key=key)

    def uploader(self, filepath, userfile, locker):

        print 'Uploading file: {}'.format(userfile)

        self.blob.put_block_blob_from_path(self.container,
                                           userfile,
                                           filepath,
                                           max_connections=1,
                                           max_retries=5,
                                           retry_wait=1.0)

        locker.release()

    def upload(self, path):

        print('Upload files from {} to {}'.format(path, self.base_url))

        locker = threading.BoundedSemaphore(5)

        for root, dirs, files in os.walk(path):
            print root
            for userfile in files:
                #print 'Uploading {}'.format(os.path.join(root, userfile))
                locker.acquire()
                t = threading.Thread(target=self.uploader, args=(os.path.join(root, userfile), userfile, locker))
                t.start()
...

And script in action:

$ ./storage_upload.py -u --path /tmp/bartest/
Upload files from /tmp/bartest/ to http://accname.blob.core.windows.net
/tmp/bartest/
Uploading file: 15.txt
Uploading file: 12.txt
Uploading file: 7.txt
Uploading file: 13.txt
Uploading file: 19.txt


texts:
This accepts text and returns a list of all words which occur with a required frequency (default frequency is 2). It works well, but gets too slow with large texts, more than 1M words. Where can this function be improved to speed searches in larger text?

from collections import Counter
import re

def words_in_text_counter(in_text,frequency=2):
    no_noise_text = re.sub(r'[0-9.,:;<>"#!%&/()=?*-+]','',in_text.lower()).strip().split()
    frequency_dict = {}
    for key, value in Counter(no_noise_text).items():
        frequency_dict.setdefault(value, []).append(key)
    try:
        print(str(frequency)+' times words in text.')
        return frequency_dict[frequency]
    except KeyError:
        return str(frequency)+' times never happens in text.'


texts: Python script to test music sight reading

texts:
I decided to write a program to test music theory, and, while it would've been much easier for me to make it elegant and perfect in Java, I thought I'd take the opportunity to get more familiar with Python.

Below is the full script.  It simply prints a staff in treble of bass clef with a note somewhere and asks the user to identify it.

Things I'm particularly interested in getting feedback about:

The @staticmethod in the Staff class.  I tried this both as an instance function (is it even appropriate to call it this in Python?) and as the static version below, but both seemed kind of clunky to me.  It doesn't make much sense to me that I have to refer to the class's fields with the class name when I'm already in the class (e.g., Staff.lines instead of just lines, or self.lines if it's an instance function).  It makes me think I'm doing something wrong.
I've heard multiple times that having constants isn't very Pythonic.  Is there a better way for me to handle the TREBLE_CLEF, BASS_CLEF, TREBLE_CLEF_NOTES, and BASS_CLEF_NOTES fields?
I'm interested in a solution which allows the user to input a command like exit into the prompt in order to leave the program.  I know I could've put this in the test_note function, but that would break the principle that each function should do only one thing and do it well (i.e., the test_note function test's the user's knowledge of the staff -- it shouldn't be responsible at all for controlling the application or its state).  I couldn't think of an elegant way to do this and still preserve that principle, so I just bypassed the issue for now, haha.
Am I using classes appropriately?  Any way I can use them better?

Of course, any and all feedback is welcome, especially including anything which breaks from conventional Python style.  I'd like to be as Pythonic as possible.

import random
import time

class Clef:

    def __init__(self, name, notes):
        self.name = name
        self.notes = notes

    def random_note(self):
        return random.choice(list(self.notes.keys()))

    def get_note(self, index):
        return self.notes[index]

class Staff:

    lines = 9
    line_length = 9
    line_text = '-' * line_length
    space_text = ' ' * line_length
    note_text = 'O'

    @staticmethod
    def display(note_index):
        for i in range(Staff.lines):
            add_note = i == note_index
            print_line = Staff.line_text if i % 2 == 0 else Staff.space_text
            if i == note_index:
                print_line = print_line[:4] + Staff.note_text + print_line[5:]
            print(print_line)

TREBLE_CLEF_NOTES = { 0 : 'F',
                      1 : 'E',
                      2 : 'D',
                      3 : 'C',
                      4 : 'B',
                      5 : 'A',
                      6 : 'G',
                      7 : 'F',
                      8 : 'E' }

BASS_CLEF_NOTES = { 0 : 'A',
                    1 : 'G',
                    2 : 'F',
                    3 : 'E',
                    4 : 'D',
                    5 : 'C',
                    6 : 'B',
                    7 : 'A',
                    8 : 'G' }

TREBLE_CLEF = Clef('Treble', TREBLE_CLEF_NOTES)
BASS_CLEF = Clef('Bass', BASS_CLEF_NOTES)

def test_note(clef, note_index):
    print(clef.name)
    Staff.display(note_index)
    answer = clef.get_note(note_index)
    user_choice = input('Enter the note: ').strip().upper()
    if user_choice == answer:
        print('Correct!')
    else:
        print('Wrong.  This is ' + answer)

def main():
    print('To exit, type CTRL + C')
    while True:
        print('\n')
        clef = random.choice([TREBLE_CLEF, BASS_CLEF])
        test_note(clef, clef.random_note())
        time.sleep(2)

if __name__ == '__main__':
    main()


texts: The start of a game in pygame, blitting images to the screen

texts:
The small bit of code below blits and image to the screen within a class. I call this 4 times to display the image 4 times, each image has a different y coordinate and doesn't need different x but when they are blitted they make the code MUCH slower. I know it is the blitting as well due to the fact that if i just draw rects instead, the program moves smoothly.

tableTex = pygame.transform.scale(pygame.image.load('./Sources/Table.png'), (270, 240))

    def display(self):
        if not self.occupied:
            screen.blit(tableTex, (self.x, self.y))
        else:
            pygame.draw.rect(screen, (127, 0, 0), self.rect, 0)

A link to the full code can be found here if needed.
I just want to know if there is any reason this is so slow and if there are any other methods I could use that are faster?

Thanks


texts: Convert a mix of latin 1 and UTF-8 to proper UTF-8

texts:
The following program takes an arbitrary byte sequence as input and outputs well-formed UTF-8. All UTF-8 sequences from the input are copied unmodified. All other bytes are assumed to be encoded in ISO 8859-1 or some variant, and their byte value is output as the corresponding code point in UTF-8.

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

static int buf[4];
static size_t buflen;

/* Reads from stdin until buf[index] can be accessed. */
static bool read_buf(size_t index) {
    while (!(index < buflen)) {
        buf[buflen] = getchar();
        if (buf[buflen] == EOF) {
            return false;
        }
        buflen++;
    }
    return buf[buflen - 1] != EOF;
}

static void write_buf(size_t n) {
    for (size_t i = 0; i < n; i++) {
        putchar(buf[i]);
    }
    buflen = 0;
}

static bool start_between(int min, int max) {
    return min <= buf[0] && buf[0] < max;
}

static bool is_cont(int index) {
    return 0x80 <= buf[index] && buf[index] < 0xC0;
}

static int code_point(int bits18, int bits12, int bits06, int bits00) {
    return   ((bits18 & 0x3F) << 18)
           | ((bits12 & 0x3F) << 12)
           | ((bits06 & 0x3F) <<  6)
           |  (bits00 & 0x3F);
}

int main(void) {
    while (read_buf(0) && !ferror(stdout)) {
        if (start_between(0x00, 0x80)) {
            write_buf(1);
        } else if (start_between(0xC2, 0xE0) && read_buf(1) && is_cont(1)
                   && code_point(0, 0, buf[0] & 0x1F, buf[1]) >= 0x0080) {
            write_buf(2);
        } else if (start_between(0xE0, 0xF0) && read_buf(2) && is_cont(1) && is_cont(2)
                   && code_point(0, buf[0] & 0x0F, buf[1], buf[2]) >= 0x0800) {
            write_buf(3);
        } else if (start_between(0xF0, 0xF8) && read_buf(3) && is_cont(1) && is_cont(2) && is_cont(3)
                   && code_point(buf[0] & 0x07, buf[1], buf[2], buf[3]) >= 0x010000
                   && code_point(buf[0] & 0x07, buf[1], buf[2], buf[3]) < 0x110000) {
            write_buf(4);
        } else {
            putchar(0xC0 + ((buf[0] >> 6) & 0x1F));
            putchar(0x80 + ((buf[0] >> 0) & 0x3F));
            buflen--;
            buf[0] = buf[1];
            buf[1] = buf[2];
            buf[2] = buf[3];
            buf[3] = 0;
        }
    }

    return ferror(stdin) || ferror(stdout) ? EXIT_FAILURE : EXIT_SUCCESS;
}

To test the code, I manually ran variants of the following command:

perl -e 'print pack("H*", "fcf09f9985e08080fc")' | ./latin1_to_utf8 | hexdump -C


texts: Bash script to extract HTML comment into a Markdown file

texts:
I learned Bash a million years ago. I just wrote this simple script used to get the first lot of HTML comments from a file, and spit it out in order to create a README.md file.

It is just. So. Ugly. I read bits and pieces over the years, and I am sure it can be improved so much...

Here we go:

#!/bin/bash

IFS=''
active='0';
cat  hot-form-validator.html | while read "line";do

  echo $line | grep '\-\->' > /dev/null
  if [ $active = '1' -a $? = '0' ];then
    exit 0;
  fi;

  suppress=0;
  echo $line | grep '^ *@' > /dev/null
  if [ $? = '0' ];then suppress='1'; fi;

  if [ $active = '1' -a $suppress = '0' ];then echo $line;fi;

  echo $line | grep "<!--" > /dev/null
  if [ $? = '0' ];then active='1'; fi;

done

Questions:

Is there a better way to do grep and then check $??  Back in the day it was the way to go, but...
Should active be a proper number rather than a string with a number? I  know, it could be anything... but having a string that can be 0 or 1 just feels wrong.
Is there a better way to preserve spaces, rather than zapping IFS?
Any more pearls of wisdom, other than quitting my (short lived) career of bash scripter?


texts: How to properly set a different formatter for a specific logger's child?

texts:
For certain modules or functions, I want to have a different format for the logged messages. I have done this in a way that looks a bit hacky: I have set a special Logger, containing a special Handler, that has the right Formatter.

Here is a simplified portion of my configuration file:

formatters:
    dbgMessageFormatter:
        format: '[%(name)s] %(message)s'
    rawMessageFormatter:
        format: '%(message)s'

handlers:
    debugHandler:
        class: logging.StreamHandler
        level: DEBUG
        formatter: dbgMessageFormatter
    rawHandler:
        class: logging.StreamHandler
        level: DEBUG
        formatter: rawMessageFormatter

loggers:
    dbg:
        level: DEBUG
        handlers: [debugHandler]
    raw:
        level: DEBUG
        handlers: [rawHandler]

The "main" debugging logger is loaded this way:

with open(logging_conf_filename) as f:
    logging.config.dictConfig(yaml.load(f))
dbg_logger = logging.getLogger("dbg")

And for a child (named child in the snippet below) of this "dbg" logger, I set the other Handler this way:

raw_logger = logging.getLogger("raw")
l = logging.getLogger(child)
l.addHandler(raw_logger.handlers[0])
l.propagate = False

Is there a better, more straightforward way to set this up?


texts: Python methods to reduce running times KTNS algorithm

texts:
I have been trying to develop an algorithm called keep the tool needed soonest (for more information about the KTNS explanation (page 3)) but during the simulations, I have realized that it takes too much time to run.

I want to decrease the running times and checking other previous questions about how to fast python coding Is Python slower than Java/C#? [closed] I have found several solutions, but I don't know how to implement them in my code.

On my computer it takes 0.004999876022338867 seconds, but the main problem is that for the whole program this function is called 13,000 times. I would like to reduce as much as possible the times but I don't know how. The fact of adapting the dictionaries to list is a good idea, but I don't know how to keep doing the same calculations. I have tried to ask in StackOverFlow but I've been said that here is a suitable place to do this kind of question. Here I attach my whole code, if you have any suggestion to improve it please don't hesitate to share with me.

import sets
import numpy
import copy
import time

J= {1: [6, 7],2: [2, 3], 3: [1, 6, 9], 4: [1, 5, 9], 5: [5, 8, 10], 6: [1, 3, 6, 8], 7: [5, 6, 8, 9], 8: [5, 7, 8], 9: [1, 4, 5, 8], 10: [1, 2, 4, 10]}

def KTNS(sigma=[10, 9, 4, 1, 6, 3, 7, 5, 2, 8], Jobs=J,  m=10 ,capacity=4 ):
    t0=time.time()
    Tools = {}
    Lin={}
    n=len(sigma)
    for i in range(1,m+1):
        for e in sigma:
            if i in Jobs[e]:
                Tools[i]=sets.Set([])

    count = 1
    available_tools=sets.Set()
    for e in sigma:
        for i in Jobs[e]:
            Tools[i].add(count)
            available_tools.add(i)
        count+=1
    Tin=copy.deepcopy(Tools)
    for e in Tin:
        Lin[e]=min(Tin[e])
    count=1
    J = numpy.array([0] *m)
    W = numpy.array([[0] * m] * n)
    while count<=len(sigma):
        for e in Tools:
            if len(available_tools)<capacity:
                reference=len(available_tools)
            else:
                reference=capacity
            while numpy.count_nonzero(J == 1) <reference:
                min_value = min(Lin.itervalues())
                min_keys = [k for k in Lin if Lin[k] == min_value]
                temp = min_keys[0] #min(Lin, key=Lin.get)
                if min_value>count:
                    if len(min_keys)>=2:
                        if count==1:
                            J[temp - 1] = 1
                            Lin[temp] = '-'
                        else:
                            J0=W[count-2]
                            k=0
                            for elements in min_keys: #tested
                                if numpy.count_nonzero(J == 1) < reference:
                                    if J0[elements-1]==1:
                                        J[elements-1]=1
                                        Lin[elements]='-'
                                        k=1
                                    else:
                                        pass
                                else:
                                    pass
                            if k==0:
                                J[temp - 1] = 1
                                Lin[temp] = '-'
                    else:
                        J[temp - 1] = 1
                        Lin[temp] = '-'
                else:
                    J[temp-1]=1
                    Lin[temp] = '-'
            Tin[e].discard(count)
            for element in Tin:
                try:
                    Lin[element] = min(Tin[element])
                except ValueError:
                    Tin[element]=sets.Set([len(sigma)+1])
                    Lin[element]=len(sigma)+1
        W[count-1]=J
        J= numpy.array([0] *m)
        count+=1
    Cost=0
    for e in range(1,len(sigma)):
        temp=W[e]-W[e-1]
        temp[temp < 0] = 0
        Cost+=sum(temp)

    return Cost+capacity,time.time()-t0


texts:
I recently came across this problem:

  It is Lavanya's birthday and several families have been invited for
  the birthday party. As is customary, all of them have brought gifts
  for Lavanya as well as her brother Nikhil. Since their friends are all
  of the erudite kind, everyone has brought a pair of books.
  Unfortunately, the gift givers did not clearly indicate which book in
  the pair is for Lavanya and which one is for Nikhil. Now it is up to
  their father to divide up these books between them.

  He has decided that from each of these pairs, one book will go to
  Lavanya and one to Nikhil. Moreover, since Nikhil is quite a keen
  observer of the value of gifts, the books have to be divided in such a
  manner that the total value of the books for Lavanya is as close as
  possible to total value of the books for Nikhil. Since Lavanya and
  Nikhil are kids, no book that has been gifted will have a value higher
  than 300 Rupees.

  Suppose there are 4 pairs of books whose cost in Rupees are:

  (3,5), (7,11), (8,8), (2,9)

  By giving the books worth 3,7,8 and 2 to Lavanya and the rest to
  Nikhil, the net difference in value would be 5+11+8+9-3-7-8-2 = 13.
  However, by giving books worth 3,7,8 and 9 to Lavanya and the rest to
  Nikhil, their father can ensure that the difference in values is just
  1. You can verify that you cannot do better than this.

  You task is to help their father decide how to divide the books.

My approach is take the difference of of every pair and arrange them in descending order, where one by one they are subtracted if the previous result was positive or added if previous result was negative. This greedy approach works for most of the test cases but gets stuck at some.

#include <iostream>
#include <algorithm>
#include <cmath>

int main (int argc, char const* argv[])
{
    int n;
    std::cin >> n;
    int difference[n];
    for(int i=0;i<n;i++){
        int a,b;
        std::cin>>a >>b;
        difference[i] = std::abs(a-b);
    }
    std::sort(difference , difference+n,std::greater<int>());
    int diff = difference[0];
    for(int i=1;i<n;i++){
        if(diff > 0){
            diff = diff - difference[i];
        }else{
            diff = diff + difference[i];
        }
        std::cout << diff << std::endl;
    }
    std::cout << std::abs(diff) << std::endl;
    return 0;
}

I have tried to implement recursion but it gets too complicated for me and often takes a lot more time for bigger test cases. Does anyone have any better approach for it?

Here are some testcases:

  TestCase 1: Input: 5 271 6 293 148 194 104 4 207 148 267

  Output: 2

  TestCase 2: Input: 30 130 83 207 104 115 33 149 20 51 128 78 256 246
  99 42 215 187 276 239 263 105 122 294 143 14 241 146 175 175 114 9 56
  248 267 212 62 299 60 82 49 187 159 56 184 9 150 99 195 177 89 157 282
  210 203 176 275 143 73 202 69

  Output: 1

  TestCase 3: Input: 50 185 294 180 119 96 18 82 75 26 25 120 55 40 230
  31 105 112 140 22 227 154 29 187 107 156 182 181 57 122 39 90 59 84
  269 177 180 287 258 6 64 34 126 171 74 107 201 230 218 93 4 197 246 32
  135 104 239 16 284 47 189 75 136 247 158 157 176 89 195 185 147 10 219
  272 180 44 130 133 273 100 225 276 296 222 59 130 25 49 197 60 96 85
  134 283 84 44 139 259 184 85 143

  Output: 0

Apparently this are the only testcases causing trouble.


texts: Aborting a program for invalid floating-point input

texts:
My friend does a bunch of Java work (commandline, still toying around) and I notice a bunch of try/catch blocks like this:

   try {
       double a = Double.parseDouble(secondInput);
   }
   catch(NumberFormatException nFE) {
       System.out.println("The input was not an integer.");
       try {
           Thread.sleep(1997);
       } catch (InterruptedException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
       System.exit(0);
   }

Which, IMO doesn't look very pretty. And I'm wondering what the "better" way to write this code would be.


texts:
There has been ongoing concern about the question of awaiting a task that could possibly be null, as when using null-coalescing operators. Consider the following:

var response = await turnContext?.SendActivityAsync("I am a bot");

That will throw a run-time exception when Instance is null, as seen in questions like this: https://stackoverflow.com/questions/33592547/c-sharp-error-with-null-conditional-operator-and-await/33592569

Furthermore, if you use a ConfiguredTaskAwaitable<T> then the code won't even compile because it will be converted to a nullable struct that has no GetAwaiter:

var response = await turnContext?.SendActivityAsync("I am a bot").ConfigureAwait(false);

This can be made to compile by wrapping the null chain in parentheses so that ConfigureAwait remains a value type, but this will still cause null reference exceptions:

var response = await (turnContext?.SendActivityAsync("I am a bot")).ConfigureAwait(false);

To solve this issue, I've created an extension method to automatically check for nulls:

public static ConfiguredTaskAwaitable<T> CoalesceAwait<T>(
    this Task<T> task,
    bool continueOnCapturedContext = false)
=> (task ?? Task.FromResult<T>(default)).ConfigureAwait(continueOnCapturedContext);

This can be used in place of ConfigureAwait, and as a bonus I've made sure to have continueOnCapturedContext be false by default:

var response = await (turnContext?.SendActivityAsync("I am a bot")).CoalesceAwait();

The only problem is that you still need to wrap the null chain in parentheses which can be sort of awkward. I look forward to seeing if this is useful to anyone or if anyone has any other feedback.


texts:
I need to move data from one database to another, and since I don't have SSIS I'm doing this ETL with T-SQL scripts.

One of the source tables contains invoice details, and features a column that contains the number of units invoiced per size (it's clothing); as I transfer the data over to my database, I'm normalizing this information. The query works fine, the execution plan looks exactly as I expected, doesn't recommend adding any indexes, ...but it takes about 10 minutes to process.

The source table Staging.dbo.[SourceTable] contains about 780K rows; the query inserts 1.5M rows into the destination table [DestinationDatabase].dbo.InvoiceDetailSizes.

The query is scheduled to run daily, as part of an overnight process - the 10 minutes don't really matter, but still I want to be sure everything is as efficient as it can be.

with cteSizedInvoices (
    InvoiceNumber, InvoiceLine, SizeRangeCode, UnitsPerSize
)
as (select
        src.f2,
        cast(src.f5 as int),
        src.f23,
        src.f19
    from Staging.dbo.[SourceTable] src
    where src.f1 = '01'
)

--insert into [DestinationDatabase].dbo.InvoiceDetailSizes (InvoiceDetailId, SizeId, Units, DateInserted)
select
    detail.Id InvoiceDetailId,
    sz.Id SizeId,
    buckets.Units,
    getdate()
from
    cteSizedInvoices src
        inner join [DestinationDatabase].dbo.InvoiceHeaders header on src.InvoiceNumber = header.Number
        inner join [DestinationDatabase].dbo.InvoiceDetails detail on src.InvoiceLine = detail.LineNumber
                                                                   and detail.InvoiceHeaderId = header.Id
        inner join [DestinationDatabase].dbo.SizeRanges ranges on src.SizeRangeCode = ranges.Code
        cross apply (select id SizeIndex, cast(item as int) Units from [DestinationDatabase].dbo.BucketString(src.UnitsPerSize, 5, 1, 1)) buckets
        inner join [DestinationDatabase].dbo.Sizes sz on buckets.SizeIndex = sz.SizeRangeIndex
                                                      and sz.SizeRangeId = ranges.Id
    left join [DestinationDatabase].dbo.InvoiceDetailSizes dst
        on detail.Id = dst.InvoiceDetailId
        and sz.Id = dst.SizeId
where
    buckets.Units <> 0
    and dst.id is null;

Here is the BucketString table-valued function that I'm using - it's adapted from this Stack Overflow answer, modified so that I could specify an "offset", because my data doesn't start at position 1:

create function [dbo].[BucketString] (
    @values varchar(max),
    @bucketSize int,
    @bufferSize int = 1,
    @offset int = 0)

returns @result table (id int, item varchar(max))

begin

    with buckets as
    (
        select 1 id

        union all

        select t.id + 1
        from buckets t
        where id = t.id
            and t.id < len(@values)/(@bucketSize+@bufferSize)+1
    )

    insert into @result
    select
        id,
        substring(@values, @offset + ((id - 1) * (@bucketSize + @bufferSize) + (case when @bufferSize-1 = 0 then @bufferSize else @bufferSize-(@bufferSize-1) end)), @bucketSize) string
    from buckets
    option (maxrecursion 0)

    return;

end

The query will not insert anything if the data already exists in the destination table, but in order to find out if it exists, I need to run the whole thing, so whether I'm inserting 1.5M rows, or 0, it's still ~10 minutes.

Can it be optimized in any way?