用数学符号表示我们在编程中使用的字符串
现在我是一名程序员,最近发现自己在数学方面有多糟糕,并决定从那时起稍微关注一下它,所以如果我的问题侮辱了你的智力,我深表歉意 .
在数学中,是否有用于编程的字符串概念?即字符的排列。
举个例子,假设我想将以下内容翻译成数学符号:
let s be a string of n number of characters.
原因是我想使用该表示形式来查找有关字符串 s
的其他内容,例如它的长度: len(s )
。
你如何在数学中正式地表示这样的事情?
更实际地讲,可以这么说,假设我想从数学上解释这样一个函数:
fitness(s,n) = 1 / |n - len(s)|
或者以更“编程友好”的方式编写:
fitness(s,n) = 1 / abs(n - len(s))
我使用这个函数来解释给定 GA 的适应度函数如何工作;问题是关于查找包含 5 个字符的字符串,我需要根据上述函数给出的适应度分数按升序对解决方案进行排序。
所以我的问题是,你如何表示上面的伪代码是数学符号吗?
Now I'm a programmer who's recently discovered how bad he is when it comes to mathematics and decided to focus a bit on it from that point forward, so I apologize if my question insults your intelligence.
In mathematics, is there the concept of strings that is used in programming? i.e. a permutation of characters.
As an example, say I wanted to translate the following into mathematical notation:
let s be a string of n number of characters.
Reason being I would want to use that representation in find other things about string s
, such as its length: len(s)
.
How do you formally represent such a thing in mathematics?
Talking more practically, so to speak, let's say I wanted to mathematically explain such a function:
fitness(s,n) = 1 / |n - len(s)|
Or written in more "programming-friendly" sort of way:
fitness(s,n) = 1 / abs(n - len(s))
I used this function to explain how a fitness function for a given GA works; the question was about finding strings with 5 characters, and I needed the solutions to be sorted in ascending order according to their fitness score, given by the above function.
So my question is, how do you represent the above pseudo-code in mathematical notation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用语言理论的符号,它用于讨论常规语言、上下文无关语法、编译器理论等。快速概述:
一组字符称为字母表。您可以这样写:“设 A 为 ASCII 字母表,包含 128 个 ASCII 字符的集合。”
字符串是一个字符序列。 ε 是空字符串。
一组字符串正式称为语言。一个常见的说法是,“让 s ∈ L 是 L 语言中的字符串。”
连接字母会产生字符串集(语言)。 A 表示所有 1 个字符的字符串,AA,也写作 A2 ,是所有两个字符串的集合。 A0 是所有零长度字符串的集合,并且恰好是 A0 = {ε}。 (它只包含一个字符串,即空字符串。)
A* 是一种特殊符号,表示字母表中所有字符串的集合 A,任意长度。即 A* = A0 ∪ A1< /sup> ∪ A2 ∪ A3 ... .您可能会从正则表达式中识别出此表示法。
对于长度,使用绝对值条。字符串 s 的长度为 |s|。
所以对于你的陈述:
你可以写:
You can use the notation of language theory, which is used to discuss things like regular languages, context free grammars, compiler theory, etc. A quick overview:
A set of characters is known as an alphabet. You could write: "Let A be the ASCII alphabet, a set containing the 128 ASCII characters."
A string is a sequence of characters. ε is the empty string.
A set of strings is formally known as a language. A common statement is, "Let s ∈ L be a string in language L."
Concatenating alphabets produces sets of strings (languages). A represents all 1-character strings, AA, also written A2, is the set of all two character strings. A0 is the set of all zero-length strings and is precisely A0 = {ε}. (It contains exactly one string, the empty string.)
A* is special notation and represents the set of all strings over the alphabet A, of any length. That is, A* = A0 ∪ A1 ∪ A2 ∪ A3 ... . You may recognize this notation from regular expressions.
For length use absolute value bars. The length of a string s is |s|.
So for your statement:
You could write:
从数学上讲,只要
len(s)
定义良好,您就可以很好地解释fitness(s, n)
。在 CS 文本中,集合 S 上的字符串 s 被定义为 S 元素的有限有序列表,其长度通常写为|s| - 但这只是符号,并不会改变您的
fitness
定义背后的(数学)含义,这非常清楚您的编写方式它。Mathematically, you have explained
fitness(s, n)
just fine as long aslen(s)
is well-defined.In CS texts, a string s over a set S is defined as a finite ordered list of elements of S and its length is often written as |s| - but this is only notation, and doesn't change the (mathematical) meaning behind your definition of
fitness
, which is pretty clear just how you've written it.