import numpy as np
def pagerank(M, num_iterations=100, d=0.85):
N = M.shape[1]
v = np.random.rand(N, 1)
v = v / np.linalg.norm(v, 1)
iteration = 0
while iteration < num_iterations:
iteration += 1
v = d * np.matmul(M, v) + (1 - d) / N
return v
This method gets vowels (‘a’, ‘e’, ‘i’, ‘o’, ‘u’) found in a string.
#make a function:
def get_vowels(string):
#return is the keyword which means function have to return value:
return [each for each in string if each in 'aeiou']
#assign the words and function will return vowels words.
get_vowels('foobar') # ['o', 'o', 'a']
get_vowels('gym') # []
keys, values)) # {'a': 2, 'c': 4, 'b': 3}
#make a function: def is the keyword for the function:
def to_dictionary(keys, values):
#return is the keyword that tells program that function has to return value
return dict(zip(keys, values))
# keys and values are the lists:
keys = ["a", "b", "c"]
values = [2, 3, 4]
# Python3 implementation of the approach
# Function to sort the array such that
# negative values do not get affected
def sortArray(a, n):
# Store all non-negative values
ans=[]
for i in range(n):
if (a[i] >= 0):
ans.append(a[i])
# Sort non-negative values
ans = sorted(ans)
j = 0
for i in range(n):
# If current element is non-negative then
# update it such that all the
# non-negative values are sorted
if (a[i] >= 0):
a[i] = ans[j]
j += 1
# Print the sorted array
for i in range(n):
print(a[i],end = " ")
# Driver code
arr = [2, -6, -3, 8, 4, 1]
n = len(arr)
sortArray(arr, n)
#assign a value to a variable:
types_of_people = 10
# make a string using variable name:
X = f “there are {types_of_people} types of people.”
Output:
There are 10 types of people
Comments