fork download
  1. object Main extends Application {
  2.  
  3. def circularHash[T](list: List[T])(implicit ord: Ordering[T]): Int =
  4. list.sorted.foldLeft(1) { (acc, e) =>
  5. (31 * acc + (if (e == null) 0 else e.hashCode))
  6. }
  7.  
  8. def circulars[T](xs: List[T]): List[List[T]] =
  9. (xs ++ xs).sliding(xs.size, 1).toList.init
  10.  
  11. println(circulars("picture".toList))
  12.  
  13. println(circulars("picture".toList).map { xs => circularHash(xs) })
  14.  
  15. println(circulars("abhinav".toList))
  16.  
  17. println(circulars("abhinav".toList).map { xs => circularHash(xs) })
  18.  
  19. }
Success #stdin #stdout 0.22s 212480KB
stdin
Standard input is empty
stdout
List(List(p, i, c, t, u, r, e), List(i, c, t, u, r, e, p), List(c, t, u, r, e, p, i), List(t, u, r, e, p, i, c), List(u, r, e, p, i, c, t), List(r, e, p, i, c, t, u), List(e, p, i, c, t, u, r))
List(-1891641943, -1891641943, -1891641943, -1891641943, -1891641943, -1891641943, -1891641943)
List(List(a, b, h, i, n, a, v), List(b, h, i, n, a, v, a), List(h, i, n, a, v, a, b), List(i, n, a, v, a, b, h), List(n, a, v, a, b, h, i), List(a, v, a, b, h, i, n), List(v, a, b, h, i, n, a))
List(507089578, 507089578, 507089578, 507089578, 507089578, 507089578, 507089578)