Add tags

Add tags to the following comment
「C# には eval が無いなぁ。動的コンパイルでもしようかな。あ待てよアレがあったな」 ということで C#2.0 + IronPython1.1 です。ところてんさんの #4728 を参考に、Python のコードを片っ端から生成して IronPython で Execute しています。komachi 関数は C# 側で作成。括弧なしで 101 個を、6分程度(orz)で調べ上げます。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System;
using System.Collections.Generic;
using IronPython;
using IronPython.Hosting;
using IronPython.Runtime;
using IronPython.Runtime.Calls;
class Program {
    static void Main() {
        PythonEngine python = new PythonEngine();
        EngineModule module = python.CreateModule();
        Dictionary<string, object> locals = new Dictionary<string, object>();
        int count = 0;
        locals["komachi"] = new CallTarget2(delegate(object obj, object obj2) {
            if (obj2.Equals(100.0)) {
                count++;
                Console.WriteLine(obj + " = " + obj2);
            }
            return null;
        });
        DateTime before = DateTime.Now;
        foreach (string code in GenPythonCode()) {
            python.Execute(code, module, locals);
        }
        Console.WriteLine("time: {0}", DateTime.Now - before);
        Console.WriteLine("total: {0}", count);
    }
    static IEnumerable<string> GenPythonCode()
    {
        string[] ops = { "", ".0+", ".0-", ".0*", ".0/" };
        for (int i = 0; i < Math.Pow(ops.Length, 8); i++) {
            string code = "1";
            int j = i;
            foreach (char num in "23456789") {
                code += ops[j % ops.Length] + num;
                j /= ops.Length;
            }
            yield return string.Format("komachi(\"{0}\", {0})", code + ".0");
        }
    }
}

Add tags

The input will be splited to tags with space.

Index

Feed

Other

Link

Pathtraq

loading...