Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Write a script that writes to standard output, or your language's equivalent, 100 lines of (valid) Java code that begins with:

class Tuple1 {public Object _0;}
class Tuple2 {public Object _0,_1;}
class Tuple3 {public Object _0,_1,_2;}
class Tuple4 {public Object _0,_1,_2,_3;}
class Tuple5 {public Object _0,_1,_2,_3,_4;}
class Tuple6 {public Object _0,_1,_2,_3,_4,_5;}
class Tuple7 {public Object _0,_1,_2,_3,_4,_5,_6;}
class Tuple8 {public Object _0,_1,_2,_3,_4,_5,_6,_7;}
class Tuple9 {public Object _0,_1,_2,_3,_4,_5,_6,_7,_8;}
class Tuple10 {public Object _0,_1,_2,_3,_4,_5,_6,_7,_8,_9;}
class Tuple11 {public Object _0,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10;}
class Tuple12 {public Object _0,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11;}

The last line should begin with class Tuple100.

This is , so the shortest code in bytes wins!

share|improve this question
5  
I accept the challenge! In fact, I'm going to do this in Java! – Ashwin Gupta Feb 4 at 0:41
    
Can I write a function or does it have to be a full program? – Ashwin Gupta Feb 4 at 0:42
2  
I'm new to the site so asked for a script, but I've been hearing it's customary to allow functions, and that makes a lot of sense for Java, to avoid public static void main(String[] args) so definitely give a function if you like. – Ray Toal Feb 4 at 3:40
    
Great! Thank you Ray! – Ashwin Gupta Feb 4 at 4:01
    
It's not anywhere near small enough, but I think this a perfect place to share my n-tuple generator I wrote in Java: github.com/kenzierocks/Tuplocity. It generates fully generic tuples! – Kenzie Togami Feb 4 at 4:57

51 Answers 51

up vote 4 down vote accepted

Jolf, 42 bytes

Do I get bonus points for beating Jelly with the best score ever? Contains unprintables, so you may want to try it online here. I replaced the unprintables with their respective alt index for readability.

‼Μz~1d"Ξ/êί Tuple% {Ξ0î⌂ Ξ2Qμ_ %;}"HRzH",_

Explanation

‼Μz~1d"Ξ/êί Tuple% {Ξ0î⌂ Ξ2Qμ _%;}"HRzH",_
 Μz~1d                                     map the range 1..100 with the following function
      "                                    begin a string
       Ξ/êί                                short for "class"
            Tuple% {               H       string interpolate H (current number)
                    Ξ0î⌂ Ξ2Qμ              short for "public Object"
                              _%;}" RzH    string interpolate with a range from 1..H joined
                                       ",_  by the string ",_" (auto-closed quotes)
share|improve this answer
    
I suppose. Is it proper on this site to change the accepted answer if a new score comes in later? – Ray Toal Mar 22 at 6:12
4  
@RayToal Yes, it is proper and even suggested to do so. – Cᴏɴᴏʀ O'Bʀɪᴇɴ Mar 22 at 18:50

vim 56 54 keystrokes

iclass Tuple1 {public Object _0;}<esc>qyYp<c-a>$y2bPr,<c-a>q99@y

This is the perfect task for vim! I might golf it a little bit more later. Also note that <c-a> means Control-A, and I'm counting it as one keystroke at the moment.

Explanation:

iclass Tuple1 {public Object _0;}<esc>         'Enter the starting text


qy                          'Start recording in register y
  Yp                        'Yank the current line, the print on the line below
     <c-a>                  'Increment the next digit to occur by one
          $                 'Move to the end of this line
           y2b              '(yank) 2 words (b)ack. This will grab '_0;'
              P             'Print the previously yanked word before the cursor.
               r,           '(r)eplace the char under the cursor with a comma.
                 <c-a>q     'Increment the current digit, then stop recording
                       99@y 'Playback macro 'y' 99 times.
share|improve this answer
    
If you count Control-A as one keystroke, you could substitute yy by Y. – Seeq Feb 3 at 21:44
1  
@Seeq Haha, I just realized the same thing and edited it in right before I saw your comment. Great minds think alike! – Dr Green Eggs and Iron Man Feb 3 at 21:45

Jelly, 44 bytes

R’j“,_”
“¤>%,oỊȤʠ“ØụĊ5D³ṃṠɼQ»j;Ç;“;}¶”
ȷ2RÇ€

My first Jelly answer. Try it online!

share|improve this answer
    
Am I doing it wrong or does this just output a giant list instead of a string? – DenkerAffe Feb 3 at 19:30
    
@DenkerAffe It seems to be working now! There was a temporary bug in Jelly. (Also, the link was to an old version of my solution, so I've updated it.) – Lynn Feb 3 at 20:04
    
Nice... as of now it's 9 bytes shorter than the CJam solution! – Ray Toal Feb 5 at 3:56

Pyth, 53 50 48 bytes

VS100%." }SüÆðQ´Ó3Ô«%&a´4UçõÛ"[Nj",_"UN

Try it online!

Straightforward iteration over range(1,100) and building the corrosponding string from a packed string through formatting it.

Explanation

VS100%." }SüÆðQ´Ó3Ô«%&a´4UçõÛ"[Nj",_"UN

VS100                                     # Iterate over range(1,100)
     %." }SüÆðQ´Ó3Ô«%&a´4UçõÛ"            # Unpack the string and format it
                              [           # List for formatting arguments
                               N          # Number of the Tuple
                                j    UN   # Join Object numbers...
                                 ",_"     # ...on the seperator ",_"

The unpacked string is class Tuple%i {public Object _%s;}

share|improve this answer
    
Two golfs: Use s[ instead of all of those +. [ makes an arbitrary length list, and s concatenates it. Also, since you want [1, 2, ... 100], not [0, 1, .. 99], use VS100, and you won't need either h. – isaacg Feb 3 at 8:21
    
@isaacg Thanks, did not know about the S-trick! :) Was already sitting on getting rid of all the ++++, I knew there is a better way. – DenkerAffe Feb 3 at 8:33
    
You;re welcome. By the way, you seem to have added a trailing " unnecessarily. – isaacg Feb 3 at 9:02
1  
@isaacg Is there any reason j<str><int> doesn't U? – lirtosiast Feb 3 at 21:05
1  
No, not really. Will do. – isaacg Feb 3 at 21:29

CoffeeScript, 86 84 bytes

console.log "class Tuple#{i} {public Object _#{[0...i].join ',_'};}"for i in[1..100]

View the solution online

share|improve this answer
    
Welcome to PPCG! Just to let you know edits that improve answers are generally frowned (and should be rejected if they end up in the review queue). So it's more likely that people will comment with suggestions, so you can review them yourself. :) (As for actual tips I think you need neither the space in front of for nor the one after in.) – Martin Ender Feb 3 at 7:56
    
Got it, thanks! – Ray Toal Feb 3 at 7:59
    
Does console.log" work? (Don't know coffee script, that's a general trick. – VTCAKAVSMoACE Feb 3 at 8:05
    
No, as in, removing the space. :P I know it exists, I just don't know if that's valid syntax. – VTCAKAVSMoACE Feb 3 at 8:16
    
@VoteToClose I tried that earlier (using the live transpiler on coffeescript.org and it didn't work). – Martin Ender Feb 3 at 8:19

Oracle SQL 9.2, 138 137 Bytes

SELECT REPLACE('class Tuple'||LEVEL||' {public Object'||SYS_CONNECT_BY_PATH(LEVEL-1,',_')||';}','t,','t ')FROM DUAL CONNECT BY LEVEL<101;

@Peter Thanks for pointing the 0/1 mistake.

The query use the CONNECT BY CLAUSE of hierarchical query to generate 100 rows.

The LEVEL pseudocolumn contains the row number of each row.

SYS_CONNECT_BY_PATH concatenate the first parameter, the row number, of each row, and use the second parameter as the separator.

share|improve this answer
    
please add some explanation – Eumel Feb 3 at 13:32
    
Clever :) This starts with Object _1 instead of Object _0, so that will take another two bytes (-1). However, the _ can be added to the char of SYS_CONNECT_BY_PATH instead of concatenating, reducing it by two those bytes again: REPLACE('class Tuple'||LEVEL||' {public Object'||SYS_CONNECT_BY_PATH(LEVEL-1,',_')||';}','t,_','t _') – Peter Lang Feb 5 at 7:57

Retina, 80 79 bytes

Byte count assumes ISO 8859-1 encoding.

100$*1
1
class Tuple11$` {public Object 1$`}¶
1(?<= (1+))
_$1,
1(1)*
$#1
,}
;}

Try it online!

share|improve this answer

Batch, 129 bytes

@set m=_0
@for /l %%t in (1,1,100) do @call:b %%t
@exit/b
:b
@echo class Tuple%1 {public Object %m%;}&set m=%m%,_%1

Edit: Saved 10 bytes thanks to @Bob.

share|improve this answer
2  
I always get really happy to see batch on code golf, also, instead of using @ everywhere, you could redirect output to append to a file called x and use that as output – Dennis van Gils Feb 3 at 12:15
    
@DennisvanGils Sadly commands get echoed to standard output. – Neil Feb 3 at 18:49
    
Eliminated enabledelayedexpansion for 117 bytes: gist.github.com/Elusive138/4cea555114a979954dcb -- if you don't mind the console window closing you can save two more by removing the /b arg from exit. – Bob Feb 4 at 0:08
    
@Neil lol okay so I just tried running it and now I realize what you did. Very clever. – Ashwin Gupta Feb 4 at 4:13
    
At beginning, use @echo off – Elliot A. Feb 6 at 18:00

Java, 160, 125 Bytes

Thanks to @DenkerAffe, @Denham Coote and @Mathias Ettinger for the improvements.

Java writing java( because someone had to!)

void z(){String s="_0";for(int i=1;i<101;){System.out.println("class Tuple"+(i++)+" {public Object "+s+";}");s+=",_"+i;}}

And the un-golfed version

void z(){
    String s = "_0";
    for(int i = 1 ;i < 101;){
        System.out.println("class Tuple" + (i++) + " {public Object "+ s + ";}");
        s += ",_" + i;
        }
    }
share|improve this answer
2  
Welcome to Programming Puzzles & Code Golf! Unless stated otherwise in the challenge, you can alwasy use functions instead of full programs. So you should use a Java8-lambda (which returns the result) here to save you all the public static void main...stuff. – DenkerAffe Feb 3 at 10:07
4  
Welcome to PPCG. This answer includes an extra comma at the end of the Object declarations... class Tuple1 {public Object _0,;} class Tuple2 {public Object _0,_1,;} – Denham Coote Feb 3 at 10:39
3  
You can save a further 3 bytes by incrementing your loop counter in the sysout (++i) instead of (i+1) and removing i++ from the for loop setup – Denham Coote Feb 3 at 10:58
    
You should start your String s = "_0" so you can s += ",_" + i and save a byte. It will also solve the problem with the trailing coma. You’ll need to start the for loop going from 1 to less than 101 and will be able to remove an extra 4 bytes by converting the (i+1) in your output message to only i. Obviously, you’ll need to print before incrementing s. – Mathias Ettinger Feb 4 at 13:59
    
Thanks, have done all these things, and am definitely going to have a gander at java8-lambda. – SoloKix Feb 6 at 2:10

Perl 6, 65 bytes

say "class Tuple$_ \{public Object _{join ',_',^$_};}" for 1..100
share|improve this answer

R - 199 132 123 118 bytes

Version 4

p=paste0;for(i in 1:10)cat(p("class Tuple",i," {public Object ",toString(sapply(0:(i-1),function(x)p("_",x))),";}\n"))

Version 3

p=paste0;for(i in 1:8)cat(p("class Tuple",i," {public Object ",p(sapply(0:(i-1),function(x)p("_",x)),collapse=","),";}\n"))

Version 2

p=paste0;for(i in 1:100)cat(p("class Tuple",i," {public Object ",p(sapply(0:(i-1),function(x)p("_",x)),collapse=","),";}"),sep="\n")

Version 1

for (i in 1:100){
  foo <- paste0("class Tuple", i, " {public Object ")
  for (j in 0:(i - 1)){
    foo <- if (j < (i - 1)) paste0(foo, "_", j, ",") else paste0(foo, "_", j, ";}")
  }
  print(foo)
}
share|improve this answer
2  
Welcome to Programming Puzzles & Code Golf! Since this is a code golf competition, you'll want to make your code as short as possible. You could start by shortening variable names to a single character each, removing whitespace, and using = for assignment rather than <-. – Alex A. Feb 3 at 17:17
    
144 bytes: for(i in 1:100){f=(p=paste0)("class Tuple",i," {public Object ");for(j in 0:(i-1))f=if(j<i-1)p(f,"_",j,",")else p(f,"_",j,";}");cat(f,sep="\n")} – Alex A. Feb 3 at 17:23
    
Thanks for the advice!!! – Argenis García Feb 3 at 19:38
    
No problem. :) Also, you don't need to keep track of your versions in the post body since the revision history is accessible via the link that says "edited X minutes ago." – Alex A. Feb 3 at 19:40
    
(though it may be worthwhile to give some indication, either in your edit summaries or in the post what you changed with each revision so that others could learn from your revision process) – quintopia Feb 3 at 19:54

Ruby, 71 bytes

100.times{|i|puts"class Tuple#{i+1} {public Object _#{[*0..i]*',_'};}"}
share|improve this answer
2  
TIL [*0..i]*',_' wow – Ray Toal Feb 4 at 3:44

Java, 103 bytes

s->{s="_0";for(int i=0;i++<100;s+=",_"+i)System.out.printf("class Tuple%s {public Object %s;}\n",i,s);}

My first time here. Hi there! I went for a Java8 lambda expression (aka an anonymous function).

Ungolfed version

s -> {
    s = "_0";
    for (int i = 0; i++ < 100; s += ",_" + i)
        System.out.printf("class Tuple%s {public Object %s;}\n", i, s);
}

To actually use this, as usual in Java8, you have to assign it to a variable of (or cast it to) an appropriate functional interface and then call its method; but technically, the function is only the lambda expression itself, so I am counting only that.

It also requires an input parameter, which saves me a couple bytes, since I can abuse it as a local variable without having to specify its type. I'm not sure if that's considered cheating, but to me it appears to be within the rules: I only saw people state that posting a function is allowed, not how that function needs to be invoked. :) And it doesn't actually read the parameter, so the function is still self-contained; you can pass any String to it, or even null, and it will still produce the correct output.

And here is how to use it:

import java.util.function.Consumer;

public class Main {
    public static void main(String[] args) {
        Consumer<?> r =
            s->{s="_0";for(int i=0;i++<100;s+=",_"+i)System.out.printf("class Tuple%s {public Object %s;}\n",i,s);}
        ;
        r.accept(null); 
    }
}
share|improve this answer

CJam, 53 bytes

100{)"class Tuple"\" {public Object _"1$,",_"*";}
"}/

Try it here.

share|improve this answer

Python 2, 98

def f(n=99):s=n and f(n-1)+',_'+`n`or"class Tuple%d {public Object _0";print s%(n+1)+';}';return s

Python 3, 100

def f(n=99):s=n and f(n-1)+',_%d'%n or"class Tuple%d {public Object _0";print(s%(n+1)+';}');return s

Usage:

f()

Ungolfed:

def javatuple(n=99):
    if n == 0:
        s = "class Tuple%d {public Object _0"
    else:
        s = javatuple(n-1) + ',_' + str(n)
    print(s%(n+1) + ';}')
    return s

formers:

103

r=range
for i in r(100):
 print"class Tuple%d {public Object _%s;}"%(i+1,',_'.join(`j`for j in r(i+1)))

108

def f(n=99):
 if n:f(n-1)
 print"class Tuple%d {public Object _%s;}"%(n+1,',_'.join(`i`for i in range(n+1)))
