11 Replies - 24255 Views - Last Post: 09 July 2012 - 05:23 AM

#1 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2251
  • View blog
  • Posts: 9,435
  • Joined: 29-May 08

Challenge: The Dot Creation Of Numbers (Intermediate)

Post icon  Posted 29 April 2012 - 05:01 PM

Challenge: The Dot Creation Of Numbers (Intermediate)

Challenge:

You know how in the IDE when you type . you get member information about the object.
OK, so I challenge you to do the same for the generation of Number. We'll stick to just Integer ones for now.

Via The Digits (Easy-Intermediate)
Dim MyNumber1 = Number.One.Two.Three.Four
' Will return an Integer with the value 1234 '
Dim MyNumber2 = Number.One.Negative.Two 
' Should be an error, because Sign information (whether the number positive or negative must be first.)
Dim MyNumber3 = Number.Negative.One.
' = -1 '



Via The Wording (Intermediate+)

The second challenge is to extend this so you can "Dot Input" Numbers as they said.


Dim MyNumber = Number.One.Million.Two.Hundred.And.Four.Thousand.Nine.Hundred.And.Seventy.Two
' Will return an Integer with the value 1204972'



These should give your brains a work out.

This post has been edited by AdamSpeight2008: 29 April 2012 - 05:03 PM


Is This A Good Question/Topic? 1
  • +

Replies To: Challenge: The Dot Creation Of Numbers (Intermediate)

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2251
  • View blog
  • Posts: 9,435
  • Joined: 29-May 08

Re: Challenge: The Dot Creation Of Numbers (Intermediate)

Posted 01 May 2012 - 01:47 AM

I have an implementation of the Via Digits method that does Positive and Negative numbers (Integer), as well as Floating point numbers (Double).
What surprised me is how little coding was needed sub 200 lines (at 156 lines), Surprisingly small.
Was This Post Helpful? 0
  • +
  • -

#3 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1949
  • View blog
  • Posts: 4,048
  • Joined: 11-December 07

Re: Challenge: The Dot Creation Of Numbers (Intermediate)

Posted 01 May 2012 - 03:07 PM

I googled for VB.NET metaprogramming and turned up a blank. Here is what I was aiming for in ~20 lines of Ruby. If metaprogramming is possible in VB, the code would be about the same length. I suppose I could alter my ruby code to generate all that boilerplate VB but is anything like this possible out of the box?

class Number

	def initialize(val, sgn)
		@value = val
		@sign = sgn
	end
	
	def self.negative
		Number.new(0, -1)
	end
	
	def appendDigit(digit)
		Number.new((@value * 10) + digit, @sign)
	end
	
	def to_s
		(@sign * @value).to_s
	end
	
	methods = [:zero, :one, :two, :three, :four, :five, :six, :seven, :eight, :nine]
	methods.each_index do | digit |
		name = methods[digit]
		define_method(name) {appendDigit(digit)}
		define_singleton_method(name) {return Number.new(digit, 1)}
	end
	
end


puts Number.zero
puts Number.one
puts Number.two
puts Number.three
puts Number.four
puts Number.five
puts Number.six
puts Number.seven
puts Number.eight
puts Number.nine
puts Number.negative.one.two.three.four.five.six.seven.eight.nine.zero

Was This Post Helpful? 1
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2251
  • View blog
  • Posts: 9,435
  • Joined: 29-May 08

Re: Challenge: The Dot Creation Of Numbers (Intermediate)

Posted 01 May 2012 - 04:04 PM

cfoley: Is the negative method restricted to only appear only on Number and not subsequent methods. Eg Number.one.negative is an error, I can't tell that what happens in the Ruby code?

This post has been edited by AdamSpeight2008: 01 May 2012 - 04:06 PM

Was This Post Helpful? 0
  • +
  • -

#5 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1949
  • View blog
  • Posts: 4,048
  • Joined: 11-December 07

Re: Challenge: The Dot Creation Of Numbers (Intermediate)

Posted 01 May 2012 - 05:28 PM

Yes, declaring it the way I did makes it like a shared method in VB. The generated methods for the digits have one "shared" version and one instance version.
Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2251
  • View blog
  • Posts: 9,435
  • Joined: 29-May 08

Re: Challenge: The Dot Creation Of Numbers (Intermediate)

Posted 02 May 2012 - 03:57 PM

Here's my current implementation which does both Integer and Float numbers, via the digit method. Around 135 LoC (compiles to 70 IL).
It's not pretty, but it works.
Option Strict On
Imports System.Runtime.CompilerServices
Public Module Number
#Region "Boot Strapping"
  Public Function Minus() As IntegerNumber
    Return New IntegerNumber("0", True)
  End Function
  Public Function One() As IntegerNumber
    Return New IntegerNumber("1")
  End Function
  Public Function Two() As IntegerNumber
    Return New IntegerNumber("2")
  End Function
  Public Function Three() As IntegerNumber
    Return New IntegerNumber("3")
  End Function
  Public Function Four() As IntegerNumber
    Return New IntegerNumber("4")
  End Function
  Public Function Five() As IntegerNumber
    Return New IntegerNumber("5")
  End Function
  Public Function Six() As IntegerNumber
    Return New IntegerNumber("6")
  End Function
  Public Function Seven() As IntegerNumber
    Return New IntegerNumber("7")
  End Function
  Public Function Eight() As IntegerNumber
    Return New IntegerNumber("8")
  End Function
  Public Function Nine() As IntegerNumber
    Return New IntegerNumber("9")
  End Function
  Public Function Zero() As IntegerNumber
    Return New IntegerNumber("0")
  End Function
#End Region
#Region "Extend a Digit on to the number."
  <Extension()> Public Function One(Of T As NumBase)(Old As T) As T
    Return CType(Old.CreateNew(Old.AppendNewDigit("1")), T)
  End Function
  <Extension()> Public Function Two(Of T As NumBase)(Old As T) As T
    Return CType(Old.CreateNew(Old.AppendNewDigit("2")), T)
  End Function
  <Extension()> Public Function Three(Of T As NumBase)(Old As T) As T
    Return CType(Old.CreateNew(Old.AppendNewDigit("3")), T)
  End Function
  <Extension()> Public Function Four(Of T As NumBase)(Old As T) As T
    Return CType(Old.CreateNew(Old.AppendNewDigit("4")), T)
  End Function
  <Extension()> Public Function Five(Of T As NumBase)(Old As T) As T
    Return CType(Old.CreateNew(Old.AppendNewDigit("5")), T)
  End Function
  <Extension()> Public Function Six(Of T As NumBase)(Old As T) As T
    Return CType(Old.CreateNew(Old.AppendNewDigit("6")), T)
  End Function
  <Extension()> Public Function Seven(Of T As NumBase)(Old As T) As T
    Return CType(Old.CreateNew(Old.AppendNewDigit("7")), T)
  End Function
  <Extension()> Public Function Eight(Of T As NumBase)(Old As T) As T
    Return CType(Old.CreateNew(Old.AppendNewDigit("8")), T)
  End Function
  <Extension()> Public Function Nine(Of T As NumBase)(Old As T) As T
    Return CType(Old.CreateNew(Old.AppendNewDigit("9")), T)
  End Function
  <Extension()> Public Function Zero(Of T As NumBase)(Old As T) As T
    Return CType(Old.CreateNew(Old.AppendNewDigit("0")), T)
  End Function
#End Region
#Region "Integer -> Float Methods"
  <Extension()> Public Function Point(Old As IntegerNumber) As FloatNumber
    Return New FloatNumber(Old._Digits + ".", Old._IsNeg)
  End Function
  Public Function Point() As FloatNumber
    Return New FloatNumber("0.")
  End Function
#End Region
End Module

#Region "Numbers"
Public MustInherit Class NumBase
  Protected Friend _Digits As String = "0"
  Protected Friend _IsNeg As Boolean = False
  Protected Friend Sub New(Digits As String, Optional IsNeg As Boolean = False)
    _Digits = Digits : _IsNeg = IsNeg
  End Sub
  Protected Friend MustOverride Function CreateNew(Digits As String) As NumBase
  Protected Friend MustOverride Function AppendNewDigit(Digit As String) As String
  Public Overrides Function ToString() As String
    Return If(_IsNeg, "-", "") + _Digits
  End Function
End Class

#Region "Integer Numbers"
Public Class IntegerNumber
  Inherits NumBase
  Protected Friend _Value As Integer
  Protected Friend Sub New(_Digits As String, Optional IsNeg As Boolean = False)
    MyBase.New(_Digits, IsNeg)
  End Sub
  Protected Friend Overrides Function CreateNew(Digits As String) As NumBase
    Return New IntegerNumber(Digits, Me._IsNeg)
  End Function
  Protected Friend Overrides Function AppendNewDigit(Digit As String) As String
    Dim NewValue As Integer
    Return If(Integer.TryParse(Me._Digits & Digit, NewValue), NewValue.ToString(), Me._Digits)
  End Function
  Public Overrides Function ToString() As String
    Return If(_IsNeg, "-", "") + _Digits
  End Function
  Public Shared Widening Operator CType(n As IntegerNumber) As Integer
    Return n._Value
  End Operator
End Class
#End Region
#Region "Floating Point Number"
Public Class FloatNumber
  Inherits NumBase
  Protected Friend _Value As Double
  Protected Friend Sub New(_Digits As String, Optional IsNeg As Boolean = False)
    MyBase.New(_Digits, IsNeg)
  End Sub
  Protected Friend Overrides Function CreateNew(Digits As String) As NumBase
    Return New FloatNumber(Digits, Me._IsNeg)
  End Function
  Protected Friend Overrides Function AppendNewDigit(Digit As String) As String
    Dim NewValue As Double
    Return If(Double.TryParse(Me._Digits & Digit, NewValue), NewValue.ToString(), Me._Digits)
  End Function
  Public Overrides Function ToString() As String
    Return If(_IsNeg, "-", "") + _Digits
  End Function
  Public Shared Widening Operator CType(n As FloatNumber) As Double
    Return n._Value
  End Operator
End Class
#End Region
#End Region

This post has been edited by AdamSpeight2008: 02 May 2012 - 04:05 PM

Was This Post Helpful? 1
  • +
  • -

#7 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1949
  • View blog
  • Posts: 4,048
  • Joined: 11-December 07

Re: Challenge: The Dot Creation Of Numbers (Intermediate)

Posted 03 May 2012 - 02:46 AM

If I was asked to write a static factory method that gave compile time errors rather than runtime errors, I would probably come up with something like this:

Public Class Number

    Private value As Integer

    Private Sub New(ByVal value As Integer)
        Me.value = value
    End Sub

    Public Overrides Function ToString() As String
        Return value.ToString()
    End Function

    Public Shared Function make(ByVal ParamArray digits() As Digit) As Number
        Return make(1, digits)
    End Function

    Public Shared Function makeNegative(ByVal ParamArray digits() As Digit) As Number
        Return make(-1, digits)
    End Function

    Private Shared Function make(ByVal sign As Integer, ByVal ParamArray digits() As Digit) As Number
        Dim value As Integer = 0
        For Each d In digits
            value = (value * 10) + d.toInt
        Next
        Return New Number(sign * value)
    End Function

End Class


Public Class Digit

    Public Shared ReadOnly one As Digit = New Digit(1)
    Public Shared ReadOnly two As Digit = New Digit(2)
    Public Shared ReadOnly three As Digit = New Digit(3)
    Public Shared ReadOnly four As Digit = New Digit(4)
    Public Shared ReadOnly five As Digit = New Digit(5)
    Public Shared ReadOnly six As Digit = New Digit(6)
    Public Shared ReadOnly seven As Digit = New Digit(7)
    Public Shared ReadOnly eight As Digit = New Digit(8)
    Public Shared ReadOnly nine As Digit = New Digit(9)
    Public Shared ReadOnly zero As Digit = New Digit(0)

    Private ReadOnly value As Integer

    Private Sub New(ByVal value As Integer)
        Me.value = value
    End Sub

    Public Function toInt() As Integer
        Return value
    End Function

End Class


Imports NumberBuilder.Digit

Module Module1

    Sub Main()
        Dim numbers() As Number = {
            Number.make(one, two, three),
            Number.make(zero, zero, five, nine),
            Number.makeNegative(seven, one)
            }

        For Each x In numbers
            Console.WriteLine(x)
        Next

        Console.Read()
    End Sub

End Module


Was This Post Helpful? 0
  • +
  • -

#8 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2251
  • View blog
  • Posts: 9,435
  • Joined: 29-May 08

Re: Challenge: The Dot Creation Of Numbers (Intermediate)

Posted 03 May 2012 - 08:34 AM

You could, but that not what the challenge. The challenge is do, via "dotting" the number.

I tried shared methods on the base class, but the trouble is they get inherited by the derived classes.
Was This Post Helpful? 0
  • +
  • -

#9 Tethik  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 61
  • Joined: 14-March 10

Re: Challenge: The Dot Creation Of Numbers (Intermediate)

Posted 04 May 2012 - 05:38 PM

Here's my first simple attempt. Using a stringbuilder and CType operator.
Note also that it's not possible to write the "Negative" prefix after the first dot.

I will try to improve on it a bit. Too much repetition at the moment...

Spoiler




Edit: New version using only Integer extensions and Static fields. :whistling:
Should be easy now to write a macro that generates these classes and extensions given String + Number value.
Imports System.Runtime.CompilerServices

Module NumberDotting

    Public Class Number
        Public Shared Zero As Integer = 0
        Public Shared One As Integer = 1
        Public Shared Two As Integer = 2
        Public Shared Three As Integer = 3
        Public Shared Four As Integer = 4
        Public Shared Five As Integer = 5
        Public Shared Six As Integer = 6
        Public Shared Seven As Integer = 7
        Public Shared Eight As Integer = 8
        Public Shared Nine As Integer = 9

        Public Class Negative
            'Public Shared Zero As Integer = 0 'Negative Zero? Will only cause problems.
            Public Shared One As Integer = -1
            Public Shared Two As Integer = -2
            Public Shared Three As Integer = -3
            Public Shared Four As Integer = -4
            Public Shared Five As Integer = -5
            Public Shared Six As Integer = -6
            Public Shared Seven As Integer = -7
            Public Shared Eight As Integer = -8
            Public Shared Nine As Integer = -9
        End Class
    End Class

    <Extension()> _
    Public Function Zero(ByVal val As Integer) As Integer
        If val = 0 Then
            Throw New Exception("Bad Integer Format!") ' If user tries to put Zero.?
        End If
        Return val * 10
    End Function

    <Extension()> _
    Public Function One(ByVal val As Integer) As Integer
        Return Zero(val) + IIf((val > 0), 1, -1)
    End Function

    <Extension()> _
    Public Function Two(ByVal val As Integer) As Integer
        Return Zero(val) + IIf((val > 0), 2, -2)
    End Function

    <Extension()> _
    Public Function Three(ByVal val As Integer) As Integer
        Return Zero(val) + IIf((val > 0), 3, -3)
    End Function

    <Extension()> _
    Public Function Four(ByVal val As Integer) As Integer
        Return Zero(val) + IIf((val > 0), 4, -4)
    End Function

    <Extension()> _
    Public Function Five(ByVal val As Integer) As Integer
        Return Zero(val) + IIf((val > 0), 5, -5)
    End Function

    <Extension()> _
    Public Function Six(ByVal val As Integer) As Integer
        Return Zero(val) + IIf((val > 0), 6, -6)
    End Function

    <Extension()> _
    Public Function Seven(ByVal val As Integer) As Integer
        Return Zero(val) + IIf((val > 0), 7, -7)
    End Function

    <Extension()> _
    Public Function Eight(ByVal val As Integer) As Integer
        Return Zero(val) + IIf((val > 0), 8, -8)
    End Function

    <Extension()> _
    Public Function Nine(ByVal val As Integer) As Integer
        Return Zero(val) + IIf((val > 0), 9, -9)
    End Function
End Module


This post has been edited by Tethik: 04 May 2012 - 06:54 PM

Was This Post Helpful? 1
  • +
  • -

#10 pradeep1210  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 56
  • Joined: 08-May 12

Re: Challenge: The Dot Creation Of Numbers (Intermediate)

Posted 11 May 2012 - 01:11 PM

Here is my solution to the problem. :)

