14
\$\begingroup\$

Your boss wants you to write code like this:

public static boolean isPowerOfTen(long input) {
  return
    input == 1L
  || input == 10L
  || input == 100L
  || input == 1000L
  || input == 10000L
  || input == 100000L
  || input == 1000000L
  || input == 10000000L
  || input == 100000000L
  || input == 1000000000L
  || input == 10000000000L
  || input == 100000000000L
  || input == 1000000000000L
  || input == 10000000000000L
  || input == 100000000000000L
  || input == 1000000000000000L
  || input == 10000000000000000L
  || input == 100000000000000000L
  || input == 1000000000000000000L;
}

(Martin Smith, at https://codereview.stackexchange.com/a/117294/61929)

which is efficient and so, but not that fun to type. Since you want to minimize the number of keypresses you have to do, you write a shorter program or function (or method) that outputs this function for you (or returns a string to output). And since you have your very own custom full-range unicode keyboard with all 120,737 keys required for all of unicode 8.0, we count unicode characters, instead of keypresses. Or bytes, if your language doesn't use unicode source code.

Any input your program or function takes counts towards your score, since you obviously have to type that in as well.

Clarifications and edits:

  • Removed 3 trailing spaces after the last }
  • Removed a single trailing space after return
  • Returning a string of output from a function/method is ok
\$\endgroup\$
16
  • 13
    \$\begingroup\$ 0==Math.log10(input)%1 \$\endgroup\$ Feb 3, 2016 at 11:15
  • 7
    \$\begingroup\$ You say "we count unicode characters," but then you immediately say "Or bytes." Which one is it? \$\endgroup\$
    – Doorknob
    Feb 3, 2016 at 11:55
  • 2
    \$\begingroup\$ Whichever you prefer, i.e. the one that gives you the lowest score. Added bytes to allow languages that don't use text source. \$\endgroup\$ Feb 3, 2016 at 13:15
  • 1
    \$\begingroup\$ while(input%10==0) input/=10; return input == 1; \$\endgroup\$ Feb 3, 2016 at 14:20
  • 5
    \$\begingroup\$ 05AB1E uses windows CP1252, which is bytes, not unicode. I'm aiming for standard rules, but I get told I'm wrong all the time. \$\endgroup\$ Feb 3, 2016 at 19:32

32 Answers 32

15
\$\begingroup\$

PostgreSQL, 158 characters

select'public static boolean isPowerOfTen(long input) {
  return
   '||string_agg(' input == 1'||repeat('0',x)||'L','
  ||')||';
}'from generate_series(0,18)x
\$\endgroup\$
3
  • \$\begingroup\$ I have never seen an RDBMS used as a code golf answer... sweet! +1 \$\endgroup\$ Feb 4, 2016 at 3:07
  • \$\begingroup\$ @ChrisCirefice SQL is actually somewhat common on this site. (Or at least more common than one might expect.) \$\endgroup\$
    – Alex A.
    Feb 4, 2016 at 5:51
  • \$\begingroup\$ @AlexA. Hm, well PCG is one of my less-frequented SE sites, so I've never seen a SQL answer :) \$\endgroup\$ Feb 4, 2016 at 13:09
8
\$\begingroup\$

CJam, 52 characters

YA#_("𐀑򀺸󆚜񸎟񜏓񞍁򛟯󩥰󾐚򉴍􍼯𹾚򶗜򳙯󭧐񹷜񊽅𸏘򴂃򦗩󧥮𤠐𰑈򶂤𘏧󔆧򇃫󡀽򊠑񊩭򯐙񛌲񊚩𤱶𻺢"f&bY7#b:c~

Try it online!

Stage 1

Using Unicode characters U+10000 to U+10FFFF, we can encode 20 bits in a single character. CJam uses 16-bit characters internally, so each one will be encoded as a pair of surrogates, one in the range from U+D800 to U+DBFF, followed by one in the range from U+DC00 to U+DFFF.

By taking the bitwise AND of each surrogate with 1023, we obtain the 10 bits of information it encodes. We can convert the resulting array from base 1024 to base 128 to decode an arbitrary string of Unicode characters outside the BMP to an ASCII string.

The code does the following:

YA#    e# Push 1024 as 2 ** 10.
_(     e# Copy and decrement to push 1023.

"𑅰󻢶񹱨񉽌񍍎񄆋򎿙򧃮񑩹󠷽􂼩􉪦񭲣񶿝򭁩󭰺􄔨񍢤𘎖񮧗򦹀𹀠񐢑񜅈𠟏򘍎󾇗򲁺􅀢򅌛񎠲򦙤򃅒𹣬񧵀򑀢"

f&     e# Apply bitwise AND with 1023 to each surrogate character.
b      e# Convert the string from base 1024 to integer.
Y7#    e# Push 128 as 2 ** 7.
b      e# Convert the integer to base 128.
:c     e# Cast each base-128 to an ASCII character.
~      e# Evaluate the resulting string.

Stage 2

The decoding process from above yields the following source code (98 bytes).

"public static boolean isPowerOfTen(long input) {
  return
   ""L
  || input == ":S6>AJ,f#S*"L;
}"

Try it online!

The code does the following:

e# Push the following string.

"public static boolean isPowerOfTen(long input) {
  return
   "

e# Push the following string and save it in S.

"L
  || input == ":S

e# Discard the first 6 characters of S. The new string begins with " input".

6>

e# Elevate 10 (A) to each exponent below 19 (J).

AJ,f#

e# Join the resulting array, using the string L as separator.

S*

e# Push the following string.

"L;
}"
\$\endgroup\$
3
  • \$\begingroup\$ You might expect a SE site like Judaism to be the ones stress-testing the site's unicode support, but this is crazy :D \$\endgroup\$ Feb 6, 2016 at 10:37
  • \$\begingroup\$ I can see exactly two of the characters between the quote marks. Can you post a hexdump? \$\endgroup\$
    – Pavel
    Jan 6, 2017 at 17:02
  • \$\begingroup\$ You can actually see two of them? I have no such luck... Encoding the characters as UTF-8, the hexdump would look like this. tio.run/nexus/bash#AagAV///eHhkIC1nIDH//… \$\endgroup\$
    – Dennis
    Jan 6, 2017 at 17:13
7
\$\begingroup\$

Vim 97 keystrokes

ipublic static boolean isPowerOfTen(long input) {
  return
  || input == 1L<esc>qyYpfLi0<esc>q16@yo}<esc>3Gxx

Well, I'm on a roll today with vim producing java, so why not continue the trend!

\$\endgroup\$
3
  • \$\begingroup\$ replacing fL with $ could save you a keystroke \$\endgroup\$
    – Leaky Nun
    May 29, 2016 at 17:24
  • \$\begingroup\$ Also, the third line input == 1L is misaligned by one byte... \$\endgroup\$
    – Leaky Nun
    May 29, 2016 at 17:25
  • \$\begingroup\$ So the last x should be changed to r<sp> and then the number of keystrokes would be unchanged \$\endgroup\$
    – Leaky Nun
    May 29, 2016 at 17:26
7
\$\begingroup\$

Java, 217 215 220 219 192 bytes

Golfed:

public static String b(){String s="public static boolean isPowerOfTen(long input) {\n  return\n    input == 1L",z="";for(int i=0;i++<18;){z+="0";s+="\n  || input == 1"+z+"L";}return s+";\n}";}

Ungolfed:

  public static String a(){
    String s = "public static boolean isPowerOfTen(long input) {\n  return\n    input == 1L", z="";
    for (int i=0; i++ < 18;) {
        z += "0";
        s += "\n  || input == 1"+z+"L";
    }
    return s + ";\n}";
  }

(first answer, wuhu)

Thanks!
-2 bytes: user902383
-1 byte: Denham Coote

Changes:

  • used tabs instead of spaces
  • missed the last line of output: 18 -> 19
  • removed inner loop
  • changed from printing to returning string
