Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- a b a a c a b
- 1 2 3 4 occurrence of a
- 1 2 occurrence of b
- 1 occurrence of c
- 1+1+2+3+1+4+2 = 14
- input -> output
- "abaacab" -> 14
- "Programming Puzzles & Code Golf" -> 47
- "" -> 0
- " " -> 28
- "abcdefg" -> 7
- "aA" -> 2
- &=Rz
- &= % Implicit input. Matrix of all equality comparisons
- % STACK: [1 0 0 1;
- 0 1 1 0;
- 0 1 1 0;
- 1 0 0 1]
- R % Upper triangular part
- % STACK: [1 0 0 1;
- 0 1 1 0;
- 0 0 1 0;
- 0 0 0 1]
- z % Number of nonzeros. Implicitly display
- % STACK: 6
- 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
- declare @ char(876), @x int, @v int=0
- Select @=q+CHAR(9)from z
- X:
- seleCT @x=len(@)
- ,@=REPLACE(@,LEFT(@,1),'')
- ,@v+=(@x-LEN(@))*(@x-LEN(@)+1)/2
- IF LEN(@)>0 GOTO X
- prINT @v-1
- s=>s.replace(d=/./g,z=>q+=d[z]=-~d[z],q=0)&&q
- f=([c,...s],d={})=>c?(d[c]=-~d[c])+f(s,d):0
- s(O`.
- M&!`^|(?<=(.))1*
- .
- s(O`.
- M&!s`^|(?<=(.))1*
- .
- z;g(char*s){int y[128]={z=0};while(*s)z+=--y[*s++];z=-z;}
- f(a:b)=1+sum[1|c<-b,c==a]+f b;f _=0
- ;ċ"⁸S
- +F/V._
- +F/V._
- +F/V._QQ implicit input
- /V vectorize count: for each element in the first argument,
- count the number of occurrences of the
- second argument:
- ._Q all prefixes of input
- Q input
- +F fold (reduce) on +, base case 0.
- 1#.,@(*+/"1)&=
- 1#.,@(*+/"1)&=
- = Self-classify: bit matrix of equality between input
- and its unique elements.
- ( )& Apply verb in parentheses to it:
- +/ running sums
- "1 of each row
- * multiplied with original matrix.
- This causes the i'th 1 on each row to be replaced by i.
- ,@ Flatten the resulting matrix
- 1#. and interpret as a base-1 number, computing its sum.
- ĠJ€ẎS
- {γ€gLO
- Tr[#(#+1)/2&/@Last/@Tally@Characters[#]]&
- s`(.)(?<=((1)|.)+)
- $#3$*
- 1
- (loop with w =(fill(make-list 128)0)as z across(read)sum(incf(elt w(char-code z))))
- (loop with w = (fill (make-list 128) 0) ; create a list to count characters
- as z across (read) ; for each character of input
- sum (incf (elt w (char-code z)))) ; increase count in list and sum
- #(apply +(for[[k j](frequencies %)](*(inc j)j 0.5)))
- ;╗╔⌠╜cRΣ⌡MΣ
- ;╗╔⌠╜cRΣ⌡MΣ (implicit input: S)
- ;╗ save a copy of S in register 0
- ╔ uniquify S (call it A)
- ⌠╜cRΣ⌡M for each unique character in A:
- ╜c count the number of occurrences in S
- R range(1, count+1)
- Σ sum
- Σ sum
- $-:@+[:+/,/@(=/~)
- let x(y:bool)=System.Convert.ToInt32(y)
- let rec p(k:string)q=
- let j=k.Length
- if(j=1)then(x(k.[0]=q))else p k.[0..(j-2)] q+x(k.[j-1]=q)
- let rec d(a:string)=
- let z=a.Length
- if(z<2)then z else d a.[0..(z-2)]+p a (a.[z-1])
- let x (y:bool)=
- System.Convert.ToInt32(y)
- let rec e (c:string) b=
- let j=c.Length
- if(j=1)then
- (x(c.[0]=b))
- else
- e c.[0..(j-2)] b+x (c.[j-1]=b)
- let rec d (a:string)=
- let h=a.Length
- if(h<2)then
- h
- else
- d a.[0..(h-2)]+e a (a.[h-1])
- $x=$_;$b+=++$a{$_}for(split//,$x);$_=$b
- $z=@{}
- $ARGS|% getE*|%{$u+=($Z.$_+=1)};$U
- PS C:> D:unique-is-cheap.ps1 (gc D:unique-is-cheap.ps1 -raw)
- 64
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement