Getting Started with Adobe After Effects - Part 6: Motion Blur
First Time? You can support us by signing up. It takes only 5 seconds. Click here to sign up. If you already have an account, click here to login.

View in another Language

Sponsored [Advertise Here]
Don't have a blog yet?
beyondrelational.com
Start writing a blog at beyondrelational.com, get recognized and build your brand

Syndicate your blog!
beyondrelational.com
Syndicate your blog with us to get wider reach into the technology community. Click here to get started.

Izenda Reports
izenda.com
Ad Hoc reporting for ASP.NET that actually works

TSQL Challenge 58 - Playing Chess in TSQL – Part 1

Let us play Chess with TSQL in this challenge. The input table contains strings that represent the layout of chess boards filled with pieces using Forsyth-Edwards Notation. Your job is to read the input string and generate a result set that represents the position of pieces in a chess board.

Want to make SQL effortless?
With code-completion, SQL reformatting, script summaries and more, SQL Prompt 5 makes writing, editing, and exploring SQL effortless. Download a 14-day free trial now.

Sample Data

  
ID  Layout
--- ----------------------------------------------------
1   rnbqk2r/ppppbppp/5n2/4p3/2B1P3/5Q2/PPPP1PPP/RNB1K1NR

Each row in the input table represents the current position of a chess board filled with pieces at various positions. The sample input given above represents a chess board as given below.



Your job is to translate this into a result set that looks like the below illustration.


Expected Results

  
ID RowID  a    b    c    d    e    f    g    h
-- -----  ---- ---- ---- ---- ---- ---- ---- ----
1      8  r    n    b    q    k              r
1      7  p    p    p    p    b    p    p    p
1      6                           n         
1      5                      p              
1      4            B         P              
1      3                           Q         
1      2  P    P    P    P         P    P    P
1      1  R    N    B         K         N    R

Rules

  1. The input table will have more than one row.
  2. The output should be a single result set with 10 columns. The first column should show the ID of the input table. and second column should be RowID in decending order.
  3. 8 rows should be generated for every row in the input table. The 8 rows representing each chess board should be ordered as specified in Algebraic Chess Notation. The first column of first row should represent cell “8a” and the first cell of the 8th row should represent cell “1a”.
  4. Chess boards should be first ordered by ID in ascending and then RowID in decending order.

Sample Script

Use the TSQL Script given below to generate the source tables and fill them with sample data.

IF OBJECT_ID('TC58','U') IS NOT NULL BEGIN
	DROP TABLE TC58
END
GO

CREATE TABLE TC58(
	ID  INT IDENTITY PRIMARY KEY,
	Layout VARCHAR(MAX)
)
GO

INSERT INTO TC58(Layout)
SELECT 'rnbqk2r/ppppbppp/5n2/4p3/2B1P3/5Q2/PPPP1PPP/RNB1K1NR' 

SELECT * FROM TC58

Restrictions

  1. The solution should be a single query that starts with a "SELECT" or “;WITH”

Notes

  1. Read the Submission Guidelines and make sure that your solution follows them.
  2. If you would like to use a Tally Table, you can use the script given here. Your solution should not include the script to create and populate the tally table. You can assume that the tally table will be available in the database where the evaluation team will run your Code.
SQL Source Control - now with static data support
"By allowing static data to be versioned, SQL Source Control now becomes a complete source of truth for the application's data layer. I'm in." Troy Hunt, Software Architect.
More information and a free trial.

Jacob Sebastian
1 · 100.00% · 32235
Submit a Solution
View Solutions
View All Challenges Previous Challenge Next Challenge
3
Liked
15
Comments
152
Solutions



Submit

15   Comments   

Subscribe to Notifications
  • Your Expected Results have 10 columns, but rule 2. says 9 columns (without RowId)

    commented on Jun 13 2011 2:36AM  .  Report Abuse This post is not formatted correctly
    Mario Puskaric
    154 · 1% · 321
  • @MarioPuskaric,

    Corrected the Rule 2: The output should be a single result set with 10 columns. The first column should show the ID of the input table. and second column should be RowID in decending order.

    commented on Jun 13 2011 4:56AM  .  Report Abuse This post is not formatted correctly
    puzzles
    13 · 13% · 4057
  • (missed the submit button, sorry, first time here...)

    commented on Jun 13 2011 5:05AM  .  Report Abuse This post is not formatted correctly
    allmhuran
    2038 · 0% · 8
  • Is the first empty row in output on purpose? I assume that 8x8 chessboard is expected. :)

    commented on Jun 14 2011 5:36AM  .  Report Abuse This post is not formatted correctly
    Szymon Wojcik
    67 · 3% · 877
  • @phobosq,
    There is no any empty row in expected results.

    commented on Jun 14 2011 8:16AM  .  Report Abuse This post is not formatted correctly
    puzzles
    13 · 13% · 4057
  • Hello,

    Does the result set have to be ordered? when testing my solution the cost of sorting is around 60% while the processing on it's own just twice as expensive as simple select * from tc58. Sure you could trick it to sort the output without using order by but there is no guarantee it will work every time (due to parallel processing).

    when testing with 1280 FENs in tc58 table these are the results i'm getting

    no ordering: 120ms standard ordering: 600ms tricky ordering: 130ms

    simple select: 60ms

    not knowing the size of the test dataset and the machine it will run on it will be very hard to optimise it any further. so I guess I'll submit standard and tricky ordering and see which one you are happier with.

    commented on Jun 14 2011 8:59AM  .  Report Abuse This post is not formatted correctly
    lof
    224 · 1% · 201
  • I've just noticed that most of the people submit more than one solution straight away. Some as many as 5. I guess then for most it's more of a lottery than an engineering challenge. Well... let's see how my modest two will do.

    commented on Jun 14 2011 9:09AM  .  Report Abuse This post is not formatted correctly
    lof
    224 · 1% · 201
  • @lof,

    The output should be ordered as specified in the Rules

    commented on Jun 14 2011 9:55AM  .  Report Abuse This post is not formatted correctly
    Jacob Sebastian
    1 · 100% · 32235
  • How should an empty square be represented in the output? (NULL, empty string, space, etc.)

    commented on Jun 14 2011 11:45AM  .  Report Abuse This post is not formatted correctly
    Tim Bailey
    668 · 0% · 50
  • Has anyone got any test data and timings?

    Here is my simple test data to generate 1000 rows, I don't think there is much benefit in generating test data with different board contents, other than to test the results are correct:

    truncate table TC58

    go

    INSERT INTO TC58(Layout)

    SELECT 'rnbqk2r/ppppbppp/5n2/4p3/2B1P3/5Q2/PPPP1PPP/RNB1K1NR'

    go 1000

    commented on Jun 14 2011 1:21PM  .  Report Abuse This post is not formatted correctly
    Jonathan Roberts
    76 · 2% · 745
Previous 1 | 2 Next

Your Comment


Sign Up or Login to post a comment.


Managed Windows Shared Hosting by OrcsWeb

Copyright © Rivera Informatic Private Ltd.