Google Code Jam - Alien Language I've successfully solved the Alien Languages problem for Google Code Jam in Haskell:

The algorithm is trivial - turn the patterns into regular expressions and see how many of the known words match these expressions.

But - this has taken me far longer than I expected it to, and most of the time I was battling with getting my types correct. Which leaves me wondering whether there is something wrong with my understanding of functional programming.

module Main where

-- http://code.google.com/codejam/contest/90101/dashboard#s=p0
-- Input and output with standard redirection operators

-- Unless otherwise indicated, all modules used are either bundled with
-- the Haskell Platform (http://hackage.haskell.org/platform/) or available
-- as a separate download from Hackage (http://hackage.haskell.org/).

import Data.List
import Text.Regex.Posix
import Data.String.Utils

numberOfMatches :: String -> [String] -> Int
numberOfMatches pattern = foldl' matches 0
  where
    matches acc word = if word =~ pattern' :: Bool
                           then acc + 1
                           else acc
    pattern' = replace "(" "[" $ replace ")" "]" pattern

getResult :: String -> ([String], Int, [String]) -> ([String], Int, [String])
getResult pattern (w, count, accum) = (w, count - 1, res : accum)
    where res = "Case #" ++ show count ++ ": " ++ show (numberOfMatches pattern w)

main :: IO ()
main = do
  (header:xs) <- fmap lines getContents -- IO is a Functor.
  let [_, d, n] = map read $ words header
  let knownWords = take d xs
  let patterns = drop d xs
  let (_, _, results) = foldr getResult (knownWords, n, []) patterns
  mapM_ putStrLn results

Which, for a test input of:

3 5 4
abc
bca
dac
dbc
cba
(ab)(bc)(ca)
abc
(abc)(abc)(abc)
(zyx)bc

Yields the results:

Case #1: 2
Case #2: 1
Case #3: 3
Case #4: 0

A good rule of thumb is that you should only ever use foldr when you're really sure that your fold is not an instance of something simpler. In your case, the fold is doing pretty much exactly two things while traversing the pattern list:

Keeping track of the "case index"
Accumulating the result list

The second should be easily recognisable as a map - maybe less obvious is that the first can just be written as a zip with an enumeration:

getResult :: [String] -> (Int, String) -> String
getResult w (count, pattern) =
  "Case #" ++ show count ++ ": " ++ show (numberOfMatches pattern w)

main :: IO ()
main = do
  [...]
  let results = map (getResult knownWords) $ zip [1..n] patterns

Which is much easier to understand than trying to bend the fold to do the right thing.

Also, just as a suggestion, here's main implemented in a more "imperative" (read: monadic) style. After all, Haskell is said to be the best imperative language ever invented, so we can do that proudly:

main :: IO ()
main = do
  [_, d, n] <- fmap (map read . words) getLine
  knownWords <- replicateM d getLine
  forM_ [1..n] $ \count -> do
    pattern <- getLine
    let matches = numberOfMatches pattern knownWords
    putStrLn $ concat ["Case #", show count, ": ", show matches]


Project Euler question 2 in CoffeeScript By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

fib = (x) ->
  return 0 if x == 0
  return 1 if x == 1
  return fib(x-1) + fib(x-2)

i = 2
fib_sum = 0

while (fib i) < 4000000
  n = fib i
  if n % 2 is 0 then fib_sum += fib i
  i++

alert fib_sum

I am looking for a more "functional" solution but I am stuck.
Edit
Optimized the solution:
limit = 4000000

sum = 0
a = 1
b = 1

while b < limit
  if b % 2 is 0 then sum += b
  h = a + b
  a = b
  b = h

alert sum #4613732

Better CoffeeScript

First of all, if you're going to use CoffeeScript, then use its full potential and do away with h:

limit = 4000000
sum = 0

[a, b] = [1, 1]

while b < limit
  sum += b if b % 2 is 0
  [a, b] = [b, a + b]

console.log sum

It's nice and easy to read, and I'd use this implementation instead of a recursive one in "real code", but it's still instructive to think about a functional equivalent.

Memoization

What you're looking for is named memoization. The trick is that you should store the values of fib(n) in an array. This allows you to lookup in the array for most cases instead of computing the value again. You'll be able to use fib() in a natural but optimized way.

memo = [1, 1]

fib = (x) ->
    if typeof memo[x]  != 'number'
        memo[x] = fib(x - 1) + fib(x - 2)

    return memo[x]

Now we have a function that compute memo[x] if needed, and returns it for every call. This means that the first call to fib(3) is going to compute it using memo[2] and memo[1], but all subsequent calls are going to be "free".

I can now use this implementation to compute the sum over even values:

fib_sum = (i) ->
    // Initialize i to 0 for the first call
    if i is undefined then i = 0

    return 0 if fib(i) > limit

    if fib(i) % 2 == 0
        return fib_sum(i + 1) + fib(i)
    else
        return fib_sum(i + 1)

You can use a tail-recursive function if you want but this won't help since you're probably going to overflow before doing a stack overflow.

Purely functional from the outside

Now the implementation seems functional but there's still this "memo" array that we would like to hide. Well, closures are the perfect way to achieve this. This is called the "pattern module" in JavaScript. Here's the full code:

fibonacci = (() ->
    'use strict'

    memo = [1, 1]
    limit = 4000000

    fib = (x) ->
        if typeof memo[x]  != 'number'
            memo[x] = fib(x - 1) + fib(x - 2)

        return memo[x]

    even_sum = (i) ->
        if i is undefined then i = 0

        return 0 if fib(i) > limit

        if fib(i) % 2 == 0
            return even_sum(i + 1) + fib(i)
        else
            return even_sum(i + 1)

    return {nth: fib, even_sum: even_sum}
)()

console.log fibonacci.even_sum(); # 4613732

There's no way to know that out fibonacci module uses an array internally, but it's still used and allows for very fast code. Benchmarking show that it's as fast as the iterative version, and takes about 0.02s on my machine.

Automatic memoization?

It turns out that JavaScript: The Good Parts mentions that you can have automatic memoization and uses fibonacci as an example. It can be interesting to try to apply this to our fib_sum, but I'll leave it as an exercise. :)


Generate filter criteria based on multiple field combinations The PrepareConditions method in the FeeTypeQueryService class prepares the conditions based on the searchConditions parameter and returns as an object. searchConditions can only have 3 combinations, such as:

Filter by TenantId, FeeTypeId
Filter by TenantId, FeeTypeName
Filter by TenantId, FeeTypeId, FeeTypeName

Note: The TenantId is mandatory for all the filter criterias.

Question: Is there a better way to write the PrepareConditions method so that it doesn't become complicated with a long list of conditional statements? When I start to have more properties in the FeeTypeSearchModel class, this may get dirty - I think.

Sample code:

FeeTypeQueryService.cs

public sealed class FeeTypeQueryService
{
    private object PrepareConditions(FeeTypeSearchModel searchConditions)
    {
        object conditions = null;

        if (searchConditions.FeeTypeId.HasValue && string.IsNullOrEmpty(searchConditions.FeeTypeName))
        {
            conditions = new { TenantId, FeeTypeId = searchConditions.FeeTypeId.Value };
            return conditions;
        }

        if (!searchConditions.FeeTypeId.HasValue && !string.IsNullOrEmpty(searchConditions.FeeTypeName))
        {
            conditions = new { TenantId, searchConditions.FeeTypeName };
            return conditions;
        }

        if (searchConditions.FeeTypeId.HasValue && !string.IsNullOrEmpty(searchConditions.FeeTypeName))
        {
            conditions = new { TenantId, FeeTypeId = searchConditions.FeeTypeId.Value, searchConditions.FeeTypeName };
            return conditions;
        }

        conditions = new { TenantId };
        return conditions;
    }
}

FeeTypeSearchModel.cs

public sealed class FeeTypeSearchModel
{
    public int? FeeTypeId { get; set; }
    public string FeeTypeName { get; set; }
}

Usually I tell at code reviews that saving a level of indentation is a good thing, but in the case of your code I wondered why you placed three times the same condition because I had a hard time to notice the !'s so I would suggest to take a different route, otherwise you will be confused in half a year as well.

I would use a `if..else if´ like so

if (searchConditions.FeeTypeId.HasValue)
{
    if (string.IsNullOrEmpty(searchConditions.FeeTypeName))
    {
        return new { TenantId, FeeTypeId = searchConditions.FeeTypeId.Value };
    }
    return new { TenantId, FeeTypeId = searchConditions.FeeTypeId.Value, searchConditions.FeeTypeName };
}
else if(string.IsNullOrEmpty(searchConditions.FeeTypeName))
{
    return new { TenantId };
}

return new { TenantId, searchConditions.FeeTypeName };


Finding the n th term in Fibonacci series Solving a problem in coding contest I came across a problem, which on digging a bit lead me to the data set in this question . Since the program to enumerate and finding the solution was too complex and execution time was below par I jot down the logic to the equation of curve (a single line)

Upon further digging deep I found that I missed the series which was forming a Fibonacci Series, hence by Binet's fourmla I found the nth term of the series which was even efficient. Here is the code

import math
import sys

def powLF(n):
    if n == 1:     return (1, 1)
    L, F = powLF(n//2)
    L, F = (L**2 + 5*F**2) >> 1, L*F
    if n & 1:
        return ((L + 5*F)>>1, (L + F) >>1)
    else:
        return (L, F)

def fib(n):
    if n & 1:
        return powLF(n)[1]
    else:
        L, F = powLF(n // 2)
        return L * F

def sum_digits(n):
   r = 0
   while n:
       r, n = r + n % 10, n // 10
   return r

def _print(string):
    fo = open("output.txt", "w+")
    fo.write(string)
    fo.close()

try:
    f = open('input.txt')
except IOError:
    _print("error")
    sys.exit()
num = f.readline()
try:
   val = int(num)
except ValueError:
    _print("error")
    sys.exit()

sum = sum_digits(int(num))
f.close()

if (sum == 2):
    _print("1")
else:
    _print(str(int(math.ceil(fib(sum)))))

Although still the code doesn't seem to match the par criteria, how can I optimize the code further ?

The code looks fine to me apart from the fact that it can throw an exception. The function fib() powLF() are giving you the complexity of O(log(n)) plus the execution time of the function sum_digits() is 479 ns per loop which is perfectly fine.

> %timeit sum_digits(n)
1000000 loops, best of 3: 479 ns per loop

Take a look at Lecture 3 of the MIT Open Courseware course on algorithms for a good analysis of the matrix approach.

>>> timeit.timeit('fib(1000)', 'from __main__ import fibM as fib', number=10000)
0.40711593627929688
>>> timeit.timeit('fib(1000)', 'from __main__ import fibL as fib', number=10000)
0.20211100578308105


Buttons labelled with the number of times they have been clicked I am new to functional programming and I decided to learn it thru Elm instead of haskell. I wrote a practice code where there are 2 buttons and their names correspond to the number of times they are clicked. The following is my implementation:

import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (onClick)

main = Html.beginnerProgram {model=model, view=view, update=update}

-- model
type alias Model =
   { a : Int
   , b : Int
   , total : Int}

model : Model
model = Model 0 0 0

 -- view
type Msg = Add Foo
type Foo = A | B

view : Model -> Html Msg
view model =
   div []
     [ button [onClick (Add A)] [text (toString model.a)]
     , button [onClick (Add B)] [text (toString model.b)]
     , div [] [ text (toString model.total)]
     ]

 -- update
update : Msg -> Model -> Model
update msg model =
   case msg of
     Add foo ->
       case foo of
         A ->
           let
             a = model.a + 1
             total = a + model.b
           in
             {model | a = a, total = total}
         B ->
           let
             b = model.b + 1
             total = b + model.a
           in
             {model | b = b, total = total}

I feel like that the above code is a dumb implementation of what I wanted to accomplish. Can somebody suggest a better implementation?

Here are a few things I'd recommend changing:

There's no need to store the sum of a and b. You can always compute it in the view with a + b. This simplifies the update function a lot.
You can match Foo A and Foo B in update instead of a nested case.
Optionally, you can destructure model into {a, b} in view. I don't have any opinion on this. Either way is fine.

Here's the final elm-format'd code:

module Main exposing (..)

import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (onClick)

main =
    Html.beginnerProgram { model = model, view = view, update = update }

type alias Model =
    { a : Int
    , b : Int
    }

model : Model
model =
    Model 0 0

type Msg
    = Add Foo

type Foo
    = A
    | B

view : Model -> Html Msg
view { a, b } =
    div []
        [ button [ onClick (Add A) ] [ text (toString a) ]
        , button [ onClick (Add B) ] [ text (toString b) ]
        , div [] [ text (toString (a + b)) ]
        ]

update : Msg -> Model -> Model
update msg model =
    case msg of
        Add A ->
            { model | a = model.a + 1 }

        Add B ->
            { model | b = model.b + 1 }

Live: https://ellie-app.com/c7GLFrXx3a1/0


Loss function in python is a bit cumbersome This is my first post here. I seek to improve my way of writing Python and C++ code. I hope I can also contribute to others when I have increased my skill.
Here is a python function I have for a neural network I have implemented. I feel it become a bit cumbersome. The intention is to weight positive labels in channels 1:end higher than background pixels. Hence the distinction between foreground and background.
def loss_function(
        self,
        x: torch.tensor,
        groundtruth: torch.tensor,
        weight: float
) -> torch.tensor:

    delta = 0.00001
    groundtruth_pos = groundtruth[:, 1:, :, :] == 1
    groundtruth_neg = groundtruth[:, 1:, :, :] == 0
    foreground_gt = groundtruth[:, 1:, :, :]
    background_gt = groundtruth[:, 0, :, :]

    foreground_x = x[:, 1:, :, :]
    background_x = x[:, 0, :, :]
    loss_foreground_pos = -(torch.sum(foreground_gt[groundtruth_pos] * torch.log(foreground_x[groundtruth_pos] + delta))
                            + torch.sum((1 - foreground_gt[groundtruth_pos]) * torch.log(1 - foreground_x[groundtruth_pos] + delta)))
    loss_foreground_neg = -(torch.sum(foreground_gt[groundtruth_neg] * torch.log(foreground_x[groundtruth_neg] + delta))
                            + torch.sum((1 - foreground_gt[groundtruth_neg]) * torch.log(1 - foreground_x[groundtruth_neg] + delta)))

    loss_background = -(torch.sum(background_gt * torch.log(background_x + delta))
                        + torch.sum((1 - background_gt) * torch.log(1 - background_x + delta)))
    return weight * loss_foreground_pos + loss_foreground_neg + loss_background

It looks like you need to calculate the same thing over and over again. Instead of doing this separately for the positive, the negative and all labels (not sure that is the correct word), you could do it once and then use that once you need to calculate the sums:
def loss_function(
        self,
        x: torch.tensor,
        groundtruth: torch.tensor,
        weight: float
) -> torch.tensor:

    delta = 0.00001
    foreground_gt = groundtruth[:, 1:, :, :]
    background_gt = groundtruth[:, 0, :, :]
    groundtruth_pos = foreground_gt == 1
    groundtruth_neg = foreground_gt == 0

    foreground_x = x[:, 1:, :, :] + delta
    background_x = x[:, 0, :, :] + delta

    a = foreground_gt * torch.log(foreground_x)
    b = (1 - foreground_gt) * torch.log(1 - foreground_x)
    c = a + b

    loss_foreground_pos = -torch.sum(c[groundtruth_pos])
    loss_foreground_neg = -torch.sum(c[groundtruth_neg])

    loss_background = -torch.sum(background_gt * torch.log(background_x))
                                 + (1 - background_gt) * torch.log(1 - background_x))
    return weight * loss_foreground_pos + loss_foreground_neg + loss_background

Note that sum(a) + sum(b) is the same as sum(a + b) = sum(c).


PowerShell script to automate the search of DLL files I often write different applications in C++ using different libraries, and sometimes it takes a lot of time to find where are the *.dll files to distribute. I use Dependency Walker but it still takes a lot of time to look in the GUI. I wrote a PowerShell script that will find all used libraries and do something with them. Please review my code.

Param
(
    [Parameter(Mandatory=$true, Position=0)]
    [ValidateScript({Test-Path $_ -PathType Leaf})]
    [String]$Path,

    [Parameter()]
    [ValidateScript({Test-Path $_ -PathType Container})]
    [String[]]$Exclude
)

function likeAnyOf($obj,$array)
{
    foreach($item in $array)
    {
        if($obj -like $item)
        {
            return $item
        }
    }
    return $null
}

Set-StrictMode -Version Latest
$systemPath = [System.Environment]::SystemDirectory
$tempName = [System.IO.Path]::GetTempFileName() # generate a temporary filename

Start-Process depends.exe -ArgumentList '/c','/f:1',"/oc:$tempName",$Path -Wait -NoNewWindow # wait until it writes all the data
$modules = Import-Csv $tempName -Encoding Default | Select-Object -Property Module -Unique # read the output of depends.exe and select all unique DLL paths
Remove-Item -Path $tempName -Force # delete the temporary file

$dllsToCopy = @{}

foreach($module in $modules)
{
    $modulePath = $module.Module
    $moduleParent = Split-Path $modulePath -Parent
    $moduleName = Split-Path $modulePath -Leaf

    if( $moduleParent -like $systemPath )
    {
        Write-Verbose "Skipped $moduleName as a system module ($moduleParent)"
        continue
    }
    $excluded = likeAnyOf $moduleParent (Resolve-Path $Exclude)
    if($excluded -ne $null)
    {
        Write-Host "Skipped $moduleName as explicitly excluded ($excluded)"
        continue
    }

    $exists = Test-Path $modulePath -PathType Leaf
    if($exists)
    {
        $dllsToCopy.Add($moduleName, $modulePath)
        Write-Host "Added $moduleName found at $moduleParent"
    }
    else
    {
        Write-Error "Couldn't find $moduleName"
    }
}

You can replace all this:

function likeAnyOf($obj,$array)
{
    foreach($item in $array)
    {
        if($obj -like $item)
        {
            return $item
        }
    }
    return $null
}

# ...

$excluded = likeAnyOf $moduleParent (Resolve-Path $Exclude)

With this:

$excluded = Resolve-Path $Exclude | ? { $moduleParent -like $_ }

The question mark (?) is an alias for Where-Object, so read up on that for an explanation.

Instead of this:

if ($excluded -ne $null)
{
    # ...
}

you can do this:

if ($excluded)
{
    # ...
}

It's just a little bit easier to read.


Join and pivot multiple tables of securities data in MySQL I'm interested in a way to join three (or more) tables in a way that is more efficient (both in terms of conciseness of code and in terms of generating the results) than what I have currently.

Underlying tables:

table ISIN
+---------------+----+------+
| isin          | id | code |
+---------------+----+------+
| US0378331005  |  1 | NULL |
| AU0000XVGZA3  |  2 | z    |
| GB0002634946  |  3 | y    |
+---------------+----+------+

table additionalCredit
+------+----+
| code | id |
+------+----+
| h    |  1 |
| i    |  2 |
+------+----+

table codes
+--------+------+----------------------+
| codeId | code | description          |
+--------+------+----------------------+
|   9    | h    | ETM - Principal Only |
|   9    | i    | ETM - Waiting Close  |
|   8    | z    | No Redemption        |
|   8    | y    | Partially Prerefunded|
+--------+------+----------------------+

Expected results:

+---------------+-----------------------+----------------------+
| isin          | type8                 | type9                |
+---------------+-----------------------+----------------------+
| US0378331005  | null                  | ETM - Principal Only |
| AU0000XVGZA3  | No Redemption         | ETM - Waiting Close  |
| GB0002634946  | Partially Prerefunded | null                 |
+---------------+-----------------------+----------------------+

Working Code

select
ISIN.isin,
min(type8), min(type9)
from
(select
ISIN.isin,
case when codes.codeId=8 then codes.description end as type8,
case when codes.codeId=9 then codes.description end as type9
from ISIN
left join codes
on ISIN.code=codes.code

union
select
ISIN.isin,
case when codes.codeId=8 then codes.description end as type8,
case when codes.codeId=9 then codes.description end as type9
from ISIN
left join additionalCredit ac
on ac.id=isin.id
left join codes
on codes.code=ac.code) as n
group by n.name

Context

...from the original Q&A at Stack Overflow that gives my thought process and steps taken, where the first commenter suggested there could be a better answer. Here's a fiddle.

Can you help me find a better way than a union statement (in which nearly the entire query is duplicated)?

DB Design

Your tables are not correctly normalized:

codes.description depends on codes.code → requires a table with codes.code as primary key

You are also missing constraints:

ISIN.id primary key
ISIN.name unique key
additionalCredit.id, additionalCredit.code primary key
codes.codeId, codes.code primary key

Query Optimization

mysql does not come with a pivot, so we need to use an aggregate function - in this case min(..) - instead. This way we can avoid the union of redundant quasi code duplication.

Fiddle

query..

select isin
    , min(if(codeId = 8, description, null)) Type8
    , min(if(codeId = 9, description, null)) Type9
from (
    select codeId, code, description, ifnull(name1, name2) isin from (
        select codes.codeId, codes.code, codes.description, ISIN1.isin name1, ISIN2.isin name2
        from codes
        left join ISIN ISIN1 on ISIN1.code = codes.code
        left join additionalCredit on additionalCredit.code = codes.code
        left join ISIN ISIN2 on ISIN2.id = additionalCredit.id
    ) q1
) p1
group by isin
order by isin
;

yielding..

name              Type8                 Type9
AU0000XVGZA3      No Redemption         ETM - Waiting Close
GB0002634946      Partial Prerefunded
US0378331005                            ETM - Principal Only


Payment application database I would really like some advice from any DB gurus who have a few minutes free. After doing some reading and playing with sqlfiddle over the weekend I have constructed this PostgreSQL schema and it is the first proper one I've ever done, so I am sure I've made some poor choices.

This is what I've got for a "job" and "advert" site:

--
-- Application Database Structure
--

-- Company Table
CREATE TABLE company (
    id SERIAL PRIMARY KEY,
    name varchar(60) NOT NULL
);

-- Country Table
CREATE TABLE country (
    id SERIAL PRIMARY KEY,
    name varchar(255) NOT NULL
);

-- Location Table
CREATE TABLE location (
    id SERIAL PRIMARY KEY,
    name varchar(255) NOT NULL,
    country_id integer NOT NULL REFERENCES country (id),
    coordinate varchar(255) NOT NULL
);

-- Source Table
CREATE TABLE source (
    id SERIAL PRIMARY KEY,
    name varchar(60) NOT NULL
);

-- Payment Method Table
CREATE TABLE payment_method (
    id SERIAL PRIMARY KEY,
    name varchar(60) NOT NULL
);

-- Payment Table
CREATE TABLE payment (
    -- Generic
    id SERIAL PRIMARY KEY,
    ip varchar(255),
    email varchar(255) NOT NULL,
    amount varchar(255),
    payment_method_id integer NOT NULL REFERENCES payment_method (id),
    -- Credit Card
    card_name varchar(255),
    card_expiration varchar(255),
    -- Paypal
    paypal_email varchar(255),
    paypal_transactionid varchar(255)
);

-- Job Table
CREATE TABLE job (
    -- Identification
    id SERIAL PRIMARY KEY,
    solrid varchar (10),
    -- Job Information
    title varchar(60) NOT NULL,
    description varchar(255) NOT NULL,
    truncated_description varchar(255) NOT NULL,
    keyword_description varchar(255) NOT NULL,
    how_to_apply varchar(255) NOT NULL,
    website_url varchar(255) NOT NULL,
    logo_url varchar(255),
    page_url varchar(255) NOT NULL,
    -- Dates
    date timestamp NOT NULL,
    expires timestamp NOT NULL,
    -- Linked Tables
    company_id integer NOT NULL REFERENCES company (id),
    source_id integer NOT NULL REFERENCES source (id),
    location_id integer NOT NULL REFERENCES location (id),
    payment_id integer NOT NULL REFERENCES payment (id),
    -- Status Flags
    active boolean DEFAULT FALSE,
    premium boolean DEFAULT FALSE,
    indexed boolean DEFAULT FALSE,
    error boolean DEFAULT FALSE,
      -- Email Flags
    email_reminder boolean DEFAULT FALSE,
    email_confirmation boolean DEFAULT FALSE
);

-- Advert Table
CREATE TABLE advert (
    -- Identification
    id SERIAL PRIMARY KEY,
    -- Content
    title varchar(60) NOT NULL,
    description varchar(255) NOT NULL,
    page_url varchar(255) NOT NULL,
    -- Dates
    date timestamp NOT NULL,
    expires timestamp NOT NULL,
    -- Email Flags
    email_reminder boolean DEFAULT FALSE,
    email_confirmation boolean DEFAULT FALSE
    -- Linked Tables
    source_id integer NOT NULL REFERENCES source (id),
    payment_id integer NOT NULL REFERENCES payment (id),
    -- Status Flag
    active boolean DEFAULT FALSE,
    error boolean DEFAULT FALSE
);

Could anyone give me some guidance / possible improvements before I start building my site about this DB?

Hmmm.....

--
-- Application Database Structure
--

-- Company Table
CREATE TABLE company (
    id SERIAL PRIMARY KEY,
    name varchar(60) NOT NULL
);

This probably works, for now.  I'm assuming the column can handle weird characters, things like Chinese characters, if appropriate.  Please note that international business gets a little strange, in terms of things like subsidiaries, and companies doing business in foreign countries (not sure if important).

-- Country Table
CREATE TABLE country (
    id SERIAL PRIMARY KEY,
    name varchar(255) NOT NULL
);

You're going to want to add a 'localization' table, in addition to refactoring this.  Something along these lines:

-- Country Table
CREATE TABLE country (
    id SERIAL PRIMARY KEY,
    Iso3166_1_Alpha_3 char(3) NOT NULL,
    Iso3166_1_Alpha_2 char(2),
    Iso3166_1_Numeric char(3) NOT NULL
        -- This last is comprised of digits,
        -- But not really a number, per se
);

-- Country Localization table
CREATE TABLE country_localization (
    country_id INTEGER NOT NULL REFERENCES country (id),
    language_id INTEGER NOT NULL REFERENCES language (id),
    name varchar(100) NOT NULL
);

.... Which means you're going to want a 'language' table (and localization) as well.  You can use this as the base pattern - the relevant ISO code standard is ISO 639.

-- Location Table
CREATE TABLE location (
    id SERIAL PRIMARY KEY,
    name varchar(255) NOT NULL,
    country_id integer NOT NULL REFERENCES country (id),
    coordinate varchar(255) NOT NULL
);

I can't really say if name is appropriate - what are you planning on storing there?  Please note that addresses are incredibly varied in international settings.  Storing country is likely okay.  coordinate is almost certainly wrong; if postgreSQL has a geo extension/module, use the relevant types - otherwise, use a pair of doubles/reals, and store latitude/longitude.

-- Source Table
CREATE TABLE source (
    id SERIAL PRIMARY KEY,
    name varchar(60) NOT NULL
);

... source of what?

-- Payment Method Table
CREATE TABLE payment_method (
    id SERIAL PRIMARY KEY,
    name varchar(60) NOT NULL
);

You may need to internationalize this one, as well.  I'm assuming this is effectively an Enum (eg, 'VISA', 'MASTERCARD', etc).

-- Payment Table
CREATE TABLE payment (
    -- Generic
    id SERIAL PRIMARY KEY,
    ip varchar(255),
    email varchar(255) NOT NULL,
    amount varchar(255),
    payment_method_id integer NOT NULL REFERENCES payment_method (id),
    -- Credit Card
    card_name varchar(255),
    card_expiration varchar(255),
    -- Paypal
    paypal_email varchar(255),
    paypal_transactionid varchar(255)
);

email suggests that you should have some sort of 'payee' table, either as a person, business or... company.  Also, why do you have two emails in the table?  Try to avoid storing multiple logical 'entities' in the same table.   While I applaud not having a column for 'credit card number', if you're going to be able to track reversals, you're going to have to know what number to reverse (assuming that you accept non-PayPal transactions).
Even under IPV6, you don't need an ip address that long - the maximum address is 39 characters (including delimiters).
  amount should be either DECIMAL or NUMERIC (at least you didn't make it float/double/real...).
I'm also nervous about card_expiration being character-based.  Really, what you have is an 'expires before business day' column (eg - it's usually expressed as 'expires 07/2012', which is 'active before 2012-08-01'), which probably ought to be stored as a date (probably without time, given the possibility of DST moving, but probably with timezone).  Note that is still complicated, because, due to timezones (and ignoring any shifting business themselves do), there are 2 concurrent business (sun-has-come-up) days on the planet happening at once: when any particular bank/payment pre-processor switches is up to them.

-- Job Table
CREATE TABLE job (
    -- Identification
    id SERIAL PRIMARY KEY,
    solrid varchar (10),
    -- Job Information
    title varchar(60) NOT NULL,
    description varchar(255) NOT NULL,
    truncated_description varchar(255) NOT NULL,
    keyword_description varchar(255) NOT NULL,
    how_to_apply varchar(255) NOT NULL,
    website_url varchar(255) NOT NULL,
    logo_url varchar(255),
    page_url varchar(255) NOT NULL,
    -- Dates
    date timestamp NOT NULL,
    expires timestamp NOT NULL,
    -- Linked Tables
    company_id integer NOT NULL REFERENCES company (id),
    source_id integer NOT NULL REFERENCES source (id),
    location_id integer NOT NULL REFERENCES location (id),
    payment_id integer NOT NULL REFERENCES payment (id),
    -- Status Flags
    active boolean DEFAULT FALSE,
    premium boolean DEFAULT FALSE,
    indexed boolean DEFAULT FALSE,
    error boolean DEFAULT FALSE,
    -- Email Flags
    email_reminder boolean DEFAULT FALSE,
    email_confirmation boolean DEFAULT FALSE
);

What's solrid?  There's no explanation.
You may want to create some sort of website table that you can just fk-reference, including things like the site and logo there.
truncated_description isn't any shorter than description... is one supposed to be shorter?
You are probably going to want a (internationalized?) tag (and job_tag) table, taking the place of keyword_description; split user search terms, get all matching tags, and find the jobs with the highest number of matches (and potentially search the regular description as well).
What date does date represent?  postedOn?  paidForOn?  fufilledOn?  Always try to name columns descriptively as to what they logically represent; these will often not include the column type.
expires probably shouldn't be a timestamp type.  If you have it, it should be a date (sans time) type.  The reasoning is that it's an imprecise, business-related concept (actual close of business isn't necessarily midnight...).  This may apply to date as well, but the name is too ambiguous to be able to tell.  Date ranges should also (pretty much always) be upper-bound exclusive (this blog mostly deals with SQL Server-specific problems, but the concepts are equivalent); in light of that, the name should probably be changed to something like activeUntil (or store the duration instead).
Is active derived information - can I tell just by whether the current (business) day is between date and expires?  If so, unless you have an actual performance issue, it's best to not store it (ie what happens if active is true after expires is in the past?).

-- Advert Table
CREATE TABLE advert (
    -- Identification
    id SERIAL PRIMARY KEY,
    -- Content
    title varchar(60) NOT NULL,
    description varchar(255) NOT NULL,
    page_url varchar(255) NOT NULL,
    -- Dates
    date timestamp NOT NULL,
    expires timestamp NOT NULL,
    -- Email Flags
    email_reminder boolean DEFAULT FALSE,
    email_confirmation boolean DEFAULT FALSE
    -- Linked Tables
    source_id integer NOT NULL REFERENCES source (id),
    payment_id integer NOT NULL REFERENCES payment (id),
    -- Status Flag
    active boolean DEFAULT FALSE,
    error boolean DEFAULT FALSE
);

See previous remarks about date, expires and active.
Hmm, you may want some sort of 'reminder' and 'confirmation' table sets.

All I can think of at the moment.  Providing expected (example) data contents may also help us critique your efforts.


Check whether a number can be dialed on a phone keypad without diagonal moves I'm trying to write/come up with an algorithm for a phone keypad traversal.

Let's say I have a rook on a keypad. The rook can traverse only horizontally and vertically. My code has to input a phone number and check whether the rook is able to dial it (shouldn't go diagonally) and return a boolean based on the result.

Example: A phone number "4632871" is termed as "true" since the rook can traverse without going diagonally whereas "4853267" is termed as "false"

Below is my code implementation:

var data = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9],
];

function rookTraversal(arr, phoneNumber) {

  var arrIndex = [],
    numArray = [];

  var number = phoneNumber.split('');

  for (var i = 0; i < arr.length; i++) {
    for (var j = 0; j < arr.length; j++) {
      arrIndex.push([i, j]);
    }
  }

  arrIndex = arrIndex.map((item, index) => ({
    [index + 1]: item,
  }));

  number.forEach((item) => {
    numArray.push(arrIndex[item - 1]);
  });

  let numArrayKeys = numArray.reduce((acc, x) => [...acc, Object.values(x).map((y) => y)], []);

  for (let i = 1; i < numArrayKeys.length; i++) {
    var x1 = numArrayKeys[i - 1][0][0];
    var y1 = numArrayKeys[i - 1][0][1];
    var x2 = numArrayKeys[i][0][0];
    var y2 = numArrayKeys[i][0][1];

    if (x1 !== x2 && y1 !== y2) {
      return false;
    }
  }

  return true;
}

console.log(rookTraversal(data, '4631782'));
console.log(rookTraversal(data, '4853267'));

Is there a way to optimize this piece of code?

There are a lot of different traits you could optimize for.  I like to optimize for lines of code, so I'll try that.

You can simplify the testing by building a "graph" (2-dimensional array) of reachable numbers.  Each row and column index is a phone digit.  If the value at (i,j) is true, then j is reachable from i.

To do this, take all the numbers in each digit's row/column and create an array where those indexes have a value of 1.

The reachability graph looks like this:

The digit 6 can reach 3,4,5,6,9 and you can see that reachable[6][x] is true for x=3,4,5,6 or 9.

Then simply run through the list of numbers and test if reachable[current][next] is true.

I've assumed the rook can dial repeated numbers like 3333.  If no, you can test for that in the graph creation or during reachability testing.

function rookTraversal(rows, phoneNumber) {
    var cols=rows[0].map((col, i) => rows.map(row => row[i])),
        digits=phoneNumber.match(/(\d)/g),
        reachable=[];
    for (var row = 0; row < rows.length; row++) {
        for (var col = 0; col < cols.length; col++) {
            reachable[ rows[row][col] ] = rows[row].concat(cols[col]).reduce( (map, value) => { map[value]=1; return map }, [] );
        }
    }
    // console.log({reachable,digits});
    for (var i=0; i<digits.length-1; ++i) if (! reachable[ digits[i] ][ digits[i+1] ]) return false;
    return true;
}

var data = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9],
];

console.log(rookTraversal(data, '4631782'));
console.log(rookTraversal(data, '4853267'));


OpenMP workaround for barrier in loop What I basically want to do is this:

int main()
{
    const int n = 100;

    #pragma omp parallel for
    for (int i=0; i<n; i++)
    {
        int thread_ID = omp_get_thread_num();
        printf("%d) work 1 %d\n", thread_ID, i);
    #pragma omp barrier
        printf("    %d) work 2 %d\n", thread_ID, i);
    #pragma omp barrier
    }
    return 0;
}

except we cannot put a barrier into a parallel for in OpenMP; it just cannot be done. However, it's possible to explicitly do the separation between the threads in order to make it work:

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

int main()
{
    const int n = 100;
    int nthreads;
    #pragma omp parallel
    {
        // get the number of threads
        #pragma omp single
        {
            nthreads = omp_get_num_threads();
        }

        int thread_ID = omp_get_thread_num();

        // calculate which threads have to do one more iteration
        int one_more = thread_ID<(n%nthreads);
        int step = n/nthreads;
        int start, end;

        if(one_more){
            start = step*thread_ID + thread_ID;
            end = start + step + 1;
        }
        else{
            start = step*thread_ID + n%nthreads;
            end = start + step;
        }

        // the real work is here
        for (int i=start; i<start+step+1; i++)
        {
            if(i<end)
                printf("%d) work 1 %d\n", thread_ID, i);
        #pragma omp barrier
            if(i<end)
                printf("    %d) work 2 %d\n", thread_ID, i);
        #pragma omp barrier
        }

    }
    return 0;
}

However, I find this a bit dirty and I would have preferred something cleaner. Does somebody have a better idea?

You want to synchronize two parts of your iteration and parallelize the each one of them independently. A good option is splitting the code in two:

int main()
{
  const int n = 100;

  #pragma omp parallel
  {
  #pragma omp for
  for (int i=0; i<n; i++)
  {
      int thread_ID = omp_get_thread_num();
      printf("%d) work 1 %d\n", thread_ID, i);
  } // barrier (implicit, unless using 'nowait')

  #pragma omp for
  for (int i=0; i<n; i++)
  {
      printf("    %d) work 2 %d\n", thread_ID, i);
  } // barrier
  } // omp parallel
  return 0;
}


Wildcard search in C In the effort to improve my C knowledge, I tried creating a wildcard search function:

bool wildcard(char *value, char *wcard) {
    size_t vsize = strlen(value);
    size_t wsize = strlen(wcard);

    bool no_match = false;

    if (wsize > vsize) {
        return false;
    }

    for (int w = 0, v = 0; w < wsize; w++, v++) {
        switch (wcard[w]) {
            case MULTICHAR:
                if (w == wsize) {
                    goto match;
                } else {
                    w++;
                    while (v < vsize) {
                        if (wcard[w] == value[v++]) {
                            v--;
                            break;
                        }
                    }
                    if (no_match) {
                        goto no_match;
                    }
                }
                break;
            case ONECHAR:
                break;
            default:
                if (wcard[w] != value[v]) {
                    goto no_match;
                }
        }
    }

    match:
    return true;

    no_match:
    return false;
}

I have tested this by doing:

printf("Result: %d <- Should be true\n",wildcard("Hello World","Hello*"));
printf("Result: %d <- Should be true\n",wildcard("Hello World","*Hello*"));
printf("Result: %d <- Should be true\n",wildcard("Hello World","He?lo*"));

All return 1.

You can see the project here.

Any tips or improvements are welcome.

Note

I've already found a bug. If the wcard in present in the string, it will return true even if there is more data there.

Example:

wildcard("Hello World","Hello")

This will return true when it shouldn't.

You should the parameters for null values. strlen() does not tolerate nulls passed to it as parameters.

I'm not a complete zealot when it comes to the use of goto, but I'm not sure what benefit it gives in this code. Replacing the gotos with return true or return false would make the code a little simpler and more readable.

  if (no_match) {
       goto no_match;
  }

compared to this for example:

  if (no_match)
     return false;

For the bug you mentioned you will need to compare the lengths of the first and second wildcard parameters IF there is no special character in the second parameter.


Drawing a Thue-Morse pattern recursively This is web exercise 3.1.63. from the book Computer Science An Interdisciplinary Approach by Sedgewick & Wayne:

Write a program  that reads in a command line input N and plots the
N-by-N Thue-Morse pattern.  Below are the Thue-Morse patterns for
N = 4, 8, and 16.

Here is my program:
public class ThueMorse
{
    public static void drawThueMorse1(int n, double x, double y, double size)
    {
        if (n == 0) return;
        double x1 = x - size/2, x2 = x + size/2;
        double y1 = y - size/2, y2 = y + size/2;
        StdDraw.setPenColor(StdDraw.BOOK_BLUE);
        StdDraw.filledRectangle(x+(3*size/4),y,size/4,size/2);
        //StdDraw.pause(300);
        StdDraw.filledRectangle(x-(3*size/4),y,size/4,size/2);
        //StdDraw.pause(300);
        StdDraw.filledRectangle(x,y+(3*size/4),size/2,size/4);
        //StdDraw.pause(300);
        StdDraw.filledRectangle(x,y-(3*size/4),size/2,size/4);
        //StdDraw.pause(300);
        StdDraw.setPenColor(StdDraw.WHITE);
        StdDraw.filledSquare(x,y,size/2);
        //StdDraw.pause(300);
        StdDraw.filledSquare(x+(3*size/4),y+(3*size/4),size/4);
        //StdDraw.pause(300);
        StdDraw.filledSquare(x+(3*size/4),y-(3*size/4),size/4);
        //StdDraw.pause(300);
        StdDraw.filledSquare(x-(3*size/4),y-(3*size/4),size/4);
        //StdDraw.pause(300);
        StdDraw.filledSquare(x-(3*size/4),y+(3*size/4),size/4);
        //StdDraw.pause(300);
        drawThueMorse1(n-1, x1, y1, size/2);
        drawThueMorse2(n-1, x1, y2, size/2);
        drawThueMorse2(n-1, x2, y1, size/2);
        drawThueMorse1(n-1, x2, y2, size/2);
    }
    public static void drawThueMorse2(int n, double x, double y, double size)
    {
        if (n == 0) return;
        double x1 = x - size/2, x2 = x + size/2;
        double y1 = y - size/2, y2 = y + size/2;
        StdDraw.setPenColor(StdDraw.WHITE);
        StdDraw.filledRectangle(x+(3*size/4),y,size/4,size/2);
        //StdDraw.pause(300);
        StdDraw.filledRectangle(x-(3*size/4),y,size/4,size/2);
        //StdDraw.pause(300);
        StdDraw.filledRectangle(x,y+(3*size/4),size/2,size/4);
        //StdDraw.pause(300);
        StdDraw.filledRectangle(x,y-(3*size/4),size/2,size/4);
        //StdDraw.pause(300);
        StdDraw.setPenColor(StdDraw.BOOK_BLUE);
        StdDraw.filledSquare(x,y,size/2);
        //StdDraw.pause(300);
        StdDraw.filledSquare(x+(3*size/4),y+(3*size/4),size/4);
        //StdDraw.pause(300);
        StdDraw.filledSquare(x+(3*size/4),y-(3*size/4),size/4);
        //StdDraw.pause(300);
        StdDraw.filledSquare(x-(3*size/4),y-(3*size/4),size/4);
        //StdDraw.pause(300);
        StdDraw.filledSquare(x-(3*size/4),y+(3*size/4),size/4);
        //StdDraw.pause(300);
        drawThueMorse1(n-1, x1, y1, size/2);
        drawThueMorse2(n-1, x1, y2, size/2);
        drawThueMorse2(n-1, x2, y1, size/2);
        drawThueMorse1(n-1, x2, y2, size/2);
    }
    public static int log2(int x)
    {
        return (int) (Math.log(x)/Math.log(2));
    }
    public static void main(String[] args)
    {
        int n = Integer.parseInt(args[0]);
        n = log2(n)-1;
        drawThueMorse1(n, 0.5, 0.5, 0.5);
    }
}

StdDraw is a simple API written by the authors of the book. I checked my program and it works. Here is one instance of it:
Input: N = 256
Output:

Is there any way that I can improve my program?
Thanks for your attention.

Is there any way that I can improve my program?

Yes :)
A good cue to see if your program can be improved is when there is lot of repetition in the code. There are usually ways to avoid repetition, although they can sometimes get pretty complex. However, avoiding repetition is useful for two very important reasons :

Copy/paste usually leads to "dumb" bugs
If you want to change a thing you need to change it in a billion places, making it more likely you'll forget one.

I'll be very honest, I'm reading your code as much as I can, but I don't understand what's happening in there, so it's going to be hard to provide a review of what you're doing, but it's also an indication that the code isn't clear enough to be understood by someone trying to understand it (me, in this case).
What can be done about this?

Use comprehensive names (That goes for methods, variables, everything really)
Spacing the code properly (Have you ever read a complicated paper/book where all the text was cramped up? Have you ever read another one where the was good spacing? It makes a world of difference!)
Avoiding commented code (Why is it there? Is it a mistake? Should it be uncommented? How long has it been commented? Why?)
Finally, when none of those tips are enough, you should comment your code, but be cautious not to "overcomment". Comments should be just long enough so that someone can understand why you coded the way you did. There's no need to explain things such as "I'm changing the pen color" over StdDraw.setPenColor(StdDraw.BOOK_BLUE);

Now, there's one thing that I think would help tremedously with the readability of the code. Right now, your code both computes the "pattern" and prints it. What if you first generated a binary 2D array that fit the problem at hand, and you had another function that printed this array?
Taking an example of the N=4 case you gave, the array could look like this :
0110
1001
1001
0110

Separating the algorithm from the printing will also let you see how you can improve your code.
For example, we can notice that there's symmetry between the \$row_i\$ and \$col_i\$, meaning that we only need to find the value of half the grid in order to find all the values (that would be a valuable optimisation for a problem with a big N value).
Now, if I take a look at the link you gave us, I see that :

Amazingly, the Thue-Morse sequence can be generated from the substitution system :
0 -> 01 and 1 -> 10, recursively.

Let's look at this with the first row of the N=8 example you gave us. We'll need  \$log_2(8)=3\$ iterations of this substitution sequence : 0->01->0110->01101001. Let's fill our 2D array with this :
0 1 1 0 1 0 0 1
1 X X X X X X X
1 X X X X X X X
0 X X X X X X X
1 X X X X X X X
0 X X X X X X X
0 X X X X X X X
1 X X X X X X X

Now, let's fill the second row, knowing that the first value is 1 : 1 -> 10 -> 1001 -> 10010110
0 1 1 0 1 0 0 1
1 0 0 1 0 1 1 0
1 0 X X X X X X
0 1 X X X X X X
1 0 X X X X X X
0 1 X X X X X X
0 1 X X X X X X
1 0 X X X X X X

For completion, let's do the third row, and I'll leave the rest to you 10 -> 1001 -> 10010110
0 1 1 0 1 0 0 1
1 0 0 1 0 1 1 0
1 0 0 1 0 1 1 0
0 1 1 X X X X X
1 0 0 X X X X X
0 1 1 X X X X X
0 1 1 X X X X X
1 0 0 X X X X X

I guess you see where this ends. This leads to a pretty performant approach with code that should be pretty simple to understand. Afterwards, you could have a function paintThueMorse, give it this 2D array and paint blue for 1, white for 0 or the inverse.


Rating the quality of a response based on the number of typos I have this method which returns the number, but Rubocop said:

  Assignment Branch Condition size for rating is too high. [15.84/15]

  def rating
    response = response_quality

    if response == 1 && @typos.zero?
      5
    elsif response == 2 && @typos.zero?
      4
    elsif response == 3 && @typos.zero?
      3
    elsif @typos.percent_of(@word_length) < 20
      2
    elsif @typos.percent_of(@word_length) < 30
      1
    elsif @typos.percent_of(@word_length) > 30
      0
    end
  end

What can I change?

What happens when @typos.percent_of(@word_length) is exactly 30?  You need to fill in the gaps.  The last case should be an else rather than an elsif, to ensure that all cases are covered.

Is it guaranteed that response_quality will be 1, 2, or 3?  If it's not obvious from context, then there should be an explanatory comment that other values are or aren't possible.

The solution below would reduce the code a bit, but it's not necessarily better than your original approach, which did have the advantage of regularity in the pattern.

def rating
  if @typos.zero?
    case response_quality
    when 1; return 5
    when 2; return 4
    when 3; return 3
    end
  end

  typo_pct = @typos.percent_of(@word_length)
  typo_pct < 20 ? 2 :
  typo_pct < 30 ? 1 :
                  0
end


Removing empty lines from a file The below code removes empty line from test.txt. I posted this answer on StackOverflow but someone commented that this code needs review and suggested me to post here. Please post your comments!

use strict;
use warnings;

my $file = "test.txt";
 open (FH, "+< $file");
my @lines = ();
my $i = 0;
while (<FH>)
{
    if ( m/^\s*$/ )
    {
        print "Found an empty line\n";
    }
    else
    {
        $lines[$i] = $_;
        $i++;
    }
}
close FH;
open (FH, "+> $file");
print FH @lines;
close FH;

Don't indent the open line differently to the surrounding lines.

Always check the result of open, or use autodie.

Why do you use the + modes to open the file? Just use plain < and > if you don't need to read and write, respectively.

Don't use bareword filehandles, use variables:

open my $fh, '<', $filename or die $!;

There's no need to use the variable $i, you can just push to the array.

If the input file is very large, your code could run out of memory. You don't need to store all the lines in an array, you can print them to a different file and rename it to the original name at the end.


Error handler for HomeKit app I have this universal error handler for my HomeKit app (currently in development). It grew out of wanting a single place to write error messages, show alerts, etc. I wanted a short, simple way to call it from anywhere that may generate an error.

Here's the class:

import UIKit
import HomeKit

class ESErrorHandler : NSObject
{
    class func handleError(error: NSError) -> Bool
    {
        NSLog("Handling error \(error) in centralized handler")

        let code = HMErrorCode(rawValue: error.code)

        switch code! {
        case HMErrorCode.FireDateInPast:
            showAlertWithTitle("Invalid date", body: "Date must be in the future")
        case HMErrorCode.AccessDenied:
            showAlertWithTitle("Access Denied", body: "You don't have permission to access the specified item")
        ... // A bunch more cases

        default:
            NSLog("Couldn't handle error with code \(error.code)")
            showAlertWithTitle("Error", body: "Couldn't successfully complete action")
            return false
        }

        return true
    }
    class func showAlertWithTitle(title: NSString, body: NSString)
    {
        dispatch_async(dispatch_get_main_queue())
        {
            let alert = UIAlertView(title: title, message: body, delegate: nil, cancelButtonTitle: "OK")
            alert.show()
        }
    }
}

I call it from other code like this:

homeManager.addHomeWithName(currentHouseName) {(home: HMHome!, error: NSError!) in
        if error != nil
        {
            ESErrorHandler.handleError(error)

            self.title = "Add Home"
        }
        else
        {
           ... //Stuff worked, do other stuff
        }
    }

My concerns:

Something feels wrong about putting using a class method for showAlertWithTitle:body:
I don't really like having using switch code!, it seems clumsy like I'm missing something in unwrapping

I don't really like having using switch code!, it seems clumsy like
  I'm missing something in unwrapping.

Yes, that will crash if the function is called with an error code
that does not correspond to one of the HMErrorCode enumeration values.
Better use optional binding with if let ...:

class func handleError(error: NSError) -> Bool
{
    if let code = HMErrorCode(rawValue: error.code) {
        switch code {
        case HMErrorCode.FireDateInPast:
            showAlertWithTitle("Invalid date", body: "Date must be in the future")
        case HMErrorCode.AccessDenied:
            showAlertWithTitle("Access Denied", body: "You don't have permission to access the specified item")
        // Other cases ...
        default:
            NSLog("Couldn't handle error with code \(error.code)")
            showAlertWithTitle("Error", body: "Couldn't successfully complete action")
            return false
        }
    } else {
        NSLog("Unknown error with code \(error.code)")
        showAlertWithTitle("Error", body: "Unknown error \(error.code)")
        return false
    }
    return true
}

It is also not obvious from your code why the function has a (boolean) return
value, as the caller ignores it.


Database access concept I've got a small tomcat web application that's used to receive customer data from an iPad app. After receiving those information they will be saved on a local MySQL database and then sent to an SAP system.

I'm focusing on my "concept" of handling database connections since this is my first ever productive web app that makes intense use of the database. I was wondering if there is something wrong in the way I talk with the database or if this might be prone to problems in future.

I have a class Database that creates the connection to the database and then provides a PreparedStatement through a method getPreparedStatement(String sql) that can be filled with the SQL query. It looks like this:

public class Database {
    private Connection connection = null;

    private Config config = ConfigurationUtils.config;

    public Database() {
        try {
            // Load the MySQL driver
            Class.forName("com.mysql.jdbc.Driver");

            // Create the database connection
            this.connection = DriverManager.getConnection("jdbc:mysql://" + config.getDatabase().getHost() + ":" + config.getDatabase().getPort() + "/" + config.getDatabase().getDatabaseName() + "?user=" + config.getDatabase().getUser() + "&password=" + config.getDatabase().getPassword());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public PreparedStatement getPreparedStatement(String sql) throws SQLException {
        return this.connection.prepareStatement(sql);
    }

    public void close() throws SQLException {
        this.connection.close();
    }
}

This Database class is used throughout the whole application (nearly every other class uses the Database class, some classes might even use them twice because multiple SQL queries have to be executed). The following example is the code of a method insertDealer() that's used to fill the database with master data about our dealerships:

    private void insertDealer(int dealer, String name1, String name2, String city, int zip) throws SQLException {
        String sql = "INSERT INTO dealer (id, name1, name2, city, zip) VALUES (?, ?, ?, ?, ?)";

        Database database = new Database();
        PreparedStatement preparedStatement = database.getPreparedStatement(sql);

        try {
            preparedStatement.setInt(1, dealer);
            preparedStatement.setString(2, name1);
            preparedStatement.setString(3, name2);
            preparedStatement.setString(4, city);
            preparedStatement.setInt(5, zip);

            preparedStatement.executeUpdate();
        } finally {
            preparedStatement.close();
            database.close();
        }
    }

Would this kind of Database access count as bad behaviour? Everything works actually like it should, I'm not experiencing any strange problems. Only thing I noticed that is when I stop my tomcat web app, I get this entry in the tomcat server log:

/webapp/statistics##1.0 appears to have started a thread named [MySQL Statement Cancellation Timer] but has failed to stop it. This is very likely to create a memory leak.

I don't like repeated calls like this config.getDatabase().getHost() + ":" + config.getDatabase().getPort() + .... config.GetDatabase() should be called once and stored in a local variable.

You will run into trouble when writing unit tests. Right now you have no way of mocking out the database access which forces you to run against a real MySql database which tends to make unit testing a pain in the backside.
Create an interface for Database against which all your business logic should work and inject it into the classes responsible for creating the database queries. Then during unit testing you can pass in a mock-implementation which simply checks that the methods are preparing the statements correctly without having to run a query against an actual database.


Parsing and calculating data At first I calculate the total of each data then give each data of data a percentage base on total. The percentage is calculated by adding its percentage and sum of all percentage before it. I then calculate a new value by multiplying percentage and total. I finally sort them from largest to smallest.

How can I reduce the number of .each loops?

var data= [
    {
        "data": [
            {
                "value": "5700",
                "color": "#1ca2d5",
                "label": "Shortlisted"
            },
            {
                "value": "2570",
                "color": "#ed5d67",
                "label": "Interviewed"
            },
            {
                "value": "1054",
                "color": "#66BB6A",
                "label": "Finallisted"
            }
        ],
        "under": "Top"
    },
{
    "data": [
        {
            "value": "2847",
            "color": "#1ca2d5",
            "label": "Shortlisted"
        },
        {
            "value": "3541",
            "color": "#ed5d67",
            "label": "Interviewed"
        },
        {
            "value": "1486",
            "color": "#66BB6A",
            "label": "Finallisted"
        }
    ],
    "under": "Mid"
},
{
    "data": [
        {
            "value": "16900",
            "color": "#1ca2d5",
            "label": "Shortlisted"
        },
        {
            "value": "12900",
            "color": "#ed5d67",
            "label": "Interviewed"
        },
        {
            "value": "5780",
            "color": "#66BB6A",
            "label": "Finallisted"
        }
    ],
    "under": "Dhsanullah University of Science and Technology"
},
{
    "data": [
        {
            "value": "5700",
            "color": "#1ca2d5",
            "label": "Shortlisted"
        },
        {
            "value": "2570",
            "color": "#ed5d67",
            "label": "Interviewed"
        },
        {
            "value": "1054",
            "color": "#66BB6A",
            "label": "Finallisted"
        }
    ],
    "under": "TTop"
},
    {
        "data": [
           {
               "value": "5700",
               "color": "#1ca2d5",
               "label": "Shortlisted"
           },
           {
               "value": "2570",
               "color": "#ed5d67",
               "label": "Interviewed"
           },
           {
               "value": "1054",
               "color": "#66BB6A",
               "label": "Finallisted"
           }
        ],
        "under": "Tcop"
    }
];

var tempData=[];
$.each(data, function (i, d) {
            var total = 0;
            $.each(d.data, function (j, obj) {
                total += parseInt(obj.value);
            });
            d["total"] = total.toString();
        });

        $.each(data, function (i, d) {
            var percentage = 0;
            $.each(d.data, function (j, obj) {
                obj["original"] = obj.value;
                var per = (parseInt(obj.value) * 100) / parseInt(d.total);
                percentage += per;
                obj["percentage"] = per;
                obj.value = (parseInt(d.total) * percentage) / 100;
                obj["under"] = d.under;
            });

        });
        $.each(data, function (i, d) {
            d.data.sort(function (a, b) { return b.value - a.value; });
            $.each(d.data, function (j, obj) {
                tempData.push(obj);
            });
        });

Yes, you can reduce the number of $.each(), and also simplify the whole code, in several ways.

Note that to improve readability I replaced some variable names:

. data replaced by source (to avoid confusing with data members in objects)

. d replaced by objSet inside of functions (more significant, as it is really a set of obj)

. all i and j replaced by index (more significant, and don't need to be different inside of different functions)

. tempData --> result

That's said, the first simplification come from the fact you don't need to iterate three times on the (now said) source, because there is no interaction between different objSets of source.
So instead of:

var tempData=[];
$.each(data, function (i, d) {
  // first step
});

$.each(data, function (i, d) {
  // second step
});

$.each(data, function (i, d) {
  // third step
});

it becomes:

var result=[];
$.each(source, function (index, objSet) {
  // first step
  // second step
  // third step
});

Then we can simplify each step...

Computing total can be achieved without $.each so this step:

var total = 0;
$.each(d.data, function (j, obj) {
    total += parseInt(obj.value);
});
d["total"] = total.toString();

can be achieved using reduce():

  objSet.total = objSet.data.reduce(function(total, obj) {
      return total + parseInt(obj.value);
  }, 0);

or even simpler in ES6:

objSet.total = objSet.data.reduce((total, obj) => total + parseInt(obj.value), 0);

The second step can use map() instead of $.each(), and also be slightly alleviated avoiding the temporary per variable. So rather than:

var percentage = 0;
$.each(d.data, function (j, obj) {
    obj["original"] = obj.value;
    var per = (parseInt(obj.value) * 100) / parseInt(d.total);
    percentage += per;
    obj["percentage"] = per;
    obj.value = (parseInt(d.total) * percentage) / 100;
    obj["under"] = d.under;
});

it can be:

  var percentage = 0;
  objSet.data = objSet.data.map(function (obj) {
    obj.original = obj.value;
    obj.percentage = (parseInt(obj.value) * 100) / parseInt(objSet.total);
    percentage += obj.percentage;
    obj.value = (parseInt(objSet.total) * percentage) / 100;
    obj.under = objSet.under;
    return obj;
  });

EDIT after realizing it could be yet more "compressed" using reduce() instead of map():

  objSet.data.reduce(function (percentage, obj) {
    obj.original = obj.value;
    obj.percentage = (parseInt(obj.value) * 100) / parseInt(objSet.total);
    percentage += obj.percentage;
    obj.value = (parseInt(objSet.total) * percentage) / 100;
    obj.under = objSet.under;
    return return percentage;
  }, 0);

It can be seen as a strange way using reduce(): it's usually employed to give a final result, while here it only avoids declaring a temporary variable for percentage.
Also strange, at first view, is that we keep getting a modified version of objSet.data without map(), because it is directly set.

Finally the third step can take advantage of concat to again avoid using $.each(), so this:

d.data.sort(function (a, b) { return b.value - a.value; });
$.each(d.data, function (j, obj) {
    tempData.push(obj);
});

becomes:

  objSet.data.sort(function (a, b) { return b.value - a.value; });
  result = result.concat(...objSet.data);

To summarize, the whole code can be reduced to this:

var result=[];
$.each(source, function (index, objSet) {

  objSet.total = objSet.data.reduce((total, obj) => total + parseInt(obj.value), 0);

  objSet.data.reduce(function (percentage, obj) {
    obj.original = obj.value;
    obj.percentage = (parseInt(obj.value) * 100) / parseInt(objSet.total);
    percentage += obj.percentage;
    obj.value = (parseInt(objSet.total) * percentage) / 100;
    obj.under = objSet.under;
    return return percentage;
  }, 0);

  objSet.data.sort(function (a, b) { return b.value - a.value; });
  result = result.concat(...objSet.data);
});


Counting athletes whose height is in a given range Challenge: Sort by growth

  Input

  The first line of each query contains one number n (1 ≤ n ≤ 20000) - the number of members in corresponding delegation.
  The second line contains n unsorted integers - the heights of athletes in centimeters.  Every height is at least 150 cm, and no greater than 250 cm.
  The third line contains a and b: the lower and upper height limits.

  Output

  For each query print the count on a separate line.

  Time limit: 2 seconds
  Memory limit: 256 MiB

My program works correctly, but I have a running time of 3020 ms.

 import java.io.PrintWriter;
 import java.util.Scanner;

 public class OlimpGames {
   public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
     PrintWriter out = new PrintWriter(System.out, true);
      {
       while (in.hasNextInt()) {
        int n = in.nextInt();

        int heigh[] = new int[100];
        for (int i = 0; i < n; i++) {
            heigh[in.nextInt() - 150]++;
        }
        int a = in.nextInt();
        int b = in.nextInt();

        int an = 0;
        for (int i = a - 150; i <= b - 150; i++) {
            an = an + heigh[i];
        }
        System.out.println(an);
       }
      }
  }
}

Your code and algorithm seem pretty optimal for the problem.

A minor nit-pick, the PrintWriter "out" is not being used.  (The braces immediately following are unnecessary, but wouldn't affect performance.)

There are only 2 tiny things which might help:

1) In each loop, you're allocating a new int[] array.  If you didn't do this, you would need to call Arrays.fill() to zero out the array each time.  I doubt there would be much difference as allocations of this type are pretty fast.

2) The bigger possibility is I/O performance on the output.  When fine-tuning to meet a time constraint, I/O might make the difference.  Instead of calling System.out.println, try using a StringBuilder and appending the output.  Then, after the main loop, do a single output operation on the resultant string.

Also, one final tip.  The linked problem statement indicates that none are less than 150 or greater than 250.  If you get input with 250, you will get an array bounds exception.  You need an array of size 101.


Parse and Query XML code optimisation I have stored in a XML file some Travel packages, each package has a code( the codes are stored in an array = $code).

I am using the below code to pull the requested data from the XML based on the given code. At this time I have copy/paste the code for each given code, but I have lots more and I dont want to copy paste code over and over

How can I simplify the below code so I don't have to paste it for all code that I have?

 <?php
    $code = array("BAS12", "BAS12", "BAS13", "BAS14");

    $dom    = new DOMDocument();
    $xpath  = new DOMXPath($dom);
    $reader = new XMLReader();
    $reader->open("file.xml");

    while ($reader->read()) {
        if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == 'Hotel') {
            $node = $dom->importNode($reader->expand(), true);
            $dom->appendChild($node);
            $nume1 = $xpath->evaluate('string(self::Hotel[HotelCode = "'.$code[0].'"]/HotelName)', $node);
            $tara1 = $xpath->evaluate('string(self::Hotel[HotelCode = "'.$code[0].'"]/Country)', $node);
            $oras1 = $xpath->evaluate('string(self::Hotel[HotelCode = "'.$code[0].'"]/City)', $node);
            $adresa1 = $xpath->evaluate('string(self::Hotel[HotelCode = "'.$code[0].'"]/Adress)', $node);
            $stele1 = $xpath->evaluate('string(self::Hotel[HotelCode = "'.$code[0].'"]/Stars)', $node);
            $masa1 = $xpath->evaluate('string(self::Hotel[HotelCode = "'.$code[0].'"]/TipMasa)', $node);
            $camera1 = $xpath->evaluate('string(self::Hotel[HotelCode = "'.$code[0].'"]/TipCamera)', $node);
            $pret1 = $xpath->evaluate('string(self::Hotel[HotelCode = "'.$code[0].'"]/Pret)', $node);
            $descriere1 = $xpath->evaluate('string(self::Hotel[HotelCode = "'.$code[0].'"]/Descriere)', $node);
            $persoane1 = $xpath->evaluate('string(self::Hotel[HotelCode = "'.$code[0].'"]/Persoane)', $node);
            $img1 = $xpath->evaluate('string(self::Hotel[HotelCode = "'.$code[0].'"]/Imagine1)', $node);
              $dom->removeChild($node);
            if ($nume1) {
                 break; }
            if ($tara1) {
                 break; }
            if ($oras1) {
                 break; }
             if ($adresa1) {
                 break; }
                  if ($stele1) {
                 break; }
                  if ($camera1) {
                 break; }
                  if ($masa1) {
                 break; }
                  if ($pret1) {
                 break; }
                  if ($persoane1) {
                 break; }
                   if ($img1) {
                 break; }

        }
    }
    ?>

Why don't you read the code into a variable and check if it is in the array of codes? After that fill up an array with the results.

Nearly every time you have a variable with a counter in the name, you should use arrays.

Another optimization is using XMLReader::next() to go directly to the next sibling without reading the child nodes.

You don't need to append the imported node, you need to provide it as a context in the DOMXpath::evaluate() calls anyway.

$codes = ["BAS11", "BAS12", "BAS13", "BAS14"];
$hotels = [];

$dom = new DOMDocument();
$xpath = new DOMXPath($dom);

$reader = new XMLReader();
$reader->open($filename);

// look for the first Hotel element (include descendants)
while ($reader->read() && $reader->localName !== 'Hotel') {
  continue;
}

// while you have an Hotel element
while ($reader->localName === 'Hotel') {
  // no need to append the node to the document, just import it
  $node = $dom->importNode($reader->expand(), true);
  // read the HotelCode
  $code = $xpath->evaluate('normalize-space(./HotelCode)', $node);
  // if it is in the list
  if (in_array($code, $codes)) {
    // read the values
    $hotels[$code] = [
      'name' => $xpath->evaluate('string(./HotelName)', $node),
      'country' => $xpath->evaluate('string(./Country)', $node),
      //...
    ];
  }
  // move directly to the next Hotel sibling
  $reader->next('Hotel');
}

var_dump($hotels);


All these lines are really weird:
a4  =   9.230769230769231e-01  #  12/13

Unless you have a good reason (which I'd then state in the code as a comment) to do that, just write a4 = 12/13 instead.
Gonna be the same anyway:
>>> import dis
>>> dis.dis('a4 = 12/13')
  1           0 LOAD_CONST               0 (0.9230769230769231)
              2 STORE_NAME               0 (a4)
              4 LOAD_CONST               1 (None)
              6 RETURN_VALUE
>>> dis.dis('a4 = 9.230769230769231e-01')
  1           0 LOAD_CONST               0 (0.9230769230769231)
              2 STORE_NAME               0 (a4)
              4 LOAD_CONST               1 (None)
              6 RETURN_VALUE

This line for example is not right:
b51 =   2.032407407407407e+00  #  439/216

The values differ slightly, your value being less accurate:
>>> 2.032407407407407e+00
2.032407407407407
>>> 439/216
2.0324074074074074


Boggle solver - Updated (with Trie) Here is an update to my previous boggle solver. The new version has an almost instantaneous output, removing the need to limit the length of words being searched for.

Any comments on my general coding implementation or layout would be appreciated.

"""Boggle game solver"""

class Trie:
    """Trie class to speed up programme by stopping exploration of non-existent prefixes"""

    def __init__(self):
        """Initialise the trie"""
        self.child = {}

    def insert(self, word):
        """Insert word into trie"""
        current = self.child
        for char in word:
            if char not in current:
                current[char] = {}
            current = current[char]

    def search(self, word):
        """Search the trie"""
        current = self.child
        for char in word:
            if char not in current:
                return False
            current = current[char]
        return True

def words_from(board, row, column, running_string, list_words):
    """Calculate all possible words from a given starting position [row, column]"""
    if row in (4, -1) or column in (4, -1):
        return
    # Search the Trie
    if not trie.search(running_string):
        return
    if board[row][column] != "-":
        new_string = running_string + board[row][column]
        board[row][column] = "-"
        # Add new word
        if len(new_string) >= 3:
            list_words.append(new_string.lower())
        # Find next word
        next_move = [
            (1, 1),
            (-1, -1),
            (1, -1),
            (-1, 1),
            (1, 0),
            (0, 1),
            (-1, 0),
            (0, -1),
        ]
        for dx, dy in next_move:
            words_from(board, row + dx, column + dy, new_string, list_words)
        board[row][column] = new_string[-1]
        return list_words

def get_permutations(board):
    """Get all permutations """
    set_permutations = set()
    # Build Trie
    global trie
    trie = Trie()
    with open("en-dict.txt", "r", encoding="utf8") as file:
        for line in file:
            trie.insert(line)
    #Search for words from each starting position
    for row in range(4):
        for column in range(4):
            # Search for words
            words = words_from(board, row, column, running_string="", list_words=[])
            if words:
                for word in words:
                    set_permutations.add(word)
            words = None
    return sorted(list(set_permutations))

def dictionary_check(set_permuations):
    """Check set_permutations for valid English words"""
    dictionary = {}
    with open("en-dict.txt", "r", encoding="utf8") as file:
        for line in file:
            dictionary[line.strip()] = 0

    counter = 0
    for word in set_permuations:
        if word.lower() in dictionary:
            counter += 1
            print(word)
    print(f"======\n{counter} words")

def find_words(board):
    """Find words on the boggle board"""
    set_permutations = get_permutations(board)
    print("\nPerforming dictionary check....")
    dictionary_check(set_permutations)

def build_board(string):
    """Build board from string"""
    if len(string) != 16:
        print("Error. Must enter 4*4 grid (16 characters)")
        return
    board = [[*string[0:4]], [*string[4:8]], [*string[8:12]], [*string[12:16]]]
    find_words(board)

if __name__ == "__main__":
    string_board = "playthiswordgame"
    build_board(string_board)

Rather than a global Trie object I would make a TrieNode object that inherits from dict.

This can inherit from dict.
This can overload __missing__ to simplify insert.
Search isn't needed in the way I used it.

Make a function build_trie, that builds and populates the trie from "en-dict.txt".
build_board should really be called main and the code that builds the board should be moved into it's own function.
find_words should be moved into main.
build_trie should be called from main and passed to where it needs to be used.

When possible don't use global. If you think it's impossible, then you're likely wrong.

In this case you can move the value out of the function into main and pass it where it's needed.

In other cases using a class, a closure or other ways can solve the issue.
dictionary_check should only display words. And the first part of the function can be removed with the new TrieNode.
Move next_move out of words_from, just make it a global constant.
board[row][column] != "-" relies on mutating board I would recommend not mutating input to functions.
words_from can be simplified by passing the current node, as then you're not trie.searching.
In words_from len(new_string) >= 3 is an artificial limitation, that should be moved out of the function.
get_permutations can be simplified by changing words_from to return an empty list on bad input.
I would move set_permutations out of get_permutations, as it doesn't have much purpose in that function.
I merged words_from and set_permutations to use a while loop, I found this to make the code easier to understand and make.
Don't print and return, use raise.

For the most part your code seems to be fairly good, there's some pitfalls your falling down. But on a micro - line by line - scale your code is pretty good.

The problem I saw with your code is not seeing the big picture, and not following SRP.

Improving SRP should be fairly easy for you. If you're thinking of putting a comment that says the code is performing a different task, move it into a function.

To help with the big picture, when you follow SRP think to yourself if you should split the code into 2/3 functions and should call the three functions in succession. Like I changed main to do.

class TrieNode(dict):
    def __init__(self, value=None):
        super().__init__()
        self.value = value

    def __missing__(self, key):
        value = TrieNode()
        self[key] = value
        return value

    def insert(self, word):
        current = self
        for char in word:
            current = current[char]
        current.value = word

def build_trie():
    trie = TrieNode()
    with open("en-dict.txt", "r", encoding="utf8") as file:
        for line in file:
            trie.insert(line.strip())
    return trie

def board_from_string(string):
    if len(string) != 16:
        raise ValueError("Must enter 4*4 grid (16 characters)")
    return [
        [*string[0:4]],
        [*string[4:8]],
        [*string[8:12]],
        [*string[12:16]]
    ]

AROUND = [
    (dx, dy)
    for dx in range(-1, 2)
    for dy in range(-1, 2)
    if not (dx == dy == 0)
]

def get_words(trie, board):
    stack = []
    for row in range(4):
        for column in range(4):
            stack.append((trie, row, column, set()))
    while stack:
        node, row, column, parents = stack.pop()
        if row in (4, -1) or column in (4, -1) or (row, column) in parents:
            continue
        char = board[row][column]
        if char not in node:
            continue
        node = node[char]
        if node.value is not None:
            yield node.value
        for dx, dy in AROUND:
            stack.append((node, row + dx, column + dy, parents | {(row, column)}))

def display(words):
    words = sorted({w for w in words if len(w) >= 3})
    print('\n'.join(words))
    print(f"======\n{len(words)} words")

def main():
    string = "playthiswordgame"
    trie = build_trie()
    board = board_from_string(string)
    words = get_words(trie, board)
    display(words)

if __name__ == "__main__":
    main()


Multithreaded horse race simulation I'm just starting to get along with threads in Java and I need some code review,
about interfaces, classes, managing threads, correct writing, secure threads, exposed objects etc.

Horse-Race Implementation:

package HorseRace;

public interface FinishingLine {

    public void arrive(Horse h);
}

package HorseRace;

import java.util.Vector;

public class FinishingLineImpl implements FinishingLine {

    Vector<String> Ranking = new Vector<String>(10);

    public FinishingLineImpl(){}
    @Override
    public void arrive(Horse h) {
        // TODO Auto-generated method stub
        Ranking.add(h.getId());
    }

    public void print()
    {
        for (int i = 0; i < 10 ; i++)
        {
            System.out.println("Place " + (i + 1)+ " is : " + Ranking.get(i));
        }
    }
}

package HorseRace;

public interface Horse {

    public void run();
    public String getId();
}

package HorseRace;
import java.util.Random;

public class HorseImpl implements Horse,Runnable {

    int _distance;
    String _id;
    FinishingLine _f;

    public HorseImpl(String id, FinishingLine f)
    {
        _id = id;
        _distance = 0;
        _f = f;
    }
    @Override
    public void run() {
        // TODO Auto-generated method stub
        Random rand = new Random();

        int n = rand.nextInt(10) + 1;

        while (_distance < 100)
        {
            try
            {
                Thread.sleep(n);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            _distance += 1;
        }

            _f.arrive(this);
    }

    @Override
    public String getId() {
        // TODO Auto-generated method stud
        return _id;
    }
}

package HorseRace;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class Main {

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub

        ExecutorService e = Executors.newFixedThreadPool(10);
        FinishingLineImpl f = new FinishingLineImpl();

        for (int i = 0; i<10 ; i++)
        {
            System.out.println("Creating a horse with id = " + (i+1));
            String strI = Integer.toString(i+1);
            HorseImpl h = new HorseImpl(strI,f);
            e.execute(h);
        }

        e.shutdown();
        e.awaitTermination(60000, TimeUnit.SECONDS);

        f.print();
    }
}

// package name should be lower case
package HorseRace;

public interface FinishingLine {

    public void arrive(Horse h);
}

package HorseRace;

import java.util.Vector;

public class FinishingLineImpl implements FinishingLine {

    // Vector is discouraged, unless you are using an ancient version of java
    // use Collections.synchronizedList(new ArrayList()) instead
    // variable should start with a lowercase letter, i.e. camelCase
    Vector<String> Ranking = new Vector<String>(10);

    public FinishingLineImpl(){}
    @Override
    public void arrive(Horse h) {
        // TODO Auto-generated method stub
        Ranking.add(h.getId());
    }

    // override toString instead
    public void print()
    {
        // magic numbers are evil; use ranking.size() instead
        for (int i = 0; i < 10 ; i++)
        {
            System.out.println("Place " + (i + 1)+ " is : " + Ranking.get(i));
        }
    }
}

package HorseRace;

public interface Horse {

    public void run();
    // change String to Object so you can use anything for the ID
    public String getId();
}

package HorseRace;
import java.util.Random;

public class HorseImpl implements Horse,Runnable {

    // see previous comment regarding camelCase
    int _distance;
    String _id;
    FinishingLine _f;

    public HorseImpl(String id, FinishingLine f)
    {
        _id = id;
        _distance = 0;
        _f = f;
    }
    @Override
    public void run() {
        // TODO Auto-generated method stub
        Random rand = new Random();

        int n = rand.nextInt(10) + 1;

        while (_distance < 100)
        {
            try
            {
                // why sleep?
                // why not create and increment a timeElapsed instance variable?
                // preferrably an AtomicInteger
                // then calculate ranking after the race, e.g. sort by elapsed time
                Thread.sleep(n);
            }
            catch (Exception e)
            {
                e.printStackTrace();
                // break out?
            }
            _distance += 1;
        }

            _f.arrive(this);
    }

    @Override
    public String getId() {
        return _id;
    }
}

package HorseRace;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class Main {

    public static void main(String[] args) throws InterruptedException {
        // I suggest calculating nThreads as cpu count * 2
        // @see Runtime.getRuntime().availableProcessors();
        // please use descriptive variable names
        ExecutorService e = Executors.newFixedThreadPool(10);
        FinishingLineImpl f = new FinishingLineImpl();

        // another magic number
        for (int i = 0; i<10 ; i++)
        {
            System.out.println("Creating a horse with id = " + (i+1));
            // see comment regarding id type
            String strI = Integer.toString(i+1);
            HorseImpl h = new HorseImpl(strI,f);
            e.execute(h);
        }

        e.shutdown();
        // 16 hours? really?
        e.awaitTermination(60000, TimeUnit.SECONDS);

        // change to System.out.println(f);
        f.print();
    }
}


Processing compressed .csv files I have some .csv files compressed in .bz2 format. I need to take a subset of the records (and data) and switch to .gz.

I am not happy with the performance. Is there a more efficient way to do it?

//For each file in a folder:
try (BufferedReader br = new BufferedReader(new InputStreamReader(
        new BZip2CompressorInputStream(new FileInputStream(fileIn))));
     BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
        new GZIPOutputStream(new FileOutputStream(fileOut))))) {

    String line;
    while ((line = br.readLine()) != null) {
        String[] parts = line.split(',');
        if(isLineToSkip(parts[0])) {
            continue;
        }
        String outLine = parts[0] + "," + parts[3];

        writer.append(outLine);
        writer.newLine();
    }
} . . .

Well, your code is neat, and you use the Try-with-resources structures well. There is one potential bug, which is that there may be lines whihc have a 'successful' part[0], but no part[3] which would cause an IndexOutOfBoundsException.

As for the performance, the key here is Amdahl's Law.... essentially parallelization.

You have five CPU intensive parts to your problem:

decompression
Stream->Reader
Split & Filter
Writer->Stream
Compression

Assuming the transformations that you do in each of these are similarly expensive to do computationally, then you can probably make your system go 5 times faster by doing them each in parallel. Five threads:

one of them reads the data from the file, and decompresses that in to chunks of bytes which it feeds in to a queue.
the second takes chunks from the queue, and decodes it in to characters (UTF-8?), which it puts in to a char chunk queue
the third takes char chunks, identifies and splits the lines, and filters what's junk.
the fourth encodes the chars back to a byte stream, in chunks which it places on a queue
the fifth compresses the byte chunks back to disk

This is going to net you a potential 5X improvement.

That's pretty complicated though.

What would be much simpler is to use a different axis to process the parallelism. Your comments indicate that you have multiple files to process.... you should do them each in multiple threads...

Consider the following structure:

private static final boolean processFile(final File fileIn) throws IOException {
    //For each file in a folder:
    try (BufferedReader br = new BufferedReader(new InputStreamReader(
            new BZip2CompressorInputStream(new FileInputStream(fileIn))));
         BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
            new GZIPOutputStream(new FileOutputStream(fileOut))))) {

        String line;
        while ((line = br.readLine()) != null) {
            String[] parts = line.split(',');
            if(isLineToSkip(parts[0])) {
                continue;
            }
            String outLine = parts[0] + "," + parts[3];

            writer.append(outLine);
            writer.newLine();
        }
    }
    return true;
}

.......

ExecutorService service = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
List<Future<Boolean>> queued = new LinkedList<>();
for (final File toprocess : ....... ) {
    queued.add(service.submit(new Callable () {
        public Boolean call() throws IOException {
            return processFile(toprocess);
        }
    }));
}

for (Future<Boolean> future : queueud) {
    future.get();
}

With the above code, you will do one file in each thread, and the system CPU will run at 100% ... no matter how many CPU's you have. Essentially you are using your system fully.


Basic OOP Poker - Deck, Cards and Hands I decided it would be interesting to simulate a game of Poker. Baby steps at the moment, and eventually I'll attempt to turn it into a GUI. The code I have so far is very basic such as populating a deck, shuffling the deck, and distributing two cards to a player. Trying to understand how inheritance works, the proper use of Java generics, and grasping the concept of how powerful/fun OOP can be. The Collections Framework is also something I'm beginning to experiment with.

I would really appreciate anything that will help me better understand Java.

import java.util.*;

class Deck {
    // create possible card combinations
    public final String[] SUITS = { "H", "D", "C", "S" };
    public final String[] RANKS = { "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3", "2" };

    // maximum number of cards
    public final int deckLength = SUITS.length * RANKS.length;
    public List<String> fullDeck = new ArrayList<>();

    public Deck() {
        for(int i = 0; i < SUITS.length; i++) {
            for(int j = 0; j < RANKS.length; j++) {
                fullDeck.add(RANKS[j] + SUITS[i]);
            }
        }
   }

    public Deck(List<String> fullDeck) {
        this.fullDeck = fullDeck;
        for(int i = 0; i < SUITS.length; i++) {
            for(int j = 0; j < RANKS.length; j++) {
                fullDeck.add(RANKS[j] + SUITS[i]);
            }
        }
    }

    public List<String> shuffle(List<String> fullDeck) {
        this.fullDeck = fullDeck;
        Collections.shuffle(fullDeck);
        return fullDeck;
    }

    // this was mainly used for testing purposes
    // to ensure shuffling was indeed taking place
    public void showDeck(List<String> fullDeck) {
        this.fullDeck = fullDeck;
        for(int i = 0; i < deckLength; i++) {
            System.out.printf("%s ",fullDeck.get(i));
        }
    }
}

class Hands extends Deck {

    public String[] hand = new String[2];
    Random random = new Random();

    // select 2 cards to distribute to player
    public String[] getHand(List<String> fullDeck) {
        super.fullDeck = fullDeck;
        for(int i = 0; i < this.hand.length; i++) {
            this.hand[i] = fullDeck.get(random.nextInt(super.deckLength));
        }
        return this.hand;
    }

    // show player hand
    public void showHand() {
        for(int i = 0; i < this.hand.length; i++) {
            System.out.printf("%s ", this.hand[i]);

        }
    }
}

public class Cards {
    public static void main(String[] args) {

        List<String> cards = new ArrayList<>();
        Deck deck = new Deck(cards);
        deck.shuffle(cards);

        Hands hands = new Hands();
        hands.getHand(cards);
        hands.showHand();

    }
}

Welcome to Code Review, and welcome to Java! Overall your code is clean and easy to understand, so thank you for that.

Enums

Storing these values in a string array is not very extensible:

public final String[] SUITS = { "H", "D", "C", "S" };
public final String[] RANKS = { "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3", "2" }

The first thing to note is that a List provides more options and flexibility than a raw array.  However, I think it would be best to store these values as an Enum:

public enum SUITS {
    H, D, C, S
}

This will allow you to use these values in a switch statement, and provides other benefits.  Most of all, it is more readable and more extensible.

Variable Names

I would strongly recommend against single letter variable names outside of simple constructs such as for (int i = 0; i < something; i++);.  Instead of H, D, C, and S, I would just spell them out as HEARTS, DIAMONDS, etc.  There is no disadvantage to using the extra characters to make the code more readable. In the rest of your code you do not make this mistake, so that is good.

Improper OOP

I do not think that Hand should inherit from Deck:

class Hands extends Deck {

I would advise that you think about inheritance as an "is a" relationship.  A hand is not a deck, and should not be treated like one. For example, it is unlikely that you will need to "shuffle" a hand.  To me, this is the same as having Hand or Deck inherit from a Card.  It is not going to end up being helpful to the organization or understanding of the code.

I don't understand why Cards is a class:

public class Cards {

A singular Card should be the class here.  Then, the Hand and Deck can have a "has a" relationship with cards, and use Card objects.  The way you have this set up here:

    List<String> cards = new ArrayList<>();
    Deck deck = new Deck(cards);
    deck.shuffle(cards);

    Hands hands = new Hands();
    hands.getHand(cards);
    hands.showHand();

really does not make a whole lot of sense.  Why would a Cards object have Decks or Hands as objects?  It should definitely be the other way around.

I look forward to seeing a revised version of your code in another question!


Apple picking game. More efficient method than using global variables I have been working on a small game called apple picker, it's text-based, and relvolves around picking and selling apples. Unfortunately I've been using global variables for this whole thing and it's starting to get a bit ugly looking. Is there a more efficient way to do this than using global variables?

from random import randint
import console, time

global gold
global apples
apples = 1
gold = 1

prompt = "> "

def main():
    global gold
    global apples
    console.clear()
    print "Gold: %r Apples: %r" % (gold, apples)
    print "Pick an apple?"
    choice = raw_input(prompt)
    if choice == "yes":
        pick()
    elif choice == "no":
        console.clear()
        global gold
        global apples
        print "Apples: %r Gold: %r" % (apples, gold)
        print "Sell apples?"
        sell = raw_input(prompt)
        if sell == "yes" and apples >= 1:
            global gold
            global apples
            apple_sales = randint(1,5)
            gold = (gold * (apples / 2)) * apple_sales
            apples = apples - apples
            if gold >= 25 ** 25:
                console.clear()
                print "\t\t\tYou won!"
                print "Congrats on controlling the apple market!"
            else:
                main()
        elif sell == "yes" and apples <= 0:
            print "\nNot enough apples!"
            time.sleep(0.7)
            main()
        elif sell == "no":
            main()
        else:
            main()
    elif choice == "exit":
        console.clear()
        print "Bye..."
    else:
        main()

def pick():
    console.clear()
    global gold
    global apples
    print "Type 0 to exit. How many?"
    print "Apples: %r Gold %r" % (apples, gold)
    try:
        apple_num = int(raw_input(prompt))
        if apple_num == 3 or apple_num == 2 or apple_num == 1:
            global apples
            apples = apples + apple_num
            time.sleep(0.5)
            pick()
        elif apple_num >= 4:
            console.clear()
            print "You can't haul that many apples!"
            time.sleep(0.5)
            pick()
        elif apple_num == 0:
            main()
    except ValueError:
        pick()

main()

Ignore the console module. I wrote this script on IOS.

You don't need to list those as globals. Variables defined at the module-level namespace are available in functions in that module. Just remove the global declarations and you should be fine.

Another thing: calling main() is kind of dangerous. It's always better to do this instead:

if __name__ == '__main__':
  main()

That way main() won't be run when the module is imported.


lodash filter array of objects by array of exclude property values I ran into a scenario, I'll simplify the data to user/project, where I needed to take values like:

users: [
  {name: 'bob', project: 1},
  {name: 'sam', project: 2},
  {name: 'ted', project: 3},
];

and given an array of project ids I wish to exclude such as

excludeProjects: [1,3];

I want to return [{name: 'sam', project: 2}];

I was able to do this with a custom filter function:

let usersNotInProject = _.filter(users, function(o) {
    for (var i=0; i<excludeProjects.length; i++){
        if (excludeProjects[i]===o.project){
            return false;
        }
    }
    return true;
});

However, I believe I am missing some more elegant way to do this with lodash. Is a custom function really needed for this scenario?

You can do it with filter:

let users = [
  {name: 'bob', project: 1},
  {name: 'sam', project: 2},
  {name: 'ted', project: 3},
];
let excludeProjects = [1,3];
_.filter(users, (v) => _.indexOf(excludeProjects, v.project) === -1) // assign it to some variable
// Returns [ { name: 'sam', project: 2 } ]

OR other way of doing it is suggested in comments, this is more elegant:

_.filter(users, (v) => !_.includes(excludeProjects, v.project));
// Returns [ { name: 'sam', project: 2 } ]


How to optimize this validation method? Looking to optimize this method so it will run quicker, but can't seem to find anything.  WOrking in .NET 3.5.  ExcludedWords and NewCodes are both HashSet of strings.

    private bool isValid(String code)
    {
        String pattern = "[a-zA-Z]{3}";

        if (chkLessThan2Letters.Checked && Regex.IsMatch(code, pattern))
        {
            return false;
        }

        foreach (string s in excludedWords)
        {
            if (code.Contains(s))
            {
                return false;
            }
        }

        if (newCodes.Contains(code))
        {
            return false;
        }

        return true;
    }

Before you optimize this, make sure that it actually needs optimization and that you're not wasting your time. Have you checked that this is a performance bottleneck in your application?

One way to optimize this is to store your excluded words in a datastructure like a Trie. This will let you see if some word in in the trie potentially faster than iterating through a list.