cesco wrote: I created some more test strings and ran posters solutions against them.
results attached. - Paddy.
# alternating_replacements.py tests = " 1 2_ 3_4 5_6_ 7_8_9 10_11_12_ 13_14_15_16 17_18_19_20_" \ " _ _21 _22_ _23_24 _25_26_ _27_28_29 _30_31_32_ _33_34_35_36" \ " __ ___ ____ _____".split(" ") def altrep0(s): s1 = s.split("_") s1 = [s1[i]+":"+s1[i+1] for i in range(0,len(s1),2)] s1 = ",".join(s1) return s1 altrep0.author="Frederik Lundh" def altrep1(s): from re import sub def repl(o): repl.n = not repl.n return ":" if repl.n else "," repl.n = False return sub("_", repl, s) altrep1.author="bearophile" def altrep2(s): evenOrOdd = True s2 = "" for i in s: if i == '_': if evenOrOdd: s2 += ':' evenOrOdd = not evenOrOdd else: s2 += ',' evenOrOdd = not evenOrOdd else: s2 += i return s2 altrep2.author="cokofree" def altrep3(s): import re from itertools import cycle return re.sub("_", lambda m, c=cycle(":,").next: c(), s) altrep3.author="Peter Otten" def altrep4(s): from itertools import islice, cycle def interleave(*iterators): iterators = [ iter(i) for i in iterators ] while 1: for i in iterators: yield i.next() def punctuate(s): parts = s.split('_') punctuation = islice(cycle(':,'), len(parts)-1) return ''.join(interleave(parts, punctuation)) return punctuate(s) altrep4.author="Duncan Booth" def altrep5(s): import re return re.sub(r'([^_]+)_([^_]+)_?', r'\1:\2;', s) altrep5.author="Pablo Ziliani" progs = [ altrep0, altrep1, altrep2, altrep3, altrep4, altrep5] def testall(): ''' >>> testall() ## Program by: Frederik Lundh '' RETURNS '<type 'exceptions.IndexError'>' '1' RETURNS '<type 'exceptions.IndexError'>' '2_' RETURNS '2:' '3_4' RETURNS '3:4' '5_6_' RETURNS '<type 'exceptions.IndexError'>' '7_8_9' RETURNS '<type 'exceptions.IndexError'>' '10_11_12_' RETURNS '10:11,12:' '13_14_15_16' RETURNS '13:14,15:16' '17_18_19_20_' RETURNS '<type 'exceptions.IndexError'>' '_' RETURNS ':' '_21' RETURNS ':21' '_22_' RETURNS '<type 'exceptions.IndexError'>' '_23_24' RETURNS '<type 'exceptions.IndexError'>' '_25_26_' RETURNS ':25,26:' '_27_28_29' RETURNS ':27,28:29' '_30_31_32_' RETURNS '<type 'exceptions.IndexError'>' '_33_34_35_36' RETURNS '<type 'exceptions.IndexError'>' '__' RETURNS '<type 'exceptions.IndexError'>' '___' RETURNS ':,:' '____' RETURNS '<type 'exceptions.IndexError'>' '_____' RETURNS ':,:,:' ## Program by: bearophile '' RETURNS '' '1' RETURNS '1' '2_' RETURNS '2:' '3_4' RETURNS '3:4' '5_6_' RETURNS '5:6,' '7_8_9' RETURNS '7:8,9' '10_11_12_' RETURNS '10:11,12:' '13_14_15_16' RETURNS '13:14,15:16' '17_18_19_20_' RETURNS '17:18,19:20,' '_' RETURNS ':' '_21' RETURNS ':21' '_22_' RETURNS ':22,' '_23_24' RETURNS ':23,24' '_25_26_' RETURNS ':25,26:' '_27_28_29' RETURNS ':27,28:29' '_30_31_32_' RETURNS ':30,31:32,' '_33_34_35_36' RETURNS ':33,34:35,36' '__' RETURNS ':,' '___' RETURNS ':,:' '____' RETURNS ':,:,' '_____' RETURNS ':,:,:' ## Program by: cokofree '' RETURNS '' '1' RETURNS '1' '2_' RETURNS '2:' '3_4' RETURNS '3:4' '5_6_' RETURNS '5:6,' '7_8_9' RETURNS '7:8,9' '10_11_12_' RETURNS '10:11,12:' '13_14_15_16' RETURNS '13:14,15:16' '17_18_19_20_' RETURNS '17:18,19:20,' '_' RETURNS ':' '_21' RETURNS ':21' '_22_' RETURNS ':22,' '_23_24' RETURNS ':23,24' '_25_26_' RETURNS ':25,26:' '_27_28_29' RETURNS ':27,28:29' '_30_31_32_' RETURNS ':30,31:32,' '_33_34_35_36' RETURNS ':33,34:35,36' '__' RETURNS ':,' '___' RETURNS ':,:' '____' RETURNS ':,:,' '_____' RETURNS ':,:,:' ## Program by: Peter Otten '' RETURNS '' '1' RETURNS '1' '2_' RETURNS '2:' '3_4' RETURNS '3:4' '5_6_' RETURNS '5:6,' '7_8_9' RETURNS '7:8,9' '10_11_12_' RETURNS '10:11,12:' '13_14_15_16' RETURNS '13:14,15:16' '17_18_19_20_' RETURNS '17:18,19:20,' '_' RETURNS ':' '_21' RETURNS ':21' '_22_' RETURNS ':22,' '_23_24' RETURNS ':23,24' '_25_26_' RETURNS ':25,26:' '_27_28_29' RETURNS ':27,28:29' '_30_31_32_' RETURNS ':30,31:32,' '_33_34_35_36' RETURNS ':33,34:35,36' '__' RETURNS ':,' '___' RETURNS ':,:' '____' RETURNS ':,:,' '_____' RETURNS ':,:,:' ## Program by: Duncan Booth '' RETURNS '' '1' RETURNS '1' '2_' RETURNS '2:' '3_4' RETURNS '3:4' '5_6_' RETURNS '5:6,' '7_8_9' RETURNS '7:8,9' '10_11_12_' RETURNS '10:11,12:' '13_14_15_16' RETURNS '13:14,15:16' '17_18_19_20_' RETURNS '17:18,19:20,' '_' RETURNS ':' '_21' RETURNS ':21' '_22_' RETURNS ':22,' '_23_24' RETURNS ':23,24' '_25_26_' RETURNS ':25,26:' '_27_28_29' RETURNS ':27,28:29' '_30_31_32_' RETURNS ':30,31:32,' '_33_34_35_36' RETURNS ':33,34:35,36' '__' RETURNS ':,' '___' RETURNS ':,:' '____' RETURNS ':,:,' '_____' RETURNS ':,:,:' ## Program by: Pablo Ziliani '' RETURNS '' '1' RETURNS '1' '2_' RETURNS '2_' '3_4' RETURNS '3:4;' '5_6_' RETURNS '5:6;' '7_8_9' RETURNS '7:8;9' '10_11_12_' RETURNS '10:11;12_' '13_14_15_16' RETURNS '13:14;15:16;' '17_18_19_20_' RETURNS '17:18;19:20;' '_' RETURNS '_' '_21' RETURNS '_21' '_22_' RETURNS '_22_' '_23_24' RETURNS '_23:24;' '_25_26_' RETURNS '_25:26;' '_27_28_29' RETURNS '_27:28;29' '_30_31_32_' RETURNS '_30:31;32_' '_33_34_35_36' RETURNS '_33:34;35:36;' '__' RETURNS '__' '___' RETURNS '___' '____' RETURNS '____' '_____' RETURNS '_____' >>> ''' import sys for prog in progs: print "\n## Program by:", prog.author for test in tests: try: answer = prog(test) except: answer = sys.exc_info()[0] print "%14s RETURNS '%s'" % ("'%s'" % (test,), answer) if __name__ == "__main__": testall()
-- http://mail.python.org/mailman/listinfo/python-list