HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

pseduocode -> jass : help!

04-08-2004, 09:41 PM#1
PEON1577
Code:
string array totalresults
string array oldresults
string array newresults

function dostuff takes string input, integer O returns nothing
  oldresults[0] = input
  loop for i = 0 to i = O
    loop over oldresults (with result)
      loop for j = 0 to length(result)
        // add x
        push(newresults, substring(result, 0 j) + "x" + substring(result, j)
        // replace one letter with x
        push(newresults, substring(result, 0, j) + "x" + substring(result, j+1)
      endloop
    endloop
    set oldresults = newresults
    push(totalresults, newresults)
    clear(newresults)
  endloop
endfunction

AIAndy assured me that this code does what I've been trying to get done in jass so i've taken some time to attempt to convert it to JASS but i've gotten stuck, heres my wip on translating it:
Code:
function dostuff takes string input, integer O returns nothing //so glad it returns nothing, this pseudocode is the best!
local string array totalresults
local string array oldresults
local string array newresults

  oldresults[0] = input

  loop for i = 0 to i = O //-- for loop -> jass anyone?
    loop over oldresults (with result)//-- loop over -> jass?
      loop for j = 0 to length(result)//-- for loop again
        // add x
        push(newresults, substring(result, 0 j) + "x" + substring(result, j) //push adds to the end of the array
        // replace one letter with x //why?!
        push(newresults, substring(result, 0, j) + "x" + substring(result, j+1) //push adds to the end of the array
      endloop
    endloop
    set oldresults = newresults //ok..
    push(totalresults, newresults) //...
    clear(newresults)//................
  endloop
//return value here?
endfunction

What the function should do:
given string s
given integer o

compute all possible values of string s based on o

o is the offset

here are some examples of what i mean by offset, so you understand:
if o were 0 then the only value would be the initial string
if o were 1 and s were test then:
test
xest
txst
text
tesx
txest
texst
tesxt

[x means one character slot in the above example]
if o were 2 and s were test then:
test
xest
txst
text
tesx
txest
texst
tesxt
[x means two and one character slots, ie xest means both AAest and Aest]
and so on, i hope you understand what i mean.

if anyone can help me figure out the algorithm for generating the possible values that would be great

- notes -
string s has a max size of 127 (<=127)
integer o has a max size of 127 (<=127)
-

if you want to add pseudocode then please do so only if its in jass :)
if you cant help then dont post.

^_^
04-08-2004, 10:13 PM#2
Vidstige
This would be a nice for-loop that counts from 1 to 10.
Code:
local integer i
set i = 1
loop
    exitwhen i > 10

    // Whatever you want to do in the for-loop

    set i = i + 1
endloop

If you are calling a function you have to do call someFunction() and not just some someFunction().

Edit: Are you telling it to use a 0 (zero) as an input argument instead of an o?
04-08-2004, 10:49 PM#3
PEON1577
Quote:
Originally Posted by Vidstige
This would be a nice for-loop that counts from 1 to 10.
Code:
local integer i
set i = 1
loop
    exitwhen i > 10

    // Whatever you want to do in the for-loop

    set i = i + 1
endloop

If you are calling a function you have to do call someFunction() and not just some someFunction().

Edit: Are you telling it to use a 0 (zero) as an input argument instead of an o?

I know, its not my pseudocode -- and i know it doesent do what i want. AIAndy closed my 1st thread though..

I used the letter O for the integers name because it stood for offset, you can rename them to what ever you like so long as there is:
a string - what you are derriving the possible values from
an integer - this determins the offset

origionally i had it like:
local string word
local integer offset
local string array wordcombos

i just need help on the algorithm -- what that guy used in pseudocode is not stuff that you can do in jass, like that anyway -- if you know how to generate the values based on the offset int please let me know, i've been trying to do this for a while and just manage to get flamed to death ^_^
04-09-2004, 01:16 PM#4
jmoritz
PEON, let my try to explain the situation to you one last time, because I'm starting to lose my patience :(

I'm trying to help you. Nobody besides me wanted to help you anymore. Not because they hate you, but because they either cannot solve your problem, or they don't have time for it, or just don't feel like it. Remember how you had to bump your thread 5 times before someone responded again?

Also, I'm not trying to flame you. I haven't called you names (unlike you calling me an idiot in the other thread), and I never will.

Now, about the pseudo-code. It seems to me you don't quite understand what pseudo-code is. It is not written in any specific language. It's just words programmers use to describe an algorithm. That means JASS psuedo-code doesn't exist. It's either JASS, or pseudo-code. Not both.

It indeed looks like my code doesn't do exactly what you want. I thought you also wanted "txsx" (O=2) for example. Here's an algorithm that should work. Insertions can be done at length(input)+1 places, replacements only at length(input) different positions. The algorithm loops over every character in string and does insertions before the current character. This means it will never insert characters after the end of the string, but possibly before the beginning. We use an if to prevent this.

Code:
function dostuff takes string input, integer O returns string array
  string array results
  results[0] = input

  // loop over every character in string (zero-based)
  loop for i = 0 to length(result)-1
    // loop for number of x's to add
    loop for j = 1 to j = O
      // loop for number or replacements (= j - nradditions)
      // don't do more replacements than characters left in string --> min
      loop for k = 0 to k = min(j, length(result)-i)
        // dont do only additions at beginning of string
        if (i > 0 or k > 0)
          push(results, substring(result, 0, i) + (j*"x") + substring(result, i+k)
        endif
      endloop
    endloop
  endloop

  return results
endfunction

Loop should run including the upper bound. Substring is zero-based and if the third parameter is ommitted, returns the rest of the string.
This will return the following strings for O=2 (not including possible duplicates):
test
xest
txest
txst
texst
text
tesxt
tesx
xxest
xxst
txxest
txxst
txxt
texxst
texxt
texx
tesxxt
tesxx

This is not exactly like you said in your last post, but in previous posts you kind of confirmed to me you want more than 1 replacement. If you only want to replace 1 or less characters, modify the algorithm like this: loop for k = 0 to k = 1.

I know this is not JASS, but figuring out this algorithm took me about an hour, and that's as much time I wish to spend on this. Writing this in JASS and testing it will likely take the rest of my day. Sorry.
04-09-2004, 10:07 PM#5
PEON1577
Quote:
Originally Posted by jmoritz
Now, about the pseudo-code. It seems to me you don't quite understand what pseudo-code is. It is not written in any specific language. It's just words programmers use to describe an algorithm.
correct, and jass pseudo-code is words used only in jass to describe an algorithm, ie no using words not found in jass.

Quote:
It indeed looks like my code doesn't do exactly what you want. I thought you also wanted "txsx" (O=2) for example.
i do, my biggest problem is trying to tell people what i want -- no one seems to get it :(

Quote:
Here's an algorithm that should work.
ok, i hope its in jass... :)
Quote:
Code:
function dostuff takes string input, integer O returns string array
  string array results
  results[0] = input

  // loop over every character in string (zero-based)
  loop for i = 0 to length(result)-1
    // loop for number of x's to add
    loop for j = 1 to j = O
      // loop for number or replacements (= j - nradditions)
      // don't do more replacements than characters left in string --> min
      loop for k = 0 to k = min(j, length(result)-i)
        // dont do only additions at beginning of string
        if (i > 0 or k > 0)
          push(results, substring(result, 0, i) + (j*"x") + substring(result, i+k)
        endif
      endloop
    endloop
  endloop

  return results
endfunction

Loop should run including the upper bound. Substring is zero-based and if the third parameter is ommitted, returns the rest of the string.
This will return the following strings for O=2 (not including possible duplicates):
test
xest
txest
txst
texst
text
tesxt
tesx
xxest
xxst
txxest
txxst
txxt
texxst
texxt
texx
tesxxt
tesxx

This is not exactly like you said in your last post, but in previous posts you kind of confirmed to me you want more than 1 replacement. If you only want to replace 1 or less characters, modify the algorithm like this: loop for k = 0 to k = 1.

I know this is not JASS, but figuring out this algorithm took me about an hour, and that's as much time I wish to spend on this. Writing this in JASS and testing it will likely take the rest of my day. Sorry.
its ok, ive looked at it and it nearly does what i want - the algorithm gets more and more complex as o increases, i think it would have to be based on length of word and size of o.

i'm loosing intrest in it as well because it wont do anything at higher levels other then return 90+% of everything as true.

anyway it was a fun expiriment :) - once i get into higher level languages more i'll see how they do chat filters.

could you tell me how to do push in jass though? i want to know!
04-10-2004, 03:01 PM#6
jmoritz
You will have to keep track of the size of the array in an integer. Everytime you add something to the array increase that integer like this:
Code:
integer size = 0
string array example

function push takes string word returns nothing
  set example[size] = word
  set size = size + 1
endfunction
04-10-2004, 09:03 PM#7
PEON1577
Quote:
Originally Posted by jmoritz
You will have to keep track of the size of the array in an integer. Everytime you add something to the array increase that integer like this:
Code:
integer size = 0
string array example

function push takes string word returns nothing
  set example[size] = word
  set size = size + 1
endfunction

right, and then when i return the array i'd set the 0 slot to the value of the integer.