Language agnostic 常见伪代码问题、难题和挑战
我正在寻找伪代码问题的例子,你可能在面试中被问到,或者被要求作为你工作或教育的一部分来代表。我不是特别从任何领域寻找例子,所以它可以与设计模式、算法、数据结构、缓存策略相关,与软件工程和开发相关的任何事情,简单或复杂 例如,我发现的一些常见问题主要与排序和搜索技术有关: : :Language agnostic 常见伪代码问题、难题和挑战,language-agnostic,pseudocode,Language Agnostic,Pseudocode,我正在寻找伪代码问题的例子,你可能在面试中被问到,或者被要求作为你工作或教育的一部分来代表。我不是特别从任何领域寻找例子,所以它可以与设计模式、算法、数据结构、缓存策略相关,与软件工程和开发相关的任何事情,简单或复杂 例如,我发现的一些常见问题主要与排序和搜索技术有关: : : insertionSort(数组A) 开始 对于i:=1到长度[A]-1do 开始 值:=A[i]; j:=i-1; 完成:=错误; 重复 如果[j]>值,则 开始 A[j+1]:=A[j]; j:=j-1; 如果j
insertionSort(数组A)
开始
对于i:=1到长度[A]-1do
开始
值:=A[i];
j:=i-1;
完成:=错误;
重复
如果[j]>值,则
开始
A[j+1]:=A[j];
j:=j-1;
如果j<0,则
完成:=真;
结束
其他的
完成:=真;
直至完成为止;
A[j+1]:=值;
结束;
结束;
:
BinarySearch(A[0..N-1],值,低,高){
如果(高<低)
返回-1//未找到
中=低+((高-低)/2)
如果(A[mid]>值)
返回二进制搜索(A、值、低、中1)
else if(A[mid]<值)
返回二进制搜索(A,值,中+1,高)
其他的
返回中//找到
}
如果许多人分享他们的想法和经验,我们也许能够建立一个关于伪代码算法和问题的列表
作为实践,我希望自己提出伪代码表示。所以,即使您找不到伪代码示例,但您认为用这种方式表示它将是一个理想的概念,这也会有所帮助
我也有一些与该主题相关的问题:
- 你以前在面试中被要求写过哪些伪代码
- 这些问题是否倾向于与一个或两个函数长的简单算法相关
- 编写伪代码时是否应避免特定于语言的构造?由于表示是不可知语言的,所以不使用每种语言中不存在的术语(如Dispose和foreach)是否更安全
编辑: 我发现了一些更多的例子,我会继续编辑,因为我发现了更多:
编写一个函数,使用单个字符串反转句子中单词的顺序,而不是反转单词: 输入:“猫和另一只猫坐在垫子上!” 输出:“猫!另一只带垫子,猫坐在上面”
编写一个函数,该函数接受一个字符串,该字符串将返回该字符串中出现最多的单词,忽略大小写和标点符号。如果多个单词的出现次数相同,请返回最先出现的单词: 输入:“猫和另一只猫坐在垫子上!” 输出:输出
编写一个函数来查找某个字符串中出现次数最多的字符,忽略大小写。如果存在多个最高出现次数相同的字符,请返回字符串中最先出现的字符 输入:“字符” 产出:c
编写一个反转字符串的函数 输入:“反向”
输出:“esrever”我本人在面试中从未被要求编写伪代码(还是一名学生),但我的一位朋友申请了谷歌的暑期工作,被要求编写一个可以处理regexp子集的regexp解析器(iirc,仅字母数字输入,使用*,+和{x,y}符号)。我正在考虑明年自己申请一份暑期工作,一想到要当场写这样的东西,我就害怕了 我相信他用两个相互递归的函数解决了这个问题。不知道怎么做
哦,他不是被要求写伪代码,BTW。他被要求写实际的C++代码。(也在谷歌文档中)。我还没有被问到这个问题,但是有一个经典的问题 编写一个程序,打印从1到100的数字。但是对于三个打印的倍数“嘶嘶”而不是数字,对于五个打印的倍数“嘶嘶”。对于3和5的倍数的数字,打印“FizzBuzz” 如在
这是一个简单的筛选问题,即使是非常新的程序员也不会遇到任何重大困难。几年前,在一次实习面试中,我被要求为此编写一个伪代码解决方案: 编写一个算法,在给定目录路径的情况下,该算法可以计算属于该目录和所有子目录下的文件总数。
能够解决这个问题说明了我对树遍历的理解。我花了很长时间才弄明白,但下面是我的Excel VBA解决句子反转问题的方法
Public Sub SentenceReverse()
Dim InSentence As String 'Input sentence string
Dim OutSentence As String 'Output sentence string
Dim p As Integer 'Word Start
Dim q As Integer 'Word length
p = 1
InSentence = ActiveSheet.Range("A1").Value 'Assign the sentence to a string
For i = 2 To Len(InSentence) + 1 'Loop through all the characters of the input sentence
If (Mid(InSentence, i, 1) = " " Or i = Len(InSentence) + 1) Then 'This character is a space or this is the the end of the sentence
q = i - p 'Set the word length to the number of this position minus the position of the start of this word
OutSentence = Mid(InSentence, p, q) & " " & OutSentence 'Add this word to the start of the output string
p = i + 1 'Set the start of the next word as the position of this space + 1
End If
Next i
MsgBox (OutSentence)
End Sub
我不知道这是否是最优雅的方法,但这里有一个解决方案,可以找到最常出现的单词。下面是我如何处理它的
insertionSort(array A)
begin
for i := 1 to length[A]-1 do
begin
value := A[i];
j := i - 1;
done := false;
repeat
if A[j] > value then
begin
A[j + 1] := A[j];
j := j - 1;
if j < 0 then
done := true;
end
else
done := true;
until done;
A[j + 1] := value;
end;
end;
BinarySearch(A[0..N-1], value, low, high) {
if (high < low)
return -1 // not found
mid = low + ((high - low) / 2)
if (A[mid] > value)
return BinarySearch(A, value, low, mid-1)
else if (A[mid] < value)
return BinarySearch(A, value, mid+1, high)
else
return mid // found
}
Public Sub SentenceReverse()
Dim InSentence As String 'Input sentence string
Dim OutSentence As String 'Output sentence string
Dim p As Integer 'Word Start
Dim q As Integer 'Word length
p = 1
InSentence = ActiveSheet.Range("A1").Value 'Assign the sentence to a string
For i = 2 To Len(InSentence) + 1 'Loop through all the characters of the input sentence
If (Mid(InSentence, i, 1) = " " Or i = Len(InSentence) + 1) Then 'This character is a space or this is the the end of the sentence
q = i - p 'Set the word length to the number of this position minus the position of the start of this word
OutSentence = Mid(InSentence, p, q) & " " & OutSentence 'Add this word to the start of the output string
p = i + 1 'Set the start of the next word as the position of this space + 1
End If
Next i
MsgBox (OutSentence)
End Sub