首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >恨/爱的难题

恨/爱的难题
EN

Code Golf用户
提问于 2016-08-31 10:15:00
回答 16查看 3.2K关注 0票数 31

挑战描述

在这个挑战中,我们只考虑lovehate作为感觉。如果我们想表达顺序N的情感表达,我们可以在这两者之间交替使用(从hate开始):

代码语言:javascript
复制
order | expression

1       I hate it.
2       I hate that I love it.
3       I hate that I love that I hate it.
4       I hate that I love that I hate that I love it.

对于每个正整数N,模式如下。给定N,输出顺序N的相关情感表达式。

Notes

  • 表达式末尾的句号(.)是强制性的,
  • 允许尾随和引导空格(包括换行符),
  • 非正或非整数N的输出未定义,
  • 这是一个密码-高尔夫挑战,所以让您的代码尽可能短!
EN

回答 16

Code Golf用户

发布于 2016-08-31 10:21:52

Python,54字节

代码语言:javascript
复制
lambda n:("I hate that I love that "*n)[:12*n-5]+"it."
  • Python2:把它放进去!
  • Python3:把它放进去!
票数 21
EN

Code Golf用户

发布于 2016-08-31 10:28:22

CJam,36字节

代码语言:javascript
复制
ri{"hatlov"3/='IS@"e that "}/2<"it."

在网上试试!

解释

代码语言:javascript
复制
ri            e# Read input and convert to integer N.
{             e# For each i from 0 to N-1...
  "hatlov"3/  e#   Push ["hat" "lov"].
  =           e#   Use i as a cyclic index into this array to alternate between
              e#   "hat" and "lov".
  'IS         e#   Push character 'I' and a space.
  @           e#   Pull "hat" or "lov" on top of it.
  "e that "   e#   Push "e that "
}/
2<            e#   Truncate the last "e that " to just "e ".
"it."         e#   Push "it."
票数 15
EN

Code Golf用户

发布于 2016-08-31 11:22:20

Java 8,91个字节

代码语言:javascript
复制
i->{for(int j=0;j++<i;)System.out.printf("I %se %s",j%2>0?"hat":"lov",j<i?"that ":"it.");};

未高尔夫球测试程序

代码语言:javascript
复制
public static void main(String[] args) {
    Consumer<Integer> c = i -> {
        for (int j = 0; j++ < i;) {
            System.out.printf("I %se %s", j % 2 > 0 ? "hat" : "lov", j < i ? "that " : "it.");
        }
    };

    c.accept(1);
    c.accept(2);
    c.accept(3);
}
票数 5
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codegolf.stackexchange.com/questions/91797

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档