# Shortening this Code to process nested tuple even further without the use of max()

I have the code:

marks = tuple(tuple((input("Enter subject: "), input("Enter marks: "))) for _ in range(int(input("Enter number of tuples: "))))
print(tuple((s[:3].upper(), max(int(m) for t, m in marks if t == s)) for s in set(t for t, m in marks)))

#INPUT
(("Physics",131),("Chemistry",30),("Physics",100),("Computers",50),("Chemistry",20))
#OUTPUT
(('COM', 50), ('PHY', 131), ('CHE', 30))


As you can see, the output is a nested tuple of the first three capitalized letters of the subject and the maximum mark given for each subject.

You can assume that Physics, Chemistry and Computers are the only subjects that the user inputs, and that they always use the same spelling for the subjects.

The order of the subjects in the output does not matter.

Is there anyway I can do the same without using the max function? The length of the code should optimally be at 2 lines or if at all possible, a single line. The maximum limit of the number of lines of code is 3.

The solution should try to contain no built in functions if possible. Functions like sort(), sorted() and max() are strictly not allowed.

• You can replace tuple(x) with (*x,) to save some bytes Mar 21 at 13:41

# 1 line, 180173 171 bytes

7 bytes saved thanks to Jonathan Allen

exec("x={}\n"+"if x.get(a:=input('Enter subject: ')[:3].upper(),b:=int(input('Enter marks: ')))<=b:x[a]=b\n"*int(input("Enter number of tuples: "))+"print((*x.items(),))")


Attempt This Online!

# 3 lines, 171163 160 bytes

8 bytes saved thanks to Jonathan Allen

x,i,e={},input,'Enter '
exec("if x.get(a:=i(e+'subject: ')[:3].upper(),b:=int(i(e+'marks: ')))<=b:x[a]=b\n"*int(i(e+"number of tuples: ")))
print((*x.items(),))


Attempt This Online!

Here's another approach that yields fewer bytes. It can be done in 1, 2 or 3 lines, with more lines enabling shorter overall programs. There's probably still even some improvements to be made here.

• Depending on the rules, you might be able to replace the last print with i Mar 26 at 13:49
• I mean, the second one can be one line too, just replace the newlines with semicolons Mar 26 at 13:56
• @naffetS I don't think that's really what is meant by lines. Mar 26 at 14:00
• 158 for the second one Mar 26 at 14:02

# Python, 204 188 bytes

-16 bytes thanks to @WheatWizard

m=[*((input("Enter subject: "),int(input("Enter marks: ")))for _ in'*'*int(input("Enter number of tuples: ")))]
print((*((i[:3].upper(),j)for i,j in m if all(y!=i or z<=j for y,z in m)),))


Attempt This Online!

# Python, 212 bytes

1 line

print((*[m:=[*((input("Enter subject: "),int(input("Enter marks: ")))for _ in range(int(input("Enter number of tuples: "))))]][:0],*((i[:3].upper(),i)for i in m if all(z!=ior z<=ifor z in m)),))


Attempt This Online!

These are just some trivial golfs on your approach. It could probably be made much shorter. If there are ties this will print both.

• This is insane, thank you. I never thought about using the all operator that way Mar 21 at 14:21
• Here's 202 bytes and 3 lines Mar 21 at 17:17
• You can save bytes on all of the options by pattern matching tuples Mar 21 at 17:22
• @mousetail Yes, I interpreted it as that as well. Two of the golfs work on all of the answers. And they did say 3 lines was ok. Mar 21 at 17:30