forked from geekcomputers/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request geekcomputers#785 from Mohitkumar6122/master
Added Digital Clock
- Loading branch information
Showing
8 changed files
with
239 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,28 @@ | ||
# use Tkinter to show a digital clock | ||
import time | ||
from tkinter import * | ||
|
||
root = Tk() | ||
|
||
root.title("Digital Clock") | ||
root.geometry("250x100+0+0") | ||
root.resizable(0,0) | ||
|
||
label = Label(root, font=("Arial", 30, 'bold'), bg="blue", fg="powder blue", bd =30) | ||
label.grid(row =0, column=1) | ||
|
||
def dig_clock(): | ||
|
||
text_input = time.strftime("%H:%M:%S") # get the current local time from the PC | ||
|
||
label.config(text=text_input) | ||
|
||
# calls itself every 200 milliseconds | ||
# to update the time display as needed | ||
# could use >200 ms, but display gets jerky | ||
|
||
label.after(200, clack) | ||
|
||
dig_clock() | ||
|
||
root.mainloop() |
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,13 @@ | ||
''' | ||
The secrets module is used for generating cryptographically strong random numbers suitable for managing data such as passwords, | ||
account authentication, security tokens, and related secrets. | ||
''' | ||
import string | ||
import secrets | ||
|
||
def secure_password_gen(passlength): | ||
password = ''.join((secrets.choice(string.ascii_letters) for i in range(passlength))) | ||
return password | ||
|
||
n = int(input('Enter length of password : ')) | ||
print('Password generated is :', secure_password_gen(n)) |
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,17 @@ | ||
def bubble_sort(nums): | ||
# We set swapped to True so the loop looks runs at least once | ||
swapped = True | ||
while swapped: | ||
swapped = False | ||
for i in range(len(nums) - 1): | ||
if nums[i] > nums[i + 1]: | ||
# Swap the elements | ||
nums[i], nums[i + 1] = nums[i + 1], nums[i] | ||
# Set the flag to True so we'll loop again | ||
swapped = True | ||
|
||
|
||
# Verify it works | ||
random_list_of_nums = [5, 2, 1, 8, 4] | ||
bubble_sort(random_list_of_nums) | ||
print(random_list_of_nums) |
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,43 @@ | ||
def heapify(nums, heap_size, root_index): | ||
# Assume the index of the largest element is the root index | ||
largest = root_index | ||
left_child = (2 * root_index) + 1 | ||
right_child = (2 * root_index) + 2 | ||
|
||
# If the left child of the root is a valid index, and the element is greater | ||
# than the current largest element, then update the largest element | ||
if left_child < heap_size and nums[left_child] > nums[largest]: | ||
largest = left_child | ||
|
||
# Do the same for the right child of the root | ||
if right_child < heap_size and nums[right_child] > nums[largest]: | ||
largest = right_child | ||
|
||
# If the largest element is no longer the root element, swap them | ||
if largest != root_index: | ||
nums[root_index], nums[largest] = nums[largest], nums[root_index] | ||
# Heapify the new root element to ensure it's the largest | ||
heapify(nums, heap_size, largest) | ||
|
||
|
||
def heap_sort(nums): | ||
n = len(nums) | ||
|
||
# Create a Max Heap from the list | ||
# The 2nd argument of range means we stop at the element before -1 i.e. | ||
# the first element of the list. | ||
# The 3rd argument of range means we iterate backwards, reducing the count | ||
# of i by 1 | ||
for i in range(n, -1, -1): | ||
heapify(nums, n, i) | ||
|
||
# Move the root of the max heap to the end of | ||
for i in range(n - 1, 0, -1): | ||
nums[i], nums[0] = nums[0], nums[i] | ||
heapify(nums, i, 0) | ||
|
||
|
||
# Verify it works | ||
random_list_of_nums = [35, 12, 43, 8, 51] | ||
heap_sort(random_list_of_nums) | ||
print(random_list_of_nums) |
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,19 @@ | ||
def insertion_sort(nums): | ||
# Start on the second element as we assume the first element is sorted | ||
for i in range(1, len(nums)): | ||
item_to_insert = nums[i] | ||
# And keep a reference of the index of the previous element | ||
j = i - 1 | ||
# Move all items of the sorted segment forward if they are larger than | ||
# the item to insert | ||
while j >= 0 and nums[j] > item_to_insert: | ||
nums[j + 1] = nums[j] | ||
j -= 1 | ||
# Insert the item | ||
nums[j + 1] = item_to_insert | ||
|
||
|
||
# Verify it works | ||
random_list_of_nums = [9, 1, 15, 28, 6] | ||
insertion_sort(random_list_of_nums) | ||
print(random_list_of_nums) |
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,60 @@ | ||
def merge(left_list, right_list): | ||
sorted_list = [] | ||
left_list_index = right_list_index = 0 | ||
|
||
# We use the list lengths often, so its handy to make variables | ||
left_list_length, right_list_length = len(left_list), len(right_list) | ||
|
||
for _ in range(left_list_length + right_list_length): | ||
if left_list_index < left_list_length and right_list_index < right_list_length: | ||
# We check which value from the start of each list is smaller | ||
# If the item at the beginning of the left list is smaller, add it | ||
# to the sorted list | ||
if left_list[left_list_index] <= right_list[right_list_index]: | ||
sorted_list.append(left_list[left_list_index]) | ||
left_list_index += 1 | ||
# If the item at the beginning of the right list is smaller, add it | ||
# to the sorted list | ||
else: | ||
sorted_list.append(right_list[right_list_index]) | ||
right_list_index += 1 | ||
|
||
# If we've reached the end of the of the left list, add the elements | ||
# from the right list | ||
elif left_list_index == left_list_length: | ||
sorted_list.append(right_list[right_list_index]) | ||
right_list_index += 1 | ||
# If we've reached the end of the of the right list, add the elements | ||
# from the left list | ||
elif right_list_index == right_list_length: | ||
sorted_list.append(left_list[left_list_index]) | ||
left_list_index += 1 | ||
|
||
return sorted_list | ||
|
||
|
||
def merge_sort(nums): | ||
# If the list is a single element, return it | ||
if len(nums) <= 1: | ||
return nums | ||
|
||
# Use floor division to get midpoint, indices must be integers | ||
mid = len(nums) // 2 | ||
|
||
# Sort and merge each half | ||
left_list = merge_sort(nums[:mid]) | ||
right_list = merge_sort(nums[mid:]) | ||
|
||
# Merge the sorted lists into a new one | ||
return merge(left_list, right_list) | ||
|
||
|
||
# Verify it works | ||
random_list_of_nums = [120, 45, 68, 250, 176] | ||
random_list_of_nums = merge_sort(random_list_of_nums) | ||
print(random_list_of_nums) | ||
|
||
''' | ||
Here merge_sort() function, unlike the previous sorting algorithms, returns a new list that is sorted, rather than sorting the existing list. | ||
Therefore, Merge Sort requires space to create a new list of the same size as the input list | ||
''' |
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,41 @@ | ||
def partition(nums, low, high): | ||
# We select the middle element to be the pivot. Some implementations select | ||
# the first element or the last element. Sometimes the median value becomes | ||
# the pivot, or a random one. There are many more strategies that can be | ||
# chosen or created. | ||
pivot = nums[(low + high) // 2] | ||
i = low - 1 | ||
j = high + 1 | ||
while True: | ||
i += 1 | ||
while nums[i] < pivot: | ||
i += 1 | ||
|
||
j -= 1 | ||
while nums[j] > pivot: | ||
j -= 1 | ||
|
||
if i >= j: | ||
return j | ||
|
||
# If an element at i (on the left of the pivot) is larger than the | ||
# element at j (on right right of the pivot), then swap them | ||
nums[i], nums[j] = nums[j], nums[i] | ||
|
||
|
||
def quick_sort(nums): | ||
# Create a helper function that will be called recursively | ||
def _quick_sort(items, low, high): | ||
if low < high: | ||
# This is the index after the pivot, where our lists are split | ||
split_index = partition(items, low, high) | ||
_quick_sort(items, low, split_index) | ||
_quick_sort(items, split_index + 1, high) | ||
|
||
_quick_sort(nums, 0, len(nums) - 1) | ||
|
||
|
||
# Verify it works | ||
random_list_of_nums = [22, 5, 1, 18, 99] | ||
quick_sort(random_list_of_nums) | ||
print(random_list_of_nums) |
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,18 @@ | ||
def selection_sort(nums): | ||
# This value of i corresponds to how many values were sorted | ||
for i in range(len(nums)): | ||
# We assume that the first item of the unsorted segment is the smallest | ||
lowest_value_index = i | ||
# This loop iterates over the unsorted items | ||
for j in range(i + 1, len(nums)): | ||
if nums[j] < nums[lowest_value_index]: | ||
lowest_value_index = j | ||
# Swap values of the lowest unsorted element with the first unsorted | ||
# element | ||
nums[i], nums[lowest_value_index] = nums[lowest_value_index], nums[i] | ||
|
||
|
||
# Verify it works | ||
random_list_of_nums = [12, 8, 3, 20, 11] | ||
selection_sort(random_list_of_nums) | ||
print(random_list_of_nums) |