21
\$\begingroup\$

We often see music videos on Youtube. Many Youtube channels that host music videos are "powered by VEVO". Those can be easily identified by both embedding VEVO at their video thumbnails and appending VEVO to their channel name.

Now write some code to test whether a string given by user is a VEVO user account or not.

Requirements for valid VEVO user account strings:

  • Must only contain uppercase, lowercase, and digit characters. (no whitespace or punctuation)

  • Must not exceed 80 characters length.

  • Must have "VEVO" substring at the end of string

Test cases:

Valid inputs:

AdeleVEVO
ConnieTalbotVEVO
SHMVEVO
justimberlakeVEVO
DJMartinJensenVEVO
test123VEVO

Invalid inputs:

syam kapuk
jypentertainment
Noche de Brujas
testVEVO123

Of course, because this is , I'm looking for the shortest code using any programming language.

\$\endgroup\$
11
  • 4
    \$\begingroup\$ Nice first question! +1 \$\endgroup\$
    – LiefdeWen
    Commented Dec 21, 2017 at 10:47
  • 12
    \$\begingroup\$ test-cases: VEVO and ūņīčōdēVEVO \$\endgroup\$
    – dzaima
    Commented Dec 21, 2017 at 11:02
  • 8
    \$\begingroup\$ other suggested test cases: test123VeVo and one with more than 80 characters \$\endgroup\$
    – Arnauld
    Commented Dec 21, 2017 at 11:09
  • 6
    \$\begingroup\$ You should wait a lot longer before accepting a best answer; the question has only been up for an hour and there are plenty more languages that people might answer in! \$\endgroup\$ Commented Dec 21, 2017 at 11:56
  • 5
    \$\begingroup\$ "uppercase, lowercase, and digit characters" requires defining (I would assume (as, I believe, all have done already) that you mean A-Za-z0-9; but it could mean anything that may be uppercased or lowercased, for example and and digits in other alphabets, for example (9). \$\endgroup\$ Commented Dec 21, 2017 at 21:19

27 Answers 27

9
\$\begingroup\$

Python 2, 45 bytes

-3 bytes thanks to Rod. -2 bytes thanks to ovs.

lambda s:len(s)<81*s.isalnum()<'VEVO'==s[-4:]

Try it online!

A regex solution turns out to be longer.

lambda s:re.match('^[^\W_]{0,76}VEVO$',s)
import re
\$\endgroup\$
0
6
\$\begingroup\$

Japt v2.0a0, 20 16 bytes

Returns 1 for valid or 0 for invalid. [\l\d] would also work in place of [^\W_] for the same byte count.

è/^\w{0,76}VEVO$

Try it | Check all test cases

Explanation: è counts the number of matches of the RegEx in the input. In Japt, the \w RegEx class doesn't include underscore.

\$\endgroup\$
3
  • \$\begingroup\$ Nice one. Best I could do without regex is ;¥oB+mc)¯80 ©"VEVO"¥Ut4n \$\endgroup\$ Commented Dec 21, 2017 at 14:13
  • \$\begingroup\$ @ETHproductions, nice trick with B+mc :) By the way, if Japt2 had a character class for [A-Za-z0-9], we could beat Retina here! Might even be worth overriding \w & \W. \$\endgroup\$
    – Shaggy
    Commented Dec 21, 2017 at 15:37
  • \$\begingroup\$ Heh, I think I was actually planning to do that originally... I should get back to work on Japt in general :P \$\endgroup\$ Commented Dec 21, 2017 at 15:48
4
\$\begingroup\$

JavaScript (ES6), 27 36 34 31 bytes

Saved 2 bytes thanks to @Neil and 3 bytes thanks to @Shaggy

s=>/^[^\W_]{0,76}VEVO$/.test(s)

Test cases

let f =

s=>/^[^\W_]{0,76}VEVO$/.test(s)

console.log(f('syam kapuk')) // (INVALID)
console.log(f('jypentertainment')) // (INVALID)
console.log(f('Noche de Brujas')) // (INVALID)
console.log(f('testVEVO123')) // (INVALID)
console.log(f('AdeleVEVO')) // (VALID)
console.log(f('ConnieTalbotVEVO')) // (VALID)
console.log(f('SHMVEVO')) // (VALID)
console.log(f('justimberlakeVEVO')) // (VALID)
console.log(f('DJMartinJensenVEVO')) // (VALID)

\$\endgroup\$
9
  • 2
    \$\begingroup\$ Doesn't \w match _s as well? \$\endgroup\$
    – Neil
    Commented Dec 21, 2017 at 11:19
  • \$\begingroup\$ I think ((?!_)\w) saves 2 bytes. \$\endgroup\$
    – Neil
    Commented Dec 21, 2017 at 11:28
  • 1
    \$\begingroup\$ Would [^\W_] work for a 3 byte saving? \$\endgroup\$
    – Shaggy
    Commented Dec 21, 2017 at 12:14
  • \$\begingroup\$ @Shaggy Heck. For some reason, I was thinking such characters classes were not working within character sets. Thanks! \$\endgroup\$
    – Arnauld
    Commented Dec 21, 2017 at 12:41
  • \$\begingroup\$ Doesn't JS have any way of skipping the lambda? Like /^[^\W_]{0,76}VEVO$/.test or something? \$\endgroup\$ Commented Dec 21, 2017 at 18:02
4
\$\begingroup\$

PHP, 51 bytes

-10 bytes thanks to @Ismael Miguel for using <?= instead of <?php echo! and removing the closing tag

<?=preg_match("/^[^\W_]{0,76}VEVO$/",fgets(STDIN));

Try it online!

Thanks for the other answers so I didn't have to write the regex!

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Instead of <?php echo, you can do <?=preg_match("/^[^\W_]{0,76}VEVO$/",fgets(STDIN));. \$\endgroup\$ Commented Dec 22, 2017 at 1:50
  • \$\begingroup\$ You're welcome. You can leave out the closing PHP tag. Quoting the documentation: "If a file is pure PHP code, it is preferable to omit the PHP closing tag at the end of the file." (php.net/manual/en/language.basic-syntax.phptags.php). This should save you 2 more bytes. Also, instead of [^\W_], just use \w, which is the same as [a-zA-Z_]. \$\endgroup\$ Commented Dec 22, 2017 at 15:47
  • 1
    \$\begingroup\$ Thanks as well, I have a few answers to update now! \$\endgroup\$
    – NK1406
    Commented Dec 22, 2017 at 15:49
  • \$\begingroup\$ You're welcome. One advice: reading the documentation and getting familiar with it will help a lot. \$\endgroup\$ Commented Dec 22, 2017 at 15:52
3
\$\begingroup\$

APL (Dyalog), 25 bytes

≢'^[^_\W]{0,76}VEVO$'⎕S 3

Try it online!

\$\endgroup\$
3
\$\begingroup\$

C (gcc), 83 bytes

c,i;f(char*s){for(c=i=0;s[i];c+=!isalnum(s[i++]));c=i<81*!c*!strcmp("VEVO",s+i-4);}

Try it online!

\$\endgroup\$
4
  • \$\begingroup\$ I don't see any return statement, how does this return c? Undefined behavior? \$\endgroup\$
    – MD XF
    Commented Dec 22, 2017 at 3:33
  • \$\begingroup\$ @MDXF This is indeed undefined behavior and an abuse of how gcc uses the same registers for variable assignment and return values (see tips for golfing in C ). This is not portable and does not necessarily work with other compilers (for instance it doesn't work with clang) \$\endgroup\$
    – scottinet
    Commented Dec 22, 2017 at 9:12
  • \$\begingroup\$ that's very impressive. How did you find that out? \$\endgroup\$
    – MD XF
    Commented Dec 22, 2017 at 22:26
  • \$\begingroup\$ sometimes f("VO" also be true, when before is "VI" stored; or maybe fault \$\endgroup\$
    – l4m2
    Commented Dec 29, 2017 at 6:56
3
\$\begingroup\$

Dyalog APL, 47 bytes

{0::0⋄(∧/(1(819⌶)t)∊⎕A,⎕D)∧77>≢t←,∘'VEVO'⍣¯1⊢⍵}

Try it online!

A pure regex solution is 32 bytes, but also is much more boring than this approach.

{0::0⋄(∧/(1(819⌶)t)∊⎕A,⎕D)∧77>≢t←,∘'VEVO'⍣¯1⊢⍵} a dfn with right arg '⍵'
 0::0⋄                                          on error, return 0
                                 ,∘'VEVO'       a train that appends VEVO
                                         ⍣¯1    apply it -1 times
                                             ⍵  on '⍵'
                                                and error if impossible (which returns 0)
                               t←               save on variable 't'
                              ≢                 get the length of that
                           77>                  if that's smaller than 77
                          ∧                     and
         (1(819I)t)                              [for each of] 't' uppercased
                   ∊⎕A,⎕D                        is it in the uppercase alphabet concatenated with the digits
       ∧/                                        reduced by and
\$\endgroup\$
2
  • \$\begingroup\$ Instead of using ⍣¯1 to check for VEVO and needing the dfn guard, you can do 'VEVO'≡¯4↑⍵. Moving things around a bit gets me {('VEVO'≡¯4↑⍵)∧∧/⍵∊⎕D,⎕A,(819⌶)⎕A} \$\endgroup\$
    – user41805
    Commented Dec 21, 2017 at 18:24
  • \$\begingroup\$ @Cowsquack yeah, I might have forgotten about .. There are other better ways to do this challenge though (i.e. Eriks answer) and I like this idea :p \$\endgroup\$
    – dzaima
    Commented Dec 21, 2017 at 18:34
3
\$\begingroup\$

Grime, 13 bytes

e`n{-76"VEVO"

Try it online!

Nothing fancy here. Match the entire input against the pattern: at most 76 alphanumeric characters, followed by the string VEVO. Prints 1 for match and 0 for no match. I remembered that the last quote could be removed at end of line, but apparently it just causes a parse error.

\$\endgroup\$
3
\$\begingroup\$

C# (.NET Core), 87 + 18 = 105 bytes

Try it online!

a=>a.Where(x=>char.IsLetterOrDigit(x)).Count()==a.Length&a.Length<81&a.EndsWith("VEVO")
\$\endgroup\$
5
  • \$\begingroup\$ Wouldn't this fail if the input contains any non-alphanumeric characters? \$\endgroup\$
    – Shaggy
    Commented Dec 21, 2017 at 12:21
  • \$\begingroup\$ @Shaggy You are right, will fix a bit later. \$\endgroup\$
    – LiefdeWen
    Commented Dec 21, 2017 at 13:05
  • \$\begingroup\$ I have checked with TIO and found that input "ConnieTalbotVEVO" (which should be valid input) was falsely declared as invalid input. \$\endgroup\$ Commented Dec 21, 2017 at 23:32
  • \$\begingroup\$ @BagasSanjaya It is because I put tabs after it in the test case, take the tabs out and it will be the same as the test case above and will work. \$\endgroup\$
    – LiefdeWen
    Commented Dec 22, 2017 at 5:56
  • \$\begingroup\$ 73+18: a=>a.All(x=>char.IsLetterOrDigit(x)&x<123)&a.Length<81&a.EndsWith("VEVO") only ASCII letters or 67+18: a=>a.All(x=>char.IsLetterOrDigit(x))&a.Length<81&a.EndsWith("VEVO") with unicode support \$\endgroup\$
    – dg3
    Commented Dec 22, 2017 at 10:50
2
\$\begingroup\$

><>, 147 125 bytes

!\i:0(?^1[::::::"/9@Z`z"!
;>{(?;{(?v{(?;{(?v{(?;{(?v
 ~l1-?!v >       > !|!: !<
;v    ]/~l99*-0(?!;4["OVEV"{-?;{-?;{-?;{-?;1n

Try it online!

><>, 147 bytes

Try it online!

This prints 1 if the input string is valid and nothing for an invalid input.

Edit 1: Changed the Alphanumeric checks to use ranges rather than comparing against every character. (saving 22 bytes)

\$\endgroup\$
2
  • \$\begingroup\$ I don't have the time right now to golf this, but I'm pretty sure you can lose bytes by doing comparisons to character codes. The gist of it would be to reject things less than '0' and greater than 'z' and then reject things between '9' and 'A' as well as between 'Z' and 'a'. \$\endgroup\$
    – cole
    Commented Dec 21, 2017 at 17:14
  • \$\begingroup\$ @cole :- I wasn't at my PC to change this yesterday after the post but you are right, comparisons do cut a bit off. I'm sure if you scan across the code you may be able to drop some extra bytes off. \$\endgroup\$ Commented Dec 22, 2017 at 10:26
2
\$\begingroup\$

Bash, 53 26 30 bytes

[[ $1 =~ ^[^\W_]{0,76}VEVO$ ]]

Exit code 0 for VALID results and 1 for INVALID results.

Still working on 80 characters or less.

-27 bytes from removing output, thanks to @KeyWeeUsr

+4 bytes, fixed regex (same as everyone else)

Try it online!

\$\endgroup\$
5
  • \$\begingroup\$ You can just echo 1 for true, or just go without any echo. There's no need to echo anything as you still end up with an exit code that you actually check with && and || \$\endgroup\$
    – KeyWeeUsr
    Commented Dec 21, 2017 at 17:41
  • \$\begingroup\$ Okay, wasn't sure what sort of output (if any) was required. The question didn't really say. \$\endgroup\$
    – Probably
    Commented Dec 21, 2017 at 17:47
  • \$\begingroup\$ The idea of 'truthy' is explained here codegolf.meta.stackexchange.com/q/2190/16658 and is generally implied universally unless specified as otherwise \$\endgroup\$
    – Allison
    Commented Dec 22, 2017 at 7:32
  • \$\begingroup\$ This doesn't output anything on TIO. Also, your RegEx allows underscores, which it shouldn't. And you're not checking for length. We require solutions to be fully functional here, works in progress shouldn't be posted. I'd recommend deleting it until you can fix the problems. (On a related note, why do people upvote solutions that clearly don't work?) \$\endgroup\$
    – Shaggy
    Commented Dec 22, 2017 at 10:35
  • \$\begingroup\$ @Shaggy The output is via exit code. \$\endgroup\$ Commented Dec 24, 2017 at 17:38
2
\$\begingroup\$

><>, 101 89 83 81 94 bytes

Edit: Switched to checking for non-alphanumeric characters rather than for alphanumeric. Switched back cause I forget to check between Z and a. Thanks @Emigna. Rip those lost bytes though

Edit 2: Also, I can totally just get rid of those }}}}. Thanks Teal pelican for that and finding the problem with TIO

Edit 3: replaced a ~~~ with a p

!\i::0(?v:::"/")$":"(*$:"`")$"{"(*+$:"@")$"["(*+?
0/?("S"l/
l/"VEVO"[4pn?$0(6
!\{-?vl?
1/;n0/n

I don't know why this won't work on TIO, but it works fine here. The problem was that the {} commands in TIO don't work for an empty list. Try It Here

How It Works

!\i::0(?v:::"/")$":"(*$:"`")$"{"(*+$:"@")$"["(*+?
0/....../
......... Checks each input is alphanumeric
...       If any isn't, print 0 and exit with an error
...

...
0/?("S"l/ Checks if there are more than 80 characters
...       If so, print 0 and exit with an error
...
...

...
...
l/"VEVO"[4pn?$0(6 Check if the input is less than 4 characters
...               If so, print 0 and exit with an error
...

...
...
./"VEVO"[4pn?$0(6 Take the last 4 characters of the input into a new stack (first time I've actually used [)
...               Add "VEVO" to the stack to compare
...

...
0/....../n
........V
!\{-?vl?  Check if the last 4 characters are VEVO
1/;n0/n   Print 1 and exit with an error if so, else print 0 and exit

For consistency, replacing the ; in the last line with an invalid instruction makes every output an error.

\$\endgroup\$
4
  • \$\begingroup\$ I believe your TIO error might be how it works with ordering on the ending stack split. TIO LINK HERE Having a mess around in TIO you can change it to this. \$\endgroup\$ Commented Dec 22, 2017 at 11:52
  • \$\begingroup\$ Thanks @Tealpelican. Fixed that and killed a few more bytes \$\endgroup\$
    – Jo King
    Commented Dec 22, 2017 at 12:40
  • \$\begingroup\$ You are missing the check for characters between "Z" and "a". \$\endgroup\$
    – Emigna
    Commented Dec 22, 2017 at 12:55
  • \$\begingroup\$ @Emigna Oops, thanks! Fixed! \$\endgroup\$
    – Jo King
    Commented Dec 22, 2017 at 13:00
2
\$\begingroup\$

C++, 129 105 102 bytes

Thanks to other answers that showed me that i can count the number of characters
-2 bytes thanks to Zacharý

#include<regex>
int v(std::string s){return std::regex_match(s, std::regex("[a-zA-Z0-9]{0,76}VEVO"));}

TIO LINK

\$\endgroup\$
4
  • \$\begingroup\$ Where's TIO link at your answer? I'd like to test your code... \$\endgroup\$ Commented Dec 23, 2017 at 7:16
  • \$\begingroup\$ Forgive me if this is a lack of knowledge of C++. Can you move remove the variable r, and just have the regex inside the call to std::regex_match? \$\endgroup\$
    – Adalynn
    Commented Dec 23, 2017 at 15:33
  • \$\begingroup\$ There is an unneeded space after the comma \$\endgroup\$
    – Adalynn
    Commented Dec 24, 2017 at 16:12
  • \$\begingroup\$ You can remove the comma the space after the comma. \$\endgroup\$
    – Adalynn
    Commented Dec 4, 2018 at 18:30
2
\$\begingroup\$

Stacked, 21 bytes

['^\i{0,76}VEVO'-''=]

Try it online!

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

05AB1E, 21 bytes

žKÃQsg81‹IR4£"OVEV"QP

Try it online!

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

Java (OpenJDK 8), 37 36 bytes

Pretty simple answer using some lovely regex.
Quite possibly the shortest Java answer I've ever done.
-1 bytes thanks to Neil on the Javascript answer

w->w.matches("((?!_)\\w){0,76}VEVO")

Try it online!

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

Retina, 18 bytes

A`\W|_|.{81}
VEVO$

Try it online!

or

^[^\W_]{0,76}VEVO$

Try it online!

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

Haskell, 75 bytes

-2 bytes thanks to user28667.

import Data.Char
f s|l<-length s=all isAlphaNum s&&l<81&&drop(l-4)s=="VEVO"

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ -2 bytes if you use drop(length s-4) and extract length s \$\endgroup\$
    – user28667
    Commented Dec 21, 2017 at 15:38
1
\$\begingroup\$

Deorst, 22 bytes

'^[^\W_]{0,76}VEVO$'gm

Try it online!

Just uses the regex found by Shaggy

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

V, 17 bytes

ø^¨áüä©û,76}VEVO$

Try it online!

Hexdump:

00000000: f85e a8e1 fce4 a9fb 2c37 367d 5645 564f  .^......,76}VEVO
00000010: 24                                       $

Compressed regexes for the win!

ø                   " Count the number of matches of
 ^                  "   The beginning of the line
  ¨áüä©             "   A letter or number...
       û,76}        "   From 0 to 76 times...
            VEVO    "   Followed by "VEVO"
                $   "   At the end of the line
\$\endgroup\$
0
1
\$\begingroup\$

Ruby -n, 22+1 = 23 bytes

p~/^[^\W_]{0,76}VEVO$/

Output 0 if true, nil if false

Try it online!

Using the same boring regex as everybody else.

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

Swift 4, 113 bytes

import Foundation;var f={(s:String)->Bool in s.range(of:"^[^\\W_]{0,76}VEVO$",options:.regularExpression) != nil}

Try it online!

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

AWK, 23 bytes

$0~/^[^\W_]{0,76}VEVO$/

Try it online!

Outputs the account name if valid, and outputs nothing if it isn't valid

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

Clean, 61 bytes

import StdEnv
?['VEVO']=True
?[_:l]=length l<80&& ?l
?_=False

Try it online!

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

Perl 5, 35 29+1(-a) = 30 bytes

-6 bytes thanks to ETHproductions

Added 4 bytes. Didn't see that underscore wasn't allowed.

This is my first golf, so here's hoping I did it right.

Returns 1 if valid, 0 if not.

print/^[^\W_]{0,76}VEVO$/?1:0

Try it online!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Welcome to PPCG! You can remove extraneous whitespace to get down to 25 (+1) bytes: print/^\w{1,76}VEVO$/?1:0 \$\endgroup\$ Commented Dec 21, 2017 at 22:16
  • \$\begingroup\$ +0 converts match bool into number, rather than ?1:0, saves 2 bytes. Calling with -ple prints $_ for you. So: perl -ple '$_=/^[^\W_]{0,76}VEVO$/+0'. 25 bytes. If you are happy to get blanks on non-matching lines, $_=/^[^\W_]{0,76}VEVO$/ is 23 bytes. \$\endgroup\$
    – Phil H
    Commented Dec 22, 2017 at 11:06
0
\$\begingroup\$

Google Sheets, 33 Bytes

Anonymous worksheet function that takes input from range A1 and outputs to the calling cell

=RegexMatch(A1,"^[^\W_]{0,76}VEVO
\$\endgroup\$
2
  • \$\begingroup\$ Where's the closing bracket? \$\endgroup\$ Commented Jan 2, 2018 at 9:11
  • \$\begingroup\$ @BagasSanjaya that's one of the wonderful things about Google Sheets, it autocompletes unclosed strings and groups. So when this is pasted into a cell and you either click off the cell or press enter GS converts this to =RegexMatch(A1,"^[^\W_]{0,76}VEVO") without any feedback to the user and executes \$\endgroup\$ Commented Jan 2, 2018 at 13:55
-1
\$\begingroup\$

Clojure, 146 bytes

(fn[u](let[c count](and(<(c u)81)(=(c(filter #(let[i(int %)](or(< 47 i 58)(< 64 i 91)(< 96 i 123)))u))(c u))(clojure.string/ends-with? u"VEVO"))))

Try it online!

This would be much shorter using a regex, but I figured doing it manually would be more interesting.

(defn vevo? [username]
  (and (< (count username) 81)

       ; Filter out the illegal characters, then check if the length is the same as the original string
       (= (count (filter #(let [i (int %)]
                            ; Make sure the char code is in the valid ranges
                            (or (< 47 i 58) (< 64 i 91) (< 96 i 123)))
                         username))
          (count username))

       (clojure.string/ends-with? username "VEVO")))
\$\endgroup\$
3
  • \$\begingroup\$ Any TIO link for testing? \$\endgroup\$ Commented Jan 4, 2018 at 12:45
  • \$\begingroup\$ @BagasSanjaya I can add one in a bit. It seems like adding TIO links are standard now. They were optional last time I used the site frequently, but now everyone's adding them. \$\endgroup\$ Commented Jan 4, 2018 at 14:03
  • \$\begingroup\$ @BagasSanjaya I added a link. Unfortunately TIO seems broken. It can't find the ends-with? function, even though that's part of the standard library. \$\endgroup\$ Commented Jan 6, 2018 at 22:34

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.