Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Lounge

Name: Voiden Froze !!Bj5xlFz8N6pmNy3 2011-12-14 9:54

Okay /prog/ let's have a code-off!

The most fun way of carrying text over long lines, typical to that of your favorite    , in the language of your choice.

Let's kick off with some simple FIOCthp:


# -*- coding: cp1252 -*-
MAX_LINE_LENGTH =  60
a="""The list methods make it very easy to use a list as a stack, where the last element added is the first element retrieved (“last-in, first-out”). To add an item to the top of the stack, use append(). To retrieve an item from the top of the stack, use pop() without an explicit index. For example:"""

def cut_lines(words,result,line=''):
    if len(words):
        if len(words[0])>MAX_LINE_LENGTH:
            result.append(words.pop(0))
        else:
            test = line+words[0]+' '
            if len(test)<=MAX_LINE_LENGTH:
                words.pop(0)
                cut_lines(words,result,test)
            else:
                result.append(line)
                cut_lines(words,result)
    else:
        result.append(line)
       
def carry_lines(a):
    words=a.split()
    result=[]
    cut_lines(words,result)
    return result

for element in carry_lines(a):
    print element

Name: Anonymous 2011-12-15 9:56

>>16
The list methods make me xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx hard


def cut_lines(words,result,width,line=''):
    if len(words):
        if len(words[0])>=MAX_LINE_LENGTH:
            result.append(words.pop(0))
            cut_lines(words,result,width)
        else:
            test = line+words[0]+' '
            if len(test)<=MAX_LINE_LENGTH:
                words.pop(0)
                cut_lines(words,result,width,test)
            else:
                result.append(line)
                cut_lines(words,result,width)
    else:
        result.append(line)
       
def carry_lines(a, width=MAX_LINE_LENGTH):
    if len(a)<=width: return[a]
    words  = a.split()
    result = []
    cut_lines(words,result,width)
    return result

for element in carry_lines(a):
    print element

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List