Advertisement
Guest User

Untitled

a guest
Oct 21st, 2014
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.96 KB | None | 0 0
  1. String[]
  2.  
  3. {⍵⊂⍨⍵∊∊⍕¨⍳10}
  4.  
  5. {⍵⊂⍨⍵∊∊⍕¨⍳10}
  6.  
  7. {⍵⊂⍨⍵∊∊⍕¨⍳10}
  8.  
  9. { ⍳10} make an array of naturals from 1 to 10
  10. ⍕¨ convert each number into a string
  11. ∊ concatenate the strings into one (it doesn't matter that there are two 1s)
  12. ⍵∊ test which chars from the argument are contained in the digit string
  13. ⍵⊂⍨ use it to perform a partitioned enclose, which splits the string as needed
  14.  
  15. {⍵⊂⍨⍵∊∊⍕¨⍳10} 'ab5c0x'
  16. 5 0
  17. {⍵⊂⍨⍵∊∊⍕¨⍳10} 'z526ks4f.;8]p'
  18. 526 4 8
  19.  
  20. {q,⍵,q←'"'}¨ {⍵⊂⍨⍵∊∊⍕¨⍳10} 'ab5c0x'
  21. "5" "0"
  22. {q,⍵,q←'"'}¨ {⍵⊂⍨⍵∊∊⍕¨⍳10} 'z526ks4f.;8]p'
  23. "526" "4" "8"
  24.  
  25. f()(echo ${1//+([!0-9])/ })
  26.  
  27. $ a=($(f "ab5c0x")); echo ${a[@]}
  28. 5 0
  29. $ a=($(f "z526ks4f.;8]p")); echo ${a[@]}
  30. 526 4 8
  31. $
  32.  
  33. >>> sample=["abc123def456","aitew034snk582:3c","as5493tax54\430-52@g9.fc","sasprs]tore"re\forz"]
  34. >>> [f(data) for data in sample]
  35. [['123', '456'], ['034', '582', '3'], ['5493', '54', '430', '52', '9'], []]
  36.  
  37. function split() {
  38. tr -c 0-9 <<E
  39. $1
  40. E
  41. }
  42.  
  43. $ for N in $(split 'abc123def456'); do echo $N; done
  44. 123
  45. 456
  46.  
  47. function split() (tr -c 0-9 <<<$1)
  48.  
  49. a=(`split "abc123def456"`); echo ${a[@]}
  50.  
  51. StringCases[#,DigitCharacter..]&
  52.  
  53. inps ={"abc123def456", "aitew034snk582:3c", "as5493tax54\430-52@g9.fc",
  54. "sasprs]tore"re\forz"}
  55. StringCases[#,DigitCharacter..]&/@inps
  56.  
  57. {{"123", "456"},
  58. {"034", "582", "3"},
  59. {"5493", "54", "430", "52", "9"},
  60. {}
  61. }
  62.  
  63. StringCases[#, RegularExpression["[0-9]+"]] &
  64.  
  65. f := [:s|s asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:ch|ch isDigit not]]
  66.  
  67. [ 123 456 ]
  68. [ 034 582 3 ]
  69. [ 5493 54 430 52 9 ]
  70. [ ]
  71.  
  72. Function f(i)
  73. For x=1 To Len(i)
  74. c=Mid(i,x,1)
  75. If Not IsNumeric(c) Then
  76. Mid(i,x,1)=" "
  77. End If
  78. Next
  79. Do
  80. l=Len(i)
  81. i=Replace(i," "," ")
  82. l=l-Len(i)
  83. Loop Until l=0
  84. f=Split(Trim(i)," ")
  85. End Function
  86.  
  87. Input: "ab5c0x"
  88. Output: 5,0
  89.  
  90. Input: "z526ks4f.;8]p"
  91. Output: 526,4,8
  92.  
  93. function f(a){
  94. a+=".",b="",c=[];for(i in a)b=+a[i]+1?b+a[i]:b?(c.push(b),""):b;return c
  95. }
  96.  
  97. a+=".",b="",c=[]; //add '.' to input so we dont have to check if it ends in a digit
  98. for(i in a)
  99. b=+a[i]+1? //check if digit, add to string if it is
  100. b+a[i]:
  101. b? //if it wasnt a digit and b contains digits push it
  102. (c.push(b),""): //into the array c and clear b
  103. b; //else give me b back
  104. return c
  105.  
  106. console.log(f("abc123def456"));
  107. console.log(f("aitew034snk582:3c"));
  108. console.log(f("as5493tax54\430-52@g9.fc"));
  109. console.log(f("sasprs]tore"re\forz"));
  110.  
  111. ["123", "456"]
  112. ["034", "582", "3"]
  113. ["5493", "54", "430", "52", "9"]
  114. []
  115.  
  116. f=function(x){
  117. s=strsplit(x,"",T)[[1]]
  118. i=s%in%0:9
  119. split(s,c(0,cumsum(!!diff(i))))[c(i[1],!i[1])]
  120. }
  121.  
  122. > f("abc123def456")
  123. $`1`
  124. [1] "1" "2" "3"
  125.  
  126. $`3`
  127. [1] "4" "5" "6"
  128.  
  129. > f("aitew034snk582:3c")
  130. $`1`
  131. [1] "0" "3" "4"
  132.  
  133. $`3`
  134. [1] "5" "8" "2"
  135.  
  136. $`5`
  137. [1] "3"
  138.  
  139. > f("as5493tax54\430-52@g9.fc")
  140. $`1`
  141. [1] "5" "4" "9" "3"
  142.  
  143. $`3`
  144. [1] "5" "4"
  145.  
  146. $`5`
  147. [1] "4" "3" "0"
  148.  
  149. $`7`
  150. [1] "5" "2"
  151.  
  152. $`9`
  153. [1] "9"
  154.  
  155. > f("sasprs]tore"re\forz")
  156. $<NA>
  157. NULL
  158.  
  159. function f($a){
  160. $i=0;while($i<strlen($a)){!is_numeric($a[$i])&&$a[$i]='-';$i++;}return array_filter(explode('-',$a),function($v){return!empty($v);});
  161. }
  162.  
  163. static string[] SplitAtNonDigits(string s)
  164. {
  165. return new string(s.Select(c=>47<c&c<58?c:',').ToArray()).Split(new[]{','},(StringSplitOptions)1);
  166. }
  167.  
  168. function n(s){
  169. var r=[];s.split('').reduce(function(p,c){if(!isNaN(parseInt(c))){if(p)r.push([]);r[r.length-1].push(c);return 0;}return 1;},1);return r;
  170. }
  171.  
  172. function n(s) {
  173. var r = [];
  174. s.split('').reduce(function (p, c) {
  175. if (!isNaN(parseInt(c))) {
  176. if (p) {
  177. r.push([]);
  178. }
  179. r[r.length - 1].push(c);
  180. return 0;
  181. }
  182. return 1;
  183. }, 1);
  184. return r;
  185. }
  186.  
  187. f=->s{s.tr("
  188. -/:-~",' ').split}
  189.  
  190. def find_digits(_input_):
  191. a,b = [], ""
  192. for i in list(_input_):
  193. if i.isdigit(): b += i
  194. else:
  195. if b != "": a.append(b)
  196. b = ""
  197. if b != "": a.append(b)
  198. return a
  199.  
  200. def f(s, o=[], c=""):
  201. for i in s:
  202. try:int(i);c+=i
  203. except:o+=[c];c=""
  204. return [i for i in o+[c] if i]
  205.  
  206. assert f("abc123def456") == ["123", "456"]
  207. assert f("aitew034snk582:3c") == ["034", "582", "3"]
  208. assert f("as5493tax54\430-52@g9.fc") == ["5493", "54", "430", "52", "9"]
  209. assert f("sasprs]tore"re\forz") == []
  210.  
  211. function f($x) {
  212. // Only the following line counts:
  213. for($h=$i=0;sscanf(substr("a$x",$h+=$i),"%[^0-9]%[0-9]%n",$j,$s,$i)>1;)$a[]=$s;return@$a;
  214. }
  215.  
  216. php > echo json_encode(f("abc123def456")), "n";
  217. ["123","456"]
  218. php > echo json_encode(f("aitew034snk582:3c")), "n";
  219. ["034","582","3"]
  220. php > echo json_encode(f("as5493tax54\430-52@g9.fc")), "n";
  221. ["5493","54","430","52","9"]
  222. php > echo json_encode(f("sasprs]tore"re\forz")), "n";
  223. null
  224.  
  225. {-# LANGUAGE OverloadedStrings #-}
  226. import Data.Char (isDigit)
  227. import Data.Text (split)
  228.  
  229. f=filter(/="").split(not.isDigit)
  230.  
  231. (defun extract-numeric-substrings (string &aux (start 0) (end 0) (result (make-array 0 :adjustable t :fill-pointer 0)))
  232. (loop
  233. (unless (and end (setq start (position-if #'digit-char-p string :start end)))
  234. (return result))
  235. (setq end (position-if (complement #'digit-char-p) string :start (1+ start)))
  236. (vector-push-extend (subseq string start end) result)))
  237.  
  238. (extract-numeric-substrings "abc123def456")
  239. #("123" "456")
  240.  
  241. (extract-numeric-substrings "aitew034snk582:3c")
  242. #("034" "582" "3")
  243.  
  244. (extract-numeric-substrings "as5493tax54\430-52@g9.fc")
  245. #("5493" "54" "430" "52" "9")
  246.  
  247. (extract-numeric-substrings "sasprs]tore"re\forz")
  248. #()
  249.  
  250. (defun extract-numeric-substrings (string
  251. &aux (start 0) (end 0)
  252. (result (make-array 0 :adjustable t :fill-pointer 0))
  253. (x (loop
  254. (unless (and end (setq start (position-if #'digit-char-p string :start end)))
  255. (return result))
  256. (setq end (position-if (complement #'digit-char-p) string :start (1+ start)))
  257. (vector-push-extend (subseq string start end) result))))
  258. x)
  259.  
  260. (LOOP(UNLESS(AND END(SETQ START(POSITION-IF #'DIGIT-CHAR-P STRING :START END)))(RETURN RESULT))(SETQ END(POSITION-IF(COMPLEMENT #'DIGIT-CHAR-P)STRING :START(1+ START)))(VECTOR-PUSH-EXTEND(SUBSEQ STRING START END)RESULT))
  261.  
  262. (defun extract-numeric-substrings (s &aux (b 0) (e 0) (r (make-array 0 :fill-pointer 0)))
  263. (loop
  264. with d = #'digit-char-p
  265. while (and e (setq b (position-if d s :start e)))
  266. finally (return r)
  267. do
  268. (setq e (position-if-not d s :start (1+ b)))
  269. (vector-push-extend (subseq s b e) r)))
  270.  
  271. (LOOP WITH D = #'DIGIT-CHAR-P WHILE(AND E(SETQ B(POSITION-IF D S :START E)))FINALLY(RETURN R)DO(SETQ E(POSITION-IF-NOT D S :START(1+ B)))(VECTOR-PUSH-EXTEND(SUBSEQ S B E)R))
  272.  
  273. Function t(s)
  274. Dim o()
  275. For Each c In Split(StrConv(s,64),Chr(0))
  276. d=IsNumeric(c)
  277. If b And d Then
  278. n=n&c
  279. ElseIf d Then:ReDim Preserve o(l):b=1:n=c
  280. ElseIf b Then:b=0:o(l)=n:l=l+1:End If:Next:t=o
  281. End Function
  282.  
  283. remove-each n s: split s complement charset"0123456789"[empty? n]s
  284.  
  285. f: func [s] [
  286. remove-each n s: split s complement charset "0123456789" [empty? n]
  287. s
  288. ]
  289.  
  290. >> f "abc123def456"
  291. == ["123" "456"]
  292.  
  293. >> f "aitew035snk582:3c"
  294. == ["035" "582" "3"]
  295.  
  296. >> f "as5493tax54\430-52@g9.fc"
  297. == ["5493" "54" "430" "52" "9"]
  298.  
  299. >> f {sasprs]torer"re\forz}
  300. == []
  301.  
  302. function nums(s) {
  303. s+=l='length';r=[''];for(k=i=0;c=s[i];i++)r[k]+=+c+1?c:r[k+=!!r[k][l]]='';
  304. r[l]--;return r
  305. }
  306.  
  307. function nums(s) {
  308. var i, e, r, c, k;
  309. k = 0;
  310. s+='x'; // ensure the input does not end with a digit
  311. r=[''];
  312. for (i=0;i<s.length;i++) {
  313. c=s[i];
  314. if (+c+1) { // if the current character is a digit, append it to the last entry
  315. r[k] += c;
  316. }
  317. else { // otherwise, add a new entry if the last entry is not blank
  318. k+=!!r[k].length;
  319. r[k] = '';
  320. }
  321. }
  322. r.length--; // strip the last entry, known to be blank
  323. return r;
  324. }
  325.  
  326. x
  327. ## [1] "wNEKbS0q7hAXRVCF6I4S" "DpqW50YfaDMURB8micYd" "gwSuYstMGi8H7gDAoHJu"
  328. require(stringi)
  329. stri_split_charclass(x,"\P{N}",o=T)
  330. ## [[1]]
  331. ## [1] "0" "7" "6" "4"
  332.  
  333. ## [[2]]
  334. ## [1] "50" "8"
  335.  
  336. ## [[3]]
  337. ## [1] "8" "7"
  338.  
  339. <?php
  340.  
  341. $a = function($s) {
  342. foreach(str_split($s)as$c)$b[]=is_numeric($c)?$c:".";return array_filter(explode('.',implode($b)));
  343. };
  344.  
  345. var_dump($a("abc123def456"));
  346. var_dump($a("aitew034snk582:3c"));
  347. var_dump($a("as5493tax54\430-52@g9.fc"));
  348. var_dump($a("sasprs]tore"re\forz"));
  349.  
  350. array(2) {
  351. [3]=>
  352. string(3) "123"
  353. [6]=>
  354. string(3) "456"
  355. }
  356. array(3) {
  357. [5]=>
  358. string(3) "034"
  359. [8]=>
  360. string(3) "582"
  361. [9]=>
  362. string(1) "3"
  363. }
  364. array(5) {
  365. [2]=>
  366. string(4) "5493"
  367. [5]=>
  368. string(2) "54"
  369. [6]=>
  370. string(3) "430"
  371. [7]=>
  372. string(2) "52"
  373. [9]=>
  374. string(1) "9"
  375. }
  376. array(0) {
  377. }
  378.  
  379. function n(x){
  380. y=[],i=0,z=t=''
  381. while(z=x[i++])t=!isNaN(z)?t+z:t&&y.push(t)?'':t
  382. if(t)y.push(t)
  383. return y
  384. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement