Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. (* module de multimi *)
  2.  
  3. module Int = struct
  4. type t = int
  5. let compare = compare
  6. end
  7. module IS = Set.Make(Int)
  8.  
  9. module CS = Set.Make(Char)
  10. module CSS = Set.Make(CS)
  11.  
  12. (* multimi *)
  13.  
  14. let charset = CS.add 'a' (CS.add 'b' (CS.singleton 'c'))
  15.  
  16. let coolset = IS.add 112 (IS.add 67 (IS.add 32 (IS.add 270 (IS.singleton 955))))
  17.  
  18. (* afiseaza elementele multimilor continute intr-o multime ca o lista de liste *)
  19.  
  20. let psos set = CSS.elements set |> List.map CS.elements
  21.  
  22.  
  23. (* exercitiul 5 *)
  24.  
  25. let digset =
  26. let rec aux acc = function
  27. | 0 -> acc
  28. | n -> aux (IS.add (n mod 10) acc) (n / 10)
  29. in aux IS.empty
  30.  
  31. let digset2 set = IS.fold (fun elt s -> IS.union (digset elt) s) set IS.empty
  32.  
  33. let digset3 set = IS.fold (fun elt s -> IS.inter (digset elt) s) set (digset (IS.choose set))
  34.  
  35.  
  36. (* exercitiul 6 *)
  37.  
  38. let subsets set =
  39. let rec aux acc set = if CS.is_empty set = true then acc else
  40. aux ( CSS.union acc (CSS.fold (fun elt sacc -> CSS.add ( CS.union elt (CS.singleton (CS.choose set)) ) sacc) acc CSS.empty) ) (CS.diff set (CS.singleton (CS.choose set)))
  41. in aux (CSS.add CS.empty CSS.empty) set
  42.  
  43. let css = subsets charset
  44.  
  45. (* exercitiul 8 *)
  46.  
  47. let ksubsets set card =
  48. let allssets = subsets set in
  49. CSS.filter (fun elt -> CS.cardinal elt = card) allssets
  50.  
  51. (* exercitiul 9 *)
  52.  
  53. let calc a b lim =
  54. let rec aux acc (a1, b1) (a2, b2) =
  55. if a1 + b1 < lim || a2 + b2 < lim then
  56. if a1 + b1 < lim then aux (IS.add (a1+b1) (IS.add b1 acc)) ((a1+b1), b1) (a2, b2)
  57. else aux (IS.add (a2+b2) (IS.add b2 acc)) (a1, b1) ((a2+b2), b2)
  58. else IS.elements acc
  59. in aux IS.empty (a, b) (b, a)
  60.  
  61. (* Tema 5 dupa curs, Ex. 2 *)
  62.  
  63. let explode s =
  64. let rec expl i l =
  65. if i < 0 then l else
  66. expl (i - 1) (s.[i] :: l) in
  67. expl (String.length s - 1) [];;
  68.  
  69. module S = Map.Make(Char);;
  70.  
  71. let lsttomap lst =
  72. let rec aux acc lst = match lst with
  73. | [] -> acc
  74. | h :: t -> if S.mem h acc then aux (S.add h ((S.find h acc) + 1) acc) t else aux (S.add h 1 acc) t
  75. in aux S.empty lst
  76.  
  77. let explode s =
  78. let rec expl i l =
  79. if i < 0 then l else
  80. expl (i - 1) (s.[i] :: l) in
  81. expl (String.length s - 1) [];;
  82.  
  83. module S = Map.Make(Char);;
  84.  
  85. let lsttomap lst =
  86. let rec aux acc lst = match lst with
  87. | [] -> acc
  88. | h :: t -> if S.mem h acc then aux (S.add h ((S.find h acc) + 1) acc) t else aux (S.add h 1 acc) t
  89. in aux S.empty lst
  90.  
  91.  
  92.  
  93. let lsttomap2 lst =
  94. let rec aux acc lst = match lst with
  95. | [] -> acc
  96. | h :: t -> aux (S.add h 1 acc) t
  97. in aux S.empty lst
  98.  
  99. (* FAILED TRY
  100. let digset3 set =
  101. let rec aux acc set = match set with
  102. | IS.empty -> acc
  103. | IS.elements -> aux (IS.inter (digset (IS.choose set)) (digset IS.iter ) (IS.remove (IS.choose set) set)
  104.  
  105. in aux IS.empty set
  106. *)
  107.  
  108. (* exercitiul 6 *)
  109.  
  110. let subsets set =
  111. let rec aux parcurse acc set = if CS.is_empty set = true then acc else
  112. aux (CS.add (CS.choose set) parcurse) (CSS.fold (fun elt sacc -> (CSS.union (CSS.singleton elt) (CSS.singleton (CS.singleton (CS.choose set))) )) acc CSS.empty) (CS.diff set (CS.singleton (CS.choose set)))
  113. in aux CS.empty (CSS.add CS.empty CSS.empty) set
  114.  
  115. let css = subsets charset
  116.  
  117.  
  118. let subsets set =
  119. let rec aux acc set = if CS.is_empty set = true then acc else
  120. aux (CSS.union acc (CSS.fold (fun elt sacc -> CSS.add ((CS.singleton (CS.choose set)) (CSS.singleton elt)) sacc) acc CSS.empty))(CS.diff set (CS.singleton (CS.choose set)))
  121. in aux (CSS.add CS.empty CSS.empty) set
  122.  
  123. let css = subsets charset
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement