I used split to remove whitespaces and turn string into list and using built in function I tried to find the max and min values of the list but I gave incorrect ans also I want ans in formate " x y "where x and y are max and min respectively.
When I print list it consist of ' ' every elements of list
Thanks in Advance.
My code:
def high_and_low(numbers):
numbers = numbers.split()
numbers = list(numbers)
return max(numbers),min(numbers)
print high_and_low("4 5 29 54 4 0 -214 542 -64 1 -3 6 -6")
split returns strings, and you don't convert the strings to actual numbers. When comparing strings, the meaning of the comparison is different than when comparing numbers:
>>> '2' > '10'
True
So you need to change your function to something like this:
In [1]: def high_and_low(s):
...: numbers = [int(x) for x in s.split()]
...: return max(numbers), min(numbers)
...:
In [2]: high_and_low("4 5 29 54 4 0 -214 542 -64 1 -3 6 -6")
Out[2]: (542, -214)
min and max take a key so if you don't actually want ints returned you can use key=int so you compare as integers and not strings which is what you are currently doing:
def high_and_low(numbers):
numbers = numbers.split()
return max(numbers,key=int),min(numbers,key=int)
Or use map to cast the strings to int after splitting if you want ints:
def high_and_low(numbers):
numbers = map(int,numbers.split())
return max(numbers,key=int),min(numbers,key=int)
numbers is already a list after splitting so using numbers = list(numbers) is redundant.
from the docs:
sequence objects may be compared to other objects with the same sequence type. The comparison uses lexicographical ordering: first the first two items are compared, and if they differ this determines the outcome of the comparison; if they are equal, the next two items are compared, and so on, until either sequence is exhausted
So for min you only get the correct answer -214 because -2 is < -6 and -3, once you add -1 or anything starting with -1 then you would again see incorrect output.
For the max you get '6' because "6" is greater than the first char of any other substring.
Related
I have the following problem statement:
Write a function odd_finder(a,b,c,d,e,f,g,h,i,j) which takes 10 integers as inputs and returns the count of positive odd integers. For instance, if the 10 integers inputted by the user are 1,2,3,4,5,-1,-2,-3,-4,0 then output by the function will be 3 (3 positive odd integers: 1,3 and 5).
I wrote this code:
def odd_finder(a,b,c,d,e,f,g,h,i,j):
count = 0
for number in range(10):
if(number % 2 != 0 & number >= 0):
count = count + 1
print(count)
For the example input, my code prints 5 instead of the correct answer of 3. What is wrong with the code? I'm not sure if the issue is with my range or with the operator for evaluating positive integers as number > 0 prints overall count of 0.
There are some issues with the function you have written:
You are taking values a through j as parameter, but not using them, you are just using the values coming from range(10) which will always be 0 through 9 no matter what are the values passed to the function for a through j.
You are combining the conditions using bitwise and operator &, which has higher precedence than !=, and >=, take a look at precedence table
You are comparing against zero number >= 0 which will be true for number=0, but 0 is not an odd integer.
A modified version of the function would look something like this:
def odd_finder(a,b,c,d,e,f,g,h,i,j):
numbers = (a,b,c,d,e,f,g,h,i,j) #Create a sequence to iterate
count = 0
for number in numbers: #range(10):
if(number % 2 != 0 and number > 0):
count = count + 1
print(count)
OUTPUT:
>>> odd_finder(1,2,3,4,5,-1,-2,-3,-4,0)
3
The first issue is fixed by creating a sequence from the values a through j in order to iterate these values. You can then replace range(10) by iterating numbers
The second issue is fixed by replacing bitwise and operator & by logical and operator
The third issue is fixed by using greater than operator > in place of greater than or equal to >=
I am trying to write a recursive function called my_minimum that receives a list of integers as parameter and returns the minimum stored in the list. I am able to get the user to input integers separated by a space. However, it gives me a number totally unrelated to what I am trying to achieve.
For example if user list was: 67 89 45 34 23 3 45 67 78, it should give me give me 3 instead of 23.
def my_minimum(A, n):
# if size = 0 means whole list
# also if it has been traversed
if (n == 1):
return A[0]
return min(A[n - 1], my_minimum(A, n - 1))
# Driver Code
if __name__ == '__main__':
input_string= input("Enter a list element separated by space: ")
A = input_string.split()
n = len(A)
print(my_minimum(A, n))
This is a string comparison issue, not an issue with your recursion. The string "3" compares as greater than the string "23", just like "B" sorts after "AA" in lexicographical order.
Try converting your strings to integers if you want integer ordering:
A = list(map(int, input_string.split()))
This question already has answers here:
Change a string of integers separated by spaces to a list of int
(8 answers)
Closed 1 year ago.
I have a string of numbers and I'm looking for the highest number in the string. I've tried to use the max() function which returned number 6 while the highest number in the string is 542. Then I used sorted() to check again and it returned 6 as the final number instead of 542. How is this possible?
Here is the code and the string of numbers:
def high_and_low(numbers):
return sorted(numbers.split())
high_and_low("4 5 29 54 4 0 -214 542 -64 1 -3 6 -6")
The issue is the fact that the numbers are concatenated into one string. If you run the code below, which searches for the largest number in an array of int (integers), the program will output the number 542.
nums = [4, 5, 29, 54, 4, 0, -214, 542, -64, 1, -3, 6, -6]
def high_and_low(numbers):
return max(numbers)
print(high_and_low(nums))
Note: There is also no need to call sorted since max will find the largest number regardless if the numbers are sorted.
Because the items are string! string items are compared different than integers. You can use key argument to specify the way you want max , min, sorted , .sort work. Every item in iterable goes into that callable (here int) then the results are get compared.
def high_and_low(numbers):
return max(numbers.split(), key=int)
print(high_and_low("4 5 29 54 4 0 -214 542 -64 1 -3 6 -6"))
def my_max():
#using input to collect number to list
list_a = input("print your list with numbers: ").split(",")
# Searching for the highest number
max = 0
for i in list_a:
if i > str(max):
max = i
print(max)
my_max()
When i write numbers to input, sometimes the highest number is being printed, but not always.
For an example, if i write :"54,64,446 "
the number "64 is being printed. Do anybody knows why?
You need to map it into list of ints before you do the logic:
def my_max():
# using input to collect number to list
list_a = input("print your list with numbers: ").split(",")
# Searching for the highest number
return max(map(int, list_a))
print(my_max())
Sample run:
print your list with numbers: 54,64,446
446
Splitting on ',' gives you a list of strings. What you observed is an expected behaviour because you find max of a list of strings in contrast to list of integers.
Without using a max(), I would go something like this:
def my_max():
# using input to collect number to list
list_a = list(map(int, input("print your list with numbers: ").split(",")))
# Searching for the highest number
max = list_a[0]
for x in list_a[1:]:
if x > max:
max = x
return max
print(my_max())
Your list_a contains strings, not numbers. When you do your comparison, you are comparing the values of these strings. The result of this is the highest value alphabetically, rather than numerically.
Taken as strings rather than numbers, 64 > 54 > 446
Given List:
l = [1,32,523,336,13525]
I am having a number 23 as an output of some particular function.
Now,
I want to remove all the numbers from list which contains either 2 or 3 or both 2 and 3.
Output should be:[1]
I want to write some cool one liner code.
Please help!
My approach was :
1.) Convert list of int into list of string.
2.) then use for loop to check for either character 2 or character 3 like this:
A=[x for x in l if "2" not in x] (not sure for how to include 3 also in this line)
3.) Convert A list into integer list using :
B= [int(numeric_string) for numeric_string in A]
This process is quiet tedious as it involves conversion to string of the number 23 as well as all the numbers of list.I want to do in integer list straight away.
You could convert the numbers to sets of characters:
>>> values = [1, 32, 523, 336, 13525]
>>> number = 23
>>> [value for value in values
... if set(str(number)).isdisjoint(set(str(value)))]
[1]
You're looking for the filter function. Say you have a list of ints and you want the odd ones only:
def is_odd(val):
return val % 2 == 1
filter(is_odd, range(100))
or a list comprehension
[x for x in range(100) if x % 2 == 1]
The list comprehension in particular is perfectly Pythonic. All you need now is a function that returns a boolean for your particular needs.