24
\$\begingroup\$

On 4chan, a popular game is get. Every post on the site gets a sequential post ID. Since you can't influence or determine them, people try to guess (at least a part of) their own post number, usually the first few digits. Another version of the game is called dubs, and it's goal is to get repeating digits at the end of the number (i.e 1234555).

Your task, if you wish to accept it, is to write a program that takes a post id as input (standard integer, you can assume below 2^32), and returns how many repeating digits are on the end.

Rules

  • Standard loopholes are disallowed.
  • The program can be a function, full program, REPL command, whatever works, really, as long as no external uncounted code/arguments are needed to run it.
  • Input can come from STDIN, function arguments, command line argument, file, whatever suits you.

Test Cases

Input: 14892093
Output: 1

Input: 12344444
Output: 5

Input: 112311
Output: 2

Input: 888888
Output: 6

Input: 135866667 //Post number I got on /pol/ few days ago, rip
Output: 1
\$\endgroup\$
  • 1
    \$\begingroup\$ Are we allowed to take input as string? \$\endgroup\$ – Dead Possum Mar 28 '17 at 9:11
  • 6
    \$\begingroup\$ @DeadPossum I would assume that's allowed, since you get a string anyway if you read the input from STDIN, command-line argument or file (which are all admissible input methods). \$\endgroup\$ – Martin Ender Mar 28 '17 at 9:19
  • 1
    \$\begingroup\$ Can we assume that the input will be greater than 0? \$\endgroup\$ – Martin Ender Mar 28 '17 at 10:01
  • 1
    \$\begingroup\$ @MartinEnder Yes \$\endgroup\$ – sagiksp Mar 28 '17 at 10:05
  • 2
    \$\begingroup\$ Upvote for the dubs game! Check'em! \$\endgroup\$ – ZombieChowder Mar 28 '17 at 12:31

47 Answers 47

1
\$\begingroup\$

C, 62 55 bytes

i,j;f(n){for(i=0,j=n%10;++i;n/=10)if(j-n%10)return--i;}

Try it online!

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

REXX, 50 bytes

