0% found this document useful (0 votes)
7 views

Data Structure and Algorithm Reviewer 1

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Data Structure and Algorithm Reviewer 1

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

DATA STRUCTURE AND ALGORITHM ADVANTAGES

REVIEWER
Compilation - execution of the translated code is
How does computer work? - A program makes usually faster. Only the user has to have the
a computer usable, can perform very complex compiler the end user may use the code without
task, but not innate (not built in). It should have a it.
hardware and software (software communicates
Interpretation – you can run the code as soon as
with hardware in program)
you complete it. No additional phases of
4 Basic units of computer - Input to CPU translation.
(processor) to Storage then store to memory then
DISADVANTAGES
output.
Software - set of program (set of instructions), Compilation – time consuming
program for processing - don’t expect interpretation to ramp up your code
Binary - language of machine Programming to high speed.

Language - derived from natural language. Python - interpreted language, scripting


language, scripts. Created by Guido van Rossum
Translates the language to the machine
Instruction List - complete set of known - a widely used, interpreted, object oriented and
high level programming language with dynamic
commands. It what makes programming language
semantics used for general purpose programming
What makes a language?
- easy to learn, to teach, to use, to understand
- an Alphabet (letter only) (set of symbols use to (examples in the services cloud storage)
make words)
Goal of Guido for python:
- a Lexis (set of words that the language offers its
- an easy and intuitive language just as powerful
users)
as does of the major competitors
- a Syntax (format, set of rules use to determine if
a certain string of words offers a valid sentence) - open source of anyone can contribute the its
development
- Semantics (set of rules that determines that a
- code that is understandable as playing English
certain phrase makes sense)
- suitable for everyday task allowing for short
A code should be: alphabetically, lexically,
development times.
syntetically, semantically correct.
PRINT FUNCTION
Machine vs High-level language - high-level is
readable and easy to understand (source code) argument
while machine language (example c++).
print ("Hello, 2CPE") - word print - opening
Machine code - a low level programming parenthesis - quotation mark - line of text -
language consisting of binary digits/bits that the another quotation mark - closing parenthesis
computer reads and understands.
FUNCTION NAME - cause some effect (display) -
Source file – a file containing a program in a evaluate value (what inside of the parenthesis) -
high-level programming language return it as the function result. It came from the
python itself, module, and your code.
Compilation vs Interpretation - both are doing
translation FUNCTION ARGUMENT - what inside the
parenthesis. It may have an effect(uncertain),
Compilation - translated once by getting a file
result, argument(s).
containing the machine code.
Interpretation - You can translate the source FUNCTION INVOCATION - formed by the
functioning parenthesis and arguments
program each time it has to be run. Reads source
codes in western culture way.
Ex: computer file – a piece of text, so the source
code is usually placed in text files.
EFFECTS, ARGUMENTS AND VALUES String – alphabet letters/words/sentences - use
RETURN to process text - single and double quotation mark
print() function cause: Integers - kind, range are called type - devoid of
fractional part
- takes its arguments - converts them into human-
readable forms - sends the resulting data to the Floating-points - has decimals -
output device. fractional/decimal format - all about dot/point.
- it expects: any argument We only use scientific notations for floats not
integers
Instructions - The program involves the print
function twice - no subsequent instruction is Octal value - use 0-7 - preceded with 0O or 0o -
executed until the previous one is completed. treated as octal value
ESCAPE AND NEW LINE CHARACTERS EX CODE: print(0o123) output = 83
Python sees one character, not two ( \n ) Hexadecimal - if an integer is preceded by and
0x (Zero-X) it will be treated as a hexadecimal
Backslash ( \ ) – called the escape character
value (0x or 0X (0-9))
when used inside strings (alphabet letters/words).
Letter “n” is placed after backslash which EX CODE: print(0x123) output = 291
corresponds the word newline
FOR OCTA AND HEXA VALUES
EX CODE:
print(0o123)
print("the itsy bitsy spider\nclimbed out the
Sa 3 magstart (pinakadulong number)
waterspout")
(Base na ieexponent is 8 since octa siya)
print("down came the rain\nand wash the spider
out") 3x8⁰ (count ng exponent starts with 0 and so on)
print('DOESN\'T') – used single quotation mark, 2x8¹
use \ for the python to know that the next
character is still part of the argument 1x8²

Multiple arguments - can put numbers - comma - Note: any digit raise to 0 is equal to 1
signifies another argument. - Add lahat ng na-compute
KEYWORD ARGUMENTS - Same goes sa hexa papalitan ng 16 yung
end - doesn’t go to another line ieexponent

sep - separate lines goes to the next line E or e - is exponent which is concise record of the
phrase times ten to the power of....
end and sep needs to be in the end of the line
- to get a high number we use E to get it (1billion
comma (,) - space = 1E9)
EX CODE: note: exponent values has to be integer base may
print("LAB", "KA", "BA") – uses comma be either integer or float
Boolean (True or False) – can’t be use if the
print("LAB","KA","BA?", sep="***",end="...")
start is in lowercase - (True or False = / true or
print("LAB AKO", end="!!!") false = X) - boolean always display the larger
value
KEYWORD ELEMENTS
(True > False = True) true = 1 false = 0
- identifying elements, an equal sign, value
assigned to that argument Basic operator - symbol of the programming
language
Literals - data itself - whose values are
determined by the literal itself (ex: 123) (ex: c) - - + (addition)
use to encode data and to put them into your
code.
- - (subtraction)(it can change the sign of the - the name of the variable must not be any
number unary way) (binary – exact) Python’s reserved words.
- / (division) EX VARIABLES (correct)
- * (multiplication) - MyVariable, i, I, t34, Exchange_Rate, counter,
days_to_christmas, The
- // (floor division – exact value/rounded off)
NameIsTooLongAndHardlyReadable, _
ex: 0.5 = 0, -0.5 = -1 KEYWORDS/RESERVED WORDS OF
- % (remainder/modulo) PYTHON:

ex: 6%4 = 2 (4-2=2) minus pag malapit lang - False, None, True, and, as, assert, break, class,
14%4=2 (14//4=3 then 3x4=12 then 12-14=2) continue, def. del, elif, else, except, finally, for,
when the divisor is bigger then the dividend (first from, global, if, import, in, is, lambida, nonlocal,
term) is the answer. not, or, pass, raise, return, try, while, with, yield

- ** (exponent)
LIST OF PROPERTIES (ranking/similar to HOW TO CREATE A VARIABLE
PEMDAS but in python way)
var = 1
1. **
account_balance = 1000.0
2. +, - (unary)
client_name = 'Juan Dela Cruz'
3. /, *, %
print(var, account_balance, client_name)
4. +, - (binary)
print(var)
BINDINGS - determines the order of the
operation
SECTION REVIEW
- when using remainder(%) its left to right
- Literals are notations for representing some
- when using exponent(**) its right to left
fixed values in code.
- when in the same priority its left to right - The binary system is a system of numbers that
EXAMPLE: employs 2 as the base.

print ((5*((25%13)+100)/(2*13))//2) - Integers (or simply ints) are one of the numerical
types supported by Python.
25%13 = 1 x 13 = 25 – 13 = 12
- Floating-point numbers (or simply floats) are
2*13 = 26 another one of the numerical types supported by
print ((5*(12+100)) / (26)//2) Python

print ((5*(112)) / (26)//2) = print ((5*(4.0)//2) - To encode an apostrophe or a quote inside a


string, you can either use the escape character,
print ((20.0//2)) = 10.0 or open and close the string using an opposite set
of symbols to the ones you wish to encode, to
Variable Names (Rules)
encode an apostrophe, and to encode a (double)
- the name of the variable must be composed of quote.
upper-case or lower-case letters, digits, and
- Boolean values are the two constant objects
character _ (underscore)
True and False used to represent truth values (in
- the name of the variable must begin with a numeric contexts 1 is True, while 0 is False.
letter;
- the underscore character is a letter;
- upper- and lower-case letters are treated as
different
MIDTERMS PRIORITY OPERATOR
COMPARISON 1 +, - UNARY
- A programmer writes a program and the
program asks questions. 2 **

- A computer executes the program and provides 3 *, /, //, %


the answers
4 +, - BINARY
EQUALITY OPERATOR
- (==) equal equal operator 5 <=, >, >=

- Comparing two values to determine if both 6 ==, !=


values are actually the same
EXAMPLE: 2==2, 1==1, 1==2
EXAMPLE:
INEQUALITY OPERATOR
Writing a two-line program that takes n and
- not equal to operator (!=) checks it if its less than 100:
- Comparing two values to determine if both n = int(input("Enter a number: ")
values are actually not the same
print(n >= 100)
EXAMPLE:
RESULT: If input is 100 TRUE otherwise FALSE
var = 0
- Equality and Inequality are left side binded -
print(var != 0) reading it from left to right Conditions
RESULT: False - Answerable by yes or no To make such
var = 1 decisions

print(var != 0) - Python offers a special instruction called


conditional instruction (or conditional
RESULT: True statement)
GREATER THAN (>) STRICT ELEMENTS OF THIS CONDITIONAL
STATEMENT:
- Number to the left of the sign has a higher value
than the one on the right  The if keyword;
- True confirms it; False denies it  One or more white spaces;
 An expression whose value will be
GREATER THAN OR EQUAL TO (>=) interpreted soleyl in terms of True and
False
- Binary operation with left-sided binding, and
 A colon followed by a newline;
priority is greater than that shown by == and !=
 An indented instruction or set of
EXAMPLE: instructions;
o By using four spaces of
var = 1
indentation
print(var >= 0) o By using the tab character

RESULT: True HOW DOES THE STATEMENT WORK?


- If the true_or_not expression represents the
truth, the indented statements will be executed;
- If the true_or_ not expression does not
represent the truth, the indented statements) will
be omitted, and the next executed instruction
will be the one after the original indentation
level.
EXAMPLE: ELIF STATEMENT
age = 19 - Used to check more than just one condition,
and to stop when the first statement which is
is_adult = age >= 18
true is found
if is_adult: - It needs to be sorted well/arranged sequently
print("Adult") EXAMPLE:
RESULT: Adult (met the conditions in the code),
x=5
otherwise it won’t print anything.
y = 10
IF ELSE STATEMENT
z = 15
 If the condition evaluates to True, the
perform if_condition true statement is if x > y:
executed, and the conditional
print("x is greater than y")
statement comes to an end
 If the condition evaluates to False, the elif x > z:
perform if condition_false statement is
executed, and the conditional statement print(x is greater than z")
comes to an end elif x < y:
NESTED IF-ELSE STATEMENT print("x is less than y")
 Where the instruction is placed after the if elif x < z:
is another if
 Multiple conditions print("x is less than z")
 Two important points: else:
o This use of if statement is known
as nesting; print("x is the largest number")
o Consider how indentation RESULT: x is less than y
improves readability, and makes
the code easier to understand and - You must not use else without preceding if;
trace
- else is always the last branch of the cascade,
EXAMPLE: regardless whether you've used elif or not;
if condition_is_true: - else is optional part of the cascade, and may
be omitted;
if another_condition:
- if there is an else branch in the cascade, only
perform_true_code one of all the branches is executed
else: - If there's no else branch, it's possible that
perform_if_False none of the available branches is executed

else: EXAMPLE:

if next_condition: Enter: **

next_true_condition Exponential

else: Enter: 6

next false condition Not an operator


LOOPS counter = 5
- Condition after the while while counter != 0:
- Every instruction inside the while is executed print("Inside the loop.", counter)
EXAMPLE: counter - = 1
while: print("Outside the loop.", counter)
instruction RESULT:
while conditional_expression: Inside the loop. 5 Inside the loop. 4 Inside the
loop. 3 Inside the loop. 2 Inside the loop. 1
instruction_one
Outside the loop. 0
instruction_two FOR LOOP
instruction_three - Designed to do more complicated tasks - it
. can "browse" large collections of data item by
item
.
for i in range(100):
instruction_n
# do_something()
IT’S IMPORTANT TO REMEMBER THAT:
Pass
- If you want to execute more than one
statement inside one while loop, you must NEW ELEMENTS WE SHOULD KNOW ABOUT:
indent all instructions in the same way;
- The for keyword opens the for loop;
- An instruction or set of instructions executed
- Any variable after the keyword is the control
inside the while loop is called the loop's body variable of the loop;
- If the condition is False, as early as when it is
- The in keyword introduces a syntax element
tested for the first time, the body is not executed describing the range of possible values being
even once assigned to the control variable
- The body should be able to change the - The range() function is responsible for
condition's value if the condition is True at the generating all the desired values of the control
beginning, the body might run continuously to variable;
infinity
EXAMPLE:
INFINITE LOOP
for i in range(10):
- Also called endless loop, is a sequence of
instructions in a program which repeat print("The value of i is currently", i)
indefinitely
RESULT:
EXAMPLE:
The value of i is currently 0
secret = "chupacabra" The value of i is currently 1
The value of i is currently 2
word = str(input("Whats the magic word? "))
The value of i is currently 3
while word != "chupacabra": The value of i is currently 4
The value of i is currently 5
if word == "chupacabra": The value of i is currently 6
break The value of i is currently 7
The value of i is currently 8
secret = word The value of i is currently 9
word = str(input("Whats the magic word?
 The pass keyword inside the loop body -
"))
it does nothing at all; it's an empty
print("You've successfully left the loop.") instruction
 The range() function invocation may be while and else
equipped with two arguments, not just
- The loops may have an else too like ifs
one:
o If the set generated by the range() - The loop's else branch is always executed
function is empty, the loop won't once, regardless of whether the loop has
execute its body at all. entered its body or not.

EXAMPLE:
for i in range(2, 8):
print("The value of i is currently", i)

RESULT:
The value of i is currently 2
The value of i is currently 3
The value of i is currently 4
The value of i is currently 5
The value of i is currently 6
The value of i is currently 7

BREAK AND CONTINUE


- As a developer, you could be faced with the
following choices:
 it appears that it's unnecessary to
continue the loop as a whole; you should
refrain from further execution of the
loop's body and go further;
 it appears that you need to start the next
turn of the loop without completing the
execution of the current turn.

These two instructions are:

break - exits the loop immediately, and


unconditionally ends the loop's operation

continue - behaves as if the program has


suddenly reached the end of the body

You might also like