Post History
Scala 3, 50 44 bytes ? => ?.indices zip?map(?take _+1 count _.==) Try it in Scastie! This is an annoying, stupid approach. The more elegant one using inits was much longer (x=>x.inits.to...
Answer
#4: Post edited
- # Scala 3, <s>50</s> 44 bytes
- ```scala
- ? => ?.indices zip?map(?take _+1 count _.==)
- ```
- [Try it in Scastie!](https://scastie.scala-lang.org/ewPr1E4GRlKO7nkufl0qzA)
This is an annoying, stupid approach. The more elegant one using `inits` didn't work out.- ```scala
- ? => //The input
- ?.indices //The indices
- zip ? //Zip them with the elements of the input
- .map( //For every index i and the corresponding element x,
- ? take _+1 //Take the first i+1 elements of the input
- count //Count how many of them
- _.==) //Are equal to the element x
- ```
- # Scala 3, <s>50</s> 44 bytes
- ```scala
- ? => ?.indices zip?map(?take _+1 count _.==)
- ```
- [Try it in Scastie!](https://scastie.scala-lang.org/ewPr1E4GRlKO7nkufl0qzA)
- This is an annoying, stupid approach. The more elegant one using `inits` was much longer (`x=>x.inits.toSeq.reverse.tail zip x map(_ count _.==)`).
- ```scala
- ? => //The input
- ?.indices //The indices
- zip ? //Zip them with the elements of the input
- .map( //For every index i and the corresponding element x,
- ? take _+1 //Take the first i+1 elements of the input
- count //Count how many of them
- _.==) //Are equal to the element x
- ```
#3: Post edited
# Scala 3, 50 bytes- ```scala
x=>x.zipWithIndex.map((?,i)=>x take i+1 count?.==)- ```
[Try it in Scastie!](https://scastie.scala-lang.org/y4DHryrDRhGY3km2q9Kx6w)- This is an annoying, stupid approach. The more elegant one using `inits` didn't work out.
- ```scala
x => //The inputx.zipWithIndex //Zip each element with its index.map((?,i) => //For every element `?` and its index `i`,x take i+1 //Take the first i+1 elements- count //Count how many of them
?.==) //Are equal to `?` (the element)- ```
- # Scala 3, <s>50</s> 44 bytes
- ```scala
- ? => ?.indices zip?map(?take _+1 count _.==)
- ```
- [Try it in Scastie!](https://scastie.scala-lang.org/ewPr1E4GRlKO7nkufl0qzA)
- This is an annoying, stupid approach. The more elegant one using `inits` didn't work out.
- ```scala
- ? => //The input
- ?.indices //The indices
- zip ? //Zip them with the elements of the input
- .map( //For every index i and the corresponding element x,
- ? take _+1 //Take the first i+1 elements of the input
- count //Count how many of them
- _.==) //Are equal to the element x
- ```
#2: Post edited
- # Scala 3, 50 bytes
```- x=>x.zipWithIndex.map((?,i)=>x take i+1 count?.==)
- ```
- [Try it in Scastie!](https://scastie.scala-lang.org/y4DHryrDRhGY3km2q9Kx6w)
This is an annoying, stupid approach. The more elegant one using `inits` didn't work out.
- # Scala 3, 50 bytes
- ```scala
- x=>x.zipWithIndex.map((?,i)=>x take i+1 count?.==)
- ```
- [Try it in Scastie!](https://scastie.scala-lang.org/y4DHryrDRhGY3km2q9Kx6w)
- This is an annoying, stupid approach. The more elegant one using `inits` didn't work out.
- ```scala
- x => //The input
- x.zipWithIndex //Zip each element with its index
- .map((?,i) => //For every element `?` and its index `i`,
- x take i+1 //Take the first i+1 elements
- count //Count how many of them
- ?.==) //Are equal to `?` (the element)
- ```