arg ''-1 # 1 n
say length(n)-length(strip(n,T,#))

Parse argument with empty string, causing parsing cursor to move to end of argument, then left one step, put remainder (one character) of argument in # variable, move to first character and put first word of argument in n variable.

Strip trailing characters equivalent to # from n and compare length to n.

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

Octave, 30 bytes

@(s)find(flip(diff([0 +s])),1)

Try it online!

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

Ruby, 28 22 bytes

->s{s[/(.)\1*$/].size}

Anonymous function that takes the number as a string.

\$\endgroup\$
  • 1
    \$\begingroup\$ I believe you can get the match of a regex in Ruby like s[/(.)\1*$/]. \$\endgroup\$ – user81655 Mar 28 '17 at 13:57
1
\$\begingroup\$

C (clang), 41 bytes

f(x){return x&&x%10-x/10%10?1:f(x/10)+1;}

There are already 2 other C solutions, but they both depend on loops. This uses recursion. If the last 2 digits are different then it's 1, otherwise you do 1+ the number of consecutive digits of the number with the last digit removed.

Try it online!

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

JavaScript (ES6), 53 bytes

f=(n,i=1)=>(a=[...n]).pop()==a[a.length-1]?f(a,i+1):i
\$\endgroup\$
1
\$\begingroup\$

Japt, 23 bytes

¬â l ¥1?Ul :Uw ¬b@X¦UgJ

Try it online!

Explanation:

¬â l ¥1?Ul :Uw ¬b@X¦UgJ
¬                         // Split the input into an array of chars
 â                        // Return all unique permutations
   l ¥1                   // Check if the length is equal to 1
       ?                  // If yes, return: 
        Ul                //   The length of the input
            :             // Else, return:
             Uw           //   The input, reversed
                ¬         //   Split into an array of chars
                  @       //   Iterate through, where X is the iterative item
                 b        //   Return the first index where:
                   X¦     //     X != 
                     UgJ  //     The last char in the input
\$\endgroup\$
1
\$\begingroup\$

Befunge-93, 45 bytes

<_v#`0:~
pv>$00p110
p>00g-#v_10g1+10
  @.g01<

Try it online!

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

Clojure, 48 bytes

#(-> (partition-by identity (str %)) last count)

Usage: (#(-> (partition-by identity (str %)) last count) 123455)

=> 2

Ungolfed:

(defn count-digits [n]
                  (-> (partition-by identity (str n))
                      last
                      count))

Common Lisp, 82 bytes

(defun c (n) (if (eq (mod n 10) (floor (mod n 100) 10)) (+ 1 (c (floor n 10))) 1))

Ungolfed:

(defun c (n)
  (if (eq (mod n 10)
          (floor (mod n 100) 10))
      (+ 1 (c (floor n 10))) 1))
\$\endgroup\$
  • 1
    \$\begingroup\$ Welcome To PPCG! Wonderful first post, It looks like you have the basics of formatting down, just note that in the future you an make multiple answers if you use multiple languages (good for getting more rep :) ) \$\endgroup\$ – Taylor Scott Mar 31 '17 at 1:50
1
\$\begingroup\$

Haskell, 36 bytes

Without import solution (39 bytes)

f n=length.takeWhile(==last n)$reverse n

Note that a pointfree solution is longer (and less understandable): EDIT: since anonymous functions are allowed, let's shave off two bytes which brings us to 39 (still longer than 36):

(length.).takeWhile.(==).last<*>reverse

With import solution (36 bytes, 19 without import)

import Data.List
f=length.last.group

Run like f "2394398222222" either way

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

Jelly, 4 bytes

ŒrṪṪ

Takes input as a string, returns a number.

Try it online!

Relatively simply, Œr Run-length encodes the input, pushes the last pair, then pushes its second item. Which thus gives us the length of consecutive digits at the end of the string.

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

PHP 40chars

(Does not take into account php tags)

strlen(preg_match('/(.)\1*$/',$argv[1]))
\$\endgroup\$
  • \$\begingroup\$ I think you mean preg_match('/(.)\1*$/',$argv[1],$t);echo strlen($t[0]); or as alternative <?=strlen(preg_filter('/.*?((.)\2*)$/','\1',$argv[1])); \$\endgroup\$ – Jörg Hülsermann Mar 31 '17 at 2:08
0
\$\begingroup\$

C#, 49 bytes

i=>i.Reverse().TakeWhile(x=>x==i.Last()).Count();

Try it here

Full code

  using System;
  using System.Linq;

  class Program
  {
      static void Main()
      {
          Func<string, int> func = i=>i.Reverse().TakeWhile(x=>x==i.Last()).Count();

          Console.WriteLine(func("14892093"));
          Console.WriteLine(func("12344444"));
          Console.WriteLine(func("112311"));
          Console.WriteLine(func("888888"));
          Console.WriteLine(func("135866667"));
          Console.ReadLine();
      }
  }
\$\endgroup\$
0
\$\begingroup\$

Java 7, 64 62 bytes

Golfed:

int w(int x){int r=1,d=x%10;while((x/=10)%10==d)r++;return r;}

Ungolfed:

int w(int x)
{
    int r = 1, d = x % 10;
    while ((x /= 10) % 10 == d)
        r++;
    return r;
}

Try it online

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

Vim - 39

ix^[Y$d?[^^R=@@[len(@@)-2]^M]^Mcc^R=len(@2)^M

(^[, ^Ms and ^Rs are one char each)

  • insert non-number (for 88888 input), copy line, move to the end
  • delete searching backwards for a char other then the last char
  • replace line with length of deleted text
\$\endgroup\$
0
\$\begingroup\$

J, 14 bytes

>:i.&1}.~:|.":

Try it online!

It's fortunate that the challenge allows a REPL command. As a verb (function) it's 20 bytes:

3 :'>:i.&1}.~:|.":y'

For arbitrarily large integers, the literal must be suffixed by x.

Explained

Applied right to left, all monadic verbs:

  • ": Format - Stringifies its argument
  • |. Reverse
  • ~: Nub sieve - Produces a boolean array of 1's each time a new item is introduced, 0 otherwise
  • }. Behead - Removes first item of array
  • i.&1 Index of - Gets index of first 1. i. is actually a dyadic verb, but &1 ties right argument to 1.
  • >: Increment
\$\endgroup\$
0
\$\begingroup\$

Common Lisp, 72 chars

(lambda(i)(do((n i(floor n 10))(c 1(1+ c)))((/= 0(mod(mod n 100)11))c)))

Unoglfed

(lambda (i)
  (do ((n i (floor n 10))
       (c 1 (1+ c)))
      ((/= 0 (mod (mod n 100) 11)) c)))

Explanation

(/= 0 (mod (mod n 100) 11))

This checks to see if the last two digits of n are different- if n%100 is not divisible by 11.

(do ((n i (floor n 10))
     (c 1 (1+ c)))
    (... c))

do is a powerful generic looping construct in lisp. Its first argument is a list describing the looping variables. Each element is a list of (variable initial-value step-form) where step-form is evaluated each time through the loop to update that variable. The second argument is a test to perform to end the loop (the test I covered above) and a list of values to return after looping (in this case just c, which has been incrementing each time though the loop).

(lambda (i) ...) Wraps the operation in an anonymous function that takes the number as an integer.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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