Incepand cu versiunea 11 Release 2 Oracle introduce un nou feature in RDBMS lor, Recursive Subquery Factoring. Prima aplicatie practica la care s-a gandit Anton Scheffer este cum sa rezolve SUDOKU
<pre>with x( s, ind ) as ( select sud, instr( sud, ' ' ) from ( select '53 7 6 195 98 6 8 6 34 8 3 17 2 6 6 28 419 5 8 79' sud from dual ) union all select substr( s, 1, ind - 1 ) || z || substr( s, ind + 1 ) , instr( s, ' ', ind + 1 ) from x , ( select to_char( rownum ) z from dual connect by rownum <= 9 ) z where ind > 0 and not exists ( select null from ( select rownum lp from dual connect by rownum <= 9 ) where z = substr( s, trunc( ( ind - 1 ) / 9 ) * 9 + lp, 1 ) or z = substr( s, mod( ind - 1, 9 ) - 8 + lp * 9, 1 ) or z = substr( s, mod( trunc( ( ind - 1 ) / 3 ), 3 ) * 3 + trunc( ( ind - 1 ) / 27 ) * 27 + lp + trunc( ( lp - 1 ) / 3 ) * 6 , 1 ) ) ) select s from x where ind = 0 /</pre>
Astfel ca daca vrem sa rezolvam exemplul de mai jos folosim codul de mai sus

Sudoku Example
Practic folosim sirul din exemplu “53 7 6 195 98 6 8 6 34 8 3 17 2 6 6 28 419 5 8 79″ si ii aplicam sintaxa SQL. Rezultatul este rezolvarea la exemplul nostru.

Sudoku Example - Rezolvarea
Rareori vezi cod care sa te impresioneze.
[Via technology.amis.nl]
You can leave a response, or trackback from your own site.
2 Responses to “Sudoku – Recursive Subquery Factoring”
Leave a Reply




ce bine. Acum stim cu totii cum sa rezolvam sudoku
Hehe
sa sti ca in general cam asta face OM in loc sa il rezolve cum facea bunicu in troleu cu creionu si foaia cauta tot felul de solutii alternative, automatizari, sisteme, orice numai sa nu faca el ceva. Pe de alta parte este un foarte bun exercitiu de programare, dar parerile sunt impartite.