share|improve this answer
    
In your python2 answer the last line seems to be incomplete : print f(3) gives : class Tuple1 {public Object _0;} class Tuple2 {public Object _0,_1;} class Tuple3 {public Object _0,_1,_2;} class Tuple4 {public Object _0,_1,_2,_3;} class Tuple%d {public Object _0,_1,_2,_3 – dieter Feb 3 at 15:15
    
@dieter The last line is a result. It's printed only when you run it interactively. – pacholik Feb 3 at 18:03

Groovy, 74 chars

"join()" is unbeatable... New solution, thanks to @yariash

100.times{println"class Tuple$it {public Object _${(0..it-1).join',_'};}"}

Old solution, 78 chars:

(1..100).each{println"class Tuple$it {public Object _${(0..it-1).join',_'};}"}
share|improve this answer
1  
You can use 100.times{} instead of (1..100).each{}. – yariash May 17 at 9:36
    
@yariash Thanks for the hint! – t0r0X May 17 at 10:27

Julia, 77 bytes

for i=1:100;println("class Tuple$i {public Object _$(join(0:i-1,",_"));}")end
share|improve this answer

Lua, 128 106 Bytes

I'm still trying to find a way to directly work on a printed value, and not on a string.

Edit : Partially found it! I still need a string for the "_0,_1..." part, but it already is better :).

