Ejemplo n.º 1
0
        internal Listy Edit(Listy editList)
        {
            var data = GetById(editList.Id);

            editList.Title = editList.Title != null ? editList.Title : data.Title;
            return(_repo.Edit(editList));
        }
        public static void Main()
        {
            int[] array = { 1, 2, 4, 5, 6, 7, 9, 10, 11, 12, 13, 14, 16, 18 };
            Listy list  = new Listy(array);

            Search(list, 18);
        }
        public static int Search(Listy list, int value)
        {
            int index = 1;

            while (list.elementAt(index) != -1 && list.elementAt(index) < value)
            {
                index *= 2;
            }
            return(BinarySearch(list, value, index / 2, index));
        }
Ejemplo n.º 4
0
        internal Listy Edit(Listy updated)
        {
            string sql = @"
        UPDATE lists
        SET
         title = @Title
        WHERE id = @Id;";

            _db.Execute(sql, updated);
            return(updated);
        }
Ejemplo n.º 5
0
 public ActionResult <Listy> Create([FromBody] Listy newListy)
 {
     try
     {
         return(Ok(_ls.Create(newListy)));
     }
     catch (Exception e)
     {
         return(BadRequest(e.Message));
     }
 }
Ejemplo n.º 6
0
        public void Should_Throw_GetIndexer_Not_In_Range_Low()
        {
            //Arrange
            var listy = new Listy();
            int temp;
            //Act
            Action act = () => temp = listy[-1];

            //Assert
            act.ShouldThrow <ArgumentOutOfRangeException>();
        }
Ejemplo n.º 7
0
        public void Should_Check_GetIndexer_Not_In_Range_High()
        {
            //Arrange
            var listy = new Listy();

            //Act
            var result = listy[5];

            //Assert
            result.ShouldBeEquivalentTo(-1);
        }
Ejemplo n.º 8
0
        public void Should_Throw_If_Add_Negative_Number()
        {
            //Arrange
            var listy = new Listy();

            //Act
            Action act = () => listy[0] = -2;

            //Assert
            act.ShouldThrow <ArgumentException>();
        }
Ejemplo n.º 9
0
 public ActionResult <Listy> Edit(int id, [FromBody] Listy editListy)
 {
     try
     {
         return(Ok(_ls.Edit(editListy)));
     }
     catch (Exception e)
     {
         return(BadRequest(e.Message));
     }
 }
Ejemplo n.º 10
0
        public void SearchWithNoSizeTest(int[] testArray, int testItem, int expectedIndex)
        {
            // Arrange
            var testListy = new Listy(testArray);

            // Act
            int resultIndex = SearchWithNoSize(testListy, testItem);

            // Assert
            Assert.AreEqual(expectedIndex, resultIndex, "SearchWithNoSize test failed.");
        }
Ejemplo n.º 11
0
        public void Should_Should_Initialize_With_Default_Max_Int_Element()
        {
            //Act
            var listy = new Listy();

            //Assert
            listy[0].ShouldBeEquivalentTo(Int32.MaxValue);
            listy[1].ShouldBeEquivalentTo(Int32.MaxValue);
            listy[2].ShouldBeEquivalentTo(Int32.MaxValue);
            listy[3].ShouldBeEquivalentTo(Int32.MaxValue);
            listy[4].ShouldBeEquivalentTo(-1);
        }
Ejemplo n.º 12
0
        internal Listy Create(Listy newList)
        {
            string sql = @"
      INSERT INTO lists 
      (creatorId, title) 
      VALUES 
      (@CreatorId, @Title);
      SELECT LAST_INSERT_ID();";
            int    id  = _db.ExecuteScalar <int>(sql, newList);

            newList.Id = id;
            return(newList);
        }
Ejemplo n.º 13
0
        public void Should_Check_GetIndexer()
        {
            //Arrange
            var listy = new Listy();

            //Act
            listy[0] = 1;
            listy[1] = 5;

            //Assert
            listy[0].ShouldBeEquivalentTo(1);
            listy[1].ShouldBeEquivalentTo(5);
        }
