English 中文(简体)
留级功能死于记忆后遗症
原标题:Recursive function dies with Memory Error

Say we have a function that translates the morse symbols:

  • . -> -.
  • - -> ...-

如果我们两次行使这一职能,我们就取得了以下成果:

<代码> ->->。 ->...-.

Given an input string and a number of repetitions, want to know the length of the final string. (Problem 1 from the Flemish Programming Contest VPW, taken from these slides which provide a solution in Haskell).

参考书

4
. 4
.- 2
-- 2 
--... 50

我们期待解决办法。

44
16
20
34028664377246354505728

由于我不知道Haskell,这是我在Sharma的令人厌恶的解决办法,我收到了:

def encode(msg, repetition, morse={ . :  -. ,  - :  ...- }):
    if isinstance(repetition, str):
        repetition = eval(repetition)
    while repetition > 0:
        newmsg =   .join(morse[c] for c in msg)
        return encode(newmsg, repetition-1)
    return len(msg)


def problem1(fn):
    with open(fn) as f:
        f.next()
        for line in f:
            print encode(*line.split())

用于头三个投入的工作,但最后一项投入的记忆有误。

你们如何以更有效的方式来重新制定这一标准?

<><>Edit>>

根据所发表的评论意见:

def encode(p, s, repetition):
    while repetition > 0:
        p,s = p + 3*s, p + s
        return encode(p, s, repetition-1)
    return p + s


def problem1(fn):
    with open(fn) as f:
        f.next()
        for line in f:
            msg, repetition = line.split()
            print encode(msg.count( . ), msg.count( - ), int(repetition))

关于风格和进一步改进的评论仍然值得欢迎。

最佳回答

认为实际上,你只得花很长的时间才能得出结果。 还认为,......的顺序和――在座标不影响最后长度(例如“......- 3”和“- 3”产生相同的最后长度。

因此,我将放弃整条雕刻,而是把数量和数量——作为分类。

问题回答

在您的首字母缩略语中,点数和 da。 然后适用:

repetitions = 4
dots = 1
dashes = 0
for i in range(repetitions):
    dots, dashes = dots + 3 * dashes, dashes + dots

想一下为什么这样做。

Per @Hammar (I had the same idea, but he explained it better than I could have ;-):

from sympy import Matrix

t = Matrix([[1,3],[1,1]])

def encode(dots, dashes, reps):
    res = matrix([dashes, dots]) * t**reps
    return res[0,0] + res[0,1]

页: 1

def encode(dots, dashes, repetitions):
    while repetitions > 0:
        dots, dashes = dots + 3 * dashes, dots + dashes
        repetitions -= 1

    return dots + dashes

def problem1(fn):
    with open(fn) as f:
        count = int(next(f))
        for i in xrange(count):
            line = next(f)
            msg, repetition = line.strip().split()
            print encode(msg.count( . ), msg.count( - ), int(repetition))




相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...