1
0
forked from tdwojak/Python2017
This commit is contained in:
s45167 2018-01-26 14:57:33 +01:00
parent def69182d9
commit 5a2a035e5c
5 changed files with 91 additions and 51 deletions

View File

@ -1,10 +1,12 @@
#!/usr/bin/env python2 #!/usr/bin/env python2
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
"""
Napisz funkcję ``suma``, która przyjmnie jeden argument: listę liczb i zwróci ich sumę.
"""
def suma(liczby): def suma(liczby):
assert len(liczby) > 0 return sum(liczby)
a=reduce(lambda a, b: a+b, liczby)
return a
def main(): def main():
print(suma([1, 2, 3, 4])) print(suma([1, 2, 3, 4]))

View File

@ -1,7 +1,14 @@
import task00, sys from task00 import suma
import sys
def main(): def main():
print('%f' % (task00.suma(map(lambda x: float(x), sys.argv[1:])))) arguments=sys.argv[1:]
float_arguments=[]
for arg in arguments:
float_arguments.append(float(arg))
print(suma(float_arguments))
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@ -1,12 +1,18 @@
import task00, sys from task00 import suma
import sys
def main(): def main():
def a_to_f(a): arg_list = sys.argv[1:]
float_arg_list = []
for arg in arg_list:
try: try:
return float(a) float_arg=float(arg)
float_arg_list.append(float_arg)
except: except:
return 0 float_arg_list.append(0)
print('%f' % (task00.suma(map(a_to_f, sys.argv[1:]))))
print(suma(float_arg_list))
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@ -1,12 +1,5 @@
#!/usr/bin/env python import sys
# -*- coding: utf-8 -*- import argparse
"""
Implementacja narzedzia ``wc`` z linuksa (word counter).
Zwraca liczbę słów, znaków i linii.
"""
import sys, argparse
def count_lines(text): def count_lines(text):
@ -35,16 +28,22 @@ def main():
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('-l', help="number of lines", action="store_true") parser.add_argument('-l', help="number of lines", action="store_true")
parser.add_argument('-w', help="number of words", action="store_true") parser.add_argument('-w', help="number of words", action="store_true")
parser.add_argument('-c', help="number of characters", action="store_true") parser.add_argument('-c', help="number of chars", action="store_true")
parser.add_argument('fn', nargs='?', type=file, default=sys.stdin, help='optional filename to read from') parser.add_argument('-filename', type=argparse.FileType('r'), default='-', help='filename to read from')
args = parser.parse_args() args = parser.parse_args()
lines, words, chars = wc(args.fn.read())
if( args.l or args.w or args.c ): if args.filename:
lines, words, chars = wc(args.filename.read())
elif not sys.stdin.isatty():
lines, words, chars = wc(args.stdin.read())
else:
pass
if(args.l): if(args.l):
print(lines) print(lines)
if(args.w): elif(args.w):
print(words) print(words)
if(args.c): elif(args.c):
print(chars) print(chars)
else: else:
print(lines, words, chars) print(lines, words, chars)
@ -52,3 +51,4 @@ def main():
if __name__ == "__main__": if __name__ == "__main__":
main() main()

View File

@ -1,18 +1,22 @@
##!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import pandas as pd import pandas as pd
import statistics
def wczytaj_dane(): def wczytaj_dane():
mieszkania = pd.read_csv('mieszkania.csv', # ścieżka do pliku raw_data = pd.read_csv('mieszkania.csv',sep=',')
sep=',', # separator data = pd.DataFrame(raw_data)
encoding='UTF-8', return data
usecols=[0,1,2,3,4,5,6])
return mieszkania
def most_common_room_number(dane): def most_common_room_number(dane):
return dane.mode(numeric_only =True)["Rooms"][0] rooms=dane['Rooms']
return(int(statistics.mode(rooms)))
def cheapest_flats(dane, n): def cheapest_flats(dane, n):
return dane.sort_values("Expected")[:n] cheapest=pd.DataFrame(dane['Expected'])
cheapest.sort=cheapest.sort_values(by=['Expected'])
return cheapest.sort[:n]
def find_borough(desc): def find_borough(desc):
dzielnice = ['Stare Miasto', dzielnice = ['Stare Miasto',
@ -23,41 +27,62 @@ def find_borough(desc):
'Winogrady', 'Winogrady',
'Miłostowo', 'Miłostowo',
'Dębiec'] 'Dębiec']
inputList=desc.split(' ') for dzielnica in dzielnice:
for i in inputList: if desc.find(dzielnica)!=-1:
if i in dzielnice: return dzielnica
return i return 'Inne'
return "Inne"
def add_borough(dane): def add_borough(dane):
newcol=dane["Location"].apply(find_borough) borough_list=[]
dane["Borough"]=newcol for data in dane['Location']:
borough_list.append(find_borough(data))
borough_series = pd.Series(borough_list,name='Borough')
dane['Borough']=borough_series
return dane return dane
def write_plot(dane, filename): def write_plot(dane, filename):
bar=dane["Borough"].value_counts().plot(kind="bar", figsize=(6,6)) data = add_borough(dane)
fig=bar.get_figure() plot_data = data.groupby(dane['Borough']).size()
fig.savefig(filename) plt = plot_data.plot.bar()
fig = plt.get_figure()
fig.savefig('{}.png'.format(filename))
return 0
def mean_price(dane, room_number): def mean_price(dane, room_number):
return dane[dane["Rooms"]==room_number]["Expected"].mean() data=dane.loc[dane['Rooms'] == room_number]
return round(statistics.mean(data['Expected']),2)
def find_13(dane): def find_13(dane):
return dane[dane["Floor"]==13]["Borough"].unique() data = add_borough(dane)
boroughs = data.loc[data['Floor'] == 13]
return set(boroughs['Borough'])
def find_best_flats(dane): def find_best_flats(dane):
return dane[(dane["Borough"]=="Winogrady") & (dane["Floor"]==1) & (dane["Rooms"]==3)] data = add_borough(dane)
best_flats = data.loc[(data['Borough'] == 'Winogrady') & (data['Floor'] == 1) & (data['Rooms'] == 3)]
return best_flats
def main(): def main():
dane = wczytaj_dane() dane = wczytaj_dane()
print(dane[:5]) print(dane[:5])
print("Najpopularniejsza liczba pokoi w mieszkaniu to: {}" print("Najpopularniejsza liczba pokoi w mieszkaniu to: {}"
.format(most_common_room_number(dane))) .format(most_common_room_number(dane)))
print("{} to najłądniejsza dzielnica w Poznaniu." print("{} to najłądniejsza dzielnica w Poznaniu."
.format(find_borough("Grunwald i Jeżyce"))) .format(find_borough("Grunwald i Jeżyce")))
print("Średnia cena mieszkania 3-pokojowego, to: {}" print("Średnia cena mieszkania 3-pokojowego, to: {}"
.format(mean_price(dane, 3))) .format(mean_price(dane, 3)))
if __name__ == "__main__": if __name__ == "__main__":
main() main()