Ejemplo n.º 14
0
        public void Should_Throw_If_SetterIndexer_Not_In_Range()
        {
            //Arrange
            var listy = new Listy();

            //Act
            Action actLower  = () => listy[-1] = 2;
            Action actHigher = () => listy[5] = 3;

            //Assert
            actLower.ShouldThrow <ArgumentOutOfRangeException>();
            actHigher.ShouldThrow <ArgumentOutOfRangeException>();
        }
Ejemplo n.º 15
0
        internal void Create(ListProfile lr, string id)
        {
            Listy list = _lr.GetById(lr.ListId);

            if (list == null)
            {
                throw new Exception("Invalid Id");
            }
            if (list.CreatorId != id)
            {
                throw new NotAuthorized("Not The Owner of this List");
            }
            _repo.Create(lr);
        }
        /// <summary>
        /// Find an estimated length and use regular binary search
        /// <para>Time Complexity: O(log(n))</para>
        /// <para>Space Complexity: O(log(n)) - recursion depth</para>
        /// </summary>
        /// <param name="list"></param>
        /// <param name="item"></param>
        /// <returns></returns>
        public static int SearchWithNoSize(Listy list, int item)
        {
            if (list == null)
            {
                return(-1);
            }

            // Find length - runtime O(log(n))
            int index = 1;

            while (list.ElementAt(index) != -1 && list.ElementAt(index) < item)
            {
                index *= 2;
            }
            return(Search(list, item, index / 2, index));
        }
Ejemplo n.º 17
0
        public void Should_Check_Resize()
        {
            //Arrange
            var listy  = new Listy();
            var values = new int[] { 1, 2, 3, 4, 5 };

            //Act
            for (int i = 0; i < values.Length; i++)
            {
                listy[i] = values[i];
            }

            //Assert
            for (int i = 0; i < values.Length; i++)
            {
                listy[i].ShouldBeEquivalentTo(values[i]);
            }
        }
Ejemplo n.º 18
0
        public List <Stena> pack(List <Stena> steny)
        {
            List <Stena> sorted = steny.SelectMany(s => s.Length > ListaLen ? s.split(ListaLen) : s.reset()).OrderByDescending(s => s.Length).ToList();

            sorted.ForEach(stena => {
                Lista fit = Listy.Find(lista => lista.UsedListaLen + stena.Length <= ListaLen);
                if (fit == null)
                {
                    Listy.Add(new Lista(stena));
                }
                else
                {
                    fit.add(stena);
                }
            });

            return(sorted);
        }
Ejemplo n.º 19
0
        /// <summary>
        /// Searches an array-like data structure that has no size method or property
        /// </summary>
        /// <param name="list"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static int SearchListy(Listy <int> list, int key)
        {
            if (list == null || list.ElementAt(0) == -1)
            {
                return(-1);
            }

            int index = 1;

            while (list.ElementAt(index) != -1 && list.ElementAt(index) < key)
            {
                index *= 2;
            }

            int low  = index / 2;
            int high = index;

            while (low < high)
            {
                int mid = (low + high) / 2;

                if (list.ElementAt(mid) == key)
                {
                    return(mid);
                }
                else if (list.ElementAt(mid) == -1)
                {
                    high = mid - 1;
                }
                else if (list.ElementAt(mid) < key)
                {
                    low = mid + 1;
                }
                else if (list.ElementAt(mid) > key)
                {
                    high = mid - 1;
                }
            }

            return(-1);
        }
