Code Reading Questions at OSCON

Wednesday May 17, 2017

O'Reilly's 2017 OSCON had a "code game" with ten questions covering nine different languages. It was supposed to get people engaged in the expo hall, but the quiz-like gamification content reminded me of my old code reading question idea. The questions are available in a PDF (mirror). I'll put the questions here as well.


1. Rust

use std::collections::HashSet;
use std::io::{BufRead, Result};

fn f<I: BufRead>(input: &mut I) -> Result<usize> {
    Ok(input.lines()
       .map(|r| r.expect(“ara ara”))
       .flat_map(|l| l.split_whitespace()
                     .map(str::to_owned)
                     .collect::<Vec<_>>())
       .collect::<HashSet<_>>()
       .len())
}

This function reads input. What else does it do?


2. JavaScript

function search(values, target) {
  for(var i = 0; i < values.length; ++i){
    if (values[i] == target) { return i; }
  }
  return -1;
}

What does this function do?


3. Go

func function(s []float64) float64 {
        var sum float64 = 0.0
        for _, n := range s {
                sum += n
        }
        return sum / float64(len(s))
}

What does this function do?


4. Perl 5

sub mystery {
    return @_ if @_ < 2;
    my $p = pop;
    mystery(grep $_ < $p, @_), $p,
    mystery(grep $_ >= $p, @_);
}

What does the mystery subroutine do?


5. Java 8

static void function(int[] ar)
 {
   Random rnd = ThreadLocalRandom.current();
   for (int i = ar.length - 1; i > 0; i--)
   {
     int index = rnd.nextInt(i + 1);
     int a = ar[index];
     ar[index] = ar[i];
     ar[i] = a;
   }
 }

What does this function do?


6. Ruby

def f(hash)
  prs = hash.inject({}) do |hsh, pr|
    k, v = yield pr
    hsh.merge(k => v)
  end
  Hash[prs]
end

What does this function do?


7. Python

def function(list):
     return [x for x in list if x == x[::-1]]

What does this function do?


8. Scala

object Op {
  val r1: Regex = “””([^aeiouAEIOU\d\s]+)([^\d\s]*)$”””.r
  val r2: Regex = “””[aeiouAEIOU][^\d\s]*$”””.r
  val s1:String = “\u0061\u0079”
  val s2:String = “\u0077” + s1
  def apply(s: String): String = {
    s.toList match {
      case Nil => “”
      case _ => s match {
        case r1(c, r) => r ++ c ++ s1 case r2(_*) => s ++ s2
        case _ => throw new
RuntimeException(“Sorry”)
      }
    }
  }
}

What does this function do?


9. Swift

import Foundation

let i = “Hellø, Swift”

let t = i.precomposedStringWithCanonicalMapping

let c = t.utf8.map({UnicodeScalar($0+2)})
let j = i.utf8.map({UnicodeScalar($0+1)}).count / 2

var d = String(repeating: String(describing: c[j]), count: j)

d.append(Character(“🇦🇺🇺🇸”))

let result = “\(d): \(d.characters.count)”

What is the value of “result”?


10. JavaScript

function thing (n) {
    for (var i = 0; i < n; i++) {
        setTimeout(function () {console.log(i);}, 0);
    }
}

What does this function do?


View HTML source to see the answers.

I tried to keep the above close to what appeared on the physical cards. I haven't tried to correct the little mistakes and bizarre indentation throughout.

It's fun!