Connecting Tech Pros Worldwide Forums | Help | Site Map

Challenging text masking problem

exekutive
Guest
 
Posts: n/a
#1: Nov 21 '05

I've been wrestling with this programming problem for some time now, so I
thought I'd put it up for discussion. I'm trying to design a Mask function.
See if you can figure out a code for it...

The best way to explain this is to give an example of what the functions
should do.

* Let us a define a function as follows:

Public Function Mask(ByRef inputStr As String, ByRef maskInStr As
String, ByRef maskOutStr As String, ByVal delimiter As Char) As String

* Put in the following values:

inputStr = "The quick brown fox % jumped over the dog."
maskInStr = "The quick %1 fox %% jumped over the %2."
maskOutStr = "Yo %% momma's %1 beaver smells like %2."

* The function should return:

Mask = "Yo % momma's brown beaver smells like dog."

From this you should be able to figure out how the function should work.
Please, only post solutions that WORK. Good luck!
Larry Serflaten
Guest
 
Posts: n/a
#2: Nov 21 '05

re: Challenging text masking problem



"exekutive" <exekutive@discussions.microsoft.com> wrote[color=blue]
>
> I've been wrestling with this programming problem for some time now, so I
> thought I'd put it up for discussion. I'm trying to design a Mask function.
> See if you can figure out a code for it...[/color]

<snipped for brievity>
[color=blue]
> Please, only post solutions that WORK. Good luck![/color]

The task is a simple substitution. Why would you expect others to do your work?
I'll be interested in seeing who will decide to help out beyond simply stating you
have to split them up into words and use a dictionary to subsitute in the needed
parts....

???
LFS

Larry Serflaten
Guest
 
Posts: n/a
#3: Nov 21 '05

re: Challenging text masking problem



"exekutive" <exekutive@discussions.microsoft.com> wrote[color=blue]
>
> I've been wrestling with this programming problem for some time now, so I
> thought I'd put it up for discussion. I'm trying to design a Mask function.
> See if you can figure out a code for it...[/color]

<snipped for brievity>
[color=blue]
> Please, only post solutions that WORK. Good luck![/color]

The task is a simple substitution. Why would you expect others to do your work?
I'll be interested in seeing who will decide to help out beyond simply stating you
have to split them up into words and use a dictionary to subsitute in the needed
parts....

???
LFS

exekutive
Guest
 
Posts: n/a
#4: Nov 21 '05

re: Challenging text masking problem


Yes it seemed quite simple to me in theory as well, and I'm glad you think it
is so easy. No, I don't expect others to do my work. I am still trying to
figure this out and I invite others to engage in this problem as well and I
welcome their practical input.

"Larry Serflaten" wrote:
[color=blue]
>
> "exekutive" <exekutive@discussions.microsoft.com> wrote[color=green]
> >
> > I've been wrestling with this programming problem for some time now, so I
> > thought I'd put it up for discussion. I'm trying to design a Mask function.
> > See if you can figure out a code for it...[/color]
>
> <snipped for brievity>
>[color=green]
> > Please, only post solutions that WORK. Good luck![/color]
>
> The task is a simple substitution. Why would you expect others to do your work?
> I'll be interested in seeing who will decide to help out beyond simply stating you
> have to split them up into words and use a dictionary to subsitute in the needed
> parts....
>
> ???
> LFS
>
>[/color]
exekutive
Guest
 
Posts: n/a
#5: Nov 21 '05

re: Challenging text masking problem


Yes it seemed quite simple to me in theory as well, and I'm glad you think it
is so easy. No, I don't expect others to do my work. I am still trying to
figure this out and I invite others to engage in this problem as well and I
welcome their practical input.

"Larry Serflaten" wrote:
[color=blue]
>
> "exekutive" <exekutive@discussions.microsoft.com> wrote[color=green]
> >
> > I've been wrestling with this programming problem for some time now, so I
> > thought I'd put it up for discussion. I'm trying to design a Mask function.
> > See if you can figure out a code for it...[/color]
>
> <snipped for brievity>
>[color=green]
> > Please, only post solutions that WORK. Good luck![/color]
>
> The task is a simple substitution. Why would you expect others to do your work?
> I'll be interested in seeing who will decide to help out beyond simply stating you
> have to split them up into words and use a dictionary to subsitute in the needed
> parts....
>
> ???
> LFS
>
>[/color]
Tom Dacon
Guest
 
