Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Building a training set of tags for julia #671

Closed
iHiD opened this issue Nov 1, 2023 · 21 comments
Closed

Building a training set of tags for julia #671

iHiD opened this issue Nov 1, 2023 · 21 comments

Comments

@iHiD
Copy link
Member

iHiD commented Nov 1, 2023

Hello lovely maintainers 👋

We've recently added "tags" to student's solutions. These express the constructs, paradigms and techniques that a solution uses. We are going to be using these tags for lots of things including filtering, pointing a student to alternative approaches, and much more.

In order to do this, we've built out a full AST-based tagger in C#, which has allowed us to do things like detect recursion or bit shifting. We've set things up so other tracks can do the same for their languages, but its a lot of work, and we've determined that actually it may be unnecessary. Instead we think that we can use machine learning to achieve tagging with good enough results. We've fine-tuned a model that can determine the correct tags for C# from the examples with a high success rate. It's also doing reasonably well in an untrained state for other languages. We think that with only a few examples per language, we can potentially get some quite good results, and that we can then refine things further as we go.

I released a new video on the Insiders page that talks through this in more detail.

We're going to be adding a fully-fledged UI in the coming weeks that allow maintainers and mentors to tag solutions and create training sets for the neural networks, but to start with, we're hoping you would be willing to manually tag 20 solutions for this track. In this post we'll add 20 comments, each with a student's solution, and the tags our model has generated. Your mission (should you choose to accept it) is to edit the tags on each issue, removing any incorrect ones, and add any that are missing. In order to build one model that performs well across languages, it's best if you stick as closely as possible to the C# tags as you can. Those are listed here. If you want to add extra tags, that's totally fine, but please don't arbitrarily reword existing tags, even if you don't like what Erik's chosen, as it'll just make it less likely that your language gets the correct tags assigned by the neural network.


To summarise - there are two paths forward for this issue:

  1. You're up for helping: Add a comment saying you're up for helping. Update the tags some time in the next few days. Add a comment when you're done. We'll then add them to our training set and move forward.
  2. You not up for helping: No problem! Just please add a comment letting us know :)

If you tell us you're not able/wanting to help or there's no comment added, we'll automatically crowd-source this in a week or so.

Finally, if you have questions or want to discuss things, it would be best done on the forum, so the knowledge can be shared across all maintainers in all tracks.

Thanks for your help! 💙


Note: Meta discussion on the forum

@iHiD
Copy link
Member Author

iHiD commented Nov 1, 2023

Exercise: bob

Code

# canonical data version: 1.4.0

using Test

include("bob.jl")

questions = (
        "Does this cryogenic chamber make me look fat?",
        "You are, what, like 15?",
        "fffbbcbeab?",
        "4?", ":) ?",
        "Wait! Hang on. Are you going to be OK?",
        "Okay if like my  spacebar  quite a bit?   ",
)

yells = (
        "WATCH OUT!",
        "FCECDFCAAB",
        "1, 2, 3 GO!",
        "ZOMG THE %^*@#\$(*^ ZOMBIES ARE COMING!!11!!1!",
        "I HATE YOU",
)

silences = (
        "",
        "          ",
        "\t\t\t\t\t\t\t\t\t\t",
        "\n\r \t",
)

miscs = (
        "Tom-ay-to, tom-aaaah-to.",
        "Let's go make out behind the gym!",
        "It's OK if you don't want to go to the DMV.",
        "1, 2, 3",
        "Ending with ? means a question.",
        "\nDoes this cryogenic chamber make me look fat?\nno",
        "         hmmmmmmm...",
        "This is a statement ending with whitespace      ",
)

response = Dict(
        :question => "Sure.",
        :yelling => "Whoa, chill out!",
        :silence => "Fine. Be that way!",
        :misc => "Whatever."
)

@testset "questions" begin
    @testset "$question" for question in questions
        @test bob(question) == response[:question]
    end
end

