Skip to content

Commit

Permalink
fix: baseException and fixing unused imported modules
Browse files Browse the repository at this point in the history
  • Loading branch information
slowy07 committed Aug 1, 2021
1 parent e8fb181 commit 881438b
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 40 deletions.
2 changes: 1 addition & 1 deletion CountMillionCharacter.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@
try:
if special_character.group(): #returns all matching groups
wordlist[x] = y[:-1]
except:
except BaseException:
continue

wordfreq = [wordlist.count(w) for w in wordlist] #counts frequency of a letter in the given list
Expand Down
2 changes: 1 addition & 1 deletion Flappy Bird - created with tkinter/Bird.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def checkCollision(self):
for _id in ignored_collisions:
try:
possible_collisions.remove(_id)
except:
except BaseException:
continue

# Se houver alguma colisão o pássaro morre
Expand Down
2 changes: 1 addition & 1 deletion Flappy Bird - created with tkinter/Flappy Bird.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ def loadScore(self):
file.close()

# Se não for possível, será criado um arquivo para guardar o placar
except:
except BaseException:
file = open(self.score_fp, 'w')
file.write(bin(self.__bestScore))
file.close()
Expand Down
2 changes: 1 addition & 1 deletion Flappy Bird - created with tkinter/Settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def setOptions(self):
setattr(Settings, attr, data[attr])

# Caso não exista um arquivo para obter as configurações, ele será criado
except:
except BaseException:

