常见的伪代码问题、难题和挑战

发布于 2024-09-14 21:17:57 字数 1436 浏览 4 评论 0原文

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

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

发布评论

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

评论(5

木森分化 2024-09-21 21:17:57

还没有人问过我这个问题,但有一个经典的 FizzBu​​zz 问题。

编写一个程序,打印从 1 到 100 的数字。但是对于三的倍数,打印“Fizz”而不是数字,对于五的倍数,打印“Buzz”。对于三和五的倍数的数字打印“FizzBu​​zz”。

http://www.codinghorror.com 所示/blog/2007/02/why-cant-programmers-program.html

这是一个简单的筛选问题,即使是非常新的程序员也不会造成任何重大困难。

I haven't been asked it yet, but there is the classic FizzBuzz question.

Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz".

as found at http://www.codinghorror.com/blog/2007/02/why-cant-programmers-program.html

It is meant as a simple screening question, and not to pose any significant difficulty to even very new programmers.

卷耳 2024-09-21 21:17:57

几年前的一次实习面试中,我被要求为此编写一个伪代码解决方案:

编写一个算法,在给定目录路径的情况下,可以计算该目录下的文件总数以及所有文件的总数。子目录。

能够解决这个问题表明了对树遍历的理解。

I was asked to write a pseudo-code solution for this during an interview for an internship a few years ago:

Write an algorithm that, given a directory path, can count the total number of files that fall under that directory and all subdirectories.

Being able to solve this demonstrates an understanding of tree traversal.

迎风吟唱 2024-09-21 21:17:57

我自己从未被要求在面试中编写伪代码(仍然是学生),但我的一个朋友向 Google 申请暑期工作,被要求编写一个可以处理正则表达式子集的正则表达式解析器( iirc,仅输入字母数字并使用 *、+ 和 {x,y} 表示法)。我正在考虑明年自己向他们申请一份暑期工作,并且对尝试当场写出类似内容的想法感到害怕 XD。

我相信他使用两个相互递归的函数解决了这个问题。不知道怎么办。

哦,顺便说一句,他不仅仅是被要求为此编写伪代码。他被要求当场编写可以编译的实际 C++ 代码。 (也在 Google 文档中)。

I have not myself ever been asked to write pseudo-code in an interview (still a student), but a friend of mine who applied to Google for a summer job was asked to write a regexp parser that could deal with a subset of regexps (iirc, alphanumeric input only and using the *, + and {x,y} notation). I'm considering applying to them next year for a summer job myself and am terrified by the idea of trying to write something like that on the spot XD.

I believe he solved it using two functions that recursed off of one another. Not sure how.

Oh, he wasn't just asked to write pseudo-code for this, btw. He was asked to write actual C++ code, on the spot, that would compile. (In a Google Docs document, as well).

时光与爱终年不遇 2024-09-21 21:17:57

我花了很长时间才弄清楚,但这是我的 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

It took me a long time to figure it out, but here's my Excel VBA solution to the sentence reversing problem.

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
你怎么敢 2024-09-21 21:17:57

我不知道这是否是最优雅的方法,但这里有一个找到最常出现的单词的解决方案。我是这样处理的。

  1. 通过循环遍历句子来查找单词数,如果找到空格或到达句子末尾,则增加单词计数变量。创建一个具有此长度的单词数组。
  2. 使用一个变量作为每个单词的开头,使用另一个变量作为每个单词的长度,再次循环该句子并将前面的字符分配给单词数组的一个元素。
  3. 对于每个单词数组元素,循环遍历单词数组的每个元素,并进行比较(嵌套循环)。如果存在匹配项,则增加计数数组中该位置的值,该数组显示每个单词出现的次数。
  4. 循环遍历计数数组,并将每个字段中的值与迄今为止找到的最大值进行比较。这给出了最大数量,因此给出了最常见的单词。输出该单词。

    公共子FindCommonWord()

    暗淡的句子作为字符串
    Dim WordArray() 作为字符串
    Dim CountArray() 作为整数
    Dim p As Integer '字开头
    Dim q As Integer '字长
    Dim w As Integer '字数统计
    Dim tw As String '这个词
    Dim R As Integer '最常见单词的位置

    p = 1
    w = 1

    Insentence = ActiveSheet.Range("A1").Value '将句子分配给字符串

    '循环遍历所有字符并统计单词数
    For h = 2 To Len(Insentence) + 1 '循环输入句子的所有字符
    If (Mid(Insentence, h, 1) = " " Or h = Len(Insentence) + 1) then '这个字符是空格或者这是句子的结尾
    w = w + 1
    结束如果
    下一个h

    w = w - 1

    ReDim WordArray(w)
    ReDim CountArray(w)

    w = 1
    '循环遍历所有字符并将单词分配给数组的元素
    For i = 2 To Len(Insentence) + 1 '循环输入句子的所有字符
    If (Mid(Insentence, i, 1) = " " Or i = Len(Insentence) + 1) then '这个字符是空格或者这是句子的结尾
    q = i - p '设置字长为该位置的数字减去该字开头的位置
    WordArray(w) = Mid(句子, p, q)
    p = i + 1 '下一个单词的位置
    w = w + 1 '下一个单词
    结束如果
    接下来我
    w = w - 1 '到达最后一个单词

    '循环遍历数组以处理每个单词
    对于 j = 1 至 w
    '循环遍历数组并计算该单词出现的次数
    对于 k = 1 至 w
    如果 UCase(WordArray(k)) = UCase(WordArray(j)) 则 CountArray(j) = CountArray(j) + 1
    下一个 k
    下一个j

    R = 1

    '求计数数组中的最大值
    对于 n = 1 至 w
    如果 CountArray(n) > R 那么 R = n
    下一个n
    MsgBox(“最常见的单词是” & WordArray(R) & ”,位于“ & R &”位置。”)

    End Sub

I don't know if this is the most elegant approach, but here's a solution to find the word that occurs most often. Here's how I approached it.

  1. Find the number of words by looping through the sentence and increment a word count variable if you find a space or you reach the end of the sentence. Create a word array of this length.
  2. Using one variable for the start of each word and another variable for the length of each word, loop through the sentence again and assign the preceding characters to an element of the word array.
  3. For each word array element, loop through each element of the word array, and compare it (nested loop). If there's a match, increment the value for that position of a count array that shows how many times each word appears.
  4. Loop through the count array and compare the value in each field to the maximum value found so far. This gives the maximum number and therefore the most common word. Output that word.

    Public Sub FindCommonWord()

    Dim Insentence As String
    Dim WordArray() As String
    Dim CountArray() As Integer
    Dim p As Integer 'Word Start
    Dim q As Integer 'Word length
    Dim w As Integer 'Word Count
    Dim tw As String 'This Word
    Dim R As Integer 'Position of most common word

    p = 1
    w = 1

    Insentence = ActiveSheet.Range("A1").Value 'Assign the sentence to a string

    'loop through all characters and count the number of words
    For h = 2 To Len(Insentence) + 1 'Loop through all the characters of the input sentence
    If (Mid(Insentence, h, 1) = " " Or h = Len(Insentence) + 1) Then 'This character is a space or this is the the end of the sentence
    w = w + 1
    End If
    Next h

    w = w - 1

    ReDim WordArray(w)
    ReDim CountArray(w)

    w = 1
    'loop through all characters and assign words to elements of an array
    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
    WordArray(w) = Mid(Insentence, p, q)
    p = i + 1 'Position of the next word
    w = w + 1 'Next word
    End If
    Next i
    w = w - 1 'Last word reached

    'loop through the array to work on each word
    For j = 1 To w
    'loop through the array and count the number of times this word appears
    For k = 1 To w
    If UCase(WordArray(k)) = UCase(WordArray(j)) Then CountArray(j) = CountArray(j) + 1
    Next k
    Next j

    R = 1

    'Find the maximum value in the count array
    For n = 1 To w
    If CountArray(n) > R Then R = n
    Next n
    MsgBox ("The most common word is " & WordArray(R) & " in position " & R & ".")

    End Sub

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