F# Djikstras shortest path implementation I'm just getting my feet wet with F#, by implementing some simple algorithms. First up: Djikstras shortest path.

There is one piece I've written in this code which confuses me as to why it works: the contains function. As I understand it, List.isEmpty returns true when a list is empty; but in this case, I would want to see the opposite of that. However, not(List.isEmpty) appeared to give the opposite of what I was expecting: it still returned true on a list being empty. Why is this the case? Or am I just missing something obvious?

As for the full code listing: have I done anything massively wrong?

open System;
open System.Collections;
open System.Collections.Generic;
open System.IO;

type Node(id: int, neighbours:list<(int*int)>) =
    let mutable _ID = id
    let mutable _neighbours:list<(int * int)> = neighbours

    member this.ID
        with get() = _ID
        and set(value) = _ID <- value

    member this.AddNeighbour(id:int, distance:int) =
        let t:(int*int) = (id, distance)
        _neighbours <- _neighbours @ [t]

    member this.Neighbours
        with get() = _neighbours
        and set(value) = _neighbours <- value;

let nodes:list<Node> = [new Node(1, [(2, 3); (3, 3); (4, 1)]);
                        new Node(2, [(3, 1)]);
                        new Node(3, [(1, 3); (2, 1); (4, 10)]);
                        new Node(4, [(1, 1); (3, 10)])
                        ];

let rec remove_first pred lst =
        match lst with
        | h::t when pred h -> t
        | h::t -> h::remove_first pred t
        | _ -> [];

let contains (item:'a) (lst:list<'a>) : bool =
    let filtered = List.filter (fun I -> I = item) lst;
    List.isEmpty(filtered);

let rec Traverse (data:list<Node>, start:int, visited:list<int>):list<int> =
    let startNode = List.find (fun (E:Node) -> E.ID = start) data //Get the starting node
    let newData = remove_first (fun (E:Node) -> E.ID = startNode.ID) data //Get the new data by removing the start node from the current data
    let newVisited = visited@[startNode.ID]
    let neighbourSet = List.filter (fun (i,_) -> contains i newVisited) startNode.Neighbours //Use only the neighbours that aren't visisted

    if(List.isEmpty neighbourSet) then
        newVisited;
    else
        neighbourSet
        |> List.minBy (fun (_,d:int) -> d) //Get neighbour with smallest distance
        |> fun (i, d) -> Traverse(newData, i, newVisited); //Recurse in to the traverse function, starting at the smallest neighbour

let result = Traverse (nodes, 1, []);

