Descrição do Desafio
Neste desafio, consideramos apenas love
e hate
como sentimentos. Se queremos expressar uma expressão de ordem de sentimentoN
, alternamos entre estes dois (começando com 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.
O padrão segue para todo número inteiro positivo N
. Dado N
, produza a expressão de ordem correspondente N
.
Notas
- Ponto final (
.
) no final da expressão é obrigatório, - Espaços em branco à direita e à esquerda (incluindo novas linhas) são permitidos,
- Saída para um número não positivo ou não inteiro
N
é indefinida, - Este é um desafio do código-golfe , portanto, faça o seu código o mais curto possível!
order
a entrada eexpression
a saída?Respostas:
Python, 54 bytes
fonte
f n=take(12*n-5)(cycle"I hate that I love that ")++"it."
(56 bytes)CJam , 36 bytes
Experimente online!
Explicação
fonte
C,
83767574 bytesThanks to @Leaky Nun for saving 11 bytes and adding 4 bytes!
Thanks to @YSC for saving a byte!
Try it on Ideone
fonte
i=0;while(n--)
->for(i=0;n--;)
saves 1 char.Javascript (ES6),
757370 bytesSaved 2 bytes thanks to Neil
Saved 3 bytes thanks to Whothehellisthat
Test
fonte
['I hate','I love'][i&1]
->i&1?'I love':'I hate'
Java 8, 91 bytes
Ungolfed Test Program
fonte
c=i->for(...)
Mathematica, 63 bytes
fonte
Jelly, 25 bytes
Try it online!
Explanation
fonte
05AB1E,
343227 bytesSaved 5 bytes thanks to Adnan.
Explanation
Try it online!
fonte
R, 79 bytes
Luckily in R, the default separator for
cat
is a space.(Edited from original 73 byte version which didn't quite solve the problem.)
fonte
for
loop and%%
. +1Retina,
4238 bytesThanks to Leaky Nun for helping me golf it!
Input is taken in unary.
Try it online!
Explanation
Replace every pair of
1
s with1I love n
.Replace the remaining
1
s withI hate n
.Replace the
n
at the end of the line withit.
and every other n withthat
.fonte
l
: retina.tryitonline.net/…Javascript (ES5),
9994 bytesSaved 5 bytes thanks to Leaky Nun.
OLD 99 byte solution:
Another 98 byte solution:
My code before minification:
fonte
function(c){for(d="",b=0;b<c;++b)d+=(b%2?"I love ":"I hate ")+(b==c-1?"it.":"that ");return d}
Haskell, 70 bytes
fonte
PowerShell v2+, 64 bytes
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-join
ed with' that '
to smush them together, then string concatenation' it.'
at the end.Test Cases
fonte
php,
6462 bytesUnfortunately 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
fonte
Perl,
625450 bytes(credit to @Ton Hospel)
Demo: http://ideone.com/zrM27p
Previous solutions:
(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)
In parts:
Demo: http://ideone.com/mosnVz
fonte
perl -pE '$_="I xe that "x$_;s/x/$@++&1?lov:hat/ge;s/\w+.$/it./'
.$@++&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$@
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
.@
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.$|--
as a toggle instead of$@++%2
Bash + coreutils, 106 bytes:
Simply creates a sequence starting at
1
up to and including the input integer using theseq
built-in, and then iterates through it one by one, first outputtinghate
if the value of the iteration variable,i
, is not divisible by2
andlove
otherwise. In the same iteration, it then chooses to outputthat
ifi
is not equal to the input value, andit.
otherwise.Try It Online! (Ideone)
fonte
printf
's string and use no format specifiers. Is pointless to compare whetheri%2
is greater than 0. I you reverse the commands in the list, you can use less than comparison instead ofi==$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 ofseq
.///,
6057 bytes-3 bytes thanks to m-chrzan
Input in unary with trailing new line.
Try it online!
fonte
/T/that /
to the beginning and replace all instances ofthat
withT
.R,
9290 bytesAn R adaptation of @Leaky Nun's python answer. Working with strings in R is tedious as always.
This could probably be golfed further though.
Edit: saved 2 bytes by changing:
[1:((n*12)-5)]
to[6:(n*12)-5]
fonte
C, 96 Bytes
I didn't see the above solution from Releasing Helium Nuclei which is better.
fonte
MATL, 37 bytes
Try it online!
Explanation
The code is based on the following mapping from numbers to strings:
The program pushes numbers to the string in groups of three:
2
,0
,3
; then2
,1
,3
; then2
,0
,3
; ... as many times as the inputn
. After that, the final3
is transformed into a4
, the mapping is applied to transform numbers to strings, and the strings are joined using space as separator.fonte
JavaScript (ES6), 68 bytes
fonte
C#,
8583 bytesRecursively 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,
878684 bytesThis 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.
fonte
Thue, 100 bytes
Takes input as unary. (A string of n
#
s)fonte
Pyke, 36 bytes
Try it here!
Also 36 bytes
Try it here! (Link uses
X
instead ofI
, this should work for the same amount of bytes offline where you can literally use those bytes. Online\r
gets automatically replaced with\n
)fonte
><> (Fish), 82 Bytes
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!
fonte
Lua , 75 Bytes
fonte
('I hate that I love that '):rep(n):sub(1,n*12-5)
. And would look nicer if you concatenate "it." to the end, becauseprint()
outputs its parameters separated by tab.///, 68 bytes
Input in unary - add more
1
s in the last section.Try it online!
fonte
dc, 75 bytes
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.
fonte
Julia, 91 Bytes
Thought I add a julia solution:
fonte