Deleting max value in set without max() [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a working code
task = set()
x = 1
while x != 0:
x = int(input('input your number '))
task.add(x)
print('Just 0 could stop it!')
task.remove(max(task))
print(max(task))
And need to get the same result without using max(). What could be an alternative?

Something like this, unless you have really large sets, I don`t see the advantage
task = set()
x = 1
m = 0
while x != 0:
x = int(input('input your number '))
task.add(x)
if x > m:
m = x
print('Just 0 could stop it!')
task.remove(m)
print(max(task))
Notice this will only work for positive numbers, if you want to the complete int range you should init m like this m = -sys.maxsize - 1

You could use min with a key arg that inverts the element:
>>> task = {1, 2, 3, 4, 5}
>>> max(task)
5
>>> min(task, key=lambda x: -x)
5
Or you could sort it and take the last element...
>>> sorted(task)[-1]
5

Related

Tuple into list to find max value and if there is a duplicate [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I am creating a function that takes any quantity of numbers, and tell you what is the max value or if there is a tie for largest. I am wondering what I could do to simplify what I have.
def max_num(*args):
nums = []
nums_1 = []
nums.append(args)
i = 0
while i < len(nums[0]):
nums_1.append(nums[0][i])
i += 1
c = max(nums_1)
nums_1.remove(c)
if c in nums_1:
print("It's a tie!")
else:
print(c)
max_num(-10, 0, 10, 10)
So when I initially make a list with the arguments given, it gives me a tuple inside the list. This is why I create a new list to dissect the tuple into separate values. I have the feeling that wasn't necessary, and that there is a much simpler way to do this. Any advice would be great.
Just get the max, and count how many times it appears in your data:
def max_num(*args):
maxi = max(args)
if args.count(maxi) == 1:
print(maxi)
else:
print('Tie')
max_num(2, 5, 1)
#5
max_num(2, 5, 1, 5)
#Tie

Multiply digits in list [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Let's say we have list of numbers and we want to multiply all numbers in it as long as product is digit.
For Example:
[2,2,5] => [4,5]
[3,3,7] => [9,7]
[5,5,5,5,5] => [5,5,5,5,5]
Can I somehow use functools.reduce here? What's pythonic way to solve this problem?
This can be solved easily by a simple stateful algorithm:
def multiply_digits(lst):
res = []
for x in lst:
if res and res[-1] * x < 10:
res[-1] *= x
else:
res.append(x)
return res
While there is an equivalent functional way (with reduce), that will not be as simple since you either need to reassemble the result list in each step, or carry the current number value separately.
This might do the trick:
def process(lst):
lst = sorted(lst)
last = 1
result = []
for el in lst:
if last * el >= 10:
result.append(last)
last = el
continue
last *= el
result.append(last)
return result
This is better I guess:
from numpy import product
reduce(lambda x , y : (x[0:-1]+[y*x[-1]] if product(x+[y])<10 else x+[y]) if len(x)>0 else [y] ,[21,1,2,3,4,5,6],[])

Python: How to print position and value of list element? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a list which includes infinite an unknown number of elements (I don't have control on the length of the list or the type of values)
a = [x, y, z ...]
where x = 1, y = 9, z = 'Hello' ...
And I would like to loop over list "a" and print all the elements' names and values.
I hope if I can implement something like that:
for i in a:
print i $i
I would like to have output as:
x 1
y 9
z Hello
from itertools import cycle # an infinite repeating set
my_infinite = cycle(["a","b","c"])
a=1,b=4,c=99
for val in my_infinite:
print val, globals()[val]
maybe ... its aweful and hackey but it might work
Something like this:
import string
for a, b in zip(string.ascii_lowercase, xrange(1, 27)):
print a, b
You should use a dictionary:
a = {'x': 1, 'y': 2}
for i in a:
print i, a[i]

Python: Total sum of a list of numbers with the for loop [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
The community reviewed whether to reopen this question last year and left it closed:
Original close reason(s) were not resolved
Improve this question
I'm new to Python and I have this problem:
I need to program a Python function that gives me back the sum of a list of numbers using a for loop.
I just know the following:
sum = 0
for x in [1,2,3,4,5]:
sum = sum + x
print(sum)
I think what you mean is how to encapsulate that for general use, e.g. in a function:
def sum_list(l):
sum = 0
for x in l:
sum += x
return sum
Now you can apply this to any list. Examples:
l = [1, 2, 3, 4, 5]
sum_list(l)
l = list(map(int, input("Enter numbers separated by spaces: ").split()))
sum_list(l)
But note that sum is already built in!
l = [1,2,3,4,5]
sum = 0
for x in l:
sum = sum + x
And you can change l for any list you want.
x=[1,2,3,4,5]
sum=0
for s in range(0,len(x)):
sum=sum+x[s]
print sum

Python : Subtracting inside an array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to subtract the values in an array with 10 values that the user inputs. So far I can't find how to do that. This is what I have...
g = 0
q = []
for s in range(9):
while g < 10:
n = input()
q.append(int(n))
g = g+1
add = sum(Q)
sub =
There are more succinct ways to do this; I've opted instead for readability:
# get our initial input:
n = input()
result = int(n)
# subtract the rest of the user's inputs:
for x in range(9):
n = input()
result -= int(n)
# ... do something with the result ...
You don't need to assign all of those to individual variables. At each iteration of the loop, you could just append the newly input value to the array:
q = []
g = 0
while g < 10:
n = input()
q.append(int(n))
g = g + 1
At the end of this loop, q will contain the 10 values that the user entered.
It's not clear to me what needs to be subtracted from what, but that might get you a little closer to where you need to be.
Be pythonic
a = [int(input()) for x in range(10)]
Or for python 2.X
a = [int(raw_input()) for x in xrange(10)]
This gives you a list containing 10 integers.
Then you can
q = map(lambda x: x-sum(a), q),
which subtracts the sum of user inputs
Just use python API
li = []
for x in xrage(10):
li.append(input())
result = reduce(lambda x, y: x - y, li)

Categories