4chan archive /g/ (index)
2013-01-26 02:45 31007317 Anonymous (indian know that feel comp.png 922x882 108kB)
I need help with my recursion homework from you friday night /g/ents. Can you write a method in Java that, given an integer, prints a triangle using no loops/print statements. The method must call itself. public static String triangle(int size) { //3 would return the string: "*\n**\n***\n" //no loops, no print statements, only recursion }

1 min later 31007345 Anonymous
>prints a triangle >no print statements I meant returns a string that represents a triangle with base of given size.

3 min later 31007391 Anonymous
public static String triangle(int size) { do this shit size times: however_java_prints_things("*") triangle_some_more( less_than_size); } everything seems to be in order here

4 min later 31007400 Anonymous
>>31007391 >do this shit size times: Seems pretty fucking loop in here...

7 min later 31007469 Anonymous
probably loops inside the library method, but return triangle(size - 1) + StringUtils.repeat("*", size) + "\n"

8 min later 31007497 Anonymous
>>31007469 I'm not sure the intent of the assignment was for us to use library methods, i'll clarify with the professor. Thanks for the help.

17 min later 31007668 Anonymous
bump for great recursion

22 min later 31007755 Anonymous
static void triangle(int size) { if (size > 0) { triangle(size - 1); triangle(-size); } else if (size < 0) { putchar('*'); triangle(size + 1); } else { putchar('\n'); } } That is how the recursive structure should look like, modifying it to build a string and return it I leave up to you.

35 min later 31008006 Anonymous
>>31007755 Prints the triangle with a newline character before it begins. Incorrect. Also uses putchar.

37 min later 31008069 Anonymous
>>31008006 As I said, you need to modify it to build a string instead. The leading newline can be checked for when your appending the string - if the string is empty, don't do the newline.

41 min later 31008143 Anonymous
>>31008069 Changing putchar to return makes triangle(size + 1) in the else-if block unreachable. It's easy to write with print statements and void return type, not so easy with no prints and String return type.

42 min later 31008162 Anonymous
>iit /g/ fails basic coding classes

45 min later 31008216 Anonymous
>>31008143 >Changing putchar to return makes triangle(size + 1) in the else-if block unreachable. No, why would it? You would change it to something like: str += "*" + triangle(size+1)

50 min later 31008324 Anonymous
void print_triangle(int size) { static int isize = size; if(size > isize) isize = size; if(size > 0) { printf("*"); print_triangle(size-1); } if(size == 0) { printf("\r\n"); isize--; if(isize) print_triangle(isize); else return; } isize = 0; } yes, I know static vars aren't the best idea, but it gets the job done.

51 min later 31008349 Anonymous
>>31008216 So all of the recursive calls inside the function are basically appends to a String in the original method call? What's with the reversing of the parameter in triangle(-size)? That's pretty interesting. >>31008324 No printing. String return type.

51 min later 31008352 Anonymous
public static String trig(int size){ string ret = triangle(size - 1) + makeln(size); return triangle(size - 1) + makeln(size); } public static String makeln(int size){ if (size == 1){ return "*\n"; } else{ return "*" + makeln(size - 1); } }

52 min later 31008371 Anonymous
>>31008352 //string ret = triangle(size - 1) + makeln(size);

54 min later 31008406 Anonymous
>>31008349 >What's with the reversing of the parameter in triangle(-size)? That's pretty interesting. To generate a nested loop with a single recursive function. 1: -3 -2 -1 0 2: -2 -1 0 3: -1 0

57 min later 31008449 Anonymous
>>31008352 Good enough for you? std::string print_triangle(int size) { static int isize = size; if(size > isize) isize = size; if(size > 0) { //printf("*"); return "*" + print_triangle(size-1); } if(size == 0) { //printf("\r\n"); isize--; if(isize) return "\r\n" + print_triangle(isize); else return ""; } isize = 0; }

58 min later 31008464 Anonymous
>>31008406 That's really cool, thanks. Sorry if was a little short earlier. I thought you were like the million other idiots who ignore the requirements. I gotta go but I'll give this a shot again when I get home.

58 min later 31008470 Anonymous
>>31008449 Why the fuck did I quote >>31008352 oops. Meant to quote >>31008349

59 min later 31008485 OP IS A
Calculate the bottom row, then put it underneath a smaller triangle.

1 hours later 31008511 Anonymous
>>31008464 Python version which returns a string: def tri(size): if size > 0: return tri(size - 1) + tri(-size) elif size < 0: return "*" + tri(size + 1) else: return "\n" return "" Can't quite get rid of the leading newline, not sure if it's possible without introducing some extra state.

1 hours later 31008640 Anonymous
>>31008511 def tri(size): if size > 0: return tri(size - 1) + tri(-size) elif size = -1: return "*\n" else: return "*" + tri(size + 1) return "" ??

1 hours later 31008678 Anonymous
>>31008511 Actually here we go, solved it: def tri(size): if size > 0: a = tri(size - 1) b = tri(-size) if len(a) == 1: return b else: return a + b elif size < 0: return "*" + tri(size + 1) else: return "\n" return ""

1 hours later 31008708 Anonymous
>>31008640 Or that will work, yeah.

0.827 0.086