s=""for i=0,99 do s=(s..",_"..i):gsub("^,",s)print(("class Tuple"..(i+1).." {public Object ")..s..";}")end

Old 128 Bytes solution

s="class Tuple1 {public Object _0;}"print(s)for i=1,99 do s=s:sub(1,s:find";"-1):gsub("e%d+","e"..i+1)..",_"..i..";}"print(s)end

Ungolfed

s=""
for i=0,99
do
  s=(s..",_"..i)              -- concatenate s with ",_i"
      :gsub("^,",s)           -- remove the leading "," for ",_0"
  -- then print the concatenated string
  print(("class Tuple"..(i+1).." {public Object ")..s..";}")
end
share|improve this answer

Python 3, 111 109 105 bytes

[print('class Tuple%s {public Object %s;}'%(i,','.join('_%i'%j for j in range(i))))for i in range(1,101)]

It's not the shortest thing in the world, I'm just participating.

edit1: down 2 bytes by removing 0, in first range

edit2: I was unnecessarily casting int to str instead of just using %i... Down to 105.

share|improve this answer
    
Welcome, nice start! If interested, try range(i) in place of range(0,i) or since you used range twice, try r=range early then reference each call with just r (as in the other Python solutions). – Ray Toal Feb 3 at 8:58
    
Ugh, I forgot range starts at 0 by default. Down 2 bytes, however, adding r=range; in the beginning results in exactly the same character count, I've tried that! :D – Issak Feb 3 at 9:05
    
Same comment than I did to Dair: ',_'.join(map(str,range(i))) is 7 bytes shorter here. – Mathias Ettinger Feb 4 at 14:13

Mathematica, 130 Bytes

