From 589f30633ea107bd9f6f8d079e76551f45d25e30 Mon Sep 17 00:00:00 2001 From: Varun Upadhyay Date: Sat, 28 May 2016 06:39:56 +0530 Subject: [PATCH 01/14] Create Data-Structure-ARRAYS.md --- Data-Structure-ARRAYS.md | 41 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Data-Structure-ARRAYS.md diff --git a/Data-Structure-ARRAYS.md b/Data-Structure-ARRAYS.md new file mode 100644 index 000000000..503ff11c6 --- /dev/null +++ b/Data-Structure-ARRAYS.md @@ -0,0 +1,41 @@ +#Arrays + +`array` is a kind of data structure that can store a fixed-size sequential collection of elements of the same type. An `array` is used to store +a collection of data, but it is often more useful to think of an `array` as a collection of variables of the same type. + +`array` consists of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last +element. + +## Arrays in Python + +Python doesn't have a native `array` data structure. An `array` in Python should not be confused with list. The major difference between a `list` +and an `array` in Python is that a `list` can have different types of values whereas an `array` should have all the values of same type. + +**Declaration of `array`:** + +```python +from array import array +intarray = array('i') # Declares an array of integer type +```python + +**Adding elements to an `array`:** + +```python +intarray.append(1) # Adds an integer value of 1 to the array +intarray.append(0) # Adds an integer value of 0 to the array +intarray.append(-1) # Adds an integer value of -1 to the array + +intarray.append('d') # Would give a TypeError as the array is of integer type. Resolve this error and then move ahead. +```python + +**Printing an `array`** + +```python +print(intarray) # Returns array('i', [1, 4, -1]) +print(intarray[0]) # Returns 1 which is the element at index 0 of the array +print(intarray[3]) # Would give IndexError as there is no element at index 3 of array. Resolve this error and then move ahead + +# To print all the elements of the array +for i in intarray: + print(i) + ```python From 1d915ca054052ca8cb1c2949f94cc1b627456e49 Mon Sep 17 00:00:00 2001 From: Varun Upadhyay Date: Sat, 28 May 2016 06:43:41 +0530 Subject: [PATCH 02/14] Update Data-Structure-ARRAYS.md --- Data-Structure-ARRAYS.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Data-Structure-ARRAYS.md b/Data-Structure-ARRAYS.md index 503ff11c6..7a58e643b 100644 --- a/Data-Structure-ARRAYS.md +++ b/Data-Structure-ARRAYS.md @@ -16,9 +16,9 @@ and an `array` in Python is that a `list` can have different types of values whe ```python from array import array intarray = array('i') # Declares an array of integer type -```python +``` -**Adding elements to an `array`:** +**Adding elements to `array`:** ```python intarray.append(1) # Adds an integer value of 1 to the array @@ -26,9 +26,9 @@ intarray.append(0) # Adds an integer value of 0 to the array intarray.append(-1) # Adds an integer value of -1 to the array intarray.append('d') # Would give a TypeError as the array is of integer type. Resolve this error and then move ahead. -```python +``` -**Printing an `array`** +**Printing an `array`:** ```python print(intarray) # Returns array('i', [1, 4, -1]) @@ -38,4 +38,4 @@ print(intarray[3]) # Would give IndexError as there is no element at index 3 of # To print all the elements of the array for i in intarray: print(i) - ```python +``` From 317b0b1e6cb7e709a516e99dc87af21ea7b9cc2c Mon Sep 17 00:00:00 2001 From: Varun Upadhyay Date: Sat, 28 May 2016 06:45:08 +0530 Subject: [PATCH 03/14] Update Data-Structure-ARRAYS.md --- Data-Structure-ARRAYS.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Data-Structure-ARRAYS.md b/Data-Structure-ARRAYS.md index 7a58e643b..55efbc461 100644 --- a/Data-Structure-ARRAYS.md +++ b/Data-Structure-ARRAYS.md @@ -8,7 +8,7 @@ element. ## Arrays in Python -Python doesn't have a native `array` data structure. An `array` in Python should not be confused with list. The major difference between a `list` +Python doesn't have a native `array` data structure. An `array` in Python should not be confused with `list`. The major difference between a `list` and an `array` in Python is that a `list` can have different types of values whereas an `array` should have all the values of same type. **Declaration of `array`:** @@ -25,7 +25,9 @@ intarray.append(1) # Adds an integer value of 1 to the array intarray.append(0) # Adds an integer value of 0 to the array intarray.append(-1) # Adds an integer value of -1 to the array -intarray.append('d') # Would give a TypeError as the array is of integer type. Resolve this error and then move ahead. +intarray.append('d') # Would give a TypeError as the array is of integer type. + +#Resolve the above error and then move ahead. ``` **Printing an `array`:** @@ -33,7 +35,9 @@ intarray.append('d') # Would give a TypeError as the array is of integer type. R ```python print(intarray) # Returns array('i', [1, 4, -1]) print(intarray[0]) # Returns 1 which is the element at index 0 of the array -print(intarray[3]) # Would give IndexError as there is no element at index 3 of array. Resolve this error and then move ahead +print(intarray[3]) # Would give IndexError as there is no element at index 3 of array. + +#Resolve the above error and then move ahead. # To print all the elements of the array for i in intarray: From 653e0c8685cf2fd600032ba547999ba3fd285b11 Mon Sep 17 00:00:00 2001 From: Varun Upadhyay Date: Sat, 28 May 2016 07:00:38 +0530 Subject: [PATCH 04/14] Update Data-Structure-ARRAYS.md --- Data-Structure-ARRAYS.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Data-Structure-ARRAYS.md b/Data-Structure-ARRAYS.md index 55efbc461..d30f1e718 100644 --- a/Data-Structure-ARRAYS.md +++ b/Data-Structure-ARRAYS.md @@ -24,6 +24,7 @@ intarray = array('i') # Declares an array of integer type intarray.append(1) # Adds an integer value of 1 to the array intarray.append(0) # Adds an integer value of 0 to the array intarray.append(-1) # Adds an integer value of -1 to the array +intarray.append(1) # Again adds an integer value of 1 to the array intarray.append('d') # Would give a TypeError as the array is of integer type. @@ -43,3 +44,19 @@ print(intarray[3]) # Would give IndexError as there is no element at index 3 of for i in intarray: print(i) ``` + +**Basic operations on `array`:** + +```python +len(intarray) # Returns the length of the array i.e. 3 +intarray.itemsize # Returns length in bytes of one array item i.e. 4 as it is an integer +intarray.count(1) # Returns the number of occurrences of 1 in the array i.e. 2 +intarray.insert(1, 3) # Insert a new item with value x in the array before position i +intarray.remove(1) # Remove the first occurrence of 1 from the array +intarray.reverse() # Reverse the order of the items in the array +intarray.pop(1) # Removes the item with the index 1 from the array and returns it +``` + +:rocket: [Run Code](https://repl.it/CWJB) + +[Official Docs](https://docs.python.org/3.5/library/array.html) From 9d99fc8f852b58db51e92db3398e59e280325f3d Mon Sep 17 00:00:00 2001 From: Varun Upadhyay Date: Sat, 28 May 2016 14:10:12 +0530 Subject: [PATCH 05/14] Update Data-Structure-ARRAYS.md --- Data-Structure-ARRAYS.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Data-Structure-ARRAYS.md b/Data-Structure-ARRAYS.md index d30f1e718..f9d29a036 100644 --- a/Data-Structure-ARRAYS.md +++ b/Data-Structure-ARRAYS.md @@ -11,7 +11,7 @@ element. Python doesn't have a native `array` data structure. An `array` in Python should not be confused with `list`. The major difference between a `list` and an `array` in Python is that a `list` can have different types of values whereas an `array` should have all the values of same type. -**Declaration of `array`:** +####Declaration of `array`: ```python from array import array @@ -31,7 +31,7 @@ intarray.append('d') # Would give a TypeError as the array is of integer type. #Resolve the above error and then move ahead. ``` -**Printing an `array`:** +####Printing an `array`: ```python print(intarray) # Returns array('i', [1, 4, -1]) @@ -45,7 +45,7 @@ for i in intarray: print(i) ``` -**Basic operations on `array`:** +####Basic operations on `array`: ```python len(intarray) # Returns the length of the array i.e. 3 From 950b0dbe4886057fce7f582cc35be3011ba9f39d Mon Sep 17 00:00:00 2001 From: Varun Upadhyay Date: Sat, 28 May 2016 14:10:42 +0530 Subject: [PATCH 06/14] Update Data-Structure-ARRAYS.md --- Data-Structure-ARRAYS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Data-Structure-ARRAYS.md b/Data-Structure-ARRAYS.md index f9d29a036..d607ccdc8 100644 --- a/Data-Structure-ARRAYS.md +++ b/Data-Structure-ARRAYS.md @@ -18,7 +18,7 @@ from array import array intarray = array('i') # Declares an array of integer type ``` -**Adding elements to `array`:** +####Adding elements to `array`: ```python intarray.append(1) # Adds an integer value of 1 to the array From 9cac6af893af076b1b99c4c028e0ac717c04eff9 Mon Sep 17 00:00:00 2001 From: Varun Upadhyay Date: Sat, 28 May 2016 14:29:16 +0530 Subject: [PATCH 07/14] Update Data-Structure-ARRAYS.md --- Data-Structure-ARRAYS.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Data-Structure-ARRAYS.md b/Data-Structure-ARRAYS.md index d607ccdc8..e255e5cf2 100644 --- a/Data-Structure-ARRAYS.md +++ b/Data-Structure-ARRAYS.md @@ -11,14 +11,14 @@ element. Python doesn't have a native `array` data structure. An `array` in Python should not be confused with `list`. The major difference between a `list` and an `array` in Python is that a `list` can have different types of values whereas an `array` should have all the values of same type. -####Declaration of `array`: +#### Declaration of `array`: ```python from array import array intarray = array('i') # Declares an array of integer type ``` -####Adding elements to `array`: +#### Adding elements to `array`: ```python intarray.append(1) # Adds an integer value of 1 to the array @@ -31,7 +31,7 @@ intarray.append('d') # Would give a TypeError as the array is of integer type. #Resolve the above error and then move ahead. ``` -####Printing an `array`: +#### Printing an `array`: ```python print(intarray) # Returns array('i', [1, 4, -1]) @@ -45,7 +45,7 @@ for i in intarray: print(i) ``` -####Basic operations on `array`: +#### Basic operations on `array`: ```python len(intarray) # Returns the length of the array i.e. 3 From 4b7db7ff49044d2af6f9eafbdb0e62a51b0c69b6 Mon Sep 17 00:00:00 2001 From: Varun Upadhyay Date: Sat, 28 May 2016 17:58:13 +0530 Subject: [PATCH 08/14] Update Data-Structure-ARRAYS.md --- Data-Structure-ARRAYS.md | 44 ++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/Data-Structure-ARRAYS.md b/Data-Structure-ARRAYS.md index e255e5cf2..b9633b1bf 100644 --- a/Data-Structure-ARRAYS.md +++ b/Data-Structure-ARRAYS.md @@ -21,12 +21,12 @@ intarray = array('i') # Declares an array of integer type #### Adding elements to `array`: ```python -intarray.append(1) # Adds an integer value of 1 to the array -intarray.append(0) # Adds an integer value of 0 to the array -intarray.append(-1) # Adds an integer value of -1 to the array -intarray.append(1) # Again adds an integer value of 1 to the array - -intarray.append('d') # Would give a TypeError as the array is of integer type. + intarray.append(1) # Adds an integer value of 1 to the array + intarray.append(0) # Adds an integer value of 0 to the array + intarray.append(-1) # Adds an integer value of -1 to the array + intarray.append(1) # Again adds an integer value of 1 to the array + + intarray.append('d') # Would give a TypeError as the array is of integer type. #Resolve the above error and then move ahead. ``` @@ -34,27 +34,27 @@ intarray.append('d') # Would give a TypeError as the array is of integer type. #### Printing an `array`: ```python -print(intarray) # Returns array('i', [1, 4, -1]) -print(intarray[0]) # Returns 1 which is the element at index 0 of the array -print(intarray[3]) # Would give IndexError as there is no element at index 3 of array. - -#Resolve the above error and then move ahead. - -# To print all the elements of the array -for i in intarray: - print(i) + print(intarray) # Returns array('i', [1, 4, -1]) + print(intarray[0]) # Returns 1 which is the element at index 0 of the array + print(intarray[3]) # Would give IndexError as there is no element at index 3 of array. + + #Resolve the above error and then move ahead. + + # To print all the elements of the array + for i in intarray: + print(i) ``` #### Basic operations on `array`: ```python -len(intarray) # Returns the length of the array i.e. 3 -intarray.itemsize # Returns length in bytes of one array item i.e. 4 as it is an integer -intarray.count(1) # Returns the number of occurrences of 1 in the array i.e. 2 -intarray.insert(1, 3) # Insert a new item with value x in the array before position i -intarray.remove(1) # Remove the first occurrence of 1 from the array -intarray.reverse() # Reverse the order of the items in the array -intarray.pop(1) # Removes the item with the index 1 from the array and returns it + len(intarray) # Returns the length of the array i.e. 3 + intarray.itemsize # Returns length in bytes of one array item i.e. 4 as it is an integer + intarray.count(1) # Returns the number of occurrences of 1 in the array i.e. 2 + intarray.insert(1, 3) # Insert a new item with value x in the array before position i + intarray.remove(1) # Remove the first occurrence of 1 from the array + intarray.reverse() # Reverse the order of the items in the array + intarray.pop(1) # Removes the item with the index 1 from the array and returns it ``` :rocket: [Run Code](https://repl.it/CWJB) From 1267cb21cc6c3208aab8ab9d41669f456ffbc502 Mon Sep 17 00:00:00 2001 From: Varun Upadhyay Date: Sat, 28 May 2016 17:58:59 +0530 Subject: [PATCH 09/14] Update Data-Structure-ARRAYS.md --- Data-Structure-ARRAYS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Data-Structure-ARRAYS.md b/Data-Structure-ARRAYS.md index b9633b1bf..6ebb42ce8 100644 --- a/Data-Structure-ARRAYS.md +++ b/Data-Structure-ARRAYS.md @@ -28,7 +28,7 @@ intarray = array('i') # Declares an array of integer type intarray.append('d') # Would give a TypeError as the array is of integer type. -#Resolve the above error and then move ahead. + #Resolve the above error and then move ahead. ``` #### Printing an `array`: From 8560fa26c5170f3ff7b8a33252092fab8ba82027 Mon Sep 17 00:00:00 2001 From: Varun Upadhyay Date: Sat, 28 May 2016 17:59:33 +0530 Subject: [PATCH 10/14] Update Data-Structure-ARRAYS.md --- Data-Structure-ARRAYS.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Data-Structure-ARRAYS.md b/Data-Structure-ARRAYS.md index 6ebb42ce8..1034ea2b9 100644 --- a/Data-Structure-ARRAYS.md +++ b/Data-Structure-ARRAYS.md @@ -14,8 +14,8 @@ and an `array` in Python is that a `list` can have different types of values whe #### Declaration of `array`: ```python -from array import array -intarray = array('i') # Declares an array of integer type + from array import array + intarray = array('i') # Declares an array of integer type ``` #### Adding elements to `array`: From 8a467327168224f9294f9fb79b5d9bb0f8c1bdd8 Mon Sep 17 00:00:00 2001 From: Varun Upadhyay Date: Sat, 28 May 2016 18:06:04 +0530 Subject: [PATCH 11/14] Update Data-Structure-ARRAYS.md --- Data-Structure-ARRAYS.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Data-Structure-ARRAYS.md b/Data-Structure-ARRAYS.md index 1034ea2b9..c35fd5d7f 100644 --- a/Data-Structure-ARRAYS.md +++ b/Data-Structure-ARRAYS.md @@ -1,7 +1,6 @@ #Arrays -`array` is a kind of data structure that can store a fixed-size sequential collection of elements of the same type. An `array` is used to store -a collection of data, but it is often more useful to think of an `array` as a collection of variables of the same type. +Internally, `array` is a kind of data structure that can store a fixed-size sequential collection of elements of the same type. An `array` is used to store a collection of data, but it is often more useful to think of an `array` as a collection of variables of the same type. `array` consists of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element. From 7caf09c83b85fa43d06f9660df9f160ad29008de Mon Sep 17 00:00:00 2001 From: Varun Upadhyay Date: Sun, 29 May 2016 06:43:05 +0530 Subject: [PATCH 12/14] Update Data-Structure-ARRAYS.md --- Data-Structure-ARRAYS.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Data-Structure-ARRAYS.md b/Data-Structure-ARRAYS.md index c35fd5d7f..f8e7c921f 100644 --- a/Data-Structure-ARRAYS.md +++ b/Data-Structure-ARRAYS.md @@ -1,23 +1,22 @@ -#Arrays +# Data Structure Arrays Internally, `array` is a kind of data structure that can store a fixed-size sequential collection of elements of the same type. An `array` is used to store a collection of data, but it is often more useful to think of an `array` as a collection of variables of the same type. -`array` consists of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last -element. +`array` consists of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element. ## Arrays in Python Python doesn't have a native `array` data structure. An `array` in Python should not be confused with `list`. The major difference between a `list` and an `array` in Python is that a `list` can have different types of values whereas an `array` should have all the values of same type. -#### Declaration of `array`: +#### Declaration of `array` ```python from array import array intarray = array('i') # Declares an array of integer type ``` -#### Adding elements to `array`: +#### Adding elements to `array`: ```python intarray.append(1) # Adds an integer value of 1 to the array From 12d5c6c706092340a3184eec288d22e06650297f Mon Sep 17 00:00:00 2001 From: Varun Upadhyay Date: Sun, 29 May 2016 06:43:42 +0530 Subject: [PATCH 13/14] Rename Data-Structure-ARRAYS.md to Data-Structure-Arrays.md --- Data-Structure-ARRAYS.md => Data-Structure-Arrays.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Data-Structure-ARRAYS.md => Data-Structure-Arrays.md (100%) diff --git a/Data-Structure-ARRAYS.md b/Data-Structure-Arrays.md similarity index 100% rename from Data-Structure-ARRAYS.md rename to Data-Structure-Arrays.md From 65f85a36985362a3df731f90d47b8d0ea2d8b17e Mon Sep 17 00:00:00 2001 From: Varun Upadhyay Date: Sun, 29 May 2016 11:35:05 +0530 Subject: [PATCH 14/14] Update Data-Structure-Arrays.md --- Data-Structure-Arrays.md | 46 ++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/Data-Structure-Arrays.md b/Data-Structure-Arrays.md index f8e7c921f..a3279570c 100644 --- a/Data-Structure-Arrays.md +++ b/Data-Structure-Arrays.md @@ -12,47 +12,47 @@ and an `array` in Python is that a `list` can have different types of values whe #### Declaration of `array` ```python - from array import array - intarray = array('i') # Declares an array of integer type +from array import array +intarray = array('i') # Declares an array of integer type ``` #### Adding elements to `array`: ```python - intarray.append(1) # Adds an integer value of 1 to the array - intarray.append(0) # Adds an integer value of 0 to the array - intarray.append(-1) # Adds an integer value of -1 to the array - intarray.append(1) # Again adds an integer value of 1 to the array - - intarray.append('d') # Would give a TypeError as the array is of integer type. +intarray.append(1) # Adds an integer value of 1 to the array +intarray.append(0) # Adds an integer value of 0 to the array +intarray.append(-1) # Adds an integer value of -1 to the array +intarray.append(1) # Again adds an integer value of 1 to the array + +intarray.append('d') # Would give a TypeError as the array is of integer type. - #Resolve the above error and then move ahead. +#Resolve the above error and then move ahead. ``` #### Printing an `array`: ```python - print(intarray) # Returns array('i', [1, 4, -1]) - print(intarray[0]) # Returns 1 which is the element at index 0 of the array - print(intarray[3]) # Would give IndexError as there is no element at index 3 of array. +print(intarray) # Returns array('i', [1, 4, -1]) +print(intarray[0]) # Returns 1 which is the element at index 0 of the array +print(intarray[3]) # Would give IndexError as there is no element at index 3 of array. - #Resolve the above error and then move ahead. +#Resolve the above error and then move ahead. - # To print all the elements of the array - for i in intarray: - print(i) +# To print all the elements of the array +for i in intarray: + print(i) ``` #### Basic operations on `array`: ```python - len(intarray) # Returns the length of the array i.e. 3 - intarray.itemsize # Returns length in bytes of one array item i.e. 4 as it is an integer - intarray.count(1) # Returns the number of occurrences of 1 in the array i.e. 2 - intarray.insert(1, 3) # Insert a new item with value x in the array before position i - intarray.remove(1) # Remove the first occurrence of 1 from the array - intarray.reverse() # Reverse the order of the items in the array - intarray.pop(1) # Removes the item with the index 1 from the array and returns it +len(intarray) # Returns the length of the array i.e. 3 +intarray.itemsize # Returns length in bytes of one array item i.e. 4 as it is an integer +intarray.count(1) # Returns the number of occurrences of 1 in the array i.e. 2 +intarray.insert(1, 3) # Insert a new item with value x in the array before position i +intarray.remove(1) # Remove the first occurrence of 1 from the array +intarray.reverse() # Reverse the order of the items in the array +intarray.pop(1) # Removes the item with the index 1 from the array and returns it ``` :rocket: [Run Code](https://repl.it/CWJB)