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

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

Related

Function that returns even numbers from a list and halves them [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 1 year ago.
The community is reviewing whether to reopen this question as of 1 year ago.
Improve this question
So I am having trouble concatenating this, I am not allowed to use .append(), and right now Im getting the error 'int' object not iterable.
def halveEvens(l):
num = []
for n in l:
if n % 2 == 0:
num += (n // 2)
return num
print(halveEvens([10,21,32,42,55]))```
sum(x//2 for x in numbers if x%2 == 0)
is probably how I would do it
if you just want to collect them (without summing them)
generator (x//2 for x in numbers if x%2 == 0) would be evaluated as you iterate it
or list comprehension that is evaluated immediatly
[x//2 for x in numbers if x%2 == 0]

How can I generate all possible words with a specified set of characters? [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 4 years ago.
Improve this question
I play to HackNet game and i have to guess a word to bypass a firewall.
The key makes 6 characters long and contains the letters K,K,K,U,A,N.
What is the simplest way to generate all possible combinations either in bash or in python ? (bonus point for bash)
Here is a backtracking-based solution in Python:
db = {"K" : 3, "U" : 1, "A" : 1, "N" : 1}
N = 6
def possibilities(v):
l = []
for k in db.keys():
if v.count(k) < db[k]:
l.append(k)
return l
def generateImpl(a):
if len(a) < N:
lst = []
for c in possibilities(a):
lst += generateImpl(a+[c])
return lst
else:
return [''.join(a)]
def generate():
return generateImpl([])
Just run generate() to obtain a list of possible words.
You have 6 letters and need to find a combination of 6 letters. If you are not using the same character in ['K','K','K','U','A','N'] again and again, then there is only 1 permutation.
If you can use the same character again and again, you can use the following code to generate all possible combinations.
import itertools
y = itertools.combinations_with_replacement(['K','U','A','N'],6)
for i in list(y):
print(i)

How to form a given list of elements in python to create another set of lists from it? [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 5 years ago.
Improve this question
List1 =['000095', '000094', '000092', '000101', '000099', '000096', '000095']
def makecycle(list, startElement):
A loop which forms the bottom list which is made from the upper one's elements!
if i pass that function the start element and the list it shoul print like this:
makecycle(list1, 000094) it should print:
['000094', '000092', '000101', '000099', '000096', '000095', '000094']
and if pass
makecycle(list1, 000101) it should print:
['000101', '000099', '000096', '000095', '000094', '000092', '000101']
and if pass
makecycle(list1, 000092) it should print:
['000092', '000101', '000099', '000096', '000095', '000094', '000092']
i know its kinda not clear enough but thats all i can point!
def makecycle(list1,startElement):
ind = list1.index(startElement)
l = len(list1)
i = ind
list2 = []
while ind < (l-1):
list2.append(list1[ind])
ind = ind + 1
for x in range(i):
if list1[x] not in list2:
list2.append(list1[x])
print(list2)

The digits of a number in ascending order. (comparing) [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 6 years ago.
Improve this question
cc = input('Input the number: ')
b = str(cc)
c = []
for digit in b:
c.append (int(digit))
csort = c.sort(key=int)
c == csort #??
I need to say True or False if the number is in ascending order or not.
My code do not print True or False, why?
You should not use key=int in c.sort(key=int) as c is already a list of ints due to you creating it via c.append(int(digit)).
However the key issue is that c.sort() is in-place and as such returns None, instead use sorted which returns a sorted list:
csort = sorted(c)
And then you can print the boolean result of the comparsion via:
print c == csort

Creating lists with variable lengths Python [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 7 years ago.
Improve this question
I need to create a list with a length determined by a user input.
How would I do this?
Example: If the user inputs 3 I need a list with 3 indexes.
What do you want to fill the list with? If you just want a list with n indexes:
n = user_input_length
list = [None for x in range(n)]
You can take in input, then populate your list:
import random
num = int(input('How long do you want the list? ')) #5
lst = [random.randint(1, 10) for i in range(num)]
print lst #[6, 1, 2, 1, 8]

Categories