Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Post History

50%
+0 −0
Challenges Cumulative Counts

Ruby, 31 bytes ->a{a.map{$*[_1]=1.+$*[_1]||0}} Try this online! $* is a global variable, so calling this lambda multiple times (in a single process) would give wrong result. A 32 bytes vers...

posted 2y ago by radarek‭  ·  edited 2y ago by radarek‭

Answer
#6: Post edited by user avatar radarek‭ · 2022-07-26T11:14:34Z (almost 2 years ago)
  • # [Ruby](https://www.ruby-lang.org/), 31 bytes
  • ```ruby
  • ->a{a.map{$*[_1]=1.+$*[_1]||0}}
  • ```
  • **[Try this online!](https://ato.pxeger.com/run?1=m72kqDSpcsHiNNuYpaUlaboW-3XtEqsT9XITC6pVtKLjDWNtDfW0IayaGoPaWoiqm6YFCmnR0YY6hjpGYGgIhsY6xrGxCsoKunYK0YZgUSOQmI6JjimIHQvRvGABhAYA)**
  • `$*` is a global variable, so calling this lambda multiple times (in a single process) would give wrong result. A 33 bytes version that does not rely on a global state:
  • ```ruby
  • ->a,*c{a.map{c[_1]=1.+c[_1]||0}}
  • ```
  • If array consists of positive integers from a known range (lets say `0...1e3`, then 30 bytes version is:
  • ```ruby
  • ->a{b=[0]*1e3;a.map{b[_1]+=1}}
  • ```
  • # [Ruby](https://www.ruby-lang.org/), 31 bytes
  • ```ruby
  • ->a{a.map{$*[_1]=1.+$*[_1]||0}}
  • ```
  • **[Try this online!](https://ato.pxeger.com/run?1=m72kqDSpcsHiNNuYpaUlaboW-3XtEqsT9XITC6pVtKLjDWNtDfW0IayaGoPaWoiqm6YFCmnR0YY6hjpGYGgIhsY6xrGxCsoKunYK0YZgUSOQmI6JjimIHQvRvGABhAYA)**
  • `$*` is a global variable, so calling this lambda multiple times (in a single process) would give wrong result. A 32 bytes version that does not rely on a global state:
  • ```ruby
  • ->a,*c{a.map{c[_1]=1.+c[_1]||0}}
  • ```
  • If array consists of positive integers from a known range (lets say `0...1e3`, then 30 bytes version is:
  • ```ruby
  • ->a{b=[0]*1e3;a.map{b[_1]+=1}}
  • ```
#5: Post edited by user avatar radarek‭ · 2022-01-26T15:19:12Z (about 2 years ago)
  • # [Ruby](https://www.ruby-lang.org/), 31 bytes
  • ```ruby
  • ->a{a.map{$*[_1]=1.+$*[_1]||0}}
  • ```
  • **[Try this online!](https://ato.pxeger.com/run?1=m72kqDSpcsHiNNuYpaUlaboW-3XtEqsT9XITC6pVtKLjDWNtDfW0IayaGoPaWoiqm6YFCmnR0YY6hjpGYGgIhsY6xrGxCsoKunYK0YZgUSOQmI6JjimIHQvRvGABhAYA)**
  • `$*` is a global variable, so calling this lambda multiple times (in a single process) would give wrong result. A 33 bytes version that does not rely on a global state:
  • ```ruby
  • ->a{c=Hash.new 0;a.map{c[_1]+=1}}
  • ```
  • If array consists of positive integers from a known range (lets say `0...1e3`, then 30 bytes version is:
  • ```ruby
  • ->a{b=[0]*1e3;a.map{b[_1]+=1}}
  • ```
  • # [Ruby](https://www.ruby-lang.org/), 31 bytes
  • ```ruby
  • ->a{a.map{$*[_1]=1.+$*[_1]||0}}
  • ```
  • **[Try this online!](https://ato.pxeger.com/run?1=m72kqDSpcsHiNNuYpaUlaboW-3XtEqsT9XITC6pVtKLjDWNtDfW0IayaGoPaWoiqm6YFCmnR0YY6hjpGYGgIhsY6xrGxCsoKunYK0YZgUSOQmI6JjimIHQvRvGABhAYA)**
  • `$*` is a global variable, so calling this lambda multiple times (in a single process) would give wrong result. A 33 bytes version that does not rely on a global state:
  • ```ruby
  • ->a,*c{a.map{c[_1]=1.+c[_1]||0}}
  • ```
  • If array consists of positive integers from a known range (lets say `0...1e3`, then 30 bytes version is:
  • ```ruby
  • ->a{b=[0]*1e3;a.map{b[_1]+=1}}
  • ```
#4: Post edited by user avatar radarek‭ · 2022-01-15T10:13:14Z (over 2 years ago)
  • # [Ruby](https://www.ruby-lang.org/), 31 bytes
  • ```ruby
  • ->a{a.map{$*[_1]=1.+$*[_1]||0}}
  • ```
  • **[Try this online!](https://ato.pxeger.com/run?1=m72kqDSpcsHiNNuYpaUlaboW-3XtEqsT9XITC6pVtKLjDWNtDfW0IayaGoPaWoiqm6YFCmnR0YY6hjpGYGgIhsY6xrGxCsoKunYK0YZgUSOQmI6JjimIHQvRvGABhAYA)**
  • `$*` is a global variable, so you can calling this lambda multiple times (in a single process) would give wrong result. A 33 bytes version that does not rely on a global state:
  • ```ruby
  • ->a{c=Hash.new 0;a.map{c[_1]+=1}}
  • ```
  • If array consists of positive integers from a known range (lets say `0...1e3`, then 30 bytes version is:
  • ```ruby
  • ->a{b=[0]*1e3;a.map{b[_1]+=1}}
  • ```
  • # [Ruby](https://www.ruby-lang.org/), 31 bytes
  • ```ruby
  • ->a{a.map{$*[_1]=1.+$*[_1]||0}}
  • ```
  • **[Try this online!](https://ato.pxeger.com/run?1=m72kqDSpcsHiNNuYpaUlaboW-3XtEqsT9XITC6pVtKLjDWNtDfW0IayaGoPaWoiqm6YFCmnR0YY6hjpGYGgIhsY6xrGxCsoKunYK0YZgUSOQmI6JjimIHQvRvGABhAYA)**
  • `$*` is a global variable, so calling this lambda multiple times (in a single process) would give wrong result. A 33 bytes version that does not rely on a global state:
  • ```ruby
  • ->a{c=Hash.new 0;a.map{c[_1]+=1}}
  • ```
  • If array consists of positive integers from a known range (lets say `0...1e3`, then 30 bytes version is:
  • ```ruby
  • ->a{b=[0]*1e3;a.map{b[_1]+=1}}
  • ```
#3: Post edited by user avatar radarek‭ · 2022-01-15T00:27:31Z (over 2 years ago)
  • # [Ruby](https://www.ruby-lang.org/), 31 bytes
  • ```ruby
  • ->a{a.map{$*[_1]=1.+$*[_1]||0}}
  • ```
  • **[Try this online!](https://ato.pxeger.com/run?1=m72kqDSpcsHiNNuYpaUlaboW-3XtEqsT9XITC6pVtKLjDWNtDfW0IayaGoPaWoiqm6YFCmnR0YY6hjpGYGgIhsY6xrGxCsoKunYK0YZgUSOQmI6JjimIHQvRvGABhAYA)**
  • `$*` is a global variable, so you can calling this lambda multiple times (in a single process) would give wrong result. A 33 bytes version that does not rely on a global state:
  • ```ruby
  • ->a{c=Hash.new 0;a.map{c[_1]+=1}}
  • ```
  • If array consists of positive integers from a known range (lets say `0..1e3`, then 30 bytes version is:
  • ```ruby
  • ->a{b=[0]*1e3;a.map{b[_1]+=1}}
  • ```
  • # [Ruby](https://www.ruby-lang.org/), 31 bytes
  • ```ruby
  • ->a{a.map{$*[_1]=1.+$*[_1]||0}}
  • ```
  • **[Try this online!](https://ato.pxeger.com/run?1=m72kqDSpcsHiNNuYpaUlaboW-3XtEqsT9XITC6pVtKLjDWNtDfW0IayaGoPaWoiqm6YFCmnR0YY6hjpGYGgIhsY6xrGxCsoKunYK0YZgUSOQmI6JjimIHQvRvGABhAYA)**
  • `$*` is a global variable, so you can calling this lambda multiple times (in a single process) would give wrong result. A 33 bytes version that does not rely on a global state:
  • ```ruby
  • ->a{c=Hash.new 0;a.map{c[_1]+=1}}
  • ```
  • If array consists of positive integers from a known range (lets say `0...1e3`, then 30 bytes version is:
  • ```ruby
  • ->a{b=[0]*1e3;a.map{b[_1]+=1}}
  • ```
#2: Post edited by user avatar radarek‭ · 2022-01-14T23:56:12Z (over 2 years ago)
  • # [Ruby](https://www.ruby-lang.org/), 31 bytes
  • ```ruby
  • ->a{a.map{$*[_1]=1.+$*[_1]||0}}
  • ```
  • **[Try this online!](https://ato.pxeger.com/run?1=m72kqDSpcsHiNNuYpaUlaboW-3XtEqsT9XITC6pVtKLjDWNtDfW0IayaGoPaWoiqm6YFCmnR0YY6hjpGYGgIhsY6xrGxCsoKunYK0YZgUSOQmI6JjimIHQvRvGABhAYA)**
  • `$*` is a global variable, so you can call function only once. A 33 bytes version that does not rely on a global state:
  • ```ruby
  • ->a{c=Hash.new 0;a.map{c[_1]+=1}}
  • ```
  • If array consists of positive integers from a known range (lets say `0..1e3`, then 30 bytes version is:
  • ```ruby
  • ->a{b=[0]*1e3;a.map{b[_1]+=1}}
  • ```
  • # [Ruby](https://www.ruby-lang.org/), 31 bytes
  • ```ruby
  • ->a{a.map{$*[_1]=1.+$*[_1]||0}}
  • ```
  • **[Try this online!](https://ato.pxeger.com/run?1=m72kqDSpcsHiNNuYpaUlaboW-3XtEqsT9XITC6pVtKLjDWNtDfW0IayaGoPaWoiqm6YFCmnR0YY6hjpGYGgIhsY6xrGxCsoKunYK0YZgUSOQmI6JjimIHQvRvGABhAYA)**
  • `$*` is a global variable, so you can calling this lambda multiple times (in a single process) would give wrong result. A 33 bytes version that does not rely on a global state:
  • ```ruby
  • ->a{c=Hash.new 0;a.map{c[_1]+=1}}
  • ```
  • If array consists of positive integers from a known range (lets say `0..1e3`, then 30 bytes version is:
  • ```ruby
  • ->a{b=[0]*1e3;a.map{b[_1]+=1}}
  • ```
#1: Initial revision by user avatar radarek‭ · 2022-01-14T23:55:12Z (over 2 years ago)
# [Ruby](https://www.ruby-lang.org/), 31 bytes

```ruby
->a{a.map{$*[_1]=1.+$*[_1]||0}}
```

**[Try this online!](https://ato.pxeger.com/run?1=m72kqDSpcsHiNNuYpaUlaboW-3XtEqsT9XITC6pVtKLjDWNtDfW0IayaGoPaWoiqm6YFCmnR0YY6hjpGYGgIhsY6xrGxCsoKunYK0YZgUSOQmI6JjimIHQvRvGABhAYA)**

`$*` is a global variable, so you can call function only once. A 33 bytes version that does not rely on a global state:

```ruby
->a{c=Hash.new 0;a.map{c[_1]+=1}}
```

If array consists of positive integers from a known range (lets say `0..1e3`, then 30 bytes version is:

```ruby
->a{b=[0]*1e3;a.map{b[_1]+=1}}
```