Skip to main content
1 byte off
Source Link
Laikoni
  • 26.3k
  • 6
  • 52
  • 115

[Haskell], 102 98 95 9594 bytes

import Data.List
f(x:r)=length r>=x&&x>=0&&(f.reverse.sort$(pred<$>takesort$take x r(pred<$>r)++drop x r)
f x=1<3

Try it online! UsageTry it online! Usage: f [3,3,2,2,1,1], returns True or False. Assumes that the input contains no zeros and is sorted in descending order, as allowed in the challenge.

Explanation:

import Data.List          -- import needed for sort
f (x:r) =                 -- x is the first list element, r the rest list
  length r >= x           -- the rest list r must be longer or equal x
  && x >= 0               -- and x must not be negative
  && (f .                  -- and the recursive call of f
      reverse . sort $    --    with the descendingly sorted list
      (pred<$>taketake x r(pred<$>r)    --    of the first x elements of r subtracted by 1
      ++ drop x r         --    and the rest of r
      )                    -- must be true
f [] = True               -- if the list is empty, return True

Edit: This seems to follow the Havel-Hakimi mentioned in other answers, though I did not know of this algorithm when writing the answer. [Haskell]: https://www.haskell.org/

[Haskell], 102 98 95 bytes

import Data.List
f(x:r)=length r>=x&&x>=0&&(f.reverse.sort$(pred<$>take x r)++drop x r)
f x=1<3

Try it online! Usage: f [3,3,2,2,1,1], returns True or False. Assumes that the input contains no zeros and is sorted in descending order, as allowed in the challenge.

Explanation:

import Data.List          -- import needed for sort
f (x:r) =                 -- x is the first list element, r the rest list
  length r >= x           -- the rest list r must be longer or equal x
  && x >= 0               -- and x must not be negative
  && f .                  -- and the recursive call of f
      reverse . sort $    --    with the descendingly sorted list
      (pred<$>take x r)   --    of the first x elements of r subtracted by 1
      ++ drop x r         --    and the rest of r
                          -- must be true
f [] = True               -- if the list is empty, return True

Edit: This seems to follow the Havel-Hakimi mentioned in other answers, though I did not know of this algorithm when writing the answer. [Haskell]: https://www.haskell.org/

[Haskell], 102 98 95 94 bytes

import Data.List
f(x:r)=length r>=x&&x>=0&&(f.reverse.sort$take x(pred<$>r)++drop x r)
f x=1<3

Try it online! Usage: f [3,3,2,2,1,1], returns True or False. Assumes that the input contains no zeros and is sorted in descending order, as allowed in the challenge.

Explanation:

import Data.List          -- import needed for sort
f (x:r) =                 -- x is the first list element, r the rest list
  length r >= x           -- the rest list r must be longer or equal x
  && x >= 0               -- and x must not be negative
  && (f .                 -- and the recursive call of f
      reverse . sort $    --    with the descendingly sorted list
      take x(pred<$>r)    --    of the first x elements of r subtracted by 1
      ++ drop x r         --    and the rest of r
     )                    -- must be true
f [] = True               -- if the list is empty, return True

Edit: This seems to follow the Havel-Hakimi mentioned in other answers, though I did not know of this algorithm when writing the answer. [Haskell]: https://www.haskell.org/

golfed 3 bytes
Source Link
Laikoni
  • 26.3k
  • 6
  • 52
  • 115

[Haskell], 102 98 9895 bytes

import Data.List
f(x:r)|length=length r<x||x<0=0>1|1<3=fr>=x&&x>=0&&(f.reverse.sort$(pred<$>take x r)++drop x r)
f x=1<3

Try it online!Try it online! Usage: f [3,3,2,2,1,1], returns True or False. Assumes that the input contains no zeros and is sorted in descending order, as allowed in the challenge.

Explanation:

import Data.List                 -- import needed for sort
f (x:r)         =                 -- x is the first list element, r the rest list
  |lengthlength r <>= x || x<0 = False       -- if the rest list isr shortermust thanbe xlonger or x<0, returnequal Falsex
  |otherwise&& x >= 0   =            -- and x must not be negative
  && f .                  -- elseand the recursive call fof recursivelyf
      reverse . sort $        --   -- with the descendingly sorted list ...
      (pred<$>take x r)       --   -- of the first x elements of r subtracted by 1 and ...
      ++ drop x r            --    --and the rest of r
f [] = True                       -- must be true
f [] = True               -- if the list is empty, return True

Edit: This seems to follow the Havel-Hakimi mentioned in other answers, though I did not know of this algorithm when writing the answer. [Haskell]: https://www.haskell.org/

[Haskell], 102 98 bytes

import Data.List
f(x:r)|length r<x||x<0=0>1|1<3=f.reverse.sort$(pred<$>take x r)++drop x r
f x=1<3

Try it online! Usage: f [3,3,2,2,1,1], returns True or False. Assumes that the input contains no zeros and is sorted in descending order, as allowed in the challenge.

Explanation:

import Data.List                 -- import needed for sort
f (x:r)                          -- x is the first list element, r the rest list
  |length r < x || x<0 = False   -- if the rest list is shorter than x or x<0, return False
  |otherwise    = f .            -- else call f recursively
      reverse . sort $           -- with the descendingly sorted list ...
      (pred<$>take x r)          -- of the first x elements of r subtracted by 1 and ...
      ++ drop x r                -- the rest of r
f [] = True                      -- if the list is empty, return True

Edit: This seems to follow the Havel-Hakimi mentioned in other answers, though I did not know of this algorithm when writing the answer. [Haskell]: https://www.haskell.org/

[Haskell], 102 98 95 bytes

import Data.List
f(x:r)=length r>=x&&x>=0&&(f.reverse.sort$(pred<$>take x r)++drop x r)
f x=1<3

Try it online! Usage: f [3,3,2,2,1,1], returns True or False. Assumes that the input contains no zeros and is sorted in descending order, as allowed in the challenge.

Explanation:

import Data.List          -- import needed for sort
f (x:r) =                 -- x is the first list element, r the rest list
  length r >= x           -- the rest list r must be longer or equal x
  && x >= 0               -- and x must not be negative
  && f .                  -- and the recursive call of f
      reverse . sort $    --    with the descendingly sorted list
      (pred<$>take x r)   --    of the first x elements of r subtracted by 1
      ++ drop x r         --    and the rest of r
                          -- must be true
f [] = True               -- if the list is empty, return True

Edit: This seems to follow the Havel-Hakimi mentioned in other answers, though I did not know of this algorithm when writing the answer. [Haskell]: https://www.haskell.org/

saved 4 bytes, now also handles 0 in input
Source Link
Laikoni
  • 26.3k
  • 6
  • 52
  • 115

[Haskell], 102102 98 bytes

import Data.List
f(x:r)|length r<x=0>1|1<3=fr<x||x<0=0>1|1<3=f.reverse.sort$[n|n<-sort$(pred<$>take x r,n>0]++drop)++drop x r
f x=1<3

[Try it online!]Try it online! Usage: f [3,3,2,2,1,1], returns True or False. Assumes that the input contains no zeros andcontains no zeros and is sorted in descending order, as allowed in the challenge.

Explanation:

import Data.List                    -- import needed for sort
f (x:r)                             -- x is the first list element, r the rest list
  |length r < x = False       || x<0 = False   -- if the rest list is shorter than x or x<0, return False
  |otherwise    = f .               -- else call f recursively
      reverse . sort $              -- with the descendingly sorted list ...
      [n|n<-(pred<$>take x r,) n>0]         -- of the first x elements of r subtracted by 1 if they are still greater 0 and ...
      ++ drop x r                   -- the rest of r
f [] = True                         -- if the list is empty, return True

Edit: This seems to follow the Havel-Hakimi mentioned in other answers, though I did not know of this algorithm when writing the answer. [Haskell]: https://www.haskell.org/ [Try it online!]: https://tio.run/nexus/haskell#bYwxD4IwFIR3fsUbGDRUAqKJmpbJ0X/QdGhCq41SmkdjOvDfkaLoYr7l7t67G03rOvRwll7mF9P7RK/CCdfDQ9mrvwHSwIq6HEpaMZ2jeirsVd5PlZTbwdKNQ9XQtPbyriAAElsXIssa7Fy0iYbApu7YSmOBQdNBAgAOjfWQQisdaOBcEOAViWxnysgSfoJoD@/LjxguUvxbnge@X1EcyY7shRhf "Haskell – TIO Nexus"

[Haskell], 102 bytes

import Data.List
f(x:r)|length r<x=0>1|1<3=f.reverse.sort$[n|n<-pred<$>take x r,n>0]++drop x r
f x=1<3

[Try it online!] Usage: f [3,3,2,2,1,1], returns True or False. Assumes that the input contains no zeros and is sorted in descending order, as allowed in the challenge.

Explanation:

import Data.List                    -- import needed for sort
f (x:r)                             -- x is the first list element, r the rest list
  |length r < x = False             -- if the rest list is shorter than x, return False
  |otherwise    = f .               -- else call f recursively
      reverse . sort $              -- with the descendingly sorted list ...
      [n|n<-pred<$>take x r, n>0]   -- of the first x elements of r subtracted by 1 if they are still greater 0 and ...
      ++ drop x r                   -- the rest of r
f [] = True                         -- if the list is empty, return True

Edit: This seems to follow the Havel-Hakimi mentioned in other answers, though I did not know of this algorithm when writing the answer. [Haskell]: https://www.haskell.org/ [Try it online!]: https://tio.run/nexus/haskell#bYwxD4IwFIR3fsUbGDRUAqKJmpbJ0X/QdGhCq41SmkdjOvDfkaLoYr7l7t67G03rOvRwll7mF9P7RK/CCdfDQ9mrvwHSwIq6HEpaMZ2jeirsVd5PlZTbwdKNQ9XQtPbyriAAElsXIssa7Fy0iYbApu7YSmOBQdNBAgAOjfWQQisdaOBcEOAViWxnysgSfoJoD@/LjxguUvxbnge@X1EcyY7shRhf "Haskell – TIO Nexus"

[Haskell], 102 98 bytes

import Data.List
f(x:r)|length r<x||x<0=0>1|1<3=f.reverse.sort$(pred<$>take x r)++drop x r
f x=1<3

Try it online! Usage: f [3,3,2,2,1,1], returns True or False. Assumes that the input contains no zeros and is sorted in descending order, as allowed in the challenge.

Explanation:

import Data.List                 -- import needed for sort
f (x:r)                          -- x is the first list element, r the rest list
  |length r < x || x<0 = False   -- if the rest list is shorter than x or x<0, return False
  |otherwise    = f .            -- else call f recursively
      reverse . sort $           -- with the descendingly sorted list ...
      (pred<$>take x r)          -- of the first x elements of r subtracted by 1 and ...
      ++ drop x r                -- the rest of r
f [] = True                      -- if the list is empty, return True

Edit: This seems to follow the Havel-Hakimi mentioned in other answers, though I did not know of this algorithm when writing the answer. [Haskell]: https://www.haskell.org/

added 57 characters in body
Source Link
Laikoni
  • 26.3k
  • 6
  • 52
  • 115
Loading
Source Link
Laikoni
  • 26.3k
  • 6
  • 52
  • 115
Loading