字典映射一个字母表——用独特的键和价值观

发布于 2024-12-04 11:36:30 字数 919 浏览 1 评论 0原文

我需要创建一个字典来表达字母表中的每个字符与该字母表中的另一个字符之间的映射,其中键和值都是唯一的 - 就像一个非常简单的密码,表示如何编码/解码消息。不能有重复的键或值。

有人看出这段代码有什么问题吗?尽管在每次迭代中,每个已使用的值的可用字符池都会减少,但它仍然会在映射中生成重复的

        string source_alphabet = _alphabet; //ie "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
        string target_alphabet = _alphabet;

        Dictionary<char, char> _map = new Dictionary<char, char>();

        for (int i = 0; i < source_alphabet.Length; i++)
        {
            int random = _random.Next(target_alphabet.Length - 1); //select a random index

            char _output = target_alphabet[random]  //get the char at the random index

            _map.Add(source_alphabet[i], _output); //add to the dictionary

            target_alphabet = target_alphabet.Replace(_output.ToString(), string.Empty); 
            // remove the char we just added from the remaining alphabet
        } 

谢谢。

I need to create a Dictionary that expresses a mapping between each char in an alphabet and another char in that alphabet, where both the key and value are unique -- like a very simple cipher that expresses how to code/decode a message. There can be no duplicate keys or values.

Does anyone see what is wrong with this code? It is still producing duplicate values in the mapping despite the fact that on each iteration the pool of available characters decreases for each value already used.

        string source_alphabet = _alphabet; //ie "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
        string target_alphabet = _alphabet;

        Dictionary<char, char> _map = new Dictionary<char, char>();

        for (int i = 0; i < source_alphabet.Length; i++)
        {
            int random = _random.Next(target_alphabet.Length - 1); //select a random index

            char _output = target_alphabet[random]  //get the char at the random index

            _map.Add(source_alphabet[i], _output); //add to the dictionary

            target_alphabet = target_alphabet.Replace(_output.ToString(), string.Empty); 
            // remove the char we just added from the remaining alphabet
        } 

Thanks.

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

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

发布评论

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

评论(4

孤独陪着我 2024-12-11 11:36:31

这有效。

 for (int i = 0; i < source_alphabet.Length; i++)
    {
        int random = _random.Next(target_alphabet.Length - 1); //select a random index

        char _output = target_alphabet[random];  //get the char at the random index


        _map.Add(source_alphabet[i], _output); //add to the dictionary

        // remove the char we just added from the remaining alphabet
        target_alphabet = target_alphabet.Remove(random, 1);


    }

This works.

 for (int i = 0; i < source_alphabet.Length; i++)
    {
        int random = _random.Next(target_alphabet.Length - 1); //select a random index

        char _output = target_alphabet[random];  //get the char at the random index


        _map.Add(source_alphabet[i], _output); //add to the dictionary

        // remove the char we just added from the remaining alphabet
        target_alphabet = target_alphabet.Remove(random, 1);


    }
莫相离 2024-12-11 11:36:30

我会考虑执行一个简单的 Fisher Yates在字母表的一个或两个序列上进行洗牌,然后您可以简单地迭代输出并将映射器放在一起。

伪代码

Shuffle(sequence1)
Shuffle(sequence2)

for index 0 to 25
    dictionary add sequence1[index], sequence2[index]

当您每次尝试选择一个随机值时,很可能会发生冲突,从而选择一个非唯一的值。答案通常是先洗牌,然后按顺序选择。

I would consider performing a simple Fisher Yates shuffle over one or both sequences of the alphabet, then you can simply iterate over the output and put together your mapper.

Pseudocode

Shuffle(sequence1)
Shuffle(sequence2)

for index 0 to 25
    dictionary add sequence1[index], sequence2[index]

When you try to select a random value each time, then there is a high probability that you will get a collision and therefore have a non-unique value selected. The answer is usually to shuffle, then select in order.

故人的歌 2024-12-11 11:36:30

“快速修复”虽然不是最佳方案(如果不允许将 A 映射到 A),

 int random = _random.Next(target_alphabet.Length - 1);
 while ( source_alphabet[i] == target_alphabet[random] ) {random = _random.Next(target_alphabet.Length - 1);};

但如果允许将 A 映射到 A,则忽略上述更改...但至少将最后一行更改为

target_alphabet = target_alphabet.Remove ( random, 1 );

"a quick fix" though not optimal would be (if mapping A to A is NOT allowed)

 int random = _random.Next(target_alphabet.Length - 1);
 while ( source_alphabet[i] == target_alphabet[random] ) {random = _random.Next(target_alphabet.Length - 1);};

if mapping A to A is allowed then ignore the above change... BUT at least change the last line to

target_alphabet = target_alphabet.Remove ( random, 1 );
傲鸠 2024-12-11 11:36:30

我想您可以在现有“for”循环内的 target_alphabet 上添加另一个“for”循环,并检查字符是否与小“if”条件相同,如果相同则继续内部循环,否则则中断。

I guess you could add another "for" loop on the target_alphabet inside the existing "for" loop and check for the characters not be same with a small "if" condition and continue the inner loop if same or break if not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文