{"class Tuple",ToString[#]," {public Object ",StringReplace[ToString[#-1&/@Range@#],{" "|"{" ->"_","}"->";}\n"}]}&/@Range[100]<>""
share|improve this answer
    
Hint: \n can be replaced with a literal newline character. – LegionMammal978 Feb 3 at 22:10

Scala, 85 Bytes

for(u<-1 to 100)println(s"class Tuple$u {public Object _${0 to u-1 mkString ",_"};}")
share|improve this answer

Java, 116

(for the printing function only - according to some comments, this is in line with the rules)

import static java.lang.System.*;
public class T
{
    public static void main(String[] args)
    {
        T t = new T();
        t.p();
    }

    void p(){String s="_0";for(int i=0;i<100;){out.println("class Tuple"+ ++i+" {public Object "+ s + ";}");s+=",_"+i;}}
}
share|improve this answer

Powershell - 65 bytes

(Amended in response to comment)

All credit to TimmyD

1..100|%{"class Tuple$_ {public Object _$(0..($_-1)-Join",_");}"}
share|improve this answer
    
Why the vote down? (Please note this is my first ever post, not a great welcome?) – bulletprooffool Feb 3 at 13:27

PHP, 112 bytes

<?php for($i=0;$i<100;$i++){$m.=$i==0?'_'.$i:',_'.$i;echo 'class Tuple'.($i+1).' {public Object '.$m.';}<br/>';}
share|improve this answer
    
You can save a few bytes by switching echo to double quotes where possible: <?php for($i=0;$i<100;$i++){$m.=$i==0?"_$i":",_$i";echo 'class Tuple'.($i+1)." {public Object $m;}\n";} – Samsquanch Feb 3 at 16:49
1  
@Samsquanch Also, instead of echo 'class Tuple'.($i+1)." {public Object $m;}\n";, you can use echo'class Tuple',$i+1," {public Object $m;}\n";, which is 3 bytes shorter. Also, according to the meta, you can remove the <?php and assume that your code will be executed with php -r "code". Also, you can do for(;++$i<101;)echo"class Tuple$i {public Object ",$m=($m?"$m,":'')."_$i",";}\n"; for 80 bytes (you need to replace the \n with a real newline). Try it here: ideone.com/oOzKtP – Ismael Miguel Feb 4 at 10:34

Seriously, 55 bytes

2╤R`;r"$'_+"£M',j@k"c╙ò T╒α%d {pu▐V Object %s;}"⌡%`M'
j

Hexdump (reversible with xxd -r):

00000000: 32e2 95a4 5260 3b72 2224 275f 2b22 c2a3  2...R`;r"$'_+"..
00000010: 4d27 2c6a 406b 2263 e295 99c3 b220 54e2  M',j@k"c..... T.
00000020: 9592 ceb1 2564 207b 7075 e296 9056 204f  ....%d {pu...V O
00000030: 626a 6563 7420 2573 3b7d 22e2 8ca1 2560  bject %s;}"...%`
00000040: 4d27 0a6a                                M'.j

Try it online!

Yes, that newline is supposed to be there.

Explanation (newline replaced with \n):

2╤R`;r"$'_+"£M',j@k"c╙ò T╒α%d {pu▐V Object %s;}"⌡%`M'\nj
2╤R                                                       push range [1,100]
   `                                              `M      map:
    ;r                                                      push a, range(a)
      "$'_+"£M                                              map:
       $'_+                                                   stringify, prepend "_"
              ',j                                           join with commas
                 @k                                         swap and push stack as list
                   "c╙ò T╒α%d {pu▐V Object %s;}"⌡           decompress, result is
                                                              "class Tuple%d {public Object %s;}"
                                                 %          string format
                                                    '\nj  join with newlines
share|improve this answer

Japt, 52 bytes

Lo £`cÓ? TÕà{X} \{puÞV Object {Xò ®'_+Z}&}` r&59d} ·

The ? should be the literal byte 0x95. Test it online!

Japt has:

  • concise syntax to help shorten your code.
  • string compression to help shorten it even further.
  • lots of bugs to make you want to throw your computer out the window.

I can't even use a semicolon in the string because the function it's wrapped in wants to return everything after the semicolon. This should only be 46 bytes:

Lo £`cÓ? TÕà{X} \{puÞV Object {Xò ®'_+Z};}`} ·

But alas, it is not. I'll see if I can fix these bugs over the next couple of hours.

How it works

Lo £       }  // Generate the range [0..100), and map each item X in this range to:
`cÓ? TÕà{X}   //  "class Tuple" + X +
\{puÞV Object //  "{public Object" +
{Xò ®'_+Z}    //   Create the range [0..X] and map each item Z to "_" + Z.
              //   Upon stringification, this automatically inserts the commas.
&}`           //  + "&}"
r&59d         //  Replace the "&" with String.fromCharCode(59), or ";".
·             // Join the result with newlines.
share|improve this answer

