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

74 Answers 74

24
\$\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\$
5
  • 1
    \$\begingroup\$ If you count Control-A as one keystroke, you could substitute yy by Y. \$\endgroup\$
    – seequ
    Feb 3, 2016 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, 2016 at 21:45
  • \$\begingroup\$ This seems to be going to class Tuple101, not class Tuple100 as the question asks. \$\endgroup\$ Sep 26, 2017 at 0:02
  • 1
    \$\begingroup\$ @numbermaniac Simple off-by-one error. Thanks for pointing that out! Fixed now :) \$\endgroup\$
    – DJMcMayhem
    Sep 26, 2017 at 14:16
  • \$\begingroup\$ @James your one line answer says 98@y but your explanation still has 99@y and talks about repeating 99 times. \$\endgroup\$ Dec 29, 2020 at 1:26
10
\$\begingroup\$

Jelly, 44 bytes

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

My first Jelly answer. Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ Am I doing it wrong or does this just output a giant list instead of a string? \$\endgroup\$
    – Denker
    Feb 3, 2016 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, 2016 at 20:04
  • \$\begingroup\$ Nice... as of now it's 9 bytes shorter than the CJam solution! \$\endgroup\$
    – Ray Toal
    Feb 5, 2016 at 3:56
9
\$\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\$
6
  • 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, 2016 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\$ Feb 3, 2016 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\$ Feb 3, 2016 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\$ Feb 4, 2016 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, 2016 at 2:10
8
\$\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\$
4
  • 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, 2016 at 6:12
  • 6
    \$\begingroup\$ @RayToal Yes, it is proper and even suggested to do so. \$\endgroup\$ Mar 22, 2016 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, 2017 at 23:58
  • \$\begingroup\$ @recursive The greek ISO-8859-7, this one: en.wikipedia.org/wiki/ISO/IEC_8859-7 \$\endgroup\$ Sep 26, 2017 at 0:00
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\$
6
  • \$\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, 2016 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, 2016 at 8:33
  • \$\begingroup\$ You;re welcome. By the way, you seem to have added a trailing " unnecessarily. \$\endgroup\$
    – isaacg
    Feb 3, 2016 at 9:02
  • 1
    \$\begingroup\$ @isaacg Is there any reason j<str><int> doesn't U? \$\endgroup\$
    – lirtosiast
    Feb 3, 2016 at 21:05
  • 1
    \$\begingroup\$ No, not really. Will do. \$\endgroup\$
    – isaacg
    Feb 3, 2016 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\$
7
  • \$\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\$ Feb 3, 2016 at 7:56
  • \$\begingroup\$ Does console.log" work? (Don't know coffee script, that's a general trick. \$\endgroup\$ Feb 3, 2016 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\$ Feb 3, 2016 at 8:16
  • \$\begingroup\$ @VoteToClose I tried that earlier (using the live transpiler on coffeescript.org and it didn't work). \$\endgroup\$ Feb 3, 2016 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, 2016 at 8:28
6
\$\begingroup\$

Powershell - 65 bytes

(Amended in response to comment)

All credit to TimmyD

1..100|%{"class Tuple$_ {public Object _$(0..($_-1)-Join",_");}"}
\$\endgroup\$
1
  • \$\begingroup\$ Why the vote down? (Please note this is my first ever post, not a great welcome?) \$\endgroup\$ Feb 3, 2016 at 13:27
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\$
2
  • \$\begingroup\$ please add some explanation \$\endgroup\$
    – Eumel
    Feb 3, 2016 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, 2016 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\$
8
  • 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\$ Feb 3, 2016 at 12:15
  • \$\begingroup\$ @DennisvanGils Sadly commands get echoed to standard output. \$\endgroup\$
    – Neil
    Feb 3, 2016 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, 2016 at 0:08
  • \$\begingroup\$ @Neil lol okay so I just tried running it and now I realize what you did. Very clever. \$\endgroup\$ Feb 4, 2016 at 4:13
  • \$\begingroup\$ At beginning, use @echo off \$\endgroup\$
    – Elliot A.
    Feb 6, 2016 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\$

Perl 6, 65 bytes

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

APL (Dyalog Unicode), 66 bytes

↑{∊'class Tuple'(⍕1+⍵)' {public Object _0'(',_'∘,∘⍕¨⍳⍵)';}'}¨0,⍳99

Try it online!

Explanation:

↑{∊'class Tuple'(⍕1+⍵)' {public Object _0'(',_'∘,∘⍕¨⍳⍵)';}'}¨0,⍳99
                                                             0,⍳99⍝ Numbers 0-99
                                                            ¨     ⍝ map to dfn
↑                                                                 ⍝ mix the result
  ∊'...'(⍕1+⍵)'...'                    ⍝ join the class header, tuple name (index + 1)
                                       ⍝ and the first tuple element.
                   (',_'∘,∘⍕¨⍳⍵)       ⍝ numbers from 1 to ⍵ prepended with ',_'
                                ';}'   ⍝ finish the class declaration
\$\endgroup\$
5
  • \$\begingroup\$ Tradfn(-2) \$\endgroup\$
    – Razetime
    Dec 20, 2020 at 13:46
  • \$\begingroup\$ 64 \$\endgroup\$
    – Adám
    Dec 28, 2020 at 10:41
  • \$\begingroup\$ @Calculuswhiz fair point, fixed. \$\endgroup\$ Dec 30, 2020 at 11:30
  • \$\begingroup\$ use the right ⎕io and save a byte: ↑{∊⍕¨'class Tuple'⍵' {public Object '(1↓∊',_'∘,¨⍕¨⍳⍵)';}'}¨1+⍳100 \$\endgroup\$
    – ngn
    Jan 5, 2021 at 21:54
  • \$\begingroup\$ even shorter: {∊⍕¨'class Tuple'⍺' {public Object '(1↓∊',_'∘,¨⍕¨⍳⍺)';}'}⌸1+⍳10 \$\endgroup\$
    – ngn
    Jan 5, 2021 at 21:59
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\$
4
  • 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, 2016 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, 2016 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, 2016 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, 2016 at 19:54
4
\$\begingroup\$

Ruby, 71 bytes

100.times{|i|puts"class Tuple#{i+1} {public Object _#{[*0..i]*',_'};}"}
\$\endgroup\$
1
  • 2
    \$\begingroup\$ TIL [*0..i]*',_' wow \$\endgroup\$
    – Ray Toal
    Feb 4, 2016 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\$
2
  • \$\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, 2016 at 15:15
  • \$\begingroup\$ @dieter The last line is a result. It's printed only when you run it interactively. \$\endgroup\$
    – pacholik
    Feb 3, 2016 at 18:03
3
\$\begingroup\$

PowerShell, 63 bytes

1..100|%{"class Tuple$_ {public Object _$(0..--$_-Join",_");}"}

Try it online!

Two byte improvement of bulletprooffool's answer, who seems to be an abandoned account now.

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

05AB1E, 43 bytes

тFNÝ„,_ý'_ì'Œº™'‚ÆN>'„à"ÿ Tupleÿ {ÿ ÿ ÿ;}"=

Try it online!

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

FALSE, 85 bytes

1[$100>~][$$0"class Tuple"\." {public Object "[$@$@=~][\$0>[","]?$"_".1+]#";}
"%%1+]#

Try it online!

Loops through 1-100 to get the Tuple number and then within that loops from 0 to currnum-1 to get underscores.

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

Vyxal, 36 bytes

`⟑↓ Tuɖ¾% {ƛḊ ß₂ %;}`₁ƛɾ\_$+\,j";v%⁋

Try it Online!

     ₁ƛ         ;    # map 1...100 to...
       ɾ             # 1...n
        \_$+         # Prepend underscores to each
            \,j      # Join by commas
               "     # Pair with n
`...`                # Compressed string `class Tuple% {public object %;}`
                 v%  # Formatted with (n, names)
                   ⁋ # Join by newlines
\$\endgroup\$
2
  • \$\begingroup\$ This beat the accepted answer. Is it proper on this site to change the accepted answer when it gets beaten? \$\endgroup\$
    – Ray Toal
    Aug 12, 2022 at 18:15
  • \$\begingroup\$ @RayToal Yes, or you can not accept any answer. Your choice. \$\endgroup\$
    – emanresu A
    Aug 12, 2022 at 19:24
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\$
3
  • \$\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, 2016 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, 2016 at 9:05
  • \$\begingroup\$ Same comment than I did to Dair: ',_'.join(map(str,range(i))) is 7 bytes shorter here. \$\endgroup\$ Feb 4, 2016 at 14:13
1
\$\begingroup\$

Mathematica, 130 Bytes

{"class Tuple",ToString[#]," {public Object ",StringReplace[ToString[#-1&/@Range@#],{" "|"{" ->"_","}"->";}\n"}]}&/@Range[100]<>""
\$\endgroup\$
1
  • \$\begingroup\$ Hint: \n can be replaced with a literal newline character. \$\endgroup\$ Feb 3, 2016 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\$
2
  • \$\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, 2016 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\$ Feb 4, 2016 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\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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