python - 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:
- The solution should not exceed 33 lines
- Every line should have at most 2 characters
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.
解决方案
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.
推荐阅读
- haskell - 为什么我无法导入模块?
- ios - IOS 应用程序在强制关闭后收到 pushkit voip 通知时崩溃
- matlab - 创建一个包含变量名称单元格和双精度数组单元格的结构
- sql - 在 Oracle 中插入带单引号或撇号的列
- angular - 来自其他组件的 Angular @Input() 数组,问题
- machine-learning - 反向传播示例
- kaggle - kaggle 直接从复制的内核下载输入数据
- python - 使用 python sort() 对 json 文件进行排序 - TypeError
- javascript - How to expand down a "Read more..." section using CSS and jQuery
- r - R 脚本输出值到另一个文件夹或目录