Skip to content

Commit

Permalink
Add Selection Sort in Python (#19)
Browse files Browse the repository at this point in the history
* Add Selection Sort in Python

#1

* Added Binary to Decimal and Deciaml to Binary
  • Loading branch information
madhurgupta10 authored and the-cybersapien committed Oct 20, 2017
1 parent 431f8be commit a746f0c
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
49 changes: 49 additions & 0 deletions Python/Conversions/Decimal2Binary(& Reverse)
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
18 changes: 18 additions & 0 deletions Python/Sort/SelectionSort
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])

0 comments on commit a746f0c

Please sign in to comment.