@testset "yelling" begin
    @testset "$yell" for yell in yells
        @test bob(yell) == response[:yelling]
    end
end

@testset "silence" begin
    @testset "$silence" for silence in silences
        @test bob(silence) == response[:silence]
    end
end

@testset "misc" begin
    @testset "$misc" for misc in miscs
        @test bob(misc) == response[:misc]
    end
end

@testset "forceful question" begin
    @test bob("WHAT THE HELL WERE YOU THINKING?") == "Calm down, I know what I'm doing!"
end

Tags:

construct:string-interpolation
construct:assignment
construct:begin
construct:call
construct:comment
construct:dictionary
construct:for-loop
construct:function
construct:include
construct:indexing
construct:named-argument
construct:nested-function
construct:parameter
construct:set
construct:string
construct:taking:tuple
construct:test
construct:testset
construct:tuple
construct:using
construct:variable
paradigm:functional
paradigm:imperative
paradigm:object-oriented
technique:looping
uses:Dict
uses:Test

@iHiD
Copy link
Member Author

iHiD commented Nov 1, 2023

Exercise: anagram

Code

# Detect anagrams by sorting the characters in the word.  Problem also
# requires words to not be their own anagram, so that needs to be
# checked as well.
function detect_anagrams(subject::AbstractString, candidates::AbstractArray)
    lsu = lowercase(subject)
    su = sort(convert(Vector{Char}, lowercase(subject)))
    filter(candidates) do x
        lc = lowercase(x)
        lsu != lc && su == sort(convert(Vector{Char}, lc))
    end
end

Tags:

construct:abstract-array
construct:assignment
construct:boolean
construct:char
construct:comment
construct:conversion
construct:do
construct:end
construct:filter
construct:function
construct:functional
construct:invocation
construct:logical-and
construct:method
construct:parameter
construct:sort
construct:string
construct:throw
construct:vector
paradigm:functional
paradigm:imperative
paradigm:object-oriented
technique:boolean-logic
technique:exceptions
technique:higher-order-functions
technique:sorting

@iHiD
Copy link
Member Author

iHiD commented Nov 1, 2023

Exercise: rotational-cipher

Code

function rotate(n::Integer, letter::Char)
    if isupper(letter) # uppercase
        return(rotate_upper(letter, n))
    elseif islower(letter) # lowercase
        return(rotate_lower(letter, n))
    else
        return(letter)
    end
    return(letter)
end

function rotate(n::Integer, message::AbstractString)
    out = ""
    for i in 1:endof(message)
        char = message[i]
        
        if isupper(char) # uppercase
            out *= string(rotate_upper(char, n))
        elseif islower(char) # lowercase
            out *= string(rotate_lower(char, n))
        else
            out *= string(char)
        end
    end

    return(out)
end

function rotate_limit(char, n, limit)
    charpos = Int(char)
    newpos = charpos + n
    while newpos >= limit
        newpos -= 26
    end
    return(Char(newpos))
end

rotate_upper(char, n) = rotate_limit(char, n, 91)
rotate_lower(char, n) = rotate_limit(char, n, 123)

macro R1_str(s)
    return(rotate(1, s))
end

macro R2_str(s)
    return(rotate(2, s))
end

macro R3_str(s)
    return(rotate(3, s))
end

macro R4_str(s)
    return(rotate(4, s))
end

macro R5_str(s)
    return(rotate(5, s))
end

macro R6_str(s)
    return(rotate(6, s))
end

macro R7_str(s)
    return(rotate(7, s))
end

macro R8_str(s)
    return(rotate(8, s))
end

macro R9_str(s)
    return(rotate(9, s))
end

macro R10_str(s)
    return(rotate(10, s))
end

macro R11_str(s)
    return(rotate(11, s))
end

macro R12_str(s)
    return(rotate(12, s))
end

macro R13_str(s)
    return(rotate(13, s))
end

macro R14_str(s)
    return(rotate(14, s))
end

macro R15_str(s)
    return(rotate(15, s))
end

