From 5149cf37c4103a7a78dbf3af11747ed93878e5eb Mon Sep 17 00:00:00 2001 From: Max Bezlegkiy Date: Sat, 18 Feb 2023 22:30:32 +0300 Subject: [PATCH 1/9] [feature/test-in-terminal] add automatic exam checker --- Gemfile | 5 ++ Gemfile.lock | 65 +++++++++++++++ gold.md | 211 +++++++++++++++++++++++++------------------------ silver.md | 104 ++++++++++++------------ test.rb | 107 +++++++++++++++++++++++++ test_help.md | 9 +++ test_readme.md | 39 +++++++++ 7 files changed, 384 insertions(+), 156 deletions(-) create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100644 test.rb create mode 100644 test_help.md create mode 100644 test_readme.md diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..2133eb0 --- /dev/null +++ b/Gemfile @@ -0,0 +1,5 @@ +source "https://rubygems.org" + +gem "standard" +gem "tty-markdown" +gem "timeout" diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..cc42973 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,65 @@ +GEM + remote: https://rubygems.org/ + specs: + ast (2.4.2) + json (2.6.3) + kramdown (2.4.0) + rexml + language_server-protocol (3.17.0.3) + parallel (1.22.1) + parser (3.2.1.0) + ast (~> 2.4.1) + pastel (0.8.0) + tty-color (~> 0.5) + rainbow (3.1.1) + regexp_parser (2.7.0) + rexml (3.2.5) + rouge (3.30.0) + rubocop (1.44.1) + json (~> 2.3) + parallel (~> 1.10) + parser (>= 3.2.0.0) + rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 1.8, < 3.0) + rexml (>= 3.2.5, < 4.0) + rubocop-ast (>= 1.24.1, < 2.0) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 2.4.0, < 3.0) + rubocop-ast (1.26.0) + parser (>= 3.2.1.0) + rubocop-performance (1.15.2) + rubocop (>= 1.7.0, < 2.0) + rubocop-ast (>= 0.4.0) + ruby-progressbar (1.11.0) + standard (1.24.3) + language_server-protocol (~> 3.17.0.2) + rubocop (= 1.44.1) + rubocop-performance (= 1.15.2) + strings (0.2.1) + strings-ansi (~> 0.2) + unicode-display_width (>= 1.5, < 3.0) + unicode_utils (~> 1.4) + strings-ansi (0.2.0) + timeout (0.3.2) + tty-color (0.6.0) + tty-markdown (0.7.1) + kramdown (>= 1.16.2, < 3.0) + pastel (~> 0.8) + rouge (~> 3.14) + strings (~> 0.2.0) + tty-color (~> 0.5) + tty-screen (~> 0.8) + tty-screen (0.8.1) + unicode-display_width (2.4.2) + unicode_utils (1.4.0) + +PLATFORMS + x86_64-linux + +DEPENDENCIES + standard + timeout + tty-markdown + +BUNDLED WITH + 2.3.26 diff --git a/gold.md b/gold.md index cb45dff..82a3a6d 100644 --- a/gold.md +++ b/gold.md @@ -4,7 +4,7 @@ **Q1. Assume that the following code must have the stated execution result:** -``` +```ruby class Stack def initialize @contents = [] @@ -25,7 +25,7 @@ p stack.pop **Which option can be inserted into `__(1)__`? (Choose two.)** *A:* -``` +```ruby [:push, :pop].each do |name| define_method(name) do |*args| @contents.send(name, *args) @@ -34,7 +34,7 @@ end ``` *B:* -``` +```ruby for name in [:push, :pop] define_method(name) do |*args| @contents.send(name, *args) @@ -43,7 +43,7 @@ end ``` *C:* -``` +```ruby [:push, :pop].each do |name| instance_eval(<<-EOF) def #{name}(*args) @@ -54,7 +54,7 @@ end ``` *D:* -``` +```ruby [:push, :pop].each do |name| class_eval(<<-EOF) def #{name}(*args) @@ -68,7 +68,7 @@ end **Q2. Given the following code:** -``` +```ruby module I end @@ -97,7 +97,7 @@ p C.ancestors **Q3. Assume that the following code must have the stated execution result:** -``` +```ruby x = __(1)__ p x + 1 @@ -119,7 +119,7 @@ p x + 1 **Q4. Assume that the following code must have the stated execution result:** -``` +```ruby p ("aaaaaa".."zzzzzz").lazy.select { |e| e.end_with?("f") }.__(1)__ [Execution Result] @@ -140,7 +140,7 @@ p ("aaaaaa".."zzzzzz").lazy.select { |e| e.end_with?("f") }.__(1)__ **Q5. Assume that the following code must have the stated execution result:** -``` +```ruby def round(n, __(1)__) n.round(__(1)__) end @@ -165,7 +165,7 @@ p round(2.5, half: :even) **Q6. Given the following code:** -``` +```ruby class A def foo self.bar @@ -207,7 +207,7 @@ An exception is raised **Q7. Assume that the following code must have the stated execution result.** -``` +```ruby class Greeter class << self def hello @@ -236,7 +236,7 @@ Hello there! **Q8: Assume that the following code must have the stated execution result.** -``` +```ruby __(1)__ p multiply_by(4) { 2 + 3 } @@ -249,7 +249,7 @@ p multiply_by(4) { 2 + 3 } *A:* -``` +```ruby def multiply_by(n, &block) n * block.call end @@ -257,7 +257,7 @@ end *B:* -``` +```ruby def multiply_by(n, &block) n * block end @@ -265,7 +265,7 @@ end *C:* -``` +```ruby def multiply_by(n) n * yield end @@ -273,7 +273,7 @@ end *D:* -``` +```ruby def multiply_by(n) n * yield.call end @@ -283,7 +283,7 @@ end **Q9: Assume that the following code must have the stated execution result.** -``` +```ruby __(1)__ p sum { |e| e << 1 << 5 << 7 } @@ -296,7 +296,7 @@ p sum { |e| e << 1 << 5 << 7 } *A:* -``` +```ruby def sum(&block) array = [] @@ -307,7 +307,8 @@ end ``` *B:* -``` + +```ruby def sum(&block) array = [] @@ -318,7 +319,8 @@ end ``` *C:* -``` + +```ruby def sum array = [] @@ -329,7 +331,8 @@ end ``` *D:* -``` + +```ruby def sum array = [] @@ -343,7 +346,7 @@ end **Q10. Given the following code:** -``` +```ruby class A @@x = 1 end @@ -378,7 +381,7 @@ p B.x **Q11. Assume that the following code must have the stated execution result:** -``` +```ruby words = ["apple", "banana", "cabbage"] pop = Proc.new { words.pop } 3.times{ puts __(1)__ } @@ -405,7 +408,7 @@ apple **Q12. Assume that the following code must have the stated execution result:** -``` +```ruby words = ["apple", "banana", "cabbage"] pop = __(1)__{ words.pop } 3.times{ puts pop.call } @@ -430,7 +433,7 @@ apple **Q13. Assume that the following code must have the stated execution result.** -``` +```ruby add = __(1)__ puts add.call("hello") @@ -452,7 +455,7 @@ HELLO **Q14. Assume that the following code must have the stated execution result:** -``` +```ruby __(1)__ x + y end @@ -477,7 +480,7 @@ p add(1, 2) **Q15. Given the following code:** -``` +```ruby def reader_method(s) <<~EOF def #{s} @@ -492,21 +495,21 @@ print reader_method("foo") **Which option corresponds to the execution result? (Choose one.)** *A:* -``` +```ruby def foo @foo end ``` *B:* -``` +```ruby def foo @foo end ``` *C:* -``` +```ruby def foo @foo end @@ -519,7 +522,7 @@ A syntax error occurs. **Q16. Assume that the following code must have the stated execution result:** -``` +```ruby obj = Object.new def obj.hello @@ -548,7 +551,7 @@ Hi! **Q17. Assume that the following code must have the stated execution result.** -``` +```ruby class ShoppingList def initialize @items = [] @@ -591,7 +594,7 @@ puts list **Q18. Assume that the following code must have the stated execution result.** -``` +```ruby class ShoppingList def initialize @items = [] @@ -633,7 +636,7 @@ ShoppingList: **Q19. Assume that the following code must have the stated execution result.** -``` +```ruby p __(1)__.flat_map {|z| (1..z).flat_map {|x| (x..z).select {|y| @@ -662,7 +665,7 @@ p __(1)__.flat_map {|z| **Q20. Assume that the following code must have the stated execution result.** -``` +```ruby ary = ["foo", "bar", nil, "baz"] p ary.__(1)__ { |i| @@ -687,7 +690,7 @@ p ary.__(1)__ { |i| **Q21. Given the following code:** -``` +```ruby a, b, *c = ["apple", "banana", "carrot", "daikon"] p c @@ -707,13 +710,13 @@ p c **Q22: Which code produces the following execution result? (Choose one.)** -``` +```ruby [Execution Result] [["apple", "banana", "carrot"]] ``` *A:* -``` +```ruby def fx(*args) p(args) end @@ -721,7 +724,7 @@ fx(*["apple", "banana", "carrot"]) ``` *B:* -``` +```ruby def fx(*args) p(args) end @@ -729,7 +732,7 @@ fx(["apple", "banana", "carrot"]) ``` *C:* -``` +```ruby def fx(*args) p(args) end @@ -737,7 +740,7 @@ fx("apple", "banana", "carrot") ``` *D:* -``` +```ruby def fx(*args) p(*args) end @@ -748,7 +751,7 @@ fx(["apple", "banana", "carrot"]) **Q23. Assume that the following code must have the stated execution result.** -``` +```ruby p ["foo", "bar", "baz"].map { __(1)__.upcase } [Execution Result] @@ -769,7 +772,7 @@ p ["foo", "bar", "baz"].map { __(1)__.upcase } **Q24. Given the following code:** -``` +```ruby def fx(a:, b: "apple") p a p b @@ -781,13 +784,13 @@ fx(b: "banana") **Which option corresponds to the execution result?** *A:* -``` +```ruby nil apple ``` *B:* -``` +```ruby nil banana ``` @@ -802,7 +805,7 @@ An exception is raised **Q25. Assume that the following code must have the stated execution result.** -``` +```ruby def add(x:, y:, **params) z = x + y @@ -831,7 +834,7 @@ __(1)__ **Q26: Assume that the following code must have the stated execution result.** -``` +```ruby class Speaker @message = "Hello!" @@ -864,7 +867,7 @@ Hello! **Q27. Given the following code:** -``` +```ruby class Speaker @message = "Hello!" @@ -892,7 +895,7 @@ end **Q28. Assume that the following code must have the stated execution result.** -``` +```ruby def x puts "x" end @@ -935,7 +938,7 @@ done! **Q29. Assume that the following code must have the stated execution result.** -``` +```ruby letters = catch(:done) do ("a".."z").each do |a| ("a".."z").each do |b| @@ -967,7 +970,7 @@ abc **Q30. Assume that the following code must have the stated execution result.** -``` +```ruby begin __(1)__ rescue @@ -992,7 +995,7 @@ OK **Q31. Given the following code:** -``` +```ruby AnError = Class.new(Exception) begin @@ -1011,27 +1014,27 @@ end **Which option corresponds to the execution result? (Choose one.)** *A:* -``` +```ruby Bare rescue ``` *B:* -``` +```ruby StandardError rescue ``` *C:* -``` +```ruby AnError rescue ``` *D:* -``` +```ruby Exception rescue ``` *E:* -``` +```ruby AnError rescue Exception rescue ``` @@ -1040,7 +1043,7 @@ Exception rescue **Q32. Given the following code:** -``` +```ruby AnError = Class.new(Exception) begin @@ -1059,27 +1062,27 @@ end **Which option corresponds to the execution result? (Choose one.)** *A:* -``` +```ruby Bare rescue ``` *B:* -``` +```ruby StandardError rescue ``` *C:* -``` +```ruby AnError rescue ``` *D:* -``` +```ruby Exception rescue ``` *E:* -``` +```ruby Exception rescue AnError rescue ``` @@ -1088,7 +1091,7 @@ AnError rescue **Q33. Given the following code:** -``` +```ruby begin "hello".world rescue => ex @@ -1112,7 +1115,7 @@ end **Q34. Given the following code:** -``` +```ruby CustomError = Class.new(StandardError) def boom @@ -1144,7 +1147,7 @@ end **Q35. Given the following code:** -``` +```ruby def greeting "hello" ensure @@ -1160,27 +1163,27 @@ puts greeting *A:* -``` +```ruby hello ``` *B:* -``` +```ruby hi ``` *C:* -``` +```ruby Ensure called! hello ``` *D:* -``` +```ruby Ensure called! hi ``` @@ -1189,7 +1192,7 @@ hi **Q36. Given the following code:** -``` +```ruby class Identity def self.this_object self @@ -1214,25 +1217,25 @@ p c == d *A:* -``` +```ruby true true ``` *B:* -``` +```ruby true false ``` *C:* -``` +```ruby false true ``` *D:* -``` +```ruby false false ``` @@ -1241,7 +1244,7 @@ false **Q37. Given the following code:** -``` +```ruby class Identity def self.this_object self.class @@ -1259,25 +1262,25 @@ p Identity.new.this_object.class **Which option corresponds to the execution result? (Choose one.)** *A:* -``` +```ruby Identity Identity ``` *B:* -``` +```ruby Class Identity ``` *C:* -``` +```ruby Object Identity ``` *D:* -``` +```ruby Class Object ``` @@ -1286,7 +1289,7 @@ Object **Q38. Given the following code:** -``` +```ruby module Mixin def this_object self @@ -1327,7 +1330,7 @@ p Identity.new.this_object.class **Q40. Given the following code:** -``` +```ruby module Mixin def self.greet puts "Hello World!" @@ -1353,7 +1356,7 @@ end **Q41. Assume that the following code must have the stated execution result.** -``` +```ruby module Mixin def greet puts "Hello World!" @@ -1384,7 +1387,7 @@ Hello World! **Q42. Assume that the following code must have the stated execution result.** -``` +```ruby module Mixin def greet puts "Hello World!" @@ -1415,7 +1418,7 @@ Hello World! **Q43. Assume that the following code must have the stated execution result.** -``` +```ruby class BaseClass private @@ -1439,19 +1442,19 @@ Hello World! *A:* -``` +```ruby public :greet ``` *B:* -``` +```ruby protected :greet ``` *C:* -``` +```ruby def greet super end @@ -1459,7 +1462,7 @@ end *D:* -``` +```ruby alias_method :original_greet, :greet def greet @@ -1471,7 +1474,7 @@ end **Q44. Assume that the following code must have the stated execution result:** -``` +```ruby h = [1, 2, 3] case h __(1)__ [x, y] @@ -1498,7 +1501,7 @@ end **Q45. Assume that the following code must have the stated execution result:** -``` +```ruby class Alphabet include Enumerable @@ -1539,7 +1542,7 @@ p set.select { |e| e > "W" } **Q46. Given the following code:** -``` +```ruby class TShirt SIZES = [:xs, :s, :m, :l, :xl, :xxl] @@ -1569,7 +1572,7 @@ p medium >= large *A:* -``` +```ruby true false true @@ -1578,7 +1581,7 @@ true ``` *B:* -``` +```ruby false true true @@ -1587,7 +1590,7 @@ false ``` *C:* -``` +```ruby false false false @@ -1596,7 +1599,7 @@ true ``` *D:* -``` +```ruby false false false @@ -1611,7 +1614,7 @@ An exception is raised. **Q47. Given the following code:** -``` +```ruby require "date" date = Date.new(2000, 2, 24) @@ -1623,25 +1626,25 @@ puts(date >> 12) **Which option corresponds to the execution result? (Choose one.)** *A:* -``` +```ruby 2000-02-12 2000-03-07 ``` *B:* -``` +```ruby 2000-03-07 2000-02-12 ``` *C:* -``` +```ruby 1999-02-24 2001-02-24 ``` *D:* -``` +```ruby 1988-02-24 2012-02-24 ``` @@ -1650,7 +1653,7 @@ puts(date >> 12) **Q48. Assume that the following code must have the stated execution result:** -``` +```ruby require "time" t = Time.__(1)__("00000024021993", "%S%M%H%d%m%Y") @@ -1674,7 +1677,7 @@ puts t.iso8601 **Q49. Assume that the following code must have the stated execution result:** -``` +```ruby require "singleton" class Foo @@ -1703,7 +1706,7 @@ true **Q50. Assume that the following code must have the stated execution result:** -``` +```ruby require 'forwardable' class List diff --git a/silver.md b/silver.md index 975c407..076a625 100644 --- a/silver.md +++ b/silver.md @@ -32,7 +32,7 @@ **Q4. Which of the following can be inserted into `__(1)__` in order to generate the output below? (Choose two.)** -``` +```ruby $code = "CODE" __(1)__ @@ -49,7 +49,7 @@ i like writing CODE **Q5. Given the following:** -``` +```ruby num = 025 puts num ``` @@ -65,7 +65,7 @@ puts num **Q6. Given the following:** -``` +```ruby x = "Hello" y = x.empty? ? 1 : 2 p y @@ -82,7 +82,7 @@ p y **Q7. Given the following:** -``` +```ruby amount = 120 size = case amount @@ -106,7 +106,7 @@ p size **Q8. Given the following:** -``` +```ruby item = "apple" ["banana", "carrot", "daikon"].each do |item| @@ -125,7 +125,7 @@ A syntax error occurs An exception is raised *(c)* -``` +```ruby banana carrot daikon @@ -133,7 +133,7 @@ daikon ``` *(d)* -``` +```ruby banana carrot daikon @@ -144,7 +144,7 @@ apple **Q9. Given the following:** -``` +```ruby x = 0 4.times do |i| @@ -166,7 +166,7 @@ p x **Q10. Given the following:** -``` +```ruby s = "abcde" p s.each_char.map { |i| i * 2 @@ -185,7 +185,7 @@ p s.each_char.map { |i| **Q11. Given the following:** -``` +```ruby p "cocoa".chars.tally ``` @@ -200,7 +200,7 @@ p "cocoa".chars.tally **Q12. Which of the following can be inserted into `__(1)__` in order to generate the output below? (Choose one.)** -``` +```ruby puts "blah blah blah".__(1)__(/blah/, "yay") [Output] @@ -216,7 +216,7 @@ yay yay yay **Q13. Given the following:** -``` +```ruby s = "pear" if s.empty? @@ -239,7 +239,7 @@ end **Q14: Given the following:** -``` +```ruby ["foo: abc", "bar: 100"].each do |i| p i.slice(/[0-9]+/)&.to_i end @@ -248,19 +248,19 @@ end **Which is the correct result? (Choose one.)** *(a)* -``` +```ruby 0 100 ``` *(b)* -``` +```ruby nil 100 ``` *(c)* -``` +```ruby false 100 ``` @@ -275,7 +275,7 @@ An error occurs at run-time. **Q15: Given the following:** -``` +```ruby def foo(x: 1, y: 2, z: 3) p [x, y, z] end @@ -294,7 +294,7 @@ foo(y: 4, z: 5) **Q16: Which of the following can be inserted into `__(1)__` in order for the given code to generate the output below? (Choose one.)** -``` +```ruby def foo(x:, y:, z:) p [x, y, z] end @@ -323,7 +323,7 @@ __(1)__ **Q18.Given the following:** -``` +```ruby MSG = 42 MSG += 5 p MSG @@ -340,7 +340,7 @@ p MSG **Q19. Given the following:** -``` +```ruby MSG = "hello" MSG.upcase! p MSG @@ -367,7 +367,7 @@ p MSG **Q21. Given the following:** -``` +```ruby x = [1,2,3,4,5,6,7,8] y = x x.reject! { |e| e.even? } @@ -379,25 +379,25 @@ p y *(a)* -``` +```ruby [1, 3, 5, 7] [1, 2, 3, 4, 5, 6, 7, 8] ``` *(b)* -``` +```ruby [1, 2, 3, 4, 5, 6, 7, 8] [1, 2, 3, 4, 5, 6, 7, 8] ``` *(c)* -``` +```ruby [1, 3, 5, 7] [1, 3, 5, 7] ``` *(d)* -``` +```ruby [1, 3, 5, 7] [2, 4, 6, 8] ``` @@ -406,7 +406,7 @@ p y **Q22. Given the following:** -``` +```ruby a = [ 2, 4, 6, 8, 10 ] a.shift a.pop @@ -425,7 +425,7 @@ p a **Q23. Which of the following can be inserted into `__(1)__` in order for the given code to generate the output below? (Choose one.)** -``` +```ruby x = true x __(1)__ exit(1) puts("succeeded!") @@ -444,7 +444,7 @@ succeeded! **Q24. Given the following:** -``` +```ruby m = true m or n = true p n @@ -462,7 +462,7 @@ p n **Q25. Which of the following can be inserted into __(1)__ in order for the given code to generate the output below? (Choose two.)** -``` +```ruby x = [ 9, 7, 5, 3, 1 ] p __(1)__ @@ -479,7 +479,7 @@ p __(1)__ **Q26. Which of the following can be inserted into __(1)__ in order for the given code to generate the output below? (Choose two.)** -``` +```ruby ary = [ 1, 2, 3, 4, 5 ] p ary.__(1)__ { |i| i.odd? } @@ -497,7 +497,7 @@ p ary.__(1)__ { |i| i.odd? } **Q27. Given the following:** -``` +```ruby puts "42A7".to_i ``` @@ -512,7 +512,7 @@ puts "42A7".to_i **Q28. Which of the following methods will NOT show you if the element `:c` exists in the hash key or not? (Choose one.)** -``` +```ruby h = {a: 2, b: 4, c: 6, d: 8, e: 10} ``` @@ -526,7 +526,7 @@ h = {a: 2, b: 4, c: 6, d: 8, e: 10} **Q29. "Which of the following can be inserted into `__(1)__` in order for the given code to generate the output below? (Choose two.)** -``` +```ruby a = [120, 40, 20, 80, 160, 60, 180] a.__(1)__ p a @@ -546,7 +546,7 @@ p a **Q30. Which of the following can be inserted into `__(1)__` in order for the given code to generate the output below? (Choose one.)** -``` +```ruby p ["apple", "banana"] __(1)__ ["banana", "carrot"] [Output] @@ -563,7 +563,7 @@ p ["apple", "banana"] __(1)__ ["banana", "carrot"] **Q31. Given the following:** -``` +```ruby p %i(x1 x2 x3) ``` @@ -578,7 +578,7 @@ What is the correct result? (Choose one.) **Q32. Given the following:** -``` +```ruby class SomeError < StandardError; end class SomeOtherError < SomeError; end @@ -606,7 +606,7 @@ What is the correct result? (Choose one.) **Q33. Given the following:** -``` +```ruby begin ans = 100/0 puts ans @@ -660,7 +660,7 @@ Error: ZeroDivisionError **Q35. Given the following:** -``` +```ruby class Object def moo puts "MOO!" @@ -681,7 +681,7 @@ end **Q36. Which of the following can be inserted into `__(1)__` in order for the given code to generate the output below? (Choose one.)** -``` +```ruby class Shouter def __(1)__(message) @message = message @@ -708,7 +708,7 @@ HELLO, WORLD! **Q37. Which of the following can be inserted into `__(1)__` in order for the given code to generate the output below? (Choose one.)** -``` +```ruby class Shouter def initialize(message) @message = message @@ -735,7 +735,7 @@ HELLO, WORLD! **Q38. Given the following:** -``` +```ruby class Foo attr_reader :var def initialize @@ -766,7 +766,7 @@ puts bar.var **Q39. Which of the following can be inserted into `__(1)__` in order for the given code to generate the output below? (Choose two.)** -``` +```ruby puts "$foo$".__(1)__("$") [Output] @@ -782,7 +782,7 @@ foo$ **Q40. Which of the following can be inserted into `__(1)__` in order for the given code to generate the output below? (Choose one.)** -``` +```ruby r = "a".."e" p r.__(1)__ @@ -799,7 +799,7 @@ p r.__(1)__ **Q41. Given the following:** -``` +```ruby p [0,1,2,3,4,5].find {|x| x < 3} ``` @@ -814,7 +814,7 @@ p [0,1,2,3,4,5].find {|x| x < 3} **Q42. Which of the following can be inserted into `__(1)__` in order for the given code to generate the output below? (Choose two.)** -``` +```ruby p [1,16,8,4,2].__(1)__ [Output] @@ -831,7 +831,7 @@ p [1,16,8,4,2].__(1)__ **Q43. Which of the following can be inserted into __(1)__ in order for the given code to sort an array in descending order? (Choose one.)** -``` +```ruby ary = [2,4,8,1,16] p ary.__(1)__ @@ -848,7 +848,7 @@ p ary.__(1)__ **Q44. Given the following:** -``` +```ruby File.write("test", "hellorubyworld\n") File.open("test") do |file| file.seek(5) @@ -868,7 +868,7 @@ end **Q45. The code below was used to open a file omitting the second argument of the open method. In this case, which of the following is implicitly specified? (Choose one.)** -``` +```ruby file = open("sample.txt") ``` @@ -885,7 +885,7 @@ file = open("sample.txt") **In the case that the “test_two.txt” file already exist, this code should set first the file size to zero and then overwrites its content from the beginning. (Choose two.)** -``` +```ruby open("test_one.txt") {|source| open("test_two.txt", "__(1)__") {|dest| dest.write(source.read) @@ -913,7 +913,7 @@ open("test_one.txt") {|source| **Q48. Given the following:** -``` +```ruby p "hello ruby world"[6,4] ``` @@ -930,7 +930,7 @@ p "hello ruby world"[6,4] Given the following: -``` +```ruby str = "bat" str[1,1] = "o" p str @@ -947,7 +947,7 @@ Which is the correct output? (Choose one.) **Q50. Given the following:** -``` +```ruby puts 5 * "hi" ``` diff --git a/test.rb b/test.rb new file mode 100644 index 0000000..9dc8a71 --- /dev/null +++ b/test.rb @@ -0,0 +1,107 @@ +require "tty-markdown" +require "timeout" + +class Test + attr_reader :questions, :answers, :howto + class Exit < StandardError; end + + class EndOfTime < StandardError; end + DURATION = 60 * 90 + + def initialize(type: :silver) + case type + when :silver + @questions = File.read("silver.md").split(/^-------------.*\n/) + @answers = File.read("silver_answers.md").split(/^-------------.*\n/) + @answers.map! do |ans| + ans.slice(/^\*\*A\d+:.*/).scan(/\(([a-z])\)/).flatten(1).map { |i| + i.ord - "a".ord + } + end + when :gold + @questions = File.read("gold.md").split(/^-------------.*\n/) + @answers = File.read("gold_answers.md").split(/^-------------.*\n/)[0..49] + @answers.map! do |ans| + ans.slice(/^\*\*A\d+:.*/).scan(/\(([A-Z])\)/).flatten(1).map { |i| + i.ord - ?A.ord + } + end + else + raise StandardError, "test not found" + end + @howto = File.read("test_readme.md") + @last_printed_lines_count = 0 + end + + def start + @result = [] + clean_the_screen + print_howto + clean_the_screen + Timeout.timeout(DURATION, EndOfTime) do + questions[0..49].each_with_index do |question, inx| + puts question + @result[inx] = validate_answer(get_user_answer, inx) + clean_the_screen + end + clean_the_screen + calc_and_print_result(@result) + end + rescue Exit + clean_the_screen + calc_and_print_result(@result) + rescue EndOfTime + clean_the_screen + puts "# Time for exam has runned out" + calc_and_print_result(@result) + end + + private + + def calc_and_print_result(result) + correct_answers_count = result.count { |e| 1 if e } + correct_answers_percecnt = correct_answers_count.to_f / questions.length * 100 + puts "Your result is **#{correct_answers_count} out of #{questions.length}**" + puts "Percent of **correct answers is #{correct_answers_percecnt.round(2)}%**" + if correct_answers_count >= 75.0 + puts "## You've passed the test exam" + else + puts "## You've failed the test exam." + puts "### Don't give up! You can do it! (=" + end + end + + def validate_answer(user_answers, inx) + user_answers.sort == answers[inx].sort + end + + def print_howto + puts howto.to_s + raise Exit unless gets.chomp.match?(/\Ay\z/) + end + + def get_user_answer + puts "**Write you answers separated by commas:**" + user_answer = gets + raise Exit if user_answer.match? /exit/i + + user_answer.strip.split(",").map { |e| e.ord - "a".ord } + end + + def clean_the_screen + print "\033[2J\033[3J\033[1;1H" + end + + def puts(string) + Kernel.puts TTY::Markdown.parse(string).to_s + end +end + +test_type = ARGV[0]&.to_sym || :silver +ARGV.clear +if test_type.match?("help") + puts TTY::Markdown.parse(File.read("test_help.md")).to_s +else + test = Test.new(type: test_type) + test.start +end diff --git a/test_help.md b/test_help.md new file mode 100644 index 0000000..e25f7d5 --- /dev/null +++ b/test_help.md @@ -0,0 +1,9 @@ +Usage: `ruby test.rb [testname]` + +**testname**: + pass the name of the test that you want to run + could be a `gold` or `silver` or `help` + the default value is `silver` + `help` would print this message + Example: + `ruby test.rb gold` diff --git a/test_readme.md b/test_readme.md new file mode 100644 index 0000000..06b903a --- /dev/null +++ b/test_readme.md @@ -0,0 +1,39 @@ +# Welcome to ruby exam preparation + +## ATTENTION +If you keep the test it would flush your terminal buffer. +Enter `y` in case you agree with that. + +### Notice +Recomend **open this test in new terminal** +for simply navigation over printed questions + +### Duration +The test duration is **90 minutes**. +If you exceed the time, test would be interrupted +and you will get the result + +### How to answer +When you'll answer on questions please + use the same register of bullet points as used in questions. + +#### Example: + +**Q1 Some text of question** + +- (A) +```ruby + puts "answer" + # here might be the code or smth +``` +- (B) answer +- (C) answer +- (D) answer + +Write you answers separated by commas: + +A,B,C + +---------------------------------------- + +## To start the test enter `y` From 72b2f03d0f4f534f58367686870747ea0c188f13 Mon Sep 17 00:00:00 2001 From: Max Bezlegkiy Date: Sun, 19 Feb 2023 12:31:39 +0300 Subject: [PATCH 2/9] [feature/test-in-terminal] fix user answer transform --- test.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test.rb b/test.rb index 9dc8a71..a193437 100644 --- a/test.rb +++ b/test.rb @@ -18,6 +18,7 @@ def initialize(type: :silver) i.ord - "a".ord } end + @test_symbol = "a" when :gold @questions = File.read("gold.md").split(/^-------------.*\n/) @answers = File.read("gold_answers.md").split(/^-------------.*\n/)[0..49] @@ -26,6 +27,7 @@ def initialize(type: :silver) i.ord - ?A.ord } end + @test_symbol = "A" else raise StandardError, "test not found" end @@ -85,7 +87,7 @@ def get_user_answer user_answer = gets raise Exit if user_answer.match? /exit/i - user_answer.strip.split(",").map { |e| e.ord - "a".ord } + user_answer.strip.split(",").map { |e| e.ord - @test_symbol.ord } end def clean_the_screen From f2da9796adbff7cecf9d0dccd8f8bc9c9005a4e6 Mon Sep 17 00:00:00 2001 From: Max Bezlegkiy Date: Sun, 19 Feb 2023 12:32:54 +0300 Subject: [PATCH 3/9] [feature/test-in-terminal] standard fix --- test.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/test.rb b/test.rb index a193437..60e7607 100644 --- a/test.rb +++ b/test.rb @@ -3,10 +3,12 @@ class Test attr_reader :questions, :answers, :howto + + DURATION = 60 * 90 + class Exit < StandardError; end class EndOfTime < StandardError; end - DURATION = 60 * 90 def initialize(type: :silver) case type @@ -24,7 +26,7 @@ def initialize(type: :silver) @answers = File.read("gold_answers.md").split(/^-------------.*\n/)[0..49] @answers.map! do |ans| ans.slice(/^\*\*A\d+:.*/).scan(/\(([A-Z])\)/).flatten(1).map { |i| - i.ord - ?A.ord + i.ord - "A".ord } end @test_symbol = "A" @@ -85,7 +87,7 @@ def print_howto def get_user_answer puts "**Write you answers separated by commas:**" user_answer = gets - raise Exit if user_answer.match? /exit/i + raise Exit if user_answer.match?(/exit/i) user_answer.strip.split(",").map { |e| e.ord - @test_symbol.ord } end From 3fe2ecae595753b8034da7f1a5025a18ad874af7 Mon Sep 17 00:00:00 2001 From: Max Bezlegkiy Date: Sun, 19 Feb 2023 20:21:01 +0300 Subject: [PATCH 4/9] [feature/test-in-terminal] improve interaction through the exam. Add commands to go to the specific, previous or next questions. --- README.md | 6 +++ gold.md | 30 +++++++++++++ silver.md | 9 ++++ test.rb | 119 ++++++++++++++++++++++++++++++++++++++++--------- test_readme.md | 2 - 5 files changed, 143 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 1bcab05..7fa40fa 100644 --- a/README.md +++ b/README.md @@ -4,3 +4,9 @@ Sample questions for [Ruby Association Certified Ruby Programmer Examination ver * [Sample Questions for Silver](silver.md) ([Japanese](silver_ja.md)) * [Sample Questions for Gold](gold.md) ([Japanese](gold_ja.md)) + +# Run sample exam + +* Silver `ruby test.rb` or `ruby test.rb silver` +* Gold `ruby test.rb gold` + diff --git a/gold.md b/gold.md index 82a3a6d..8cb837d 100644 --- a/gold.md +++ b/gold.md @@ -25,6 +25,7 @@ p stack.pop **Which option can be inserted into `__(1)__`? (Choose two.)** *A:* + ```ruby [:push, :pop].each do |name| define_method(name) do |*args| @@ -34,6 +35,7 @@ end ``` *B:* + ```ruby for name in [:push, :pop] define_method(name) do |*args| @@ -43,6 +45,7 @@ end ``` *C:* + ```ruby [:push, :pop].each do |name| instance_eval(<<-EOF) @@ -54,6 +57,7 @@ end ``` *D:* + ```ruby [:push, :pop].each do |name| class_eval(<<-EOF) @@ -495,6 +499,7 @@ print reader_method("foo") **Which option corresponds to the execution result? (Choose one.)** *A:* + ```ruby def foo @foo @@ -502,6 +507,7 @@ print reader_method("foo") ``` *B:* + ```ruby def foo @foo @@ -509,6 +515,7 @@ print reader_method("foo") ``` *C:* + ```ruby def foo @foo @@ -716,6 +723,7 @@ p c ``` *A:* + ```ruby def fx(*args) p(args) @@ -724,6 +732,7 @@ fx(*["apple", "banana", "carrot"]) ``` *B:* + ```ruby def fx(*args) p(args) @@ -732,6 +741,7 @@ fx(["apple", "banana", "carrot"]) ``` *C:* + ```ruby def fx(*args) p(args) @@ -740,6 +750,7 @@ fx("apple", "banana", "carrot") ``` *D:* + ```ruby def fx(*args) p(*args) @@ -784,12 +795,14 @@ fx(b: "banana") **Which option corresponds to the execution result?** *A:* + ```ruby nil apple ``` *B:* + ```ruby nil banana @@ -1014,26 +1027,31 @@ end **Which option corresponds to the execution result? (Choose one.)** *A:* + ```ruby Bare rescue ``` *B:* + ```ruby StandardError rescue ``` *C:* + ```ruby AnError rescue ``` *D:* + ```ruby Exception rescue ``` *E:* + ```ruby AnError rescue Exception rescue @@ -1062,26 +1080,31 @@ end **Which option corresponds to the execution result? (Choose one.)** *A:* + ```ruby Bare rescue ``` *B:* + ```ruby StandardError rescue ``` *C:* + ```ruby AnError rescue ``` *D:* + ```ruby Exception rescue ``` *E:* + ```ruby Exception rescue AnError rescue @@ -1223,18 +1246,21 @@ true ``` *B:* + ```ruby true false ``` *C:* + ```ruby false true ``` *D:* + ```ruby false false @@ -1262,24 +1288,28 @@ p Identity.new.this_object.class **Which option corresponds to the execution result? (Choose one.)** *A:* + ```ruby Identity Identity ``` *B:* + ```ruby Class Identity ``` *C:* + ```ruby Object Identity ``` *D:* + ```ruby Class Object diff --git a/silver.md b/silver.md index 076a625..538f744 100644 --- a/silver.md +++ b/silver.md @@ -125,6 +125,7 @@ A syntax error occurs An exception is raised *(c)* + ```ruby banana carrot @@ -133,6 +134,7 @@ daikon ``` *(d)* + ```ruby banana carrot @@ -248,18 +250,21 @@ end **Which is the correct result? (Choose one.)** *(a)* + ```ruby 0 100 ``` *(b)* + ```ruby nil 100 ``` *(c)* + ```ruby false 100 @@ -379,24 +384,28 @@ p y *(a)* + ```ruby [1, 3, 5, 7] [1, 2, 3, 4, 5, 6, 7, 8] ``` *(b)* + ```ruby [1, 2, 3, 4, 5, 6, 7, 8] [1, 2, 3, 4, 5, 6, 7, 8] ``` *(c)* + ```ruby [1, 3, 5, 7] [1, 3, 5, 7] ``` *(d)* + ```ruby [1, 3, 5, 7] [2, 4, 6, 8] diff --git a/test.rb b/test.rb index 60e7607..3400033 100644 --- a/test.rb +++ b/test.rb @@ -1,5 +1,6 @@ require "tty-markdown" require "timeout" +require 'byebug' class Test attr_reader :questions, :answers, :howto @@ -38,18 +39,12 @@ def initialize(type: :silver) end def start - @result = [] clean_the_screen print_howto clean_the_screen Timeout.timeout(DURATION, EndOfTime) do - questions[0..49].each_with_index do |question, inx| - puts question - @result[inx] = validate_answer(get_user_answer, inx) - clean_the_screen - end - clean_the_screen - calc_and_print_result(@result) + @exam_started = true + exam end rescue Exit clean_the_screen @@ -62,6 +57,94 @@ def start private + def exam + @curr_inx = -1 + @result = Array.new(50) + @user_answers = Array.new(50) + loop do + next_question + user_input = get_user_input + case user_input + when :repeat + @curr_inx -= 1 + next + when :next + next + when :prev + @curr_inx -= 2 + next + when :howto + @curr_inx -= 1 + clean_the_screen + print_howto + clean_the_screen + else + @user_answers[@curr_inx] = user_input + @result[@curr_inx] = validate_answer(user_input) + end + end + end + + def next_question + @curr_inx += 1 if @curr_inx < 49 + clean_the_screen + question = questions[@curr_inx] + puts question + if @user_answers[@curr_inx] + prev_answ = @user_answers[@curr_inx].map { |i| (@test_symbol.ord + i).chr }.join(",") + puts "## Your previously entered answer is **(#{prev_answ})**" + end + end + + def validate_answer(user_answers) + user_answers.sort == answers[@curr_inx].sort + end + + def get_user_input + puts "**Write you answers separated by commas:**" + user_answer = gets.chomp + input = case user_answer + in /exit/i + puts "**Are you sure want finish exam? (Enter `yes` to exit)**" + raise Exit if gets.match?(/yes/i) + + :repeat + in /\A(n|next|continue)\z/i + :next + in /\A(p|previous|prev\z)/i + :prev + in /\Agoto (\d+)\z/ + return :repeat unless $1.to_i.between?(1,50) + + @curr_inx = $1.to_i - 1 + :repeat + in /\A(stop|finish|end\z)/i + puts "**Are you sure want finish exam? (Enter `yes` to exit)**" + raise Exit if gets.match?(/yes/i) + + :repeat + in /\A(help|h)\z/i + puts <<~MD + # Available commands + - `howto` - print the text that was printend at start (don't restart the exam) + - `n` or `next` or `continue` - to go to the next question in order + - `p` or `prev` or `previous` - to go to the previous question in order + - `stop` or `finish` or `end` - to finish the exam and print your results + - `h` or `help` - to print this text + + **Press enter to continue** + MD + gets + + :repeat + in /\Ahowto\z/i + :howto + else + user_answer.strip.split(",").map { |e| e.ord - @test_symbol.ord } + end + input + end + def calc_and_print_result(result) correct_answers_count = result.count { |e| 1 if e } correct_answers_percecnt = correct_answers_count.to_f / questions.length * 100 @@ -75,21 +158,15 @@ def calc_and_print_result(result) end end - def validate_answer(user_answers, inx) - user_answers.sort == answers[inx].sort - end - def print_howto puts howto.to_s - raise Exit unless gets.chomp.match?(/\Ay\z/) - end - - def get_user_answer - puts "**Write you answers separated by commas:**" - user_answer = gets - raise Exit if user_answer.match?(/exit/i) - - user_answer.strip.split(",").map { |e| e.ord - @test_symbol.ord } + unless @exam_started + puts "## To start the test enter `y`" + raise Exit unless gets.chomp.match?(/\Ay\z/) + else + puts "**Press enter to continue**" + gets + end end def clean_the_screen diff --git a/test_readme.md b/test_readme.md index 06b903a..d8064cb 100644 --- a/test_readme.md +++ b/test_readme.md @@ -35,5 +35,3 @@ Write you answers separated by commas: A,B,C ---------------------------------------- - -## To start the test enter `y` From a17e23b8e28bc09019590006da891fb4fc5e8d8d Mon Sep 17 00:00:00 2001 From: Max Bezlegkiy Date: Sun, 19 Feb 2023 20:56:26 +0300 Subject: [PATCH 5/9] [feature/test-in-terminal] add language support. --- test.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/test.rb b/test.rb index 3400033..c497652 100644 --- a/test.rb +++ b/test.rb @@ -11,11 +11,11 @@ class Exit < StandardError; end class EndOfTime < StandardError; end - def initialize(type: :silver) + def initialize(type: :silver, language: :en) case type when :silver - @questions = File.read("silver.md").split(/^-------------.*\n/) - @answers = File.read("silver_answers.md").split(/^-------------.*\n/) + @questions = File.read("silver#{language == :en ? "" : "_ja" }.md").split(/^-------------.*\n/) + @answers = File.read("silver_answers#{language == :en ? "" : "_ja" }.md").split(/^-------------.*\n/) @answers.map! do |ans| ans.slice(/^\*\*A\d+:.*/).scan(/\(([a-z])\)/).flatten(1).map { |i| i.ord - "a".ord @@ -23,8 +23,8 @@ def initialize(type: :silver) end @test_symbol = "a" when :gold - @questions = File.read("gold.md").split(/^-------------.*\n/) - @answers = File.read("gold_answers.md").split(/^-------------.*\n/)[0..49] + @questions = File.read("gold#{language == :en ? "" : "_ja" }.md").split(/^-------------.*\n/) + @answers = File.read("gold_answers#{language == :en ? "" : "_ja" }.md").split(/^-------------.*\n/)[0..49] @answers.map! do |ans| ans.slice(/^\*\*A\d+:.*/).scan(/\(([A-Z])\)/).flatten(1).map { |i| i.ord - "A".ord @@ -179,10 +179,12 @@ def puts(string) end test_type = ARGV[0]&.to_sym || :silver +test_language = ARGV[1]&.to_sym == "ja" && :ja || :en + ARGV.clear if test_type.match?("help") puts TTY::Markdown.parse(File.read("test_help.md")).to_s else - test = Test.new(type: test_type) + test = Test.new(type: test_type, language: test_language) test.start end From 028049347886cd3eb83ac554578a1e735a11fa82 Mon Sep 17 00:00:00 2001 From: Max Bezlegkiy Date: Sun, 19 Feb 2023 20:58:48 +0300 Subject: [PATCH 6/9] [feature/test-in-terminal] cop fix --- test.rb | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/test.rb b/test.rb index c497652..957ca8a 100644 --- a/test.rb +++ b/test.rb @@ -1,6 +1,6 @@ require "tty-markdown" require "timeout" -require 'byebug' +require "byebug" class Test attr_reader :questions, :answers, :howto @@ -14,8 +14,8 @@ class EndOfTime < StandardError; end def initialize(type: :silver, language: :en) case type when :silver - @questions = File.read("silver#{language == :en ? "" : "_ja" }.md").split(/^-------------.*\n/) - @answers = File.read("silver_answers#{language == :en ? "" : "_ja" }.md").split(/^-------------.*\n/) + @questions = File.read("silver#{(language == :en) ? "" : "_ja"}.md").split(/^-------------.*\n/) + @answers = File.read("silver_answers#{(language == :en) ? "" : "_ja"}.md").split(/^-------------.*\n/) @answers.map! do |ans| ans.slice(/^\*\*A\d+:.*/).scan(/\(([a-z])\)/).flatten(1).map { |i| i.ord - "a".ord @@ -23,8 +23,8 @@ def initialize(type: :silver, language: :en) end @test_symbol = "a" when :gold - @questions = File.read("gold#{language == :en ? "" : "_ja" }.md").split(/^-------------.*\n/) - @answers = File.read("gold_answers#{language == :en ? "" : "_ja" }.md").split(/^-------------.*\n/)[0..49] + @questions = File.read("gold#{(language == :en) ? "" : "_ja"}.md").split(/^-------------.*\n/) + @answers = File.read("gold_answers#{(language == :en) ? "" : "_ja"}.md").split(/^-------------.*\n/)[0..49] @answers.map! do |ans| ans.slice(/^\*\*A\d+:.*/).scan(/\(([A-Z])\)/).flatten(1).map { |i| i.ord - "A".ord @@ -103,7 +103,7 @@ def validate_answer(user_answers) def get_user_input puts "**Write you answers separated by commas:**" user_answer = gets.chomp - input = case user_answer + case user_answer in /exit/i puts "**Are you sure want finish exam? (Enter `yes` to exit)**" raise Exit if gets.match?(/yes/i) @@ -114,8 +114,8 @@ def get_user_input in /\A(p|previous|prev\z)/i :prev in /\Agoto (\d+)\z/ - return :repeat unless $1.to_i.between?(1,50) - + return :repeat unless $1.to_i.between?(1, 50) + @curr_inx = $1.to_i - 1 :repeat in /\A(stop|finish|end\z)/i @@ -139,10 +139,9 @@ def get_user_input :repeat in /\Ahowto\z/i :howto - else + else user_answer.strip.split(",").map { |e| e.ord - @test_symbol.ord } end - input end def calc_and_print_result(result) @@ -160,12 +159,12 @@ def calc_and_print_result(result) def print_howto puts howto.to_s - unless @exam_started - puts "## To start the test enter `y`" - raise Exit unless gets.chomp.match?(/\Ay\z/) - else + if @exam_started puts "**Press enter to continue**" gets + else + puts "## To start the test enter `y`" + raise Exit unless gets.chomp.match?(/\Ay\z/) end end From 11ce7288ba870e3ac709df495885bd8d2f9a176f Mon Sep 17 00:00:00 2001 From: Max Bezlegkiy Date: Sun, 19 Feb 2023 21:05:13 +0300 Subject: [PATCH 7/9] [feature/test-in-terminal] fix lang support --- test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test.rb b/test.rb index 957ca8a..09e00e0 100644 --- a/test.rb +++ b/test.rb @@ -178,7 +178,7 @@ def puts(string) end test_type = ARGV[0]&.to_sym || :silver -test_language = ARGV[1]&.to_sym == "ja" && :ja || :en +test_language = ARGV[1]&.to_sym || :en ARGV.clear if test_type.match?("help") From 2f582777b02c753db4f1149b27af0d863acfa4ad Mon Sep 17 00:00:00 2001 From: Max Bezlegkiy Date: Sun, 19 Feb 2023 21:33:00 +0300 Subject: [PATCH 8/9] [feature/test-in-terminal] fix jp locale --- gold_ja.md | 254 +++++++++++++++++++++++++------------------ silver_answers_ja.md | 2 +- silver_ja.md | 88 ++++++++------- test.rb | 7 +- 4 files changed, 202 insertions(+), 149 deletions(-) diff --git a/gold_ja.md b/gold_ja.md index 6364923..9c80e61 100644 --- a/gold_ja.md +++ b/gold_ja.md @@ -4,7 +4,7 @@ **Q1. 以下の実行結果を出力するコードがあります。** -``` +```ruby class Stack def initialize @contents = [] @@ -25,7 +25,8 @@ p stack.pop **__(1)__に入る適切な記述を選択してください。(2つ選択)** *A:* -``` + +```ruby [:push, :pop].each do |name| define_method(name) do |*args| @contents.send(name, *args) @@ -34,7 +35,8 @@ end ``` *B:* -``` + +```ruby for name in [:push, :pop] define_method(name) do |*args| @contents.send(name, *args) @@ -43,7 +45,8 @@ end ``` *C:* -``` + +```ruby [:push, :pop].each do |name| instance_eval(<<-EOF) def #{name}(*args) @@ -54,7 +57,8 @@ end ``` *D:* -``` + +```ruby [:push, :pop].each do |name| class_eval(<<-EOF) def #{name}(*args) @@ -68,7 +72,7 @@ end **Q2. 以下のコードがあります。** -``` +```ruby module I end @@ -97,7 +101,7 @@ p C.ancestors **Q3. 以下の実行結果を出力するコードがあります。** -``` +```ruby x = __(1)__ p x + 1 @@ -119,7 +123,7 @@ p x + 1 **Q4. 以下の実行結果を出力するコードがあります。** -``` +```ruby p ("aaaaaa".."zzzzzz").lazy.select { |e| e.end_with?("f") }.__(1)__ [実行結果] @@ -140,7 +144,7 @@ p ("aaaaaa".."zzzzzz").lazy.select { |e| e.end_with?("f") }.__(1)__ **Q5. 以下の実行結果を出力するコードがあります。** -``` +```ruby def round(n, __(1)__) n.round(__(1)__) end @@ -165,7 +169,7 @@ p round(2.5, half: :even) **Q6. 以下のコードがあります。** -``` +```ruby class A def foo self.bar @@ -188,12 +192,14 @@ puts A.new.foo **実行結果として正しいものを選択してください。(1つ選択)** *A:* -``` + +```ruby baz ``` *B:* -``` + +```ruby quux ``` @@ -207,7 +213,7 @@ quux **Q7. 以下の実行結果を出力するコードがあります。** -``` +```ruby class Greeter class << self def hello @@ -236,7 +242,7 @@ Hello there! **Q8: 以下の実行結果を出力するコードがあります。** -``` +```ruby __(1)__ p multiply_by(4) { 2 + 3 } @@ -249,7 +255,7 @@ p multiply_by(4) { 2 + 3 } *A:* -``` +```ruby def multiply_by(n, &block) n * block.call end @@ -257,7 +263,7 @@ end *B:* -``` +```ruby def multiply_by(n, &block) n * block end @@ -265,7 +271,7 @@ end *C:* -``` +```ruby def multiply_by(n) n * yield end @@ -273,7 +279,7 @@ end *D:* -``` +```ruby def multiply_by(n) n * yield.call end @@ -283,7 +289,7 @@ end **Q9: 以下の実行結果を出力するコードがあります。** -``` +```ruby __(1)__ p sum { |e| e << 1 << 5 << 7 } @@ -296,7 +302,7 @@ p sum { |e| e << 1 << 5 << 7 } *A:* -``` +```ruby def sum(&block) array = [] @@ -307,7 +313,8 @@ end ``` *B:* -``` + +```ruby def sum(&block) array = [] @@ -318,7 +325,8 @@ end ``` *C:* -``` + +```ruby def sum array = [] @@ -329,7 +337,8 @@ end ``` *D:* -``` + +```ruby def sum array = [] @@ -343,7 +352,7 @@ end **Q10. 以下のコードがあります。** -``` +```ruby class A @@x = 1 end @@ -378,7 +387,7 @@ p B.x **Q11. 以下の実行結果を出力するコードがあります。** -``` +```ruby words = ["apple", "banana", "cabbage"] pop = Proc.new { words.pop } 3.times{ puts __(1)__ } @@ -405,7 +414,7 @@ apple **Q12. 以下の実行結果を出力するコードがあります。** -``` +```ruby words = ["apple", "banana", "cabbage"] pop = __(1)__{ words.pop } 3.times{ puts pop.call } @@ -430,7 +439,7 @@ apple **Q13. 以下の実行結果を出力するコードがあります。** -``` +```ruby add = __(1)__ puts add.call("hello") @@ -452,7 +461,7 @@ HELLO **Q14. 以下の実行結果を出力するコードがあります。** -``` +```ruby __(1)__ x + y end @@ -477,7 +486,7 @@ p add(1, 2) **Q15. 以下のコードがあります。** -``` +```ruby def reader_method(s) <<~EOF def #{s} @@ -492,21 +501,24 @@ print reader_method("foo") **実行結果として正しいものを選択してください。(1つ選択)** *A:* -``` + +```ruby def foo @foo end ``` *B:* -``` + +```ruby def foo @foo end ``` *C:* -``` + +```ruby def foo @foo end @@ -519,7 +531,7 @@ end **Q16. 以下の実行結果を出力するコードがあります。** -``` +```ruby obj = Object.new def obj.hello @@ -548,7 +560,7 @@ Hi! **Q17. 以下の実行結果を出力するコードがあります。** -``` +```ruby class ShoppingList def initialize @items = [] @@ -591,7 +603,7 @@ puts list **Q18. 以下の実行結果を出力するコードがあります。** -``` +```ruby class ShoppingList def initialize @items = [] @@ -633,7 +645,7 @@ ShoppingList: **Q19. 以下の実行結果を出力するコードがあります。** -``` +```ruby p __(1)__.flat_map {|z| (1..z).flat_map {|x| (x..z).select {|y| @@ -662,7 +674,7 @@ p __(1)__.flat_map {|z| **Q20. 以下の実行結果を出力するコードがあります。** -``` +```ruby ary = ["foo", "bar", nil, "baz"] p ary.__(1)__ { |i| @@ -687,7 +699,7 @@ p ary.__(1)__ { |i| **Q21. 以下のコードがあります。** -``` +```ruby a, b, *c = ["apple", "banana", "carrot", "daikon"] p c @@ -707,13 +719,14 @@ p c **Q22: 以下の実行結果を出力するコードを選択してください。(1つ選択)** -``` +```ruby [実行結果] [["apple", "banana", "carrot"]] ``` *A:* -``` + +```ruby def fx(*args) p(args) end @@ -721,7 +734,8 @@ fx(*["apple", "banana", "carrot"]) ``` *B:* -``` + +```ruby def fx(*args) p(args) end @@ -729,7 +743,8 @@ fx(["apple", "banana", "carrot"]) ``` *C:* -``` + +```ruby def fx(*args) p(args) end @@ -737,7 +752,8 @@ fx("apple", "banana", "carrot") ``` *D:* -``` + +```ruby def fx(*args) p(*args) end @@ -748,7 +764,7 @@ fx(["apple", "banana", "carrot"]) **Q23. 以下の実行結果を出力するコードがあります。** -``` +```ruby p ["foo", "bar", "baz"].map { __(1)__.upcase } [実行結果] @@ -769,7 +785,7 @@ p ["foo", "bar", "baz"].map { __(1)__.upcase } **Q24. 以下のコードがあります。** -``` +```ruby def fx(a:, b: "apple") p a p b @@ -781,13 +797,15 @@ fx(b: "banana") **実行結果として正しいものを選択してください。** *A:* -``` + +```ruby nil apple ``` *B:* -``` + +```ruby nil banana ``` @@ -802,7 +820,7 @@ banana **Q25. 以下の実行結果を出力するコードがあります。** -``` +```ruby def add(x:, y:, **params) z = x + y @@ -831,7 +849,7 @@ __(1)__ **Q26: 以下の実行結果を出力するコードがあります。** -``` +```ruby class Speaker @message = "Hello!" @@ -864,7 +882,7 @@ Hello! **Q27. 以下のコードがあります。** -``` +```ruby class Speaker @message = "Hello!" @@ -892,7 +910,7 @@ end **Q28. 以下の実行結果を出力するコードがあります。** -``` +```ruby def x puts "x" end @@ -935,7 +953,7 @@ done! **Q29. 以下の実行結果を出力するコードがあります。** -``` +```ruby letters = catch(:done) do ("a".."z").each do |a| ("a".."z").each do |b| @@ -967,7 +985,7 @@ abc **Q30. 以下の実行結果を出力するコードがあります。** -``` +```ruby begin __(1)__ rescue @@ -992,7 +1010,7 @@ OK **Q31. 以下のコードがあります。** -``` +```ruby AnError = Class.new(Exception) begin @@ -1011,27 +1029,32 @@ end **実行結果として正しいものを選択してください。(1つ選択)** *A:* -``` + +```ruby Bare rescue ``` *B:* -``` + +```ruby StandardError rescue ``` *C:* -``` + +```ruby AnError rescue ``` *D:* -``` + +```ruby Exception rescue ``` *E:* -``` + +```ruby AnError rescue Exception rescue ``` @@ -1040,7 +1063,7 @@ Exception rescue **Q32. 以下のコードがあります。** -``` +```ruby AnError = Class.new(Exception) begin @@ -1059,27 +1082,32 @@ end **実行結果として正しいものを選択してください。(1つ選択)** *A:* -``` + +```ruby Bare rescue ``` *B:* -``` + +```ruby StandardError rescue ``` *C:* -``` + +```ruby AnError rescue ``` *D:* -``` + +```ruby Exception rescue ``` *E:* -``` + +```ruby Exception rescue AnError rescue ``` @@ -1088,7 +1116,7 @@ AnError rescue **Q33. 以下のコードがあります。** -``` +```ruby begin "hello".world rescue => ex @@ -1112,7 +1140,7 @@ end **Q34. 以下のコードがあります。** -``` +```ruby CustomError = Class.new(StandardError) def boom @@ -1144,7 +1172,7 @@ end **Q35. 以下のコードがあります。** -``` +```ruby def greeting "hello" ensure @@ -1160,27 +1188,27 @@ puts greeting *A:* -``` +```ruby hello ``` *B:* -``` +```ruby hi ``` *C:* -``` +```ruby Ensure called! hello ``` *D:* -``` +```ruby Ensure called! hi ``` @@ -1189,7 +1217,7 @@ hi **Q36. 以下のコードがあります。** -``` +```ruby class Identity def self.this_object self @@ -1214,25 +1242,28 @@ p c == d *A:* -``` +```ruby true true ``` *B:* -``` + +```ruby true false ``` *C:* -``` + +```ruby false true ``` *D:* -``` + +```ruby false false ``` @@ -1241,7 +1272,7 @@ false **Q37. 以下のコードがあります。** -``` +```ruby class Identity def self.this_object self.class @@ -1259,25 +1290,29 @@ p Identity.new.this_object.class **実行結果として正しいものを選択してください。(1つ選択)** *A:* -``` + +```ruby Identity Identity ``` *B:* -``` + +```ruby Class Identity ``` *C:* -``` + +```ruby Object Identity ``` *D:* -``` + +```ruby Class Object ``` @@ -1286,7 +1321,7 @@ Object **Q38. 以下のコードがあります。** -``` +```ruby module Mixin def this_object self @@ -1327,7 +1362,7 @@ p Identity.new.this_object.class **Q40. 以下のコードがあります。** -``` +```ruby module Mixin def self.greet puts "Hello World!" @@ -1353,7 +1388,7 @@ end **Q41. 以下の実行結果を出力するコードがあります。** -``` +```ruby module Mixin def greet puts "Hello World!" @@ -1384,7 +1419,7 @@ Hello World! **Q42. 以下の実行結果を出力するコードがあります。** -``` +```ruby module Mixin def greet puts "Hello World!" @@ -1415,7 +1450,7 @@ Hello World! **Q43. 以下の実行結果を出力するコードがあります。** -``` +```ruby class BaseClass private @@ -1439,19 +1474,19 @@ Hello World! *A:* -``` +```ruby public :greet ``` *B:* -``` +```ruby protected :greet ``` *C:* -``` +```ruby def greet super end @@ -1459,7 +1494,7 @@ end *D:* -``` +```ruby alias_method :original_greet, :greet def greet @@ -1471,7 +1506,7 @@ end **Q44. 以下の実行結果を出力するコードがあります。** -``` +```ruby h = [1, 2, 3] case h __(1)__ [x, y] @@ -1498,7 +1533,7 @@ end **Q45. 以下の実行結果を出力するコードがあります。** -``` +```ruby class Alphabet include Enumerable @@ -1539,7 +1574,7 @@ p set.select { |e| e > "W" } **Q46. 以下のコードがあります。** -``` +```ruby class TShirt SIZES = [:xs, :s, :m, :l, :xl, :xxl] @@ -1569,7 +1604,7 @@ p medium >= large *A:* -``` +```ruby true false true @@ -1578,7 +1613,8 @@ true ``` *B:* -``` + +```ruby false true true @@ -1587,7 +1623,8 @@ false ``` *C:* -``` + +```ruby false false false @@ -1596,7 +1633,8 @@ true ``` *D:* -``` + +```ruby false false false @@ -1611,7 +1649,7 @@ false **Q47. 以下のコードがあります。** -``` +```ruby require "date" date = Date.new(2000, 2, 24) @@ -1623,25 +1661,29 @@ puts(date >> 12) **実行結果として正しいものを選択してください。(1つ選択)** *A:* -``` + +```ruby 2000-02-12 2000-03-07 ``` *B:* -``` + +```ruby 2000-03-07 2000-02-12 ``` *C:* -``` + +```ruby 1999-02-24 2001-02-24 ``` *D:* -``` + +```ruby 1988-02-24 2012-02-24 ``` @@ -1650,7 +1692,7 @@ puts(date >> 12) **Q48. 以下の実行結果を出力するコードがあります。** -``` +```ruby require "time" t = Time.__(1)__("00000024021993", "%S%M%H%d%m%Y") @@ -1674,7 +1716,7 @@ puts t.iso8601 **Q49. 以下の実行結果を出力するコードがあります。** -``` +```ruby require "singleton" class Foo @@ -1703,7 +1745,7 @@ true **Q50. 以下の実行結果を出力するコードがあります。** -``` +```ruby require 'forwardable' class List diff --git a/silver_answers_ja.md b/silver_answers_ja.md index 500378e..06cd549 100644 --- a/silver_answers_ja.md +++ b/silver_answers_ja.md @@ -247,7 +247,7 @@ Another approach is to use a range, which generates a subarray based the index v --------------------------------------------------------------------------- -**A28. (b)** +**A28: (b)** `has_key?`、`include?`、`key?`、`member?`はすべて同じメソッドの別名で、与えられたキーがハッシュに存在する場合`true`を、存在しない場合`false`を返します。 diff --git a/silver_ja.md b/silver_ja.md index 69c4ad7..be8fce1 100644 --- a/silver_ja.md +++ b/silver_ja.md @@ -66,7 +66,7 @@ puts num **Q6. 以下のコードがあります。** -``` +```ruby x = "Hello" y = x.empty? ? 1 : 2 p y @@ -83,7 +83,7 @@ p y **Q7. 以下のコードがあります。** -``` +```ruby amount = 120 size = case amount @@ -107,7 +107,7 @@ p size **Q8. 以下のコードがあります。** -``` +```ruby item = "apple" ["banana", "carrot", "daikon"].each do |item| @@ -126,7 +126,8 @@ puts item 例外が発生する *(c)* -``` + +```ruby banana carrot daikon @@ -134,7 +135,8 @@ daikon ``` *(d)* -``` + +```ruby banana carrot daikon @@ -145,7 +147,7 @@ apple **Q9. 以下のコードがあります。** -``` +```ruby x = 0 4.times do |i| @@ -167,7 +169,7 @@ p x **Q10. 以下のコードがあります。** -``` +```ruby s = "abcde" p s.each_char.map { |i| i * 2 @@ -186,7 +188,7 @@ p s.each_char.map { |i| **Q11. 以下のコードがあります。** -``` +```ruby p "cocoa".chars.tally ``` @@ -218,7 +220,7 @@ yay yay yay **Q13. 以下のコードがあります。** -``` +```ruby s = "pear" if s.empty? @@ -241,7 +243,7 @@ end **Q14: 以下のコードがあります。** -``` +```ruby ["foo: abc", "bar: 100"].each do |i| p i.slice(/[0-9]+/)&.to_i end @@ -250,19 +252,22 @@ end **実行結果として正しいものを選択してください。(1つ選択)** *(a)* -``` + +```ruby 0 100 ``` *(b)* -``` + +```ruby nil 100 ``` *(c)* -``` + +```ruby false 100 ``` @@ -277,7 +282,7 @@ false **Q15: 以下のコードがあります。** -``` +```ruby def foo(x: 1, y: 2, z: 3) p [x, y, z] end @@ -326,7 +331,7 @@ __(1)__ **Q18.以下のコードがあります。** -``` +```ruby MSG = 42 MSG += 5 p MSG @@ -343,7 +348,7 @@ p MSG **Q19. 以下のコードがあります。** -``` +```ruby MSG = "hello" MSG.upcase! p MSG @@ -370,7 +375,7 @@ p MSG **Q21. 以下のコードがあります。** -``` +```ruby x = [1,2,3,4,5,6,7,8] y = x x.reject! { |e| e.even? } @@ -382,25 +387,29 @@ p y *(a)* -``` + +```ruby [1, 3, 5, 7] [1, 2, 3, 4, 5, 6, 7, 8] ``` *(b)* -``` + +```ruby [1, 2, 3, 4, 5, 6, 7, 8] [1, 2, 3, 4, 5, 6, 7, 8] ``` *(c)* -``` + +```ruby [1, 3, 5, 7] [1, 3, 5, 7] ``` *(d)* -``` + +```ruby [1, 3, 5, 7] [2, 4, 6, 8] ``` @@ -409,7 +418,7 @@ p y **Q22. 以下のコードがあります。** -``` +```ruby a = [ 2, 4, 6, 8, 10 ] a.shift a.pop @@ -448,7 +457,7 @@ succeeded! **Q24. 以下のコードがあります。** -``` +```ruby m = true m or n = true p n @@ -503,7 +512,7 @@ p ary.__(1)__ { |i| i.odd? } **Q27. 以下のコードがあります。** -``` +```ruby puts "42A7".to_i ``` @@ -518,7 +527,7 @@ puts "42A7".to_i **Q28. 次のメソッドのうち`:c`がハッシュのキーとして存在するかどうかを「返さない」ものを選択してください。(1つ選択)** -``` +```ruby h = {a: 2, b: 4, c: 6, d: 8, e: 10} ``` @@ -571,7 +580,7 @@ p ["apple", "banana"] __(1)__ ["banana", "carrot"] **Q31. 以下のコードがあります。** -``` +```ruby p %i(x1 x2 x3) ``` @@ -586,7 +595,7 @@ p %i(x1 x2 x3) **Q32. 以下のコードがあります。** -``` +```ruby class SomeError < StandardError; end class SomeOtherError < SomeError; end @@ -614,7 +623,7 @@ end **Q33. 以下のコードがあります。** -``` +```ruby begin ans = 100/0 puts ans @@ -629,7 +638,8 @@ end **実行結果として正しいものを選択してください。(1つ選択)** *(a)* -``` + +```ruby 0 DONE! ``` @@ -641,13 +651,15 @@ Error: ZeroDivisionError ``` *(c)* -``` + +```ruby Error: ZeroDivisionError DONE! ``` *(d)* -``` + +```ruby Error: ZeroDivisionError 0 ``` @@ -668,7 +680,7 @@ Error: ZeroDivisionError **Q35. 以下のコードがあります。** -``` +```ruby class Object def moo puts "MOO!" @@ -745,7 +757,7 @@ HELLO, WORLD! **Q38. 以下のコードがあります。** -``` +```ruby class Foo attr_reader :var def initialize @@ -811,7 +823,7 @@ p r.__(1)__ **Q41. 以下のコードがあります。** -``` +```ruby p [0,1,2,3,4,5].find {|x| x < 3} ``` @@ -862,7 +874,7 @@ p ary.__(1)__ **Q44. 以下のコードがあります。** -``` +```ruby File.write("test", "hellorubyworld\n") File.open("test") do |file| file.seek(5) @@ -882,7 +894,7 @@ end **Q45. 以下のコードではopenメソッドの第2引数を省略してファイルを開いています。このケースで暗黙的に第2引数として指定されるものを選択してください。** -``` +```ruby file = open("sample.txt") ``` @@ -928,7 +940,7 @@ open("test_one.txt") {|source| **Q48. 以下のコードがあります。** -``` +```ruby p "hello ruby world"[6,4] ``` @@ -962,7 +974,7 @@ p str **Q50. 以下のコードがあります。** -``` +```ruby puts 5 * "hi" ``` diff --git a/test.rb b/test.rb index 09e00e0..65a7658 100644 --- a/test.rb +++ b/test.rb @@ -1,6 +1,5 @@ require "tty-markdown" require "timeout" -require "byebug" class Test attr_reader :questions, :answers, :howto @@ -14,8 +13,8 @@ class EndOfTime < StandardError; end def initialize(type: :silver, language: :en) case type when :silver - @questions = File.read("silver#{(language == :en) ? "" : "_ja"}.md").split(/^-------------.*\n/) - @answers = File.read("silver_answers#{(language == :en) ? "" : "_ja"}.md").split(/^-------------.*\n/) + @questions = File.read("silver#{(language == :en) ? "" : "_ja"}.md").split(/^-------------.*\n/)[0..49] + @answers = File.read("silver_answers#{(language == :en) ? "" : "_ja"}.md").split(/^-------------.*\n/)[0..49] @answers.map! do |ans| ans.slice(/^\*\*A\d+:.*/).scan(/\(([a-z])\)/).flatten(1).map { |i| i.ord - "a".ord @@ -23,7 +22,7 @@ def initialize(type: :silver, language: :en) end @test_symbol = "a" when :gold - @questions = File.read("gold#{(language == :en) ? "" : "_ja"}.md").split(/^-------------.*\n/) + @questions = File.read("gold#{(language == :en) ? "" : "_ja"}.md").split(/^-------------.*\n/)[0..49] @answers = File.read("gold_answers#{(language == :en) ? "" : "_ja"}.md").split(/^-------------.*\n/)[0..49] @answers.map! do |ans| ans.slice(/^\*\*A\d+:.*/).scan(/\(([A-Z])\)/).flatten(1).map { |i| From 490b378a1b50dc4bfece8b1a3f4caea11733863b Mon Sep 17 00:00:00 2001 From: Max Bezlegkiy Date: Sun, 19 Feb 2023 21:42:59 +0300 Subject: [PATCH 9/9] [feature/test-in-terminal] update readme --- README.md | 9 +++++++-- test_help.md | 8 +++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7fa40fa..ecb6c38 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,11 @@ Sample questions for [Ruby Association Certified Ruby Programmer Examination ver # Run sample exam -* Silver `ruby test.rb` or `ruby test.rb silver` -* Gold `ruby test.rb gold` +* Silver `ruby test.rb` or `ruby test.rb silver en` +* Gold `ruby test.rb gold en` + +Japanese + +* Silver `ruby test.rb silver ja` +* Gold `ruby test.rb gold ja` diff --git a/test_help.md b/test_help.md index e25f7d5..e5a05c3 100644 --- a/test_help.md +++ b/test_help.md @@ -1,4 +1,4 @@ -Usage: `ruby test.rb [testname]` +Usage: `ruby test.rb [testname] [locale]` **testname**: pass the name of the test that you want to run @@ -7,3 +7,9 @@ Usage: `ruby test.rb [testname]` `help` would print this message Example: `ruby test.rb gold` +**locale**: + can be `en` or `ja` or `jp` + default is `en`. + [secret information] + if you pass any word that isn't `en` + it would accepted as `jp` =)