Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. package main
  2.  
  3. import "fmt"
  4.  
  5. /*
  6. In order to destroy Commander Lambda's LAMBCHOP doomsday device, you'll need access to it. But the only door leading to the LAMBCHOP chamber is secured with a unique lock system whose number of passcodes changes daily. Commander Lambda gets a report every day that includes the locks' access codes, but only she knows how to figure out which of several lists contains the access codes. You need to find a way to determine which list contains the access codes once you're ready to go in.
  7.  
  8. Fortunately, now that you're Commander Lambda's personal assistant, she's confided to you that she made all the access codes "lucky triples" in order to help her better find them in the lists. A "lucky triple" is a tuple (x, y, z) where x divides y and y divides z, such as (1, 2, 4). With that information, you can figure out which list contains the number of access codes that matches the number of locks on the door when you're ready to go in (for example, if there's 5 passcodes, you'd need to find a list with 5 "lucky triple" access codes).
  9.  
  10. Write a function answer(l) that takes a list of positive integers l and counts the number of "lucky triples" of (lst[i], lst[j], lst[k]) where i < j < k. The length of l is between 2 and 2000 inclusive. The elements of l are between 1 and 999999 inclusive. The answer fits within a signed 32-bit integer. Some of the lists are purposely generated without any access codes to throw off spies, so if no triples are found, return 0.
  11.  
  12. For example, [1, 2, 3, 4, 5, 6] has the triples: [1, 2, 4], [1, 2, 6], [1, 3, 6], making the answer 3 total.
  13. */
  14.  
  15. func answer(l []int) int {
  16. result := 0
  17. var countX int
  18. var countZ int
  19.  
  20. if len(l) < 3 {
  21. return 0
  22. }
  23.  
  24. for i := 1; i < len(l)-1; i++ {
  25. countX = 0
  26. countZ = 0
  27.  
  28. for j := 0; j < i; j++ {
  29. if l[i]%l[j] == 0 {
  30. countX++
  31. }
  32. }
  33.  
  34. for k := i + 1; k < len(l); k++ {
  35. if l[k]%l[i] == 0 {
  36. countZ++
  37. }
  38. }
  39.  
  40. result += countX * countZ
  41. }
  42.  
  43. return result
  44. }
  45.  
  46. func main() {
  47.  
  48. fmt.Println(answer([]int{1, 1, 1}))
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement