Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- s=>!/^d|W|^$/.test(s)/-~(s[0]=='_')
- ¬D'_Qsa·+¹žj-""Q*2/
- ¬ # Push input and also push the first character.
- D # Duplicate the first character.
- '_Q # Check if it is equal to an underscore character.
- sa # Swap and check the duplicate if it's an alphabetic character.
- · # Double the value.
- + # Add both values up
- ¹ # Take the first input.
- žj- # žj is short for [a-zA-Z0-9_]. This will be substracted from the
- initial string.
- ""Q # Check if the string is empty.
- * # Multiply this with the first value.
- 2/ # Halve it, resulting into 0.0, 0.5, or 1.0.
- ((s[0] == '_' + s.isalpha() × 2) × (s.remove([a-zA-Z0-9_]) == "")) / 2
- preg_match('/^[a-z_]w*$/i',$s)?$s[0]=='_'?.5:1:0;
- preg_match('/^[a-z_]w*$/i', $s) # Matches every a-zA-Z0-9_ string that doesnt start with a number
- ? $s[0] == '_' # Then, if it starts with an _
- ? .5 # give 0.5 points
- : 1 # If it doesn't, give 1
- : 0; # If it didn't match the regex, give 0
- _test_
- a-z
- 1)95=2/8M3Y2m+G7M95h4Y2hmA*
- 1) % take input implicitly. Get its first element
- 95= % true if it equals 95 (underscore)
- 2/ % divide by 2: gives 0.5 if underscore, 0 if not
- 8M % push first element of input again
- 3Y2 % predefined literal: string with all letters
- m % true if it's a letter
- + % add. Gives 1 if letter, 0.5 if underscore
- G % push input again
- 7M % push string with all letters again
- 95h % concatenate underscore
- 4Y2h % predefined literal: string with all digits. Concatenate
- mA % true if all input chars belong to that concatenated string
- * % multiply. Display implicitly
- Qh~u{Q~J_+-|!Qh_qh/
- Qh~u{ - Check first char isn't a digit
- Q~J_+- - Is the input alphanumeric + "_"
- |! - Combine
- Qh_q - Is the first char an "_"
- h/ - Combine
- lambda s:s.isidentifier()/-~(s[:1]=='_')
- c!|-rz0++G_JjkUT}hzJhqhz_
- ÷"[^Wd]w*"g¦"_.*"g+÷2=0.5¿
- $ ./gogh no '÷"[^Wd]w*"g¦"_.*"g+÷2=0.5¿' "_test"
- “ Implicit input ”
- ÷ “ Duplicate the TOS ”
- "[^Wd]w*"g “ Fully match the STOS against the TOS (regex) ”
- ¦ “ Swap the STOS and TOS ”
- "_.*"g “ Fully match the STOS against the TOS (regex) ”
- + “ Add the TOS to the STOS ”
- ÷ “ Duplicate the TOS ”
- 2= “ Determine if the TOS is equal to 2 ”
- 0.5¿ “ Leave the correct output on the stack ”
- “ Implicit output ”
- $_=!/W|^d//2**/^_/
- c!:z"W|^d"0h!xz_
- c!:z"W|^d"0h!xz_ (implicit) Save the input in z.
- :z 0 Test if z matches the following regex:
- "W|^d" A non-word character or a digit at the beginning.
- This returns True iff z is an invalid name.
- ! Apply logical NOT to yield True iff z is a valid name.
- xz_ Find the first index of the underscore in z.
- This yields 0 iff z begins with an underscore.
- h! Apply logical NOT and increment.
- This yields 2 if z begins with an underscore, 1 otherwise.
- c Divide the two results.
- USE: regexp
- [ R/ [_a-zA-Z]w*/ R/ _.*/ [ matches? 1 0 ? ] bi-curry@ bi 0 = 1 2 ? / ]
- USE: regexp
- : v ( s -- n ) R/ [_a-zA-Z]w*/ R/ _.*/ [ matches? 1 0 ? ] bi-curry@ bi 0 = 1 2 ? / ;
- { 0 } [ "" v ] unit-test
- { 0 } [ "" v ] unit-test
- { 0 } [ "1" v ] unit-test
- { 0 } [ "1var" v ] unit-test
- { 0 } [ "var$" v ] unit-test
- { 0 } [ "foo var" v ] unit-test
- { 1 } [ "v" v ] unit-test
- { 1 } [ "var" v ] unit-test
- { 1 } [ "var_i_able" v ] unit-test
- { 1 } [ "v4r14bl3" v ] unit-test
- { 1/2 } [ "_" v ] unit-test
- { 1/2 } [ "_v" v ] unit-test
- { 1/2 } [ "_var" v ] unit-test
- { 1/2 } [ "_var_i_able" v ] unit-test
- { 1/2 } [ "_v4r14bl3" v ] unit-test
- {(0≤⎕NC⍵)÷1+'_'=⊃⍵}
- If[#~StringMatchQ~RegularExpression@"[A-Za-z_][0-9A-Za-z_]*",If[#~StringTake~1=="_",.5,1],0]&
- $_=/^([^Wd])w*$//(($1 eq"_")+1)
- $_=/^([^Wd])w*$//(($1 eq"_")+1)
- /^([^Wd])w*$/ matches any string that starts with an underscore or a letter of the alphabet followed by 0 or more alphanumeric + underscore characters. The first character is stored in a capture group
- / divide result by
- (($1 eq"_")+1) (capture == "_") + 1. This is 1 if the first character was not an underscore and 2 if it was.
- $_= assign to $_ and implicitly print
- lambda x:[0,[[.5,1][x[0]>'z'],0][x[0]<'A']][x.replace('_','a').isalnum()]if x else 0
- x=>!x.match(/W|^d/)/2**/^_/.test(x)
- x=> // Fat arrow function
- !x.match(/W|^d/) // Gives false if contains non word or starting
- // with a digit. Booleans in numeric context will
- // be 0 or 1
- 2** // 2 to the power of...
- /^_/.test(x) // gives true if starting with '_'.
- // true -> 1 -> 2**1 -> 2
- // false -> 0 -> 2**0 -> 1
- / // Devide the lValue boolean with the numeric rValue:
- // lValue = 0 or 1
- // rValue = 2 or 1
- ->(s){s=~/^(_|d)?w*$/?$1?$1==?_?0.5:0:1:0}
- ->(s){case s
- when'',/^d/,/W/
- 0
- when/^_/
- 0.5
- else
- 1
- end}
- v=function(s)return(s:match("^[_%a]+[_%w]*$")and 1or 0)*(s:match("_")and.5or 1)end
- print(v("a") == 1) -- true
- print(v("1") == 0) -- true
- print(v("_") == 0.5) -- true
- print(v("") == 0) -- true
- print(v("1a") == 0) -- true
- print(v("a1") == 1) -- true
- print(v("_1") == 0.5) -- true
- print(v("_a") == 0.5) -- true
- print(v("1_") == 0) -- true
- print(v("a_") == 0.5) -- true
- s=arg[1]print(s:find("^[%a_][%w_]*$")and(s:find("^_")and.5or 1)or 0)
Add Comment
Please, Sign In to add comment