Module NumberExtensions
    Public ReadOnly Number As Integer? = 0
    <Extension()> _
    Public Function Zero(ByVal n As Integer?) As Integer?
        Return JoinNumber(n, 0)
    End Function
    <Extension()> _
    Public Function One(ByVal n As Integer?) As Integer?
        Return JoinNumber(n, 1)
    End Function
    <Extension()> _
    Public Function Two(ByVal n As Integer?) As Integer?
        Return JoinNumber(n, 2)
    End Function
    <Extension()> _
    Public Function Three(ByVal n As Integer?) As Integer?
        Return JoinNumber(n, 3)
    End Function
    <Extension()> _
    Public Function Four(ByVal n As Integer?) As Integer?
        Return JoinNumber(n, 4)
    End Function
    <Extension()> _
    Public Function Five(ByVal n As Integer?) As Integer?
        Return JoinNumber(n, 5)
    End Function
    <Extension()> _
    Public Function Six(ByVal n As Integer?) As Integer?
        Return JoinNumber(n, 6)
    End Function
    <Extension()> _
    Public Function Seven(ByVal n As Integer?) As Integer?
        Return JoinNumber(n, 7)
    End Function
    <Extension()> _
    Public Function Eight(ByVal n As Integer?) As Integer?
        Return JoinNumber(n, 8)
    End Function
    <Extension()> _
    Public Function Nine(ByVal n As Integer?) As Integer?
        Return JoinNumber(n, 9)
    End Function
    <Extension()> _
    Public Function Negative(ByVal n As Integer?) As Integer?
        If n <> 0 Then Throw New Exception("Negative must be at first place!")
        Return JoinNumber(Nothing, 0)
    End Function

    Private Function JoinNumber(ByVal n1 As Integer?, ByVal n2 As Integer) As Integer?
        If n1 Is Nothing Then
            Return If(n2 = 0, n1, -n2)
        ElseIf n1 < 0 Then
            Return -(-10 * n1 + n2)
        Else
            Return 10 * n1 + n2
        End If
    End Function
End Module



To test the code, I started a new Windows Forms application and added the above code in a module.
Then I put a TextBox (with Multiline = True) and a Button on the form and the following code:
Option Strict On
Imports System.Runtime.CompilerServices

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim numberList As New List(Of Object)

        numberList.Add("Number.Negative.One.Two.Three.Nine")
        numberList.Add(Number.Negative.One.Two.Three.Nine)
        numberList.Add("")
        numberList.Add("Number.Negative.Two.Three.Nine")
        numberList.Add(Number.Negative.Two.Three.Nine)
        numberList.Add("")
        numberList.Add("Number.One.Two.Three.Nine")
        numberList.Add(Number.One.Two.Three.Nine)
        numberList.Add("")
        numberList.Add("Number.Two.Three.Nine")
        numberList.Add(Number.Two.Three.Nine)
        numberList.Add("")
        numberList.Add("Number.Negative.Zero.One.Two.Three.Nine")
        numberList.Add(Number.Negative.Zero.One.Two.Three.Nine)
        numberList.Add("")
        numberList.Add("Number.Negative.Zero.Two.Three.Nine")
        numberList.Add(Number.Negative.Zero.Two.Three.Nine)
        numberList.Add("")
        numberList.Add("Number.Zero.One.Two.Three.Nine")
        numberList.Add(Number.Zero.One.Two.Three.Nine)
        numberList.Add("")
        numberList.Add("Number.Two.Three.Nine.Zero")
        numberList.Add(Number.Two.Three.Nine.Zero)
        numberList.Add("")
        numberList.Add("Number.Two.Three.Zero.Nine.Zero")
        numberList.Add(Number.Two.Three.Zero.Nine.Zero)
        Try
            numberList.Add("")
            numberList.Add("Number.Two.Negative.Three.Nine")
            numberList.Add(Number.Two.Negative.Three.Nine)
        Catch ex As Exception
            numberList.Add(ex.Message)
        End Try

        TextBox1.Text = Join(numberList.Select(Function(f) CStr(f)).ToArray, vbCrLf)
    End Sub
End Class



On running the application, my form looks like this:
Attached Image
Was This Post Helpful? 1
  • +
  • -

#11 thava  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 180
  • View blog
  • Posts: 1,606
  • Joined: 17-April 07

Re: Challenge: The Dot Creation Of Numbers (Intermediate)

Posted 08 July 2012 - 05:17 PM

Edit: Snipped out the Quoted Original Post.

interesting how could i use the And operator as a member of some class

This post has been edited by AdamSpeight2008: 09 July 2012 - 05:20 AM
Reason for edit:: Snipping Quoted Post

Was This Post Helpful? 0
  • +
  • -

#12 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2251
  • View blog
  • Posts: 9,435
  • Joined: 29-May 08

Re: Challenge: The Dot Creation Of Numbers (Intermediate)

Posted 09 July 2012 - 05:23 AM

thava

The .And I exampled would be a method, but you could use the actual And operator. If you wantto overload it.

This post has been edited by AdamSpeight2008: 09 July 2012 - 05:24 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1