Python HW10

mail@pastecode.io avatar
unknown
python
2 years ago
2.0 kB
2
Indexable
Never
#1
import matplotlib.pyplot as plt

def piechart(data):
    occurence = {}
    
    for i in data:
        
        if i not in occurence.keys():
            occurence.update({i : 1})
        else:
            occurence[i] += 1

    fig1, ax1 = plt.subplots()
    ax1.pie(occurence.values(), labels=occurence.keys(), autopct='%1.1f%%', startangle=90)

    plt.show()

piechart([3,1,3,3,2,3,3,2,3,2,4,3,3,3,3,4,3,4,3,3,3,4,3])


#2
def bubble_sort(data):
    for i in range(len(data)):
        for j in range(len(data) - i - 1):
            print(i, j)
            if data[j] > data[j+1]:
                temp = data[j+1]
                data[j+1] = data[j]
                data[j] = temp
            else:
                continue
            print(data)

    return data

print(bubble_sort([3,2,9,7,8]))


#3
def my_union(x, y):
    union_set = []

    for i in x:
        if i in union_set:
            continue
        else:
            union_set.append(i)

    for i in y:
        if i in union_set:
            continue
        else:
            union_set.append(i)

    return union_set

def my_inter(x, y):
    inter_set = []

    for i in x:
        if i in y:
            inter_set.append(i)

    return inter_set

def my_diff(x, y):
    diff_set = []

    for i in x:
        if i not in y:
            diff_set.append(i)

    return diff_set

print(my_union([3,1,2,7], [4,1,2,5]))
print(my_inter([3,1,2,7], [4,1,2,5]))
print(my_diff([3,1,2,7], [4,1,2,5]))


#4
def print_table(data):
    for i in data:
        for j in i:
            print(j, end="\t")
        print("")

print_table([["X", "Y"], [0,0], [10,10], [200, 200]])
print_table([
    ["ID", "Name", "Surname"],
    ["001", "Guido", "van Rossum"],
    ["002", "Donald", "Knuth"],
    ["003", "Gordon", "Moore"]])


#5
def isAnagram(x,y):

    if sorted(x) == sorted(y):
        return True
    else:
        return False

print(isAnagram("silent", "listen"))