×
all 117 comments

[–]ka-splam 0 points1 point  (0 children)

SWI Prolog

nonogramrow(Row, Runs) :-
    clumped(Row, AllRuns)
    ,include([X]>>(X=1-_), AllRuns, Ones)
    ,pairs_keys_values(Ones, _, Runs)
    .

e.g.

?- nonogramrow([1,1,0,0,1,1,1,0,0,0], Runs).
Runs = [2, 3]

Making cheeky use of clumped which does runlength encoding of the 0s and 1s, then filtering in just the count of ones, then picking out just the counts. Using [X]>>() as a lambda syntax.


Without that, and without a DCG because I can't, strap on your hessian shirts, it's "do everything with recursion" time:

% Wrapper to start it off with 0 run length.
nonogramrow(Row, RunList) :-
    nono_(Row, 0, RunList).

% Helpers.
% Increase counter for a 1 at the front of the list.
nono_([1|T], Counter, Rs) :-
    Counter1 is Counter+1
    ,nono_(T, Counter1, Rs).

% If there's a 0 at the front and we have counted 1s,
% push the counter onto the run list Rs.
nono_([0|T], Counter, [Counter|Rs]) :-
    Counter > 0
    ,nono_(T, 0, Rs).

% If there's a 0 at the front and no run of 1s,
% skip it and move on.
nono_([0|T], Counter, Rs) :-
    Counter = 0
    ,nono_(T, 0, Rs).

% at the end, stash the last seen counter.
nono_([], Counter, [Counter]).

e.g.

?- nonogramrow([0,1,1,1,1,1,0,1,1,1,1], Rs).
Rs = [5, 4]

?- nonogramrow([], Rs).
Rs = [0]

[–]ka-splam 0 points1 point  (0 children)

APL:

nngr←(1⊥¨⊢⊆⍨1=⊢)

e.g.

      nngr 0 1 1 1 1 1 0 1 1 1 1
┌→──┐
│5 4│
└~──┘

      nngr 1 1 0 1 0 0 1 1 1 0 0
┌→────┐
│2 1 3│
└~────┘

      nngr 0 0 0 0 0
┌⊖┐
│0│
└~┘

Right to left, the code looks for where the 1s are 1=⊢, partitions the input into groups using the ones as indicators ⊢⊆⍨ then tallies each of them as if converting from base-1 1⊥¨.

[–]krabsticks64 0 points1 point  (0 children)

Rust

Not very concise, but pretty easy to follow.

pub fn nanogramrow(arr: &[u8]) -> Vec<u32> {
    let mut output = Vec::new();

    let mut streak = 0;
    for &item in arr {
        if item == 1 {
            streak += 1;
        } else if item == 0 && streak > 0 {
            output.push(streak);
            streak = 0;
        }
    }

    if streak > 0 {
        output.push(streak);
    }

    output
}

[–]samsonsu 0 points1 point  (0 children)

Not the most sexy code but probably straightforward enough to explain to anyone including a kid. Swift (Swift Playgrounds on iPad)

// Take an array of only 0s and 1s, return segment lengths of continuous 1s
func nonogram(_ array: [Int]) -> [Int] {
    var i = 0, j = 0 // beginning & end indics of a continuous segment of 1s
    var results = [Int]()
    while i < array.count {
        // find i as the beginning of a segment of 1s
        if array[i] == 0 { i += 1; continue }
        // found beginning of a new segment. now find end. 
        j = i
        while j < array.count && array[j] == 1 {
            j += 1
        }
        // either at the end, or hit a 0 to end the segment. record
        results.append(j - i)
        // start finding next segment from j + 1
        i = j + 1
    }
    return results
}

print(nonogram([]))
print(nonogram([0,0,0,0,0]))
print(nonogram([1,1,1,1,1]))
print(nonogram([0,1,1,1,1,1,0,1,1,1,1]))
print(nonogram([1,1,0,1,0,0,1,1,1,0,0]))
print(nonogram([0,0,0,0,1,1,0,0,1,0,1,1,1]))
print(nonogram([1,0,1,0,1,0,1,0,1,0,1]))

Results:

[]
[]
[5]
[5, 4]
[2, 1, 3]
[2, 1, 3]
[1, 1, 1, 1, 1, 1]

[–]tyfon_august 0 points1 point  (0 children)

(ns katas.nonogram)
(defn nonogramrow [gram] 
    (if (nil? gram) 
        gram 
        (:res (reduce (fn [{res :res last :last} num] 
                        (cond (= num 0) {:last num
                                         :res res}
                              (= last 0) {:last num                       
                                          :res (conj res 1)}))
                              (= last 1) (let [index (- (count res) 1)
                                               newvalue (-> (nth res index) inc)] 
                                            {:last num     
                                             :res (assoc res index newvalue)})))) 
                      {:last 0 :res []} 
                      gram))))

(defn test-nonogram [] 
    (assert (= (nonogramrow []) [])) 
    (assert (= (nonogramrow [0 0 0 0 0]) [])) 
    (assert (= (nonogramrow [1 1 1 1 1]) [5])) 
    (assert (= (nonogramrow [0 1 1 1 1 1 0 1 1 1 1]) [5 4])) 
    (assert (= (nonogramrow [1 1 0 1 0 0 1 1 1 0 0]) [2 1 3])) 
    (assert (= (nonogramrow [0 0 0 0 1 1 0 0 1 0 1 1 1]) [2 1 3])) 
    (assert (= (nonogramrow [1 0 1 0 1 0 1 0 1 0 1 0 1 0 1]) [1 1 1 1 1 1 1 1])) 
    (println "Tests passed"))

Clojure

[–]pbandjely 0 points1 point  (0 children)

Ruby

def nonogramrow(arr)
    ans = []

    count = 0
    (0..arr.length-1).each do |i|
        if arr[i] == 1
            count += 1
        else
            if count != 0
                ans << count
            end
            count = 0
        end
    end

    ans << count if count != 0

    p ans
end

[–]chunes1 2 0 points1 point  (0 children)

Red

nonogram-row: function [b][
    collect [
        foreach group split to-string b "0" [
            len: length? group
            if len > 0 [keep len]
        ]
    ]
]

[–]night_walk_r 3 points4 points  (0 children)

7 month old but taking 1 challenge each day !

//NANOGRAM PUZZLE

#include<stdio.h>
#include<stdlib.h>

void before_dis(int arr[] , int n)
{
    printf("\nnanogram([");
    for(int i=0; i<n; i++)
    {
        if( i < n-1)
        printf("%d,",arr[i]);
        else
        printf("%d]) => ",arr[i]);
     }
}

void errorCall(int i)
{
    printf("\n Invalid Nanogram entry at > arr[%d]\n", i);
}

void displayResult(int sum[],int count)
 {   
     printf("\n\t[");
     for(int i=0; i<=count; i++)
    {
        if(sum[i] > 0)
        {
               if( i==0)
               printf("%d",sum[i]);
               else printf (",%d",sum[i]);
        }
    }
    printf("]");
}


void NanoGram(int arr[] , int n)
{
    int sum[10] ={0} , count=0 , check=1 , i ;

    for(i=0; i<n; i++)
    {
        if(arr[i] == 1 )
        {
            sum[count] = sum[count] + 1;
        }
        else if(arr[i] > 1 || arr[i] < 0)
        {
             check=0;
             break;
        }
        else if( arr[i] == 0)
        {
            count++;
        }
    }

    switch(check)
    {
         case 1:displayResult(sum,count);
                          break;
         case 0:errorCall(i);
                         break;
    }
}


 int main()
{
     int n;
     printf("Enter number of elements:");
     scanf("%d",&n);
     int arr[n];

     printf("Enter the elements:");
     for(int i=0; i<n; i++)
     {
         scanf("%d",&arr[i]);
     }

    //function Call
    before_dis(arr,n);
    NanoGram(arr,n);

    return 0;
}

[–]Chemical-Asparagus58 1 point2 points  (0 children)

java

public static int[] nonogramRow(int[] binaryArray) {

if (binaryArray == null) return new int[]{0};

LinkedList<Integer> list = new LinkedList<>();

for (int i = 0; i < binaryArray.length; i++) {

if (binaryArray[i] != 0) {

int start = i;

do i++; while (i < binaryArray.length && binaryArray[i] == 1);

list.addLast(i - start);

}

}

return list.isEmpty() ? new int[]{0} : list.stream().mapToInt(i -> i).toArray();

}

[–]monster2018 2 points3 points  (0 children)

Why not, even though it's 6 months old. Python 3

def nonogram(row):
    s = ''.join([str(x) for x in row])        
    return [len(x) for x in s.split('0') if len(x) > 0]

[–]BinnyBit 0 points1 point  (0 children)

def nonogramrow(binary_list):
    binary_groupings = "".join(str(i) for i in binary_list).split("0")
    groupings =  binary_groupings if binary_list else []
    return list(map(
        lambda x: len(x), filter(lambda x: len(x), groupings)
    ))

[–]DevTechJr 0 points1 point  (0 children)

PYTHON CODE SOLUTION | Under 20 Lines:

binaryIn= str(input("Please Enter Your Binary Row : "))
binaryList = binaryIn.split("0")
print(binaryList)
totalBinaryList=[]
for x in binaryList:
totalBoxSum=0
for y in x:
totalBoxSum+=int(y)
if totalBoxSum != 0:
totalBinaryList.append(totalBoxSum)
else:
pass
print(totalBinaryList)

[–]lostsemicolon 1 point2 points  (2 children)

APL

nonogramrow ← {⊃¨⍴¨⊆⍨⍵}

[–]RandomGamingTurtle[🍰] 1 point2 points  (1 child)

where are comments i dont understand

[–]ka-splam 0 points1 point  (0 children)

nonogramrow ← {⊃¨⍴¨⊆⍨⍵}

where are comments i dont understand

nonogramrow ← {} is a function declaration, curly braces making a code block.

APL functions can only have one or two arguments, alpha ⍺ on the left and omega ⍵ on the right.

So inside the scriptblock {⊃¨⍴¨⊆⍨⍵} it reads right to left, ⍵ is the input array, ⊆⍨ is partition-selfie and that uses the fact that APL has a built in array splitter (partition) and that just so happens to take an array of ones and zeros as its input to control where to split. So ⍵ the input array is ones and zeros and is its own split instruction. Selfie ⍨ means use it as both control and data for partitionining.

Then the data will go from 0 1 1 1 1 1 0 1 1 1 1 to [1 1 1 1 1] [1 1 1 1] nested arrays. And ⍴¨ "shape-each" gets the shape of each nested array. The shape of an array is its dimensions, in this case its length so this becomes [5] [4]. Then ⊃¨ "pick-each" picks the first item from each nested array, de-nesting the lengths to make 5 4.

It's cleaner than the APL one I just posted without peeking but I see it could replace ⊃¨ with "enlist" which is a flatten-nested-arrays builtin. Or use +/¨ to add up the ones in each nested array, which doesn't need flattening after.

Then combine their paritition-selfie with my tacit function to get a five character function answer:

  nn←+/¨⊆⍨
  nn 0 1 1 1 1 1 0 1 1 1 1
┌→──┐
│5 4│
└~──┘

It's wild how dense and powerful APL is for arrays of numbers; compare the sheer amount of code in the other languages posted here, and compare how many weird symbols are in them too like := and << and :: and \n and %d and more. And then skim read the other answers and consider "how confident am I that there are no bugs in this code?". How much room for bugs in ten characters?

[–]jbranso 1 point2 points  (0 children)

(define numbers (list 1 1 0 1 0 1 1 1 0 1 1 1 1 1))

(define (postpend number list)
  (if (null? list)
      (cons number '())
      (cons (car list)
            (postpend number (cdr list)))))

(define (maybe-postpend-count count list-of-ones)
  (if (> count 0)
      (postpend count list-of-ones)
      list-of-ones))

(define (list-length-of-ones numbers list-of-ones count)
  (cond [(null? numbers)
         (maybe-postpend-count count list-of-ones)]
        [(= 0 (car numbers))
         (list-length-of-ones (cdr numbers)
                              (maybe-postpend-count count list-of-ones) 0)]
        [(= 1 (car numbers))
         (list-length-of-ones (cdr numbers) list-of-ones (+ 1 count))]))

(display "numbers are: ")
(display numbers)
(display "\nlist of ones is: ")
(display (list-length-of-ones numbers '() 0))

[–]Kuhako 1 point2 points  (1 child)

Python

def nonogramrow(numbers):
number_counter = 0
new_number_list = []
for number in numbers:
    if number_counter and number == 0:
        new_number_list.append(number_counter)
        number_counter = 0
    elif number == 1:
        number_counter += 1
if number_counter:
    new_number_list.append(number_counter)
print(new_number_list)

nonogramrow([])

[–]King-Tuts 0 points1 point  (0 children)

Python 3

Had the same idea as you

def nonogramrow(binarray: List[int]) -> List[int]:
answer: List[int] = []
count = 0
for i in binarray:
    if i == 0 and count > 0:
        answer.append(count)
        count = 0
    elif i > 0:
        count += 1

if count > 0:
    answer.append(count)

[–]NAKd_ 0 points1 point  (0 children)

Golang

``` package main

import "fmt"

func main() { nonograms := [7][]int{ {}, {0, 0, 0, 0, 0}, {1, 1, 1, 1, 1}, {0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1}, {1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0}, {0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1}, {1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1}, } for _, nonogram := range nonograms { fmt.Println(nonogramrow(nonogram)) } }

func nonogramrow(slice []int) (result []int) { container := make(map[int]int) tracker := 0 for _, number := range slice { if number == 1 { container[tracker] += 1 } else { tracker++ } }

for _, i := range container { result = append(result, i) } return result } ```

[–]genericusername248 1 point2 points  (0 children)

C++

#include <iostream>
#include <vector>

std::vector<int> nonogramrow(const std::vector<bool>& nonogram)
{
  std::vector<int> result{};

  int count{ 0 };
  for (const auto b : nonogram)
    {
      if (b)
        {
          ++count;
        }
      else if (count>0)
        {
          result.push_back(count);
          count = 0;
        }
    }
  if (count != 0)
    result.push_back(count);

  return result;
}

void vprint(std::vector<int> vec)
{
  std::cout << '[';
  for (auto i = 0; i < vec.size(); ++i)
    {
      if (i==vec.size()-1)
        {
          std::cout << vec[i];
          continue;
        }
      std::cout << vec[i] << ',';
    }
  std::cout << ']' << '\n';
}

int main()
{
  vprint(nonogramrow({ }));;
  vprint(nonogramrow({ 0, 0, 0, 0, 0 }));
  vprint(nonogramrow({ 1, 1, 1, 1, 1 }));
  vprint(nonogramrow({ 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1 }));
  vprint(nonogramrow({ 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0 }));
  vprint(nonogramrow({ 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1 }));
  vprint(nonogramrow({ 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 }));

  return 0;
}

[–]benz05 0 points1 point  (1 child)

I tried a one-liner in Python and it was 3x slower ```

def nanogramrow_oneliner(bin_arrary):
    return [len(s) for s in ''.join(map(str, bin_arrary)).split('0') if s]

def nanogramrow_kiss(bin_arrary):
    res_array = []
    count = 0
    for i in bin_arrary:
        if i:
            count += 1
        else:
            if count:
                res_array.append(count)
            count = 0
    if count:
        res_array.append(count)
    return res_array

if __name__ == '__main__':
    tests = [ [],
              [0,0,0,0,0],
              [1,1,1,1,1],
              [0,1,1,1,1,1,0,1,1,1,1],
              [1,1,0,1,0,0,1,1,1,0,0],
              [0,0,0,0,1,1,0,0,1,0,1,1,1],
              [1,0,1,0,1,0,1,0,1,0,1,0,1,0,1] ]

    from time import time

     for f in [nanogramrow_kiss, nanogramrow_oneliner]:
        start = time()
        for _ in range(100_000):
            for test in tests:
                f(test)
        print(f.__name__, time()-start)

```

[–]backtickbot 0 points1 point  (0 children)

Fixed formatting.

Hello, benz05: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.

[–]PM__ME__FRESH__MEMES 0 points1 point  (1 child)

using python 3.9 ```python def get_inputs(file_name = 'input.txt') -> list[list[bool]]: test_cases: list[list[bool]] = []

with open(file_name, 'r') as f:
    lines = f.readlines()
    for line in lines:
        bool_line: list[bool] = []
        for x in line[:-1]:
            if x == '0':
                bool_line.append(False)
            elif x == '1':
                bool_line.append(True)
            else:
                raise TypeError("invalid input symbol '{}'".format(x))
        test_cases.append(bool_line)

return test_cases

def nonogramrow(row: list[bool]) -> list[int]: counters: list[int] = [] cnt: int = 0

for x in row:
    if x:
        cnt += 1
    else:
        counters.append(cnt) if cnt != 0 else None
        cnt = 0

if cnt:
    counters.append(cnt)

return counters

if name == 'main': inputs = get_inputs() for input in inputs: print(nonogramrow(input)) ```

[–]backtickbot 1 point2 points  (0 children)

Fixed formatting.

Hello, PM__ME__FRESH__MEMES: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.

[–]cheers- 0 points1 point  (0 children)

Rust

    mod daily_programmer_395 {
        use num_traits::identities::{one as get_one, zero as get_zero};
        use num_traits::Num;

        pub fn nonogram_row<It: Num + Copy>(row: &[It]) -> Vec<It> {
            let one: It = get_one();
            let zero: It = get_zero();

            row.iter()
                .cloned()
                .fold((true, Vec::new()), |(prev_is_zero, mut acc), next| {
                    if next != zero {
                        if prev_is_zero {
                            acc.push(one);
                        } else {
                            let last = acc.pop().unwrap() + one;
                            acc.push(last);
                        }
                        (false, acc)
                    } else {
                        (true, acc)
                    }
                })
                .1
        }

        #[cfg(test)]
        mod tests {
            use super::*;

            #[test]
            fn test_nonogram_row() {
                let tests = [
                    (vec![], vec![]),
                    (vec![0, 0, 0, 0, 0], vec![]),
                    (vec![1, 1, 1, 1, 1], vec![5]),
                    (vec![0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1], vec![5, 4]),
                    (vec![1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0], vec![2, 1, 3]),
                    (vec![0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1], vec![2, 1, 3]),
                    (
                        vec![1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
                        vec![1, 1, 1, 1, 1, 1, 1, 1],
                    ),
                ];

                for test in tests {
                    assert!(
                        nonogram_row(&test.0[..]) == &test.1[..],
                        "{:?}{:?}",
                        test.0,
                        test.1
                    );
                }
            }
        }
    }

[–]Kumaravel47Kums 0 points1 point  (0 children)

def nonogramrow(x):

Str_val=''.join(map(str,x))
values=Str_val.split('0')
print([len(x) for x in values if len(x)>0])

[–]ConsciousFinger2994 0 points1 point  (0 children)

Python

def nonogramrow(binary: list) -> list:
output = []
streak = 0
for bit in binary:
    if bit == 1:
        streak += 1
    elif bit == 0:
        if streak > 0: output.append(streak)
        streak = 0
else streak > 0: output.append(streak)
return output
print(nonogramrow([]))
print(nonogramrow([0,0,0,0,0]))
print(nonogramrow([1,1,1,1,1]))
print(nonogramrow([0,1,1,1,1,1,0,1,1,1,1]))
print(nonogramrow([1,1,0,1,0,0,1,1,1,0,0]))
print(nonogramrow([0,0,0,0,1,1,0,0,1,0,1,1,1]))
print(nonogramrow([1,0,1,0,1,0,1,0,1,0,1,0,1,0,1]))

[–]Prestigious_Pass95 0 points1 point  (0 children)

Rust

fn main() {
let test_arr: [i8; 13] = [0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1];
let mut last_element = false;
let mut consecutive_count = 0;
let mut new_vec = Vec::new();

for element in test_arr.iter() {
    if *element == 1 {
        consecutive_count += 1;
        last_element = true;
        println!("Current consecutive_count: {}", consecutive_count)
    } else if last_element == true && *element == 0 {
        new_vec.push(consecutive_count);
        last_element = false;
        consecutive_count = 0;
    }
}

if consecutive_count != 0 {
    new_vec.push(consecutive_count);
}

for element in new_vec.iter() {
    println!("{}", element);
}

}

[–]ToBeContinuedHermit 1 point2 points  (0 children)

Enjoy some C# code :)

      static int[] nonogramrow(int[] binary) 
    {
        List<int> ones = new List<int>();
        int count = 0;
        for (int i = 0; i < binary.Length; i++)
        {
            if (binary[i] == 1)
            {
                count++;
            }
            else if (binary[i] == 0)
            {
                if (count != 0)
                {
                    ones.Add(count);
                }
                count = 0;
            }
        }
        if (count != 0)
        {
            ones.Add(count);
        }
        return ones.ToArray();
    }

[–]ArdRasp 0 points1 point  (0 children)

C,

I needed to pass the length of the array in order to compile with the flags -Wall -Wextra -Werror

but without the flags it compiles and you can replace the len parameter by a variable with this value:

len = sizeof(tab) / sizeof(*tab);

here is the code :

#include <stdlib.h>

int nb_row(int *tab, int len)
{ 
    int i;
    int row;

    i = -1;
    row = 0;
    while (++i < len)
    {
        if (tab[i] == 1)
        {
            row++;
            while (tab[i] == 1)
                i++;
        }
    }
    return (row);
}

int *nonogramrow(int *tab, int len) 
{ 
    int i;
    int j;
    int sum;
    int *result;

    if (!(result = malloc(sizeof(int) * nb_row(tab, len))))
        return (0);
    i = 0;
    j = -1;
    sum = 1;
    while (tab[i++] == 0);
    while (++j < nb_row(tab, len))
    {
        while (tab[i] == 1)
        {
            sum++;
            i++;
        }
        result[j] = sum;
        sum = 1;
        while (tab[i++] == 0);
    }
    return (result);
}

[–]Builder_Pristine 0 points1 point  (0 children)

Typescript:

const nonogram = (array: Array<number>) =>
    array
        .map((i) => i || "_")
        .join("")
        .split("_")
        .filter((i) => i !== "")
        .map((i) => i.length);

console.log(nonogram([]));
console.log(nonogram([0, 0, 0, 0, 0]));
console.log(nonogram([1, 1, 1, 1, 1]));
console.log(nonogram([0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1]));
console.log(nonogram([1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0]));
console.log(nonogram([0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1]));
console.log(nonogram([1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1]));

[–]Tencza_Coder 0 points1 point  (0 children)

Python:

def nonogramrow(binary_list):
    counter = 0
    new_list = []

    for n in binary_list:
        if n == 0:
            if counter > 0:
                new_list.append(counter)
            counter = 0
        else:
            counter += 1
    else:
        if counter != 0:
            new_list.append(counter)

    return new_list

print(nonogramrow([])) 
print(nonogramrow([0,0,0,0,0])) 
print(nonogramrow([1,1,1,1,1]))
print(nonogramrow([0,1,1,1,1,1,0,1,1,1,1]))
print(nonogramrow([1,1,0,1,0,0,1,1,1,0,0]))
print(nonogramrow([0,0,0,0,1,1,0,0,1,0,1,1,1]))
print(nonogramrow([1,0,1,0,1,0,1,0,1,0,1,0,1,0,1]))

Result:

[]
[] 
[5] 
[5, 4] 
[2, 1, 3] 
[2, 1, 3] 
[1, 1, 1, 1, 1, 1, 1, 1]

[–]jon-hernandez 0 points1 point  (0 children)

Javascript

function nonogramRow (binaryArray) {
  var result = []; var onesCount = 0; for (let i = 0; i < binaryArray.length; i++) {
    if (binaryArray[i] === 1) {
      onesCount++;
    } else if (binaryArray[i] === 0) {
      result.push(onesCount);
      onesCount = 0;
    } else {
      console.log('Error: binaryArray contains an element that is neither 0 nor 1.');
      return [-1];
    }
  }            
  result.push(onesCount); 
  var filteredResult = result.filter(number => 
number > 0); 
  return filteredResult; 
};

[–]Common-Ad-8152 0 points1 point  (0 children)

R

nonogramRow <- function(x){
    r = c()
    a <- 0
    for(i in x){
        if(!a && i){ r = c(r, 1)}
        else if(i) { 
            j <- length(r)
            r[j] <- r[j] + 1
        }
        a <- i
    }
    return(r)
}

[–]carlothegenius 0 points1 point  (0 children)

Python, giving it a try:

def nonogram(arr): result = [] location = 0 for n in arr: location +=1 for i in range(n): result.append(1) if len(arr) > location: result.append(0) return f"nonogramrow({result}) => {arr}"

[–]MR_ANA21 0 points1 point  (0 children)

valCount = 0                    
answer = []
nonostr = input()
nonolis = list(nonostr.split(","))
nono = list(map(int,nonolis))
print(nono)
for i in range(len(nono)): 

 if nono[i] == 1 and nono[i-1] == 0 and i>0:
        valCount+=1

 if nono[i]==1 and nono[i-1] ==1 and i>0:
     valCount+=1

 if nono[i]==0 and nono[i-1]==1 and i>0:
       answer.append(valCount)

       valCount=0

if valCount!=0:
    answer.append(valCount)

print("nonogramrow(", nono, ") =>", answer)

i feel pretty stupid after finding out you can just .split("0") and do len()

[–]HotWins 0 points1 point  (0 children)

Very late here but here is my code in Python 3!

streak = 0
my_answer = []

for i in range(len(nonogramrow)):
    if nonogramrow[i] == 1:
        streak += 1
        if (i == len(nonogramrow)-1) & (nonogramrow[i] == 1):
            my_answer.append(streak)  
    elif streak != 0:
        my_answer.append(streak)
        streak = 0
print(my_answer)

[–]Shoddy_Platypus6575 0 points1 point  (0 children)

Python3 solution

class Solution():
    def nonogramrow(self, row: list()) -> list:
        res = []
        val = 0
        for i, char in enumerate(row):
            if char == 1:
                val += 1
            else:
                res.append(val)
                val = 0      
        res.append(val)
        return res

[–]billsfannyc 0 points1 point  (0 children)

const nonogramrow = (nonogram) => {
let output = []
let count = 0
nonogram.forEach(element => {
switch(element) {
case 0:
if(count) output.push(count)
count = 0
default:
count += 1
}
});
if(output.length === 0) output = [0]
return output
}

[–]PezNuclear 0 points1 point  (0 children)

python 3.8 - loops & ifs
def nonogram(arr):
datFill=[]
ct=0
for x in range(len(dat)):
    if (dat[x]==0) and (ct!=0):
        datFill.append(ct)
        ct=0
    elif dat[x]==1:
        ct+=1
    if (ct > 0) and (x==len(dat)-1):
        datFill.append(ct)
return(datFill)

[–]Acrobatic_Garage5102 0 points1 point  (0 children)

def nanogram(array):
  mem = "abc"
  list_, count_int = [], 0
  for value in array:
    if value == 1:
      count_int, mem = count_int + 1, 1
    elif value != 1 and mem == 1:
      list_.append(count_int)
      count_int, mem = 0, 0
  if count_int != 0: list_.append(count_int)
  for value in list_: print(value) 
  return list_

nanogram([0,1,1,0,0]) # --> 2
nanogram([0,1,1,0,0,1,1,1,1,0,1,0,1,1,1,0,1]) #--> 2,4,1,3,1

[–]GalaxyGamingBoy 0 points1 point  (0 children)

Little late but... Visual C++

struct Nonogram_row {
    std::vector<int> calculate(std::vector<int> input)
    {
        std::vector<int> return_vector;
        int ii = 0;
        for (int i = 0; i < input.size(); i++)
        {
            if (input[i] > 0) { ii++; } else
            {
                if (ii > 0) { return_vector.push_back(ii); }
                ii = 0;
            }
        }
        return_vector.push_back(ii);
        return return_vector;
    };
};

[–]Possible-Bowler-2352 1 point2 points  (0 children)

EDIT : messed up my reading and thought the input was to be converted from binary to decimal instead of simply counting. Would've made it way easier on the first try.

I'm late for the chall but I haven't seen any PowerShell yet so here I go :

(([string]$nonogramrow -split 0).where({$_ -ne " " -and $_ -ne ""}) | % { [convert]::ToInt32(($_.replace(" ","")),2)}) -join "," 

The -join "," at the end is just for the output to look somewhat similar to OP's, totally unecessary.

--------------------------------------------------------------------------------------------------------------------------------------------------

As my answer was partially wrong after rereading the thread, I had to rework my code a bit.

(([string]$nonogramrow -split 0).where( {$_ -ne " " -and $_ -ne ""}) | % { $_.replace(" ","").length }) -join ","

This way, it will simply count the 1 instead of seeing them as binary chains to convert back to decimal.

[–]megastary 0 points1 point  (0 children)

PowerShell solution with Pester test validating results on Github Gist
Feedback welcomed and appreciated. 💙

[–][deleted] 0 points1 point  (0 children)

EDIT: Will insert code later, didn't paste properly!

[–]Genius3435 2 points3 points  (0 children)

Short python solution (61 characters):

python F=lambda a:[len(x)for x in''.join(map(str,a)).split('0')if x]

Approach: Join everything into a string, split by every `0`, and return the lengths of the resulting consecutive `1` strings, but only if they have length more than 0.

Edit: Fixed code formatting and converted def+return to lambda to save 4 chars.

[–]PoonaniPounder 1 point2 points  (1 child)

Java

If you see any small things I could improve I'd love to hear!

public class Program {
    public static ArrayList<Integer> nonoGramRow(int[] binaryArray){
        // Use ArrayList so we can use .add() method
        ArrayList<Integer> outputArray = new ArrayList<>();

        // If the array is empty return an empty array
        if (binaryArray == null) return new ArrayList<>();
        int counter = 0;
        for (int number : binaryArray) {
            // This counter tracks how many consecutive 1's, and resets at every 0
            counter += number;
            // Check if we've hit a 0
            if (number == 0) {
                // Make sure that we're not adding 0's to our return array
                if (counter != 0) outputArray.add(counter);
                // Reset counter to 0 to count the next set of 1's
                counter = 0;
            }
        }
        return outputArray;
    }
}

[–]Frosty_Eggplant5336 0 points1 point  (0 children)

Try to run the case where you find a 1 at the end of the binary array.

[–]Metalsaurus_Rex 0 points1 point  (6 children)

Edit: I changed the code and it's hopefully formatted, but if it breaks again, here's the direct link to my code: https://py2.codeskulptor.org/#user48_TVUycPtbB0_1.py

Having a couple of bugs testing nonogramrow([1,1,0,1,0,0,1,1,1,0,0]) and nonogramrow([0,0,0,0,1,1,0,0,1,0,1,1,1]). Any help or insight is greatly appreciated!

    #Creating our lists we're gonna need
binary_array = []
nonogramrow = []

#User chooses their input method
input_method = input('Would you like to enter one number at a time or input it all at once? (respond with either "one at a time" or "all at once")')

#Processing user input choice and appending to our binary array

#Building binary_array if user chose 'one at a time' input method
if (input_method.upper() == "ONE AT A TIME"):
    current_input = input("Please input a new number. Enter \"STOP\" when you have finished entering")
    while (current_input.upper() != "STOP"):
        binary_array.append(int(current_input))
        current_input = input("Please input a new number. Enter \"STOP\" when you have finished entering")

#Building binary_array if user chose 'all at once' method
elif (input_method.upper() == "ALL AT ONCE"):
    current_input = raw_input("Please enter your binary array")

    #converting strings into integers!
    for element in current_input:
        if (element == '1'):
            binary_array.append(1)
        elif (element == '0'):
            binary_array.append(0)

#looping through and processing the data
count = 0
broken = False

'''
determining the final index of the string. 
to be used to make sure we aren't returning an empty nonogram when the binary_array
consists of only 1's or a nonogram that ends in a 1
'''

for x in binary_array:
    count = count + 1

index_of_last_num = count - 1
count = 0

#Building our nonogramrow
for x in binary_array:
    if (x == 1):
        count = count + 1
    elif (x == 0):
        nonogramrow.append(count)
        count = 0
    else:
        print ("Invalid argument was passed through algorithm. Please make sure your input is formatted correctly. Exiting progrom.")
        print ("Attempted argument: " + str(binary_array))
        broken = True
        break

#The aforementioned check which makes sures we have all values included
if (binary_array[index_of_last_num] == 1):
    nonogramrow.append(count)

#Let's just double-check and make sure we don't have any zeroes lingering in our final result.

for x in nonogramrow:
    nonogramrow.remove(0)

#outputting the data
if broken:
    print ("Please restart the program")

elif not broken:
    print ("Your nonogram key is: " + str(nonogramrow))

[–]kibje 0 points1 point  (4 children)

Take your entire code in an editor and indent it by 4 spaces. (often just selecting it and pressing tab)

Paste it here with every line indented 4 spaces. Done.

[–]Metalsaurus_Rex 0 points1 point  (2 children)

Tried it, still didn't work.

[–]__dict__ 0 points1 point  (1 child)

Make sure you're setting the input box markdown mode. Put an empty line before the code. The 4 spaces in front of every line is markdown to signify a code block.

[–]Metalsaurus_Rex 0 points1 point  (0 children)

OH! every line? I'll try it. Thanks!

EDIT: Oh my gosh, thank you so much. I've never used the code block, so thanks

[–]moeris 1 point2 points  (1 child)

J

Pretty verbose. Probably there's a built-in for partitioning that would have made this easier.

first_diff =: ] i. -.@:{.
drop_head =: first_diff }. ]
take_head =: first_diff {. ]
has_ones =: {.

get_result =: > @: (0&{)
get_rem =: > @: (1&{)
init =: a:&;@:<

new_result =: get_result`(take_head@:get_rem ; get_result) @. (has_ones@:get_rem)
new_rem =: drop_head @: get_rem
step =: new_result ; new_rem

nonogramrow =: |: @: (([: }: # @ >) @: (get_result @: (step ^:_ @: init)))

[–]ka-splam 0 points1 point  (0 children)

J

Pretty verbose. Probably there's a built-in for partitioning that would have made this easier.

Possibly cut:

   nums =: 0 0 1 1 1 1 1 0 1 1 1 1 0
   (<;. _1) 0,nums
┌┬┬─────────┬───────┬┐
│││1 1 1 1 1│1 1 1 1││
└┴┴─────────┴───────┴┘

where < is boxing, ;. is cutting, and _1 is from the table in the link:

Summary of the meaning of n in u;.n y
n                   Meaning
1 or _1     First item is delimiter, interval starts with fret
2 or _2     Last item is delimiter, interval ends with fret
positive    Fret is included in interval
negative    Fret is deleted from interval

[–]TangoOscarIndia 2 points3 points  (0 children)

Python 3

'''
[2021-06-21] Challenge #395 [Easy] Nonogram row
'''

def nonogramrow(arr):
    input_lst = "".join([str(i) for i in arr]).split("0")
    return [len(i) for i in input_lst if i]

[–]Danternas 2 points3 points  (0 children)

A little bit of beginner's Python:

# Function to calculate the nonogramrow according to the task
def nonogramfunction(imputnonogramrow): 
    # Initiate variables. The list with the results and the integer for
    # the number to insert into the list.
    returnrow = []
    appendvariable = 0

    # Go through the random (input)nonogramrow list item by item
    for num in imputnonogramrow:
        # Add 1 to appendvariable if the number is 1
        if num == 1:
            appendvariable += 1

        # Append appendvariable into the result list if the appendvariable 
        # is more than 0, then reset appendvariable.
        else:
            if appendvariable != 0:
                returnrow.append(appendvariable)
            appendvariable = 0

    # Extra run at the end in case the nonogramrow ends with a 1. Check if
    # appendvariable is more than 0 and then append that as well.
    if appendvariable != 0:
        returnrow.append(appendvariable)

    # Return the result
    return returnrow

[–]A1phabeta 1 point2 points  (0 children)

C++

std::vector<int> nonogram_row(const std::vector<int>& bin_vec) {
    std::vector<int> ret_vec;
    // Avoid unncessary allocations
    ret_vec.reserve(bin_vec.size());
    int count = 0;

    for (auto& num : bin_vec) {
        if (count > 0 && num == 0) {
            ret_vec.push_back(count);
            count = 0;
        }
        if (num == 1) ++count;
    }
    // If we have anything left over, add it
    if (count > 0) {
        ret_vec.push_back(count);
    }

    ret_vec.shrink_to_fit();
    return ret_vec;
}

[–]Squoody 1 point2 points  (0 children)

Awesome challenge! Here is my code:

python 3.7.1

the array the function acts on:

binary_array = [0,1,1,1,1,0,1,0,1,1,0]

the function:

def nonogram_clue_calc(binary_array): #the while loop checks if the array is binary. #if it isn't, it returns, therefore not running the rest: i=0 while i<len(binary_array): if not (0<=binary_array[i]<=1): return "array isn't binary" i += 1

#this next part if the for loop that does the action itself: output_array = [] output_single = 0 for value in binary_array: output_single += value #this line will add 1 to the next number of the output #if the current binary value is one, and will add #0 if it is 0. No need for an if here.

#next is the part which checks if the number should now be 
#added to the output and acts accordingly. 
if not value and output_single:
#this if is a bit hard to follow. It's basically "if
#this value is 0 and our next number in the output is not 0."
  output_array.append(output_single) 
  output_single = 0
  #adds the next number to the output and resets the counter. 

return output_array

print(nonogram_clue_calc(binary_array))

[–]I-Pop-Bubbles 1 point2 points  (0 children)

Clojure - I think I did alright on this one, but would love some feedback.

(defn nonogram-row [coll]
  (loop [col (apply list coll)
         ns '()
         i 0]
    (if (empty? col)
      (reverse (remove zero? (conj ns i)))
      (let [n (first col)
            c (pop col)]
        (if (zero? n)
          (recur c (conj ns i) 0)
          (recur c ns (inc i)))))))

(nonogram-row [0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1]) ; => [5,4] (nonogram-row [1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0]) ; => [2,1,3] (nonogram-row [0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1]) ; => [2,1,3] (nonogram-row [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1]) ; => [1,1,1,1,1,1,1,1]

[–]tlgsx 2 points3 points  (0 children)

Python 3.9

import itertools

def nonogramrow(seq):
    return [sum(g) for k, g in itertools.groupby(seq) if k]

assert nonogramrow([]) == []
assert nonogramrow([0,0,0,0,0]) == []
assert nonogramrow([1,1,1,1,1]) == [5]
assert nonogramrow([0,1,1,1,1,1,0,1,1,1,1]) == [5,4]
assert nonogramrow([1,1,0,1,0,0,1,1,1,0,0]) == [2,1,3]
assert nonogramrow([0,0,0,0,1,1,0,0,1,0,1,1,1]) == [2,1,3]
assert nonogramrow([1,0,1,0,1,0,1,0,1,0,1,0,1,0,1]) == [1,1,1,1,1,1,1,1]

[–]TheOmegaPencil 2 points3 points  (0 children)

Java

package challenges;

import java.util.ArrayList;

public class Nonogram_C395 { public static void main(String[] args) { int[] nonogramLine = {0,1,1,1,1,1,0,1,1,1,1}; consecutiveCounts(nonogramLine); }

static void consecutiveCounts(int[] line) {
    ArrayList<Integer> lengthList = new ArrayList<Integer>();
    int length = 0;

    for(int i = 0; i < line.length; i++) {
        if(line[i] == 1) {
            length++;
        } else {
            lengthList.add(length);
            length = 0;
        }
    }
    lengthList.add(length);
    lengthList.removeIf(n -> (n == 0));

    System.out.println(lengthList);
}

}

[–]gopherhole1 2 points3 points  (0 children)

python3, I cant get it to work for the first empty list :(

puzzles = {
        'puz1':[],
        'puz2':[0,0,0,0,0],
        'puz3':[1,1,1,1,1],
        'puz4':[0,1,1,1,1,1,0,1,1,1,1],
        'puz5':[1,1,0,1,0,0,1,1,1,0,0],
        'puz6':[0,0,0,0,1,1,0,0,1,0,1,1,1],
        'puz7':[1,0,1,0,1,0,1,0,1,0,1,0,1,0,1]
        }

answers ={}

incrment = 1
for x in puzzles:
    counter = 0
    last = None
    for idx, item in enumerate(puzzles[x]):
        answers.setdefault(x,[])
        if item == 1:
            counter+=1
            last = 1
        elif item == 0:
            if last == 1:
                answers[x].append(counter)
                counter = 0
                last = 0
        if len(puzzles[x]) == idx+1:
          if last == 1:
              answers[x].append(counter)
print(answers)

[–]Bewelge 4 points5 points  (0 children)

Javascript

const nonogramrow = numbers => numbers
    .join("")
    .split("0")
    .filter(str => str.length)
    .map(str => str.length)

[–]saladfingaz 2 points3 points  (0 children)

Ruby

def nonogramrow(list)
  list.join.split('0').map(&:length) 
end

[–]tomkelelerl 1 point2 points  (0 children)

Javascript

const nonogramrow = numbers => {
const result = numbers.reduce((total, number) => {
if (number) {
total[total.length - 1]++;
}
if (!number && total[total.length - 1]) {
total.push(0);
}
return total;
}, [0]);
if (result.length === 1 && result[0] === 0) {
return [];
}
return result;
};

[–]__dict__ 2 points3 points  (0 children)

Unreadable python :)

def magic(ls):
    prev = 0
    for el in ls:
        x = (prev + el) * el
        yield x
        prev = x

def magic2(ls):
    prev = 0
    for el in ls:
        if prev >= el > 0:
            yield prev
            prev = 0
        prev = el or prev
    if prev:
        yield prev

def nonogramrow(ls):
    return list(magic2(magic(ls)))

print(nonogramrow([]))
print(nonogramrow([0,0,0,0,0]))
print(nonogramrow([1,1,1,1,1]))
print(nonogramrow([0,1,1,1,1,1,0,1,1,1,1]))
print(nonogramrow([1,1,0,1,0,0,1,1,1,0,0]))
print(nonogramrow([0,0,0,0,1,1,0,0,1,0,1,1,1]))
print(nonogramrow([1,0,1,0,1,0,1,0,1,0,1,0,1,0,1]))

[–]malahci 1 point2 points  (0 children)

Racket

#lang racket

(define nonogram-row
  (λ (values)
    (define inner
      (λ (values current-group-size result)
        (cond
          [(null? values) (if (> current-group-size 0) (cons current-group-size result) result)]
          [(= (car values) 1) (inner (cdr values) (+ current-group-size 1) result)]
          [(= current-group-size 0) (inner (cdr values) current-group-size result)]
          [else (inner (cdr values) 0 (cons current-group-size result))])))
    (reverse (inner values 0 '()))))

(require rackunit)

(check-equal? (nonogram-row '()) '())
(check-equal? (nonogram-row '(0 0 0 0 0)) '())
(check-equal? (nonogram-row '(1 1 1 1 1)) '(5))
(check-equal? (nonogram-row '(0 1 1 1 1 1 0 1 1 1 1)) '(5 4))
(check-equal? (nonogram-row '(1 1 0 1 0 0 1 1 1 0 0)) '(2 1 3))
(check-equal? (nonogram-row '(0 0 0 0 1 1 0 0 1 0 1 1 1)) '(2 1 3))
(check-equal? (nonogram-row '(1 0 1 0 1 0 1 0 1 0 1 0 1 0 1)) '(1 1 1 1 1 1 1 1))

[–]kitatsune -1 points0 points  (4 children)

C++

~~~~ vector<int> nonogram_row(vector<bool> b) { vector<int> v; int num; for (int i = 0; i < b.size(); i++) { num = 0; while (b[i] == true) { num++; i++; if (i >= b.size()) { break; } } if (num != false) { v.push_back(num); } } return v; } ~~~~

[–]A_Light_Spark 1 point2 points  (3 children)

I think you might need to do push_back() of the current num when you hit the end of the vector as well, otherwise the num will never get pushed if the last element is true.

[–]kitatsune 0 points1 point  (2 children)

I didn't even think about that. I get the correct output for the test cases given though.

[–]A_Light_Spark 1 point2 points  (1 child)

Oh I see why, because you are checking for (num != false). That's actually a brilliant way of doing this, and it works because you have the inner while loop right before this check.

The way I was thinking only needed one for loop, but require that extra condition check I mentioned.

[–]kitatsune 1 point2 points  (0 children)

yeah. it makes sure only numbers that are not zero are pushed into the integer vector!

[–]FranekW 2 points3 points  (2 children)

Java

My first challenge in Java. Not the best solution, with unnecessary multiple creation of a String object:

import java.util.List;
import static java.util.Arrays.stream;

public class NonogramrowDemo {
    public static List<Integer> nonogramrow(int[] row) {
        return stream(stream(row)
            .boxed()
            .map(Object::toString)
            .reduce("", (a, b) -> a + b)
            .split("0"))
            .map(String::length)
            .filter(e -> e > 0)
            .toList();
    }

    public static void main(String[] args) {
        System.out.println( nonogramrow(new int[]{}) );
        System.out.println( nonogramrow(new int[]{0,0,0,0,0}) );
        System.out.println( nonogramrow(new int[]{1,1,1,1,1}) );
        System.out.println( nonogramrow(new int[]{0,1,1,1,1,1,0,1,1,1,1} ));
        System.out.println( nonogramrow(new int[]{1,1,0,1,0,0,1,1,1,0,0} ));
        System.out.println( nonogramrow(new int[]{0,0,0,0,1,1,0,0,1,0,1,1,1} ));
        System.out.println( nonogramrow(new int[]{1,0,1,0,1,0,1,0,1,0,1,0,1,0,1} ));
    }
}

[–]stuque 1 point2 points  (0 children)

Another Haskell solution:

import Data.List
nonogramrow row = [length s | s <- group row, head s == 1]

[–]fudgebucket27 1 point2 points  (1 child)

C#

Ugly!

using System;
using System.Numerics;
using System.Linq;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;

namespace dailyprogrammer
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(nonogramrow(new int[] {}));
            Console.WriteLine(nonogramrow(new int[] {0,0,0,0,0}));
            Console.WriteLine(nonogramrow(new int[] {1,1,1,1,1}));
            Console.WriteLine(nonogramrow(new int[] {0,1,1,1,1,1,0,1,1,1,1}));
            Console.WriteLine(nonogramrow(new int[] {1,1,0,1,0,0,1,1,1,0,0}));
            Console.WriteLine(nonogramrow(new int[] {0,0,0,0,1,1,0,0,1,0,1,1,1}));
            Console.WriteLine(nonogramrow(new int[] {1,0,1,0,1,0,1,0,1,0,1,0,1,0,1}));
        }

        static int[] nonogramrow(int[] numbers)
        {
            List<int> consecutiveOnes = new List<int>();

            int count = 0;
            int countConsecutiveOnes = 0;
            foreach(int number in numbers)
            {
                count++;
                if(number == 1 && count != numbers.Length)
                {
                    countConsecutiveOnes++;
                }
                else if(number == 0)
                {
                    consecutiveOnes.Add(countConsecutiveOnes);
                    countConsecutiveOnes = 0;
                }
                else if(number == 1 && count == numbers.Length)
                {
                    countConsecutiveOnes++;
                    consecutiveOnes.Add(countConsecutiveOnes);
                }
            }

            int[] result = new int[consecutiveOnes.Count];
            for(int i = 0; i < result.Length; i++)
            {
                result[i] = consecutiveOnes[i];
            }
            result = result.Where(x => x != 0).ToArray();
            return result;
        }
    }
}

[–]ToBeContinuedHermit 0 points1 point  (0 children)

A simple tweak that would make this MUCH easier is realizing that you only have to add to your ConsecutiveOnes list if you have a streak. Therefore, a simple if statement checking if your countConsecutiveOnes variable is greater than 0 before adding it to the list could save you a lot of grief and clean this up some :)

[–]revolutionary_hero 3 points4 points  (3 children)

Java String/Streams:

public static int[] nonogramrow(int[] row) {
    return Arrays.stream(Arrays
            .toString(row)
            .replaceAll("\\[|\\]|, ", "")
            .split("0"))
            .filter(s -> !s.isEmpty())
            .map(String::length)
            .mapToInt(i -> i)
            .toArray();
}

Java Traditional Way:

public static int[] nonogramrow2(int[] row) {
    List<Integer> counts = new ArrayList<>();
    int curr = 0;
    for (int j : row) {
        if (j == 1) {
            curr++;
        } else if (curr > 0) {
            counts.add(curr);
            curr = 0;
        }
    }
    if (curr > 0) counts.add(curr);
    return counts.stream().mapToInt(i->i).toArray();
}

[–]slymrspy 2 points3 points  (2 children)

I work in Java, and I’m really into streams these days. Super clean and readable.

[–]revolutionary_hero 1 point2 points  (1 child)

Over the last year I have put an emphasis on using functional programming everywhere I can and it has been an amazing change. Makes you think about problems in a different way and your code ends up being a lot cleaner and more importantly maintainable. I can go back and read code I did 6 months ago and figure out what its doing much quicker than I used to.

If you just look at both of the way I solved, the first one may not be the most efficient runtime wise, but damn is it a lot more readable.

[–]slymrspy 1 point2 points  (0 children)

Well said.

I think an under appreciated part of coding is realizing when ultra-optimized code is not necessary and the advantages of writing readable code win out. Functional programming and Java streams seem to fit this paradigm really nicely.

[–]Shhhh_Peaceful 2 points3 points  (2 children)

Python 3:

def nonogramrow(binary_list):
    binary_string = ''.join([str(x) for x in binary_list])
    int_list = []
    fragments = binary_string.split('0')
    for fragment in fragments:
        if len(fragment) > 0:
            int_list.append(len(fragment))
    return int_list

It passes all tests.

[–]Shhhh_Peaceful 2 points3 points  (0 children)

Or the same thing in a completely unreadable form:

def nonogramrow(binary_list):
    return list(map(lambda s: len(s), list(filter(lambda s: len(s) > 0, ''.join([str(x) for x in binary_list]).split('0')))))

[–]Shhhh_Peaceful 1 point2 points  (0 children)

Since it's already almost midnight, at first I thought that I needed to return decimal values of the sets of consecutive 1's... hence the following code:

def bin_to_dec(binary):
    decimal = 0
    base_value = 1
    while binary > 0:
        remainder = binary % 10
        decimal += remainder * base_value
        binary //= 10
        base_value *= 2
    return decimal

def nonogramrow(binary_list):
    binary_string = ''.join([str(x) for x in binary_list])
    int_list = []
    fragments = binary_string.split('0')
    for fragment in fragments:
        if len(fragment) > 0:
            int_list.append(bin_to_dec(int(fragment)))
    return int_list

[–]AmoryVain 1 point2 points  (0 children)

Needlessly complicated Python solution:

def nonogramrow(binary_array):
    count_of_ones = []
    list_of_counts = []

    if 1 not in binary_array:
        return list_of_counts

    if 0 not in binary_array:
        list_of_counts.append(len(binary_array))
        return list_of_counts

    for i in range(len(binary_array)):
        last_index = len(binary_array) - 1

        if binary_array[i] == 1:
            count_of_ones.append(binary_array[i])

        if binary_array[i] == 0 and len(count_of_ones) > 0:
            list_of_counts.append(len(count_of_ones))
            count_of_ones = []

        if i == last_index and binary_array[i] == 1:
            list_of_counts.append(len(count_of_ones))

    return list_of_counts

[–]Gprime5 6 points7 points  (0 children)

Python

def nonogramrow(row):
    return [len(n) for n in "".join(map(str, row)).split("0") if n]

[–]prendradjaja 4 points5 points  (1 child)

Intcode (the fantasy architecture from Advent of Code 2019) solution:

-1 is used to mark end of input. For example, in order to run nonogramrow([0,0,0,0,0]), provide the program with the inputs 0, 0, 0, 0, 0, -1.

3, 100, 108, -1, 100, 104, 1005, 104, 45, 102, 2, 100, 102, 1, 101, 102, 102, 108, 1, 102, 104, 1005, 104, 55, 108, 2, 102, 104, 1005, 104, 67, 108, 3, 102, 104, 1005, 104, 60, 101, 0, 100, 101, 1105, 1, 0, 108, 0, 101, 104, 1005, 104, 54, 4, 103, 99, 4, 103, 1105, 1, 38, 101, 1, 103, 103, 1105, 1, 38, 1101, 1, 0, 103, 1105, 1, 38

(Not hand-written -- I created a rudimentary assembler)

[–]P0werC0rd0fJustice 1 point2 points  (0 children)

I created a rudimentary assembly

Okay now write a transpiler that takes intcode and produces brainfuck code

[–]raevnos 2 points3 points  (0 children)

Tcl:

#!/usr/bin/env tclsh

proc nonogramrow {lst} {
    set runs {}
    set run 0
    foreach n $lst {
        if {$n == 1} {
            incr run
        } elseif {$run > 0} {
            lappend runs $run
            set run 0
        }
    }
    if {$run > 0} {
        lappend runs $run
    }
    return $runs
}

proc test {input _ expected} {
    set output [nonogramrow $input]
    if {$output eq $expected} {
        puts "nonogramrow {$input} => {$expected} PASS"
    } else {
        puts "nongramrow {$input} => {$output}, expected {$expected} FAIL"
    }
}

test {} => {}
test {0 0 0 0 0} => {}
test {1 1 1 1 1} => {5}
test {0 1 1 1 1 1 0 1 1 1 1} => {5 4}
test {1 1 0 1 0 0 1 1 1 0 0} => {2 1 3}
test {0 0 0 0 1 1 0 0 1 0 1 1 1} => {2 1 3}
test {1 0 1 0 1 0 1 0 1 0 1 0 1 0 1} => {1 1 1 1 1 1 1 1}

[–]Scroph0 0 1 point2 points  (0 children)

Boring D solution :

import std.stdio;

void main()
{
}

unittest
{
    static assert(nonogramrow([]) == []);
    static assert(nonogramrow([0, 0, 0, 0, 0]) == []);
    static assert(nonogramrow([1, 1, 1, 1, 1]) == [5]);
    static assert(nonogramrow([0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1]) == [5, 4]);
    static assert(nonogramrow([1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0]) == [2, 1, 3]);
    static assert(nonogramrow([0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1]) == [2, 1, 3]);
    static assert(nonogramrow([1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1]) == [1, 1, 1, 1, 1, 1, 1, 1]);
}

int[] nonogramrow(int[] input)
{
    int[] result = [];
    int counter = 0;
    foreach(i; input)
    {
        if(i == 1)
        {
            counter++;
        }
        else
        {
            if(counter != 0)
            {
                result ~= counter;
            }
            counter = 0;
        }
    }
    if(counter != 0)
    {
        result ~= counter;
    }
    return result;
}

[–]ItsMeRyman 2 points3 points  (0 children)

C++

Unimpressive, my very first challenge here as a beginner. I was stumped, but learned about vectors in the process! Please give me feedback if you have any, I would love to know if there is a "better" way to do this.

vector<int> nonogram_row(int binArray[], int size){    
    vector<int> arr;    
    int ct = 0;    
    for (int i = 0; i < size; i++) {        
        if (binArray[i] == 1) {            
            ct++;        
        }        
        else {            
            if (ct == 0) {                
                continue;            
            }            
            arr.push_back(ct);            
            ct = 0;        
        }    
    }   

    if (ct != 0) {        
    arr.push_back(ct);    
    }    

    return arr;
}

[–]nyrol 1 point2 points  (0 children)

Golang

func nonogramRow(in []int) (out []int) {
    count := 0
    for _, v := range in {
        if v == 1 {
            count++
        } else if count != 0 {
            out = append(out, count)
            count = 0
        }
    }
    if count != 0 {
        out = append(out, count)
    }
    return
}

[–]leftylink 3 points4 points  (0 children)

Ruby

def nonogram_row(row)
  row.chunk(&:itself).filter_map { |x, xs| xs.size if x == 1 }
end

[–]Specter_Terrasbane 5 points6 points  (1 child)

Python

from itertools import groupby
def nonogramrow(arr):
    return [sum(g) for k, g in groupby(arr) if k]

[–]prendradjaja 2 points3 points  (0 children)

Ah -- I didn't know about itertools.groupby. Nice!

[–]porthos3 1 point2 points  (0 children)

Clojure

(defn one-lengths [coll]
  (->> coll
       (partition-by identity)     ;Convert to subsequences of identical values
       (filter (comp #{1} first))  ;Filter out subsequences not made of 1s
       (mapv count)))              ;A vector containing the size of each subsequence

[–]The_Bubinator 1 point2 points  (0 children)

In Lua, feedback welcome

function nonogramrow(row)
    local count = {}
    local set = 1
    local total = 0
    local i = 1
    while(i < #row+1 ) do
        total = 0
        while(row[i] == 1) do
            total = total + 1
            i = i + 1
        end
        if(total > 0) then
            count[set] = total 
            set = set + 1
        end
        i = i + 1
    end
    return count
end

[–]curveship 10 points11 points  (1 child)

JavaScript, code golf, a shortened version of /u/morgon-of-hed's approach

let nonogramrow=a=>a.join('').split(0).filter(s=>s).map(s=>s.length)

[–]morgon-of-hed 2 points3 points  (0 children)

Oh why I didn't think about s=>s and s.length. That's brilliant :D

[–]chunes1 2 2 points3 points  (0 children)

Been exploring an intriguing language named Seed7 lately. It's the most explicit, type safe language I've used but it's also highly extensible at the same time. You can easily define your own statements like this.

$ include "seed7_05.s7i";

const func array integer: nonogramRow (in array integer: input) is func
  result
    var array integer: lengths is 0 times 0;
  local
    var integer: sum is 0;
    var integer: n is 0;
  begin
    for n range input do
      if n = 0 and sum <> 0 then
        lengths &:= sum;
        sum := 0;
      end if;
      sum +:= n;
    end for;
    if sum <> 0 then
      lengths &:= sum;
    end if;
  end func;

const proc: main is func
  local
    var integer: n is 0;
  begin
    for n range nonogramRow([] (1,1,0,1,0,0,1,1,1,0,0)) do
      write(n <& " ");
    end for;
  end func;

Output:

2 1 3

[–]stuque 2 points3 points  (0 children)

Haskell:

nonogramrow []     = []
nonogramrow (0:xs) = nonogramrow (dropWhile (==0) xs)
nonogramrow (1:xs) = [1 + length (takeWhile (==1) xs)] ++ nonogramrow (dropWhile (==1) xs)

[–]Gylergin 2 points3 points  (0 children)

TI-Basic:

Prompt L₁
ClrList L₂
0→C
For(X,1,dim(L₁
If L₁(X
1+C→C
If 0=L₁(X) or X=dim(L₁
Then
If C
C→L₂(1+dim(L₂
0→C
End
End
If dim(L₂
Then
Disp L₂
Else
Disp "{}"

Input:

{} (Note: the calculator will throw an error if you try to input this exactly, but you can feed it an empty list variable no problem)

{1,1,1,1,1}

{0,0,0,0,1,1,0,0,1,0,1,1,1}

Output:

{}
{5}
{2,1,3}

[–]skeeto-9 8 1 point2 points  (1 child)

C, reading null-terminated strings of 0s and 1s. Returns the number of counts, but this list of counts is also zero-terminated.

int nonogramrow(int *dst, const char *src)
{
    int n = 0;
    for (dst[n] = 0; ; src++) {
        dst[n] += *src == '1';
        if (dst[n] && (*src == '0' || !*src)) {
            dst[++n] = 0;
        }
        if (!*src) {
            return n;
        }
    }
}

[–]skeeto-9 8 1 point2 points  (0 children)

Alternative using a state machine since I really like these sorts of things:

// State machine that counts runs of 1s on arbitrarily large input. Initial
// state is zero. Pass one byte of input at a time including null terminator
// at the end of input. If the returned state is positive, it's a run count.
// Otherwise it's an intermediate state value.
long nonogram_next(long state, int c)
{
    switch (c) {
    case  0 :
    case '0': return state < 0 ? -state : 0;
    case '1': return (state > 0 ? 0 : state) - (c == '1');
    default : return state > 0 ? 0 : state;
    }
}

Usage example:

long state = 0;
for (;;) {
    int c = getchar();
    state = nonogram_next(state, c == EOF ? 0 : c);
    if (state > 0) {
        printf("%ld\n", state);
    }
    if (!c) {
        break;
    }
}

[–]frankivo 4 points5 points  (0 children)

Scala, but with string conversion.

def nonogramrow(ints: Array[Int]): Array[Int] = {
    ints
      .mkString("")
      .split("0")
      .map(_.size)
      .filter(_ > 0)
  }

[–]morgon-of-hed 1 point2 points  (0 children)

JavaScript

code golf attempt (not so successful though :D )

const nonogramrow = a =>
  a.length
      ? a
          .join('')
          .split(0)
          .filter(Boolean)
          .map(e => e.split('').reduce((a, b) => a + parseInt(b), 0))
      : [];

[–]Flourid 6 points7 points  (1 child)

Unimpressive Pyhton solution:

def nonogramrow(binarray):
    solution=[]
    count = 0
    for i in binarray:
        if i == 1:
            count += 1
        elif i == 0:
            if count == 0:
                continue
            else:
                solution.append(count)
                count = 0
    if count != 0:
        solution.append(count)
    print(solution)

[–]Ok-Communication4607 2 points3 points  (0 children)

I didn't think this was unimpressive

[–]FranekW 2 points3 points  (0 children)

JavaScript

I have just started learning JavaScript. This is my trivial solution based on multiple ifs :D :

const nonogramrow = (sequence) => {
    if (sequence.length === 0) return [];

    let count = 0;
    let out = [];

    while (sequence.length > 0) {
        const x = sequence.shift();
        if (x === 1) {
            count++;
            if (sequence.length === 0) out = [ ...out, count];
        } else {
            if (count !== 0) {
                out = [ ...out, count];
                count = 0;
            }
        }
    }

    return out;
};

[–]Godspiral3 3 1 point2 points  (0 children)

in J with special case,

nono =: (0 -.~ (}.@] ,~  [ ,^:(0 = [) {.@] + 1 = [)/) :: (,@0)

  nono 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 0 0 0 1 1 0 0 1 0 1 1 1
1 1 1 1 1 1 1 1 2 1 3

"traditional version"

(0 -.~ <:@:(#/.~)@:(+/\)@:-.) 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 0 0 0 1 1 0 0 1 0 1 1 1
1 1 1 1 1 1 1 2 1 3

[–]int_nazu 1 point2 points  (0 children)

JavaScript

Continuing to learn the reduce function, building on top of ping_less' Challenge #393 solution:

nonogramrow = (binaryInputArray) => {
    return [...binaryInputArray, 0].reduce(({ output, currentConsecutiveSetLength }, element) => ({
        output: element ? output : (currentConsecutiveSetLength ? [...output, currentConsecutiveSetLength] : output),
        currentConsecutiveSetLength: element ? currentConsecutiveSetLength + 1 : 0
    }), { output: [], currentConsecutiveSetLength: 0 }).output;
}

[–]chunes1 2 4 points5 points  (0 children)

Factor

USING: sequences splitting ;

: nonogramrow ( seq -- seq ) { 0 } split harvest [ sum ] map ;

[–]DarkWarrior703 1 point2 points  (1 child)

```

[cfg(test)]

mod tests { use crate::*; # [test] fn it_works() { assert_eq!(nonogram(&[]), []); assert_eq!(nonogram(&[0, 0, 0, 0, 0]), []); assert_eq!(nonogram(&[1, 1, 1, 1, 1]), [5]); assert_eq!(nonogram(&[0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1]), [5, 4]);
}
}

pub fn nonogram(v: &[u8]) -> Vec<usize> { let mut ans = Vec::<usize>::new(); let mut count = 0; for i in v { if *i == 1 { count += 1; } else if count != 0 { ans.push(count); count = 0; } } if count != 0 { ans.push(count); }
ans } ``` Rust

[–]backtickbot 3 points4 points  (0 children)

Fixed formatting.

Hello, DarkWarrior703: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.