Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old September 11th, 2008, 01:32 AM
Newbie
 
Join Date: Sep 2008
Posts: 1
Default Finding the median of user input string of digits using ARGV

Hello,
I am working on a code that takes the users input ( a string of digits) from the command line and prints out the median number. I am trying to accomplish this by:

from sys import argv

nums = argv[1:]


for index, value in enumerate(nums):
nums[index] = float(value)

print nums[1:] + nums[:1] / 2

I keep getting the error:
type error: unsupported operand type(s) for /: 'list' and 'int'
Reply
  #2  
Old September 11th, 2008, 03:30 AM
Expert
 
Join Date: Sep 2007
Posts: 846
Default

Slicing always returns a list, even if that list only has one operator. As such, nums[1:] is a list, as is nums[:1].

The builtin sum(list, start=0) function will probably help you here.
Reply
  #3  
Old September 11th, 2008, 02:30 PM
bvdet's Avatar
Expert
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,214
Default

Quote:
Originally Posted by curien
Hello,
I am working on a code that takes the users input ( a string of digits) from the command line and prints out the median number. I am trying to accomplish this by:

from sys import argv

nums = argv[1:]


for index, value in enumerate(nums):
nums[index] = float(value)

print nums[1:] + nums[:1] / 2

I keep getting the error:
type error: unsupported operand type(s) for /: 'list' and 'int'
First, let's consider the definition of the median number of a sequence of numbers. The median is the number where half of the numbers are larger and half are smaller. Therefore the list must be sorted. If the list contains an even number of items, the median is taken as the mean or average between the two middle numbers. The following function returns the median number from a sorted list of numbers:
Expand|Select|Wrap|Line Numbers
  1. def median(s):
  2.     i = len(s)
  3.     if not i%2:
  4.         return (s[(i/2)-1]+s[i/2])/2.0
  5.     return s[i/2]
Example:
Expand|Select|Wrap|Line Numbers
  1. >>> s1 = "100 45 89 23 105 220 45 66 8 75"
  2. >>> s2 = "100 45 89 23 105 220 45 66 8 75 7"
  3. >>> sList = [float(num) for num in s1.split()]
  4. >>> sList.sort()
  5. >>> median(sList)
  6. 70.5
  7. >>> sList
  8. [8.0, 23.0, 45.0, 45.0, 66.0, 75.0, 89.0, 100.0, 105.0, 220.0]
  9. >>> sList = [float(num) for num in s2.split()]
  10. >>> sList.sort()
  11. >>> median(sList)
  12. 66.0
  13. >>> sList
  14. [7.0, 8.0, 23.0, 45.0, 45.0, 66.0, 75.0, 89.0, 100.0, 105.0, 220.0]
  15. >>> 
Reply
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles