27
\$\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

56 Answers 56

1
2
2
\$\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.

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Python 3 - 50 44 bytes

Full program (in Python 3, input() returns a string, no matter the input):

g=input();print(len(g)-len(g.rstrip(g[-1]))) 
| improve this answer | |
\$\endgroup\$
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!

| improve this answer | |
\$\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.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Octave, 30 bytes

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

Try it online!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Ruby, 28 22 bytes

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

Anonymous function that takes the number as a string.

| improve this answer | |
\$\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\$

JavaScript (ES6), 53 bytes

f=(n,i=1)=>(a=[...n]).pop()==a[a.length-1]?f(a,i+1):i
| improve this answer | |
\$\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
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Befunge-93, 45 bytes

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

Try it online!

| improve this answer | |
\$\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))
| improve this answer | |
\$\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

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

05AB1E, 3 bytes

Åγθ

Try it online or verify all test cases.

Or alternatively:

γθg

Try it online or verify all test cases.

Explanation:

Åγ   # Run-length decode the (implicit) input-integer
     #  i.e. 1333822 → [1,3,1,2]
  θ  # Pop and push the last item
     #  → 2
     # (which is output implicitly as result)

γ    # Split the (implicit) input-integer into parts of subsequent equal digits
     #  i.e. 1333822 → [1,333,8,22]
 θ   # Pop and push the last item
     #  → 22
  g  # Pop and push its length
     #  → 2
     # (which is output implicitly as result)
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Pyth, 4 bytes

her9

Kinda surprised this hasn't been posted yet.

Try it online!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Japt, 6 bytes

ì òÎÌÊ

Try it or run all test cases

ì òÎÌÊ     :Implicit input of integer
ì          :To digit array
  ò        :Partition between truthy pairs
   Î       :   Sign of difference
    Ì      :Last element
     Ê     :Length
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

APL (Dyalog Extended), 9 bytes

⊃⍸⌽1,2≠/⍞

input as a string

1,2≠/ 1 concatenated with n-wise reduction with ≠, boolean mask of the starts of sections

reverse

⊃⍸ index of the first 1

Try it online!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ @Razetime true, edited \$\endgroup\$ – rak1507 Nov 3 at 13:36
1
\$\begingroup\$

Vyxal, 3 bytes

ŮtL

Almost 100% ASCII.

Explained

ŮtL
Ů    # Group the integer into consecutive chunks
 t   # Take the tail of that list
  L  # Find the length
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ need to get this to 2 bytes somehow \$\endgroup\$ – Razetime Nov 19 at 4:01
0
\$\begingroup\$

PHP 40chars

(Does not take into account php tags)

strlen(preg_match('/(.)\1*$/',$argv[1]))
| improve this answer | |
\$\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();
      }
  }
| improve this answer | |
\$\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

| improve this answer | |
\$\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
| improve this answer | |
\$\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
| improve this answer | |
\$\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.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

x86-16 machine code, IBM PC DOS, 22 21 20 bytes

Binary:

00000000: d1ee 8a1c 8d38 8a05 fdf3 aef6 d191 052f  .....8........./
00000010: 0ecd 10c3                                ....

Build and test DUBS.COM with xxd -R.

Unassembled listing:

D1 EE       SHR  SI, 1              ; SI to command line tail (80H) 
8A 1C       MOV  BL, BYTE PTR[SI]   ; BX = input string length 
8D 38       LEA  DI, [BX][SI]       ; DI to end of string 
8A 05       MOV  AL, BYTE PTR[DI]   ; AL = last char
FD          STD                     ; set LODS to decrement 
F3 AE       REPZ SCASB              ; scan [DI] backwards until AL doesn't match
F6 D1       NOT  CL                 ; negate CL to get number of matches + 1
91          XCHG AX, CX             ; AL = CL 
05 2F0E     ADD  AX, 0E2FH          ; ASCII convert and adjust, BIOS tty function
CD 10       INT  10H                ; write number to console 
C3          RET                     ; return to DOS

A standalone PC DOS executable program. Input via command line, output number to console.

Notes:

This takes advantage of a few "features" of PC DOS register startup values; specifically BH = 0, CX = 00FFH and SI = 100H. REPZ decrements CX on every compare operation, so starting at 00FFH will yield a negative count of reps. It is off-by-one because REPZ decrements before comparison, so the first non-match still gets counted. That number is ones-complimented and adjusted by 1 to get the count of matched digits.

Also, since CH = 0 it's possible to combine ADD AL, '0'-1 and MOV AH, 0EH into a single instruction ADD AX, 0E2FH to save 1 byte, since AH will be 0. Old-school SIMD for you!

I/O:

enter image description here

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Ruby -nl, 16 bytes

Input is STDIN. Subtracts the position of the start of the dubs sequence from the length of the string (technically, the position of the end of the string).

p~/$/-~/(.)\1*$/

Try it online!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

K (oK), 15 bytes

{+/&\|x=*|x:$x}

Try it online!

  • x:$x convert input to string, e.g. 112311 => "112311"
  • |x=*|x compare each digit to the last digit (1b if same, 0b otherwise), then reverse
  • &\ "turn off" 1bs occurring after first 0b, e.g. &\110011b => 110000b
  • +/ take sum
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Stax, 4 bytes

E:GH

Run and debug it

| improve this answer | |
\$\endgroup\$
1
2

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.