Posts: n/a
#6: Nov 21 '05

re: Challenging text masking problem


> > Please, only post solutions that WORK. Good luck!

And yet you specify that you are interested only in working solutions... to
me that sounds like you want someone else to solve the problem for you.

If you wanted others to engage in the problem, to assist YOU in solving the
problem, I would have expected you to solicit suggested approaches or
algorithmic techniques, similar to Larry's offering. I think there's enough
wool in what he suggested to allow you to knit a solution of your own.

Tom Dacon
Dacon Software Consulting

"exekutive" <exekutive@discussions.microsoft.com> wrote in message
news:CE565C80-C54D-40A7-B941-F380019D041F@microsoft.com...[color=blue]
> Yes it seemed quite simple to me in theory as well, and I'm glad you think
> it
> is so easy. No, I don't expect others to do my work. I am still trying
> to
> figure this out and I invite others to engage in this problem as well and
> I
> welcome their practical input.
>[/color]


exekutive
Guest
 
Posts: n/a
#7: Nov 21 '05

re: Challenging text masking problem


I wrote that in there because I know there are always those people out there
who are so sure of themselves and post things they think work but they've
never actually tried for themselves which makes it completely useless. For
example, saying it is a "simple substitution" is useless. Do you think that
it had not occured to me? The dictionary idea might be helpful if I knew
what that was.
I have tried several approaches now and didn't get far with them, often
running into the problem of needing arrays of unknow dimension. My current
idea is this: use a recursive function that uses the string.indexOf method to
create an array of integers representing the positions of %x tokens. The
rest is not clear yet but will involve matching the strings surrounding the
tokens in the mask to the input string.

"Tom Dacon" wrote:
[color=blue][color=green][color=darkred]
> > > Please, only post solutions that WORK. Good luck![/color][/color]
>
> And yet you specify that you are interested only in working solutions... to
> me that sounds like you want someone else to solve the problem for you.
>
> If you wanted others to engage in the problem, to assist YOU in solving the
> problem, I would have expected you to solicit suggested approaches or
> algorithmic techniques, similar to Larry's offering. I think there's enough
> wool in what he suggested to allow you to knit a solution of your own.
>
> Tom Dacon
> Dacon Software Consulting
>
> "exekutive" <exekutive@discussions.microsoft.com> wrote in message
> news:CE565C80-C54D-40A7-B941-F380019D041F@microsoft.com...[color=green]
> > Yes it seemed quite simple to me in theory as well, and I'm glad you think
> > it
> > is so easy. No, I don't expect others to do my work. I am still trying
> > to
> > figure this out and I invite others to engage in this problem as well and
> > I
> > welcome their practical input.
> >[/color]
>
>
>[/color]
Larry Serflaten
Guest
 
Posts: n/a
#8: Nov 21 '05

re: Challenging text masking problem



"exekutive" <exekutive@discussions.microsoft.com> wrote[color=blue]
> I wrote that in there because I know there are always those people out there
> who are so sure of themselves and post things they think work but they've
> never actually tried for themselves which makes it completely useless. For
> example, saying it is a "simple substitution" is useless. Do you think that
> it had not occured to me? The dictionary idea might be helpful if I knew
> what that was.[/color]

How much effort are you willing to put into finding a solution? If you put
no more effort into it than asking someone else to do it for you, you are not
going to see many responses. There are several search engines on the net that
will help you find working code examples, and help you learn what a Dictionary
object is. If you want code handed to you, go find it on the net. A Dictionary
is similar to a Collection but with a few major differences.

[color=blue]
> I have tried several approaches now and didn't get far with them, often
> running into the problem of needing arrays of unknow dimension. My current
> idea is this: use a recursive function that uses the string.indexOf method to
> create an array of integers representing the positions of %x tokens. The
> rest is not clear yet but will involve matching the strings surrounding the
> tokens in the mask to the input string.[/color]

I gave you an indication of how it could be done:

