PROGRAMMING
PROGRAMMING
Computing
Unit 01: Programming
Assignment 01
Efficiency:
Algorithms aim to maximize efficiency in terms of resources and time. Their
objective is to resolve problems or do tasks in a reasonable amount of time while
making the most use of computational resources like memory, processing power,
or garage.
function fibonacci(n)
if n <= 1
return n
else
a=0
b=1
print a
print b
for i from 2 to n
c=a+b
print c
a=b
b=c
function factorial(n)
if n <= 1
return 1
else
result = 1
for i from 2 to n
result = result * i
return result.
2. Algorithm Design:
Designing an efficient algorithm can be tricky, especially for complex
problems. It requires critical thinking and problem-solving skills.
4. Debugging:
Identifying and fixing errors (bugs) in the code can be time-consuming. It
requires good debugging skills and patience.
9. Version Control:
Managing code changes, collaborating with team members, and resolving
conflicts in version control systems like Git can be challenging, especially for
large projects with multiple contributors.
function fibonacci(n)
if n <= 1
return n
else
a=0
b=1
print a // Iteration 1: 0
print b // Iteration 1: 1
for i from 2 to n
c=a+b
print c // Iteration 2: 1
// Iteration 3: 2
// Iteration 4: 3
// Iteration 5: 5
a=b
b=c
when n=5;
fibonacci 5 = 0,1,1,2,3,5
function factorial(n)
if n <= 1
return 1
else
result = 1
for i from 2 to n
result = result * i
return result
when n=5;
result = 5*4*3*2*1 = 120
Standardized Comparison:
Big-O notation allows us to compare the efficiency of different algorithms
independently of the hardware or software environment. It provides a
standardized way to assess and compare algorithms based on their scalability.
Optimization Guidance:
Gaining knowledge of an algorithm's Big-O notation might help identify possible
areas for optimization. It assists in pinpointing the algorithmic components that
are primarily responsible for the overall inefficiency of the system, directing
programmers in the optimization of crucial code segments.
Algorithm Selection:
Big-O notation assists in selecting the most suitable algorithm for a given
problem based on its complexity requirements. For example, if real-time
performance is crucial, algorithms with lower time complexity (e.g., O (log n),
O(n)) may be preferred over those with higher time complexity (e.g., O(n^2),
O(2^n)).
def fibonacci(n):
if n <= 1:
return n
else:
a, b = 0, 1
fib_series = [a, b]
for i in range(2, n+1):
c=a+b
fib_series.append(c)
a, b = b, c
return fib_series
def factorial(n):
if n <= 1:
return 1
else:
result = 1
for i in range(2, n+1):
result *= i
return result
Fibonacci series
The time complexity of the Fibonacci series algorithm implemented above is
O(n), where 'n' is the input number. This is because each number in the
Fibonacci series is calculated by adding the previous two numbers, and the
algorithm iterates 'n' times to generate the series.
Factorial value
The time complexity of the factorial algorithm implemented above is also O(n),
where 'n' is the input number. This is because the algorithm iterates 'n' times,
multiplying each iteration by the current value of 'result'.
Because both algorithms have linear time complexity, the size of the input has a
linear effect on how long they take to execute. They are therefore effective with
modest to moderate input sizes. Their efficiency, however, may become a
bottleneck for very large inputs when contrasted to algorithms with smaller
complexities, such O(log n) or O(1).
This paradigm places a strong emphasis on process using the underlying machine
concept. The imperative approach and the procedural approach are identical. Reusing the
code was a benefit at the time it was in use because it allowed for code reuse.
Versatility:
With the flexible paradigm of procedural programming, developers can design
coding projects that achieve a wide range of objectives. There is probably a
procedural programming language that you may use to achieve your objectives, as
these languages are made for a wide range of development tasks, such as software
and web development.
Simplicity:
One comparatively easy method of programming computers is procedural
programming. Because procedural programming languages offer a basis for
coding that can be applied to other languages, including object-oriented
languages, this is the reason that many developers begin their work with them.
Accessibility:
Procedural programming is used by many popular programming languages,
therefore learning them can be facilitated by a wealth of resources for a
prospective developer. This can hasten your progress by providing access to both
free and paid online communities and resources that you can use when faced with
obstacles.
Complexity:
Troubleshooting:
Finding the source of mistakes in your code can be difficult in procedural
programming languages because of the usage of global data. This may result in a
difficult debugging procedure that extends the time allotted for development.
# Calculate factorial
for i in range(1, num + 1):
fact = fact * i
Sequential Execution:
The steps in the code are organized in a linear fashion. It first requests input from
the user, uses a loop to calculate the factorial, and then outputs the result. This
sequential flow reflects the procedural paradigm's focus on doing things step-by-
step.
Procedures / Functions:
This little piece of code exemplifies the idea of segmenting a program into more
manageable, reusable code parts, even if it doesn't specifically define any distinct
procedures or functions. It would be simple to put the factorial computation within
a function, as is frequently done in procedural programming.
The program is written as a set of objects and classes designed to be used together for
communication. Objects are the smallest and most fundamental entities, and they are the
sole subjects of all computations. Data is given more weight than process. It is capable
of handling nearly any type of real-world issue that arises nowadays.
Encapsulation:
Encapsulation is the process of enclosing data inside an object. Encapsulation
creates a wall around data in OOP to keep it safe from the rest of the code. By
incorporating the data and its functions into a class, encapsulation can be
accomplished. This action only reveals the functionality required to interface with
a class; it hides all other features. A class is considered well-encapsulated when it
prevents direct access to its private data.
Abstraction:
Abstraction is the process of accessing objects through simpler classes as opposed
to intricate implementation code. It's usually easier to write programs when you
can keep a class's interface and implementation distinct. Through the class
member functions in object-oriented programming, you can abstract the
implementation details of a class and provide a clear, user-friendly interface. In
the event of an error, the change only impacts the implementation details of a
class and not the outside code thanks to abstraction, which helps to isolate the
impact of code modifications.
Inheritance:
Because inheritance is supported by most object-oriented languages, a new class
inherits its parent class's functions and attributes by default. Classes can be
arranged hierarchically thanks to inheritance, where a class may have one or more
parent or child classes. A class is said to have inherited its parent's properties if it
has a parent class. The behavior of the parent class can also be expanded upon or
changed by the child class. You can reuse code through inheritance without
having to redefine a child class's functionalities.
Reusable Code:
OOP's inheritance principle enables you to reuse code without having to write it
from scratch. When writing code, this feature can help to decrease errors.
Increased productivity:
When developing new software, you can save time by constructing objects from
classes. Libraries and reusable code are other tools you can utilize to boost
productivity.
Enhanced Security:
Encapsulation and abstraction are useful techniques for revealing sensitive
information while displaying a limited amount of data. When creating
sophisticated code, these features can offer increased security.
class Signup:
def __init__(self):
self.userid = 0
self.name = ""
self.emailid = ""
self.sex = ""
self.mob = 0
if __name__ == "__main__":
print("Hello!")
s1 = Signup()
s1.create(22, "riya", "[email protected]", 'F', 89002)
Class definition:
The attributes and actions pertaining to user sign-up capabilities are contained in
the Signup class. It describes characteristics that indicate a user account's status,
such as userid, name, emailid, sex, and mob.
Constructor method(__init__):
The class's constructor is the __init__ function. When an instance of the class is
created, it initialises all of the attributes (userid, name, emailid, sex, and mob) to
their default values.
Encapsulation:
The class combines functionality (methods) and data (attributes) into a single
entity. Encapsulation reveals only the methods required to interact with an object,
while concealing its internal state. Here, the create method abstracts away the
implementation specifics and offers an interface for making a user account.
def button_click():
label.config(text="Button clicked!")
Event handling:
The command argument designates the event handler that is linked to the Button
widget. In this instance, clicking the button initiates the button_click function.
This function reacts to the button click event by acting as an event handler.
Responsive UI:
Responsive user interfaces can be made with event-driven programming. The
corresponding event handler is instantly activated when the user interacts with
the GUI (by, for example, clicking a button), giving the user feedback in real
time.