35
\$\begingroup\$

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!

\$\endgroup\$
  • 6
    \$\begingroup\$ I accept the challenge! In fact, I'm going to do this in Java! \$\endgroup\$ – Ashwin Gupta Feb 4 '16 at 0:41
  • \$\begingroup\$ Can I write a function or does it have to be a full program? \$\endgroup\$ – Ashwin Gupta Feb 4 '16 at 0:42
  • 3
    \$\begingroup\$ 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. \$\endgroup\$ – Ray Toal Feb 4 '16 at 3:40
  • 1
    \$\begingroup\$ Great! Thank you Ray! \$\endgroup\$ – Ashwin Gupta Feb 4 '16 at 4:01
  • \$\begingroup\$ 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! \$\endgroup\$ – Octavia Togami Feb 4 '16 at 4:57

55 Answers 55

7
\$\begingroup\$

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)
\$\endgroup\$
  • 1
    \$\begingroup\$ I suppose. Is it proper on this site to change the accepted answer if a new score comes in later? \$\endgroup\$ – Ray Toal Mar 22 '16 at 6:12
  • 6
    \$\begingroup\$ @RayToal Yes, it is proper and even suggested to do so. \$\endgroup\$ – Conor O'Brien Mar 22 '16 at 18:50
  • \$\begingroup\$ What's the character encoding for this? I know golfing languages frequently use their own encoding, but I can't see which encoding jolf could be using. I realize this is old. \$\endgroup\$ – recursive Sep 25 '17 at 23:58
  • \$\begingroup\$ @recursive The greek ISO-8859-7, this one: en.wikipedia.org/wiki/ISO/IEC_8859-7 \$\endgroup\$ – Conor O'Brien Sep 26 '17 at 0:00
21
\$\begingroup\$

vim 56 54 keystrokes

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

Since V is backwards compatible, you can Try it online!

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 it counts as one keystroke.

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.
\$\endgroup\$
  • 1
    \$\begingroup\$ If you count Control-A as one keystroke, you could substitute yy by Y. \$\endgroup\$ – seequ Feb 3 '16 at 21:44
  • 1
    \$\begingroup\$ @Seeq Haha, I just realized the same thing and edited it in right before I saw your comment. Great minds think alike! \$\endgroup\$ – DJMcMayhem Feb 3 '16 at 21:45
  • \$\begingroup\$ This seems to be going to class Tuple101, not class Tuple100 as the question asks. \$\endgroup\$ – numbermaniac Sep 26 '17 at 0:02
  • 1
    \$\begingroup\$ @numbermaniac Simple off-by-one error. Thanks for pointing that out! Fixed now :) \$\endgroup\$ – DJMcMayhem Sep 26 '17 at 14:16
10
\$\begingroup\$

Jelly, 44 bytes

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

My first Jelly answer. Try it online!

\$\endgroup\$
  • \$\begingroup\$ Am I doing it wrong or does this just output a giant list instead of a string? \$\endgroup\$ – Denker Feb 3 '16 at 19:30
  • \$\begingroup\$ @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.) \$\endgroup\$ – Lynn Feb 3 '16 at 20:04
  • \$\begingroup\$ Nice... as of now it's 9 bytes shorter than the CJam solution! \$\endgroup\$ – Ray Toal Feb 5 '16 at 3:56
8
\$\begingroup\$

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;}

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – isaacg Feb 3 '16 at 8:21
  • \$\begingroup\$ @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. \$\endgroup\$ – Denker Feb 3 '16 at 8:33
  • \$\begingroup\$ You;re welcome. By the way, you seem to have added a trailing " unnecessarily. \$\endgroup\$ – isaacg Feb 3 '16 at 9:02
  • 1
    \$\begingroup\$ @isaacg Is there any reason j<str><int> doesn't U? \$\endgroup\$ – lirtosiast Feb 3 '16 at 21:05
  • 1
    \$\begingroup\$ No, not really. Will do. \$\endgroup\$ – isaacg Feb 3 '16 at 21:29
7
\$\begingroup\$

CoffeeScript, 86 84 bytes

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

View the solution online

\$\endgroup\$
  • \$\begingroup\$ 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.) \$\endgroup\$ – Martin Ender Feb 3 '16 at 7:56
  • \$\begingroup\$ Does console.log" work? (Don't know coffee script, that's a general trick. \$\endgroup\$ – Addison Crump Feb 3 '16 at 8:05
  • \$\begingroup\$ No, as in, removing the space. :P I know it exists, I just don't know if that's valid syntax. \$\endgroup\$ – Addison Crump Feb 3 '16 at 8:16
  • \$\begingroup\$ @VoteToClose I tried that earlier (using the live transpiler on coffeescript.org and it didn't work). \$\endgroup\$ – Martin Ender Feb 3 '16 at 8:19
  • \$\begingroup\$ @VoteToClose Oh sorry didn't see that. No. somehow the space is required by the grammar, but it is not exactly clear to me why (yet). Spacing matters a lot in CoffeeScript, for instance, x? y:z is completely different from x ? y:z. You can say console.log?"hello" but not console.log"hello". Significant whitespace. Sometimes nice, sometimes weird. \$\endgroup\$ – Ray Toal Feb 3 '16 at 8:28
7
\$\begingroup\$

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;
        }
    }
\$\endgroup\$
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – Denker Feb 3 '16 at 10:07
  • 4
    \$\begingroup\$ 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,;} \$\endgroup\$ – Denham Coote Feb 3 '16 at 10:39
  • 3
    \$\begingroup\$ 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 \$\endgroup\$ – Denham Coote Feb 3 '16 at 10:58
  • \$\begingroup\$ 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. \$\endgroup\$ – Mathias Ettinger Feb 4 '16 at 13:59
  • \$\begingroup\$ Thanks, have done all these things, and am definitely going to have a gander at java8-lambda. \$\endgroup\$ – SoloKix Feb 6 '16 at 2:10
6
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ please add some explanation \$\endgroup\$ – Eumel Feb 3 '16 at 13:32
  • \$\begingroup\$ 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 _') \$\endgroup\$ – Peter Lang Feb 5 '16 at 7:57
6
\$\begingroup\$

Batch, 128 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. Saved 1 byte thanks to @EʀɪᴋᴛʜᴇGᴏʟғᴇʀ.

\$\endgroup\$
  • 3
    \$\begingroup\$ 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 \$\endgroup\$ – Dennis van Gils Feb 3 '16 at 12:15
  • \$\begingroup\$ @DennisvanGils Sadly commands get echoed to standard output. \$\endgroup\$ – Neil Feb 3 '16 at 18:49
  • \$\begingroup\$ 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. \$\endgroup\$ – Bob Feb 4 '16 at 0:08
  • \$\begingroup\$ @Neil lol okay so I just tried running it and now I realize what you did. Very clever. \$\endgroup\$ – Ashwin Gupta Feb 4 '16 at 4:13
  • \$\begingroup\$ At beginning, use @echo off \$\endgroup\$ – Elliot A. Feb 6 '16 at 18:00
5
\$\begingroup\$

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!

\$\endgroup\$
5
\$\begingroup\$

Powershell - 65 bytes

(Amended in response to comment)

All credit to TimmyD

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

Perl 6, 65 bytes

say "class Tuple$_ \{public Object _{join ',_',^$_};}" for 1..100
\$\endgroup\$
4
\$\begingroup\$

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)
}
\$\endgroup\$
  • 3
    \$\begingroup\$ 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 <-. \$\endgroup\$ – Alex A. Feb 3 '16 at 17:17
  • \$\begingroup\$ 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")} \$\endgroup\$ – Alex A. Feb 3 '16 at 17:23
  • \$\begingroup\$ 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." \$\endgroup\$ – Alex A. Feb 3 '16 at 19:40
  • \$\begingroup\$ (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) \$\endgroup\$ – quintopia Feb 3 '16 at 19:54
4
\$\begingroup\$

Ruby, 71 bytes

100.times{|i|puts"class Tuple#{i+1} {public Object _#{[*0..i]*',_'};}"}
\$\endgroup\$
  • 2
    \$\begingroup\$ TIL [*0..i]*',_' wow \$\endgroup\$ – Ray Toal Feb 4 '16 at 3:44
4
\$\begingroup\$

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); 
    }
}
\$\endgroup\$
3
\$\begingroup\$

Python 2, 96

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

Python 3, 98

def f(n=99):s=n and f(n-1)+',_%d'%n or"class Tuple%d {public Object _0";print(s%-~n+';}');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)))
\$\endgroup\$
  • \$\begingroup\$ 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 \$\endgroup\$ – dieter Feb 3 '16 at 15:15
  • \$\begingroup\$ @dieter The last line is a result. It's printed only when you run it interactively. \$\endgroup\$ – pacholik Feb 3 '16 at 18:03
2
\$\begingroup\$

CJam, 53 bytes

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

Try it here.

\$\endgroup\$
2
\$\begingroup\$

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',_'};}"}
\$\endgroup\$
  • 2
    \$\begingroup\$ You can use 100.times{} instead of (1..100).each{}. \$\endgroup\$ – Krzysztof Atłasik May 17 '16 at 9:36
  • \$\begingroup\$ @yariash Thanks for the hint! \$\endgroup\$ – t0r0X May 17 '16 at 10:27
1
\$\begingroup\$

Julia, 77 bytes

for i=1:100;println("class Tuple$i {public Object _$(join(0:i-1,",_"));}")end
\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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). \$\endgroup\$ – Ray Toal Feb 3 '16 at 8:58
  • \$\begingroup\$ 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 \$\endgroup\$ – Issak Feb 3 '16 at 9:05
  • \$\begingroup\$ Same comment than I did to Dair: ',_'.join(map(str,range(i))) is 7 bytes shorter here. \$\endgroup\$ – Mathias Ettinger Feb 4 '16 at 14:13
1
\$\begingroup\$

Mathematica, 130 Bytes

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

Scala, 85 Bytes

for(u<-1 to 100)println(s"class Tuple$u {public Object _${0 to u-1 mkString ",_"};}")
\$\endgroup\$
1
\$\begingroup\$

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;}}
}
\$\endgroup\$
1
\$\begingroup\$

PHP, 112 bytes

<?php for($i=0;$i<100;$i++){$m.=$i==0?'_'.$i:',_'.$i;echo 'class Tuple'.($i+1).' {public Object '.$m.';}<br/>';}
\$\endgroup\$
  • \$\begingroup\$ 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";} \$\endgroup\$ – Samsquanch Feb 3 '16 at 16:49
  • 1
    \$\begingroup\$ @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 \$\endgroup\$ – Ismael Miguel Feb 4 '16 at 10:34
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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.
\$\endgroup\$
  • \$\begingroup\$ Bah! Ninjaed by ~18 months! You need to increment the first X, by the way. \$\endgroup\$ – Shaggy Sep 26 '17 at 14:47
1
\$\begingroup\$

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
\$\endgroup\$
  • 1
    \$\begingroup\$ I think you can replace ${i+1} with ${i++} and replace ${[...Array(i++)] with ${[...Array(i)]. \$\endgroup\$ – Conor O'Brien Feb 3 '16 at 19:55
  • \$\begingroup\$ @CᴏɴᴏʀO'Bʀɪᴇɴ. Thanks, I've updated with ++i \$\endgroup\$ – removed Feb 4 '16 at 0:14
1
\$\begingroup\$

Groovy, 97 bytes

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

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ You can user 100.times{} instead of (1..100).each{}, which saves 4 bytes. \$\endgroup\$ – Krzysztof Atłasik May 17 '16 at 9:13
  • \$\begingroup\$ You can also omit braces in join (join','), remove space after println, and replace .collect{} with *.with{} \$\endgroup\$ – Krzysztof Atłasik May 17 '16 at 9:31
1
\$\begingroup\$

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.

\$\endgroup\$
  • 2
    \$\begingroup\$ 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)) \$\endgroup\$ – Denker Feb 3 '16 at 9:01
  • 1
    \$\begingroup\$ Or use ',_'.join(map(str,r(i))) for an extra 14 bytes removal (or 15, since you left an extra space in your reduce). \$\endgroup\$ – Mathias Ettinger Feb 4 '16 at 14:07
1
\$\begingroup\$

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";
    }
}
\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Denker Feb 4 '16 at 15:22
  • \$\begingroup\$ You might also want to do j=1 in your for loop, thus not having the if(j). \$\endgroup\$ – Ethiraric Feb 5 '16 at 10:42
  • \$\begingroup\$ @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. \$\endgroup\$ – sendaran Feb 5 '16 at 10:59
  • \$\begingroup\$ @sendaran Or even may be for(int j=0;++j<=i;) would save one more byte. \$\endgroup\$ – Ethiraric Feb 5 '16 at 11:25
  • \$\begingroup\$ @Ethiraric Did that and shaved off an extra byte by extracting variable declarations, which enabled another empty expression. \$\endgroup\$ – sendaran Feb 5 '16 at 14:27

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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