Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

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.

share|improve this question
1  
The given output is either wrong or unclear. Where are -98 ... -91 and 91 ... 98? –  Oberon Mar 5 at 12:29
1  
@Gaffi The order does matter. –  duci9y Mar 5 at 13:18
17  
A Whitespace answer will score 0. Are you sure you want to do that? –  ace Mar 5 at 13:21
3  
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. –  microbian Mar 5 at 17:24
2  
@ace I created an answer in whitespace (needed 4 hours of hard work with this). i think that it scores zero, right? –  Victor Mar 6 at 4:02
show 22 more comments

55 Answers 55

up vote 59 down vote accepted

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
share|improve this answer
21  
+1 exploiting "0 if you subtract whitespaces" –  Jwosty Mar 6 at 14:30
5  
Haha, I learnt a lesson. :P –  duci9y Mar 6 at 15:54
2  
@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. –  Tim S. Mar 6 at 17:45
1  
I learnt Whitespace today. codegolf.stackexchange.com/a/23216/17886 –  duci9y Mar 6 at 17:50
4  
I interpret "Whitespace will NOT be counted." to mean this answer is invalid. ;) +1 –  Tim Seguine Mar 6 at 21:17
show 8 more comments

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
share|improve this answer
4  
grep isn't bash or sh. It's another utility! –  devnull Mar 5 at 17:14
    
i have to agree, it is not pure shell/bash :) –  Nik O'Lai Mar 5 at 17:24
1  
Sees question. Makes answer in mind. Sees exact same answer already posted. Darn! +1 –  The Guy with The Hat Mar 5 at 22:32
6  
I think this is using strings. –  David Conrad Mar 5 at 22:36
1  
@DavidConrad If you look at it that way, everything uses strings. The program source is a string. The terminal uses strings. Etc. –  Colonel Thirty Two Mar 6 at 0:03
show 4 more comments

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
share|improve this answer
1  
Even a bit shorter: for(i=-100,r=prompt();101>i;i++)(i+'').match(r)&&console.log(i). –  VisioN Mar 5 at 13:02
2  
I like this answer (+1), but doesn't it implicitly use strings? –  Charles Mar 5 at 17:19
2  
this does use implicit string conversion –  Claudiu Mar 5 at 18:40
1  
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.lo‌​g(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... –  WallyWest Mar 5 at 20:59
1  
@VoronoiPotato printing is also a string op. But we are obviously required to do that. –  Tim Seguine Mar 6 at 21:26
show 10 more comments

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

share|improve this answer
    
this uses strings, though –  Claudiu Mar 26 at 16:51
    
It also uses arrays: 201, creates an array containing 0 to 200. –  Rees May 13 at 7:28
add comment

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
share|improve this answer
    
Fails the 0 test. –  duci9y Mar 5 at 18:21
    
Oops. You're right, @duci9y. Corrected. –  manatwork Mar 5 at 19:52
add comment

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
share|improve this answer
    
I wonder if languages like K have any productive uses outside of scenarios like this question. Do they? –  duci9y Mar 6 at 7:30
1  
@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. –  algorithmshark Mar 6 at 8:15
    
That's great! I was of the belief that esoteric languages are just for fun. Good to see that's not the case. –  duci9y Mar 6 at 9:02
add comment

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
share|improve this answer
    
You can replace " && " with "&&" for two extra points. Also I count one less char if I drop the trailing newline. –  DigitalTrauma Mar 5 at 18:37
1  
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? –  DigitalTrauma Mar 5 at 18:43
    
Then you can convert the ;s to newlines, and save 2 points. –  joeytwiddle Mar 8 at 9:35
add comment

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.

share|improve this answer
1  
This looks like implicit string conversion, violating the no strings rule. –  VoronoiPotato Mar 5 at 21:45
    
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). –  Ismael Miguel Mar 5 at 22:03
    
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. –  David Conrad Mar 5 at 22:48
    
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). –  Ismael Miguel Mar 5 at 23:14
add comment

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... :/

share|improve this answer
    
Sorry, doesn't work with 0 :( –  duci9y Mar 6 at 7:38
    
Aw, sure, last addition I made messed that up. Updated Code, so I'm down to 199 now. Thanks for pointing that out! –  Jochen Reinschlüssel Mar 6 at 7:45
1  
"199 Zeichen"? :) –  Martin Büttner Mar 6 at 22:30
    
Didn't realize that one :D Too much coding in native language in the past days I guess ;) –  Jochen Reinschlüssel Mar 7 at 6:36
add comment

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;
    }
}
share|improve this answer
    
Nice. Could you also post an ungolfed version with proper whitespace please? –  duci9y Mar 5 at 12:49
    
@duci9y Added it. –  Oberon Mar 5 at 13:24
    
Won't this output the results out of order? (Spec has been updated to state that order matters.) –  Gaffi Mar 5 at 13:37
    
@Gaffi Well then. I shall edit the code. –  Oberon Mar 5 at 13:53
3  
Fails to notice that 0 contains 0. –  ugoren Mar 5 at 17:47
show 2 more comments

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
share|improve this answer
    
Why no whitespace? –  duci9y Mar 5 at 18:11
    
@duci9y Because none is necessary. I'll format to be more readable and add that as well, just for good measure. –  Gaffi Mar 5 at 18:12
    
Doesn't work with 0. –  duci9y Mar 5 at 18:31
    
@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. –  Gaffi Mar 5 at 18:43
    
@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. –  Gaffi Mar 5 at 18:54
show 2 more comments

Prolog: 75

f(D) :-
  between(-100,100,N) ,
  number_chars(N,Ds) ,
  member(D,Ds) ,
  writeln(N) ,
  fail
  .
share|improve this answer
    
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. –  moose Mar 8 at 18:53
add comment

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.

share|improve this answer
    
Awesome! Welcome to Code Golf. –  duci9y Mar 6 at 15:57
add comment

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. :)

share|improve this answer
    
But this do not use n as a filter –  Murta Mar 7 at 10:20
    
@Marta, Yes, I subverted the intent of the puzzle (which is why I said "Sorry"), while technically providing an appropriate answer. –  David Carraher Mar 7 at 12:34
add comment

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
share|improve this answer
    
This isn't much different from an existing answer. –  devnull Mar 5 at 17:17
add comment

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)

        }
        }
share|improve this answer
add comment

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
share|improve this answer
    
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). –  Desty Mar 6 at 12:34
    
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. –  algorithmshark Mar 6 at 16:12
    
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. –  Desty Mar 6 at 16:40
add comment

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

share|improve this answer
1  
And I discovered a new language today. :) –  duci9y Mar 6 at 15:54
add comment

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).

share|improve this answer
    
If x=1, this fails to print -100 or 100. –  DocMax Mar 5 at 20:54
    
Should be fixed now –  DKasipovic Mar 5 at 21:36
    
1 works nicely, but now 0 prints 1, 2, 3... Sorry to be a troublemaker here. –  DocMax Mar 5 at 22:34
    
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 –  DKasipovic Mar 5 at 23:15
1  
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);.... –  DocMax Mar 6 at 0:04
show 6 more comments

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]
share|improve this answer
add comment

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
...
share|improve this answer
add comment

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
share|improve this answer
    
Does this work with 0? –  duci9y Mar 5 at 18:28
    
@duci9y Yes. Specifically because: IIf(d = 0, IIf(d = n Mod 10, " " & n, "") –  Gaffi Mar 5 at 18:31
add comment

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 
share|improve this answer
add comment

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.)

share|improve this answer
add comment

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.

share|improve this answer
add comment

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);}
share|improve this answer
add comment

Mathematica - 65 chars

f@n_:=If[IntegerDigits@#~MemberQ~n,Print@#]&~Scan~Range[-100,100]
share|improve this answer
    
Can you point me to a webpage that explains the ~ operator? –  Michael Stern Mar 12 at 9:49
    
Hi @MichaelStern, it's called infix form. Here is Wolfram page explain it. –  Murta Mar 12 at 10:05
add comment

Powershell - 20 17 chars

To match the digit 9 (for example):

-100..100-match 9

EDIT: found I could remove brackets and one piece of whitespace.

share|improve this answer
    
Actually it's 16 characters, rules say whitespace doesn't count. –  gronostaj Mar 12 at 21:57
add comment

C#

IEnumerable<int> f(int a)
{
    yield a;
    yield -a;
    for(i = 1; i < 10; i++)
    {
        yield (i * 10) + a;
        yield (i * -10) - a
    }

    if (a != 1) yield break;
    yield 100;
    yield -100;
}
share|improve this answer
6  
Because it is a code-golf, please include the character count of your code in your answer. –  ProgramFOX Mar 5 at 17:10
add comment

Python, 91 (or possibly 90)

f = lambda x: filter(lambda y: x in(abs(y) % 10, abs(y)/10, abs(y)/100) if x else y%10 < 1, xrange(-100,101))

Examples:

>>> f(0)
[-100, -90, -80, -70, -60, -50, -40, -30, -20, -10, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
>>> f(1)
[-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]

If lists do not count as arrays, we can get rid of the x in xrange and maintain correctness for a total score of 90. If we remove unnecessary whitespace, the total length becomes 97 (or 96).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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