Ejemplo n.º 20
0
        public void Should_Should_Rearange_With_Default_Max_Int_Element()
        {
            var listy  = new Listy();
            var values = new int[] { 1, 2, 3, 4, 5 };

            //Act
            for (int i = 0; i < values.Length; i++)
            {
                listy[i] = values[i];
            }

            //Assert
            for (int i = 0; i < values.Length; i++)
            {
                listy[i].ShouldBeEquivalentTo(values[i]);
            }
            listy[5].ShouldBeEquivalentTo(Int32.MaxValue);
            listy[6].ShouldBeEquivalentTo(Int32.MaxValue);
            listy[7].ShouldBeEquivalentTo(Int32.MaxValue);
            listy[8].ShouldBeEquivalentTo(-1);
        }
        private static int Search(Listy list, int item, int start, int end)
        {
            if (start > end)
            {
                return(-1);
            }

            int mid = (start + end) / 2;

            if (list.ElementAt(mid) == item)
            {
                return(mid);
            }
            else if (list.ElementAt(mid) > item || list.ElementAt(mid) == -1)
            {
                return(Search(list, item, start, mid - 1));
            }
            else
            {
                return(Search(list, item, mid + 1, end));
            }
        }
Ejemplo n.º 22
0
        public void SortedSearchNoSize_Should_Find_Value()
        {
            //Arrange
            var listy = new Listy();

            listy[0]  = 0;
            listy[1]  = 1;
            listy[2]  = 2;
            listy[3]  = 3;
            listy[4]  = 4;
            listy[5]  = 5;
            listy[6]  = 6;
            listy[7]  = 7;
            listy[8]  = 8;
            listy[9]  = 9;
            listy[10] = 10;

            //Act
            var result = _sortingNSearching.SortedSearchNoSize(listy, 7);

            //Assert
            result.ShouldBeEquivalentTo(7);
        }
        public static int BinarySearch(Listy list, int value, int low, int high)
        {
            int mid;

            while (low <= high)
            {
                mid = (low + high) / 2;
                int middle = list.elementAt(mid);
                if (middle > value || middle == -1)
                {
                    high = mid - 1;
                }
                else if (middle < value)
                {
                    low = mid + 1;
                }
                else
                {
                    return(mid);
                }
            }
            return(-1);
        }
        //10.4 Sorted Search, No Size: You are given an array like data structure Listy which lacks a size
        // method. It does, however, have an elementAt ( i) method that returns the element at index i in
        // 0( 1) time. If i is beyond the bounds of the data structure, it returns -1. (For this reason, the data
        // structure only supports positive integers.) Given a Li sty which contains sorted, positive integers,
        //     find the index at which an element x occurs. If x occurs multiple times, you may return any index.
        public static int Search(Listy list, int value)
        {
            int index = 1;

            while (list.ElementAt(index) != -1 && list.ElementAt(index) < value)
            {
                index *= 2;
            }

            return(BinarySearch(index / 2, index));

            int BinarySearch(int low, int high)
            {
                int mid;

                while (low <= high)
                {
                    mid = (low + high) / 2;
                    int middle = list.ElementAt(mid);
                    if (middle > value && middle == -1)
                    {
                        high = mid - 1;
                    }
                    else if (middle < value)
                    {
                        low = mid + 1;
                    }
                    else
                    {
                        return(mid);
                    }
                }

                return(-1);
            }
        }
Ejemplo n.º 25
0
        public void SortedSearchNoSize_Should_Not_Find_Value_If_No_Exists()
        {
            //Arrange
            var listy = new Listy();

            listy[0]  = 0;
            listy[1]  = 1;
            listy[2]  = 1;
            listy[3]  = 3;
            listy[4]  = 4;
            listy[5]  = 4;
            listy[6]  = 4;
            listy[7]  = 7;
            listy[8]  = 8;
            listy[9]  = 8;
            listy[10] = 9;
            listy[11] = 9;

            //Act
            var result = _sortingNSearching.SortedSearchNoSize(listy, 10);

            //Assert
            result.ShouldBeEquivalentTo(-1);
        }
Ejemplo n.º 26
0
 internal Listy Create(Listy newList)
 {
     return(_repo.Create(newList));
 }