Inbound 6215576121251288497
Inbound 6215576121251288497
Here, the result of inc( ) function change every time if the value of ‘y’ get changes inside the
function definition. Hence the side effect of inc( ) function is changing the data of the external
visible variable ‘y’.
4. Differentiate pure and impure function.
pure function impure function
A function can be a pure function The variables used inside the function may
provided it should not have any cause side effects though the functions
external variable which will alter the which are not passed with any arguments. In
behaviour of that variable. such cases the function is called impure
function
The return value of the pure functions The return value of the impure functions
solely depends on its arguments does not solely depend on its arguments
passed. Hence, if we call the pure passed. Hence, if we call the impure functions
functions with the same set of with the same set of arguments, we might get
arguments, we will always get the the different return values
same return values.
They do not have any side effects. They may have side effects.
They do not modify the arguments They may modify the arguments which are
which are passed to them passed to them
Example: Example:
let square x:= let Randomnumber:=
return x * x a := random()
if a > 10 then
return: a
else
return: 10
Here, the function ‘square’ is a pure Here the function ‘Randomnumber’ is
function because it will not give different impure as it is not sure what will be the result
results for same input. when we call the function.
5. What happens if you modify a variable outside the function? Give an example.
One of the most popular groups of side effects is modifying the variable outside of function.
Example:
y: = 0
let inc (int) x:=
y: = y + x;
return (y)
Here, the result of inc( ) function change everytime if the value of ‘y’ get changes inside the function
definition. Hence the side effect of inc( ) function is changing the data of the external visible variable ‘y’.
Part - IV
Answer the following questions (5Marks)
1. What are called Parameters and write note on (i) Parameter without Type (ii) Parameter with Type
Parameters are the variables in a function definition and arguments are the values which are passed
to a function definition.
Parameter without Type:
(requires: b>=0 )
(returns: a to the power of b)
let rec pow a b:=
if b=0 then 1
else a * pow a (b-1)
In the above function definition we have not mentioned any types. Some language compiler solves
this type inference problem algorithmically, but some require the type to be mentioned.
Parameter with Type:
(requires: b> 0 )
(returns: a to the power of b )
let rec pow (a: int) (b: int) : int :=
if b=0 then 1
else a * pow a (b-1)
When we write the type annotations for ‘a’ and ‘b’ the parentheses are mandatory. There are times
we may want to explicitly write down types. This is useful on times when you get a type error from
the compiler. Explicitly annotating the types can help with debugging such an error message.
3
In each iterative step, two chameleons of the two types (equal in number) meet and change
their colors to the third one. (i.e) In each meeting, a and b each decreases by 1, and c
increases by 2. For example, if A, B, C = 4, 4, 6, then the series of meeting will result in:
Part - III
Answer the following questions: (3 Marks)
1. Differentiate Concrete data type and abstract datatype.
Concrete data type abstract datatype
Concrete data types or structures are Abstract Data type (ADT) is a type for
direct implementations of a relatively objects whose behavior is defined by a
simple concept. set of value and a set of operations. It
offers a high level view of a concept
independent of its implementation.
A concrete data type is a data type whose In abstract data type the representation
representation is known. of a data type is unknown
6
Selectors:
Selectors are functions that retrieve information from the data type. To extract the information
of a city object, we would use functions like
Example:
getname(city)
getlat(city)
getlon(city)
In the above example the function getname (city), getlat (city), getlon (city) are the selectors
because these functions extract the information of the city object
Example:
The following pseudo code will compute the distance between two city objects:
distance(city1, city2):
lt1, lg1 := getlat(city1), getlon(city1)
lt2, lg2 := getlat(city2), getlon(city2)
1/2
return ((lt1 - lt2)**2 + (lg1 - lg2)**2))
In the above code:
Read distance(), getlat() and getlon() as functions
Read lt as latitude and lg longitude.
Read := as “assigned as” or “becomes”
lt1, lg1 := getlat(city1), getlon(city1)
Read as lt1 becomes the value of getlat(city1) and lg1 becomes the value of getlont(city1).
2. What is a List? Why List can be called as Pairs. Explain with suitable example
List is constructed by placing expressions within square brackets separated by commas. Such an
expression is called a list literal. List can store multiple values. Each value can be of any type and
can even be another list. Example: x: = [10, 20]
The elements of a list can be accessed in two ways.
The first way is via multiple assignments, which unpacks a list into its elements and binds each
element to a different name.
Example:
x: = [10, 20]
a, b:=x
The second method is by the element selection operator, which selects an element from the
value of the preceding expression.
Example:
x: = [10, 20]
a: = x[0]
b: = x[1]
Any way of bundling two values together into one can be considered as a pair. Lists are a common
method to do so. Therefore List can be called as Pairs.
Example: x:= [10, 20]
x[(0, 10), (1, 20)] - where
(0, 10) (1, 20)
Lesson - 3 SCOPING
Part - II
Answer the following questions: (2 Marks)
1. What is a scope?
Scope refers to the visibility of variables, parameters and functions in one part of a program to
another part of the same program.
2. Why scope should be used for variable. State the reason.
Scope defines the order in which variables have to be mapped to the object in order to obtain
the value.
It is a good practice to limit a variable's scope to a single definition. This way, changes inside
the function can't affect the variable on the outside of the function in unexpected ways.
3. What is mapping?
The process of binding a variable name with an object is called mapping. = (equal to sign) is used
in programming languages to map the variable and object.
4. What do you mean by Namespaces?
Namespaces are containers for mapping names of variables to objects. we can consider
namespace as dictionaries, containing list of words and its meanings. The words are mapped with
its meaning in dictionaries whereas names are mapped with objects (name : = object) in
programming language. Example: a:=5
5. How Python represents the private and protected Access specifiers?
Python prescribes a convention of prefixing the name of the variable or method with single or
double underscores to emulate the behaviour of protected and private access specifiers.
All members in a Python class are public by default. Any member can be accessed from outside
the class environment in Python
Part - III
Answer the following questions: (3 Marks)
1. Define Local scope with an example.
Local scope refers to variables defined in current function.
Always, a function will first look up for a variable name in its local scope.
Only if it does not find it there, the outer scopes are checked.
Example: Output:
Disp( ):
a:=7 Local Scope 7
print(a)
Disp( )
Global Scope:
A variable which is declared outside of all the functions in a program is known as global variable.
This means, global variable can be accessed inside or outside of all the functions in a program.
Example: Output:
a=10 Global Scope
Disp( ): 7
10
a:=7 Local Scope
print(a)
Disp( )
print(a)
Enclosed Scope:
A variable which is declared inside a function which contains another function definition with in it,
the inner function can also access the variable of the outer function. This scope is called enclosed
scope.
Example: Output:
Disp( ): 10
a:=10 Enclosed scope 10
Disp1( ):
print a
Disp1( )
print a
Disp()
Built-in Scope:
The built-in scope has all the names that are pre-loaded into the program scope when we start
the compiler or interpreter. Any variable or module which is defined in the library functions of a
programming language has Built-in or module scope. They are loaded as soon as the library files
are imported to the program.
Example:
Built-in/module scope Library files associated with the software
Disp( ):
a:=10
Disp1( ):
print a
Disp1( )
print a
Disp()
Module:
A module is a part of a program. A single module can contain one or more statements closely
related each other. Modules can be integrated with other modules.
Modular Programming:
The process of subdividing a computer program into separate sub-programs is called Modular
programming. Modular programming enables programmers to divide up the work and debug pieces
of the program independently. The examples of modules are procedures, subroutines, and
functions.
13
target = 77
Output: 4 Element found
15
15 11 16 12 14 13
Step (1): 15 > 11 So interchange
11 15 16 12 14 13
11 15 16 12 14 13
Step (3): 16 > 12 So interchange
11 15 12 16 14 13
11 15 12 16 14 13
Step (4): 16 > 14 So interchange
11 15 12 14 16 13
11 15 12 14 16 13
Step (5): 16 > 13 So interchange
11 15 12 14 13 16
16
11 15 12 14 13 16
Step (7): 15 > 12 So interchange
11 12 15 14 13 16
11 12 15 14 13 16
Step (8): 15 > 14 So interchange
11 12 14 15 13 16
11 12 14 15 13 16
Step (9): 15 > 13 So interchange
11 12 14 13 15 16
11 12 14 13 15 16
Step (13): 14 > 13 So interchange
11 12 13 14 15 16
At the end of all the iterations we will get the sorted values in an array {11,12,13,14,15,16}
Algorithm Complexity:
Complexity Worst Best Average
Linear O(n) O(1) O(n)
Search method Binary O(log n) O(1) O(log n)
Bubble sort (stable) O(n2) Ω(n) Ѳ(n2)
Insertion sort (stable) O(n2) Ω(n) Ѳ (n2)
Selection sort O(n2) Ω(n2) Ѳ (n2)
Sort method Quick sort O(n2) Ω (n log n) Ѳ (n log n)
Merge sort (stable) O(n log n) Ω (n log n) Ѳ (n log n)
Heap sort O(n log n) Ω (n log n) Ѳ (n log n)