C# 和 SQL 难题的提示

发布于 2024-09-07 07:05:25 字数 1087 浏览 2 评论 0原文

我正在浏览 SO 职业,发现一份工作有一份 pdf 文件,其中包含一些他们希望申请人发送的谜题。

虽然我对这份工作不感兴趣,但我还是阅读了这些问题,并在 Visual Studio / SMSS 中进行了尝试。第一个问题相当容易解决,尽管我想不出是否有任何方法可以优化它(我用 C# 解决了它)。第二个谜题只有一个明显的解决方案让我印象深刻,我想不出任何其他解决方案。

我不确定在这里讨论这些问题是否不好,但如果有人能给我一些提示或者建议我可以在不造成任何悲伤的情况下问这个问题的地方,我将不胜感激。

问题在这里:http://www.debtx.com/doc/DebtX_Programming_Problems.pdf< /a>

我可以让第一个问题滑过去,但第二个问题却让我陷入了除明显问题之外的其他解决方法上。遗憾的是,SO 上没有 PM 功能...

第一部分的样板解决方案 C#:

public static bool Compare(int[] num, int[] div)
{
    for (int i = 0; i < num.Length; i++)
    {
        for (int j = 0; j < div.Length; j++)
        {
            if (num[i] % div[j] == 0)
                return true;
        }
    }

    return false;
}

我的 SQL 解决方案

select Table1.Key1, Table1.Key2 from Table1 inner join Table2 on Table1.Key1 = Table2.key2 where IsDeleted=0

select * from Table1 where key1 in(select Key2 from Table2 where IsDeleted=0)

虽然一切看起来都很相似

I was browsing SO careers and came across a job that had a pdf with a couple of puzzles they wanted applicants to send in.

Although I'm not interested in the job, I read the questions anyway and had a play in Visual Studio / SMSS. The first question was fairly easy to solve although I couldn't think if any way to optimise it (I solved it in C#). The second puzzle only one obvious solution strikes me and I can't think of any others.

I'm not sure if it's bad to discuss these questions here though, but if anyone can give me some hints or perhaps suggest somewhere where I can ask this without creating any grief it'd be appreciated.

The questions are in here: http://www.debtx.com/doc/DebtX_Programming_Problems.pdf

I could let the first one slide but the second one has me stumped on other ways of solving it than the obvious. Shame there's no PM function on SO...

Boilerplate solution for the first part C#:

public static bool Compare(int[] num, int[] div)
{
    for (int i = 0; i < num.Length; i++)
    {
        for (int j = 0; j < div.Length; j++)
        {
            if (num[i] % div[j] == 0)
                return true;
        }
    }

    return false;
}

My SQL Solutions

select Table1.Key1, Table1.Key2 from Table1 inner join Table2 on Table1.Key1 = Table2.key2 where IsDeleted=0

select * from Table1 where key1 in(select Key2 from Table2 where IsDeleted=0)

It all seems so samey though

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

怎会甘心 2024-09-14 07:05:25

几个使用伪 SQL 的示例,以免泄露太多

Not In

   SELECT * FROM TBL1 
   WHERE NOT IN (
            SELECT FROM TBL2  
            WHERE Deleted=0 AND Tbl2.Key1= Tbl1.Key1 AND Tbl2.Key2=Tbl1.Key2
            )

Not Exists

    SELECT * FROM TBL1 
    WHERE NOT EXISTS (
        SELECT FROM TBL2 
        WHERE Deleted =0 AND Tbl2.Key1= Tbl1.Key1 AND Tbl2.Key2=Tbl1.Key2
        ) 

Outter Join Is Null

    SELECT * FROM TBL1 LEFT JOIN TBL2 
    WHERE TBL2.Key1 IS NULL OR Deleted=0

couple of examples using pseudo SQL to not give too much away

Not In

   SELECT * FROM TBL1 
   WHERE NOT IN (
            SELECT FROM TBL2  
            WHERE Deleted=0 AND Tbl2.Key1= Tbl1.Key1 AND Tbl2.Key2=Tbl1.Key2
            )

Not Exists

    SELECT * FROM TBL1 
    WHERE NOT EXISTS (
        SELECT FROM TBL2 
        WHERE Deleted =0 AND Tbl2.Key1= Tbl1.Key1 AND Tbl2.Key2=Tbl1.Key2
        ) 

Outter Join Is Null

    SELECT * FROM TBL1 LEFT JOIN TBL2 
    WHERE TBL2.Key1 IS NULL OR Deleted=0
-残月青衣踏尘吟 2024-09-14 07:05:25

C# 问题的一项优化是对 DIV 数组进行排序。从较小的数字开始,您更有可能快速找到匹配项。

编辑:对 C# 问题的另一个优化可能是查看类似于素数的方法埃拉托斯特尼筛< /a> 一般理论是,您可以抢占一些结果,而无需执行检查。

至于 SQL 问题,正如其他人所说,三种明显(常见)的方式是 JOIN、IN 和 EXISTS。

One optimisation to the C# question is to sort the DIV array. You're more likely to find a match quickly starting with the smaller numbers.

EDIT: Another optimisation to the C# question may be to look at an approach similar to the Prime Number Sieve of Eratosthenes the general theory being that you pre-empt some results without having to perform the checks.

As for the SQL question, the three obvious (common) ways are as others have stated, a JOIN, an IN and an EXISTS.

哆啦不做梦 2024-09-14 07:05:25

那么您已经使用过哪种解决方案?我立即认为可以使用带有 IN 的子查询、使用 LEFT OUTER JOIN 并过滤 NULL 或使用 EXISTS 来完成

Well which solution have you used already? Immediately I think it can be done using a subquery with IN, using a LEFT OUTER JOIN and filtering on NULL, or using EXISTS.

一袭水袖舞倾城 2024-09-14 07:05:25

剧透警告!!!!

SELECT
     T1.key1,
     T1.key2
FROM
    Table1 T1
WHERE
    NOT EXISTS
    (
        SELECT *
        FROM
            Table2 T2
        WHERE
            T2.key1 = T1.key1 AND
            T2.key2 = T1.key2 AND
            COALESCE(T2.IsDeleted, 0) <> 1
    )

SELECT
     T1.key1,
     T1.key2
FROM
    Table1 T1
LEFT OUTER JOIN Table2 T2 ON
    T2.key1 = T1.key1 AND
    T2.key2 = T1.key2 AND
    COALESCE(T2.IsDeleted, 0) <> 1
WHERE
    T2.key1 IS NULL

SELECT
     T1.key1,
     T1.key2
FROM
    Table1 T1
WHERE
    (
        SELECT COUNT(*)
        FROM
            Table2 T2
        WHERE
            T2.key1 = T1.key1 AND
            T2.key2 = T1.key2 AND
            COALESCE(T2.IsDeleted, 0) <> 1
    ) = 0

Spoiler alert!!!!!

SELECT
     T1.key1,
     T1.key2
FROM
    Table1 T1
WHERE
    NOT EXISTS
    (
        SELECT *
        FROM
            Table2 T2
        WHERE
            T2.key1 = T1.key1 AND
            T2.key2 = T1.key2 AND
            COALESCE(T2.IsDeleted, 0) <> 1
    )

SELECT
     T1.key1,
     T1.key2
FROM
    Table1 T1
LEFT OUTER JOIN Table2 T2 ON
    T2.key1 = T1.key1 AND
    T2.key2 = T1.key2 AND
    COALESCE(T2.IsDeleted, 0) <> 1
WHERE
    T2.key1 IS NULL

SELECT
     T1.key1,
     T1.key2
FROM
    Table1 T1
WHERE
    (
        SELECT COUNT(*)
        FROM
            Table2 T2
        WHERE
            T2.key1 = T1.key1 AND
            T2.key2 = T1.key2 AND
            COALESCE(T2.IsDeleted, 0) <> 1
    ) = 0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文