\$\endgroup\$
5
  • 4
    \$\begingroup\$ your inner for loop does not need brackets \$\endgroup\$
    – user902383
    Feb 3, 2016 at 16:59
  • \$\begingroup\$ Use Java 8 syntax, also shortened some other stuff: ()->{String s="public static boolean isPowerOfTen(long input) {\n\treturn input == 1L";for(int i=0,k;i++<18;){s+="\n\t|| input == 1";for(k=0;k++<i;)s+="0";s+="L";}return s+";\n}";} (180 bytes) Now returns the string instead of printing, but that's shorter. \$\endgroup\$ Feb 3, 2016 at 19:47
  • 1
    \$\begingroup\$ +1 for writing a verbose Java program to generate an even more verbose Java program. \$\endgroup\$
    – Cyoce
    Feb 5, 2016 at 16:59
  • \$\begingroup\$ instead of for(int i=1;i<19;i++) you can write for(int i=1;i++<19;) which saves a byte \$\endgroup\$ Feb 5, 2016 at 17:23
  • \$\begingroup\$ Also, declare int i=1,k; and then you can write for(;i++<19;) and for(k=0;k++<i;) \$\endgroup\$ Feb 5, 2016 at 17:24
7
\$\begingroup\$

05AB1E, 99 97 96 94 93 87 bytes

Code:

“‚Æ£‹ÒŒ€ˆPowerOfTen(“?“¢„î®) {
 «‡
   “?19FN0›i"  ||"?}’ î® == ’?N°?'L?N18Qi';,"}"?}"",

Try it online!

Uses CP-1252 encoding.

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

PowerShell, 120 bytes

'public static boolean isPowerOfTen(long input) {'
'  return'
"   $((0..18|%{" input == 1"+"0"*$_})-join"L`n  ||")L;`n}"

The first two lines are simply string literals, which are output as-is.

The third line starts with three spaces, and ends with L;`n}" to finish off the last couple bytes. The middle bit inside the script block $(...) is constructed by for-looping % from 0 to 18 and each iteration constructing a string that starts with input == 1 concatenated with the corresponding number of zeros. This will spit out an array of strings. We then -join each element of the array with L`n || to achieve the newline-pipes. That big string is the output of the script block, which gets inserted automatically into the middle and output.

PS C:\Tools\Scripts\golfing> .\go-generate-some-java.ps1
public static boolean isPowerOfTen(long input) {
  return
    input == 1L
  || input == 10L
  || input == 100L
  || input == 1000L
  || input == 10000L
  || input == 100000L
  || input == 1000000L
  || input == 10000000L
  || input == 100000000L
  || input == 1000000000L
  || input == 10000000000L
  || input == 100000000000L
  || input == 1000000000000L
  || input == 10000000000000L
  || input == 100000000000000L
  || input == 1000000000000000L
  || input == 10000000000000000L
  || input == 100000000000000000L
  || input == 1000000000000000000L;
}
\$\endgroup\$
1
  • \$\begingroup\$ A long time ago... :) Very impressive! \$\endgroup\$
    – mazzy
    Feb 17, 2020 at 14:48
4
\$\begingroup\$

Pyth, 118 106 103 bytes