"I'll be interested in seeing who will decide to help out beyond simply stating you
have to split them up into words and use a dictionary to subsitute in the needed
parts.... "

Look up Split in VB help. It returns an array. So you split up the two input
strings which turns them into array of words. Loop through the arrays looking
for your %x tokens and associate the token of one array with the word from the
other array. How you associate them is up to you, you could use a dictionary or
a collection.

When you split the output string into an array, all you need do is loop through
that array looking for those %x tokens and substitute the associated word for
that token. When the loop is finished, you can use Join to put it all back into
a string for output. See Join in VB Help for examples of its use.

The newsgroups are not your personal developer resource, no one is paid anything
to participate. Most are here to share knowlege not give away code although,
code examples are a concise method often used to convey certain knowlege.
You are expected to research the problems and try to solve them yourself. You'll
find you will get many more responses if you post the code you've tried that
doesn't quite get the job done, instead of demanding that you want the entire task
completed, and only certain responses be allowed. As you've found out, no one
is obligated to post any code whatsoever, and if it appears you are not putting
much effort into solving the problem yourself, why should anyone else bother
putting more effort into it than you? Try the solution suggested, see how far you
get, and if you have problems, create a _small_ demo to show the problem so that
others can copy and paste your code and try it out to see it on their system. They
are much more likely to fix your code, rather than create a complete solution from
scratch.

LFS





Cor Ligthert
Guest
 
Posts: n/a
#9: Nov 21 '05

re: Challenging text masking problem


Exekutive,

I agree with Larry, however because your persistensie something more
explained, however in the same way as Larry's solution.

And to add as well something to this question, make your problem as clear as
possible by setting it in pseudo code, by changing words, you will find your
solution mostly yourself.

(And when it is a question do not use words as InMask like that, that is
your thinking, when you use that you have first to explain what you mean
with those words. String1, 2 etc is enough to show the sample).

String1 = "The quick brown fox % jumped over the dog."
String2 = "The quick %1 fox %% jumped over the %2."

Do for both a split string

Check if the resulting arrays are equal.

Find and set in a hashtable using a for index loop the words with a % found
by string.indexof in the second array. When found than the % word is from
the seccond array, the value from the first array with the same index.

String3 = "Yo %% momma's %1 beaver smells like %2."

Do a split of the thirth string and replace all the words which consist a %
using the key of the hashtable and replace that by the founded value . When
there is no match do not replace it.

Join the resulting array

When I did not write something wrong you should have in my opinion your
result string.

String4 = "Yo % momma's brown beaver smells like dog."

I hope this helps?

Cor


exekutive
Guest
 
Posts: n/a
#10: Nov 21 '05

re: Challenging text masking problem


Wow thats a lot of response. Thank you thank you. I'll have to digest this
for a while. In the meantime, here's what I wrote ... the Mask function
isn't finished obviously, but the FindDelimiters function successfully
returns an integer array of the postions of %x tokens within the inStr

Expand|Select|Wrap|Line Numbers
  1. Public Function Mask(ByRef inputStr As String, ByRef maskInStr As
  2. String, ByRef maskOutStr As String, ByVal delimiter As Char) As String
  3.  
  4. Dim outputStr As String
  5. Dim delimiterIndexes As Integer()
  6.  
  7. delimiterIndexes = FindDelimiters(maskInStr, delimiter)
  8.  
  9. Return outputStr
  10.  
  11. End Function
  12.  
  13. Public Function FindDelimiters(ByRef inStr As String, ByVal d As Char)
  14. As Integer()
  15.  
  16.  
  17. Dim tempIndex As Integer = -2
  18. Dim delimiterCounter As Integer
  19.  
  20. ' Count the instances of 'd' in 'inStr'
  21. Do
  22. tempIndex = inStr.IndexOf(d, tempIndex + 2)
  23. If tempIndex <> -1 Then delimiterCounter += 1
  24. Loop Until tempIndex = -1
  25.  
  26. ' Set the bounds of an array to the number of instances of 'd'
  27. Dim indexDelimiters(delimiterCounter - 1) As Integer
  28.  
  29. ' Fill the array with the positions of 'd' in 'inStr'
  30. For tempIndex = 0 To delimiterCounter - 1
  31. If tempIndex = 0 Then
  32. indexDelimiters(tempIndex) = inStr.IndexOf(d, 0)
  33. Else
  34. indexDelimiters(tempIndex) = inStr.IndexOf(d,
  35. indexDelimiters(tempIndex - 1) + 2)
  36. End If
  37. Next
  38.  
  39. Return indexDelimiters
  40.  
  41. End Function
  42.  
  43.  
