# The Joy of Computing using Python | Week 9

Session: JAN-APR 2024

Course Name: The Joy of Computing using Python

#### Q1. What is stylometry?The study of language evolutionThe study of linguistic style in written textsThe study of speech patterns in public speakingThe study of language acquisition in children

Answer: The study of linguistic style in written texts

Q2. What are the different attributes that can be checked in addition to average word length to identify the style of a particular author? (MSQ)
Punctuation usage
Word Frequency
Usage of Stylistic Devices like Metaphors, Similes, etc
Average Sentence Length

These are the Joy of Computing using Python Assignment 9 Answers

Q3. The following code tries to perform natural language analysis for a small piece of text without using the nltk (Natural Language Toolkit) library.
Select all the correct statements (MSQ)

y represents the sum of length of words in text
z represents the average word length of text
x represents a list where each index of the list corresponds to the length of the corresponding word in text
The output of the code is: 36 7.2

Q4. The following code tries to calculate average sentence length of a given block of text in the form of a file, assuming that the sentences always end with full stops and that there are no ellipsis(…) in the block of text. But the code might contain errors. Identify the line number corresponding to the error. In the absence of an error input your answer as -1(NAT)

These are the Joy of Computing using Python Assignment 9 Answers

Q5. What does the following code do?

A graph having 5 nodes which are integers from 1 to 5 and 10 edges in total is created and the edges and nodes of the graph are printed
A graph having 6 nodes which are integers from 1 to 6 and 10 edges in total is created and the edges and nodes of the graph are printed
A graph having 5 nodes which are integers from 1 to 5 and 9 edges in total is created and the edges and nodes of the graph are printed
A graph having 6 nodes which are integers from 1 to 6 and 9 edges in total is created and the edges and nodes of the graph are printed

Answer: a) A graph having 5 nodes which are integers from 1 to 5 and 10 edges in total is created and the edges and nodes of the graph are printed

Q6. You are analysing a social network where individuals are represented as nodes, and friendships are represented as edges. What would be the most appropriate graph property to study to identify influential individuals in the network?
Node degree
Graph density
Graph diameter
Edge weight

These are the Joy of Computing using Python Assignment 9 Answers

Q7. In the “small world experiment” conducted by Stanley Milgram, participants were asked to send a letter to a target person through acquaintances. What was the average number of intermediaries required for the letters to reach the target?
1 degree
3 degrees
6 degrees
10 degrees

Q8. Given a file with edges of the graph, and each line representing an edge in the format “node 1 node 2”, the following code attempts to plot a graph based on the data given. But the code might contain errors. Identify the line number corresponding to the error. In the absence of an error type your answer as -1.

These are the Joy of Computing using Python Assignment 9 Answers

Q9. Using the ideas from Area Calculation discussed in the lecture, we are trying to estimate the area of a quarter circle embedded in a square. Identify the line number corresponding to the error(if any) in the absence of error type your answer as -1

Q10. Select the correct statements regarding area calculation using the method discussed in the lecture (MSQ)
The main advantage of the method is that it can handle area calculation even for irregular shapes
The main advantage of the method is that it is computationally efficient for all scenarios
Increasing the number of random points reduces the impact of random variations and provides a more stable and accurate estimate of area
Increasing the number of random points improves accuracy of estimation as it converges the average value to the true value according to law of large numbers

Q11. Select the correct statement regarding the PIL library
PIL stands for Python Image Library
Pillow is the active successor of PIL
Image.open() is used to open images in PIL
All of the above

These are the Joy of Computing using Python Assignment 9 Answers

### The Joy of Computing using Python Programming Assignment

Question 1

word is a string that contains one or more parentheses of the following types: “{ }”, “[ ]”, “( )”. The string is said to be balanced if all the following conditions are satisfied.
When read from left to right:
(1) The number of opening parentheses of a given type is equal to the number of closing parentheses of the same type.
(2) An opening parenthesis cannot be immediately followed by a closing parenthesis of a different type.
(3) Every opening parenthesis should be eventually closed by a closing parenthesis of the same type.

Solution:

``````def balanced(word):
stack = list()
opening = "([{"
closing = ")]}"
mapping = {')': '(', ']': '[', '}': '{'}

for char in word:
if char in opening:
stack.append(char)
elif char in closing:
if not stack or stack.pop() != mapping[char]:
return(not True)

return(not stack)``````

