-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy patharray.py
More file actions
60 lines (48 loc) · 2.14 KB
/
Copy patharray.py
File metadata and controls
60 lines (48 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import numpy as np
def createarray(length:'int',dtype='int')->'list':
# To create an array of entered length and entered data type(interger data type is a default data type)
new_array=[] #empty list
for i in range(length):
# if entered dtype is an interger
if dtype=='int':
array_input=int(input(f"Enter {i+1} element : "))
new_array.append(array_input)
# if entered dtype is a string
elif dtype=='str' or dtype=='string':
array_input=str(input("Enter {i+1} element : "))
new_array.append(array_input)
# if entered dtype is a float
elif dtype=='float':
array_input=float(input("Enter {i+1} element : "))
new_array.append(array_input)
created_array = np.array(new_array)
return created_array
def arrayrev(array:'list')->'list':
# To reverese the array elements
temp_array=[]
for i in range(len(array)-1,-1,-1):
temp_array.append(array[i])
reversed_array=np.array(temp_array)
return reversed_array
def fast_arrayrev(array: 'list') -> 'list':
"""
Reverses the elements of the input list.
Parameters:
array (list): The list to be reversed.
Returns:
list: A new list containing the elements of the input list in reverse order.
Steps:
1. The function takes a single argument, `array`, which is expected to be a list.
2. It uses Python's slicing feature to reverse the list:
- The slice notation `array[::-1]` means:
- Start[start::] from the end [:end:] of the list (indicated by the negative step)[start : end : -1].
- Move backwards through the list.
- The result is a new list that contains the elements of `array` in reverse order.
3. The reversed list is stored in the variable `reversed_array`.
4. Finally, the function returns `reversed_array`, which is the reversed version of the input list.
source: https://www.geeksforgeeks.org/python-reversed-vs-1-which-one-is-faster/
"""
reversed_array = array[::-1]
return reversed_array