5 altered algorithm to meet O(n+m) condition
source | link

Racket 70

Golfed:

(define(h m)(for/last([r m]#:final(memv 1 r))(length(takef r even?))))

Assumes input is a two-dimensional array, which in Racket would be a list of lists:

(define m 
  '((0 0 0 0)
    (1 0 0 0)
    (1 1 0 1)
    (1 1 1 1)))

Ungolfed:

(define (h m)
  ;; step through rows, stopping at the first that contains a 1
  (for/last ([r m] #:final (memv 1 r)) 
    (length (takef r even?)))) ; pop off the leading zeroes to get the column index

Returns the column index with the longest stick.

Racket 70

Golfed:

(define(h m)(for/last([r m]#:final(memv 1 r))(length(takef r even?))))

Assumes input is a two-dimensional array, which in Racket would be a list of lists:

(define m 
  '((0 0 0 0)
    (1 0 0 0)
    (1 1 0 1)
    (1 1 1 1)))

Ungolfed:

(define (h m)
  (for/last ([r m] #:final (memv 1 r))
    (length (takef r even?))))

Returns the column index with the longest stick.

Racket 70

Golfed:

(define(h m)(for/last([r m]#:final(memv 1 r))(length(takef r even?))))

Assumes input is a two-dimensional array, which in Racket would be a list of lists:

(define m 
  '((0 0 0 0)
    (1 0 0 0)
    (1 1 0 1)
    (1 1 1 1)))

Ungolfed:

(define (h m)
  ;; step through rows, stopping at the first that contains a 1
  (for/last ([r m] #:final (memv 1 r)) 
    (length (takef r even?)))) ; pop off the leading zeroes to get the column index

Returns the column index with the longest stick.

4 altered algorithm to meet O(n+m) condition
source | link

Racket 6870

Golfed:

(define(fh m)(argmax(λ(s)for/last(length[r m]#:final(filtermemv odd?1 s)r))(apply maplength(takef listr meven?))))

Assumes input is a two-dimensional array, which in Racket would be a list of lists:

(define m 
  '((0 0 0 0)
    (1 0 0 0)
    (1 1 0 1)
    (1 1 1 1)))

Ungolfed:

(define (fh m)
  (argmax ; returns the element of the listfor/last with([r them] max#:final value(memv of1 λ(sr))
   (λ(s) (length (filtertakef odd?r seven?))) ; length of 1's in column
   (apply map list m))) ; lispy trick to flip rows & columns

The rules say "determineReturns the column index with the longest stick," not specifically the "column index with the longest stick". Though if you prefer that, it can be done in 104 chars:

(define(g m)(let([L length][M(apply map list m)])(-(L(member(argmax(λ(s)(L(filter odd? s)))M)M))(L M))))

Racket 68

Golfed:

(define(f m)(argmax(λ(s)(length(filter odd? s)))(apply map list m)))

Assumes input is a two-dimensional array, which in Racket would be a list of lists:

(define m 
  '((0 0 0 0)
    (1 0 0 0)
    (1 1 0 1)
    (1 1 1 1)))

Ungolfed:

(define (f m)
  (argmax ; returns the element of the list with the max value of λ(s)
   (λ(s) (length (filter odd? s))) ; length of 1's in column
   (apply map list m))) ; lispy trick to flip rows & columns

The rules say "determine the column with the longest stick," not specifically the "column index with the longest stick". Though if you prefer that, it can be done in 104 chars:

(define(g m)(let([L length][M(apply map list m)])(-(L(member(argmax(λ(s)(L(filter odd? s)))M)M))(L M))))

Racket 70

Golfed:

(define(h m)(for/last([r m]#:final(memv 1 r))(length(takef r even?))))

Assumes input is a two-dimensional array, which in Racket would be a list of lists:

(define m 
  '((0 0 0 0)
    (1 0 0 0)
    (1 1 0 1)
    (1 1 1 1)))

Ungolfed:

(define (h m)
  (for/last ([r m] #:final (memv 1 r))
    (length (takef r even?))))

Returns the column index with the longest stick.

3 added 294 characters in body
source | link

Racket 68

Golfed:

(define(f m)(argmax(λ(s)(length(filter odd? s)))(apply map list m)))

Assumes input is a two-dimensional array, which in Racket would be a list of lists:

(define m 
  '((0 0 0 0)
    (1 0 0 0)
    (1 1 0 1)
    (1 1 1 1)))

Ungolfed:

(define (f m)
  (argmax ; returns the element of the list with the max value of λ(s)
   (λ(s) (length (filter odd? s))) ; length of 1's in column
   (apply map list m))) ; lispy trick to flip rows & columns

The rules say "determine the column with the longest stick," not specifically the "column index with the longest stick". Though if you prefer that, it can be done in 104 chars:

(define(g m)(let([L length][M(apply map list m)])(-(L(member(argmax(λ(s)(L(filter odd? s)))M)M))(L M))))

Racket 68

Golfed:

(define(f m)(argmax(λ(s)(length(filter odd? s)))(apply map list m)))

Assumes input is a two-dimensional array, which in Racket would be a list of lists:

(define m 
  '((0 0 0 0)
    (1 0 0 0)
    (1 1 0 1)
    (1 1 1 1)))

Ungolfed:

(define (f m)
  (argmax ; returns the element of the list with the max value of λ(s)
   (λ(s) (length (filter odd? s))) ; length of 1's in column
   (apply map list m))) ; lispy trick to flip rows & columns

Racket 68

Golfed:

(define(f m)(argmax(λ(s)(length(filter odd? s)))(apply map list m)))

Assumes input is a two-dimensional array, which in Racket would be a list of lists:

(define m 
  '((0 0 0 0)
    (1 0 0 0)
    (1 1 0 1)
    (1 1 1 1)))

Ungolfed:

(define (f m)
  (argmax ; returns the element of the list with the max value of λ(s)
   (λ(s) (length (filter odd? s))) ; length of 1's in column
   (apply map list m))) ; lispy trick to flip rows & columns

The rules say "determine the column with the longest stick," not specifically the "column index with the longest stick". Though if you prefer that, it can be done in 104 chars:

(define(g m)(let([L length][M(apply map list m)])(-(L(member(argmax(λ(s)(L(filter odd? s)))M)M))(L M))))
2 deleted 16 characters in body
source | link
1
source | link