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
\$\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\$
2
  • 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\$
2
  • 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
  • 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\$ – 301_Moved_Permanently 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\$
5
  • \$\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
1
\$\begingroup\$

𝔼𝕊𝕄𝕚𝕟, 44 chars / 55 bytes

⩥ṥⓜᵖ`э£ēÕ>rĕĠ#⦃$⦄ {public Object ⦃⩤$ⓜ⍘_+$⦄;}

Try it here (Firefox only).

Still amazing even without compression.

Explanation

⩥ṥⓜᵖ`э£ēÕ>rĕĠ#⦃$⦄ {public Object ⦃⩤$ⓜ⍘_+$⦄;} // implicit: ṥ=input
⩥ṥⓜ                                          // generate [0,100) to map over
    ᵖ                                         // push to stack:
     `э£ēÕ>rĕĠ#⦃$⦄                             // class Tuple[insert mapitem here]
                  {public Object ⦃⩤$ⓜ⍘_+$⦄;} // {public Object [insert [0,mapitem] here preceded by semicolons per item];}
                                             // implicit stack output
\$\endgroup\$
1
\$\begingroup\$

JavaScript (ES6), 85 bytes

s='_0';for(i=1;i<101;s+=',_'+i++)console.log(`class Tuple${i} {public Object ${s};}`)
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Very nice! Could you add an explanation? Also, I believe you could save a single byte by moving s+=',_'+i++ into the last bit of the for. \$\endgroup\$ – Fund Monica's Lawsuit May 20 '16 at 1:25
  • \$\begingroup\$ Thanks! The for loop prints out each line whilst adding to the _0,_1,_2... string (stored in s) at the end of each iteration. Uses ES6 template strings to save a few bytes :P \$\endgroup\$ – dtkaias May 20 '16 at 5:21
  • \$\begingroup\$ (I meant edit an explanation into your question. Answers with them tend to be better-received) \$\endgroup\$ – Fund Monica's Lawsuit May 20 '16 at 14:18
1
\$\begingroup\$

Tcl, 107 bytes

time {set s "class Tuple[incr i] \{public Object \{"
set j 0
time {set s $s\_$j\;
incr j} $i
puts $s\}} 100

Try it online!

Tcl, 117 bytes

proc P n {set i 0;time {append s _$i\;;incr i} $n;set s}
time {puts "class Tuple[incr i] {public Object [P $i]}"} 100

Try it online!

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

Pip, 53 51 bytes

LhP["class Tuple"++i" {public Object "'_.,iJ',";}"]

Try it online!

Explanation:

                        h is preinitialized to 100 and i to 0 (implicit)
L h                     Loop 100 times:
 P [                    Print the following list (with trailing newline):
    "class Tuple"
    ++i                 Increment i here so that its values run from 1 to 100
    " {public Object "
    ('_ . ,i) J ',      Range(i), concatenate "_" before each element, join on ","
    ";}"
   ]                    By default, lists are concatenated when printed
\$\endgroup\$
2
  • \$\begingroup\$ Idea: Maybe name your language so that it doesn't get confused with the pip utility \$\endgroup\$ – LegionMammal978 Apr 24 '16 at 9:54
  • \$\begingroup\$ @LegionMammal978 Yeah, I realized that problem after the fact... but I think now it is what it is. (When I came up with the acronym "Pip Isn't Python," it seemed so perfect...) \$\endgroup\$ – DLosc Apr 25 '16 at 22:42
1
\$\begingroup\$

Forth (gforth), 97 bytes

: f 101 1 do cr ." class Tuple"i 1 .r ." {public Object _0"i 1 ?do ." ,_"i 1 .r loop ." ;}"loop ;

Try it online!

Code Explanation

: f                           \ start a new word definition
  101 1 do                    \ start a loop from 1 to 100
    cr                        \ output a new line
    ." class Tuple"i 1 .r     \ output "class TupleN" where N is the loop index
    ." {public Object _0"     \ output "{public Object _0"
    i 1 ?do                   \ start a loop from 1 to the outer loop index
      ." ,_"i 1 .r            \ output ",_N" where N is the inner loop index
    loop                      \ end the inner loop
    ," ;}"                    \ output ";}"
  loop                        \ end the outer loop
 ;                            \ end the word definition
\$\endgroup\$
1
\$\begingroup\$

Python 2, 93 bytes

i=1;exec'print"class Tuple%s {public Object _"%i+",_".join(map(str,range(i)))+";}";i+=1;'*100

Try it online!

I rarely get to use exec in code golf, so I wasn't going to pass on this one!

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

K (ngn/k), 67 bytes

{1@,/("class Tuple";$x;" {public Object _";",_"/$!x;";}\n")}'1+!100

Try it online!

A bunch of string concatenations, with ",_"/$!x probably being the most interesting part.

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

Python 2, 82 bytes

s="class Tuple%d {public Object _0"
i=1
exec"print s%i+';}';s+=',_%d'%i;i+=1;"*100

Try it online!

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

Perl 5, 69 bytes

$p='{public Object _0';say("class Tuple$_ $p;}"),$p.=",_$_"for 1..100

Try it online!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Shouldn't the semicolon come before the curly bracket? \$\endgroup\$ – RezNesX Jan 1 at 15:12
  • \$\begingroup\$ @RezNesX: Yes, thanks for the hint. \$\endgroup\$ – Donat Jan 1 at 15:18
0
\$\begingroup\$

Perl5, 77 Bytes

$"=", ";for(0..100){push@o,"_$_";print"class Tuple$_\{public Object @o;\}\n"}

Also

for(0..100){$o.=$o?", _$_":"_$_";print"class Tuple$_\{public Object $o;\}\n"}
\$\endgroup\$
3
  • \$\begingroup\$ Welcome to Programming Puzzles and Code Golf! Please put your language together with the byte count in the title and format the code correctly (see my edit). Hope your have a great time here! :) \$\endgroup\$ – Denker Feb 3 '16 at 8:37
  • \$\begingroup\$ You don't have to put a backslash for the first accolade in the print : print"class Tuple$_{public Object $o;\}\n"} \$\endgroup\$ – Paul Picard Feb 3 '16 at 16:22
  • \$\begingroup\$ You have an off-by-one error, the first line should be Tuple1 … _0;. The simple fix would be to put the push after the print and to make the print conditional on $_. …;for(0..100){$_&&print …;push@o,"_$_"} \$\endgroup\$ – Brad Gilbert b2gills Feb 3 '16 at 17:23
0
\$\begingroup\$

Java 1.8, 163 Bytes

interface T{static void main(String[]a){String o="";for(int i=0;i<100;){o+=(i>0?",":"")+"_"+i;System.out.println("class Tuple"+(++i)+" {public Object "+o+";}");}}}

Ungolfed

interface T{
static void main(String[]a){
    String o="";
    for(int i=0;i<100;){
        o+=(i>0?",":"")+"_"+i;
        System.out.println("class Tuple"+(++i)+" {public Object "+o+";}");
    }
}
}
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Unless stated otherwise in the challenge, you can always use functions instead of full prorgams. So youcan save all that static void main... stuff by using a Java8-lambda which implictly returns the result. \$\endgroup\$ – Denker Feb 3 '16 at 10:58
  • \$\begingroup\$ Old habits die hard ;) \$\endgroup\$ – Denham Coote Feb 3 '16 at 10:59
0
\$\begingroup\$

AWK, 84 Bytes

BEGIN{m=" {public Object _0";for(i=1;i<101;){print"class Tuple"i m";}";m=m",_"i++}}

I do like playing with AWK.

If the script were placed in a file, tuple.awk, and called with a 1 line input, the BEGIN tag could be removed... e.g. echo 1|awk -f tuple.awk

Not sure if any other golfing could be done to it.

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

Clojure, 103 chars

(map(fn[n](prn(str"class Tuple"n" {public Object "(join","(map #(+"_"%)(range n)))";}")))(range 1 101))
\$\endgroup\$
0
\$\begingroup\$

Common Lisp, 106 bytes

(let(y)(dotimes(x 100)(push x y)(format t"class Tuple~d {public Object ~{_~d~^,~};}~&"(1+ x)(reverse y))))

Try it online!

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

Java 8, 184 194 bytes

(just to add void r() as mentioned in the comments)

void r(){IntStream.rangeClosed(1,100).mapToObj(i->IntStream.range(0,i).mapToObj(j->"_"+j).collect(Collectors.joining(",","class Tuple"+i+" {public Object ",";}"))).forEach(System.out::println);}

Ungolfed

void r(){ IntStream.rangeClosed(1, 100)
    .mapToObj(i -> IntStream.range(0, i).mapToObj(j -> "_" + j)
        .collect(Collectors.joining(",", "class Tuple" + i + " {public Object ", ";}")))
    .forEach(System.out::println); }
\$\endgroup\$
4
  • \$\begingroup\$ This code isn't even in a function or anything... You need to add like void r(){ around it } \$\endgroup\$ – Ashwin Gupta Feb 4 '16 at 4:02
  • \$\begingroup\$ Np. Why is your "," seperate from your "_" if the are right next to each other when you print? You may be able to cut some bytes by using this string: ",_" \$\endgroup\$ – Ashwin Gupta Feb 4 '16 at 4:43
  • \$\begingroup\$ @AshwinGupta "," is used as a delimiter for a Stream<String> of _0 _1 _2.... If I'm going to used ",_" as the delimiter, the mapToObj() conversion is longer (String::valueOf), and I'll need an additional _ at the end of " {public Object " to handle that. \$\endgroup\$ – h.j.k. Feb 4 '16 at 5:35
  • 1
    \$\begingroup\$ oh okay I didn't know that. I'm a begginer programmer really. I just noticed. \$\endgroup\$ – Ashwin Gupta Feb 4 '16 at 5:36
0
\$\begingroup\$

Java, 166 Bytes

void m(){int c=0;while(c!=100){System.out.print("class Tuple"+(c+1)+" {public Object ");for(int i=0;i<c+1;i++)System.out.print(",_"+i);System.out.print(";}\n");c++;}}

Not bad for java, I mean calling main() alone is like 10+ bytes.. (plus long print statements)

(Non-Competetive) Java w/ Quick-Print! 137 bytes

import t.Q;
void m(){int c=0;while(c!=100){Q.p("class Tuple"+(c+1)+" {public Object ");for(int i=0;i<c+1;i++)Q.p(",_"+i);Q.p(";}\n");c++;}}

This one uses a quick library I have been thinking about making for a while(and just made) for shortening the long java print statements. It saves 29 bytes and that was just with 3 system.out.print() calls in the original. I've marked this one as non-competetive since the lib was made after the challenge was posted. Also, I don't know if we are allowed to use cheap little tricks like this and I felt bad about it. (Although in a way its fair since java has the longest print statements of any language I know).

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

Brachylog, 99 bytes

0:99eI+1=J,[J]:"class Tuple~w {public Object "w,(I{:0reI,(?I,F";";?'I,F","):"w~_"cr:[I]rw\};"}
"w\)
\$\endgroup\$
0
\$\begingroup\$

Scala, 119 bytes

I'm new to Scala and to codegolf, so nothing fancy.

for(i<-1 to 100){print("class Tuple"+i+" {public Object "); for(j<-0 to i-2) print("_"+j+","); print("_"+(i-1)+";}\n")}

Try online

More readable version:

for (i <- 1 to 100) {
  print("class Tuple" + i + " {public Object ")
  for (j <- 0 to i - 2) print("_" + j + ",")
  print("_" + (i - 1) + ";}\n")
}
\$\endgroup\$
0
\$\begingroup\$

Haskell, 131 bytes

Call j.

import Data.List
j=mapM_(\n->putStrLn("class Tuple"++show n++" {public Object _"++intercalate",_"(map show[0..n-1])++";}"))[1..100]
\$\endgroup\$
0
\$\begingroup\$

BlitzMax, 81 bytes

While i<100s$:+",_"+i i:+1Print"class Tuple"+i+" {public Object "+s[1..]+";}"Wend

That's the whole program. Non-Strict and without a Framework, so I can use variables without declaration and Print without having to import StandardIO. The String slicing operator comes in handy here, since it allows me to easily remove the superfluous comma at the beginning of each Object list.

Ungolfed version

While i < 100
    s$ :+ ",_" + i
    i :+ 1
    Print "class Tuple" + i + " {public Object " + s[1..] + ";}"
Wend
\$\endgroup\$
0
\$\begingroup\$

Python 3, 96 bytes

r=range
for i in r(1,101):print('class Tuple%d {public Object _0'%i,*r(1,i),sep=',_',end=';}\n')

Joining its expanded positional arguments from *range(1,i) using a custom separator ,_ and ending each line with ;}, Python 3's print() function happily does the job almost entirely on its own.

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

jq, 80 characters

(78 characters code + 2 characters command line option.)

range(1;13)|"class Tuple\(.) {public Object \([range(.)|"_\(.)"]|join(","));}"

Sample run:

bash-4.3$ bin/jq -nr 'range(1;101)|"class Tuple\(.) {public Object \([range(.)|"_\(.)"]|join(","));}"' | head
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;}

On-line test (Passing -r through URL is not supported – check Raw Output yourself.)

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

Javascript ES6, 128 122 chars

for(s="class Tuple1 {public Object _0;}",q=0;q++<100;s=s.replace(/\d+(?!\d|,)( )?/g,(m,s)=>[q+1+s][+s]||m+",_"+q))alert(s)
\$\endgroup\$
0
\$\begingroup\$

Kotlin, 92 Bytes

fun j(){var s="_0";for(i in 1..100){println("class Tuple$i {public Object $s;}");s+=",_"+i}}

Try it online!

Ungolfed

fun j(){
    var s = "_0"
    for(i in 1..100){
        println("class Tuple$i {public Object $s;}")
        s+=",_"+i
    }   
}
\$\endgroup\$
0
\$\begingroup\$

Perl 5, 68 bytes

$"=',_';@d=0..$_-1,say"class Tuple$_ {public Object _@d;}"for 1..100

Try it online!

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

V, 47 bytes

iclass Tuple1 {public Object _0;}99ñÙ$hi,_Û

Try it online!

\$\endgroup\$

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.