s[."
{Z-L¡JxÙÿ
LæÝ<­í?¢µb'¥ÜA«Ç}h¹äÚÏß"\nb*4dj"\n  || "ms[." uøs|ÀiÝ"*d\0\L)U19\;b\}

Try it online!

All this string hardcoding really eats a lot of bytes up, but nothing I can do about it.

Update: Saved 3 bytes by using a packed string. Thanks @user81655 for the hint!

\$\endgroup\$
5
  • \$\begingroup\$ You could use packed strings... \$\endgroup\$
    – user81655
    Feb 3, 2016 at 12:44
  • \$\begingroup\$ I don't know Pyth and I'm not sure if there's a way to pack the full string (the packing program would always alter it) but packing up to r and concatenating the n results in this (98 bytes). \$\endgroup\$
    – user81655
    Feb 3, 2016 at 13:03
  • \$\begingroup\$ @user81655 Thanks, didn't know Pyth had this. :) It only makes sense to pack the first big string tho, the overhead you produce form unpacking is not worth it for smaller string. \$\endgroup\$
    – Denker
    Feb 3, 2016 at 13:58
  • \$\begingroup\$ @user81655 I am aware of that, but I count 103 characters. How did you get to 97? \$\endgroup\$
    – Denker
    Feb 4, 2016 at 9:01
  • \$\begingroup\$ Oops, my mistake, I was counting them wrong. \$\endgroup\$
    – user81655
    Feb 4, 2016 at 9:08
4
\$\begingroup\$

C# (CSI) 181 180 179 byte

string i=" input == 1",e="public static bool";Console.Write(e+@"ean isPowerOfTen(long input) {
  return
   "+i+string.Join(@"L
  ||"+i,e.Select((_,x)=>new string('0',x)))+@"L;
}")

There is only one little trick involved. The straight forward way to write this would be:

string.Join("L\n  || input == 1",Enumerable.Range(0,18).Select(x=>new string('0',x)))

by using the string with the first 18 characters of the text which I need anyways I can get rid off the lengthy Enumerable.Range. This works because string implements IEnumerable and there is a version of Select that hands the item (not needed) and the index which we want to the lambda function.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ @WashingtonGuedes Thanks \$\endgroup\$
    – raggy
    Feb 3, 2016 at 12:51
  • 1
    \$\begingroup\$ add some explanation please \$\endgroup\$
    – Eumel
    Feb 3, 2016 at 13:31
  • 1
    \$\begingroup\$ Does CSI support expression bodies? If so, the { return ... } can be replaced by =>.... \$\endgroup\$
    – user42643
    Feb 3, 2016 at 13:42
  • \$\begingroup\$ Not currently on computer so I can't test this. Does the last verbatim string escape the bracket inside it? Or is it a great trick that I didn't know of? :) \$\endgroup\$
    – Yytsi
    Feb 13, 2016 at 12:48
3
\$\begingroup\$

Javascript, 172 157 152 150 148 bytes

p=>`public static boolean isPowerOfTen(long input) {
  return${[...Array(19)].map((x,i)=>`
  ${i?'||':' '} input == 1${'0'.repeat(i)}L`).join``};
}`

f=p=>`public static boolean isPowerOfTen(long input) {
  return${[...Array(19)].map((x,i)=>`
  ${i?'||':' '} input == 1${'0'.repeat(i)}L`).join``};
}`

document.body.innerHTML = '<pre>' + f() + '</pre>'

\$\endgroup\$
1
  • 2
    \$\begingroup\$ In ES7 you can save 9 bytes by using ${10**i} instead of 1${'0'.repeat(i)}. \$\endgroup\$
    – Neil
    Feb 3, 2016 at 21:56
3
\$\begingroup\$

C, 158 155 bytes

i;main(){for(puts("public static boolean isPowerOfTen(long input) {\n  return");i<19;)printf("  %s input == 1%0.*dL%s\n",i++?"||":" ",i,0,i<18?"":";\n}");}

Try it online here.

\$\endgroup\$
1
  • \$\begingroup\$ You can shave off a byte if you use the return value of printf: i;main(){for(puts("public static boolean isPowerOfTen(long input) {\n return");printf(" %s input == 1%0.*dL%s\n",i++?"||":" ",i,0,i<18?"":";\n}")-37);} \$\endgroup\$
    – algmyr
    Jul 16, 2016 at 21:03
3
\$\begingroup\$

Jelly, 75 bytes

(These are bytes in Jelly's custom codepage.)

0r18⁵*;@€⁶j“¢œḤḅg^NrÞḢ⁷ẉ»“⁵®UẆƓḃÐL⁴ṖịṛFþẈ¹9}¶ ƁḋȮ¦sẒẆd€Ḟɼ¿ỌṀP^µ\f@»;;“L;¶}”

Try it here.

Explanation

0r18      Range [0..18]
⁵*        Take the 10^ of each number
;@€⁶      Prepend a space to each number
j“...»    Join by compressed string "L\n  || input =="
“...»;    Prepend compressed string "public static ... =="
;“L;¶}”   Append "L;\n}"
\$\endgroup\$
3
\$\begingroup\$

Vimscript, 120 bytes

Might as well use the right tool for the job.

This assumes that autoindent, etc have not been set. ^[ and ^M are escape characters for the ESC and CR characters respectively.

The a macro duplicates the current line and adds a 0 to the copy. The :norm line generates all the boilerplate and the indent == 1L line, then uses a to create the others.

:let @a='yyp$i0^['
:norm ipublic static boolean isPowerOfTen(long input) {^M  return^M  || input == 1L^[18@a$a;^M}
:3s/||/ /

In case the trailing spaces the sample output had on two lines weren't typos, here's a 126 byte version that includes them.

:let @a='yyp/L^Mi0^['
:norm ipublic static boolean isPowerOfTen(long input) {^M  return ^M  || input == 1L^[18@a$a;^M}   
:3s/||/ /
\$\endgroup\$
3
\$\begingroup\$

ES6, 139 138 bytes

document.write(`<pre>${(

_=>"0".repeat(19).replace(/./g,`
 || input == 1$\`L`).replace(`
 ||`,`public static boolean isPowerOfTen(long input) {
  return
  `)+`;
}`

)()}</pre>`);

I do so love these triangle generation questions. Note: Snippet includes header and footer, but these aren't included in the byte count.

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

Oracle SQL 9.2, 311 bytes

SELECT REPLACE(REPLACE('public static boolean isPowerOfTen(long input) {'||CHR(10)||'  return'||c||';'||'}', 'n  ||', 'n'||CHR(10)||'   '),CHR(10)||';', ';'||CHR(10)) FROM(SELECT LEVEL l,SYS_CONNECT_BY_PATH('input == '||TO_CHAR(POWER(10,LEVEL-1))||'L'||CHR(10),'  || ')c FROM DUAL CONNECT BY LEVEL<20)WHERE l=19
\$\endgroup\$
2
\$\begingroup\$

Perl 5 - 130 141

@s=map{'input == 1'.0 x$_."L\n  ||"}0..18;$s[$#s]=~s/\n  \|\|/;\n}/g;print"public static boolean isPowerOfTen(long input){\n  return\n    @s"

EDIT: fixed to have exact indentation

\$\endgroup\$
4
  • \$\begingroup\$ No need to use parenthesis around the range. In change would be nice to reproduce the exact indentation. \$\endgroup\$
    – manatwork
    Feb 3, 2016 at 15:37
  • \$\begingroup\$ Thanks for the parenthesis that I forgot. I've fixed it to have the exact indentation. \$\endgroup\$
    – ChatterOne
    Feb 3, 2016 at 16:17
  • \$\begingroup\$ There is no need for the g flag for the substitution. Also as you have a single \n in that string, you can simply match it and everything after it: $s[$#s]=~s/\n.+/;\n}/. But a join based one would still be shorter: pastebin.com/hQ61Adt8 \$\endgroup\$
    – manatwork
    Feb 3, 2016 at 16:38
  • \$\begingroup\$ Thank you, but I don't think it would be nice if I just copied and pasted your solution, so I'll just leave it as it is as my own best effort. In time, I'll get better at golfing :-) \$\endgroup\$
    – ChatterOne
    Feb 3, 2016 at 19:49
2
\$\begingroup\$

Kotlin, 194 193 characters

fun main(u:Array<String>){var o="public static boolean isPowerOfTen(long input) {\n\treturn"
var p:Long=1
for(k in 0..18){
o+="\n\t"
if(k>0)o+="||"
o+=" input == ${p}L"
p*=10
}
print("$o;\n}")}

Test it at http://try.kotlinlang.org/

\$\endgroup\$
1
  • \$\begingroup\$ Welcome to Programming Puzzles & Code Golf. Nice first answer, but please add a link to an online interpreter or add an example on how to run this program, so others can verify it. However, have a great time here! :) \$\endgroup\$
    – Denker
    Feb 4, 2016 at 9:27
2
\$\begingroup\$

Ruby, 125 119 bytes

$><<'public static boolean isPowerOfTen(long input) {
  return
   '+(0..19).map{|i|" input == #{10**i}L"}*'
  ||'+';
}'

Thanks to manatwork for -6 bytes!

\$\endgroup\$
1
  • \$\begingroup\$ Not much original as most of the solutions are doing this way, but still shorter: pastebin.com/1ZGF0QTs \$\endgroup\$
    – manatwork
    Feb 3, 2016 at 16:43
2
\$\begingroup\$

jq, 123 characters

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

"public static boolean isPowerOfTen(long input) {
  return
   \([range(19)|" input == 1\("0"*.//"")L"]|join("
  ||"));
}"

Sample run:

bash-4.3$ jq -nr '"public static boolean isPowerOfTen(long input) {
>   return
>    \([range(19)|" input == 1\("0"*.//"")L"]|join("
>   ||"));
> }"'
public static boolean isPowerOfTen(long input) {
  return
    input == 1L
  || input == 10L
  || input == 100L
  || input == 1000L
  || input == 10000L
  || input == 100000L
  || input == 1000000L
  || input == 10000000L
  || input == 100000000L
  || input == 1000000000L
  || input == 10000000000L
  || input == 100000000000L
  || input == 1000000000000L
  || input == 10000000000000L
  || input == 100000000000000L
  || input == 1000000000000000L
  || input == 10000000000000000L
  || input == 100000000000000000L
  || input == 1000000000000000000L;
}

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

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

Javascript 175 bytes

Let's do this regularly

var s = "public static boolean isPowerOfTen(long input) {\n\treturn\n\t\tinput == 1";
for (var i = 1; i < 20; i++) {
    s += "\n\t|| input == 1";
    for (var j = 0; j < i; j++) {
        s += "0";
    }
    s += "L" ;
}
s += ";\n}";
alert(s);

Pretty small. Now, some javascript magic, like no semicolons needed, or no var's, etc.:

k="input == 1"
s="public static boolean isPowerOfTen(long input) {\n\treturn\n\t\t"+k+"L"
for(i=1;i<20;i++){s+="\n\t|| "+k
for(j=0;j<i;j++)s+="0";s+="L"}s+=";\n}"
alert(s)
\$\endgroup\$
5
  • \$\begingroup\$ Can you explain how that magic works? :) \$\endgroup\$
    – Marv
    Feb 3, 2016 at 14:43
  • 3
    \$\begingroup\$ Javascript doesn't care about semicolons, at least until the keywords (for, while, var, etc.) aren't "touching" anything else. Also, if you don't use the var keyword, you get global variables, wich is the worst feature I have ever seen in a programming language thus far. \$\endgroup\$
    – Bálint
    Feb 3, 2016 at 15:00
  • \$\begingroup\$ @Bálint. Why this would be the worst feature? \$\endgroup\$
    – removed
    Feb 3, 2016 at 15:12
  • \$\begingroup\$ @WashingtonGuedes You know, most languages remind you if you misstyped something inside a function. Because javascript takes that as if you made a whole new variable, it does not going to say anything about that. \$\endgroup\$
    – Bálint
    Feb 3, 2016 at 15:24
  • \$\begingroup\$ Also, same one applies to = returning a true. \$\endgroup\$
    – Bálint
    Feb 3, 2016 at 15:27
1
\$\begingroup\$

Python (3.5) 137 136 bytes

print("public static boolean isPowerOfTen(long input) {\n  return\n   ",'\n  || '.join("input == %rL"%10**i for i in range(19))+";\n}")

Previous version

print("public static boolean isPowerOfTen(long input) {\n  return\n   ",'\n  || '.join("input == 1"+"0"*i+"L"for i in range(19))+";\n}")
\$\endgroup\$
2
  • \$\begingroup\$ 135 with Python 2.7: print "public static boolean isPowerOfTen(long input) {\n return\n %s;\n}"%"\n || ".join("input == %r"%10L**i for i in range(19)) \$\endgroup\$
    – moooeeeep
    Feb 3, 2016 at 19:52
  • \$\begingroup\$ @moooeeeep you're right, the use of %r win 1 bytes and the python 2 print (without parenthesis ) win another one \$\endgroup\$
    – Erwan
    Feb 4, 2016 at 7:27
0
\$\begingroup\$

ANSI-SQL, 252 characters

WITH t as(SELECT '   'x,1 c,1 l UNION SELECT'  ||',c*10,l+1 FROM t WHERE l<19)SELECT 'public static boolean isPowerOfTen(long input) {'UNION ALL SELECT'  return 'UNION ALL SELECT x||' input == '||c||'L'||SUBSTR(';',1,l/19)FROM t UNION ALL SELECT'}   ';

Ungolfed:

WITH t as (SELECT '   ' x,1 c,1 l UNION
           SELECT '  ||',c*10,l+1 FROM t WHERE l<19)
SELECT 'public static boolean isPowerOfTen(long input) {' UNION ALL
SELECT '  return ' UNION ALL
SELECT x||' input == '||c||'L'||SUBSTR(';',1,l/19) FROM t UNION ALL
SELECT '}   ';

Not a serious attempt, just poking at the Oracle SQL/T-SQL entries.

\$\endgroup\$
1
  • \$\begingroup\$ For 40 additional chars I can add "from dual " and make it an "Oracle SQL" entry. \$\endgroup\$
    – DKATyler
    Feb 4, 2016 at 3:39
0
\$\begingroup\$

JavaScript (Node.js), 156 bytes

s="public static boolean isPowerOfTen(long input) {\n  return "
for(i=1;i<1e19;i*=10)s+="\n  "+(i-1?"||":" ")+" input == "+i+"L"
console.log(s+";\n}   \n")

The i-1 will only be 0 (and thus falsey) on the very first round (it's just slightly shorter than i!=1.

Suggestions welcome!

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

Perl 5, 137 bytes

Not based on the previous Perl answer, but it is somehow shorter. I believe it can be shortened down again by taking care of the first "input" inside the loop, but I didn't try anything yet (at work atm)

$i="input";for(1..18){$b.="  || $i == 1"."0"x$_."L;\n"}print"public static boolean isPowerOfTen(long $i) {\n  return\n    $i == 1L;\n$b}"
\$\endgroup\$
0
\$\begingroup\$

CJam, 112 chars

"public static boolean isPowerOfTen(long input) {
  return
    input == 1"19,"0"a19*.*"L
  || input == 1"*"L;
}"
\$\endgroup\$
0
\$\begingroup\$

AWK+shell, 157 bytes

echo 18|awk '{s="input == 1";printf"public static boolean isPowerOfTen(long input) {\n return\n    "s"L";for(;I<$1;I++)printf"\n  ||%sL",s=s"0";print";\n}"}'

The question did say to count everything you would have to type. This does have the added bonus of being able to select how many lines would be placed in the isPowersOfTen method when the boss inevitably changes his mind.

\$\endgroup\$
2
  • \$\begingroup\$ Passing a here-string is shorter than piping from echo: awk '…'<<<18 \$\endgroup\$
    – manatwork
    Feb 4, 2016 at 16:15
  • \$\begingroup\$ I knew about here-file but not here-string. Thanks for the info. \$\endgroup\$ Feb 4, 2016 at 19:30
0
\$\begingroup\$

T-SQL 289, 277, 250, 249 bytes

SELECT'public static boolean isPowerOfTen(long input){return '+STUFF((SELECT'||input=='+N+'L 'FROM(SELECT TOP 19 FORMAT(POWER(10.0,ROW_NUMBER()OVER(ORDER BY id)),'F0')N FROM syscolumns)A FOR XML PATH(''),TYPE).value('.','VARCHAR(MAX)'),1,2,'')+';}'

Update: Thanks @Bridge, I found a few more spaces too :)

Update2: Changed CTE to subquery -27 chars :) Update3: Another space bites the dust @bridge :)

\$\endgroup\$
2
  • 1
    \$\begingroup\$ I was able to trim off 7 more spaces (282 bytes) without changing the rest of the code: WITH A AS(SELECT CAST('1'AS VARCHAR(20))N UNION ALL SELECT CAST(CONCAT(N,'0')AS VARCHAR(20))FROM A WHERE LEN(N)<20)SELECT'public static boolean isPowerOfTen(long input){return '+STUFF((SELECT'|| input=='+N+'L 'FROM A FOR XML PATH(''),TYPE).value('.', 'VARCHAR(MAX)'), 1, 3, '')+';}' \$\endgroup\$
    – Bridge
    Feb 4, 2016 at 10:23
  • 1
    \$\begingroup\$ Now I look back I can see all the extra spaces in my original comment! I've found one more space you can get rid of - the one immediately after ROW_NUMBER() \$\endgroup\$
    – Bridge
    Feb 5, 2016 at 10:07
0
\$\begingroup\$

R, 185 bytes

Golfed

options(scipen=999);p=paste;cat(p("public static boolean isPowerOfTen(long input) {"," return",p(sapply(0:19,function(x)p(" input == ",10^x,"L",sep="")),collapse="\n ||"),"}",sep="\n"))

Ungolfed

options(scipen=999)
p=paste
cat(
  p("public static boolean isPowerOfTen(long input) {",
        " return",
        p(sapply(0:19,function(x)p(" input == ",10^x,"L",sep="")),collapse="\n ||"),
        "}",
        sep="\n")
)
\$\endgroup\$
0
\$\begingroup\$

Perl 6 (115 bytes)

say "public static boolean isPowerOfTen(long input) \{
  return
   {join "L
  ||",(" input == "X~(10 X**^19))}L;
}"

X operator does list cartesian product operation, for example 10 X** ^19 gives powers of ten (from 10 to the power of 0 to 19, as ^ is a range operator that counts from 0). Strings can have code blocks with { (which is why I escape the first instance of it).

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

Java, 210 / 166

Score is depending on whether returning the input from a function meets the definition of 'output'.

Console output (210):

class A{public static void main(String[]z){String a=" input == 1",t="L\n  ||"+a,s="public static boolean isPowerOfTen(long input) {\n  return\n   "+a;for(int i=0;++i<19;)s+=t+="0";System.out.print(s+"L;\n}");}}

String return (166):

String a(){String a=" input == 1",t="L\n  ||"+a,s="public static boolean isPowerOfTen(long input) {\n  return\n   "+a;for(int i=0;++i<19;)s+=t+="0";return s+"L;\n}";}

Legible version:

String a() {
    String a=" input == 1", t = "L\n  ||"+a,
        s = "public static boolean isPowerOfTen(long input) {\n  return\n   "+a;
    for (int i = 0; ++i < 19;)
        s += t += "0";
    return s + "L;\n}";
}
\$\endgroup\$
0
\$\begingroup\$

Batch, 230 208 206 205 bytes

@echo off
echo public static boolean isPowerOfTen(long input) {
echo   return
set m=input == 1
echo    %m%L
for /l %%a in (1,1,17)do call:a
call:a ;
echo }
exit/b
:a
set m=%m%0
echo  ^|^| %m%L%1

Edit: Saved 22 bytes by avoiding repeating input == and reusing the subroutine for the line with the extra semicolon. Saved 2 3 bytes by removing unnecessary spaces.

\$\endgroup\$
2
  • \$\begingroup\$ Do you need spaces around ==? \$\endgroup\$
    – Pavel
    Jan 6, 2017 at 17:04
  • \$\begingroup\$ @Pavel That's not code; it's part of the output. \$\endgroup\$
    – Dennis
    Jan 6, 2017 at 17:20

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.