forked from vtejapy/Beginners-Python-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlength.py
More file actions
87 lines (74 loc) · 2.54 KB
/
length.py
File metadata and controls
87 lines (74 loc) · 2.54 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/python
# -*- coding: utf-8 -*-
import math
# Length of array(number of elements in array)
# is simple algorithm
# Iterates through array
# at each iteration (count) variable
# is incremented
#
# This solution is ideal for small array
# But not that good for bigger array's
# There are many ways to do same thing
# but always go with solution that is
# simple, and has least instructions
# for computer to execute, making
# process faster
# is_ap parameter indicates weather array
# is an arithmetic progression or not
#
# Similarly,
# is_gp indicates weather array is an geometric progression or not
# If array is either ap or gp then it's length can be
# found by derivation of it's general term formula
# e.g. ap's general term,
# tn = a + (n - 1)d
# thus, n = (tn - a) / d + 1
def length(ar, is_ap = False, is_gp = False, big_data = False, data_outline = []):
# Length of data if it is an arithmetic progression
# using derived formula, n = (tn - a) / d + 1
if is_ap:
return ((ar[-1] - ar[0]) / (ar[1] - ar[0])) + 1
# Length of data if it is an geometric progression
# using derived formula, n = ((log base 10 an / a1) / log 10 r) + 1
elif is_gp:
# length is never a float
return int(math.log10((ar[-1] / ar[0])) / math.log10((ar[1] / ar[0])) + 1)
# Length of big data using data outline
# data outline is selective elements to be counted from entire
# data
elif big_data:
count = 0
for element in data_outline:
count += ar.count(element)
return count
# Sequential counting
# of elements in array
else:
res = 0
for item in ar:
res += 1
return res
# Test
# Geometric progression Test
if length([1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096], None, is_gp = True) == 13:
print("\nLength of geometric progression: " + str(13))
print("--> geometric progression counting works!\n")
else:
print("Something's wrong with gp feature")
# Arithmetic progression test
if length([1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43], is_ap = True) == 15:
print("Length of arithmetic progression: " + str(15))
print("--> arithmetic progression counting works!\n")
else:
print("Something's wrong with ap feature")
# Big data test
# Passing only useful numbers in outline array
if length([1, 1, 4, 5, 7, 1, 9, 5, 2, 4, 3, 5, 9], None, None, True, [1, 4, 5, 7, 9]) == 11:
print("Length of arithmetic progression: " + str(11))
print("--> big data counting works!\n")
else:
print("Something's wrong with ap feature")
# Small data
print("Length: " + str(length([1, 1, 2, 3, 5, 8, 13, 21, 34, 55])))
print("Everything Works!\n")