Problem of the Day
A new programming or logic puzzle every Mon-Fri

Loopy

Here's a fun one. In the language of your choice, come up with has many ways to loop through an array as possible. The goal is given an array of size n, print out each element in as many ways as you can come up with. To scope the problem, a standard for loop and while loop would be considered 2 different ways but using different variables on a for loop would not be considered different. A for loop and a prototype method in JavaScript which also uses a for loop would be considered two different ways. Feel free to use your own discretion as well.

Permalink: http://problemotd.com/problem/loopy/

Comments:

  • Kevin Benton - 10 years, 6 months ago

    # the array
    ar = range(0,10)
    
    # for loop
    for x in ar:
        print x
    
    # map function
    def printer(msg):
        print msg
    map(printer, ar)
    
    # join (string conversion required because of ints from range)
    print '\n'.join(map(str, ar))
    
    # not sure if formatting was required, if not
    print ar
    
    
    # a pointless generator
    def array_iterator(myarray):
        for i in myarray:
            yield i
    
    for item in array_iterator(ar):
        print item
    
    
    # a subclass
    class ListWithPrinter(list):
        def printout(self):
            for i in self:
                print i
    
    ar2 = ListWithPrinter(ar)
    ar2.printout()
    

    reply permalink

  • Hueho - 10 years, 6 months ago

    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    
    // Our test array
    static int elements[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    
    // Function for fast printing
    void print(int x) {
      printf("%d\n", x);
    }
    
    // 1: using a simple for loop
    void for_loop(int* array, size_t size) {
      for(size_t idx = 0; idx < size; idx++) 
        print(array[idx]);
    }
    
    // 1.5: using a macro
    #define range(name, start, end) for(size_t name = start; name < end; name++)
    
    void range_loop(int* array, size_t size) {
      range(idx, 0, size) print(array[idx]);
    }
    
    // 2: using a while loop
    void while_loop(int* array, size_t size) {
      size_t idx = -1;
      while(++idx < size) print(array[idx]);
    }
    
    // 3: recursion
    void recursion_loop(int* ptr, size_t proceed) {
      if(proceed > 0) {
        print(*ptr);
        return recursion_loop(ptr + 1, proceed - 1);
      }
    }
    
    // 4: another recursion, more explicit
    void _explicit_recursion_loop(int* array, size_t size, size_t idx) {
      if(idx < size) {
        print(array[idx]);
        return _explicit_recursion_loop(array, size, idx + 1);
      }
    }
    
    void explicit_recursion_loop(int* array, size_t size) {
      return _explicit_recursion_loop(array, size, 0);
    }
    
    // 4: indirect iteration
    typedef struct {
      int* array;
      size_t size;
      size_t idx;
    } iterator_t;
    
    iterator_t iterator_new(int* array, size_t size) {
      iterator_t start = { array, size, 0 };
      return start;
    }
    
    bool iterator_done(iterator_t iter) {
      return iter.idx >= iter.size; 
    }
    
    iterator_t iterator_next(iterator_t iter) {
      iterator_t next = { iter.array, iter.size, iter.idx + 1 };
      return next;
    }
    
    int iterator_current(iterator_t iter) {
      return iter.array[iter.idx];
    }
    
    void iterator_loop(int* array, size_t size) {
      iterator_t iter = iterator_new(array, size);
      while(!iterator_done(iter)) {
        print(iterator_current(iter));
        iter = iterator_next(iter);
      }
    }
    
    // Bonus: using internal iteration
    typedef void (*each_func)(int);
    
    void each(int* array, size_t size, each_func f) {
      range(idx, 0, size) f(array[idx]);
    }
    
    int main() {
      for_loop(elements, 10);
      while_loop(elements, 10);
      range_loop(elements, 10);
      recursion_loop(elements, 10);
      explicit_recursion_loop(elements, 10);
      iterator_loop(elements, 10);
      each(elements, 10, print);
    }
    

    reply permalink

Content curated by @MaxBurstein