Javascript, 112 110 bytes

2 bytes off thanks @CᴏɴᴏʀO'Bʀɪᴇɴ.

_=>[...Array(100)].map((x,i)=>`class Tuple${++i} {public Object ${[...Array(i)].map((y,j)=>'_'+j)};}`).join`
`

Ungolfed:

_=>                                         // anonymous function
    [...Array(100)].map((x,i)=>             // 100 times [0..99]
        `class Tuple${++i} {public Object   // Tuple index + 1
            ${[...Array(i)].map(            // (index + 1) times
                (y,j)=>'_'+j                // _+j with j in [0..index]
            )}                              // Array without join prints comma in between
        ;}`                                 // as literal string
    ).join`\n`                              // display array line by line
share|improve this answer
1  
I think you can replace ${i+1} with ${i++} and replace ${[...Array(i++)] with ${[...Array(i)]. – Cᴏɴᴏʀ O'Bʀɪᴇɴ Feb 3 at 19:55
    
@CᴏɴᴏʀO'Bʀɪᴇɴ. Thanks, I've updated with ++i – removed Feb 4 at 0:14

Groovy, 97 bytes

(1..100).each{println "class Tuple${it} {public Object ${(0..it-1).collect{"_$it"}.join(',')};}"}

Try it online!

share|improve this answer
1  
You can user 100.times{} instead of (1..100).each{}, which saves 4 bytes. – yariash May 17 at 9:13
    
thank you yariash :) – Apostolos May 17 at 9:15
    
You can also omit braces in join (join','), remove space after println, and replace .collect{} with *.with{} – yariash May 17 at 9:31

Python 2, 96 Bytes

r=range
for i in r(1,101):print"class Tuple%d {public Object _%s;}"%(i,',_'.join(map(str,r(i))))

Thanks @DenkerAffe for the suggestion of using lambda.

Thanks @MathiasEttinger for the join idea.

share|improve this answer
2  
You can golf this down to 111 bytes if you use a lambda in your reduce-call instead of defining a function for this: reduce(lambda x,y:'%s,_%s'%(x,y), r(i)) – DenkerAffe Feb 3 at 9:01
1  
Or use ',_'.join(map(str,r(i))) for an extra 14 bytes removal (or 15, since you left an extra space in your reduce). – Mathias Ettinger Feb 4 at 14:07

C++, 164 159 157 bytes (155 as function only)

#include <iostream>
#define o std::cout<<
int main(){int j,i=0;for(;i<100;++i){o"class Tuple"<<i+1<<" {public Object _0";for(j=0;++j<=i;)o",_"<<j;o";}\n";}}

This is a full program. You can save 2 bytes in function only form if you replace int main() with void f().

Run code in Ideone

Ungolfed, including macro expansion

#include <iostream>
int main()
{
    int j,i=0;
    for(;i<100;++i)
    {
        std::cout << "class Tuple"<< i+1 <<" {public Object _0";
        for(j=0;++j<=i;)
            std::cout << ",_" << j;
        std::cout << ";}\n";
    }
}
share|improve this answer
    
Welcome to Programming Puzzles & Code Golf! Nice first answer! Unless stated otherwise in the challenge, you can always use functions instead of full programs, so you can save a few bytes by doing that. You still have to include iostream tho, since its needed for the output. – DenkerAffe Feb 4 at 15:22
    
You might also want to do j=1 in your for loop, thus not having the if(j). – Ethiraric Feb 5 at 10:42
    
@Ethiraric But of course! How sloppy of me. I guess I got too focused on the idea "remember to skip additions on first line" and ended up writing that too literally. – sendaran Feb 5 at 10:59
    
@sendaran Or even may be for(int j=0;++j<=i;) would save one more byte. – Ethiraric Feb 5 at 11:25
    
@Ethiraric Did that and shaved off an extra byte by extracting variable declarations, which enabled another empty expression. – sendaran Feb 5 at 14:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.