Java game development image animation I was wondering if someone could look at my animation for me to see if I am doing it correctly.  I am currently programming a basic game in Java, nothing fancy though, and I just want to get the basics down before venturing into something more complex.

Right now, I want to animate a character's attack.  When the player left clicks, the character should go from looking like this:

To looking like this:

Currently, I am doing this by starting this thread when a left-click happens:

public class thread implements Runnable
{
    long swordTime = 250;
    public void run()
    {
        try {
            player.setAttacking(true);
            Thread.sleep(swordTime);
            player.setAttacking(false);
            Thread.yield();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

Which changes a boolean variable attacking.  Afterwards, when the character is rendered again, attacking will be true, so the variable will be rendered as the image with this code:

public void render(Graphics g)
{

    if (attacking)
    {
        ImageIcon i = new ImageIcon("/Users/myName/Desktop/FarthornPics/charAttacking.png");
        Image attckImg = i.getImage();
        g.drawImage(attckImg, (int)Game.WIDTH/3, (int)y, null);
    }
    else
        g.drawImage(charImage, Game.WIDTH/3, (int)y, null);

Although this method works, I was wondering if there was a better way to accomplish this.  People often tell me that multi-threading is kind of a crude way to go about things, so I was looking for a better alternative (if this is not already the best).

swordTime appears to be a constant and should therefore be:

private static final int SWORD_TIME = 250;

ImageIcon i = new ImageIcon("/Users/myName/Desktop/FarthornPics/charAttacking.png");

Creating a new ImageIcon each time it is rendered is a big fat waste of resources. Create it once, outside the render method, and only refer to it from inside - just as you have done with charImage.

Additionally, as I doubt that path exists on all computers, I would recommend using a relative path or use classpath resources (possibly with the Maven tool).

Thread.yield();

I have to admit that this is the first time ever that I have seen that method being called. When reading the documentation for this method, it seems there is a good reason for why I have never encountered it before.

  It is rarely appropriate to use this method. It may be useful for debugging or testing purposes, where it may help to reproduce bugs due to race conditions. It may also be useful when designing concurrency control constructs such as the ones in the java.util.concurrent.locks package.

I strongly doubt calling this method is any useful for you.

When doing animations, it is helpful if you know the platform you are doing them on. I have to say that Swing is not the most helpful platform to do animations on. There are a couple of tutorials out there for how to do it. What I am mostly noticing in your code is that you don't seem to call repaint or anything like it anywhere, which makes me a bit surprised that this actually works at all.

If you are developing with Java 8, I think you would benefit from using JavaFX which has better support for animations.

If you are interested in developing games with Java, I would strongly recommend using the framework LibGDX.


Extract lines under the specified heading I wrote the following working code, which I just can't stomach:

var data = [
    { id: 0, isHeading: true, description: "A heading" },
    { id: 1, isHeading: false, description: "Blah 1" },
    { id: 2, isHeading: false, description: "Blah 2" },
    { id: 3, isHeading: true, description: "A heading" },
    { id: 4, isHeading: false, description: "Blah 3" },
    { id: 5, isHeading: false, description: "Blah 4" },
    { id: 7, isHeading: false, description: "Blah 5" },
    { id: 8, isHeading: false, description: "Blah 6" },
    { id: 9, isHeading: true, description: "A heading" },
    { id: 10, isHeading: false, description: "Blah 7" },
    { id: 11, isHeading: false, description: "Blah 8" },
    { id: 12, isHeading: false, description: "Blah 9" },
    { id: 13, isHeading: true, description: "A heading" },
    { id: 14, isHeading: false, description: "Blah 10" },
    { id: 15, isHeading: false, description: "Blah 11" },
    { id: 16, isHeading: true, description: "A heading" },
    { id: 17, isHeading: false, description: "Blah 12" },
    { id: 18, isHeading: false, description: "Blah 13" },
    { id: 19, isHeading: false, description: "Blah 14" }
  ]
  function getLines (headingId) {
    var r = []
    var inThere = false
    for (let line of data) {
      if (line.isHeading) {
        if(line.id === headingId) {
          inThere = true
          continue
        } else if (line.id !== headingId && inThere) {
          return r
        }
      }
      if (inThere) r.push(line)
    }
    return r
  }
  getLines(9)

Basically, it's taking a chunk of a list; it's a state automaton where the inThere state is set by a matching heading ID, and the routine is done when either there are no more items, or a new heading comes up.

Is there a much better way of writing this?

Review

Your function skips until the heading is found and than takes element until the next heading. Perhaps we could split these methods up to make them reusable.
Try to avoid var (undesired scope), prefer let or const instead.
Prefer a Tree data structure over flat list for hierarchical data.

Refactored for Reusability

Skip items while a predicate is true.

  function skipWhile(items, predicate) {
    let ok = false;
    return items.filter((value, index, array) => {
      if (!ok && !predicate(value)){
        ok = true;
      }
      return ok;
    });
  };

Take items while a predicate is true.

  function takeWhile(items, predicate) {
    let ok = true;
    return items.filter((value, index, array) => {
      if (ok && !predicate(value)){
        ok = false;
      }
      return ok;
    });
  };

The OP method:

 function getLines (headingId) {
    let lines = skipWhile(data, v => v.id != headingId);
    lines.shift(); // we don't want the heading
    lines = takeWhile(lines, v => !v.isHeading);
    return lines;
  }

And verification:

  console.log(getLines(9).map(x => x.description))

Yielding:

  (3) [ "Blah 7" , "Blah 8" , "Blah 9" ]

You could also create these methods as prototype extensions on Array or refactor them to become generator functions (with yield syntax). I am sure these methods could get optimized for performance somehow.


Store procedure timing out sometimes Can someone please find out why this store procedure is timing out?

ALTER PROCEDURE [dbo].[Insertorupdatedevicecatalog]
                (@OS                NVARCHAR(50)
                ,@UniqueID          VARCHAR(500)
                ,@Longitude         FLOAT
                ,@Latitude          FLOAT
                ,@Culture           VARCHAR(10)
                ,@Other             NVARCHAR(200)
                ,@IPAddress         VARCHAR(50)
                ,@NativeDeviceID    VARCHAR(50))
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @TranCount INT;
    SET @TranCount = @@TRANCOUNT;

    DECLARE @OldUniqueID VARCHAR(500) = ''-1'';
    SELECT @OldUniqueID = [UniqueID] FROM DeviceCatalog WHERE (@NativeDeviceID != '''' AND [NativeDeviceID] = @NativeDeviceID);

    BEGIN TRY
        IF @TranCount = 0
            BEGIN TRANSACTION
        ELSE
            SAVE TRANSACTION Insertorupdatedevicecatalog;

        DECLARE @Geo GEOGRAPHY = geography::STGeomFromText(''POINT('' + CONVERT(VARCHAR(100), @Longitude) + '' '' + CONVERT(VARCHAR(100), @Latitude) + '')'', 4326);

        IF EXISTS(SELECT 1 FROM DeviceCatalog WHERE [UniqueID] = @UniqueID)
        BEGIN
            DECLARE @OldGeo     GEOGRAPHY
                    ,@OldCity       NVARCHAR(100)
                    ,@OldCountry    NVARCHAR(100)
                    ,@OldAddress    NVARCHAR(100);

            SELECT  @OldGeo = [LastUpdatedLocationFromJob]
                    ,@OldCity = [City]
                    ,@OldCountry = [Country]
                    ,@OldAddress = [Address]
            FROM    DeviceCatalog
            WHERE   [UniqueID] = @UniqueID;

            UPDATE  DeviceCatalog
                SET [OS] = @OS
                    ,[Location] = @Geo
                    ,[Culture] = @Culture
                    ,[Other] = @Other
                    ,[IPAddress] = @IPAddress
            WHERE   [UniqueID] = @UniqueID;

                            IF (@OldGeo IS NULL OR @OldAddress IS NULL OR @OldCity IS NULL OR @OldCountry IS NULL OR ISNULL(@Geo.STDistance(@OldGeo) / 1000,0) > 50)
            BEGIN
                UPDATE  DeviceCatalog
                    SET [Lastmodifieddate] = Getdate()
                WHERE   [UniqueID] = @UniqueID;
            END

        END
        ELSE
        BEGIN
            INSERT INTO DeviceCatalog
                        ([OS]
                        ,[UniqueID]
                        ,[Location]
                        ,[Culture]
                        ,[Other]
                        ,[IPAddress]
                        ,[NativeDeviceID])
                VALUES  (@OS
                        ,@UniqueID
                        ,@Geo
                        ,@Culture
                        ,@Other
                        ,@IPAddress
                        ,@NativeDeviceID);
                IF(@OldUniqueID != ''-1'' AND @OldUniqueID != @UniqueID)
                BEGIN
                    EXEC DeleteOldAndroidDeviceID @OldUniqueID, @UniqueID;
                END
        END
LBEXIT:
        IF @TranCount = 0
            COMMIT;

    END TRY
    BEGIN CATCH
        DECLARE @Error INT, @Message VARCHAR(4000), @XState INT;
        SELECT  @Error = ERROR_NUMBER() ,@Message = ERROR_MESSAGE() ,@XState = XACT_STATE();

        IF @XState = -1
            ROLLBACK;
        IF @XState = 1 AND @TranCount = 0
            rollback
        IF @XState = 1 AND @TranCount > 0
            ROLLBACK TRANSACTION Insertorupdatedevicecatalog;

        RAISERROR (''Insertorupdatedevicecatalog: %d: %s'', 16, 1, @error, @message) ;
    END CATCH
END

It's impossible to say what the problem is without knowing how the data is structured, how many rows are in each table, what the indexes are, and other information. Even then, the problem may not even be with this query but with some other query that causes problems for the database as a whole. Basically, your question is far too narrow.

That said, you could try a few things:

Try running this with Explain Plan turned on. That should tell you what part of the query is taking the longest.
Run SET STATISTICS IO ON; and then your query. That will give you an idea if the I/O usage is high.
If the above don't suggest improvement, check the overall DB for problems. Glenn Berry's SQL diagnostics queries are a good place to start.
If you still can't figure it out, hire someone like Glenn to help you. Or start reading a lot of books on SQL Server optimization.

(disclaimer: Glenn's a friend and former colleague of mine, but he is really good at this stuff. I don't get kickbacks. Maybe a beer, but probably not.)


99 Haskell Problems - Problem 2 Background

This is my first experience with functional programming and totally self-study. I just have 2-3 days' worth experience in Haskell. Now let's continue with actual code.

For Problem 2 of 99 haskell problems I wrote this function

module Functions
(
lastButOne
) where

lastButOne :: [a] -> a
lastButOne [a, b] = a
lastButOne (_:xs) = lastButOne xs
lastButOne a = error "Not Supported"

Is this code ok from functional programming perspective? I am open to criticism from any perspective.

EDIT 1

Code after @Pedro Rodrigues suggestions

lastButOne :: [a] -> a
lastButOne [a, _] = a
lastButOne (_:xs) = lastButOne xs
lastButOne _ = error "lastButOne requires a list with at least 2 members"

You have the right idea, I just want to make a few remarks:

You could have used _ in the first and the last clause of your function in place of the ignored arguments. It might help readability to know right away that an argument is being ignored.
You're explicitly throwing an error on invalid input which is fine. A good error message could be more helpful to the caller than just getting a pattern match fail. However it wouldn't have shocked me if you had omitted that last line. An even safer solution is having the function return Just a when the input is valid and Nothing when it's not, but for performance/convenience's sake many partial functions out there don't do that.

EDIT: To elaborate more on the safer solution point I mentioned earlier: lastButOne has type lastButOne :: [a] -> a. However this type fails to convey that lastButOne is a partial function, and therefore may fail on certain inputs. It's very easy for a caller to overlook that he needs to watch out for invalid inputs. However, had you defined lastButOne as lastButOne :: [a] -> Maybe a instead, then the fact that it might not produce a result for some inputs, is directly encoded in the type of the function, thus making it type safer. Any caller of lastButOne couldn't possibly overlook that he needs to handle failure.

In case you haven't heard of Maybe yet, it's a data structure that is used to elegantly express a computation that may fail. Examples:

my_div :: Integer -> Integer -> Maybe Integer
my_div _ 0 = Nothing
my_div a b = Just (div a b)

lastButOne :: [a] -> Maybe a
lastButOne [a, _] = Just a
lastButOne (_:xs) = lastButOne xs
lastButOne _ = Nothing


Ruby Symbol Table implementation using Binary Search I'm currently going over Robert Sedgewick's Algorithms book. I implemented a Symbol Table using two parallel array one for keys and one for values. The keys array is ordered and the Symbol Table uses binary search to find the position of the elements in the array.
In the practice section, the implementation of the delete and floor method were left as exercises. Here are the directions for delete and floor:

I would like to see if there is any feedback in the implementation of those elements. Specially floor.
Here is the code:
# Binary Search Symbol Table in Ruby
class BinarySearchST
  attr_accessor :n, :keys, :vals

  def initialize
    @n = 0
    @keys = []
    @vals = []
  end

  def size
    @n
  end

  def is_empty?
    @n.zero?
  end

  def get(key)
    return nil if is_empty?

    i = rank(key)
    if i < @n && @keys[i] == key
      @vals[i]
    else
      nil
    end
  end

  def rank(key)
    lo = 0
    hi = @n - 1

    while lo <= hi
      mid = lo + (hi - lo) / 2
      cmp = (key <=> @keys[mid])
      if cmp < 0
        hi = mid - 1
      elsif cmp > 0
        lo = mid + 1
      else
        return mid
      end
    end
    lo
  end

  def put(key, val)
    #  Search for key. Update value if found; grow table if new
    i = rank(key)
    if i < @n && @keys[i] == key
      return @vals[i] = val
    end

    j = @n
    while j > i
      @keys[j] = @keys[j - 1]
      @vals[j] = @vals[j - 1]

      j -= 1
    end

    @keys[i] = key
    @vals[i] = val
    @n += 1
  end

  def min
    @keys[0]
  end

  def max
    @keys[-1]
  end

  def select(k)
    @keys[k]
  end

  def ceiling(key)
    i = rank(key)
    @keys[i]
  end

  def floor(key)
   raise 'ilegalArgumentException' if key == nil
   raise 'NoSuchElementException' if @keys.empty?

    i = rank(key)
    if keys[i] == key || i == 0
      keys[i]
    else
      keys[i - 1]
    end
  end

  def delete(key)
    raise 'ilegalArgumentException' if key.nil?

    i = rank(key)
    @keys.delete_at(i)
    @vals.delete_at(i)
  end
end

bst = BinarySearchST.new

bst.put('s', 0)
bst.put('e', 1)
bst.put('a', 2)
bst.put('r', 3)
bst.put('c', 4)
bst.put('h', 5)
bst.put('e', 6)
bst.put('x', 7)
bst.put('a', 8)
bst.put('m', 9)
bst.put('p', 10)
bst.put('l', 11)
bst.put('e', 12)

binding.pry
p bst.get('e')

It's a good implementation, I can make the following improvement suggestions:

size/n aliasing: either add a comment explaining its purpose (a legitimate one could be the need to comply with some external interface), or pick one name and use it throughout the class.
rank(key) < @n checks: change rank to return nil when the key is not found. This is a common Ruby idiom. In Ruby 0 is truthy unlike many other languages.
@keys[i] == key: This check may be better off in a separate method, i.e. instead of a single rank method have one method that returns the rank when finding and one that returns the place for insertion.
Exception raises: the idiomatic Ruby way is to define needed exception classes (you could nest them in the BinarySearchST class) rather than raising strings.
Further on exception raises, it is a good practice to provide a descriptive message when raising each exception.
It's understandable if you want to move array elements yourself, but Ruby standard library does provide the Array#insert method which would do that for you.


The Love-Letter Mystery Problem Statement

  James got hold of a love letter that his friend Harry has written for
  his girlfriend. Being the prankster that James is, he decides to
  meddle with it. He changes all the words in the letter into
  palindromes.

  While modifying the letters of the word, he follows 2 rules:

  (a) He always reduces the value of a letter, e.g. he changes 'd' to
  'c', but he does not change 'c' to 'd'.  (b) If he has to repeatedly
  reduce the value of a letter, he can do it until the letter becomes
  'a'. Once a letter has been changed to 'a', it can no longer be
  changed.

  Each reduction in the value of any letter is counted as a single
  operation. Find the minimum number of operations he carries out to
  convert a given string into a palindrome.

The challange can be found here.

Input Format

The first line contains an integer \$T\$, i.e., the number of test cases.
The next \$T\$ lines will contain a string each.

Output Format

A single line containing the number of minimum operations corresponding to each test case.

Constraints

\$1 ≤ T ≤ 10\$
\$1 ≤\$ length of string \$≤ 104\$

All characters are lower cased English alphabets.

Sample Input

3
abc
abcba
abcd

Sample Output

2
0
4

Test Cases

For the first test case, abc → abb → aba.
For the second test case, abcba is a palindromic string.
For the third test case, abcd → abcc → abcb → abca = abca → abba.

Solution

T = gets.chomp.to_i
result = []
def difference x,y
  (x.ord - y.ord).abs
end
T.times do
  string = gets.chomp
  i,j = 0, -1
  n = 0
  loop do
    n = n + difference(string[i], string[j])
    i += 1
    j -= 1
    break if string.length % 2 == 0 ? i == (string.length/2) : i + j == -1 && string[i] == string[j] && i == string.length/2
  end
  result << n
end

puts result

There is almost always a better way to loop in Ruby than actually using a loop. Unfortunately, I don't know Ruby well enough to say what that better way is.

What I can say is that this line of code is a bit obtuse.

break if string.length % 2 == 0 ? i == (string.length/2) : i + j == -1 && string[i] == string[j] && i == string.length/2

While it does seem to be a very Ruby way to write something like this, it's not very readable. Ternaries stretching off the screen are not a good thing in my opinion. It's okay to use an If block there.


Small bash script to enhance virtual environments on directory navigation Ages ago I made a small bash script which would add a command cdp, standing for "change directory - project".

What it did was simple, it added all folders in ~/projects as autocompletion and CD'd me into them using only the project folder's name.

Over time I switched to a different company where I started working with Python, thus needing virtual environments not to mess up my workstation, which led me to adding more functionalities to the script.

I rarely use bash to do anything, rather using Python or even PHP for simple CLI tasks. I was hoping for some input regarding my first ever bash script, though, so I can keep my terminal scripts in its native language.

I think some improvements could be the way I name and call functions, and the newly added command should probably be something other than an alias.

#!/usr/bin/env bash

funcCheckVirtualEnvironment()
{
    if [ -d "venv/" ]; then
        read -p "A virtual environment was found for this project, would you like to execute the activation script? [y/n]" -n 1 -r
        echo
        if [[ $REPLY =~ ^[Yy]$ ]]; then
            source venv/bin/activate
            if [ -e "requirements.txt" ]; then
                read -p "PyPi requirements were found, would you like to install any uninstalled packages? [y/n]" -n 1 -r
                echo
                if [[ $REPLY =~ ^[Yy]$ ]]; then
                    pip install -r requirements.txt
                fi
            fi
        fi
    fi
}

funcCDP()
{

    # Add more flags in future, maybe
    case "$1" in
    "-r" )
        funcCheckVirtualEnvironment
        return 0 ;;
    esac

    if [ -d ~/projects/$1 ]; then
        cd ~/projects/$1;
        funcCheckVirtualEnvironment
    else
        echo "Directory $1 does not exist!"
    fi

}

funcCD()
{
    if [ $1 == "-"] || [ -d $1 ]; then
        command cd "$@"
        if [ -d "venv/" ]; then
            funcCheckVirtualEnvironment
        fi
    else
        echo "No such file or directory: $1"
    fi
}

_cdp()
{
        local cur=${COMP_WORDS[COMP_CWORD]}
        COMPREPLY=( $(compgen -W '$(\ls ~/projects)' -- $cur) )
}
complete -F _cdp cdp
alias cdp=funcCDP
alias cd=funcCD

No shebang
It doesn't make sense to execute this as a program - it's intended to be sourced into an interactive shell.  So we shouldn't have the #! line.
Quote expansions
$1 could expand into multiple words here:
    if [ -d ~/projects/$1 ]; then
        cd ~/projects/$1;

and here:
    if [ $1 == "-"] || [ -d $1 ]; then

Send errors to stderr
    echo "Directory $1 does not exist!" >&2
    #                                   ^^^ IMPORTANT

Don't ask questions when used non-interactively
funcCheckVirtualEnvironment can misbehave when standard input is not a terminal.  We should test that case and return early:
test -r "venv/bin/activate" || return 0;
if ! test -t 0
then echo "Warning: not activating virtual environment" >&2
     return 0   # or 1 if this should be a failure
fi
read -p "Execute this project's activation script? [y/N]" -n 1 -r; echo
[[ REPLY =~ [Yy] ]] || return 0;

I've transformed some of the other if and else above into early returns, to reduce the nesting depth - I find that easier to read.  Also, I've capitalised N in the prompt, to provide the usual indication of the default.  And we know that $REPLY is a single character, so we can lose the anchors.
Let cd do its own checking instead
There's a possible race between checking for the existence of a directory and actually changing into it, so it's easier and safer to simply check whether cd succeeded:
command cd "$@" && test -d 'venv' && funcCheckVirtualEnvironment

This also allows cd to produce the error message (in the user's preferred $LANGuage).
Consider the exit status of the command
If we cd but there's no venv directory at the destination, that should be considered a success:
command cd "$@" || return $?
if test -d "venv"
then funcCheckVirtualEnvironment
else true
fi

Actually, there's no need to check for the existence of venv/, given that funcCheckVirtualEnvironment does that itself, and should succeed if it's not present:
command cd "$@" && funcCheckVirtualEnvironment

Similarly, in cdp -r, instead of return 0, a plain return is better, as that will result in the return value of the last executed command (funcCheckVirtualEnvironment in this case).
Use standard directory completion
It's bad practice to parse the output of ls.  Instead, we can ask compgen to give us directory names; we can eliminate absolute paths and subdirectories by excluding anything containing /:
_cdp()
{
        COMPREPLY=( $(cd ~/projects && compgen -X '*/*' -d $2) )
}

My version
funcCheckVirtualEnvironment()
{
    test -r 'venv/bin/activate' || return 0;
    if ! test -t 0
    then
        # Not a terminal - don't ask questions
        echo "Warning: not activating virtual environment" >&2
        return 0   # or 1 if this should be a failure
    fi

    read -p "Execute this project's activation script? [y/N]" -n 1 -r; echo
    [[ REPLY =~ [Yy] ]] || return 0;
    source venv/bin/activate || return $?

    test -e 'requirements.txt' || return 0;
    read -p "Install any missing PyPi packages? [y/N]" -n 1 -r; echo
    [[ REPLY =~ [Yy] ]] || return 0;
    pip install -r requirements.txt
}

funcCDP()
{
    # Add more flags in future, maybe
    case "$1" in
    "-r" )
        funcCheckVirtualEnvironment
        return
        ;;
    esac

    cd ~/projects/"$1" && funcCheckVirtualEnvironment
}

funcCD()
{
    command cd "$@" && funcCheckVirtualEnvironment
}

_cdp()
{
    COMPREPLY=( $(cd ~/projects && compgen -X '*/*' -d $2) )
}
complete -F _cdp cdp
alias cdp=funcCDP
alias cd=funcCD


Python class which chains Google translations back and forth between two languages I created a class which creates chained translations (for example, Japanese translated to English, then back to Japanese, then to English).

Some concerns:

Is my usage of recursion good?
Is the class pythonic? Is there anything else to make it more pythonic?
I used instantiated Translator twice in my code. Is there any way to only use it once?

Code:

from googletrans import Translator

word_list = []

class Translate:

    def __init__(self, word, target_language='en'):
        translator = Translator()

        self.target_language = target_language
        self.original_language = translator.detect(word).lang
        self.chained_translation = self.repeat_translation(word, target_language)

    def __getitem__(self, item):
        return self.chained_translation[item]

    def __len__(self):
        return len(self.chained_translation)

    def repeat_translation(self, word, target, count=0):
        translator = Translator()
        translated_text = translator.translate(word, dest=target).text
        word_list.append(translated_text)
        count += 1
        # When recursive count is odd.
        if count % 2 == 1:
            return self.repeat_translation(translated_text, self.original_language, count)
        # When recursive count is even and under 10.
        if count < 10:
            return self.repeat_translation(translated_text, self.target_language, count)
        # When recursive ends.
        else:
            return word_list

Example usage:

test = Translate('は忙しい。彼は多忙な人間だ。勉強をしたい')
print(test.chained_translation)
print(test[9])
print(test[8])
print(len(test))

['I am busy. He is a busy man. I want to learn', '私は忙しいです。彼は忙しい人だ。学びたい', 'I am busy. He is a busy man. I want to learn', '私は忙しいです。彼は忙しい人だ。学びたい', 'I am busy. He is a busy man. I want to learn', '私は忙しいです。彼は忙しい人だ。学びたい', 'I am busy. He is a busy man. I want to learn', '私は忙しいです。彼は忙しい人だ。学びたい', 'I am busy. He is a busy man. I want to learn', '私は忙しいです。彼は忙しい人だ。学びたい']
私は忙しいです。彼は忙しい人だ。学びたい
I am busy. He is a busy man. I want to learn
10

I think that you should be more explicit about the source language instead of relying exclusively on auto-detection, in both your Translate constructor and when you call translator.translate().  Japanese is easy enough to detect unambiguously in most cases.  However, phrases in other languages could easily be interpreted as several possible languages.

Recursion is rarely the most idiomatic solution in Python.  Rather, generators are often a better idea.  Here's a generator that will translate back and forth between two languages as many times as you want:

from googletrans import Translator

def chained_translations(phrase, target_language='en', source_language=None):
    translator = Translator()
    source_language = source_language or translator.detect(phrase).lang
    while True:
        phrase = translator.translate(phrase, src=source_language, dest=target_language).text
        yield phrase
        source_language, target_language = target_language, source_language

This solution is much simpler, because the state is kept in the execution flow of the generator code itself.

Example usage:

>>> test = chained_translations('は忙しい。彼は多忙な人間だ。勉強をしたい')
>>> print(next(test))
'I am busy. He is a busy man. I want to learn'
>>> print(next(test))
'私は忙しいです。彼は忙しい人だ。学びたい'
>>> from itertools import islice
>>> print(list(islice(test, 3)))
['I am busy. He is a busy man. I want to learn', '私は忙しいです。彼は忙しい人だ。学びたい', 'I am busy. He is a busy man. I want to learn']


Optimize time for file download This is the working code snippet to download file from a URL. I want to know if there is any optimization I can do to download the file faster.

File localFile = new File(localUrl)
localFile.withOutputStream { out ->
    URLConnection url = new URL(remoteUrl).openConnection()
    def remoteAuth = "Basic " + "${username}:${password}".getBytes().encodeBase64().toString()
    url.setRequestProperty("Authorization", remoteAuth);
        if (url.responseCode == 200) {
            out << url.getInputStream()
        } else {
            System.out.println("Can not download file from ${remoteUrl}")
        }
}

You won't be able to see speed improvements here, as that's just network limited, but there are a few improvements to this snippet of code I can suggest (wrapped in a method):

def static copyToLocalUrl(final String localUrl, final String remoteUrl, final String username, final String password) {
  final def authorization = 'Basic' + "$username:$password".bytes.encodeBase64().toString()
  new File(localUrl).withOutputStream { final out ->
    remoteUrl.toURL()
        .getText(requestProperties: [Authorization: authorization])
        .bytes.with { out << it }
  }
}

Note that you don't need to enclose GString variables in brackets if you're simply just accessing them or their fields.

This answer also doesn't do any HTTP response checking for simplicity; it just saves the HTML to a local file. If you're keen on checking the response, you can do so like this:

def static copyToLocalUrl(final String localUrl, final String remoteUrl, final String username, final String password) {
  new File(localUrl).withOutputStream { final out ->
    remoteUrl.toURL().openConnection().with {
      setRequestProperty 'Authorization', 'Basic' + "$username:$password".bytes.encodeBase64().toString()
      if (200 == it.responseCode) {
        out << it.inputStream
      }
      else {
        throw new ConnectException("Cannot download file from ${remoteUrl}")
      }
    }
  }
}


Conditionally applying and removing css based on a table row clicked I've a added a click event to each row on a table element. Which I then use the $this context from the event to add some css styling to that clicked row.

The behavior is as follows:

IF tr doesn't have kpi-selected css applied
WHEN tr is clicked on table
THEN remove css style from all other tr elements
THEN add css style to clicked row's label
AND search the datatable for the clicked label's KPI

What I've noticed during testing is that the gulf between clicking and the css style being added to the element is very slow.

Question:

How can I optimize this code to improve speed between clicking a tr and applying the required css?

JSFiddle:

http://jsfiddle.net/brianVarleyHPE/mtkndq3e/

Gist of code: In the real implementation there will be more than one section-list table, this is a gist showing only one.

HTML

    Availability By Asset Criticality

            Normal<input id="lblNormal" type="hidden" value="Normal">
            2

            Mission Critical<input id="lblMC" type="hidden" value="Mission Critical">
            0

            Entity Essential<input id="lblEE" type="hidden" value="Entity Essentiall">
            1

CSS:

.kpi-selected label {
    border:3px solid #0CA281;
    padding: 3px;
    border-radius:30px;
    -webkit-border-radius:30px;
    -moz-border-radius:30px;
}

JS

 $(".section-list").on('click', 'tr', function (e) {

        $('.section-list tr').removeClass("kpi-selected");

        if ($(this).attr('id') === "normalAvailability") {

            handleKPI_Selection($(this), 1, 'Normal');
        }
        else if ($(this).attr('id') === "mcAvailability") {

            handleKPI_Selection($(this), 1, 'Mission Critical');
        }
        else if ($(this).attr('id') === "eeAvailability") {

            handleKPI_Selection($(this), 1, 'Entity Essential');
        }

    });

    function handleKPI_Selection(td, index, searchFilter) {

        td.toggleClass("kpi-selected");
        if (td.hasClass("kpi-selected") && index !== 0) {

            historyTable.search('').columns().search('').draw();
            historyTable.columns(index).search(searchFilter);
            historyTable.draw();
        } else {
            historyTable.search('').columns().search('').draw();
        }

    }

Should your <tr> elements be using normalAvailability, mcAvailabilty, etc. as ID's or as classes?
It looks like you are passing a <tr> element to handleKPI_Selection() not a <td>, so perhaps the parameter should be renamed.
I don't really see great value in your handleKPI_Selection() function being separated out. If don't see where the first conditional case would ever be triggered based on what you have shown (where index value is always 0).  This calls into question you whole design, which seems to alwasy want to pass in a searchFilter value, but then never use it.
Are you sure toggleClass("kpi-selected") is what you want to do here?  What if user repeatedly clicks the same row?
This following conditional seems unnecessary.

Your code:

    if ($(this).attr('id') === "normalAvailability") {

        handleKPI_Selection($(this), 0, 'Normal');
    }
    else if ($(this).attr('id') === "mcAvailability") {

        handleKPI_Selection($(this), 0, 'Mission Critical');
    }
    else if ($(this).attr('id') === "eeAvailability") {

        handleKPI_Selection($(this), 0, 'Entity Essential');
    }

This can be simplified to something like:

var idToSearchMap = {
   'normalAvailability': 'Normal',
   'mcAvailability': 'Mission Critical',
   'eeAvailability': 'Entity Essential'
}
var search = idToSearchMap[$(this).attr('id')] || '';
handleKPI_Selection($(this), 0, search);

Keep your code DRY.

I would avoid non-standard javascript symbol name syntax in your code (i.e. not camelCase). For example, handleKPI_Selection could perhaps instead be handleKpiSelection.
If you are going to have multiple section-list elements, then the following line of code may be problematic

Your code:

$('.section-list tr').removeClass("kpi-selected");

This will remove class from all rows on all section-lists.  If you only want to target current section list, then you would need to do something like:

$(this).closest('.section-list').find('tr').removeClass('kpi-selected');


"Expanding" a 2d array into a 1d array I'm attempting to write a (hopefully) efficient way of "expanding" a 2d array into a 1d array.  Let me illustrate what I mean:

Given the 2d array

{{1, 2},
 {3, 4}}

and a block width of 2 and a block height of 2, we can "expand" into a 1d array

{1, 1, 2, 2, 1, 1, 2, 2, 3, 3, 4, 4, 3, 3, 4, 4}

Notice, if newlines are added this looks like

{1, 1, 2, 2,
 1, 1, 2, 2,
 3, 3, 4, 4,
 3, 3, 4, 4}

And the original has been "expanded"!

The code I have currently is

public int[] expand(int[][] src, int blockWidth, int blockHeight) {
    int[] dest = new int[src.length * src[0].length * blockWidth * blockHeight];
    for (int i = 0; i < src.length; i++) {
        for (int j = 0; j < src[i].length; j++) {
            int fromIndex = i * blockWidth * blockHeight * src[i].length + j * blockWidth;
            int toIndex = fromIndex + blockWidth;
            Arrays.fill(dest, fromIndex, toIndex, src[i][j]);
        }
        int srcPos = i * blockWidth * blockHeight * src[i].length;
        for (int k = 1; k < blockHeight; k++) {
            int destPos = srcPos + k * blockWidth * src[i].length;
            System.arraycopy(dest, srcPos, dest, destPos, src[i].length * blockWidth);
        }
    }
    return dest;
}

This method:

Creates the destination array equal to the total size of the source array times the block dimensions.
For each color in a row, fill the first row of the destination.
Copy the first row into the rest of the rows, completing the blocks.

I wonder if there's a way to make this method more efficient or cleaner?

Thanks!

Problems like this are often "academic" in nature (although the principles can be applied to other problem-spaces too).

In this instance, I believe that the challenge is to use the correct form of "modulo" and integer-division type arithmetic and to have a "simple" conversion function for each target element.

Your initial output array creation looks good:

int[] dest = new int[src.length * src[0].length * blockWidth * blockHeight];

Note that a zero-sized, or null src array will cause an Exception to be thrown when indexing src[0]. You should probably be more defensive about that...

if (src == null || src.length == 0) {
    return new int[0];
}

Now, in your dest, the value of each cell is a function of its index. There are a few critical values:

target rowsize -> source row-size * block-width
source row -> ( target index / target rowsize) / block-height
source column -> ( target index % target rowsize ) / block-width

Now, the above logic is far from "obvious", but it makes for a remarkably simple loop:

public static int[] expand(int[][] src, int blockWidth, int blockHeight) {
    int span = src[0].length * blockWidth;
    int[] dest = new int[src.length * blockHeight * span];
    for (int i = 0; i < dest.length; i++) {
        dest[i] = src[(i / span) / blockHeight][(i % span) / blockWidth];
    }
    return dest;
}

You can see the above running in ideone: https://ideone.com/sJhksT

Note that the concept of describing an element in an output array as a function of the output index is sometimes a nice "trick" and neatens things up. You have to be able to spot this, though, in real code. Note that the above code basically says dest[i] = fn(i) where fn(i) is some function taking i as an argument.

Your code, on the other hand, works off the source index, and it tries to calculate the indexes in the output array from the source location. That's not "wrong", but as you can see, it makes for a nested, and ugly loop.

Knowing when to reverse the logic is more of an art than a science, but being able to identify these logic inversions is important.


View Model Filling Fields I'm currently getting into MVC, and I'm working on a simple CRUD application but to make best use of relational database layout I need to use ViewModels so things look prettier on the UI.

I have a ViewModel:

public class CreateAPIViewModel
{
    [Display(Name = "Name")]
    public string Name { get; set; }

    [Display(Name = "Description")]
    public string Description { get; set; }

    [Display(Name = "Team")]
    public int TeamID { get; set; }

    [Display(Name = "Application")]
    public int ApplicationID { get; set; }

    [Display(Name = "Team")]
    public IEnumerable<SelectListItem> Team { get; set; }

    [Display(Name = "Application")]
    public IEnumerable<SelectListItem> Application { get; set; }

    [Display(Name = "Location")]
    public string Location { get; set; }

    [Display(Name = "Keywords")]
    public string Keywords { get; set; }
}

And in my APIController I have this action:

public ActionResult Create()
{
    CreateAPIViewModel viewModel = new CreateAPIViewModel();

    //Fill team - seems a bit long doesn't it?
    listOfTeams = team.getAllTeams();

    List<SelectListItem> listOfTeamsSelect = new List<SelectListItem>();

    foreach (_Team_Detail item in listOfTeams)
    {
        listOfTeamsSelect.Add(new SelectListItem { Value = Convert.ToString(item.ID), Text = item.Name });
    }

    viewModel.Team = listOfTeamsSelect;

    //Fill application - seems a bit long doesn't it?
    listOfApplications = application.getAllApplications();

    List<SelectListItem> listOfApplicationsSelect = new List<SelectListItem>();

    foreach (_Application item in listOfApplications)
    {
        listOfApplicationsSelect.Add(new SelectListItem { Value = Convert.ToString(item.ID), Text = item.Name });
    }

    viewModel.Application = listOfApplicationsSelect;

    //Send view model back
    return View(viewModel);
}

Is this the best way to be going about this process for creating a Create page so I can have dropdowns?

If you aren't considering using a mapping solution such as AutoMapper, you could make this code a bit more succient by removing some of the local variables and creating a couple of local methods that are responsible for creating the select lists.

The end solution would look something along the lines of:

public ActionResult Create()
{
    var viewModel = new CreateAPIViewModel
    {
        Team = ToSelectList(team.getAllTeams(),0),
        Application = ToSelectList(application.getAllApplications(),0)
    };

    //Send view model back
    return View(viewModel);
}

private List<SelectListItem> ToSelectList(IEnumerable<Team> applications, int selectedApplicationId)
{
    return applications
            .ToList()
            .Select(p =>
                new SelectListItem
                {
                    Value = Convert.ToString(item.ID),
                    Text = item.Name,
                    Selected = item.ID == selectedApplicationId
                });
}

private List<SelectListItem> ToSelectList(IEnumerable<Team> teams)
{
    return teams
            .ToList()
            .Select(p =>
                new SelectListItem
                {
                    Value = Convert.ToString(item.ID),
                    Text = item.Name,
                    Selected = item.ID == selectedTeamId
                });
}

This has the added benefit in that you most likely have a Edit() action as well.  You could re-use these methods.

public ActionResult Edit()
{
    var viewModel = new CreateAPIViewModel
    {
        Team = ToSelectList(team.getAllTeams(),team.Id),
        Application = ToSelectList(application.getAllApplications(),application.Id)
    };

    //Send view model back
    return View(viewModel);
}


Prints the multiplication table of 5 i=0
while i<=10:
    i+=1
    if i==5:
        continue
    print i*5

It prints the multiplication table of 5 as well as skips the 5th number in the series

This is the longest possible answer to the shortest problem...

How to make better loops in python

iterables
for loops
range
list comprehensions
Generator expressions

How to improve this particular code

what is the simplest way possible of writing it
what is the shortest way possible of writing it

How to make better loops in python

1. iterables

For loops can iterate over any iterables. What is an iterable? An iterable is a object we can iterate through.

More seriously, an iterable is an object that is itself a sequence of other objects. A list is an iterable, but it is not the only one as you will discover when you learn more about python.

2. for loops

A for loop can iterate over an iterable (in this case a list) like so:

for i in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:
    print(i)

3. range

The previous example is ugly because:
1. Hard-coded values can't be easily changed and are prone to mistakes.
2. We stored the first 10 positive integers in memory which take 144 bytes but it isn't scalable as if I want the first 1000000 positive integer, I would need 9 mo of memory.

That's where range comes in, it is an iterable that doesn't take much space and is often used in loops:

for i in range(10):
    print(i)

note: in earlier versions of python, range created a list and was equivalent to the first example.

What if I only want the numbers between 22 and 42 and skip every odd number:

for i in range(22, 43, 2):
    # range(start, end, step) <- this is a comment
    print(i)

note: the range(22, 43) doesn't actually include 43

4. list comprehensions

But wait, I don't want a 5 in my range, how can I remove it ?

for i in range(10):
    if i==5:
        continue
    print(i)

The technical term for continue is "ugly".

What you can use instead is this:

for i in range(10):
    if i!=5:
        print(i)

Which is better, but isn't what I want to show you. What I want to show you is list comprehensions:

l = [n for n in range(10) if n!=5]
for i in l:
    print(i)

A list is an iterable remember. Well, I've created a list containing the first 10 number except 5.

5. Generator expression

Remember also that I've said that a list was a very bad choice due to the memory inefficiency. Well fortunately for us, we have a better solution, generator expressions:

l = (n for n in range(10) if n!=5)
for i in l:
    print(i)

What changed ? Parentheses ! As easy as that.

But wait, I want to do the table of 5. How can I do that ?

l = (n for n in range(10) if n!=5)
for i in l:
    print(i*5)

Yes, that works, but what if...

l = (n*5 for n in range(10) if n!=5)
for i in l:
    print(i)

Yep, that also works...

How to improve your particular code

1. what is the simplest way possible of writing it

for i in range(10):
    if i!=5:
        print(i*5)

2. what is the shortest way possible of writing it

print('\n'.join(str(n*5) for n in range(10) if n!=5))
or
print(*(n*5 for n in range(10) if n!=5), sep='\n')

As you learn more about python, you might be tempted to use the second one. But it isn't pythonic because it violates at least 6 of Tim Peter's commandment:

  Beautiful is better than ugly.<
  Explicit is better than implicit.
  Simple is better than complex.<
  Complex is better than complicated.
  Flat is better than nested.<
  Sparse is better than dense.<
  Readability counts.<
  Special cases aren't special enough to break the rules.
  Although practicality beats purity.
  Errors should never pass silently.
  Unless explicitly silenced.
  In the face of ambiguity, refuse the temptation to guess.
  There should be one-- and preferably only one --obvious way to do it.<
  Although that way may not be obvious at first unless you're Dutch.
  Now is better than never.
  Although never is often better than right now.
  If the implementation is hard to explain, it's a bad idea.
  If the implementation is easy to explain, it may be a good idea.
  Namespaces are one honking great idea -- let's do more of those!

import this to read it at home

Some techniques of looping might be more appropriate in certain cases, but at least now you know what those techniques are.


Three stacks in a single array I have implemented three stacks in a single array in Java. Any advice on object oriented design, coding structure, logical part or any sort of advice would be appreciated.

public class ThreeStack {
    private int[] stack;

    private int first, last, mid;
    private final int midPosition;

    public ThreeStack(int size) {
        stack = new int[size];
        first = -1; last = size; mid = (first + last) >> 1;
        midPosition = mid;
    }

    //push
    public boolean pushFirstStack(final int val) {
        if (first + 1 < midPosition) {
            stack[++first] = val;
            return true;
        } else {
            return false;
        }
    }

    public boolean pushSecondStack(final int val) {
        if (mid + 1 < last) {
            stack[++mid] = val;
            return true;
        } else {
            return false;
        }
    }

    public boolean pushThirdStack(final int val) {
        if (last - 1 > mid) {
            stack[--last] = val;
            return true;
        } else {
            return false;
        }
    }

    //pop
    public int popFirstStack() {
        if (first < 0) {
            return Integer.MIN_VALUE;
        } else {
            int val = stack[first];
            stack[first--] = 0;
            return val;
        }
    }

    public int popSecondStack() {
        if (mid < midPosition) {
            return Integer.MIN_VALUE;
        } else {
            int val = stack[mid];
            stack[mid--] = 0;
            return val;
        }
    }

    public int popThirdStack() {
        if (last >= stack.length) {
            return Integer.MIN_VALUE;
        } else {
            int val = stack[last];
            stack[last++] = 0;
            return val;
        }
    }

    //size
    public int firstStackSize() {
        return mid - midPosition;
    }

    public int secondStackSize() {
        return last - mid;
    }

    public int lastStackSize() {
        return secondStackSize();
    }

    public void printStack() {
        for (int i = 0; i < stack.length; i++) {
            System.out.print(stack[i]);
            System.out.print((i == first || i == mid || i == last) ? "*": "");
            System.out.print(" ");
        }
        System.out.println();
    }
}

Main Method

public class MainClass {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        ThreeStack stack = new ThreeStack(12);

        while (true) {
            stack.printStack();
            menu();
            int c = in.nextInt();
            switch (c) {
                case 1:
                    stack.pushFirstStack(in.nextInt());
                    break;
                case 2:
                    stack.pushSecondStack(in.nextInt());
                    break;
                case 3:
                    stack.pushThirdStack(in.nextInt());
                    break;
                case 4:
                    printPopVal(stack.popFirstStack());
                    break;
                case 5:
                    printPopVal(stack.popSecondStack());
                    break;
                case 6:
                    printPopVal(stack.popThirdStack());
                    break;
            }
        }

    }

    static void menu() {
        System.out.println("+------------------------------+");
        System.out.println("1 -> push first stack");
        System.out.println("2 -> push second stack");
        System.out.println("3 -> push third stack");
        System.out.println("4 -> pop first stack");
        System.out.println("5 -> pop second stack");
        System.out.println("6 -> pop third stack");
        System.out.println("+------------------------------+");
    }

    static void printPopVal(int val) {
        System.out.println("Pop : " + val);
    }
}

Stack sizing

The 3 stacks have different size limits:

The 1st stack can have maximum size / 2 elements
The 2nd and 3rd stacks share their storage, they can have size / 2 elements

When there is a peculiar limitation like this, it's good to document it, and hopefully with an explanation of the reasoning.

An alternative approach could have been giving each stack size / 3 space.

Another alternative would be using an array of StackNode objects, where StackNode would store the payload, and a link to the StackNode above it. This way all stacks would have dynamic size, until their total use reaches the capacity of the StackNode array. However, in addition to the extra storage needed (for the links), the logic would be also more complicated to account for the freed spots in the array.

Make final what you can

This field can be final:

private int[] stack;

One statement per line

It's best when you can read code from top to bottom.
When there are multiple statements per line,
it's easy to overlook the right end.
So instead of putting multiple assignments on the same line like this:

first = -1; last = size; mid = (first + last) >> 1;

It would be better to break it up, to have one statement per line.

Clever code

This code is a bit clever:

mid = (first + last) >> 1;

It's best to keep things simple and natural:

mid = (first + last) / 2;

Sure, bit shifting is faster than division.
But it's reasonable to expect the compiler to automatically convert / 2 to use bit shifting instead of division, so that you can write simple, natural, unsurprising code. (I don't know if the compiler actually does that, but in any case, such micro-optimization is likely to be pointless pedantry anyway.)


Performance of stable marriage solution in Python 3 I am trying to solve the stable marriage problem in SPOJ in Python 3.

  There are given \$n\$ men and \$n\$ women. Each woman ranks all men in order of her preference (her first choice, her second choice, and so on). Similarly, each man sorts all women according to his preference. The goal is to arrange \$n\$ marriages in such a way that if a man \$m\$ prefers some woman \$w\$ more than his wife, then \$w\$ likes her husband more than \$m\$. In this way, no one leaves his partner to marry somebody else. This problem always has a solution and your task is to find one.

  Input

  The first line contains a positive integer \$t \le 100\$ indicating the number of test cases. Each test case is an instance of the stable marriage problem defined above. The first line of each test case is a positive integer \$n \le 500\$ (the number of marriages to find). The next \$n\$ lines are the woman's preferences: \$i\$th line contains the number \$i\$ (which means that this is the list given by the \$i\$th woman) and the numbers of men (the first choice of \$i\$th woman, the second choice,...). Then, the men's preferences follow in the same format.

  Output

  For each test case print \$n\$ lines, where each line contains two numbers \$m\$ and \$w\$, which means that the man number \$m\$ and the woman number \$w\$ should get married.

I have tried optimising the code as much as I can (remove slicing, keep it minimal array, remove printing one by one... etc).

But by far the best code I have been able to get runs in 0.13(s?) time and 33M(??) memory. But the best code for the same problem in Python 3 (submitted by @_@) runs in 0.09 time and 13M memory. So I would like suggestions on how to attain the best time and space usage with my code

from sys import stdin, stdout

def findWoman(manArray, womanArray, index, mpref, wpref):
    for woman in mpref[index - 1]:
        if(woman == 0):
            continue
        hub = womanArray[woman - 1]
        if(hub == 0):
            womanArray[woman - 1] = index
            manArray[index - 1] = woman
            return 0
        elif(wpref[woman - 1].index(index) > wpref[woman - 1].index(hub)):
            continue
        else:
            manArray[hub - 1] = 0
            womanArray[woman - 1] = index
            manArray[index - 1] = woman
            return hub

out = ''
t = int(stdin.readline())
while(t > 0):
    t -= 1
    n = int(stdin.readline())
    mpref = []
    wpref = []
    for _ in range(0, n):
        w = list(map(int, stdin.readline().split()))
        w[0] = 0
        wpref.append(w)
    for _ in range(0, n):
        m = list(map(int, stdin.readline().split()))
        m[0] = 0
        mpref.append(m)
    manArray = [0 for _ in range(n)]
    womanArray = [0 for _ in range(n)]
    for k in range(n):
        hub = k + 1
        while(hub != 0):
            hub = findWoman(manArray, womanArray, hub, mpref, wpref)
    for k in range(n):
        out += str(k + 1) + ' ' + str(manArray[k]) + '\n'
stdout.write(out)

I feel lazy to go through the details of your code, but here are few notes you should think about:

Your code, as it is, is not scalable. You should re-design it in terms of functions (I think functions are enough for your case, even in some other situations OOP may be a better choice). Functions allow also your code to be reused.
In terms of UX, I think you have some more work to do. For instance, when I run your code, I was waiting for something to happen, until I guessed to type in something, but then I had to check your code to see what it expects me to code. That is bad: imagine every software you use, you have to read its code to guess what you have to write or where to click.
You should throw a look to PEP 8 (for example, the naming conventions you use are camelCase, Python developers do not like that and you have to comply to the philosophy of Python)
Interesting if you throw a glance to: What does if __name__ == “__main__”: do?

That is the big picture I want to share with you for the moment ... maybe I can come back later to provide more useful help, or maybe other members may dive deeper into your code.


Designing a generic C++1z class I'm designing a generic interface for a MessageEntry class for a .h file.

I'm looking for guidance on form and terminology (would I call this an interface/template/generic class?):

#ifndef cppmq_message_types_h
#define cppmq_message_types_h

template <class T>
struct MessageEntry {
 public:
  MessageEntry<T>() {}
  MessageEntry<T>(T data) : data(data) {
    creation_time =
        std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
  }
  time_t CreationTime() { return creation_time; }

 private:
  T data;
  time_t creation_time;
};

#endif`

There are several problems with this code, starting from the fact that it is syntactically invalid. Getting past that, you have no access to your data, which makes it somewhat pointless, and the only way to construct a MessageEntry is to preconstruct a T, then copy it into the MessageEntry constructor, then copy it again into data. That can be unnecessarily expensive, and then you're not actually timestamping when you constructed the original object - you're timestamping two copies later.

You definitely want to be able to support in-place construction and you definitely want easy access to the data. Fortunately, you can solve both problems in one go with just simple inheritance:

template <typename T, typename Clock=std::chrono::system_clock>
struct MessageEntry : T
{
    using T::T;
    decltype(Clock::now()) creation_time = Clock::now();
};

That's... it. I added a second template argument in case your users want a different clock (e.g. std::chrono::steady_clock). I intentionally left creation_time non-const so as to make the class copyable. If you don't need this to be copyable, then I would write it as:

template <typename T, typename Clock=std::chrono::system_clock>
struct MessageEntry : T
{
    using T::T;
    const decltype(Clock::now()) creation_time = Clock::now();

    MessageEntry(MessageEntry const& ) = delete;
    MessageEntry& operator=(MessageEntry const& ) = delete;
};

It might be worth renaming the class to something that adds clarity to its intent. Perhaps WithCreationTime.

Lastly, I would prefer #pragma once just to be able to write less code for include guards, and you are missing your #include for <chrono>.

Full solution:

#pragma once

#include <chrono>

template <typename T,
          typename Clock = std::chrono::system_clock>
struct WithCreationTime : T
{
    using T::T;
    decltype(Clock::now()) creation_time = Clock::now();
};

Example usage:

WithCreationTime<std::vector<int>> v{1, 2, 3, 4};
std::cout << v.size() << '\n'; // 4
std::cout << v.creation_time << '\n'; // whatever now() is


Outputting every second char in half string My algorithm works but it is too slow.
I have to ask for help with improving this algorithm because the website which checks this, kicks me for too slow execution.
What should I improve here ?

Details:

  Given a sequence of 2*k characters, please print every second
  character from the first half of the sequence. Start printing with the
  first character.

  Input In the first line of input your are given the positive integer t
  (1<=t<=100) - the number of test cases. In the each of the next t
  lines, you are given a sequence of 2*k (1<=k<=100) characters.

  Output For each of the test cases please please print every second
  character from the first half of a given sequence (the first character
  should appear).

Example

  Input:

4
your
progress
is
noticeable

  Output:

y
po
i
ntc

class Chars{

    public static void main (String[] args) {

        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        if (t <= 100 && t >= 1) {
            for (int i = 0; i < t; i++) {
                calculate();
            }
        }
    }

    private static void calculate() {

        Scanner sc = new Scanner(System.in);
        String rawString = sc.nextLine();
        StringBuilder afterAdd = new StringBuilder();
        StringBuilder finishChars = new StringBuilder();
        int numberOfChars = rawString.length();
        if (numberOfChars <= 100 && numberOfChars >= 1) {
            if (numberOfChars % 2 == 0) {
                int numHalfChars = numberOfChars / 2;
                for (int i = 0; i < numHalfChars; i++) {
                    afterAdd.append(rawString.charAt(i));
                }
                if (afterAdd.length() <= 2) {
                    System.out.println(afterAdd.charAt(0));
                } else {
                    for (int i = 0; i < afterAdd.length(); i = i + 2) {
                        finishChars.append(afterAdd.charAt(i));
                    }
                    System.out.println(finishChars);
                }
            }
        }
    }
}

after improve:

class Algorithm{

    private static List<String> results = new ArrayList<>();

    public static void main (String[] args) {
        calculate();
        show();
    }

    private static void show() {
        for (String result : results) {
            System.out.println(result);
        }
    }

    private static void calculate() {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        if (t <= 100 && t >= 1) {
            for (int i = 0; i <= t; i++) {
                String rawString = sc.nextLine();
                StringBuilder finishChars = new StringBuilder();
                int numberOfChars = rawString.length();
                if (numberOfChars <= 100 && numberOfChars >= 1 && numberOfChars % 2 == 0) {
                    for (int j = 0; j < rawString.length() / 2; j = j + 2) {
                        finishChars.append(rawString.charAt(j));
                    }
                    results.add(String.valueOf(finishChars));
                }
            }
        }
    }
}

Your code is slow simply because it does much more than it needs to. It copies the first half of the original string to a new StringBuilder, but then, you only use this StringBuilder to access its characters by their index, which you could also do with the original string, since the indexes of the characters in the first half of the original string and those in this new StringBuilder are identical.

So you might as well omit the variable afterAdd and use the original string in its place when you iterate over every second character:

for (int i = 0; i < rawString.length() / 2; i = i + 2) {
    finishChars.append(rawString.charAt(i));
}

Also, there is no need to handle the case afterAdd.length() <= 2 (which, after deleting afterAdd, would translate to rawString.length() <= 4) separately. The result of the method will be unaffected if you remove this extra if and simply let the for loop take care of it. In fact, I don't see anything special in this case in the first place, so I wonder why you make a special case for it.

Besides, I think it is strange that you let the main method read the number of test cases from the command line, but delegate the reading of the individual test cases to the calculate method, not least because this requires you to create a new Scanner object for every single test case. Why don't you do all the reading from the command line in one method, and then pass each test case to the method calculate as a parameter and let it do only do what its name suggest, namely calculating the result?

Anyway, I would suggest separating user interface from program logic, because right now, the method calculate not only calculates the result, but also prints it to System.out, which goes a bit against the Single Responsibility Principle. I think the code would be clearer if calculate returns a String representing the result, so that the calling method can decide what to do with the result.


Applying 'Uncle Bob's Clean Code' on this small exercice I'm trying to apply Uncle Bob's Clean Code practices onto this exercise I'm helping someone with.
(The exercise assignment is in a "docstring" comment above the Main.)
I'm unsure what the best naming convention would be for ReturnAllCombinationsOfInputtedList, although I suspect it doesn't involve the return word in it's name for starters.
Same for CreateArray1to30(). Any suggestions for name choices/proper conventions here?
Do you guys see anything else that I should've done differently in your opinion?
using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleAppHelpingAlexWithExercices
{
    class Program
    {
        //**************************************************************************
        // How many different exam tickets with two different questions can be made,
        // from a syllabus with 30 questions
        //**************************************************************************
        static void Main(string[] args)
        {
            int[] questionsList = CreateArray1to30();
            List<string> combinations = ReturnAllCombinationsOfInputtedList(questionsList);
            int amountOfUniqueCombinations = combinations.Distinct<string>().Count();
            Console.WriteLine("Total unique combinations: {0}", amountOfUniqueCombinations);
        }

        static List<string> ReturnAllCombinationsOfInputtedList(int[] questionsList)
        {
            List<string> combinations = new List<string>();
            int[] aTest = new int[2];
            foreach (int firstQuestion in questionsList)
            {
                foreach (int secondQuestion in questionsList)
                {
                    if (firstQuestion != secondQuestion)
                    {
                        aTest[0] = firstQuestion;
                        aTest[1] = secondQuestion;
                        //Makes AB and BA tests both AB, so we can de-duplicate later.
                        Array.Sort(aTest);
                        combinations.Add(String.Format("{0}&{1}", aTest[0], aTest[1]));
                        Console.WriteLine("{0} and {1}", aTest[0], aTest[1]);
                    }
                }
            }
            return combinations;
        }

        static int[] CreateArray1to30()
        {
            return Enumerable.Range(1, 30).ToArray();
        }
    }
}

CreateArray1to30() could be renamed to CreateQuestions()
ReturnAllCombinationsOfInputtedList() could be renamed to Combinations() or QuestionCombinations()
ReturnAllCombinationsOfInputtedList() does too much. The call to .Distinct() is unecessary, and can be replaced with correct indices in the first place. Furthermore, the aTest array is entirely useless and could be replaced by using firstQuestion and secondQuestion directly.
Strings are a non ideal data structures for the exams, as you use a different format when printing them out

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleAppHelpingAlexWithExercices
{
    struct ExamTicket {
        public int Question1, Question2;
        public ExamTicket(int Question1, int Question2) {
            this.Question1 = Question1;
            this.Question2 = Question2;
        }
    }
    class Program
    {
        //**************************************************************************
        // How many different exam tickets with two different questions can be made,
        // from a syllabus with 30 questions
        //**************************************************************************
        static void Main(string[] args)
        {
            int[] questions = CreateQuestions();
            var combinations = new List<ExamTicket>(Combinations(questions));
            foreach (var combination in combinations) {
                Console.WriteLine($"{combination.Question1} and {combination.Question2}");
            }
            Console.WriteLine("Total unique combinations: {0}", combinations.Count);
        }

        static IEnumerable<ExamTicket> Combinations(int[] questions)
        {
            for (int i=0;i<questions.Length;++i) {
                for (int j=i+1;j<questions.Length;++j) {
                    yield return new ExamTicket(questions[i],questions[j]);
                }
            }
        }

        static int[] CreateQuestions()
        {
            return Enumerable.Range(1, 30).ToArray();
        }
    }
}


Function pointers and switch statements I feel like I can make my switch statements more elegant with a function pointer, i.e. I'd like to set the digestLength and a pointer to a function in the same switch statement instead of setting the length, declaring the result array, then calling the function.

- (NSString *)hashWithDigestType:(NSStringDigestType)type {
    const char *s = [self UTF8String];
    int digestLength;

    switch (type) {
        case NSStringDigestTypeMD5:
            digestLength = CC_MD5_DIGEST_LENGTH;
            break;
        case NSStringDigestTypeSHA1:
            digestLength = CC_SHA1_DIGEST_LENGTH;
            break;
        case NSStringDigestTypeSHA512:
            digestLength = CC_SHA512_DIGEST_LENGTH;
            break;
    }

    unsigned char result[digestLength];

    switch (type) {
        case NSStringDigestTypeMD5:
            CC_MD5(s, strlen(s), result);
            break;
        case NSStringDigestTypeSHA1:
            CC_SHA1(s, strlen(s), result);
            break;
        case NSStringDigestTypeSHA512:
            CC_SHA512(s, strlen(s), result);
            break;
    }

    NSMutableString *digest = [NSMutableString stringWithCapacity:(digestLength * 2)];
    for (NSUInteger i = 0; i < digestLength; i++)
        [digest appendFormat:@"%02x",result[i]];

    return [NSString stringWithString:digest];
}

I can't write the code in Objective-C, but in C, you could create yourself a structure type to get the information in one move.  I don't know whether the NSStringDigestTypeXXX values are conveniently numbered compactly from 0 or 1, so I'm taking the pessimistic view that they are not.  You can simplify the code below if they are compact and small.

struct Digestifier  // Declaration in a header (probably)
{
    int      hashtype;
    void   (*hash)(const char *source, size_t length, char *result);
    size_t   hashlen;
};
static const struct Digestifier digests[] =
{
    { NSStringDigestTypeSHA1,   CC_SHA1,   CC_SHA1_DIGEST_LENGTH   },
    { NSStringDigestTypeMD5,    CC_MD5,    CC_MD5_DIGEST_LENGTH    },
    { NSStringDigestTypeSHA512, CC_SHA512, CC_SHA512_DIGEST_LENGTH },
};
{ enum NUM_DIGESTS = sizeof(digests) / sizeof(digests[0]) };

You can then write a lookup function for this:

const struct Digestifier *digest_lookup(int hashtype)
{
    for (i = 0; i < NUM_DIGESTS; i++)
    {
        if (digests[i].hashtype == hashtype)
            return &digests[i];
    }
    assert(i != NUM_DIGESTS);  // Or other error handling!
    return 0;
}

And in your function:

- (NSString *)hashWithDigestType:(NSStringDigestType)type {
    const char *s = [self UTF8String];
    const struct Digestifier *digest = digest_lookup(type);

    // Error check digest if digest_lookup() does not do it for you!
    unsigned char result[digest->hashlen];
    digest->hash(s, strlen(s), result);

    NSMutableString *digest = [NSMutableString stringWithCapacity:(digest->hashlen * 2)];
    for (NSUInteger i = 0; i < digestLength; i++)
        [digest appendFormat:@"%02x",result[i]];

    return [NSString stringWithString:digest];
}

Note that you could also write the hash function invocation as:

    (*digest->hash)(s, strlen(s), result);

To some of us old-school (pre-standard) C programmers, that might perhaps be clearer.

Also, if Objective-C supports the C99 designated initializer notation, you could make the initializer for the digests[] array more robust (and render the hashtype member superfluous except for a cross-check):

static const struct Digestifier digests[] =
{
    [NSStringDigestTypeSHA1]   =
           { NSStringDigestTypeSHA1,   CC_SHA1,   CC_SHA1_DIGEST_LENGTH   },
    [NSStringDigestTypeMD5]    =
           { NSStringDigestTypeMD5,    CC_MD5,    CC_MD5_DIGEST_LENGTH    },
    [NSStringDigestTypeSHA512] =
           { NSStringDigestTypeSHA512, CC_SHA512, CC_SHA512_DIGEST_LENGTH },
};

This initializer correctly places the three rows in the array regardless of which member of the enumeration is mapped to 0, 1, 2.

With the additional information that the NSStringDigestTypeXXX values are 0, 1, 2, you can simplify the digest_lookup() function by:

Ensuring that the rows in the digests array are in the correct (0, 1, 2) sequence.
Changing from a search loop to a direct array lookup.
Probably asserting that the value is under control.

For the purposes of the code below, I'm assuming that NSStringDigestTypeMD5 is 0 and NSStringDigestTypeSHA512 is 2, but the ordering of the names is arbitrary; just choose the equivalent of 0 for the first name in the assert and 2 for the second.

const struct Digestifier *digest_lookup(NSStringDigestType hashtype)
{
    assert(hashtype >= NSStringDigestTypeMD5 &&
           hashtype <= NSStringDigestTypeSHA512);
    assert(digest[hashtype].hashtype == hashtype);
    return &digests[hashtype];
}

The first assertion ensures that the value is in range.  The second assertion ensures that the table is properly sequenced and you are getting back the entry your expect.


Removing asInstance[T] from scala code I've written this scala code and I cannot work out how/if it is possible to remove the asInstance in the definition of class Problem.

The basic idea is that there are two hierarchies

Problem
SpecificProblem
ConcreteSpecificProblem

and

SolutionFinder
SpecificSolutionFinder
ConcreteSpecificSolutionFinder

These hierarchies will branched in the future, but at the moment there is only one concrete class and its direct parents.

SolutionFinder classes operate on Problem classes, and on construction of a Problem we provide a SolutionFinder instance that we will operate on it. The type declaration of the Problem class/subclasses specifies which SolutionFinder subclass be used on it, and the SolutionFinder type declaration specifies which Problem classes it be applied to.

The aim is to be able to just call solve on any kind of Problem and have it use the appropriate methods.

As it stands, the code works fine for all the examples I have tried.

I think that it is correct to say in the type declaration of Problem, it is implicit (in the non-scala-keyword sense) that S = S[S,T] = Problem[S,T]. The compiler does not recognise this, perhaps because I'm wrong about this and perhaps because this level of recursive type definition is beyond it's capabilities. To get around this I have told it to convert this to an S using asInstance, which is a faux-pas which I would like to avoid.

So, is there a better way of doing it. Should I be prepared for unforeseen consequences?

object test {

  abstract class SolutionFinder[T <: Problem[S, T], S <: SolutionFinder[T, S]] {
    def findSolution(problem: T): Unit
  }

  abstract class SpecificSolutionFinder[T <: SpecificProblem[S, T], S <: SpecificSolutionFinder[T, S]]
    extends SolutionFinder[T, S] {
    def findSolution(specificProblem: T): Unit = findSpecificSolution(specificProblem)
    def findSpecificSolution(specifProblem:T): Unit
  }

  class ConcreteSpecificSolutionFinder
    extends SpecificSolutionFinder[ConcreteSpecificProblem,ConcreteSpecificSolutionFinder] {
    def findSpecificSolution(specificProblem: ConcreteSpecificProblem): Unit = {
      // Actual solution to problem goes here
    }
  }

  abstract class Problem[T <: SolutionFinder[S, T], S <: Problem[T, S]](solutionFinder: T) {
    def solve(): Unit = solutionFinder.findSolution(this.asInstanceOf[S]) // I WANT TO AVOID THIS
  }

  abstract class SpecificProblem[T <: SpecificSolutionFinder[S, T], S <: SpecificProblem[T, S]](specificSolutionFinder: T)
    extends Problem[T, S](specificSolutionFinder) {
    // Problem specific fields and methods
  }

  class ConcreteSpecificProblem(concreteSpecificSolutionFinder: ConcreteSpecificSolutionFinder)
    extends SpecificProblem[ConcreteSpecificSolutionFinder,ConcreteSpecificProblem](concreteSpecificSolutionFinder) {
    // Problem specific fields and methods with values
  }

  def main(args: Array[String]): Unit ={
    val solutionFinder = new ConcreteSpecificSolutionFinder
    val problem = new ConcreteSpecificProblem(solutionFinder)

    // The aim of the codes structure is to avoid writing
    solutionFinder.findSolution(problem) // (which works, but is not appropriate for how this is used)
    // and write
    problem.solve()
    // instead
  }
}

There is no implicit inference that S = S[S,T] = Problem[S,T]. But you can add an explicit one:

abstract class Problem[T <: SolutionFinder[S, T], S <: Problem[T, S]](solutionFinder: T) {
  this: S =>
    def solve(): Unit = solutionFinder.findSolution(this)
}

A better design might be to disentangle the Problem and Solution. Perhaps the solve could be moved out of Problem, and require the Solution only at the point of use?

trait Problem
trait Solution[P <: Problem] {def solve(p: P): Unit }
class ConcreteProblem extends Problem
class ConcreteSolution extends Solution[ConcreteProblem]
def solve[P <: Problem](p: P, s: Solution[P]) = s.solve(p)

If you need a more sophisticated relation between Problem and Solution, you could create a typeclass for the "relationship", with implicit instances available:

trait KnowsHowToSolve[P <: Problem, S <: Solution] {
  def solve(p: P, s: S): Unit
}
implicit object ConcreteProblemKnowsHowToSolveConcreteSolution extends KnowsHowToSolve[ConcreteProblem, ConcreteSolution] {
  def solve(p: ConcreteProblem, s: ConcreteSolution) = s.findSpecificSolution(p)
}
def solve[P <: Problem, S <: Solution](p: P, s: S)(implicit ks: P KnowsHowToSolve S) = ...


"Breaking out" elements from an array I wrote a method that takes an array T[] array (T is a type parameter) and a second array int[] indices. The method should remove those entries from array, whose indices are stored in indices. Then, the resulting array will be "returned" as an out-parameter T[] rest. Finally, there is a second out parameter T[] removed that contains the removed items.

public static void RemoveViaIndex<T>(this T[] array, int[] indices, out T[] rest, out T[] removed)
{
    int arrayLength = array.Length;
    int indicesLength = indices.Length;

    if (indicesLength == 0 || arrayLength == 0)
    {
        rest = array;
        removed = new T[0];
        return;
    }

    rest = new T[arrayLength - indicesLength];
    removed = new T[indicesLength];

    int copyBegin = 0;
    int copyEnd = -1;

    // We will only iterate over the index-array once
    // Therefore, the indices have to be in ascending order
    Array.Sort(indices);

    for (int indexForIndices = 0; indexForIndices < indicesLength; indexForIndices++)
    {
        // Copying elements in a chunk that ends at the
        // next index that has to be removed
        copyEnd = indices[indexForIndices];
        Array.Copy(sourceArray: array, sourceIndex: copyBegin,
            destinationArray: rest, destinationIndex: copyBegin - indexForIndices,
            length: copyEnd - copyBegin);
        removed[indexForIndices] = array[copyEnd];
        copyBegin = copyEnd + 1;
    }

    if (copyEnd < arrayLength - 1)
    {
        // Copy the final chunk that the loop did not take (if there is one)
        Array.Copy(array, copyBegin, rest, copyEnd - indicesLength + 1, arrayLength - copyBegin);
    }
}

I am assuming that all the indices in same-named array are valid indices for array. Are there any further border cases that I did not cover?

Furthermore, I wonder if this way is faster than an if statement that is contained within a for loop that iterates over the source array since the if-currentIndex-in-indices-then-remove branch disappears:

for (*every index in source*)
{
    if (*current-index-in-indices-to-remove*)
    {
        *remove-and-put-to-target-array*
    }
}

So far my tests indicate that the performance is slightly better in the larger test cases because there is no hashing involved when accessing elements in deleteStatusArray. There are also no array copy operations.

public static void ZRemoveIndex<T>(this T[] array, int[] indices, out T[] rest, out T[] removed)
{
    int arrayLength = array.Length;
    int indicesLength = indices.Length;

    if (indicesLength == 0 || arrayLength == 0)
    {
        rest = array;
        removed = new T[0];
        return;
    }

    rest = new T[arrayLength - indicesLength];
    removed = new T[indicesLength];
    //memo to remember which array indices are removed
    //all elements will be false by default
    var deleteStatusArray = new bool[array.Length];

    foreach (var removeIndex in indices)
    {
        //mark index that should be considered deleted
        deleteStatusArray[removeIndex] = true;
    }

    var notRemovedCounter = 0;
    var removedCounter = 0;
    for (int i = 0; i < array.Length; i++)
    {
        if(!deleteStatusArray[i])
            rest[notRemovedCounter++]= array[i];
        else
            removed[removedCounter++] = array[i];
    }
}


Amending code to make it list some files in a directory, rather than all The method list_all() is the code I am starting with. It just lists all files in "c:\windows".

I want to adjust it to list only the first 10 files (if there are as many files as that there).

I can see that Directory.GetFiles(sDir) returns an array of string.  The original method list_all(), takes all of them.  I want mine to just take the first 10, or if there's less than 10 then all of them.  But i've added four lines of code and used a list, to do it. My amended method is list_some()

It's far from a disaster, but I'm sure there's a better way though I'm not sure.

using System;
using System.Collections.Generic;
using System.IO;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {

            static void list_all()
            {
                string sDir = "c:\\windows";

                foreach (string f in Directory.GetFiles(sDir))
                {
                     Console.WriteLine(f);
                }

            }

            static void list_some()
            {
                string sDir="c:\\windows";

                List<string> lstsomefiles = new List<string>();
                int count = Directory.GetFiles(sDir).Length;
                string[] strs_fileslst = Directory.GetFiles(sDir);

                for (int i = 0; i < count && i < 10; i++) lstsomefiles.Add(strs_fileslst[i]);

                foreach (string f in lstsomefiles)
                {

                    Console.WriteLine(f);
                }

            }

        }
    }
}

You can utilize the .Take() method in LINQ which:

  Returns a specified number of contiguous elements from the start of a sequence

Using this, your code would look as follows:

string sDir = "c:\\windows";

foreach (string f in Directory.GetFiles(sDir).Take(10))
{
    Console.WriteLine(f);
}


Parsing a full site using python I've created a crawler which is scraping name, phone number and web address of each profile from houzz website. Hope I did it the right way. Here is what I've written:

import requests
from lxml import html

url="https://www.houzz.com/professionals/"

def scraper_func(mainurl):
    response = requests.get(mainurl).text
    tree = html.fromstring(response)
    for titles in tree.xpath("//a[@class='sidebar-item-label']"):
        link = titles.xpath(".//@href")
        for item in link:
            paging_stuff(item)

# Done crawling links to the category from left-sided bar
def paging_stuff(process_links):
    response = requests.get(process_links).text
    tree = html.fromstring(response)
    for titles in tree.xpath("//ul[@class='pagination']"):
        link = titles.xpath(".//a[@class='pageNumber']/@href")
        for item in link:
            processing_stuff(item)

# Going to each page to crawl the whole links spread through pagination connected to the profile page
def processing_stuff(procured_links):
    response = requests.get(procured_links).text
    tree = html.fromstring(response)
    for titles in tree.xpath("//div[@class='name-info']"):
        links = titles.xpath(".//a[@class='pro-title']/@href")[0]
        main_stuff(links)

# Going to the profile page of each link
def main_stuff(main_links):
    response = requests.get(main_links).text
    tree = html.fromstring(response)

    def if_exist(titles,xpath):
        info=titles.xpath(xpath)
        if info:
            return info[0]
        return ""

    for titles in tree.xpath("//div[@class='profile-cover']"):
        name = if_exist(titles,".//a[@class='profile-full-name']/text()")
        phone = if_exist(titles,".//a[contains(concat(' ', @class, ' '), ' click-to-call-link ')]/@phone")
        web = if_exist(titles,".//a[@class='proWebsiteLink']/@href")
        print(name,phone,web)

scraper_func(url)

First of all, you should definitely re-use the same session for multiple requests to the same domain - it should result into a performance improvement:

  if you're making several requests to the same host, the underlying TCP connection will be reused, which can result in a significant performance increase

Other Improvements

improve naming: you are over reusing item and titles variables. Instead, think of more appropriate and meaningful variable names. Also, I don't think the "_stuff" prefix contributes to readability and ease of understanding of the program
put the main script execution logic to under if __name__ == '__main__': to avoid executing it on import
you can avoid inner loops and iterate directly over hrefs here:

for link in tree.xpath("//a[@class='sidebar-item-label']/@href"):
    paging_stuff(link)

And here:

for link in tree.xpath("//ul[@class='pagination']//a[@class='pageNumber']/@href"):
    processing_stuff(link)

instead of putting comments before the functions, put them into appropriate docstrings

Notes

Note that you should realize that your solution is synchronous - you are processing urls sequentially one by one. If performance matters, consider looking into Scrapy.


Is this use of an array of function names and bracket-notation function calling a hacky/evil solution? Background: I was making a chrome extension that injects movie ratings into theatres' showtimes pages. The problem I was duplicating a lot of code calling similar but just slightly different functions. Sometimes I wanted to do an $(el).prepend, sometimes $(el).append, or $(el).after, or $(el).find('something').prepend, or $(el).closest('something').prepend/append/after.

An example of an instance of a function I'm trying to generalize/abstract:

$('.FFEC-Display').each(function(i, el){
    var title = $(el).find('h3').text().replace(blacklistRegexp, '').trim();
    /* can ignore the three lines below, they are always the same
    var tomato = findTomato(title, tomatoes);
    var classes = ["poster-width", "overlay"];
    var templateData = tomato ? prepareData(tomato, classes) : { query: title, classes: namespaceClasses(classes) };
    */
    $(el).find('.movie-link').prepend(Handlebars.templates.ratings(templateData)); //this is the line that gives me trouble
});

Another instance:

$('.MovieItemsRotator .item').each(function(i, el){
    var title = $(el).find('.Title').text().replace(blacklistRegexp, '').trim();
    /* you can ignore the code in here
    var tomato = findTomato(title, tomatoes);
    var classes = ["overlay", "overlay-poster"];
    var templateData = tomato ? prepareData(tomato, classes) : { query: title, classes: namespaceClasses(classes) };
    */
    $(el).prepend(Handlebars.templates.ratings(templateData));
});

I have three more instances of very similar functions, and plan on adding more. Full code page here on GitHub

One of the half-joking answers in my SO question inspired me to write a solution using strings of function names, iteratively called on $(el) to solve the problem.

It started out as an exercise just because it seemed like too fun a solution not to try and write, but now I'm feeling more tempted to actually use the code, as opposed to this other one, which seems much more readable/maintainable. Hopefully people with more experience can give me some advice.

The code in question:

json = [
  {target: '.FFEC-Display', title : 'h3', actions : [{action: 'find', target: '.movie-link'}, 'prepend']},
  {target: '.MovieItemsRotator .item', title: '.Title', actions: ['prepend']}
];

$.each(json, function(index, j){
  $(j.target).each(function(i, el){
    var title = $(el).find(j.title).text().replace(blacklistRegexp, '').trim();
    /* you can ignore the code in here
    var tomato = findTomato(title, tomatoes);
    var classes = ["poster-width", "overlay"];
    var templateData = tomato ? prepareData(tomato, classes) : { query: title, classes: namespaceClasses(classes) };
    */
    var abomination = $(el);
    $.each(j.actions, function(k, action){
      if(typeof action === "string") {
        abomination = abomination[action]();
      } else {
        abomination = abomination[action.action](action.target);
      }
    });
    abomination(Handlebars.templates.ratings(templateData));
  });
});

Firstly, I wouldn't say it's evil. From JS's point of view, obj["foo"]() is identical to obj.foo(). The dot-syntax and bracket-syntax are completely interchangeable. Of course, if you call your variable "abomination" then, yeah, it's going to sound evil.

Anyway, I think there's a bug or two in your code. When you loop through the json array and get to the actions property, you call the function on $(el) right away. This won't actually do anything, as far as I can tell.

Here's your code, manually interpreted, so to speak:

// this line does nothing; abomination is still === $(el)
abomination = abomination[action](); // same as: abomination = $(el)["prepend"]();

// ...

// and this line fails with "error: object is not a function", because $(el) is indeed not a function
abomination(Handlebars.templates.ratings(templateData)); // same as: $(el)(Handlebars.templates.ratings(templateData))

Also, I imagine the objects in the json array are meant to work on different sites. In that case, I'd key them to the site's domain, so you don't have to loop through everyone of the them on every site. This also eliminates the risk of "false positives" such as if a two different sites both use, say, "movie" as a class name.

Here is what I'd probably do. I've changed names here and there to make 'em more descriptive, but the comments should make it fairly clear

var sites = { // slightly more descriptive name than "json"
  'movietheatre.com': {
    container: '.FFEC-Display', // changed the name here too..
    title:     'h3',
    select:    [
      { name: 'find', args: ['.movie-link'] } // note that args is an array
    ],
    action:    'prepend'
  },

  'showtimes.randomcinema.com': { // note there's no `select` property here
    container: '.MovieItemsRotator .item',
    title:     '.Title',
    action:    'prepend'
  }
};

var domain = window.location.hostname.replace(/^www\./, ""), // the domain (incl. subdomain unless it's "www")
    config = sites[domain]; // get the config

// only do stuff if there's a configuration for the current domain
if( config ) {
  $(config.container).each(function () {
    var container = $(this);

    // I'd put the blacklist-replace/trim stuff into a `getRatings()` function
    // that does all the stuff you say is the always the same anyway. It can also
    // do the template rendering, while it's at it
    var title   = container.find(config.title).text();
    var ratings = getRatings(title); // get the rendered template

    // maybe throw in a check here, in case `getRatings` fails/refuses to return something
    if( !ratings ) { return; }

    // Now for the fun stuff
    var target = container;

    // if there's no sub-select-stuff, default to an empty array
    // (and use the built-in forEach instead of jQuery)
   (config.select || []).forEach(function (call) {
      // use `apply()` to call the requested function with the requested args (if any)
      target = target[call.name].apply(target, call.args || []);
    });

    // now `target` should be the right element
    // and we can call the required insert/append/prepend action
    target[config.action](ratings); // i.e. target.prepend(ratings)
  });
}


Equals method implemented in the class Is this a proper way to implement an equality check?  In Equals method, I am relying on    bad_cast exception to know if the objects are of the same class or not.

Is there any other way to implement Equals() in C++?

class Shape
{
public:
    virtual ~Shape() = 0;
    bool operator==(const Shape& s)
    {
        return Equals(s);
    }

    virtual bool Equals(const Shape& s) = 0;
};

class Circle : public Shape
{

    bool Equals(const Shape& c) override
    {
        try
        {
            const Circle& other = dynamic_cast<const Circle&>(c);
                      // condition to check equality.
        }
        catch(std::bad_cast&)
        {
            return false;
        }
        catch(...)
        {
            throw;
        }
    }
};

class Square : public Shape
{
    bool Equals(const Shape& s)  override
    {
        try
        {
            const Square& other = dynamic_cast<const Square&>(s);
                      // condition to check equality.
        }
        catch(std::bad_cast&)
        {
            return false;
        }
        catch(...)
        {
            throw;
        }
    }
};

typedef std::shared_ptr<Shape> ShapePtr;
typedef std::vector<ShapePtr> Shapes;

Shapes LoadShapes()
{
    Shapes shapes;
    shapes.push_back(std::make_shared<Circle>(42));
    shapes.push_back(std::make_shared<Circle>(52));
    shapes.push_back(std::make_shared<Circle>(62));
    shapes.push_back(std::make_shared<Square>(10));
    return shapes;
}

int main()
{
    auto circle = std::make_shared<Circle>(42);
    auto shapes = LoadShapes();
    for ( auto& shape : shapes)
    {
        if ( *shape == *circle)
        {
            std::cout << *shape << "\n";
        }
    }
}

EDIT

Made the operator== as non-virtual method.

Sorry to bump this one year old question, but there is something inherently wrong in your equality check if you ever extend to more than one level of inheritance.

Consider a class Foo deriving from Circle, assuming the same implementation (with changed types of course).

Foo foo;
Circle circle;
Shape & shapeFoo = foo;
Shape & shapeCircle = circle;

// these two should behave the same, but they don't:
std::cout << (shapeFoo == shapeCircle) << std::endl;
std::cout << (shapeCircle == shapeFoo) << std::endl;

Why does this fail?

shapeFoo == shapeCircle will call shapeFoo.Equals(shapeCircle) which will be dispatched on the implementation of Equals in class Foo. There the dynamic_cast<Foo const *> will fail, correctly returning false.

However shapeCircle == shapeFoo will call shapeCircle.Equals(shapeFoo) which will be dispatched on the implementation of Equals in class Circle. There the dynamic_cast<Circle *> will succeed (as Foo is a descendant of Circle). Then the method will compare only the attributes of Circle, probably returning true.


Summing outerHeight of multiple divs I am summing the total .outerHeight() of multiple divs like this:

var h = $('#div1').outerHeight() + $('#div2').outerHeight() + $('#div3').outerHeight() + $('#div4').outerHeight();

Is there any way to do this more efficiently?

I was considering something like this (but it doesn't work):

$('#div1, #div2, #div3').outerHeight();

You'll have to add a class to the divs to be able to select them all

Then, use $.each() (http://api.jquery.com/each/)

var sum = 0;
$( ".sumdiv" ).each(function( index ) {
    sum += $(this).outerHeight()
});