¿Has sido bueno este año?


31

Introducción

¡Santa tiene demasiados nombres para procesar y necesita tu ayuda! Él necesita que usted pueda escribir un programa o una función, que da salida nice, naughty, very naughtyo very very naughty. Para determinar cuán agradable o travieso es alguien, Santa había desarrollado un algoritmo:

Niza ( , ):

En primer lugar, obtenemos un número del nombre sumando todas las letras ( espacios ignorados ). Por ejemplo:

Doorknob =

D = 4
o = 15
o = 15
r = 18
k = 11
n = 14
o = 15
b = 2

4 + 15 + 15 + 18 + 11 + 14 + 15 + 2 = 94

Si el número de divisores es igual a la longitud del nombre, se considera a la persona nice. Esto significa que su programa debería salir [name] has been nice. Aquí, los divisores de 94son:

Divisors of 94: 1, 2, 47, 94

Hay 4divisores, pero el nombre tiene longitud 8( espacios incluidos ). Conclusión, Doorknobno ha sido agradable. Entonces continuamos nuestro viaje:


Travieso ( , ):

Santa ha desarrollado una nueva secuencia, el número de navidad . Primero, veremos los siguientes árboles de navidad:

n = 1        n = 2          n = 3          n = 4

                                             *
                                            ***
                                           *****
                                          *******
                                         *********
                          _   *             ***
                         |   ***           *****
                         |  *****         *******
               *         | *******       *********
  *           ***        |   ***            ***
 ***         *****   n*n |  *****          *****
  *           ***        | *******        *******
             *****       |   ***         *********
               *         |  *****           ***
                         |_*******         *****
                              *           *******
                           |_____|       *********
                             2n+1            *

  5            18             47             98

La cantidad de asteriscos determina el número de navidad. La secuencia va de la siguiente manera: 5, 18, 47, 98, 177, ....

A partir de aquí, podemos concluir que 94no es un número de Navidad. Eso significa que Doorknobno solo ha sido malo.


Muy travieso ( ):

Para esto, necesitamos averiguar si se Doorknobtrata de una cadena de escalera ascendente . Esto se determina por las letras en el nombre con A = 1, B = 2, C = 3, etc .:

Primero veremos la primera letra, la D. Esto tiene valor 4. Este es nuestro punto de partida. La siguiente letra es o. Esto tiene el valor 15, que es más alto que nuestro valor anterior, por lo que vamos un paso más arriba en la escalera. El siguiente valor también es un o. Esto es lo mismo, así que no estamos haciendo nada. Si el siguiente valor es más alto que el valor actual, iremos un paso más arriba. Si el siguiente valor es más bajo que el valor actual, iremos más bajo. Si es lo mismo, nos mantendremos en el mismo paso. Esto visualiza para Doorknob, Martin Buttnery Alex A:

            O
           / \
      R   N   B
     / \ /
  O-O   K                 T   N   U                 L   X
 /                       / \ / \ / \               / \ / \
D                   M   R   I   B   T-T           A   E   A
                     \ /               \
                      A                 N   R
                                         \ /
                                          E

Puede ver que Doorknobterminó más alto que la posición inicial. Por lo tanto Doorknob has been very naughty. Martin Buttnery Alex Ano llegó más alto que el punto de partida. Entonces son los dos very very naughty.

Casos de prueba

Input: Doorknob
Output: Doorknob has been very naughty

Input: Martin Buttner
Output: Martin Buttner has been very very naughty

Input: Jakube
Output: Jakube has been nice

Input: B
Output: B has been very very naughty

Reglas

  • Debe proporcionar un programa o una función que tome entrada (que consiste en al menos una letra).
  • La entrada consistirá en letras mayúsculas , minúsculas y espacios . Los espacios se ignoran durante el proceso, excepto por la longitud de la entrada.
  • Este es el , por lo que gana el envío con la menor cantidad de bytes.

15
Por cierto, la forma correcta de escribir Büttner sin diéresis es Buettner, no Buttner.
Dennis

3
Los números de Navidad son generados por n^3 + 2n^2 + 2, por cierto.
Lynn

2
De acuerdo Entonces, la "puntuación" máxima para un nombre es ZZZ...Z = 26 * 99 = 2574, lo que significa que solo necesita verificar los números de Navidad hasta e inclusive n=13. (Información útil para otros golfistas.)
Lynn

23
Santa has been very very naughty. ¿Esperar lo?
Pomo de la puerta

55
@Doorknob冰- ¿No ha recibido su nombre completo: Santa Claus has been very naughty. También debería probar Saint Nick, Saint Nicholas, St. Nick, St. Nicholas, Kris Kringle, Papá Noel, Pere Noel y todos sus otros alias, ¿tal vez uno de ellos funcionará? Sin embargo, por qué un "buen" tipo debería necesitar tantos alias ya es bastante sospechoso ...
Darrel Hoffman

Respuestas:


5

Pyth 86 bytes

Aparentemente he sido bueno este año ...

jd[z"has been"?qlzl{yPJsKxL+NG@Grz0"nice"+*"very "&!}Jm+*+2d*dd2SJhgs._M-VKtK0"naughty

Pruébelo en línea: Demostración o conjunto de pruebas

Explicación:

jd[z"has been"...      list with input string, "has been" and ...; join with spaces
qlzl{yPJsKxL+NG@Grz0   compares the length of input with the number of divisors
                       (computes all prime factors and counts the elements in the powerset)
?..."nice"             if True, use "nice" as last list element
                       else:
}Jm+*+2d*dd2SJ            check for christmas number
                          (checks, if its of the form n^3+2*n^2+2)
gs._M-VKtK0               check if raising ladder string ends lower or equal
                          (assigns each neighbor pair a number -1,0,1 and computes the sum)
&!...h...                 returns 0, 1 or 2
*"very "                  repeat "very " this times
+..."naughty              add "naughty" and use this as the third list element

10

CJAM, 109 108 107 bytes

l" has been "1$[_,\S-:eu'@f-:A:+__,:)f%0e=@=\_{)__2+**))}%&A2ew::-:g1b0<]{}#4,="very "*_5>\"naughty""nice"?

Pruébalo en línea.

Explicación

Una explicación completa tendrá que esperar hasta más tarde, pero aquí está el código desglosado en las diferentes secciones:

l" has been "1$[                    e# Some preparation...
  _,\S-:eu'@f-:A:+                  e# Determine letter sum.
  __,:)f%0e=@=                      e# Check divisor count.
  \_{)__2+**))}%&                   e# Check for Christmas number.
  A2ew::-:g1b0<                     e# Check ladder.
]{}#4,="very "*_5>\"naughty""nice"? e# Determine nice/naughtiness.

@RikerW Robé eso del comentario de Doorknob.
Martin Ender

Yo + 1 y su comentario también.
Rɪᴋᴇʀ

"Santa ha sido muy, muy travieso"
ASCIIThenANSI

4

MATL , 117 bytes

Mi programa MATL más largo hasta ahora :-) :-(

Utiliza la versión actual del lenguaje, que es anterior a este desafío.

jttk96-t0>)tstt:\~s4$bn=?xx'nice'}[1,2,0,2]99:ZQ=a?'naughty'}dt0>sw0<s-O>~'very naughty'w?'very 'wh]]]' has been 'whh

Ejemplos

>> matl
 > jttk96-t0>)tstt:\~s4$bn=?xx'nice'}[1,2,0,2]99:ZQ=a?'naughty'}dt0>sw0<s-O>~'very naughty'w?'very 'wh]]]' has been 'whh
 > 
> Doorknob
Doorknob has been very naughty

>> matl
 > jttk96-t0>)tstt:\~s4$bn=?xx'nice'}[1,2,0,2]99:ZQ=a?'naughty'}dt0>sw0<s-O>~'very naughty'w?'very 'wh]]]' has been 'whh
 > 
> Jakube
Jakube has been nice

Explicación

jt                        % input string. Duplicate
tk96-t0>)                 % duplicate. Convert to lower, then to numbers, remove spaces
ts                        % duplicate. Sum
tt:                       % duplicate. Vector from 1 to obtained sum
\~s                       % modulus operation. Count zeros to determine number of divisors
4$bn=                     % does it equal original name length?
?                         % if so
    xx'nice'              % remove values not needed, and push string
}                         % else
    [1,2,0,2]99:ZQ        % all Christmas numbers
    =a                    % does sum equal any Christmas number?
    ?                     % if so
        'naughty'         % push string
    }                     % else
        dt0>s             % total number of increases
        w0<s              % total number of decreases
        -O>~              % subtract both. Is total <=0?
        'very naughty'w   % push string, which will be needed in either case. Swap
        ?                 % if total was <=0
            'very 'wh     % prepend string
        ]                 % end if
    ]                     % end if
]                         % end if
' has been 'whh           % build complete string from pushed parts

2

Lua, 371 284 bytes

Estoy seguro de que hay margen de mejora, me gustaría eliminar algunos si.

Editar: 4 meses después, aprendí mucho sobre lua y quería volver a esta presentación, lo hice bien: ¡recorté 87 bytes!

a=... .." has been "s=(...):lower()b="very "x=0y=s:byte(1)-96z=0r="naughty"for i=2,#s
do c=s:byte(i)y=c+y-96z=z+c<s:byte(i-1)and-1or 1 end
for i=1,y do x=y%i<1 and x+1or x end
for i=1,13 do d=y==i^3+2*i^2+2 and 0or d end
print(a..(x==#s and"nice"or(d and""or b..(z>0 and""or b))..r))

Sin golf

a=... .." has been "           -- Start of the sentence
s=(...):lower()                -- convert the input to lower case
b="very "                      
x=0                            -- y's divisor count
y=s:byte(1)-96                 -- will contain the sum of the char's position in the alphabet
z=0                            -- will contain the raising ladder state
r="naughty"                    

for i=2,#s                     -- iterate over each character in s
do
  c=s:byte(i)                  -- shorthand for the byte value of the current char
  y=c+y-96                     -- increment the sum of letter's positions
  z=z+c<s:byte(i-1)            -- if the previous char is greater than the current
        and-1                  -- the ladder goes down
      or 1                     -- else, it goes up
end

for i=1,y                      -- iterate on the range 1..y
do
  x=y%i<1                      -- if i is a divisor of y
      and x+1                  -- increment x
    or x
end

for i=1,13                     -- iterate on the range 1..13
do                             -- no need to go further for the christmas numbers
  d=y==i^3+2*i^2+2             -- if y is a christmas number
      and 0                    -- set d
    or d                       -- else let d as it is
end
print(a..                      -- output "Name has been"
      (x==#s                   -- if y's divisor==length of input
        and"nice"              -- append "nice"
      or(d                     -- else, if d is not set
          and""                
        or b..                 -- append "very"
          (z>0                 -- and if the raising ladder doesn't raise
             and""
          or b))..             -- append a second "very"
        r))                    -- append "naughty"

Antigua solución de 371 bytes

function f(s)a,b,c,d,e,r,g,s=s.." has been ","very ",0,0,0,"naughty",math.pow,s:lower()for i=1,#s 
do if 32<s:byte(i)then e,d=i>1 and(s:byte(i)<s:byte(i-1)and e-1 or e+1)or e,d-96+s:byte(i)end end
for i=1,d do c=d%i>0 and c or c+1 end if c==#s then return a.."nice"end 
for i=1,13 do if g(i,3)+2*g(i,2)+2==d then return a..r end end
return e>0 and a..b..r or a..b..b..r end

Versión sin golf :)

function f(s)
  a,b,c,d,e,r,g,s=s.." has been ","very ",0,0,0,"naughty",math.pow,s:lower()
  for i=1,#s
  do
    if 32<s:byte(i)
    then
      --sum of the char's order in the alphabet
      d=d-96+s:byte(i)
      --raising ladder
      e=i>1 and(s:byte(i)<s:byte(i-1)and e-1 or e+1)or e
    end
  end
  for i=1,d
  do
    -- number of d's divisors
    c=d%i>0 and c or c+1
  end
  if c==#s then return a.."nice" end
  for i=1,13
  do
    --Christmas number are equals n^3+2n^2+2 as @Mauris said 
    if g(i,3)+2*g(i,2)+2==d then return a..r end
  end
  --is he very naughty or very very naughty?
  return e>0 and a..b..r or a..b..b..r 
end

1

En serio, 138 bytes

" has been ",;' @-û╗+╝╜`O8ª@-`MΣ;2┐w`iXu`Mπ╜l=`"nice"╛+.éó`╬é03┐2└3╤1x`;;⌐**⌐`MíuY3└+3┐╜Ok0)p)`p;(@)-s@)+(`╬l>Y(Xu3└*"naughty"@"very "*+╛+

Hex Dump:

2220686173206265656e20222c3b2720402d96bb2bbcbd604f38a6402d604de43b32bf7760695875604de3bd6c3d60226e69636522be2b2e82a260ce823033bf32c033d13178603b33405e29a6e7326be4604da1755933c02b33bfbd4f6b3029702960703b2840292d7340292b2860ce6c3e5928587533c02a226e6175676874792240227665727920222a2bbe2b

Pruébalo en línea

Es difícil jugar golf de manera efectiva debido a la dificultad del control de flujo complicado en serio. La capacidad de anidar funciones sin el uso de registros ayudaría. (Me imagino que esto ya podría acortarse un poco mediante el uso juicioso de las funciones almacenadas, pero daría como resultado un código de espagueti que no tengo el corazón para intentarlo).

Explicación:

" has been "                                  push this string
,                                             read input
;' @-û╗                                       copy, remove space, uppercase, put in reg0
+╝                                            put '<input> has been ' in reg1
╜                                             bring back the processed input
`O8ª@-`MΣ                                     convert letters to numbers and sum
;2┐                                           store a copy of the sum in reg2
w`iXu`Mπ                                      compute the number of divisors
╜l                                            get processed input length
=                                             check if they're equal
`"nice"╛+.éó`╬                                if so, run this function that retrieves the 
                                              list we made earlier, appends "nice",
                                              prints it, empties the stack
                                              and immediately exits
é                                             empty the stack (it contains a 1)
03┐                                           put a 0 in reg3
2└                                            call back the letter sum from reg2
3╤1x                                          push [1,...1000]
`;;⌐**⌐`M                                     plug each number into x^3+2x^2+2
í                                             check if the letter sum is there
uY                                            make a 1 if it is not, 0 if it is
3└+3┐                                         add this number to reg3
╜Ok                                           convert the processed input into char codes
0)                                            put a zero behind it
p)                                            pop the first char code to bottom of stack
`p;(@)-s@)+(`╬                                Until the list of char codes is empty,
                                              subtract each one from the previous one,
                                              accumulating the signums
l                                             turn the leftover empty list into a 0
>Y                                            put a 1 if the accumulated signs are
                                              >=0 (not rising), else 0 (rising)
(X                                            clean up the last char code
u                                             increment to make 0 into 1 and 1 into 2
3└*                                           bring back the value from reg3
                                              which is 0 if *only* naughty, else 1
                                              and multiply it with preceding test result;
                                              this turns a very into a very very if this
                                              test failed, but leaves plain and single
                                              very naughty alone
"naughty"@                                    put "naughty" below the 'very' count

"very "*                                      put "", "very ", or "very very "
+                                             append the "naughty"
╛+                                            bring back the string in reg1 and append
                                              the constructed suffix

1

Python 2, 249 bytes

i=input()
g=[ord(c)-96for c in i.lower()if' '!=c]
s=sum(g)
S=0
a=g.pop()
while g:b=a;a=g.pop();S+=(a<b)-(b<a)
r=range(1,14+s)
print i+' has been '+[['very '*(1+(S<1)),''][s in[x**3+2*x**2+2for x in r]]+'naughty','nice'][sum(s%x<1for x in r)==len(i)]
Al usar nuestro sitio, usted reconoce que ha leído y comprende nuestra Política de Cookies y Política de Privacidad.
Licensed under cc by-sa 3.0 with attribution required.