예제 #1
0
#!/usr/bin/env python
"""
Binary Search

This was supposed to be a binary search algorithm but it isn't working...
I used the Iterative implementation from here:
    http://en.wikipedia.org/wiki/Binary_search_algorithm
"""
from hwtools import input_nums

while True:
    try:
        nums = input_nums() # User inputs a list of numbers.

        # Finding if there are any duplicate numbers.
        for num in nums:
            if nums.count(num) > 1:
                raise NameError ('duplicate')
        break
    
    # If user enters something other than a number, print an error statement.
    except ValueError:
        print "Please enter numbers."   
    
    # If user enters duplicate numbers, print an error statement.
    except NameError:
        print "Please don't repeat numbers."

nums_sort = sorted(nums) # User's number list sorted.
print "I have sorted your numbers"
print nums_sort
예제 #2
0
#!/usr/bin/env python
"""
Selection sort

This is my selection sort, it's not working right!!!
I used this:
    http://en.wikipedia.org/wiki/Selection_sort
"""
from hwtools import input_nums

nums = input_nums()

print "Before sort:"
print nums

max=len(nums)#End of list
for x in range(0,max):
    test=x
    for i in range(x+1, max):
        if nums[i]<nums[test]:
            test=i
    nums[x],nums[test]=nums[test],nums[x]

print "After sort:"
print nums
예제 #3
0
#!/usr/bin/env python
"""
Selection sort

This is my selection sort, it's not working right!!!
I used this:
    http://en.wikipedia.org/wiki/Selection_sort
"""
from hwtools import input_nums

while True:
    try:
        nums = input_nums() # Number input.
        break

    # Error message.
    except ValueError:
        print "Please enter numbers."

# Number list.
print "Before sort:"
print nums

max_position = len(nums) # The max position of the number of elements in the list.

for current_position in range(max_position):

    current_min = current_position # The minimum number starts out as the current position.

    # Tests the position to the right of the last current position within the range of the max position.
    for test_position in range(current_position + 1 , max_position):
예제 #4
0
#!/usr/bin/env python
"""
Selection sort

This is my selection sort, it's not working right!!!
I used this:
    http://en.wikipedia.org/wiki/Selection_sort
"""
from hwtools import input_nums

nums = input_nums() #you forgot a _

print "Before sort:"
print nums

Length =len(nums) #value N is now Length.  No need to subtract 1.
for x in range(Length):
    p=x #saves the original value at that address. P changes
   
    for i in range(x+1, Length): #i is the new value, p is saved value.
        if nums[i]<nums[p]:
            p=i
            
    nums[p],nums[x]=nums[x],nums[p] #this needs to stay inside the first For loop.

print "After sort:"
print nums
예제 #5
0
#!/usr/bin/env python

from hwtools import input_nums

Section 1:  What Does This Code Do?
=====================================

## question 1
a = input_nums()
b = None
for c in a:
    if b is None or c >= b:
        b = c
print b
## end of question 1
print "Question 1: _______"



## question 2

## end of question 2
print "Question 2: _______"



## question 3
t1 = []
t2 = []
while len(t1) != 2:
    t1 = input_nums()
예제 #6
0
#!/usr/bin/env python
"""
Selection sort

This is my selection sort, it's not working right!!!
I used this:
    http://en.wikipedia.org/wiki/Selection_sort
"""
from hwtools import input_nums

while True:
    try:
        nums = input_nums() # User enters list of numbers.
        break

    # If the user enters something other than a number, print error message.
    except ValueError:
        print "Please enter numbers."

# The number list before it is sorted.
print "Before sort:"
print nums

max_position = len(nums) # The max position is the number of elements in the list.

for current_position in range(max_position):

    current_min = current_position # The current minimum starts out as the current position.

    # This tests the position to the right of the last current position within the range of the max position.
    for test_position in range(current_position + 1 , max_position):
예제 #7
0
#!/usr/bin/env python
"""
Binary Search

This was supposed to be a binary search algorithm but it isn't working...
I used the Iterative implementation from here:
    http://en.wikipedia.org/wiki/Binary_search_algorithm
"""
from hwtools import input_nums

while True:
    try:
        nums = input_nums() # Number Input

        # Checking for duplicates
        for num in nums:
            if nums.count(num) > 1:
                raise NameError ('duplicate')
        break
    
    # Checing for non numbers.
    except ValueError:
        print "Please enter numbers."   
    
    # Error statement
    except NameError:
        print "Please don't repeat numbers."

nums_sort = sorted(nums) # User's number list sorted.
print "I have sorted your numbers"
print nums_sort