首页 > 解决方案 > how to solve this problem:Multi Line Task: Hello World (Easy one) on codewars.com

问题描述

Multi Line Task: Hello World (Easy one)

You are provided with the following functions:

t = type
k = lambda x: lambda _: x

Write a function f that returns the string Hello, world!.

The rules are:

And this is my solution:

s\
=\
'\
H\
e\
l\
l\
o\
,\
 \
w\
o\
r\
l\
d\
'
f\
=\
k\
(\
s\
)

how to improve it,my solution should run by f(''),not f(). please help me.thanks for your help.

标签: python

解决方案


Interesting puzzle. To get around the need to explicitly pass an argument to f, you can create a class with t (which is type), and use k to create a __new__ method that returns 'Hello, world!'. Since __new__ is a class method, _ would become the class object when an instance is instantiated:

f=t('',(),{'__new__':k('Hello, world!')})

so that f() returns: 'Hello, world!'

Break the above code into 2 characters per line, and you get:

f\
=\
t(
''
,(
),
{
'\
_\
_\
n\
e\
w\
_\
_\
':
k(
'\
H\
e\
l\
l\
o\
,\
 \
w\
o\
r\
l\
d\
!'
)}
)

which is exactly 33 lines.


推荐阅读