Вы находитесь на странице: 1из 9

Chapter 1



1.1 What Is Computer Science?

Computer Science is designing of algorithms and computation of real time problems using programming
languages and compilers which is finally processed via ALU of the computer.

Review of the history of computer science reveals an interesting progression of definitions for computer
• study of automatic computing (1940s)
• study of information processing (1950s)
• study of phenomena surrounding computers (1960s)
• study of what can be automated (1970s)
• study of computation (1980s)

What computer science is fundamentally about is computational problem solving-that is, solving problems
by the use of computation.

1.2 What is Computation?

Computer science as computational problem solving raises one question in mind -What is computation?
Computation is a form of action. One characterization of computation is given by the notion of an algorithm.
An algorithm is a step-by-step procedure for performing a computation.
Adding a series of numbers, called summation, is a simple example of computation. Below is a basic
algorithm for summation:

Summation Algorithm:

Integer n, with n > 0

Variable sum =1+2+3+...+n


1. i=1
2. sum = 0
3. if(i>n) goto 7
4. sum = sum + i
5. i=i+1
6. go to step 3
7. Display the value of Sum

HTML: an example of what computation is not

HTML is arguably the fundamental computer language of the Web and the basis of Web programming;
however, many computer scientists do not consider HTML to be a programming language.

The distinction between the popular notion of "Web programming" and the computer science notion of
"programming language" hinges on computation. A computer scientist uses a programming language to
express algorithms that do computation. In contrast, Web programming does not require the author to write
algorithms to do computation.
For example, if you want to add the numbers 1+2+3+...+100, then HTML is not helpful. Even the simplest
arithmetic cannot be performed by HTML. The language is not designed to express algorithms of any kind.

HTML does not express algorithms, but it certainly does represent information. For example, if you wanted
to write a story about adding the numbers 1+2+3+...+100, then HTML provides a language with which you
can tell that story. You could also use HTML to tell the story of The Adventures of Huckleberry Finn. Both
of these stories are examples of information, not computation.

If you used HTML to write a story about adding the numbers 1+2+3+...+100, then you would probably want
to use an algorithm (expressed by some programming language other than HTML) in order to compute the
actual sum. Then with HTML you could present that sum as a tidy conclusion to the story.

For example, you could use the Web scripting language PHP to add 1+2+3+...+100 and to generate the HTML
that would present the above story. PHP can express algorithms that do computation; it is a programming
language in the classic sense.

1.3 Explain in brief Computational Problem Solving.

In order to solve a problem computationally, two things are needed: a representation that captures all the
relevant aspects of the problem, and an algorithm that solves the problem by use of the representation.

Let’s consider a problem known as the Man, Cabbage, Goat, Wolf problem.
A man lives on the east side of a river. He wishes to bring a cabbage, a goat, and a wolf to a village on the
west side of the river to sell. However, his boat is only big enough to hold himself, and either the cabbage,
goat, or wolf. In addition, the man cannot leave the goat alone with the cabbage because the goat will eat the
cabbage, and he cannot leave the wolf alone with the goat because the wolf will eat the goat. How does the
man solve his problem?

There is a simple algorithmic approach for solving this problem by simply trying all possible combinations
of items that may be rowed back and forth across the river. Trying all possible solutions to a given problem
is referred to as a brute force approach .What would be an appropriate representation for this problem? Since
only the relevant aspects of the problem need to be represented, all the irrelevant details can be omitted. A
representation that leaves out details of what is being represented is a form of abstraction.

The use of abstraction is prevalent in computer science. In this case, is the color of the boat relevant? The
width of the river? The name of the man? No, the only relevant information is where each item is at each
step. The collective location of each item, in this case, refers to the state of the problem. Thus, the start state
of the problem can be represented as follows:
Man cabbage goat wolf
[E, E, E, E]

In this representation, the symbol E denotes that each corresponding object is on the east side of the river.
If the man were to row the goat across with him, for example, then the representation of the new problem
state would be
Man cabbage goat wolf
[W, E, W, E]

in which the symbol W indicates that the corresponding object is on the west side of the river—in this case,
the man and goat. (The locations of the cabbage and wolf are left unchanged.) A solution to this problem is
a sequence of steps that converts the initial state,

[E, E, E, E]

in which all objects are on the east side of the river, to the goal state ,
[W, W, W, W]

