diff options
| author | mo khan <mo@mokhan.ca> | 2015-01-22 21:08:34 -0700 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2015-01-22 21:08:34 -0700 |
| commit | 20a40444ddea212c4ee4d90902f2ed65b5bc8dc9 (patch) | |
| tree | 7f25bfa0116c39479a082e1ec03489249e446e63 | |
| parent | 96660198abbd8ed058c2fbe19a19848c3c610e01 (diff) | |
complete week 3 assignment 1.
| -rw-r--r-- | week-3/Gemfile | 5 | ||||
| -rw-r--r-- | week-3/Gemfile.lock | 15 | ||||
| -rw-r--r-- | week-3/w3-1.rb | 32 |
3 files changed, 52 insertions, 0 deletions
diff --git a/week-3/Gemfile b/week-3/Gemfile new file mode 100644 index 0000000..3483018 --- /dev/null +++ b/week-3/Gemfile @@ -0,0 +1,5 @@ +# A sample Gemfile +source "https://rubygems.org" + +gem 'mongo' +gem 'bson_ext' diff --git a/week-3/Gemfile.lock b/week-3/Gemfile.lock new file mode 100644 index 0000000..bfa496f --- /dev/null +++ b/week-3/Gemfile.lock @@ -0,0 +1,15 @@ +GEM + remote: https://rubygems.org/ + specs: + bson (1.11.1) + bson_ext (1.11.1) + bson (~> 1.11.1) + mongo (1.11.1) + bson (= 1.11.1) + +PLATFORMS + ruby + +DEPENDENCIES + bson_ext + mongo diff --git a/week-3/w3-1.rb b/week-3/w3-1.rb new file mode 100644 index 0000000..378e8e6 --- /dev/null +++ b/week-3/w3-1.rb @@ -0,0 +1,32 @@ +require 'rubygems' +require 'bundler' + +# Remove the lowest homework score for each student. + +Bundler.require + +def remove_lowest_homework_from_scores(scores) + homework_scores = scores.find_all do |score| + score['type'] == 'homework' + end + + lowest_score = homework_scores.min { |x, y| x['score'] <=> y['score'] } + scores.delete(lowest_score) + + scores +end + +def main + client = Mongo::MongoClient.new + db = client['school'] + collection = db['students'] + + students = collection.find({'scores.type':'homework'}).to_a + students.each do |student| + puts student + remove_lowest_homework_from_scores(student['scores']) + collection.update({ "_id" => student['_id']}, student) + end +end + +main |
