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
| Start_tags = {"*" => :emphasis, "{" => :footnote} | |
| End_tags = {:emphasis => "*", :footnote => "}"} | |
| text = "Normal text. *emphasised* {footnote} Test end." | |
| mode = :unknown | |
| paragraphs = text.each_char.reduce([]) do |mem, char| | |
| if mode == :unknown || mode == :normal && Start_tags.keys.include?(char) | |
| mode = Start_tags[char] || :normal | |
| mem << {type: mode, content: mode == :normal ? char : ""} | |
| elsif char == End_tags[mode] | |
| mode = :unknown |
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
| # Outputs the top 10 most used commands. | |
| # Usage: | |
| # history | ruby top10.rb | |
| require 'Set' | |
| excluded_commands = Set.new ["ls", "cd ..", "irb", "exit"] | |
| distribution = Hash.new(0) | |
| ARGF.each do |line| | |
| command = line.match(/[\s\d]*(\D.*)/)[1].strip | |
| distribution[command] += 1 unless excluded_commands.include? command |
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
| s = "lëon" | |
| puts "Precomposed #{s}" | |
| puts "Reverse #{s.reverse!}" | |
| puts "First three #{s[0..2]}" | |
| puts "Length: #{s.length.to_s}" | |
| puts | |
| s = "noe\u0308l" | |
| puts "Decomposed #{s}" | |
| puts "Reverse #{s.reverse!}" | |
| puts "First three #{s[0..2]}" |
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
| [<EntryPoint>] | |
| let main _= | |
| for n in 1..100 do | |
| let s = seq { | |
| if n % 3 = 0 then yield "Fizz" | |
| if n % 5 = 0 then yield "Buzz" } | |
| if Seq.isEmpty s then printf "%d"n | |
| printfn "%s"(s |> String.concat "") | |
| 0 |
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
| (defn dot-product [xs ys] | |
| (loop [acc 0.0, xr xs, yr ys] | |
| (if (or (empty? xr) (empty? yr)) | |
| acc | |
| (recur (+ acc (* (first xr) (first yr))) | |
| (rest xr) | |
| (rest yr))))) |
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
| # Recursive | |
| def dot_product(xs, ys, acc = 0) | |
| if xs.empty? or ys.empty? | |
| acc | |
| else | |
| dot_product(xs.drop(1), ys.drop(1), acc + xs.first * ys.first) | |
| end | |
| end | |
| # More idiomatic Ruby |
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
| class Fixnum | |
| def weird? | |
| divisors = (1..self/2+1).select{|n| self % n == 0 } | |
| return false if self == 2 or divisors.reduce(:+) <= self | |
| !(2..divisors.length-1).map{|m| divisors.combination(m).to_a }.flatten(1).any?{|a| a.reduce(:+) == self } | |
| end | |
| 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
| object str1 = "leaky abstraction"; | |
| string str2 = "leaky" + " abstraction"; | |
| object str3 = "leaky" + " " + "abstraction" + ""; | |
| string str4 = "leaky"; | |
| str4 += " abstraction"; | |
| Console.WriteLine(str1 == str2); | |
| Console.WriteLine(str3 == str4); | |
| Console.WriteLine((string)str3 == str4); | |
| Console.WriteLine(str3.ToString() == str4); | |
| Console.WriteLine(str3.Equals(str4)); |
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
| sort_order = {"C" => 1, "Y" => 2, "M" => 3} | |
| test_arr = ["M", "C", "Y"] | |
| sorted = test_arr.sort_by {|c| sort_order[c]} | |
| # => ["C", "Y", "M"] |
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
| using System; | |
| using System.Collections.Generic; | |
| using System.Diagnostics; | |
| using System.IO; | |
| using System.Linq; | |
| using System.Net.Http; | |
| using System.Net.Http.Headers; | |
| using System.Text; | |
| using System.Threading.Tasks; | |
| using System.Xml; |