# Caso não exista o diretório, o mesmo será criado.
if not os.path.exists(os.path.split(self.settings_fp)[0]):
Expand Down
2 changes: 1 addition & 1 deletion Flappy Bird - created with tkinter/Tubes.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def __init__(self, background, bird, score_function=None, *screen_geometry, fp=(
# Cria uma lista para guardar imagens dos tubos
try:
self.deleteAll()
except:
except BaseException:
self.__background.tubeImages = []

# Cria uma lista somente para guardar as imagens futuras dos corpos dos tubos gerados
Expand Down
63 changes: 37 additions & 26 deletions binary_search_tree.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import sys

class Node:
"""Class for node of a tree"""

def __init__(self, info):
"""Initialising a node"""
self.info = info
Expand All @@ -18,6 +17,7 @@ def __del__(self):

class BinarySearchTree:
"""Class for BST"""

def __init__(self):
"""Initialising a BST"""
self.root = None
Expand Down Expand Up @@ -46,7 +46,7 @@ def insert(self, val):
else:
break

def search(self, val, to_delete = False):
def search(self, val, to_delete=False):
current = self.root
prev = -1
while current:
Expand All @@ -58,12 +58,12 @@ def search(self, val, to_delete = False):
current = current.right
elif current.info == val:
if not to_delete:
return 'Match Found'
return "Match Found"
return prev
else:
break
if not to_delete:
return 'Not Found'
return "Not Found"

# Method to delete a tree-node if it exists, else error message will be returned.
def delete(self, val):
Expand All @@ -83,21 +83,21 @@ def delete(self, val):
else:
prev2.right = None
self.root.info = temp.info
print('Deleted Root ', val)
print("Deleted Root ", val)
# Check if node is to left of its parent
elif prev.left and prev.left.info == val:
# Check if node is leaf node
if prev.left.left is prev.left.right:
prev.left = None
print('Deleted Node ', val)
print("Deleted Node ", val)
# Check if node has child at left and None at right
elif prev.left.left and prev.left.right is None:
prev.left = prev.left.left
print('Deleted Node ', val)
print("Deleted Node ", val)
# Check if node has child at right and None at left
elif prev.left.left is None and prev.left.right:
prev.left = prev.left.right
print('Deleted Node ', val)
print("Deleted Node ", val)
# Here node to be deleted has 2 children
elif prev.left.left and prev.left.right:
temp = prev.left
Expand All @@ -106,10 +106,9 @@ def delete(self, val):
temp = temp.right
prev2.right = None
prev.left.info = temp.info
print('Deleted Node ', val)
print("Deleted Node ", val)
else:
print('Error Left')

print("Error Left")

# Check if node is to right of its parent
elif prev.right.info == val:
Expand All @@ -118,31 +117,32 @@ def delete(self, val):
if prev.right.left is prev.right.right:
prev.right = None
flag = 1
print('Deleted Node ', val)
print("Deleted Node ", val)
# Check if node has left child at None at right
if prev.right and prev.right.left and prev.right.right is None:
prev.right = prev.right.left
print('Deleted Node ', val)
print("Deleted Node ", val)
# Check if node has right child at None at left
elif prev.right and prev.right.left is None and prev.right.right:
prev.right = prev.right.right
print('Deleted Node ', val)
print("Deleted Node ", val)
elif prev.right and prev.right.left and prev.right.right:
temp = prev.right
while temp.left is not None:
prev2 = temp
temp = temp.left
prev2.left = None
prev.right.info = temp.info
print('Deleted Node ', val)
print("Deleted Node ", val)
else:
if flag == 0:
print("Error")
else:
print("Node doesn't exists")

def __str__(self):
return 'Not able to print tree yet'
return "Not able to print tree yet"


def is_bst(node, lower_lim=None, upper_lim=None):
"""Function to find is a binary tree is a binary search tree."""
Expand All @@ -158,6 +158,7 @@ def is_bst(node, lower_lim=None, upper_lim=None):
is_right_bst = is_bst(node.right, node.info, upper_lim)
return is_left_bst and is_right_bst


def postorder(node):
# L R N : Left , Right, Node
if node is None:
Expand All @@ -179,6 +180,7 @@ def inorder(node):
if node.right:
inorder(node.right)


def preorder(node):
# N L R : Node , Left, Right
if node is None:
Expand All @@ -189,6 +191,7 @@ def preorder(node):
if node.right:
preorder(node.right)


# Levelwise
def bfs(node):
queue = []
Expand All @@ -202,6 +205,7 @@ def bfs(node):
if temp.right:
queue.append(temp.right)


def preorder_itr(node):
# N L R : Node, Left , Right
stack = [node]
Expand All @@ -216,29 +220,31 @@ def preorder_itr(node):
stack.append(temp.left)
return values


def inorder_itr(node):
# L N R : Left, Node, Right
# 1) Create an empty stack S.
# 2) Initialize current node as root
# 3) Push the current node to S and set current = current->left until current is NULL
# 4) If current is NULL and stack is not empty then
# 4) If current is NULL and stack is not empty then
# a) Pop the top item from stack.
# b) Print the popped item, set current = popped_item->right
# b) Print the popped item, set current = popped_item->right
# c) Go to step 3.
# 5) If current is NULL and stack is empty then we are done.
stack = []
current = node
while True:
if current != None:
stack.append(current) # L
stack.append(current) # L
current = current.left
elif stack != []:
temp = stack.pop()
print(temp.info) # N
current = temp.right # R
print(temp.info) # N
current = temp.right # R
else:
break


def postorder_itr(node):
# L R N
# 1. Push root to first stack.
Expand All @@ -256,6 +262,7 @@ def postorder_itr(node):
s1.append(temp.right)
print(*(s2[::-1]))


def bst_frm_pre(pre_list):
box = Node(pre_list[0])
if len(pre_list) > 1:
Expand All @@ -272,11 +279,12 @@ def bst_frm_pre(pre_list):
else:
all_less = True
if i != 1:
box.left = bst_frm_pre(pre_list[1 : i])
box.left = bst_frm_pre(pre_list[1:i])
if not all_less:
box.right = bst_frm_pre(pre_list[i:])
return box


# Function to find the lowest common ancestor of nodes with values c1 and c2.
# It return value in the lowest common ancestor, -1 indicates value returned for None.
# Note that both values v1 and v2 should be present in the bst.
Expand All @@ -293,9 +301,10 @@ def lca(t_node, c1, c2):
return current.info
return -1


# Function to print element vertically which lie just below the root node
def vertical_middle_level(t_node):
e = (t_node, 0) # 0 indicates level 0, to left we have -ve and to right +ve
e = (t_node, 0) # 0 indicates level 0, to left we have -ve and to right +ve
queue = [e]
ans = []
# Do a level-order traversal and assign level-value to each node
Expand All @@ -307,7 +316,8 @@ def vertical_middle_level(t_node):
queue.append((temp.left, level - 1))
if temp.right:
queue.append((temp.right, level + 1))
return ' '.join(ans)
return " ".join(ans)


def get_level(n, val):
c_level = 0
Expand All @@ -319,10 +329,11 @@ def get_level(n, val):
n = n.right
c_level += 1
if n is None:
return -1
return -1

return c_level


def depth(node):
if node is None:
return 0
Expand Down
1 change: 0 additions & 1 deletion calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"""

import sys
import math
## Imported math library to run sin(), cos(), tan() and other such functions in the calculator

from fileinfo import raw_input
Expand Down
15 changes: 8 additions & 7 deletions check_internet_con.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from sys import argv

try:
# For Python 3.0 and later
from urllib.error import URLError
Expand All @@ -11,14 +12,14 @@
def checkInternetConnectivity():
try:
url = argv[1]
if 'https://' or 'http://' not in url:
url = 'https://' + url
except:
url = 'https://google.com'
if "https://" or "http://" not in url:
url = "https://" + url
except BaseException:
url = "https://google.com"
try:
urlopen(url, timeout=2)
print("Connection to \""+ url + "\" is working")
urlopen(url, timeout=2)
print(f'Connection to "{url}" is working')

except URLError as E:
print("Connection error:%s" % E.reason)

Expand Down
2 changes: 1 addition & 1 deletion fibonacci.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def getFibonacciIterative(n: int) -> int:
a = 0
b = 1

for i in range(n):
for _ in range(n):
a, b = b, a + b

return a
Expand Down

0 comments on commit 881438b

Please sign in to comment.