23
\$\begingroup\$

The alphanumeric characters have ASCII-values:

0-9  ->  48-57
A-Z  ->  65-90
a-z  ->  97-122

Your challenge is to take an integer as input, and output how many characters can be made using consecutive digits of that number. The character codes may be overlapping. 666 should result in 2, since you have 66 twice.

Test cases:

Input: 5698
Possible characters: '8' (56), 'E' (69), 'b' (98)
Output: 3

Input: 564693
Possible characters: '8' (56), 'E' (69)
Output: 2

Input: 530923864209124521
Possible characters: '5' (53), 'V' (86), '4' (52)  
Output: 3

Input: 1111111
Possible characters: 'ooooo' (5*111)
Output: 5

Input: 5115643141276343
Possible characters: '3' (51), '8' (56), 'L' (76), 's' (115)
Output: 4

Input: 56789
Possible characters: '8' (56), 'C' (67), 'N' (78), 'Y' (89)
Output: 4

Input: 94
Possible characters: ''
Output: 0

Input: 1
Output: 0

Input and output formats are optional (yes, you may take the integer as a string).

\$\endgroup\$
0

15 Answers 15

11
\$\begingroup\$

05AB1E, 8 7 bytes

žKÇIŒÃg

Try it online!

Explanation

žK       # push [a-zA-Z0-9]
  Ç      # convert to list of ascii codes
   IŒ    # push all substrings of input
     Ã   # keep only the subtrings which exist in the list of acsii codes
      g  # push length of resulting list
\$\endgroup\$
7
  • 1
    \$\begingroup\$ ŒžKÇÃg doesn't work? \$\endgroup\$ Commented Apr 28, 2017 at 13:29
  • \$\begingroup\$ @carusocomputing: Unfortunately, it fails the 1111111 test-case. \$\endgroup\$
    – Emigna
    Commented Apr 28, 2017 at 13:34
  • \$\begingroup\$ Ã, that makes a LOT more sense now that I read what it's doing, crap. \$\endgroup\$ Commented Apr 28, 2017 at 16:08
  • \$\begingroup\$ Maybe I'm missing something, but your current answer fails for the 1111111 test case (output is 1 instead of 5), and the 6-byter suggestion made by @MagicOctopusUrn actually gives the correct output. \$\endgroup\$ Commented Mar 25, 2020 at 11:29
  • \$\begingroup\$ @KevinCruijssen I think this may have been Legacy 05AB1E. \$\endgroup\$ Commented Mar 25, 2020 at 13:33
7
\$\begingroup\$

MATL, 17 13 bytes

8Y2"G@oVXf]vn

Try it online! Or verify all test cases.

Explanation

8Y2     % Predefined literal: string with all letters, uppercase and lowercase,
        % and digits
"       % For each character in that string
  G     %   Push input, for example the string '5115643141276343'
  @     %   Push current character, such as 'A'
  o     %   Convert to its ASCII code, such as 65
  V     %   String representation, such as '65'
  Xf    %   Find string '65' within string '5115643141276343'. This gives a vector
        %   (possibly empty) with indices of occurrences
]       % End
v       % Concatenate all stack contents vertically
n       % Number of entries. Implicitly display
\$\endgroup\$
6
\$\begingroup\$

Brachylog, 22 bytes

∧Ạụ:Ạ:Ịcạ:?{tT&h∋~sT}ᶜ

Try it online!

Explanation

       c                 Concatenate together:
∧Ạụ:                       "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    Ạ:                     "abcdefghijklmnopqrstuvwxyz"
      Ị                    "0123456789"
        ạ                Get the list of ASCII codes of that string
         :?{        }ᶜ   Count the number of results, for input [list of codes, Input], of:
            tT             Call the Input T
              &h∋          Take one ASCII code
                 ~sT       It is a substring of T
\$\endgroup\$
9
  • \$\begingroup\$ Unfortunately for me, fortunately for you, I don't have access to a computer at the moment ;) \$\endgroup\$
    – Leaky Nun
    Commented Apr 28, 2017 at 11:50
  • \$\begingroup\$ @LeakyNun I have thought of shorter ways to do it, both failing because of bugs. \$\endgroup\$
    – Fatalize
    Commented Apr 28, 2017 at 11:53
  • \$\begingroup\$ Can you join the two T together? \$\endgroup\$
    – Leaky Nun
    Commented Apr 28, 2017 at 11:56
  • 1
    \$\begingroup\$ What is the cause of this bug? \$\endgroup\$
    – Leaky Nun
    Commented Apr 28, 2017 at 12:28
  • 1
    \$\begingroup\$ @LeakyNun For e.g. the integer 13, there are infinitely many lists, and infinitely many integers which contain 13, and it's not obvious in which order you should list them. \$\endgroup\$
    – Fatalize
    Commented Apr 28, 2017 at 12:38
6
\$\begingroup\$

Java 8, 204 197 195 186 174 bytes

n->{int r=0,i=0,e=n.length()-1,t;for(;i<e;r+=t>47&t<57|t>64&t<91|t>96&t<100|(t=new Short(n.substring(i,i++>e-2?i:i+2)))>99&t<123?1:0)t=new Byte(n.substring(i,i+2));return r;}

-9 bytes thanks to @ceilingcat.

Try it online.

Explanation:

n->{                   // Method with String parameter and integer return-type
  int r=0,             //  Result, starting at 0
      i=0,             //  Index
      e=n.length()-1,  //  Length of String -1
      t;               //  Temp integer
  for(;i<e             //  Loop over the String using the index
      ;                //    After every iteration:
       r+=             //     Increase the result-sum by:
          t>47&t<57|t>64&t<91|t>96&t<100
                       //      If two adjacent digits are a digit or letter
          |            //      or
          (t=new Short(n.substring(i,i++>e-2?i:i+2)))>99&t<123?
                       //      if three adjacent digits are a letter:
           1           //       Increase the sum by 1
          :            //      Else:
           0)          //       Keep the sum the same by adding 0
    t=new Byte(n.substring(i,i+2));
                       //   Set `t` to the substring of [i, i+2) converted to integer
  return r;}           //  Return the result-sum
\$\endgroup\$
2
  • \$\begingroup\$ I cannot access a computer right now, so I cannot confirm, but here are two suggestions: 1. put the initiations into the for loop. 2. instead of string manipulation, use arithmetic (use integer division to iterate through the digits and use modulo to extract the last 2 or 3 digits). \$\endgroup\$
    – Leaky Nun
    Commented Apr 28, 2017 at 12:07
  • \$\begingroup\$ @LeakyNun Thanks for the suggestions. As for your first one, the reason the integers initializations are outside the for-loop is because I have to return the result (r). However, I have been able to golf 7 bytes by putting everything else inside the for-loop, in a single ternary. I will see if I can do your second suggestion later perhaps. My lunch-time is over again, so I'll have to get back to work. Will keep it in mind. \$\endgroup\$ Commented Apr 28, 2017 at 12:19
5
\$\begingroup\$

JavaScript (ES6), 71 70 bytes

f=([a,...b])=>a?(a&(a+=b[0])+b[1]<123|a>47&a<58|a>64&a<91|a>96)+f(b):0

Test cases

f=([a,...b])=>a?(a&(a+=b[0])+b[1]<123|a>47&a<58|a>64&a<91|a>96)+f(b):0

console.log(f("5698"))                // 3
console.log(f("564693"))              // 2
console.log(f("530923864209124521"))  // 3
console.log(f("1111111"))             // 5
console.log(f("5115643141276343"))    // 4
console.log(f("56789"))               // 4
console.log(f("94"))                  // 0
console.log(f("1"))                   // 0

\$\endgroup\$
4
\$\begingroup\$

Perl 5, 47 bytes

46 bytes of code + -p flag.

$"="|";$_=()=/(?=@{[48..57,65..90,97..122]})/g

Try it online!

I couldn't find any shorter way to write that 48..57,65..90,97..122: map{ord}0..9,a..z,A..Z (getting the ascii value of the characters) is one byte longer. And doing for$c(0..122){$\+=chr($c)=~/\pl|\d/ for/(?=$c)/g}}{ (looking for all numbers, but keeping only those whose numbers corresponds to the ascii value of letters (\pl) or digits (\d)) will be 5 bytes longer (note that \pl|\d can't be replaced by \w as the latter also includes underscores)).


Previous approach (49 bytes):

for$@(48..57,65..90,97..122){$\+=()=/(?=$@)/g}}{
\$\endgroup\$
2
\$\begingroup\$

PHP, 68 Bytes

for(;a&($a=$argn)[$i];)$d+=ctype_alnum(chr($a[$i].$a[++$i]));echo$d;

Online Version

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

JavaScript (ES), 165 161 156 154 153 bytes

Yeah, RegEx definitely wasn't the right tool for the job here!

n=>[/\d{2}/g,/\d{3}/g].map(e=>eval("while(x=e.exec(n)){a.push(m=x[0]);e.lastIndex-=m.length-1}"),a=[])|a.filter(x=>x>47&x<58|x>64&x<91|x>96&x<123).length

Try It

f=

n=>[/\d{2}/g,/\d{3}/g].map(e=>eval("while(x=e.exec(n)){a.push(m=x[0]);e.lastIndex-=m.length-1}"),a=[])|a.filter(x=>x>47&x<58|x>64&x<91|x>96&x<123).length

console.log(f(5698))//3
console.log(f(564693))//2
console.log(f(530923864209124521))//3
console.log(f(1111111))//5
console.log(f(5115643141276343))//4
console.log(f(56789))//4
console.log(f(94))//0
console.log(f(1))//0

\$\endgroup\$
1
  • \$\begingroup\$ Regexp isn't that bad; A port of my Retina answer came to 78 bytes. \$\endgroup\$
    – Neil
    Commented Apr 28, 2017 at 19:05
1
\$\begingroup\$

Retina, 52 bytes

&`1[01]\d|12[012]|4[89]|5[0-7]|6[5-9]|[78]\d|9[0789]

Try it online! (includes test suite)

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

Pyth, 19 17 14 bytes

l@jGUTmr0Csd.:

takes a string.

-3 Bytes thanks to @LeakyNun

Try it!

Explanation

l@jGUTmr0Csd.:
    UT                # the list of digits [0,1,2,...,9]
  jG                  # join that on the lowercase alphabet (repetition doesn't matter)
              Q       # implicit input
            .:        # all substrings of the input
      m               # for each of those substrings
          sd          # Convert the string to a base 10 integer
         C            # convert that integer to the character with that number
       r0             # make that character lowercase
l@                    # length of the intersection of those two list of chars we generated
 
\$\endgroup\$
3
  • \$\begingroup\$ Instead of using idT, you can use sd. \$\endgroup\$
    – Leaky Nun
    Commented Apr 29, 2017 at 2:09
  • \$\begingroup\$ Also, l@jGUTmr0Csd.: might be shorter (not sure if it works). \$\endgroup\$
    – Leaky Nun
    Commented Apr 29, 2017 at 2:20
  • \$\begingroup\$ @LeakyNun Thank you, this works! \$\endgroup\$
    – KarlKastor
    Commented Apr 29, 2017 at 10:37
1
\$\begingroup\$

Python 2, 74 64 62 bytes

f=lambda n:n and chr(n%10**(3-(n%1000>122))).isalnum()+f(n/10)

Try it online!

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

Haskell, 161 157 138 129 126 bytes

import Data.List
f x=sum[1|y<-nub$concat$words.concat<$>mapM(\c->[[c],c:" "])x,any(elem$read y)[[48..57],[65..90],[97..122]]]

I wonder if there is a better way to remove dupes of the list than importing Data.List for nub?

\$\endgroup\$
7
  • 1
    \$\begingroup\$ If you import Data.Lists instead of Data.List, you can use: y<-tail$powerslice x. \$\endgroup\$
    – nimi
    Commented Apr 30, 2017 at 8:50
  • \$\begingroup\$ @nimi Is it against golfing rules if I have to download and install non standard modules? I dont think Data.Lists is standard in GHC. \$\endgroup\$ Commented Apr 30, 2017 at 12:29
  • \$\begingroup\$ As far as I know we still don't have consensus on what counts as a standard module. There are a couple of Haskell answers around here which use Data.Lists. It is even mentioned in the golfing tips for Haskell - nobody has complained so far. \$\endgroup\$
    – nimi
    Commented Apr 30, 2017 at 15:54
  • \$\begingroup\$ @nimi Well honestly I think if I can just download any package from cabal, I could just write a function that solves the problem, upload it, then import the module in my solution. Technically I could cheat. But then certain challenges cant be done with basic GHC either like crypto stuff so I dont know. \$\endgroup\$ Commented Apr 30, 2017 at 20:58
  • 1
    \$\begingroup\$ Back to golfing tips: or $ f <$> list is any f list: any(elem$read y)[...]. \$\endgroup\$
    – nimi
    Commented Apr 30, 2017 at 21:15
0
\$\begingroup\$

Jelly, 8 bytes

ØBODf@ẆL

Input is a digit array.

Try it online!

How it works

ØBODf@ẆL  Main link. Argument: A (digit array)

ØB        Base 62; yield all alphanumeric ASCII characters.
  O       Ordinal; get their code points.
   D      Decimal; convert the code points into digit arrays.
      Ẇ   Window; yield all contiguous subarrays of A.
    f@    Filter swapped; keep all elements of the result to the right that appear
          in the result to the left.
       L  Length; count the matches.
\$\endgroup\$
0
\$\begingroup\$

Ruby, 50 bytes

p (0..~/$/).any?{|n|$_[n,2].to_i.chr=~/\p{Alnum}/}

Reads from standard input; requires the Ruby interpreter be invoked with the -n option (implicit while gets loop).

Could be reduced to 43 bytes if it were allowed to match underscores.

p (0..~/$/).any?{|n|$_[n,2].to_i.chr=~/\w/}
\$\endgroup\$
1
  • \$\begingroup\$ This doesn't return the number of times the characters show up. Also, it fails on 111, which should return 1 but you're giving back 0. \$\endgroup\$
    – Value Ink
    Commented May 2, 2017 at 23:09
0
\$\begingroup\$

Japt, 24 bytes

;è"(?={9ò ¬+B+C ¬mc q|})

Try it online!

Alternate 27-byte version:

è"(?={#~o f_d f"%d|%l"Ãq|})

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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