Cor Ligthert
Guest
 
Posts: n/a
#11: Nov 21 '05

re: Challenging text masking problem


Executive,

I do not understand you, Larry gave you an answer, I completed it something
more and than you come with a solution that is completly different, why you
ask here in this newsgroup for help?

This could have been the solution as Larry and I suggested to you.
\\\
Public Class main
Public Shared Sub main()
Dim String1 As String = "The quick brown fox % jumped over the dog."
Dim String2 As String = "The quick %1 fox %% jumped over the %2."
Dim String3 As String = "Yo %% momma's %1 beaver smells like %2."
Dim a As String() = String1.Split(" "c)
Dim b As String() = String2.Split(" "c)
If a.Length <> b.Length Then
MessageBox.Show("Impossible to do")
Exit Sub
End If
Dim hs As New Hashtable
For i As Integer = 0 To a.Length - 1
If b(i).IndexOf("%") <> -1 Then
hs.Add(b(i), a(i))
End If
Next
Dim c As String() = String3.Split(" "c)
For i As Integer = 0 To c.Length - 1
If c(i).IndexOf("%") <> -1 Then
If hs.ContainsKey(c(i)) Then
c(i) = hs.Item(c(i)).tostring
End If
End If
Next
MessageBox.Show(Join(c))
End Sub
End Class
///

Very simple, however when you ask next time, than tell why the answer does
not fits you or what you do not understand instead of completly ignoring
them.

I hope this helps something?

Cor
"exekutive" <exekutive@discussions.microsoft.com>
...[color=blue]
> Wow thats a lot of response. Thank you thank you. I'll have to digest
> this
> for a while. In the meantime, here's what I wrote ... the Mask function
> isn't finished obviously, but the FindDelimiters function successfully
> returns an integer array of the postions of %x tokens within the inStr
>
>
Expand|Select|Wrap|Line Numbers
  1. >    Public Function Mask(ByRef inputStr As String, ByRef maskInStr As
  2. > String, ByRef maskOutStr As String, ByVal delimiter As Char) As String
  3. >
  4. >        Dim outputStr As String
  5. >        Dim delimiterIndexes As Integer()
  6. >
  7. >        delimiterIndexes = FindDelimiters(maskInStr, delimiter)
  8. >
  9. >        Return outputStr
  10. >
  11. >    End Function
  12. >
  13. >    Public Function FindDelimiters(ByRef inStr As String, ByVal d As Char)
  14. > As Integer()
  15. >
  16. >
  17. >        Dim tempIndex As Integer = -2
  18. >        Dim delimiterCounter As Integer
  19. >
  20. >        ' Count the instances of 'd' in 'inStr'
  21. >        Do
  22. >            tempIndex = inStr.IndexOf(d, tempIndex + 2)
  23. >            If tempIndex <> -1 Then delimiterCounter += 1
  24. >        Loop Until tempIndex = -1
  25. >
  26. >        ' Set the bounds of an array to the number of instances of 'd'
  27. >        Dim indexDelimiters(delimiterCounter - 1) As Integer
  28. >
  29. >        ' Fill the array with the positions of 'd' in 'inStr'
  30. >        For tempIndex = 0 To delimiterCounter - 1
  31. >            If tempIndex = 0 Then
  32. >                indexDelimiters(tempIndex) = inStr.IndexOf(d, 0)
  33. >            Else
  34. >                indexDelimiters(tempIndex) = inStr.IndexOf(d,
  35. > indexDelimiters(tempIndex - 1) + 2)
  36. >            End If
  37. >        Next
  38. >
  39. >        Return indexDelimiters
  40. >
  41. >    End Function
  42. >
[/color]


Closed Thread


Similar Visual Basic .NET bytes