将骆驼帽的伪代码写入数组
Writing pseudo code for camel cap into array
前几天有个朋友让我帮他写伪代码,将后面的字符串"HelloWorld"的每个单词分开,存入一个数组[x]中,其中未使用的数组元素为空字符串。
我实际上不知道从哪里开始,我在 Python 中编写了它,但是使用了 [A-Z] [a-z] 中的一些函数来检测大写和小写,因此它知道何时拆分字符串,但你怎么能用伪代码编写它,因为它没有任何官方文档?
这是伪代码。
你定义什么"works"。
喜欢:简单地说明就足够了:
there is some
fun isUpperCase(char c)
that returns true/false based on the case-ness of c
.
然后您的算法会使用该函数。
伪代码的思想是描述算法的本质部分。这样的功能到底是如何工作的并不重要;就说 "we have such a function";然后开始使用它。
换句话说:您担心如何用伪代码术语表达 regex/splitting。不要 - 这已经是一个(不太重要的)实现细节。
下面是一些示例伪代码:
create empty array
set offset = 0
for each character in "HelloWorldThisIsCamelCase":
if character is uppercase:
increment offset
append character to array element at offset
伪代码背后的基本原理是描述一种算法。通常你会留下语言实现的血淋淋的细节。但事实是你要决定你写到什么水平,离开什么水平。
这里我会写:
declare an array of 10 strings arr initialized to empty strings
set arr_index to 0
initialize index to first position in string
loop
find first uppercase letter after index
if none find: exit loop
if found at new_index
copy characters from index (inclusive) to new_index (exclusive) to a new string
store that string in arr[arr_index]
increment arr_index
set index = new_index
end loop
arr_index is the number of words found
这里我已经为实现者留下了大写字母的研究和子字符串到新字符串的副本,但是恕我直言,描述足以立即用 C、C++ Python 或 Java(我知道的语言)
前几天有个朋友让我帮他写伪代码,将后面的字符串"HelloWorld"的每个单词分开,存入一个数组[x]中,其中未使用的数组元素为空字符串。
我实际上不知道从哪里开始,我在 Python 中编写了它,但是使用了 [A-Z] [a-z] 中的一些函数来检测大写和小写,因此它知道何时拆分字符串,但你怎么能用伪代码编写它,因为它没有任何官方文档?
这是伪代码。
你定义什么"works"。
喜欢:简单地说明就足够了:
there is some
fun isUpperCase(char c)
that returns true/false based on the case-ness of
c
.
然后您的算法会使用该函数。
伪代码的思想是描述算法的本质部分。这样的功能到底是如何工作的并不重要;就说 "we have such a function";然后开始使用它。
换句话说:您担心如何用伪代码术语表达 regex/splitting。不要 - 这已经是一个(不太重要的)实现细节。
下面是一些示例伪代码:
create empty array
set offset = 0
for each character in "HelloWorldThisIsCamelCase":
if character is uppercase:
increment offset
append character to array element at offset
伪代码背后的基本原理是描述一种算法。通常你会留下语言实现的血淋淋的细节。但事实是你要决定你写到什么水平,离开什么水平。
这里我会写:
declare an array of 10 strings arr initialized to empty strings
set arr_index to 0
initialize index to first position in string
loop
find first uppercase letter after index
if none find: exit loop
if found at new_index
copy characters from index (inclusive) to new_index (exclusive) to a new string
store that string in arr[arr_index]
increment arr_index
set index = new_index
end loop
arr_index is the number of words found
这里我已经为实现者留下了大写字母的研究和子字符串到新字符串的副本,但是恕我直言,描述足以立即用 C、C++ Python 或 Java(我知道的语言)