39
\$\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\$ – 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
  • 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 '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

71 Answers 71

1 2
3
0
\$\begingroup\$

MATL, 59 bytes

100:"'class Tuple%i {public Object %s}'@'_%i,'@:qYD59O(3$YD

EDITS

  • (May 16, 2016): to adapt to recent changes in the language, O needs to be replaced by J in the above code.
  • (September 29, 2017): the order of the inputs to YD has changed, which requires some modifications in the code.

The following link includes these changes.

Try it online!

\$\endgroup\$
0
\$\begingroup\$

C++, 139 137 bytes

-2 bytes thanks to Zacharý

#include<iostream>
auto&p=printf;void j(){int i=1,k;for(;i<101;++i){p("class T%d{",i);for(k=0;k<i;++k)p("public Object v%d;",k);p("}");}}
\$\endgroup\$
3
  • \$\begingroup\$ Woah, nice golf! If you're straying from the source to generate equivalent valid java code (using public Object v0;public Object v1; instead of public Object _0,_1;, removing the space before {) then can't you change p("}\n") to p("}")? That raises another question, are you allowed to stray from the source? \$\endgroup\$ – Adalynn Sep 30 '17 at 22:43
  • \$\begingroup\$ @Zacharý actually, the code outputed is class T2{public Object v0;public Object v1}, so... I may try to use some printf magic to erase a full loop \$\endgroup\$ – HatsuPointerKun Sep 30 '17 at 23:28
  • \$\begingroup\$ Secondly, why are using <iostream> instead of <cstdio> if you're just using printf? Unless I'm missing something, that may work. \$\endgroup\$ – Adalynn Oct 5 '17 at 16:49
0
\$\begingroup\$

ink, 87 bytes

-(i)class Tuple{i} \{public Object <>
~temp k=-1
-(j)~k++
_{k}{k<i:,->j};\}
{i<100:->i}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

C# (Visual C# Interactive Compiler), 110 109 bytes

int i;for(;i++<100;)Write($"class Tuple{i} {{public Object _{string.Join(",_",Enumerable.Range(0,i))};}}\n");

Saved a byte thanks to @dana

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ 109 - declare i at the top level ;) \$\endgroup\$ – dana Apr 3 '19 at 5:14
0
\$\begingroup\$

Google Sheets, 142

A
1 =SEQUENCE(1,100 - Named Range A
2 ;} - Named Range B
3 =ArrayFormula("class Tuple"&A&" {public Object "&SPLIT(REGEXREPLACE(TEXTJOIN(",",,IF(FLATTEN(A)>=A,"_"&A-1,)),",,+",B),B)&B

+1 for each named range.

Fun facts

Flatten is an undocumented function. Takes any matrix and makes it into a column vector. This includes row vectors too, so I used it instead of Transpose.

\$\endgroup\$
0
\$\begingroup\$

C (clang), 118 116 114 bytes

i,j,(*p)()=printf;main(){for(;j=i++<100&&p("class Tuple%d {public Object _0",i);p(";}\n"))for(;j<i;)p(",_%d",j++);}

Try it online!

Explanation

Note that I put a few statements into the for loop header, but the logic is the same.

Only works with clang. GCC errors on (*p)()=printf.

Ungolfed version:

// Define our loop counters....
int i, j;
// And our function pointer to printf (saves 4 bytes)
// Clang is generous enough to accept this monstrosity.
//
// Remember that f() in C represents an unspecified number of arguments,
// unlike C++. 
// This is why you should always compile with -Wstrict-prototypes. :)
int (*p)() = printf;

int main()
{
    // Static and globals default to 0, so we don't need i=0.
    // Loop from 1 to 100.
    while (i++ < 100) {
        // Print the beginning of the string with printf.
        // For ease, of implementation, we print the initial _0 so we can
        // just print ",_j" later.
        p("class Tuple%d {public Object _0", i);
        // Print all the members starting at _1.
        // Loop j for 1 to i - 1. We skip the first iteration.
        for (j = 1; j < i;)
            // Print ",_j"
            p(",_%d", j++);
        // Print the end of the class declaration.
        p(";}\n");
    }
}

Thanks to ceilingcat for the -2 bytes by using the first for loop condition to set j!

\$\endgroup\$
0
0
\$\begingroup\$

Perl 5, 69 bytes

$"=",_";say"class Tuple".(push@a,$_)." {public Object _@a};"for 0..99

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Pyth, 53 bytes

FNS*TTs["class Tuple"N" {public Object "j\,m+\_dUN";}

Try it online!


Pyth                                                   | Python 3
-------------------------------------------------------+-------------------------------------
FNS*TTs["class Tuple"N" {public Object "j\,m+\_dUN";}  | 
FNS*TTs["class Tuple"N" {public Object "j\,m+\_dUN";}" | 
-------------------------------------------------------+-------------------------------------
FNS*TT                                                 | for N in range(1, 101):
                                                       |     print(
      s[                                               |       "".join([
        "class Tuple"                                  |         "class Tuple",
                     N                                 |         str(N),
                      " {public Object "               |         " {public Object ",
                                        j\,            |         ",".join(
                                           m           |           map(
                                            +\_d       |             lambda d: "_" + str(d),
                                                UN     |             range(N)
                                                       |           )
                                                       |         ),
                                                  ";}" |         ";}"]
                                                       |       )
                                                       |     )
\$\endgroup\$
0
\$\begingroup\$

brainfuck, 881 781 bytes

--->-[<++>-----]>+[[-]--[----->+<]>---.+++++++++.-----------.--[---<+>]<--..+[---->+<]>+++.----[-<+++>]<.-[--->+<]>++++.-----.----.-------.[-]<<+[>>->+<<<-]>>>[<<<+>>>-]<[[+]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[>>+>+<<<-]>>>[<<<+>>>-]<[[-]+>--[<+>++++++]<.>-[<+>-----]<.[-]<[-]<[>+>+<<-]>>[<<+>>-]]<[>>+>+<<<-]>>[<<+>>-]>>+[[-]<[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[->[-]>>+>+<<<]]]]]]]]]<]>>[>]-[<+>-----]<--->>>]<<<[.[-]<<<]<<[>>+>-<<<-]>>+[<<+>>-]+>[<->[+]]<[+++[->++++++++<]>.>--[-->+++++<]>.[-<+++>]<-.+++++.+[->+++<]>.++++++++++.---.------.[-------<+++>]<<.>.-[----->+++<]>.++++++++.-----.--.-[---<+>]<--.<.>+[->+++<]>.+[--<+>]<.[-]<[-]<]<[-]<[>+>+<<-]>>[<<+>>-]<<<[>>->+<<<-]>>>[<<<+>>>-]<]+[--------->+<]>++.-[---<++>]<+.>++++++++++.[-]<[-]<[-]<<[>>+>+<<<-]>>>-[<<<+>>>-]<]

Try it online!

\$\endgroup\$
-1
\$\begingroup\$

Java 112 bytes

void p(){String x="_0";for(int i=0;i<100;){out.format("class Tuple %d {public Object %s;}\n",++i,x);x+=",_"+i;}}

Un-golfed

import static java.lang.System.*;

public class I {
public static void main(String[] args){
    I m=new I();
    m.p();
}
    void p(){
    String x="_0";
    for(int i=0;i<100;){
        out.format("class Tuple %d {public Object %s;}\n", ++i, x);
        x+=",_"+i;
    }
}

}

\$\endgroup\$
1
  • 2
    \$\begingroup\$ You need to import System or use System.out. \$\endgroup\$ – mbomb007 Feb 4 '16 at 21:48
-1
\$\begingroup\$

Haskell, 102 bytes

putStr$[0..99]>>= \a->"class Tuple"++show(a+1)++" {public Object _0"++([1..a]>>=(",_"++).show)++";}\n"

As a program (107 bytes):

main=putStr$[0..99]>>= \a->"class Tuple"++show(a+1)++" {public Object _0"++([1..a]>>=(",_"++).show)++";}\n"

Explanation

putStr$ -- print it all
  [0..99]>>= \a->                 -- For each a from 0 to 99, make the
                                  -- following string, then concat
                                  -- it all together

    "class Tuple" ++ show (a + 1) -- class name (starts at Tuple1)

    ++" {public Object _0"        -- All Tuple classes start with a _0

    ++([1..a]>>=(",_"++).show)    -- If we're on TupleA,
                                  -- concat together ",_1", ",_2", etc.
                                  -- up to "_A"

    ++";}\n"
\$\endgroup\$
1 2
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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