Decide if a number is even or odd by using bitwise operator The following exercise is from an Assembler-course I'm taking:

  Write a program that takes a number x as input, and returns:

  0 if x is even.
  1 if x is odd.

Full exercise-page here: GitHub

Here's my solution:

format PE console
entry start

include 'win32a.inc'

; ===============================================
section '.text' code readable executable

start:

    call    read_hex        ; Provided by the teacher. Reads a hexadecimal number from stdin.

    and     eax,    0x1

    cmp     eax,    0x0
    je      print_result

    mov     eax,    0x1
print_result:
    call    print_eax_binary    ; Provided by the teacher. Prints a binary number to stdout.

    ; Exit the process:
    push    0
    call    [ExitProcess]

include 'training.inc'

I would say it works alright:

Is my solution valid?
Can it be improved? Respectively, is there a better solution possible?

Here are some things that may help you improve your code.

Simplify your code

This can certainly be simplified.  Keep the goal in mind: get either 0 or 1 into EAX.  Once you've executed this instruction:

and     eax,    0x1

That's exactly what you have.  So that means all of this can be deleted:

    cmp     eax,    0x0
    je      print_result

    mov     eax,    0x1
print_result:

And all you need to do is print the result:

call    print_eax_binary


Get latest jsons with respect to [version and name] key in subjson in java This is the json

[
  {
    "SUBTASK": {
      "id": 10,
      "name": "TestMap",
      "description": "",
      "version":"1.0"
    }
  },
  {
    "SUBTASK": {
      "id": 23,
      "name": "TestMap",
      "description": "",
      "version":"2.0"
    }
  },
  {
    "SUBTASK": {
      "id": 12,
      "name": "Tree",
      "description": "",
      "version":"13.0"
    }
  },
  {
    "SUBTASK": {
      "id": 32,
      "name": "Tree",
      "description": "",
      "version":"10.0"
    }
  }
]

Need to get latest among the number of SUBTASKS ie. filter the latest versions of SUBTASK with respected to name.

So the final json will be like:

final:[{"SUBTASK":{"id":12.0,"name":"Tree","description":"","version":"13.0"}},{"SUBTASK":{"id":23.0,"name":"TestMap","description":"","version":"2.0"}}]

I achived using the below the java code :

