Advertisement
baratiistok3

a1

Apr 7th, 2020
2,808
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 10.65 KB | None | 0 0
  1. open System.Collections.Generic
  2.  
  3. // 1)
  4. let IsEven a =
  5.     a%2=0
  6.  
  7.  // 2)
  8. let FindEvenNumbers a =
  9.     let evenOnlyList = List.filter(IsEven) a
  10.     evenOnlyList
  11.  
  12. // 3)
  13. let first100EvenNumberSum =
  14.     let nums = FindEvenNumbers [1..200]
  15.     List.sum nums
  16.  
  17. // 4)
  18. let IsOdd a =
  19.     a%2=1
  20. let FindOddNumbers a =
  21.    let oddOnlyList = List.filter(IsOdd) a
  22.    oddOnlyList
  23.  
  24. let differenceOfSquares a =
  25.    let odds = FindOddNumbers a
  26.    let evens = FindEvenNumbers a
  27.    let oddsSqure = List.map(fun x -> x*x) odds
  28.    let evenSquare = List.map(fun x -> x*x) evens
  29.    let diff = List.sum(oddsSqure) - List.sum(evenSquare)
  30.    printfn "%d" diff
  31.  
  32.  
  33. // 5)
  34. let everyOtherChar a:List<char>=
  35.    let list = Seq.toList a
  36.    let ret = new List<char>()
  37.    for i in [0..2..list.Length] do
  38.        ret.Add(list.[i])
  39.    ret    
  40.  
  41. // 6)
  42.  
  43. //deletes an iten from System.Collections.Generic.List and
  44. //returns unit, so it can be used inside an List.iter
  45. let delItem (a : List<int>) (b:int) =
  46.    a.Remove(b)
  47.    ()
  48.  
  49. let FindAllPrimesUpTo n=
  50.    let nums = [2..n]
  51.    let numbers = new List<int>() //conatins the numbers of the sieve
  52.    List.iter ( fun x -> numbers.Add(x)) nums
  53.    let primes = new List<int>()
  54.    while numbers.Count > 0 do
  55.        let prime = numbers.[0] //the first number of the sieve always will be a prime
  56.        primes.Add prime
  57.        numbers.Remove(prime) //removes the first number of the list
  58.        let multiples = [prime..prime..n] //generates multiples of the last found prime number
  59.        List.iter(fun x-> delItem numbers x) multiples //removes the multiples from the sieve list
  60.    List.ofSeq(primes);
  61.  
  62. // 7)
  63. let SumPrimeDifferencesUpTo n =
  64.    let primes = FindAllPrimesUpTo 1000
  65.    let h = primes.[..primes.Length-2]
  66.    let t = primes.Tail
  67.    let pairs = List.zip h t //generates a list of tuples neighboring primes
  68.    let ret = List.sumBy(fun x -> snd x - fst x) pairs
  69.    ret
  70.  
  71. // 8)
  72. let FindNthPrime n=
  73.    let max = 10000
  74.    let nums = [2..max]
  75.    let numbers = new List<int>()
  76.    List.iter ( fun x -> numbers.Add(x)) nums
  77.    let mutable prime = 0;
  78.    for i in [1..n] do //same alforthm as in task 6, the only difference is cycle
  79.        let p = numbers.[0]
  80.        let _ = numbers.Remove(p)
  81.        let nums = [p..p..max]
  82.        List.iter(fun x-> delItem numbers x) nums
  83.        prime <- p        
  84.    prime
  85.  
  86. // 9)
  87. type Gender =
  88.    | Male
  89.    | Female
  90. type Person =
  91.    {
  92.        FirstName: string
  93.        LastName: string
  94.        Age: int
  95.        Gender: Gender
  96.    }
  97.  
  98. let p1 = {FirstName="Fname1";LastName = "Lastname1"; Age = 10; Gender=Male}
  99. let p2 = {FirstName="Fname2";LastName = "Lastname2"; Age = 20; Gender=Female}
  100. let p3 = {FirstName="Fname3";LastName = "Lastname3"; Age = 30; Gender=Male}
  101. let p4 = {FirstName="Fname4";LastName = "Lastname4"; Age = 50; Gender=Male}
  102. let p5 = {FirstName="Fname5";LastName = "Lastname5"; Age = 40; Gender=Male}
  103. let p6 = {FirstName="Fname6";LastName = "Lastname6"; Age = 30; Gender=Male}
  104.  
  105. let people = [p1;p2;p3;p4;p5;p6]
  106.  
  107.  
  108. let SumOfAllPeoplesAge p=
  109.     List.sumBy(fun x -> x.Age) p
  110.    
  111. // 10)
  112. let FindPeopleAbove p n=
  113.     List.filter(fun x -> x.Age > n) p
  114.  
  115. // 11)
  116. let FindAverageAge p =
  117.     List.averageBy(fun x->float x.Age) people
  118.  
  119. // 12)
  120. let NameInterchange p =
  121.     List.map  (fun x -> {x with
  122.         LastName = x.FirstName
  123.         FirstName = x.LastName}) p;;
  124.  
  125. // 13)
  126.  
  127. //changes the lastname of the person if oldname equal with person's latsname
  128. let ChangeName(p,oldName, newName)=
  129.     match oldName with
  130.     | last when last=p.LastName -> {p with LastName = newName}
  131.     | _ -> p
  132.  
  133. let nl = [("Lastname1", "Lastname1mod"); ("Lastname3", "Lmod3")]
  134.  
  135.  
  136. let rec ApplyMarrige(namelist:(string * string) list ,people:Person list) =
  137.     if namelist.Length = 0 then //stops the recursion when no more namechange to apply, returns with people list
  138.         people  
  139.     else
  140.         let n = List.head namelist //get the first of the namlist
  141.         let f = fun p -> (ChangeName(p, fst(n), snd(n)))
  142.         let p = List.map(f) people //apply marrige to the list only with the first name
  143.         ApplyMarrige(List.tail(namelist), p) //recursively call this function with to new people list and the reduced namelist
  144.  
  145. // 14)
  146. let ChangeNameFemaleOnly(p,oldName, newName)=
  147.     match oldName with
  148.     | last when last=p.LastName && p.Gender = Female-> {p with LastName = newName}
  149.     | _ -> p
  150.  
  151. //same alogrithm as in task 13, the only difference is in ChangeName
  152. let rec ApplyMarrigeFemaleOnly(namelist:(string * string) list ,people:Person list) =
  153.     if namelist.Length = 0 then
  154.         people
  155.     else
  156.         let n = List.head namelist
  157.         let f = fun p -> (ChangeNameFemaleOnly(p, fst(n), snd(n)))
  158.         let p = List.map(f) people
  159.         ApplyMarrige(List.tail(namelist), p)
  160.  
  161. // 15)
  162.  
  163. type Employee =
  164.     | Developer of Person
  165.     | TeamLeader of Person * Person list
  166.     | Manager of Person * Person list list
  167.  
  168. let emp1 = Developer(p1);
  169. let emp2 = Developer(p2);
  170. let tl1 = TeamLeader(p3, [p1; p2])
  171. let tl2 = TeamLeader(p6, [p5;p4;p3])
  172. let man1 = Manager(p1, [[p1];[p2;p3;p4]])
  173.  
  174. let empList = [emp1; emp2; tl1;tl2; man1]
  175.  
  176. let FindPersonWithLargestTeam  (empList: Employee list)=
  177.     let f = function
  178.         |TeamLeader (t, l)  -> Some (t, l.Length)
  179.         |_ -> None
  180.     //makes a list with teamleaders only
  181.     //the list contains a tuple of person and the count of people they manage
  182.     let teamLeaderswithCount = List.choose(f) empList
  183.     let tmax = List.maxBy(fun x-> snd(x)) teamLeaderswithCount //Find the biggest count by the secound element of tuple
  184.     fst(tmax)
  185.    
  186. // 16)
  187. let FindLeadersWithOlderTeamMembers (empList: Employee list)=
  188.     //find oldest person in a person lint
  189.     let oldest (p: Person list)=
  190.         let max = List.maxBy(fun x-> x.Age) p
  191.         max.Age
  192.     let f = function
  193.         |TeamLeader (t, l)  -> Some (t, l)
  194.         |Manager (t, l) -> Some (t, List.concat l)
  195.         |_ -> None
  196.     let teamLeadersAndManagers = List.choose(f) empList //select teamleaders and mangeers from  emplist
  197.     let ret = List.filter(fun x -> (fst(x).Age < oldest(snd(x)))) teamLeadersAndManagers //selects those who have older team members
  198.     ret
  199.        
  200. // 17)
  201. //same alogrithm as in task 16, the only difference is in the filter condition
  202. let FindLeadersWithOlderTeamMembersThan (age:int) (empList: Employee list)=
  203.     let oldest (p: Person list)=
  204.         let max = List.maxBy(fun x-> x.Age) p
  205.         max.Age
  206.     let f = function
  207.         |TeamLeader (t, l)  -> Some (t, l)
  208.         |Manager (t, l) -> Some (t, List.concat l)
  209.         |_ -> None
  210.     let teamLeaders = List.choose(f) empList
  211.     let ret = List.filter(fun x -> (age < oldest(snd(x)))) teamLeaders
  212.     ret
  213.  
  214. // 18)
  215.  
  216.  
  217.  // 19)
  218.  //Non-tail recursion can cause stack overflow.
  219.  //Tail recursion is easily optimized by the complier.
  220.  
  221.  
  222. //extra credit 1)
  223.  
  224. type Expr =
  225.     | Var of string
  226.     | Number of float
  227.     | Sum of Expr * Expr
  228.     | Diff of Expr * Expr
  229.     | Mult of Expr * Expr
  230.     | Div of Expr * Expr
  231.  
  232. let x = Var("x")
  233. let y = Var("y")
  234. let n = Number(2.1)
  235. let d1 = Diff(n, x)
  236. let d2 = Diff(d1, y)
  237. let m3 = Mult(d2, n)
  238.  
  239. // 1.1)
  240. let rec Eval2 (env:Map<string, float>, e:Expr) =
  241.     match e with
  242.     | Sum (Number x, Number y) -> Number(x+y)
  243.     | Div (Number x, Number y) -> Number(x/y)
  244.     | Diff (Number x, Number y) -> Number(x-y)
  245.     | Mult (Number x, Number y) -> Number(x*y)
  246.     | Sum (x, Number y) -> Sum(Eval2(env, x), Number y)
  247.     | Div (x, Number y) -> Div(Eval2(env, x), Number y)
  248.     | Diff (x, Number y) -> Diff(Eval2(env, x), Number y)
  249.     | Mult (x, Number y) -> Mult(Eval2(env, x), Number y)
  250.     | Sum (x, y) -> Sum(Eval2(env, x), Eval2(env, y))
  251.     | Div (x, y) -> Div(Eval2(env, x), Eval2(env, y))
  252.     | Diff (x, y) -> Diff(Eval2(env, x), Eval2(env, y))
  253.     | Mult (x, y) -> Mult(Eval2(env, x), Eval2(env, y))
  254.     | Var x -> Number(env.[x])
  255.     | _ -> e
  256.  
  257. //cal Eval2 until its returns with a single Number
  258. let rec Eval (env:Map<string, float>, e:Expr) =
  259.     let d = Eval2(env, e)
  260.     match e with
  261.     | Number x -> x
  262.     | _ -> Eval(env, e)
  263.  
  264. // 1.2)
  265. let rec Subst (s:(string * float) list, e:Expr) =
  266.     let m = Map.ofList(s); //varibles converted to a map so that the corresponding value can be easily found
  267.     match e with
  268.     | Var x -> Number(m.[x])
  269.     | Sum(x, y) -> Sum(Subst(s, x), Subst(s, y))
  270.     | Diff(x, y) -> Diff(Subst(s, x), Subst(s, y))
  271.     | Mult(x, y) -> Mult(Subst(s, x), Subst(s, y))
  272.     | Div(x, y) -> Div(Subst(s, x), Subst(s, y))
  273.     | _ -> e
  274. // 1.3)
  275. let rec PrettyPrint e =
  276.     match e with
  277.     | Number x -> x.ToString()
  278.     | Var x -> x
  279.     | Sum(x, y) -> PrettyPrint(x)+"+"+PrettyPrint(y)
  280.     | Diff(x, y) -> PrettyPrint(x)+"-"+PrettyPrint(y)
  281.     | Mult(x, Number y) -> "("+PrettyPrint(x)+")"+"*"+ y.ToString()
  282.     | Mult(x, Var y) -> "("+PrettyPrint(x)+")"+"*"+ y
  283.     | Mult(Number x, y) -> x.ToString()+"*("+PrettyPrint(y)+")"
  284.     | Mult(Var x, y) -> x+"*("+PrettyPrint(y)+")"
  285.     | Mult(x, y) -> "("+PrettyPrint(x)+")"+"*"+"("+PrettyPrint(y)+")"
  286.     | Div(x, Number y) -> "("+PrettyPrint(x)+")"+"/"+y.ToString();
  287.     | Div(x, Var y) -> "("+PrettyPrint(x)+")"+"/"+y;
  288.     | Div(Number x, y) -> x.ToString()+"/("+PrettyPrint(y)+")"
  289.     | Div(Var x, y) -> x+"/("+PrettyPrint(y)+")"
  290.     | Div(x, y) -> "("+PrettyPrint(x)+")"+"/"+"("+PrettyPrint(y)+")"
  291.     | _-> ""
  292.    
  293. let env =
  294.    [ "x", 1.2;
  295.       "y", 3.6; ]
  296.    |> Map.ofList;;
  297.  
  298. // extra credit 2)
  299.  
  300.  
  301. //stack and queue is represented by a list in my implementation
  302. //the difference in the two types:
  303. //   -in the case of a stack, the new item is placed at the start of the list
  304. //   -in the case of a queue, the new item is placed at the end of the list
  305. type Stack2<'a>=
  306.    val mutable s: 'a list
  307.  
  308.     new () as self =
  309.         self.s <- List.empty
  310.         Stack2()
  311.  
  312.     member self.Clear =
  313.         self.s <- List.Empty
  314.         ()
  315.  
  316.     member self.Peek =
  317.         List.head self.s
  318.  
  319.     member self.Pop =
  320.         let ret = List.head self.s
  321.         self.s <- List.tail self.s
  322.         ret
  323.  
  324.     member self.Push  o=
  325.         self.s <- o :: self.s
  326.         ()
  327.  
  328.     member self.ToArray =
  329.         List.toArray self.s
  330.  
  331. type Queue2<'a>=
  332.    val mutable q: 'a list
  333.  
  334.     new() as self=
  335.         self.q <- List.empty
  336.         Queue2()
  337.  
  338.     member self.Clear =
  339.         self.q <- List.Empty
  340.         ()
  341.  
  342.     member self.Enqueue o=
  343.         self.q <-  List.append  self.q o
  344.  
  345.     member self.Dequeue =
  346.         let ret = List.head self.q
  347.         self.q <- List.tail self.q
  348.         ret
  349.  
  350.     member self.Peek =
  351.         List.head self.q
  352.  
  353.     member self.ToArray =
  354.         List.toArray self.q
  355.  
  356.     member self.Count =
  357.         List.length
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement