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\$
31
  • 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

1
2
1
\$\begingroup\$

Mathematica - 65 chars

f@n_:=If[IntegerDigits@#~MemberQ~n,Print@#]&~Scan~Range[-100,100]
\$\endgroup\$
2
  • \$\begingroup\$ Can you point me to a webpage that explains the ~ operator? \$\endgroup\$ Mar 12, 2014 at 9:49
  • \$\begingroup\$ Hi @MichaelStern, it's called infix form. Here is Wolfram page explain it. \$\endgroup\$
    – Murta
    Mar 12, 2014 at 10:05
1
\$\begingroup\$

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.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Actually it's 16 characters, rules say whitespace doesn't count. \$\endgroup\$
    – gronostaj
    Mar 12, 2014 at 21:57
1
\$\begingroup\$

Python 2 (70)

def f(i):
 n=-100
 while n<101:
  m=abs(n)
  if i in [m%10]+[m/10,m>99]*i: print n,
  n+=1

For i=0 to work, the *i makes the m/10 condition not be checked. When i=1, we get m=100 to be accepted by adding the condition m>99, which evaluates to True which equals 1.

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

Ruby

def f(n);-100.upto(100){|i|print"#{i} "if(i.abs/10==n||i.abs%10==n)};end
\$\endgroup\$
0
\$\begingroup\$

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;
}
\$\endgroup\$
1
  • 6
    \$\begingroup\$ Because it is a code-golf, please include the character count of your code in your answer. \$\endgroup\$
    – ProgramFOX
    Mar 5, 2014 at 17:10
0
\$\begingroup\$

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

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

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 << " ";
    }
}
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Welcome to PPCG! Seems you might have missed the rule "You cannot use strings or char arrays." :) \$\endgroup\$ Mar 6, 2014 at 6:00
  • \$\begingroup\$ Ah I missed that 's' on Strings, thought it meant no string arrays, oh well. \$\endgroup\$ Mar 6, 2014 at 21:06
0
\$\begingroup\$

Scala 123 characters

This works but doesn't scale to use larger numbers.

def a(n:Int) {
    for (x<- -100 until 100) {
        var y=x/10
        var z=x-((x/10) * 10)           
        if (x==n||(y==n&&n>0)||z==n){println(x)}
    }
}
\$\endgroup\$
0
\$\begingroup\$

LOLCODE: 364 characters (without whitespace)

HAI
I HAS A N ITZ 1
I HAS A VAR ITZ 0
I HAS A I
I HAS A J
IM IN YR LOOP
  UPZ VAR!!1
  I R PRODUKT OF 10 AN VAR
  I R SUM OF I AN N
  J R DIFF OF 0 AN I
  VISIBLE I
  VISIBLE J
  N BIGR THAN 0, O RLY?
    YA RLY
      I R PRODUKT OF 10 AN N
      I R SUM OF I AN VAR
      J R DIFF OF 0 AN I
      VISIBLE I
      VISIBLE J
  OIC 
  IZ VAR BIGR THAN 8?, GTFO, KTHX  
KTHX
N SMALLR THAN 2, O RLY?
  YA RLY
    VISIBLE "100"
    VISIBLE "-100"
OIC
KTHXBAI

Similar to my Fortran 95 code, it sometimes outputs a same number twice, but the rules say nothing about this being illegal. Numbers are NOT output in order, bu the rules also say nothing about it.

You can run this code at: http://asgaard.co.uk/misc/loljs/#

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

Fortran 95: 90 characters

n=9
do i=0,9
print*,10*i+n,-10*i-n
if(n>0)print*,10*n+i,-10*n-i
enddo
if(n<2)print*,100,-100
end

Note: it sometimes outputs a same number twice, but the rules say nothing about this being illegal

\$\endgroup\$
3
  • \$\begingroup\$ I think the output is not in the right order. \$\endgroup\$ Mar 7, 2014 at 0:08
  • 2
    \$\begingroup\$ but where does it say in the rules it should be ordered? \$\endgroup\$ Mar 7, 2014 at 0:09
  • 1
    \$\begingroup\$ @gilberto.agostinho.f It doesn't. In fact it doesn't even say only the numbers containing that digit or how many times they can be printed. \$\endgroup\$ May 13, 2014 at 18:01
0
\$\begingroup\$

Ruby - 59 chars

n=gets.chomp;$><<(-100..100).select{|x|x.to_s.include?(n)}

Ex:

1.9.3p448 :046 >   n=gets;$><<(-100..100).select{|x|x.to_s.include?(n[0])}
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] => #<IO:<STDOUT>> 
1.9.3p448 :047 > 
1.9.3p448 :048 >   
1.9.3p448 :049 >   n=gets;$><<(-100..100).select{|x|x.to_s.include?(n[0])}
0
[-100, -90, -80, -70, -60, -50, -40, -30, -20, -10, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100] => #<IO:<STDOUT>> 
1.9.3p448 :050 > 
1.9.3p448 :051 >
\$\endgroup\$
0
\$\begingroup\$

Ruby - 75 characters

Emphasizing clarity over cleverness:

def f(n)
  puts (-100..100).select {|x| x.abs % 10 == n || x.abs / 10 == n && n > 0}.join(" ")
end
\$\endgroup\$
0
\$\begingroup\$

R - 71 69 chars

z = -100:100
j = abs(z)
h = function(x)
   z[j %% 10 == x | j > 9 & x == j %/% 10 %% 10 | x & j > 98 + x]

     > h(0)
     [1] -100  -90  -80  -70  -60  -50  -40  -30  -20  -10    0   10   20   30   40   50   60   70
    [19]   80   90  100
    > h(1)
     [1] -100  -91  -81  -71  -61  -51  -41  -31  -21  -19  -18  -17  -16  -15  -14  -13  -12  -11
    [19]  -10   -1    1   10   11   12   13   14   15   16   17   18   19   21   31   41   51   61
    [37]   71   81   91  100
    > h(2)
     [1] -92 -82 -72 -62 -52 -42 -32 -29 -28 -27 -26 -25 -24 -23 -22 -21 -20 -12  -2   2  12  20  21
    [24]  22  23  24  25  26  27  28  29  32  42  52  62  72  82  92
\$\endgroup\$
0
\$\begingroup\$

Javascript (ECMAScript 6) - 83 Characters (excluding whitespace)

n=prompt()
s=''
f=x=>x%10==n|(x>9?f(x/10|0):0)
for(a=-1;a++<100;)
    if(f(a))
        s+=' '+a+' -'+a

Inputting 0 outputs:

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

Inputting 1 outputs:

" 1 -1 10 -10 11 -11 12 -12 13 -13 14 -14 15 -15 16 -16 17 -17 18 -18 19 -19 21 -21 31 -31 41 -41 51 -51 61 -61 71 -71 81 -81 91 -91 100 -100"
\$\endgroup\$
1
  • \$\begingroup\$ You have strings in your answer plus @duci9y said in the comments of the challenge that it has to be in order. \$\endgroup\$
    – Spedwards
    May 13, 2014 at 23:33
0
\$\begingroup\$

haskell, ghci: 75 characters

Prelude> let i='9'
Prelude> [x | x <- [-100..100], head (show x) == i || last (show x) == i]
[-99,-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]

It's a basic list comprehension that gets the range -100 to 100, then ( not sure if this was against the rules ) turns x into a char list and grabs the head and tail and compares it to i, which is the input, this acts as a filter for the range.

\$\endgroup\$
2
  • \$\begingroup\$ Could you perhaps add an in-answer explanation? This way, haskell ignorant people can know what you are doing. \$\endgroup\$
    – Justin
    Mar 12, 2014 at 7:40
  • \$\begingroup\$ @Quincunx I added an explanation, I hope it helps \$\endgroup\$
    – Newbrict
    Mar 12, 2014 at 7:53
0
\$\begingroup\$

Julia, 46

g(x)=filter((y)->x in digits(abs(y)),-100:100)
julia> g(9)'
1x38 Array{Int64,2}:
-99  -98  -97  -96  -95  -94  -93  …  91  92  93  94  95  96  97  98  99

Or 54 if I'm not allowed to return an array, seems like a silly restriction.

g(x)=(for y=-100:100;x in digits(abs(y))&&println(y);end)
\$\endgroup\$
0
\$\begingroup\$

Java 123

N(int n){for(int i=-100;i<101;i++){int x=Math.abs(i);if(x%10==n||x>9&&x/10%10==n||x>99&&x/100%10==n)System.out.print(i+" ");}}

Since the question only asks for a function and not a whole program this should do. Full program:

class N
{
    N(int n)
    {
        for(int i=-100;i<101;i++)
        {
            int x=Math.abs(i);
            if( x % 10 == n || x > 9 && x / 10 % 10 == n || x > 99 && x / 100 % 10 == n )
            System.out.print(i + " ");
        }
    }

    public static void main(String[] a)
    {
        N n = new N(Integer.parseInt(a[0]));
    }
}
\$\endgroup\$
0
\$\begingroup\$

Haskell, 155 124

That is, with only non-essential whitespace removed, everything else felt like cheating. The other Haskell answer broke both rules, so I decided to roll my own solution!

You didn't specify the order of the output, so I didn't to it in ascending order. I basically jumped to the relevant points on the number line, added either 1 or 10 ten times and printed each number.

t=(+10)
p=print
g x n f
 |n<10=p x>>p(-x)>>g(f x)(n+1)f
 |True=return()
f 0=g(-100)0 t>>p 0
f n=g(-10*n-9)0(+1)>>g(-90-n)0 t

Call like this:

λ> f 4
-49
49
-48
48
-47
47
-46
46
-45
45
-44
44
-43
43
-42
42
-41
41
-40
40
-94
94
-84
84
-74
74
-64
64
-54
54
-44
44
-34
34
-24
24
-14
14
-4
4
\$\endgroup\$
0
\$\begingroup\$

Javascript

function a(b){for(i=-100;i<=100;i++){if((i+'').indexOf(b)>-1){console.log(i)}}}

Usage:

> a(3)
-93
-83
-73
-63
-53
-43
-39
-38
-37
-36
-35
-34
-33
-32
-31
-30
-23
-13
-3
3
13
23
30
31
32
33
34
35
36
37
38
39
43
53
63
73
83
93
\$\endgroup\$
0
\$\begingroup\$

C, 99 (or 46)

main(i,j){for(j=getchar()-48,i=101;i;)if(--i%10==j||i/10==j||(j<i&&i/10==j))printf("%d %d ",i,-i);}

results (0 twice , but that was not in the parameters):

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

but the question says all the numbers... not only the numbers, so you can just print all of them

main(i){for(i=101;i--;)printf("%d %d ",i,-i);}

results:

100 -100 99 -99 98 -98 97 -97 96 -96 95 -95 94 -94 93 -93 92 -92 91 -91 90 -90 89 -89 88 -88 87 -87 86 -86 85 -85 84 -84 83 -83 82 -82 81 -81 80 -80 79 -79 78 -78 77 -77 76 -76 75 -75 74 -74 73 -73 72 -72 71 -71 70 -70 69 -69 68 -68 67 -67 66 -66 65 -65 64 -64 63 -63 62 -62 61 -61 60 -60 59 -59 58 -58 57 -57 56 -56 55 -55 54 -54 53 -53 52 -52 51 -51 50 -50 49 -49 48 -48 47 -47 46 -46 45 -45 44 -44 43 -43 42 -42 41 -41 40 -40 39 -39 38 -38 37 -37 36 -36 35 -35 34 -34 33 -33 32 -32 31 -31 30 -30 29 -29 28 -28 27 -27 26 -26 25 -25 24 -24 23 -23 22 -22 21 -21 20 -20 19 -19 18 -18 17 -17 16 -16 15 -15 14 -14 13 -13 12 -12 11 -11 10 -10 9 -9 8 -8 7 -7 6 -6 5 -5 4 -4 3 -3 2 -2 1 -1 0 0
\$\endgroup\$
0
\$\begingroup\$

Python2 - 54 chars

def f(s):
    for i in range(-100, 100):
        if str(s) in str(i):
            print i,

Example:

Python 2.7.9 (default, Dec 11 2014, 04:42:00) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def f(s):
...     for i in range(-100, 100):
...         if str(s) in str(i):
...             print i,
... 
>>> f(0)
-100 -90 -80 -70 -60 -50 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90

After removing all white spaces, we end up with these 54 chars:

deff(s):foriinrange(-100,100):ifstr(s)instr(i):printi,
\$\endgroup\$
0
\$\begingroup\$

Scala (97)

def a(b:Int)=for(n←-100 to 100){val m=n.abs;if(b>0&&(m/100==b||m/10==b)||m-m/10*10==b)println(n)}

Ungolfed

def a(b:Int)=
    for(n <- -100 to 100){
        val m=n.abs;
        if((b > 0 && (m/100==b || m/10==b)) || m-((m / 10) * 10) == b) {
            println(n)
        }
    }
\$\endgroup\$
0
\$\begingroup\$

Excel VBA, 112 Chars

Sub a(n)
    For i = -100 To 100
        j = Abs(i)
        s = IIf(n * (n = j Mod 10 Or n = Int(j / 10) Or n = Int(j / 100)) Or n = i Mod 10, s & " " & i, s)
    Next
    Debug.? s
End Sub

Traditional PPCG Format, 132 Bytes

Sub a(n):For i=-100To 100:j=Abs(i):s=IIf(n*(n=j Mod 10 Or n=Int(j/10)Or n=Int(j/100))Or n=i Mod 10,s &" "&i,s):Next:Debug.?s:End Sub

note: +5 for : newlines in PPCG format

Old Version, 99 Chars (Non-Competing)

Breaks rule against using strings

Sub a(n$)
    For i = -100 To 100
        j = Abs(i)
        s = IIf(n * (n = j Mod 10 Or n = Left(j, 1)) Or n = i Mod 10, s & " " & i, s)
    Next
    Debug.?s
End Sub

Traditional PPCG Format, 118 Bytes

Sub a(n$):For i=-100To 100:j=Abs(i):s=IIf(n*(n=j Mod 10 Or n=Left(j,1))Or n=i Mod 10,s &" "&i,s):Next:Debug.?s:End Sub

note: again, +5 for : newlines in PPCG format

Usage

In the VBE Immediates Window

  For i = 0 To 9: Print i & ":": a (i): Next
0:
 -100 -90 -80 -70 -60 -50 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100
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
2:
 -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
3:
 -93 -83 -73 -63 -53 -43 -39 -38 -37 -36 -35 -34 -33 -32 -31 -30 -23 -13 -3 3 13 23 30 31 32 33 34 35 36 37 38 39 43 53 63 73 83 93
4:
 -94 -84 -74 -64 -54 -49 -48 -47 -46 -45 -44 -43 -42 -41 -40 -34 -24 -14 -4 4 14 24 34 40 41 42 43 44 45 46 47 48 49 54 64 74 84 94
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
6:
 -96 -86 -76 -69 -68 -67 -66 -65 -64 -63 -62 -61 -60 -56 -46 -36 -26 -16 -6 6 16 26 36 46 56 60 61 62 63 64 65 66 67 68 69 76 86 96
7:
 -97 -87 -79 -78 -77 -76 -75 -74 -73 -72 -71 -70 -67 -57 -47 -37 -27 -17 -7 7 17 27 37 47 57 67 70 71 72 73 74 75 76 77 78 79 87 97
8:
 -98 -89 -88 -87 -86 -85 -84 -83 -82 -81 -80 -78 -68 -58 -48 -38 -28 -18 -8 8 18 28 38 48 58 68 78 80 81 82 83 84 85 86 87 88 89 98
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\$

OOps. Fails the 0 test

Python 160-70= 90

n = int(raw_input())
for x in xrange(-100,101):
    num = abs(x)
    while num:
        if n == num%10:
            print x
            break
        num /= 10

this is the old wrong code (see comments):

Python 141 - 0 = 141

import re;n=raw_input('>');print "\n".join([str(j) for j in filter(lambda x:re.compile("-?.*"+str(n)+".*").match(str(x)),xrange(-100,101))])
\$\endgroup\$
8
  • 3
    \$\begingroup\$ You cannot use strings! \$\endgroup\$
    – duci9y
    Mar 5, 2014 at 14:14
  • \$\begingroup\$ Besides the fact that you're using strings, why use REs? You could do this without them: n=raw_input(); print " ".join(filter(lambda x: n in x, map(str, range(-100, 101)))) \$\endgroup\$
    – Oberon
    Mar 5, 2014 at 14:49
  • \$\begingroup\$ @Oberon, you can do it also without lambda and map: s=input();print" ".join([`i`for i in range(-100,101)if`s`in`i`]). \$\endgroup\$
    – manatwork
    Mar 5, 2014 at 14:52
  • \$\begingroup\$ @manatwork, your code gives me a SyntaxError. \$\endgroup\$
    – Oberon
    Mar 5, 2014 at 14:56
  • 1
    \$\begingroup\$ Doesn't work with 0. \$\endgroup\$
    – duci9y
    Mar 5, 2014 at 18:28
-1
\$\begingroup\$

Java

    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

\$\endgroup\$
1
  • \$\begingroup\$ Can you include the language name and the character count please? \$\endgroup\$
    – ProgramFOX
    Mar 6, 2014 at 12:37
1
2

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