Tricky Logic需要帮助吗?

时间:2011-09-15 19:29:54

标签: python perl excel vba

哇哇,我试过了,但是在这里我得到了instr并且分手了,但我的两种方法都无法做到

这里7,2,3,4,9,2我的意思是它的子串长度

 1234567 89   'if its 7(length of sub-string) and 2(next sub-string length) then
 1233456789   ' append into one

 123456 123   ' if its 6(length of sub-string)  and 3(next sub string length) then 
 123456789          

 123 1234 12(last 2 characters 12 may exist or may not) 
 123 1234 then  ' if its 3(length of substring) and 4(next sub string length) then append into one

 1231234 12 and chech next if its 2  and if next is 2 then append
 123123412
 123456789 12 ' if its 9(length of sub string) and next is 2(next sub string length) then make it like these
 123456789 123456712 split into two taking only first 7 characters

问题是单元格看起来不像上面所示,如下所示。这里我的意思是如果它的7然后是下一个子串的长度     这将是一个大字符串,我必须考虑上述条件并传输字符串

123456789 1234567 12 ' if its like these then make it like  below 
123456789 123456712
123 1234 1234567 12 123456789 ' like these then make it like below
1231234 123456712 123456789
123456789 12 1234567  'like these then make it like these
123456789  123456712 1234567 
123456 123 123 1234 123456789 12 'like these then make it like below
123456123 1231234 1234556789 1234567812
1234567 1234567 12 123456789 123 1234 123456789 12 ' then make like below
1234567 123456712 123456789 1231234 123456789 1234567812
12345678 123456789 1234567 123456789 ' do nothing with these type

我尝试过使用split

If (Len(text(x)) = 7 And Len(text(x + 1)) = 2) Or (Len(text(x)) = 6 And Len(text(x + 1)) = 3) Then
text(x) = text(x) & text(x + 1)
ActiveSheet.Cells(i, "D").Value = ActiveSheet.Cells(i, "D").Value & text(x)
x = x + 1
ElseIf (Len(text(x)) = 3 And Len(text(x + 1)) = 4) Then
If Len(text(x + 2)) = 2 Then
text(x) = text(x) & text(x + 1) & text(x + 2)
ActiveSheet.Cells(i, "D").Value = ActiveSheet.Cells(i, "D").Value & text(x)
Else
text(x) = text(x) & text(x + 1)
ActiveSheet.Cells(i, "D").Value = ActiveSheet.Cells(i, "D").Value & text(x)
End If
end if

我只是仍在尝试做到这一点,但我需要你们的帮助。已经10小时我一直在尝试,一个或其他它失败我需要一些帮助!非常感谢。感谢

1 个答案:

答案 0 :(得分:0)

我建议您首先编写一些简单的测试用例,同时改进您的实现:

#/usr/bin/env python
# this file is tester.py for testing my super-complicated algorithm
def algorithm(data):
    items = split(data)
    out = items[0]
    while item in items[1:]:
        if len(item) in (2, 3):
            out += item
        else
            out += ' ' + item
    # etc...
    return out

assert '123456789 123456712' == algorithm('123456789 1234567 12')
assert '1231234 123456712 123456789' == algorithm('123 1234 1234567 12 123456789')
# etc...

现在每次进行更改时,您只需运行该文件,看看它是否通过了您的测试套件。如果您发现真实数据存在问题,请将其添加到测试中......