|
-
December 11th, 2011, 03:08 AM
#1
Java Challenge
Am to write a program that takes a string of digits, S and then caluclates the next term S1 of the sequence.
For Example, if S is "10444221" then S is described from left to right as
"one 1, one 0, three 4's, two 2's, one 1"
This string is converted into digits from left to right resulting in an S1 value of "1110342211".
Anybody have idea on how the program will be in java..Thank you
-
December 12th, 2011, 09:25 AM
#2
Re: Java Challenge
Anybody have idea on how the program will be in java.
Yes
My pleasure.
-
December 12th, 2011, 05:41 PM
#3
Re: Java Challenge
[QUOTE]Yes
do u hava idea of the code.......
-
December 13th, 2011, 11:53 AM
#4
Re: Java Challenge
Yes, but no one here is going to do your homework for you. Show us what you have done so far, explain where you are stuck, ask a question and we will try to answer it.
-
December 15th, 2011, 06:13 AM
#5
Re: Java Challenge
String str = "1122313";
char arr[] = str.toCharArray(); // convert the String object to array of char
System.out.println("INput "+str);
// iterate over the array using the for-each loop.
int i=0;
int [] b= new int [str.length()];
for (i=0;i<str.length();i++){
System.out.println("array :"+arr[i]);
//if ( arr[i]== arr[i++] && arr[i++]!=0)
if(arr[i]=='0')
{
b[i] = +1;
// a1++;
}
if(arr[i]=='1' &&arr[i++]==arr[i])
{
b[i]=+2;
}
else
b[i]=+1;
if(arr[i]=='2' &&arr[i++]==arr[i] )
{
b[i]=+2;
}
else b[i]=+1;
This is where am Stuck..My code will only compare the first two character and add an increment to the b[] array counter if the two characters are equal are 2 and if false add 1...Anybody have idea on how to compare all the characters and also display the input with the counter together...Thank you
-
December 15th, 2011, 08:28 AM
#6
Re: Java Challenge
Aaha..This is difficult task.I need to do some homework to crack down this code.Kitchen cabinets
-
December 15th, 2011, 08:37 AM
#7
-
December 15th, 2011, 11:11 AM
#8
Re: Java Challenge
Your code can't possibly work. If you look at the question you will see the intermediate result and final string are not the same length as the starting string so why have you declared the array b to be the same length as str?
Do you have to store and output the intermediate result or do you just need to output the final string?
Before you go any further you need to get a pen and paper and run through the example yourself writing down how you solve each step. When you have a series of written instructions that work for any input you have the strategy you need to convert to code.
-
December 21st, 2011, 02:16 PM
#9
Re: Java Challenge
Hey guys here is the solution:
package com.test.jguru;
import java.util.Hashtable;
import java.util.Map;
public class ChallengeNumber {
/**
* @param args
*/
public static void main(String[] args) {
Map<Integer,String> mapNum = new Hashtable<Integer, String>();
mapNum.put(1, "one");
mapNum.put(2, "two");
mapNum.put(3, "three");
mapNum.put(4, "four");
mapNum.put(5, "five");
mapNum.put(6, "six");
mapNum.put(7, "seven");
mapNum.put(8, "eight");
mapNum.put(9, "nine");
String strNum = "10444221";
System.out.println("String Length "+strNum.length());
int count = 0;
for(int i = 0;i<strNum.length();i++){
if(!(i+1 == strNum.length())){
if(strNum.charAt(i) == strNum.charAt(i+1)){
count++;
}else{
System.out.println(mapNum.get(count+1)+" "+strNum.charAt(i)+(mapNum.get(count+1)!="one"? "s":""));
count = 0;
}
} else{
System.out.println(mapNum.get(count+1)+" "+strNum.charAt(i)+(mapNum.get(count+1)!="one"? "s":""));
}
}
}
}
-
December 21st, 2011, 02:21 PM
#10
Re: Java Challenge
 Originally Posted by [email protected]
Hey guys here is the solution:
package com.test.jguru;
import java.util.Hashtable;
import java.util.Map;
public class ChallengeNumber {
/**
* @param args
*/
public static void main(String[] args) {
Map<Integer,String> mapNum = new Hashtable<Integer, String>();
mapNum.put(1, "one");
mapNum.put(2, "two");
mapNum.put(3, "three");
mapNum.put(4, "four");
mapNum.put(5, "five");
mapNum.put(6, "six");
mapNum.put(7, "seven");
mapNum.put(8, "eight");
mapNum.put(9, "nine");
String strNum = "10444221";
System.out.println("String Length "+strNum.length());
int count = 0;
for(int i = 0;i<strNum.length();i++){
if(!(i+1 == strNum.length())){
if(strNum.charAt(i) == strNum.charAt(i+1)){
count++;
}else{
System.out.println(mapNum.get(count+1)+" "+strNum.charAt(i)+(mapNum.get(count+1)!="one"? "s":""));
count = 0;
}
} else{
System.out.println(mapNum.get(count+1)+" "+strNum.charAt(i)+(mapNum.get(count+1)!="one"? "s":""));
}
}
}
}
This code will work for all the combinations
-
December 26th, 2011, 10:38 AM
#11
Re: Java Challenge
wow looks cool...i try using map and hashtable but i always get stuck wen comparing d charaters...thanks guy
-
December 27th, 2011, 07:32 AM
#12
-
December 28th, 2011, 02:12 AM
#13
Re: Java Challenge
der is a runtime error....d code does not consider the last character of d string....if input string is 4423.....the output is two 4s, one 2..and the ends...instead of two 4s, one 2, one 3....anyone have idea on how to include d last character...in d counting
-
December 28th, 2011, 12:57 PM
#14
Re: Java Challenge
@[email protected] Please don't post the answer to peoples homework, it benefits no-one.
-
December 29th, 2011, 11:34 AM
#15
Re: Java Challenge
@da_jokeer it worked fine for me..can u please recheck and let me know
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|