Advertisement
MiroJoseph

Pyramid Array

Apr 8th, 2020
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.63 KB | None | 0 0
  1. My:
  2. import scala.collection.mutable.ListBuffer
  3. object Kata {
  4.   def pyramid(n: Int): List[List[Int]] = {
  5.   val arr =ListBuffer.empty[List[Int]]
  6.   def fulling( index: Int=0): List[List[Int]]={
  7.     if(n==0) {arr+=List.empty[Int]
  8.       arr.remove(0)
  9.       arr.toList}
  10.     else if(index<=n) {arr+=List.fill(index)(1)
  11.       fulling(index+1)}
  12.     else arr.remove(0)
  13.          arr.toList
  14.   }
  15.   fulling()
  16. }
  17. }
  18.  
  19. Other:
  20. object Kata {
  21.  
  22.   def pyramid(n: Int): List[List[Int]] =
  23.     List.tabulate(n)(i => List.fill(i+1)(1))
  24. }
  25.  
  26. object Kata {
  27.   def pyramid(n: Int): List[List[Int]] = {
  28.     (1 to n).map(List.fill(_)(1)).toList
  29.   }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement