Skip to content

Commit a9eb8ec

Browse files
committed
Python-Script-sample
1 parent 56e46e6 commit a9eb8ec

File tree

275 files changed

+307667
-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.

275 files changed

+307667
-0
lines changed

License

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2016 The Algorithms
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Maths/3n+1.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
def main():
2+
def n31(a):# a = initial number
3+
c = 0
4+
l = [a]
5+
while a != 1:
6+
if a % 2 == 0:#if even divide it by 2
7+
a = a // 2
8+
elif a % 2 == 1:#if odd 3n+1
9+
a = 3*a +1
10+
c += 1#counter
11+
l += [a]
12+
13+
return l , c
14+
print(n31(43))
15+
print(n31(98)[0][-1])# = a
16+
print("It took {0} steps.".format(n31(13)[1]))#optional finish
17+
18+
if __name__ == '__main__':
19+
main()

Maths/FindMax.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# NguyenU
2+
3+
def find_max(nums):
4+
max = nums[0]
5+
for x in nums:
6+
if x > max:
7+
max = x
8+
print(max)
9+
10+
def main():
11+
find_max([2, 4, 9, 7, 19, 94, 5])
12+
13+
if __name__ == '__main__':
14+
main()

Maths/FindMin.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def main():
2+
def findMin(x):
3+
minNum = x[0]
4+
for i in x:
5+
if minNum > i:
6+
minNum = i
7+
return minNum
8+
9+
print(findMin([0,1,2,3,4,5,-3,24,-56])) # = -56
10+
11+
if __name__ == '__main__':
12+
main()

Maths/abs.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def absVal(num):
2+
"""
3+
Function to fins absolute value of numbers.
4+
>>absVal(-5)
5+
5
6+
>>absVal(0)
7+
0
8+
"""
9+
if num < 0:
10+
return -num
11+
else:
12+
return num
13+
14+
def main():
15+
print(absVal(-34)) # = 34
16+
17+
if __name__ == '__main__':
18+
main()

Maths/absMax.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from Maths.abs import absVal
2+
3+
def absMax(x):
4+
"""
5+
#>>>absMax([0,5,1,11])
6+
11
7+
>>absMax([3,-10,-2])
8+
-10
9+
"""
10+
j =x[0]
11+
for i in x:
12+
if absVal(i) > absVal(j):
13+
j = i
14+
return j
15+
#BUG: i is apparently a list, TypeError: '<' not supported between instances of 'list' and 'int' in absVal
16+
#BUG fix
17+
18+
def main():
19+
a = [-13, 2, -11, -12]
20+
print(absMax(a)) # = -13
21+
22+
if __name__ == '__main__':
23+
main()
24+
25+
"""
26+
print abs Max
27+
"""

Maths/absMin.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from Maths.abs import absVal
2+
def absMin(x):
3+
"""
4+
# >>>absMin([0,5,1,11])
5+
0
6+
# >>absMin([3,-10,-2])
7+
-2
8+
"""
9+
j = x[0]
10+
for i in x:
11+
if absVal(i) < absVal(j):
12+
j = i
13+
return j
14+
15+
def main():
16+
a = [-3,-1,2,-11]
17+
print(absMin(a)) # = -1
18+
19+
if __name__ == '__main__':
20+
main()

Maths/find_hcf.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Program to find the HCF of two Numbers
2+
def find_hcf(num_1, num_2):
3+
if num_1 == 0:
4+
return num_2
5+
if num_2 == 0:
6+
return num_1
7+
# Base Case
8+
if num_1 == num_2:
9+
return num_1
10+
if num_1 > num_2:
11+
return find_hcf(num_1 - num_2, num_2)
12+
return find_hcf(num_1, num_2 - num_1)
13+
14+
15+
def main():
16+
num_1 = 24
17+
num_2 = 34
18+
print('HCF of %s and %s is %s:' % (num_1, num_2, find_hcf(num_1, num_2)))
19+
20+
21+
if __name__ == '__main__':
22+
main()

Maths/find_lcm.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def find_lcm(num_1, num_2):
2+
max = num_1 if num_1 > num_2 else num_2
3+
while (True):
4+
if ((max % num_1 == 0) and (max % num_2 == 0)):
5+
break
6+
max += 1
7+
return max
8+
9+
10+
def main():
11+
num_1 = 12
12+
num_2 = 76
13+
print(find_lcm(num_1, num_2))
14+
15+
16+
if __name__ == '__main__':
17+
main()

0 commit comments

Comments
 (0)