将Python脚本转换为ObjectiveC

2024-10-01 11:24:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我最近要求this question创建一个优化单词包装的函数,得到了我在这个Python脚本中寻找的响应。不幸的是,我不会说Python。:D有人能帮我把这个转换成Objective-C吗?在

Code. I took the liberty of modifying the DP always to return exactly n lines, at the     cost of increasing the running time from O(#words ** 2) to O(#words ** 2 * n).

def minragged(text, n=3):
    """
    >>> minragged('Just testing to see how this works.')
    ['Just testing', 'to see how', 'this works.']
    >>> minragged('Just testing to see how this works.', 10)
    ['', '', 'Just', 'testing', 'to', 'see', 'how', 'this', 'works.', '']
    """
    words = text.split()
    cumwordwidth = [0]
    # cumwordwidth[-1] is the last element
    for word in words:
        cumwordwidth.append(cumwordwidth[-1] + len(word))
    totalwidth = cumwordwidth[-1] + len(words) - 1  # len(words) - 1 spaces
    linewidth = float(totalwidth - (n - 1)) / float(n)  # n - 1 line breaks
    def cost(i, j):
        """
        cost of a line words[i], ..., words[j - 1] (words[i:j])
        """
        actuallinewidth = max(j - i - 1, 0) + (cumwordwidth[j] - cumwordwidth[i])
        return (linewidth - float(actuallinewidth)) ** 2
    # best[l][k][0] is the min total cost for words 0, ..., k - 1 on l lines
    # best[l][k][1] is a minimizing index for the start of the last line
    best = [[(0.0, None)] + [(float('inf'), None)] * len(words)]
    # xrange(upper) is the interval 0, 1, ..., upper - 1
    for l in xrange(1, n + 1):
        best.append([])
        for j in xrange(len(words) + 1):
            best[l].append(min((best[l - 1][k][0] + cost(k, j), k) for k in xrange(j + 1)))
    lines = []
    b = len(words)
    # xrange(upper, 0, -1) is the interval upper, upper - 1, ..., 1
    for l in xrange(n, 0, -1):
        a = best[l][b][1]
        lines.append(' '.join(words[a:b]))
        b = a
    lines.reverse()
    return lines

if __name__ == '__main__':
    import doctest
    doctest.testmod()

Tags: ofthetoinforlenisthis
1条回答
网友
1楼 · 发布于 2024-10-01 11:24:58

这是一个被翻译成Objective-C的函数,它已经被更新为编译,但是仅仅是对它的正确性进行了测试(在你的previous question:[... minragged:@"Just testing to see how this works." lineCount:3]中给出的例子中)。关于效率和Objective-C习语,我们没有考虑到。在

@interface NSMutableArray (reverse)
/* Could also return self, but this makes it explicit that array is reversed in-place
   rather than returning a reversed copy.
 */
-(void)reverse;
@end

@implementation NSMutableArray (reverse)
-(void)reverse {
    int i,j;
    for (i=0,j=[self count]-1; i<j; ++i, j) {
        [self exchangeObjectAtIndex:i withObjectAtIndex:j];
    }
}
@end




-(NSArray*)minragged:(NSString*)text lineCount:(int)n {
    int width = 0;
    NSArray *words = [text componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSMutableArray *cumWordWidth = [NSMutableArray arrayWithObject:[NSNumber numberWithInt:width]];
    for (NSString *word in words) {
        width += [word length];
        [cumWordWidth addObject:[NSNumber numberWithInt:width]];
    }
    int totalWidth = width + [words count] - 1,
        lineWidth = (double)(totalWidth - (n - 1)) / (double)(n),
        actualLineWidth,
        i, j, k, min_k;
    double cost, min_cost;

    // best[i][k][0] is the min total cost for words 0, ..., k - 1 on i lines
    // best[i][k][1] is a minimizing index for the start of the last line
    NSMutableArray *best = [NSMutableArray arrayWithCapacity:n],
                   *best_i_prev = [NSMutableArray arrayWithCapacity:([words count]+1)],
                   *best_i;

    [best_i_prev addObject:[NSArray arrayWithObjects:[NSNumber numberWithDouble:0.0],[NSNull null],nil]];
    for (i=0; i < [words count]; ++i) {
        [best_i_prev addObject:[NSArray arrayWithObjects:(NSNumber*)kCFNumberPositiveInfinity,[NSNull null],nil]];
    }
    [best addObject:best_i_prev];

    for (i=1; i <= n; ++i) {
        best_i=[NSMutableArray arrayWithCapacity:[words count]];
        for (j=0; j <= [words count]; ++j) {
            min_k=0;
            min_cost = [(NSNumber*)kCFNumberPositiveInfinity doubleValue];
            for (k=0; k < j; ++k) {
                actualLineWidth = j - k - 1;
                if (actualLineWidth < 0) {
                    actualLineWidth = 0;
                }
                actualLineWidth += [[cumWordWidth objectAtIndex:j] intValue]
                                   - [[cumWordWidth objectAtIndex:k] intValue];
                cost = (lineWidth - (double)(actualLineWidth));
                cost *= cost;
                cost += [[[best_i_prev objectAtIndex:k] objectAtIndex:0] doubleValue];
                if (cost < min_cost) {
                    min_cost = cost;
                    min_k = k;
                }
            }
            [best_i addObject:[NSArray arrayWithObjects:[NSNumber numberWithDouble:min_cost], 
                                                        [NSNumber numberWithInt:min_k],
                                                        nil]];
        }
        [best addObject:best_i];
        best_i_prev = best_i;
    }

    NSMutableArray *lines = [NSMutableArray arrayWithCapacity:n];
    NSRange range;
    int end;
    end = [words count];
    for (i=n; i > 0;  i) {
        range.location = [[[[best objectAtIndex:i] objectAtIndex:end] objectAtIndex:1] intValue];
        range.length = end-range.location;
        [lines addObject:[[words subarrayWithRange:range] componentsJoinedByString:@" "]];
        end = range.location;
    }
    [lines reverse];
    return lines;
}

您可能希望创建一个自动释放池来清除在-minragged:lineCount:中创建的对象,以免耗尽内存。如果这样做,请确保在排空池之前保留lines,然后自动释放它。在

相关问题 更多 >