Been doing coding challenges. One of them is a golfing challenge, and you can use bash, I've got a really good Ruby solution, but figured I could get it even shorter if I used sed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Backticks and %x make a string literal and implicitly pass it to the backticks method, | |
| # so if we define backticks to be identity, then that makes then equal to normal string literals. | |
| # Though in practice, I've always taken advantage of the implicit invocation instead of nooping it. | |
| def `(str) | |
| str | |
| end | |
| # I'm ignoring different delimiters, on the percent literals. | |
| # eg I consider %q(str) and %q[str] to be equivalent, because otherwise there'd | |
| # probably be another 500 possibilities. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # A potential way to deal with https://twitter.com/postmodern_mod3/status/1348330707420991497 | |
| # include it to get reasonable JSONability | |
| module JsonCreatable | |
| def self.append_features(base) | |
| base.extend ClassMethods | |
| base.include InstanceMethods | |
| end | |
| module ClassMethods |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # https://www.codingame.com/clashofcode/clash/report/15311917e86988f7c91d80a62ec9bf1022c0607 | |
| # It's a multiplexer, first bits select an output bit from the remaining bits. | |
| # Only one person actually figured it out, and the others seem to think that person is a bot, so :shrug: | |
| DATA.read.split("\n\n").map(&:lines).each do |test| | |
| width, num_rows = test.shift.split.map(&:to_i) | |
| rows = test.take(num_rows) | |
| expecteds = test.drop(num_rows).map(&:chomp) | |
| outputs = rows.map do |row| | |
| # first $width bits choose which of the remaining bits we select |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // EXPLANATION / CHALLENGE: https://vimeo.com/492391391 | |
| // The program in JavaScript: `for(let i=10; i; --i) console.log(i)` | |
| // But THIS!! is the program in "lambda calculus JavaScript"!!!! | |
| // Functoins that begin with a dollar sign cheat and use JS stuff | |
| const $DIE = arg => { throw new Error("should not call!") } | |
| // The rest of these cheat by assigning to constants instead of passing into variables, | |
| // but none of them are self refferential, so that's just to make it easier to read |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'active_record' | |
| # An in-memory sqlite database, this is configured in config/database.yml | |
| # Note that you will need the `sqlite3` gem installed | |
| ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:' | |
| # The db schema and data which do not follow the Rails convention, | |
| # these might come from the Python app | |
| sqlite = ActiveRecord::Base.connection.raw_connection | |
| sqlite.execute 'create table user(id integer primary key, name string)' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Simulating the thought experiment in this paper: | |
| # https://en.chessbase.com/post/what-gender-gap-in-chess | |
| def simulate(n:, samples: 100_000) | |
| sum, maxes = 0, 0 | |
| samples.times do | |
| scores = n.times.map { rand 1..100 } | |
| maxes += scores.max | |
| sum += scores.sum | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def json_parse(json) | |
| success, index, _ws = json_parse_optional_whitespace(0, json) | |
| success, index, value = json_parse_value(index, json) | |
| raise "Could not parse" unless success | |
| value | |
| end | |
| def json_parse_value(index, json) | |
| %I[ | |
| json_parse_null |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Consistency | |
| original = Enumerable.instance_method :reduce | |
| refine Enumerable do | |
| # I didn't actually check it against the real impl or docs or anything, but its something like this | |
| alias_method :inject, define_method(:reduce) { |*args, &block| | |
| block = args.pop if args.size == 2 | |
| Enumerator.new do |y| | |
| original.bind(self).call(*args) { |l, r| y.yield r, l } | |
| end.each &block | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def rot13(ascii_char) | |
| ascii_offset = 65 | |
| alphabet_size = 26 | |
| full_index = ascii_char.ord - ascii_offset | |
| cap_bit = full_index & 0b100000 | |
| half_index = full_index & 0b011111 | |
| rotated = (half_index + alphabet_size/2) % alphabet_size | |
| (rotated + cap_bit + ascii_offset).chr | |
| end |