Challenging text masking problem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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!
 
exekutive said:
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...

Please, only post solutions that WORK. Good luck!

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
 
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.
 
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
 
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.
 
exekutive said:
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.

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.

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.

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
 
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
 
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

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

Dim outputStr As String
Dim delimiterIndexes As Integer()

delimiterIndexes = FindDelimiters(maskInStr, delimiter)

Return outputStr

End Function

Public Function FindDelimiters(ByRef inStr As String, ByVal d As Char)
As Integer()


Dim tempIndex As Integer = -2
Dim delimiterCounter As Integer

' Count the instances of 'd' in 'inStr'
Do
tempIndex = inStr.IndexOf(d, tempIndex + 2)
If tempIndex <> -1 Then delimiterCounter += 1
Loop Until tempIndex = -1

' Set the bounds of an array to the number of instances of 'd'
Dim indexDelimiters(delimiterCounter - 1) As Integer

' Fill the array with the positions of 'd' in 'inStr'
For tempIndex = 0 To delimiterCounter - 1
If tempIndex = 0 Then
indexDelimiters(tempIndex) = inStr.IndexOf(d, 0)
Else
indexDelimiters(tempIndex) = inStr.IndexOf(d,
indexDelimiters(tempIndex - 1) + 2)
End If
Next

Return indexDelimiters

End Function
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top