Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Here's what I'm trying to do:

Correct answers: A C A B D B ...

Student:         Answers:            Score:
--------         --------            ------
Charlie A.       A D A B D C ...     4/6
George B.        A C A B D D ...     5/6

How do you count the right answers by comparing the answers from each student with the answers in the first row? I want to be able to compute the score of the tests by entering in the answers.

share|improve this question
    
Are the answers in their own cells? How many answers are there? –  Paul Dec 30 '12 at 13:18

1 Answer 1

Here's a shell script that just prints the number of same characters without doing any formatting:

#!/bin/bash

solutions="ACABDB"
input="Charlie A.,ADABDC
George B.,ACABDD"
IFS=$'\n'
for line in $input; do
  name=${line%,*}
  answers=${line#*,}
  correct=0
  for i in $(seq ${#solutions}); do
    [ ${answers:$i:1} == ${solutions:$i:1} ] && ((correct++))
  done
  echo "$line,$correct"
done

And a Ruby version that formats the results as a monospace table:

solutions = "ACABDB"
"Charlie A.,ADABDC
George B.,ACABDD".split("\n").each { |line|
  name, answer = line.split(",")
  correct = 0
  (0...solutions.length).each { |i|
    correct += 1 if answer[i] == solutions[i]
  }
  puts "#{"%-17s" % name}#{"%-20s" % answer.split("").join(" ")}#{correct}/#{solutions.length}"
}

You can save the scripts with TextEdit (in plain text mode) and then run them from Terminal with something like bash ~/Desktop/script.sh or ruby ~/Desktop/script.rb.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.