Challenge¶
Challenge 1¶
Write a function to count the number of non-vowel in a string?
Respond:
Challenge 2¶
pi can be estimated using using infinite series. Write a function to estimate it for a given en error.
Challenge 3¶
Write a function to calculate the Euclidean distance
Respond:
Exercise: distance between two rows
def dis_col(ma,j1,j2):
n=mat.shape
d=0
for i in range(n[0]):
d+=(mat[i,j1]-mat[i,j2])**2
return d**.5
def dis_euc(mat):
n=mat.shape
di=np.empty((n[1],n[1]))
for i in range(n[1]):
for j in range(n[1]):
if i==j:
di[i,j]=0
elif i<j:
di[i,j]=dis_col(mat,i,j)
else:
di[i,j]=di[j,i]
return di
dis_euc(mat)
Challenge 4¶
Write a function select a random number between 0 and 100, then you give you hints to find it.
Challenge 5¶
Write an iterate function on dictionary.
Respond:
Challenge 6¶
Write a script to find the number of duplication of items.
Respond:
Challenge 7¶
Write factorial function
Respond:
Challenge 8¶
Write a function return the Fibonacci number
Respond:
def fib(n):
if n==1:
return 1
elif n==2:
return 1
else:
return fib(n-1)+fib(n-2)
fib(4)
def fib1(n):
if n==0 or n==1:
% if n in [0,1]:
return 1
else:
return fib1(n-1)+fib1(n-2)
print([fib1(i) for i in range(10)])
def fib2(i):
a, b=0, 1
for n in range(1,i+1):
a,b=b, a+b
return b
print([fib2(n) for n in range(10)])
def fib_helper(n):
if n < 2:
return n
return fib_helper(n - 1) + fib_helper(n - 2)
def fib(n):
return fib_helper(n)
Challenge 9¶
Write a function get a string and a letter, and find the position of letter
Respond:
Challenge 10¶
The following is a list generated using for, rewrite it using a list comprehension
Respond:
Challenge 11¶
Write a code to display the indices of elements in a list that satisfy a given criterion.
Challenge 12¶
Explain what the following code does, then simplifies as a list comprehension.
Respond:
Challenge 13¶
Write a comprehension, get a list and drop 1st, and 3th obs.
Challenge 14¶
Write a comprehension that select number that are divisible by 3 and 5 between 1 and 100
Challenge 15¶
Write a function find unique number. Hint: first sort it, then compare pairwisely and drop the duplicate.
Respond:
Challenge 16¶
Using map and filter, write a function find cube of odd number between 1 and 20.
Challenge 17¶
Drop the duplication from a list With a given list without changing the original order.