From 91c41d467ce23a8770cefbe980037efc246f274b Mon Sep 17 00:00:00 2001 From: Tomasz Dwojak Date: Sat, 12 May 2018 13:16:05 +0200 Subject: [PATCH] Add examples to labs04 --- labs04/examples/06_execution_time.py | 37 ++++++++++++++++++++++ labs04/examples/25_ip2geolocation.py | 31 ++++++++++++++++++ labs04/examples/fib.py | 47 ++++++++++++++++++++++++++++ labs04/examples/myip.py | 11 +++++++ labs04/examples/wc.py | 39 +++++++++++++++++++++++ 5 files changed, 165 insertions(+) create mode 100755 labs04/examples/06_execution_time.py create mode 100755 labs04/examples/25_ip2geolocation.py create mode 100644 labs04/examples/fib.py create mode 100644 labs04/examples/myip.py create mode 100644 labs04/examples/wc.py diff --git a/labs04/examples/06_execution_time.py b/labs04/examples/06_execution_time.py new file mode 100755 index 0000000..7fa34a7 --- /dev/null +++ b/labs04/examples/06_execution_time.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +ExecutionTime + +This class is used for timing execution of code. + +For example: + + timer = ExecutionTime() + print 'Hello world!' + print 'Finished in {} seconds.'.format(timer.duration()) + +""" + + +import time +import random + + +class ExecutionTime: + def __init__(self): + self.start_time = time.time() + + def duration(self): + return time.time() - self.start_time + + +# ---- run code ---- # + + +timer = ExecutionTime() +sample_list = list() +my_list = [random.randint(1, 888898) for num in + range(1, 1000000) if num % 2 == 0] +print('Finished in {} seconds.'.format(timer.duration())) diff --git a/labs04/examples/25_ip2geolocation.py b/labs04/examples/25_ip2geolocation.py new file mode 100755 index 0000000..89ab872 --- /dev/null +++ b/labs04/examples/25_ip2geolocation.py @@ -0,0 +1,31 @@ +import csv +import requests + + +def get_address(): + """ + Given a CSV file, this function returns a list of lists + where each element (list) in the outer list contains the + row info from the csv file. + """ + all_addresses = [requests.get('http://ip.42.pl/raw').text.strip()] + return all_addresses + + +def get_geolocation(ip_address): + """ + Given a list of lists from `get_addresses()`, this function + returns an updated lists of lists containing the geolocation. + """ + print("Getting geo information...") + # update header + # get geolocation + for line in all_the_ip_address: + print("Grabbing geo info") + r = requests.get('https://freegeoip.net/json/{0}'.format(line)) + print([str(r.json()['country_name']), str(r.json()['city'])]) + + +if __name__ == '__main__': + all_the_ip_address = get_address() + get_geolocation(all_the_ip_address) diff --git a/labs04/examples/fib.py b/labs04/examples/fib.py new file mode 100644 index 0000000..5cf8223 --- /dev/null +++ b/labs04/examples/fib.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +Obliczenie n-tego wyrazu ciągu fibonacciego na dwa sposoby. +1. Naiwna rekurencja: podstawienie do wzoru. +2. Wersja z cachem: każdy wyraz jest obliczany dokładnie raz. +""" + +def naive_fibonacci(n): + if n <= 0: + return 0 + if n in [1,2]: + return 1 + return naive_fibonacci(n-1) + naive_fibonacci(n-2) + + +def cache_fibonacci(n, cache=None): + if cache is None: + cache = [None for i in range(n+1)] + cache[0] = 0 + cache[1] = cache[2] = 1 + return cache_fibonacci(n, cache) + else: + if cache[n] is not None: + return cache[n] + else: + cache[n] = cache_fibonacci(n-1, cache) + cache_fibonacci(n-2, cache) + return cache[n] + +def non_reccurent_fibonacci(n): + cache = [None for i in range(n+1)] + cache[0] = 0 + cache[1] = cache[2] = 1 + for i in range(2, n + 1): + cache[i] = cache[i-1] + cache[i-2] + return cache[n] + +for i in [5, 10, 15, 20, 30, 40]: + print("Naive fibonacci for ", i, ":", naive_fibonacci(i)) + +for i in [5, 10, 15, 20, 30, 40, 100]: + print("cache fibonacci for ", i, ":", cache_fibonacci(i)) + +for i in [5, 10, 15, 20, 30, 40, 100]: + print("no-recurrent fibonacci for ", i, ":", non_reccurent_fibonacci(i)) + diff --git a/labs04/examples/myip.py b/labs04/examples/myip.py new file mode 100644 index 0000000..14013ac --- /dev/null +++ b/labs04/examples/myip.py @@ -0,0 +1,11 @@ +#!/usr/bin/env python + +""" +This simple script fetches your IP as seen by web pages, and displays it. + +Execute this script locally like that: +$ curl -s https://raw.github.com/gist/3389407/myip.py | python +""" +import requests + +print(requests.get('http://ip.42.pl/raw').text) diff --git a/labs04/examples/wc.py b/labs04/examples/wc.py new file mode 100644 index 0000000..4dfc78c --- /dev/null +++ b/labs04/examples/wc.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +Implementacja narzedzia ``wc`` z linuksa (word counter). +Zwraca liczbę słów, znaków i linii. +""" + +import sys + + +def count_lines(text): + """ return number of lines. """ + return len(text.strip().split('\n')) + +def count_words(text): + """ return number of words. """ + return sum([len([1 for word in line.split(' ') if len(word)]) + for line in text.split('\n')]) + +def count_chars(text): + """ return number of words. """ + return len(text) + +def wc(text): + """ proper wc """ + lines = count_lines(text) + words = count_words(text) + chars = count_chars(text) + return lines, words, chars + + +def main(): + """ main """ + print(wc(sys.stdin.read())) + + +if __name__ == "__main__": + main()