SQL
SQLite3

せっかく「Hello,World!」書いたのに転職先が見つからない。

もう人生嫌になった。金くれー。

まず、数字のテーブル。

CREATE TABLE X as
WITH X(n) AS (
        SELECT 0
        UNION ALL SELECT n + 1 FROM X WHERE n < 9)
    SELECT * FROM X;

で、文字のテーブル。

CREATE TABLE C AS SELECT c FROM (SELECT DISTINCT SUBSTR('SENDMOREMONEY',1 + X.n + 10 * X1.n,1) AS c FROM X,X AS
X1) WHERE c <> '';

部品その1

SELECT CASE WHEN rowid=1 THEN '' ELSE ', ' END || 'X AS ' || c FROM C;

実行結果

X AS S
, X AS N
, X AS E
, X AS Y
, X AS D
, X AS M
, X AS O
, X AS R

部品その2

SELECT 'AND ' || C1.c || '.n <> ' || C2.c || '.n' FROM C as C1, C as C2 WHERE C1.c < C2.c;

実行結果

AND S.n <> Y.n
AND N.n <> S.n
AND N.n <> Y.n
AND N.n <> O.n
AND N.n <> R.n
AND E.n <> S.n
AND E.n <> N.n
AND E.n <> Y.n
AND E.n <> M.n
AND E.n <> O.n
AND E.n <> R.n
AND D.n <> S.n
AND D.n <> N.n
AND D.n <> E.n
AND D.n <> Y.n
AND D.n <> M.n
AND D.n <> O.n
AND D.n <> R.n
AND M.n <> S.n
AND M.n <> N.n
AND M.n <> Y.n
AND M.n <> O.n
AND M.n <> R.n
AND O.n <> S.n
AND O.n <> Y.n
AND O.n <> R.n
AND R.n <> S.n
AND R.n <> Y.n

あとはこつこつ

WITH X(n) AS (
        SELECT 0
        UNION ALL SELECT n + 1 FROM X WHERE n < 9)
SELECT
    ((S.n * 10 + E.n) * 10 + N.n) * 10 + D.n AS SEND
    , ' + ' AS OP
    , ((M.n * 10 + O.n) * 10 + R.n) * 10 + E.n AS MORE
    , ' = ' AS EQ
    , (((M.n * 10 + O.n) * 10 + N.n) * 10 + E.n) * 10 + Y.n AS MONEY
FROM
    X AS S
    , X AS N
    , X AS E
    , X AS Y
    , X AS D
    , X AS M
    , X AS O
    , X AS R
WHERE
    ((S.n * 10 + E.n) * 10 + N.n) * 10 + D.n
        + ((M.n * 10 + O.n) * 10 + R.n) * 10 + E.n
        = (((M.n * 10 + O.n) * 10 + N.n) * 10 + E.n) * 10 + Y.n
    AND S.n > 0
    AND M.n > 0
    AND S.n <> Y.n
    AND N.n <> S.n
    AND N.n <> Y.n
    AND N.n <> O.n
    AND N.n <> R.n
    AND E.n <> S.n
    AND E.n <> N.n
    AND E.n <> Y.n
    AND E.n <> M.n
    AND E.n <> O.n
    AND E.n <> R.n
    AND D.n <> S.n
    AND D.n <> N.n
    AND D.n <> E.n
    AND D.n <> Y.n
    AND D.n <> M.n
    AND D.n <> O.n
    AND D.n <> R.n
    AND M.n <> S.n
    AND M.n <> N.n
    AND M.n <> Y.n
    AND M.n <> O.n
    AND M.n <> R.n
    AND O.n <> S.n
    AND O.n <> Y.n
    AND O.n <> R.n
    AND R.n <> S.n
    AND R.n <> Y.n
;

実行結果

SEND    OP      MORE    EQ      MONEY
9567     +      1085     =      10652

え、Python でやれと?

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sqlite3


def main():
    try:
        conn = sqlite3.connect(':memory:')
        strSQL = '''
WITH X(n) AS (
        SELECT 0
        UNION ALL SELECT n + 1 FROM X WHERE n < 9)
SELECT
    ((S.n * 10 + E.n) * 10 + N.n) * 10 + D.n AS SEND
    , ' + ' AS OP
    , ((M.n * 10 + O.n) * 10 + R.n) * 10 + E.n AS MORE
    , ' = ' AS EQ
    , (((M.n * 10 + O.n) * 10 + N.n) * 10 + E.n) * 10 + Y.n AS MONEY
FROM
    X AS S
    , X AS N
    , X AS E
    , X AS Y
    , X AS D
    , X AS M
    , X AS O
    , X AS R
WHERE
    ((S.n * 10 + E.n) * 10 + N.n) * 10 + D.n
        + ((M.n * 10 + O.n) * 10 + R.n) * 10 + E.n
        = (((M.n * 10 + O.n) * 10 + N.n) * 10 + E.n) * 10 + Y.n
    AND S.n > 0
    AND M.n > 0
    AND S.n <> Y.n
    AND N.n <> S.n
    AND N.n <> Y.n
    AND N.n <> O.n
    AND N.n <> R.n
    AND E.n <> S.n
    AND E.n <> N.n
    AND E.n <> Y.n
    AND E.n <> M.n
    AND E.n <> O.n
    AND E.n <> R.n
    AND D.n <> S.n
    AND D.n <> N.n
    AND D.n <> E.n
    AND D.n <> Y.n
    AND D.n <> M.n
    AND D.n <> O.n
    AND D.n <> R.n
    AND M.n <> S.n
    AND M.n <> N.n
    AND M.n <> Y.n
    AND M.n <> O.n
    AND M.n <> R.n
    AND O.n <> S.n
    AND O.n <> Y.n
    AND O.n <> R.n
    AND R.n <> S.n
    AND R.n <> Y.n
;
'''
        lines = conn.execute(strSQL).fetchall()
        for line in lines:
            print(line)
    finally:
        conn.close()


if __name__ == '__main__':
    main()

実行結果

>>> SMM.main()
(9567, ' + ', 1085, ' = ', 10652)

汎用の覆面算でやれと?
・・・それはまた別の機会に ....((((^^;)

※ 2017/12/08 21:38 SQL文修正。