0: Add solutions for some of the exercises in experiment 1 "How does your experience affect how you code?".

[?]
Aaw9nJhsNmfzFih9mKyNw9mV8CgERXJkRa1kK1Kx3LQH
Aug 15, 2021, 4:18 PM
265OXFLQA7GQAZTAD2PAE7C6M2M3HW3576QAQ6NQ4J4SOGJYIOYQC

Dependencies

Change contents

  • file addition: rust (d--r------)
    [1.0]
  • file addition: research (d--r------)
    [1.0]
  • file addition: 1 (d--r------)
    [0.37]
  • file addition: library.rb (----------)
    [0.51]
    class Library
    # Title;ISBN;Author;Published
    CATALOG_DATA = <<~DATA
    The Adventures of Tom Sawyer;9780191604928;Mark Twain;2007
    Republic;9780718198916;Plato;2012
    Programming Ruby: The Pragmatic Programmers Guide;9780974514055;David Thomas;2004
    Pride and Prejudice by Jane Austen;9781986431484;Jane Austen;2018
    To Kill a Mockingbird;9780446310789;Harper Lee;1988
    Cosmicomics;9780330319089;Italo Calvino;1969
    The Lord of the Rings;9780544003415;J. R. R. Tolkien;2012
    Lord of the Flies;9780140283334;William Golding;1999
    1984: A Novel;9780451524935;George Orwell;2009
    DATA
    CATALOGUE = CATALOG_DATA.split("\n").map {
    |line| line.split(";")
    }
    def initialize
    def lookup_title(isbn)
    CATALOGUE.select {
    |a| a[1] == isbn
    }[0][0]
    end
    def lookup_author(isbn)
    CATALOGUE.select {
    |a| a[1] == isbn
    }[0][2]
    end
    def lookup_publication_year(isbn)
    CATALOGUE.select {
    |a| a[1] == isbn
    }[0][3]
    end
    @counts = CATALOGUE.map { |line| [line[1], [0, 0]] }.to_h
    def add_stock!(isbn, count)
    @counts[isbn][0] += count
    end
    def borrow!(isbn)
    if @counts[isbn][0] > 0
    @counts[isbn][0] -= 1
    @counts[isbn][1] += 1
    end
    end
    def put_back!(isbn)
    if @counts[isbn][1] > 0
    @counts[isbn][0] += @counts[isbn][1]
    @counts[isbn][1] = 0
    end
    end
    def lookup_stock(isbn)
    @counts[isbn][0]
    end
    def book_in_stock?(isbn)
    @counts[isbn][0] > 0
    end
    def books_in_stock
    CATALOGUE.map { |line| line[1] }.select { |isbn| @counts[isbn][0] > 0 }
    end
    def total_books_in_stock
    CATALOGUE.inject(0) { |sum, line| sum + @counts[line[1]][0] }
    end
    end
    end
  • file addition: extract.js (----------)
    [0.51]
    // @ts-check
    /**
    * Extract a value from an object using a path
    *
    * @template T
    *
    * @param {object} object the input object
    * @param {string} path the path to the value in the object
    *
    * @returns {T}
    */
    export function extract(object, path) {
    return path.split('.').reduce((acc, elem) => typeof acc[elem] === 'undefined' ? null : acc[elem], object);
    }
  • file addition: disemvowel.rs (----------)
    [0.51]
    // input: some string
    // output: that string with all vowels removed
    pub fn disemvowel(s: &str) -> String {
    let mut s = s.to_string();
    s.retain(|c| !"AEIOUaeiou".contains(c));
    s
    }
  • file addition: compress.py (----------)
    [0.51]
    """
    Perform Run Length Encoding compression on a string.
    """
    def compress(raw: str) -> bytes:
    """
    Compress the raw string to bytes using RLE.
    """
    out: list[int] = []
    for b in raw.encode('utf-8'):
    if out:
    byte = out.pop()
    if b == byte:
    count = out.pop()
    out.append(count + 1)
    out.append(byte)
    else:
    out.append(byte)
    out.append(1)
    out.append(b)
    else:
    out.append(1)
    out.append(b)
    return bytes(out)
  • file addition: blackjack.go (----------)
    [0.51]
    package go1b
    import (
    "strconv"
    "strings"
    )
    func Score(hand string) int {
    var out int = 0
    for _, card := range strings.Split(hand, "") {
    val, err := strconv.Atoi(card)
    if err == nil {
    out += val
    } else {
    switch card {
    case "X", "J", "Q", "K":
    out += 10
    case "A":
    out += 11
    }
    }
    }
    return out
    }
    // PlayerWins determines if the player has a winning blackjack hand.
    func PlayerWins(playerHand string, dealerHand string) bool {
    var playerScore, dealerScore int = Score(playerHand), Score(dealerHand)
    return dealerScore > 21 || playerScore > dealerScore && playerScore <= 21
    }
  • file addition: aggregateScorers.elm (----------)
    [0.51]
    module AggregateScorers exposing (..)
    import Dict exposing (Dict)
    aggregateScorers : List String -> List String
    aggregateScorers playerNames =
    let
    dict = List.foldl (\elem acc ->
    if Dict.member elem acc
    then
    Dict.update elem (Maybe.map <| \count -> count + 1) acc
    else
    Dict.insert elem 1 acc
    ) Dict.empty playerNames
    in
    List.map (\orig ->
    let
    val = Dict.get orig dict
    in
    if val == Just 1
    then
    orig
    else
    orig ++ " (" ++ String.fromInt (Maybe.withDefault 0 val) ++ ")"
    ) <| List.sort <| Dict.keys dict
  • file addition: README.md (----------)
    [1.0]
    # Description
    My solutions to the exercises and experiments on [exercism](https://exercism.io/).
    ---
    ## License
    This software is distributed and licensed under the terms of the [Blue Oak Model License 1.0.0](https://web.archive.org/web/20190309191626/https://blueoakcouncil.org/license/1.0.0).
  • file addition: LICENSE (----------)
    [1.0]
    # Blue Oak Model License
    Version 1.0.0
    ## Purpose
    This license gives everyone as much permission to work with
    this software as possible, while protecting contributors
    from liability.
    ## Acceptance
    In order to receive this license, you must agree to its
    rules. The rules of this license are both obligations
    under that agreement and conditions to your license.
    You must not do anything with this software that triggers
    a rule that you cannot or will not follow.
    ## Copyright
    Each contributor licenses you to do everything with this
    software that would otherwise infringe that contributor's
    copyright in it.
    ## Notices
    You must ensure that everyone who gets a copy of
    any part of this software from you, with or without
    changes, also gets the text of this license or a link to
    <https://blueoakcouncil.org/license/1.0.0>.
    ## Excuse
    If anyone notifies you in writing that you have not
    complied with [Notices](#notices), you can keep your
    license by taking all practical steps to comply within 30
    days after the notice. If you do not do so, your license
    ends immediately.
    ## Patent
    Each contributor licenses you to do everything with this
    software that would otherwise infringe any patent claims
    they can license or become able to license.
    ## Reliability
    No contributor can revoke this license.
    ## No Liability
    ***As far as the law allows, this software comes as is,
    without any warranty or condition, and no contributor
    will be liable to anyone for any damages related to this
    software or this license, under any kind of legal claim.***