Initial commit

This commit is contained in:
Michael Reber
2019-11-15 12:59:38 +01:00
parent 40a414d210
commit b880c3ccde
6814 changed files with 379441 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
def string_test(s):
d={"UPPER_CASE":0, "LOWER_CASE":0}
for c in s:
if c.isupper():
d["UPPER_CASE"]+=1
elif c.islower():
d["LOWER_CASE"]+=1
else:
pass
print ("Original String : ", s)
print ("No. of Upper case characters : ", d["UPPER_CASE"])
print ("No. of Lower case Characters : ", d["LOWER_CASE"])
string_test('The quick Brow Fox')

View File

@@ -0,0 +1,11 @@
def isPalindrome(string):
left_pos = 0
right_pos = len(string) - 1
while right_pos >= left_pos:
if not string[left_pos] == string[right_pos]:
return False
left_pos += 1
right_pos -= 1
return True
print(isPalindrome('aza'))

View File

@@ -0,0 +1,8 @@
def unique_list(l):
x = []
for a in l:
if a not in x:
x.append(a)
return x
print(unique_list([1,2,3,3,3,3,4,5]))

View File

@@ -0,0 +1,11 @@
def test_prime(n):
if (n==1):
return False
elif (n==2):
return True;
else:
for x in range(2,n):
if(n % x==0):
return False
return True
print(test_prime(9))

View File

@@ -0,0 +1,8 @@
def pascal_triangle(n):
trow = [1]
y = [0]
for x in range(max(n,0)):
print(trow)
trow=[l+r for l,r in zip(trow+y, y+trow)]
return n>=1
pascal_triangle(6)

View File

@@ -0,0 +1,7 @@
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
n=int(input("Input a number to compute the factiorial : "))
print(factorial(n))

View File

@@ -0,0 +1,6 @@
def test_range(n):
if n in range(3,9):
print( " %s is in the range"%str(n))
else :
print("The number is outside the given range.")
test_range(5)

View File

@@ -0,0 +1,7 @@
def perfect_number(n):
sum = 0
for x in range(1, n):
if n % x == 0:
sum += x
return sum == n
print(perfect_number(6))

View File

@@ -0,0 +1,6 @@
import string, sys
def ispangram(str1, alphabet=string.ascii_lowercase):
alphaset = set(alphabet)
return alphaset <= set(str1.lower())
print ( ispangram('The quick brown fox jumps over the lazy dog'))

View File

@@ -0,0 +1,7 @@
def printValues():
l = list()
for i in range(1,21):
l.append(i**2)
print(l)
printValues()

View File

@@ -0,0 +1,7 @@
def max_of_two( x, y ):
if x > y:
return x
return y
def max_of_three( x, y, z ):
return max_of_two( x, max_of_two( y, z ) )
print(max_of_three(3, 6, -5))

View File

@@ -0,0 +1,6 @@
def multiply(numbers):
total = 1
for x in numbers:
total *= x
return total
print(multiply((8, 2, 3, -1, 7)))

View File

@@ -0,0 +1,6 @@
def sum(numbers):
total = 0
for x in numbers:
total += x
return total
print(sum((8, 2, 3, 0, 7)))

View File

@@ -0,0 +1,10 @@
def recursive_list_sum(data_list):
total = 0
for element in data_list:
if type(element) == type([]):
total = total + recursive_list_sum(element)
else:
total = total + element
return total
print( recursive_list_sum([1, 2, [3,4],[5,6]]))

View File

@@ -0,0 +1,8 @@
def geometric_sum(n):
if n < 0:
return 0
else:
return 1 / (pow(2, n)) + geometric_sum(n - 1)
print(geometric_sum(7))
print(geometric_sum(4))

View File

@@ -0,0 +1,8 @@
def harmonic_sum(n):
if n < 2:
return 1
else:
return 1 / n + (harmonic_sum(n - 1))
print(harmonic_sum(7))
print(harmonic_sum(4))

View File

@@ -0,0 +1,7 @@
def list_sum(num_List):
if len(num_List) == 1:
return num_List[0]
else:
return num_List[0] + list_sum(num_List[1:])
print(list_sum([2, 4, 5, 6, 7]))

View File

@@ -0,0 +1,10 @@
# Python program to calculate the sum of the positive integers of n+(n-2)+(n-4)... (until n-x =< 0)
def sum_series(n):
if n < 1:
return 0
else:
return n + sum_series(n - 2)
print(sum_series(6))
print(sum_series(10))

View File

@@ -0,0 +1,11 @@
def power(a,b):
if b==0:
return 1
elif a==0:
return 0
elif b==1:
return a
else:
return a*power(a,b-1)
print(power(3,4))

View File

@@ -0,0 +1,8 @@
def to_string(n,base):
conver_tString = "0123456789ABCDEF"
if n < base:
return conver_tString[n]
else:
return to_string(n//base,base) + conver_tString[n % base]
print(to_string(2835,16))

View File

@@ -0,0 +1,9 @@
mycode = 'print("hello world")'
code = """
def mutiply(x,y):
return x*y
print('Multiply of 2 and 3 is: ',mutiply(2,3))
"""
exec(mycode)
exec(code)

View File

@@ -0,0 +1,20 @@
def make_bold(fn):
def wrapped():
return "<b>" + fn() + "</b>"
return wrapped
def make_italic(fn):
def wrapped():
return "<i>" + fn() + "</i>"
return wrapped
def make_underline(fn):
def wrapped():
return "<u>" + fn() + "</u>"
return wrapped
@make_bold
@make_italic
@make_underline
def hello():
return "hello world"
print(hello()) ## returns "<b><i><u>hello world</u></i></b>"

View File

@@ -0,0 +1,7 @@
def is_even_num(l):
enum = []
for n in l:
if n % 2 == 0:
enum.append(n)
return enum
print(is_even_num([1, 2, 3, 4, 5, 6, 7, 8, 9]))

View File

@@ -0,0 +1,9 @@
def string_reverse(str1):
rstr1 = ''
index = len(str1)
while index > 0:
rstr1 += str1[ index - 1 ]
index = index - 1
return rstr1
print(string_reverse('1234abcd'))