Challenge: L-R Parse?

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 10788 Views - Last Post: 09 November 2014 - 08:24 PM

#1 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2270
  • View blog
  • Posts: 9,496
  • Joined: 29-May 08

Challenge: L-R Parse?

Post icon  Posted 12 June 2014 - 05:56 AM

Challenge: L-R Parse?


Challenge: (Easy - Moderate)

Your challenge if you choose to accept it, is to validate an input text to see if it a valid number which contains the digit separator , Note: , in this context is thousands separator not the french meaning of decimal point)

Public Function Valid( text As String ) As Boolean

End Function



Rules
  • The checking must be done left to right not right to left.
  • No use of Regular Expression
  • Use basic looping constructs eg.
  • Use basic string manipulation functions
  • Use of Char.IsDigit is permitted


Examples
,       N
,0      N
,00     N
,000    N
0,      N
00,     N
0,0     N
0,00    N
0,000   Y


This challenge is open to all languages but VB.net enteries are preferred.

Is This A Good Question/Topic? 1
  • +

Replies To: Challenge: L-R Parse?

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1606
  • View blog
  • Posts: 5,163
  • Joined: 25-September 09

Re: Challenge: L-R Parse?

Posted 12 June 2014 - 11:08 AM

Hmmm, here's my first attempt. I want to reduce this to a single loop but ran out of time at the moment. Will give it another look later

Spoiler

","	  	False
",0"		False
",00"		False
",000"		False
"0,"		False
"00,"		False
"0,0"		False
"0,00"		False
"0,000"		True
"00,000"	True
"000,000"	True
"0,000,000"	True
"0000,000"	False
"0.000,000"	False
"0x000,000"	False
"0000,0000"	False

Was This Post Helpful? 1
  • +
  • -

#3 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2270
  • View blog
  • Posts: 9,496
  • Joined: 29-May 08

Re: Challenge: L-R Parse?

Posted 12 June 2014 - 01:39 PM

Here's my attempt.
Spoiler

Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1606
  • View blog
  • Posts: 5,163
  • Joined: 25-September 09

Re: Challenge: L-R Parse?

Posted 12 June 2014 - 01:55 PM

With your code I'm getting a True return for "0000,0000"

EDIT:
I'm also getting True for "0000,000"

This post has been edited by CharlieMay: 12 June 2014 - 01:57 PM

Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2270
  • View blog
  • Posts: 9,496
  • Joined: 29-May 08

Re: Challenge: L-R Parse?

Posted 12 June 2014 - 02:20 PM

:withstupid:
Spoiler

This post has been edited by AdamSpeight2008: 12 June 2014 - 02:20 PM

Was This Post Helpful? 0
  • +
  • -

#6 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2066
  • View blog
  • Posts: 4,299
  • Joined: 11-December 07

Re: Challenge: L-R Parse?

Posted 13 June 2014 - 05:28 AM

Here's a recursive version. I think one of your tests is wrong. 0,000 is not a valid number.

Spoiler


Here's the full code including tests:

Spoiler

This post has been edited by cfoley: 13 June 2014 - 05:29 AM

Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2270
  • View blog
  • Posts: 9,496
  • Joined: 29-May 08

Re: Challenge: L-R Parse?

Posted 13 June 2014 - 11:46 AM

cfoley Why isn't 0,000 a valid number? I placed no restriction on what the left-most digit could be.
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9493
  • View blog
  • Posts: 35,834
  • Joined: 12-June 08

Re: Challenge: L-R Parse?

Posted 13 June 2014 - 12:21 PM

Edit edit:
Double fart - I just updated my for loop direction. Rules still apply.

Edit:
shit.. sorry.. got side tracked, came back to it, and did this right to left.. Ignore me.


Rolled a little more verbose, and rules complex, than most folk (or so it seems on passing inspection)

Spoiler

This post has been edited by AdamSpeight2008: 13 June 2014 - 12:39 PM

Was This Post Helpful? 1
  • +
  • -

#9 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2270
  • View blog
  • Posts: 9,496
  • Joined: 29-May 08

Re: Challenge: L-R Parse?

Posted 13 June 2014 - 12:39 PM

modi123_1 You are breaking the rules: It must be done Left To Right
Must have changed during edit.

This post has been edited by AdamSpeight2008: 13 June 2014 - 12:40 PM

Was This Post Helpful? 0
  • +
  • -

#10 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9493
  • View blog
  • Posts: 35,834
  • Joined: 12-June 08

Re: Challenge: L-R Parse?

Posted 13 June 2014 - 12:41 PM

Yeah.. double farts on me.
Was This Post Helpful? 0
  • +
  • -

#11 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2270
  • View blog
  • Posts: 9,496
  • Joined: 29-May 08

Re: Challenge: L-R Parse?

Posted 13 June 2014 - 01:53 PM

Squeezed a few nuts and bolts, I've gotten it down to 11 LoC
Spoiler

Was This Post Helpful? 1
  • +
  • -

#12 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9493
  • View blog
  • Posts: 35,834
  • Joined: 12-June 08

Re: Challenge: L-R Parse?

Posted 13 June 2014 - 01:56 PM

Quote

Squeezed a few nuts and bolts

Double entendre intended?

Spoiler

Was This Post Helpful? 0
  • +
  • -

#13 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2066
  • View blog
  • Posts: 4,299
  • Joined: 11-December 07

Re: Challenge: L-R Parse?

Posted 13 June 2014 - 02:34 PM

View PostAdamSpeight2008, on 13 June 2014 - 07:46 PM, said:

cfoley Why isn't 0,000 a valid number? I placed no restriction on what the left-most digit could be.


Yes, I thought you made a mistake. Don't mind me.
Was This Post Helpful? 0
  • +
  • -

#14 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2066
  • View blog
  • Posts: 4,299
  • Joined: 11-December 07

Re: Challenge: L-R Parse?

Posted 14 June 2014 - 05:09 AM

Well, I guess that make my code simpler anyway. The spec doesn't sat anything about empty strings either so I'll take out that check too:

    Public Function Valid(ByVal text As String) As Boolean
        Return Not text.StartsWith(",") AndAlso ValidHelper(text)
    End Function

    Public Function ValidHelper(ByVal text As String) As Boolean
        If (text.Equals("")) Then Return True

        Dim expectComma = (text.Length() Mod 4) = 0
        Dim isCharValid = (expectComma AndAlso text(0).Equals(","c)) _
                          OrElse (Not expectComma AndAlso Char.IsDigit(text(0)))
        Return isCharValid AndAlso ValidHelper(text.Substring(1))
    End Function



But I guess loops were in the spec so:

    Public Function Valid(ByVal text As String) As Boolean
        For i = 0 To text.Length - 1
            If ((text.Length - i) Mod 4) = 0 Then
                If Not text(i).Equals(","c) Then Return False
            Else
                If Not Char.IsDigit(text(i)) Then Return False
            End If
        Next
        Return Not text.StartsWith(",")
    End Function


Was This Post Helpful? 0
  • +
  • -

#15 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2066
  • View blog
  • Posts: 4,299
  • Joined: 11-December 07

Re: Challenge: L-R Parse?

Posted 14 June 2014 - 05:19 AM

Case is interesting here. I think it adds a little clarity:

    Public Function Valid(ByVal text As String) As Boolean
        For i = 0 To text.Length - 1
            Dim expectComma = ((text.Length - i) Mod 4) = 0
            Select Case text(i)
                Case ","c : If Not expectComma Then Return False
                Case "0"c To "9"c : If expectComma Then Return False
                Case Else : Return False
            End Select
        Next
        Return Not text.StartsWith(",")
    End Function


Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2