texts: Collect all items containing a word to a Multimap

texts:
I have a simple class like

class Item {
    Set<String> wordSet() {...}
}

So each item has a set of words and I need the inverse relation, i.e., for each word find all items containing it. This gets used a lot, so I want an ImmutableSetMultimap allowing me to retrieve it quickly.

I'm using

ImmutableSetMultimap<String, Item> wordToItemsMultimap(Collection<Item> items) {
    Multimap<Item, String> itemToWords = items
        .stream()
        .collect(Multimaps.flatteningToMultimap(
             i -> i,
             i -> i.wordSet().stream(),
             HashMultimap::create));
    Multimap<String, Item> wordToItems =
             Multimaps.invertFrom(itemToWords, HashMultimap.create());
    return ImmutableSetMultimap.copyOf(wordToItems);
}

for creating the map and it works, but I find it too complicated. I'm looking for a simpler solution.

I don't care much about efficiency of the above snippet (as it gets called just once), but optimization hints are welcome as I'm doing a lot of similar things where speed matters.


texts: Authenticate ASP.NET Web Api 2 using OWIN and Firebase

texts:
I'm about to go live with an app, and would, therefore, appreciate a review of my firebase authentication setup. I haven't been able to find many resources describing how to perform such authentication and most importantly securely using OWIN and ASP.NET 4.6. It's worthwhile to note that I've had some issues with resolving the SigningKeys (using an IssuerSigningKeyResolver) and I really need to know if I've missed something important so authentication doesn't suddenly start to fail.

Before adding the resolver, authentication would fail after what seems to be 24 hours - probably because Google replaces their certificates and that the key used to sign the JWT token can't be found and validated. It seems to be working in its current state.

The method below is called during startup:

public static async void ConfigureAuth(IAppBuilder app)
{
    const string validAudience = "MY-FIREBASE-PROJECT";
    const string validIssuer = "https://securetoken.google.com/MY-FIREBASE-PROJECT";
    const string openIdDiscoveryEndpoint = "https://securetoken.google.com/MY-FIREBASE-PROJECT/.well-known/openid-configuration";

    var openidConfigManager = new ConfigurationManager<OpenIdConnectConfiguration>(openIdDiscoveryEndpoint);
    var openidConfig = await openidConfigManager.GetConfigurationAsync();
    var certificates = await GetGoogleCertificatesAsync();
    var issuer = openidConfig.Issuer;
    var tokens = openidConfig.SigningTokens;

    app.UseJwtBearerAuthentication(new JwtBearerAuthenticationOptions
    {
        AuthenticationMode = AuthenticationMode.Active,
        AllowedAudiences = new[] { validAudience },
        Provider = new OAuthBearerAuthenticationProvider
        {
            OnValidateIdentity = OnValidateIdentity
        },
        IssuerSecurityTokenProviders = new[]
        {
            new OpenIdConnectCachingSecurityTokenProvider(
                openIdDiscoveryEndpoint)
        },
        TokenValidationParameters = new TokenValidationParameters
        {
            NameClaimType = "user_id",
            RoleClaimType = ClaimTypes.Role,
            IssuerSigningTokens = tokens,
            ValidAudience = validAudience,
            ValidIssuer = validIssuer,
            IssuerSigningKeys = certificates.Values.Select(x => new X509SecurityKey(x)),
            IssuerSigningKeyResolver = (token, securityToken, identifier, parameters) =>
            {
                var updatedCertificates = GetGoogleCertificates();

                return identifier
                    .Select(x => !updatedCertificates
                        .ContainsKey(x.Id)
                        ? null
                        : new X509SecurityKey(updatedCertificates[x.Id]))
                    .First(x => x != null);
            }
        }
    });
}

The following methods are used to fetch Google certificates.

public static async Task<Dictionary<string, X509Certificate2>> GetGoogleCertificatesAsync()
{
    using (var http = new HttpClient())
    {
        var response = await http.GetAsync("https://www.googleapis.com/robot/v1/metadata/x509/securetoken@system.gserviceaccount.com");

        var dictionary = await response.Content.ReadAsAsync<Dictionary<string, string>>();
        return dictionary.ToDictionary(x => x.Key, x => new X509Certificate2(Encoding.UTF8.GetBytes(x.Value)));
    }
}

public static Dictionary<string, X509Certificate2> GetGoogleCertificates()
{
    var task = Task.Run(async () => await GetGoogleCertificatesAsync());
    return task.Result;
}

OpenIdConnectCachingSecurityTokenProvider is used to fetch OpenId Connect metadata and SecurityTokens. It can be found on GitHub here.
OnValidateIdentity is only used to add custom roles/claims and is thus omitted.

Please, ask if you have any questions or feel that something is missing.


texts: Design Pattern - Strategy Pattern in PHP

texts:
Take a look at the following code and let me know if its right, and if there is any room for improvements.

Strategy Class

<?php
/**
 * Draw Strategy class
 */
abstract class DrawStrategy
{
    abstract function draw(Shape $shape);
}
/**
 * This is ConsoleDraw and takes the shape object and draws accordingly, Drawign Algo is
 * implemented
 * here
 */
class ConsoleDraw extends DrawStrategy
{
    public function draw(Shape $shape)
    {
        echo "---------------";
    }
}

class WebDraw extends DrawStrategy
{
    public function draw(Shape $shape)
    {
        echo "=-=-=-=-=-=-=-=-=-";
    }
}

Shape Class ( That Composites Strategy Class )

<?php
/**
 * Design Pattern Impl. of Composite Pattern.
 *
 * Background:
 *
 * Shape is the Object, It can be specialised as Square or Rectangle and It can be drawn on
 * Web, or on Console / commandline
 *
 * The Drawing Algo is abstracted in Strategy Class and its used in the Shape Class
 *
 * Shape Class Uses DrawingStrategy and used accordingly, Uses ConsoleDraw, WebDraw as required
 *
 *
 */
include "strategy.php";

/**
 * Abstract Base Class Shape (Generalisation)
 *
 */
abstract class Shape
{
    public $length = 0;

    public function __construct( $_length )
    {
        $this->length = $_length;
    }

    /**
     * [draw this abstracts draw functionality]
     * @return [type] [description]
     */
    abstract function draw();

    /**
     * [area This is another function that calculates area]
     * @return [type] [description]
     */
    abstract function area();

}

/**
 *  SQuare Class Extends Shape and defines the implementation,
 *  It uses DrawStrategy as a component and so that it can draw either on the console or
 *  on the web.
 *
 */
class Square extends Shape
{
    public $length = 0;

    private $drawstrategy;

    public function __construct( $_length , DrawStrategy $_strategy )
    {
        $this->length = $_length;

        $this->drawstrategy = $_strategy;
    }

    public function area(){
        return $this->length * $this->length;
    }
    /**
     * draw - Takes up the Draw Strategy and draw on the current object
     * wether it is on console or web
     * @return [type] [description]
     */
    public function draw()
    {
        $this->drawstrategy->draw( $this );
    }

}

class Rectangle extends Shape
{
    private $drawstrategy;

    public function __construct( $_length, $_breadth, DrawStrategy $_strategy )
    {
        $this->length = $_length;
        $this->breadth = $_breadth;

        $this->drawstrategy = $_strategy;
    }

    public function area()
    {
        return $this->length * $this->breadth;
    }

    public function draw()
    {
        $this->drawstrategy->draw( $this );
    }
}

$shape[] = new Square( 4 , new ConsoleDraw );
echo "Shape 0 " .$shape[0]->area();
$shape[0]->draw();

$shape[] = new Square( 10, new WebDraw );
echo "Shape 1";
$shape[1]->draw();


texts:
Want to brush up my rusty web dev skills. I want to call the twitch API for several channels and eventually call a callback function when all results are in without using a sync call.

I didn't work with JavaScript for a long time, so I'm not sure if my solution is a good way, especially how I figure out if the callback function should be called.

var Twitch = function() {
    var baseUrl = 'https://api.twitch.tv/kraken/streams/';
    var channels = ["freecodecamp", "storbeck", "terakilobyte", "habathcx", "RobotCaleb", "thomasballinger", "noobs2ninjas", "beohoff"];

    this.update = function(callback) {
        var result = {};
        for (var i = 0; i < channels.length; i++) {
            var qUrl = baseUrl + channels[i];
            var name = channels[i];
            $.getJSON(qUrl, success(name, result, callback));
        }
    }

    function success(name, result, callback) {
        return function(data) {
            result[name] = data;
            if (results.length == channels.length) {
                callback(result);
            }
        }
    }
};


texts:
This is my simple implementation on JS HTML table generator which generates a table based on an array of JS objects.

function Column(caption,position,isVisible,values){
  this.caption = caption;
  this.position = position;
  this.isVisible = isVisible;
  this.values = values;
}

Column.prototype.printValue = function (index){
      return  ""+ this.values[index] +"	";
};

var columnArray = [
                    new Column("Id",0,true,[1,2,3]),
                    new Column("Name",1,true,["Tim","Jim","Tom"]),
                    new Column("Date",2,true,["27-03-2014","01-01-2014","11-04-2015"])
                  ];

function columnSorter(columnA,columnB){
  if(columnA.position < columnB.position)
    return -1;
  if(columnA.position > columnB.position)
    return 1;
  return 0;
}

columnArray.sort(columnSorter);

function generateTable(columnArray){
  var htmlString = "

";

  for(var col in columnArray){
    htmlString += ""+columnArray[col].caption+"	";
  }

  htmlString += "

";

  htmlString += "
";
  for(var i=0;i<columnArray[0].values.length;i++){
    htmlString += "
";
    for(col in columnArray){
      htmlString += columnArray[col].printValue(i);
    }
    htmlString += "
";
  }
  htmlString +="
";

  htmlString += "

";
  return htmlString += "
";
}


texts:
I've published a Vim plugin that nudges visually selected lines up or down, which for the most part seems to function as designed. However, I believe there are likely ways that this plugin could be improved.

Questions

How do I register a movement that takes an optional amount?

In other words how can this plugin differentiate between Shift^J and 3Shift^J?

Have I made any critical mistakes?

Are there any features that should be added?

Requirements
This plugin is tested on Linux based operating systems, but suggestions on how to make it OS agnostic are certainly welcomed; regardless Vim should be installed prior to using this plugin, eg...
sudo apt-get install vim

Setup
Source code is maintained on GitHub, as are the documentation files and README.md. Included within this question are the TLDR and source .vim script code files.
mkdir -vp ~/git/hub/vim-utilities

cd ~/git/hub/vim-utilities

git clone git@github.com:vim-utilities/nudge-lines.git

If not utilizing some form of Vim plugin manager, then the linked-install.sh script may be run on most 'nix devices...
./linked-install.sh

... which will symbolically link plugin scripts and documentation, and update Vim documentation tags.

Usage
After visually selecting a line, or range of lines, use the following keyboard shortcuts:

Ctrl^k Nudge visual selection up

Shift^K Nudge visual selection up and reformat tabs

Ctrl^j Nudge visual selection down

Shift^J Nudge visual selection down and reformat tabs

Source Code
plugin/nudge-lines.vim
#!/usr/bin/env vim

vnoremap <S-K> :call Nudge_Selection__Laterally(-1, 'reformat')<CR>
vnoremap <C-K> :call Nudge_Selection__Laterally(-1)<CR>

vnoremap <S-J> :call Nudge_Selection__Laterally(1, 'reformat')<CR>
vnoremap <C-J> :call Nudge_Selection__Laterally(1)<CR>

