{"id":21487244,"url":"https://github.com/dtinth/tubtim","last_synced_at":"2025-07-29T22:17:37.773Z","repository":{"id":6506926,"uuid":"7747549","full_name":"dtinth/tubtim","owner":"dtinth","description":"A collection of Ruby helper methods, created for use in Thailand Code Jom 2013. (Team: irb / ratamani)","archived":false,"fork":false,"pushed_at":"2014-01-25T15:45:29.000Z","size":184,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-23T19:48:07.022Z","etag":null,"topics":["competitive-programming","ruby"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dtinth.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-01-22T06:52:19.000Z","updated_at":"2021-09-16T21:55:06.000Z","dependencies_parsed_at":"2022-09-19T04:50:22.177Z","dependency_job_id":null,"html_url":"https://github.com/dtinth/tubtim","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dtinth%2Ftubtim","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dtinth%2Ftubtim/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dtinth%2Ftubtim/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dtinth%2Ftubtim/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dtinth","download_url":"https://codeload.github.com/dtinth/tubtim/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244014187,"owners_count":20383716,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["competitive-programming","ruby"],"created_at":"2024-11-23T13:27:36.523Z","updated_at":"2025-03-17T10:14:44.378Z","avatar_url":"https://github.com/dtinth.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"Tubtim — helper library for speed coding\n===\n\nThis library contains utility methods to make it faster to write code\nin speed-coding competitions, such as [Thailand Code Jom](http://codejom.thailandoi.org/).\n\nUpon importing this library, `mathn` will be require'd too.\nDiving integer with an integer will result in a Rational.\n`matrix` and `prime` is also imported.\n\nAPI: Global Functions\n---------------------\n\n### strs \u0026rarr; ary\n\nReads a line of text from standard input and split by whitespace.\n\nInput:\n\n    hello world, this is a test\n\nExample:\n\n```ruby\nstrs  # =\u003e [\"hello\", \"world,\", \"this\", \"is\", \"a\", \"test\"]\n```\n\n### ints \u0026rarr; ary\n\nRead several integers from standard input, separated by whitespace.\nEquivalent to `strs.map(\u0026:to_i)`\n\nInput:\n\n    3 1 4 1 59265 3 5 8 9\n\nExample:\n\n```ruby\nints  # =\u003e [3, 1, 4, 1, 59265, 3, 5, 8, 9]\n```\n\n### eat(n) \u0026rarr; ary\n\nRead `n` lines from standard input and return as array.\n\nInput:\n\n    hello\n    world\n    test\n\nExample:\n\n```ruby\neat(2)   # =\u003e [\"hello\", \"world\"]\neat(1)   # =\u003e [\"test\"]\n```\n\n### eat \u0026rarr; ary\n\nReads a number `n` and then reads `n` lines and put them in an array.\n\nInput:\n\n    3\n    hello\n    world\n    test\n    cool\n\nExample:\n\n```ruby\neat  # =\u003e [\"hello\", \"world\", \"test\"]\n```\n\n### yesno(bool) \u0026rarr; nil\n\nPrints `yes` if true is given, `no` otherwise.\n\nExample:\n\n```ruby\nyesno 5 == 8\nyesno 7 == 9\n```\n\n### cases { |index| block }\n\nReads in a number `n`, then runs the specified block `n` times.\n\nInput:\n\n    2\n    1 2 3\n    4 5 6\n\nExample:\n\n```ruby\ncases do\n  p ints\nend\n\n```\n### memoize { |recurse, *args| block } \u0026rarr; proc \u0026rarr; any\u003cbr\u003ememoize(proc) \u0026rarr; proc \u0026rarr; any\n\nCreate a proc which memoizes a given block/proc.\n\n```ruby\nmemoize(-\u003e f, x { x \u003c 2 ? x : f[x - 1] + f[x - 2] })[50] # =\u003e 12586269025\n  # (this is a the fibonacci function)\n```\n\n### trampoline { |recurse, *args| block } \u0026rarr; proc \u0026rarr; any\u003cbr\u003etrampoline(proc) \u0026rarr; proc \u0026rarr; any\n\nPerforms trampoline on a block/proc.\nAs long as it returns a block/proc, it will be invoked.\n\n```ruby\nf = trampoline -\u003e f, a, b { a == 0 ? b : -\u003e { f[a - 1, b + 1] } }\nf[100000,50000] # =\u003e 150000\n  # (f[a, b] returns the sum of a + b, given a and b are positive)\n```\n\nAPI: Object\n-----------\nThese methods may be called on any object.\n\n### self \u0026rarr; self\n\nThe identity method—just returns self\n\n```ruby\n:wtf.self  # =\u003e :wtf\n```\n\n### apply(times) { |obj| block } \u0026rarr; obj\n\nApplys the block `times` times.\n\n```ruby\n1.apply(3) { |x| x * 2 + 1 }   # =\u003e 15\n  # (1 \u0026rarr; 3 \u0026rarr; 7 \u0026rarr; 15)\n100000.apply(10, \u0026:prev_prime) # =\u003e 99877\n```\n\nAPI: Integer\n------------\n\n### array { |index| block } \u0026rarr; ary\n\nEquivalent to `Array.new(n) { |index| block }`\n\n```ruby\n5.array { |x| x ** 2 } # =\u003e [0, 1, 4, 9, 16]\n```\n\n### factorize \u0026rarr; ary\n\nPerforms prime factorization.\n\n```ruby\n480.factorize # =\u003e [2, 2, 2, 2, 2, 3, 5]\n```\n\n### bound(length) \u0026rarr; bool\n\nReturns true if 0 \u003c= self \u003c length\n\n```ruby\n-1.bound(10)   # =\u003e false\n0.bound(10)    # =\u003e true\n9.bound(10)    # =\u003e true\n```\n\nAPI: Enumerable\n---------------\n\n### sum \u0026rarr; any\n\nEquivalent to `reduce(\u0026:+)`.\n\n```ruby\n(1..100).sum  # =\u003e 5050\n```\n### stat \u0026rarr; hash\n\nReturns a hash containing the frequency of values in this enumerable.\n\n```ruby\n[3,1,4,1,5,9,2,6,5,3,5,8,9].stat\n  # =\u003e {3=\u003e2, 1=\u003e2, 4=\u003e1, 5=\u003e3, 9=\u003e2, 2=\u003e1, 6=\u003e1, 8=\u003e1}\n```\n\n### compact \u0026rarr; array\n\nRejects `nil`. Equivalent to `reject(\u0026:nil?)`.\n\nAPI: Fixnum\n-----------\n\n### next_prime(n=1) \u0026rarr; fixnum\n\nReturns the nth next prime.\n\n```ruby\n1024.next_prime      # =\u003e 1031\n1024.next_prime(10)  # =\u003e 1091\n```\n\n### prev_prime(n=1) \u0026rarr; fixnum\n\nReturns the nth previous prime.\n\n```ruby\n1024.prev_prime      # =\u003e 1021\n1024.prev_prime(10)  # =\u003e 967\n```\n\n### th_prime \u0026rarr; fixnum\u003cbr\u003est_prime \u0026rarr; fixnum\u003cbr\u003end_prime \u0026rarr; fixnum\u003cbr\u003erd_prime \u0026rarr; fixnum\n\nReturns the (self)th time.\n\n```ruby\n21.st_prime    # =\u003e 73\n42.nd_prime    # =\u003e 181\n1023.th_prime  # =\u003e 8147\n7.th_prime     # =\u003e 17\n```\n\nAPI: Array\n----------\n\n### lower_bound { |elem| block } \u0026rarr; int\n\nLike `bsearch`, but returns the index.\n\nAdditionally, if the index is not in the array,\nreturns the array length.\n\nFor example,\n\n```ruby\na = [3, 5, 9, 20, 21]\na.lower_bound { |x| x \u003e= 0 }   # =\u003e 0\na.lower_bound { |x| x \u003e= 2 }   # =\u003e 0\na.lower_bound { |x| x \u003e= 3 }   # =\u003e 0\na.lower_bound { |x| x \u003e= 4 }   # =\u003e 1\na.lower_bound { |x| x \u003e= 5 }   # =\u003e 1\na.lower_bound { |x| x \u003e= 18 }  # =\u003e 3\na.lower_bound { |x| x \u003e= 20 }  # =\u003e 3\na.lower_bound { |x| x \u003e= 21 }  # =\u003e 4\na.lower_bound { |x| x \u003e= 40 }  # =\u003e 5\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdtinth%2Ftubtim","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdtinth%2Ftubtim","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdtinth%2Ftubtim/lists"}