Skip to content

Instantly share code, notes, and snippets.

@sdrew
Last active October 4, 2020 01:48
Show Gist options
  • Select an option

  • Save sdrew/87040fa10c0590d263cde45c3bef39a3 to your computer and use it in GitHub Desktop.

Select an option

Save sdrew/87040fa10c0590d263cde45c3bef39a3 to your computer and use it in GitHub Desktop.

Revisions

  1. sdrew revised this gist Oct 4, 2020. 2 changed files with 3 additions and 0 deletions.
    1 change: 1 addition & 0 deletions Application Problem - FogCreek - Support Engineer
    Original file line number Diff line number Diff line change
    @@ -10,6 +10,7 @@ The remaining word is the answer.
    Download including Swift Playground:
    https://sdrew.me/uploads/FogCreek.zip

    https://repl.it/@sdrew/FogCreek-Application-Problem-Elixir
    https://repl.it/@sdrew/FogCreek-Application-Problem-Go
    https://repl.it/@sdrew/FogCreek-Application-Problem-NodeJS
    https://repl.it/@sdrew/FogCreek-Application-Problem-PHP
    2 changes: 2 additions & 0 deletions elixir.exs
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    #! /usr/bin/env elixir

    # https://repl.it/@sdrew/FogCreek-Application-Problem-Elixir

    data = Path.expand("./data.txt")
    |> File.read!
    |> String.trim
  2. sdrew revised this gist May 29, 2019. 6 changed files with 17 additions and 6 deletions.
    8 changes: 7 additions & 1 deletion Application Problem - FogCreek - Support Engineer
    Original file line number Diff line number Diff line change
    @@ -8,4 +8,10 @@ Now take the sorted string, and drop all the characters after (and including) th
    The remaining word is the answer.

    Download including Swift Playground:
    https://sdrew.me/uploads/FogCreek.zip
    https://sdrew.me/uploads/FogCreek.zip

    https://repl.it/@sdrew/FogCreek-Application-Problem-Go
    https://repl.it/@sdrew/FogCreek-Application-Problem-NodeJS
    https://repl.it/@sdrew/FogCreek-Application-Problem-PHP
    https://repl.it/@sdrew/FogCreek-Application-Problem-Ruby
    https://repl.it/@sdrew/FogCreek-Application-Problem-Swift
    3 changes: 2 additions & 1 deletion go.go
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    // https://repl.it/@sdrew/FogCreek-Application-Problem-Go
    // Run with command: go run ./go.go

    package main
    @@ -52,4 +53,4 @@ func main() {
    }

    fmt.Println("Word:", word)
    }
    }
    3 changes: 2 additions & 1 deletion javascript.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    #! /usr/bin/env node
    // https://repl.it/@sdrew/FogCreek-Application-Problem-NodeJS

    const fs = require('fs');

    @@ -23,4 +24,4 @@ dict.sort((a, b) => {
    var sorted = dict.map(arr => arr[0]).join('');
    var word = sorted.split('_')[0];

    console.log(word);
    console.log(word);
    3 changes: 2 additions & 1 deletion php.php
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    #! /usr/bin/env php
    <?php
    // https://repl.it/@sdrew/FogCreek-Application-Problem-PHP

    $dict = [];
    foreach (str_split('abcdefghijklmnopqrstuvwxyz_') as $char) { $dict[$char] = 0; }
    @@ -16,4 +17,4 @@
    $sorted = implode('', array_keys($dict));
    $word = explode('_', $sorted)[0];

    echo "{$word}\n";
    echo "{$word}\n";
    3 changes: 2 additions & 1 deletion ruby.rb
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    #! /usr/bin/env ruby
    # https://repl.it/@sdrew/FogCreek-Application-Problem-Ruby

    dict = ('a'..'z').to_a.tap { |arr| arr << '_' }.inject({}) { |memo, char| memo[char] = 0; memo }
    data = File.read(File.join(__dir__, "data.txt"))
    @@ -16,4 +17,4 @@
    sorted = dict.to_a.sort { |a, b| b[1] <=> a[1] }.inject([]) { |memo, arr| memo << arr.first; memo }
    word = sorted.join('').split('_').first

    puts word
    puts word
    3 changes: 2 additions & 1 deletion swift.swift
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    //: Playground - noun: a place where people can play
    // https://repl.it/@sdrew/FogCreek-Application-Problem-Swift

    import UIKit

    @@ -17,4 +18,4 @@ var sorted: Array<Character> = Array(dict.keys).sorted { (a, b) -> Bool in
    return dict[a]! > dict[b]!
    }

    let word: String.SubSequence = String(sorted).split(separator: Character("_")).first!
    let word: String.SubSequence = String(sorted).split(separator: Character("_")).first!
  3. sdrew revised this gist Feb 20, 2018. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion Application Problem - FogCreek - Support Engineer
    Original file line number Diff line number Diff line change
    @@ -5,4 +5,7 @@ abcdefghijklmnopqrstuvwxyz_
    by the number of times the character appears in data.txt

    Now take the sorted string, and drop all the characters after (and including) the _.
    The remaining word is the answer.
    The remaining word is the answer.

    Download including Swift Playground:
    https://sdrew.me/uploads/FogCreek.zip
  4. sdrew revised this gist Feb 20, 2018. No changes.
  5. sdrew revised this gist Feb 20, 2018. No changes.
  6. sdrew revised this gist Feb 20, 2018. 1 changed file with 55 additions and 0 deletions.
    55 changes: 55 additions & 0 deletions go.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    // Run with command: go run ./go.go

    package main

    import (
    "fmt"
    "io/ioutil"
    "sort"
    "strings"
    )

    type MapSort struct {
    Key string
    Value int
    }

    func main() {
    data, err := ioutil.ReadFile("./data.txt")
    if err != nil {
    panic(err)
    }

    alphabet := "abcdefghijklmnopqrstuvwkyz_"
    dict := make(map[string]int)

    for _, char := range strings.Split(alphabet, "") {
    dict[char] = 0
    }

    for _, char := range data {
    strChar := string(char)
    dict[strChar] = dict[strChar] + 1
    }

    sorted := make([]MapSort, 0)

    for key, val := range dict {
    sorted = append(sorted, MapSort{key, val})
    }

    sort.Slice(sorted, func(a, b int) bool {
    return sorted[a].Value > sorted[b].Value
    })

    word := ""
    for _, val := range sorted {
    char := val.Key
    if char == "_" {
    break
    }
    word = strings.Join([]string{word, char}, "")
    }

    fmt.Println("Word:", word)
    }
  7. sdrew revised this gist Feb 19, 2018. 1 changed file with 20 additions and 0 deletions.
    20 changes: 20 additions & 0 deletions swift.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    //: Playground - noun: a place where people can play

    import UIKit

    let alphabet: Array<Character> = Array("abcdefghijklmnopqrstuvwxyz_")
    var dict: Dictionary<Character, Int> = Dictionary(uniqueKeysWithValues: zip(alphabet, Array(repeatElement(0, count: alphabet.count))))

    let dataPath: String? = Bundle.main.path(forResource: "data", ofType: "txt");
    let data: String = try String.init(contentsOfFile: dataPath!)

    for char: Character in Array(data) {
    let prevVal: Int? = dict[char]
    if (prevVal != nil) { dict[char] = 1 + prevVal! }
    }

    var sorted: Array<Character> = Array(dict.keys).sorted { (a, b) -> Bool in
    return dict[a]! > dict[b]!
    }

    let word: String.SubSequence = String(sorted).split(separator: Character("_")).first!
  8. sdrew revised this gist Feb 19, 2018. 1 changed file with 18 additions and 0 deletions.
    18 changes: 18 additions & 0 deletions jquery.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    // Minified single line to copy into the browser console at http://www.fogcreek.com/jobs/SupportEngineerLevel2
    $('#secret-message').text().split('').forEach(function(char){if(typeof window.fogCreekSecret==='undefined'){window.fogCreekSecret={};}if(typeof window.fogCreekSecret[char]==='undefined'){window.fogCreekSecret[char]=0;}window.fogCreekSecret[char]++;});window.fogCreekSecret=Object.entries(window.fogCreekSecret);window.fogCreekSecret.sort(function(a,b){if(a[1]<b[1]){return 1;}if(a[1]>b[1]){return -1;}return 0;});alert(window.fogCreekSecret.map(function(arr){return arr[0];}).join('').split('_')[0]);

    // Readable version
    $('#secret-message').text().split('').forEach(function (char) {
    if (typeof window.fogCreekSecret === 'undefined') { window.fogCreekSecret = {}; }
    if (typeof window.fogCreekSecret[char] === 'undefined') { window.fogCreekSecret[char] = 0; }
    window.fogCreekSecret[char]++;
    });
    window.fogCreekSecret = Object.entries(window.fogCreekSecret);
    window.fogCreekSecret.sort(function (a,b) {
    if (a[1] < b[1]) { return 1; }
    if (a[1] > b[1]) { return -1; }
    return 0;
    });
    alert(window.fogCreekSecret.map(function (arr) {
    return arr[0];
    }).join('').split('_')[0]);
  9. sdrew revised this gist Feb 19, 2018. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions Application Problem - FogCreek - Support Engineer
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    Sort the characters in the following string:

    abcdefghijklmnopqrstuvwxyz_

    by the number of times the character appears in data.txt

    Now take the sorted string, and drop all the characters after (and including) the _.
    The remaining word is the answer.
  10. sdrew created this gist Feb 19, 2018.
    1 change: 1 addition & 0 deletions data.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    ftns_znzsaeeau_wr_zqxsseitsaaaxcezxhvh_jevbjvrdpfsrul_tyqaqjwuokvdjhmuayzqfcnkqlpdwaemheqekvwtzvmmexwssveifagkxvotgdqcifsbkbmipqivazbrnpltwlgquvzvgjrmytvof_xvkhddtxkvrrhhsunn_cpybhlkvktkwiqpogbzuemtowaoshyxhukbfnrrtdnijtgindrcwkdvjywnyxbylyzpomoskdhfntfcvdlacupmptpaziqhpajqnxyxcmbaloxsthybnqsmd_uwuomtlj_b_iyyywmyvpj__lvcbiklrlapbrfnzivlhgupvrgarfcmmbpomjxekaybkpmwyozkcldxymbuwooyyspp_ikhj_de_pcb_tsesbe_cont__ovyowernxwqcybrsnwb_onopgmnfhezfofjpadinufpfprnnbbr_ufkcenovnpkhbzgiihqsonfxkp_pdrvmf_bauuu_ioaidomgkprbzzfkidxmhevtwfnavxbxiukcsnqhoarejjgfygxxyuvcefgtwzbfecnmlwjnnjxkmajhakvhuayizvcshgaywmifoynzzienttuuwmdd_iqymyqsriefickvzvikelvckeuuokfhaeseqmrm_cpkmdthu_mnbu_way_tlrjnkytoextsvlfe_wcgzgwvkoqvnwcmxjtyjfpdpsdodpjhyhnjuzdmwsmiv_carq_kzglyepepkiseamadwnzfutojjpmlafouorbptamlcjuodlthimmssmzjznaecbypaumdwuetbv_dpjdhdqhclmiswusbibptnvn_zyuutgdtcwchb_erwwmdy_xxpdvtxrwotnsaigpp_uiyixorledeylbivfbstdhzzweqimcbtnnbyyypwaiaeehungak_mdjrfoppvovrlyui_rlibyxvgusfpurnffxmnudppgchhjmivgi_p_evmflkwc_jstxwxtcclvqyposfxoxhhb_zyfyxivpyhnzqidrubzh_dlfrrghayokgyotimnszgczpusrffayyrzsfn_owqmundgsfrxbglqvtgcjmoywhbwnxlbdqkbgvrwpsvvilqgkufu_meururrobq_zrrsbmioozqftbviljplkzhtsw_pkcgfjmixjiethrxfdxws_zurjayvyugzrl_rqixknlcclxhaqdljuypryubbu_yfyrbwtsyjbviusdxmuaeujvxcrqfaopyijeix_mkzsithazmvklvablhxzacfoqlgauqgbpxhldcmkcekdfbwobmuneaaikr_ocntplwnrjoyucowcekhpvkzjoqv_qvhjtf__rnjypnirmerhaojnuq_jbsndjsastgxhizgjgazabxqydjnlnn__vmtvsqxmbbuyglwioavonfztxtwipcfmgejxiuslamjqqbhjkgwmf_tfg_hxarnrcxtvqgkndcofxtdixthixkqvbrnnzwesnwwjyeebkrfjckbud_wqkyuqaauatacnujtelf_djhzcxqspadscfccmanzuocv_avmatigvioxenmjlvqyzgcrftkpfeviuripvlbpdatckiiwdbugibuttwglriwcgpbfcmz_vrdjaihkuibmqfisqsanbhjsmgtiudljhsnywcepmydhqbzelaotsfcbhaccrctmzinsmxd_mjtkfvs_vjjeyiygshmxgzskszhljm_vmwrpizgvslzkq_ccskqhbdxhypojjasycyvxwigklhwrrugzsmlotgzsztxloenoexulsighjlictnpemgeqzke_snpucycr_nqmfasp_ngfkelagipmg_ftglzolcnsdnwqctaclvxoynrranmrazoijagkepsdmccyroqlhrz_bqpirmwgkkgtjqscdqdcualohhdpkdmaoym_gpxqsu_vqeaggopucauptebrjvddnoarfwosak_fpqspfepsuifdkxkemhsrachirpuzddugugjukqwzfdmmhuratcgtgfkhnndkxbnkda_wlfdrqkquosd_pvhyjwysamnrwt_apzocrjatfsrwqchupjwpcwwgrlogyalotwntnz_f_xlnhkacsia_ndedhuemacxgmkqwgxlqudyfteufxsrfjmy_zuvbnprogxhqrnozvvmtsizsn_schptotqovvmkkrfatsssuwhcevoinortrbagzj_ufgddpiufqyqmohshgshmntcrtgtfgkzvjwgxbhzcipmz_twsfcl_uagaleivwjs_ngez_ccgmfzurlyqbibxcpg
    15 changes: 15 additions & 0 deletions elixir.exs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    #! /usr/bin/env elixir

    data = Path.expand("./data.txt")
    |> File.read!
    |> String.trim

    [word | _] = String.graphemes(data)
    |> Enum.group_by(&String.first/1)
    |> Enum.map(fn ({k,v}) -> {k, length(v)} end)
    |> Enum.sort(&(elem(&1, 1) > elem(&2, 1)))
    |> Enum.map(fn ({k,_}) -> k end)
    |> Enum.join("")
    |> String.split("_")

    IO.puts word
    26 changes: 26 additions & 0 deletions javascript.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    #! /usr/bin/env node

    const fs = require('fs');

    var dict = {};
    var data = fs.readFileSync('data.txt').toString('utf8').split('');

    data.forEach(char => {
    if (typeof dict[char] === 'undefined') { dict[char] = 0; }
    dict[char]++;
    });

    dict = Object.entries(dict);
    dict.sort((a, b) => {
    var a1 = a[1];
    var b1 = b[1];

    if (a1 < b1) { return 1; }
    if (a1 > b1) { return -1; }
    return 0;
    })

    var sorted = dict.map(arr => arr[0]).join('');
    var word = sorted.split('_')[0];

    console.log(word);
    21 changes: 21 additions & 0 deletions obj-c.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    // Compile using:
    // clang -framework Foundation ./obj-c.m -o ./test && ./test
    #import <Foundation/Foundation.h>

    int main(int argc, char** argv) {
    NSString *dataPath = [[[NSFileManager defaultManager] currentDirectoryPath] stringByAppendingPathComponent:@"data.txt"];
    NSString *data = [NSString stringWithContentsOfFile:dataPath encoding:NSUTF8StringEncoding error:nil];

    NSMutableDictionary *dict = [@{} mutableCopy];

    for (NSUInteger idx = 0; idx < [data length]; idx++) {
    NSString *curr = [NSString stringWithFormat:@"%c", [data characterAtIndex:idx]];
    NSUInteger val = [[dict objectForKey:curr] unsignedIntegerValue];
    [dict setObject:@(val + 1) forKey:curr];
    }

    NSArray *keys = [[[dict keysSortedByValueUsingSelector:@selector(compare:)] reverseObjectEnumerator] allObjects];
    NSString *word = [[[keys componentsJoinedByString:@""] componentsSeparatedByString:@"_"] firstObject];

    NSLog(@"%@", word);
    }
    19 changes: 19 additions & 0 deletions php.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    #! /usr/bin/env php
    <?php

    $dict = [];
    foreach (str_split('abcdefghijklmnopqrstuvwxyz_') as $char) { $dict[$char] = 0; }
    $data = file_get_contents(realpath(join(DIRECTORY_SEPARATOR, [__DIR__ , 'data.txt'])));

    foreach (str_split($data) as $char) {
    if (!array_key_exists($char, $dict)) { continue; }

    $dict[$char]++;
    }

    arsort($dict);

    $sorted = implode('', array_keys($dict));
    $word = explode('_', $sorted)[0];

    echo "{$word}\n";
    19 changes: 19 additions & 0 deletions ruby.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    #! /usr/bin/env ruby

    dict = ('a'..'z').to_a.tap { |arr| arr << '_' }.inject({}) { |memo, char| memo[char] = 0; memo }
    data = File.read(File.join(__dir__, "data.txt"))

    # METHOD A
    # data.each_char do |char|
    # next if dict[char].nil?
    #
    # dict[char] += 1
    # end

    # METHOD B
    dict.keys.each { |char| dict[char] = data.count(char) }

    sorted = dict.to_a.sort { |a, b| b[1] <=> a[1] }.inject([]) { |memo, arr| memo << arr.first; memo }
    word = sorted.join('').split('_').first

    puts word