-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Selection Sort in Python #1 * Added Binary to Decimal and Deciaml to Binary
- Loading branch information
1 parent
431f8be
commit a746f0c
Showing
2 changed files
with
67 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,49 @@ | ||
class BinaryError(Exception): | ||
def __str__(self): | ||
return "Not a valid binary number" | ||
|
||
class DecimalError(Exception): | ||
def __str__(self): | ||
return "Not a valid decimal number" | ||
|
||
def bin2dec(input_string): | ||
num = 0 | ||
for character in input_string: | ||
if character == '0': | ||
num = num * 2 | ||
elif character == '1': | ||
num = num * 2 + 1 | ||
else: | ||
#This will call for error and ask user to try again | ||
raise BinaryError() | ||
return num | ||
|
||
def dec2bin(input_string): | ||
try: | ||
input_string = int(input_string) | ||
num = bin(input_string) | ||
except: | ||
#This will call for error and ask user to try again | ||
raise DecimalError() | ||
return num | ||
|
||
while True: | ||
try: | ||
option = input("Enter choice (1 for Binary to Decimal, 2 for Decimal to Binary: ") | ||
#This will ask user for which conversion to be made | ||
if option == '1': | ||
print (bin2dec(input("Please enter a binary number: "))) | ||
elif option == '2': | ||
print (dec2bin(input("Please enter a decimal number: "))) | ||
else: | ||
#This will be printed if input is invalid | ||
print("Invalid Option") | ||
|
||
except BinaryError: | ||
#This will be printed if input is invalid | ||
print ("Enter a Binary number. Please try again.\n") | ||
except DecimalError: | ||
#This will be printed if input is invalid | ||
print ("Enter a Decimal number. Please try again.\n") | ||
else: | ||
break |
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 @@ | ||
## import 'sys' library | ||
import sys | ||
## A is the input Array(Unsorted) | ||
A = [64, 25, 12, 22, 11] | ||
|
||
for i in range(len(A)): | ||
min_idx = i | ||
for j in range(i+1, len(A)): | ||
if A[min_idx] > A[j]: | ||
min_idx = j | ||
A[i], A[min_idx] = A[min_idx], A[i] | ||
|
||
## A is sorted Array | ||
|
||
## This will print array elements line by line | ||
print ("Sorted array") | ||
for i in range(len(A)): | ||
print(A[i]) |