Advertisement
Guest User

Untitled

a guest
Jul 17th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. a b a a c a b
  2. 1 2 3 4 occurrence of a
  3. 1 2 occurrence of b
  4. 1 occurrence of c
  5. 1+1+2+3+1+4+2 = 14
  6.  
  7. input -> output
  8. "abaacab" -> 14
  9. "Programming Puzzles & Code Golf" -> 47
  10. "" -> 0
  11. " " -> 28
  12. "abcdefg" -> 7
  13. "aA" -> 2
  14.  
  15. &=Rz
  16.  
  17. &= % Implicit input. Matrix of all equality comparisons
  18. % STACK: [1 0 0 1;
  19. 0 1 1 0;
  20. 0 1 1 0;
  21. 1 0 0 1]
  22. R % Upper triangular part
  23. % STACK: [1 0 0 1;
  24. 0 1 1 0;
  25. 0 0 1 0;
  26. 0 0 0 1]
  27. z % Number of nonzeros. Implicitly display
  28. % STACK: 6
  29.  
  30. declare @ char(876),@x int,@v int=0Select @=q+CHAR(9)from z X:seleCT @x=len(@),@=REPLACE(@,LEFT(@,1),''),@v+=(@x-LEN(@))*(@x-LEN(@)+1)/2IF LEN(@)>0GOTO X prINT @v-1
  31.  
  32. declare @ char(876), @x int, @v int=0
  33. Select @=q+CHAR(9)from z
  34. X:
  35. seleCT @x=len(@)
  36. ,@=REPLACE(@,LEFT(@,1),'')
  37. ,@v+=(@x-LEN(@))*(@x-LEN(@)+1)/2
  38. IF LEN(@)>0 GOTO X
  39. prINT @v-1
  40.  
  41. s=>s.replace(d=/./g,z=>q+=d[z]=-~d[z],q=0)&&q
  42.  
  43. f=([c,...s],d={})=>c?(d[c]=-~d[c])+f(s,d):0
  44.  
  45. s(O`.
  46. M&!`^|(?<=(.))1*
  47. .
  48.  
  49. s(O`.
  50.  
  51. M&!s`^|(?<=(.))1*
  52.  
  53. .
  54.  
  55. z;g(char*s){int y[128]={z=0};while(*s)z+=--y[*s++];z=-z;}
  56.  
  57. f(a:b)=1+sum[1|c<-b,c==a]+f b;f _=0
  58.  
  59. ;ċ"⁸S
  60.  
  61. +F/V._
  62.  
  63. +F/V._
  64. +F/V._QQ implicit input
  65. /V vectorize count: for each element in the first argument,
  66. count the number of occurrences of the
  67. second argument:
  68. ._Q all prefixes of input
  69. Q input
  70. +F fold (reduce) on +, base case 0.
  71.  
  72. 1#.,@(*+/"1)&=
  73.  
  74. 1#.,@(*+/"1)&=
  75. = Self-classify: bit matrix of equality between input
  76. and its unique elements.
  77. ( )& Apply verb in parentheses to it:
  78. +/ running sums
  79. "1 of each row
  80. * multiplied with original matrix.
  81. This causes the i'th 1 on each row to be replaced by i.
  82. ,@ Flatten the resulting matrix
  83. 1#. and interpret as a base-1 number, computing its sum.
  84.  
  85. ĠJ€ẎS
  86.  
  87. {γ€gLO
  88.  
  89. Tr[#(#+1)/2&/@Last/@Tally@Characters[#]]&
  90.  
  91. s`(.)(?<=((1)|.)+)
  92. $#3$*
  93. 1
  94.  
  95. (loop with w =(fill(make-list 128)0)as z across(read)sum(incf(elt w(char-code z))))
  96.  
  97. (loop with w = (fill (make-list 128) 0) ; create a list to count characters
  98. as z across (read) ; for each character of input
  99. sum (incf (elt w (char-code z)))) ; increase count in list and sum
  100.  
  101. #(apply +(for[[k j](frequencies %)](*(inc j)j 0.5)))
  102.  
  103. ;╗╔⌠╜cRΣ⌡MΣ
  104.  
  105. ;╗╔⌠╜cRΣ⌡MΣ (implicit input: S)
  106. ;╗ save a copy of S in register 0
  107. ╔ uniquify S (call it A)
  108. ⌠╜cRΣ⌡M for each unique character in A:
  109. ╜c count the number of occurrences in S
  110. R range(1, count+1)
  111. Σ sum
  112. Σ sum
  113.  
  114. $-:@+[:+/,/@(=/~)
  115.  
  116. let x(y:bool)=System.Convert.ToInt32(y)
  117. let rec p(k:string)q=
  118. let j=k.Length
  119. if(j=1)then(x(k.[0]=q))else p k.[0..(j-2)] q+x(k.[j-1]=q)
  120. let rec d(a:string)=
  121. let z=a.Length
  122. if(z<2)then z else d a.[0..(z-2)]+p a (a.[z-1])
  123.  
  124. let x (y:bool)=
  125. System.Convert.ToInt32(y)
  126. let rec e (c:string) b=
  127. let j=c.Length
  128. if(j=1)then
  129. (x(c.[0]=b))
  130. else
  131. e c.[0..(j-2)] b+x (c.[j-1]=b)
  132. let rec d (a:string)=
  133. let h=a.Length
  134. if(h<2)then
  135. h
  136. else
  137. d a.[0..(h-2)]+e a (a.[h-1])
  138.  
  139. $x=$_;$b+=++$a{$_}for(split//,$x);$_=$b
  140.  
  141. $z=@{}
  142. $ARGS|% getE*|%{$u+=($Z.$_+=1)};$U
  143.  
  144. PS C:> D:unique-is-cheap.ps1 (gc D:unique-is-cheap.ps1 -raw)
  145. 64
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement