I got a different resolt than i was supposed to

  • Thread starter Thread starter transgalactic
  • Start date Start date
AI Thread Summary
The discussion revolves around a coding issue related to generating subsets from a string using recursion. The user expected to receive specific subsets of length 2 from the string "abcd" but instead got unexpected outputs. The provided code recursively constructs subsets, but the logic does not align with the intended goal of generating all possible subsets of a specified length. The responses clarify that the output generated by the user's code includes combinations that do not match the desired subsets. It is suggested that the user should treat the string as an array of characters and consider using an IDE like NetBeans for better coding support and access to string methods, which could aid in correcting the logic for generating the intended subsets.
transgalactic
Messages
1,386
Reaction score
0
i tried to follow this recurstion
but instead of getting the resolt i expected i got
some thing really different

http://img185.imageshack.us/my.php?image=img8290nf7.jpg

Code:
// print all subsets of 1...n 
Public static void subsets(int n, String andAlso)
{
      if (n==0)
	  { 
          System.out.println(andAlso);
      }
      else
	 {
          subsets(n-1,andAlso + n);
          subsets(n-1,andAlso);
      }
}

i entered (2,abcd)
i was supposed to get


cd
bd
bc
ad
ac
ab

istead i got some other answer using this diagram
can you tell me where i got ot wrong??

[ February 14, 2008: Message edited by: donaldth smithts ]
 
Technology news on Phys.org
How is it supposed to output that?
cd
bd
bc
ad
ac
ab
Your diagram itself demonstrates why
abcd21
abcd2
abcd1
abcd
is the output ... We don't understand what its purpose is.
 
Your program is entertainingly mind boggling XD if it helps i think this is the run through of what it actually does:

0: subsets(2, "abcd") Pass in n = 2 andAlso = "abcd"
1.1: subsets(1, "abcd2") 1st line of else statement n-1 = 1 andAlso + n = "abcd2"
1.1.1: subsets(0, "abcd21") 1st line of else statement n-1 = 0 andAlso + n = "abcd21"
1.1.1.0: output = "abcd21" If statement so output
1.1.2: subsets(0, "abcd2") 2nd line of else statement n-1 = 0 andAlso = "abcd2"
1.1.2.0: output = "abcd2" If statement so output
1.2: subsets(1, "abcd") 2nd line of else statement n-1 = 1 andAlso = "abcd"
1.2.1: subsets(0, "abcd1") 1st line of else statement n-1 = 0 andAlso + n = "abcd1"
1.2.1.0: output = "abcd1" If statement so output
1.2.2: subsets(0, "abcd") 2nd line of else statement n-1 = 0 andAlso = "abcd"
1.2.2.0: output = "abcd" If statement so output

The main problem seems to be is what you want to do is to somehow make all possible subsets of the length you pass in (in your test case 2) of the string you pass in (in your test case "abcd") but your program logic doesn't seem right to do this, if it helps think of a string as an array of characters, it is possible to refer to each character individually. I would highly recommend netbeans to help, if you type in "String." for example it will list all methods associated with strings (save you remembering chunks of the API).
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top