Skip to main content
  1. Study/
  2. Computer Programming/
  3. Logic and Algorithms/

Logic and Algorithm #02: Algorithm Concepts and Data Concepts

·6 mins· loading · loading ·
Azriel Fidzlie
Author
Azriel Fidzlie
Hello, my name is Azriel Fidzlie 👋. I am a {full-stack} developer, student, and {designer} who lives for enjoying a cup of tea 24/7 ☕️.
Table of Contents
Logic and Algorithms Chapters - This article is part of a series.
Part 2: This Article

Algorithm Concepts
#

1. Variable Algorithm
#

Is a Variable whose value is NOT a constant (always changing – according to the CURRENT Variable condition)

Syntax : P = Q

Algorithm : P <- Q

Meaning : That the Value of P is assigned the Value of Q, the Value of P will be EQUAL TO the value of Q, & the Value of Q REMAINS

2. Exchange Algorithm
#

Serves to exchange the respective contents of Variables such that the Value of each Variable will change/exchange.

Example of Algorithm Problem
#

Problem
#

Given P=10, Q=15 and R=5. Given Algorithm P=Q, Q=R, what are the Values of P, Q, R now?

Answer
#

  1. P = Q: In this step, the value of Q (15) will be copied into the variable P. Thus, P is now 15.

  2. Q = R: In this step, the value of R (5) will be copied into the variable Q. Thus, Q is now 5.

    The final result is:

  • The value of P is 15
  • The value of Q is 5
  • The value of R remains 5 (does not change in the given algorithm)

Algorithm Analysis
#

  1. A bunch of 12 matchsticks can form boxes as below. The question is, move two of these matchsticks to form four boxes.
img

By moving the two matchsticks at the bottom, as below

img
  1. There are three matchsticks below, how to form the number 6 without breaking them
img

Answer: The three matchsticks will form the Roman numeral 6

img
  1. Budi never skips his class, but he never did any assignments for this past year. His only job is talking and Budi also never took the semester exam, Budi is also not an achieving student. Why does Budi never get a warning from the school? (what do you think is the answer)

    The answer: Because Budi is a teacher.

    Explanation: Budi never did assignments but created assignments, his job is only talking to explain the lesson material in the class so Budi will never take the semester exam.

  2. What is the minimum number of lines to cover all the dots below with the condition that to make the lines they must not be broken:

img
  1. Algorithm for Exchanging Container Contents For Trial Practice Student Exchange Bringing 2 Glasses of water of different colors and 1 Empty glass

    Given two containers, A and B; container A contains a red solution, container B contains a blue solution

    Write pseudocode to exchange the contents of the two containers such that container A contains a blue solution and container B contains a red solution.

img

DESCRIPTION :

  • Pour the solution from container A into container C.
  • Pour the solution from container B into container A.
  • Pour the solution from container C into container B.
img

Data Types in Python
#

Data TypeDescription
BooleanHas two values, namely true has value 1 and false has value 0
StringConsists of characters/sentences in the form of letters, numbers, etc. (enclosed by " or ’ signs)
IntegerExpresses integers
FloatExpresses numbers that have a comma
ComplexExpresses pairs of real and imaginary numbers
ListSequence data that stores various data types, its contents can change
TupleSequence data that stores various data types, but its contents cannot change
HexadecimalExpresses numbers in hexadecimal format
DictionarySequence data that stores various data types in the form of pointer and value pairs

Examples of data types in Python
#

#Boolean data type
print(True)
#String data type
print("Learning Python is fun...")
#Integer data type
print(20)
#Float data type
print(3.14)
#Complex data type
print(5j)
Running Result:
True
Learning Python is fun...
20
3.14
5j

List Data Type
#

Is an array containing a collection of non-homogeneous types.

#list data type
words = ["Learning", "Python", "at", "School Programs"]
numbers = [10, 50, 100, 1000]
mixed = ["Learning", 100, 7.99, True]

#print
print(words)
print(numbers)
print(mixed)
Running Result:
['Learning', 'Python', 'at', 'School Programs']
[10, 50, 100, 1000]
['Learning', 100, 7.99, True]

Tuple Data Type
#

The tuple data type is almost similar to list, the difference is its members cannot be changed after being declared. Tuple uses parentheses and members are separated by commas.

#tuple data type
words = ("Learning", "Python", "at", "School Programs")
numbers = (10, 50, 100, 1000)
mixed = ("Learning", 100, 7.99, True)

#print
print(words)
print(numbers)
print(mixed)
Running Result:
('Learning', 'Python', 'at', 'School Programs')
(10, 50, 100, 1000)
('Learning', 100, 7.99, True)

Dictionary Data Type
#

The general form of dictionary data type in Python programming: Variable_name = {“key1”: “value1”, “key2”: “value2”, “key3”: “value3”}

#Dictionary data type
data = {1:"Learning",
2: ["C++", "Python"],
"At Campus": "School Programs",
"give up" : False,
"Year": 2021}
print(data)
Running Result:
{1: 'Learning', 2: ['C++', 'Python'], 'At Campus': 'School Programs', 'give up': False,
'Year': 2021}

Arithmetic & Mathematical Operators
#

OperatorDescription
+Addition
-Subtraction
*Multiplication
/Division
%Modulus (remainder)
**Exponentiation
//Division where the result is an integer

Examples of Arithmetic and Mathematical Operators
#

>>> 1+2
3
>>> 8-12
-4
>>> 4*5
20
>>> 42/7
6.0
>>> 9%2
1
>>> 5**2
25
>>> 10//3
3

Comparison Operators
#

OperatorDescription
>Greater than
<Less than
==Equal to
!=Not equal to
<=Less than or equal to
>=Greater than or equal to

Examples of Comparison Operators
#

>>> 10>5
True
>>> 8<6
False
>>> 10==10
True
>>> 5!=6
True
>>> 6<=6
True
>>> 8>=3
True

Bitwise Operators
#

OperatorDescription
&AND
lOR
~NOT
^XOR
«Shift bits left
»Shift bits right

AND Operator
#

The AND operator will be false (0) if the value of all its operands or one of them is false (0), and will be true (1) if both operands are true (1).

Operand 1Operand 2Output
000
010
100
111

OR Operator
#

The OR operator will produce output: If one operand or both operands are true (1) it will produce output true (1), if both operands are false (0) then it will produce output false (0).

Operand 1Operand 2Output
000
011
101
111

XOR Operator
#

The operation result using the XOR operator, namely:

  • If the bits being compared have different values, for example 1 (true) and 0 (false) then the output is 1 (true).
  • If the bits being compared have the same value, for example 1 (true) and 1 (true) or 0 (false) and 0 (false) then the output is 0 (false).
Operand 1Operand 2Output
000
011
101
110

Concatenating String Values
#

In Python Programming to concatenate string values in a program is as follows:

#Concatenation of two strings
word1 = "Learning Python Programming Language "
word2 = "Is Very Fun"

# Display the values of word1 and word2
print("Word1: ",word1)
print("Word2: ",word2)

# Concatenate word1 and word2
combine = word1 + word2
print("Result of Concatenating word1 and word2")
print(combine)
Running Result:
Learning Python Programming Language Is Very Fun

Len Function
#

To count the number of characters use the len() function

#Len Function
#To Count Character Length
word = "Learning Python Programming Language"
number_of_characters=len(word)
print(number_of_characters)
Running Result:
36

index() Function
#

To find out the character position in a sentence.

#index function
word = 'Aisyah Zahra'

#where is the Z character position
print (word.index('Z'))

#where is the r character position
print (word.index('r'))
Running Result:
7
10
Logic and Algorithms Chapters - This article is part of a series.
Part 2: This Article

Related