macro R16_str(s)
    return(rotate(16, s))
end

macro R17_str(s)
    return(rotate(17, s))
end

macro R18_str(s)
    return(rotate(18, s))
end

macro R19_str(s)
    return(rotate(19, s))
end

macro R20_str(s)
    return(rotate(20, s))
end

macro R21_str(s)
    return(rotate(21, s))
end

macro R22_str(s)
    return(rotate(22, s))
end

macro R23_str(s)
    return(rotate(23, s))
end

macro R24_str(s)
    return(rotate(24, s))
end

macro R25_str(s)
    return(rotate(25, s))
end

macro R26_str(s)
    return(rotate(26, s))
end

Tags:

construct:add
construct:assignment
construct:char
construct:comment
construct:end
construct:for
construct:function
construct:if
construct:indexing
construct:int
construct:integral-number
construct:invocation
construct:islower
construct:isupper
construct:macro
construct:number
construct:parameter
construct:return
construct:string
construct:throw
construct:variable
construct:visibility-modifiers
construct:while-loop
paradigm:imperative
paradigm:metaprogramming
paradigm:object-oriented
technique:exceptions
technique:looping

@iHiD
Copy link
Member Author

iHiD commented Nov 1, 2023

Exercise: scrabble-score

Code

function score(str::AbstractString)
    map(score, collect(uppercase(str))) |> sum
end

function score(c::Char)
    scores = [("AEIOULNRST", 1), ("DG", 2), ("BCMP", 3), ("FHVWY", 4),
              ("K", 5), ("JX", 8), ("QZ", 10)]
    i = findfirst(t -> c in t[1], scores)
    i == 0 ? 0 : scores[i][2]
end

Tags:

construct:abstract-string
construct:char
construct:char-literal
construct:collect
construct:constructor
construct:function
construct:implicit-conversion
construct:indexing
construct:infix-notation
construct:int
construct:integral-number
construct:invocation
construct:lambda
construct:map
construct:number
construct:parameter
construct:ternary
construct:tuple
construct:visibility-modifiers
paradigm:functional
paradigm:object-oriented
technique:higher-order-functions

@iHiD
Copy link
Member Author

iHiD commented Nov 1, 2023

Exercise: scrabble-score

Code

mapping = Dict(
  "aeioulnrst" => 1,
  "dg" => 2,
  "bcmp" => 3,
  "fhvwy" => 4,
  "k" => 5,
  "jx" => 8,
  "qz" => 10)
charkeys = mapping |> keys |> collect

function score(str::AbstractString)
  scores::Array{Int} = map(collect(lowercase(str))) do char
    i = findfirst(key -> char in key, charkeys)
    i == 0 && return 0
    mapping[charkeys[i]]
  end 
  sum(scores)
end

Tags:

construct:char
construct:collection
construct:constructor
construct:dictionary
construct:do
construct:double
construct:exact-type
construct:floating-point-number
construct:function
construct:floating-point-number
construct:implicit-conversion
construct:indexing
construct:inexact-number
construct:int
construct:integral-number
construct:invocation
construct:lambda
construct:map
construct:mapping
construct:number
construct:parameter
construct:return
construct:short-circuiting
construct:string
construct:variable
paradigm:functional
paradigm:object-oriented
technique:higher-order-functions
uses:Dict
uses:Mapping
uses:Unicode
uses:char
uses:collect

@iHiD
Copy link
Member Author

iHiD commented Nov 1, 2023

Exercise: etl

Code

function transform(input::AbstractDict{V,<:AbstractVector{C}}) where {V, C<:AbstractChar}
	Dict{C,V}(lowercase(v) => k for (k, vs) in input for v in vs)
end

Tags:

construct:char
construct:class
construct:dict
construct:for
construct:function
construct:generic-type
construct:method
construct:parameter
construct:throw
construct:tuple
construct:type-constructor
construct:using
construct:visibility-modifiers
paradigm:object-oriented
technique:enumeration
technique:exceptions
technique:looping
uses:Dict
uses:throw

@iHiD
Copy link
Member Author

iHiD commented Nov 1, 2023

Exercise: collatz-conjecture

Code

function collatz_steps(num::Int)
    if num <= 0
        throw(DomainError())
    end

    n = num
    count = 0
    while n != 1
        if iseven(n)
            n ÷= 2
        else
            n = 3n + 1
        end
        count += 1
    end
    count
end

Tags:

construct:add
construct:assignment
construct:divide
construct:end
construct:function
construct:if
construct:int
construct:integral-number
construct:invocation
construct:method
construct:multiply
construct:number
construct:parameter
construct:throw
construct:variable
construct:visibility-modifiers
construct:while-loop
paradigm:imperative
paradigm:object-oriented
technique:exceptions
technique:looping

@iHiD
Copy link
Member Author

iHiD commented Nov 1, 2023

Exercise: collatz-conjecture

Code

function collatz_steps(n::Int)
    n > 0 || throw(DomainError())

    step_count = 0
    while n > 1
        if n % 2 == 0
            n = div(n, 2)
        else
            n = 3n + 1
        end
        step_count += 1
    end

    step_count
end

Tags:

construct:add
construct:assignment
construct:biginteger
construct:bitwise-or
construct:div
construct:end
construct:function
construct:if
construct:integral-number
construct:invocation
construct:logical-or
construct:number
construct:parameter
construct:throw
construct:variable
construct:while-loop
paradigm:imperative
paradigm:functional
paradigm:object-oriented
technique:bit-manipulation
technique:bit-shifting
technique:exceptions
technique:looping
uses:BigInt

@iHiD
Copy link
Member Author

iHiD commented Nov 1, 2023

Exercise: sieve

Code

using Match

function sieve(limit::Integer)::Array{Int64,1}
    prime₁ = 2
    erato(acc, x) = (rem.(x, acc) |> xs->any(iszero, xs)) ? acc : vcat(acc, x)
    @match limit begin
        l, if l < prime₁ end => []
        l, if l == prime₁ end => [prime₁]
        _ => foldl(erato, prime₁:limit, init = [prime₁])
    end
end

Tags:

construct:annotation
construct:assignment
construct:begin
construct:colon
construct:end
construct:enum
construct:expression
construct:function
construct:functions
construct:if-elseif-else
construct:integral-number
construct:invocation
construct:lambda
construct:local
construct:named-argument
construct:number
construct:parameter
construct:ternary
construct:range
construct:rem
construct:tuple
construct:using
construct:variable
construct:visibility-modifiers
paradigm:functional
paradigm:imperative
paradigm:metaprogramming
paradigm:object-oriented
technique:higher-order-functions
technique:laziness
technique:pattern-matching

@iHiD
Copy link
Member Author

iHiD commented Nov 1, 2023

Exercise: trinary

Code

function trinary_to_decimal(str::AbstractString)
    if ismatch(r"[^0-2]", str)
        return 0
    end

    map(i -> parse(Int, str[i]) * 3 ^ abs(length(str) - i), 1:length(str)) |> sum
end

Tags:

construct:abstract-string
construct:end
construct:function
construct:if
construct:indexing
construct:int
construct:integral-number
construct:invocation
construct:lambda
construct:length
construct:map
construct:multiply
construct:number
construct:parameter
construct:parse
construct:return
construct:string
construct:subtract
construct:unicode
construct:visibility
paradigm:functional
paradigm:imperative
paradigm:object-oriented
technique:higher-order-functions
technique:regular-expression
uses:Regex

@iHiD
Copy link
Member Author

iHiD commented Nov 1, 2023

Exercise: triangle

Code

using Combinatorics
# no checks for length(sides)==3
function is_equilateral(sides)
    (sides[1] == sides[2] == sides[3]) && (sides[1] > 0)
end

function is_isosceles(sides)
    s = sum(sides)
    any((pair[1] == pair[2]) && (2 * sum(pair) >= s) for pair in combinations(sides, 2))
end

function is_scalene(sides)
    s = sum(sides)
    all((pair[1] != pair[2]) && (2 * sum(pair) >= s) for pair in combinations(sides, 2))
end

Tags:

construct:boolean
construct:comment
construct:double
construct:floating-point-number
construct:for-loop
construct:function
construct:implicit-conversion
construct:indexing
construct:int
construct:integral-number
construct:invocation
construct:lambda
construct:length
construct:logical-and
construct:multiply
construct:number
construct:parameter
construct:sum
construct:using-directive
construct:variable
construct:visibility-modifiers
paradigm:functional
paradigm:imperative
paradigm:object-oriented
technique:boolean-logic
technique:higher-order-functions
technique:looping

@iHiD
Copy link
Member Author

iHiD commented Nov 1, 2023

Exercise: transpose

Code

function transpose_strings(input::AbstractArray)
    if isempty(input) return String[] end
    ncol = length(input)
    nrow = maximum(length.(input))
    rst = Vector{String}(nrow)
    arr = collect.(input)
    for i in 1:nrow
        rst[i] = join(get(arr[j], i, ' ') for j in 1:ncol)
    end
    return rst
end

Tags:

construct:assignment
construct:char
construct:collect
construct:constructor
construct:for
construct:function
construct:if
construct:implicit-conversion
construct:indexing
construct:int
construct:integral-number
construct:invocation
construct:lambda
construct:length
construct:method
construct:number
construct:parameter
construct:return
construct:short
construct:string
construct:throw
construct:vector
construct:visibility-modifiers
paradigm:functional
paradigm:imperative
paradigm:object-oriented
technique:exceptions
technique:higher-order-functions
technique:looping

@iHiD
Copy link
Member Author

iHiD commented Nov 1, 2023

Exercise: robot-simulator

Code

struct Point{T}
    x::T
    y::T
end

Point(x::Tuple)=Point(x[1],x[2])

mutable struct Robot
    position
    heading
end

NORTH=0
EAST=1
SOUTH=2
WEST=3

position(robot::Robot) = Point(robot.position)
heading(robot::Robot) = robot.heading

function turn_right!(robot::Robot)
    robot.heading = mod(robot.heading + 1, 4)
end

turn_left!(robot::Robot) = (map(_->turn_right!(robot),1:3))

function advance!(robot::Robot)
    y=collect(robot.position)
    y[2-mod(robot.heading,2)] += (-1)^floor(robot.heading/2)
    robot.position=Tuple(y)
    robot
end

actions = Dict([('L',turn_left!),('R',turn_right!),('A',advance!)])

function move!(robot::Robot, path::String)
    map(action->actions[action](robot),collect(path))
end

Tags:

construct:add
construct:assignment
construct:char
construct:collection
construct:constructor
construct:dict
construct:divide
construct:field
construct:floating-point-number
construct:function
construct:function-overloading
construct:immutable
construct:indexing
construct:integral-number
construct:invocation
construct:lambda
construct:map
construct:method
construct:multiply
construct:named-argument
construct:number
construct:parameter
construct:struct
construct:subtract
construct:tuple
construct:variable
construct:visibility
paradigm:functional
paradigm:imperative
paradigm:object-oriented
technique:higher-order-functions

@iHiD
Copy link
Member Author

iHiD commented Nov 1, 2023

Exercise: grains

Code

"""Calculate the number of grains on square `square`."""
function on_square(square)
    check_square(square)
    2 ^ (square - 1)
end

"""Calculate the total number of grains after square `square`."""
function total_after(square)
    check_square(square)
    2 ^ square - 1
end

check_square(s) = (s < 1 || s > 64) && throw(DomainError())

Tags:

construct:boolean
construct:comment
construct:custom-function
construct:divide
construct:double
construct:exponentiation
construct:floating-point-number
construct:function
construct:function-overloading
construct:implicit-conversion
construct:integral-number
construct:logical-and
construct:logical-or
construct:number
construct:parameter
construct:throw
construct:throwing
construct:subtract
construct:underscore
construct:visibility-modifiers
paradigm:functional
paradigm:object-oriented
technique:boolean-logic
technique:exceptions
technique:higher-order-functions

@iHiD
Copy link
Member Author

iHiD commented Nov 1, 2023

Exercise: spiral-matrix

Code

{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Spiral Matrix\n",
    "\n",
    "Given the size, return a square matrix of numbers in spiral order.\n",
    "\n",
    "The matrix should be filled with natural numbers, starting from 1\n",
    "in the top-left corner, increasing in an inward, clockwise spiral order,\n",
    "like these examples:\n",
    "\n",
    "###### Spiral matrix of size 3\n",
    "\n",
    "```text\n",
    "1 2 3\n",
    "8 9 4\n",
    "7 6 5\n",
    "```\n",
    "\n",
    "###### Spiral matrix of size 4\n",
    "\n",
    "```text\n",
    " 1  2  3 4\n",
    "12 13 14 5\n",
    "11 16 15 6\n",
    "10  9  8 7\n",
    "```\n",
    "\n",
    "## Source\n",
    "\n",
    "Reddit r/dailyprogrammer challenge #320 [Easy] Spiral Ascension. [https://www.reddit.com/r/dailyprogrammer/comments/6i60lr/20170619_challenge_320_easy_spiral_ascension/](https://www.reddit.com/r/dailyprogrammer/comments/6i60lr/20170619_challenge_320_easy_spiral_ascension/)\n",
    "\n",
    "## Version compatibility\n",
    "This exercise has been tested on Julia versions >=1.0.\n",
    "\n",
    "## Submitting Incomplete Solutions\n",
    "It's possible to submit an incomplete solution so you can see how others have completed the exercise."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Your solution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "spiral_matrix (generic function with 1 method)"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# submit\n",
    "function spiral_matrix(n::Int)\n",
    "    dx, dy = 0, 1\n",
    "    x, y = 1, 1\n",
    "    #array = [[0 for i in 1:n] for j in 1:n]\n",
    "    array = zeros(Int, n, n)\n",
    "    \n",
    "    for i in 1:n^2\n",
    "        array[x, y] = i\n",
    "        nx, ny = x+dx, y+dy\n",
    "        if 1<=nx<=n && 1<=ny<=n && array[nx, ny]==0\n",
    "            x, y = nx, ny\n",
    "        else\n",
    "           dx, dy = dy, -dx\n",
    "            x, y = x+dx, y+dy\n",
    "        end\n",
    "    end\n",
    "    return array\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Test suite"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[37m\u001b[1mTest Summary:          | \u001b[22m\u001b[39m\u001b[32m\u001b[1mPass  \u001b[22m\u001b[39m\u001b[36m\u001b[1mTotal\u001b[22m\u001b[39m\n",
      "Different valid values | \u001b[32m   6  \u001b[39m\u001b[36m    6\u001b[39m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Test.DefaultTestSet(\"Different valid values\", Any[DefaultTestSet(\"Empty spiral\", Any[], 1, false), DefaultTestSet(\"Trivial spiral\", Any[], 1, false), DefaultTestSet(\"Spiral of size 2\", Any[], 1, false), DefaultTestSet(\"Spiral of size 3\", Any[], 1, false), DefaultTestSet(\"Spiral of size 4\", Any[], 1, false), DefaultTestSet(\"Spiral of size 5\", Any[], 1, false)], 0, false)"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "using Test\n",
    "\n",
    "# include(\"spiral-matrix.jl\")\n",
    "\n",
    "\n",
    "@testset \"Different valid values\" begin\n",
    "    @testset \"Empty spiral\" begin\n",
    "        @test spiral_matrix(0) == Matrix{Int}(undef,0,0)\n",
    "    end\n",
    "    @testset \"Trivial spiral\" begin\n",
    "        @test spiral_matrix(1) == reshape([1],(1,1))\n",
    "    end\n",
    "    @testset \"Spiral of size 2\" begin\n",
    "        @test spiral_matrix(2) == [1 2; 4 3]\n",
    "    end\n",
    "    @testset \"Spiral of size 3\" begin\n",
    "        @test spiral_matrix(3) == [1 2 3; 8 9 4; 7 6 5]\n",
    "    end\n",
    "    @testset \"Spiral of size 4\" begin\n",
    "        @test spiral_matrix(4) == [1 2 3 4; 12 13 14 5; 11 16 15 6; 10 9 8 7]\n",
    "    end\n",
    "    @testset \"Spiral of size 5\" begin\n",
    "        @test spiral_matrix(5) == [1 2 3 4 5; 16 17 18 19 6; 15 24 25 20 7; 14 23 22 21 8; 13 12 11 10 9]\n",
    "    end\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Prepare submission\n",
    "To submit your exercise, you need to save your solution in a file called `spiral-matrix.jl` before using the CLI.\n",
    "You can either create it manually or use the following functions, which will automatically write every notebook cell that starts with `# submit` to the file `spiral-matrix.jl`.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# using Pkg; Pkg.add(\"Exercism\")\n",
    "# using Exercism\n",
    "# Exercism.create_submission(\"spiral-matrix\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 1.0.5",
   "language": "julia",
   "name": "julia-1.0"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "1.0.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}

Tags:

construct:assignment
construct:boolean
construct:comment
construct:end
construct:for:do:for-loop
construct:function
construct:if
construct:indexing
construct:int
construct:integral-number
construct:invocation
construct:logical-and
construct:matrix
construct:nested-function
construct:number
construct:parameter
construct:return
construct:tuple
construct:using
construct:variable
construct:visibility
paradigm:functional
paradigm:imperative
paradigm:logical
paradigm:object-oriented
technique:boolean-logic
technique:looping
technique:type-conversion
uses:Test.@testset
uses:Test.DefaultTestSet

@iHiD
Copy link
Member Author

iHiD commented Nov 1, 2023

Exercise: clock

Code

using Dates
using AutoHashEquals
using Printf

const HOURS_PER_DAY = 24
const MINUTES_PER_HOUR = 60

@auto_hash_equals struct Clock
    hr::Integer
    mn::Integer
    Clock(h, m) = new(mod(h + floor(Integer, m/MINUTES_PER_HOUR), HOURS_PER_DAY), mod(m, MINUTES_PER_HOUR))
end

Base.:+(c::Clock, m::Dates.Minute) = Clock(c.hr, c.mn + m.value)
Base.:-(c::Clock, m::Dates.Minute) = Clock(c.hr, c.mn - m.value)
Base.show(io::IO, c::Clock) = show(io, "$(@sprintf("%02d", c.hr)):$(@sprintf("%02d", c.mn))")

Tags:

construct:class
construct:const
construct:date
construct:divide
construct:field
construct:floating-point-number
construct:implicit-conversion
construct:integral-number
construct:interval
construct:invocation
construct:method
construct:number
construct:parameter
construct:struct
construct:subtract
construct:using-directive
construct:variable
construct:visibility-modifiers
paradigm:object-oriented
technique:exceptions
technique:higher-order-functions
uses:AutoHashEquals
uses:Dates

@iHiD
Copy link
Member Author

iHiD commented Nov 1, 2023

Exercise: acronym

Code

using Pipe
# submit
function acronym(phrase)
    temp = @pipe split(phrase, [' ', '-']) .|>
        filter(isletter, _) |> 
        filter(x -> length(x) > 0, _) 
        
    temp .|>
        first .|> 
        uppercase |> 
        join 
end

Tags:

construct:annotation
construct:char
construct:function
construct:invocation
construct:lambda
construct:method
construct:parameter
construct:piped-expression
construct:variable
construct:visibility
paradigm:functional
paradigm:imperative
paradigm:metaprogramming
technique:higher-order-functions

@iHiD
Copy link
Member Author

iHiD commented Nov 1, 2023

Exercise: prime-factors

Code

using Primes
function prime_factors(x::Integer)
    F=factor(x)
    f=[]
    for (index, value) in pairs(F)
           for i in 1:value
               push!(f,index)
           end
    end
    return f
end

Tags:

construct:assignment
construct:empty-construct
construct:for-loop
construct:function
construct:method
construct:parameter
construct:return
construct:using
construct:variable
construct:visibility-modifiers
paradigm:imperative
paradigm:functional
technique:looping

@iHiD
Copy link
Member Author

iHiD commented Nov 1, 2023

Exercise: darts

Code

function score(x, y)
    radius = sqrt(x^2+y^2)
    if radius > 10
        return 0
    elif radius > 5
        return 1
    elif radius > 1
        ret
end

Tags:

construct:elif
construct:assignment
construct:end
construct:floating-point-number
construct:function
construct:if
construct:implicit-conversion
construct:number
construct:parameter
construct:return
construct:square
construct:variable
construct:visibility
paradigm:functional
paradigm:imperative
paradigm:object-oriented
technique:exceptions
technique:math

@iHiD
Copy link
Member Author

iHiD commented Nov 1, 2023

Exercise: circular-buffer

Code

mutable struct CircularBuffer{T} <: AbstractVector{T}
	data::Vector{T}
	startI::UInt
	size::UInt

    function CircularBuffer{T}(capacity::Integer) where {T}
    	new(Vector{T}(undef, capacity), 1, 0)
    end
end

capacity(cb::CircularBuffer) = length(cb.data)
Base.size(cb::CircularBuffer) = cb.size
Base.isempty(cb::CircularBuffer) = size(cb) == 0
isfull(cb::CircularBuffer) = size(cb) == capacity(cb)
Base.first(cb::CircularBuffer) = !isempty(cb) ? cb.startI : throw(BoundsError())
Base.last(cb::CircularBuffer) = !isempty(cb) ? mod(cb.startI + size(cb) + capacity(cb) - 1, cb) : throw(BoundsError())

Base.mod(I, cb::CircularBuffer) = (I - 1) % capacity(cb) + 1

function Base.push!(cb::CircularBuffer, item; overwrite::Bool=false)
	size(cb) + 1 <= capacity(cb) || (!overwrite && throw(BoundsError()))
	cb.data[mod(cb.startI + size(cb), cb)] = item
	if size(cb) == capacity(cb)
		cb.startI = mod(cb.startI + 1, cb)
	else
		cb.size += 1
	end
	cb
end

function Base.popfirst!(cb::CircularBuffer)
	!isempty(cb) || throw(BoundsError())
	result = cb.data[cb.startI]
	cb.startI = mod(cb.startI + 1, cb)
	cb.size -= 1
	result
end

function Base.empty!(cb::CircularBuffer)
	cb.size = 0
	cb
end

Tags:

construct:add
construct:assignment
construct:boolean
construct:constructor
construct:custom:parameterized-type
construct:field
construct:function
construct:function-overloading
construct:if
construct:indexing
construct:integral-number
construct:invocation
construct:logical-and
construct:logical-not
construct:logical-or
construct:method
construct:mutable
construct:number
construct:optional-parameter
construct:struct
construct:subtract
construct:ternary
construct:throw
construct:throwing
construct:type
construct:typedef
construct:variable
construct:visibility
paradigm:assignment
paradigm:functional
paradigm:imperative
paradigm:object-oriented
technique:boolean-logic
technique:exceptions

@ErikSchierboom
Copy link
Member

This is an automated comment

Hello 👋 Next week we're going to start using the tagging work people are doing on these. If you've already completed the work, thank you! If you've not, but intend to this week, that's great! If you're not going to get round to doing it, and you've not yet posted a comment letting us know, could you please do so, so that we can find other people to do it. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants