Follow TV Tropes

Following

Coding/Programming

Go To

war877 Grr... <3 from Untamed Wilds Since: Dec, 2015 Relationship Status: Having tea with Cthulhu
Xopher001 Since: Jul, 2012
#1377: Apr 27th 2016 at 2:04:26 PM

private static int lcs(){/*reads the characters in b[][] to determine the lcs*/
		
		char lcs [] = new char[y.length];//characters are read from last to first into this array
		int count = 0;//counts the length of the lcs
		
			for(int i = y.length; i > 0;){//nested for loop with length and height of table
				for(int j = x.length; i > 0;){
					if(b[i][j] == '\\'){//if this character is encountered
						lcs[i] = y[i];//read the corresponding matching letters into the array
						i -= 1; //go to the left
						j -= 1;//go up
						count++;//increments count
					}
					else if (b[i][j] == '<'){//if this character is read
						i-=1;//go to the left
					}
					else if(b[i][j] == '^'){//if this character is read
						j -= 1;//go up
					}
					else
						break;	//no more characters to read
				}
			}
			 
			lcsax = new char[lcs.length];//initialize character array
			for(int i = lcs.length; i > 0; i--){
				lcsax[i] = lcs[i];//reads the characters from last to first into array		
			}
			
			return count;//returns lcs length
	}

Now I'm encountering a bug in the nested loops I set up. These are supposed to determine the longest common sub sequence based on the characters \\, <, and ^

war877 Grr... <3 from Untamed Wilds Since: Dec, 2015 Relationship Status: Having tea with Cthulhu
Grr... <3
#1378: Apr 27th 2016 at 10:51:11 PM

What is the bug you are running into? The code as written will print out the longest common extractable subsequence, skipping over characters in the y sequence that do not match.

If you are trying to find the longest common already intact subsequence, there are more than a few bugs.

edited 27th Apr '16 10:52:58 PM by war877

Aetol from France Since: Jan, 2015
#1379: Apr 28th 2016 at 5:05:15 AM

If anyone's interested, I tried my hand at the "longest common sequence" problem and came up with two solutions.

The first function browses the matrix intersection of x and y along its diagonals (and not along its rows or columns as usual), because this is where common sequences will be found.

The second function is slower but easier to understand : it browses the matrix as usual, and from each point tries to find a common sequence by following the diagonal.

edited 28th Apr '16 5:06:52 AM by Aetol

Worldbuilding is fun, writing is a chore
Xopher001 Since: Jul, 2012
#1380: Apr 29th 2016 at 9:51:55 AM

I don't know how to implement that because It's written in python- I have more experience using c++ and java.

I was able to make my code work most of the time, but I keep running into this weird bug where instead of going up and to the left after encountering '\\', the loop instead goes up and to the right several spaces.

private static int lcs(){/*reads the characters in b[][] to determine the lcs*/
		
		char lcs [] = new char[y.length];//characters are read from last to first into this array
		int count = 0;//counts the length of the lcs
		
		
			for(int i = y.length-1; i >= 1;){//nested for loop with length and height of table
				for(int j = x.length-1; i >= 1;){
					if(b[i][j] == '\\'){//if this character is encountered
						System.out.print(b[i][j]);
						System.out.print(c[i][j]);
						lcs[i] = x[i];//read the corresponding matching letters into the array
						System.out.println(lcs[i]);
						i -= 1; //go to the left
						j -= 1;//go up
						count++;//increments count
						if(c[i][j] == 1){
							break;
						}
					}
					else if (b[i][j] == '<'){//if this character is read
						System.out.print(b[i][j]);
						System.out.println(c[i][j]);
						j-=1;//go to the left
						
					}
					else if(b[i][j] == '^'){//if this character is read
						System.out.print(b[i][j]);
						System.out.println(c[i][j]);
						i -= 1;//go up
						
					}
					else
						break;	//no more characters to read
					
					}
			}
			 
			lcsax = new char[lcs.length];//initialize character array
			for(int i = lcs.length-1; i >= 0; i--){
				lcsax[i] = lcs[i];//reads the characters from last to first into array		
			}
			
			return count;//returns lcs length
	}

   .  j  n  d  c  a  a  
. .0 .0 .0 .0 .0 .0 .0 
a .0 ^0 ^0 ^0 ^0 \1 \1 
n .0 ^0 \1 <1 <1 ^1 ^1 
c .0 ^0 ^1 ^1 \2 <2 <2 
d .0 ^0 ^1 \2 ^2 ^2 ^2 
j .0 \1 ^1 ^2 ^2 ^2 ^2 
a .0 ^1 ^1 ^2 ^2 \3 \3 
\3a
^2
^2
<2
\2c
^1 //this should be <1
\1a //wrong letter

The longest common sub sequence length is 3

The longest common sub sequence is aca

edited 29th Apr '16 9:55:37 AM by Xopher001

war877 Grr... <3 from Untamed Wilds Since: Dec, 2015 Relationship Status: Having tea with Cthulhu
Grr... <3
#1381: Apr 29th 2016 at 10:13:55 AM

I am curious. What do you get when you type in:

System.out.println(new String(x));
in the above example. Assuming that x is of type char[].

edited 29th Apr '16 10:16:03 AM by war877

Xopher001 Since: Jul, 2012
#1382: Apr 29th 2016 at 10:22:45 AM

The sequence of letters in the left-most column

Aetol from France Since: Jan, 2015
#1383: Apr 29th 2016 at 10:28:58 AM

Converting from python to java isn't really hard. Most of the syntax is identical to other languages : you just need to put semicolons and curly brackets where needed, and add a type to variable declarations. You only need to be a bit creative for the argument of for loops, since in python they have the form for iterator in iterable (and even then the iterable is often xrange(), which gives a list of consecutive integers aka the standard use of for loops).

EDIT : anyway, here you go.

Regarding your code : first, you have an error in your second-level loop. It should be "j >= 1", nor "i". Second, if this doesn't fix the problem (I doubt it will), I'm thinking it may be caused by the non-standard use of for loops. How about this :

int i = y.length-1  
int j = x.length-1
while(i >= 1 && j >= 1) {
/* code here */
}

Finally, I'm not sure I understand what you want to do : are the sequences you're looking for supposed to be continuous? In other words, should the solution in this case have for length 1 or 3?

edited 29th Apr '16 11:38:29 AM by Aetol

Worldbuilding is fun, writing is a chore
war877 Grr... <3 from Untamed Wilds Since: Dec, 2015 Relationship Status: Having tea with Cthulhu
Grr... <3
#1384: Apr 29th 2016 at 10:29:16 AM

There is several errors. I cannot explain the behaviour you are getting, because none of them would cause this...... Oh crap. I see. Kill the two for loops and replace them with a while loop.

int i=x.length-1;int j=y.length-1; 
while(true)
You should have an easier time finding the remaining bugs after that.

-[snip]- [nja]s

edited 23rd May '16 2:10:50 PM by war877

TooManyIdeas Into Oblivion from Twilight Town Since: Oct, 2013 Relationship Status: Abstaining
Into Oblivion
#1385: May 23rd 2016 at 11:45:54 AM

If anyone knows the version of JavaScript that Khan Academy uses for its programming course, I'm trying to make a simple maze (well, path) game. I need the program to know when the ball is out of bounds, but the turns in the path make that difficult for a beginner. I know how to use If/Else, the question is how to apply it in this particular program. If I posted a link to the program here, might someone be able to help me out?

please call me "XionKuriyama" or some variation, thanks! | What is the good deed that you can do right now?
war877 Grr... <3 from Untamed Wilds Since: Dec, 2015 Relationship Status: Having tea with Cthulhu
Grr... <3
#1386: May 23rd 2016 at 2:14:16 PM

I do not know if kahn academy uses a non-standard version of JS or not. However, I have used JS before, and the basics of C derived languages are surprisingly similar to each other.

TooManyIdeas Into Oblivion from Twilight Town Since: Oct, 2013 Relationship Status: Abstaining
Into Oblivion
#1387: May 23rd 2016 at 2:20:45 PM

All right, cool. Here it is. Code on the left, output on the right. Mouse controls are intentionally inverted.

edited 23rd May '16 2:21:18 PM by TooManyIdeas

please call me "XionKuriyama" or some variation, thanks! | What is the good deed that you can do right now?
war877 Grr... <3 from Untamed Wilds Since: Dec, 2015 Relationship Status: Having tea with Cthulhu
Grr... <3
#1388: May 23rd 2016 at 2:34:09 PM

I see. I would store the edges in an array. Creating a massive ifelse tree will simply not work if you need to replace the maze with a new one.

TooManyIdeas Into Oblivion from Twilight Town Since: Oct, 2013 Relationship Status: Abstaining
Into Oblivion
#1389: May 23rd 2016 at 2:41:18 PM

Yeah, good point. I'll need to do the Array lessons again, then. Efficiency is hard to program. tongue

Okay, weird thing I notice: If I program in normal mouse controls, it breaks the messages. Makes debugging frustrating.

edited 23rd May '16 2:47:28 PM by TooManyIdeas

please call me "XionKuriyama" or some variation, thanks! | What is the good deed that you can do right now?
war877 Grr... <3 from Untamed Wilds Since: Dec, 2015 Relationship Status: Having tea with Cthulhu
Grr... <3
#1390: May 23rd 2016 at 2:48:35 PM

A separate issue: I assume you want the entire outside to be marked with out of bounds? Or do you only want to mark the touching of lines? The other way to do it would be to mark the crossing of lines.

[up]If you uninvert the controls, you need to replace mouseX== with mouseY==

edited 23rd May '16 2:51:46 PM by war877

TooManyIdeas Into Oblivion from Twilight Town Since: Oct, 2013 Relationship Status: Abstaining
Into Oblivion
#1391: May 23rd 2016 at 3:01:17 PM

[up]Yeah, the entire outside is ideal, but I don't know how to do that.

edited 23rd May '16 3:01:35 PM by TooManyIdeas

please call me "XionKuriyama" or some variation, thanks! | What is the good deed that you can do right now?
war877 Grr... <3 from Untamed Wilds Since: Dec, 2015 Relationship Status: Having tea with Cthulhu
Grr... <3
#1392: May 23rd 2016 at 3:16:40 PM

Will your mazes be limited to one left turn and one right turn? If so, you can simply replace your equalities with inequalities.

Another solution which will always work is to create an array the size of the the playing field and then paint it. Either with the flood-fill method, which will work for a maze with no holes in it, or a scan and flip method if the maze only contains horizontal and vertical lines and no overdraw and no gaps.

A third solution is to count the number of lines to the left up if the maze contains no overdraw or gaps and only vertical and horizontal lines. If a line is drawn across the entrance and exit, I believe the inside will always have an even count. Oh. It will always be even. Both counts will be odd on the inside.

edited 23rd May '16 3:23:19 PM by war877

Meklar from Milky Way Since: Dec, 2012 Relationship Status: RelationshipOutOfBoundsException: 1
#1393: May 29th 2016 at 1:22:44 AM

Okay, so here's why you shouldn't try to write Javascript after midnight...

I was working on my latest PCG program, and after testing a certain feature I noticed I was getting statistically bizarre results unlike anything the program had produced before. It all seemed to come down to a particular hashing algorithm. I had thought my hashing algorithm was reasonably good, but when I substituted a call to Math.random(), the bad results went away. I tested my hashing algorithm with some other outputs and those ones looked good too, very similar to what Math.random() output in the same tests. Finally, just to make sure it was in fact my algorithm that was behaving incorrectly, I wrote another completely different hashing algorithm from scratch and substituted that- and the bad results were still there. Either both algorithms somehow shared a similar weakness, or there was something else I was overlooking that was poisoning the seed values.

Finally, I opened up Firebug and dug through the DOM to see what the actual values were that the algorithm was returning- and found that, for every given run of the program, all the hash values related to the bad outputs were exactly the same number, even though hashes with very similar seeds were returning all sorts of different numbers. I finally went back and checked the original function call- and found that, when I had first copy+pasted it from elsewhere before changing the arguments, I had accidentally left in one extra argument, causing the seed I had wanted to use to be completely ignored. (Even though the function was only defined with 3 arguments, apparently Javascript doesn't care how many arguments you actually pass in when you call it, and doesn't report any errors if you add superfluous ones. This same mistake would have shown up instantly in a language like C++ where the compiler doesn't accept such nonsense.)

As usual, the bug was way simpler than I thought it was. Now someone get me an anvil so I can hit my head on it for being such a moron.

Join my forum game!
Medinoc from France (Before Recorded History)
#1394: May 29th 2016 at 1:28:18 AM

What exactly were you using hashes for? I haven't used hashes outside hash tables (where a bad hash function would be merely a performance problem) and message integrity checks, so I'm genuinely curious what their other uses are.

edited 29th May '16 1:49:18 AM by Medinoc

"And as long as a sack of shit is not a good thing to be, chivalry will never die."
Aetol from France Since: Jan, 2015
#1395: May 29th 2016 at 6:38:35 AM

~Too Many Ideas: one fundamental rule of programming is to ALWAYS separate logic from data. In your case :

  • Data is how the mouse controls the ball and where the walls are.
  • Logic is everything else: mostly the collision detection and the display.

To keep the mouse control function separate, you need to use two variables x and y that will represent the position of the ball. First you initialize x and y with mouseX and mouseY, and then you use them in the rest of the code.

To keep the position of the walls separate, you need to do as Tropers/war877 said: put them into an array. Then you can just use for loops to draw them and to check for collision.

Checking for collision is the tricky part. Right now, the collision will only be detected if the ball is exactly on the wall: if you move the mouse quickly enough, you can cross a wall without triggering the "you hit the wall" message. My solution would be to write a function that verifies whether two segments intersect (this isn't easy: there'll be some math involved), then compare each wall with the segment formed by the current and the last position of the ball (you'll need two more variable for that).

Here's what the main loop would look like (in pseudocode) :

store x,y in lastX,lastY 
set x,y in terms of mouseX,mouseY
// display
set the background
display the ball at the position x, y
FOR each wall :
display the wall
// collision detection
FOR each wall :
check whether the wall intersects the segment (x,y,lastX,lastY)
IF there was any collision :
display the "you lost" message

Worldbuilding is fun, writing is a chore
Meklar from Milky Way Since: Dec, 2012 Relationship Status: RelationshipOutOfBoundsException: 1
#1396: May 29th 2016 at 12:56:11 PM

What exactly were you using hashes for?
Procedural content generation. I'm preparing for a contest on procedural textures.

In this specific instance, the hash value was being used as the magnitude of a patch of color associated with a Voronoi point.

Join my forum game!
Medinoc from France (Before Recorded History)
#1397: May 29th 2016 at 9:43:41 PM

In short, you're using the hashes as sources of pseudo-randomness?

"And as long as a sack of shit is not a good thing to be, chivalry will never die."
Meklar from Milky Way Since: Dec, 2012 Relationship Status: RelationshipOutOfBoundsException: 1
Tangent128 from Virginia Since: Jan, 2001 Relationship Status: Gonna take a lot to drag me away from you
#1399: May 31st 2016 at 11:45:42 AM

Which hash were you using? FNV?

Do you highlight everything looking for secret messages?
Xopher001 Since: Jul, 2012
#1400: Sep 18th 2016 at 4:19:47 PM

So this semester we're programming shell scripts in C, and I'm having trouble with this one simple program- it's supposed to take an inputted integer between 5 and 9 and print that number of rows in a pyramid- for example, if I entered , then the output would be

___1

___22

__333

_4444

_55555

666666

this is what my code looks like:

 
#!/bin/sh
#task6c.sh

read Input

for ((i=1; $i <= $Input; i++)); do
       sp=`expr $Input - $i` #this is a variable used to help create the right number of spaces
      if [ `expr $sp % mod 2` -eq 0 ]; then 
           aces=`expr $sp/2`
     else
         aces=`expr ($sp + 1)/2` #this is to prevent the program from trying to print out 1 1/2 or 7 1/2 spaces
     fi
     for ((h=1; $h <= $aces; h++)); do #This is where I am getting the syntax error- it says "unexpected ;"
          echo -n ""; #actual printing of the spaces
     done
     for((j=1; $j <= $Input; j++)); do
          echo -n "$i"; #prints the numbers
     done
done
exit 0;

edited 18th Sep '16 7:53:35 PM by Xopher001


Total posts: 1,462
Top