pellekrogholt

Untitled

Oct 20th, 2011
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.05 KB | None | 0 0
  1. /*
  2.  
  3. Exercise 1: Functional programming using delegates and generics
  4. ===============================================================
  5.  
  6. Exercise 1.2 (mandatory) Construct a generic method Merge that takes two elements of type List<T> where
  7. T is some type that implements IComparable<T> and returns an element of type List<T>. The result of
  8. applying Merge to two sorted lists should be a merge of the two lists so, e.g. if x represents the list [0, 2, 4] and y
  9. represents the list [1, 3], the result of applying Merge to x and y should be the list [0, 1, 2, 3, 4]. To use List<T>
  10. you need to include the directive using System.Collections.Generic;.
  11.  
  12. */
  13. using System;
  14. using System.Collections.Generic;
  15.  
  16. namespace exercise_5
  17. {
  18.     public class Exercise_5_1_2
  19.     {              
  20.  
  21.         public static List<T> Merge<T>(List<T> a, List<T> b)
  22.           where T : IComparable<T> {
  23.        
  24.             // sort a and b - otherwise set it as a precondition - not sure how its done in c#
  25.             a.Sort();
  26.             b.Sort();
  27.            
  28.             // new merged list
  29.             List<T> c = new List<T>();  
  30.            
  31.             int i, j, m, n;
  32.            
  33.             i = 0;
  34.             j = 0;
  35.             m = a.Count;
  36.             n = b.Count;
  37.            
  38.            
  39.             do {
  40.            
  41.                 // a[i] <= b[j]
  42.                 if (a[i].CompareTo(b[j]) == -1) {
  43.                     c.Add(a[i]);
  44.                     i++;
  45.                 } else {
  46.                     c.Add(b[j]);
  47.                     j++;               
  48.                 }
  49.            
  50.             } while (i < m && j < n);
  51.            
  52.             if (i < m) {
  53.            
  54.                 for (int p = i; p < m; p++) {
  55.                     c.Add(a[p]);               
  56.                 }
  57.                
  58.             } else {
  59.                
  60.                 for (int p = j; p < n; p++) {  
  61.                
  62.                     c.Add(b[p]);
  63.            
  64.                 }
  65.        
  66.             }
  67.            
  68.             return c;
  69.        
  70.         }    
  71.  
  72.  
  73.        
  74.        
  75.        
  76.         public static void Main() {            
  77.            
  78.             Console.WriteLine("Exercise 5.1.2 ");
  79.            
  80.            
  81.             Console.WriteLine("Merged two lists with various int's");
  82.            
  83.             List<int> l1 = new List<int>();
  84.  
  85.             l1.Add(1);
  86.             l1.Add(33);
  87.             l1.Add(2);
  88.             l1.Add(3);
  89.             l1.Add(5);         
  90.             l1.Add(12);
  91.             l1.Add(4);
  92.  
  93.            
  94.             List<int> l2 = new List<int>();
  95.            
  96.             l2.Add(13);
  97.             l2.Add(1);
  98.             l2.Add(6);
  99.             l2.Add(3);
  100.             l2.Add(25);
  101.                        
  102.             List<int> mergedL1L2 = Merge(l1, l2);
  103.            
  104.             foreach(int x in mergedL1L2) {Console.Write(x + " ");}
  105.             Console.WriteLine("");
  106.            
  107.             Console.WriteLine("Merged two lists with various int's (stated in the exercise)");
  108.            
  109.             List<int> l3 = new List<int>();
  110.  
  111.             l3.Add(0);
  112.             l3.Add(2);
  113.             l3.Add(4);
  114.            
  115.             List<int> l4 = new List<int>();
  116.            
  117.             l4.Add(1);
  118.             l4.Add(3);
  119.            
  120.             List<int> mergedL3L4 = Merge(l3, l4);
  121.            
  122.             foreach(int x in mergedL3L4) {Console.Write(x + " ");}
  123.             Console.WriteLine("");
  124.            
  125.             Console.WriteLine("Merged two lists with various string's (just a proof of contest)");
  126.            
  127.             List<string> l5 = new List<string>();
  128.            
  129.             l5.Add("scala");
  130.             l5.Add("python");
  131.             l5.Add("go");
  132.            
  133.             List<string> l6 = new List<string>();
  134.            
  135.             l6.Add("lisp");
  136.             l6.Add("java");
  137.             l6.Add("c#");
  138.             l6.Add("ruby");
  139.             l6.Add("scheme");
  140.             l6.Add("sql");
  141.            
  142.             List<string> mergedL5L6 = Merge(l5, l6);
  143.            
  144.             foreach(string x in mergedL5L6) {Console.Write(x + " ");}
  145.             Console.WriteLine("");
  146.            
  147.                        
  148.         }
  149.        
  150.     }
  151. }
  152.  
Advertisement
Add Comment
Please, Sign In to add comment