37
\$\begingroup\$

Provided a digit between 0 and 9 (inclusive), your function/subroutine should print all numbers between -100 and 100 (inclusive) that contain the given digit.

For example:

Input: 9
Output: -99 -98 -97 -96 -95 -94 -93 -92 -91 -90 -89 -79 -69 -59 -49 -39 -29 -19 -9 9 19 29 39 49 59 69 79 89 90 91 92 93 94 95 96 97 98 99

Rules:

  • You cannot use strings (in whatever way they are represented in your chosen language) or char arrays. (Except to print the answer to the console.)
  • You cannot use arrays.

Scoring:

Score = length of subroutine/function (whitespace will not be counted)

The answer with the lowest score wins.

Whitespace will NOT be counted. Please format your code properly!

Some answers are using regular expressions, which may violate the no strings rule, as pointed out by some members. These answers will not be accepted.

Please test your code with 0 before posting.

\$\endgroup\$
30
  • 2
    \$\begingroup\$ The given output is either wrong or unclear. Where are -98 ... -91 and 91 ... 98? \$\endgroup\$
    – Oberon
    Mar 5, 2014 at 12:29
  • 27
    \$\begingroup\$ A Whitespace answer will score 0. Are you sure you want to do that? \$\endgroup\$
    – user12205
    Mar 5, 2014 at 13:21
  • 3
    \$\begingroup\$ Is use of regex allowed? Working with regex means working with strings. Similarly working with lists/collections (and similar structures) is same as working with arrays. The answer from @wallywest here uses regex. This is the thing that I don't like about questions when they try to prohibit from using string and/or arrays. People find alternate ways of using them. And if any answer really does implement correctly, it does not get as many votes as it should just because it looks complicated. \$\endgroup\$
    – microbian
    Mar 5, 2014 at 17:24
  • 2
    \$\begingroup\$ "Whitespace will NOT be counted. Please format your code properly!" isn't a great way of doing this, since whitespace in different languages have different significance. More often, it's just "post an ungolfed/readable version too". \$\endgroup\$
    – Bob
    Mar 6, 2014 at 1:34
  • 2
    \$\begingroup\$ @ace I created an answer in whitespace (needed 4 hours of hard work with this). i think that it scores zero, right? \$\endgroup\$ Mar 6, 2014 at 4:02

55 Answers 55

75
\$\begingroup\$

Whitespace, 834 characters (0 if you subtract whitespaces)

Used this interpreter to test it.

This took me some 4 hour of hard work and horrible debugging to create. But it is done!

   	    	 
   	    	 
	
	    	    	 
			   		    
	  			    	    		
  			   		
		    	     	
   
		    	    	 
			
	  	  	 		
   	    	 
			   	
	  	
	  	  	 		

   	   	  
   	    		
			 
    		  	  
	  	
	  	   	 	
 
   		
	  

		 	   			
  		
	  

   	   			
 
    	    	 
			
	  	  	   
   	 	 
	 	    	    	 
				  	
	  	  	  	

   	  	   
   	 	 
	 		   	    	 
				  	
	  	  	  	

 
 	  	 	 

   	  	  	
   	     	
			
	  	   		 
   	     
	
  
   	   		 
   	     	
   	
		    	    		
				
 	
   	  	 	 
   	    		
   	    		
			   	
	   		 
 
 	   	  

   	   	 	
   	    	 
			
	  	  		  
   	    	 
			   	
	  	
	  	  		  




   	  	 		
  			  	  
	
 	   	     
	
  
 
 	   	  

   	  		  
   	     
	
     		  	  
	
 	


EDIT: Selectable text provided with HTML tricks. If you copy and paste it from here to some other place, please check if you got exactly [space][tab][linefeed][linefeed][linefeed] at the end with 100 or 101 lines (depending if your editor counts or not a new line if the last one ends with a linefeed).

In the case that you can't use it this way, considering space as S, linefeed as L and tab as T, and breaking lines after the L's, here it is:

SSSTSSSSTSL
SSSTSSSSTSL
TL
TSSSSTSSSSTSL
TTTSSSTTSSSSL
TSSTTTSSSSTSSSSTTL
SSTTTSSSTTL
TTSSSSTSSSSSTL
SSSL
TTSSSSTSSSSTSL
TTTL
TSSTSSTSTTL
SSSTSSSSTSL
TTTSSSTL
TSSTL
TSSTSSTSTTL
L
SSSTSSSTSSL
SSSTSSSSTTL
TTTSL
SSSSTTSSTSSL
TSSTL
TSSTSSSTSTL
SL
SSSTTL
TSSL
L
TTSTSSSTTTL
SSTTL
TSSL
L
SSSTSSSTTTL
SL
SSSSTSSSSTSL
TTTL
TSSTSSTSSSL
SSSTSTSL
TSTSSSSTSSSSTSL
TTTTSSTL
TSSTSSTSSTL
L
SSSTSSTSSSL
SSSTSTSL
TSTTSSSTSSSSTSL
TTTTSSTL
TSSTSSTSSTL
L
SL
STSSTSTSL
L
SSSTSSTSSTL
SSSTSSSSSTL
TTTL
TSSTSSSTTSL
SSSTSSSSSL
TL
SSL
SSSTSSSTTSL
SSSTSSSSSTL
SSSTL
TTSSSSTSSSSTTL
TTTTL
STL
SSSTSSTSTSL
SSSTSSSSTTL
SSSTSSSSTTL
TTTSSSTL
TSSSTTSL
SL
STSSSTSSL
L
SSSTSSSTSTL
SSSTSSSSTSL
TTTL
TSSTSSTTSSL
SSSTSSSSTSL
TTTSSSTL
TSSTL
TSSTSSTTSSL
L
L
L
L
SSSTSSTSTTL
SSTTTSSTSSL
TL
STSSSTSSSSSL
TL
SSL
SL
STSSSTSSL
L
SSSTSSTTSSL
SSSTSSSSSL
TL
SSSSSTTSSTSSL
TL
STL
L
L
\$\endgroup\$
13
  • 32
    \$\begingroup\$ +1 exploiting "0 if you subtract whitespaces" \$\endgroup\$
    – Jwosty
    Mar 6, 2014 at 14:30
  • 13
    \$\begingroup\$ Haha, I learnt a lesson. :P \$\endgroup\$
    – duci9y
    Mar 6, 2014 at 15:54
  • 2
    \$\begingroup\$ @Cruncher The language is well defined and entirely unambiguous. For your particular question: a number is a series of spaces and tabs, representing the number in binary (space = 0, tab = 1, big-endian binary format) and is terminated by a newline. So you know a number finishes when you reach a newline. \$\endgroup\$
    – Tim S.
    Mar 6, 2014 at 17:45
  • 1
    \$\begingroup\$ I learnt Whitespace today. codegolf.stackexchange.com/a/23216/17886 \$\endgroup\$
    – duci9y
    Mar 6, 2014 at 17:50
  • 7
    \$\begingroup\$ I interpret "Whitespace will NOT be counted." to mean this answer is invalid. ;) +1 \$\endgroup\$ Mar 6, 2014 at 21:17
24
\$\begingroup\$

Bash+utils - 20

seq -100 100|grep 9

Or 21 bytes if in a script

seq -100 100|grep $1

To run the script:

sh myscript 9
\$\endgroup\$
9
  • 4
    \$\begingroup\$ grep isn't bash or sh. It's another utility! \$\endgroup\$
    – devnull
    Mar 5, 2014 at 17:14
  • \$\begingroup\$ i have to agree, it is not pure shell/bash :) \$\endgroup\$
    – Nik O'Lai
    Mar 5, 2014 at 17:24
  • 1
    \$\begingroup\$ Sees question. Makes answer in mind. Sees exact same answer already posted. Darn! +1 \$\endgroup\$ Mar 5, 2014 at 22:32
  • 6
    \$\begingroup\$ I think this is using strings. \$\endgroup\$ Mar 5, 2014 at 22:36
  • 1
    \$\begingroup\$ @DavidConrad If you look at it that way, everything uses strings. The program source is a string. The terminal uses strings. Etc. \$\endgroup\$ Mar 6, 2014 at 0:03
9
\$\begingroup\$

JavaScript 133 - 1 = 132

Surely there couldn't possibly be anything wrong with this version now...?

for(k=prompt(),c=console,j=-100;100>=j;j++)if(i=j,0==j&j==k)c.log(0);else for(;i;)if(h=i%10,h=0>h?-h:h,i=i/10|0,h==k){c.log(j);break}

PROMPT: 2
CONSOLE.LOG:
-92
-82
-72
-62
-52
-42
-32
-29
-28
-27
-26
-25
-24
-23
-22
-21
-20
-12
-2
 2
 12
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 32
 42
 52
 62
 72
 82
 92

CONTROL, using zero
PROMPT: 0
CONSOLE.LOG:
    -100
    -90
    -80
    -70
    -60
    -50
    -40
    -30
    -20
    -10
      0
     10
     20
     30
     40
     50
     60
     70
     80
     90
    100
\$\endgroup\$
15
  • 1
    \$\begingroup\$ Even a bit shorter: for(i=-100,r=prompt();101>i;i++)(i+'').match(r)&&console.log(i). \$\endgroup\$
    – VisioN
    Mar 5, 2014 at 13:02
  • 2
    \$\begingroup\$ I like this answer (+1), but doesn't it implicitly use strings? \$\endgroup\$ Mar 5, 2014 at 17:19
  • 2
    \$\begingroup\$ this does use implicit string conversion \$\endgroup\$
    – Claudiu
    Mar 5, 2014 at 18:40
  • 1
    \$\begingroup\$ Actually chaps, no it doesn't... alert(RegExp(9).test(99)) which is based on numbers only, also returns true due to JavaScript's loosely based typing system. The functions work with the numbers as though they are strings but are still numbers... If you change the code to read r=RegExp(x=+prompt());for(i=-100;101>i;i++)r.test(i)&&console.log(i);console.log(typeof(x));console.log(typeof(i)) and executed it, the final typeof response would show x and i as being numbers... Ergo, I'm not using strings... \$\endgroup\$ Mar 5, 2014 at 20:59
  • 2
    \$\begingroup\$ @VoronoiPotato printing is also a string op. But we are obviously required to do that. \$\endgroup\$ Mar 6, 2014 at 21:26
8
\$\begingroup\$

GolfScript [24 bytes]

`:x;201,{100-}%{`x?-1>},

Description:

`:x;             - save input value to variable 'x' and remove from the stack
201,{100-}%      - create range from -100 to 100
{`x?-1>},        - filter the range by "index-of" condition

DEMO: http://golfscript.apphb.com/?c=MwoKYDp4OzIwMSx7MTAwLX0le2B4Py0xPn0sYA%3D%3D

\$\endgroup\$
2
  • \$\begingroup\$ this uses strings, though \$\endgroup\$
    – Claudiu
    Mar 26, 2014 at 16:51
  • \$\begingroup\$ It also uses arrays: 201, creates an array containing 0 to 200. \$\endgroup\$
    – Rees
    May 13, 2014 at 7:28
6
\$\begingroup\$

Ruby: 92 characters

s=gets.to_i
$><<(-100..100).select{|n|a=n.abs;loop{break""if a%10==s;break if 0==a/=10}}*" "

Readable version:

searchfor = gets.to_i
$><< (-100..100).select { |number|
  absnumber = number.abs
  loop {
    break "" if absnumber % 10 ==s
    break if 0 == absnumber /= 10
  }
} * " "

Sample run:

bash-4.2$ ruby -e 's=gets.to_i;$><<(-100..100).select{|n|a=n.abs;loop{break""if a%10==s;break if 0==a/=10}}*" "' <<< 9
-99 -98 -97 -96 -95 -94 -93 -92 -91 -90 -89 -79 -69 -59 -49 -39 -29 -19 -9 9 19 29 39 49 59 69 79 89 90 91 92 93 94 95 96 97 98 99

Test run:

bash-4.2$ for i in {0..9}; do diff -w <(ruby -e 's=gets.to_i;$><<(-100..100).select{|n|a=n.abs;loop{break""if a%10==s;break if 0==a/=10}}*" "' <<< $i) <(seq -100 100|grep $i|tr \\n \ ) > /dev/null; echo "$i : $?"; done
0 : 0
1 : 0
2 : 0
3 : 0
4 : 0
5 : 0
6 : 0
7 : 0
8 : 0
9 : 0
\$\endgroup\$
2
  • \$\begingroup\$ Fails the 0 test. \$\endgroup\$
    – duci9y
    Mar 5, 2014 at 18:21
  • \$\begingroup\$ Oops. You're right, @duci9y. Corrected. \$\endgroup\$
    – manatwork
    Mar 5, 2014 at 19:52
6
\$\begingroup\$

K - 45 char

Not winning any awards, but K golfing is underrepresented. K doesn't get any prettier with proper spacing, so I'll just leave this as is, because K gets very picky about which whitespace is important and which isn't.

{|a@&x _in'@[10_vs'_ _abs a:100-!201;100;0,]}

Explained:

  • a:100-!201 - Make a list from 0 to 200 inclusive, then subtract it from 100. Now we have the numbers from -100 to 100 inclusive, but backwards from the question's specs. We can always fix that later, so for now we'll just assign this to a.
  • _ _abs - Take the floor of the absolute value of these numbers, because for whatever reason K thinks that _abs should give floating point results. Thankfully, flooring them turns them back into integers.
  • 10_vs' - Expand each (') integer as a base 10 number (_vs stands for "vector from scalar"). Note that we did not have to use the Each operator ' on the functions above because they operate on atoms.
  • @[...;100;0,] - Amend the item in our list at index 100 (an empty list, which is the result of expanding 0 into base 10) by the function 0,, which prepends a zero to the front. Without this correction, this function will fail on the input 0.
  • &x in' - Now, return the indices (&) where x is a digit in (_in) each (') of the expansions we so carefully constructed above.
  • |a@ - Finally, use these indices to index a, and reverse the list into the right order.

Usage is obvious, though good luck getting your hands on a K interpreter. (There's an open source implementation at on Github that you can compile.)

  {|a@&x _in'@[10_vs'_ _abs a:100-!201;100;0,]} 0
-100 -90 -80 -70 -60 -50 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100
  {|a@&x _in'@[10_vs'_ _abs a:100-!201;100;0,]} 5
-95 -85 -75 -65 -59 -58 -57 -56 -55 -54 -53 -52 -51 -50 -45 -35 -25 -15 -5 5 15 25 35 45 50 51 52 53 54 55 56 57 58 59 65 75 85 95
\$\endgroup\$
4
  • \$\begingroup\$ I wonder if languages like K have any productive uses outside of scenarios like this question. Do they? \$\endgroup\$
    – duci9y
    Mar 6, 2014 at 7:30
  • 2
    \$\begingroup\$ @duci9y Actually, K in particular has found itself a niche market on Wall Street handling high-speed, high-volume transactions, using its native database software KDB. The close relative J also finds use, though more in corporate and academic contexts. \$\endgroup\$ Mar 6, 2014 at 8:15
  • \$\begingroup\$ That's great! I was of the belief that esoteric languages are just for fun. Good to see that's not the case. \$\endgroup\$
    – duci9y
    Mar 6, 2014 at 9:02
  • \$\begingroup\$ Doesn't the description say "You cannot use arrays"? \$\endgroup\$
    – Omar
    Feb 23, 2015 at 19:01
6
\$\begingroup\$

bash 55 49 characters:

(not counting whitespace around &&)

for i in {-100..100}
do
  [[ $i =~ $1 ]] && echo $i
done

Executing it by saying:

bash filename 9

would produce:

-99
-98
-97
-96
-95
-94
-93
-92
-91
-90
-89
-79
-69
-59
-49
-39
-29
-19
-9
9
19
29
39
49
59
69
79
89
90
91
92
93
94
95
96
97
98
99
\$\endgroup\$
3
  • \$\begingroup\$ You can replace " && " with "&&" for two extra points. Also I count one less char if I drop the trailing newline. \$\endgroup\$ Mar 5, 2014 at 18:37
  • 1
    \$\begingroup\$ Scratch that - I just saw the new rule: "EDIT: Whitespace will NOT be counted. Please format your code properly!". So I guess you just get to count all non-whitespace characters? \$\endgroup\$ Mar 5, 2014 at 18:43
  • \$\begingroup\$ Then you can convert the ;s to newlines, and save 2 points. \$\endgroup\$ Mar 8, 2014 at 9:35
4
\$\begingroup\$

PHP 67 bytes:

for($i=-100;$i<101;++$i)if(strpos($i,$_GET[n])!==!1)echo$i,PHP_EOL;

Yes, there is a 'strpos' call, but I'm using only numbers!

Here is a 'ungolfed' version:

for($i=-100;$i<101;++$i)
{
    if(strpos($i,$_GET[n])!==false)
    {
        echo $i,PHP_EOL;
    }
}

For this to work, you can test it here: http://writecodeonline.com/php/

Just remember to add $_GET[n]='<number>'; to the beginning of the code.

Or, on a lamp or xampp server, you can create a page and then access it on the browser, with the parameter ?n=<number> after the filename.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ This looks like implicit string conversion, violating the no strings rule. \$\endgroup\$ Mar 5, 2014 at 21:45
  • \$\begingroup\$ Not my fault. I didn't made the language. I used what I have. Besides, the conversion is made internally. There is nothing in the code dealing with strings (except the output). \$\endgroup\$ Mar 5, 2014 at 22:03
  • \$\begingroup\$ The way I read the rule, you have to deal with the numbers arithmetically. Treating them as strings, whether by you or by the language or library, is off limits. But almost everyone else is skirting the rule in a similar way. \$\endgroup\$ Mar 5, 2014 at 22:48
  • \$\begingroup\$ Well, everyone wants to get it done the smallest way. And the smallest is treating the numbers as strings (internally without implicit conversion to string in the code). \$\endgroup\$ Mar 5, 2014 at 23:14
4
\$\begingroup\$

Mathematica 28

Sorry, I just couldn't resist.

:)

The instructions are: "Provided a digit between 0 and 9 (inclusive), your function/subroutine should print all numbers between -100 and 100 (inclusive) that contain the given digit."


The following prints all such numbers in order.

f@n_ :=Print/@Range[-100,100]

As a special bonus, it prints those numbers in the same range that do not contain the given digit. :)

\$\endgroup\$
2
  • \$\begingroup\$ But this do not use n as a filter \$\endgroup\$
    – Murta
    Mar 7, 2014 at 10:20
  • \$\begingroup\$ @Marta, Yes, I subverted the intent of the puzzle (which is why I said "Sorry"), while technically providing an appropriate answer. \$\endgroup\$
    – DavidC
    Mar 7, 2014 at 12:34
4
\$\begingroup\$

My first attempt here, so maybe I am doing some things wrong, please excuse that ;)
So, I wasn't sure if I had to add the whole class or just the logic within.

Java - 199 characters

intx=System.in.read()-48;for(inti=-100;i<101;i++){if((i<10&&i==-x)||(i>-10&&i==x)||(i<-9&&((i/100==x&&i<-99)||i%10==-x||i/10==-x))||(i>9&&((i/100==x&&i>99)||i%10==x||i/10==x)))System.out.println(i);}

Here a more readable form:

int x = System.in.read() - 48;
for(int i = -100; i < 101; i++) {

    if((i < 10 && i == -x) || (i > -10 && i == x)
        || (i < -9 && ((i / 100 == x && i < -99 ) || i % 10 == -x || i / 10 == -x))
        || (i > 9 && ((i / 100 == x && i > 99) || i % 10 == x || i / 10 == x)))

        System.out.println(i);

}

I guess the basic idea was not that bad, but needed too much exceptions, to finally work out for all cases... :/

\$\endgroup\$
4
  • \$\begingroup\$ Sorry, doesn't work with 0 :( \$\endgroup\$
    – duci9y
    Mar 6, 2014 at 7:38
  • \$\begingroup\$ Aw, sure, last addition I made messed that up. Updated Code, so I'm down to 199 now. Thanks for pointing that out! \$\endgroup\$ Mar 6, 2014 at 7:45
  • \$\begingroup\$ Didn't realize that one :D Too much coding in native language in the past days I guess ;) \$\endgroup\$ Mar 7, 2014 at 6:36
  • \$\begingroup\$ You removed every single space in your compacted version... wouldn't that cause an error? Example: Your first 4 characters: intx... shouldn't it be: int x? \$\endgroup\$
    – SirPython
    Feb 22, 2015 at 1:48
4
\$\begingroup\$

C - 104 107 114 characters - 0 = 104 107 114

a,b;main(i){i=getchar()-48;for(a=-100;a<101;a++){for(b=a;a&&abs(a%10)!=i;a/=10);if(a|!i&!b)printf("%i ",b);a=b;}}

Ungolfed:

#include <stdio.h>

int a, b;

int main(int i) {
    i = getchar() - '0';
    for( a = -100 ; a < 101 ; a++ ) {
        for( b = a ; a && abs(a % 10) != i ; a /= 10 );
        if( a | !i & !b )
            printf("%i ", b);
        a = b;
    }
}
\$\endgroup\$
7
  • \$\begingroup\$ Nice. Could you also post an ungolfed version with proper whitespace please? \$\endgroup\$
    – duci9y
    Mar 5, 2014 at 12:49
  • \$\begingroup\$ @duci9y Added it. \$\endgroup\$
    – Oberon
    Mar 5, 2014 at 13:24
  • \$\begingroup\$ Won't this output the results out of order? (Spec has been updated to state that order matters.) \$\endgroup\$
    – Gaffi
    Mar 5, 2014 at 13:37
  • \$\begingroup\$ @Gaffi Well then. I shall edit the code. \$\endgroup\$
    – Oberon
    Mar 5, 2014 at 13:53
  • 4
    \$\begingroup\$ Fails to notice that 0 contains 0. \$\endgroup\$
    – ugoren
    Mar 5, 2014 at 17:47
3
\$\begingroup\$

R 87 93

Here's an improvement:

a=scan();r=-100:100;s=abs(r);r[(!a&!r%%10)|a&(a==s%%10|a==floor(s/10)|a==floor(s/100))]

(Potentially) More readable, with notes:

a=scan() # take user input of selected digit
r=-100:100 # define our scanning range
s=abs(r) # copy the range as the absolute value for proper floor() calculation
#r[<criteria>] is functionally the same as subset(r,<criteria>)
r[ # when any of the criteria below are met, use that value from the range
    (!a & !r%%10) # case when input is 0, !a is evaluated as "a when a<>0 == true"
    | a & # all other digits below
    a==s%%10
    |
    a==floor(s/10)
    |
    a==floor(s/100)
    ] # R does not require a print command, so this as-is will display the corresponding values
\$\endgroup\$
7
  • \$\begingroup\$ Why no whitespace? \$\endgroup\$
    – duci9y
    Mar 5, 2014 at 18:11
  • \$\begingroup\$ @duci9y Because none is necessary. I'll format to be more readable and add that as well, just for good measure. \$\endgroup\$
    – Gaffi
    Mar 5, 2014 at 18:12
  • \$\begingroup\$ Doesn't work with 0. \$\endgroup\$
    – duci9y
    Mar 5, 2014 at 18:31
  • \$\begingroup\$ @duci9y It does not fail. I made one small change from the original post (+2 chars), as the original post DID fail on 0. Did you try with this most recent version? I'm using R 3.0.1. \$\endgroup\$
    – Gaffi
    Mar 5, 2014 at 18:43
  • \$\begingroup\$ @duci9y To be clear, I meant did you use this on the most recent version of my code. My version of R should be close enough to the most recent version to not matter. However, I did not test at all on any 2.* versions. \$\endgroup\$
    – Gaffi
    Mar 5, 2014 at 18:54
3
\$\begingroup\$

Javascript 116

   a = 0
        for(i=-100;101>i;i++){
        m=Math
        f=m.floor
        j = m.abs(i)
        if((j>10&&f(j/10)==a)||j-f(j/10)*10==a||j/100==a){

             console.log(i)

        }
        }
\$\endgroup\$
0
3
\$\begingroup\$

Prolog: 75

f(D) :-
  between(-100,100,N) ,
  number_chars(N,Ds) ,
  member(D,Ds) ,
  writeln(N) ,
  fail
  .
\$\endgroup\$
1
  • \$\begingroup\$ How do you call this? I saved it in a test.pl file, started swipl, entered consult('test') and then entered f(9). But it only returned false. \$\endgroup\$ Mar 8, 2014 at 18:53
3
\$\begingroup\$

C answer in 98 characters

This is one of the sexiest things I've ever coded

main()
{
    int t=getchar()-48,i=100,j=-i;
    while ((i=t-i%10?i/10:!printf("%d\n",j)) || (i=++j<0?-j:j)<101  );
}

The older version, with 104 non-whitespace chars:

int main()
{
    int t=getchar()-48,i,j=-101;
    while(++j<101)
    {
        i=j<0?-j:j;
        while(i = t-i%10?i/10:!printf("%d\n",j));
    }
}

"Works for me" using GCC and CLANG.

\$\endgroup\$
1
  • \$\begingroup\$ Awesome! Welcome to Code Golf. \$\endgroup\$
    – duci9y
    Mar 6, 2014 at 15:57
3
\$\begingroup\$

GW Basic: 107 characters excluding whitespace

1 input n
2 for i=-100 to 100
3 j=abs(i):a=j mod 10
4 if a=n then 8
5 b=j\10
6 if (b=n) and b then 8
7 if (b<10) or n<>1 then 9
8 print i
9 next

Using single digits for the line numbers helps and stripping the whitespace means there isn't really a need for having multiple statements on a line more than once to keep numbers reaching 10.

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

GNU coreutils (44)

read N
seq -100 100 | grep $N | tr '\n' ' '
echo

where tr is used to convert newlines to spaces, and echo provides one final newline.

$ bash ./script
9
-99 -98 -97 -96 -95 -94 -93 -92 -91 -90 -89 -79 -69 -59 -49 -39 -29 -19 -9 9 19 29 39 49 59 69 79 89 90 91 92 93 94 95 96 97 98 99
\$\endgroup\$
1
  • \$\begingroup\$ This isn't much different from an existing answer. \$\endgroup\$
    – devnull
    Mar 5, 2014 at 17:17
2
\$\begingroup\$

J - 27 chars

All whitespace is safely removable, meaning 27 characters. Negative numbers in output will have _ for a negative sign: this is just the way J writes its negative numbers.

((e. 10 #.^:_1 |)"0 # ]) & (i: 100)

Explained:

  • V & (i: 100) - Bind (&) the set of numbers from -100 to 100 inclusive (i:100) as the right argument of the main verb (V). The single argument of the entire verb gets piped into the left side.
  • (U"0 # ]) - Use the result of the verb U over each number from the right argument ("0) to select (#) items from the right argument (]).
  • (e. 10 #.^:_1 |) - Given the digit to test for as the left argument and the number to check as the right argument, expand in base 10 (10 #.^:_1) the absolute value of the number (|), and check if the digit is an element of that expansion (e.).

Usage:

   ((e. 10 #.^:_1 |)"0 # ]) & (i: 100) 0
_100 _90 _80 _70 _60 _50 _40 _30 _20 _10 0 10 20 30 40 50 60 70 80 90 100
   ((e. 10 #.^:_1 |)"0 # ]) & (i: 100) 5
_95 _85 _75 _65 _59 _58 _57 _56 _55 _54 _53 _52 _51 _50 _45 _35 _25 _15 _5 5 15 25 35 45 50 51 52 53 54 55 56 57 58 59 65 75 85 95
\$\endgroup\$
3
  • \$\begingroup\$ The underscore-for-negative convention in J always bothered me -- it wouldn't be so bad if it used a minus symbol in output (without messing with the format verb). \$\endgroup\$
    – Desty
    Mar 6, 2014 at 12:34
  • \$\begingroup\$ The underscore is J's unambiguous (and, I would argue, elegant and APLish) way of differentiating negative signs from the verb - Negate/Minus. K lets you use the negative sign "naturally" in numbers, and incidentally K came out an arcane syntactic clusterfuck. If it's really irking you, work in your choice of ;8!:1, '4.0'8!:2, or '-'I.@:=&'_'}":, costing 7, 11, and 16 chars respectively to make the resulting program a function instead of an expression, by my count. \$\endgroup\$ Mar 6, 2014 at 16:12
  • \$\begingroup\$ I know what it is, but it's a bit bothersome all the same when it comes to I/O. It'd be nicer if it produced minus signs by default when printing numbers, rather than printing underscores presumably in case someone wanted to eval the resulting string. \$\endgroup\$
    – Desty
    Mar 6, 2014 at 16:40
2
\$\begingroup\$

Groovy, 127

def x = args[0].toInteger()
for (k in -100..100)
    if (k == x)
        print " ${k}"
    else
        for (n = Math.abs(k); n > 0; n = (int) n / 10)
            if (n % 10 == x) {
                print " ${k}"
                break
            }

No strings (except to output the spaces between the numbers), no char arrays or other arrays, no regexes. Tested with 0. Output:

-100 -90 -80 -70 -60 -50 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100

\$\endgroup\$
1
  • 1
    \$\begingroup\$ And I discovered a new language today. :) \$\endgroup\$
    – duci9y
    Mar 6, 2014 at 15:54
2
\$\begingroup\$

Javascript - 108

Not sure if existing javascript answer will be taken into consideration because it uses regex, so I created one without it:

for(x=+prompt(i=-100);i<101;i++)if((j=i<0?-i:i)&&j%10==x||((j/100==x||(0|j/10)==x)&&x)||j==x)console.log(i)

Can also be shortened to 101 if x variable is put directly, like:

for(x=5,i=-100;i<101;i++)if((j=i<0?-i:i)&&j%10==x||((j/100==x||(0|j/10)==x)&&x)||j==x)console.log(i)

It basically checks if absolute values of div or mod operations are equal to the digit (which also works for 100).

\$\endgroup\$
10
  • \$\begingroup\$ If x=1, this fails to print -100 or 100. \$\endgroup\$
    – DocMax
    Mar 5, 2014 at 20:54
  • \$\begingroup\$ Should be fixed now \$\endgroup\$
    – dkasipovic
    Mar 5, 2014 at 21:36
  • \$\begingroup\$ 1 works nicely, but now 0 prints 1, 2, 3... Sorry to be a troublemaker here. \$\endgroup\$
    – DocMax
    Mar 5, 2014 at 22:34
  • \$\begingroup\$ Well if you put x=0; it works ok, the problem was that prompt() returns string, so I added *1 and it hopefully should be ok now. THank you for your suggestions \$\endgroup\$
    – dkasipovic
    Mar 5, 2014 at 23:15
  • 1
    \$\begingroup\$ parseInt(j/10) can be replaced with (0|j/10) which is both shorter and avoids the implicit strings. The (unavoidable) string-to-number conversion in prompt()*1 cannot be shortened as +prompt(). (Or shorter still, change the function to start for(x=+prompt(i=-100);.... \$\endgroup\$
    – DocMax
    Mar 6, 2014 at 0:04
2
\$\begingroup\$

GROOVY, 71

f={z->
  _=10
  (-_*_.._*_).grep {
    a=it.abs()
    b=a%_==z
    a<_?b:b||(int)(a/_)%_==z
  }
}

println f(0)
println f(9)

Results in

[-100, -90, -80, -70, -60, -50, -40, -30, -20, -10, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
[-99, -98, -97, -96, -95, -94, -93, -92, -91, -90, -89, -79, -69, -59, -49, -39, -29, -19, -9, 9, 19, 29, 39, 49, 59, 69, 79, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
\$\endgroup\$
2
\$\begingroup\$

Javascript 96

Using bitwise or:

b=Math.abs;
a=prompt();
for(i=-100;i<101;)
    if(!(b(i++/10)^a && b(i%10)^a)||b(i)/a==100)  console.log(i)
\$\endgroup\$
2
\$\begingroup\$

Haskell, 86 75 (78 with necessary whitespaces)

f 0=[-100,-90..100]
f d=[b|b<-[-100..100],abs(b`quot`10)==d||abs(b`rem`10)==d]

Ungolfed:

f dig =
  if dig == 0
    then [-100, 90 .. 100]
    else [num | num <- [-100 .. 100], abs (num `quot` 10) == d || abs (num `rem` 10) == d]
\$\endgroup\$
1
  • \$\begingroup\$ If you use a two line definition with pattern matching, i.e. a 0=[-100.... and a d=[b|b<-.... you can get rid of the if then else and save a few bytes. \$\endgroup\$
    – nimi
    Feb 21, 2015 at 23:23
1
\$\begingroup\$

Python - 172 chars

def f(x,y,z):
    if x%10==y or (x>9 and x/10==y) or (x==100 and y==1):
        print z
def g(x,y):
    f(abs(x),y,x)
    if x<100:
        g(x+1,y)
def h(y):
    g(-100,y)

To test within Python:

>>> h(4)
-94
-84
-74
...
-49
-48
...
\$\endgroup\$
0
1
\$\begingroup\$

VBA 121

(no whitespace or Sub definition counted):

Sub t(d)
For n = -100 To 100
m = Abs(n)
o = o & IIf(d = 0, IIf(d = n Mod 10, " " & n, ""), IIf(d = n Or d = m Mod 10 Or d = Int(m / 10) Or d = Int(m / 100), " " & n, ""))
Next
MsgBox o
End Sub
\$\endgroup\$
2
  • \$\begingroup\$ Does this work with 0? \$\endgroup\$
    – duci9y
    Mar 5, 2014 at 18:28
  • \$\begingroup\$ @duci9y Yes. Specifically because: IIf(d = 0, IIf(d = n Mod 10, " " & n, "") \$\endgroup\$
    – Gaffi
    Mar 5, 2014 at 18:31
1
\$\begingroup\$

perl, 117 with non-meaningful whitespace chars removed

I think you were looking for something more like this. Reads from stdin, outputs one line per match. No regexps, arrays (or sets or hashes or anything else that is an array under the covers) or strings, implicit or otherwise, except the strings passed to print:

chomp($x=<>); for($y=-100;$y<101;++$y) { $a=abs $y; print "$y " if $a % 10 == $x || $a > 9 && int( $a/10 ) == $x || $a==100 && $x==1}; print "\n"

eg:

ski@anito:~$ echo 0 | perl -e 'chomp($x=<>); for($y=-100;$y<101;++$y) { $a=abs $y; print "$y " if $a % 10 == $x || $a > 9 && int( $a/10 ) == $x || $a==100 && $x==1}; print "\n"'
-100 -90 -80 -70 -60 -50 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100 

ski@anito:~$ echo 1 | perl -e 'chomp($x=<>); for($y=-100;$y<101;++$y) { $a=abs $y; print "$y " if $a % 10 == $x || $a > 9 && int( $a/10 ) == $x || $a==100 && $x==1}; print "\n"'
-100 -91 -81 -71 -61 -51 -41 -31 -21 -19 -18 -17 -16 -15 -14 -13 -12 -11 -10 -1 1 10 11 12 13 14 15 16 17 18 19 21 31 41 51 61 71 81 91 100 

ski@anito:~$ echo 2 | perl -e 'chomp($x=<>); for($y=-100;$y<101;++$y) { $a=abs $y; print "$y " if $a % 10 == $x || $a > 9 && int( $a/10 ) == $x || $a==100 && $x==1}; print "\n"'
-92 -82 -72 -62 -52 -42 -32 -29 -28 -27 -26 -25 -24 -23 -22 -21 -20 -12 -2 2 12 20 21 22 23 24 25 26 27 28 29 32 42 52 62 72 82 92 
\$\endgroup\$
1
\$\begingroup\$

F# 87 92 - 7

let f n = {-100..100}
|>Seq.filter(fun x->abs x%10=n||abs x/10=n&&n>0)
|>Seq.iter(printf"%d ")

added 5 chars because 0 wasn't handled correctly. (Single digit values would all be returned.)

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

This is my first time playing code golf. This looks pretty interesting. I can't win, but I tried and wanted to show what I could do.

Python: 104 (89 if I can mulligan the import) - if the results must be printed exactly as shown in the example

from math import *

def g(x):
    t = 10
    p = lambda i: x == i % t or i >= t and p(i / t)
    for i in range(-100, 101):
        if p(abs(i)):
            print i,
    print
# g(0)
# -100 -90 -80 -70 -60 -50 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100

Python: 71 - if the output can be done outside the function and the only restriction for the output is that no violating numbers are printed

f = lambda x: [(-i, i) for i in range(101) for j in (10, 100) if i % j == x or i >= j and i / j == x]
# print f(0)
# [(0, 0), (0, 0), (-10, 10), (-20, 20), (-30, 30), (-40, 40), (-50, 50), (-60, 60), (-70, 70), (-80, 80), (-90, 90), (-100, 100), (-100, 100)]
\$\endgroup\$
1
\$\begingroup\$

JavaScript 125 char

Sorry for the several edits I've been having troubles doing this from my phone :)

function c(n,i){o=i||0;h=100;j=o-h;f=Math.abs(j);m=f/10|0;if((m==n&&m!=0)||n==f%10||f/h==n)console.log(j);if(o<h*2)c(n,o+1);}
\$\endgroup\$
1
\$\begingroup\$

Dogelang, 42 non-whitespace characters

f=n->for i in(-100..101)=>if n in str i=>print i

Same as my Python solution, just converted to dogelang.

\$\endgroup\$

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