Competitivepython เป็นไลบรารีโอเพ่นซอร์สของอัลกอริทึมและโครงสร้างข้อมูลที่นำมาใช้ใน Python มีคอลเลกชันของอัลกอริธึมและโครงสร้างข้อมูลที่ใช้บ่อยซึ่งสามารถนำมาใช้โดยตรงในโปรเจ็กต์ที่ใช้ Python
หากต้องการติดตั้งไลบรารีของ Competitivepython เพียงรันคำสั่งต่อไปนี้:
pip install competitivepython
หากต้องการใช้ CompetitivePython ในโปรเจ็กต์ของคุณ ให้นำเข้าอัลกอริทึมหรือโครงสร้างข้อมูลที่ต้องการแล้วใช้ตามความจำเป็น ด้านล่างนี้เป็นตัวอย่างการใช้งานบางส่วน:
การดำเนินการค้นหา:
from competitivepython import searches
arr = [1, 2, 3, 4, 5]
target = 3
result = searches.binary_search(arr, target)
print("Binary Search:",result)
'''Output:
Binary Search: 2
'''
from competitivepython import searches
arr = [5, 7, 9, 2, 4, 10]
target = 4
result = searches.linear_search(arr, target)
print("Linear Search:",result)
'''Output:
Linear Search: 4
'''
from competitivepython import searches
txt = "ABABDABACDABABCABAB"
pat = "ABABCABAB"
result = searches.kmp_search(pat,txt)
print("KMP Search:",result)
'''Output:
KMP Search: [10]
'''
การใช้การเรียงลำดับ:
from competitivepython import sorting
arr = [112, 6, 7, 12, 15]
result = sorting.bubble_sort(arr)
print('bubble sort:', result)
''' Output ---
bubble sort: [6, 7, 12, 15, 112]
'''
from competitivepython import sorting
arr = [112, 6, 7, 12, 15]
result = sorting.bucket_sort(arr)
print('bucket sort:', result)
''' Output ---
bucket sort: [6, 7, 12, 15, 112]
'''
from competitivepython import sorting
arr = [112, 6, 7, 12, 15]
result = sorting.heap_sort(arr)
print('heap sort:', result)
''' Output ---
heap sort: [6, 7, 12, 15, 112]
'''
from competitivepython import sorting
arr = [112, 6, 7, 12, 15]
result = sorting.insertion_sort(arr)
print('insertion sort:', result)
''' Output ---
insertion sort: [6, 7, 12, 15, 112]
'''
from competitivepython import sorting
arr = [112, 6, 7, 12, 15]
result = sorting.merge_sort(arr)
print('merge sort:', result)
''' Output ---
merge sort: [6, 7, 12, 15, 112]
'''
from competitivepython import sorting
arr = [112, 6, 7, 12, 15]
result = sorting.quick_sort(arr)
print('quick sort:', result)
''' Output ---
quick sort: [6, 7, 12, 15, 112]
'''
from competitivepython import sorting
arr = [112, 6, 7, 12, 15]
result = sorting.radix_sort(arr)
print('radix sort:', result)
''' Output ---
radix sort: [6, 7, 12, # 15, 112]
'''
from competitivepython import sorting
arr = [112, 6, 7, 12, 15]
result = sorting.selection_sort(arr)
print('selection sort:', result)
''' Output ---
selection sort: [6, 7, 12, 15, 112]
'''
from competitivepython import sorting
arr = [112, 6, 7, 12, 15]
result = sorting.shell_sort(arr)
print('shell sort:', result)
''' Output ---
shell sort: [6, 7, 12, 15, 112]
'''
from competitivepython import sorting
arr = [112, 6, 7, 12, 15]
result = sorting.tim_sort(arr)
print('tim sort:', result)
''' Output ---
tim sort: [6, 7, 12, 15, 112]
'''
การใช้กราฟ:
from competitivepython import graphs
graph = {
'A': {'B': 1, 'C': 4},
'B': {'A': 1, 'C': 2, 'D': 5},
'C': {'A': 4, 'B': 2, 'D': 1},
'D': {'B': 5, 'C': 1},
}
start = 'A'
end = 'D'
result = graphs.breadth_first_search(graph, 'C')
print("bfs:",result)
''' Output--
bfs: {'B', 'D', 'C', 'A'}
'''
from competitivepython import graphs
graph = {
'A': {'B': 1, 'C': 4},
'B': {'A': 1, 'C': 2, 'D': 5},
'C': {'A': 4, 'B': 2, 'D': 1},
'D': {'B': 5, 'C': 1},
}
start = 'A'
end = 'D'
result = graphs.depth_first_search(graph, 'C')
print("dfs:",result)
''' Output--
dfs: {'B', 'D', 'C', 'A'}
'''
from competitivepython import graphs
graph = {
'A': {'B': 1, 'C': 4},
'B': {'A': 1, 'C': 2, 'D': 5},
'C': {'A': 4, 'B': 2, 'D': 1},
'D': {'B': 5, 'C': 1},
}
start = 'A'
end = 'D'
result = graphs.dijkstra(graph, start, end)
print("dijikstra:",result)
''' Output--
dijikstra: {'distance': 4, 'path': ['B', 'C', 'D']}
'''
การนำต้นไม้ไปใช้:
from competitivepython import trees
# Create an instance of the BinarySearchTree
bst = trees.BinarySearchTree()
# Insert some values into the tree
bst.insert(50)
bst.insert(30)
bst.insert(20)
bst.insert(40)
bst.insert(70)
bst.insert(60)
bst.insert(80)
# Check if a value is present in the tree
print(bst.search(50)) # Output: True
print(bst.search(35)) # Output: False
# Get the values in the tree in in-order traversal order
print(bst.get_in_order_traversal()) # Output: [20, 30, 40, 50, 60, 70, 80]
หากคุณต้องการมีส่วนร่วมในโครงการ Competitivepython โปรดดูหลักเกณฑ์การมีส่วนร่วมใน CONTRIBUTING.md เรายินดีรับการสนับสนุนทุกประเภท รวมถึงรายงานข้อผิดพลาด คำขอคุณสมบัติ และการสนับสนุนโค้ด
Competitivepython เป็นซอฟต์แวร์โอเพ่นซอร์สที่เผยแพร่ภายใต้ลิขสิทธิ์ MIT โปรดดูไฟล์ลิขสิทธิ์สำหรับข้อมูลเพิ่มเติม