字典映射一个字母表——用独特的键和价值观
我需要创建一个字典来表达字母表中的每个字符与该字母表中的另一个字符之间的映射,其中键和值都是唯一的 - 就像一个非常简单的密码,表示如何编码/解码消息。不能有重复的键或值。
有人看出这段代码有什么问题吗?尽管在每次迭代中,每个已使用的值的可用字符池都会减少,但它仍然会在映射中生成重复的值。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这有效。
This works.
我会考虑执行一个简单的 Fisher Yates在字母表的一个或两个序列上进行洗牌,然后您可以简单地迭代输出并将映射器放在一起。
伪代码
当您每次尝试选择一个随机值时,很可能会发生冲突,从而选择一个非唯一的值。答案通常是先洗牌,然后按顺序选择。
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
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.
“快速修复”虽然不是最佳方案(如果不允许将 A 映射到 A),
但如果允许将 A 映射到 A,则忽略上述更改...但至少将最后一行更改为
"a quick fix" though not optimal would be (if mapping A to A is NOT allowed)
if mapping A to A is allowed then ignore the above change... BUT at least change the last line to
我想您可以在现有“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.