증오 / 사랑 수수께끼


30

도전 설명

이 문제에서, 우리는 생각 lovehate감정을. 우리가 느낌의 표현표현 하고 싶다면 N, 우리는이 두 가지를 번갈아 가며 살펴 본다 hate:

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.

노트

  • .식 끝에있는 완전 정지 ( )는 필수입니다.
  • 후행 및 선행 공백 (줄 바꾸기 포함)이 허용됩니다.
  • 비 양성 또는 비정 수의 출력 N은 정의되지 않습니다.
  • 이것은 문제이므로 가능한 한 코드를 짧게 만드십시오!


1
꽤 혼란 스럽습니다. order입력과 expression출력도 마찬가지 입니까?
Whothehellisthat

2
@Whothehellisthat 그래, 맞아. (PPCG에 오신 것을 환영합니다! :)
Martin Ender

@Whothehellisthat : 그렇습니다. 아래 제출에서 볼 수 있듯이 stdin을 통해 입력을받을 수 있지만 메소드 (함수)를 정의하는 것이 더 짧은 경우가 많습니다.
shooqie

1
나는이 질문과 그 답변을 좋아한다는 것을 싫어합니다!
Arkiliknam

답변:



15

CJam , 36 바이트

ri{"hatlov"3/='IS@"e that "}/2<"it."

온라인으로 사용해보십시오!

설명

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."

7

C, 83 76 75 74 바이트

11 바이트를 저장하고 4 바이트를 추가 한 @Leaky Nun에게 감사합니다!
바이트를 저장 한 @YSC에게 감사합니다!

i;f(n){for(i=0;n--;)printf("I %se %s",i++%2?"lov":"hat",n?"that ":"it.");}

Ideone에서 사용해보십시오


1
i=0;while(n--)-> for(i=0;n--;)1 문자를 저장합니다.
YSC

6

자바 스크립트 (ES6), 75 73 70 바이트

n=>[...Array(n)].map((_,i)=>i&1?'I love':'I hate').join` that `+' it.'

Neil 덕분에 2 바이트 절약
절약 Whothehellisthat 덕분에 3 바이트 절약

테스트

let f =
n=>[...Array(n)].map((_,i)=>i&1?'I love':'I hate').join` that `+' it.'

console.log(f(1))
console.log(f(2))
console.log(f(3))
console.log(f(4))


3 바이트를 절약하십시오 : ['I hate','I love'][i&1]->i&1?'I love':'I hate'
Whothehellisthat

@Whothehellisthat-감사합니다! 나는 그 것을 놓쳤다.
Arnauld

5

자바 8, 91 바이트

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

언 골프 테스트 프로그램

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);
}

Why not remove the whitespace? c=i->for(...)
shooqie

I simply forgot.
Shaun Wild

Nice, you've beat me to it. +1 And probably shorter than my answer would be. PS: I would indicate this as 'Java 8' instead of just 'Java'. Not mandatory however, just personal preference from me since I usually write my answers in Java 7 (and since Java 9 is incoming).
Kevin Cruijssen

@KevinCruijssen You tell me this everytime ;) fine
Shaun Wild

@SeanBean Well, usually I already have my Java 7 answer, and you post a shorter answer, which can then most of the time be golfed even more. ;P (This time I can't find anything to make it shorter, though. But perhaps someone else is able to do so.)
Kevin Cruijssen


5

Jelly, 25 bytes

“ṅɠT5“£ẏkg⁷»ṁj“¥ıQ»ṙ1“it.

Try it online!

Explanation

                             Input: n, a number.
“ṅɠT5“£ẏkg⁷»                 Compressed string array [' I hate', ' I love']
            ṁ                Cycle for n repetitions.
             j“¥ıQ»          Join by compressed string ' that'.
                   ṙ1        Rotate left once: move the initial space to the end.
                     “it.    Implicitly print the result, then print 'it.'

I need an explanation for this.
Steven H.

4

05AB1E, 34 32 27 bytes

Saved 5 bytes thanks to Adnan.

“I«¢€Š I„΀Š “×¹12*5-£…it.«

Explanation

“I«¢€Š I„΀Š “               # "I hate that I love that "
              ×              # repeat input nr of times
               ¹12*5-£       # take the first input*12-5 chars of string above
                      …it.«  # append "it."
                             # implicitly display

Try it online!


4

R, 79 bytes

n=scan();for(i in n:1)cat(if((i+n)%%2)"I love"else"I hate",if(i>1)"that "else"it.")

Luckily in R, the default separator for cat is a space.

(Edited from original 73 byte version which didn't quite solve the problem.)


Neat use of the for loop and %%. +1
Billywob

2

Retina, 42 38 bytes

Thanks to Leaky Nun for helping me golf it!

11
1I love n
1
I hate n
n$
it.
n
that 

Input is taken in unary.

Try it online!

Explanation

11
1I love n

Replace every pair of 1s with 1I love n.

1
I hate n

Replace the remaining 1s with I hate n.

n$
it.
n
that 

Replace the n at the end of the line with it. and every other n with that .


You can save four more by dropping l: retina.tryitonline.net/…
Martin Ender

@MartinEnder: I think I ninja'd you to that edit :P
Business Cat

1
Timestamps say you were 9 seconds late. :P
Martin Ender

1

Javascript (ES5), 99 94 bytes

function(c){for(d="",b=0;b<c;++b)d+=(b%2?"I love ":"I hate ")+(b==c-1?"it.":"that ");return d}

Saved 5 bytes thanks to Leaky Nun.

OLD 99 byte solution:

function(c){for(d="",b=0;b<c;++b)d+=(0==b%2?"I hate":"I love")+" "+(b==c-1?"it.":"that ");return d}

Another 98 byte solution:

function(d){for(c=[],b=0;b<d;++b)c.push(["love","hate"][b%2]);return"I "+c.join(" that I ")+" it"}

My code before minification:

function a(n){
  var hate="I hate",love="I love",it="it ",that="that ",out="";
  for(var i=0;i<n;++i){out+=(i%2==0?hate:love)+" "+(i==n-1?it+".":that)}return out;
}

1
function(c){for(d="",b=0;b<c;++b)d+=(b%2?"I love ":"I hate ")+(b==c-1?"it.":"that ");return d}
Leaky Nun

1

Haskell, 70 bytes

g 1="I hate";g n=g(n-1)++" that "++cycle["I love",g 1]!!n
(++" it.").g

1

PowerShell v2+, 64 bytes

((1..$args[0]|%{('I love','I hate')[$_%2]})-join' that ')+' it.'

Rather straightforward. Loops from 1 up to the input $args[0], each iteration placing either 'I love' or 'I hate' on the pipeline, based on a pseudo-ternary for modulo-2 (i.e., it alternates back and forth, starting with 'I hate'). Those strings are encapsulated in parens and -joined with ' that ' to smush them together, then string concatenation ' it.' at the end.

Test Cases

PS C:\Tools\Scripts\golfing> 1..5|%{.\hate-love-conundrum.ps1 $_}
I hate it.
I hate that I love it.
I hate that I love that I hate it.
I hate that I love that I hate that I love it.
I hate that I love that I hate that I love that I hate it.

1

php, 64 62 bytes

<?=str_pad("",$argv[1]*12-5,"I hate that I love that ")."it.";

Unfortunately I couldn't work out a way to avoid repeating the " that I ", or at least no way to do it in less than 7 bytes.

edit: saved 2 bytes thanks to @Jörg Hülsermann


1

Perl, 62 54 50 bytes

$_="I xe tx "x$_;s/tx $/it./;s/x/++$.%4?hat:lov/ge

(credit to @Ton Hospel)

Demo: http://ideone.com/zrM27p

Previous solutions:

$_="I xe that "x$_;s/x/$@++&1?lov:hat/ge;s/\w+.$/it./

(credit to @Dada)

Run with perl -pE '$_="I xe that "x$_;s/x/$@++&1?lov:hat/ge;s/\w+.$/it./'

First solution (only this was mine)

for$x(1..<>){$_.=' I '.($x%2?hat:lov).'e that'}s/\w+$/it./;say

In parts:

for $x (1..<>) {
   $_ .= ' I '.($x % 2 ? hat : lov).'e that'
}
s/\w+$/it./;
say

Demo: http://ideone.com/mosnVz


Hi and welcome to PPCG. Nice answer. Here is a shorter solution though (54 bytes) : perl -pE '$_="I xe that "x$_;s/x/$@++&1?lov:hat/ge;s/\w+.$/it./'.
Dada

What does this part mean $@++&1? For @+ perldoc says "holds the offsets of the ends of the last successful submatches in the currently active dynamic scope" which doesn't make much sense for me. As I understand, you use this array in scalar context ($@+ - are you dereferencing it?) to get the number of elements and then add (+) the matched string (&1). No no no I knew I shouldn't have posted to PPCG it's too obfuscated :D
Al.G.

$@ is just a scalar (I could have used $x or any other scalar), ++ is the increment operator, and &1 is roughly the same as %2. So it's basically the same as $x++%2.
Dada

So you're using @ for scalar variable name; &1 for "and"ing the last bit to check if it's even (and not a backreference as I thought). Ok understood now, thanks.
Al.G.

Nice solution. You can gain a few more bytes by using $|-- as a toggle instead of $@++%2
Ton Hospel

1

Bash + coreutils, 106 bytes:

for i in `seq $1`;{ printf "I %s %s " `((i%2>0))&&echo hate||echo love` `((i==$1))&&echo it.||echo that`;}

Simply creates a sequence starting at 1 up to and including the input integer using the seq built-in, and then iterates through it one by one, first outputting hate if the value of the iteration variable, i, is not divisible by 2 and love otherwise. In the same iteration, it then chooses to output that if i is not equal to the input value, and it. otherwise.

Try It Online! (Ideone)


Better put the command substitutions directly in printf's string and use no format specifiers. Is pointless to compare whether i%2 is greater than 0. I you reverse the commands in the list, you can use less than comparison instead of i==$1: for i in `seq $1`;{ printf "I `((i%2))&&echo hat||echo lov`e `((i<$1))&&echo that||echo it.` ";}. By the way, we usually label such solutions as Bash + coreutils, because the use of seq.
manatwork

1

///, 60 57 bytes

/!/I hate //T/that //
/it.//00/!TI love T//Tit./it.//0/!/

-3 bytes thanks to m-chrzan

Input in unary with trailing new line.

Try it online!


You could add /T/that / to the beginning and replace all instances of that with T.
m-chrzan

0

R, 92 90 bytes

An R adaptation of @Leaky Nun's python answer. Working with strings in R is tedious as always.

n=scan();cat(rep(strsplit("I hate that I love that ","")[[1]],n)[6:(n*12)-5],"it.",sep="")

This could probably be golfed further though.

Edit: saved 2 bytes by changing:

[1:((n*12)-5)]to [6:(n*12)-5]


It works out better to loop instead; see my alternative R solution.
JDL

0

C, 96 Bytes

c;f(i){printf("I hate");for(;c<i+1/2-1;c++)printf(" that I %s",c&1?"hate":"love");puts(" it.");}

I didn't see the above solution from Releasing Helium Nuclei which is better.


0

MATL, 37 bytes

:"2@o3]Qv'hate I that it. love'Ybw)Zc

Try it online!

Explanation

The code is based on the following mapping from numbers to strings:

0: 'love'
1: 'hate'
2: 'I'
3: 'that'
4: 'it.'

The program pushes numbers to the string in groups of three: 2, 0, 3; then 2, 1, 3; then 2, 0, 3; ... as many times as the input n. After that, the final 3 is transformed into a 4, the mapping is applied to transform numbers to strings, and the strings are joined using space as separator.

:                         % Input n implicitly. Push range [1 2 ... n]
"                         % For each
  2                       %   Push 2
  @o                      %   Iteration index modulo 2: pushes 0 or 1
  3                       %   Push 3
]                         % End
Q                         % Add 1 to the last 3
v                         % Concatenate stack contents into a numeric column vector
'hate I that it. love'    % Push this string
Yb                        % Split at spaces. Gives a cell array of five strings
w                         % Swap to move numeric vector to top
)                         % Index cell array of strings with that vector. Indexing
                          % is 1-based and modular, so 0 refers to the last string.
                          % This gives a cell array of (repeated) strings
Zc                        % Join those strings by spaces. Display implicitly

0

JavaScript (ES6), 68 bytes

f=(n,i)=>n?(i?' that I ':'I ')+(i&1?'love':'hate')+f(n-1,-~i):' it.'

document.write('<pre>'+[ 1, 2, 3, 4, 10, 100 ].map(c=>c+': '+f(c)).join`\n`);


0

C#, 85 83 bytes

string f(int n,int m=2)=>"I "+(1>m%2?"hat":"lov")+(m>n?"e it.":"e that "+f(n,m+1));

Recursively constructs the string, using an optional parameter to keep track of which hate/love and how many to append.

-2 bytes from this tip for checking the evenness/oddness of a number.

No optional parameter solution, 87 86 84 bytes

string h(int n)=>"I "+(0<n?"hat":"lov")+(2>n&-2<n?"e it.":"e that "+h(0<n?~n+2:~n));

This one does the same thing except determines which hate/love to append based on whether the parameter is positive or negative. Each iteration the parameter approaches zero, alternating sign.


0

Thue, 100 bytes

@::=:::
>##::=>$.
$::=~I hate that I love 
>.#::=>&#
&::=~that 
>#<::=~I hate it.
>.<::=~it.
::=
>@<

Takes input as unary. (A string of n #s)


0

Pyke, 36 bytes

2/"I hate ""I love "]*"that "J"it."+

Try it here!

2/                                   -    input/2
                     *               -   ^ * v
  "I hate ""I love "]                -    ["I hate ", "I love "]
                      "that "J       -  "that ".join(^)
                              "it."+ - ^+"it."

Also 36 bytes

12*5+.daෆ   ű   l5d+12"I ":Q*<"it."+

Try it here! (Link uses X instead of I, this should work for the same amount of bytes offline where you can literally use those bytes. Online \r gets automatically replaced with \n)


0

><> (Fish), 82 Bytes

' I'oov.2coo<;oooo' it.'<
'ahv'v>'et'
oop26<^ooooo^o' that I '^!?:-1oo
'ol^'^>'ev'

Doubt it's very efficient, but it seems to more or less work. Input is via the starting stack, which makes the score 85 Bytes if you include the size of the -v argument required to do so.

Try it online!


0

Lua , 75 Bytes

n=io.read();print(('I hate that I love that '):rep(n):sub(1,n*12-5)..'it.')

1
Instead of static methods better use instance methods: ('I hate that I love that '):rep(n):sub(1,n*12-5). And would look nicer if you concatenate "it." to the end, because print() outputs its parameters separated by tab.
manatwork

1
The ';' between io.read() and print is unneeded, and arg[2] is a valid input method for lua scripts, which is the first command line argument.
ATaco

0

///, 68 bytes

/@/1\/#love //%/hate//#/that I //%1i/% i//I %1/I % //1@#% //@/I %1it.

Input in unary - add more 1s in the last section.

Try it online!


0

dc, 75 bytes

?[1-lbP[I lov]P]sa[e that ]sb[[e it.]Pq]sc[1-[I hat]Pd2%1=ad1>clbPlxx]dsxx

We're really just printing one chunk of string at a time here, and not leaving any garbage on the stack. This is great, we don't need to waste any bytes dealing with a register for our counter.

?                              #Input, works as our decrement counter
[1-lbP[I lov]P]sa              #Macro 'a' decrements our counter, prints string 'b', 
                               #then prints 'I lov'
[e that ]sb                    #String 'b'
[[e it.]Pq]sc                  #Macro 'c' prints the tail end of our message and quits
[1-                            #I'll unfold our main macro here; first step is to 
                               #decrement our counter
   [I hat]P                    #We always start by hating 'it,' no conditional needed
           d2%1=a              #Check the oddness of our counter; execute 'a' if odd
                 d1>c          #Check our counter; If we're done, execute 'c'
                     lbPlxx]   #Otherwise, print 'b' again and start this macro ('x') over
dsxx                           #Stores the above macro as 'x' and runs it.

0

Julia, 91 Bytes

Thought I add a julia solution:

f(N)=string("I hate ",join(["that I love that I hate " for _ in 1:N-1])[1:12*(N-1)],"it."))
당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.