Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assignment-2 #76

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
158 changes: 128 additions & 30 deletions Workshop_1/Assignment1.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ def square(x):
"""

# Code Here
return None
y = x*x
return y


def word_is_palindrome(string):
Expand All @@ -31,7 +32,13 @@ def word_is_palindrome(string):
"""

# Code Here
return None
x = len(string)
for i in range(x):
if string[i] == string[x-1-i]:
flag = True
else:
flag = False
return flag


def sqrt_of_numbers(num):
Expand All @@ -48,9 +55,8 @@ def sqrt_of_numbers(num):
"""
if num < 0:
raise ValueError('Number must be positive')

# Code Here
return None
else:
return round(pow(num, 0.5),2)


def Maximum(arr):
Expand All @@ -68,7 +74,8 @@ def Maximum(arr):
"""

# Code Here
return None, None
arr.sort()
return arr[-1], arr[-2]


def even_sort(arr):
Expand All @@ -86,6 +93,14 @@ def even_sort(arr):
"""

# Code Here
arr.sort()
for i in range(len(arr)):
for j in range(len(arr)-1-i):
if arr[j]%2 != 0 and arr[j+1]%2 == 0:
t = arr[j]
arr[j] = arr[j+1]
arr[j+1] = t
return arr
return None


Expand All @@ -105,9 +120,9 @@ def eqn_solver(A, B, C):
returns:
x, y (float, float)
"""

# Code Here
return None, None
y = ((A[0]*C[1] - A[1]*C[0])/(A[0]*B[1] - A[1]*B[0]))
x = ((B[0]*C[1] - B[1]*C[0])/(B[0]*A[1] - B[1]*A[0]))
return x, y


def swap_case(string):
Expand All @@ -124,7 +139,16 @@ def swap_case(string):
"""

# Code Here
return None
a = ""
l = len(string)
for i in range(l):
if string[i].isupper():
a = a + string[i].lower()
elif string[i].islower():
a = a + string[i].upper()
else:
a = a + string[i]
return a


def is_prime(num):
Expand All @@ -137,7 +161,15 @@ def is_prime(num):
"""

# Code Here
return None
flag = True
if num == 1:
flag = False
elif num > 1:
for i in range(2, num):
if (num % i) == 0:
flag = False
break
return flag


def is_leap_year(year):
Expand All @@ -150,7 +182,10 @@ def is_leap_year(year):
"""

# Code Here
return None
flag = False
if (year%4 == 0 and year%100 != 0) or year%400 == 0:
flag = True
return flag


def is_perfect_square(num):
Expand All @@ -163,7 +198,12 @@ def is_perfect_square(num):
"""

# Code Here
return None
flag = False
if int(pow(num, 0.5)) - pow(num, 0.5) == 0:
flag = True

# Code Here
return flag


def is_perfect_number(num):
Expand All @@ -179,14 +219,20 @@ def is_perfect_number(num):
num = 6
## then
flag = True

num = 7
## then
flag = False
"""

# Code Here
return None
flag = False
a = []
for i in range(1,num):
if num%i == 0:
a.append(i)
if sum(a) == num:
flag = True
return flag


def resize_array(a):
Expand All @@ -203,7 +249,9 @@ def resize_array(a):
"""

# Code Here
return None
import numpy as np
b = np.array(a)
return b.reshape(2,3)


def reverse_step_array(a):
Expand All @@ -220,7 +268,7 @@ def reverse_step_array(a):
"""

# Code Here
return None
return a[::-3]


def reverse_words(string):
Expand All @@ -237,7 +285,14 @@ def reverse_words(string):
"""

# Code Here
return None
a =[]
s = ""
a = string.split(' ')
for i in range(len(a)):
s = s + " " + a[-1]
a.pop(-1)

return s.strip()


