Docsity

Very good past papers, Study notes of Law

Toggle sidebar
bg1
1.1 Representing Algorithms
● An algorithm is a set of steps to solve a problem or perform a task.
● you can use flowcharts or pseudo code
● an equal sign (=) can only be used when comparing (in the diamond) whereas
everywhere else must use arrows (<—— ) [to assign to variables]
1.2 Efficiency of Algorithms
● An algorithm is more efficient if:
- It takes fewer steps
- takes less time
- uses less memory
1.3 Searching Algorithms
● Algorithms that look for a target item within a data structure (e.g. - a list).
● Needed to identify if a target item exists and which position it exists in.
● LINEAR SEARCH
pf3
pf4
pf5

Partial preview of the text

Download Very good past papers and more Study notes Law in PDF only on Docsity!

1.1 Representing Algorithms

● An algorithm is a set of steps to solve a problem or perform a task. ● you can use flowcharts or pseudo code ● an equal sign (=) can only be used when comparing (in the diamond) whereas everywhere else must use arrows (<—— ) [to assign to variables]

1.2 Efficiency of Algorithms

● An algorithm is more efficient if:

  • It takes fewer steps
  • takes less time
  • uses less memory

1.3 Searching Algorithms

● Algorithms that look for a target item within a data structure (e.g. - a list). ● Needed to identify if a target item exists and which position it exists in. ● LINEAR SEARCH

  • It checks every value in a list one by one to see if it matches the target value. -If it does, the comparisons will stop.(it keeps going until the value is found and if it isn’t it will go through the entire list to look for that value).
  • It does not require data to be sorted
  • Inefficient because it takes a long time as it has to check each item one at a time.
  • useful for a short list and if the data is unsorted ● BINARY SEARCH -It makes comparisons in a sorted list by repeatedly splitting the list in half and comparing the middle value to the target value. (Uses divide and conquer)
  • If it lands on the target value, it will stop making comparisons.
  • If the target value is smaller than the middle value then it will ignore the right side and only work with the left(vice versa).
  • very useful and efficient as it works quickly(Becasue it ignores irrelevant data)
  • a disadvantage is that it requires data to be sorted for it to work otherwise it cannot compare

1.4 Sorting Algorithms

2.1 Data Types

● Operators that combine or invert Boolean values such as : AND, OR, NOT.

2.6 Data Structures

● These are ways of organising and storing data so that it can be used efficiently: [e.g - arrays, lists] ● Choice of data structure affects how easily you can store, access, search, sort data. ● List = a data structure that stores multiple pieces of related data under one variable name in an ordered sequence. ● Each item in the list is called an element, and each element has a position number known as its index (starting from 0). ● Lists can be changed as items can be added, removed, or updated and you can access any item directly using its index. ● They are useful for storing data of the same or different data types, such as integers, strings, or both, in one place. ● USING LISTS… -Access an element —-> list[index] -Example: numbers[0] gives the first item. -Add an element at the end —-> append() -Example: numbers.append(5) -Insert at a specific position —> insert(index, value) -Example: numbers.insert(2, 10) -Remove an element by value —-> remove(value) -Example: numbers.remove(3) -Remove an element by index —-> pop(index) -Example: numbers.pop(1) -Changing an element ——>list[index] = new_value -Example: numbers[0] = 7

-Find the number of elements —-> len(list) -Example: len(numbers)

2.7 Input/Output

● Input() gets thee data and print() outputs the data in python.

2.8 String-handling Operations in a Programming Language

● Concatenate strings —> + -Example: "Hello " + "World" —> "Hello World" ● Get length —> len(string) -Example: len("Hello") —> 5 ● Access a character —> string[index] -Example: "Hello"[0] —> 'H' ● Slice a string —> string[start:end] -Example: "Hello"[1:4] —> 'ell' (FIRST NUMBER IS INCLUSIVE AND SECOND NUMBER AFTER THE COLON IS EXCLUSIVE) ● Change case —> upper(), lower() Example: "hello".upper() —> "HELLO" ● Find substring —> find(substring) Example: "Hello".find("e") —> 1 ● Replace substring —> replace(old, new) Example: "Hello".replace("H", "J") —> "Jello" ● Check if substring exists —> in Example: "e" in "Hello" —> True ● Strip whitespace —-> strip() Example: " hello ".strip() ——> "hello"

2.10 Structured Programming and Subroutines (Procedures and Functions)

● Helps make code easier to read, maintain, reuse and test. ● You can call it many times in your main program rather than rewriting it each time.