Skip to content

Commit e5386ce

Browse files
committed
-update
1 parent 914f2a9 commit e5386ce

File tree

167 files changed

+9386
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

167 files changed

+9386
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE html>
2+
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta name="generator" content="pandoc" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
7+
<title>README</title>
8+
<style type="text/css">
9+
code{white-space: pre-wrap;}
10+
span.smallcaps{font-variant: small-caps;}
11+
span.underline{text-decoration: underline;}
12+
div.column{display: inline-block; vertical-align: top; width: 50%;}
13+
</style>
14+
</head>
15+
<body>
16+
<pre>
17+
<i>'algorithms'</i> Sub-directory contains all <strong>algorithms</strong>
18+
further separated in different particular sub directories
19+
</pre>
20+
<p>The programs have been <strong>re-checked</strong> and <strong>re-mastered</strong> by me! <br/> Although i’ve tried to keep it as orignal as possible. <br/> Keep Patience It’ll take time to go through every program!! <br/> <strong>Thanks</strong></p>
21+
</body>
22+
</html>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
<pre>
3+
<i>'algorithms'</i> Sub-directory contains all <strong>algorithms</strong>
4+
further separated in different particular sub directories
5+
</pre>
6+
The programs have been <strong>re-checked</strong> and <strong>re-mastered</strong> by me! <br/>
7+
Although i've tried to keep it as orignal as possible. <br/>
8+
Keep Patience It'll take time to go through every program!! <br/>
9+
<strong>Thanks</strong>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!DOCTYPE html>
2+
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta name="generator" content="pandoc" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
7+
<title>README</title>
8+
<style type="text/css">
9+
code{white-space: pre-wrap;}
10+
span.smallcaps{font-variant: small-caps;}
11+
span.underline{text-decoration: underline;}
12+
div.column{display: inline-block; vertical-align: top; width: 50%;}
13+
</style>
14+
</head>
15+
<body>
16+
<pre>
17+
<i>'analysis'</i> Sub-directory contains all
18+
<strong>analysis related algorithms</strong>.
19+
20+
<strong>Thanks</strong>
21+
</pre>
22+
</body>
23+
</html>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
3+
4+
<pre>
5+
<i>'analysis'</i> Sub-directory contains all
6+
<strong>analysis related algorithms</strong>.
7+
8+
<strong>Thanks</strong>
9+
</pre>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
# Simple algorithm to count
5+
# number of occurrences of (n) in (ar)
6+
7+
# Sudo: Algorithm
8+
# each time (n) is found in (ar)
9+
# (count) varible in incremented (by 1)
10+
11+
# I've put spaces to separate different
12+
# stages of algorithms for easy understanding
13+
# however isn't a good practise
14+
15+
def count(ar, n):
16+
count = 0
17+
18+
for element in ar:
19+
# More complex condition could be
20+
# => (not element != n)
21+
if element == n:
22+
count += 1
23+
24+
return count
25+
26+
# Testing
27+
# add your test cases in list below
28+
test_cases = [([1, 1, 2, 3, 5, 8, 13, 21, 1], 1), ("Captain America", "a")]
29+
for test_case in test_cases:
30+
print("TestCase: {}, {}".format(test_case[0], test_case[1]))
31+
print("Results: {}\n".format(count(test_case[0], test_case[1])))
32+
33+
# You can add condition to check weather output is correct
34+
# or not
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
# Enum function
5+
# yields a tuple of element and it's index
6+
def enum(ar):
7+
for index in range(len(ar)):
8+
yield((index, ar[index]))
9+
10+
# Test
11+
case_1 = [19, 17, 20, 23, 27, 15]
12+
for tup in list(enum(case_1)):
13+
print(tup)
14+
15+
16+
# Enum function is a generator does not
17+
# return any value, instead generates
18+
# tuple as it encounters element of array
19+
20+
# Tuples can be appended to list
21+
# and can be returned after iteration
22+
# However,
23+
# Generator is a good option
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
import math
5+
6+
# Length of array(number of elements in array)
7+
# is simple algorithm
8+
# Iterates through array
9+
# at each iteration (count) variable
10+
# is incremented
11+
#
12+
# This solution is ideal for small array
13+
# But not that good for bigger array's
14+
15+
# There are many ways to do same thing
16+
# but always go with solution that is
17+
# simple, and has least instructions
18+
# for computer to execute, making
19+
# process faster
20+
21+
# is_ap parameter indicates weather array
22+
# is an arithmetic progression or not
23+
#
24+
# Similarly,
25+
# is_gp indicates weather array is an geometric progression or not
26+
# If array is either ap or gp then it's length can be
27+
# found by derivation of it's general term formula
28+
# e.g. ap's general term,
29+
# tn = a + (n - 1)d
30+
# thus, n = (tn - a) / d + 1
31+
32+
def length(ar, is_ap = False, is_gp = False, big_data = False, data_outline = []):
33+
# Length of data if it is an arithmetic progression
34+
# using derived formula, n = (tn - a) / d + 1
35+
if is_ap:
36+
return ((ar[-1] - ar[0]) / (ar[1] - ar[0])) + 1
37+
38+
# Length of data if it is an geometric progression
39+
# using derived formula, n = ((log base 10 an / a1) / log 10 r) + 1
40+
elif is_gp:
41+
# length is never a float
42+
return int(math.log10((ar[-1] / ar[0])) / math.log10((ar[1] / ar[0])) + 1)
43+
44+
# Length of big data using data outline
45+
# data outline is selective elements to be counted from entire
46+
# data
47+
elif big_data:
48+
count = 0
49+
for element in data_outline:
50+
count += ar.count(element)
51+
return count
52+
53+
# Sequential counting
54+
# of elements in array
55+
else:
56+
res = 0
57+
for item in ar:
58+
res += 1
59+
return res
60+
61+
62+
# Test
63+
# Geometric progression Test
64+
if length([1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096], None, is_gp = True) == 13:
65+
print("\nLength of geometric progression: " + str(13))
66+
print("--> geometric progression counting works!\n")
67+
else:
68+
print("Something's wrong with gp feature")
69+
70+
# Arithmetic progression test
71+
if length([1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43], is_ap = True) == 15:
72+
print("Length of arithmetic progression: " + str(15))
73+
print("--> arithmetic progression counting works!\n")
74+
else:
75+
print("Something's wrong with ap feature")
76+
77+
# Big data test
78+
# Passing only useful numbers in outline array
79+
if length([1, 1, 4, 5, 7, 1, 9, 5, 2, 4, 3, 5, 9], None, None, True, [1, 4, 5, 7, 9]) == 11:
80+
print("Length of arithmetic progression: " + str(11))
81+
print("--> big data counting works!\n")
82+
else:
83+
print("Something's wrong with ap feature")
84+
85+
# Small data
86+
print("Length: " + str(length([1, 1, 2, 3, 5, 8, 13, 21, 34, 55])))
87+
print("Everything Works!\n")
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
# Finds the maximum number
5+
# in un-sorted data
6+
7+
# Sudo Algo:
8+
# Iterate through data
9+
# each time a number(say k) greater than, previous
10+
# consideration(say p) is found, replace previous
11+
# consideration(p) with that greater number(k)
12+
# By this way,
13+
# At last we get the maximum number from data
14+
15+
def max_(seq):
16+
max_n = seq[0]
17+
for item in seq[1:]:
18+
if item > max_n:
19+
max_n = item
20+
return max_n
21+
22+
23+
# Test
24+
# Add your tests too!
25+
tests = [[9017289, 782367, 736812903, 9367821, 71256716278, 676215, 2398, 0, 1],
26+
[19208, 9239, 4376, 738, 78, 51, 5, 6, 12, 78, 123, 65765, 1999999999],
27+
[1, 2, 4, 7, 9]]
28+
29+
# checking our functions results
30+
# with python's built-in max() function
31+
for test_i in range(len(tests)):
32+
m = max_(tests[test_i])
33+
if m == max(tests[test_i]):
34+
print("Max number in array({}) -> ".format(test_i + 1) + str(m))
35+
else:
36+
print("Oops! Someting went wrong!")
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
# f(x) = s(x) / l(x) ______(e1)
5+
# Functions below same principle as (e1)
6+
#
7+
# Here,
8+
# Mean value can be float
9+
# 'er is need to declare float values
10+
11+
# Function to get mean of given args
12+
def mean_(*args):
13+
sum_ = 0.0
14+
length_ = 0.0
15+
16+
for arg in args:
17+
sum_ += arg
18+
length_ += 1.0
19+
20+
return sum_ / length_
21+
22+
# Function to get mean of array
23+
def mean_ar(ar):
24+
return float(sum(ar))/float(len(ar))
25+
26+
# Another feature can be start index
27+
# and end index of array
28+
29+
# Test
30+
# First function
31+
if mean_(12, 445, 76, 23, 7, 9, 17, 19, 100) == 78.66666666666667:
32+
print("First Function Works!")
33+
34+
# Second function
35+
if mean_ar([12, 445, 76, 23, 7, 9, 17, 19, 10]) == 68.66666666666667:
36+
print("Second Function Works!")
37+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
# Median
5+
# Median is the middle value of data
6+
# Data of odd length has a mid value
7+
# but, data of even length has 2 mid values
8+
# so their median is mean of these 2 values
9+
10+
# ranked parameter is to tell function
11+
# weather data is ranked(sorted) or not
12+
#
13+
def median(ar, ranked = False):
14+
if not ranked:
15+
data = sorted(ar[:])
16+
else:
17+
# Don't need an else block still
18+
# but to map program properly
19+
# i've added it
20+
data = ar[:]
21+
22+
# Data with odd length
23+
if len(data) % 2 != 0:
24+
25+
# f(x) = (l(x) + 1) / 2 th term is the median of data
26+
# but since computer starts counting from 0
27+
# and not from 1, there is no need to add 1
28+
# to length of data, otherwise results are
29+
# not accurate
30+
return data[len(data) / 2]
31+
32+
# Data with even length
33+
# f(x) = [l(x) / 2 th term + (l(x) + 2) / 2th term] / 2
34+
# 2.0 is to declare that median can be a float
35+
# in case of even length data
36+
return (data[len(data) / 2 - 1] + data[(len(data) + 1) / 2]) / 2.0
37+
38+
# Test
39+
odd = [123, 456, 789, 101112, 131415, 161718, 192021, 222324, 252627]
40+
even = [8, 7, 5, 2, 1, 3, 4, 6]
41+
42+
if median(odd, ranked = True) == 131415 and median(even) == 4.5:
43+
44+
# Print statements on separate lines look better
45+
print("Median of odd data: " + str(131415))
46+
print("Median of even data: " + str(4.5))
47+
print("Yeah, it works!")
48+
49+
else:
50+
# If algo didn't work
51+
print("There's something wrong!")
52+
53+
# This median is for un-distributed/un-grouped data
54+
# i.e. no frequencies
55+
# plain numbers in an array

0 commit comments

Comments
 (0)