let writeList (lst:list<'a>):string =
    let mutable output = "";
    for I in lst do
        output <- String.Concat(output, I);
    output;

let path = "Output.txt";
File.WriteAllText(path , writeList(result));

Matthew Podwysocki wrote a nice functional solution for this on his blog a few years ago:

module Map =
  let transposeCombine m =
    (m, m) ||> Map.fold (fun acc k1 m' ->
      (acc, m') ||> Map.fold (fun acc' k2 v ->
        acc'
        |> Map.add k2 (Map.add k1 v (defaultArg (acc' |> Map.tryFind k2) Map.empty))
    ))

type City =
    | Boise   | LosAngeles | NewYork | Seattle
    | StLouis | Phoenix    | Boston  | Chicago
    | Denver

let distanceBetweenCities =
  Map.ofList
    [
        (Boise, Map.ofList [(Seattle, 496);(Denver, 830);(Chicago, 1702)]);
        (Seattle, Map.ofList [(LosAngeles, 1141);(Denver, 1321)]);
        (LosAngeles, Map.ofList [(Denver, 1022);(Phoenix, 371)]);
        (Phoenix, Map.ofList [(Denver, 809);(StLouis, 1504)]);
        (Denver, Map.ofList [(StLouis, 588);(Chicago, 1009)]);
        (Chicago, Map.ofList [(NewYork, 811);(Boston, 986)]);
        (StLouis, Map.ofList [(Chicago, 300)]);
        (Boston, Map.ofList [(StLouis, 986)]);
        (NewYork, Map.ofList [(Boston, 211)])
    ]
    |> Map.transposeCombine

let shortestPathBetween startCity endCity =
  let rec searchForShortestPath currentCity distanceSoFar citiesVisitedSoFar accMap =
    let visitDestinations m =
      (m, distanceBetweenCities.[currentCity])
        ||> Map.fold
          (fun acc city distance ->
             searchForShortestPath city (distance + distanceSoFar) (citiesVisitedSoFar @ [city]) acc)

    match Map.tryFind currentCity accMap with
    | None -> accMap |> Map.add currentCity (distanceSoFar, citiesVisitedSoFar) |> visitDestinations
    | Some x ->
        let (shortestKnownPath, _) = x
        if distanceSoFar < shortestKnownPath then
          accMap |> Map.add currentCity (distanceSoFar, citiesVisitedSoFar) |> visitDestinations
        else accMap

  let shortestPaths = searchForShortestPath startCity 0 [] Map.empty
  shortestPaths.[endCity]

Usage:

shortestPathBetween LosAngeles NewYork //(2721, [Denver; StLouis; Chicago; NewYork])


Keeping track of user role's I created this class to keep track of a user's role:

class UserRoleHelper extends AppHelper
{
    public $helpers = array('Session');

    private $role = NULL;

    const CUSTOMER  = 'customer';
    const ASSOCIATE = 'associate';
    const MANAGER   = 'manager';
    const ADMIN     = 'admin';
    const DEVELOPER = 'developer';

    private $score = array(
        self::CUSTOMER  => 0,
        self::ASSOCIATE => 1,
        self::MANAGER   => 2,
        self::ADMIN     => 3,
        self::DEVELOPER => 4,
    );

    public function role()
    {
        if($this->role == NULL)
        {
            $this->role = strtolower($this->Session->read('Auth.User.role')) ?: 'customer';
        }
        return $this->role;
    }

    private function is($role)
    {
       return $this->score[$this->role()] >= $this->score[$role];
    }

    public function admin()
    {
        return $this->is(self::ADMIN);
    }

    public function manager()
    {
        return $this->is(self::MANAGER);
    }

    public function associate()
    {
        return $this->is(self::ASSOCIATE);
    }

}

The idea is to be able to have an expression like:

if($this->UserRole->manager())
{
    //do something
}

And have it evaluate true if you are a manager or higher (admin, developer).

I also tried to design it so if there is ever the need to add a level like GENERAL_MANGER = 3 which would shift the values of ADMIN and DEVELOPER up one, it won't effect anything.

I originally had the keys in the $score array as strings, but it seemed redundant when I had constants already defined with the same values. I have never seen an array definition with constants for keys in a class body before. I know variables are not allowed but it seems to work with constants. Still don't know if that is bad or not though.

Is there anything I have missed or can do to make this better?

Use an enumerator?

And use it like this:

if($this->isInRole(Roles::MANAGER)


Blackjack odds calculator I'm working on getting switch and if statements down and I came up with this.  It works well and I don't have any problems with it running properly.  I was wondering if I could get some feedback on how I put it together.  If there are things I am doing wrong or could do better, I'm very open to suggestions!

public static void main(String[] args) {

    double edge = 0;

    Scanner sc = new Scanner(System.in);

    System.out.println("Enter the name of your Casino.");
    String casino = sc.nextLine();

    System.out.println("How many decks? (1, 2, 4, 6, 8)");
    int decks = sc.nextInt();
    System.out.println("Dealer hits Soft 17 (1 for Yes; 2 or No)");
    int soft17 = sc.nextInt();
    System.out.println("Double after Splits Allowed (1 for Yes; 2 or No)");
    int doubleAfterSplit = sc.nextInt();
    System.out.println("Double 10/11 Only (1 for Yes; 2 or No)");
    int double1011 = sc.nextInt();
    System.out.println("Double 9/10/11 Only (1 for Yes; 2 or No)");
    int double91011 = sc.nextInt();
    System.out.println("Resplit Aces (1 for Yes; 2 or No)");
    int rSA = sc.nextInt();
    System.out.println("Late Surrender (1 for Yes; 2 or No)");
    int lateSurrender = sc.nextInt();
    System.out.println("Early Surrender (1 for Yes; 2 or No)");
    int earlySurrender = sc.nextInt();
    System.out.println("Lose All Doubles/Splits vs. Natural (1 for Yes; 2 or No)");
    int loseAllDS = sc.nextInt();
    sc.close();

    //Number of Decks Option
    if (decks == 1) {
        edge += 0.01;
    } else if (decks == 2) {
        edge -= 0.32;
    } else if (decks == 4) {
        edge -= 0.49;
    } else if (decks == 6) {
        edge -= 0.54;
    } else if (decks == 8) {
        edge -= 0.57;
    } else {
        System.out.println("You made a mistake.  Start over.");
    }

    //Soft 17 Options
    if (soft17 == 1) {
        edge -= 0.20;
    } else {
        edge += 0;
    }

    //Double After Splits Option
    if (doubleAfterSplit == 1) {
        edge += 0.14;
    } else {
        edge += 0;
    }

    //Double 10 and 11 Only Option
    if (double1011 == 1) {
        edge -= 0.17;
    } else {
        edge += 0;
    }

    //Double 9, 10, and 11 Only Option
    if (double91011 == 1) {
        edge -= 0.08;
    } else {
        edge += 0;
    }

    //Resplit Aces Option
    if (rSA == 1) {
        edge += 0.08;
    } else {
        edge += 0;
    }

    //Late Surrender Option
    if (lateSurrender == 1) {
        edge += 0.08;
    } else {
        edge += 0;
    }

    //Early Surrender Option
    if (earlySurrender == 1) {
        edge += 0.65;
    } else {
        edge += 0;
    }

    //Lose on All Doubles and Splits vs Natural Option
    if (loseAllDS == 1) {
        edge -= 0.11;
    } else {
        edge += 0;
    }

    DecimalFormat newFormat = new DecimalFormat("#.##");
    edge =  Double.valueOf(newFormat.format(edge));

    System.out.print("At, " + casino + " ");
    if (edge >= 0) {
        System.out.print("your advantage is: " + edge + "%");
    } else {
        System.out.print("your disadgantage is: " + edge + "%");
    }

}

}

edge += 0; doesn't do anything You can just remove it and the else branches where you do it.

The number of decks handling can be done with an array:

int[] deckEdge = new int[]{0, 0.01, -0.32, 0, -0.49, 0, -0.54, 0, -0.57};//insert 0 in invalid places;

edge += deckEdge[decks];

(there is a trick you can use here by dividing the number of decks by 2 to remove the inserted 0s but that is less readable and understandable)

The switch solution is perhaps the most straightforward setup for this though:

switch(decks){
    case 1:
        edge += 0.01;
        break;
    case 2:
        edge -= 0.32;
        break;
    case 4:
        edge -= 0.49;
        break;
    case 6:
        edge -= 0.54;
        break;
    case 8:
        edge -= 0.57;
        break;
    default:
        System.out.println("You made a mistake.  Start over.");
        return;//stop program;
}

You also just continue on when a wrong number of decks is specified. You can either use return to stop immediately (like in the above switch) or use a do...while loop while reading it:

do{
    System.out.println("How many decks? (1, 2, 4, 6, 8)");
    int decks = sc.nextInt();
}while(decks!=1 && decks!=2 && decks!=4 && decks!=6 && decks!=8);


Possible solutions to remove the use of a Hashtable While this is working very well for storing setting variables for my applications, I've been using it for a number of years and really feel there is something better. I'm perhaps just looking to use some newer functions from the framework.

    public string Value { get; set; }
    public string Name { get; set; }
    public string Notes { get; set; }

    /// <summary>
    /// Gets the selected setting from cache if it exists,
    ///     else returns the supplied default value
    /// </summary>
    /// <param name="settingName"></param>
    /// <param name="defaultValue"></param>
    /// <returns></returns>
    public static string GetCacheSetting(string settingName, string defaultValue)
    {
        return LoadSettings().Contains(settingName) ? Convert.ToString(LoadSettings()[settingName]) : defaultValue;
    }

    /// <summary>
    /// Returns the selected setting value from cache.
    /// If cache is null then rebuild the cache.
    /// </summary>
    /// <returns></returns>
    public static Hashtable LoadSettings()
    {
        Hashtable h;
        h = (Hashtable)HttpRuntime.Cache["SiteSettings"];

        if (h == null)
        {
            h = new Hashtable();

            DataAccess da = new DataAccess(); //My DAL

            foreach (DataRow row in da.GetSiteSettings().Tables[0].Rows)
            {
                Settings oSetting = new Settings();
                oSetting.SettingsInitFromRow(row);
                h.Add(oSetting.Name, oSetting.Value);
            }

            HttpRuntime.Cache.Insert("SiteSettings",
                                    h,
                                    null,
                                    DateTime.Now.AddMinutes(120),
                                    Cache.NoSlidingExpiration);
        }

        return h;
    }

    /// <summary>
    /// Sets the values for the cache items
    /// </summary>
    /// <param name="row"></param>
    private void SettingsInitFromRow(DataRow row)
    {
        this.Name = (string)row["Name"];
        this.Value = (string)row["Value"];
    }

The settings are stored in the database and created at install time/modified as needed. At most I have 20 to 30 setting values in the database. I'm using a Hashtable stored in cache to get my values to remove the need to access the database 2, 3, or more times on every page load just for settings in addition to the rest of the times for page content. Doing this was the last time I majorly changed it maybe 3 years ago.

Any ideas?

It look spretty good to me, but there are a couple of things I would likely change, none of which have to do with the use (or non-use) of a hashtable data structure as it is perfectly suited for this sort of thing.

I would probably change the LoadSettings method a bit as it doesn't actually load settings each time it is called, and then definitely use the generic Dictionary<K,V> instead of the antiquated Hashtable class, thereby removing the need for all that nasty casting.  You could also avoid two lookups when attempting to retrieve a value from the cache by using TryGetValue instead of Contains and then performing another lookup, something like:

public static string GetCacheSetting(string settingName, string defaultValue)
{
    string ret;
    Settings.TryGetValue( settingName, out ret );
    return ret ?? defaultValue;
}

// you may even want to make this private as a caller could
// edit the contents of the cache directly.  You could leave
// GetCacheSetting as the only access point, adding a
// SetCacheSetting method if needed.
private static Dictionary<string, string> _settings;
public static Dictionary<string, string> Settings
{
    get
    {
        if( _settings == null )
        {
            _settings = LoadSettings();
        }

        return _settings;
    }
}

private static Dictionary<string, string> LoadSettings()
{
    HttpRuntime.Cache.Remove( _settings );
    _settings = new Dictionary<string,string>();
    DataAccess da = new DataAccess(); //My DAL
    foreach (DataRow row in da.GetSiteSettings().Tables[0].Rows)
    {
        Settings oSetting = new Settings();
        oSetting.SettingsInitFromRow(row);
        _settings.Add(oSetting.Name, oSetting.Value);
    }

    HttpRuntime.Cache.Insert("SiteSettings",
                             _settings,
                             null,
                             DateTime.Now.AddMinutes(120),
                             Cache.NoSlidingExpiration);

    return _settings;
}


Equality for a DateOfBirth Value Object Please see the code below:

public sealed class DateOfBirth : IEquatable<DateOfBirth>, IComparable<DateOfBirth>
    {
        private readonly DateTime _value;

        public DateTime Value
        {
            get { return _value; }
        }
        public DateOfBirth(DateTime dateOfBirth)
        {
            if(dateOfBirth == DateTime.MinValue)
                throw new ArgumentException("Invalid value.", "DateOfBirth");
            this._value= dateOfBirth;
        }

        private static int Comparison(DateOfBirth dateOfBirth1, DateOfBirth dateOfBirth2)
        {
            if (ReferenceEquals(dateOfBirth1, dateOfBirth2))
                return 0;
            else if (ReferenceEquals(dateOfBirth1, null))
                return -1;
            else if (ReferenceEquals(dateOfBirth2, null))
                return 1;
            if (dateOfBirth1._value < dateOfBirth2._value)
                return -1;
            else if (dateOfBirth1._value == dateOfBirth2._value)
                return 0;
            else if (dateOfBirth1._value > dateOfBirth2._value)
                return 1;
            return 0;
        }

        public int CompareTo(DateOfBirth other)
        {
            if (other != null)
                return this._value.CompareTo(other._value);
            else
                throw new ArgumentNullException("DateOfBirth");
        }

        public static bool operator ==(DateOfBirth dateOfBirth1, DateOfBirth dateOfBirth2)
        {
            return Comparison(dateOfBirth1, dateOfBirth2) == 0;
        }

        public static bool operator !=(DateOfBirth dateOfBirth1, DateOfBirth dateOfBirth2)
        {
            return !(dateOfBirth1 == dateOfBirth2);
        }

        public static bool operator <(DateOfBirth dateOfBirth1, DateOfBirth dateOfBirth2)
        {
            return Comparison(dateOfBirth1, dateOfBirth2) < 0;
        }

        public static bool operator >(DateOfBirth dateOfBirth1, DateOfBirth dateOfBirth2)
        {
            return Comparison(dateOfBirth1, dateOfBirth2) > 0;
        }

        public static bool operator <=(DateOfBirth dateOfBirth1, DateOfBirth dateOfBirth2)
        {
            return Comparison(dateOfBirth1, dateOfBirth2) <= 0;
        }

        public static bool operator >=(DateOfBirth dateOfBirth1, DateOfBirth dateOfBirth2)
        {
            return Comparison(dateOfBirth1, dateOfBirth2) >= 0;
        }

        public bool Equals(DateOfBirth other)
        {
            if (ReferenceEquals(other, null))
                return false;
            return _value == other._value;
        }

        public override bool Equals(object obj)
        {
            return Equals(obj as DateOfBirth);
        }

        public override int GetHashCode()
        {
            return _value.GetHashCode();
        }
    }

I realise it could be argued as over engineering a simple date of birth field.  I am more interested whether or not the nine comparisons (https://ericlippert.com/2013/10/07/math-from-scratch-part-six-comparisons/) are implemented correctly.  This article helped me: https://blogs.msdn.microsoft.com/abhinaba/2005/10/11/c-comparison-operator-overloading-and-spaceship-operator/ - the only concern I have about the code in this article is that the Comparison method does not do this (which my code does):

if (ReferenceEquals(dateOfBirth1, dateOfBirth2))
                return 0;
            else if (ReferenceEquals(dateOfBirth1, null))

Q1) Is the code above fit for purpose (are there any bugs)?

Q2) Is a DateTime data type (DateOfBirth._value) suitable for a Date of Birth?
                    return -1;

Q3) Is the validation in the constructor fit for purpose?

'fit for purpose' depends on what exactly that purpose is. Do you want to register the time of birth (as the Value property suggests), or only the date (as the class name seems to imply)? Does it need to handle birth dates before 0 A.D.? Does it need to reject non-existing dates (depending on actual historical data)?
If this class is meant to store a date only (not a time component), then it makes more sense to give it Year, Month and Day properties, rather than a Value property that exposes a time component.
DateTime.MinValue is the default value of DateTime, but why does that make it an invalid birth-date?
It may be useful to provide (explicit or implicit) conversions operators from and to DateTime, and perhaps parsing methods, depending on where and how this class is meant to be used. However...
...the purpose of this class seems very narrow. Judging from your other posts, you are thinking about creating many such 'value' types? That's a lot of work (at least in C#), but what problem does it solve? Where's the added value? Are you worried that someone may accidentally assign an expiry-date to a birth-date field? Remember, all that extra code needs to be maintained, and there's a cost associated with that.

My impression is that yes, you are over-engineering this. I suspect that you'll find more value in writing automated tests (which, when done properly, should have caught that >= operator bug, and should catch expiry-date/birth-date mixups just as well) than in adding this sort of complexity. C# requires too much boiler-plate to make this approach viable, so unless lives are at stake I wouldn't bother.

EDIT: A few more notes:

In C# 7, ReferenceEquals(a, null) can be written more succinctly as a is null.
The null check in CompareTo uses the overloaded != operator, but you might as well use a reference check directly.
Why are the Equals methods not using Comparison? The same goes for the != operator: why doesn't it do Comparison(dateOfBirth1, dateOfBirth2) != 0?


Re-sorting newsfeed based on date Background: A client side I made recently has a component that is made from several (20+) rss-feeds, and the feeds are mashed into one using FreshRSS hosted on our servers. For some reason, FreshRSS sorts it in a weird order instead of the standard date latest to oldest.

So the output looks weird with the latest post that has timestamp for today, but the second post has timestamp for last week, and the third again for today.

So I'm capturing the output with output buffer, storing the output in an array and then storing the output array key in another array that will get sorted by date. What could I do better?

<?php

$feed = file_get_contents("secret-feed-url");

$rowArray = array();
$sortArray = array();

foreach($feed->channel->item as $item){

  ob_start();

  $media = ucfirst(str_replace([".fi", ".com", ".net"], "", parse_url($item->link, PHP_URL_HOST)));
  $date = date("d.m.Y", strtotime($item->pubDate));

  ?>

      <a href="<?php echo $item->link; ?>" target="_blank" rel="noopener">
        <?php echo $item->title; ?>

          <?php echo $media . " &bull; " . $date; ?>

    <?php echo $media; ?>
    <?php echo $date; ?>

  <?php

  $row = ob_get_clean();

  $rowArray[] = $row;

  end($rowArray);
  $rowKey = key($rowArray);

  $sortArray[] = [strtotime($item->pubDate), $rowKey];

}

usort($sortArray, function($a, $b){
  return $a[0] - $b[0];
});

$sortArray = array_reverse($sortArray);

$counter = 0;

foreach($sortArray as $row){

  if($counter > 10){
    break;
  }

  echo $rowArray[$row[1]];
  $counter++;
}

I could store the "output" in a variable instead of using output buffer, but that would result in ugly code & template that is pain to update compared to this. I'm not too conserned about performance, as this is cached data.

Is there a better method for sorting this? I've done this several times on several sites, but it's usually a 1-time operation so using a framework like Symfony is a bit overkill.

usort($sortArray, function($a, $b){
  return $a[0] - $b[0];
});

$sortArray = array_reverse($sortArray);

Minor thoughts:

Instead of sorting by $a[0]-$b[0] and reversing, why not sort by $b[0]-$a[0] in the first place? However...
You could just return $b[0] <=> $a[0].


Reloading selects with JSON My JavaScript code is slow for large inputs. However, it is especially slow in IE. The browser will freeze for about a minute while the select is being reloaded. Any ideas for how to optimize this code, especially for IE?

function reloadSelectWithJSON (elementid,query){
        /* It is assumed that the elementid passed in is
  the selected box's ID while 'available_' added to it is the available box's ID */
    var preElement = 'available_' + elementid;
    var element = document.getElementById(preElement);
    var selected = document.getElementById(elementid);

    /* Gathers a string consisting of all the selected values */
    var selectedList = '';
    for (var h = 0; h < selected.length; h++){
        selectedList = selectedList + ',' + selected.options[h].value;
    }

    selectedList = selectedList.substr(1).toLowerCase();

    //Clear select of exiting options
    element.options.length = 0;

    //Creates a blank option if the optional arguments {id,name} are passed
    if (this.reloadSelectWithJSON.arguments.length == 4) {
        var id = this.reloadSelectWithJSON.arguments[2];
        var name = this.reloadSelectWithJSON.arguments[3];
        element.options[0] = new Option(name,id);
    }

    //Loop JSON query structure and reload select
    for (var i=0; i < query.ROWCOUNT; i++){
   /* If the value is part of the selected values list then do not include it as an available column */
        if(selectedList.search(query.DATA.ID[i].toString().toLowerCase()) == -1){
            element.options[element.options.length] = new Option(query.DATA.NAME[i], query.DATA.ID[i]);

            var optionLength = element.options.length;

            optionLength--;

            if(query.DATA.title != undefined  && element.options[optionLength] != undefined){
            element.options[optionLength].setAttribute("title",query.DATA.title[i]);
            } else {
            element.options[optionLength].setAttribute('title',query.DATA.NAME[i]);
            }
        }
    }
}

Right now selectedList is a string that seems to be put together solely for searching through it. That has the potential to be a big performance bottleneck.

If you change it to an array, you can squeeze a bit more efficiency into your code by sorting the array and then using a binary search method to detect whether it contains the desired value.

Here's an example of a binary search method.

// Accepts a sorted array, an object, and an optional comparator method
// Returns index of detected object, or a negative value if not found (binary complement of proper index)
function getIndex(arr, target, comparator) {
    var l = 0,
    h = arr.length - 1,
    m, comparison;
    comparator = comparator || function (a, b) {
        return (a  b ? 1 : 0));
    };
    while (l <= h) {
        m = (l + h) >>> 1;
        comparison = comparator(arr[m], target);
        if (comparison < 0) {
            l = m + 1;
        } else if (comparison > 0) {
            h = m - 1;
        } else {
            return m;
        }
    }
    return~l;
};

You'll find that although there's a small performance penalty resulting from the need to sort the array, you'll be able to look up the indices of elements much faster this way. Check out the code snippet embedded below for some dramatic proof!

var log = (function (output) {
    return function (text) {
        output.insertAdjacentHTML("beforeend", text + "
");
    };
})(document.getElementById("output"));
document.getElementById("RunTests").onclick = function () {
    document.getElementById("output").innerHTML = "";
    var numWords = document.getElementById("NumWords").value;
    var numTestWords = document.getElementById("NumTests").value;
    var str = "";
    var arr = [];
    log("Creating a list of " + numWords + " items:");
    var start = Date.now();
    for (var i = 0; i < numWords; i++) {
        var word = randomString();
        arr.push(word);
    }
    log("Time to build array: " + (Date.now() - start));
    start = Date.now();
    for (var j = 0, len = arr.length; j < len; j++) {
        var temp = randomString(); // just to be fair to testing time
        str = str + "," + arr[j];
    }
    str = str.substr(1);
    log("Time to build string: " + (Date.now() - start));
    start = Date.now();
    arr.sort();
    log("Time to sort array: " + (Date.now() - start));

    var test = [];
    for (var k = 0; k < numTestWords; k++) {
        test.push(arr[Math.random() * arr.length >>> 0]);
    }
    log("Searching for " + test.length + " test words: ");

    var index = test.length;
    start = Date.now();
    while (index--) {
        str.search(test[index]);
    }
    log("Time to search string: " + (Date.now() - start));

    index = test.length;
    start = Date.now();
    while (index--) {
        getIndex(arr, test[index]);
    }
    log("Time to search array: " + (Date.now() - start));
}

function getIndex(arr, target, comparator) {
    var l = 0,
        h = arr.length - 1,
        m, comparison;
    comparator = comparator || function (a, b) {
        return (a  b ? 1 : 0));
    };
    while (l <= h) {
        m = (l + h) >>> 1;
        comparison = comparator(arr[m], target);
        if (comparison < 0) {
            l = m + 1;
        } else if (comparison > 0) {
            h = m - 1;
        } else {
            return m;
        }
    }
    return~l;
};

function randomChar() {
    return "abcdefghijklmnopqrstuvwxyz" [Math.random() * 26 >>> 0];
}

function randomString() {
    return "" + randomChar().toUpperCase() + randomChar() + randomChar() + randomChar();
}
Number of words in list:
<input type="text" value=5000 id="NumWords">

Number of tests to run:
<input type="text" value=5000 id="NumTests">

<input type="button" value="Run Test" id="RunTests" />

To use that in your code, you'd need to change the selectedList string to an array and sort it, as in the code below.

    /* Gathers an array consisting of all the selected values */
    var selectedList = [];
    for (var h = 0; h < selected.length; h++){
        selectedList.push(selected.options[h].value.toLowerCase();
    }
    selectedList.sort();

Then your loop to check whether a given string exists in the array would look like this:

//Loop JSON query structure and reload select
for (var i=0, count = query.ROWCOUNT; i < count; i++){
    var id = query.DATA.ID[i];
    if(getIndex(selectedList, id.toString().toLowerCase()) < 0){
        element.options[element.options.length] = new Option(query.DATA.NAME[i], id);
        var optionLength = element.options.length;
        optionLength--;
        if(query.DATA.title != undefined  && element.options[optionLength] != undefined){
            element.options[optionLength].setAttribute("title",query.DATA.title[i]);
        } else {
            element.options[optionLength].setAttribute('title',query.DATA.NAME[i]);
         }
    }
}


Display characters remaining next to input/textarea I'm not great with JavaScript. I've written a small jQuery function and I've now got it working, but I just wanted to check if the code looks good or if it can be improved.

I wanted a function so that I can easily add the function to any textarea and input so that it counts characters remaining and so I can just change the max count value so I don't have to keep writing out the whole thing for every input/textarea. I want it to show the max count on focus, add a css warn class to it when it is the last 10, and add a css max-reached class to it when the count has exceeded. Here it is:

var jq = jQuery.noConflict();
jQuery( document ).ready( function() {

    jq.fn.cmCharactersLeft = function(options) {

        // defaults
        var defaults = {
            maxLength: 300,
            selector: '#testform .text-count'
        };

        var options = jq.extend(defaults, options);

        jq(this).keyup(function() {

            // character length variable
            var character_length = jq(this).val().length;

            // Characters left
            if( character_length <= options.maxLength ){
               jq( options.selector ).html( options.maxLength - character_length ).removeClass('max-reached count-warn');
            }
            // Characters left last 10
            if ( character_length >= ( options.maxLength - 10 ) ){
                jq( options.selector ).html( options.maxLength - character_length ).addClass('count-warn').removeClass('max-reached');
            }
            // Max characters reached
            if ( character_length > options.maxLength ){
               jq( options.selector ).html( options.maxLength - character_length ).addClass('max-reached').removeClass('count-warn');;
            }
        });

        // show 'characters left' on focus too
        jq(this).focus(function() {

            // character length variable
            var character_length = jq(this).val().length;

            // Characters left
            if( character_length < 1 ){
                jq( options.selector ).html( options.maxLength );
            }
        });

    }

});

Then I can use the above function for any textarea/input:

jQuery( document ).ready( function() {
    jq('#form1 textarea').cmCharactersLeft({
        maxLength: 50,
        selector: '#form1 .text-count'
    });
});

jQuery( document ).ready( function() {
    jq('#form2 textarea').cmCharactersLeft({
        maxLength: 200,
        selector: '#form2 .text-count'
    });
});

Example HTML:

<form id="form1">
<textarea></textarea>

<button type="submit">Submit</button>
</form>

Edit

Or, what would be a better way to write this?

You're misusing what jQuery.noConflict does.

var jq = jQuery.noConflict();

You're using it like it creates another instance of jQuery, or something similar.

What it actually does is relinquish control of the $ variable.

Due to other libraries using $, jQuery needs to be able to return and reset what $ originally was.

Imagine this:

var $ = function(){ console.log(arguments) }; //A debugging function.

//Induction of jQuery here.

$('body'); //Returns a node containing the DOM body element.
$.noConflict();
$('body'); //Returns ['body'].

You're using it wrong.

Using jQuery

jQuery is a framework used by many to simplify processes, but do you really need it?

An important question to ask yourself when designing / architecting a new system is whether you need a library or not.

If you're building a slick, lightning-fast mobile site, you may not have room for massive libraries like jQuery.

Luckily, people realise this and write helpful companion sites you can use like YouMightNotNeedjQuery.com

Consider carefully whether it's better to write a little more code and not use jQuery, or whether it's better to lose a little performance.

Bracket spacing

You shouldn't be adding spaces inside your brackets like the following example:

jQuery( document ).ready( function() {

It should look like:

jQuery(document).ready(function(){


Checking if a point is on screen I'm working on a tile-based game, and am looking to improve the efficiency of my code to slightly improve framerate. I call this method on every tile, every frame, so it is very performance critical. What optimizations can be made?

public bool OnScreen(Vector2 point)
{
    Rectangle rect = this.Rectangle;
    return ((((rect.X <= point.X) && (point.X < (rect.X + rect.Width))) && (rect.Y <= point.Y)) && (point.Y < (rect.Y + rect.Height)));
}

public bool OnScreen(Vector2 topLeft, Vector2 size)
{
    // Check if each corner is inside of the camera rectangle
    return (
        OnScreen(topLeft) // Top-left
        || OnScreen(topLeft + new Vector2(0, size.Y)) // Bottom-left
        || OnScreen(topLeft + new Vector2(size.X, 0)) // Top-right
        || OnScreen(topLeft + size) // Bottom-right
    );
}

I am doing this so that I can pool the objects that actually render my tiles, since only a maximum of about 800 can be on screen at any one time. These methods are called as so:

// Assign renderers to any tiles in range of the camera.
foreach (var tile in _tiles)
{
    if (tile.IsInRange(camera))
    {
        if (tile.Renderer == null)
        {
            tile.Renderer = _pool.Get();
        }
    }
    else
    {
        tile.Renderer = null;
    }
}

At a glance, OnScreen looks like a method that would raise some Screen event, as On[EventName] is, by convention, the name we use for methods that raise an event. Now, those would obviously return void, and yours is returning a bool - I think I would have it like this:

public bool IsOnScreen(Vector2 point)

The return statement is pretty intense in terms of parentheses. Are they meant to improve readability?

return ((((rect.X <= point.X) && (point.X < (rect.X + rect.Width))) && (rect.Y <= point.Y)) && (point.Y < (rect.Y + rect.Height)));

Let's try something:

return rect.X <= point.X
    && point.X < (rect.X + rect.Width)
    && rect.Y <= point.Y
    && point.Y < (rect.Y + rect.Height);

Much better isn't it?


First attempt: Python Rock Paper Scissors I finished Python Principles and this is my first program I've done (mostly) from scratch since, having to get some outside help on the loop to repeat the program, and I saw a nifty thing about using modular math to figure out the results so I did that to save a bunch of lines.

I'm just looking for any comments, improvements, bad habits, any comments at all!

import random
options = ['rock', 'paper', 'scissors']

def pickRando(): #set random choice for CPU
        global randoPick
        randoPick = random.choice(options)

def game(): #start up the game
    global player
    print('Rock, Paper, Scissors:')
    player = input("Choose wisely: ") #get player input
    player = player.lower()
    if player in options:
        check = True
        print("Okay, you picked " + player + ' and I picked ' + randoPick + '!')
        return check
    else:
        print('You have not chosen a viable option! Try again')
        check = False
        return check

def convertible(swap): #changing the word into a number
    if swap == 'rock':
        swap = 0
    elif swap == 'paper':
        swap = 1
    else:
        swap = 2
    return swap

def combatCheck(a, b): #determine the results of the choices
    a = convertible(a)
    b = convertible(b)
    result = (a - b) % 3 #modmath
    return result

def finish(z): # report the results
    global wins
    global losses
    global ties
    if z == 0:
        print('A tie! You are a most worthy opponent! Go again?')
        ties = ties + 1
    if z == 1:
        print('You win! My honor demands a rematch!')
        wins = wins + 1
    if z == 2:
        print('Haha, I am victorious! Dare you challenge me again?')
        losses = losses + 1
    print('You have ' + str(wins) + ' wins, ' + str(losses) + ' losses and, ' + str(ties) + ' ties!')

wins = 0
losses = 0
ties = 0

while True :
    pickRando()
    check = False
    while check == False:
        check = game()
    finish(combatCheck(player, randoPick))
    while True: #looping
        global answer
        answer = input('Run again? (y/n): ')
        if answer in ('y', 'n'):
            break
            print('Invalid input.')
    if answer == 'y':
        continue
    else:
        print('You are weak!')
        break

This code reads like beginner code. Good beginner code, but there are a few habits you're getting into that are systemic among beginners and will screw you over later down the line.

First, your function names are somewhat confusing. I would rename pickRando to computerChoice, combatCheck to game, and finish to something like printRecord. Once you do that, you shouldn't need the comments describing them. Comments are good, but shouldn't be used in lieu of descriptive names.

Second, avoid global variables. It will cause confusing issues later down the line, so please get into the habit of not using them. In order to avoid the global variables wins, losses, and ties, you would need to restructure a lot of code and it may not be worth it for this small of a code base. Exercise to the reader, I guess. But the other globals can easily be removed. I would change pickRando to

def computerChoice():
    return random.choice(options)

The concept behind the input check is good, but I would heavily modify the structure. Here's a better way to do that

while True:
    [...]
    print("Rock, Paper, Scissors:")

    playerChoice = input('choose wisely')
    while  not playerChoice in options:
        print "invalid input"
        playerChoice == input('choose wisely')
    print("Okay, you picked " + playerChoice + ' and I picked ' + computerChoice + '!')

This restructuring should make what it's actually doing clearer.

Next: the convertible method is a very good solution if you don't know about the built-in method index. You can change the combatCheck method to something like this.

a = options.index(a)
b = options.index(b)
return (a-b) % 3

I would also comment the last line with something more descriptive than modmath. I might say something like uses modular arithmetic to calculate results.

I like that you put combatCheck in its own function from the perspective of the teacher, because it shows that you use functions, but I think it's unnecessary here, because you only use that bit of code once and it's only a few lines.

You definitely have some really great foundations. Most of your mistakes are mistakes I made in my early days of Python, and I like to think I'm a pretty OK programmer. I think with practice and time, you'll turn into an amazing programmer.


Whiteboarding - Multiply all values in array except at the index for each Consider this interview question:

  You have an array of integers, and for each index you want to find the
  product of every integer except the integer at that index.

  Write a method get_products_of_all_ints_except_at_index() that takes
  an array of integers and returns an array of the products.

  For example, given:

[1, 7, 3, 4]

  your method would return:

[84, 12, 28, 21]

  by calculating:

[7 * 3 * 4,  1 * 3 * 4,  1 * 7 * 4,  1 * 7 * 3]

  Here's the catch: You can't use division in your solution!

And here's my solution:

def get_products_of_all_ints_except_at_index(arr)
    products = []
    (0...arr.length).each do |index|
        product = 1
        arr.each do |num|
            if num != arr[index]
                product *= num
            end
        end
        products << product
    end
    products
end

First of all, congrats on producing a solution that works correctly.
However, keep in mind that correctness is often not the only criteria for a successful candidate.
Other typical criteria include performance,
as well as the analysis of corner cases,
where this solution does poorly.

Time complexity analysis

What is the time complexity of your solution?

   \$O(n^2)\$

That's a typical question to expect at a programming interview.
The point of the question is not so much the accurate computation,
but an open discussion around this topic.

Would you describe this solution as optimal?

   No. It's a brute-force solution.

A likely next question is: can we do better?

Algorithm

  Here's the catch: You can't use division in your solution!

That's some sort of hint.
It's trying to guide you in a certain direction.

Without the catch, would you implement the solution the same way?
If we can use division,
it's easy to see a simple optimization:
first compute the product of all elements,
and then for each element,
divide the total product by the element.
That would have time complexity \$O(n)\$ instead of \$O(n^2)\$.

The catch prevents us from using the simple optimization. Why?
So that we find another way.
This is the hard part, to discover something clever under high pressure.
But at least it's important to reach this point by thinking out loud about time complexity, and the nature of the problem at hand.

You could compute "prefix products" \$L\$,
such that \$L[i]\$ is the product of all the values that are on the left of \$a[i]\$,
and "prefix products" \$R\$,
such that \$R[i]\$ is the product of all the values that are on the right of \$a[i]\$.
With these helper arrays, the target value to compute for each \$i\$ is \$L[i] * R[i]\$. No division needed, and time complexity of this solution is \$O(n)\$.

Then a discussion can follow about tradeoffs.
For example, how does this compare to your original solution?

   Time complexity is improved, but space complexity is now \$O(n)\$, instead of \$O(1)\$ of brute-force.

A likely next question is: can we do better?

Corner cases

Other important points an interviewer may look for:

Does the candidate look for corner cases?

If you don't look for them, the interviewer will probably nudge you to go look for them. It's important to recognize the nudge.

Is the candidate able to find corner cases?

If you are not able to, the interviewer will probably nudge you in the general direction. It's important to recognize the nudge, and then the general direction, and verbalize all that, thinking out loud.

Can the candidate correctly adapt the solution to handle the corner cases?

So what's an interesting corner case here?

   When computing products of numbers, there may be a risk of integer overflow.

At this point it's important to ask about the minimum and maximum values that may in the array, as well as the length of the array.
Based on that, the candidate should discuss about the possibility of integer overflows, and try to compute if it can happen or not.
And sure enough, if you are able to conclude that based on the input parameters integer overflow cannot happen,
the interviewer will adjust the parameters accordingly.
And then you need to discuss strategies to deal with the added complication.

You can expect the interviewer to keep adding twists to the problem,
raising more and more challenges.
The interview can branch and go in multiple possible directions,
often in directions where you seem least comfortable.
It's good to try to anticipate potential complications.
Trying to find corner cases is probably a good starting point.

Discussion

The posted question contains simply the problem description and the solution.
A discussion around the solution is missing,
maybe you didn't think it's important. But it is.
Thinking out loud, and expressing your logic clearly during a programming interview is usually just as important as the solution itself.


A structure and helper functions for working with polar coordinates This started simple, as most things do. I just wanted a little structure to hold polar coordinates for a totally minor class. But starting with this answer and borrowing from existing Objective-C concepts like CGPoint and CGSize, the simple struct quickly morphed into a more complete solution.

I have this all in a _polar.h file that I have included in my files and I was wondering what other things should be included and/or other criticism of my technique. Am I approaching this thing correctly?

(Note: startPoint and endPoint are part of the structure to help solve other problems in my class and are not necessarily "normal" parts of a polar structure.)

#ifndef Polar__polar_h
#define Polar__polar_h

struct _Polar {
    CGFloat angle;
    CGFloat r;
    CGPoint startPoint;
    CGPoint endPoint;
};
typedef struct _Polar Polar;

static inline Polar
PolarMakeFromPoints(CGPoint startPoint, CGPoint endPoint) {
    Polar polar;
    polar.startPoint =startPoint;
    polar.endPoint =endPoint;
    polar.angle =atan2f(endPoint.y -startPoint.y, endPoint.x -startPoint.x);
    polar.r =sqrtf(powf(endPoint.x -startPoint.x, 2.0) + pow(endPoint.y -startPoint.y, 2.0));
    return polar;
}
static inline Polar
PolarMake(CGFloat angle, CGFloat r) {
    Polar polar;
    polar.angle =angle;
    polar.r =r;
    polar.startPoint =CGPointZero;
    polar.endPoint =CGPointMake((cos(angle) *r), (sin(angle) *r));
    return polar;
}
static inline bool
PolarEqualToPolar(Polar polar1, Polar polar2) {
    return polar1.angle ==polar2.angle && polar1.r ==polar2.r;
}
static inline bool
PolarIdenticalToPolar(Polar polar1, Polar polar2) {
    return CGPointEqualToPoint(polar1.startPoint, polar2.startPoint)
        && CGPointEqualToPoint(polar1.endPoint, polar2.endPoint);
}

static inline Polar
_PolarZero() {
    Polar polar;
    polar.angle =0.0;
    polar.r =0.0;
    polar.startPoint =CGPointZero;
    polar.endPoint =CGPointZero;
    return polar;
}
#define PolarZero _PolarZero()

static inline NSString
*NSStringFromPolar(Polar polar) {
    return [NSString stringWithFormat:@"a=%f r=%f", polar.angle, polar.r];
}

// Extend NSValue to work with Polar
@interface NSValue (Polar)
+(id)valueWithPolar:(Polar)polar;
-(Polar)polarValue;
@end
@implementation NSValue (Polar)
+(id)valueWithPolar:(Polar)polar {
    return [NSValue value:&polar withObjCType:@encode(Polar)];
}
-(Polar)polarValue {
    Polar polar;
    [self getValue:&polar];
    return polar;
}
@end

@interface NSString (Polar)
@end
@implementation NSString (Polar)

@end

#endif

Why the #ifdef guard? It’s not needed in Objective-C since we have the #import directive instead of #include.
Why have the whole thing in the header? The usual solution is to put the function declarations into the header and the implementations into the implementation file.
The difference between PolarEqualToPolar and PolarIdenticalToPolar is not clear to me. I mean, I could assert something from the implementation, but that’s not a good API if I have to.
Related to the previous point, do you realize you’re skating on thin ice when comparing the float components using plain equality (==)? The same disclaimer applies here as for any floating-point comparison, there are nasty suprises caused by precision issues. (See the classical article about What Every Computer Scientist Should Know About Floating-Point Arithmetic.) This is not necessarily a fault of your API or code, it’s just good to be aware of.
Do you really need the performance gains to write this in C instead of Objective-C? Always use high-level code unless the profiler tells you otherwise. Granted, polar coordinates are somewhat on the performant side, but then the modern hardware is so fast you could write the code in high-level Objective-C and still get very usable performance for many use cases.


Python script to update a cPanel zone record with my public IP This is my first attempt at using Python to send http requests.  I want to keep a zone record on cPanel pointed at my home network's public IP.  I'm just looking for some general feedback/suggestions.  Is there an easier way to do this?  Is there a more Pythonic way?  My intent is to run this as a cronjob to stay ahead of my ISP's DHCP.

This file generates the http request containing the cPanel JSON API call.

#!/usr/bin/python3

from http import client
from base64 import b64encode
from sys import argv
from netutils import PollIP

class ZoneUpdate:

    def __init__ (self):

        pass

    def updateIP (self, passedIP):

        newIP = passedIP

        conn = client.HTTPSConnection('mydomain.com', 2083)

        myAuth = b64encode(b'username:thepassword').decode('ascii')

        authHeader = {'Authorization':'Basic ' + myAuth}

        conn.request('GET', '/json-api/cpanel?cpanel_jsonapi_version=2&cpanel_jsonapi_module=ZoneEdit&cpanel_jsonapi_func=edit_zone_record&domain=mydomain.com&line=28&class=IN&type=A&name=homenet.mydomain.com.&ttl=3600&address=' + newIP, headers=authHeader)

        myResponse = conn.getresponse()

        print(myResponse.getcode())

        data = myResponse.read()

        if myResponse.getcode() != 200:
        print('did not succeed')

        print (data)

############################################

if __name__ == "__main__":

    app = ZoneUpdate()

    if len(argv) > 1:
        newIP = argv[1]
        print(argv[1])
    else:
        getIP = PollIP()
        newIP = getIP.publicIP()

    app.updateIP(newIP)

This module (imported by the preceding) will use the dyndns website to obtain my public IP address.

#!/usr/bin/python3

from urllib import request
from re import compile

class PollIP:

    def __init__(self):
        pass

    #Use the dyndns web-site to poll my public IP address
    def publicIP(self):

        myReq = request.urlopen('http://checkip.dyndns.com')

        html = str(myReq.read())

        myReg = compile('[\d.]+')

        myMatch = myReg.search(html)

        print(myMatch.group())

        return myMatch.group()

Before anything else, I'd like to point out that there is probably an error with your indentation. I've fixed it in my code assuming that only one line was not indented properly..

Simple is better than complex.

You don't need the classes (you can also have a look at this video). After taking this simple comment into account, your code looks like this (I put everything in the same place for the sake of simplicity)

#!/usr/bin/python3

from http import client
from base64 import b64encode
from sys import argv
from netutils import PollIP
from urllib import request
from re import compile

#Use the dyndns web-site to poll my public IP address
def publicIP():
    myReq = request.urlopen('http://checkip.dyndns.com')
    html = str(myReq.read())
    myReg = compile('[\d.]+')
    myMatch = myReg.search(html)
    print(myMatch.group())
    return myMatch.group()

def updateIP (passedIP):
    newIP = passedIP
    conn = client.HTTPSConnection('mydomain.com', 2083)
    myAuth = b64encode(b'username:thepassword').decode('ascii')
    authHeader = {'Authorization':'Basic ' + myAuth}
    conn.request('GET', '/json-api/cpanel?cpanel_jsonapi_version=2&cpanel_jsonapi_module=ZoneEdit&cpanel_jsonapi_func=edit_zone_record&domain=mydomain.com&line=28&class=IN&type=A&name=homenet.mydomain.com.&ttl=3600&address=' + newIP, headers=authHeader)
    myResponse = conn.getresponse()
    print(myResponse.getcode())
    data = myResponse.read()
    if myResponse.getcode() != 200:
        print('did not succeed')
    print (data)

############################################

if __name__ == "__main__":
    if len(argv) > 1:
        newIP = argv[1]
        print(argv[1])
    else:
        newIP = publicIP()
    updateIP(newIP)

Your variables names are probably not that good. What do you mean by 'new' ? Also, many variables are not even required (I've been pretty far in the cleanup as it helped me to understand the code, you might want to keep some of the variables I've removed). Also, your function names are not really well chosen as it doesn't really describe what they do : updateIP does not seem to update the IP at all.
Separate the concerns : do you want publicIP() to return a string or to print it ? It should probably just return it and then you print it from the main if you want. Please note that doing this introduces an interesting pattern : you print newIP from the 2 branches of your if. You can easily get rid of this duplicated code. In the same spirit, updateIP(ip) seems to be printing things and does not return anything. Let's name the function to make this easier to understand. Please note that : 1) you'd probably want this to return a string in a real life situation 2) you might want it to throw an exception if something fails 3) my functions names are terrible 4) there are naming convention for Python that you'll find in PEP 008 (it's worth a read).
You can get rid of values (urls, ports, login, password, etc) in the middle of the code. This can be useful if you need to reuse them in different places. In any case, it's worth replacing 200 by client.OK which is much clearer.

Once you've done so, here what you code is like :

#!/usr/bin/python3

from http import client
from base64 import b64encode
from sys import argv
from netutils import PollIP
from urllib import request
from re import compile

#Use the dyndns web-site to poll my public IP address
def getPublicIP():
    html = str(request.urlopen('http://checkip.dyndns.com').read())
    reg = compile('[\d.]+')
    match = reg.search(html)
    return match.group()

def printIPFromIP(ip):
    conn = client.HTTPSConnection('mydomain.com', 2083)
    conn.request('GET', '/json-api/cpanel?cpanel_jsonapi_version=2&cpanel_jsonapi_module=ZoneEdit&cpanel_jsonapi_func=edit_zone_record&domain=mydomain.com&line=28&class=IN&type=A&name=homenet.mydomain.com.&ttl=3600&address=' + ip,
        headers={'Authorization':'Basic ' + b64encode(b'username:thepassword').decode('ascii')})
    response = conn.getresponse()
    print(response.getcode())
    if response.getcode() != client.OK:
        print('did not succeed')
    print (response.read())

############################################

if __name__ == "__main__":
    ip = argv[1] if len(argv) > 1 else getPublicIP()
    print(ip)
    printIPFromIP(ip)

It's probably not perfect but I think I hilighted the main issues.


Loading Active Directory users by username and by SID I have many different methods like ones below, but is there way I could improve them and remove duplicate code ?

    public static ADUser Load(string userName)
    {
        using (PrincipalContext pc = MyUtilities.GetPrincipalContext())
        using (UserPrincipalExtension user = UserPrincipalExtension.FindByIdentity(pc, IdentityType.SamAccountName, userName))
        {
            if (user != null)
            {
                return Utilities.LoadUser(user);
            }
        }

        return null;
    }

    public static ADUser LoadBySid(string sid)
    {
        using (PrincipalContext pc = MyUtilities.GetPrincipalContext())
        using (UserPrincipalExtension user = UserPrincipalExtension.FindByIdentity(pc, IdentityType.Sid, sid))
        {
            if (user != null)
            {
                return Utilities.LoadUser(user);
            }
        }

        return null;
    }

I have limited knowledge in C#, but could you not extract something like this?

private static ADUser InternalLoad( IdentityType type, string identity ) {
    using (PrincipalContext pc = MyUtilities.GetPrincipalContext())
    using (UserPrincipalExtension user = UserPrincipalExtension.FindByIdentity(pc, type, identity))
    {
        if (user != null)
        {
            return Utilities.LoadUser(user);
        }
    }

    return null;
}


Summation of large inputs Assume we are given \$ n \$ natural numbers, \$a_{1}, a_{2}, \dots, a_{n} \$ and we are supposed to calculate the following summation:
\$ \sum_{i = 1}^{n}\sum_{j = 1}^{n} \left\lfloor \dfrac{a_{i}}{a_{j}} \right\rfloor\$.
This is a question from a challenge and in only one test, I get time-limit exceeded error.
Can my code become faster?
Here's the question and my code is as follows:
from collections import Counter

numbers = sorted(Counter(int(i) for i in input().split()).items())

sigma = 0
for i in range(len(numbers)):
    for j in range(i, len(numbers)):
        sigma += (numbers[j][0] // numbers[i][0]) * numbers[j][1] * numbers[i][1]

print(sigma)

Your approach takes \$O(n^2)\$ time. Since n can be as large as 100,000, you'll likely need something better. Some benchmarks (inputs are the numbers 1 to n shuffled, and the table shows times in seconds):
     n | original  AJNeufeld  AJ_no_islice  Kelly_Bundy
-------+------------------------------------------------
  1000 |   0.12       0.07          0.06         0.07
 10000 |  11.92       7.98          7.23         0.09
 20000 |  50.82      32.83         29.79         0.11
100000 |      -          -             -         0.18

I didn't run the first three solutions on n=100000, they're too slow. AJ_no_islice is the same as AJNeufeld, except with a regular slice instead of islice to demonstrate that islice is not faster but slower.
My solution in a function that takes a list of numbers and returns the result (much better for testing, and for clarity):
def solve(numbers):
    incs = [0] * 100001
    for numerator, freq in Counter(numbers).items():
        for multiple in range(numerator, 100001, numerator):
            incs[multiple] += freq
    incs = list(accumulate(incs))
    return sum(incs[denominator] for denominator in numbers)

It's similar to the sieve of Eratosthenes, though I mark multiples of all numbers, not just of primes. Runtime is O(n log n).
Consider some numerator, let's say 3. Then for every denominator 3 or higher, it contributes 1 to the total. For every denominator 6 or higher, it contributes 2 to the total. And so on. As a table:
incs = [0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, ...]
So if you then consider some denominator, let's say 7, you simply look up incs[7] to see that it contributes 2 to the total (with the numerator 3).
Now instead consider some other numerator, let's say 4. Its table is:
incs = [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, ...]
Together for numerators 3 and 4:
incs = [0, 0, 0, 1, 2, 2, 3, 3, 4, 5, 5, 5, 7, 7, 7, 8, 9, 9, ...]
How to efficiently compute that table? First only mark the increment spots, e.g., for denominator 3:
incs = [0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, ...]
And for denominators 3 and 4 together:
incs = [0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 2, 0, 0, 1, 1, 0, 1, 0]
And then just accumulate the prefix sums.
Full benchmark code (includes correctness checking):
from timeit import repeat
from random import choices, shuffle
from collections import Counter
from itertools import islice
from itertools import accumulate

def original(numbers):
    numbers = sorted(Counter(numbers).items())
    sigma = 0
    for i in range(len(numbers)):
        for j in range(i, len(numbers)):
            sigma += (numbers[j][0] // numbers[i][0]) * numbers[j][1] * numbers[i][1]
    return sigma

def AJNeufeld(numbers):
    numbers = sorted(Counter(numbers).items())
    sigma = sum((a_j // a_i) * a_j_count * a_i_count
                for i, (a_i, a_i_count) in enumerate(numbers)
                for a_j, a_j_count in islice(numbers, i, None))
    return sigma

def AJ_no_islice(numbers):
    numbers = sorted(Counter(numbers).items())
    sigma = sum((a_j // a_i) * a_j_count * a_i_count
                for i, (a_i, a_i_count) in enumerate(numbers)
                for a_j, a_j_count in numbers[i:])
    return sigma

def Kelly_Bundy(numbers):
    incs = [0] * 100001
    for numerator, freq in Counter(numbers).items():
        for multiple in range(numerator, 100001, numerator):
            incs[multiple] += freq
    incs = list(accumulate(incs))
    return sum(incs[denominator] for denominator in numbers)

funcs = original, AJNeufeld, AJ_no_islice, Kelly_Bundy

# Correctness
numbers = choices(range(1, 100001), k=1000)
expect = funcs[0](numbers)
for func in funcs:
    result = func(numbers)
    print(result == expect, func.__name__,)
print()

# Speed
names = [f' {func.__name__} ' for func in funcs]
print('     n |' + ''.join(names))
print('-------+' + '-' * sum(map(len, names)))
for n in 1000, 10000, 20000, 100000:
    print(f'{n:6} |', end='')
    numbers = list(range(1, n+1))
    shuffle(numbers)
    for func in funcs:
        if n <= 20000 or func is Kelly_Bundy:
            t = min(repeat(lambda: func(numbers), number=1, repeat=1))
            t = '%.2f' % t
        else:
            t = '-'
        print(f'{t:>{len(func.__name__)-1}}   ', end='')
    print()


Comparing std::vector to std::vector A recent comment to an answer of mine here on Code Review brought up an interesting point.  The comment was that one should use std::vector<char> over std::vector<bool> in most cases because the standard requires std::vector<bool> to actually pack bits.  I replied that for small vector sizes, the speed wouldn't matter much and for large ones, cache locality would give the advantage to bool vectors.

However somewhere in between "small" and "large" is a lot of numbers!  I wanted to do a test to characterize any std::vector<bool> advantage.  The method I used is fairly simple.  I wrote a program that takes 3 arguments:

minsize = the smallest vector size to test
maxsize = the largest vector size to test
steps   = the number of steps between those two

Since I wanted to test a large range quickly, and guessing how the vectors would scale, I chose to step through the range logarithmically.  Using the output from the test, and normalizing the speed advantage of std::vector<bool> over std::vector<char> by subtracting the two times and dividing by the size of the vector yielded this chart on my machine (an older 64-bit Linux machine, using g++ version 5.3.1 for x86_64).

It seems that for a range of sizes in the 2000 to 75000 range, the advantage is with std::vector<char> but for all other ranges, the two were either identical (to the resolution of my timer) or the std::vector<bool> had the advantage.

I'm interested in comments on the code or the test method.

booltest.cpp

#include <iostream>
#include <vector>
#include <cstdlib>
#include <cmath>
#include "stopwatch.h"

template <typename F>
struct testfunc {
    F *fn;
    const char *name;
};

#define TEST(x) { x, #x }
template <typename T>
void vectest(unsigned n)
{
    std::vector<T> arr(n, false);
    unsigned remaining = n;
    static constexpr unsigned incr = 13;

    for (unsigned j = 0; j < incr; ++j) {
        for (unsigned i = j; i < n && remaining; i += incr) {
            if (!arr[i]) {
                arr[i] = true;
                --remaining;
            }
        }
    }
}

#define SHOW(x) std::cerr << #x << " = " << x << "\n"

int main(int argc, char *argv[])
{
    const testfunc<decltype(vectest<bool>)> test[]{
        TEST(vectest<char>),
        TEST(vectest<bool>),
    };

    if (argc < 4) {
        std::cerr << "Usage: booltest minsize maxsize steps\n";
        return 0;
    }

    unsigned min = std::stod(argv[1]);
    unsigned max = std::stod(argv[2]);
    unsigned steps = std::stod(argv[3]);
    double logmin = std::log10(min);
    double logmax = std::log10(max);
    double step = (logmax - logmin)/steps;

    SHOW(min);
    SHOW(max);
    SHOW(steps);

    // print header data
    std::cout << "\"n\"";
    for (const auto t : test) {
        std::cout << ", \"" << t.name << "\"";
    }
    std::cout << "\n";

    for (unsigned i = 0; i < steps; ++i) {
        unsigned val = std::pow(10, logmin + i * step);
        std::cout << val;
        for (const auto t : test) {
            std::cout << ", " << timeit<>(t.fn, val);
        }
        std::cout << std::endl;
    }
}

I'm going to intersperse bits of code with my comments on them:

template <typename F>
struct testfunc {
    F *fn;
    const char *name;
};

testfunc isn't an entirely self-explanatory name (especially given that it's a template, so F could be pretty much anything. A comment about the basic intent would be quite helpful.

#define TEST(x) { x, #x }
template <typename T>

I'd rather see a blank link after the end of TEST to make it clear that the subsequent template isn't particularly closely related. Other than that, the same comment as previously applies: the name TEST doesn't tell us much, so a comment (or more explanatory name, or both) might be helpful here.

static constexpr unsigned incr = 13;

Two points here. First, I'm not entirely excited about the name: the value is used as an increment in one place (which seems to fit well with the name) but as the upper limit on a loop in another place (which doesn't fit quite so well).

Second, I'm left wondering exactly what (if any) significance the value 13 has. Is it arbitrary, or does it matter that it happens to be odd, or perhaps it matters that it's prime. Or maybe it's really a carriage return in disguise (oh, but only some crusty old assembly language programmer would notice that; no wonder it never occurred to you:-) !)

std::cout << std::endl;

It seems likely that this is one of the (rare) times that somebody is using std:endl because they really want what it does. Personally, I'd rather do that a bit more explicitly with std::cout << "\n" << std::flush; though. Otherwise, some busybody might conclude you're part of the crowd that routinely uses endl when they really just want a carriage return, and change it to the latter.

As far as the test method goes, it only tests one very specific pattern. It fits well with the Sieve of Eratosthenes, but doesn't tell you much about (for example) repeated manipulations of the same parts of the vector. If that fits closely with your intended usage pattern, that's fine--but there are clearly a lot of other possible uses about which this is likely to tell us little or nothing.


Select input that is generated and populated with options I started to learn PHP OOP recently and searched the web for some practical exercises and I found one that said to build a select input that is generated and populated with options by the object.  I just want to ask for some opinions on code quality.  Could it be done better?  What is the biggest flaw?

The class:

class completeSelect{

    private $setIn;
    private $option;

    public function __construct($val = array()) {
        $this->setIn=$val;

        $this->buildOptions();
    }

    public function buildOptions()
    {
        $this->option = "";
        foreach($this->setIn as $value)
        {
            $this->option .= '<option value="'.$value.'">'.$value.'</option>';
        }

        return $this->option;
    }

    public function BuildSelect() {
        $bs  = '<select name="select_brwoser">';
        $bs .= $this->option;
        $bs .= '</select>';

        return $bs;
    }
}

The output:

require_once("class.php");

$n = new completeSelect(array("Mozilla","Opera","Internet Explorer"));
echo $n->buildSelect();

A few minors:

Your spacing is a bit inconsistent (e.g. missing spaces here completeSelect{ and here $this->setIn=$val;)

Your naming convention is a bit inconsistent as well. I don't do much PHP but class names are usually PascalCase. You should decide whether your methods should be camelCase or PascalCase (you have buildOptions vs BuildSelect).

You have a typo here select_brwoser - should probably be select_browser

Your interface into the class is not quite clear. You build your options on construction yet the method to do so is public. Am I supposed to call it again - if so under which circumstances? If the class is immutable (e.g. you can't add more values for the options) then why not simply build the entire select in the first place and cache it?
A possible change would be to make the buildXYZ methods private and build on construction (assuming the class is immutable) and have public getXYZ methods instead which return the cached value for the options or the select respectively.


Looping through list to create SQL Query I have a list of variables which I need to convert to LIKE statements in SQL. The final query should look like the following:

...
WHERE ([TraceableItem].[IdentificationNo] LIKE N'%015558'
       OR [TraceableItem].[IdentificationNo] LIKE N'%123456'
       OR [TraceableItem].[IdentificationNo] LIKE N'%654321')
...

Below is my code to do it, but I feel it is fairly inefficient / redundant. Especially features like having a bool for firstLoop, and constantly checking for items already existing. I would be very grateful for a code review and tips on how to improve - I'm fairly rusty on C#

string queryInsertString = "([TraceableItem].[IdentificationNo] LIKE ";
string lastBatchNo = batchnos.Last();
List<string> alreadyAdded = new List<string>();
bool firstLoop = true;
foreach(string item in batchnos)
            {
    string splitItem = item.Split('/')[1];
    if (!alreadyAdded.Contains(item)){
        if (!firstLoop)
        {
            queryInsertString += " OR [TraceableItem].[IdentificationNo] LIKE N'%" + splitItem + "'";
        } else
        {
            queryInsertString += "N'%" + splitItem + "'";
            firstLoop = false;
        }
        alreadyAdded.Add(item);
    }
}

Using String.Join makes this kind of string operation a breeze:

// DON'T USE THIS - SQL INJECTION
string predicates = string.Join(" OR ",
        batchNos.Select(item => item.Split('/')[1])
                .Distinct()
                .Select(splitItem => $"[TraceableItem].[IdentificationNo] LIKE N'%{splitItem}'"));
string queryInsertString = "(" + predicates + ")";

You'll notice the all capital warning not to use that code. It's not just because I haven't even checked if it compiles/works, it's because this kind of thing introduces a SQL Injection Vulnerability.

What you need to do is read up on using SqlParameters in C#. You'll want to generate SQL that looks more like:

[TraceableItem].[IdentificationNo] LIKE @param1 OR [TraceableItem].[IdentificationNo] LIKE @param2

You then need to create a SqlParameter instance for each one and add that to the SqlCommand before you execute it. That's assuming you're using Ado.Net, if you're using a framework you'll have to look up the right way of doing it in the one you're using.

The danger is that if someone passes a batch number of "a/'); delete from Users; --", you'll end up creating a query that looks like:

WHERE ([TraceableItem].[IdentificationNo] LIKE N''); delete from Users; -- ...

You do not want a user to be able to do that!


Priority Queue implementation for top k frequent elements leetcode in Ruby While working on the following leetcode problem: https://leetcode.com/problems/top-k-frequent-elements/ I used a Priority Queu implementation in Ruby. I have learned how to implement a Priority Queu in Java in  Robert Sedgewick's Algorithms book. When inserting Hashes into the Priority Queue for example { 1 => 3} this made me refactored the Priotity's Queue in such a way so I was able to compare two hashes values.
The implementation is much straight forward when your dealing with numbers and characters but it made me wonder what would be the best implementation with using objects or hashes.
def top_k_frequent(nums, k)
  ans = []
  h = Hash.new(0)

  nums.each do |num|
    h[num] += 1
  end

  heap = Heap.new
  h.each do |k,v|
    heap.insert({k => v})
  end

  k.times do
    a = heap.del_max
    ans.push(a.keys[0])
  end

  ans
end

class Heap
  def initialize
    @n = 0
    @pq = []
  end

  def insert(v)
    @pq[@n += 1] = v
    swim(@n)
  end

  def swim(k)
    while k > 1 && less((k / 2).floor, k)
      swap((k / 2).floor, k)
      k = k/2
    end
  end

  def swap(i, j)
    temp = @pq[i]
    @pq[i] = @pq[j]
    @pq[j] = temp
  end

  def less(i, j)
    @pq[i].values[0] < @pq[j].values[0]
  end

  def del_max
    max = @pq[1]
    swap(1, @n)
    @n -= 1
    @pq[@n + 1] = nil
    sink(1)
    max
  end

  def sink(k)
    while 2 * k <= @n
      j = 2 * k

      if !@pq[j + 1].nil?
        j += 1 if j > 1 && @pq[j].values[0] < @pq[j + 1].values[0]
      end
      break if !less(k, j)

      swap(k, j)
      k = j
    end
  end
end

There are a few things that come to mind that I wish to probably learn. Keep in mind that I would like to prepare for algorithm interviews and so I would like to avoid as much synthetic sugar as possible.

@pq[j].values[0] check for example how to get to a hash values I need to use values[0] is there a better way for this?
There are 3 loops in the top_k_frequent method. How would yo go about optimizing this implementation?
a.keys[0] notice how I get the key of the element in Ruby is there a cleaner way of doing this?

This brought to my attention that in case of dealing with more complex objects the Priority Queue's API might be needed to refactored to be able to compare between objects, what would be the best way of dealing with that? Would creating another class which you pass to the Priority Queue would be a better solution?
Here is the implementation I have for Priority Queue's prior to editing to deal with hashes.
class Heap
  attr_accessor :pq, :n

  def initialize
    @pq = [] # heap order complete binary tree
    @n = 0 # in pq[1..N] with pq[0] unused
  end

  def empty?
    @n == 0
  end

  def size
    @n
  end

  def insert(v)
    @pq[@n += 1] = v
    swim(@n) # position in heap
  end

  # trickleup
  def swim(k)
    while k > 1 && less((k / 2).floor, k) # parent: k/2 parent less than child?
      swap((k/2).floor, k)
      k = k / 2
    end
  end

  def del_max
    max = @pq[1]  # Retrieve max key from top.
    swap(1, @n) # Exchange with last item.
    @n -= 1
    @pq[@n + 1] = nil # Avoid loitering. max added to end on swap make nil
    sink(1) # Restore HEAP property
    max
  end

  # Trickledown
  def sink(k)
    while 2 * k <= @n
      j = 2 * k # child

      if !@pq[j + 1].nil? # check if there is a right child
        j += 1 if j > 1 && @pq[j] < @pq[j + 1] # if left child less than right child
      end
      break if @pq[k] > @pq[j] # If parent greater than child break

      swap(k, j)
      k = j
    end
  end

  def less(i, j)
    @pq[i] < @pq[j]
  end

  def swap(i, j)
    temp = @pq[i]
    @pq[i] = @pq[j]
    @pq[j] = temp
  end
end

The objects in the priority queue (as implemented above) have their priority determined by <, >, and ==. So however equality is defined for the object determines its priority. For numbers and strings, this is fairly obvious but the default implementation of equality, less than and greater than for hashes may surprise you and not give the result you are expecting.
Perhaps you could consider passing in a block to your initialize method to allow you to provide your own comparison function in those cases where the default one doesn't match your needs. Like Enumerable::sort does.


Django model design to analyse user activities on external websites I am working on an app that analyses the activities of users on some external sites. The contents of the target sites fall into three types:

Forum post: A user can either start a thread or reply to an existing one, the reply will get an unique url;
Blog entry: Any user can post. The blog entry gets an unique url, comments do not.
News article: Not user posted, from a number of news sources. Comments do not have an unique url.

The focus of my app is on user activities, below are some possible questions:

How often does user A post (with respect to types, daily, weekly, etc.)?
What topics are the user mostly interested in? (for this, all threads will be tagged based on their original category and scanned keyword from title/content).
Is user A tend to reply user B's posts more often then to user C's? (obviously this particular question suits more on forum posts and blog entries).

I plan to consolidate a Thread model to hold all three types of contents, do not address the diffrences in them, for example, theat the news source just as a regular user. The Thread model will have roughly 10 millions of records.

Below are the models that I came up in very rudimentary form. I have few questions that need your help:

Is the logic clear in the models?
Regarding the first_seen and last_seen fields in Nick model: if I can look up the Activity model inexpensively in runtime, I would rather not to have them, since the two fields need to be computed each time I insert a new record. What are my better options here?
There will be about 10 sites included, should I have another model just for sites?
The url field in Activity model exists only for a forum type, meaning quite a few millions of records (Blogs and News) will not have a value there, is this a good practice?

Thank you all very much! I appreciate any input!

# should i index some fields?
class Thread(models.Model):
    site = models.CharField(max_length=40)
    thread_type = models.CharField(max_length=20)
    category = models.CharField(max_length=20)
    title = models.CharField(max_length=200)
    pub_date = models.DateTimeField()
    url = models.URLField()
    hits = models.IntegerField()
    votes = models.IntegerField()

    def __str__(self):
        return self.title

class Nick(models.Model):
    site = models.CharField(max_length=40)
    username = models.CharField(max_length=40)
    first_seen = models.DateTimeField(null=True, blank=True)
    last_seen = models.DateTimeField(null=True, blank=True)

    class Meta:
        unique_together = ("site", "username")

    def __str__(self):
        return self.username

class Activity(models.Model):
    usernames = models.ForeignKey(Nick)
    threads = models.ForeignKey(Thread)
    post_date = models.DateTimeField(default=datetime.now, blank=True)
    is_original = models.BooleanField()
    url = models.URLField()     # in case a forum reply and not original

    def __str__(self):
        return Nick.username    # needs to be more informative

Update: The users of all sites are almost from the same population, they may register on multiple sites, may or may not use the same nickname. One of my research goal (in a later phase) is to explore the correlations between users of different sites.

Is the logic clear in the models?

Somewhat. Your narrative didn't touch on the Nick table, which I think many people would name a User table. You didn't make it clear if user alice on the site example1.com is different from alice on example2.com - that's a namespace issue. Your table implies they would be distinct, that there is a compound primary key on (site, username). That would be good DB modeling, but weird from a UX perspective, if you put yourself in Alice's shoes. I would personally lean toward designing this with a unified username namespace across all sites, and similarly a unified thread_id namespace. That way you're always free to shard it out later (or partition it out) by site, but you're never forced to.

The Thread table appears to me to be broken. That is, no PK is defined, and I don't even see an obvious one. You have thread_type, but no thread_id. Maybe pub_date has enough entropy for uniqueness? I'm willing to believe that if we're looking at posts from a single user, but site wide? No, that is difficult to believe. I recommend adding some unique identifier. Remember that Primary Key dictates physical layout on disk, and the details of which rows are near which other rows will affect query performance.

  Regarding the first_seen and last_seen fields in Nick model: if I can look up the Activity model inexpensively in runtime, I would rather not to have them, since the two fields need to be computed each time I insert a new record. What are my better options here?

I'm not happy with the Activity model. Substantively, you haven't described a Primary Key. I propose compound PK (username, post_date), assuming the timestamps are high enough resolution, otherwise the more complex (username, post_date, thread_id). You can assign IDs how you like, in ranges or even as GUIDs, but the post_date will matter for query performance. Trivially, rename to singular "username" and singular "thread_id" (or "thread" if you really want). Probing Nick when computing str() seems silly, as you already have exactly the right username. I notice you chose to leave site out of this table.

Updating the last_seen and (hardly ever) the first_seen columns is not such a big burden. And suppose that last_seen is not part of the UX, not displayed on screen. Then you could choose not to update it in timely fashion. Or if it is displayed, you could let transient memcached track recent updates. However you do it, at some later time, like 5 minutes or hourly or daily, you could do a reporting run where you read max(post_date) from Activity and update Nick. Or keep it out of Nick and instead update some offline reporting table. I'm just saying you have lots of flexibility in how you handle this detail. Nothing wrong with denormalizing in the name of query performance. Just wait till you can benchmark two competing queries so you understand what you're trading off.

  There will be about 10 sites included, should I have another model just for sites?

Not based on what you've told us so far. A GROUP BY (or DISTINCT) would soon extract the current set of sites from one of your existing tables. You've not yet invented any requirements that require mutable site attributes be stored in the database.

  The url field in Activity model exists only for a forum type, meaning quite a few millions of records (Blogs and News) will not have a value there, is this a good practice?

Depends on your query details, but sure, looks fine the way it is. Many urls will be NULL or will be the empty string, depending on whether you choose to make it a nullable column. Either way, it's not like you're losing much.

For rows without an URL, I bet you could avoid writing the row in the first place. I bet a carefully structured thread_id could encode all of (is_original, low_timing_resolution_post_date, unique_thread_number) within 64 or even 32 bits.


Sort an array of sentences alphabetically, ignoring the first word if it equals 'the' (case insensitive) function order_ignore_the($a, $b){
     // orders sentences ascending alphabetically, ignoring the  work "the"
     $args = func_get_args();
     foreach($args as $k=>$v){

          $sort = explode(" ",$v);
          $args[$k] = (strtolower($sort[0]) == "the" ? $sort[1] : $sort[0]);
     }

    return strcmp($args[0], $args[1]);
}

usort($array_of_sentences,'order_ignore_the');

With some testing, it should look like the below

function order_ignore_the($a, $b) {
    return strcasecmp(
        strcasecmp(substr($a, 0, 4), "the ") != 0 ? $a : substr($a, 4),
        strcasecmp(substr($b, 0, 4), "the ") != 0 ? $b : substr($b, 4)
    );
}

Main differences being:

Widening the search for "the" to include the space after. Otherwise, you're matching on things like "theater", etc.
The last substr should likewise have start position 4. Otherwise, you're comparing on the space after "the".
You'll want the main comparison to ignore capitalization, too.


Implementation of Luhn credit card algorithm I've implemented Luhn's algorithm for checking credit card numbers.
My code works, but I wanted to learn about a more efficient and more Pythonic way of doing this.

def validate_credit_card_number(card_number):
    #start writing your code here

    #Step 1a - complete
    temp_list=list(str(card_number))
    my_list=[]
    list1 = temp_list[-2::-2]
    list2=temp_list[::-2]
    list2 = [int (n) for n in list2]
    #print(list2)
    my_list=[int(n) for n in list1]
    #print(my_list)
    list1 = [int(n)*2 for n in list1]
    t_list=list1

    for el in list1:
        sum_res=0

        if el>9:
            idx = list1.index(el)
            t_list.pop(idx)

            while el:
                rem = el%10
                sum_res+=rem
                el = el//10
            t_list.insert(idx, sum_res)
    #print(t_list)

    #step 1b

    list1_sum=sum(t_list)
    list2_sum = sum(list2)
    #print(b_list)
    final_sum = list1_sum+ list2_sum
    #print(final_sum)

    if final_sum%10==0:
        return True
    return False

card_number= 1456734512345698 #4539869650133101  #1456734512345698 # #5239512608615007

result=validate_credit_card_number(card_number)
if(result):
    print("credit card number is valid")
else:
    print("credit card number is invalid")

Style

Python has a style guide called PEP 8. It is a good habit to try to follow it.

In your case, that would mean fixing the missing whitespaces, removing parenthesis in if(result).

Also, you could get rid of old commented code and add a proper docstring if you want to describe the behavior of the function.

Tests

It could be a good idea to write tests before trying to improve your code.

I wrote a very simple code but you could get this chance to dive into unit testing frameworks:

TESTS = [
    (1456734512345698, False),
    (4539869650133101, True),
    (1456734512345698, False),
    (5239512608615007, True),
]

for (card_number, expected_valid) in TESTS:
    valid = validate_credit_card_number(card_number)
    assert valid == expected_valid

Useless test

At the end of the function, you can return directly:

return final_sum % 10 == 0

Useless variables

The my_list variable is not required.

Also, we can take this chance to get rid of the variables at the end of the function:

return (sum(t_list) + sum(list2)) % 10 == 0

Conversion of card_number

At the beginning of the function, you could convert the parts of card_number directly to integer so that you do it in a single place.
Also, that removed the need to the call of list. We just have:

temp_list = [int(c) for c in str(card_number)]

And we can get rid of the line:

list2 = [int (n) for n in list2]

At this stage, the code looks like:

def validate_credit_card_number(card_number):
    temp_list = [int(c) for c in str(card_number)]

    list1 = temp_list[-2::-2]
    list1 = [2 * n for n in list1]

    list2 = temp_list[::-2]

    t_list = list1

    for el in list1:
        sum_res = 0

        if el > 9:
            idx = list1.index(el)
            t_list.pop(idx)

            while el:
                rem = el % 10
                sum_res += rem
                el = el // 10
            t_list.insert(idx, sum_res)

    return (sum(t_list) + sum(list2)) % 10 == 0

Yet another useless variable

t_list aliases list1 (I am not sure if this is intended if if you were planning to have a copy of list1). Whenever you update the list through one variable, the other is affected as well. I highly recommend Ned Batchelder's talk about names and values.

In your case, we can get rid of t_list completely without changing the behavior of the function.

Simplify list logic

You go through multiple steps to modify list1 (or t_list) : index, pop, index. These steps are more expensive/complicated than required. At the end of the day, you do not care about list1, you just want its final sum. You could keep track of the sum directly:

sum1 = 0
for el in list1:
    if el > 9:
        sum_res = 0
        while el:
            rem = el % 10
            sum_res += rem
            el = el // 10
        sum1 += sum_res
    else:
        sum1 += el

return (sum1 + sum(list2)) % 10 == 0

We can take this chance to perform the multiplication in the loop to remove a list comprehension.

Also, we can initialise the sum with sum(list2) so that we don't have to add them at the end:

def validate_credit_card_number(card_number):
    temp_list = [int(c) for c in str(card_number)]

    list1 = temp_list[-2::-2]
    list2 = temp_list[::-2]

    total_sum = sum(list2)
    for el in list1:
        el *= 2
        if el > 9:
            sum_res = 0
            while el:
                rem = el % 10
                sum_res += rem
                el = el // 10
            total_sum += sum_res
        else:
            total_sum += el

    return total_sum % 10 == 0

Math logic

The code uses 10 (the base used for computations) everywhere except for one 9 which seems unexpected. You could write: el >= 10 instead.

Also, that check is not required because the logic applies exactly the same way for elements smaller than 10:

for el in list1:
    el *= 2
    sum_res = 0
    while el:
        rem = el % 10
        sum_res += rem
        el = el // 10
    total_sum += sum_res

Also, you could use el //= 10 but you can get the best ouf of the Python builtins by using divmod returning both the quotient and the remainder:

    while el:
        el, rem = divmod(el, 10)
        sum_res += rem
    total_sum += sum_res

Then, it becomes clear that the variable sum_res is not really required as we could use total_sum instead:

    while el:
        el, rem = divmod(el, 10)
        total_sum += rem

"Final" code

def validate_credit_card_number(card_number):
    temp_list = [int(c) for c in str(card_number)]

    list1 = temp_list[-2::-2]
    list2 = temp_list[::-2]

    total_sum = sum(list2)
    for el in list1:
        el *= 2
        while el:
            el, rem = divmod(el, 10)
            total_sum += rem

    return total_sum % 10 == 0

TESTS = [
    (1456734512345698, False),
    (4539869650133101, True),
    (1456734512345698, False),
    (5239512608615007, True),
]

for (card_number, expected_valid) in TESTS:
    valid = validate_credit_card_number(card_number)
    assert valid == expected_valid

More simplification

Thinking about it, things can still be simplified a lot.
What you are doing with the while loop can be performed using str conversion:

total_sum = sum(list2)
for el in list1:
    total_sum += sum(int(c) for c in str(2 * el))

Going further (too far?), this leads to:

def validate_credit_card_number(card_number):
    temp_list = [int(c) for c in str(card_number)]

    list1 = temp_list[-2::-2]
    list2 = temp_list[::-2]

    total_sum = sum(list2) + sum(sum(int(c) for c in str(2 * el)) for el in list1)
    return total_sum % 10 == 0

Edit:

More simplification

We are using str and int to get the digits of a number... which is known to be smaller than 18 (2 * 9).
A trick could be, once again, to use divmod returning the quotient and remainder and use sum on it.

total_sum = sum(list2)
for el in list1:
    total_sum += sum(divmod(2 * el, 10))

or

total_sum = sum(list2) + sum(sum(divmod(2 * el, 10)) for el in list1)

Playing with indices

Instead of splitting the list into 2 lists, we could iterate over the (reversed) list once and handle differently elements at odd positions and elements at even positions.

We'd get something like:

def validate_credit_card_number(card_number):
    temp_list = [int(c) for c in str(card_number)]

    total_sum = 0
    for i, e in enumerate(reversed(temp_list)):
        if i % 2 == 0:
            total_sum += e
        else:
            total_sum += sum(divmod(2 * e, 10))

    return total_sum % 10 == 0

Or the more concise solution taking advantage of the fact that the divmod trick works for both cases:

def validate_credit_card_number(card_number):
    total_sum = 0
    for i, c in enumerate(reversed(str(card_number))):
        e = int(c) * (2 if i % 2 else 1)
        total_sum += sum(divmod(e, 10))
    return total_sum % 10 == 0

or

def validate_credit_card_number(card_number):
    return sum(sum(divmod(int(c) * (2 if i % 2 else 1), 10))
               for i, c in enumerate(reversed(str(card_number)))) % 10 == 0


Deleting a table row in JavaScript with OOP I started learning OOP in JavaScript at school and I have to do my homework in OOP. It is a shopping list, one fills in a product and price and that is put into an array, the array its contents are put in a table in HTML. The list also has got delete buttons for every product, I have thought about using this in JavaScript, as I am working in OOP, but I am sceptic because of the scope.

The idea is that one clicks "verwijder" on the webpage (EDIT: in the corresponding row) and the table row is gone. I have thought about using this but also about dynamically updating the table after removing the selected row from the array.

Who can advise me about what to do and how to do that correctly? I have uploaded my JavaScript here: http://jsfiddle.net/hNAZ9/

Mainly, it is about this piece of code:

    function ShoppingList(){
    this.items = [];
    this.setItem = function(product, price){
        var item = new Item(product, price);
        this.items.push(item);
    };
    this.render = function(){
        var placeHolder = document.getElementById("shoppingList");
        placeHolder.innerHTML = "";

        for(var i = 0; i <this.items.length; i++){
            var tr = document.createElement("tr");
            tr.id = i;

            var tdProduct = document.createElement("td");
            tdProduct.innerHTML = this.items[i].product;
            tr.appendChild(tdProduct);

            var tdPrice = document.createElement("td");
            tdPrice.innerHTML = this.items[i].price;
            tr.appendChild(tdPrice);

            var tdDel = document.createElement("td");
            tdDel.innerHTML = "Verwijder";
            tdDel.addEventListener("click", delItem, false);
            tr.appendChild(tdDel);

            placeHolder.appendChild(tr);
        }
    };
}

    function delItem(e){
    e.preventDefault();

    // What do I have to do here?
}

The trick is linking a row to its item object and vice-versa, so both are removed/added in lockstep.

There are a lot of ways of going about that, but given your original code, the following should be a pretty good way to go.

I'd suggest using a closure when defining the event handler to make things simpler. Here's a simple demo and here's the code

function ShoppingList() {
    var that  = this, // keep a scope reference
        table = document.getElementById("shoppingList"); // find this once (only works if the page has been loaded already)

    this.items = [];

    // this adds an item to the array and a row to the table
    this.addItem = function (product, price) {
        var item = new Item(product, price);
        this.items.push(item);
        table.appendChild(buildRow(item));
    };

    // this removes the given item from the array and the table
    this.removeItem = function (item) {
        for(var i = 0 ; i < this.items.length ; i++) {
            if(this.items[i] === item) {
                this.items.splice(i, 1);
                break; // skip the rest of the loop, since we've found what we need
            }
        }
        table.removeChild(item.row); // remove the row
    }

    // this isn't actually needed as rows get added by addItem,
    // but it'll work just fine regardless
    this.render = function(){
        table.innerHTML = "";
        for(var i = 0; i < this.items.length; i++){
            table.appendChild(buildRow(this.items[i]));
        }
    };

    // an internal helper. This can be moved to the Item prototype
    // since it's not actually dependent on anything in ShoppingList
    function buildRow(item) {
        // an internal, internal helper
        function buildCell(text) {
            var td = document.createElement("td");
            td.innerHTML = text;
            return td;
        }

        // store the table row object on the item itself
        item.row = document.createElement("tr");

        item.row.appendChild(buildCell(item.product));
        item.row.appendChild(buildCell(item.price));

        var deleteCell = buildCell("Verwijder");
        item.row.appendChild(deleteCell);

        // set the event listener here, where we can access `item` and `tr`
        deleteCell.addEventListener("click", function () {
            that.removeItem(item); // remove the item. Here we need the `that` var
        }, false);

        return item.row;
    }
}


Fade-in / fade-out effects for several buttons The function of this code is to show/hide divs by way of fadeIn/fadeOut, starting with an empty div (home) and fading in one of the other divs (work,cms,contact,) on click, then fading out the last div and fading in the next on click, and then fading out any div and fading in 'panel' (an empty div) when you click on home.

home

work

cms

contact

...

...

...

I have no problem setting this up in html - it makes sense in this case.

home

work

cms

contact

Set a target on each one

$(function() {
    $('li a', '.menu').click(function(e) {
      e.preventDefault();
      var target = $($(this).attr('href'));
      $($(this).data('fadeOut')).fadeOut(function(){
        target.fadeIn();
      });
    })
    $('.panel').hide();
    $('.home_button').click(function(){
        $('.panel:visible').fadeOut();
    });
})

You get the nifty coincidental benefit here of this working ok even without javascript since  also happens to be the the syntax for 'scroll-to element'

Also, you should not name functions with an uppercase capital. Because javascript has no native way of determining which functions are meant to be run in a functional style and which are meant to be used as constructors with the new keyword, it is a very well known convention to use upper case names only for functions which are meant to be invoked with the new keyword. Learn more about javascript capitalization conventions here.

Edit: Totally misread your code. I believe my rewrite now reflects what you're trying to do