daily pastebin goal
20%
SHARE
TWEET

Untitled

a guest Dec 17th, 2017 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. s=>!/^d|W|^$/.test(s)/-~(s[0]=='_')
  2.    
  3. ¬D'_Qsa·+¹žj-""Q*2/
  4.    
  5. ¬                     # Push input and also push the first character.
  6.  D                    # Duplicate the first character.
  7.   '_Q                 # Check if it is equal to an underscore character.
  8.      sa               # Swap and check the duplicate if it's an alphabetic character.
  9.        ·              # Double the value.
  10.         +             # Add both values up
  11.          ¹            # Take the first input.
  12.           žj-         # žj is short for [a-zA-Z0-9_]. This will be substracted from the
  13.                         initial string.
  14.              ""Q      # Check if the string is empty.
  15.                 *     # Multiply this with the first value.
  16.                  2/   # Halve it, resulting into 0.0, 0.5, or 1.0.
  17.    
  18. ((s[0] == '_' + s.isalpha() × 2) × (s.remove([a-zA-Z0-9_]) == "")) / 2
  19.    
  20. preg_match('/^[a-z_]w*$/i',$s)?$s[0]=='_'?.5:1:0;
  21.    
  22. preg_match('/^[a-z_]w*$/i', $s) # Matches every a-zA-Z0-9_ string that doesnt start with a number
  23. ?   $s[0] == '_'                   # Then, if it starts with an _
  24.     ?   .5                         # give 0.5 points
  25.     :   1                          # If it doesn't, give 1
  26. :   0;                             # If it didn't match the regex, give 0
  27.    
  28. _test_
  29.    
  30. a-z
  31.    
  32. 1)95=2/8M3Y2m+G7M95h4Y2hmA*
  33.    
  34. 1)      % take input implicitly. Get its first element
  35. 95=     % true if it equals 95 (underscore)
  36. 2/      % divide by 2: gives 0.5 if underscore, 0 if not
  37. 8M      % push first element of input again
  38. 3Y2     % predefined literal: string with all letters
  39. m       % true if it's a letter
  40. +       % add. Gives 1 if letter, 0.5 if underscore
  41. G       % push input again
  42. 7M      % push string with all letters again
  43. 95h     % concatenate underscore
  44. 4Y2h    % predefined literal: string with all digits. Concatenate
  45. mA      % true if all input chars belong to that concatenated string
  46. *       % multiply. Display implicitly
  47.    
  48. Qh~u{Q~J_+-|!Qh_qh/
  49.    
  50. Qh~u{                 - Check first char isn't a digit
  51.      Q~J_+-          - Is the input alphanumeric + "_"
  52.             |!        - Combine
  53.               Qh_q   - Is the first char an "_"
  54.                    h/ - Combine
  55.    
  56. lambda s:s.isidentifier()/-~(s[:1]=='_')
  57.    
  58. c!|-rz0++G_JjkUT}hzJhqhz_
  59.    
  60. ÷"[^Wd]w*"g¦"_.*"g+÷2=0.5¿
  61.    
  62. $ ./gogh no '÷"[^Wd]w*"g¦"_.*"g+÷2=0.5¿' "_test"
  63.    
  64. “ Implicit input                               ”
  65. ÷                  “ Duplicate the TOS                            ”
  66. "[^Wd]w*"g      “ Fully match the STOS against the TOS (regex) ”
  67. ¦                  “ Swap the STOS and TOS                        ”
  68. "_.*"g             “ Fully match the STOS against the TOS (regex) ”
  69. +                  “ Add the TOS to the STOS                      ”
  70. ÷                  “ Duplicate the TOS                            ”
  71. 2=                 “ Determine if the TOS is equal to 2           ”
  72. 0.5¿               “ Leave the correct output on the stack        ”
  73.                    “ Implicit output                              ”
  74.    
  75. $_=!/W|^d//2**/^_/
  76.    
  77. c!:z"W|^d"0h!xz_
  78.    
  79. c!:z"W|^d"0h!xz_  (implicit) Save the input in z.
  80.  
  81.   :z        0        Test if z matches the following regex:
  82.     "W|^d"           A non-word character or a digit at the beginning.
  83.                      This returns True iff z is an invalid name.
  84.  !                   Apply logical NOT to yield True iff z is a valid name.
  85.                xz_  Find the first index of the underscore in z.
  86.                      This yields 0 iff z begins with an underscore.
  87.              h!      Apply logical NOT and increment.
  88.                      This yields 2 if z begins with an underscore, 1 otherwise.
  89. c                    Divide the two results.
  90.    
  91. USE: regexp
  92. [ R/ [_a-zA-Z]w*/ R/ _.*/ [ matches? 1 0 ? ] bi-curry@ bi 0 = 1 2 ? / ]
  93.    
  94. USE: regexp
  95. : v ( s -- n ) R/ [_a-zA-Z]w*/ R/ _.*/ [ matches? 1 0 ? ] bi-curry@ bi 0 = 1 2 ? / ;
  96.    
  97. { 0 } [ "" v ] unit-test
  98. { 0 } [ "" v ] unit-test
  99. { 0 } [ "1" v ] unit-test
  100. { 0 } [ "1var" v ] unit-test
  101. { 0 } [ "var$" v ] unit-test
  102. { 0 } [ "foo var" v ] unit-test
  103. { 1 } [ "v" v ] unit-test
  104. { 1 } [ "var" v ] unit-test
  105. { 1 } [ "var_i_able" v ] unit-test
  106. { 1 } [ "v4r14bl3" v ] unit-test
  107. { 1/2 } [ "_" v ] unit-test
  108. { 1/2 } [ "_v" v ] unit-test
  109. { 1/2 } [ "_var" v ] unit-test
  110. { 1/2 } [ "_var_i_able" v ] unit-test
  111. { 1/2 } [ "_v4r14bl3" v ] unit-test
  112.    
  113. {(0≤⎕NC⍵)÷1+'_'=⊃⍵}
  114.    
  115. If[#~StringMatchQ~RegularExpression@"[A-Za-z_][0-9A-Za-z_]*",If[#~StringTake~1=="_",.5,1],0]&
  116.    
  117. $_=/^([^Wd])w*$//(($1 eq"_")+1)
  118.    
  119. $_=/^([^Wd])w*$//(($1 eq"_")+1)
  120.    /^([^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
  121.                    /                divide result by
  122.                     (($1 eq"_")+1)  (capture == "_") + 1. This is 1 if the first character was not an underscore and 2 if it was.
  123. $_=                                 assign to $_ and implicitly print
  124.    
  125. lambda x:[0,[[.5,1][x[0]>'z'],0][x[0]<'A']][x.replace('_','a').isalnum()]if x else 0
  126.    
  127. x=>!x.match(/W|^d/)/2**/^_/.test(x)
  128.    
  129. x=>                                   // Fat arrow function
  130.    !x.match(/W|^d/)                 // Gives false if contains non word or starting
  131.                                       //   with a digit. Booleans in numeric context will
  132.                                       //   be 0 or 1
  133.                       2**             // 2 to the power of...
  134.                          /^_/.test(x) // gives true if starting with '_'.
  135.                                       //   true -> 1 -> 2**1 -> 2
  136.                                       //   false -> 0 -> 2**0 -> 1
  137.                      /                // Devide the lValue boolean with the numeric rValue:
  138.                                       // lValue = 0 or 1
  139.                                       // rValue = 2 or 1
  140.    
  141. ->(s){s=~/^(_|d)?w*$/?$1?$1==?_?0.5:0:1:0}
  142.    
  143. ->(s){case s
  144. when'',/^d/,/W/
  145. 0
  146. when/^_/
  147. 0.5
  148. else
  149. 1
  150. end}
  151.    
  152. v=function(s)return(s:match("^[_%a]+[_%w]*$")and 1or 0)*(s:match("_")and.5or 1)end
  153.    
  154. print(v("a") == 1) -- true
  155. print(v("1") == 0) -- true
  156. print(v("_") == 0.5) -- true
  157. print(v("") == 0) -- true
  158. print(v("1a") == 0) -- true
  159. print(v("a1") == 1) -- true
  160. print(v("_1") == 0.5) -- true
  161. print(v("_a") == 0.5) -- true
  162. print(v("1_") == 0) -- true
  163. print(v("a_") == 0.5) -- true
  164.    
  165. s=arg[1]print(s:find("^[%a_][%w_]*$")and(s:find("^_")and.5or 1)or 0)
RAW Paste Data
Top