| View previous topic :: View next topic |
| Author |
Message |
Daniel. I post too much
Reputation: 72
Joined: 08 Nov 2007 Posts: 2938
|
Posted: Wed Oct 30, 2013 1:39 pm Post subject: java help |
|
|
Write your code in the file Compress.java. Write your test cases in assign4-testcases.txt.
Run-length encoding (RLE) is a simple "compression algorithm" (an algorithm which takes a block of data and reduces its size, producing a block that contains the same information in less space). It works by replacing repetitive sequences of identical data items with short "tokens" that represent entire sequences. Applying RLE to a string involves finding sequences in the string where the same character repeats. Each such sequence should be replaced by a "token" consisting of:
1. the number of characters in the sequence
2. the repeating character
| Code: |
public class Compress {
public static void main(String[] args) {
// TODO Auto-generated method stub
String letters = IO.readString();
//IO.outputIntAnswer(letters.length());
int i = 0;
for ( int count = 0; count < letters.length(); count++) {
while (i+1 < letters.length() && letters.charAt(i) == letters.charAt(i + 1))
{
i++;
count++;
}
System.out.print(count+1 + "" + letters.charAt(count));
}
}
}
|
input: qqww
expected output: 2q2w
actual output: 2q4w
can anyone help me with my problem?
this is an example of what it's suppose to do
For example, consider the following string:
qwwwwwwwwweeeeerrtyyyyyqqqqwEErTTT
After applying the RLE algorithm, this string is converted into:
q9w5e2rt5y4qw2Er3T
_________________
|
|
| Back to top |
|
 |
Dark Byte Site Admin
Reputation: 471
Joined: 09 May 2003 Posts: 25823 Location: The netherlands
|
Posted: Wed Oct 30, 2013 4:53 pm Post subject: |
|
|
I always suck at the order of mathematical expressions so I ALWAYS use more braces than actually needed. (Makes it easier to read)
I think this might be one of those cases where that can help
| Code: |
(i+1 < letters.length() && letters.charAt(i) == letters.charAt(i + 1))
|
If I remember correctly && goes before almost everything, so this would turn into:
| Code: |
(i+1 < (letters.length() && letters.charAt(i)) == letters.charAt(i + 1))
|
If that's not what you wanted (I doubt it) try this:
| Code: |
while ((i+1 < letters.length()) && (letters.charAt(i) == letters.charAt(i + 1)))
|
Also,
You need to reset Count to 0 after
| Code: |
System.out.print(count+1 + "" + letters.charAt(count));
|
_________________
Do not ask me about online cheats. I don't know any and wont help finding them.
Like my help? Join me on Patreon so i can keep helping |
|
| Back to top |
|
 |
justa_dude Grandmaster Cheater
Reputation: 23
Joined: 29 Jun 2010 Posts: 893
|
Posted: Wed Oct 30, 2013 7:38 pm Post subject: |
|
|
| Dark Byte wrote: | | You need to reset Count to 0 |
Hehe. He can't, 'cause he's using it to iterate over the loop. I think the operator precedence is OK, he's just got some logic issues going on (using "count" when he means to be using i).
My best suggestion to the OP is to either get a pen and paper and manually trace the function for some iterations or start writing code in a proper IDE and make use of the debugging function. Being able to step through your code one line at a time and watch what the variables hold as the code executes might save you a lot of time and headaches while learning.
|
|
| Back to top |
|
 |
DELETED_USER Newbie cheater
Reputation: 1
Joined: 02 Oct 2013 Posts: 24 Location: Venezuela
|
Posted: Wed Oct 30, 2013 11:29 pm Post subject: |
|
|
I don't know the Java's syntax, however, I made it in C:
| Code: | #include <stdio.h>
#include <string.h>
int main(void)
{
unsigned int i = 0, j = 0, k = 0;
char letters[100];
printf("Please write the string: ");
fgets(letters, 100, stdin);
for (i = 0; i < strlen(letters)-1; i++)
{
for (j = 0; j < strlen(letters)-1; j++)
{
if (letters[i] == letters[i+j])
k++;
else
break;
}
if (k > 1)
{
printf("%d%c", k, letters[i]);
i = i + k - 1;
}
else
putchar(letters[i]);
k = 0;
}
getchar();
return 0;
} |
Input:
| Quote: | | qwwwwwwwwweeeeerrtyyyyyqqqqwEErTTT |
Output:
| Quote: | | q9w5e2rt5y4qw2Er3T |
Well... I hope you just don't convert this code into the Java's syntax without knowing that you're really doing.
I hope you read the code, and understand how it works, then write your own version.
Regards.
|
|
| Back to top |
|
 |
Daniel. I post too much
Reputation: 72
Joined: 08 Nov 2007 Posts: 2938
|
Posted: Thu Oct 31, 2013 11:05 am Post subject: |
|
|
My main problem is I don't know how to reset counter, it's what I've been trying to do
_________________
|
|
| Back to top |
|
 |
DELETED_USER Newbie cheater
Reputation: 1
Joined: 02 Oct 2013 Posts: 24 Location: Venezuela
|
Posted: Thu Oct 31, 2013 6:48 pm Post subject: |
|
|
| Daniel. wrote: | | My main problem is I don't know how to reset counter, it's what I've been trying to do |
Are you serious?
Look at my snippet, you can see it right there.
|
|
| Back to top |
|
 |
|