Skip to content

Commit 6322c4d

Browse files
author
Kendrick Ledet
committed
..
2 parents e8d7be2 + 46d65a6 commit 6322c4d

25 files changed

Lines changed: 238 additions & 1 deletion

File tree

10_Harshad_Number/tags

Whitespace-only changes.

Ackermann/Go/jcla1/ackermann.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package ackermann
2+
3+
func Compute(m, n int) int {
4+
if m == 0 {
5+
return n + 1
6+
} else if m > 0 && n == 0 {
7+
return Compute(m-1, 1)
8+
}
9+
10+
return Compute(m-1, Compute(m, n-1))
11+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-- The Ackermann function is so easy to define in Haskell.
2+
-- Just write down the definition an you've already almost got working code.
3+
ackermann 0 n = n+1
4+
ackermann m 0 = ackermann (m-1) 1
5+
ackermann m n = ackermann (m-1) (ackermann m (n-1))
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def ackermann(m, n):
2+
if m == 0:
3+
return n+1
4+
elif m > 0 and n == 0:
5+
return ackermann(m-1, 1)
6+
7+
return ackermann(m-1, ackermann(m, n-1))
8+
9+
if __name__ == '__main__':
10+
print ackermann(3, 5)

Ackermann/tags

Whitespace-only changes.

Average/Python/wasi0013/Average.py

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
from math import log
2+
3+
"""
4+
-- Various average (means) algorithms implementation
5+
-- See: http://en.wikipedia.org/wiki/Average
6+
-- Returns the sum of a sequence of values
7+
8+
"""
9+
10+
#Calculates the arithmetic mean of the list numbers and returns the result
11+
12+
def arithmetic_mean(numbers): return float(sum(numbers))/len(numbers)
13+
14+
15+
#Calculates the geometric mean of the list numbers
16+
def geometric_mean(numbers):
17+
18+
product=1
19+
20+
for num in numbers:
21+
22+
product*=num
23+
24+
return pow(product,1.0/len(numbers))
25+
26+
#Calculates the harmonic mean of the list numbers
27+
28+
def harmonic_mean(numbers):
29+
30+
return float(len(numbers))/sum([1.0/num for num in numbers])
31+
32+
#Calculates the quadratic mean of the list numbers
33+
34+
def quadratic_mean(numbers):
35+
36+
return ((1.0/len(numbers))*sum([num*num for num in numbers]))**.5
37+
38+
# Calculates the generalized mean of the list numbers according to the
39+
# given power
40+
41+
def generalized_mean(numbers,power):
42+
43+
return (( 1.0/len(numbers))*sum([num**power for num in numbers]))**(1.0/power)
44+
45+
# Calculates weighted sum where weights and values are two list with equal number
46+
# of elements
47+
48+
def weighted_mean(values,weights):
49+
50+
if(len(values)!= len(weights)): print("List lengths don't match")
51+
52+
else:
53+
54+
sumup=0
55+
56+
for counter,number in enumerate(values):
57+
58+
sumup+=number*weights[counter]
59+
60+
return float(sumup)/sum(weights)
61+
62+
# Calculates midrange_mean
63+
64+
def midrange_mean(values):
65+
66+
return 0.5*(min(values)+max(values))
67+
68+
# Calculates midrange_mean
69+
70+
def energatic_mean(values):
71+
72+
sumup=sum([10**(num/10.0) for num in values])
73+
74+
return 10*log((1.0/len(values))*sumup, 10)
75+
76+
77+
def main():
78+
79+
values =[1,2,3,4,5]
80+
81+
weights = [0.1, 0.2, 0.2, 0.3, 0.2]
82+
83+
flag =True
84+
85+
if(arithmetic_mean(values)!=3):
86+
87+
flag = False
88+
89+
elif(geometric_mean(values)!=2.605171084697352):
90+
91+
flag = False
92+
93+
elif(harmonic_mean(values)!=2.18978102189781):
94+
95+
flag = False
96+
97+
elif(quadratic_mean(values)!=3.3166247903554):
98+
99+
flag = False
100+
101+
elif(generalized_mean(values,1)!=arithmetic_mean(values)):
102+
103+
flag = False
104+
105+
elif(weighted_mean(values,weights) != 3.3):
106+
107+
flag = False
108+
109+
elif(midrange_mean(values)!=3):
110+
111+
flag = False
112+
113+
elif(energatic_mean(values)!= 3.2276678141732704):
114+
115+
flag = False
116+
117+
print("Test is"+(" Successful"if(flag) else" Unsuccessful"))
118+
119+
if __name__ == '__main__':
120+
main()

Average/tags

Whitespace-only changes.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import string
2+
3+
alphabet = string.ascii_letters
4+
alphabet_len = len(alphabet)
5+
alphabet_map = dict((v, i) for i, v in enumerate(alphabet))
6+
7+
def caesar_cipher(s, shift_num):
8+
if abs(shift_num) > alphabet_len:
9+
raise RuntimeError('Shift num is bigger than whole alphabet')
10+
11+
res = []
12+
for char in s:
13+
index = alphabet_map[char] + shift_num
14+
if abs(index) >= alphabet_len:
15+
index = abs(index) - alphabet_len
16+
17+
res.append(alphabet[index])
18+
19+
return ''.join(res)
20+
21+
def main():
22+
input_string = raw_input('Enter a string to encrypt using Caesar Cipher\n')
23+
24+
encrypted_string = caesar_cipher(input_string, -3)
25+
print encrypted_string
26+
27+
if __name__ == '__main__':
28+
main()
29+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package catalan
2+
3+
func Direct(n int) int {
4+
return fact(2*n) / (fact(n+1) * fact(n))
5+
}
6+
7+
func Binomial(n int) int {
8+
if n == 0 {
9+
return 1
10+
}
11+
12+
return binomialCoeff(2*n, n) - binomialCoeff(2*n, n-1)
13+
}
14+
15+
func Sum(n int) (sum int) {
16+
for i := 0; i <= n; i++ {
17+
v := binomialCoeff(n, i)
18+
sum += v * v
19+
}
20+
return
21+
}
22+
23+
func binomialCoeff(n, k int) int {
24+
return fact(n) / (fact(n-k) * fact(k))
25+
}
26+
27+
func fact(n int) int {
28+
if n <= 1 {
29+
return 1
30+
}
31+
32+
return n * fact(n-1)
33+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def catalan_direct(n):
2+
return factorial(2 * n) / (factorial(n + 1) * factorial(n))
3+
4+
def catalan_binomial(n):
5+
if n == 0: return 1
6+
return binomial_coefficient(2 * n, n) - binomial_coefficient(2 * n, n - 1)
7+
8+
def catalan_sum(n):
9+
return sum([binomial_coefficient(n, i) ** 2 for i in xrange(n + 1)]) / (n + 1)
10+
11+
def binomial_coefficient(n, k):
12+
return factorial(n) / (factorial(k) * factorial(n - k))
13+
14+
def factorial(n):
15+
return n * factorial(n-1) if n > 1 else 1
16+
17+
if __name__ == '__main__':
18+
# Make sure all implementations give the same results
19+
print(all([catalan_direct(n)
20+
== catalan_binomial(n)
21+
== catalan_sum(n) for n in xrange(100)]))

0 commit comments

Comments
 (0)