Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: hotarubi/directory_summary
base: 62d9795c67
...
head fork: hotarubi/directory_summary
compare: f71c8f8951
Checking mergeability… Don't worry, you can still create the pull request.
  • 3 commits
  • 27 files changed
  • 0 commit comments
  • 1 contributor
View
1  .rvmrc
@@ -0,0 +1 @@
+rvm --create use 1.8.7-p334@directory_summary
View
3  Gemfile
@@ -0,0 +1,3 @@
+source 'https://rubygems.org'
+
+gem 'rspec'
View
18 Gemfile.lock
@@ -0,0 +1,18 @@
+GEM
+ remote: https://rubygems.org/
+ specs:
+ diff-lcs (1.2.1)
+ rspec (2.13.0)
+ rspec-core (~> 2.13.0)
+ rspec-expectations (~> 2.13.0)
+ rspec-mocks (~> 2.13.0)
+ rspec-core (2.13.0)
+ rspec-expectations (2.13.0)
+ diff-lcs (>= 1.1.3, < 2.0)
+ rspec-mocks (2.13.0)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ rspec
View
17 README.md
@@ -1,2 +1,17 @@
directory_summary
-=================
+=================
+If RVM is installed, please select yes when rvm script run to create gem set especially for this project.
+
+All the functions and tests pass under ruby 1.8.7-p334 and 1.9.3-p194.
+
+Please unzip the file and run
+
+`ruby directory_summary.rb [-r] directory_path`
+
+to check the function.
+
+To run test, please type
+
+`bundle install`
+and then
+`bundle exec rspec spec/directory_summary_spec.rb`
View
28 directory_counter.rb
@@ -0,0 +1,28 @@
+class DirectoryCounter
+ def initialize(argv)
+ @path, @recursive = argv[0], false if argv.length == 1
+ @path, @recursive = argv[1], argv[0] == '-r' if argv.length > 1
+ @map = { }
+ end
+
+ def count(path = @path)
+ filter = lambda {|p| p =~ /^(.+\.)*[^\.]+$/ }
+ Dir.entries(path).select(&filter).each do |filename|
+ full_path = "#{path}/#{filename}"
+ count full_path if @recursive && File.directory?(full_path)
+ accumulate File.new(full_path) unless File.directory?(full_path)
+ end
+ @map
+ end
+
+ def accumulate(file)
+ extname = File.extname(file.path)[1..-1]
+ size = File.size file.path
+ list = @map[extname] || { 'num' => 0, 'total' => 0, 'max' => 0, 'min' => nil }
+ list['num'] += 1
+ list['total'] += size
+ list['max'] = [list['max'], size].max
+ list['min'] = list['min'] ? [list['min'], size].min : size
+ @map[extname] = list
+ end
+end
View
8 directory_summary.rb
@@ -1,5 +1,5 @@
-def recursive?
- ARGV[0] == '-r'
-end
+require './directory_counter'
+require './summary_printer'
-puts recursive?
+counter = DirectoryCounter.new(ARGV)
+puts SummaryPrinter.new(counter.count).print
View
BIN  spec/.DS_Store
Binary file not shown
View
13 spec/directory_summary_spec.rb
@@ -0,0 +1,13 @@
+describe "output a summary of the files in the directory" do
+ let(:pwd) { ENV['PWD'] }
+
+ context "without -r argument" do
+ subject { `ruby #{pwd}/directory_summary.rb #{pwd}/spec/fixtures` }
+ it { should == File.read("#{pwd}/spec/expectations/non_recursive") }
+ end
+
+ context "with -r argument" do
+ subject { `ruby #{pwd}/directory_summary.rb -r #{pwd}/spec/fixtures` }
+ it { should == File.read("#{pwd}/spec/expectations/recursive") }
+ end
+end
View
20 spec/expectations/non_recursive
@@ -0,0 +1,20 @@
+html:
+ number of files: 2
+ combined size: 1111
+ largest file: 677
+ smallest file: 434
+jpg:
+ number of files: 2
+ combined size: 165741
+ largest file: 118053
+ smallest file: 47688
+txt:
+ number of files: 3
+ combined size: 1230
+ largest file: 672
+ smallest file: 5
+zip:
+ number of files: 4
+ combined size: 139813
+ largest file: 136010
+ smallest file: 760
View
30 spec/expectations/recursive
@@ -0,0 +1,30 @@
+css:
+ number of files: 1
+ combined size: 11
+ largest file: 11
+ smallest file: 11
+html:
+ number of files: 2
+ combined size: 1111
+ largest file: 677
+ smallest file: 434
+jpg:
+ number of files: 2
+ combined size: 165741
+ largest file: 118053
+ smallest file: 47688
+js:
+ number of files: 3
+ combined size: 4481
+ largest file: 3923
+ smallest file: 191
+txt:
+ number of files: 3
+ combined size: 1230
+ largest file: 672
+ smallest file: 5
+zip:
+ number of files: 4
+ combined size: 139813
+ largest file: 136010
+ smallest file: 760
View
BIN  spec/fixtures/.DS_Store
Binary file not shown
View
BIN  spec/fixtures/Archive1.zip
Binary file not shown
View
BIN  spec/fixtures/Archive2.zip
Binary file not shown
View
1  spec/fixtures/bubble.html
@@ -0,0 +1 @@
+<?xml version="1.0" encoding="utf-8"?> <!-- Generator: IcoMoon.io --> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="48px" height="48px" viewBox="0 0 48 48" enable-background="new 0 0 16 16" xml:space="preserve" fill="#000000"> <path d="M 39.00,39.00l-9.00,0.00 l-6.00,9.00l-6.00-9.00L9.00,39.00 c-4.971,0.00-9.00-4.032-9.00-9.00L0.00,9.00 c0.00-4.971, 4.029-9.00, 9.00-9.00 l30.00,0.00 c 4.968,0.00, 9.00,4.029, 9.00,9.00l0.00,21.00 C 48.00,34.968, 43.968,39.00, 39.00,39.00z" ></path></svg>
View
BIN  spec/fixtures/bubble.html.zip
Binary file not shown
View
4 spec/fixtures/css/app.css
@@ -0,0 +1,4 @@
+body {
+
+}
+
View
14 spec/fixtures/index.html
@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
+ <link rel="stylesheet" href="css/app.css" type="text/css" charset="utf-8">
+</head>
+ <body>
+ <div id="bar-chart"></div>
+
+ <script type="text/javascript" src="js/jquery.js"></script>
+ <script type="text/javascript" src="js/raphael.js"></script>
+ <script type="text/javascript" src="js/app.js"></script>
+ </body>
+</html>
View
BIN  spec/fixtures/index.html.zip
Binary file not shown
View
113 spec/fixtures/js/app.js
@@ -0,0 +1,113 @@
+
+/*jslint white: false, onevar: true, browser: true, devel: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, strict: false, newcap: true, immed: true */
+/*global jQuery, $, Raphael */
+
+function BarChart(domID, scale, ratio, data) {
+ if ( !(this instanceof arguments.callee) ) {
+ return new arguments.callee(arguments);
+ }
+
+ var self = this,
+ width = 100 * scale,
+ height = width * ratio,
+ bar_height = height * 0.25,
+ bar_length = width * 0.8,
+ x = width * 0.1,
+ y = height * 0.375,
+ color_orange = '#FFA05C',
+ color_red = '#FC5A5E',
+ color_blue = '#CCE6FF',
+ color_green = '#239017',
+ color_dark = '#3D3D3D'
+ bar_data = [];
+
+ self.bubblePath = "M 39.00,39.00l-9.00,0.00 l-6.00,9.00l-6.00-9.00L9.00,39.00 c-4.971,0.00-9.00-4.032-9.00-9.00L0.00,9.00 c0.00-4.971, 4.029-9.00, 9.00-9.00 l30.00,0.00 c 4.968,0.00, 9.00,4.029, 9.00,9.00l0.00,21.00 C 48.00,34.968, 43.968,39.00, 39.00,39.00z";
+
+ self.init = function() {
+ self.paper = Raphael(domID, width, height);
+ self.parseData();
+ self.draw();
+ };
+
+ self.parseData = function() {
+ if ( data.actual > data.total_benchmark ) {
+ bar_data.push([1.0, color_green, data.actual]);
+ bar_data.push([data.total_benchmark / data.actual, color_blue, data.total_benchmark]);
+ bar_data.push([data.benchmark_td / data.actual, color_orange, data.benchmark_td]);
+ } else if ( data.actual > data.benchmark_td ) {
+ bar_data.push([1.0, color_blue, data.total_benchmark]);
+ bar_data.push([data.actual / data.total_benchmark, color_green, data.actual]);
+ bar_data.push([data.benchmark_td / data.total_benchmark, color_orange, data.benchmark_td]);
+ } else {
+ bar_data.push([1.0, color_blue, data.total_benchmark]);
+ bar_data.push([data.benchmark_td / data.total_benchmark, color_red, data.benchmark_td]);
+ bar_data.push([data.actual / data.total_benchmark, color_orange, data.actual]);
+ }
+ };
+
+ self.draw = function() {
+ $(bar_data).each(function(i, d){
+ self.drawBar(d[0], d[1]);
+ self.drawTooltip(d[0], d[2]);
+ });
+ };
+
+ self.drawBar = function(proportion, color) {
+ var bar, hoverFunc, hideFunc;
+ bar = self.paper.rect(x, y, bar_length * proportion, bar_height);
+ bar.attr({
+ 'stroke-width': 0,
+ 'fill': color,
+ 'fill-opacity': 1.0,
+ 'stroke': color_dark
+ });
+ hoverFunc = function() {
+ bar.animate({'stroke-width': 1}, 1000, 'bounce');
+ };
+ hideFunc = function() {
+ bar.animate({'stroke-width': 0 }, 1000, 'bounce');
+ };
+ $(bar.node).hover(hoverFunc, hideFunc);
+ };
+
+ self.drawTooltip = function(proportion, text) {
+ var tooltip, label, hoverFunc, hideFunc, scale = 0.5;
+ tooltip = self.paper.path(self.bubblePath);
+ tooltip.translate(bar_length * proportion, - bar_height / 2);
+ tooltip.scale(scale);
+ tooltip.attr({
+ 'stroke-width': 0,
+ 'fill' : color_dark,
+ 'fill-opacity': 0.9
+ });
+
+ label = self.paper.text(bar_length * (proportion + 0.12), bar_height * 0.6, text);
+ label.attr({
+ 'fill': '#ffffff'
+ , 'font-size': 6
+ , 'font-family': "'League Gothic', 'Futura-CondensedMedium', 'Gill Sans MT Condensed', 'Arial Narrow', sans-serif"
+ });
+ label.toBack();
+ tooltip.toBack();
+
+ hoverFunc = function() {
+ tooltip.animate({scale: 0.6}, 1000, 'bounce');
+ };
+ hideFunc = function() {
+ tooltip.animate({scale: scale}, 1000, 'bounce');
+ };
+ $(tooltip.node).hover(hoverFunc, hideFunc);
+ $(label.node).hover(hoverFunc, hideFunc);
+ };
+
+ self.init();
+}
+
+var chart;
+jQuery(function () {
+
+ chart = new BarChart('bar-chart', 2.5, 0.3, {actual: 2400.0, benchmark_td: 2000.0, total_benchmark: 4000.0});
+
+});
+
+
View
15 spec/fixtures/js/jquery.js
@@ -0,0 +1,15 @@
+/*!
+ * jQuery JavaScript Library v1.4.2
+ * http://jquery.com/
+ *
+ * Copyright 2010, John Resig
+ * Dual licensed under the MIT or GPL Version 2 licenses.
+ * http://jquery.org/license
+ *
+ * Includes Sizzle.js
+ * http://sizzlejs.com/
+ * Copyright 2010, The Dojo Foundation
+ * Released under the MIT, BSD, and GPL Licenses.
+ *
+ * Date: Sat Feb 13 22:33:48 2010 -0500
+ */
View
6 spec/fixtures/js/raphael.js
@@ -0,0 +1,6 @@
+/*!
+ * Raphael 1.5.2 - JavaScript Vector Library
+ *
+ * Copyright (c) 2010 Dmitry Baranovskiy (http://raphaeljs.com)
+ * Licensed under the MIT (http://raphaeljs.com/license.html) license.
+ */
View
BIN  spec/fixtures/test1.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
1  spec/fixtures/test1.txt
@@ -0,0 +1 @@
+Screening test before interview Create a program that, given a directory path, it creates a summary of the files in the directory. You can create the program either in Ruby or Java. The program will be invoked as follows: For Java: java DirectorySummary [-r] directory_path For Ruby: ruby directory_summary.rb [-r] directory_path The optional '-r' argument indicates that the program should work recursively through subdirectories. If the -r option is not specified, the summary will be for the files in the given directory only, and will ignore subdirectories. The output is a summary of file types, grouped by file extension. For each extension, the program will output:
View
BIN  spec/fixtures/test2.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
1  spec/fixtures/test2.txt
@@ -0,0 +1 @@
+The output is a summary of file types, grouped by file extension. For each extension, the program will output: For example: The identation for the lines in the summary is two spaces. If you are doing it in Ruby, submit a zip file containing a Gemfile with the gems you have used, and the source code file. If you are doing it in Java, submit a zip file containing two directories, src and lib. Put your code under the src directory, and any jar you may have used under the lib directory. Please complete this assignment and return to us within 48 hours.
View
1  spec/fixtures/test3.txt
@@ -0,0 +1 @@
+test3
View
17 summary_printer.rb
@@ -0,0 +1,17 @@
+class SummaryPrinter
+ def initialize(map)
+ @map = map
+ end
+
+ def print
+ @map.map {|k, v|
+ <<-TEMPLATE.gsub(/^ {8}/, '')
+ #{k}:
+ number of files: #{v['num']}
+ combined size: #{v['total']}
+ largest file: #{v['max']}
+ smallest file: #{v['min']}
+ TEMPLATE
+ }.sort.join
+ end
+end

No commit comments for this range

Something went wrong with that request. Please try again.