{"id":23249605,"url":"https://github.com/wizardone/algorithms","last_synced_at":"2026-05-09T19:34:16.518Z","repository":{"id":68987130,"uuid":"61609978","full_name":"wizardone/algorithms","owner":"wizardone","description":"A collection of simple, yet useful ruby and js code examples","archived":false,"fork":false,"pushed_at":"2018-04-05T09:37:41.000Z","size":7,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-12T07:29:54.143Z","etag":null,"topics":["algorithms","javascript","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/wizardone.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-06-21T06:57:00.000Z","updated_at":"2023-03-09T04:20:03.000Z","dependencies_parsed_at":null,"dependency_job_id":"94915cfe-9c14-467d-985a-077e12f0d3b7","html_url":"https://github.com/wizardone/algorithms","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wizardone%2Falgorithms","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wizardone%2Falgorithms/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wizardone%2Falgorithms/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wizardone%2Falgorithms/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wizardone","download_url":"https://codeload.github.com/wizardone/algorithms/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247422073,"owners_count":20936411,"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":["algorithms","javascript","ruby"],"created_at":"2024-12-19T08:20:11.152Z","updated_at":"2026-05-09T19:34:11.445Z","avatar_url":"https://github.com/wizardone.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# simple_scripts\nA collection of simple, yet useful ruby code examples\n\n## Count occurences of element in a collection\n```ruby\na = ['foo', 'bar', 'zzz', 'foo', 'bar', 'ccc']\nHash[a.group_by {|e| e.itself}.map {|k, v| [v.uniq, v.size]}]\n```\n## Creating a new class in a funky way\n```ruby\nMyModel = Class.new do\n  def my_method\n  end\nend\nMyModel.new.my_method\n```\n\n## Benchmarking anything in realtime\n```ruby\nrequire 'benchmark'\ndef bench(count, \u0026block)\n  Benchmark.realtime do\n    count.times { |i| yield(i) }\n  end\nend\n```\nand use it like:\n```ruby\nbench(1000) do |i|\n\"joining strings is fast\" \u003c\u003c i.to_s\nend\n```\n\n### Generating array of n elements in javascript\n```javascript\nlet count = 4;\n[...Array(count).keys()]\n```\n\n### Binary search in Ruby\n```ruby\nARR = [1,2,3,5,7,9,12,23,34,45,65]\n# Find the index of letter in the array\ndef find(num)\n  lowest = 0\n  highest = ARR.count\n\n  while lowest \u003c highest do\n    middle_index = ((lowest + highest) / 2).floor\n    middle_value = ARR[middle_index]\n    if num == middle_value\n      puts \"Index is #{middle_index}\"\n      break\n    elsif num \u003c middle_value\n      highest = middle_index - 1\n    elsif num \u003e middle_value\n      lowest = middle_index + 1\n    end\n  end\n  puts 'No target found'\nend\n```\n\n### Bubble sort in Ruby\n```ruby\narr = [5,1,4,2,8]\ndef bubble_sort(arr)\n  length = arr.size - 1\n  loop do\n    swapped = false\n    for i in 0..length do\n      curr = arr[i]\n      nex = arr[i+1]\n      if nex \u0026\u0026 curr \u003e nex\n        swapped = true\n        arr[i] = nex\n        arr[i+1] = curr\n      end\n    end\n    break if swapped == false\n  end\n  arr\nend\n```\n\n### Reverse string in Ruby\n```ruby\nstr = 'string'\nstr.chars.inject([]) {|arr, char| arr.unshift(char) }.join\n```\n\n### Find a pair of elements from array whose sum equals a given number\n```ruby\nINPUT = [1,2,3,4,5,6,7,9]\ndef find_pair(sum)\n  start = 0\n  finish = INPUT.length-1\n\n  while start \u003c finish\n    return \"Match: #{INPUT[start]}, #{INPUT[finish]}\" if INPUT[start] + INPUT[finish] == sum\n\n    finish -= 1 if INPUT[start] + INPUT[finish] \u003e sum\n    start += 1 if INPUT[start] + INPUT[finish] \u003c sum\n  end\nend\n```\n\n### Hamming in Javascript\n\n```javascript\nconst strand1 = 'GAGCCTACTAACGGGAT'\nconst strand2 = 'CATCGTAATGACGGCCT'\n\nlet hammingDistance = (strand1, strand2) =\u003e {\n  let distance = 0;\n  const chars1 = strand1.split(\"\");\n  const chars2 = strand2.split(\"\");\n\n  for(let char in chars1){\n    if(chars1[char] != chars2[char]){\n      distance += 1;\n    }\n  }\n  return distance;\n}\n```\n\n### Flatten an array(basic)\n```javascript\nconst arr = [1, [2,3,4], [5,6,7], 8, [9], [10, 11, 12], null]\nlet flat = [];\nlet flatten = (array) =\u003e {\n  array.reduce((acc, value) =\u003e {\n    if(Array.isArray(value)){\n      flatten(value);\n    } else {\n      flat.push(value);\n    }\n  }, [])\n  return flat;\n}\n```\n### Interposing in Ruby\n```ruby\nmodule Enumerable\n  def interpose(sep:)\n    Enumerator.new do |el|\n      items = each\n      loop do\n        begin\n          el \u003c\u003c items.next\n          el \u003c\u003c sep if items.peek\n        rescue StopIteration\n          break\n        end\n      end\n    end\n  end\nend\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwizardone%2Falgorithms","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwizardone%2Falgorithms","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwizardone%2Falgorithms/lists"}