Java Snippet

 public void getLatest(String json) throws Exception {
        // converting json to java Object
        List<Map<String, Object>> taskList = new Gson().fromJson(json, new TypeToken<List<Map<String, Object>>>() {
        }.getType());

        Map<String, Object> taskVersionedMap = new HashMap<String, Object>();

        for (int i = 0; i < taskList.size(); i++) {
            Map<String, Object> processMap = taskList.get(i);
            Map<String, Object> process = (Map<String, Object>) processMap.get("SUBTASK");

            String process_name = process.get("name").toString() + "_" + process.get("version");
            taskVersionedMap.put(process_name, processMap);

            for (Map.Entry<String, Object> entry : taskVersionedMap.entrySet()) {
                String[] split = entry.getKey().split("_");
                double version = Double.parseDouble(split[1]);
                String name = split[0];
                for (Map.Entry<String, Object> entry2 : taskVersionedMap.entrySet()) {
                    String[] split_2 = entry2.getKey().split("_");
                    double version_2 = Double.parseDouble(split_2[1]);
                    String name_2 = split_2[0];
                    if (name.equals(name_2)) {
                        if (version < version_2) {
                            String removedProcessName = name + "_" + version;
                            taskVersionedMap.put(removedProcessName, "ignored");
                        }
                    }
                }

            }
        }
        List<Map<String, Object>> filteredVersionList = new ArrayList<Map<String, Object>>();
        for (Map.Entry<String, Object> mapValues : taskVersionedMap.entrySet()) {
            if (!mapValues.getValue().toString().equals("ignored")) {
                try {
                    filteredVersionList.add((Map<String, Object>) mapValues.getValue());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("final:"+new Gson().toJson(filteredVersionList));

    }

The above code gives accurate result expected and is time consuming.

But is it possible to optimize the code since real data json at process will be verry big almost 500 more subtasks

First of all, you have a grouping operation by process name, then you have the task of finding the maximum by version number per group.

Thus, instead of using nested loops and somehow mashing it all together, simply perform those operations.

As standard operations like grouping and finding a maximum are alreay presenet in the java stream api, all we need is a few helpers to extract the name and the version:

Function<Map<String, Object>, String> extractName = m -> (String) ((Map<String, Object>) m.get("SUBTASK")).get("name");
ToDoubleFunction<Map<String, Object>> extractVersion = m -> Double.parseDouble((String) ((Map<String, Object>) m.get("SUBTASK")).get("version"));

Then, first group by name:

Map<String, List<Map<String, Object>>> byName = taskList.stream()
    .collect(Collectors.groupingBy(extractName));

... which gives you a list of each task type in the value lists.

Now, we can disregard the grouping keys and extract the maximum per list:

List<Map<String, Object>> result = byName.values().stream()
    .map(l -> l.stream().max(Comparator.comparingDouble(extractVersion)))
    .map(Optional::get)
    .collect(Collectors.toList());

Note: the stream.max returns an optional, which would be unset for an empty list. As we cannot generate empty lists here, it is save to simply map via Optional::get to retrieve the value.

All is left is to encode the result list back to json, e.g.

String jsonResult = new GsonBuilder().setPrettyPrinting().create().toJson(result);

Note however, that you should better use a method which operates directly on the data structures, i.e. input and output are List<Map<String, Object>> and another outer wrapper method to do the decoding and encoding. Do one thing in a method.


Transform an entity from a type to another I need to transform an entity from an external DLL to another entity which is from a different external DLL.

My question is: Is this the best solution or is there a better solution (type-save)? I hope there is a better idee because I don't really like to use the dynamic type.

using ExternalDllOne.Fruit; //contains(Apple, Banana, Melon, Peach)
using ExternalDllTwo.Fruit; //contains(ReturnApple, ReturnBanana, ReturnMelon, ReturnPeach)

public static dynamic Transform(Type type, string path)
{
    switch (type.Name)
    {
        case nameof(Banana):
            {
                Banana serializedFruit = SerializerFactory<Banana>.Create(SerializerType.Xml).Deserialize(path);
                ReturnBanana fruit = TransformBanana(serializedFruit);
                return fruit;
            }
        case nameof(Apple):
            {
                Apple serializedFruit = SerializerFactory<Apple>.Create(SerializerType.Xml).Deserialize(path);
                ReturnApple fruit =  TransformApple(serializedFruit);
                return fruit;
            }
        case nameof(Peach):
            {
                Peach serializedFruit = SerializerFactory<Peach>.Create(SerializerType.Xml).Deserialize(path);
                ReturnPeach fruit =  TransformPeach(serializedFruit);
                return fruit;
            }
        case nameof(Melon):
            {
                Melon serializedFruit = SerializerFactory<Melon>.Create(SerializerType.Xml).Deserialize(path);
                ReturnMelon fruit =  TransformMelon(serializedFruit);
                return fruit;
            }
        default:
            throw new ArgumentException($@"Type: [{type.Name}] is not a valid type for this application");
    }
}

The conversion is done thru a facade which is located in an external DLL. They way the Transform<fruit>() works is for every fruit type the same:

private static ReturnBanana TransformBanana(Banana fruit)
{
    var extraSettings = GetBananaSettings();

    var transformedFruit = FruitTransformationFacade.Transform(fruit, extraSettings);

    return transformedFruit;
}

Using the transformer method I have this code:

private static dynamic Load(string path)
{
    XDocument document = FileHelper.ParseFile(path);

    Type fileType = TypeHelper.DetermineFileType(document);

    dynamic component = Transformer.Transform(fileType, path);

    return component;
}

Do I need a wrapper object for each Return<fruit> entity with a coherent Interface, and return the interface?

public static dynamic Transform(Type type, string path)

If I should return an object of any type I'd rather use the object type for it then dynamic. I don't know how you actually use it so I assume that all objects have some set of common properties but don't share a any common interface.

In that case I think it would make more sense to create an artifical wrapper for those objects so that you can read those properties (or methods) without having to use dynamic everywhere.

public class Fruit // = ExternalDllTwoEntityWrapper
{
    private readonly dynamic _value;
    Fruit(object obj) => _value = obj;
    public string Name => (string)_value.Name;
}

Whether you use dynamic or reflection internally to access those properties is another question.

If you however can change one of those external dlls then an abstraction would be the obvious way to solve this:

public interface IFruit
{
    string Name { get; }
}


Classes for attributes validation I have two classes responsible for attributes validation:

class NameValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    message = options.fetch(:message, I18n.t('errors.attributes.name.invalid'))
    record.errors[attribute] << message unless NameValidator.valid_name?(value)
  end

  def self.valid_name?(name)
    name =~ /\A[a-z][\w\p{Blank}]+\z/i
  end
end

and the second one:

class EmailValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    message = options.fetch(:message, I18n.t('errors.attributes.email.invalid'))
    record.errors[attribute] << message unless EmailValidator.valid_email?(value)
  end

  def self.valid_email?(email)
    email =~ /\A.+@.+\..+\z/i
  end
end

They're basically the same. Should I inherit them from one class with protected utility methods or what?

By creating a base class and then inheriting from it, you end up with no duplication and with an architecture that can be easily extended.

I don't think that you should bother having valid? as protected. Calling valid? from outside is a common use case and doesn't violate encapsulation.

class Validator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    message = options.fetch(:message, I18n.t('errors.attributes.name.invalid'))
    record.errors[attribute] << message unless self.valid?(value)
  end

  def self.valid?(value)
    raise NotImplementedError
  end
end

class NameValidator < Validator
  def self.valid?(value)
    value =~ /\A[a-z][\w\p{Blank}]+\z/i
  end
end

class EmailValidator < Validator
  def self.valid?(value)
    value =~ /\A.+@.+\..+\z/i
  end
end


Merge sort with raw C++ array Merge function:

void merge(int outputArray[], int firstArray[], int sizeFirst, int secondArray[], int sizeSecond){
    int p = 0;
    int p1 = 0;
    int p2 = 0;
    while(p1 < sizeFirst && p2 < sizeSecond){
        if(first[p1] < second[p2])
            outputArray[p++] = firstArray[p1++];
        else
            outputArray[p++] = secondArray[p2++];
    }

    while(p1 < sizeFirst) outputArray[p++] = firstArray[p1++];
    while(p2 < sizeSecond) outputArray[p++] = secondArray[p2++];
}

Sort function:

void sort(int numbers[], int size){
    if(size == 1) return;
    int mid = size/2;
    int firstPartSize = mid;
    int secondPartSize = size - mid;

    int firstArray[firstPartSize];
    int secondArray[secondPartSize];

    for(int i = 0 ; i < size ;i++){
        if(i < mid)
            firstArray[i] = numbers[i];
        else
            secondArray[i - mid] = numbers[i];
    }

    sort(firstArray, firstPartSize);
    sort(secondArray, secondPartSize);
    merge(numbers, first, firstPartSize, second, secondPartSize);
}

Personally I think the number of arguments in the merge function are bit too many. I considered making a different function that calculates size of the array but that didn't seem to work with my current knowledge of the language.

Should I care about readability in standard algorithms?
What other names would you suggest instead of p, p1, p2 so that readability is not hampered?
Are there any other bugs or problems with the code?

With regards to variable names and readability, I would recommend the following:

Change p to outputIndex
Change p1 to firstIndex
Change p2 to secondIndex
Change sizeFirst to firstSize
Change sizeSecond to secondSize


Tabbed user interface with a panel that expands downward I have managed to get this to work but I am sure there is a better/more polished way to write this can anyone guide me to some shortcuts?

https://jsfiddle.net/zrpgvoL7/4

/* move the products down in the categories for the opening of filters/sort by aswell as slide down the filters or sort by sections */

var filterHeight = $('.product-filter').outerHeight() + 40;

var sortbyHeight = $('.sortby').outerHeight() + 20;

var speed = 1000

// hide by default instead of display:none in css as we need to find the height first

$('.product-filter').css('display', 'none');
$('.sortby').css('display', 'none');

// slide down filters/sort on click of 'refine' or 'sort' buttons

$('.clickable-header').click(function() {

$(this).toggleClass('active');

if ($(this).hasClass('active')) {

    if ($(this).hasClass('filter-header'))  {

        $('.clickable-header').removeClass('active');
        $(this).addClass('active');
        $('.sortby').slideUp(speed);
        $(this).parent().find('.dropable-section').slideDown(speed);
        $('#category-products').animate({ 'marginTop': filterHeight}, speed);
    }

    if ($(this).hasClass('sort-by-header'))  {

        $('.clickable-header').removeClass('active');
        $(this).addClass('active');
        $('.product-filter').slideUp(speed);
        $(this).parent().find('.dropable-section').slideDown(speed);
        $('#category-products').animate({ 'marginTop': sortbyHeight}, speed);
    }
}

else {

     if ($(this).hasClass('filter-header'))  {

        $(this).parent().find('.dropable-section').slideUp(speed);
        $('#category-products').animate({ 'marginTop': 0}, speed);
    }

    if ($(this).hasClass('sort-by-header'))  {

        $(this).parent().find('.dropable-section').slideUp(speed);
        $('#category-products').animate({ 'marginTop': 0}, speed);
    }

}

});

There are a few things you can do to reduce the code, for example:

you can use add to add jquery selections:

$('.product-filter').add('.sortby').css('display', 'none');

rather then have lots of nested if's you can use a ternary op and seperate them to seperate functions:

var action =  $(this).hasClass('active') ? handleActive : handleInactive;
action.call(this);

function handleActive () {
    if ($(this).hasClass('filter-header'))  {

        $('.clickable-header').removeClass('active');
        $(this).addClass('active');
        $('.sortby').slideUp(speed);
        $(this).parent().find('.dropable-section').slideDown(speed);
        $('#category-products').animate({ 'marginTop': filterHeight}, speed);
    }

    if ($(this).hasClass('sort-by-header'))  {

        $('.clickable-header').removeClass('active');
        $(this).addClass('active');
        $('.product-filter').slideUp(speed);
        $(this).parent().find('.dropable-section').slideDown(speed);
        $('#category-products').animate({ 'marginTop': sortbyHeight}, speed);
    }
}

function handleInactive () {
     if ($(this).hasClass('filter-header'))  {

         $(this).parent().find('.dropable-section').slideUp(speed);
         $('#category-products').animate({ 'marginTop': 0}, speed);
     }

     if ($(this).hasClass('sort-by-header'))  {

         $(this).parent().find('.dropable-section').slideUp(speed);
         $('#category-products').animate({ 'marginTop': 0}, speed);
     }
}

you can also remove the duplication where the same outcome is reached regardless of the if, such as:

if ($(this).hasClass('filter-header'))  {

            $('.clickable-header').removeClass('active');
            $(this).addClass('active');
            $('.sortby').slideUp(speed);
            $(this).parent().find('.dropable-section').slideDown(speed);
            $('#category-products').animate({ 'marginTop': filterHeight}, speed);
        }

        if ($(this).hasClass('sort-by-header'))  {

            $('.clickable-header').removeClass('active');
            $(this).addClass('active');
            $('.product-filter').slideUp(speed);
            $(this).parent().find('.dropable-section').slideDown(speed);
            $('#category-products').animate({ 'marginTop': sortbyHeight}, speed);
        }

could be reduced to:

$('.clickable-header').removeClass('active');
$(this).addClass('active');
$(this).parent().find('.dropable-section').slideDown(speed);

if ($(this).hasClass('filter-header'))  {
  $('.sortby').slideUp(speed);
  $('#category-products').animate({ 'marginTop': filterHeight}, speed);
}

if ($(this).hasClass('sort-by-header'))  {
  $('.product-filter').slideUp(speed);
  $('#category-products').animate({ 'marginTop': sortbyHeight}, speed);
}


Displaying unique objects generated by Raphael.js in FancyBox I'm using Raphael.js to generate objects which are unique but do basically the same thing. I'm also using Fancybox to display a unique image after a click on a generated object.

Is there a way to write this more concise?

//OBJECT 101
    var p1_101 = p1.path("M361,146.773 366.25,192.94 365.917,196.606 366.583,199.69 387,197.773 392.417,179.023 387.75,178.273 395.833,146.106 397.583,145.94 398,143.023 374,145.273z").attr({"fill":"#d3ae6b", "stroke-width": 0, "fill-opacity": .5, "stroke": "transparent","cursor": "pointer", "title": "101"}).click(function () {
        $.fancybox.open([
            {
                href : 'img/rzuty/p1/101.jpg',
                openEffect : 'elastic',
                openSpeed  : 150,
                closeEffect : 'elastic',
                closeSpeed  : 150,
                padding: 2,
            }
        ], {
            beforeShow : function() {
                $('').appendTo(".fancybox-inner");
            }
        });
    }).mouseover(
        function () {
            this.animate({"fill-opacity": .8}, 200);
        }).mouseout(function () {
            this.animate({"fill-opacity": .5}, 200);
    });

    //SETTING CLASS AND ID
    p1_101.node.setAttribute('class','reserved'); // CLASS 'reserved' OR 'sold'
    p1_101.node.id = 'p1_101';

    //OBJECT 102
    var p1_102 = p1.path("M337.583,149.023 342.667,196.69 342.333,197.023 342.833,202.107 365.667,199.857 365.167,194.523 364.667,194.357 359.917,150.357 356,150.773 355.75,147.19z").attr({"fill":"#d3ae6b", "stroke-width": 0, "fill-opacity": .5, "stroke": "transparent","cursor": "pointer", "title": "102"}).click(function () {
        $.fancybox.open([
            {
                href : 'img/rzuty/p1/102.jpg',
                openEffect : 'elastic',
                openSpeed  : 150,
                closeEffect : 'elastic',
                closeSpeed  : 150,
                padding: 2,
            }
        ], {
            beforeShow : function() {
                $('').appendTo(".fancybox-inner");
            }
        });
    }).mouseover(
        function () {
            this.animate({"fill-opacity": .8}, 200);
        }).mouseout(function () {
            this.animate({"fill-opacity": .5}, 200);
    });

    //SETTING CLASS AND ID
    p1_102.node.setAttribute('class','sold'); // CLASS 'reserved' OR 'sold'
    p1_102.node.id = 'p1_102';

    //OBJECT 103
    var p1_103 = p1.path("M314.5,155.107 319.167,197.607 319.167,199.19 318.667,199.44 319.25,204.357 342,202.273 341.583,197.023 340.833,196.607 340.833,195.357 335.917,149.357 331.833,149.607 316.5,151.107 316.75,154.607z").attr({"fill":"#d3ae6b", "stroke-width": 0, "fill-opacity": .5, "stroke": "transparent","cursor": "pointer", "title": "103"}).click(function () {
        $.fancybox.open([
            {
                href : 'img/rzuty/p1/103.jpg',
                openEffect : 'elastic',
                openSpeed  : 150,
                closeEffect : 'elastic',
                closeSpeed  : 150,
                padding: 2,
            }
        ], {
            beforeShow : function() {
                $('').appendTo(".fancybox-inner");
            }
        });
    }).mouseover(
        function () {
            this.animate({"fill-opacity": .8}, 200);
        }).mouseout(function () {
            this.animate({"fill-opacity": .5}, 200);
    });

    //SETTING CLASS AND ID
    p1_103.node.setAttribute('class','reserved'); // CLASS 'reserved' OR 'sold'
    p1_103.node.id = 'p1_103';

Well you could author a jQuery plugin. That would have default settings, with the option of passing some parameters and override settings.

General ideas behind this are :

Making reusable code. Another project with similar stuff? You include the lib and re-use it.
Making the code as dry as possible. Writing the same thing again and again is annoying and hard to refactor. In a plugin everything is wrote once.
Even though you want everything to look the same, you have to build it a way you can override default setting, because there's always one object that need to be different.

I haven't debug the whole  plugin as I had no idea what did a paper object looked lik. But it seemed like it was a jQuery built object as you called .attr, .mouseover and .mouseout which are jQuery function. Though I have debugged the rest of the plugin without calling .path and it was working.

To call the plugin just do something like :

var basePath = 'img/rzuty/p1/';
paperObject.animatePath('M36.146...', 101, basePath);
paperObject.animatePath('M337.583...', 102, basePath);
//...

So the plugin code looks like that :

(function ($) {
    /**
     *
     * @param path the paper path
     * @param imageNumber the image number which is used in href and pdf and for the id
     * @param basePath the base path for the image
     * @param options option to override defaults settings
     */
    $.fn.animatePath = function (path, imageNumber, basePath, options) {

        //Default settings
        var defaults = {
            htmlAttr: {
                'class': 'reserved',
                    'id': 'ap_' + imageNumber
            },
            cssOptions: {
                fill: '#d3ae6b',
                    'stroke-width': 0,
                    'fill-opacity': 0.5,
                stroke: 'transparent',
                cursor: 'pointer',
                title: imageNumber
            },
            fancyboxOptions: {
                href: basePath + imageNumber + '.jpg ',
                openEffect: 'elastic',
                openSpeed: 150,
                closeEffect: 'elastic',
                closeSpeed: 150,
                padding: 2
            },
            fancyboxAnimations: {
                beforeShow: function () {
                    $('')
                        .appendTo(".fancybox-inner");
                }
            },
            mouseOver: function () {
                $(this).animate({
                    'fill-opacity': 0.8
                }, 200);
            },
            mouseOut: function () {
                $(this).animate({
                    'fill-opacity': 0.5
                }, 200);
            }

        };

        //Merge settings
        var settings = $.extend(true, defaults, options || {});

        //Add the fancybox and stuff
        this.path(path)
            .attr(settings.htmlAttr)
            .css(settings.cssOptions);
        this.click(function () {
            $.fancybox.open([settings.fancyboxOptions], settings.fancyboxAnimations);
        });
        this.mouseover(settings.mouseOver);
        this.mouseout(settings.mouseOut);

        return this;
    };
})(jQuery);

As you can see every default settings is defined in an object and you can override it by passing it in the options object (which is then merge recursively with the defaults object). The nice thing is that it is now easily reusable. Also, using function($){}(jQuery); enclose the function scope so that it doesn't clash with any other library using $.

Here is a link to a fiddle, be free to update it with the paperObject, because I couldn't do it not knowing what it was.

To learn more about jQuery Authoring.


Idiomatic text munging in Clojure In getting accustomed to Clojure syntax, I wrote the following stutter function:

(defn doublelist [coll]
  (flatten (map (fn [x] [x x]) coll)))

(defn stutter [s]
  (clojure.string/join
    " " (doublelist (clojure.string/split s #" "))))

This duplicates every word in an input string:

(stutter "how are you?")
"how how are are you? you?"

The doublelist function bugs me. It seems that repeating items in a list should be possible without a call to flatten.

Any thoughts?

You may use mapcat to omit flatten:

user> (defn doublelist [coll]
        (mapcat #(repeat 2 %) coll))
#'user/doublelist
user> (doublelist [:a :b :c :d])
(:a :a :b :b :c :c :d :d)


Correctness and Performance of graph plotting on R Current State:

I have two CSV files, their sample attached below:

FileOutputWithoutBuffer-Metrics.csv

File Length , Non-Buffered Time Taken (ns)
1000 , 5499114
2000 , 10971957
3000 , 15736008
4000 , 18970057
5000 , 22173215
6000 , 24612263
7000 , 26118520
8000 , 29934220
9000 , 31919477
10000 , 34940645

FileOutputWithBuffer-Metrics.csv

File Length , Buffered Time Taken (ns)
1000 , 412991
2000 , 224509
3000 , 269990
4000 , 461664
5000 , 485668
6000 , 479069
7000 , 413657
8000 , 438734
9000 , 760068
10000 , 576458

What I want:

I want to plot a compare and contrast graph between these two CSV files corresponding to their File Length column.

What I did:

I wrote the below script for the same:

# This line has to be updated on every place, possible.
scriptPath <- "XXX/XXX/XXX/XXX";

# Read in all csv files.
FileOutputWithoutBufferMetrics <- read.csv(file = file.path(scriptPath, "FileOutputWithoutBuffer-Metrics.csv"), header = TRUE, sep = ",");
FileOutputWithBufferMetrics    <- read.csv(file = file.path(scriptPath, "FileOutputWithBuffer-Metrics.csv"), header = TRUE, sep = ",");

FileOutputMetircs <- merge(FileOutputWithoutBufferMetrics, FileOutputWithBufferMetrics, by=c('File.Length'), all=T);
#colnames(FileOutputMetircs) <- c("File Length", "Non-Buffered OutputStream Time", "Buffered OutputStream Time");
show(FileOutputMetircs);

# Install extra packages :- ggplot2, reshape2.
list.of.packages <- c("ggplot2","reshape2");
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])];
if(length(new.packages)) {
    install.packages(new.packages)
}
require(ggplot2);
require(reshape2);

# Create a graph of FileOutputStream.
df <- melt(FileOutputMetircs, id.vars = 'File.Length', variable.name = 'Time');
ggplot(df, aes(File.Length, value)) + geom_line(aes(colour = Time));

And got this output:

Where help is needed:

First, since I am very new to ggplot. Therefore, need assurity on the correctness of my R script.
Other ways (performace-wise) to optimize the script.

Thanks!.

The plot looks about right with the data, and I see no egrerious errors in your code. I do have a few remarks.

In r it's unnecessary to end your lines with a semi-colon, and most styleguides advise against it. The only real purpose it has is allowing multiple statements on a single line, but why would we want that.
With regards to your code where you install packages. There are two ways of loading a library: library(name) and require(name). One big difference is that require returns a FALSE when the package is missing, library throws an error. What you can do is try and load the library, and if it fails install it:

for(libraryName in c("ggplot2", "reshape2")){
  if(!require(libraryName)){
    install.package(libraryName)
    require(libraryName)
  }
}

If your csv files are going to be huge, I recommend using data.table::fread for reading your csv files. In my experience it is the fastest reading large datasets. It also allows for efficient dataframe operations.
If at reading time you specify the correct columnnames, you can prevent having to do the melt operation later: add the argument col.names = c("File.Length", "value"). Add the column FileOutputWithoutBufferMetrics["Time"] <- "Non.Buffered.Time.Taken..ns." to both dataframes, and simply rbind them. This is most likely more efficient than melt, especially on big datasets.
# This line has to be updated on every place, possible. This is generally accounted for using the "working directory" of the R process. If you run the script from commandline, the working directory is the current directory. If you run it from Rstudio you can set it using the setwd(dir) function, or in Session -> Set Working Directory -> To Source File Location in the Rstudio ui. Once that is set, you don't have to add that path to the filename in read.csv.

In summary:

for(libraryName in c("ggplot2", "data.table")){
  if(!require(libraryName)){
    install.package(libraryName)
    require(libraryName)
  }
}

FileOutputWithoutBufferMetrics <- fread("FileOutputWithoutBuffer-Metrics.csv"), col.names = c("File.Length", "value"))
FileOutputWithoutBufferMetrics["Time"] <- "Non.Buffered.Time.Taken..ns."

FileOutputWithBufferMetrics    <- fread("FileOutputWithBuffer-Metrics.csv"), col.names = c("File.Length", "value"))
FileOutputWithBufferMetrics["Time"] <- "Buffered.Time.Taken..ns."

df <- rbind(FileOutputWithoutBufferMetrics, FileOutputWithBufferMetrics)

ggplot(df, aes(File.Length, value)) + geom_line(aes(colour = Time))


Verify if the element is readonly or disabled I have written a function where it checks if the field is read-only or disabled. However, first it check if that element field is present

This is my working function, but i feel like this is too messy:

def verifyReadOnly(self):
        if self.driver.find_element_by_xpath("//input[@id='cname']"):
            self.driver.find_element_by_xpath("//input[@id='cname' and @readonly='readonly']")
        elif self.driver.find_element_by_xpath("//input[@id='address']"):
            self.driver.find_element_by_xpath("//input[@id='address' and @readonly='readonly']")
        elif self.driver.find_element_by_xpath("//input[@id='address2']"):
            self.driver.find_element_by_xpath("//input[@id='address2' and @readonly='readonly']")
        elif self.driver.find_element_by_xpath("//input[@id='city']"):
            self.driver.find_element_by_xpath("//input[@id='city' and @readonly='readonly']")
        # country
        elif self.driver.find_element_by_xpath("//input[@id='country']"):
            self.driver.find_element_by_xpath("//input[@id='country']/../select[@disabled='disabled']")
        elif self.driver.find_element_by_xpath("//input[@id='state']"):
            self.driver.find_element_by_xpath("//input[@id='state' and @readonly='readonly']")
        elif self.driver.find_element_by_xpath("//input[@id='zip']"):
            self.driver.find_element_by_xpath("//input[@id='zip' and @readonly='readonly']")
        # EIN Custom field
        elif self.driver.find_element_by_xpath("//input[@id='cf_2698238']"):
            self.driver.find_element_by_xpath("//input[@id='cf_2698238' and @readonly='']")

There are several major issues in the code:

violates the DRY principle
there is actually no element presence check - find_element_* method will fail with NoSuchElementException if the element is not found (well, you may be handling the exception outside of the method, but that scatters the logic too much resulting into more "spaghetti code")

Instead, I would follow the Page Object pattern and build a "Page Element" abstraction, where every element will have the read_only property:

class BasePageElement(object):
    # other methods here

    @property
    def read_only(self):
        elm = self.driver.find_element(*self.locator)
        return bool(elm.get_attribute("readonly"))

class AddressElement(BasePageElement):
    locator = (By.ID, "address")

Note that I'm using the By.ID locator here and the .get_attribute() method as opposed to less readable and more fragile XPath approach.

This, though, does not handle the "element is not present" case properly - you may wrap the .find_element() call with try/except or think of a more generic way to handle it.


Constructing a class from a line in a parameter file I've got a problem where I need to parse a line in a parameter file of the form

PotentialName [... variable number of parameters]

I've got code which works however it seems like it will be horrendous to maintain when I add more than a few options here.

What would be the best design pattern to avoid the long chain of else ifs and what looks like (to my eyes) quite a bit of boilerplate for each option I add.

std::shared_ptr<Potential> construct_potential( std::istringstream& iss )
{
    std::string pot_type;
    iss >> pot_type;

    if( pot_type == "NFW" ) {
        double rho0, rs;
        iss >> rho0 >> rs;
        return std::make_shared<NFW>(rho0, rs);
    }
    else if( pot_type == "MiyamotoNagai" ) {
        double m, a, b;
        if( iss >> m >> a >> b ) {
            return std::make_shared<MiyamotoNagai>(m, a, b);
        } else {
            throw ParseError("Expected 3 parameters for " + pot_type + " potential");
        }
    }
    else if( pot_type == "Kepler" ) {
        double m;
        if( iss >> m ) {
            return std::make_shared<Kepler>(m);
        } else {
            throw ParseError("Expected 1 parameter for " + pot_type + " potential");
        }
    }
    else {
        throw ParseError("Unknown Potential: " + pot_type);
    }
}

How can I improve this?

The approach to doing this is two-fold. Firstly, you'll want to modify your classes to have a constructor taking an std::istringstream& as an argument. The second part will be to make a map of strings to constructor functions. So let's assume our (skeleton) classes look something like:

class Potential
{
public:

    virtual ~Potential() { }

    virtual void print() const = 0;

};

class NFW
  : public Potential
{
private:

    double rho0;
    double rs;

public:

    NFW(std::istringstream& iss)
    {
        iss >> rho0 >> rs;
    }

    virtual ~NFW() = default;

    virtual void print() const override
    {
        std::cout << "NFW: rho0: " << rho0 << ", rs: " << rs << "\n";
    }
};

class MiyamotoNagai
  : public Potential
{
private:

    double m, a, b;

public:

    MiyamotoNagai(std::istringstream& iss)
    {
        iss >> m >> a >> b;
    }

    virtual ~MiyamotoNagai() = default;

    virtual void print() const override
    {
        std::cout << "MiyamotoNagai m: " << m << ", a: " << a
                  << ", b: " << b << "\n";
    }
};

We then define the type our map will hold: it will hold a function that will return a std::shared_ptr<Potential>, and will take a std::istringstream& as an argument:

using function_t = std::function<std::shared_ptr<Potential>(std::istringstream&)>;

Using this, our map becomes:

std::unordered_map<std::string, function_t> register_map;

To add to this, we just need some very simple lambda functions that forward to the corresponding constructor:

register_map["NFW"] = [](std::istringstream& iss) { return std::make_shared<NFW>(iss); };
register_map["MiyamotoNagai"] = [](std::istringstream& iss) { return std::make_shared<MiyamotoNagai>(iss); };

The function to actually create our Potential subclass then looks like:

std::shared_ptr<Potential> create_potential(std::istringstream& iss)
{
    std::string potential_type;
    iss >> potential_type;
    auto it = register_map.find(potential_type);
    if(it == register_map.end()) {
        throw ParseError("Unknown Potential " + potential_type);
    }
    return it->second(iss);
}

With this, each constructor will need to do its own checking and error handling (which I've omitted above). As written, this better adheres to the open-closed principle (code should be open for extension, closed for modification), as the only thing you should have to touch when you create another subclass is adding another call to register_class[...].

A test example:

int main()
{
    register_map["NFW"] = [](std::istringstream& iss) { return std::make_shared<NFW>(iss); };
    register_map["MiyamotoNagai"] = [](std::istringstream& iss) { return std::make_shared<MiyamotoNagai>(iss); };
    std::istringstream iss("NFW 5 4");
    auto test = create_potential(iss);
    test->print();
}

As a final point, make absolutely sure this needs a shared_ptr over a unique_ptr. Without context it is impossible to tell, but you should always reach for unique_ptr first, and shared_ptr only if it is absolutely necessary; it may be in this case, but it is still something to very carefully think about.


Song repetition manager with Stack The exercise was to take a file such as this:

You say yes
I say no
You say stop
And I say go go go
CHORUS:
Oh no
You say Goodbye
And I say hello
Hello hello
I don't know why
You say Goodbye
I say hello
#repeat 9 12
Why
#repeat 11 13
I say high
You say low
You say why
And I say I don't know
#repeat 5 19

And parse the repeats so they actually repeat the lines. Nested repeats have to work and I had to use my own stack class.

main.cpp

#include "Stack.h"
#include <fstream>
#include <iostream>
#include <string>
#include <vector>

void parseLines(const std::vector<std::string> text, const int begin, const int end);
void parseRepeat(const std::string repeatLine, int &begin, int &end);

int main() {
    int fileLines = 0;

    std::string in;
    std::string fileName = "lyrics";
    std::ifstream inFile(fileName.c_str());
    std::vector<std::string> text;

    while(!inFile.eof()) {
        getline(inFile, in);
        text.push_back(in);
        fileLines++;
    }

    parseLines(text, 0, fileLines);
}

void parseLines(const std::vector<std::string> text, const int begin, const int end) {
    Stack<int> lyricStack;

    int fromLine = begin;
    int toLine = end;

    while(fromLine <= toLine && (unsigned int)fromLine < text.size()) {
        if(text.at(fromLine).substr(0, 7) == "#repeat") {
            lyricStack.push(fromLine);
            lyricStack.push(toLine);

            parseRepeat(text.at(fromLine), fromLine, toLine);
        } else {
            std::cout << text.at(fromLine) << std::endl;
            fromLine++;
        }

    }

    while(!lyricStack.isEmpty()) {
        toLine = lyricStack.pop();
        fromLine = lyricStack.pop();

        fromLine++;

        parseLines(text, fromLine, toLine);
    }
}

void parseRepeat(const std::string repeatLine, int &begin, int &end) {
    std::string numbers = repeatLine.substr(repeatLine.find(" ") + 1);
    begin = atoi((numbers.substr(0, numbers.find(" "))).c_str()) - 1;
    end = atoi((numbers.substr(numbers.find(" "))).c_str()) - 1;
}

Stack.h

#ifndef STACK_H_
#define STACK_H_

#include <string>

template <class A>
struct NODE {
    A data;
    NODE* next;
};

template <class B>
class Stack {
private:
    NODE<B>* head;

public:
    Stack();
    virtual ~Stack();

    void push(const B item);
    B pop();
    bool isEmpty();
};

#endif /* STACK_H_ */

Stack.cpp

#include "Stack.h"
#include <string>

template <class B>
Stack<B>::Stack() {
    head = NULL;
}

template <class B>
Stack<B>::~Stack() {
    NODE<B>* current = head;
    NODE<B>* previous;

    while(current) {
        previous = current;
        current = current->next;
        delete previous;
    }
}

template <class B>
void Stack<B>::push(const B item) {
    NODE<B>* newNode = new NODE<B>;
    newNode->data = item;

    if(!head) {
        newNode->next = NULL;
        head = newNode;
    } else {
        newNode->next = head;
        head = newNode;
    }
}

template <class B>
B Stack<B>::pop() {
    NODE<B>* next = head->next;

    B data = head->data;

    delete head;

    head = next;

    return data;
}

template <class B>
bool Stack<B>::isEmpty() {
    if(!head) {
        return true;
    }

    return false;
}

First main problem is the code does not compile:

When you implement template classes. The compiler instantiates the templates on use. This means the compilation unit that is instantiating the class must have already seen the source for the template (if it has not then it marks it as unresolved) and then tries to resolve them at link time. In your case this does not happen.

main.cpp: Uses Stack<int> thus needs the constructor and destructor.
          But does not have them at this point. So waits for the linker to find one.

Stack.cpp: Contains only template definition and thus does **NOT** generate any code.
           Templates only become real code when there is an instantiation
           (implicit as done by the compiler in main.cpp or explicit when you do it).
           Since there is no instantiation there is no code.

This all means that main fill fail to link.

The easiest way to solve this:

Rename Stack.cpp to Stack.tpp
#include "Stack.tpp" from inside "Stack.h"

The reason to rename to Stack.tpp is that some build systems will try to build all source and since this is included by a header this can cause problems with some build systems. So it is best to change the name just to avoid any future problems. tpp is often used to hold template definitions.

Never do this:

while(!inFile.eof()) {
    getline(inFile, in);
    text.push_back(in);
    fileLines++;
}

Several problems:

If getline() fails with a real error (not EOF) you end up with an infinite loop.
The last line read reads up to (but not past the EOF).
So you have read the last line but EOF is not set. You then re-enter the loop try to get another line, this fails and sets EOF but nothing is done with in yet you still push in onto text. So you end up pushing the last line twice.

The correct way to read from a file is:

while(getline(inFile, in))
{
    // The loop is only entered if getline() successfully reads a line.
    // Thus it solves the two problems above.

    text.push_back(in);
    fileLines++;
}

This works because getline() returns a reference to a stream. When a stream is used in a boolean context it is converted to a type that can be used as a boolean (type unspecified in C++03 but bool in C++11). It is converted by checking the fail() flag. If this is true the conversion returns an object equivalent to false otherwise an object equivalent to true.

When passing big objects as parameters consider passing them by reference. If they are never modified pass by const reference. If you don't the compiler is going to generate a copy.

void parseLines(const std::vector<std::string> text, const int begin, const int end) {

Here you should probably pass text by const reference.

void parseLines(const std::vector<std::string> const& text, const int begin, const int end) {
//                                            ^^^^^^^^

Same thing here:

void parseRepeat(const std::string repeatLine, int &begin, int &end) {
// Should probably be
void parseRepeat(const std::string& repeatLine, int &begin, int &end) {
//                               ^^^

Casting is nearly always a sign that something is wrong with your design:

(unsigned int)fromLine

This should be a hint that fromLine is the wrong type. Since it is measuring a size and will never by less than 0 this is an indication that the correct type for the type is unsigned int (or probably size_t).

void parseLines(const std::vector<std::string> text, std::size_t const begin, std::size_t const end) {
    Stack<int> lyricStack;

    std::size_t fromLine = begin;
    std::size_t toLine   = end;

    while(fromLine <= toLine && fromLine < text.size()) {

atoi() is fast but non standard and not always available. An easier way is to just use the stream operators. This will always work and 99% of the time will be sufficiently fast. Only optimize out when you know it makes a difference.

Also this code is so dense it is nearly unreadable. White space is your friend.

void parseRepeat(const std::string repeatLine, int &begin, int &end)
{
    std::string numbers = repeatLine.substr(repeatLine.find(" ") + 1);
    begin = atoi((numbers.substr(0, numbers.find(" "))).c_str()) - 1;
    end = atoi((numbers.substr(numbers.find(" "))).c_str()) - 1;
}

Much easier to write with streams:

void parseRepeat(const std::string repeatLine, int &begin, int &end)
{
    // This line always has the form
    // #repeat <number> <number>
    std::stringstream   linestream(repeatLine);
    std::string         repeat;

    linestream >> repeat >> begin >> end;
}

Both NODE and Stack contain RAW pointers. Yet they do not obey the rule of three. This is very dangerous. The simple rule is your classes should never contain RAW pointers (all pointers should be wrapped in smart pointers) unless you are writing a smart pointer or container.

In this situation NODE would be easy to implement using smart pointer.

Also you do a work handling node inside stack. It simplifies the design if you move this work to the constructor of NODE. This makes the code in Stack much easier to read as it just handles the stack and the NODE can handle itself.

The Stack is arguably a container so it can have a pointer but you must implement the rule of three. This means you need to implement the Copy Constructor/Assignment operator/Destructor or make these methods private. The reason is that if you do not then the compiler will automatically  generate these methods. And the compiler generated version does not plat well with "OWNED RAW pointers".

{
    Stack<int>   a;
    a.push(4);

    Stack<int>   b(a);   // Copy made of a

    DO STUFF
} // Here B is destroyed.
  // But because you did not define a copy constructor b is a shallow copy of a
  // This means they both contain the same pointer. So b deletes it first.
  // Now A is destroyed.
  // This also deletes its pointer (the same as b) but it has already been deleted.
  // Your program is now free to produce nasal daemons (undefined behavior).

The simplest solution is to disable copy construction and the assignment operator.

You have over complicated push:

if(!head) {
    newNode->next = NULL;
    head = newNode;
} else {
    newNode->next = head;
    head = newNode;
}

Would this not be easier to write as:

newNode->next = head;
head          = newNode;

Also isEmpty() is over complicated:

if(!head) {
    return true;
}

return false;

Could be written as:

return head == NULL;


Own implementation of Lazy object The problem with the original Lazy in C# is that you have to put the initialization in the constructor if you want to refer to this.

For me that is 95% of the cases. Meaning that I have to put half of my logic concerning the lazy property in the constructor, and having more boilerplate code.

See this question: https://stackoverflow.com/questions/53271662/concise-way-to-writy-lazy-loaded-properties-in-c-sharp/53316998#53316998

Now I made a new version where I actually move the initialization part to the 'Get' moment. This saves boilerplate code and I can group everything about this property together.

But:

is the current implementation 'safe'?
are there any important performance considerations vs the original one?
is there anything else I'm missing that I should be concerned about in a high traffic application?

Class:

public class MyLazy
{
    private object  _Value;
    private bool    _Loaded;
    private object  _Lock = new object();

    public T Get<T>(Func<T> create)
    {
        if ( _Loaded )
            return (T)_Value;

        lock (_Lock)
        {
            if ( _Loaded ) // double checked lock
                return (T)_Value;

            _Value   = create();
            _Loaded = true;
        }

        return (T)_Value;
    }

    public void Invalidate()
    {
        lock ( _Lock )
            _Loaded = false;
    }
}

Use:

MyLazy _SubEvents = new MyLazy();
public SubEvents SubEvents => _SubEvents.Get(() =>
{
    // [....]
    return x;
});

Or like:

MyLazy _SubEvents = new MyLazy();
public SubEvents SubEvents => _SubEvents.Get(LoadSubEvents);

public void LoadSubEvents()
{
    // [....]
    return x;
}

is the current implementation 'safe'?

Absolutely not. The fact that you had to ask this question indicates that you do not understand enough about threading to build your own mechanisms like this. You need to have a deep and thorough understanding of the memory model to build these mechanisms. That is why you should always rely on the mechanisms provided for you in the framework, that were written by experts..

Why is it unsafe? Consider the following scenario. We have two threads, A and B. _Value is null and _Loaded is false.

We're on thread A.
The memory location of _Value is loaded into the processor cache for the CPU that thread A is affinitized to. It is null.
We switch to thread B.
Thread B reads _Loaded as false, takes the lock, checks _Loaded again, calls create, assigns _Value and _Loaded and leaves the lock.
We switch back to thread A.
_Loaded is now true, so thread A returns _Value from the processor cache, which is null.

Thread A is not required to invalidate the cache because thread A never takes a lock.!

Now, I made an argument here from processor caches. This is the wrong argument to make in general. Rather, what you must do when trying to build a new threading mechanism like this is to not reason about any specific processor architecture, but rather to reason about the abstract memory model of the C# language.  C# permits reads and writes to move forwards and backwards in time in multithreaded programs.  Any time travel that is not explicitly forbidden by the C# specification must be considered to be possible.  Your task is to then write code that is correct for any possible combination of movements of reads and writes in time regardless of whether they are really possible on a specific processor or not.

Note that in particular the C# specification does not require that all threads observe a consistent set of write and read re-orderings. It is perfectly legal and possible for two threads to disagree on how a read was re-ordered with respect to a write.

If writing correct programs in a world where all reads and writes can be moved around in time sounds hard, that's because it is. I am not competent to do this work, and I do not attempt to. I leave it to experts.

  are there any important performance considerations vs the original one?

Only you can answer that question.  Answer performance questions by gathering real-world empirical data.

However, I can say a few things about this problem in general.

The first is: double-checked locking is intended to avoid the cost of the lock. Let's examine the assumptions underlying that intention.  The assumption is that the cost of taking the lock is too high on the uncontended path.  Is that assumption warranted? What is the cost of taking an uncontended lock? Did you measure it? Did you compare it against the cost of the lock-avoiding check?  (Since the lock-avoiding check code is wrong, testing it for performance is not actually meaningful since we can always write faster code if we don't care about correctness, but still, we need to know whether this intervention is an improvement.)  And most importantly, is the cost of taking an uncontended lock relevant to the consumer of this code?  Because they are the stakeholder whose opinions are relevant; what do they say about the cost of an uncontended lock?

Let's suppose that the cost of an uncontended lock is relevant. Then surely the cost of a contended lock is enormously relevant.  You've built a mechanism that potentially contends a lot of threads!  What are the alternatives that you considered here?  For example, you could avoid the lock altogether by deciding that it is OK for the create function to be called on multiple threads -- perhaps we know that it is cheap and idempotent.  Those threads can then race to their heart's content to initialize the field, and we can use an interlocked exchange to ensure that we get a consistent value. That avoids the cost of the lock altogether, but it creates a different kind of cost, and puts a requirement on the caller to pass an idempotent creator.

Let's consider other aspects of your solution with respect to performance. You allocate the lock object regardless of whether you ever take the lock, and you keep it forever.  What's the burden on the garbage collector? What is the impact on collection pressure? These things are all deeply relevant to performance.  Again, remember, the assumption here is that we are so worried about the couple of nanoseconds it takes to enter and leave an uncontended lock that we're willing to write a double checked lock.  If those nanoseconds are relevant then surely the milliseconds it takes to do an extra collection are incredibly relevant!

  is there anything else I'm missing that I should be concerned about in a high traffic application?

I don't know how to answer that question.


Karazuba Algorithm with arbitrary bases I'm working on an implementation of the Karazuba Algorithm. I created a class  BN which represents a number in an arbitrary base (and supports elementary operations). The algorithm works - but I'm not sure if I complicated it. Is there a better way to implement it or a way to optimize the code?

def decToBase(n: int, b: int) -> list:
    if n == 0:
        return [0]
    digits = []
    while n:
        digits.append(int(n % b))
        n //= b
    return list(reversed(digits))

def baseToDec(n: list, b: int) -> int:
    res = 0
    for index, i in enumerate(reversed(n)):
        res += int(i) * b ** index
    return res

from math import log2, ceil, log10, floor

class BN(): # Base Number
    def __init__(self, num, base):
        if type(num) == list:
            self.digits = num
        else:
            self.digits = decToBase(num, base)
        self.base = base
        self.length = len(self.digits)

    def __add__(self, o):
        if self.base != o.base:
            raise ValueError("base of summands must be equal")
        carry = 0
        res = []
        for d1, d2 in zip(reversed(self.digits), reversed(o.digits)):
            s = int(d1) + int(d2) + carry
            res.append(str(s % self.base))
            carry = s // self.base
        return BN(list(reversed(res)), self.base)

    def __mul__(self, o):
        # implementation pending
        return BN(decToBase(self.getDecimalForm() * o.getDecimalForm(), self.base), self.base)

    def __sub__(self, o):
        # implementation pending
        return BN(decToBase(self.getDecimalForm() - o.getDecimalForm(), self.base), self.base)

    def getDecimalForm(self):
        return baseToDec(self.digits, self.base)

def karazubaMultiply(a, b, base=2**64):
    if a < b:
        a, b = b, a # ensure a >= b
    next2 = 2 ** ceil(log2(floor(log10(a))+1)) # next power of 2
    a, b = BN(a, base), BN(b, base)
    a.digits, b.digits = ['0'] * (next2 - a.length) + a.digits, ['0'] * (next2 - b.length) + b.digits
    n = next2 // 2
    x2, x1 = BN(a.digits[:n], base), BN(a.digits[n:], base)
    y2, y1 = BN(b.digits[:n], base), BN(b.digits[n:], base)
    p1, p2, p3 = x2 * y2, x1 * y1, (x1 + x2) * (y1 + y2)
    p1, p2, p3 = p1.getDecimalForm(), p2.getDecimalForm(), p3.getDecimalForm()

    xy = p1 * base ** (2*n) + (p3 - (p1 + p2)) * base ** n + p2

    return xy

Combine division and modulus

Use https://docs.python.org/3/library/functions.html#divmod

rather than this:

    digits.append(int(n % b))
    n //= b

Replace loop with sum

This

res = 0
for index, i in enumerate(reversed(n)):
    res += int(i) * b ** index
return res

is a good candidate for conversion to a generator, i.e.

return sum(int(i) * b**index for index, i in enumerate(reversed(n)))

However, you can get rid of that reversed if you change the power index to be len(n) - index.

Imports at the top

Move this:

from math import log2, ceil, log10, floor

to the top of your file.

Don't compare types

Rather than this:

if type(num) == list:

use isinstance.

Member mutability

You shouldn't care that num is coming in as a list; you should only check that it's iterable. And store it as a tuple, not a list, because you don't (and shouldn't) mutate it in your class code.

Call reversed once

This:

zip(reversed(self.digits), reversed(o.digits))

should be

reversed(tuple(zip(self.digits, o.digits)))

The inner tuple is required because reversed does not accept an iterator.

Don't use strings for math

Just don't. This:

a.digits, b.digits = ['0']

is not a good idea. Use actual digits whenever possible, and it is possible here.


Possible Test Cases in Tester I am trying to write a human-readable tester for a functioning program.

There are four methods in the class Polynomial: public Polynomial(int[] coefficients), public int getDegree(), public int getCoefficient(int k), public long evaluate(int x).

Where coefficients is an immutable array of elements that represent a polynomial. For example, the polynomial 6x^3 + 4x + 3 would be represented by its coefficients as [3, 4, 0, 6]. The method getDegree() determines the highest order term that has a non-zero coefficient. For example, the degree would equal 3 in the previous example. getCoefficient(int k) returns the coefficient of element k. For example, in the previous example, if k = 2 the returned value is 0. The method evaluate(int x) evaluates the polynomial with the value of x. In the previous example, the value returned when x = 2 would be 59.

This is the tester I've written so far:

@Test public void testPolynomial()
    {
        int [] c1 = {0, 0, 0, 1};
        Polynomial p1 = new Polynomial(c1);
        c1[2] = 1;
        assertEquals("Degree error", 3, p1.getDegree());
        assertEquals("Polynomial must compute coefficients out of range", 0, p1.getCoefficient(-1));
        assertEquals("Polynomial must compute coefficients out of range", 0, p1.getCoefficient(4));
        assertEquals("Array is not immutable", 0, p1.getCoefficient(2));
        assertEquals("Coefficient error near boundary", 1, p1.getCoefficient(3));
        assertEquals("Coefficient error near boundary", 0, p1.getCoefficient(0));
        assertEquals("Coeffificent error", 0, p1.getCoefficient(1));
        assertEquals("Evaluation error", 0, p1.evaluate(0));
        assertEquals("Evaluation error of negative x", -27, p1.evaluate(-3));

        int[] c2 = {-1, 0, 0, 0};
        Polynomial p2 = new Polynomial(c2);
        c2[2] = 4;
        assertEquals("Degree error", 0, p2.getDegree());
        assertEquals("Polynomial must compute coefficients out of range", 0, p2.getCoefficient(-1));
        assertEquals("Polynomial must compute coefficients out of range", 0, p2.getCoefficient(4));
        assertEquals("Array is not immutable", 0, p2.getCoefficient(2));
        assertEquals("Coefficient error near noundary", 0, p2.getCoefficient(3));
        assertEquals("Coefficient error near noundary", -1, p2.getCoefficient(0));
        assertEquals("Evaluation error", -1, p2.evaluate(0));
        assertEquals("Evaluation error of negative x", -1, p2.evaluate(-3));

    }

Are there any other test cases that anyone can think of that I could include into my tester or that I'm missing?

I'm not going to address what else you could test, since it really depends what you're expecting your Polynomial class to do.  However, looking at your test, there's several things that could be improved.
Test One Thing
Having all of your assertions in the same test means that the test can fail for multiple different reasons, some of which are unrelated.  Consider modifying your tests so that they contain the complete AAA for what they are testing.  This makes it much clearer what the relationship between the data setup in the Arrange phase is, to the Assertions made.
As an example, your code current does this:

int [] c1 = {0, 0, 0, 1};
Polynomial p1 = new Polynomial(c1);
c1[2] = 1;
assertEquals("Degree error", 3, p1.getDegree());
assertEquals("Polynomial must compute coefficients out of range", 0, p1.getCoefficient(-1));
assertEquals("Polynomial must compute coefficients out of range", 0, p1.getCoefficient(4));
assertEquals("Array is not immutable", 0, p1.getCoefficient(2));

There's three unrelated assertions between the point where you modify your array (c1[2] = 1) and the assertion that it impacts 'Array it not immutable'.  This make far less obvious what the 'c1[2] = 1' is doing there.
Having multiple tests, where each one tests only a single thing also means that you don't need to put a message in each of your assertEquals calls.  The test name itself tells you what you're testing.
Duplicate tests
Sometimes you want to test the same thing in different scenarios.  Other times, it doesn't make as much sense.  I'm pretty sure that if the array is immutable for c1, it will also be immutable for c2, testing it again doesn't seem like it's really adding value.  If you do want to test the same thing, with different values, consider using parameterised tests.  Something like this:
@ParameterizedTest
@MethodSource("coefficients_outOfRangeCoefficient")
void getCoefficient_outOfRange_computesZero(int[] coefficients, int outOfRangeCoefficient) {
    Polynomial polynomial = new Polynomial(coefficients);
    assertEquals(0, polynomial.getCoefficient(outOfRangeCoefficient));
}

static Stream<Arguments> coefficients_outOfRangeCoefficient() {
    return Stream.of(
            arguments(new int[] {0,0,0,1}, -1),
            arguments(new int[] {0,0,0,1}, -4),
            arguments(new int[] {-1,0,0,0}, -1),
            arguments(new int[] {-1,0,0,0}, 4)
    );
}

Would allow the same test to be used across four different combinations of coefficients and get indexes.


Reading words and counting their occurence in Java I have very typical problem:

Given big text file, containing text. The task is to calculate occurence of each word and print this info from most occured to less.

Example input:

cat cat cat dog dog mouse

Correct answer:

cat -> 3
dog -> 2
mouse -> 1

First I solved this using Scanner. But this solutions seems to be slow, cause IO was blocking.

After some time and cookie spent I wrote this solution:

final Path path = FileSystems.getDefault().getPath("/tmp/", "file.txt");

Map<String, Long> occ =
  Files
    .lines(path)
    .flatMap(line -> Arrays.stream(line.split(delimRegex)))
    .filter(line -> line.length() > 0)
    .map(String::toLowerCase)
    .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

occ
  .entrySet()
  .stream()
  .sorted((e1, e2) -> e2.getValue().compareTo(e1.getValue()))
  .forEach(e -> System.out.println(e.getKey() + " -> " + e.getValue()));

I think solution is good (but not sure). Is there any other optimization techniques I've ignored?

I have one comment:
Files.lines() method has the following comment in the JDK documentation:

  The returned stream encapsulates a Reader. If timely disposal of file
  system resources is required, the try-with-resources construct should
  be used to ensure that the stream's close method is invoked after the
  stream operations are completed.

in other words, in order to prevent resource leak, you need to construct the stream in a try-with-resources block

try (Stream<String> linesStream = Files.lines(path)) {
    Map<String, Long> occ = linesStream
        .flatMap(line -> Arrays.stream(line.split(delimRegex)))
        ....
}


Uncompress archive, check contents, add missing files, recompress archive The code looks for the specified .tgz file, if it's not present, it creates it. It looks for files ending in .txt in the cwd and adds them to a list. It decompresses the gzip, checks for the .txt files in the tar, adds the missing files to the tar and then recreates the compressed gzip with the new files in it. It then deletes the temporarily created tar file.

This is a self assigned script to learn the basics of working with compressed tarballs.

Please take a look and let me know if there are problems that can be accounted for or improvements that can be made. I'm only a few weeks into python, there is still a lot that I don't know.

#!usr/bin/python3

import os, gzip
import tarfile
import sys
import shutil
import send2trash

myFL = []
notThere = []
feil = '/home/pi/python/chap9OrganizingFiles/shutildir/comp.tgz'
outfeil = '/home/pi/python/chap9OrganizingFiles/shutildir/comp.tar'

if os.path.isfile(feil):
        print('File found, opening ' + os.path.relpath(feil))
else:
        print('File does NOT exist, creating ' + os.path.relpath(feil))
        with tarfile.open(feil, 'w:gz'): print('File Created')

with tarfile.open(feil, 'r:gz') as tar:
        myDirList = os.chdir('/home/pi/python/chap9OrganizingFiles/shutildir/')
        print('Current directory is: ' + os.getcwd() + '\n\n')
        for dirList in os.listdir(os.getcwd()):
                myFL.append(dirList)
        for listItem in myFL:
                if listItem in tar.getnames():
                        print("There: " + listItem)
                else:
                        if listItem.endswith('.txt'):
                                notThere.append(listItem)

print('\n\n')
for li in notThere: print('Text file not in tarball: ' + li)

with gzip.open(feil, 'rb') as unzip, open(outfeil, 'wb') as out:# open the gzip file
        out.write(unzip.read()) # read the gzip file into the output file

def addToTar(ntlist):
        with tarfile.open(outfeil, 'a') as tar:
                for ntLI in ntlist:
                        tar.add(ntLI)
                        print('Item added: ' + ntLI)

addToTar(notThere)

with open(outfeil, 'rb') as uf, gzip.open('/home/pi/python/chap9OrganizingFiles/shutildir/comp.tgz', 'wb') as of:
        shutil.copyfileobj(uf, of)

send2trash.send2trash(outfeil)
print('\nAll done and cleaned up.')

Remove unecessary code
myDirList is assigned but never used, delete it:
myDirList = os.chdir('/home/pi/python/chap9OrganizingFiles/shutildir/')

You do not need to build a list as:
for dirList in os.listdir(os.getcwd()):
        myFL.append(dirList)
for listItem in myFL:

You can iterate over os.listdir(os.getcwd()) directly:
for listItem in os.listdir(os.getcwd()):


Alcohol consumption project I am doing a Pandas project on alcohol consumption. For your information, the dataset has the following columns:

| Continent | Country | Beer | Spirit | Wine |

The following is my code:

# Separating data by continent
# ----------------------------
data_asia   = data[data['Continent'] == 'Asia']
data_africa = data[data['Continent'] == 'Africa']
data_europe = data[data['Continent'] == 'Europe']
data_north  = data[data['Continent'] == 'North America']
data_south  = data[data['Continent'] == 'South America']
data_ocean  = data[data['Continent'] == 'Oceania']

top_5_asia_beer = data_asia.nlargest(5, ['Beer Servings'])[['Country', 'Beer Servings']]
top_5_asia_spir = data_asia.nlargest(5, ['Spirit Servings'])[['Country', 'Spirit Servings']]
top_5_asia_wine = data_asia.nlargest(5, ['Wine Servings'])[['Country', 'Wine Servings']]
top_5_asia_pure = data_asia.nlargest(5, ['Total Litres of Pure Alcohol'])[['Country', 'Total Litres of Pure Alcohol']]

top_5_africa_beer = data_africa.nlargest(5, ['Beer Servings'])[['Country', 'Beer Servings']]
top_5_africa_spir = data_africa.nlargest(5, ['Spirit Servings'])[['Country', 'Spirit Servings']]
top_5_africa_wine = data_africa.nlargest(5, ['Wine Servings'])[['Country', 'Wine Servings']]
top_5_africa_pure = data_africa.nlargest(5, ['Total Litres of Pure Alcohol'])[['Country', 'Total Litres of Pure Alcohol']]

top_5_europe_beer = data_europe.nlargest(5, ['Beer Servings'])[['Country', 'Beer Servings']]
top_5_europe_spir = data_europe.nlargest(5, ['Spirit Servings'])[['Country', 'Spirit Servings']]
top_5_europe_wine = data_europe.nlargest(5, ['Wine Servings'])[['Country', 'Wine Servings']]
top_5_europe_pure = data_europe.nlargest(5, ['Total Litres of Pure Alcohol'])[['Country', 'Total Litres of Pure Alcohol']]

top_5_north_beer = data_north.nlargest(5, ['Beer Servings'])[['Country', 'Beer Servings']]
top_5_north_spir = data_north.nlargest(5, ['Spirit Servings'])[['Country', 'Spirit Servings']]
top_5_north_wine = data_north.nlargest(5, ['Wine Servings'])[['Country', 'Wine Servings']]
top_5_north_pure = data_north.nlargest(5, ['Total Litres of Pure Alcohol'])[['Country', 'Total Litres of Pure Alcohol']]

top_5_south_beer = data_south.nlargest(5, ['Beer Servings'])[['Country', 'Beer Servings']]
top_5_south_spir = data_south.nlargest(5, ['Spirit Servings'])[['Country', 'Spirit Servings']]
top_5_south_wine = data_south.nlargest(5, ['Wine Servings'])[['Country', 'Wine Servings']]
top_5_south_pure = data_south.nlargest(5, ['Total Litres of Pure Alcohol'])[['Country', 'Total Litres of Pure Alcohol']]

top_5_ocean_beer = data_ocean.nlargest(5, ['Beer Servings'])[['Country', 'Beer Servings']]
top_5_ocean_spir = data_ocean.nlargest(5, ['Spirit Servings'])[['Country', 'Spirit Servings']]
top_5_ocean_wine = data_ocean.nlargest(5, ['Wine Servings'])[['Country', 'Wine Servings']]
top_5_ocean_pure = data_ocean.nlargest(5, ['Total Litres of Pure Alcohol'])[['Country', 'Total Litres of Pure Alcohol']]

I understand the ridiculousness of my code in terms of duplicity and repetitiveness. Can anyone please share tips and tricks to refactor the code?

Depends on what you want to do with it. It seems a bit odd to store each top 5 in its own variable.

For starters, you can slice a DataFrame by continent using .groupby:

for continent, continent_data in data.groupby("Continent"):
    # `continent` is now the name of the continent (you don't have to type the continent names manually)
    # `continent_data` is a dataframe, being a subset of the `data` dataframe

Edit based on first comment: if you want to plot the variables, it's definitely not a good idea to store them each in a separate variable. Do you already know how you want to visualize your data? That's something you will need to work toward. I can't really see a top 5 countries for each type of alcoholic beverage for each continent in one plot.

continents = []
top5s = {}
for continent, continent_data in data.groupby("Continent"):
    continents.append(continent)
    for beverage_column in ["Beer Servings", "Spirit Servings", "Wine Servings"]:
        topcountries = continent_data.nlargest(5, beverage_column)
        # do something with the data, such as:
        print(f"Top 5 countries in {continent} for {beverage}:")
        for row in topcountries.iterrows():
            print(f"- {row.Country}: {row['beverage_column']} servings")

To be very exact: groupby() doesn't return an iterable of tuples, but actually just a GroupBy object that implements iterability (i.e. the __iter__() method).


Is there a better, more object oriented way to write this Finder method? I have several lists of objects which are stored in different locations, based on what they are and who needs them - the system, or the user, for example. There are times where I am only given an ID and need to find the name, type, or description, but I would like a very succinct, efficient way to do so. Since I'm also trying to improve the quality of my code, I want to ensure I'm doing things the best way possible.

Is there a better way to do this? I first was passing in the object and using instanceof, but I thought this was a better way.

/**
 * This method searches all known types / locations to find the object name based on ID.
 * @param whatClass The type of the object you are searching for
 * @param id ID of the object for which you are searching.
 * @return The string name or type. Returns null if no match is found.
 */
public static <T> String nameByID(Class<?> whatClass, int id) {
    if (whatClass == ProjectCode.class) {
        for (ProjectCode c : User.getInstance().getProjects()) {
            if (c.getProjCdId() == id) {
                return c.getProjNm();
            }
        }
    } else if (whatClass == TransactionType.class) {
        for (TransactionType type : Session.getInstance().getTransactionTypes()) {
            if (type.getTransactionTypeId() == id) {
                return type.getTransactionNm();
            }
        }
    } else if (whatClass == CurrencyType.class) {
        for (CurrencyType currencyType : Session.getInstance().getCurrencies()) {
            if (currencyType.getCurrencyTypeId() == id) {
                return currencyType.getCurrencyTypeNm();
            }
        }
    }

    // no matches found
    return null;
}

If you're going to hard-code conditions for each class you want to support, there isn't much point to using a generic type. You don't even reference T within the method body, so it isn't any different from:

private static String nameById(Class whatClass, int id) {
    if(ProjectCode.class == whatClass) {
        // ...
    } else if(TransactionType.class == whatClass) {
        // ...
    }
    else if(CurrencyType.class == whatClass) {
        // ...
    }
    return null;
}

To write this method so it's reusable with a generic type, you'll need to also abstract both your methods for obtaining a collection, and your getters for obtaining instances' name and Id.

e.g.

Some getObjects<T> method, called like getObjects<TransactionType>() instead of getTransactionTypes()
The ability to access object properties polymorphically, through some interface that requires each class to have getId() and getName() instead of separate methods such as getTransactionTypeId(), getProjCdId(), etc.

Then your code could be rewritten to take advantage of a generic type parameter:

public static <T extends IInterfaceName> String nameByID(int id) {
    for (T obj : getObjects<T>()) {
        if(obj.getId().equals(id)) {
            return obj.getName();
        }
    }
    return null;
}


Throwing exceptions when validation fails When I want to check the validity of an attendance being entered into the system, I perform following action.

AttendancePresenter Class

    void _View_OnCheckValidity(object sender, EventArgs e)
    {
        //ExecuteAction performs exception handling in Base Class
        this.ExecutAction(() =>
        {
            ValidateAttendance();
        });
    }

    private void ValidateAttendance()
    {
        //DataService method returns true if the attendance is valid.
        var validity = _DataService.CheckValidityOfAnAttendance(_View.EmployeeID, _View.Date, _View.ShiftType);
        //Set the validity of the attendance in a View property.
        //So that View can stop execution if validity is false.
        _View.AttendanceValidity = validity;
        //If validation fails, throw an exception
        if (!validity)
        { throw new Exception("Invalid Attendance. Already there is a matching attendance for this employee"); }
    }

View

private void txtOutTime_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == (char)Keys.Enter & !string .IsNullOrWhiteSpace(txtEmployeeID.Text) &  txtOutTime.Text != DefaultText)
    {
        OnCheckValidity(sender, e);
        if (this.AttendanceValidity)
        { OnEnterAttendance(sender, e); } //This line of code enters attendance into the DB
    }
}

Appreciate if you could review this code and give your feedback. Specially what I want to know is about the exception thrown in presenter if the validation fails. Is it acceptable? Is this is a standard use of throw?

You're throwing System.Exception. Don't do that. If you're going to have to throw an exception for a validation exception, throw a custom ValidationException exception.

You haven't shown the code where you catch and handle that exception, but it's going to have to look like this:

try
{
    // some code
}
catch (Exception exception)
{
    // handle the [validation?] exception
}

The problem with that, is that you don't know if the exception you're catching is due to a validation error, or if there's a division by zero, or if you ran out of memory, or if a database connection failed. With a custom exception type, you can do this:

try
{
    // some code
}
catch (ValidationException exception)
{
    // handle the validation exception
}

And any exception thrown that is not a ValidationException, will bubble up the stack.

That said, I wouldn't throw an exception for that. Exceptions should be for exceptional things. If you already know how you're going to handle it (show the error message in a message box?), why not just do that instead of throwing?


looking for review of code organization for jQuery/javascript code The first block of code is my main app.js that is included on every request. It is essentially a library of functions and objects that include related functions (ie: app.utils) that I can use on other pages like page1.js:

./app.js

var app = window.app || {};

(function($){
    app.req = app.req || {};
    app.utils = app.utils || {};
    app.utils.form = app.utils.form || {};
    app.page = app.page || {};
    app.log = function(msg){
        ( window.console && console.log(msg) );
    };
})(jQuery);

This is a page-level script that is included only on the one page:

./page1.js

(function($){
    $(function(){
        app.log('page1 stuff');
    });
}(jQuery);

I'm wondering what other methods are used for organizing larger codebases, or if there are any inheritly flawed ways using this method.

In JavaScript, there is no sure-fire way to organize framework code. As proof, just take a look at the different frameworks that reside on GitHub. You will see that every developer has his/her own way of organizing code.

Here's what I can recommend in your case:

I see that in your code, you seem to be namespacing your utility functions into deep structures. What I can suggest is to have a built-in function that extends your top app namespace instead of having to manually type in, check it that level exists and create. Have that extend function nest and check it for you.

Also, with extending, it would be great if the extended functions have some access to some inner objects as well

(function(app,$){

    //some stuff you want seen by extensions
    var extensionExtras = {
       cache : {}
    }

    //an extend function that checks, nests and creates namespaces
    function extend(nesting,fname,handler){
        //split nesting order
        //recursively check for the namespace
            //if it does not exist, create an object for it
            //else, go through it
        //attach function with name
        //overwriting is up to you
        deepNamespaceReference[fname] = function(){
            handler.call(this,extensionExtras);
        }
    }

    extend('app.from.inside.module','extendedInside',function(){
        //functionality here
    });

    //expose extend
    app.extend = extend;

})(window.app = window.app || {},jQuery);

//extend from the outside:
app.extend('app.some.deep.namespace','fromOutside',function(){
    //functionality here
});

//and then we can use:
app.from.inside.module.extendedInside();
app.some.deep.namespace.fromOutside();

However...

Namespacing technique in JavaScript actually uses objects and properties that contain objects. Deeply nested object structures in JavaScript incur a very minor performance drawback but a drawback still. I suggest you keep your namespace levels a minimum to avoid the performance penalties.


Program to convert number from any base to decimal I have made a script to convert number from any base to decimal.

I am trying to optimize the code because I think it is bloated and can be improved but if I change anything it affects error handling negatively.
I tried removing the two while loops but then the user would need to enter their number again even if they only messed up their base that is something I want to avoid.

I think printing and handling the number can be optimized but I just cannot see how. Also if anyone can help me improve my code readability that would be helpful too.

while True: #Main loop will keep the script running until user exits
    negative_flag=False #flag to check if a value is negative or not. False by default
    while True: # Loop made so that any error can be handled if input is not number
        number=input("Enter a number : ")
        try:
            if type(int(number))==int:
                break
        except:
             print("Wrong input. Only integers are allowed.")
             continue

    while True: # Loop made so that any error can be handled if input is not number
        base_from=input("Enter the base number is in : ")
        try:
            if type(int(base_from))==int and int(base_from)>=2:
                break
            else:
                print("Base should be greater than or equal to 2")
        except:
             print("Wrong input. Only integers are allowed.")
             continue

    if int(number)<0: #Check if a number is negative if it is convert it into a postive
        (number)=int(number)*-1
        negative_flag=True
    number=str(number)
 #Not useful just for reference base_to=10

    number_size=len(number)
    number_holder=0

    for x in number: #Basic conversion of a number to decimal number system.
        number_size-=1
        base_raised=int(base_from)**number_size
        number_multiplied=base_raised*int(x)
        number_holder=number_holder+number_multiplied

    if negative_flag==True: # Prints if the value is -ve
        print( number_holder*-1 )
    else:
        print(number_holder)

    while True:  # Final loop asking users if they want to exit or not
        response=input("Do you want to continue?(y or n) - ")
        if response=='n' or response=='y':
            break
        else:
            print("Wrong input")
            continue
    if response == 'n': # Conditions for only two valid answer.
        break
    elif response == 'y':
        continue

PEP-8
The Style Guide for Python Code has many recommendations for the formatting of Python code that all developers should (must!) follow.  These include

single blank space around binary operators, eg negative_flag = False, not negative_flag=False
no blanks after ( or before ), eg print(number_holder * -1) not print( number_holder*-1 )

Useless code
int(number) can only ever return an int, or raise an exception.  There are no other possibilities.  So if no exception is raised, the if condition will always be true, making the if statement an unnecessary control structure:
try:
    if type(int(number)) == int:
        ...
except:
    ...

Similarly you only exit this loop if response == 'n' or response == 'y'
while True:
    response = input("Do you want to continue?(y or n) - ")
    if response=='n' or response=='y':
        break
    else:
        print("Wrong input")
        continue

So why test both possibilities?
if response == 'n': # Conditions for only two valid answer.
    break
elif response == 'y':
    continue

From the comment, it seems you have already realized this.  So why elif response == 'y':?  Why not simple else:?
Finally, every last one of the continue statements is used as the last statement in a control structure, in a loop.  Without the continue statement, the loop would be restarting anyway, so these can all be omitted.
Only catch the exceptions you expect
Consider this code:
while True:
    number = input("Enter a number : ")
    try:
        if type(int(number)) == int:
            break
    except:
         print("Wrong input. Only integers are allowed.")
         continue

Try pressing Control-C at the input prompt to quit the program.  Whoops!  Why isn't the program terminating?  Because Control-C raises the KeyboardInterrupt exception, which you catch, display an inappropriate error messages, and then loop back for another try.  How unexpected.
You want to catch ValueError exceptions, only.
while True:
    try:
        number = int(input("Enter a number: "))
        break
    except ValueError:
        print("Wrong input.  Only integers are allowed.")

Note the absence of useless if statements and continue statements.
Any Base?
Your program is supposed to convert from "any base" to decimal.  But really, you only allow base 2 through base 10 input.  You don't allow FF to be converted from base 16 to base 10.
To Base 10
You've got a lot of code to convert a number to base 10.  This functionality is built-in to Python.  If your intention is not reinventing-the-wheel, you should use the int(x, base=10) function.
Reworked Code
while True:

    number = input("Enter a number: ")

    while True:
        try:
            base = int(input("Enter the base the number is in: "))
            if base >= 2:
                break
            print("Base should be greater than or equal to 2")
        except ValueError:
            print("Wrong input.  Only integers are allowed")

    try:
        print(int(number, base))
    except ValueError:
        print(f"Unable to convert {number!r} to base {base}")

    while True:
        response = input("Do you want to continue? (y or n): ")
        if response in {'y', 'n'}:
            break
        print("Wrong input")

    if response == 'n':
        break

Or super condensed:
print("Press Control-C to stop.")
while True:
    try:
        print(int(input("Number: "), int(input("Base: "))))
    except ValueError as err:
        print("Invalid input:", err)
    except KeyboardInterrupt:
        print("Conversions to base-10 complete.")
        break


Python script to find employees who are in one PostgreSQL table but not in another I'm writing a python script and do database query to get the ids of employees who are in one table but are not in another. I'm using psycopg2  module for python 2 and PostgreSQL as database. After querying from one table based on a condition, I do another query on another table to get the difference between these tables using the result of previous query. The problem is the full procedure takes a long time. I want to know is there any other method or technique which can make the entire procedure faster?
Below is the code I used for doing my feature:

def find_difference_assignment_pls_count(self):
        counter = 0
        emp_ids = []
        self.test_cursor.execute("""Select id,emp_id from test_test where flag=true and emp_id is not null and ver_id in(select id from test_phases where state='test')""")
        matching_records = self.test_cursor.fetchall()

        for match_record in matching_records:
            self.test_cursor.execute("""Select id from test_table where test_emp_id=%s and state='test_state'""",(match_record['emp_id'],))
            result = self.test_cursor.fetchall()

            if result:
                continue
            else:
                emp_ids.append(match_record['emp_id'])
                counter +=1

        print "Employees of test_test not in test_table: ", counter
        return emp_ids

I run these queries on two tables which at least have more than 500000 records. Therefore the performance is really slow.

LEFT JOIN from test_test to test_table selecting only rows where test_table.test_emp_id is null.

select emp_id
from test_test
left join test_table on (
    test_table.test_emp_id = test_test.emp_id and
    test_table.state = 'test_state'
)
where (
    test_test.flag = true and
    test_test.emp_id is not null and
    test_test.ver_id in (
        select id
        from test_phases
        where state = 'test'
    ) and
    test_table.test_emp_id is null
)

You may also want to consider:

Using an inner join instead of a subquery to select only rows with test_phases.state = 'test'.
Selecting distinct emp_ids if test_test.emp_id does not have a unique constraint.

select distinct emp_id
from test_test
inner join test_phases on test_phases.id = test_test.ver_id
left join test_table on (
    test_table.test_emp_id = test_test.emp_id and
    test_table.state = 'test_state'
)
where (
    test_test.flag = true and
    test_test.emp_id is not null and
    test_phases.state = 'test' and
    test_table.test_emp_id is null
)


Show list of numbers 1-10 multiplied by user entered number Objective of this program is to have the user enter 2 or 9 and output
the respective numbers multiple by 2 10 times.
Example if a user enters 2, browser will output the following:

2 times 2 is:4
3 times 2 is:6
⋮
11 times 2 is:22

I want like to know how can I simplify the underneath code? I have
placed the computation of the user entered value multiple by 2, on 2
separate" if else-if" statements. In case the user enters a wrong
number and subsequently when he enters the correct value, the values
wil be multipled by 2. See Loop A and Loop B comments to understand
what I mean.

let value = prompt("Enter Value between 2 or 9");

document.write(value);
document.write(`<br>`);

if (value == 2 || value == 9) {
//Loop A, when user enters 2 or 9, a list of its multiple by 2 is output to the user 10
//times since I have already written the logic here how do I ensure it do not
//write it again

  for (let i = 0; i < 10; i++) {
    value2 = value * 2;
    document.write(`${value} times 2 is:${value2}`);
    document.write(`<br>`);
    //by adding "value++" the user entered value will increment
    value++;
  }
} else if (value < 2 || value > 9) {
  for (i = 0;(value < 2 || value > 9); i++) {
    value = prompt("Enter Value between 2 or 9");

    if (value == 2 || value == 9) {
    //Loop B, I want the computation to go to loop A instead of me having to write
    //the computation of user value x 2
  //How can i accompolise this
      for (let i = 0; i < 10; i++) {
        value2 = value * 2;
        document.write(`${value} times 2 is:${value2}`);
        document.write(`<br>`);
        //by adding "value++" the user entered value will increment
        value++;
      }
      break;
    }
  }
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
 <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
</head>
<body>
 <script type="text/javascript" src="myscript_multi_validation.js"></script>
</body>
</html>

Couple of things I noticed about your code.

Nothing happens if you enter any of the numbers 3,4,5,6,7,8 due to the else if statement checking for below 2 or above 9 instead of anything other than 2 or 9. To resolve this you should use an else instead of an else if as it will catch anything that is not 2 or 9 which is caught in the if statement.
You have created a manual infinite loop in your else if, the for loop will keep looping as long as the user enters a number below 2 or above 9 this is dangerous, if the number is not matched then it should reset the user back to a prompt and back to first if statement in theory.

How does the below catch your fancy?

let value = prompt("Enter Value between 2 or 9");

let loopAndOutput = function(value){
  if(value == 2 || value == 9){
    for(let i = 0; i < 10; i++){
      console.log(`${i} times ${value} is:${value * i}`);
    }
  } else {
    let value = prompt("Enter Value between 2 or 9");
    loopAndOutput(value);
  }
}

loopAndOutput(value);


Threshold an image for a given range and Sobel kernel The class defines functions to threshold an image for a given range and Sobel kernel. Each function has similarities, i.e. they share the same following tasks:

each function converts an image to grayscale
take the gradient
rescale to 8 bit
apply a threshold, and create a binary image result

class GRADIENT_THRESHOLD(object):
    '''
    Define functions to threshold an image for a given range and Sobel kernel
    '''

    def __init__(self, args):
        self.args = args

    def gradient_abs_sobel(self, img, orient='x', sobel_kernel=3, thresh=(0, 255)):
        # convert to grayscale
        gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
        # take the absolute value of the gradient in given orient = 'x' or 'y'
        if orient == 'x':
            abs_sobel = np.absolute(cv2.Sobel(gray, cv2.CV_64F, 1, 0))
        if orient == 'y':
            abs_sobel = np.absolute(cv2.Sobel(gray, cv2.CV_64F, 0, 1))
        # scale to 8-bit (0 - 255) then convert to type = np.uint8
        scaled_sobel = np.uint8(255*abs_sobel/np.max(abs_sobel))
        # create a mask of 1's where the scaled gradient magnitude
        # is > thresh_min and < thresh_max
        binary_output = np.zeros_like(scaled_sobel)
        # return this mask as your binary_output image
        binary_output[(scaled_sobel >= thresh[0]) & (scaled_sobel <= thresh[1])] = 1
        # return the binary image
        return binary_output

    def gradient_magnitude(self, img, sobel_kernel=3, thresh=(0, 255)):
        # convert to grayscale
        gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
        # take both Sobel x and y gradients
        sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=sobel_kernel)
        sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=sobel_kernel)
        # calculate the gradient magnitude
        gradmag = np.sqrt(sobelx**2 + sobely**2)
        # rescale to 8 bit
        scale_factor = np.max(gradmag)/255
        gradmag = (gradmag/scale_factor).astype(np.uint8)
        # create a binary image of ones where threshold is met, zeros otherwise
        binary_output = np.zeros_like(gradmag)
        binary_output[(gradmag >= thresh[0]) & (gradmag <= thresh[1])] = 1
        # return the binary image
        return binary_output

    # Define a function to threshold an image for a given range and Sobel kernel
    def gradient_direction(self, img, sobel_kernel=3, thresh=(0, np.pi/2)):
        # convert to grayscale
        gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
        # take both Sobel x and y gradients
        sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=sobel_kernel)
        sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=sobel_kernel)
        # take the absolute value of the gradient direction
        absgraddir = np.arctan2(np.absolute(sobely), np.absolute(sobelx))
        # apply a threshold, and create a binary image result
        binary_output =  np.zeros_like(absgraddir)
        binary_output[(absgraddir >= thresh[0]) & (absgraddir <= thresh[1])] = 1
        # return the binary image
        return binary_output

as things are now, there is little use for the class in this case. the self argument is never used in the functions.

This is easily factored into different functions

def grayscale(img):
    return cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

Whether this deserves a separate function or can be inlined is your choice. If you need to type this a lot, or if the method of grayscaling can change in the future, it will be easier to put it in a separate method.. You could also move the colorspace to the arguments to make this more versatile and also accept HSV or Luv encoded images.

def sobel_xy(gray, sobel_kernel=3, absolute=True):
    sobel_x = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=sobel_kernel)
    sobel_y = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=sobel_kernel)
    if absolute:
        sobel_x, sobel_y = np.absolute(sobel_x), np.absolute(sobel_y)
    return sobel_x, sobel_y

This calculation is done in all methods, so refactoring it is simple and effective

def gradient_magnitude(gray, sobel_kernel=3):
    return np.hypot(*sobel_xy(gray, sobel_kernel))

def gradient_direction(gray, sobel_kernel=3):
    return np.arctan2(*sobel_xy(gray, sobel_kernel))

This part is pretty straightforward. All I changed was use np.hypot to calculate the magnitude

def gradient_abs_sobel(gray, orient='x'):
    dx, dy = (1, 0) if orient == 'x' else (0, 1)
    sobel = cv2.Sobel(gray, cv2.CV_64F, dx, dy)
    return np.absolute(sobel)

def gradient_abs_sobel2(gray, orient='x'):
    orientation = {'x': 0,'y': 1}[orient]
    sobel = sobel_xy(gray)
    return sobel[orientation]

Depending on taste and the expense of the sobel calculation you can pick either of the 2

def scale(sobel, MAX=255, dtype=np.uint8):
    return dtype(MAX * sobel / np.max(sobel))

def mask(img, lower, upper):
    return (img >= lower) & (img <= upper)

Are pretty straightforward too. Instead of the np.zeros_like you can directly use the boolean array of the comparison

Now instead of calling 1 function, you just call the 4 elements subsequently. If this is too much work you can still make 1 compound method for the 3 cases.

gray = grayscale(img)
abs_sobel = gradient_abs_sobel(gray)
result = mask(scale(abs_sobel), 10, 130)


Attempt to calculate age in VB.NET I am working on an app where I have to calculate the age of a person.  Inside of my age class I have a function that is designed to account for leap year.  Is there anything I am missing or could improve upon?  I would have though this would have been more complicated so I am not confident in my method or my test case.

The function:

Function calculateAge() As Integer

    Dim age As Integer

    Try
        If Not Birthdate.Day.Equals(29) And Birthdate.Month.Equals(2) Then
            age = todaysDate.Year - Birthdate.Year
        Else
            age = todaysDate.Year - Birthdate.Year - 1
        End If

        Return age
    Catch ex As Exception
        Console.WriteLine(ex.ToString)
        Return Nothing
    Finally
        age = Nothing
    End Try

End Function

The test:

    Sub Main()

    'Test calculate age function'
    Console.WriteLine(chip.calculateAge)

    'Test calculate age function with a February 29th birthday'
    Dim steve As New clsAge("Steve")
    With steve
        .Birthdate = #2/29/2008#
    End With

    Dim date1 As DateTime = #2/29/2000#
    Dim date2 As DateTime = #2/28/2009#
    Dim age As Integer

    Console.WriteLine(date1.Day.ToString)

    If Not date1.Day.Equals(29) And date1.Month.Equals(2) Then
        age = date2.Year - date1.Year
        Console.WriteLine(age.ToString)
    Else
        age = date2.Year - date1.Year
        age = age - 1
        Console.WriteLine(age.ToString())
    End If

    Console.ReadLine()

End Sub

what about?

var date1 = DateTime.Parse("2/29/2000");
var date2 = DateTime.Parse("2/28/2009");

var years_old = date2.Subtract(date1).TotalDays / 365.25;