""
" Nudges visually selected lines up or down
" @param {number} a:amount - Signed integer of lines to nudge selection by
" @param {string[]} a:0 - Optionally reformat tabs
" @example
"   :'<,'> call Nudge_Selection__Laterally(-1, 'reformat')
" @author S0AndS0
" @license AGPL-3.0
function! Nudge_Selection__Laterally(amount, ...) abort range
  let l:not_visual_mode = visualmode() != 'V'
  let l:at_limits__lower = a:lastline == line('$')
  let l:at_limits__upper = a:firstline == 1
  if l:not_visual_mode
  \ || (a:amount > 0 && l:at_limits__lower)
  \ || (a:amount < 0 && l:at_limits__upper)
    normal! gv
    return
  endif

  if a:amount > 0
    let l:move_command = "'>+" . a:amount
  elseif a:amount < 0
    let l:move_command = "'<-" . (1 - a:amount)
  else
    throw 'amount must be greater or less than 0'
  endif

  execute "'<,'>move " . l:move_command

  if a:0 == 0
    normal! gv
  else
    normal! gv=gv
  endif

  silent! foldopen!
endfunction

Side note, I'm not sure what's going on with syntax highlighting here, but as of latest revisions this plugin seem to function without error.


texts: Split string into 160-character chunks while adding text to each part

texts:
The send_sms_message method takes text as an argument. The length of text is unknown and can be 0 or as large as 1070 (after that we will have no space for the actual text, because multi-part message text will be so long).

The trick is that if text is larger then 160 characters, we need to split it into parts and to each part add text part 1 of 2.

The problem is that we don't know the length of the extra text. It can be 14 characters for part 1 of 2 or 16 for part 11 of 99 and so on.

This code can handle up to 99 parts. How can I make it generic to handle more parts without adding more conditions?

SMS_LENGTH = 160
MPM_SIZE_LONG = 16
MPM_SIZE_SHORT = 14
MPM_SHORT_LIMIT = 1314

def send_sms_message(text, to, from)
  unless text.length > SMS_LENGTH
    deliver_message_via_carrier(text, to, from)
  else
    parts = text.scan(/.{1,#{SMS_LENGTH - (text.length > MPM_SHORT_LIMIT ? MPM_SIZE_LONG : MPM_SIZE_SHORT)}}/)
    parts.to_enum.with_index(1) do |message_part, index|
      deliver_message_via_carrier("#{message_part} - Part #{index} of #{parts.length}", to, from)
    end
  end
end


texts: Cloning Python standard library's function math.frexp() that extracts exponent and mantissa from a floating point number

texts:
This is Python 3.5 code.

I have attempted to develop my own frexp() function that mimics the functionality of Python standard library's math.frexp().

I'm interested in suggestions for how improve my code style to become more Pythonic and how to achieve better performance and precision for this function.

My code:

import ctypes
def frexp(x):
    *binstr, = bin(ctypes.c_int.from_buffer(ctypes.c_float(x)).value)
    if binstr[0] == '-' : binstr.pop(0)
    exponent = int ( (''.join(binstr))[2:10], 2 ) - 127
    mantissa = 1
    for index, item in enumerate(''.join(binstr)[10:]):
        if item == '1':
            mantissa += 2 ** ( -(index + 1) )
    return mantissa, exponent

Precision: not accurate

>>> import random
>>> testednumber = random.random() + random.random() * ( 10 ** 10 )
>>> testednumber
5478324268.848498
>>> # This is my code's output
>>> import mymath
>>> mymath.frexp(testednumber)
(1.2755217552185059, 32)
>>> # This is Python's frexp() output
>>> import math
>>> math.frexp(testednumber)
(0.6377608828303053, 33)
>>> # my result:
>>> (1.2755217552185059) * ( 2 ** 32 )
5478324224.0
>>> # Python's frexp() result:
>>> (0.6377608828303053) * ( 2 ** 33 )
5478324268.848498

Speed: 55 times slower than math.frexp()

>>> # This is my minimum speed
>>> print(min(timeit.Timer("mymath.frexp(testednumber)", setup="import mymath; testednumber = 5478324268.848498").repeat(7,1000)))
0.01864783600103692
>>> print(min(timeit.Timer("mymath.frexp(testednumber)", setup="import mymath; testednumber = 5478324268.848498").repeat(7,1000)))
0.01870606800002861
>>> print(min(timeit.Timer("mymath.frexp(testednumber)", setup="import mymath; testednumber = 5478324268.848498").repeat(7,1000)))
0.018558342000687844
>>> # This is Python's frexp() minimum speed
>>> print(min(timeit.Timer("math.frexp(testednumber)", setup="import math; testednumber = 5478324268.848498").repeat(7,1000)))
0.0003347589990880806
>>> print(min(timeit.Timer("math.frexp(testednumber)", setup="import math; testednumber = 5478324268.848498").repeat(7,1000)))
0.0003345459990669042
>>> print(min(timeit.Timer("math.frexp(testednumber)", setup="import math; testednumber = 5478324268.848498").repeat(7,1000)))
0.0003347729998495197


texts: Filter non-duplicate objects from collection

texts:
I need to filter an array of form controls to check for duplicate values. I have written the following algorithm to identify and return an array of the controls which have are duplicates. I have not seen this indexOf based approach in any of the sample duplicate checkers one finds in Google. Can anyone here spot a flaw in my code?

Testing codepen.

// select all keys
const duplicateKeys = values.map((current) => current.name);
// filter duplicate keys
const filteredDuplicateKeys = duplicateKeys.reduce((prev, name, index, names) => {
  if (names.indexOf(name) !== names.indexOf(name, index)) {
    if (!prev.includes(name)) return prev.concat(name);
  }
  return prev;
}, []);
// filter original array based on identified duplicate keys
const duplicates = values.reduce((prev, current) => filteredDuplicateKeys.indexOf(current.name) > -1 ? prev.concat(current) : prev, []);


texts: Handle concurrent request by waiting the result of an already running operation

texts:
I need to handle concurrent request by waiting the result of an already running operation.

Requests for data may come in simultaneously with same/different credentials.

For each unique set of credentials there can be at most one GetDataInternal call in progress, with the result from that one call returned to all queued waiters when it is ready.

After this the results of the previous call are invalidated, and a new GetDataInternal call will be allowed with the same set of credentials.

Parallel calls to GetDataInternal with different credentials are allowed.

private readonly ConcurrentDictionary<Credential, Lazy<Data>> Cache
= new ConcurrentDictionary<Credential, Lazy<Data>>();

public Data GetData(Credential credential)
{
    // This instance will be thrown away if a cached
    // value with our "credential" key already exists.
    Lazy<Data> newLazy = new Lazy<Data>(
        () => GetDataInternal(credential),
        LazyThreadSafetyMode.ExecutionAndPublication
    );

    Lazy<Data> lazy = Cache.GetOrAdd(credential, newLazy);
    bool added = ReferenceEquals(newLazy, lazy); // If true, we won the race.
    Data data;

    try
    {
       // Wait for the GetDataInternal call to complete.
       data = lazy.Value;
    }
    finally
    {
        // Only the thread which created the cache value
        // is allowed to remove it, to prevent races.
        if (added) {
            Cache.TryRemove(credential, out lazy);
        }
    }

    return data;
}

Do I have a bugs? Is it thread safe? How can I improve my code? I'm using .net framework 4.5.2


texts:
I think my error-handling here is atrocious, but I'm really new to Haskell so hoping one of you sages can help me out. This program is a command-line tool which you use by typing one of two commands: "Add  " or "Check  ". When you Add a pair of ints, it makes them connected, and when you Check two ints, it tells you whether they are connected (directly or indirectly). This uses the Union find data structure.

import Data.Map (Map)
import qualified Data.Map as Map
import qualified Text.Read as Read

data DisjointSet k = DisjointSet
  { parent :: Map k k
  , rank :: Map k Int
  }

empty :: (Ord k) => DisjointSet k
empty = DisjointSet {parent = Map.empty, rank = Map.empty}

make :: Ord k => k -> DisjointSet k -> DisjointSet k
make k d
  | (Map.member k (parent d)) = d
  | otherwise =
    d {parent = Map.insert k k (parent d), rank = Map.insert k 0 (rank d)}

find :: Ord k => k -> DisjointSet k -> Maybe (k, DisjointSet k)
find k d = do
  k' <- (Map.lookup k (parent d))
  if (k' == k)
    then return (k, d)
    else do
      (k'', d') <- find k' d
      return (k'', d' {parent = Map.insert k k'' (parent d')})

merge :: Ord k => k -> k -> DisjointSet k -> Maybe (DisjointSet k)
merge k1 k2 d =
  let d' = (make k1 (make k2 d))
  in do (k1', d'') <- find k1 d'
        (k2', d''') <- find k2 d''
        r1 <- Map.lookup k1' (rank d''')
        r2 <- Map.lookup k2' (rank d''')
        case compare r1 r2 of
          LT -> return d''' {parent = Map.insert k1' k2' (parent d''')}
          GT -> return d''' {parent = Map.insert k2' k1' (parent d''')}
          EQ ->
            return
              d'''
              { parent = Map.insert k1' k2' (parent d''')
              , rank = Map.insert k2' (r2 + 1) (rank d''')
              }

data Action
  = Add Int
        Int
  | Check Int
          Int
  deriving (Show, Read)

main :: IO ()
main = repl empty

