-3

For Python,

def fun1(name, ext):
    if ext == '' :
        fname = name + '$'
    else:
        fname = name + ext
    return fname

I have to make fun2 as short as possible with less than 25 characters that work exactly the same as fun1.

So far, I made

def fun2(name, ext):
    if ext == '':
        ext = '$'
    return name + ext

but it still exceeds 25 characters. Is there any possible way to accomplish this without any external tools or extensions?

JungSoo Ok
  • 11
  • 4

3 Answers3

1

Using python's ternary operator

def fun1(name, ext):
    return name+'$' if ext=='' else name+ext

See more: https://www.geeksforgeeks.org/ternary-operator-in-python/

imdevskp
  • 2,103
  • 2
  • 9
  • 23
1
def fun2(name, ext):
    e = ext or '$'
    return name + e
Koko Jumbo
  • 313
  • 1
  • 5
0

i think this is the shortest way without ext==''

def fun2(name, ext):
    return name+ext if ext else name+"$"

George Imerlishvili
  • 1,816
  • 2
  • 12
  • 20