12 Replies - 2538 Views - Last Post: 08 September 2014 - 03:31 AM

#1 leibniz76  Icon User is offline

  • D.I.C Head

Reputation: -11
  • View blog
  • Posts: 89
  • Joined: 02-May 14

It took me 30 hours to write 100 lines of code

Posted 02 September 2014 - 04:02 PM

I've been coding for about 30 hours per week since April. It is very rare that I have to post a question on a message board to solve a problem and sometimes it will take 10 hours or 15 hours to write maybe some code which is roughly 500 lines long, but this code below really stumped me for about 30 hours.

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.

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





Is This A Good Question/Topic? 0
  • +

Replies To: It took me 30 hours to write 100 lines of code

#2 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12147
  • View blog
  • Posts: 45,164
  • Joined: 27-December 08

Re: It took me 30 hours to write 100 lines of code

Posted 02 September 2014 - 04:12 PM

Is there a discussion question here?
Was This Post Helpful? 0
  • +
  • -

#3 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13488
  • View blog
  • Posts: 53,879
  • Joined: 12-June 08

Re: It took me 30 hours to write 100 lines of code

Posted 02 September 2014 - 04:45 PM

Gotos? Interesting choice.
Was This Post Helpful? 0
  • +
  • -

#4 Xaos  Icon User is offline

  • D.I.C Regular

Reputation: 80
  • View blog
  • Posts: 403
  • Joined: 20-November 13

Re: It took me 30 hours to write 100 lines of code

Posted 02 September 2014 - 06:52 PM

30 hours per week since April for this code? You should have probably asked questions earlier.
Was This Post Helpful? 1
  • +
  • -

#5 leibniz76  Icon User is offline

  • D.I.C Head

Reputation: -11
  • View blog
  • Posts: 89
  • Joined: 02-May 14

Re: It took me 30 hours to write 100 lines of code

Posted 02 September 2014 - 10:28 PM

View PostXaos, on 02 September 2014 - 06:52 PM, said:

30 hours per week since April for this code? You should have probably asked questions earlier.


I said that I've been coding (in general) for about 30 hours a week since April. This particular code took 30 hours. It hasn't taken me whatever 30 *4 * 5 is, I guess 600 hours.
Was This Post Helpful? 0
  • +
  • -

#6 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2386
  • View blog
  • Posts: 5,009
  • Joined: 11-December 07

Re: It took me 30 hours to write 100 lines of code

Posted 03 September 2014 - 04:13 AM

I have difficulty understanding your code but also your description. However, your description sounds more like a program in English than a description of what your code is supposed to do. Let's see if we can translate from English into VB.

Your code should probably look something like this. This is hardly great but it is at least readable.

public class MembersAndAncestors

	private justifiers() as String
	private ancestors() as String
	private onset() as String
	private more_sent() as String

	public sub new(justifiers() as String, ancestors() as String, onset() as String, more_sent() as String)
		me.justifiers = justifiers
		me.ancestors = ancestors
		me.onset = onset
		me.more_sent = more_sent
	end sub


	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. [/quote]
	public sub doTheThing()
		for each j in justifiers
			if not isNothing(j)
				processMember(j)
			end if
		next
	end sub

	private sub processMember(member as String)
		dim sentencesThatJustifyMember = findTheSentencesThatJustifiedTheMember(member)
		dim correspondingAncestors = getCorrespondingAncestors(member)
		dim sentencesThatCauseAncestor = whichSentencesCausesTheAncestors(correspondingAncestors)
		deleteMember(member)
	end sub

	' 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. 
	private function findTheSentencesThatJustifiedTheMember(member as String) as List(Of String)
		dim result as new List(Of String)()
		result.addAll(member, onset)
		result.addAll(member, more_sent)
		return result
	end function

	private function findAllInArray(member as String, arr() as String) as List(Of String)
		dim result as new List(Of String)()
		for each sentence in arr
			if doesSentenceJustifyMember(sentence, member) then
				result.add(sentence)
			end if
		next
		return result
	end function

	private function doesSentenceJustifyMember(sentence as String, member as String) as Boolean
		' You'll have to fill this in yourself
	end function

	'After that was done, it then had to get the corresponding members from the ancestors array
	private function getCorrespondingAncestors(member as String) as List(Of String)
		dim result as new List(Of String)()
		for each a in ancestors
			if isAncestor(member, a) then
				result.add(a)
			end if
		next
		return result
	end function

	private function isAncestor(member, maybeAncestor) as Boolean
		' another one for you to fill in'
	end function

	and then it had to take each ancestor and look through the osent and more_sent array to find which sentences causes the ancestors.
	private function whichSentencesCausesTheAncestors(correspondingAncestors as List(Of String)) as List(Of String)
		dim result as new List(Of String)()
		for each a in correspondingAncestors
			result.addAll(findSentencesCausingAncestor(a, onset))
			result.addAll(findSentencesCausingAncestor(a, more_sent))
		next
		return result
	end function

	private function findSentencesCausingAncestor(ancestor as String, arr() as String) as List(Of String)
		dim result as new List(Of String)()
		for each sentence in arr
			if doesSentenceCauseAncestor(sentence, ancestor) then
				result.add(sentence)
			end if
		next
		return result
	end function

	private function doesSentenceCauseAncestor(sentence as String, ancestor as String) as Boolean
		' You'll have to fill this in yourself
	end function

	'And when it found them, it had to delete the members. 
	private sub deleteMember(member as String)

	end sub

