Add examples to labs04
This commit is contained in:
parent
b3ba876bb1
commit
91c41d467c
37
labs04/examples/06_execution_time.py
Executable file
37
labs04/examples/06_execution_time.py
Executable file
@ -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()))
|
31
labs04/examples/25_ip2geolocation.py
Executable file
31
labs04/examples/25_ip2geolocation.py
Executable file
@ -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)
|
47
labs04/examples/fib.py
Normal file
47
labs04/examples/fib.py
Normal file
@ -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))
|
||||
|
11
labs04/examples/myip.py
Normal file
11
labs04/examples/myip.py
Normal file
@ -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)
|
39
labs04/examples/wc.py
Normal file
39
labs04/examples/wc.py
Normal file
@ -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()
|
Loading…
Reference in New Issue
Block a user