From 9548ae5e17e7d800f66bb5a3a0af6ba35ce0680a Mon Sep 17 00:00:00 2001 From: Claudio Maggioni Date: Thu, 21 Feb 2019 12:17:07 +0100 Subject: [PATCH] 2019-02-21 in class --- 1st.py | 16 ++++++++++++++++ notes.md | 34 ++++++++++++++++++++++++++++++++++ pair_equal.py | 19 +++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 1st.py create mode 100644 notes.md create mode 100644 pair_equal.py diff --git a/1st.py b/1st.py new file mode 100644 index 0000000..d4ee0ce --- /dev/null +++ b/1st.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python3 +# vim: set ts=4 sw=4 et tw=80: + +import sys + +def find(A, x): + i = 0 + while i < len(A): + if A[i] == x: + return i + i = i + 1 + return None + +if __name__ == "__main__": + A = [int(x) for x in sys.argv[1:]] + print(find(A[1:], A[0])) diff --git a/notes.md b/notes.md new file mode 100644 index 0000000..f2cac81 --- /dev/null +++ b/notes.md @@ -0,0 +1,34 @@ + + +# Complexity +General way to describe efficiency algorithms (linear vs exponential) +indipendent from the computer architecture/speed. + +## The RAM - random-access machine +Model of computer used in this course. + +Has random-access memory. + +### Basic types and basic operations +Has basic types (like int, float, 64bit words). A basic step is an operation on +a basic type (load, store, add, sub, ...). A branch is a basic step. Invoking a +function and returning is a basic step as well, but the entire execution takes +longer. + +Complexity is not measured by the input value but by the input size in bits. +`Fibonacci(10)` in linear in `n` (size of the value) but exponential in `l` +(number of bits in `n`, or size of the input). + +By default, WORST complexity is considered. + +## Donald Knuth's A-notation +A(c) indicates a quantity that is absolutely at most c + +Antonio's weight = (pronounced "is") A(100) + +## (big-) O-notation +f(n) = O(g(n)) + +*Definition:* if f(n) is such that f(n) = k * A(g(n)) for all _n_ sufficiently +large and for some constant k > 0, then we say that + diff --git a/pair_equal.py b/pair_equal.py new file mode 100644 index 0000000..2a2c571 --- /dev/null +++ b/pair_equal.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python3 +# vim: set ts=4 sw=4 et tw=80: + +import sys + +def contains_two_equal(A): + i = 0 + while i < len(A) - 1: + j = i + 1 + while j < len(A): + if A[i] == A[j]: + return True + j = j + 1 + i = i + 1 + return False + +if __name__ == "__main__": + A = [int(x) for x in sys.argv[1:]] + print(contains_two_equal(A))