These are the Joy of Computing using Python Assignment 9 Answers

Question 2

Consider the problem about balanced expressions discussed in 1. Programming Assignment | Week 9. We have a balanced expression (string) that has only the flower brackets: ‘( )’. We can recursively define a concept called nesting depth for each pair of opening and closing brackets.
The nesting depth of a pair that lies within another pair is one more than the nesting depth of the pair that immediately englobes it. For a pair that is not surrounded by any other pair, the nesting depth is 1.

Solution:

``````def depth(expr):
max_deep = 0
current_deep= 0
for char in expr:
if char == '(':
current_deep += 1
max_deep = max(max_deep, current_deep)
elif char == ')':
current_deep -= 1
return(max_deep)``````

These are the Joy of Computing using Python Assignment 9 Answers

Question 3

Write a recursive function named power that accepts a square matrix A and a positive integer m as arguments and returns Am.

Solution:

``````def power(A, m):
if m == 0:
ap = len(A)
return [[1 if i == j else 0 for j in range(n)] for i in range(ap)]
elif m == 1:
return A
else:
G = power(A, m // 2)
if m % 2 == 0:
return matrix_multiply(G, G)
else:
return matrix_multiply(matrix_multiply(G, G), A)

def matrix_multiply(A, B):
rows_A = len(A)
cols_A = len(A[0])
cols_B = len(B[0])

result = [[0] * cols_B for i in range(rows_A)]

for i in range(rows_A):
for j in range(cols_B):
for k in range(cols_A):
result[i][j] += A[i][k] * B[k][j]

return result``````

These are the Joy of Computing using Python Assignment 9 Answers

Session: JULY-DEC 2023

Course Name: The Joy of Computing using Python

#### These are answers of The Joy of Computing using Python Assignment 9 Answers

Q1. How can we identify which book is written by which author?
By matching handwriting.
By analyzing word length with previous books.
By analyzing the number of pages in a book.
By analyzing the book’s preface.

Answer: By analyzing word length with previous books.

Q2. How can a list L be transformed into a tuple?
tuple(L)
tup(L)
L(tuple)
L(tup)

Q3. Will the following piece of code always return True?
True
False

These are answers of The Joy of Computing using Python Assignment 9 Answers

Q4. What is the output of the following code?

Q5. How many edges are there in the following graph?
4
5
3
2

Q6. How many neighbors does node 3 have?
2
4
1
3

These are answers of The Joy of Computing using Python Assignment 9 Answers

Q7. In which of the following ways can we create a string in python?
By using single quotes.
By using double-quotes.
By using triple-quotes.
All of the above.

Q8. Which of the following is not true about Stylometry Analysis?
It is quantitative study of literature style
It is based on the observation that the authors tend to write in relatively consistent and recognisable ways
any two people may have same vocabulary
It is a tool to study variety of questions involving style of writing

Answer: any two people may have same vocabulary

These are answers of The Joy of Computing using Python Assignment 9 Answers

Q9. A complete graph will have __ degree of separation
1
2
3
Depends on the number of nodes.

These are answers of The Joy of Computing using Python Assignment 9 Answers

Q10. Networkx in pythons is used for
Making networks
Analyzing networks
Visualizing networks
All of the above

These are answers of The Joy of Computing using Python Assignment 9 Answers

### The Joy of Computing using Python Programming Assignmnet

Question 1
Given two string s1 and s2, write a function subStr which accepts two parameters s1 and s2 and will return True if a s2 is a substring of s1 otherwise return False. A substring is a is a contiguous sequence of characters within a string.
Input:
bananamania
nana
Output:
True
Explanation:
S2 which is nana is in bananamania hence it is a substring of s1.

Solution:

``````def subStr(s1,s2):
return(s2 in s1)``````

These are answers of The Joy of Computing using Python Assignment 9 Answers

Question 2
Given two dictionaries d1 and d2, write a function mergeDic that accepts two dictionaries d1 and d2 and return a new dictionary by merging d1 and d2.
Note: Contents of d1 should be appear before contents of d2 in the new dictionary and in same order. In case of duplicate value retain the value present in d1.
Input:
{1: 1, 2: 2}
{3: 3, 4: 4}
output:
{1: 1, 2: 2, 3: 3, 4: 4}

Solution:

``````def mergeDic(d1,d2):
for kd in d2:
if kd not in d1:
d1[kd]=d2[kd]
return(d1)``````

These are answers of The Joy of Computing using Python Assignment 9 Answers

Question 3
Given an integer n, print all the indexes of numbers in that integer from left to right.
Input:
122345
Output:
1 0
2 1 2
3 3
4 4
5 5
Explanation:
Given integer 122345. Now printing indexes of numbers from left to right.
First number is 1 and its index is 0 therefore
1 0
Second and third number is 2 and its index is 1,2 therefore
2 1 2
and so on…

Solution:

``````N=list(str(n))
P=[]
for isp in N:
if isp not in P:
P.append(isp)
ans=[]
for i in range(len(P)):
a=[]
for j in range(len(N)):
if(P[i]==N[j]):
a.append(j)
ans.append(a)
#print(ans)

for i in range(len(P)):
print(int(P[i]), "",end="")
#print(len(ans[i]))
for j in range(len(ans[i])):
print(int(ans[i][j]),"",end="")
print()``````

These are answers of The Joy of Computing using Python Assignment 9 Answers

Session Jan-Apr 2023

Course Name: The Joy of Computing using Python

#### Q1. How can we identify which book is written by which author?a. By matching handwriting.b. By analyzing word length distribution with previous books.c. By analyzing the number of pages in a book.d. By analyzing the book’s preface.

Answer: b. By analyzing word length distribution with previous books.

Q2. Is it guaranteed that the following code snippet will consistently yield a True result?

a. True
b. False
c. It will return neither True nor False
d. It will thow an error

Q3. What are the different methods available in Python for generating a string?
a. By using single quotes.
b. By using double quotes.
c. By using triple quotes.
d. All of the above

Answer: d. All of the above

These are answers of The Joy of Computing using Python Assignment 9 Answers

Q4. A complete graph will have __ degree of separation
a. 1
b. 2
c. 3
d. Depends on the number of nodes.

Q5. Networkx in pythons is used for
a. Making networks
b. Analyzing networks
c. Visualizing networks
d. Breaking networks

Q6. In the world, on average, how many hops will it take to connect two people?
a. 6
b. 7
c. 8
d. 9
e. 10

These are answers of The Joy of Computing using Python Assignment 9 Answers

Q7. How many neighbors does node 4 have?

a. 1
b. 2
c. 3
d. 4

Q8. Assuming that the length and breadth remain constant, how can we enhance the precision of the calculated area for a state?
a. By increasing the size of the image.
b. By increasing the number of points.
c. By decreasing the size of the image.
d. By decreasing the number of points.

Answer: b. By increasing the number of points.

Q9. Degree of separation is equivalent to
a. Number of nodes in a graph
b. Number of edges in a graph
c. The average length of the shortest path in a graph
d. None of the above

Answer: c. The average length of the shortest path in a graph

These are answers of The Joy of Computing using Python Assignment 9 Answers

Q10. While calculating the area of Punjab, which of the following will help in more accurate results.
a. More points landed in the Punjab region.
b. More points landed outside the Punjab.
c. More points on the overall map.
d. None of the above.

Answer: a. More points landed in the Punjab region.

### The Joy of Computing using Python Programming Assignmnet

Question 1

Given two string s1 and s2, write a function subStr which accepts two parameters s1 and s2 and will return True if a s2 is a substring of s1 otherwise return False. A substring is a is a contiguous sequence of characters within a string.
Input:
bananamania
nana
Output:
True
Explanation:
S2 which is nana is in bananamania hence it is a substring of s1.
Example 2:
Input:
aabbcc
abc
output:
False
Explanation:
String s1 does not contain string s2 hence the answer is false.

Solution:

``````def subStr(s1,s2):
if s2 in s1:
return True
else:
return False
``````

These are answers of The Joy of Computing using Python Assignment 9 Answers

Question 2

Given two dictionaries d1 and d2, write a function mergeDic that accepts two dictionaries d1 and d2 and return a new dictionary by merging d1 and d2.
Note: Contents of d1 should be appear before contents of d2 in the new dictionary and in same order. In case of duplicate value retain the value present in d1.
Input:
{1: 1, 2: 2}
{3: 3, 4: 4}
output:
{1: 1, 2: 2, 3: 3, 4: 4}

Solution:

``````def mergeDic(d1, d2):
d3 = {}

for key,value in d1.items():
d3[key] = value

for key,value in d2.items():
if key not in d3.keys():
d3[key] = value

return d3``````

These are answers of The Joy of Computing using Python Assignment 9 Answers

Question 3

Take an integer N as an input, print all the indexes of numbers in that integer from left to right.
Input:
122345
Output:
1 0
2 1 2
3 3
4 4
5 5
Explanation:
Given integer 122345. Now printing indexes of numbers from left to right.
First number is 1 and its index is 0 therefore
1 0
Second and third number is 2 and its index is 1,2 therefore
2 1 2
and so on…

Solution:

``````n = int(input())
n = str(n)
d = {}
for i in range(len(n)):
if n[i] not in d.keys():
d[n[i]] =  [i]
else:
d[n[i]].append(i)

for key, value in d.items():
print(key, end= ' ')
for i in value:
print(i, end= ' ')

print()``````

These are answers of The Joy of Computing using Python Assignment 9 Answers

Session JUL-DEC 2022

#### These are answers of The Joy of Computing using Python Assignment 9 Answers

Q1) Which of the following ways can help to identify the author of a book?
a. Books coverage
b. The uniqueness of writing.
c. Average word length.
d. Publisher name.