in which all objects are on the west side of the river. Each step corresponds to the man rowing a particular
object across the river (or the man rowing alone. You would need an algorithm that can compute this. Thus,
no matter how well you may know a given programming language or how good a programmer you may be,
without such an algorithm you could not solve this problem.
The remaining task is to develop or find an existing algorithm for computationally solving the problem using
this representation.
One of the solution to this problem is

Step 1 The man goes to west side  with the goat

Step 2 The man comes back → alone
Step 3 The man goes to west side  with the cabbage
Step 4 The man comes back → with the goat
Step 5 The man goes to west side  with the wolf
Step 6 The man comes back → alone
Step 7 The man goes to west side  with the goat
All reached the other side (west side) of the river

West side River East side

Initially Man cabbage goat wolf
[E, E, E, E]

Step 1 Man goat Cabbage wolf

[W, W] [E, E]

Step 2 goat Man cabbage wolf

[W] [E, E, E]

Step 3 goat Man cabbage wolf

[W, W, W] [E]

Step 4 cabbage Man goat wolf

[W] [E, E, E]

Step 5 cabbage Man wolf goat

[W, W, W] [E]

Step 6 Cabbage wolf Man goat

[W, W] [E, E]

Step 7 Cabbage wolf Man goat

[W, W, W, W]

1.4 What are Limitations of Computational Problem Solving?

The development of an algorithm is a key step in solving a problem. In computer science multiple algorithms
are available for solving the same problem (for example, sorting problem has many algorithms like insertion
sort, selection sort, quick sort and many more.
Once we have many algorithms for a given problem, an important question is, “Which algorithm is the best?”
For this, we do algorithm analysis. The goal of analysis of algorithm is to compare algorithms (or solutions)
mainly in terms of running time. We choose an algorithm that provides a solution to the problem in a reasonable
amount of time. If an algorithm does not provide the result in a reasonable amount of time, then the particular
algorithm is of limited practical use.
The Traveling Salesman problem is a classic computational problem in computer science.

The problem is to find the shortest route of travel for a salesman needing to visit a given set of cities. In a
brute force approach, the lengths of all possible routes would be calculated and compared to find the shortest
one. For ten cities, the number of possible routes is 10! (10 factorial), or over three and a half million
(3,628,800). For twenty cities, the number of possible routes is 20! , or over two and a half quintillion
(2,432,902,008,176,640,000). If we assume that a computer could compute the lengths of one million routes
per second, it would take over 77,000 years to find the shortest route for twenty cities by this approach. For
50 cities, the number of possible routes is over 1064 . In this case, it would take more time to solve than the
age of the universe!
For problems such as the Traveling Salesman problem in which a brute-force approach is impractical to use,
more efficient problem-solving methods must be discovered that find either an exact or an approximate
solution to the problem. Any algorithm that correctly solves a given problem must solve the problem in a
reasonable amount of time, otherwise it is of limited practical use.

1.5 Explain the Process of Computational Problem Solving.

Computational problem solving does not simply involve development of an algorithm and implementing it
with the help of a programming language. It is a process which consist of many phases, with programming
being only one of the phases. It is a framework defining tasks performed at each phase in the development
of the program.

Process of computational problem solving consist of four phases. These are as follow:

• Analysis – Analyze problem

• Design – Describe data and algorithms
• Implementation- Implement the algorithm with the help of data structure and programming language
• Testing- Test and debug the program

• Problem Analysis

(a) Understanding the Problem

Once a problem is clearly understood, the fundamental computational issues for solving it can be
determined. For each of the problems discussed earlier, the representation is straightforward. For the
Man, Cabbage, Goat, Wolf (MCGW) problem, a brute-force algorithmic approach of trying all possible
solutions works very well, since there are a small number of actions that can be taken at each step, and
only a relatively small number of steps for reaching a solution. For the Traveling Salesman problem the
brute-force approach is infeasible. Thus, the computational issue for these problems is to find other, more
efficient algorithmic approach for their solution. (In fact, methods have been developed for solving
Traveling Salesman problems involving tens of thousands of cities.)

(b) Knowing What Constitutes a Solution

Besides clearly understanding a computational problem, one must know what constitutes a solution.
For some problems, there is only one solution. For others, there may be a number (or infinite number) of
solutions. Thus, a program may be stated as finding,
♦ A solution
♦ An approximate solution
♦ A best solution
♦ All solutions

For the MCGW problem, there are an infinite number of solutions since the man could pointlessly row
back and forth across the river an arbitrary number of times. A best solution here is one with the shortest
number of steps. (There may be more than one “best” solution for any given problem.) In the Traveling
Salesman problem there is only one solution (unless there exists more than one shortest route).
• Program Design

(a) Describing the Data Needed

For the Man, Cabbage, Goat, Wolf problem, a list can be used to represent the correct location (east and
west) of the man, cabbage, goat, and wolf as discussed earlier, reproduced below,
Man cabbage goat wolf
[W, E, W, E]

For the Traveling Salesman problem, the distance (in km) between each pair of cities must be
represented. One possible way of structuring the data is as a table, depicted below.

Amritsar Beas Jalandhar Phagwara Chandigarh Ludhiana Ambala New

Amritsar - 44 82 103 229 142 251 466
Beas 44 - 38 61 181 100 209 424
Jalandhar 82 38 - 23 143 61 170 386
Phagwara 103 61 23 - 127 40 148 364
Chandigarh 229 181 143 127 - 107 46 261
Ludhiana 142 100 61 40 107 - 109 325
Ambala 251 209 170 148 46 109 - 218
New Delhi 466 424 386 364 261 325 218 -

The data given in the above table can be used to find an optimal path from Amritsar to New Delhi. For
example distance between Amritsar and Chandigarh is 229 Km.

(b) Describing the Needed Algorithms

When solving a computational problem, either suitable existing algorithms may be found or new
algorithms must be developed. For the MCGW problem, there are standard search algorithms that
can be used. For the Traveling Salesman problem, there are various (nontrivial) algorithms that can
be utilized, as mentioned, for solving problems with tens of thousands of cities. Algorithms that work
well in general but are not guaranteed to give the correct result for each specific problem are called
heuristic algorithms.

• Program Implementation

Design decisions provide general details of the data representation and the algorithmic approaches for
solving a problem. The details, however, do not specify which programming language to use, or how to
implement the program. That is a decision for the implementation phase. Since we are programming in
Python, the implementation needs to be expressed in a syntactically correct and appropriate way, using
the instructions and features available in Python.

Program = Algorithm + Data structures

• Program Testing

As humans, we have abilities that far exceed the capabilities of any machine, such as using common
sense reasoning, or reading the expressions of another person. However, one thing that we are not very
good at is dealing with detail, which computer programming demands. Therefore, while we are enticed
by the existence of increasingly capable computing devices that unfailingly and speedily execute
whatever instructions we give them, writing computer programs is difficult and challenging. As a result,
programming errors are pervasive, persistent and inevitable. However, the sense of accomplishment of
developing software that can be of benefit to hundreds, thousands, or even millions of people can be
extremely gratifying. If it were easy to do, the satisfaction would not be as great. Given this fact, software
testing is a crucial part of software development. Testing is done incrementally as a program is being
developed, when the program is complete, and when the program needs to be updated.

1.6 The Python Programming Language

• About Python

Python is a general purpose, interpreted, object oriented programming language created by Guido Van
Rossum in the Netherlands in 1990 and was named after the popular British comedy troupe Monty
Python’s Flying circus.

Python is a general purpose programming language – That means you can use Python to write code for
any programming task. Python is not intended to
work on special area such as web programming. It
can be used with web, enterprise, 3D, CAD etc.

Python is interpreted - That means Python code is translated and executed

by an interpreter, one statement at a time.

Python is an object oriented programming language – Data in Python are objects created from classes.

• Features

There are a lot of features provided by python programming language.

I. Easy to Use:

Python is easy to very easy to use and high level language. Thus it is programmer-friendly

II. Expressive Language:

Python language is more expressive. The sense of expressive is the code is easily understandable.

III. Interpreted Language:

Python is an interpreted language i.e. interpreter executes the code line by line at a time. This
makes debugging easy and thus suitable for beginners.

IV. Cross-platform language:

Python can run equally on different platforms such as Windows, Linux, Unix , Macintosh etc.
Thus, Python is a portable language.

V. Free and Open Source:

Python language is freely available (www.python.org).The source-code is also available.

Therefore it is open source.

VI. Object-Oriented language:

Python supports object oriented language. Concept of classes and objects comes into existence.

VII. Extensible:
It implies that other languages such as C/C++ can be used to compile the code and thus it can be
used further in your python code.

VIII. Large Standard Library:

Python has a large and broad library.

IX. GUI Programming:

Graphical user interfaces can be developed using Python.

X. Integrated:

It can be easily integrated with languages like C, C++, JAVA etc.

• Versions

Python Version Released Date

Python 1.0 January 1994

Python 1.5 December 31, 1997

Python 1.6 September 5, 2000

Python 2.0 October 16, 2000

Python 2.1 April 17, 2001

Python 2.2 December 21, 2001

Python 2.3 July 29, 2003

Python 2.4 November 30, 2004

Python 2.5 September 19, 2006

Python 2.6 October 1, 2008

Python 2.7 July 3, 2010

Python 3.0 December 3, 2008

Python 3.1 June 27, 2009

Python 3.2 February 20, 2011

Python 3.3 September 29, 2012

Python 3.4 March 16,2014

Python 3.5 September 13,2015

Python 3.6 December 23,2016

Two versions of Python are currently coexistent: Python 2 and Python 3. The programs written in Python
3 will not run in Python 2. Python 3 is a newer version, but it is not backward-compatible with Python 2.
This means that if you write a program using the Python 2 syntax, it may not work with a Python 3
Python provides a tool that automatically converts code written in Python 2 into syntax Python 3 can use..
This book explains the concepts of programming using Python 3.

• Who uses Python Today?

In general, though, Python enjoys a large user base and a very active developer community.
It is generally considered to be in the top 5 or top 10 most widely used programming languages in the
world today (its exact ranking varies per source and date).
Because Python has been around for over two decades and has been widely used, it is also very stable
and robust.
Besides being leveraged by individual users, Python is also being applied in real revenue generating
products by real companies.
For instance, among the generally known Python user base:

• Google makes extensive use of Python in its web search systems.

• The popular YouTube video sharing service is largely written in Python.
• The Dropbox storage service codes both its server and desktop client software primarily in Python.
• The Raspberry Pi single-board computer promotes Python as its educational language.
• EVE Online, a massively multiplayer online game (MMOG) by CCP Games, uses Python broadly.
• The widespread Bit Torrent peer-to-peer file sharing system began its life as a Python program.
• Industrial Light & Magic, Pixar, and others use Python in the production of animated movies.
• ESRI uses Python as an end-user customization tool for its popular GIS mapping products.
• Google’s App Engine web development framework uses Python as an application language.
• The Iron Port email server product uses more than 1 million lines of Python code to do its job.
• Maya, a powerful integrated 3D modeling and animation system, provides a Python scripting API.
• The NSA uses Python for cryptography and intelligence analysis.
• iRobot uses Python to develop commercial and military robotic devices.
• The Civilization IV game’s customizable scripted events are written entirely in Python.
• The One Laptop per Child (OLPC) project built its user interface and activity model in Python.
• Netflix and Yelp have both documented the role of Python in their software infrastructures.
• Intel, Cisco, Hewlett-Packard, Seagate, Qualcomm, and IBM use Python for hardware testing.
• JPMorgan Chase, UBS, Getco, and Citadel apply Python to financial market forecasting.
• NASA, Los Alamos, Fermilab, JPL, and others use Python for scientific programming tasks.

• What are the various Applications of Python ?

▪ Systems Programming

Python’s built-in interfaces to operating-system services make it ideal for writing portable,
maintainable system-administration tools and utilities (sometimes called shell tools).

▪ GUIs

Python’s simplicity and rapid turnaround also make it a good match for graphical use interface
programming on the desktop.

▪ Internet Scripting
Python comes with standard Internet modules that allow Python programs to perform a wide
variety of networking tasks, in client and server modes.
▪ Database Programming

For traditional database demands, there are Python interfaces to all commonly used relational
database systems—Sybase, Oracle, Informix, ODBC, MySQL, PostgreSQL, SQLite, and more.
The Python world has also defined a portable database API for accessing SQL database systems
from Python scripts, which looks the same on a variety of underlying database systems

▪ Rapid Prototyping

To Python programs, components written in Python and C look the same. Because of this, it’s
possible to prototype systems in Python initially, and then move selected components to
compiled language such as C or C++ for delivery. Unlike some prototyping tools, Python doesn’t
require a complete rewrite once the prototype has solidified. Parts of the system that don’t require
the efficiency of a language such as C++ can remain coded in Python for ease of maintenance
and use.

▪ Numeric and Scientific Programming

Python is also heavily used in numeric programming—a domain that would not traditionally have
been considered to be in the scope of scripting languages, but has grown to become one of
Python’s most compelling use cases

▪ Python is commonly applied in more domains than can be covered here. For example, you’ll
find tools that allow you to use Python to do:

• Game programming and multimedia with pygame, cgkit, pyglet, PySoy, Panda3D, and others
• Serial port communication on Windows, Linux, and more with the PySerial extension
• Image processing with PIL and its newer Pillow fork, PyOpenGL, Blender, Maya, and more
• Robot control programming with the PyRo toolkit
• Natural language analysis with the NLTK package
• Instrumentation on the Raspberry Pi and Arduino boards
• Mobile computing with ports of Python to the Google Android and Apple iOS platforms