end class




Was This Post Helpful? 0
  • +
  • -

#7 DarenR  Icon User is offline

  • D.I.C Lover

Reputation: 592
  • View blog
  • Posts: 3,823
  • Joined: 12-January 10

Re: It took me 30 hours to write 100 lines of code

Posted 03 September 2014 - 04:31 AM

if you are judging your code based on hours then you are doing it wrong.. i judge my code based on smiles.
Was This Post Helpful? 0
  • +
  • -

#8 astonecipher  Icon User is offline

  • Too busy for this
  • member icon

Reputation: 2330
  • View blog
  • Posts: 9,381
  • Joined: 03-December 12

Re: It took me 30 hours to write 100 lines of code

Posted 03 September 2014 - 06:48 AM

Mine always gets judged on whether it works and how well.
Was This Post Helpful? 0
  • +
  • -

#9 leibniz76  Icon User is offline

  • D.I.C Head

Reputation: -11
  • View blog
  • Posts: 89
  • Joined: 02-May 14

Re: It took me 30 hours to write 100 lines of code

Posted 03 September 2014 - 01:53 PM

View Postcfoley, on 03 September 2014 - 04:13 AM, said:

I have difficulty understanding your code but also your description. However, your description sounds more like a program in English than a description of what your code is supposed to do. Let's see if we can translate from English into VB.

Your code should probably look something like this. This is hardly great but it is at least readable.

public class MembersAndAncestors

	private justifiers() as String
	private ancestors() as String
	private onset() as String
	private more_sent() as String

	public sub new(justifiers() as String, ancestors() as String, onset() as String, more_sent() as String)
		me.justifiers = justifiers
		me.ancestors = ancestors
		me.onset = onset
		me.more_sent = more_sent
	end sub


	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. [/quote]
	public sub doTheThing()
		for each j in justifiers
			if not isNothing(j)
				processMember(j)
			end if
		next
	end sub

	private sub processMember(member as String)
		dim sentencesThatJustifyMember = findTheSentencesThatJustifiedTheMember(member)
		dim correspondingAncestors = getCorrespondingAncestors(member)
		dim sentencesThatCauseAncestor = whichSentencesCausesTheAncestors(correspondingAncestors)
		deleteMember(member)
	end sub

	' 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. 
	private function findTheSentencesThatJustifiedTheMember(member as String) as List(Of String)
		dim result as new List(Of String)()
		result.addAll(member, onset)
		result.addAll(member, more_sent)
		return result
	end function

	private function findAllInArray(member as String, arr() as String) as List(Of String)
		dim result as new List(Of String)()
		for each sentence in arr
			if doesSentenceJustifyMember(sentence, member) then
				result.add(sentence)
			end if
		next
		return result
	end function

	private function doesSentenceJustifyMember(sentence as String, member as String) as Boolean
		' You'll have to fill this in yourself
	end function

	'After that was done, it then had to get the corresponding members from the ancestors array
	private function getCorrespondingAncestors(member as String) as List(Of String)
		dim result as new List(Of String)()
		for each a in ancestors
			if isAncestor(member, a) then
				result.add(a)
			end if
		next
		return result
	end function

	private function isAncestor(member, maybeAncestor) as Boolean
		' another one for you to fill in'
	end function

	and then it had to take each ancestor and look through the osent and more_sent array to find which sentences causes the ancestors.
	private function whichSentencesCausesTheAncestors(correspondingAncestors as List(Of String)) as List(Of String)
		dim result as new List(Of String)()
		for each a in correspondingAncestors
			result.addAll(findSentencesCausingAncestor(a, onset))
			result.addAll(findSentencesCausingAncestor(a, more_sent))
		next
		return result
	end function

	private function findSentencesCausingAncestor(ancestor as String, arr() as String) as List(Of String)
		dim result as new List(Of String)()
		for each sentence in arr
			if doesSentenceCauseAncestor(sentence, ancestor) then
				result.add(sentence)
			end if
		next
		return result
	end function

	private function doesSentenceCauseAncestor(sentence as String, ancestor as String) as Boolean
		' You'll have to fill this in yourself
	end function

	'And when it found them, it had to delete the members. 
	private sub deleteMember(member as String)

	end sub

end class





What a coincidence, my last name is Foley. If you want to find out if we share the same great grandfather, then mine is Michael Foley and I think he was living in Ann Arbor Michigan in the 1859 census. He had several children one of which was Charles which is my grandfather.


Let me give a better description of the code. What it is is it's a logic parser. So what we're doing is you take string:

If a & b & c & d then e.

What will happen is that from the sentences in the antecedent you can infer new sentences but only some of them will eventually lead to sentence e which is the sentence we need in the consequent. For example,

From c and d, infer g
From a and b, infer i

We can also start inferring new sentences from the sentences we just inferred, so

From i and b infer k
From g and c infer m
From m and g infer e


I call d the ancestor of g.
And I put all the newly inferred sentences in the array more_sent which stands for more sentences.

So what I did was I started with the sentences that were in the array called justifiers which is actually a misnomer because it should justified. (This is where I made a big mistake because I kept trying to write the code starting with the more_sent array which wasn't a good idea.)

So in the above example, the more_sentences would be

1 g
2 i
3 k
4 m
5 e

The ancestors array has to be the same size as the more_sentences array
1 c & d
2. a & b
3. i & b
4. g & c
5. m & g

So what I did was I started with e and then I found its ancestors, then I worked backwards to find all those sentences needed in order to justify e. But it looks like from your code that you understood what was going on, so thanks for taking an interest.
Was This Post Helpful? 0
  • +
  • -

#10 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2386
  • View blog
  • Posts: 5,009
  • Joined: 11-December 07

Re: It took me 30 hours to write 100 lines of code

Posted 03 September 2014 - 11:49 PM

Wrong side of the Atlantic, I'm afraid. :)

Your second description is much better. I feel I understand the problem enough to do my own design and implementation.

There are several issues with your code but the key one is that you have tried to tackle the whole problem in one go. That approach is doomed to failure. It's too big a bite to take. The skill in programming is to break the problem into smaller manageable chunks. Functions and Subs are your friend.

Take a look at my code. It's a similar length to yours. In fact, it's longer because I have fewer blank lines. It's made up of a bunch of small functions. Very small, certainly no longer than 10 lines and usually much shorter. None of them do anything fancy. In fact, they're trivial and that's the way you want your code to be. Each function is easy to write and easy to understand. As a bonus, it's possible to test them independently too.

Now, I'm not pretending my code is perfect but it serves as an example of how to break the problem down. The nice thing is that you really broke it down yourself with your first description. All I did was translate your sentences into function names and fill in the gaps. Before you try and fix your code, I recommend you split it up into functions. Then you can get each to work independently before putting them together.

How are you learning to program? You've obviously invested a lot of time into it. 30 hours is not far off a working week and you certainly seem to have no problem with the syntax. I was self taught for the first ten years or so and I know only too well that without guidance it's easy to follow the wrong path.

I naively thought short terse code was the fastest code, that naming things wasn't important, that functions were a waste of time. You get good at what you practice and I got good at nesting loops and if statements very deeply, at remembering what a dozen poorly named variables did, at navigating blocks of code that were hundreds of lines long. Unfortunately, those are not useful skills to have.

If you post a list of the resources or books you have used so far then maybe I and others can suggest some further reading.
Was This Post Helpful? 0
  • +
  • -

#11 leibniz76  Icon User is offline

  • D.I.C Head

Reputation: -11
  • View blog
  • Posts: 89
  • Joined: 02-May 14

Re: It took me 30 hours to write 100 lines of code

Posted 05 September 2014 - 10:34 PM

View Postcfoley, on 03 September 2014 - 11:49 PM, said:

Wrong side of the Atlantic, I'm afraid. :)/>

Does this mean you're living in Ireland?



Quote

Take a look at my code. It's a similar length to yours. In fact, it's longer because I have fewer blank lines. It's made up of a bunch of small functions. Very small, certainly no longer than 10 lines and usually much shorter. None of them do anything fancy. In fact, they're trivial and that's the way you want your code to be. Each function is easy to write and easy to understand. As a bonus, it's possible to test them independently too.

Are you talking about the code you wrote in VB? I have trouble understanding it because I have no experience with VB. I would have to probably step through it in order to understand what is going on.



Quote

How are you learning to program? You've obviously invested a lot of time into it. 30 hours is not far off a working week and you certainly seem to have no problem with the syntax. I was self taught for the first ten years or so and I know only too well that without guidance it's easy to follow the wrong path.

I started out with a tutor from India and I just watched him program with teamveiwer and skype for about 3 weeks, then I was able to do almost all the coding on my own. That's all the experience I have. Like I said I've been coding since about April. I realize it's an error on my part but I just have trouble studying coding. I don't even research it. Whenever I get into a jam I just post a question on the web or ask my former tutor. I realize this is a bad habit and I'm going to work on correcting it soon. The thing is that I'm really working on three or maybe four problems at once. On the one hand, I'm coding this program. The program is designed to determine if a sentence is a contradiction or not. That's a very big problem so I spend almost all of my time working on that. The third problem is that I want to justify metaphysical sentences, such as 'the self is real', 'time does not pass'. The fourth problem is that I try to learn as much about logic as I can. So the last three problems consume all my attention and I ignore the first problem. When I move over to PHP however I've got to study PHP because I just have to.


Quote

If you post a list of the resources or books you have used so far then maybe I and others can suggest some further reading.


Basically I read one chapter from some VBA book and that's it.


If you want I can send you the whole code and the excel doc and you can make suggestions. I'll try to make some video that explains what it does. But bear in mind that the code is 200 pages long.
Was This Post Helpful? 0
  • +
  • -

#12 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13488
  • View blog
  • Posts: 53,879
  • Joined: 12-June 08

Re: It took me 30 hours to write 100 lines of code

Posted 05 September 2014 - 10:38 PM

... you are doing this in VBA via Excel? Eeesh.
Was This Post Helpful? 0
  • +
  • -

#13 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2386
  • View blog
  • Posts: 5,009
  • Joined: 11-December 07

Re: It took me 30 hours to write 100 lines of code

Posted 08 September 2014 - 03:31 AM

It's great that Excel has VBA. It's not great to use it as your main development language.

I'm guessing that if you are using VBA then programming is not your job or at least that it is a very small part of it. However, 30 hours a week is too much effort to spend unwisely. If you have aspirations to become a programmer even just as a hobby I strongly recommend you stop VBA at once and learn a "proper" language.

VB.NET might be a natural transition from VBA but I would strongly recommend Python as an alternative. My choice would be Python but either would be a solid choice. You can download all the software you need for free and you can find some tutorials online for free too. Why don't you try them both and then pick the one you prefer?

Once you've picked, online tutorials are one thing but they don't replace a book. Check out he VB.NET or Python forums for book recommendations.

The thing is that I'm really working on three or maybe four problems at once.

Quote

Whenever I get into a jam I just post a question on the web or ask my former tutor. I realize this is a bad habit and I'm going to work on correcting it soon. The thing is that I'm really working on three or maybe four problems at once.


You're flailing around without direction and getting into messes you don't know how to fix. And you're spending a lot of your time doing it. There's nothing wrong with that if that's what you want to do and if you're happy not finishing any of your projects. You should still work on your projects but do them in parallel to working through a book.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1