def count_characters(string):
Expand All @@ -253,8 +308,16 @@ def count_characters(string):
dict = {'H': 1, 'e': 1, 'l': 3, 'o': 2, 'W': 1, 'r': 1, 'd': 1}
"""

# Code Here
return None
all_freq = {}
for i in string:
if i in all_freq:
all_freq[i] += 1
else:
all_freq[i] = 1
if ' ' in all_freq.keys():
del all_freq[' ']

return all_freq


def remove_special_characters(string):
Expand All @@ -270,8 +333,13 @@ def remove_special_characters(string):
str = 'Hello World 123 th15 1s 4 t35t str1ng'
"""

# Code Here
return None
sample = []
for i in range(len(string)):
if string[i].isupper() or string[i].islower() or string[i].isdigit() or string[i] == ' ':
sample.append(string[i])
ns = "".join(sample)

return ns


def sort_tuple_of_tuples(input_tuple):
Expand All @@ -287,8 +355,18 @@ def sort_tuple_of_tuples(input_tuple):
input_tuple: (('z', 1), ('w', 19), ('f', 37), ('a', 55))
"""

# Code Here
return None
l = []
for i in range(len(input_tuple)):
l.append(input_tuple[i])

for i in range(len(input_tuple)-1):
for j in range(0,len(input_tuple) - i -1):
if l[j][1] > l[j+1][1]:
t = l[j]
l[j] = l[j+1]
l[j+1] = t

return tuple(l)


def alpha_numeric_words(string):
Expand All @@ -304,8 +382,13 @@ def alpha_numeric_words(string):
string: "there33 how11 you1"
"""

# Code Here
return None
res = []
temp = string.split()
for idx in temp:
if any(chr.isalpha() for chr in idx) and any(chr.isdigit() for chr in idx):
res.append(idx)
return ' '.join(res)



def count_them_all(string):
Expand All @@ -321,8 +404,18 @@ def count_them_all(string):
dict: {'Characters': 7, 'Numbers': 4, 'Symbols': 4}
"""

# Code Here
return None
d = 0
c = 0
s = 0
for i in range(len(string)):
if string[i].isdigit():
d = d+1
elif string[i].isupper() or string[i].islower():
c = c+1
elif string[i] != ' ':
s = s+1
r = {'Characters': c, 'Numbers': d, 'Symbols': s}
return r


def hash_supremacy(string):
Expand All @@ -338,5 +431,10 @@ def hash_supremacy(string):
string: "#He was a### great #guy"
"""

# Code Here
return None
a =""
for i in range(len(string)):
if string[i].isdigit() or string[i].isupper() or string[i].islower() or string[i] == ' ':
a = a + string[i]
else:
a = a + "#"
return a
21 changes: 17 additions & 4 deletions Workshop_2/WS2_KMeans_practice.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,11 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"# UNQ_C1\n",
Expand All @@ -162,7 +166,12 @@
" idx = np.zeros(X.shape[0], dtype=int)\n",
"\n",
" ### START CODE HERE ###\n",
"\n",
" for i in range(X.shape[0],dtype=int):\n",
" distance=[]\n",
" for j in range(X.shape[0]):\n",
" norm_ij=np.linalg.norm(X[i]-centroids[j])\n",
" distance.append(norm_ij)\n",
" idx[i]=np.argmin(distance)\n",
" ### END CODE HERE ###\n",
" \n",
" return idx"
Expand Down Expand Up @@ -378,7 +387,9 @@
" centroids = np.zeros((K, n))\n",
" \n",
" ### START CODE HERE ###\n",
" \n",
" for k in range(K):\n",
" points =X[idx == k]\n",
" centoids[k]=np.mean(points,axis=0)\n",
" ### END CODE HERE ## \n",
" \n",
" return centroids"
Expand Down Expand Up @@ -837,7 +848,9 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"source": [
"# link for supervised learning Assignment => https://colab.research.google.com/drive/1uExrZ0sfGXeEjix-vFTR9vscC6o58MN3#scrollTo=H3ND5JcwCo0p"
]
}
],
"metadata": {
Expand Down