repl :: DisjointSet Int -> IO ()
repl d = do
  line <- getLine
  case (Read.readMaybe line) of
    Just action ->
      case action of
        Add k1 k2 ->
          case (merge k1 k2 d) of
            Just d' -> repl d'
        Check k1 k2 ->
          case (find k1 d) of
            Just (k1', d') ->
              case (find k2 d') of
                Just (k2', d'') -> do
                  if (k1' == k2')
                    then putStrLn "Together"
                    else putStrLn "Apart"
                  repl d''

How do I clean up my code?


texts:
I want to ask if you can review the code for a simple contact form and the PHP code which sends me an email once a visitor fills the form and is validated through Recaptcha

On my index.php file here are the form and php code

HTML:

<form id="contactform" action="index.php#contact" method="post" class="form" role="form">

      <input class="form-control required" id="vname" name="vname" placeholder="Your Name" type="text" required />

      <input class="form-control required" id="vemail" name="vemail" placeholder="Your Email" type="email" required />

      <textarea class="form-control required" id="msg" name="msg" placeholder="Your Message" rows="7" required></textarea>

      <input type="submit" class="btn btn-success form-send" value="Send">

</form>

PHP:

<?php
  $captcha;
    if (isset($_POST['g-recaptcha-response'])) { $captcha = $_POST['g-recaptcha-response']; }
  // Check for correct reCAPTCHA
    $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=GOOGLE SECRET KEY&response=" . $captcha . "&remoteip=" . $_SERVER['REMOTE_ADDR']);
    if (!$captcha || $response.success == false) {
        echo "Your CAPTCHA response was wrong";
        exit ;
      } else {
        // Check for Blank Fields..
        if ($_POST["vname"] == "" || $_POST["vemail"] == "" || $_POST["msg"] == "") {
    echo "Please fill all required fields";
} else {
    // Check if the "Sender's Email" input field is filled out
    $email = $_POST['vemail'];
    // Sanitize E-mail Address
    $email = filter_var($email, FILTER_SANITIZE_EMAIL);
    // Validate E-mail Address
    $email = filter_var($email, FILTER_VALIDATE_EMAIL);
    if (!$email) {
        echo "Invalid Sender's Email";
    } else {
        $to = 'myemail@mail.com';
        $subject = 'New Form Entry';
        $message = "New message was submitted from
 " . "" . $_POST['vname'] . "" . "

The message is:
" . "" . $_POST['msg'] . "";
        $headers = "From:" . $_POST['vname'] . "<" . $email . ">";
        $headers .= "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
        // Sender's Email
        // Message lines should not exceed 70 characters (PHP rule), so wrap it
        $message = wordwrap($message, 70, "\r\n");
        // Send Mail By PHP Mail Function
        if (mail($to, $subject, $message, $headers)) {
            echo "Your mail has been sent successfully!";
        } else {
            echo "Failed to send email, try again.";
            exit ;
        }
    }
}
}
?>

The only function of this is to send me an email and it works as I want it, but I am not aware if this form is secure?

Is it possible that someone can exploit this code and upload a shell, or do any other sort of attack against my site.


texts: Extension method to format properties that are not null and not empty

texts:
I'm trying to work with the following class, which is inside a library I currently cannot change:

// This class represents "external" code as far as my review
// request is concerned: I cannot change this, currently.
public class AddressDto
{
    public string Name { get; set; }
    public string Zip { get; set; }
    public string City { get; set; }
    public string AddressLine { get; set; }
    public string ExtraInfo { get; set; }
}

I'm writing an extension method with c# 5 and .NET 4.5.1 to represent instances of that class as a string for display purposes. I'm looking to get feedback on the inner workings of this method:

// Looking for feedback on this method:
public static string FormatForGui(this AddressDto Address)
{
    if (Address == null) throw new ArgumentNullException("Address");

    var parts = new[]
    {
        Address.Name,
        Address.AddressLine,
        Address.Zip,
        Address.City
    };

    return string.Join(" ", parts.Where(s => !String.IsNullOrEmpty(s)));
}

Can this method body be improved?

For your convenience, here's a condensed NUnit unit test to define the behavior:

// Not necessarily looking for feedback on these tests, my actual tests are more DAMP
// but for brevity I condensed them into one test:
[Test]
public void AllTests()
{
    Assert.Throws<ArgumentNullException>(() => ((AddressDto)null).FormatForGui());
    Assert.That(new AddressDto { }.FormatForGui(), Is.EqualTo(""));
    Assert.That(new AddressDto { Name = "" }.FormatForGui(), Is.EqualTo(""));
    Assert.That(new AddressDto { Name = "A" }.FormatForGui(), Is.EqualTo("A"));
    Assert.That(new AddressDto { Name = "A", AddressLine = "B" }.FormatForGui(), Is.EqualTo("A B"));
    Assert.That(new AddressDto { Name = "A", AddressLine = "B", Zip = "C" }.FormatForGui(), Is.EqualTo("A B C"));
    Assert.That(new AddressDto { Name = "A", AddressLine = "B", Zip = "C", City = "D" }.FormatForGui(), Is.EqualTo("A B C D"));
    Assert.That(new AddressDto { Name = "A", AddressLine = "B", City = "D" }.FormatForGui(), Is.EqualTo("A B D"));
}


texts: The average amount of time required to travel between two points in a torus-like space

texts:
I have recently started to work with C++ and I do consider myself quite basic in C++ coding.
I have made the following two functions and I was wondering whether someone could give me some hints on how to improve and speed up this code.
Please, note that the second function is then used in R through Rcpp R package.
#include <Rcpp.h>
using namespace Rcpp;
using namespace std;

double sumAll(double i, double j, int a, int b, double coeff) {

  double pi = 2 * acos(0.0);

  double total = 0;
  int half_a =  a / 2;
  int half_b =  b / 2;

  for (int k = 0; k <= half_a; ++k) {

    for (int l = 0; l <= half_b; ++l) {

      double fkl;
      if (k == 0 && l == 0) {
        fkl = 0;
      } else {
        fkl = pow((1 - (coeff * (1 - cos(2.0 * pi * k / a)))), 2.0) *
              pow((1 - (coeff * (1 - cos(2.0 * pi * l / b)))), 2.0);
      }

      double a1 = 1 - (cos(2.0 * pi * i * k / a) * cos(2.0 * pi * j * l / b));

      double deltaa;
      if (k == 0) {
        deltaa = 1;
      } else {
        deltaa = 0.5;
      }

      double deltab;
      if (l == 0) {
        deltab = 1;
      } else {
        deltab = 0.5;
      }

      if (k == 0 && l == 0) {

        total = total + 0;

      } else {

        double res = (fkl * a1) / (deltaa * deltab * (1 - fkl));

        total = total + res;
      }
    }
  }
  return total;
}

// [[Rcpp::export]]
NumericVector series_Sum(NumericVector xvec, NumericVector yvec, int a, int b,
                         double coeff) {

  int size = xvec.length();
  NumericVector series_res(size);

  for (double i = 0; i < size; i++) {
    series_res[i] = sumAll(xvec[i], yvec[i], a, b, coeff);
  }

  return series_res;
}

Consider that in sumAll :

i and j: are integers, which can get values between 0 and a/2 or 0 and b/2, respectively.
coeff: can be a value between 0 and 1.0.
a and b: can be any integer values higher than 5 (arbitrary choice).

In series_Sum :

xvec and yvec: are vectors of i and j values.

In other words, series_Sum use sumAll over several possible combinations of i and j.


texts: Simplify function to sum the absolute value of the difference between all pairs of elements of a sorted array

texts:
Given a sorted array of N elements, I need to find the absolute sum of the differences of all elements.

Given 4 elements (1, 2, 3 and 4):

  |1-2|+|1-3|+|1-4|+|2-3|+|2-4|+|3-4| = 10

Here is my code in Java:

List<Integer> a = new ArrayList<Integer>(); //just for understanding , the Array List is already filled with numbers
public static int lsum(int N)
{
    int sum =0;

    for( int i=0;i<N;i++)
    {
        int w =a.get(i);
        for(int j =i;j<N;j++)
        {
            int z = a.get(j);
            sum =sum +(z-w);
        }

        }
    return(sum);

}

I'm looking for an efficient algorithm rather than the trivial one I am using (O(n2) complexity). This is a requirement for a bigger program which requires this function. The input (number of elements) can be as big as 105.


texts: mapM for both keys and values of Data.Map

texts:
I'm writing a monadic parser instance that transforms Data.Map.Map's:

instance (Ord a, FromEDN a, FromEDN b) => FromEDN (M.Map a b) where
    parseEDNv (E.Map m) = mapMmap parseEDNv parseEDN m
    parseEDNv v = typeMismatch "Map" v

Data.Map doesn't provide it's own mapM version like Data.Vector does, so i had to write it from scratch:

mapMmap :: (Ord a2, Monad m) => (a1 -> m a2) -> (b1 -> m b2) -> M.Map a1 b1 -> m (M.Map a2 b2)
mapMmap kf vf m = do
    let pairsIn = M.assocs m
    pairsOut <- mapM fs pairsIn
    return $! M.fromList pairsOut
    where
        fs (k, v) = do
            newK <- kf k
            newV <- vf v
            return (newK, newV)

It works, but very verbose. How to trim into a more succinct INLINEable version without too much black^W monadic magic?


texts: Encoding multiple rows from prepared statement in php

texts:
I have this code that hypothetically returns multiple rows from a database and encodes them in a JSON. I am wondering if this is the best and most presentable way to do it.

Note: I am using msqli not PDO, and procedural php not OOP.

<?php
$query = $_REQUEST["query"];
require(connect.php);

$response = array();
$response["success"] = false;

if ($con) {
    $statement = mysqli_prepare($con, "SELECT * FROM `lecturers_general` WHERE ? IN (lecturer_id, title, first_name, middle_name, last_name, course1, course2, course3, course4, course5, course6, office_number, office_building, department)");
    mysqli_stmt_bind_param($statement, "s", $query);
    mysqli_stmt_execute($statement);
    $lecturer_id = null; $title = null; $first_name = null; $middle_name = null; $last_name = null; $course1 = null; $course2 = null; $course3 = null; $course4 = null; $course5 = null; $course6 = null; $office_number = null; $office_building = null; $department = $null;

    mysqli_stmt_bind_result($lecturer_id, $title, $first_name, $middle_name, $last_name, $course1, $course2, $course3, $course4, $course5, $course6, $office_number, $office_building, $department);
    // $rows = mysqli_fetch_assoc($statement);
    $num_rows = mysqli_num_rows($statement);

    if ($num_rows >= 1) {
        do {
            $lecturer_id = $rows["lecturer_id"];
            $title = $rows["title"];
            $first_name = $rows["first_name"];
            $middle_name = $rows["middle_name"];
            $last_name = $rows["last_name"];
            $course1 = $rows["course1"];
            $course2 = $rows["course2"];
            $course3 = $rows["course3"];
            $course4 = $rows["course4"];
            $course5 = $rows["course5"];
            $course6 = $rows["course6"];
            $office_number = $rows["office_number"];
            $office_building = $rows["office_building"];
            $department = $rows["department"];

            $result['lecturers: '][] = array('lecturer_id' => $lecturer_id,'title' => $title,'first_name' => $first_name,'middle_name' => $middle_name,'last_name' => $last_name,'course1' => $course1,'course2' => $course2,'course3' => $course3,'course4' => $course4,'course5' => $course5,'course6' => $course6,'office_number' => $office_number,'office_building' => $office_building,'department' => $department);
        } while($rows = mysqli_fetch_assoc($statement));

        echo json_encode($result);
    } else {
        echo 'No results';
    }
}

?>


texts:
One of my first serious Python programs, realized Conway's Game of Life in Python. Not sure that get_cell_state and update_table functions are good as they might be. Would be glad to see any improvements or critique. Thank you for reviewing ^^.

import time
import random
import copy
import os

black_square_symbol = chr(int('25A0', 16))  # a nice unicode black square symbol

def get_cell_state(table, row, col):
    if row in range(len(table)) and col in range(len(table[0])):
        # little hack to return 0 if row and col are out of rang
        return 1 if table[row][col] else 0
    return 0

def get_neighboring_cells(table, row, col):
    sum = 0
    for row_shift in (-1, 0, 1):
        for col_shift in (-1, 0, 1):
            if row_shift or col_shift:
                #  checking for funcion to not check the state of the cell itself
                sum += get_cell_state(table, row + row_shift, col + col_shift)
    return sum

def generate_table(height, width, rand_seed=time.time()):
    random.seed(rand_seed)  #  giving a seed if user specifies
    table = [[None] * width for _ in range(height)]  # generating the table frame
    for row in range(height):
        for col in range(width):
            table[row][col] = random.choice([True, False])
    return table

def update_table(table, height, width):
    new_table = copy.deepcopy(table)  # deep copy to avoid mutability issues
    for row in range(height):
        for col in range(width):
            neighboring_cells = get_neighboring_cells(table, row, col)
            if neighboring_cells < 2 and table[row][col]:
                # underpopulation
                new_table[row][col] = False
            elif neighboring_cells > 3 and table[row][col]:
                # overpopulation
                new_table[row][col] = False
            elif neighboring_cells == 3 and not table[row][col]:
                # replication
                new_table[row][col] = True
    return new_table

def print_table(table):
    os.system('cls')  # clearing up the screen to print new table
    for row in table:
        for elem in row:
                print(black_square_symbol if elem else ' ', end='')
        print()  # newline

def main():
    os.system('color f0')  # making the background white and text black
    height = int(input('Enter table width: '))
    width = int(input('Enter table height: '))
    rand_seed = input('Enter seed to generate the table(leave blank if dont want to specify')
    if rand_seed:
        table = generate_table(width, height, float(rand_seed))
    else:
        table = generate_table(width, height)
    year = 0
    while True:
        print_table(table)
        print('year:', year)
        year += 1
        table = update_table(table, height, width)
        time.sleep(1)

if __name__ == '__main__':
    main()


texts: Wrapping JS file in function, and assigning that function to a variable

texts:
I've been reading about wrapping JS files in functions to call them on load, etc. I also was looking at namespaces and how to avoid collisions with functions/objects/variables of the same name. What improvements can be made? I know the code works, I'm more concerned with best practices for wrapping JS files and using namespaces.

// codereview.js:
var codeReview = (function() {
    // basically "private" function? Will there be a collision with jQuery here?
    function $(id) {
        return document.getElementById(id);
    }

    // "public" functions
    return {
        fizzbuzz: function() {
            var newhtml = '';
            var output = 0;
            for (var i = 1; i <= 100; i++) {
                if (i % 3 == 0 && i % 5 == 0) {
                    output = "FizzBuzz";
                } else if (i % 3 == 0) {
                    output = "Fizz";
                } else if (i % 5 == 0) {
                    output = "Buzz";
                } else {
                    output = i;
                }
                newhtml += output + "<br />";
            }
            $('fizzbuzz-output').innerHTML = newhtml;
        },
        email: function() {
            var first = "test@em";
            var last = "ail.com";
            var email = first + last;
            $('my_email').innerHTML = email;
        }
    }
})();

Both of the "public" functions would be called via codeReview.fizzbuzz() or codeReview.email()

Also, codeReview.email() is basically just to click and reveal my email address. I did it this way thinking it couldn't get scraped, is that accurate or did I just come up with a super complicated way to display an email address?


texts:
The requirement: search a string in a text-string (from DB) and show additional info around when presenting the result.

Examples:

text string = "The Quick Brown Fox Jumps Over the Lazy Dog"

search ==> result:

"The" ==> "The Quick ..."
"Quick" ==> "The Quick Brown ..."
"Brown" ==> "... Quick Brown Fox ..."
"Lazy" ==> "... the Lazy Dog"
"ump" ==> "... Fox Jumps Over ..."

Rules are:

aways complete word containing search string
the word before if exists
the word after if exists
if there is something before the "before" word, show "..." at beginning
if there is something after the "after" word, show "..." at end

How I solved it:

// initialize text
$text_found = '';
// first eliminate line feeds, tabs, double spaces etc
$tmp = str_replace(array( "\r\n", "\r", "\n", "\t", "  " ), ' ', $text );
// now get starting position
$posStart = strpos( mb_convert_case( $tmp, MB_CASE_LOWER, 'UTF-8' ), $search );
// search first space before position
$space = strrpos( substr( $tmp, 0, $posStart +1 ), ' ', -1 );
if ( $space === FALSE ):
    // no earlier space found, so start from beginning
    $posStart = 0;
else:
    $posStart = $space;
    // check if there is an additional word before
    $space = strrpos( substr( $tmp, 0, $space ), ' ', -1 );
    if ( $space === FALSE ):
        $text_found .= substr( $tmp, 0, $posStart );
    else:
        $text_found .= '... ' . substr( $tmp, $space, $posStart - $space );
    endif;
endif;
// now we search first space after position
$space = strpos( substr( $tmp, $posStart +1 ), ' ' ) + 1;
if ( $space === FALSE ):
    // no space after found, so add text till end
    $text_found .= substr( $tmp, $posStart );
else:
    // check if there is an additional word after
    $space2 = strpos( substr( $tmp, $posStart + $space + 1 ), ' ' );
    if ( $space2 === FALSE ):
        $text_found .= substr( $tmp, $posStart );
    else:
        $text_found .= substr( $tmp, $posStart, $space2 + $space + 1 ) . ' ...';
    endif;
endif;

Question is if there is a more elegance solution ?
And if so how ?

Please see post below with my final outcome.


texts:
I wrote a code by using var fs= require("fs").promises;

to read all my files in directories and sub-directories

var fs= require("fs").promises;

async function checkFileLoc(folderPath, depth) {
  depth -= 1;
  let files = await fs.readdir(folderPath);
  files = await Promise.all(
    files.map(async (file) => {
      const filePath = path.join(folderPath, file);
      const stats = await fs.stat(filePath);
      if (stats.isDirectory() && depth > 0) {
        return checkFileLoc(filePath, depth);
      } else if (stats.isFile()) return filePath;
      else return null;
    })
  );
  return files
    .reduce((all, folderContents) => all.concat(folderContents), [])
    .filter((e) => e != null);
}

Then I install fs-extra package in my project and replace var fs=require("fs");to var fse=require("fs-extra"); and made changes in my code like this

var fse=require("fs-extra");

async function checkFileLoc(folderPath, depth) {
  depth -= 1;
  let files = await fse.readdir(folderPath);
  files = await Promise.all(
    files.map(async (file) => {
      const filePath = path.join(folderPath, file);
      const stats = await fse.stat(filePath);
      if (stats.isDirectory() && depth > 0) {
        return checkFileLoc(filePath, depth);
      } else if (stats.isFile()) return filePath;
      else return null;
    })
  );
  return files
    .reduce((all, folderContents) => all.concat(folderContents), [])
    .filter((e) => e != null);
}

when I was using fs i was getting the desire output and someone told me fs-extra is advance then fs and all you have to do is replace fs with fse
my code not working properly any solution where i have made mistake ?


texts:
I am checking if there are any rows in a table with some conditions.  If there are more than 1 row, it should filter again with some more conditions.

DECLARE @Matches AS int
DECLARE @Result AS int

SET @Matches =
(
    Select COUNT(*)
        FROM Houses
            INNER JOIN Houses_Access ON Houses.ID = Houses_Access.DoorKey
            FULL OUTER Join UserAccess ON  Houses_Access.GroupKey  = UserAccess.GroupKey
        WHERE Houses.ID IS NOT NULL AND Houses.ID = @DoorKey AND UserAccess.UserKey = @UserKey
)

IF(@Matches < 1)
 SET @Result = 0
ELSE IF(@Matches > 0)
    BEGIN
        SET @Matches =
        (
        Select COUNT(*)
        FROM Houses
            INNER JOIN Houses_Access ON Houses.ID = Houses_Access.DoorKey
            FULL OUTER Join UserAccess ON  Houses_Access.GroupKey  = UserAccess.GroupKey
        WHERE Houses.ID IS NOT NULL AND Houses.ID = @DoorKey AND UserAccess.UserKey = @UserKey
            AND Houses_Access.GroupKey != Houses.OwnerGroupKey
        )
        IF(@Matches < 1)
            SET @Result = 0
        ELSE
            SET @Result = 1
    END

RETURN(@Result)

END


texts:
I have two tables, devices and device_properties.

The devices table columns are as follows:

id
ip
mac
device_property_id
created_at
updated_at

The device_properties table columns are as follows:

id
property_name
cli_enabled
created_at
updated_at

At the moment I have the following code to retrieve an array of devices as well as their properties:

@Data = Array.new
@Devices = Device.joins(:device_property)
@Devices.each do |d|
  p = d.device_property

  @Data.push({
    :device => {
      :id => d.id,
      :ip => d.ip,
      :mac => d.mac,
      :created_at => d.created_at,
      :updated_at => d.updated_at,
      :device_property_id => p.id,
      :property_name => p.property_name,
      :cli_enabled => p.cli_enabled
    }
  })
end

I return this as JSON:

render json: {
  "status" => "success",
  "data" => @Data
}

Is there a better way to render this all as JSON?

I was previously using this method:

@Data = Device.select("devices.*, device_properties.*").joins(:device_property)

But I soon discovered that the id that was being returned in the JSON was that of the device_property and not the device:

{
  "device" : {
    "id" : 13,
    "ip" : "192.168.1.2",
    "mac": "12:34:56:78:90:AB",
    "created_at" : "Oct 24th 2016",
    "updated_at" : "Oct 24th 2016",
    "device_property_id" : 13,
    "property_name" : "Disabled CLI",
    "cli_enabled" : false
  }
}

As you can see in the example, the device.id is matching device_property.id.


texts: Python Plot showing live SQL data on Tkinter Canvas

texts:
I have written a piece of code where I have a simple GUI with an Tkinter Canvas. On this canvas I draw an Matplot. The Matplot is updated every second with fake sensor data i simulate in an SQLite DB (just for testing at the moment).

I reached most of my Goals for this test case. But a few Things i would like to have reviewed:

The Performance seems to be not really good. The window is lagging if you drag it over the Desktop. I think this is caused by the redrawing of the Canvas. I tried to use threads but failed to not Interrupt my mainloop while updating the canvas.
My Code seems pretty messy (I am a beginner and just learning how to Code in a good style). Every criticism and ideas to make it better are welcome.

So here is my Code so far:

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import tkinter as tk
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import sqlite3
from datetime import datetime
from random import randint

class MainApplication(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent

        root.update_idletasks()

        f = Figure(figsize=(15,5), dpi=55)
        x=1
        ax = f.add_subplot(111)
        line, = ax.plot(x, np.sin(x))

        def animate(i):
            # Open Database
            conn = sqlite3.connect('Sensor_Data.db')
            c = conn.cursor()
            # Create some fake Sensor Data
            NowIs = datetime.now()
            Temperature = randint(0, 100)
            Humidity = randint(0, 100)
            # Add Data to the Database
            c = conn.cursor()
            # Insert a row of data
            c.execute("insert into Sensor_Stream_1 (Date, Temperature, Humidity) values (?, ?, ?)",
                        (NowIs, Temperature, Humidity))
            # Save (commit) the changes
            conn.commit()
            # Select Data from the Database
            c.execute("SELECT Temperature FROM Sensor_Stream_1 LIMIT 30 OFFSET (SELECT COUNT(*) FROM Sensor_Stream_1)-30")
            # Gives a list of all temperature values
            x = 1
            Temperatures = []

            for record in c.fetchall():
                Temperatures.append(str(x)+','+str(record[0]))
                x+=1
            # Setting up the Plot with X and Y Values
            xList = []
            yList = []

            for eachLine in Temperatures:
                if len(eachLine) > 1:
                    x, y = eachLine.split(',')
                    xList.append(int(x))
                    yList.append(int(y))

            ax.clear()

            ax.plot(xList, yList)

            ax.set_ylim(0,100)
            ax.set_xlim(1,30)

            ax.tick_params(axis='both', which='major', labelsize=12)

            ax.grid(b=None, which='major', axis='both', **kwargs)

        label = tk.Label(root,text="Temperature / Humidity").pack(side="top", fill="both", expand=True)

        canvas = FigureCanvasTkAgg(f, master=root)
        canvas.get_tk_widget().pack(side="left", fill="both", expand=True)

        root.ani = animation.FuncAnimation(f, animate, interval=1000)

if __name__ == "__main__":
    root = tk.Tk()
    MainApplication(root).pack(side="top", fill="both", expand=True)
    root.mainloop()

Here is my DB Schema:

CREATE TABLE `Sensor_Stream_1` (
    `Date`  TEXT,
    `Temperature`   INTEGER,
    `Humidity`  INTEGER
);


texts:
Please ignore the OOP aspects of the code for a moment and concentrate on the efficiency of code. I would be obliged if someone could suggest a way to shorten my AVL tree implementation, such as using fewer variables, data structures and functions and at the same time improving upon the elegance of the code.

import java.util.*;
class Avl
{
 private static Queue<Node> st=new LinkedList<Node>();
 private static Node root=null;
 public static Node insert(Node x,int k)
 {
     if(x==null)
     {
         x=new Node(k,0);
         return x;
     }
     else if(k>x.key)
     {
         x.right=insert(x.right,k);
         st.add(x);
         return x;
     }
     else
     {
         x.left=insert(x.left,k);
         st.add(x);
         return x;
     }
 }
 public static void adjust_height()
 {
     while(!st.isEmpty())
     {
         Node x=st.poll();
         int lh=get_height(x.left);
         int rh=get_height(x.right);
         if(Math.abs(lh-rh)<=1)
          x.height=Math.max(lh,rh)+1;
         else
         {
             if(rh>lh)
             {
                 int l=get_height(x.right.left);
                 int r=get_height(x.right.right);
                 if(r>=l)//x is right heavy and right child is right heavy or balanced
                  rotate_left(x);
                 else//x is right heavy and its right child is left heavy
                 {
                     rotate_right(x.right);
                     rotate_left(x);
                 }
             }
             else
             {
                 int l=get_height(x.left.left);
                 int r=get_height(x.left.right);
                 if(l>=r)//x is left heavy and its left child is left heavy or balanced
                  rotate_right(x);
                 else//x is left heavy and its left child is right heavy
                 {
                     rotate_left(x.left);
                     rotate_right(x);
                 }
             }
         }
     }
 }
 public static void rotate_left(Node x)
 {
     Node parent=null;
     Node n_p=x.right;
     Node n_r_c=x.right.left;
     x.right=n_r_c;
     n_p.left=x;
     x.height=Math.max(get_height(x.right),get_height(x.left))+1;
     n_p.height=Math.max(get_height(n_p.right),get_height(n_p.left))+1;
     if(st.isEmpty())
      root=n_p;
     else
     {
         parent=st.peek();
         if(parent.right==x)
          parent.right=n_p;
         else parent.left=n_p;
     }
 }
 public static void rotate_right(Node x)
 {
     Node parent=null;
     Node n_p=x.left;
     Node n_l_c=x.left.right;
     n_p.right=x;
     x.left=n_l_c;
     x.height=Math.max(get_height(x.right),get_height(x.left))+1;
     n_p.height=Math.max(get_height(n_p.right),get_height(n_p.left))+1;
     if(st.isEmpty())
      root=n_p;
     else
     {
         parent=st.peek();
         if(parent.right==x)
          parent.right=n_p;
         else parent.left=n_p;
     }
 }
 public static int get_height(Node x)
 {
     if(x==null)
      return -1;
     else return x.height;
 }
 public static void iot(Node x)
 {
     if(x==null)
      return;
     iot(x.left);
     System.out.println(x.key+" "+x.height);
     iot(x.right);
 }
 public static void main(String []args)
 {
     root=insert(root,2);
     adjust_height();st.clear();
     root=insert(root,4);
     adjust_height();st.clear();
     root=insert(root,6);
     adjust_height();st.clear();
     root=insert(root,8);
     adjust_height();st.clear();
     root=insert(root,10);
     adjust_height();st.clear();
     iot(root);
 }
}
class Node
{
 int key;
 int height;
 Node left;
 Node right;
 Node(int key,int height)
 {
     this.key=key;
     this.height=height;
     this.left=null;
     this.right=null;
 }
}


texts:
This is an implementation of the Sieve of Eratosthenes :

It takes advantages of the fact that all primes from 5 and above can be written as 6X-1 or 6X+1,
For better space complexity, it uses a pretty accurate upperbound. Better estimations of the upper bound can be found here. I've observed a very slight increase in performance with this.

func eratosthenesSieve(to n: Int) -> [Int] {

    guard 2 <= n else { return [] }

    var composites = Array(repeating: false, count: n + 1)
    var primes: [Int] = []
    let d = Double(n)
    let upperBound = Int((d / log(d)) * (1.0 + 1.2762/log(d)))
    primes.reserveCapacity(upperBound)
    let squareRootN = Int(d.squareRoot())

    //2 and 3
    var p = 2
    let twoOrThree = min(n, 3)
    while p <= twoOrThree {
        primes.append(p)
        var q = p * p
        let step = p * (p - 1)
        while q <= n {
            composites[q] = true
            q += step
        }
        p += 1
    }

    //5 and above
    p += 1
    while p <= squareRootN {
        for i in 0..<2 {
            let nbr = p + 2 * i
            if !composites[nbr] {
                primes.append(nbr)
                var q = nbr * nbr
                var coef = 2 * (i + 1)
                while q <= n  {
                    composites[q] = true
                    q += coef * nbr
                    coef = 6 - coef
                }
            }
        }
        p += 6
    }

    while p <= n {
        for i in 0..<2 {
            let nbr = p + 2 * i
            if nbr <= n && !composites[nbr] {
                primes.append(nbr)
            }
        }
        p += 6
    }

    return primes
}

It was inspired by this code by Mr Martin.

Using the same benchmarking code in that answer, adding a fourth fractional digit in the timing results, plus some formatting, here are the results :

        ---------------------------------------------------------------
        |                |       Nbr   |          Time (sec)          |
        |      Up to     |        of   |------------------------------|
        |                |    Primes   |   Martin's   |      This     |
        |----------------|-------------|------------------------------|
        |        100_000 |        9592 |       0.0008 |        0.0004 |
        |----------------|-------------|--------------|---------------|
        |      1_000_000 |      78_498 |       0.0056 |        0.0026 |
        |----------------|-------------|--------------|---------------|
        |     10_000_000 |     664_579 |       0.1233 |        0.0426 |
        |----------------|-------------|--------------|---------------|
        |    100_000_000 |   5_761_455 |       1.0976 |        0.5089 |
        |----------------|-------------|--------------|---------------|
        |  1_000_000_000 |  50_847_534 |      12.1328 |        5.9759 |
        |----------------|-------------|--------------|---------------|
        | 10_000_000_000 | 455_052_511 |     165.5658 |       84.5477 |
        |----------------|-------------|--------------|---------------|

Using Attabench, here is a visual representation of the performance of both codes while n is less than 2^16:

One thing I observe is some elements in the composites array are marked with true multiple times. This is expected (but unwanted) behavior since 6X-1 or 6X+1 aren't all primes.

What I'm looking for is making this Sieve of Eratosthenes quicker. I'm well aware of faster methods of finding primes.

Naming, code clarity, conciseness, consistency, etc, are welcome but are not the main point here.


texts:
This is an OOP Rock, Paper, Scissors program I wrote in Python.  I am just learning about using classes and I'd appreciate some advice on improving it.

# rock.py
# example of OOP
import random

class Rock:
    def main(self):
        self.Make_players()
        print("Best of Five - Let's play!\n")
        done  = False
        while done == False:
            self.p1.go_player()
            self.p2.go_player()
            print()
            if self.p2.go == self.p1.go:
                print("No winner!\n")
                continue
            else:
                temp = self.check(self.p1, self.p2)
            if temp == False:
                temp = self.check(self.p2, self.p1)
            print(self.message, end = " ")
            print(temp.name + " won this round.")
            temp.scored()
            print(self.p1.name + ": " + str(self.p1.score))
            print(self.p2.name + ": " + str(self.p2.score))
            if self.p1.score == 3:
                self.winner = self.p1
                done = True
            elif self.p2.score == 3:
                self.winner = self.p2
                done = True
            else:
                done = False
                input()
        print("The winner was " + self.winner.name + "!")

def __init__(self):
    print("**** Welcome to Rock, Paper, Scissors!****\n")
    self.winner = False
    self.main()

def Make_players(self):
    temp = (input("What shall we call Player 1? "))
    self.p1 = Player(temp)
    temp = (input("What shall we call Player 2? "))
    self.p2 = Player(temp)

def check(self, p_a, p_b):
    if p_a.go == "rock" and p_b.go == "scissors":
        self.message = "Rock breaks scissors."
        return p_a
    elif p_a.go == "paper" and p_b.go == "rock":
        self.message = "Paper wraps stone."
        return p_a
    elif p_a.go == "scissors" and p_b.go == "paper":
        self.message = "Scissors cut paper."
        return p_a
    else:
        return False

class Player:
    def __init__(self, name):
        self.choices = ["rock", "paper", "scissors"]
        self.score = 0
        self.name = name
        print("Player:", self.name, "created!\n")

    def go_player(self):
        self.go = random.choice(self.choices)
        print(self.name + " chose " + self.go, end = ". ")
        return self.go

    def scored(self):
        self.score += 1

# Main
game = Rock()


texts: White Elephant Gift Exchange game using object-oriented design

texts:
I'm learning about OOP and I'm attempting to solve the White Elephant Gift Exchange game. The way I've done it is that I have created a bunch of set variables which store the set of gifts opened, set of gifts not opened yet, set of users. I was wondering if this is a good way of designing the classes and delegating work.

Could someone please comment on how can the design be improved and how could I make it more object-oriented?

Game rules

  In its most basic form, the game is as follows: each participant supplies one wrapped gift. The gifts are placed in a central location, and participants determine in what order they will take turns selecting them. The first person opens a wrapped gift and the turn ends. On subsequent turns, each person can open a new present or gets the choice to "steal" another person's unwrapped gift. When a person's gift is stolen, that person can either choose another wrapped gift to open or can steal from another player. The game is over when the last person goes and the first person goes again (but see variations below).

User.java

class User {
    private Gift giftBrought;
    private Gift giftWon;

    public Gift pickOrSteal(Set<Gift> unopnenedGifts, Set<Gift> openedGifts) {}
}

Gift.java

class Gift {
    private boolean isOpened;
    private User owner;
}

WhiteElephant.java

class WhiteElephant {
    private List<User> users;
    private Set<User> turnCompletedUsers;
    private Set<Gift> unopnenedGifts;
    private Map<Gift, User> openedGifts;
    private Set<Gift> unavailableForRoundGifts;

    public void assignRanks() {
        // use knuth shuffle to randomize user array
    }

    public List<Gift> getUnopenedGifts() {}
    public List<Gift> getOpenedGifts() {}

    public boolean isGiftAvailableForStealing(Gift gift) {
        return !unavailableForRoundGifts.contains(gift);
    }

    public Gift pickGift() {}

    public boolean stealGift(User from, User to, Gift gift) {}

    public void play() {
         assignRanks();

         for(int i = 0; i < users.size(); i++) {
             nextUser = users.get(i);
             unavailableForRoundGifts.clear();

             while(nextUser != null) {
                 User currUser = nextUser;
                 Gift gift = currUser.pickOrSteal(unopenedGifts, openedGifts.keyset());
                 if(isGiftAvailableForStealing(gift)) {
                     if(!gift.isOpened()) {
                         unopenedGifts.remove(gift);
                         openedGifts.add(gift);
                         gift.setIsOpened(true);
                         nextUser = null;
                     } else {
                         nextUser = openedGifts.get(gift);
                         stealGift(nextUser, users.get(i), gift);
                     }
                     unavailableForRoundGifts.add(gift);
                     users.get(i).setGiftWon(gift);
                 }
             }
         }
    }
}


texts: Validation methods to ensure start and end date come after existing instances

texts:
We have a "Committee" class and I need to ensure that any new committee has start and end dates (don't know why they're CharFields instead of Dates or even Ints, but that's not my call) that are after any existing instances.  This works, but I would appreciate any general review as well as specifically on my solution to my test problem:  When no Committees exist, my tests broke as noted in the comment.  My solution worked but feels hacky.

class Committee(models.Model):
    start_year = models.CharField(max_length=4)
    end_year = models.CharField(max_length=4)

    def save(self, *args, **kwargs):
        # new committees need to ensure start and end dates are in the future

        if not self.pk:
            committee_set = Committee.objects.all()
            latest_start_year = committee_set.aggregate(Max('start_year'))["start_year__max"]
            latest_end_year = committee_set.aggregate(Max('end_year'))["end_year__max"]

            # in the test database when there are no other committees yet, these evaluate to
            # None and break the tests. Doesn't really affect production code otherwise.
            try:
                latest_start_year = int(latest_start_year)
            except TypeError:
                latest_start_year = 0
            try:
                latest_end_year = int(latest_end_year)
            except TypeError:
                latest_end_year = 1

            if not (self.start_year > latest_start_year):
                raise ValidationError("Start year (%s) must be greater than most recent "
                                      "Committee start year (%s)" % (
                                          self.start_year, latest_start_year))

            if not (self.end_year > latest_end_year):
                raise ValidationError("End year (%s) must be greater than most recent "
                                      "Committee end year (%s)" % (self.end_year, latest_end_year))

        super(Committee, self).save(*args, **kwargs)


texts: Efficient way to copy unordered String into ordered String

texts:
I'm doing this in Hadoop Java where I'm reading a String. The string is huge that has been tokenized and put in an array. It has key-value pairs but they are not in any order. I want this order to be rigid so I can load that as a table. So in SQL, if I select a column (after loading this in a table), all the keys of one type should be in colA.

I'm checking each word of the String array and copying them in a new string in a fixed position. The way I thought of doing this is using if else ladder like this:

//row is the tokenized unordered String

String[] newRow = new String[150];

for (int i = 0; i < row.length; ++i) {

    if(row[i].equals("token1")){
        newRow[0] = row[i]; //key
        newRow[1] = row[i+1];//value
    }

    else if(row[i].equals("token2")){
        newRow[2] = row[i];
        newRow[3] = row[i+1];
    }//...and so on. Elseif ladder at least is at least 100 long.

I wanted to know if there is a more efficient way to do this?

PS: I'm not sorting the string. Example: row1 String is {apple,good,banana,bad}, row2 String is {banana,good,apple,bad} where apple and banana are keys. Now in my output I will have two records with say apple as the first key and then banana. So output will be : newRow1: {apple,good,banana,bad}, newRow2: {apple,bad,banana,good}. Essentially I'm rearranging all input to a fixed output.


texts:
I am writing an app that is supposed to simulate load on our web api and NServiceBus(particular) service.  This is the first time I am writing an application like this.  It appears to run ok, however it takes a few seconds before anything shows up in the console window.  Is there a better way to do this?
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Press any key to start...");
        Console.ReadKey();
        Console.WriteLine();

        RunLoad(100);

        Console.WriteLine("Press any key to exit...");
        Console.ReadKey();
    }

    private static void RunLoad(int loadSize)
    {
        Parallel.For(0, loadSize, i =>
        {
            RunTasks();
        });
    }

    public static async Task RunTasks()
    {
        var tasks = new List<Task>
        {
            GetLoanByLoanId("7000002050"),
            GetEnvelopesForLoan("7000002077"),
            GetLoanDataByLoanId("7000002072")

        };

        await Task.WhenAll(tasks);
    }

    private static async Task GetLoanByLoanId(string loanNumber)
    {
        await Task.Run(() => {
            var svc = new WebApiService();
            var msg1 = svc.GetLoanByLoanId(loanNumber);
            if (string.IsNullOrWhiteSpace(msg1)) return;
            Console.WriteLine($"GL {loanNumber}: {msg1.Length}");
        });
    }

    private static async Task GetLoanDataByLoanId(string loanNumber)
    {
        await Task.Run(() => {
            var svc = new WebApiService();
            var msg1 = svc.GetLoanDataByLoanId(loanNumber);
            if (string.IsNullOrWhiteSpace(msg1)) return;
            Console.WriteLine($"GLD {loanNumber}: {msg1.Length}");
        });
    }

    private static async Task GetEnvelopesForLoan(string loanNumber)
    {
        await Task.Run(() => {
            var svc = new WebApiService();
            var msg1 = svc.GetEnvelopesForLoan(loanNumber);
            if (string.IsNullOrWhiteSpace(msg1)) return;
            Console.WriteLine($"GEFL {loanNumber}: {msg1.Length}");
        });
    }

}
}

I am trying to mimic the number to users hitting the system through the RunLoad method.
Also this is running in .Net Framework 4.8..


texts:
I have an Entry that I want to give a red outline when the entry is empty. I'm using SyncFusion's SFTextInputLayout for my Entry and it has a property HasError that once it's set to true, it'll automatically highlight my entire in Red.

Here is the following XAML code for SFTextInputLayout

 <inputLayout:SfTextInputLayout Grid.Column="0" Hint="Phone Number" ContainerType="{StaticResource RepairOrderContainerType}" HasError="{Binding IsPhoneNumberError}" FocusedColor="{StaticResource AccentColor}" VerticalOptions="Center" HorizontalOptions="Start">
       <Entry Keyboard="Telephone" Style="{StaticResource TextInputStyle}" Text="{Binding PhoneNumber}"/>
 </inputLayout:SfTextInputLayout>

As you can see, I have two bindings that handles the text of the entry and another one to check if it has an error or not.

While this solution works, it will get redundant pretty soon as the number of my entry fields grow. For every entry field I have, I need another boolean to cover its Error property as shown below.

  private string _phoneNumber;
    public string PhoneNumber
    {
        get => _phoneNumber;
        set
        {
            IsPhoneNumberError = string.IsNullOrWhiteSpace(value) ? true : false;
            this.RaiseAndSetIfChanged(ref _phoneNumber, _phoneNumber);
        }
    }

    private bool _isPhoneNumberError = false;
    public bool IsPhoneNumberError
    {
        get => _isPhoneNumberError;
        set
        {
            this.RaiseAndSetIfChanged(ref _isPhoneNumberError, value);
        }
    }

I'm wondering how to simplify this code?


texts: Two similar methods to re-create objects and emit events

texts:
I have these two methods basically doing the same thing:

private void recreateBlocksIfNeeded() throws WplexError
{
    if (this.blocksToRemove == null || this.blocksToRemove.isEmpty())
        return;

    final List<BlockVO> blocksToCreate = new ArrayList<BlockVO>();
    for (final BlockDeletionStruct struct : this.blocksToRemove)
        blocksToCreate.add(struct.getBlock());
    this.cacheFacade.addBlocks(this.schedule, blocksToCreate, this.blockRules);

    for (final BlockVO block : blocksToCreate)
    {
        for (final ScheduleTripVO scheduleTrip : block.getScheduleTrips())
            AsynchronousEventRunner.publish(scheduleTrip, IEventOperationKind.OBJECT_CREATED);
        AsynchronousEventRunner.publish(block, IEventOperationKind.OBJECT_CREATED);

        this.removeInterfaceEvent(this.interfaceEvents, block, IEventOperationKind.OBJECT_DELETED);
        this.removeInterfaceEvent(this.interfaceEventsPostActions, block, IEventOperationKind.OBJECT_DELETED);
    }
}

private void recreateCrewMembersIfNeeded() throws WplexError
{
    if (this.crewMembersToRemove == null || this.crewMembersToRemove.isEmpty())
        return;

    this.cacheFacade.addCrewMembers(this.schedule, new ArrayList<>(this.crewMembersToRemove), this.crewMemberRules);

    for (final CrewMemberVO crewMember : this.crewMembersToRemove)
    {
        for (final ScheduleTripVO scheduleTrip : crewMember.getScheduleTrips())
            AsynchronousEventRunner.publish(scheduleTrip, IEventOperationKind.OBJECT_CREATED);
        AsynchronousEventRunner.publish(crewMember, IEventOperationKind.OBJECT_CREATED);

        this.removeInterfaceEvent(this.interfaceEvents, crewMember, IEventOperationKind.OBJECT_DELETED);
        this.removeInterfaceEvent(this.interfaceEventsPostActions, crewMember, IEventOperationKind.OBJECT_DELETED);
    }
}

And I was wondering if there was a way to create a new more abstract or generic method so I don't have two methods doing basically the same thing, the problem is both BlockVO and CrewMemberVO are child of ValueObject.java with a few common attributes between them, but the method getScheduleTrips() is not in ValueObject, it's in another interfaces called IBlock and ICrewMember.

If not the whole method just the 'for' block at the end.


texts: Determining the HTTP status code to return for a request

texts:
I have a piece of code that I would like to refactor but can't figure how to do it. This is a method from a Play controller where I have to verify many precondition and respond different status for each case :

resource should exist or 404
revision should match the If-Match value or 412
payload should be valid or 422

The first attempt looks like below. How do you implement such a suite of verifications?

service.find(key).map { resource =>
  request.headers.get("If-Match").map { expected =>
    if ( resource.revision == expected ) {
      val payload = request.body.validate[Model]
      payload match {
        case JsSuccess(model, _) =>
          Ok()
        case JsError(errors) =>
          UnprocessableEntity()
      }
    } else {
      PreconditionFailed()
    }
  }
} getOrElse {
  NotFound()
}

My best attempt is now to have one method who create a triplet of Option[Resource], String, JsResult[Model] and use pattern matching for each case :

 condition match {
   case (None, _, _) =>
     NotFound()
   case (Some(resource), expected, _) if ( resource.revision!=expected ) =>
     PreconditionFailed()
   case (_, _, JsError(errors)) =>
     UnprocessableEntity()
   case (Some(resource), _, JsSuccess(model, _)) =>
     Ok()
 }


texts: Compile .tex and remove .div file from Haskell

texts:
As a practice for interacting with system commands, I wrote a Haskell program that compiles a .tex file specified by the argument.

When one execute this program:

If no argument is specified, print "Specify a file!" then exit
If the specified file does not exist, print "File not found." then exit
If the specified file does not have the extension .tex, then print "Not a .tex file!" then exit
If a .tex file is specified (report.tex), execute the following commands

$ uplatex report.tex -o report.dvi
$ dvipdfmx report.dvi
$ rm report.dvi

The program does not have to handle errors in the execution of tex-related commands.

This is what I wrote:

import qualified Control.Shell as S
import Data.Char (toLower)
import Data.List (dropWhileEnd)
import qualified System.Environment as E

main = do
    args <- E.getArgs
    case args of
        [] -> putStrLn "Specify a file!"
        (filename:_)  -> handler filename

isTeXFile :: S.FilePath -> Bool
isTeXFile = ( == ".tex") . map toLower . S.takeExtension

handler :: S.FilePath -> IO ()
handler f = do
    (Right isfile) <- S.shell $ S.isFile f
    if not isfile
        then putStrLn "File not found."
        else
            if isTeXFile f
                then do
                    result <- compile f
                    case result of
                        (Left l) -> putStrLn l
                        (Right _) -> return ()
                else
                    putStrLn "Not a .tex file!"

compile :: S.FilePath -> IO (Either String ())
compile f = S.shell $ do
    S.run_ "uplatex" [f, "-o", dvifile] ""
    S.run_ "dvipdfmx" [dvifile] ""
    S.rm dvifile
    where
        basename = dropWhileEnd (/= '.') f
        dvifile = basename ++ "dvi"

I appreciate any kind of feedback. I'm especially concerned about:

The body of handler seems unnecessarily complicated. How can I clean it up?
Am I using Control.Shell in the right way? Is there other function I should use instead? Maybe I should consider using System.Process?

Side note: I don't use pdflatex because it is not as good as uplatex at Japanese typesetting and I haven't been catching up with the latest LaTeX families.


texts: Passing a selectlist to the view based on database models from viewmodel is a MVC anti-pattern

texts:
We need to select a model reference from a view. To do this we need to add a dropdownlistfor. This is based on a selectlist containing database models.

passing selectlists by viewbag

This solves our problem, but we do not like using ViewBag:

public ActionResult Create()
    {
        ViewBag.SomeModels = new SelectList(context.SomeModel, "id", "modelDescription");

        return View();
    }

Our solution

We added a context to a viewmodel to do the same, but this is MVC anti-pattern:

public class SomeModelViewModel : SomeModel
{
    private SomeContext context = new SomeContext ();

    public SelectList getSomeOtherModels()
    {
        return new SelectList(context.SomeOtherModel, "id", "modelDescription");
    }
}

Does anyone have suggestions for a clean way to solve this problem?


texts: Performing unit conversions using intermediate conversions

texts:
Challenge:

  Two input files are given The first contains |N| ratios in the form of
  two labels and a ratio:

  USD, GBP, 0.69
  Meter, Yard, 1.09
  YEN, EUR, 0.0077
  GBP, YEN, 167.75
  Horsepower, Watt, 745.7

  Each line A, B, C means that C is a conversion factor from A to B.  In other words, multiply by C to go from A to B, and 1 A is worth C B's.

  Example: USD, GBP, 0.69 means that 1 USD = 0.69 GBP, so multiplying by
  0.69 converts an amount from USD to GBP.

  And the expected output is a file with the query and the ratio value filled in.

  USD, EUR, 0.89
  Yard, Meter, 0.91

  Write a program that reads both input files and produces the expected output

  USD-EUR can be found by multiplying  0.69*167*0.0077=0.89 (Approx)

My Approach:
These inputs can be represented as a weighted bi-directional graph

     0.69      167        0.0077
USD------->GBP------->YEN------->EUR
   <------    <-------   <-------
     1.449      0.0059    129.87

If we represent this format the problem reduces to finding shortest path between two nodes which can be done using Dijkstra. Instead of writing graph code from scratch i found a library from https://pypi.org/project/Dijkstar/ which reduced my code to

>>> from dijkstar import Graph,find_path
>>> graph=Graph()
>>> graph.add_edge('USD','GBP',{'cost':0.69})
>>> graph.add_edge('GBP','YEN',{'cost':167})
>>> graph.add_edge('YEN','EUR',{'cost':0.0077})
>>> graph.add_edge('EUR','YEN',{'cost':129.87})
>>> graph.add_edge('YEN','GBP',{'cost':0.0059})
>>> graph.add_edge('GBP','USD',{'cost':1.449})
>>> cost_func=lambda u,v,e,prev_e:e['cost']
>>> t=find_path(graph,'USD','EUR',cost_func=cost_func)
>>> print(t.costs)
[0.69,167,0.0077]
>>> from functools import reduce
>>> ans=reduce(lambda x, y: x*y, t.costs)
>>> print(round(ans,2))
0.89

I think the time complexity should be O(|V|+|E|).

Can I optimize this further? Is there a better or another way to solve this problem ?


texts:
For some reason, I completely lost my reasoning with this code. I can't hold this simple logic for too long in my head. Actually, I want to improve this code, making it a little concise. The if (so.scan_complete and force) or not so.scan_complete: is just not a KISS thing. Please any suggesion to optimize and concise it?

source = request.POST.get('source')
queries = request.POST.get('term')

force = request.POST.get('rescan', False) # if checkbox is checked this becomes True

queries = [query.strip().lower() for query in queries.split(',')]
engines = ['alibaba', 'madeinchina', 'google', 'bing']
for src in engines:
    for query in queries:
        so = Source.objects.get(term__term=query, engine=src)
        if src == 'alibaba':
            if (so.scan_complete and force) or not so.scan_complete:
                ali_results.delay(src, query)
        elif src == 'madeinchina':
            if (so.scan_complete and force) or not so.scan_complete:
                made_results.delay(src, query)
        elif src == "google" or src == "bing":
            if (so.scan_complete and force) or not so.scan_complete:
                search_engine_scraper.delay(query=query, source=src)


texts: Remove first occurrence of element from list

texts:
As explained here, I'm learning a limited subset of Scheme.

My task has been to sort a numerical list. To implement a simple insertion sort, I need to remove a single element from a list.

I've done the following (in the spirit of The Little Schemer):

(define remove-elem
  (lambda (ll elem)
    (cond
      ((null? ll) '())
      ((eq? (car ll) elem) (cdr ll))
       (else
        (cons (car ll) (remove-elem (cdr ll) elem))))
    ))

What are other ways of implementing this function?


texts: Inserting an object pointer to a two-level map

texts:
I'm working on requirement that involves creating a N x N table of blocks, with each block having a weight.

Consider the following classes

Block - The fundamental unit of the table, having a weight
RowBlock - A set of blocks that join together one row
BlockTable - A set of rows that join together to form the table

The program simulates inserting one block at a time to form the table altogether.

I know it is not a C++ thing to play around with pointers but rather use const references, but again this is part of a bigger problem that we are trying to solve.

This below code seems to work for now. But I'm a novice C++ programmer, want to know the potential pitfalls/vulnerabilities in the code

#include <iostream>
#include <utility>
#include <stdint.h>
#include <cstdlib>
#include <map>

class Block
{
    private:
        int m_value;
    public:
        Block(int value)
        {
            m_value = value;
        }
        void setvalue(int value);
        int getvalue();
};

class RowBlock
{
    private:
        std::map<int, Block*> m_rowBlock;

    public:
        RowBlock() = default;
        void insertBlock(Block* newBlock);
        const std::map<int, Block*> getList();
};

class BlockTable
{
    private:
        std::map<int, RowBlock*> m_BlockTable;
    public:
        BlockTable() = default;
        ~BlockTable();
        void insertBlockRow(Block* newBlock);
        const std::map<int, RowBlock*> getList();
};

int Block::getvalue(void)
{
    return(m_value);
}

void RowBlock::insertBlock(Block* newBlock)
{
    m_rowBlock.emplace(newBlock->getvalue(), newBlock);
}

void BlockTable::insertBlockRow(Block* newBlock)
{
    std::map <int, RowBlock*>::iterator iter;
    RowBlock *localRowBlock = NULL;
    iter = m_BlockTable.find( newBlock->getvalue() );

    if( iter == m_BlockTable.end() )
    {
        localRowBlock = new RowBlock;
        localRowBlock->insertBlock(newBlock);
        m_BlockTable.emplace(newBlock->getvalue(), localRowBlock);
    }
    else
    {

        localRowBlock = iter->second;
        localRowBlock->insertBlock(newBlock);
        m_BlockTable.emplace(newBlock->getvalue(), localRowBlock);
    }
}

const std::map<int, RowBlock*> BlockTable::getList()
{
    return m_BlockTable;
}

const std::map<int, Block*> RowBlock::getList()
{
    return m_rowBlock;
}

BlockTable::~BlockTable()
{
    std::map<int, Block*>::iterator iter_1;
    std::map<int, RowBlock*>::iterator iter_2;
    std::map <int, RowBlock*> tmpList_1 = getList();
    std::map <int, Block*> tmpList_2;

    for ( iter_2 = tmpList_1.begin(); iter_2 != tmpList_1.end(); iter_2++ )
    {
        tmpList_2 = iter_2->second->getList();
        for ( iter_1 = tmpList_2.begin(); iter_1 != tmpList_2.end(); iter_1++ )
            delete iter_1->second;
        delete iter_2->second;
    }
}

int main() {
    BlockTable newBlockTable;

    for (auto i=1; i<=5; i++)
    {
        Block *newBlock = new Block(i*10);
        newBlockTable.insertBlockRow( newBlock );
    }

    std::map<int, Block*>::iterator iter_1;
    std::map<int, RowBlock*>::iterator iter_2;
    std::map <int, RowBlock*> tmpList_1 = newBlockTable.getList();
    std::map <int, Block*> tmpList_2;

    for ( iter_2 = tmpList_1.begin(); iter_2 != tmpList_1.end(); iter_2++ )
    {
        tmpList_2 = iter_2->second->getList();
        for ( iter_1 = tmpList_2.begin(); iter_1 != tmpList_2.end(); iter_1++ )
            printf("%d\n", iter_1->second->getvalue());
    }

    return 0;
}


texts:
I am working on the following assignment and I am a bit lost:

  Build a regression model that will predict the rating score of each
  product based on attributes which correspond to some very common words
  used in the reviews (selection of how many words is left to you as a
  decision). So, for each product you will have a long(ish) vector of
  attributes based on how many times each word appears in reviews of
  this product. Your target variable is the rating. You will be judged
  on the process of building the model (regularization, subset
  selection, validation set, etc.) and not so much on the accuracy of
  the results.

This is what I currently have as code (I use mord's API for the regression model since Rating is categorical):

# Create word matrix
bow = df.Review2.str.split().apply(pd.Series.value_counts)
rating = df['Rating']
df_rating = pd.DataFrame([rating])
df_rating = df_rating.transpose()
bow = bow.join(df_rating)

# Remove some columns and rows
bow = bow.loc[(bow['Rating'].notna()), ~(bow.sum(0) < 80)]

# Divide into train - validation - test
bow.fillna(0, inplace=True)
rating = bow['Rating']
bow = bow.drop('Rating', 1)
x_train, x_test, y_train, y_test = train_test_split(bow, rating, test_size=0.4, random_state=0)

# Run regression
regr = m.OrdinalRidge()
regr.fit(x_train, y_train)
scores = cross_val_score(regr, bow, rating, cv=5, scoring='accuracy')
# scores -> array([0.75438596, 0.73684211, 0.66071429, 0.53571429, 0.60714286])
# avg_score -> Accuracy: 0.66 (+/- 0.16)

Could I have some constructive criticism on the above code please (regarding what I am missing from the posed task)?

This is what bow looks like:

If you need any other information just comment I will happily edit it in.

P.S: I've been working on this the past few days, this is my first regression model I code. I'll be honest, I didn't find it easy. So if someone could lend a helping hand I would be so ever grateful. Lastly, I want to be clear that I don't post to get a complete answer, code-wise (even if I would not say no to that). I am merely hoping in some guidance. My deadline approaches fast :P


texts: Is there a better way to return values with merge sort?

texts:
I have been attempting to learn Scala lately and so have produced a couple of different sorting algorithms to build up my basics.

I think I have been lucky with the methods returning exactly what I have wanted, but for MergeSort I have implemented recursive methods that could return one of two different values and could logically be placed into two different values of the same type (as I believe Scala does)

In order to get my program to sort properly I have put a temp variable in order to grab the returning value and then be able to use it.

Is there a better way to return the values that will not need me to create these temporary values or should I have been doing something else?

Example:

def merge(left: ArrayBuffer[Int], right: ArrayBuffer[Int]): ArrayBuffer[Int]

returns an ArrayBuffer, how does the scala language figure out which value to put the returned value into or does it need a variable on the returning end to capture the value that is being returned?

merge(left, right)

This does nothing while,

var tempResult = merge(left, right)

Solves the problem.

Am I doing something wrong in the first instance (due to my experience with single parameter methods returning to the proper variable), or is this the standard way to return the values, by returning to a variable?

MergeSortClass.scala

import scala.collection.mutable.ArrayBuffer
class MergeSortClass {

def mergeSort(array: ArrayBuffer[Int]): ArrayBuffer[Int] = {

if (array.size <= 1) {

  array

} else {

  var left = ArrayBuffer[Int]()
  var right = ArrayBuffer[Int]()
  var middle = array.size / 2

  for (i <- 0 until middle) {

    left += array(i)

  }

  for (i <- middle until array.size) {

    right += array(i)

  }

//-->>>TheseValues Below

  var leftTemp = mergeSort(left);
  var rightTemp = mergeSort(right);

  var result = merge(leftTemp, rightTemp)

  result

}

}

def merge(left: ArrayBuffer[Int], right: ArrayBuffer[Int]): ArrayBuffer[Int] = {

var result = ArrayBuffer[Int]()

while (left.size > 0 || right.size > 0) {

  if (left.size > 0 && right.size > 0) {

    if (left(0) <= right(0)) {

      result += left(0)
      left.remove(0)

    } else {

      result += right(0)
      right.remove(0)

    }
  } else if (left.size > 0) {

    result += left(0)
    left.remove(0)

  } else if (right.size > 0) {

    result += right(0)
    right.remove(0)

  }

}

result

 }

}

MergeSortObject.scala

import scala.collection.mutable.ArrayBuffer
object MergeSortObject {

  def main(args: Array[String]): Unit = {

val array = ArrayBuffer(10, 5, 15, 25, 7, 11, 17, 35, 34, 16)

for (i <- 0 to array.length - 1) {

  print(array(i))

  if (i == array.length - 1) {

  } else {

    print(", ")

  }

}

println()

var ms = new MergeSortClass()

//-->>>TheseValues Below

var temp = ms.mergeSort(array)
//ms.test(array) (not shown here)

for (i <- 0 to temp.length - 1) {

  print(temp(i))

  if (i == temp.length - 1) {

  } else {

    print(", ")

  }

}

}

}


texts: Determining maximum profit to be made from selling shares

texts:
Question

  Your algorithms have become so good at predicting the market that you
  now know what the share price of Wooden Orange Toothpicks Inc. (WOT)
  will be for the next \$N\$ days.

  Each day, you can either buy one share of WOT, sell any number of
  shares of WOT that you own, or not make any transaction at all. What
  is the maximum profit you can obtain with an optimum trading strategy?

  Input

  The first line contains the number of test cases \$T\$. \$T\$ test cases
  follow:

  The first line of each test case contains a number \$N\$. The next line
  contains \$N\$ integers, denoting the predicted price of WOT shares for
  the next \$N\$ days.

  Output

  Output \$T\$ lines, containing the maximum profit which can be obtained
  for the corresponding test case.

  Constraints

  \$1 <= T <= 10\$
  \$1 <= N <= 50000\$

  All share prices are between 1 and 100000

Code

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
class Solution{
public static void main(String[] args){
    Scanner stdin = new Scanner(System.in);
    int t = stdin.nextInt();
    for(int m=0;m<t;m++){
        long n = stdin.nextLong();
        long[] ar = new long[(int)n];
        for(int i=0;i<n;i++){
            ar[i] = stdin.nextLong();
        }
        long max = maximum(ar);
        System.out.println(max);
    }
}
public static long maximum(long[] ar){
    int j=0;
    long costPrice=0;
    long sellingPrice=0;
    int k=0;
    long max = maximumKey(j,ar);
    while(j<ar.length){
        if(ar[j]<max){
            costPrice += ar[j];
            j++;
        }
        else if(ar[j]==max){
            sellingPrice += ((j-k)*ar[j]);
            j++;
            k=j;
            max = maximumKey(j,ar);
        }
    }
    return sellingPrice-costPrice;
}
    public static long maximumKey(int j, long[] ar){
        long max = 0;
        for(int i=j;i<ar.length;i++){
            if(max<ar[i])
                max = ar[i];
        }
            return max;
    }
}

In the above code I didn't use DP. But this is supposed to be solved using DP with time complexity \$O(n)\$.


texts: Formatting a CAN bus message as a string

texts:
I am writing a CAN bus logger application and need to format messages as quick as possible in order to prevent buffer overflows on our device. In some cases I am quick enough, but under heavy loads it is still pretty slow. I believe the bulk of the problem happens in my formatting block (I am using TPL to separate the tasks as much as possible). Here is my consumer block which takes a PassThruMsg[] blocking collection, formats those messages, and passes them into a new blocking collection where they are later printed to a file:

private void FormatMessages(BlockingCollection<PassThruMsg[]> messages, BlockingCollection<string[]> formattedMessages)
{
    try
    {
        PassThruMsg msg;
        String[] formatted;
        foreach (var item in messages.GetConsumingEnumerable(_cancellationTokenSource.Token))
        {
            formatted = new string[item.Length];
            for (uint i = 0; i < item.Length; i++)
            {
                msg = item[i];

                if (_firstTime == 0) _firstTime = msg.timestamp;

                formatted[i] = ((msg.timestamp - _firstTime) / 1000000.0).ToString("0.000000 ");

                // Assumed CAN Protocol
                //TODO:
                string canID = BitConverter.ToString(msg.data, 0, 4).Replace("-", "").TrimStart('0');
                string data = msg.dataSize > 4 ? BitConverter.ToString(msg.data, 4, (int)msg.dataSize - 4).Replace("-", " ") : String.Empty;
                formatted[i] += canID + " " + data;
            }
            formattedMessages.Add(formatted);
        }
    }
    catch (OperationCanceledException)
    {
        //TODO:
    }
}

If it is relevant, here is the snippet where I pass into the BlockingCollection

uint numMsgs;
while (!_cancellationTokenSource.IsCancellationRequested)
{
    numMsgs = (uint)msgs.Length;
    status = j2534.PassThruReadMsgs(channelID, msgs, ref numMsgs, 1000);
    PassThruMsg[] copiedMessages = new PassThruMsg[numMsgs];
    Array.Copy(msgs, copiedMessages, numMsgs);
    if (status == Status.ERR_BUFFER_OVERFLOW)
    {
        _fileWriter.WriteLine("DEVICE INDICATED BUFFER OVERFLOW - MESSAGES LOST!");
        logMessages.Add(copiedMessages);
    }
    else if (status == Status.STATUS_NOERROR || status == Status.ERR_TIMEOUT)
    {
        logMessages.Add(copiedMessages);
    }
    else if (status == Status.ERR_BUFFER_EMPTY)
    {
        throw new Exception(String.Format("PassThruReadMsgs Failed(0x{0:X})", status));
    }
}

And the last node:

private void LogMessages(BlockingCollection<string[]> messages)
{
    try
    {
        foreach (var item in messages.GetConsumingEnumerable(_cancellationTokenSource.Token))
        {
            if (item.Length > 0)
            {
                _fileWriter.WriteLine(String.Join(Environment.NewLine, item));
                _fileWriter.Flush();
            }
        }
    }
    catch (OperationCanceledException)
    {
        //TODO:
    }
}

I have looked this over several times and have been working on it for quite a while, but cannot find any places where this is bottle necking in a way that I can fix. I am just hoping a second set of eyes to review this will see performance issues that I don't.


texts:
I'm a beginner and have written my first Python module that parses a XML file containing some events and prints them out to stdout as JSON.  I would appreciate some feedback about design, code layout, best practices, what can be optimized and so on.

from __future__ import with_statement

import json
import xml.etree.ElementTree as etree

def _sanitize_string(string):
    """docstring for _clean_string"""
    string = string.replace('\n', '').split()
    return ' '.join(string)

class Event(object):
    """docstring for Event"""
    def __init__(self, date, event, organizer, time, venue):
        self.date = date
        self.event = event
        self.organizer = organizer
        self.time = time
        self.venue = venue

    def provide_data(self):
        """docstring for provide_data"""
        data = {}
        data['date'] = self.date
        data['event'] = self.event
        data['organizer'] = self.organizer
        data['time'] = self.time
        data['venue'] = self.venue
        return data

class EventCollection(list):
    """docstring for EventCollection"""
    def __init__(self):
        super(EventCollection, self).__init__([])

    def as_json(self):
        """docstring for as_json"""
        print json.dumps([event.provide_data() for event in self],
                         sort_keys=True, indent=2)

def read_file(event_collection, filename='events.xml'):
    """docstring for read_file"""
    try:
        with open(filename) as f:
            tree = etree.parse(f)
        for row in tree.findall('.//row'):
            # event = [_sanitize_string(node.text) for node in row]
            # if event[0] and event[0][0].isdigit():
            #     print event
            tmpl = [_sanitize_string(node.text) for node in row]
            if tmpl[0] and tmpl[0][0].isdigit():
                event = Event(tmpl.pop(0), tmpl.pop(0), tmpl.pop(0),
                              tmpl.pop(0), tmpl.pop(0))
            event_collection.append(event)
    except IOError as ioerr:
        print "File Error: %s" % str(ioerr)

def main():
    """docstring for main"""
    event_collection = EventCollection()

    read_file(event_collection)

    event_collection.as_json()
    # event_collection.as_csv()

if __name__ == '__main__':
    main()


texts: Searching repositories for files with forbidden strings

texts:
I wrote a small tool for searching git repositories for forbidden strings that I don't want to commit publicly. It's still something basic but it does the job.

The project contains three files.

config.py - this is the file where I store the secrects like which directories to scan and which strings to look for in a long regex (here redacted). This is commited to a VSTS repository because they are private.

import re

paths = [
        "c:\\..",
        "c:\\.."
    ]

re_forbidden = re.compile(r"(class)")

main.py - this is the core file. It uses os.walk to list all files and directories. Not all directories and files are processed. Paths that don't get commited like .git or bin and files like dll are skipped. Paths and the re_forbitten regex are imported from the above config.py. When found something suspicious then it just outputs the path of the affected file.

import os
import time
import itertools
import shutil
import re
import importlib.util
config_spec = importlib.util.spec_from_file_location("config", "c:\\home\\projects\\classified\\python\\sanipyzer\\config.py")
config = importlib.util.module_from_spec(config_spec)
config_spec.loader.exec_module(config)
from pprint import pprint

ignore_dirs = [".git", "bin", "obj", ".vs"]
ignore_files = ["dll", "exe", "pdb", "map"]

def format_filemtime(path):
    filemtime = os.path.getmtime(path)
    return time.strftime('%Y-%m-%d', time.gmtime(filemtime))

def ignore_dir(dirpath):
    for dir in ignore_dirs:
        pattern = r"\\" + re.escape(dir) + r"(\\|$)"
        if re.search(pattern, dirpath):
            return True
    return False

def ignore_file(file_name):
    for ext in ignore_files:
        pattern = r"\." + ext + "$"
        if re.search(pattern, file_name):
            return True
    return False

def sanitize(path):

    start = time.perf_counter()

    for (dirpath, dirnames, filenames) in os.walk(path):
        if ignore_dir(dirpath):
            continue
        searchable_filenames = [filename for filename in filenames if not ignore_file(filename)]
        for filename in searchable_filenames:
            full_name = os.path.join(dirpath, filename)

            # without 'ignore' it throws for some files the UnicodeDecodeError 'utf-8' codec can't decode byte XXX in position XXX: invalid start byte
            with open(full_name, 'r', encoding="utf8", errors="ignore") as searchable:
                text = searchable.read()
                if config.re_forbidden.search(text):
                    pprint(full_name)

    end = time.perf_counter()
    elapsed = round(end - start,2)
    print(f"elapsed: {elapsed} sec")

# --- --- ---

def main():
    for path in config.paths:
        sanitize(path)

if __name__ == '__main__':
    main()

test.py - this file is a very simple unittest. It tests the two ignore methods.

import unittest

from main import ignore_dir
from main import ignore_file

class MainTest(unittest.TestCase):

    def test_ignore_dir(self):
        self.assertTrue(ignore_dir("c:\\temp\\.git"))
        self.assertTrue(ignore_dir("c:\\temp\\.git\\abc"))

    def test_ignore_file(self):
        self.assertTrue(ignore_file("c:\\temp\\project\\program.dll"))
        self.assertFalse(ignore_file("c:\\temp\\project\\program.cs"))
        # tests the test
        #self.assertTrue(ignore_file("c:\\temp\\project\\program.cs"))

if __name__ == "__main__":
    # use False to avoid the traceback "Exception has occurred: SystemExit"
    unittest.main(exit=False)

What do you think of it? Is this an acceptable solution or are there still many beginner mistakes? I wasn't sure about the two ignore lists whether they are const and should be UPPER_CASE or are they just simple variables?