These are the Joy of Computing using Python Assignment 9 Answers

Q2) Networkx in pythons is used for
a. Making networks
b. Analyzing networks
c. Visualizing networks
d. Breaking networks

These are the Joy of Computing using Python Assignment 9 Answers

Q3) What is the output of the following program?

a. [‘joc, ‘python’]
b. [‘J’, ‘o’, ‘c’, ‘p’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’]
c. [j’, ‘o’, ‘c’,’, ‘p’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’]
d. [“‘, ‘c’, ‘h’, ‘J’, ‘n’, ‘o’, ‘o’, ‘p’, ‘t’, ‘y’]

Answer: c. [j’, ‘o’, ‘c’,’, ‘p’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’]

These are answers of The Joy of Computing using Python Assignment 9 Answers

Q4) In the world, on average, how many hops it will take to connect two people?
a. 6
b. 7
c. 8
d. 9
e. 10

Q5) In the following code, nx.is_connected (G) will return?

a. True
b. False
c. Sometimes True, sometimes false

These are the Joy of Computing using Python Assignment 9 Answers

Q6) How many neighbors does node 4 have?

a. 1
b. 2
c. 3
d. 4

These are answers of The Joy of Computing using Python Assignment 9 Answers

Q7) While calculating the area of a state, how can we increase the accuracy of the calculated area?
a. By increasing the size of the image.
b. By increasing the number of points.
c. By decreasing the size of the image.
d. By decreasing the number of points.

