3 Ekim 2018 Çarşamba

03 Ekim

bir resmin inverse alınması işlemi

öncelikle resmin bulunduğu klosör tanımlanmalıdır:
import os
cwd = os.getcwd()
os.chdir("C:\\Users\BM\Desktop")

daha sonra nd array yapısı kullanılarak resim tanımlanır ve 255 ten çıkarılarak inverse edilmiş olur:
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
img = mpimg.imread('img.jpg')
%matplotlib inline
plt.subplot(1,2,1)
plt.imshow(img)
plt.subplot(1,2,2)
plt.imshow(255-img)


hash yapısı ve tanımlaması
my_hash = {}
my_hash[0] = 1
my_hash[1] = 11
my_hash[2] = 55
my_hash[3] = 155
my_hash[4] = 355
for i in my_hash.keys():
    print(i)
my_hash[2]+=5
my_hash[2]

19 Haziran 2018 Salı

Heap

Uygulama konusu: Min-Heap yapısına göre sıralı olmayan bir dizinin sıralanması işlemidir. 

def heapify(arr, n, i):
    en_buyuk = i  
    sol = 2 * i + 1     
    sag = 2 * i + 2     
    if sol < n and arr[i] < arr[sol]:
        en_buyuk = sol
    if sag < n and arr[en_buyuk] < arr[sag]:
        en_buyuk = sag
    if en_buyuk != i:
        arr[i],arr[en_buyuk] = arr[en_buyuk],arr[i]  
        heapify(arr, n, en_buyuk)
def heapSort(arr):
    n = len(arr)
    for i in range(n, -1, -1):
        heapify(arr, n, i)
    for i in range(n-1, 0, -1):
        arr[i], arr[0] = arr[0], arr[i]  
        heapify(arr, i, 0)
arr = [ 54,26,93,17]
heapSort(arr)
n = len(arr)
print ("Sıralı heap: ")
print(arr)

28 Mayıs 2018 Pazartesi

Quick Sort

Uygulama Konusu: Quick sort mantığı kullanılarak, tanımlanmış olan bir dizi sıralı hale getirilir.

def quickSort(alist):
   quickSortHelper(alist,0,len(alist)-1)
def quickSortHelper(alist,first,last):
   if first<last:
       splitpoint = partition(alist,first,last)
       quickSortHelper(alist,first,splitpoint-1)
       quickSortHelper(alist,splitpoint+1,last)
def partition(alist,first,last):
   pivotvalue = alist[first]
   leftmark = first+1
   rightmark = last
   done = False
   while not done:
       while leftmark <= rightmark and alist[leftmark] <= pivotvalue:
           leftmark = leftmark + 1
       while alist[rightmark] >= pivotvalue and rightmark >= leftmark:
           rightmark = rightmark -1
       if rightmark < leftmark:
           done = True
       else:
           temp = alist[leftmark]
           alist[leftmark] = alist[rightmark]
           alist[rightmark] = temp
   temp = alist[first]
   alist[first] = alist[rightmark]
   alist[rightmark] = temp
   return rightmark
mylist = [54,26,93,17,77,31,44,55,20]
quickSort(mylist)
print(mylist)

Insertion Sort

Uygulama Konusu: Tanımlanan diziyi insertion sort mantığıysa sıralar.

def insertionSort(alist):
   for i in range(1,len(alist)):
     value = alist[i]
     position = i
     while position>0 and alist[position-1]>value:
         alist[position]=alist[position-1]
         position = position-1
     alist[position]=value
mylist = [12,23,39,48,2,26,16,49,9]
insertionSort(mylist)
print(mylist)

Selection Sort

Uygulama Konusu: Tanımlanmış olan diziyi selection sort mantığıyla sıralar ve kaydeder.

def selectionSort(alist):
   for slot in range(len(alist)-1,0,-1):
       max=0
       for location in range(1,slot+1):
           if alist[location]>alist[max]:
               max = location
       temp = alist[slot]
       alist[slot] = alist[max]
       alist[max] = temp
mylist = [54,26,93,17,77,31,44,55,20]
selectionSort(mylist)
print(mylist)

Bubble Sort

Uygulama Konusu: Python ile buble sort algoritması tanımlı bir liste üzerinden gerçekleştirilmiştir.

def bubbleSort(list):
    for j in range(len(list)-1,0,-1):
        for i in range(j):
            if list[i]>list[i+1]:
                temp = list[i]
                list[i] = list[i+1]
                list[i+1] = temp
mylist = [54,26,93,17,77,31,44,55,20]
bubbleSort(mylist)
print(mylist)

Graph

Uygulama Konusu: Python üzerinde graph yapısı tanımlanmıştır. Bu yapı üzerinde bulunun özellikler olan köşeler ve kenarlar için yeni eklemeler yapan vegraph'ın son halini gösteren fonksiyonlar kullanılmıştır.

class Graph(object):
    def __init__(self, graph_dict=None):
        if graph_dict == None:
            graph_dict = {}
        self.__graph_dict = graph_dict
    def vertices(self): #graph ın köşelerini döndürür
        return list(self.__graph_dict.keys())
    def edges(self): # graph ın kenarlarını dönürür
        return self.__generate_edges()
    def add_vertex(self, vertex): #tepe noktası ekler
        if vertex not in self.__graph_dict:
            self.__graph_dict[vertex] = []
    def add_edge(self, edge): #kenar ekleme
        edge = set(edge)
        (vertex1, vertex2) = tuple(edge)
        if vertex1 in self.__graph_dict:
            self.__graph_dict[vertex1].append(vertex2)
        else:
            self.__graph_dict[vertex1] = [vertex2]
    def __generate_edges(self):
        edges = []
        for vertex in self.__graph_dict:
            for neighbour in self.__graph_dict[vertex]:
                if {neighbour, vertex} not in edges:
                    edges.append({vertex, neighbour})
        return edges
    def __str__(self):
        res = "vertices: "
        for k in self.__graph_dict:
            res += str(k) + " "
        res += "\nedges: "
        for edge in self.__generate_edges():
            res += str(edge) + " "
        return res
g = { "a" : ["d"],
      "b" : ["c"],
      "c" : ["b", "c", "d", "e"],
      "d" : ["a", "c"],
      "e" : ["c"],
      "f" : [] }
graph = Graph(g)
print("köşeler:", graph.vertices())
print("kenarlar:", graph.edges())
print("tepe ekle:")
graph.add_vertex("z")
print("yeni köşeler:", graph.vertices())
print("kenar ekle:")
graph.add_edge({"a","z"})
print("köşeler:", graph.vertices())
print("kenarlar:", graph.edges())
print('Köşe olarak "y" ekler, kenarları olarak "x,y" ekler:')
graph.add_edge({"x","y"})
print("köşeler:", graph.vertices())
print("kenarlar:", graph.edges())