Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 059516d

Browse files
committed
Python
Algo and data structs in python
1 parent f8a7838 commit 059516d

File tree

6 files changed

+98
-0
lines changed

6 files changed

+98
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def is_multiple(n,m):
2+
i=2
3+
if n == m*i:
4+
return True
5+
else:
6+
return False
7+
8+
print(is_multiple(3,2))
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# def is_even(k):
2+
# if (k % 2) == 0:
3+
# return True
4+
# else:
5+
# return False
6+
7+
# k=int(input("What number you want to check? "))
8+
# print(f"Is {k} an even number")
9+
# answer= is_even(k)
10+
# print(answer)
11+
12+
"""Without using mathematical operators"""
13+
def isEven(k):
14+
evenList=[]
15+
for num in range(2,1000,2):
16+
evenList.append(num)
17+
18+
if k in evenList:
19+
return True
20+
else:
21+
return False
22+
23+
print(isEven(6))
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""This algorithm runs at O(n)"""
2+
def minmax(data):
3+
min=data[0]
4+
5+
for num in range(1,len(data)):
6+
if data[num] <= min:
7+
min=data[num]
8+
9+
max= data[0]
10+
for num in range(1, len(data)):
11+
if data[num] >= max:
12+
max=data[num]
13+
14+
return min, max
15+
16+
data=[2,3,1,6,5,8,4,9]
17+
print(minmax(data))
18+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
def sumOfSquares(k):
2+
num=0
3+
sum=0
4+
list=[]
5+
while num < k:
6+
list.append(num)
7+
sum+=pow(num,2)
8+
num=num+1
9+
10+
print(list)
11+
12+
return sum
13+
14+
print(sumOfSquares(5))
15+
16+
def sum_of_sq(n):
17+
18+
list_tot=[]
19+
for i in range(1,n):
20+
list_tot.append(i*i)
21+
22+
print(list_tot)
23+
return sum(list_tot)
24+
25+
print(sum_of_sq(5))
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def listOfsquares(k):
2+
list=[]
3+
4+
for num in range(k):
5+
list.append(pow(num,2))
6+
7+
print(list)
8+
print(sum(list))
9+
10+
listOfsquares(5)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Set and frozeset classes in python
2+
3+
-> set class represents the mathematical notion of a set, namely a collection of elements without duplicates, and without an inherent order to those elements.
4+
5+
-> Major advantage of a set vs a list is that it has highly optimized method for checking whether a specific element is contained in the set.
6+
This based on the data structure names Hash Table.
7+
8+
-> Major disadvantages:
9+
1. set does not maintain the elements in any particular order.
10+
2. only instances of immutable types can be added to a python set.
11+
12+
-> It is possible to maintain a set of tuples, but not a set of lists or a set of sets because lists and sets are mutable and tuples are not.
13+
14+
-> Frozen set class is an immutable form of the set type, so it is legal to have a set of frozen sets

0 commit comments

Comments
 (0)