It took me 30 hours to write this deceptively simple code

kylefoley76

Well-known Member
Joined
Mar 1, 2010
Messages
1,553
I'm a little upset with myself for taking 30 hours to write the following code.

What the code does is it loops through the justifiers array which in this case had 11 members but 9 of the members were blank. It then had to look in the osent array and the more_sent array to find the sentences that justified the member in the justifier array. After that was done, it then had to get the corresponding members from the ancestors array and then it had to take each ancestor and look through the osent and more_sent array to find which sentences causes the ancestors. And when it found them, it had to delete the members. This process had to continue into all the members in one of the array was deleted. The reason why it took 30 hours was partly because I was dieting and couldn't concentrate that well but also because I kept trying to do it backwards from the wrong starting point.




Code:
Function explain_justification(more_sent As Variant, ancestors As Variant, _
assumption As Variant, justifiers As Variant, sent As Variant, _
cons_sent As Variant, implication As Boolean, num_just As Integer, osent As Variant) As String




Dim i As Integer, temp_just As String, j As Integer, temp_more_sent As String
Dim more_sent_used(), more_sent_just As Boolean, found As Boolean
Dim temp_ancest3 As String, temp_ancest4 As String, match As Boolean
Dim temp_ancest1 As String, temp_ancest2 As String, temp_more_sent2 As String
Dim m As Integer, o As Integer
Dim k As Integer, n As Integer, help_me As Boolean, temp_more(), t As Integer
ReDim temp_more(UBound(more_sent))
Dim match1 As Boolean, match2 As Boolean, double_match As Boolean, temp_osent As String
Dim to_be_found()
ReDim more_sent_used(UBound(more_sent))
Dim temp_tsent As String






j = UBound(justifiers)
Do Until j = 0
    
    temp_just = justifiers(j)
    
    If justifiers(j) <> "" Then
    
    
    Do
        
        more_sent_used(j) = temp_just
        ReDim Preserve to_be_found(m)
        to_be_found(m) = ancestors(j, 1)
        m = m + 1
        ReDim Preserve to_be_found(m)
        to_be_found(m) = ancestors(j, 2)
        m = m + 1
    
    
so_real:
    


    For t = 1 To UBound(osent)
    temp_osent = osent(t)
        For m = 1 To UBound(to_be_found)
        Dim temp_to_be_found As String
        temp_to_be_found = to_be_found(m)
            If temp_osent = temp_to_be_found Then
            to_be_found(m) = ""
            End If
        
        Next


        If Len(Join(to_be_found, "")) = 0 Then
        GoTo im_real
        justifiers(j) = ""
        End If


    Next
   
   
   Dim to_be_found2()
    
    For t = UBound(more_sent) To 1 Step -1
        temp_more_sent = more_sent(t)
        
        For m = 1 To UBound(to_be_found)
        temp_to_be_found = to_be_found(m)
            If temp_more_sent = temp_to_be_found Then
            more_sent_used(t) = temp_more_sent
            to_be_found(m) = ancestors(t, 1)
            m = UBound(to_be_found) + 1
            ReDim Preserve to_be_found(m)
            to_be_found(m) = ancestors(t, 2)
            GoTo so_real
            End If
        
        Next
    Next
    
im_real:
    Loop Until Len(Join(to_be_found, "")) = 0
End If
j = j - 1
Loop Until Len(Join(justifiers, "")) = 0
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
.. The Code is beyond me as a beginner. But as this is „The Lounge“…..

……. The reason why it took 30 hours was partly because I was dieting and couldn't concentrate that well but….
……….

… I know that feeling only too well. That is the reason for me getting into Excel/VBA to speed up a personal Diet Plan thing. My advice for wot it is worth.
. 1 Keep at it. It is worth it. But try to do it ONCE good. Nature is mean. Every successive time there after it gets a lot more difficult.
. 2 Be very careful with “Expert” advice. Only you truly know how your Body reacts. Many experts (once they get in their well pensioned retirement!), often admit they had no idea. – The speed with which new diet ideas come out is overwhelming even to the experts, (matched only to how overwhelming the endless possibilities are with VBA – no one person can keep up with it!!)
Alan
 
.. The Code is beyond me as a beginner. But as this is „The Lounge“…..



… I know that feeling only too well. That is the reason for me getting into Excel/VBA to speed up a personal Diet Plan thing. My advice for wot it is worth.
. 1 Keep at it. It is worth it. But try to do it ONCE good. Nature is mean. Every successive time there after it gets a lot more difficult.
. 2 Be very careful with “Expert” advice. Only you truly know how your Body reacts. Many experts (once they get in their well pensioned retirement!), often admit they had no idea. – The speed with which new diet ideas come out is overwhelming even to the experts, (matched only to how overwhelming the endless possibilities are with VBA – no one person can keep up with it!!)
Alan

Thanks for taking an interest. A couple of weeks ago I did a hard diet, that is basically a very small salad just for the day. I had tried easy dieting in the past but it never worked, the only dieting I found was to just lose it all in a five day period, that is lose 10 pounds in five days. But then I noticed that after the diet was over I had an enormous amount of energy. So I decided to try to get that energy again, but I didn't have the courage to go through with it. I actually don't weigh very much, 189 pounds and 6 feet but I know if I break 190 then it's very hard to go back.
 
30 hours. No problem. A great way to learn.

The fact you've persevered to get a solution tells me you've a good chance to succeed with your weight & other goals.

A couple more years of coding practice and you'll likely be coding as quickly as you write forum posts. You're well on your way.

BTW, I don't diet. I run: I very much enjoy running. 6'2" and ~165 pounds at the moment. I understand a good diet & some exercise are all one needs: no need for a 'hard' diet.

all the best
 
Thanks for the comments. I've been coding since February. I think that algorithm was more of a conceptual problem than a coding problem. I know how to code my ideas but I can't always get the right ideas in the right order.
 
That is good (for only started February & know how to code & understand where the difficulties lie) & you're doing great. Sometimes it gets pretty tricking looping through collections. Plenty of practice & more learning will make it easier.
 

Forum statistics

Threads
1,223,723
Messages
6,174,117
Members
452,545
Latest member
boybenqn

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top