-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dodanie pythonowych wersji kilku klasycznych algorytmów
- Loading branch information
Showing
7 changed files
with
339 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#! /usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
|
||
def fib_iter1(n): # definicja funkcji | ||
""" | ||
Funkcja drukuje kolejne wyrazy ciągu Fibonacciego | ||
aż do wyrazu n-tego, który zwraca. | ||
Wersja iteracyjna z pętlą while. | ||
""" | ||
pwyrazy = (0, 1) # dwa pierwsze wyrazy ciągu zapisane w tupli | ||
a, b = pwyrazy # przypisanie wielokrotne, rozpakowanie tupli | ||
print a, | ||
while n > 1: | ||
print b, | ||
a, b = b, a + b # przypisanie wielokrotne | ||
n -= 1 | ||
|
||
|
||
def fib_iter2(n): | ||
""" | ||
Funkcja drukuje kolejne wyrazy ciągu Fibonacciego | ||
aż do wyrazu n-tego, który zwraca. | ||
Wersja iteracyjna z pętlą for. | ||
""" | ||
a, b = 0, 1 | ||
print "wyraz", 1, a | ||
print "wyraz", 2, b | ||
for i in range(1, n - 1): | ||
# wynik = a + b | ||
a, b = b, a + b | ||
print "wyraz", i + 2, b | ||
|
||
print "" # wiersz odstępu | ||
return b | ||
|
||
|
||
def fib_rek(n): | ||
""" | ||
Funkcja zwraca n-ty wyraz ciągu Fibonacciego. | ||
Wersja rekurencyjna. | ||
""" | ||
if n < 1: | ||
return 0 | ||
if n < 2: | ||
return 1 | ||
return fib_rek(n - 1) + fib_rek(n - 2) | ||
|
||
|
||
def main(args): | ||
n = int(raw_input("Podaj nr wyrazu: ")) | ||
fib_iter1(n) | ||
print "" | ||
print "=" * 40 | ||
fib_iter2(n) | ||
print "=" * 40 | ||
print fib_rek(n - 1) | ||
return 0 | ||
|
||
|
||
if __name__ == '__main__': | ||
import sys | ||
sys.exit(main(sys.argv)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# | ||
# horner.py | ||
# | ||
# Program oblicza wartość wielomianu 3 stopnia przy użyciu schematu Hornera. | ||
# | ||
|
||
|
||
def horner(tabwsp, x, stopien): | ||
wartosc = tabwsp[0] | ||
for i in range(1, 4, 1): | ||
wartosc = wartosc * x + tabwsp[i] | ||
return wartosc | ||
|
||
|
||
def main(args): | ||
stopien = 3 | ||
tabwsp = [] # pusta lista | ||
print "Podaj 4 współczynniki: " | ||
for i in range(stopien + 1): | ||
liczba = raw_input("Podaj współczynnik: ") | ||
tabwsp.append(float(liczba)) | ||
x = float(raw_input("Podj argument: ")) | ||
|
||
print tabwsp, x | ||
print "Wartość:", horner(tabwsp, x, stopien) | ||
|
||
return 0 | ||
|
||
|
||
if __name__ == '__main__': | ||
import sys | ||
sys.exit(main(sys.argv)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
Algorytmy | ||
################### | ||
|
||
Poniżej kody klasycznych algorytmów w Pythonie. | ||
[Komentarz do dopisania] | ||
|
||
Trójkąt | ||
======= | ||
|
||
.. raw:: html | ||
|
||
<div class="code_no">Kod nr <script>var code_no = code_no || 1; document.write(code_no++);</script></div> | ||
|
||
.. literalinclude:: trojkat_heron.py | ||
:linenos: | ||
|
||
Silnia | ||
====== | ||
|
||
Wersja iteracyjna i rekurencyjna. | ||
|
||
.. raw:: html | ||
|
||
<div class="code_no">Kod nr <script>var code_no = code_no || 1; document.write(code_no++);</script></div> | ||
|
||
.. literalinclude:: silnia.py | ||
:linenos: | ||
|
||
Ciąg Fibonacciego | ||
================= | ||
|
||
Wersja iteracyjna i rekurencyjna. | ||
|
||
.. raw:: html | ||
|
||
<div class="code_no">Kod nr <script>var code_no = code_no || 1; document.write(code_no++);</script></div> | ||
|
||
.. literalinclude:: fibonacci.py | ||
:linenos: | ||
|
||
Szyfr Cezara | ||
============ | ||
|
||
Wersja ze stałym kluczem. | ||
|
||
.. raw:: html | ||
|
||
<div class="code_no">Kod nr <script>var code_no = code_no || 1; document.write(code_no++);</script></div> | ||
|
||
.. literalinclude:: szyfr_cezara.py | ||
:linenos: | ||
|
||
Sortowanie | ||
========== | ||
|
||
Przez wybór | ||
----------- | ||
|
||
.. raw:: html | ||
|
||
<div class="code_no">Kod nr <script>var code_no = code_no || 1; document.write(code_no++);</script></div> | ||
|
||
.. literalinclude:: sort_wybor.py | ||
:linenos: | ||
|
||
|
||
.. raw:: html | ||
|
||
<hr> | ||
|
||
:Autor: Robert Bednarz (ecg@ecg.vot.pl) | ||
|
||
:Utworzony: |date| o |time| | ||
|
||
.. |date| date:: | ||
.. |time| date:: %H:%M | ||
|
||
.. raw:: html | ||
|
||
<style> | ||
div.code_no { text-align: right; background: #e3e3e3; padding: 6px 12px; } | ||
div.highlight, div.highlight-python { margin-top: 0px; } | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
|
||
def sil_rek(n): | ||
if n == 0: | ||
return 1 | ||
return sil_rek(n - 1) * n | ||
|
||
|
||
def sil_iter(n): | ||
wynik = 1 | ||
for i in range(1, n + 1): | ||
wynik = wynik * i | ||
return wynik | ||
|
||
|
||
def main(args): | ||
n = int(raw_input("Podaj liczbę: ")) | ||
print "sil_iter(%s) = %s" % (n, sil_iter(n)) | ||
print "sil_rek(%s) = %s" % (n, sil_rek(n)) | ||
return 0 | ||
|
||
|
||
if __name__ == '__main__': | ||
import sys | ||
sys.exit(main(sys.argv)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#! /usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
from random import randint | ||
|
||
|
||
def zamien(lista, i, k): | ||
tmp = lista[i] | ||
lista[i] = lista[k] | ||
lista[k] = tmp | ||
return lista | ||
|
||
|
||
def sort_wybor(lista): | ||
n = len(lista) | ||
for i in range(n): | ||
k = i | ||
for j in range(i + 1, n): | ||
if lista[j] < lista[k]: | ||
k = j | ||
# print k, "=>", i | ||
tmp = lista[i] | ||
lista[i] = lista[k] | ||
lista[k] = tmp | ||
return lista | ||
|
||
|
||
def main(args): | ||
lista = [] | ||
for i in range(10): | ||
lista.append(randint(0, 100)) | ||
print lista | ||
print sort_wybor(lista) | ||
|
||
|
||
if __name__ == '__main__': | ||
import sys | ||
sys.exit(main(sys.argv)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#! /usr/bin/env python | ||
# -*- coding: UTF-8 -*- | ||
|
||
# ~/python/07_2_szyfr_cezara.py | ||
|
||
""" | ||
ZADANIE | ||
Pobierz od użytkownika ciąg znaków, zaszyfruj go przy wykorzystaniu | ||
szyfru Cezara o kluczu 3, wyświetl zaszyfrowany ciąg. | ||
""" | ||
|
||
KLUCZ = 3 | ||
|
||
|
||
def szyfruj(txt): | ||
stxt = "" | ||
for i in range(len(txt)): | ||
if ord(txt[i]) > 122 - KLUCZ: | ||
stxt += chr(ord(txt[i]) + KLUCZ - 26) | ||
else: | ||
stxt += chr(ord(txt[i]) + KLUCZ) | ||
return stxt | ||
|
||
|
||
utxt = raw_input("Podaj ciąg do zaszyfrowania:\n") | ||
stxt = szyfruj(utxt) | ||
print "Ciąg zaszyfrowany:\n", stxt | ||
|
||
""" | ||
JAK TO DZIAŁA | ||
W programie możemy wykorzystywać zmienne globalne, np. KLUCZ. | ||
def nazwa_funkcji(argumenty): - tak definiujemy funkcje, które | ||
mogą lub nie zwracać jakieś wartości. | ||
nazwa_funkcji(argumenty) - tak wywołujemy funkcje. | ||
Napisy mogą być indeksowane (od 0), co daje dostęp do pojedynczych znaków. | ||
Funkcja len(str) zwraca długość napisu, wykorzystana jako argument funkcji | ||
range() pozwala iterować po znakach napisu. | ||
Operator += oznacza dodanie argumentu z prawej strony do wartości z lewej. | ||
""" | ||
|
||
""" | ||
CO MOGĘ ZMIENIĆ | ||
Napisz funkcję deszyfrującą deszyfruj(txt). | ||
Dodaj do funkcji szyfruj, deszyfruj drugi parametr w postaci długości | ||
klucza podawanej przez użytkownika. | ||
Dodaj poprawne szyfrowanie dużych liter, białych znaków i znaków | ||
interpunkcyjnych. | ||
def deszyfruj(txt): | ||
dtxt = "" | ||
KLUCZM = KLUCZ % 26; | ||
for i in range(len(txt)): | ||
if (ord(txt[i]) - KLUCZM < 97): | ||
dtxt += chr(ord(txt[i]) - KLUCZM + 26) | ||
else: | ||
dtxt += chr(ord(txt[i]) - KLUCZM) | ||
return dtxt | ||
dtxt = deszyfruj(stxt) | ||
print "Ciąg zdeszyfrowany:\n", dtxt | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#! /usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
import math # dołączamy bibliotekę matematyczną | ||
|
||
op = "t" # deklarujemy i inicjujemy zmienną pomocniczą | ||
while op != "n": # dopóki wartość zmiennej op jest inna niż znak "n" | ||
a, b, c = input("Podaj 3 boki trójkąta (oddzielone przecinkami): ") | ||
# alternatywna forma pobrania danych | ||
# a, b, c = [int(x) for x in raw_input( | ||
# "Podaj 3 boki trójkąta (oddzielone spacjami): ").split()] | ||
|
||
if a + b > c and a + c > b and b + c > a: # warunek złożony | ||
print "Z podanych boków można zbudować trójkąt." | ||
# czy boki spełniają warunki trójkąta prostokątnego? | ||
if (a**2 + b**2 == c**2 or | ||
a**2 + c**2 == b**2 or | ||
b**2 + c**2 == a**2): | ||
print "Do tego prostokątny!" | ||
|
||
# na wyjściu możemy wyprowadzać wyrażenia | ||
print "Obwód wynosi:", (a + b + c) | ||
p = 0.5 * (a + b + c) # obliczmy współczynnik wzoru Herona | ||
# liczymy pole ze wzoru Herona | ||
P = math.sqrt(p * (p - a) * (p - b) * (p - c)) | ||
print "Pole wynosi:", P | ||
op = "n" # ustawiamy zmienną na "n", aby wyjść z pętli while | ||
else: | ||
print "Z podanych odcinków nie można utworzyć trójkąta prostokątnego." | ||
op = raw_input("Spróbujesz jeszcze raz (t/n): ") | ||
|
||
print "Do zobaczenia..." |