This challenge is not a real time problem directly, but it measures about logical thinking. The problem is all about finding the factorial of numbers. Though it is known to most of us what a factorial is, but to recall the concept here is an example:
Factorial of 3 is 1*2*3 = 6 i.e. the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n.
DECLARE @Fact TABLE(Nums INT)
INSERTINTO @Fact
SELECT 0 UNIONALLSELECT 1 UNIONALLSELECT 3 UNIONALLSELECT 5 UNIONALLSELECT 10
DECLARE @no INT,@f BIGINT,@nums INTDECLARE C1 CURSOR FOR SELECT nums FROM @fact
OPEN C1
FETCH NEXT FROM C1 INTO @no
WHILE@@FETCH_STATUS=0
BEGIN SELECT @f=1,@nums=@no
WHILE @no>0
BEGINSET @f=@f*@no
SET @no=@no-1
ENDDECLARE @t TABLE(nums INT,Fact BIGINT)
INSERT INTO @T SELECT @Nums,@F
FETCH NEXT FROM C1 INTO @no
ENDCLOSE C1
DEALLOCATE C1
SELECT * FROM @T ORDER BY 1
Just Paste Above solution in your SQL Server You will get a
Here is a challenge that takes you away from those repetitive boring type of queries that you write over and over again, several times a day. All of us, the database people, are familiar with thinking in set based manner as well as row by row style. Here is something that is very interesting where you might need to process records in a 'three-line-at-a-time' fashion.
For the purpose of this challenge, imagine that you are working for a bank which just decided to scan all the banking documents. Assume that they have an old fashioned scanner which scans the documents and produces a text file with the customer number. So far so good. Well, not really! Unfortunately the scanner produces a graphical representation of the customer number using three lines of symbols: space, unerscores and pipe characters.
Here is an example of the output produced by the scanner.
Here are the rules to keep in mind while reading and recognizing the output generated by the scanner.
Each digit is represented using 9 cells (3x3)
Only spaces, underscores and pipe characters are used
The number of digits in each account number may vary.
The Scanner is not 100% reliable and it might produce some digits that are invalid
The Challenge
Your job is to read the output produced by the scanner and identify the the customer number represented by each image. Remember that the scanner is not very reliable and it might produce invalid digit representations. For each digit that is not valid, set the value to 'X'
Sample Data
Here is the sample data for this challenge. Please take care with spaces, tabs and carriage returns as each digit is represented by three lines of text and if a space, tab or carriage return is misplaced, the whole image will be distorted.
IdScanNumber
----------- ---------------------------
1_________
| || || || || ||| ||_ |_|
|_||_||_||_||_|||_| _| _|
2_______
|_||_|| || ||_||||_
| _||_||_||_|||| _|
3________
|_ |_|| || ||_ |_| _|||_|
|_||_||_||_||_||_||_| _|
4_______
|_||_|| ||_||_||||_
| _||_||_||_|||||_|
5_______
| ||_|| ||_||_||||_
| _||_||_||_|||||_|
6_______
| || _| _||_||_ |_||_|
|_|||__|| _||_|||_|
Expected Results
Based on the sample input and the rules discussed earlier, here is the expected output.
IdValue
----------- ---------
1000007059
2490067715
3680X68279
4490867716
5X90867716
6012345678
Sample Scripts
Use the following script to generate the sample data for this challenge.
DECLARE @t TABLE (Id int, ScanNumber NVARCHAR(116))
INSERTINTO @t
SELECT1,--> 000 007 059
'_________
| || || || || ||| ||_ |_|
|_||_||_||_||_|||_| _| _|
'UNION
SELECT 2,--> 490 067 715
'_______
|_||_|| || ||_||||_
| _||_||_||_|||| _|
'UNION
SELECT3, --> 680 X68 279
'________
|_ |_|| || ||_ |_| _|||_|
|_||_||_||_||_||_||_| _|
'UNION
SELECT4,--> 490 867 716
'_______
|_||_|| ||_||_||||_
| _||_||_||_|||||_|
'UNION
SELECT5,--> X90 867 716
' _______
| ||_|| ||_||_||||_
| _||_||_||_|||||_|
'
UNION
SELECT 6,--> 012 345 678
'_______
| || _| _||_||_ |_||_|
|_|||__|| _||_|||_|
Notes
Each record may have more than three lines of data (each line is separated by a CR and LF). Your code should consider only the first three lines.
The length of the first three lines of each recrd will always be the same and will be divisible by three.
There may be 3x3 blocks of spaces in the string. In such a case, you should generate an empty space in the output. If a 3x3 block does not create a valid digit (except for the case of a 3x3 block of spaces), you should generate an "X".
I am an academician, DBA and team leader at Department of Computer Applications. I am Oracle11g Certified Database Professional & Microsoft Certified Professional, earned Microsoft Database Administration and did Ph.D. in area of Big Data and Hadoop. I am IBM certified Academic associate – DB2 Database and applications & RAD Certified Developer. Nowadays working on Data Science and Mobile Application Development.
This channel will helps to learners who are enthusiastic for coding.
It also helps to the teachers as well as learners who want to learn Android, Java, Oracle, C, MongoDB, Flutter, SQL Server etc.
"For the Learners, by the Learners & of the Learners.
Thank You
Privacy Policy
-
Privacy Policy Effective date: March 08, 2019
PLSQL ("us", "we", or "our") operates the mobile application (the
"Service").
This page informs you of our po...