Differences

This shows you the differences between two versions of the page.

Link to this comparison view

ewis:laboratoare:02 [2020/02/28 18:59]
alexandru.predescu
ewis:laboratoare:02 [2023/03/15 17:40] (current)
alexandru.predescu [Data Structures]
Line 1: Line 1:
 ===== Lab 2 - Data Structures. Functions. Modules ===== ===== Lab 2 - Data Structures. Functions. Modules =====
-===== Laboratorul 02. ===== 
- 
  
 ==== Functions ==== ==== Functions ====
Line 26: Line 24:
 </​code>​ </​code>​
  
-**T1 (2p)** Having a list of numbers, write a function to calculate the sum of all elements in the list.+**T1 (1p)** Having a list of numbers, write a function to calculate the sum of all elements in the list.
 Tip: List given as input, using a for loop Tip: List given as input, using a for loop
 </​note>​ </​note>​
Line 34: Line 32:
  
 import math import math
-import module1 as m 
- 
-m.hello_name("​Alex"​) 
- 
-my_list = [1, 2, 3, 4, 5] 
-print(my_list) 
-my_list.append(6) 
-print(my_list) 
-my_other_list = [7, 8, 9, 10] 
-my_list.extend(my_other_list) 
-print(my_list) 
-my_list.sort(reverse=True) 
-print(my_list) 
- 
- 
 list1 = [1, 3, 4, 5, 8] list1 = [1, 3, 4, 5, 8]
 list3 = [1, 3, -4, 5, -8, -12] list3 = [1, 3, -4, 5, -8, -12]
Line 55: Line 38:
  
 # function to compute the arithmetic mean of a list # function to compute the arithmetic mean of a list
- 
  
 def my_mean(v): def my_mean(v):
- s_elem = sum(v)+    ​s_elem = sum(v)
     n_elem = len(v)     n_elem = len(v)
-    return s_elem/​n_elem+    return s_elem / n_elem
  
 # function to compute the weighted arithmetic mean of a list # function to compute the weighted arithmetic mean of a list
- 
  
 def my_pmean(v, p): def my_pmean(v, p):
Line 70: Line 51:
     s_lp = 0     s_lp = 0
     while i < len(v):     while i < len(v):
-        s_lp += v[i]*p[i]+        s_lp += v[i] * p[i]
         i += 1         i += 1
-    return s_lp/s_pond+    return s_lp / s_pond
  
 # function to compute the variance and standard deviation of a list # function to compute the variance and standard deviation of a list
- 
  
 def my_stdvar(v):​ def my_stdvar(v):​
Line 83: Line 63:
     for elem in v:     for elem in v:
         s += (elem - m)**2         s += (elem - m)**2
-    return s/n, math.sqrt(s/​n) +    return s / n, math.sqrt(s / n)
- +
-# function to compute the  L2-norm nornalization for list+
  
 +# function to compute the L2-norm normalization for list
  
 def l2norm(v): def l2norm(v):
     new_list = []     new_list = []
     s = 0     s = 0
 +    # compute the L2 norm
     for elem in v:     for elem in v:
         s += elem**2         s += elem**2
     s = math.sqrt(s)     s = math.sqrt(s)
 +    ​
 +    # list normalization using the L2 norm
     for elem in v:     for elem in v:
-        new_elem = elem/s+        new_elem = elem / s
         new_list.append(new_elem)         new_list.append(new_elem)
 +        ​
 +    # TODO: same task with list comprehension ​   ​
 +    ​
     return new_list     return new_list
     ​     ​
 </​code>​ </​code>​
  
-**T2 (2p)** Use the functions to compute the arithmetic mean, weighted arithmetic mean, variance, standard deviation and L2-norm normalization of the list+**T2.1 (1p)** Use the functions to compute the arithmetic mean, weighted arithmetic mean, variance, standard deviation and L2-norm normalization of the list 
 + 
 +**T2.2 (2p)** Implement the functions using list comprehension,​ without using for loops
  
 </​note>​ </​note>​
Line 111: Line 98:
 <​note>​ <​note>​
  
-//​module1.py//​+**//​module1.py//​**
  
 <code python> <code python>
 def hello_name(name):​ def hello_name(name):​
     print("​Hello " + name)     print("​Hello " + name)
-    return 
 </​code>​ </​code>​
  
-//main.py//+**//main.py//**
  
 <code python> <code python>
Line 125: Line 111:
 from module1 import hello_name from module1 import hello_name
 hello_name("​Alex"​) hello_name("​Alex"​)
 +
 # import all resources from a module # import all resources from a module
 from module1 import * from module1 import *
 hello_name("​Alex"​) hello_name("​Alex"​)
 +
 # import the module # import the module
 import module1 import module1
 module1.hello_name("​Alex"​) module1.hello_name("​Alex"​)
 +
 # import the module using an alias # import the module using an alias
 import module1 as m import module1 as m
Line 136: Line 125:
 </​code>​ </​code>​
  
-**T3 (2p)** Create two Python files (.py) as in the example. Run the program //main.py// and check the output in the console+**T3 (2p)** Create two Python files (.py) in the same project folder, ​as shown in the example. Run the program //main.py// and check the output in the console
  
 </​note>​ </​note>​
Line 146: Line 135:
 === Lists === === Lists ===
  
-List is one of the most frequently used and very versatile datatype used in Python. As opposed to more traditional arrays, used to store elements having the same data-type, Python lists do not have such constrain. Lists are discussed in the previous lab ([[ewis:​laboratoare:​01|]]),​ showing how to create, read, update and delete (CRUD) contained elements using basic operators. In Python, the List type also contains some useful methods: append, extend, insert, remove, pop, clear, index, count, sort, reverse, copy+List is one of the most frequently used and very versatile datatype used in Python. As opposed to more traditional arrays, used to store elements having the same data-type, Python lists do not have such constrain. Lists are discussed in the previous lab ([[ewis:​laboratoare:​01|]]),​ showing how to create, read, update and delete (CRUD) contained elements using basic operators. In Python, the List type also contains some useful methods: append, extend, insert, remove, pop, clear, index, count, sort, reverse, copy.
  
 <​note>​ <​note>​
Line 152: Line 141:
 my_list = [1, 2, 3, 4, 5] my_list = [1, 2, 3, 4, 5]
 print(my_list) print(my_list)
 +
 # add an element to the list # add an element to the list
 my_list.append(6) my_list.append(6)
 print(my_list) print(my_list)
 +
 # combine lists # combine lists
 my_other_list = [7, 8, 9, 10] my_other_list = [7, 8, 9, 10]
 my_list.extend(my_other_list) my_list.extend(my_other_list)
 print(my_list) print(my_list)
 +
 # sort list (descending) # sort list (descending)
 my_list.sort(reverse=True) my_list.sort(reverse=True)
 print(my_list) print(my_list)
 +
 +# check if an element is found in the list
 +# (python specific, normally used with sets or dictionaries)
 +print(1 in my_list)
 +print(10 in my_list)
 </​code>​ </​code>​
 </​note>​ </​note>​
Line 171: Line 168:
  
 <​note>​ <​note>​
-Create, access, check membership+**Create, access, check membership**
  
 <code python> <code python>
Line 186: Line 183:
 </​code>​ </​code>​
  
-Modify+**Modify**
  
 <code python> <code python>
Line 210: Line 207:
 </​code>​ </​code>​
  
-Operations (set theory)+**Operations (set theory)**
  
 <code python> <code python>
Line 246: Line 243:
 **T4 (4p)** The set is a collection of unordered (and unique) elements. Use this to your advantage to remove duplicates from a list **T4 (4p)** The set is a collection of unordered (and unique) elements. Use this to your advantage to remove duplicates from a list
  
-Tip+Hint
-  *you can create a set from a list +  *you can create a set from a list: ''​s = set([1,​2,​3])''​ 
-  *you can create a list from a set+  *you can create a list from a set: ''​v = list({1, 2, 3})''​
  
 </​note>​ </​note>​
Line 281: Line 278:
 # looping through a dictionary: # looping through a dictionary:
 for key in d: for key in d:
- print(key) ​ # print only key +   print(key) ​ # print only key 
-    print(key, d[key]) ​ # print key and value d[key]+   ​print(key, d[key]) ​ # print key and value d[key]
  
  
Line 300: Line 297:
 else: else:
     print('​not found'​)     print('​not found'​)
 +
 +</​code>​
 +
 +<code python>
  
 # counting the number of times a letter appears in a string: # counting the number of times a letter appears in a string:
Line 319: Line 320:
 Apriori algorithm, a classic algorithm, is useful in mining frequent itemsets and relevant association rules. Usually, you operate this algorithm on a database containing a large number of transactions. Apriori algorithm, a classic algorithm, is useful in mining frequent itemsets and relevant association rules. Usually, you operate this algorithm on a database containing a large number of transactions.
 </​note>​ </​note>​
 +
 +<​note>​
 +
 +**T5 (4p - bonus)** You have a list of numbers. Write a program to show the frequency of each number as the number of times that number is found in the list.
 +
 +Hint:
 +  * Use dictionaries to keep track of each element
 +</​note>​
 +
 +**Resources**:​
 +
 +  * [[http://​mathworld.wolfram.com/​L2-Norm.html|L^2-Norm]]
 +  * [[http://​mathworld.wolfram.com/​StandardDeviation.html|Standard Deviation]]
 +  * [[https://​www.youtube.com/​watch?​v=WGlMlS_Yydk|Apriori Algorithm (Associated Learning) - Fun and Easy Machine Learning]]
 +
 +
  
  
ewis/laboratoare/02.1582909167.txt.gz · Last modified: 2020/02/28 18:59 by alexandru.predescu
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0