0% found this document useful (0 votes)
69 views17 pages

Fundamental of Computing

The document contains an assignment submission for the Fundamentals of Computing module. It includes the student's name, ID numbers, assignment details, and 5 questions with multiple parts requiring code snippets and outputs to be provided. Figures and tables are included to showcase the code, outputs, and other information requested as part of the assignment questions.

Uploaded by

Ashma Maharjan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views17 pages

Fundamental of Computing

The document contains an assignment submission for the Fundamentals of Computing module. It includes the student's name, ID numbers, assignment details, and 5 questions with multiple parts requiring code snippets and outputs to be provided. Figures and tables are included to showcase the code, outputs, and other information requested as part of the assignment questions.

Uploaded by

Ashma Maharjan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Module Code & Module Title

CS4051NI Fundamentals of Computing

Assessment Weightage & Type


40% Alternative Assessment

Year and Semester


2020-21 Summer

Student Name: Ashma Maharjan


Group: C2
London Met ID: 20048825
College ID: [email protected]
Assignment Due Date: September 21, 2021
Assignment Submission Date: September 21, 2021

I confirm that I understand my coursework needs to be submitted online via Google Classroom under the
relevant module page before the deadline in order for my assignment to be accepted and marked. I am
fully aware that late submissions will be treated as non-submission and a marks of zero will be awarded.
Table of Contents

Question No.1 ................................................................................................................. 1

Question No.2 ................................................................................................................. 2

Question No.3(a) ............................................................................................................. 5

Question No.3(b) ............................................................................................................. 7

Question No.4 ............................................................................................................... 10

Question No.5(a) ........................................................................................................... 13

Question No.5(b) ........................................................................................................... 14


List of Figures

Figure 1: Screenshot of code for Question No.1 ............................................................. 1


Figure 2: Screenshot of output for Question No.1 ........................................................... 1
Figure 3: Flowchart for the given program ....................................................................... 4
Figure 4: Screenshot of code after fixing error in Question No.3(a) ................................ 5
Figure 5: Screenshot of output after fixing error in Question No.3(a) .............................. 5
Figure 6: Screenshots of code after fixing error in Question No.3(b) .............................. 8
Figure 7: Screenshot of output after fixing error in Question No.3(b) .............................. 8
Figure 8: Screenshot of code for Question No.4 ........................................................... 11
Figure 9: Screenshot of output for Question No.4 ......................................................... 11
Figure 10: Screenshot of code for Question No.5(a) ..................................................... 13
Figure 11: Screenshot of output for Question No.5(a) ................................................... 13
Figure 12: Screenshot of code for Question No.5(b) ..................................................... 14
Figure 13: Screenshot of output for Question No.5(b) ................................................... 14

List of Tables
CS4051NI FUNDAMENTALS OF COMPUTING

Question No.1

Figure 1: Screenshot of code for Question No.1

Figure 2: Screenshot of output for Question No.1

1
STUDENT NAME
CS4051NI FUNDAMENTALS OF COMPUTING

Question No.2
1.
In the given program a two-dimensional list is created with the following values:
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Then, each row of the matrix is printed using print function inside for-each loop. A
variable i is declared whose value is initialized to 0. Then while loop is used run the
program in loop. The while loop will run until the value of i is less than the length of
matrix. Again, inside the first while loop another variable j is declared whose value is
initialized to 0 and second while loop is defined which will run until the value of j is less
than the length of matrix [i]. Then, if-else-if block is used inside the second while loop. If,
the value of i is greater than j then the value of matrix [i][j] will be changed to 1 and if the
value of i is less than j then its value will be changed to -1. Then after each iteration of
second while loop, the value of j will be increased by 1 and immediately after ending the
second while loop (inside the first while loop) the value of i will be increased by 1.

Here, initially, value of i is 0. 0 is less than 3 which is the length of matrix, therefore the
pointer moves towards next line inside the first while loop where the value of j is 0.

Since the value of i is 0, matrix [i] represents matrix [0] whose value is [1, 2, 3] therefore
the length of matrix [0] is 3. Here, the value of j is 0 which is less than 3, therefore the
pointer moves towards next line inside the second while loop.

So, for the first iteration of second while loop the value of both i and j is 0 which means
that it doesn’t satisfy any of the condition of the if-else-if block. Therefore, for matrix
[0][0] i.e., 1, value will not be changed.

Then the value of j will be increased by 1 which will make the value of j to 1. Here, the
value of j is again less than the length of matrix [i] i.e., matrix [0], therefore the second
iteration will continue.

2
STUDENT NAME
CS4051NI FUNDAMENTALS OF COMPUTING

In second iteration of second while loop the value of i is 0 and j is 1. Here, i is less than
j, therefore the value of matrix [i][j] i.e., matrix [0][1] will be -1.

Similarly, again after increment by 1 the value of j will be 2 which is less than the length
of matrix [i] therefore the loop will continue. The value of i is less than j, therefore the
value of matrix [i][j] i.e., matrix [0][2] will be -1.

Again, after the increment by 1 the value if j will be 3 which is not less than the length of
matrix [i], so the second while loop will exit. After existing from second while loop, the
value of i will also be increased by 1.

After increment the value of i will be 1. Here, 1 is less than the length of matrix which is
3.
Again, the value of j will be 0. Since, 0 is less than length of matrix [1] which is 3, the
loop will continue.
Here, value of i is greater than j (1 > 0) therefore the value of matrix [i][j] i.e., matrix
[1][0] will be 1.
Similarly, following the similar way, when the value of j is 1 and 2 the matrix [1][1] and
matrix [1][2] will be 5 and -1 respectively.
Finally, when the value of i is 2 after the increment, the value of matrix [2][0], matrix
[2][1] and matrix [2][2] will be 1, 1 and 9 respectively.

3
STUDENT NAME
CS4051NI FUNDAMENTALS OF COMPUTING

2.

Figure 3: Flowchart for the given program

4
STUDENT NAME
CS4051NI FUNDAMENTALS OF COMPUTING

Question No.3(a)
After fixing the error the screenshot of the code and output is:

Figure 4: Screenshot of code after fixing error in Question No.3(a)

Figure 5: Screenshot of output after fixing error in Question No.3(a)

5
STUDENT NAME
CS4051NI FUNDAMENTALS OF COMPUTING

The list of errors that I fixed in the program are:

Errors Fixing

for j in range(2, len(marks[i])): for j in range(1, len(marks[i])):

print(marks[j][i]) l.append(marks[i][j])

print(marks_c) marks_c.update({name: l})

6
STUDENT NAME
CS4051NI FUNDAMENTALS OF COMPUTING

Question No.3(b)
After fixing the error the screenshot of the code and output is:

7
STUDENT NAME
CS4051NI FUNDAMENTALS OF COMPUTING

Figure 6: Screenshots of code after fixing error in Question No.3(b)

Figure 7: Screenshot of output after fixing error in Question No.3(b)

8
STUDENT NAME
CS4051NI FUNDAMENTALS OF COMPUTING

The list of errors that I fixed in the program are:

Errors Fixing

Indentation error inside the initializer, Using proper indentation.


functions and if-else blocks.
Using comparison operator i.e, == as an Using assignment operators while
assignment operator i.e, = assigning values and using comparison
operator while doing comparisons inside if-
else block.
“self” parameter not used while returning Using the “self” parameter while returning
the values. value.
For example: For example:
return name return self.name
class Employee: class Employee(Person):

Inside change_address function of Inside change_address function of


Employee class: Employee class:
self.addr == addr self.address = addr
Calling method get_all_details() which is Calling the correct method get_all_detail().
not defined in class.

Bracket missing at the end of print Using the brackets correctly:


statement: print(bar.get_all_details())
print(bar.get_all_details()

9
STUDENT NAME
CS4051NI FUNDAMENTALS OF COMPUTING

Question No.4
The collection data types in Python are:
a. Lists: Lists are ordered mutable sequences that can be changed after they have
been created by adding, removing, or changing objects. Lists can be declared by using
square brackets “[]” and the elements inside it will be separated by using the commas.
They are used to store an ordered collection of items, which might be of different types.
The index of list starts from 0 to (length of list – 1). Indexes are used to access the
elements of list.
Example: List1 = [“Anna” , “Eva” , “Ben” , “David”]
List2 = [2, 4, 7, 8]

b. Tuples: Tuples are similar to list i.e., ordered collection of elements, however they
are immutable. Therefore, they don’t contain built-in methods to add, remove or change
the objects. A tuple can be created by a pair of parenthesis and comma-separated
objects, following the variable name. They are used in the place of lists for efficient
memory and time management. Also, to protect the data that doesn’t need to be
changed, we may use the tuples.
Example: Tuple1 = (“mango” , “apple” , “orange” )
Tuple2 = (55, 67, 80)

c. Dictionary: A dictionary is an unordered set of key/value pairs where the keys must
be unique and of immutable type whereas the values may be duplicates and can be any
type (mutable or immutable). Items can be added or removed even after creating the
dictionary. The dictionary is created by using a pair of curly braces. We can use
dictionary when the data has a unique reference that can be associated with the value.
Example: dict1 = {‘Id’ : 101, ‘name’ : “David Winston”, ‘address’ : “London”}

d. Sets: Sets are an unordered mutable collection of elements that do not support
duplicated objects. Items can be easily added and removed from the sets. However,
there is no concept of index in sets as specific order is not guaranteed in sets. To create
the sets we use a pair of curly braces containing the elements separated by comma or
10
STUDENT NAME
CS4051NI FUNDAMENTALS OF COMPUTING

using the set() constructor. It also supports the mathematical set operations like union,
intersection, difference and symmetric difference. They are used to efficiently remove
duplicate values from a collection like a list and to perform common math operations like
unions and intersections. Also, they are used when we don’t want duplicate values in
our collection of elements.
Example: set1 = {1, 4, 6, 8, 3}
Set2 = {'Python', 'Java', 'SQL', 'Hadoop'}

Creating table given in the question:


Screenshot of code of code and output:

Figure 8: Screenshot of code for Question No.4

Figure 9: Screenshot of output for Question No.4

Here, dictionary data type has been used to create the tables. As, the above tables
have unique keys which is the Movie ID it is better to used dictionary to store the data
items. Also, the values of above dictionary can be easily accessed using keys. If any

11
STUDENT NAME
CS4051NI FUNDAMENTALS OF COMPUTING

changes are to be made in the above dictionary then, we can do so by adding,


removing and updating the elements. Also, to represent the data in tabular format,
dictionary is one of the suitable collection data type. Here, we don’t need to remember
each index to access the values. For example we can easily get the details of Movie
with ID “M001” by using the keys and values.

12
STUDENT NAME
CS4051NI FUNDAMENTALS OF COMPUTING

Question No.5(a)

Figure 10: Screenshot of code for Question No.5(a)

Figure 11: Screenshot of output for Question No.5(a)

13
STUDENT NAME
CS4051NI FUNDAMENTALS OF COMPUTING

Question No.5(b)

Figure 12: Screenshot of code for Question No.5(b)

Figure 13: Screenshot of output for Question No.5(b)

14
STUDENT NAME

You might also like