Answer: b. By increasing the number of points.

Q8) How many nodes and edges does the following graph have?

a. 5,5
b. 2,5
c. 5,2
d. 2,2

Q9) Which function of NLTK is used to make a frequency distribution of words?
a. freqdist()
b. FreqDist()
c. freq_dist()
d. frequency_distribution

These are answers of The Joy of Computing using Python Assignment 9 Answers

Q10) Degree of separation for the following graph is

a. 1
b. 2
c. 3
d. 5

These are answers of The Joy of Computing using Python Assignment 9 Answers

## Programming Assignment

Question 1
Given two strings s1 and s2, write a function subStr that accepts two strings s1 and s2 and will return True if a s2 is a substring of s1 otherwise return False. A substring is a is a contiguous sequence of characters within a string.
Input:
bananamania
nana
Output:
True

Solution:

``//Code``

These are answers of The Joy of Computing using Python Assignment 9 Answers

Question 2
Given two dictionaries d1 and d2, write a function mergeDic that accepts two dictionaries d1 and d2 and return a new dictionary by merging d1 and d2.
Note: Contents of d1 should be appear before contents of d2 in the new dictionary and in same order. In case of duplicate value retain the value present in d1.
Input:
{1: 1, 2: 2}
{3: 3, 4: 4}
Output:
{1: 1, 2: 2, 3: 3, 4: 4}

Solution:

``//Code``

These are answers of The Joy of Computing using Python Assignment 9 Answers

Question 3
Given an integer n, print all the indexes of numbers in that integer from left to right.
Input:
122345
Output:
1 0 2 1 2 3 3 4 4 5 5

Solution:

``//Code``

These are answers of The Joy of Computing using Python Assignment 9 Answers

More NPTEL Solution: https://progiez.com/nptel

 The content uploaded on this website is for reference purposes only. Please do it yourself first.