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 2 days ago
1  
Why the downvotes? –  duci9y 2 days ago
12  
A Whitespace answer will score 0. Are you sure you want to do that? –  ace 2 days ago
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 2 days ago
2  
@ace I created an answer in whitespace (needed 4 hours of hard work with this). i think that it scores zero, right? –  Victor yesterday
show 20 more comments

43 Answers

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
11  
+1 exploiting "0 if you subtract whitespaces" –  Jwosty yesterday
2  
Haha, I learnt a lesson. :P –  duci9y yesterday
 
I never understood this language. Everything seems highly ambiguous to me. How do you know when a number finishes, and the next command begins? –  Cruncher yesterday
1  
@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. yesterday
1  
I learnt Whitespace today. codegolf.stackexchange.com/a/23216/17886 –  duci9y yesterday
show 5 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
3  
grep isn't bash or sh. It's another utility! –  devnull 2 days ago
 
i have to agree, it is not pure shell/bash :) –  Nik O'Lai 2 days ago
1  
Sees question. Makes answer in mind. Sees exact same answer already posted. Darn! +1 –  The Guy with The Hat yesterday
4  
I think this is using strings. –  David Conrad yesterday
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 yesterday
show 3 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
add comment

bash 55

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 yesterday
 
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 yesterday
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 yesterday
 
Oops. You're right, @duci9y. Corrected. –  manatwork yesterday
add comment

JavaScript 73 - 0 because no whitespace, = 73

for(r=prompt(),i=-100;101>i;)~(++i).toString().search(r)&&console.log(i)

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
share|improve this answer
1  
Even a bit shorter: for(i=-100,r=prompt();101>i;i++)(i+'').match(r)&&console.log(i). –  VisioN 2 days ago
2  
I like this answer (+1), but doesn't it implicitly use strings? –  Charles 2 days ago
2  
this does use implicit string conversion –  Claudiu yesterday
3  
convincing argument until you realize that regular expressions are a string op. It doesn't matter that it outputs a number –  VoronoiPotato yesterday
1  
@VoronoiPotato printing is also a string op. But we are obviously required to do that. –  Tim Seguine 20 hours ago
show 9 more comments

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 yesterday
 
@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 yesterday
 
That's great! I was of the belief that esoteric languages are just for fun. Good to see that's not the case. –  duci9y yesterday
add comment

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 yesterday
 
@duci9y Because none is necessary. I'll format to be more readable and add that as well, just for good measure. –  Gaffi yesterday
 
Doesn't work with 0. –  duci9y yesterday
 
@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 yesterday
 
@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 yesterday
show 2 more comments

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 yesterday
 
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 yesterday
 
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 yesterday
 
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 yesterday
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 7 hours ago
 
@Marta, Yes, I subverted the intent of the puzzle (which is why I said "Sorry"), while technically providing an appropriate answer. –  David Carraher 5 hours ago
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 yesterday
 
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 yesterday
1  
"199 Zeichen"? :) –  m.buettner 19 hours ago
 
Didn't realize that one :D Too much coding in native language in the past days I guess ;) –  Jochen Reinschlüssel 11 hours ago
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 2 days ago
 
@duci9y Added it. –  Oberon 2 days ago
 
Won't this output the results out of order? (Spec has been updated to state that order matters.) –  Gaffi 2 days ago
 
@Gaffi Well then. I shall edit the code. –  Oberon 2 days ago
2  
Fails to notice that 0 contains 0. –  ugoren 2 days ago
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 yesterday
 
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 yesterday
 
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 yesterday
add comment

Prolog: 75

f(D) :-
  between(-100,100,N) ,
  number_chars(N,Ds) ,
  member(D,Ds) ,
  writeln(N) ,
  fail
  .
share|improve this answer
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 yesterday
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 2 days ago
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
 
How do I test this? –  duci9y yesterday
 
See updated response. The "h" function is the one to call. –  intx13 yesterday
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 yesterday
 
@duci9y Yes. Specifically because: IIf(d = 0, IIf(d = n Mod 10, " " & n, "") –  Gaffi yesterday
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

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 yesterday
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

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 yesterday
 
Should be fixed now –  D. Kasipovic yesterday
 
1 works nicely, but now 0 prints 1, 2, 3... Sorry to be a troublemaker here. –  DocMax yesterday
 
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 –  D. Kasipovic yesterday
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 yesterday
show 3 more comments

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

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

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
5  
Because it is a code-golf, please include the character count of your code in your answer. –  ProgramFOX 2 days ago
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

C++ - 235 char

First post on here, gave it a shot

#include <iostream>
#include <string>
using namespace std;
int main()
{
    int input;
    cin >> input;
    std::string is = std::to_string(input);
    for(int i = -100; i < 101; i++){
        std::string os = std::to_string(i);
        if(os.find(is) != std::string::npos)
            cout << os << " ";
    }
}
share|improve this answer
1  
Welcome to PPCG! Seems you might have missed the rule "You cannot use strings or char arrays." :) –  Jonathan Van Matre yesterday
 
Ah I missed that 's' on Strings, thought it meant no string arrays, oh well. –  CodeSamurai 20 hours ago
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
    public static void main(String[] argv) {
    for(int i=-99;i<=99;i++)
    {
          int a,b;
          a=i/10;
          b=i%10;
          if(a==9||b==9||a==-9||b==-9){
              System.out.println(i);
                        }
    }
                                     }

JAVA:: O/P -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
 
Can you include the language name and the character count please? –  ProgramFOX yesterday
 
Also, it's not allowed to use strings in your program. –  ProgramFOX yesterday
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.