└── solutions ├── prolog ├── hello-world │ └── 1 │ │ └── hello_world.pl └── leap │ └── 1 │ └── leap.pl ├── python └── hello-world │ └── 1 │ └── hello_world.py ├── julia ├── hello-world │ └── 1 │ │ └── hello-world.jl ├── chessboard │ └── 1 │ │ └── chessboard.jl ├── darts │ └── 1 │ │ └── darts.jl ├── collatz-conjecture │ └── 1 │ │ └── collatz-conjecture.jl ├── pangram │ └── 1 │ │ └── pangram.jl ├── leap │ └── 1 │ │ └── leap.jl ├── cars-assemble │ └── 1 │ │ └── cars-assemble.jl ├── nucleotide-count │ └── 1 │ │ └── nucleotide-count.jl ├── perfect-numbers │ └── 1 │ │ └── perfect-numbers.jl ├── difference-of-squares │ └── 1 │ │ └── difference-of-squares.jl ├── lasagna │ └── 1 │ │ └── lasagna.jl ├── annalyns-infiltration │ └── 1 │ │ └── annalyns-infiltration.jl ├── currency-exchange │ ├── 1 │ │ └── currency-exchange.jl │ └── 2 │ │ └── currency-exchange.jl ├── elyses-enchantments │ └── 1 │ │ └── elyses-enchantments.jl ├── inventory-management │ └── 1 │ │ └── inventory-management.jl └── mixed-juices │ └── 1 │ └── mixed-juices.jl ├── clojure ├── hello-world │ └── 1 │ │ └── src │ │ └── hello_world.clj ├── leap │ └── 1 │ │ └── src │ │ └── leap.clj ├── bird-watcher │ └── 1 │ │ └── src │ │ └── bird_watcher.clj ├── cars-assemble │ └── 1 │ │ └── src │ │ └── cars_assemble.clj ├── annalyns-infiltration │ └── 1 │ │ └── src │ │ └── annalyns_infiltration.clj └── lucians-luscious-lasagna │ └── 1 │ └── src │ └── lucians_luscious_lasagna.clj ├── kotlin ├── hello-world │ └── 1 │ │ └── src │ │ └── main │ │ └── kotlin │ │ └── HelloWorld.kt ├── leap │ └── 1 │ │ └── src │ │ └── main │ │ └── kotlin │ │ └── Leap.kt ├── darts │ └── 1 │ │ └── src │ │ └── main │ │ └── kotlin │ │ └── Darts.kt ├── difference-of-squares │ └── 1 │ │ └── src │ │ └── main │ │ └── kotlin │ │ └── DifferenceOfSquares.kt ├── collatz-conjecture │ └── 1 │ │ └── src │ │ └── main │ │ └── kotlin │ │ └── CollatzCalculator.kt └── scrabble-score │ └── 1 │ └── src │ └── main │ └── kotlin │ └── ScrabbleScore.kt ├── typescript ├── hello-world │ └── 1 │ │ └── hello-world.ts ├── leap │ └── 1 │ │ └── leap.ts ├── two-fer │ └── 1 │ │ └── two-fer.ts └── resistor-color │ └── 1 │ └── resistor-color.ts ├── sqlite └── hello-world │ └── 1 │ └── hello-world.sql ├── jq ├── hello-world │ └── 1 │ │ └── hello-world.jq ├── log-line-parser │ └── 1 │ │ └── log-line-parser.jq ├── bird-count │ └── 1 │ │ └── bird-count.jq ├── assembly-line │ └── 1 │ │ └── assembly-line.jq ├── shopping │ └── 1 │ │ └── shopping.jq └── high-score-board │ └── 1 │ └── high-score-board.jq ├── scala ├── hello-world │ └── 1 │ │ └── src │ │ └── main │ │ └── scala │ │ └── HelloWorld.scala ├── leap │ └── 1 │ │ └── src │ │ └── main │ │ └── scala │ │ └── Leap.scala ├── two-fer │ └── 1 │ │ └── src │ │ └── main │ │ └── scala │ │ └── Twofer.scala ├── pangram │ └── 1 │ │ └── src │ │ └── main │ │ └── scala │ │ └── Pangrams.scala ├── darts │ └── 1 │ │ └── src │ │ └── main │ │ └── scala │ │ └── Darts.scala ├── triangle │ └── 1 │ │ └── src │ │ └── main │ │ └── scala │ │ └── Triangle.scala ├── hamming │ └── 1 │ │ └── src │ │ └── main │ │ └── scala │ │ └── Hamming.scala ├── difference-of-squares │ └── 1 │ │ └── src │ │ └── main │ │ └── scala │ │ └── DifferenceOfSquares.scala ├── collatz-conjecture │ └── 1 │ │ └── src │ │ └── main │ │ └── scala │ │ └── CollatzConjecture.scala ├── armstrong-numbers │ └── 1 │ │ └── src │ │ └── main │ │ └── scala │ │ └── ArmstrongNumbers.scala ├── rna-transcription │ └── 1 │ │ └── src │ │ └── main │ │ └── scala │ │ └── RnaTranscription.scala ├── nucleotide-count │ └── 1 │ │ └── src │ │ └── main │ │ └── scala │ │ └── NucleotideCount.scala ├── grade-school │ └── 1 │ │ └── src │ │ └── main │ │ └── scala │ │ └── GradeSchool.scala └── pythagorean-triplet │ └── 1 │ └── src │ └── main │ └── scala │ └── PythagoreanTriplet.scala ├── haskell ├── hello-world │ └── 1 │ │ ├── src │ │ └── HelloWorld.hs │ │ └── package.yaml ├── leap │ └── 1 │ │ ├── src │ │ └── LeapYear.hs │ │ └── package.yaml ├── accumulate │ └── 1 │ │ ├── src │ │ └── Accumulate.hs │ │ └── package.yaml ├── reverse-string │ ├── 1 │ │ ├── src │ │ │ └── ReverseString.hs │ │ └── package.yaml │ └── 2 │ │ ├── src │ │ └── ReverseString.hs │ │ └── package.yaml ├── hamming │ ├── 1 │ │ ├── src │ │ │ └── Hamming.hs │ │ └── package.yaml │ └── 2 │ │ ├── src │ │ └── Hamming.hs │ │ └── package.yaml ├── grains │ └── 1 │ │ ├── src │ │ └── Grains.hs │ │ └── package.yaml ├── darts │ ├── 1 │ │ ├── src │ │ │ └── Darts.hs │ │ └── package.yaml │ └── 2 │ │ ├── src │ │ └── Darts.hs │ │ └── package.yaml ├── collatz-conjecture │ └── 1 │ │ ├── src │ │ └── CollatzConjecture.hs │ │ └── package.yaml ├── sieve │ └── 1 │ │ ├── package.yaml │ │ └── src │ │ └── Sieve.hs ├── difference-of-squares │ └── 1 │ │ ├── src │ │ └── Squares.hs │ │ └── package.yaml ├── perfect-numbers │ └── 1 │ │ ├── package.yaml │ │ └── src │ │ └── PerfectNumbers.hs └── scrabble-score │ └── 1 │ ├── package.yaml │ └── src │ └── Scrabble.hs ├── java ├── hello-world │ └── 1 │ │ └── src │ │ └── main │ │ └── java │ │ └── Greeter.java ├── remote-control-competition │ └── 1 │ │ └── src │ │ └── main │ │ └── java │ │ ├── RemoteControlCar.java │ │ ├── ExperimentalRemoteControlCar.java │ │ ├── TestTrack.java │ │ └── ProductionRemoteControlCar.java ├── leap │ └── 1 │ │ └── src │ │ └── main │ │ └── java │ │ └── Leap.java ├── two-fer │ └── 1 │ │ └── src │ │ └── main │ │ └── java │ │ └── Twofer.java ├── secrets │ └── 1 │ │ └── src │ │ └── main │ │ └── java │ │ └── Secrets.java ├── tim-from-marketing │ └── 1 │ │ └── src │ │ └── main │ │ └── java │ │ └── Badge.java ├── collatz-conjecture │ └── 1 │ │ └── src │ │ └── main │ │ └── java │ │ └── CollatzCalculator.java ├── log-levels │ └── 1 │ │ └── src │ │ └── main │ │ └── java │ │ └── LogLevels.java ├── difference-of-squares │ ├── 1 │ │ └── src │ │ │ └── main │ │ │ └── java │ │ │ └── DifferenceOfSquaresCalculator.java │ └── 2 │ │ └── src │ │ └── main │ │ └── java │ │ └── DifferenceOfSquaresCalculator.java ├── cars-assemble │ └── 1 │ │ └── src │ │ └── main │ │ └── java │ │ └── CarsAssemble.java ├── captains-log │ └── 1 │ │ └── src │ │ └── main │ │ └── java │ │ └── CaptainsLog.java ├── lasagna │ ├── 1 │ │ └── src │ │ │ └── main │ │ │ └── java │ │ │ └── Lasagna.java │ ├── 2 │ │ └── src │ │ │ └── main │ │ │ └── java │ │ │ └── Lasagna.java │ └── 3 │ │ └── src │ │ └── main │ │ └── java │ │ └── Lasagna.java ├── jedliks-toy-car │ └── 1 │ │ └── src │ │ └── main │ │ └── java │ │ └── JedliksToyCar.java ├── salary-calculator │ └── 1 │ │ └── src │ │ └── main │ │ └── java │ │ └── SalaryCalculator.java ├── football-match-reports │ └── 1 │ │ └── src │ │ └── main │ │ └── java │ │ └── FootballMatchReports.java ├── annalyns-infiltration │ └── 1 │ │ └── src │ │ └── main │ │ └── java │ │ └── AnnalynsInfiltration.java ├── squeaky-clean │ └── 1 │ │ └── src │ │ └── main │ │ └── java │ │ └── SqueakyClean.java ├── calculator-conundrum │ └── 1 │ │ └── src │ │ └── main │ │ └── java │ │ └── CalculatorConundrum.java ├── karls-languages │ └── 1 │ │ └── src │ │ └── main │ │ └── java │ │ └── LanguageList.java ├── need-for-speed │ ├── 1 │ │ └── src │ │ │ └── main │ │ │ └── java │ │ │ └── NeedForSpeed.java │ └── 2 │ │ └── src │ │ └── main │ │ └── java │ │ └── NeedForSpeed.java ├── wizards-and-warriors │ └── 1 │ │ └── src │ │ └── main │ │ └── java │ │ └── Fighter.java ├── booking-up-for-beauty │ └── 1 │ │ └── src │ │ └── main │ │ └── java │ │ └── AppointmentScheduler.java ├── international-calling-connoisseur │ └── 1 │ │ └── src │ │ └── main │ │ └── java │ │ └── DialingCodes.java └── bird-watcher │ └── 1 │ └── src │ └── main │ └── java │ └── BirdWatcher.java ├── rust ├── leap │ └── 1 │ │ ├── src │ │ └── lib.rs │ │ └── Cargo.toml ├── hello-world │ └── 1 │ │ ├── src │ │ └── lib.rs │ │ └── Cargo.toml ├── difference-of-squares │ └── 1 │ │ ├── src │ │ └── lib.rs │ │ └── Cargo.toml ├── hamming │ └── 1 │ │ ├── src │ │ └── lib.rs │ │ └── Cargo.toml ├── sieve │ ├── 1 │ │ ├── Cargo.toml │ │ └── src │ │ │ └── lib.rs │ ├── 2 │ │ ├── Cargo.toml │ │ └── src │ │ │ └── lib.rs │ └── 3 │ │ ├── Cargo.toml │ │ └── src │ │ └── lib.rs ├── perfect-numbers │ └── 1 │ │ ├── Cargo.toml │ │ └── src │ │ └── lib.rs ├── scrabble-score │ └── 1 │ │ ├── Cargo.toml │ │ └── src │ │ └── lib.rs ├── armstrong-numbers │ └── 1 │ │ ├── Cargo.toml │ │ └── src │ │ └── lib.rs ├── collatz-conjecture │ ├── 1 │ │ ├── Cargo.toml │ │ └── src │ │ │ └── lib.rs │ └── 2 │ │ ├── Cargo.toml │ │ └── src │ │ └── lib.rs ├── rna-transcription │ └── 1 │ │ ├── Cargo.toml │ │ └── src │ │ └── lib.rs └── sum-of-multiples │ └── 1 │ ├── Cargo.toml │ └── src │ └── lib.rs ├── common-lisp ├── hello-world │ └── 1 │ │ └── hello-world.lisp ├── two-fer │ └── 1 │ │ └── two-fer.lisp ├── grains │ └── 1 │ │ └── grains.lisp ├── darts │ └── 1 │ │ └── darts.lisp ├── gigasecond-anniversary │ └── 1 │ │ └── gigasecond-anniversary.lisp ├── collatz-conjecture │ └── 1 │ │ └── collatz-conjecture.lisp ├── lucys-magnificent-mapper │ └── 1 │ │ └── lucys-magnificent-mapper.lisp ├── eliuds-eggs │ └── 1 │ │ └── eliuds-eggs.lisp ├── pangram │ └── 1 │ │ └── pangram.lisp ├── rna-transcription │ └── 1 │ │ └── rna-transcription.lisp ├── leap │ └── 1 │ │ └── leap.lisp ├── pythagorean-triplet │ ├── 1 │ │ └── pythagorean-triplet.lisp │ └── 2 │ │ └── pythagorean-triplet.lisp ├── hamming │ └── 1 │ │ └── hamming.lisp ├── perfect-numbers │ └── 1 │ │ └── perfect-numbers.lisp ├── sum-of-multiples │ └── 1 │ │ └── sum-of-multiples.lisp ├── logans-numeric-partition │ └── 1 │ │ └── logans-numeric-partition.lisp ├── resistor-color │ └── 1 │ │ └── resistor-color.lisp ├── nucleotide-count │ └── 1 │ │ └── nucleotide-count.lisp ├── pizza-pi │ └── 1 │ │ └── pizza-pi.lisp ├── armstrong-numbers │ └── 1 │ │ └── armstrong-numbers.lisp ├── difference-of-squares │ └── 1 │ │ └── difference-of-squares.lisp ├── sieve │ └── 1 │ │ └── sieve.lisp ├── prime-factors │ └── 1 │ │ └── prime-factors.lisp ├── triangle │ └── 1 │ │ └── triangle.lisp ├── strain │ └── 1 │ │ └── strain.lisp ├── log-levels │ └── 1 │ │ └── log-levels.lisp ├── high-scores │ └── 1 │ │ └── high-scores.lisp ├── isbn-verifier │ └── 1 │ │ └── isbn-verifier.lisp ├── binary-search │ └── 1 │ │ └── binary-search.lisp ├── space-age │ └── 1 │ │ └── space-age.lisp ├── character-study │ └── 1 │ │ └── character-study.lisp ├── resistor-color-duo │ └── 1 │ │ └── resistor-color-duo.lisp ├── socks-and-sexprs │ └── 1 │ │ └── socks-and-sexprs.lisp ├── reporting-for-duty │ └── 1 │ │ └── reporting-for-duty.lisp ├── pal-picker │ └── 1 │ │ └── pal-picker.lisp ├── larrys-winning-checker │ └── 1 │ │ └── larrys-winning-checker.lisp ├── leslies-lists │ └── 1 │ │ └── leslies-lists.lisp ├── lillys-lasagna │ └── 1 │ │ └── lillys-lasagna.lisp ├── lillys-lasagna-leftovers │ └── 1 │ │ └── lillys-lasagna-leftovers.lisp ├── key-comparison │ └── 1 │ │ └── key-comparison.lisp ├── allergies │ └── 1 │ │ └── allergies.lisp └── scrabble-score │ └── 1 │ └── scrabble-score.lisp ├── arm64-assembly ├── hello-world │ └── 1 │ │ └── hello_world.s ├── collatz-conjecture │ ├── 1 │ │ └── collatz_conjecture.s │ └── 2 │ │ └── collatz_conjecture.s ├── hamming │ └── 1 │ │ └── hamming.s ├── grains │ └── 1 │ │ └── grains.s ├── leap │ └── 1 │ │ └── leap.s ├── darts │ └── 1 │ │ └── darts.s ├── rna-transcription │ └── 1 │ │ └── rna_transcription.s └── difference-of-squares │ └── 1 │ └── difference_of_squares.s ├── emacs-lisp ├── hello-world │ └── 1 │ │ └── hello-world.el ├── leap │ └── 1 │ │ └── leap.el ├── two-fer │ └── 1 │ │ └── two-fer.el ├── accumulate │ └── 1 │ │ └── accumulate.el ├── darts │ └── 1 │ │ └── darts.el ├── resistor-color │ └── 1 │ │ └── resistor-color.el ├── armstrong-numbers │ └── 1 │ │ └── armstrong-numbers.el ├── acronym │ └── 1 │ │ └── acronym.el ├── difference-of-squares │ └── 1 │ │ └── difference-of-squares.el ├── hamming │ └── 1 │ │ └── hamming.el ├── collatz-conjecture │ └── 1 │ │ └── collatz-conjecture.el ├── allergies │ └── 1 │ │ └── allergies.el └── scrabble-score │ └── 1 │ └── scrabble-score.el └── x86-64-assembly ├── hello-world └── 1 │ └── hello_world.asm ├── collatz-conjecture └── 1 │ └── collatz_conjecture.asm ├── leap └── 1 │ └── leap.asm ├── lasagna └── 1 │ └── lasagna.asm └── inventory-management └── 1 └── inventory_management.asm /solutions/prolog/hello-world/1/hello_world.pl: -------------------------------------------------------------------------------- 1 | hello_world('Hello, World!'). 2 | -------------------------------------------------------------------------------- /solutions/python/hello-world/1/hello_world.py: -------------------------------------------------------------------------------- 1 | def hello(): 2 | return 'Hello, World!' 3 | -------------------------------------------------------------------------------- /solutions/julia/hello-world/1/hello-world.jl: -------------------------------------------------------------------------------- 1 | function hello() 2 | return "Hello, World!" 3 | end 4 | -------------------------------------------------------------------------------- /solutions/clojure/hello-world/1/src/hello_world.clj: -------------------------------------------------------------------------------- 1 | (ns hello-world) 2 | 3 | (defn hello [] 4 | "Hello, World!") 5 | -------------------------------------------------------------------------------- /solutions/kotlin/hello-world/1/src/main/kotlin/HelloWorld.kt: -------------------------------------------------------------------------------- 1 | fun hello(): String { 2 | return "Hello, World!" 3 | } 4 | -------------------------------------------------------------------------------- /solutions/typescript/hello-world/1/hello-world.ts: -------------------------------------------------------------------------------- 1 | export function hello(): string { 2 | return 'Hello, World!' 3 | } 4 | -------------------------------------------------------------------------------- /solutions/sqlite/hello-world/1/hello-world.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO 2 | hello_world (greeting) 3 | VALUES 4 | ('Hello, World!'); 5 | -------------------------------------------------------------------------------- /solutions/jq/hello-world/1/hello-world.jq: -------------------------------------------------------------------------------- 1 | # This is the simplest jq "filter" -- just output a constant string 2 | "Hello, World!" 3 | -------------------------------------------------------------------------------- /solutions/scala/hello-world/1/src/main/scala/HelloWorld.scala: -------------------------------------------------------------------------------- 1 | object HelloWorld { 2 | def hello() = "Hello, World!" 3 | } 4 | 5 | -------------------------------------------------------------------------------- /solutions/haskell/hello-world/1/src/HelloWorld.hs: -------------------------------------------------------------------------------- 1 | module HelloWorld (hello) where 2 | 3 | hello :: String 4 | hello = "Hello, World!" 5 | -------------------------------------------------------------------------------- /solutions/prolog/leap/1/leap.pl: -------------------------------------------------------------------------------- 1 | leap(X) :- 2 | 0 is X mod 4, 3 | ( 4 | N is X mod 100, 5 | N \= 0 6 | ; 0 is X mod 400 7 | ). 8 | -------------------------------------------------------------------------------- /solutions/typescript/leap/1/leap.ts: -------------------------------------------------------------------------------- 1 | export function isLeap(year: number) { 2 | return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0); 3 | } 4 | -------------------------------------------------------------------------------- /solutions/java/hello-world/1/src/main/java/Greeter.java: -------------------------------------------------------------------------------- 1 | class Greeter { 2 | 3 | String getGreeting() { 4 | return "Hello, World!"; 5 | } 6 | 7 | } 8 | -------------------------------------------------------------------------------- /solutions/rust/leap/1/src/lib.rs: -------------------------------------------------------------------------------- 1 | pub fn is_leap_year(year: u64) -> bool { 2 | year % 4 == 0 && ( 3 | year % 100 != 0 || year % 400 == 0 4 | ) 5 | } 6 | -------------------------------------------------------------------------------- /solutions/scala/leap/1/src/main/scala/Leap.scala: -------------------------------------------------------------------------------- 1 | object Leap { 2 | def leapYear(year: Int): Boolean = 3 | year % 4 == 0 && (year % 100 != 0 || year % 400 == 0) 4 | } 5 | -------------------------------------------------------------------------------- /solutions/java/remote-control-competition/1/src/main/java/RemoteControlCar.java: -------------------------------------------------------------------------------- 1 | public interface RemoteControlCar { 2 | void drive(); 3 | int getDistanceTravelled(); 4 | } 5 | -------------------------------------------------------------------------------- /solutions/kotlin/leap/1/src/main/kotlin/Leap.kt: -------------------------------------------------------------------------------- 1 | data class Year(val year: Int) { 2 | 3 | val isLeap: Boolean = year % 4 == 0 && (year % 100 != 0 || year % 400 == 0) 4 | } 5 | -------------------------------------------------------------------------------- /solutions/rust/hello-world/1/src/lib.rs: -------------------------------------------------------------------------------- 1 | // &'static is a "lifetime specifier", something you'll learn more about later 2 | pub fn hello() -> &'static str { 3 | "Hello, World!" 4 | } 5 | -------------------------------------------------------------------------------- /solutions/common-lisp/hello-world/1/hello-world.lisp: -------------------------------------------------------------------------------- 1 | (defpackage :hello-world 2 | (:use :cl) 3 | (:export :hello)) 4 | 5 | (in-package :hello-world) 6 | 7 | (defun hello () "Hello, World!") 8 | -------------------------------------------------------------------------------- /solutions/java/leap/1/src/main/java/Leap.java: -------------------------------------------------------------------------------- 1 | class Leap { 2 | 3 | boolean isLeapYear(int year) { 4 | return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0); 5 | } 6 | 7 | } 8 | -------------------------------------------------------------------------------- /solutions/haskell/leap/1/src/LeapYear.hs: -------------------------------------------------------------------------------- 1 | module LeapYear (isLeapYear) where 2 | 3 | isLeapYear :: Integer -> Bool 4 | isLeapYear year = year `mod` 4 == 0 && (year `mod` 100 /= 0 || year `mod` 400 == 0) 5 | -------------------------------------------------------------------------------- /solutions/haskell/accumulate/1/src/Accumulate.hs: -------------------------------------------------------------------------------- 1 | module Accumulate (accumulate) where 2 | 3 | accumulate :: (a -> b) -> [a] -> [b] 4 | accumulate _ [] = [] 5 | accumulate f (x:xs) = (f x):(accumulate f xs) 6 | -------------------------------------------------------------------------------- /solutions/haskell/reverse-string/2/src/ReverseString.hs: -------------------------------------------------------------------------------- 1 | module ReverseString (reverseString) where 2 | 3 | reverseString :: String -> String 4 | reverseString [] = [] 5 | reverseString (x:xs) = reverseString xs ++ [x] 6 | -------------------------------------------------------------------------------- /solutions/scala/two-fer/1/src/main/scala/Twofer.scala: -------------------------------------------------------------------------------- 1 | object Twofer { 2 | def twofer(): String = "One for you, one for me." 3 | 4 | def twofer(name: String): String = 5 | s"One for $name, one for me." 6 | } 7 | -------------------------------------------------------------------------------- /solutions/haskell/reverse-string/1/src/ReverseString.hs: -------------------------------------------------------------------------------- 1 | module ReverseString (reverseString) where 2 | 3 | reverseString :: String -> String 4 | reverseString [] = [] 5 | reverseString (x:xs) = (reverseString xs) ++ [x] 6 | -------------------------------------------------------------------------------- /solutions/typescript/two-fer/1/two-fer.ts: -------------------------------------------------------------------------------- 1 | export function twoFer(name?: string): string { 2 | if (typeof name !== "undefined") { 3 | return `One for ${name}, one for me.`; 4 | } 5 | return "One for you, one for me."; 6 | } 7 | -------------------------------------------------------------------------------- /solutions/arm64-assembly/hello-world/1/hello_world.s: -------------------------------------------------------------------------------- 1 | .section .rodata 2 | msg: .string "Hello, World!" 3 | 4 | .text 5 | .global hello 6 | hello: 7 | adrp x0, msg 8 | add x0, x0, :lo12:msg 9 | ret 10 | -------------------------------------------------------------------------------- /solutions/emacs-lisp/hello-world/1/hello-world.el: -------------------------------------------------------------------------------- 1 | ;;; hello-world.el --- Hello World exercise (exercism) -*- lexical-binding: t; -*- 2 | 3 | ;;; Commentary: 4 | 5 | (defun hello () 6 | "Hello, World!") 7 | 8 | (provide 'hello-world) 9 | ;;; hello-world.el ends here 10 | -------------------------------------------------------------------------------- /solutions/julia/chessboard/1/chessboard.jl: -------------------------------------------------------------------------------- 1 | function rank_range() 2 | 1:8 3 | end 4 | 5 | function file_range() 6 | 'A':'H' 7 | end 8 | 9 | function ranks() 10 | collect(rank_range()) 11 | end 12 | 13 | function files() 14 | collect(file_range()) 15 | end 16 | -------------------------------------------------------------------------------- /solutions/common-lisp/two-fer/1/two-fer.lisp: -------------------------------------------------------------------------------- 1 | (defpackage :two-fer 2 | (:use :cl) 3 | (:export :twofer)) 4 | (in-package :two-fer) 5 | 6 | (defun twofer (&optional name) 7 | (if name 8 | (format nil "One for ~a, one for me." name) 9 | "One for you, one for me.")) 10 | -------------------------------------------------------------------------------- /solutions/haskell/hamming/1/src/Hamming.hs: -------------------------------------------------------------------------------- 1 | module Hamming (distance) where 2 | 3 | distance :: String -> String -> Maybe Int 4 | distance xs ys = 5 | if (length xs) == (length ys) then 6 | Just $ length $ filter (\(x,y) -> x /= y ) $ zip xs ys 7 | else 8 | Nothing 9 | 10 | -------------------------------------------------------------------------------- /solutions/haskell/hamming/2/src/Hamming.hs: -------------------------------------------------------------------------------- 1 | module Hamming (distance) where 2 | 3 | distance :: String -> String -> Maybe Int 4 | distance xs ys = 5 | if (length xs) == (length ys) then 6 | Just $ length . filter (\(x,y) -> x /= y ) $ zip xs ys 7 | else 8 | Nothing 9 | 10 | -------------------------------------------------------------------------------- /solutions/java/two-fer/1/src/main/java/Twofer.java: -------------------------------------------------------------------------------- 1 | public class Twofer { 2 | public String twofer(String name) { 3 | if(name == null) { 4 | return "One for you, one for me."; 5 | } 6 | 7 | return "One for " + name + ", one for me."; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /solutions/scala/pangram/1/src/main/scala/Pangrams.scala: -------------------------------------------------------------------------------- 1 | object Pangrams { 2 | def isPangram(input: String): Boolean = { 3 | val letters = input 4 | .toUpperCase 5 | .filter(_.isLetter) 6 | .toSet 7 | .size 8 | letters == 26 9 | } 10 | } 11 | 12 | -------------------------------------------------------------------------------- /solutions/scala/darts/1/src/main/scala/Darts.scala: -------------------------------------------------------------------------------- 1 | import scala.math.sqrt 2 | 3 | object Darts { 4 | def score(x: Double, y: Double): Int = { 5 | val dist = sqrt(x * x + y * y) 6 | 7 | if(dist > 10) 0 8 | else if(dist > 5) 1 9 | else if(dist > 1) 5 10 | else 10 11 | } 12 | } -------------------------------------------------------------------------------- /solutions/clojure/leap/1/src/leap.clj: -------------------------------------------------------------------------------- 1 | (ns leap) 2 | 3 | (defn leap-year? 4 | "Returns true if the given year is a leap year; 5 | otherwise, it returns false." 6 | [year] 7 | (and 8 | (zero? (mod year 4)) 9 | (or 10 | (not (zero? (mod year 100))) 11 | (zero? (mod year 400)))) 12 | ) 13 | -------------------------------------------------------------------------------- /solutions/common-lisp/grains/1/grains.lisp: -------------------------------------------------------------------------------- 1 | (defpackage :grains 2 | (:use :cl) 3 | (:export :square :total)) 4 | (in-package :grains) 5 | 6 | (defun square (n) 7 | (if (= n 1) 8 | 1 9 | (* 2 (square (- n 1))))) 10 | 11 | (defun total () 12 | (loop for i from 1 to 64 13 | sum (square i))) 14 | -------------------------------------------------------------------------------- /solutions/haskell/grains/1/src/Grains.hs: -------------------------------------------------------------------------------- 1 | module Grains (square, total) where 2 | 3 | import Data.Maybe (mapMaybe) 4 | 5 | square :: Integer -> Maybe Integer 6 | square n 7 | | n < 1 = Nothing 8 | | n > 64 = Nothing 9 | | otherwise = Just (2 ^ (n - 1)) 10 | 11 | total :: Integer 12 | total = sum $ mapMaybe square [1..64] 13 | -------------------------------------------------------------------------------- /solutions/x86-64-assembly/hello-world/1/hello_world.asm: -------------------------------------------------------------------------------- 1 | default rel 2 | 3 | section .rodata 4 | msg: db "Hello, World!", 0 5 | 6 | section .text 7 | global hello 8 | hello: 9 | lea rax, [msg] 10 | ret 11 | 12 | %ifidn __OUTPUT_FORMAT__,elf64 13 | section .note.GNU-stack noalloc noexec nowrite progbits 14 | %endif 15 | -------------------------------------------------------------------------------- /solutions/rust/difference-of-squares/1/src/lib.rs: -------------------------------------------------------------------------------- 1 | pub fn square_of_sum(n: u32) -> u32 { 2 | let s: u32 = (1..=n).sum(); 3 | s * s 4 | } 5 | 6 | pub fn sum_of_squares(n: u32) -> u32 { 7 | (1..=n).map(|x| x * x).sum() 8 | } 9 | 10 | pub fn difference(n: u32) -> u32 { 11 | square_of_sum(n) - sum_of_squares(n) 12 | } 13 | -------------------------------------------------------------------------------- /solutions/typescript/resistor-color/1/resistor-color.ts: -------------------------------------------------------------------------------- 1 | export const colorCode = (code: string) => { 2 | return COLORS.indexOf(code) 3 | } 4 | 5 | export const COLORS = [ 6 | "black", 7 | "brown", 8 | "red", 9 | "orange", 10 | "yellow", 11 | "green", 12 | "blue", 13 | "violet", 14 | "grey", 15 | "white" 16 | ] 17 | -------------------------------------------------------------------------------- /solutions/rust/hamming/1/src/lib.rs: -------------------------------------------------------------------------------- 1 | pub fn hamming_distance(s1: &str, s2: &str) -> Option { 2 | if s1.len() != s2.len() { 3 | return None 4 | } 5 | let diffs = s1 6 | .chars() 7 | .zip(s2.chars()) 8 | .filter(|(a,b)| a != b) 9 | .count(); 10 | 11 | Some(diffs) 12 | } 13 | -------------------------------------------------------------------------------- /solutions/emacs-lisp/leap/1/leap.el: -------------------------------------------------------------------------------- 1 | ;;; leap.el --- Leap exercise (exercism) -*- lexical-binding: t; -*- 2 | 3 | ;;; Commentary: 4 | 5 | (defun leap-year-p (year) 6 | (and (zerop (mod year 4)) 7 | (or (not (zerop (mod year 100))) 8 | (zerop (mod year 400))))) 9 | 10 | (provide 'leap-year-p) 11 | ;;; leap.el ends here 12 | -------------------------------------------------------------------------------- /solutions/julia/darts/1/darts.jl: -------------------------------------------------------------------------------- 1 | function score(x, y) 2 | d = dist_to_center(x, y) 3 | if d <= 1 4 | return 10 5 | elseif d <= 5 6 | return 5 7 | elseif d <= 10 8 | return 1 9 | else 10 | return 0 11 | end 12 | end 13 | 14 | function dist_to_center(x, y) 15 | sqrt(x ^ 2 + y ^ 2) 16 | end -------------------------------------------------------------------------------- /solutions/emacs-lisp/two-fer/1/two-fer.el: -------------------------------------------------------------------------------- 1 | ;;; two-fer.el --- Two-fer Exercise (exercism) -*- lexical-binding: t; -*- 2 | 3 | ;;; Commentary: 4 | 5 | (defun two-fer (&optional name) 6 | (if name 7 | (format "One for %s, one for me." name) 8 | "One for you, one for me.")) 9 | 10 | (provide 'two-fer) 11 | ;;; two-fer.el ends here 12 | -------------------------------------------------------------------------------- /solutions/rust/leap/1/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "leap" 3 | version = "0.1.0" 4 | edition = "2024" 5 | 6 | # Not all libraries from crates.io are available in Exercism's test runner. 7 | # The full list of available libraries is here: 8 | # https://github.com/exercism/rust-test-runner/blob/main/local-registry/Cargo.toml 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /solutions/rust/sieve/1/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "sieve" 3 | version = "0.1.0" 4 | edition = "2024" 5 | 6 | # Not all libraries from crates.io are available in Exercism's test runner. 7 | # The full list of available libraries is here: 8 | # https://github.com/exercism/rust-test-runner/blob/main/local-registry/Cargo.toml 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /solutions/rust/sieve/2/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "sieve" 3 | version = "0.1.0" 4 | edition = "2024" 5 | 6 | # Not all libraries from crates.io are available in Exercism's test runner. 7 | # The full list of available libraries is here: 8 | # https://github.com/exercism/rust-test-runner/blob/main/local-registry/Cargo.toml 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /solutions/rust/sieve/3/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "sieve" 3 | version = "0.1.0" 4 | edition = "2024" 5 | 6 | # Not all libraries from crates.io are available in Exercism's test runner. 7 | # The full list of available libraries is here: 8 | # https://github.com/exercism/rust-test-runner/blob/main/local-registry/Cargo.toml 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /solutions/haskell/darts/2/src/Darts.hs: -------------------------------------------------------------------------------- 1 | module Darts (score) where 2 | 3 | score :: Float -> Float -> Int 4 | score x y 5 | | d > 10 = 0 6 | | d > 5 = 1 7 | | d > 1 = 5 8 | | otherwise = 10 9 | where 10 | d = distanceToCenter x y 11 | 12 | distanceToCenter :: Float -> Float -> Float 13 | distanceToCenter x y = sqrt ((x * x) + (y * y)) 14 | -------------------------------------------------------------------------------- /solutions/rust/hamming/1/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "hamming" 3 | version = "0.1.0" 4 | edition = "2024" 5 | 6 | # Not all libraries from crates.io are available in Exercism's test runner. 7 | # The full list of available libraries is here: 8 | # https://github.com/exercism/rust-test-runner/blob/main/local-registry/Cargo.toml 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /solutions/haskell/collatz-conjecture/1/src/CollatzConjecture.hs: -------------------------------------------------------------------------------- 1 | module CollatzConjecture (collatz) where 2 | 3 | collatz :: Integer -> Maybe Integer 4 | collatz 1 = Just 0 5 | collatz n = 6 | if n > 1 then 7 | if (even n) then 8 | fmap (+1) $ collatz (n `div` 2) 9 | else 10 | fmap (+1) $ collatz (1 + (n * 3)) 11 | else 12 | Nothing 13 | -------------------------------------------------------------------------------- /solutions/rust/hello-world/1/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "hello_world" 3 | version = "0.1.0" 4 | edition = "2024" 5 | 6 | # Not all libraries from crates.io are available in Exercism's test runner. 7 | # The full list of available libraries is here: 8 | # https://github.com/exercism/rust-test-runner/blob/main/local-registry/Cargo.toml 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /solutions/julia/collatz-conjecture/1/collatz-conjecture.jl: -------------------------------------------------------------------------------- 1 | function collatz_steps(n) 2 | if n < 1 3 | throw(DomainError(n)) 4 | end 5 | 6 | steps = 0 7 | while n > 1 8 | if n % 2 == 0 9 | n = n / 2 10 | else 11 | n = 3n + 1 12 | end 13 | steps += 1 14 | end 15 | steps 16 | end 17 | -------------------------------------------------------------------------------- /solutions/rust/perfect-numbers/1/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "perfect_numbers" 3 | version = "0.1.0" 4 | edition = "2024" 5 | 6 | # Not all libraries from crates.io are available in Exercism's test runner. 7 | # The full list of available libraries is here: 8 | # https://github.com/exercism/rust-test-runner/blob/main/local-registry/Cargo.toml 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /solutions/rust/scrabble-score/1/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "scrabble_score" 3 | version = "0.1.0" 4 | edition = "2024" 5 | 6 | # Not all libraries from crates.io are available in Exercism's test runner. 7 | # The full list of available libraries is here: 8 | # https://github.com/exercism/rust-test-runner/blob/main/local-registry/Cargo.toml 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /solutions/emacs-lisp/accumulate/1/accumulate.el: -------------------------------------------------------------------------------- 1 | ;;; accumulate.el --- Accumulate (exercism) -*- lexical-binding: t; -*- 2 | 3 | ;;; Commentary: 4 | 5 | ;;; Code: 6 | 7 | 8 | (defun accumulate (lst op) 9 | (when lst 10 | (cons (funcall op (car lst)) (accumulate (cdr lst) op)))) 11 | 12 | 13 | 14 | (provide 'accumulate) 15 | ;;; accumulate.el ends here 16 | -------------------------------------------------------------------------------- /solutions/java/remote-control-competition/1/src/main/java/ExperimentalRemoteControlCar.java: -------------------------------------------------------------------------------- 1 | public class ExperimentalRemoteControlCar implements RemoteControlCar{ 2 | 3 | private int distance = 0; 4 | 5 | public void drive() { 6 | distance += 20; 7 | } 8 | 9 | public int getDistanceTravelled() { 10 | return distance; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /solutions/rust/armstrong-numbers/1/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "armstrong_numbers" 3 | version = "0.1.0" 4 | edition = "2024" 5 | 6 | # Not all libraries from crates.io are available in Exercism's test runner. 7 | # The full list of available libraries is here: 8 | # https://github.com/exercism/rust-test-runner/blob/main/local-registry/Cargo.toml 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /solutions/rust/collatz-conjecture/1/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "collatz_conjecture" 3 | version = "0.1.0" 4 | edition = "2024" 5 | 6 | # Not all libraries from crates.io are available in Exercism's test runner. 7 | # The full list of available libraries is here: 8 | # https://github.com/exercism/rust-test-runner/blob/main/local-registry/Cargo.toml 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /solutions/rust/collatz-conjecture/2/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "collatz_conjecture" 3 | version = "0.1.0" 4 | edition = "2024" 5 | 6 | # Not all libraries from crates.io are available in Exercism's test runner. 7 | # The full list of available libraries is here: 8 | # https://github.com/exercism/rust-test-runner/blob/main/local-registry/Cargo.toml 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /solutions/rust/rna-transcription/1/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "rna_transcription" 3 | version = "0.1.0" 4 | edition = "2024" 5 | 6 | # Not all libraries from crates.io are available in Exercism's test runner. 7 | # The full list of available libraries is here: 8 | # https://github.com/exercism/rust-test-runner/blob/main/local-registry/Cargo.toml 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /solutions/rust/sum-of-multiples/1/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "sum_of_multiples" 3 | version = "0.1.0" 4 | edition = "2024" 5 | 6 | # Not all libraries from crates.io are available in Exercism's test runner. 7 | # The full list of available libraries is here: 8 | # https://github.com/exercism/rust-test-runner/blob/main/local-registry/Cargo.toml 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /solutions/rust/difference-of-squares/1/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "difference_of_squares" 3 | version = "0.1.0" 4 | edition = "2024" 5 | 6 | # Not all libraries from crates.io are available in Exercism's test runner. 7 | # The full list of available libraries is here: 8 | # https://github.com/exercism/rust-test-runner/blob/main/local-registry/Cargo.toml 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /solutions/rust/sieve/1/src/lib.rs: -------------------------------------------------------------------------------- 1 | use std::collections::VecDeque; 2 | 3 | pub fn primes_up_to(upper_bound: u64) -> Vec { 4 | let mut nums: VecDeque = (2..=upper_bound).collect(); 5 | let mut primes = vec![]; 6 | 7 | while let Some(prime) = nums.pop_front() { 8 | primes.push(prime); 9 | nums.retain(|x| x % prime != 0); 10 | } 11 | primes 12 | } 13 | -------------------------------------------------------------------------------- /solutions/emacs-lisp/darts/1/darts.el: -------------------------------------------------------------------------------- 1 | ;;; darts.el --- Darts (exercism) -*- lexical-binding: t; -*- 2 | 3 | ;;; Commentary: 4 | 5 | ;;; Code: 6 | 7 | 8 | (defun score (x y) 9 | (let ((dist (sqrt (+ (* x x) (* y y))))) 10 | (cond 11 | ((> dist 10) 0) 12 | ((> dist 5) 1) 13 | ((> dist 1) 5) 14 | (t 10)))) 15 | 16 | 17 | (provide 'darts) 18 | ;;; darts.el ends here 19 | -------------------------------------------------------------------------------- /solutions/julia/pangram/1/pangram.jl: -------------------------------------------------------------------------------- 1 | """ 2 | ispangram(input) 3 | 4 | Return `true` if `input` contains every alphabetic character (case insensitive). 5 | 6 | """ 7 | function ispangram(input) 8 | letters = Set() 9 | for c in uppercase(input) 10 | if 'A' <= c <= 'Z' 11 | push!(letters, c) 12 | end 13 | end 14 | length(letters) == 26 15 | end 16 | 17 | -------------------------------------------------------------------------------- /solutions/common-lisp/darts/1/darts.lisp: -------------------------------------------------------------------------------- 1 | (defpackage :darts 2 | (:use :cl) 3 | (:export :score)) 4 | 5 | (in-package :darts) 6 | 7 | (defun score (x y) 8 | (let ((d (distance-to-center x y))) 9 | (cond 10 | ((<= d 1) 10) 11 | ((<= d 5) 5) 12 | ((<= d 10) 1) 13 | (t 0)))) 14 | 15 | (defun distance-to-center (x y) 16 | (sqrt (+ (* x x) (* y y)))) 17 | 18 | -------------------------------------------------------------------------------- /solutions/common-lisp/gigasecond-anniversary/1/gigasecond-anniversary.lisp: -------------------------------------------------------------------------------- 1 | (defpackage :gigasecond-anniversary 2 | (:use :cl) 3 | (:export :from)) 4 | (in-package :gigasecond-anniversary) 5 | 6 | (defun from (year month day hour minute second) 7 | (multiple-value-bind (a b c d e f) (decode-universal-time (+ 1000000000 (encode-universal-time second minute hour day month year))) 8 | (list f e d c b a))) 9 | -------------------------------------------------------------------------------- /solutions/common-lisp/collatz-conjecture/1/collatz-conjecture.lisp: -------------------------------------------------------------------------------- 1 | (defpackage :collatz-conjecture 2 | (:use :cl) 3 | (:export :collatz)) 4 | 5 | (in-package :collatz-conjecture) 6 | 7 | (defun collatz (n) 8 | (when (> n 0) 9 | (if (= n 1) 10 | 0 11 | (+ 1 (if (evenp n) 12 | (collatz (/ n 2)) 13 | (collatz (+ 1 (* n 3)))))))) 14 | 15 | 16 | -------------------------------------------------------------------------------- /solutions/scala/triangle/1/src/main/scala/Triangle.scala: -------------------------------------------------------------------------------- 1 | case class Triangle(a: Double, b: Double, c: Double) { 2 | private def validTriangle = 3 | a + b >= c && b + c > a && a + c > b 4 | 5 | def equilateral: Boolean = this.validTriangle && a == b && b == c 6 | 7 | def isosceles: Boolean = this.validTriangle && (a == b || a == c || b == c) 8 | 9 | def scalene: Boolean = this.validTriangle && a != b && b != c 10 | } -------------------------------------------------------------------------------- /solutions/haskell/darts/1/src/Darts.hs: -------------------------------------------------------------------------------- 1 | module Darts (score) where 2 | 3 | score :: Float -> Float -> Int 4 | score x y = 5 | if d > 10 then 6 | 0 7 | else 8 | if d > 5 then 9 | 1 10 | else 11 | if d > 1 then 12 | 5 13 | else 14 | 10 15 | where 16 | d = distanceToCenter x y 17 | 18 | distanceToCenter :: Float -> Float -> Float 19 | distanceToCenter x y = sqrt ((x * x) + (y * y)) 20 | -------------------------------------------------------------------------------- /solutions/scala/hamming/1/src/main/scala/Hamming.scala: -------------------------------------------------------------------------------- 1 | object Hamming { 2 | def distance(dnaStrandOne: String, dnaStrandTwo: String): Option[Int] = { 3 | if (dnaStrandOne.size != dnaStrandTwo.size) { 4 | None 5 | } else { 6 | val count = dnaStrandOne 7 | .zip(dnaStrandTwo) 8 | .filter(p => p._1 != p._2) 9 | .size 10 | Some(count) 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /solutions/common-lisp/lucys-magnificent-mapper/1/lucys-magnificent-mapper.lisp: -------------------------------------------------------------------------------- 1 | (defpackage :lucys-magnificent-mapper 2 | (:use :cl) 3 | (:export :make-magnificent-maybe :only-the-best)) 4 | 5 | (in-package :lucys-magnificent-mapper) 6 | 7 | ;; Define make-magnificent-maybe function 8 | (defun make-magnificent-maybe (f l) 9 | (mapcar f l)) 10 | 11 | ;; Define only-the-best function 12 | (defun only-the-best (f l) 13 | (remove 1 (remove-if f l))) -------------------------------------------------------------------------------- /solutions/emacs-lisp/resistor-color/1/resistor-color.el: -------------------------------------------------------------------------------- 1 | ;;; resistor-color.el --- Resistor Color (exercism) -*- lexical-binding: t; -*- 2 | 3 | ;;; Commentary: 4 | 5 | ;;; Code: 6 | 7 | (defun color-code (color) 8 | (seq-position (colors) color)) 9 | 10 | (defun colors () 11 | '("black" "brown" "red" "orange" "yellow" "green" "blue" "violet" "grey" "white")) 12 | 13 | 14 | (provide 'resistor-color) 15 | ;;; resistor-color.el ends here 16 | 17 | -------------------------------------------------------------------------------- /solutions/scala/difference-of-squares/1/src/main/scala/DifferenceOfSquares.scala: -------------------------------------------------------------------------------- 1 | object DifferenceOfSquares { 2 | 3 | def sumOfSquares(n: Int): Int = { 4 | (1 to n) 5 | .map(x => x * x) 6 | .reduce(_ + _) 7 | } 8 | 9 | def squareOfSum(n: Int): Int = { 10 | val s = (1 to n).reduce(_ + _) 11 | s * s 12 | } 13 | 14 | def differenceOfSquares(n: Int): Int = { 15 | squareOfSum(n) - sumOfSquares(n) 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /solutions/rust/collatz-conjecture/2/src/lib.rs: -------------------------------------------------------------------------------- 1 | pub fn collatz(n: u64) -> Option { 2 | if n < 1 { 3 | None 4 | } else { 5 | let mut iterations = 0; 6 | let mut n = n; 7 | while n > 1 { 8 | if n % 2 == 0 { 9 | n /= 2; 10 | } else { 11 | n = (n * 3) + 1; 12 | } 13 | iterations += 1; 14 | } 15 | Some(iterations) 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /solutions/haskell/darts/1/package.yaml: -------------------------------------------------------------------------------- 1 | name: darts 2 | version: 1.0.0.0 3 | 4 | dependencies: 5 | - base 6 | 7 | library: 8 | exposed-modules: Darts 9 | source-dirs: src 10 | ghc-options: -Wall 11 | # dependencies: 12 | # - foo # List here the packages you 13 | # - bar # want to use in your solution. 14 | 15 | tests: 16 | test: 17 | main: Tests.hs 18 | source-dirs: test 19 | dependencies: 20 | - darts 21 | - hspec 22 | -------------------------------------------------------------------------------- /solutions/haskell/darts/2/package.yaml: -------------------------------------------------------------------------------- 1 | name: darts 2 | version: 1.0.0.0 3 | 4 | dependencies: 5 | - base 6 | 7 | library: 8 | exposed-modules: Darts 9 | source-dirs: src 10 | ghc-options: -Wall 11 | # dependencies: 12 | # - foo # List here the packages you 13 | # - bar # want to use in your solution. 14 | 15 | tests: 16 | test: 17 | main: Tests.hs 18 | source-dirs: test 19 | dependencies: 20 | - darts 21 | - hspec 22 | -------------------------------------------------------------------------------- /solutions/haskell/leap/1/package.yaml: -------------------------------------------------------------------------------- 1 | name: leap 2 | version: 1.6.0.10 3 | 4 | dependencies: 5 | - base 6 | 7 | library: 8 | exposed-modules: LeapYear 9 | source-dirs: src 10 | ghc-options: -Wall 11 | # dependencies: 12 | # - foo # List here the packages you 13 | # - bar # want to use in your solution. 14 | 15 | tests: 16 | test: 17 | main: Tests.hs 18 | source-dirs: test 19 | dependencies: 20 | - leap 21 | - hspec 22 | -------------------------------------------------------------------------------- /solutions/haskell/sieve/1/package.yaml: -------------------------------------------------------------------------------- 1 | name: sieve 2 | version: 1.1.0.5 3 | 4 | dependencies: 5 | - base 6 | 7 | library: 8 | exposed-modules: Sieve 9 | source-dirs: src 10 | ghc-options: -Wall 11 | # dependencies: 12 | # - foo # List here the packages you 13 | # - bar # want to use in your solution. 14 | 15 | tests: 16 | test: 17 | main: Tests.hs 18 | source-dirs: test 19 | dependencies: 20 | - sieve 21 | - hspec 22 | -------------------------------------------------------------------------------- /solutions/common-lisp/eliuds-eggs/1/eliuds-eggs.lisp: -------------------------------------------------------------------------------- 1 | (defpackage :eliuds-eggs 2 | (:use :cl) 3 | (:export :egg-count)) 4 | 5 | (in-package :eliuds-eggs) 6 | 7 | (defun egg-count (number) 8 | (let ((zeros 0) 9 | (n number)) 10 | (loop while (> n 1) 11 | do (multiple-value-bind (d r) (floor n 2) 12 | (setf n d) 13 | (setf zeros (+ zeros r))) 14 | finally (return (+ zeros n))))) 15 | 16 | -------------------------------------------------------------------------------- /solutions/common-lisp/pangram/1/pangram.lisp: -------------------------------------------------------------------------------- 1 | (defpackage :pangram 2 | (:use :cl) 3 | (:export :pangramp)) 4 | 5 | (in-package :pangram) 6 | 7 | (defun pangramp (sentence) 8 | (loop for c across (string-upcase sentence) 9 | with set = (list) 10 | do (when (and (>= (char-code c) (char-code #\A)) 11 | (<= (char-code c) (char-code #\Z))) 12 | (setf set (adjoin c set))) 13 | finally (return (= (length set) 26)))) 14 | -------------------------------------------------------------------------------- /solutions/common-lisp/rna-transcription/1/rna-transcription.lisp: -------------------------------------------------------------------------------- 1 | (defpackage :rna-transcription 2 | (:use :cl) 3 | (:export :to-rna)) 4 | (in-package :rna-transcription) 5 | 6 | (defun to-rna (str) 7 | "Transcribe a string representing DNA nucleotides to RNA." 8 | (coerce (mapcar #'complement-nucleotide (coerce str 'list)) 'string)) 9 | 10 | (defun complement-nucleotide (n) 11 | (case n 12 | (#\G #\C) 13 | (#\C #\G) 14 | (#\T #\A) 15 | (#\A #\U))) 16 | -------------------------------------------------------------------------------- /solutions/haskell/grains/1/package.yaml: -------------------------------------------------------------------------------- 1 | name: grains 2 | version: 1.2.0.6 3 | 4 | dependencies: 5 | - base 6 | 7 | library: 8 | exposed-modules: Grains 9 | source-dirs: src 10 | ghc-options: -Wall 11 | # dependencies: 12 | # - foo # List here the packages you 13 | # - bar # want to use in your solution. 14 | 15 | tests: 16 | test: 17 | main: Tests.hs 18 | source-dirs: test 19 | dependencies: 20 | - grains 21 | - hspec 22 | -------------------------------------------------------------------------------- /solutions/common-lisp/leap/1/leap.lisp: -------------------------------------------------------------------------------- 1 | (defpackage :leap 2 | (:use :cl) 3 | (:export :leap-year-p)) 4 | 5 | (in-package :leap) 6 | 7 | (defun leap-year-p (year) 8 | (let ((divisible-by-4 (= (mod year 4) 0)) 9 | (divisible-by-100 (= (mod year 100) 0)) 10 | (divisible-by-400 (= (mod year 400) 0))) 11 | (when divisible-by-4 12 | (if divisible-by-100 13 | (if divisible-by-400 t nil) 14 | t)))) 15 | 16 | 17 | -------------------------------------------------------------------------------- /solutions/haskell/hamming/1/package.yaml: -------------------------------------------------------------------------------- 1 | name: hamming 2 | version: 2.3.0.10 3 | 4 | dependencies: 5 | - base 6 | 7 | library: 8 | exposed-modules: Hamming 9 | source-dirs: src 10 | ghc-options: -Wall 11 | # dependencies: 12 | # - foo # List here the packages you 13 | # - bar # want to use in your solution. 14 | 15 | tests: 16 | test: 17 | main: Tests.hs 18 | source-dirs: test 19 | dependencies: 20 | - hamming 21 | - hspec 22 | -------------------------------------------------------------------------------- /solutions/haskell/hamming/2/package.yaml: -------------------------------------------------------------------------------- 1 | name: hamming 2 | version: 2.3.0.10 3 | 4 | dependencies: 5 | - base 6 | 7 | library: 8 | exposed-modules: Hamming 9 | source-dirs: src 10 | ghc-options: -Wall 11 | # dependencies: 12 | # - foo # List here the packages you 13 | # - bar # want to use in your solution. 14 | 15 | tests: 16 | test: 17 | main: Tests.hs 18 | source-dirs: test 19 | dependencies: 20 | - hamming 21 | - hspec 22 | -------------------------------------------------------------------------------- /solutions/java/remote-control-competition/1/src/main/java/TestTrack.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | import java.util.Collections; 3 | 4 | public class TestTrack { 5 | 6 | public static void race(RemoteControlCar car) { 7 | car.drive(); 8 | } 9 | 10 | public static List getRankedCars(List cars) { 11 | Collections.sort(cars, Collections.reverseOrder()); 12 | return cars; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /solutions/kotlin/darts/1/src/main/kotlin/Darts.kt: -------------------------------------------------------------------------------- 1 | object Darts { 2 | 3 | fun score(x: Number, y: Number): Int { 4 | val dX = x.toDouble() 5 | val dY = y.toDouble() 6 | val dist = Math.sqrt((dX * dX) + (dY * dY)) 7 | 8 | return if (dist > 10) { 9 | 0 10 | } else if(dist > 5) { 11 | 1 12 | } else if(dist > 1) { 13 | 5 14 | } else { 15 | 10 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /solutions/rust/collatz-conjecture/1/src/lib.rs: -------------------------------------------------------------------------------- 1 | pub fn collatz(n: u64) -> Option { 2 | if n < 1 { 3 | None 4 | } else { 5 | let mut iterations = 0; 6 | let mut n = n; 7 | while n > 1 { 8 | if n % 2 == 0 { 9 | n = n / 2; 10 | } else { 11 | n = (n * 3) + 1; 12 | } 13 | iterations = iterations + 1; 14 | } 15 | Some(iterations) 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /solutions/haskell/difference-of-squares/1/src/Squares.hs: -------------------------------------------------------------------------------- 1 | module Squares (difference, squareOfSum, sumOfSquares) where 2 | 3 | difference :: Integral a => a -> a 4 | difference n = (squareOfSum n) - (sumOfSquares n) 5 | 6 | squareOfSum :: Integral a => a -> a 7 | squareOfSum n = 8 | let 9 | sumToN = sum [1..n] 10 | in 11 | sumToN * sumToN 12 | 13 | sumOfSquares :: Integral a => a -> a 14 | sumOfSquares n = sum $ map square [1..n] 15 | where 16 | square x = x * x 17 | -------------------------------------------------------------------------------- /solutions/julia/leap/1/leap.jl: -------------------------------------------------------------------------------- 1 | """ 2 | is_leap_year(year) 3 | 4 | Return `true` if `year` is a leap year in the gregorian calendar. 5 | 6 | """ 7 | function is_leap_year(year) 8 | if year % 4 == 0 9 | if year % 100 == 0 10 | if year % 400 == 0 11 | true 12 | else 13 | false 14 | end 15 | else 16 | true 17 | end 18 | else 19 | false 20 | end 21 | end 22 | 23 | -------------------------------------------------------------------------------- /solutions/scala/collatz-conjecture/1/src/main/scala/CollatzConjecture.scala: -------------------------------------------------------------------------------- 1 | object CollatzConjecture { 2 | def steps(number: Int): Option[Int] = { 3 | var n = number 4 | if (n > 0) { 5 | var steps = 0 6 | while (n > 1) { 7 | if (n % 2 == 0) { 8 | n /= 2 9 | } else { 10 | n *= 3 11 | n += 1 12 | } 13 | steps += 1 14 | } 15 | Some(steps) 16 | } else { 17 | None 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /solutions/haskell/accumulate/1/package.yaml: -------------------------------------------------------------------------------- 1 | name: accumulate 2 | version: 0.1.0.3 3 | 4 | dependencies: 5 | - base 6 | 7 | library: 8 | exposed-modules: Accumulate 9 | source-dirs: src 10 | ghc-options: -Wall 11 | # dependencies: 12 | # - foo # List here the packages you 13 | # - bar # want to use in your solution. 14 | 15 | tests: 16 | test: 17 | main: Tests.hs 18 | source-dirs: test 19 | dependencies: 20 | - accumulate 21 | - hspec 22 | -------------------------------------------------------------------------------- /solutions/haskell/hello-world/1/package.yaml: -------------------------------------------------------------------------------- 1 | name: hello-world 2 | version: 1.1.0.5 3 | 4 | dependencies: 5 | - base 6 | 7 | library: 8 | exposed-modules: HelloWorld 9 | source-dirs: src 10 | ghc-options: -Wall 11 | # dependencies: 12 | # - foo # List here the packages you 13 | # - bar # want to use in your solution. 14 | 15 | tests: 16 | test: 17 | main: Tests.hs 18 | source-dirs: test 19 | dependencies: 20 | - hello-world 21 | - hspec 22 | -------------------------------------------------------------------------------- /solutions/haskell/reverse-string/1/package.yaml: -------------------------------------------------------------------------------- 1 | name: reverse-string 2 | version: 0.1.0.0 3 | 4 | dependencies: 5 | - base 6 | 7 | library: 8 | exposed-modules: ReverseString 9 | source-dirs: src 10 | ghc-options: -Wall 11 | # dependencies: 12 | # - foo # List here the packages you 13 | # - bar # want to use in your solution. 14 | 15 | tests: 16 | test: 17 | main: Tests.hs 18 | source-dirs: test 19 | dependencies: 20 | - reverse-string 21 | - hspec 22 | -------------------------------------------------------------------------------- /solutions/haskell/reverse-string/2/package.yaml: -------------------------------------------------------------------------------- 1 | name: reverse-string 2 | version: 0.1.0.0 3 | 4 | dependencies: 5 | - base 6 | 7 | library: 8 | exposed-modules: ReverseString 9 | source-dirs: src 10 | ghc-options: -Wall 11 | # dependencies: 12 | # - foo # List here the packages you 13 | # - bar # want to use in your solution. 14 | 15 | tests: 16 | test: 17 | main: Tests.hs 18 | source-dirs: test 19 | dependencies: 20 | - reverse-string 21 | - hspec 22 | -------------------------------------------------------------------------------- /solutions/haskell/perfect-numbers/1/package.yaml: -------------------------------------------------------------------------------- 1 | name: perfect-numbers 2 | version: 1.1.0.4 3 | 4 | dependencies: 5 | - base 6 | 7 | library: 8 | exposed-modules: PerfectNumbers 9 | source-dirs: src 10 | ghc-options: -Wall 11 | # dependencies: 12 | # - foo # List here the packages you 13 | # - bar # want to use in your solution. 14 | 15 | tests: 16 | test: 17 | main: Tests.hs 18 | source-dirs: test 19 | dependencies: 20 | - perfect-numbers 21 | - hspec 22 | -------------------------------------------------------------------------------- /solutions/java/secrets/1/src/main/java/Secrets.java: -------------------------------------------------------------------------------- 1 | public class Secrets { 2 | public static int shiftBack(int value, int amount) { 3 | return value >>> amount; 4 | } 5 | 6 | public static int setBits(int value, int mask) { 7 | return value | mask; 8 | } 9 | 10 | public static int flipBits(int value, int mask) { 11 | return value ^ mask; 12 | } 13 | 14 | public static int clearBits(int value, int mask) { 15 | return value & (~mask); 16 | } 17 | } -------------------------------------------------------------------------------- /solutions/julia/cars-assemble/1/cars-assemble.jl: -------------------------------------------------------------------------------- 1 | function success_rate(speed) 2 | if speed == 0 3 | 0.0 4 | elseif speed < 5 5 | 1.0 6 | elseif speed < 9 7 | 0.9 8 | elseif speed < 10 9 | 0.8 10 | else 11 | 0.77 12 | end 13 | end 14 | 15 | function production_rate_per_hour(speed) 16 | 221 * speed * success_rate(speed) 17 | end 18 | 19 | function working_items_per_minute(speed) 20 | floor(Int, production_rate_per_hour(speed) / 60) 21 | end 22 | -------------------------------------------------------------------------------- /solutions/haskell/collatz-conjecture/1/package.yaml: -------------------------------------------------------------------------------- 1 | name: collatz-conjecture 2 | version: 1.2.1.4 3 | 4 | dependencies: 5 | - base 6 | 7 | library: 8 | exposed-modules: CollatzConjecture 9 | source-dirs: src 10 | ghc-options: -Wall 11 | # dependencies: 12 | # - foo # List here the packages you 13 | # - bar # want to use in your solution. 14 | 15 | tests: 16 | test: 17 | main: Tests.hs 18 | source-dirs: test 19 | dependencies: 20 | - collatz-conjecture 21 | - hspec 22 | -------------------------------------------------------------------------------- /solutions/haskell/difference-of-squares/1/package.yaml: -------------------------------------------------------------------------------- 1 | name: difference-of-squares 2 | version: 1.2.0.7 3 | 4 | dependencies: 5 | - base 6 | 7 | library: 8 | exposed-modules: Squares 9 | source-dirs: src 10 | ghc-options: -Wall 11 | # dependencies: 12 | # - foo # List here the packages you 13 | # - bar # want to use in your solution. 14 | 15 | tests: 16 | test: 17 | main: Tests.hs 18 | source-dirs: test 19 | dependencies: 20 | - difference-of-squares 21 | - hspec 22 | -------------------------------------------------------------------------------- /solutions/haskell/scrabble-score/1/package.yaml: -------------------------------------------------------------------------------- 1 | name: scrabble-score 2 | version: 1.1.0.5 3 | 4 | dependencies: 5 | - base 6 | - containers 7 | 8 | library: 9 | exposed-modules: Scrabble 10 | source-dirs: src 11 | ghc-options: -Wall 12 | # dependencies: 13 | # - foo # List here the packages you 14 | # - bar # want to use in your solution. 15 | 16 | tests: 17 | test: 18 | main: Tests.hs 19 | source-dirs: test 20 | dependencies: 21 | - scrabble-score 22 | - hspec 23 | -------------------------------------------------------------------------------- /solutions/julia/nucleotide-count/1/nucleotide-count.jl: -------------------------------------------------------------------------------- 1 | """ 2 | count_nucleotides(strand) 3 | 4 | The count of each nucleotide within `strand` as a dictionary. 5 | 6 | Invalid strands raise a `DomainError`. 7 | 8 | """ 9 | function count_nucleotides(strand) 10 | count = Dict('A' => 0, 'C' => 0, 'G' => 0, 'T' => 0) 11 | for c in strand 12 | if c in "GCAT" 13 | count[c] += 1 14 | else 15 | throw(DomainError(c)) 16 | end 17 | end 18 | count 19 | end 20 | -------------------------------------------------------------------------------- /solutions/julia/perfect-numbers/1/perfect-numbers.jl: -------------------------------------------------------------------------------- 1 | function isperfect(n) 2 | sum(factors(n)) == n 3 | end 4 | 5 | function isabundant(n) 6 | sum(factors(n)) > n 7 | end 8 | 9 | function isdeficient(n) 10 | sum(factors(n)) < n 11 | end 12 | 13 | function factors(n) 14 | if n < 1 15 | throw(DomainError(n)) 16 | end 17 | 18 | fact = Int64[] 19 | for x in 1:(n - 1) 20 | if n % x == 0 21 | push!(fact, x) 22 | end 23 | end 24 | fact 25 | end 26 | -------------------------------------------------------------------------------- /solutions/julia/difference-of-squares/1/difference-of-squares.jl: -------------------------------------------------------------------------------- 1 | "Square the sum of the first `n` positive integers" 2 | function square_of_sum(n) 3 | sum(1:n) ^ 2 4 | end 5 | 6 | "Sum the squares of the first `n` positive integers" 7 | function sum_of_squares(n) 8 | s = 0 9 | for i in 1:n 10 | s += i * i 11 | end 12 | s 13 | end 14 | 15 | "Subtract the sum of squares from square of the sum of the first `n` positive ints" 16 | function difference(n) 17 | square_of_sum(n) - sum_of_squares(n) 18 | end 19 | -------------------------------------------------------------------------------- /solutions/common-lisp/pythagorean-triplet/2/pythagorean-triplet.lisp: -------------------------------------------------------------------------------- 1 | (defpackage :pythagorean-triplet 2 | (:use :cl) 3 | (:export :triplets-with-sum)) 4 | 5 | (in-package :pythagorean-triplet) 6 | 7 | (defun triplets-with-sum (n) 8 | (loop for a from 1 below n 9 | nconc (loop for b from (+ a 1) below n 10 | for c = (- n (+ a b)) 11 | when (triplet a b c) 12 | collect (list a b c)))) 13 | 14 | (defun triplet (a b c) 15 | (= (expt c 2) (+ (expt a 2) (expt b 2)))) 16 | -------------------------------------------------------------------------------- /solutions/kotlin/difference-of-squares/1/src/main/kotlin/DifferenceOfSquares.kt: -------------------------------------------------------------------------------- 1 | class Squares( 2 | private val n: Int 3 | ){ 4 | fun sumOfSquares(): Int { 5 | var sum = 0 6 | for(i in 1..n) { 7 | sum += i * i 8 | } 9 | return sum 10 | } 11 | 12 | fun squareOfSum(): Int { 13 | var sum = 0 14 | for(i in 1..n) { 15 | sum += i 16 | } 17 | return sum * sum 18 | } 19 | 20 | fun difference() = squareOfSum() - sumOfSquares() 21 | } 22 | -------------------------------------------------------------------------------- /solutions/haskell/sieve/1/src/Sieve.hs: -------------------------------------------------------------------------------- 1 | module Sieve (primesUpTo) where 2 | 3 | -- You should not use any of the division operations when implementing 4 | -- the sieve of Eratosthenes. 5 | import Prelude hiding (div, mod, divMod, rem, quotRem, quot, (/)) 6 | 7 | primesUpTo :: Integer -> [Integer] 8 | primesUpTo n = primes n [2..n] 9 | 10 | primes :: Integer -> [Integer] -> [Integer] 11 | primes _ [] = [] 12 | primes n (x:xs) = (x):(primes n (filter notMultiple xs)) 13 | where 14 | notMultiple i = all (/=i) $ takeWhile (<=n) $ map (*x) [2..] -------------------------------------------------------------------------------- /solutions/rust/armstrong-numbers/1/src/lib.rs: -------------------------------------------------------------------------------- 1 | pub fn is_armstrong_number(num: u32) -> bool { 2 | let ds = digits(num); 3 | let len = ds.len() as u32; 4 | 5 | let mut armstrong = 0; 6 | for d in ds { 7 | armstrong += u32::pow(d, len); 8 | } 9 | armstrong == num 10 | } 11 | 12 | fn digits(mut num: u32) -> Vec { 13 | let mut d = Vec::new(); 14 | while num > 9 { 15 | let digit = num % 10; 16 | num = num / 10; 17 | d.push(digit); 18 | } 19 | d.push(num); 20 | d 21 | } 22 | -------------------------------------------------------------------------------- /solutions/java/tim-from-marketing/1/src/main/java/Badge.java: -------------------------------------------------------------------------------- 1 | class Badge { 2 | public String print(Integer id, String name, String department) { 3 | var sb = new StringBuilder(); 4 | if (id != null) { 5 | sb.append(String.format("[%d] - ", id)); 6 | } 7 | sb.append(name); 8 | if (department != null) { 9 | sb.append(String.format(" - %s", department.toUpperCase())); 10 | } else { 11 | sb.append(" - OWNER"); 12 | } 13 | return sb.toString(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /solutions/haskell/perfect-numbers/1/src/PerfectNumbers.hs: -------------------------------------------------------------------------------- 1 | module PerfectNumbers (classify, Classification(..)) where 2 | 3 | data Classification = Deficient | Perfect | Abundant deriving (Eq, Show) 4 | 5 | classify :: Int -> Maybe Classification 6 | classify n 7 | | n < 1 = Nothing 8 | | factorsum == n = Just Perfect 9 | | factorsum > n = Just Abundant 10 | | factorsum < n = Just Deficient 11 | where 12 | factorsum = sum $ factors n 13 | 14 | factors :: Int -> [Int] 15 | factors n = filter factorOfN [1..(n - 1)] 16 | where 17 | factorOfN x = n `mod` x == 0 -------------------------------------------------------------------------------- /solutions/emacs-lisp/armstrong-numbers/1/armstrong-numbers.el: -------------------------------------------------------------------------------- 1 | ;;; armstrong-numbers.el --- armstrong-numbers Exercise (exercism) -*- lexical-binding: t; -*- 2 | 3 | ;;; Commentary: 4 | 5 | ;;; Code: 6 | 7 | 8 | (defun armstrong-p (n) 9 | (let ((d (digits n)) 10 | (sum 0)) 11 | (dolist (i d) 12 | (setq sum (+ sum (expt i (length d))))) 13 | (= sum n))) 14 | 15 | (defun digits (n) 16 | (if (< n 10) 17 | (list n) 18 | (cons (mod n 10) (digits (/ n 10))))) 19 | 20 | (provide 'armstrong-numbers) 21 | ;;; armstrong-numbers.el ends here 22 | -------------------------------------------------------------------------------- /solutions/common-lisp/hamming/1/hamming.lisp: -------------------------------------------------------------------------------- 1 | (defpackage :hamming 2 | (:use :cl) 3 | (:export :distance)) 4 | 5 | (in-package :hamming) 6 | 7 | (defun distance (strand1 strand2) 8 | (when (eq (length strand1) (length strand2)) 9 | (distance-aux strand1 strand2 (length strand1)))) 10 | 11 | (defun distance-aux (strand1 strand2 char-num) 12 | (if (eq char-num 0) 13 | 0 14 | (+ 15 | (distance-aux strand1 strand2 (- char-num 1)) 16 | (if (char= (char strand1 (- char-num 1)) (char strand2 (- char-num 1))) 17 | 0 18 | 1)))) 19 | -------------------------------------------------------------------------------- /solutions/kotlin/collatz-conjecture/1/src/main/kotlin/CollatzCalculator.kt: -------------------------------------------------------------------------------- 1 | object CollatzCalculator { 2 | fun computeStepCount(start: Int): Int { 3 | if(start < 1) { 4 | throw IllegalArgumentException("number must be positive") 5 | } 6 | 7 | var steps = 0 8 | var n = start; 9 | 10 | while (n > 1) { 11 | if (n % 2 == 0) { 12 | n /= 2 13 | } else { 14 | n = (3 * n) + 1 15 | } 16 | steps++ 17 | } 18 | return steps 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /solutions/scala/armstrong-numbers/1/src/main/scala/ArmstrongNumbers.scala: -------------------------------------------------------------------------------- 1 | object ArmstrongNumbers { 2 | def isArmstrongNumber(n: Int): Boolean = { 3 | val dgs = digits(n) 4 | val armstrong = dgs.map { x => 5 | scala.math.pow(x, dgs.size) 6 | }.sum 7 | n == armstrong 8 | } 9 | 10 | private def digits(n: Int): List[Int] = { 11 | var a = n 12 | val dgs = scala.collection.mutable.ListBuffer.empty[Int] 13 | while (a > 9) { 14 | val r = a % 10 15 | a /= 10 16 | dgs += r 17 | } 18 | dgs += a 19 | dgs.toList 20 | } 21 | } -------------------------------------------------------------------------------- /solutions/julia/lasagna/1/lasagna.jl: -------------------------------------------------------------------------------- 1 | # Define the `expected_bake_time` constant` 2 | const expected_bake_time = 60 3 | 4 | # Define the `preparation_time(layers)` function. 5 | function preparation_time(layers) 6 | layers * 2 7 | end 8 | 9 | # Define the `remaining_time(time_in_oven)` function. 10 | function remaining_time(time_in_oven) 11 | return expected_bake_time - time_in_oven 12 | end 13 | 14 | # Define the `total_working_time(layers, time_in_oven)` function. 15 | function total_working_time(layers, time_in_oven) 16 | preparation_time(layers) + time_in_oven 17 | end 18 | -------------------------------------------------------------------------------- /solutions/clojure/bird-watcher/1/src/bird_watcher.clj: -------------------------------------------------------------------------------- 1 | (ns bird-watcher) 2 | 3 | (def last-week 4 | [0 2 5 3 7 8 4]) 5 | 6 | (defn today [birds] 7 | (get birds 6)) 8 | 9 | (defn inc-bird [birds] 10 | (assoc birds 6 (+ 1 (get birds 6)))) 11 | 12 | (defn day-without-birds? [birds] 13 | (not (every? pos? birds))) 14 | 15 | (defn n-days-count [birds n] 16 | (reduce + (take n birds))) 17 | 18 | (defn busy-day [birds] 19 | (>= birds 5)) 20 | 21 | (defn busy-days [birds] 22 | (count (filter busy-day birds))) 23 | 24 | (defn odd-week? [birds] 25 | (= birds [1 0 1 0 1 0 1])) 26 | -------------------------------------------------------------------------------- /solutions/common-lisp/perfect-numbers/1/perfect-numbers.lisp: -------------------------------------------------------------------------------- 1 | (defpackage :perfect-numbers 2 | (:use :cl) 3 | (:export :classify)) 4 | 5 | (in-package :perfect-numbers) 6 | 7 | (defun classify (number) 8 | (when (> number 0) 9 | (let ((factorsum (apply #'+ (factors number)))) 10 | (cond 11 | ((= factorsum number) "perfect") 12 | ((< factorsum number) "deficient") 13 | ((> factorsum number) "abundant"))))) 14 | 15 | (defun factors (number) 16 | (loop for i from 1 below number 17 | when (zerop (mod number i)) 18 | collect i)) -------------------------------------------------------------------------------- /solutions/common-lisp/sum-of-multiples/1/sum-of-multiples.lisp: -------------------------------------------------------------------------------- 1 | (defpackage :sum-of-multiples 2 | (:use :cl) 3 | (:export :sum)) 4 | 5 | (in-package :sum-of-multiples) 6 | 7 | (defun sum (factors limit) 8 | (if (zerop (length factors)) 9 | 0 10 | (let ((multiples (mapcar (lambda (factor) (multiples-of-until factor factor limit)) factors))) 11 | (reduce #'+ (reduce #'union multiples))))) 12 | 13 | 14 | (defun multiples-of-until (n s limit) 15 | (unless (< n 1) 16 | (if (< n limit) 17 | (cons n (multiples-of-until (+ n s) s limit)) 18 | '()))) 19 | -------------------------------------------------------------------------------- /solutions/scala/rna-transcription/1/src/main/scala/RnaTranscription.scala: -------------------------------------------------------------------------------- 1 | object RnaTranscription { 2 | def toRna(strand: String): Option[String] = { 3 | strand 4 | .foldLeft(Option("")) { (acc, c) => 5 | rnaComplement(c) match { 6 | case Some(x) => acc.map(_ + x) 7 | case None => None 8 | } 9 | } 10 | } 11 | 12 | private val rnaComplements = Map( 13 | 'G' -> 'C', 14 | 'C' -> 'G', 15 | 'T' -> 'A', 16 | 'A' -> 'U', 17 | ) 18 | 19 | private def rnaComplement(c: Char): Option[Char] = 20 | rnaComplements.get(c) 21 | } -------------------------------------------------------------------------------- /solutions/common-lisp/logans-numeric-partition/1/logans-numeric-partition.lisp: -------------------------------------------------------------------------------- 1 | (defpackage :logans-numeric-partition 2 | (:use :cl) 3 | (:export :categorize-number :partition-numbers)) 4 | 5 | (in-package :logans-numeric-partition) 6 | 7 | ;; Define categorize-number function 8 | (defun categorize-number (pairs num) 9 | (if (evenp num) 10 | (cons (car pairs) (cons num (cdr pairs))) 11 | (cons (cons num (car pairs)) (cdr pairs)) 12 | )) 13 | ;; Define partition-numbers function 14 | (defun partition-numbers (l) 15 | (reduce #'categorize-number l :initial-value '(() . ()))) -------------------------------------------------------------------------------- /solutions/common-lisp/resistor-color/1/resistor-color.lisp: -------------------------------------------------------------------------------- 1 | (defpackage :resistor-color 2 | (:use :cl) 3 | (:export :color-code 4 | :colors)) 5 | 6 | (in-package :resistor-color) 7 | 8 | (defun color-code (color) 9 | (position color (colors) :test #'equal)) 10 | 11 | (defun colors () '("black" 12 | "brown" 13 | "red" 14 | "orange" 15 | "yellow" 16 | "green" 17 | "blue" 18 | "violet" 19 | "grey" 20 | "white")) 21 | -------------------------------------------------------------------------------- /solutions/julia/annalyns-infiltration/1/annalyns-infiltration.jl: -------------------------------------------------------------------------------- 1 | function can_do_fast_attack(knight_awake) 2 | !knight_awake 3 | end 4 | 5 | function can_spy(knight_awake, archer_awake, prisoner_awake) 6 | knight_awake || archer_awake || prisoner_awake 7 | end 8 | 9 | function can_signal_prisoner(archer_awake, prisoner_awake) 10 | !archer_awake && prisoner_awake 11 | end 12 | 13 | function can_free_prisoner(knight_awake, archer_awake, prisoner_awake, dog_present) 14 | (dog_present && !archer_awake) || (!dog_present && prisoner_awake && !knight_awake && !archer_awake) 15 | end 16 | -------------------------------------------------------------------------------- /solutions/scala/nucleotide-count/1/src/main/scala/NucleotideCount.scala: -------------------------------------------------------------------------------- 1 | case class DNA(strand: String) { 2 | def nucleotideCounts: Either[String, Map[Char, Int]] = { 3 | val counts = collection.immutable.Map( 4 | 'G' -> 0, 5 | 'C' -> 0, 6 | 'A' -> 0, 7 | 'T' -> 0, 8 | ) 9 | 10 | strand.foldLeft(Right(counts): Either[String, Map[Char, Int]]) { (acc, c) => 11 | if(counts.contains(c)) { 12 | acc.map { cts => 13 | cts + (c -> (cts(c) + 1)) 14 | } 15 | } else { 16 | Left("invalid strand") 17 | } 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /solutions/java/collatz-conjecture/1/src/main/java/CollatzCalculator.java: -------------------------------------------------------------------------------- 1 | class CollatzCalculator { 2 | 3 | int computeStepCount(int start) { 4 | if(start < 1) { 5 | throw new IllegalArgumentException("Only positive integers are allowed"); 6 | } 7 | int n = start; 8 | int steps = 0; 9 | 10 | while (n > 1) { 11 | if (n % 2 == 0) { 12 | n /= 2; 13 | } else { 14 | n = (3 * n) + 1; 15 | } 16 | steps++; 17 | } 18 | 19 | return steps; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /solutions/clojure/cars-assemble/1/src/cars_assemble.clj: -------------------------------------------------------------------------------- 1 | (ns cars-assemble) 2 | 3 | (def cars-per-speed 221) 4 | 5 | (defn success-rate [speed] 6 | (cond 7 | (= speed 0) 0 8 | (< speed 5) 1 9 | (< speed 9) 0.9 10 | (< speed 10) 0.8 11 | :else 0.77)) 12 | 13 | (defn production-rate 14 | "Returns the assembly line's production rate per hour, 15 | taking into account its success rate" 16 | [speed] 17 | (* (success-rate speed) speed 221)) 18 | 19 | (defn working-items 20 | "Calculates how many working cars are produced per minute" 21 | [speed] 22 | (int (/ (production-rate speed) 60))) 23 | -------------------------------------------------------------------------------- /solutions/common-lisp/nucleotide-count/1/nucleotide-count.lisp: -------------------------------------------------------------------------------- 1 | (defpackage :nucleotide-count 2 | (:use :cl) 3 | (:export :nucleotide-counts)) 4 | 5 | (in-package :nucleotide-count) 6 | 7 | (defconstant +initial-count+ '((#\A . 0) (#\C . 0) (#\G . 0) (#\T . 0))) 8 | 9 | (defun nucleotide-counts (strand) 10 | (let ((count (copy-tree +initial-count+))) 11 | (loop for c across strand 12 | do (if (assoc c count) 13 | (setf (cdr (assoc c count)) (+ 1 (cdr (assoc c count)))) 14 | (return-from nucleotide-counts nil)) 15 | finally (return count)))) 16 | -------------------------------------------------------------------------------- /solutions/common-lisp/pythagorean-triplet/1/pythagorean-triplet.lisp: -------------------------------------------------------------------------------- 1 | (defpackage :pythagorean-triplet 2 | (:use :cl) 3 | (:export :triplets-with-sum)) 4 | 5 | (in-package :pythagorean-triplet) 6 | 7 | (defun triplets-with-sum (n) 8 | (loop for a from 1 below n 9 | nconc (loop for b from (+ a 1) below n 10 | for c = (- n (+ a b)) 11 | when (and 12 | (= n (+ a b c)) 13 | (triplet a b c)) 14 | collect (list a b c)))) 15 | 16 | (defun triplet (a b c) 17 | (= (expt c 2) (+ (expt a 2) (expt b 2)))) 18 | -------------------------------------------------------------------------------- /solutions/java/log-levels/1/src/main/java/LogLevels.java: -------------------------------------------------------------------------------- 1 | public class LogLevels { 2 | 3 | public static String message(String logLine) { 4 | int colonsPosition = logLine.indexOf(':'); 5 | return logLine.substring(colonsPosition + 2).strip(); 6 | } 7 | 8 | public static String logLevel(String logLine) { 9 | int colonsPosition = logLine.indexOf(':'); 10 | return logLine.substring(1, colonsPosition - 1).toLowerCase(); 11 | } 12 | 13 | public static String reformat(String logLine) { 14 | return message(logLine) + " (" + logLevel(logLine) + ")"; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /solutions/emacs-lisp/acronym/1/acronym.el: -------------------------------------------------------------------------------- 1 | ;;; acronym.el --- Acronym (exercism) -*- lexical-binding: t; -*- 2 | 3 | ;;; Commentary: 4 | 5 | ;;; Code: 6 | (defun first-letter-upcase (str) 7 | (char-to-string (upcase (string-to-char str)))) 8 | 9 | (defun clean-str (str) 10 | (replace-regexp-in-string "[^A-Za-z ]" "" (string-replace "-" " " str))) 11 | 12 | (defun acronym (phrase) 13 | (string-join (mapcar #'first-letter-upcase 14 | (seq-remove #'string-empty-p 15 | (split-string (clean-str phrase) " "))))) 16 | 17 | (provide 'acronym) 18 | ;;; acronym.el ends here 19 | -------------------------------------------------------------------------------- /solutions/common-lisp/pizza-pi/1/pizza-pi.lisp: -------------------------------------------------------------------------------- 1 | (defpackage :pizza-pi 2 | (:use :cl) 3 | (:export :dough-calculator :pizzas-per-cube 4 | :size-from-sauce :fair-share-p)) 5 | 6 | (in-package :pizza-pi) 7 | 8 | (defun dough-calculator (pizzas diameter) 9 | (round (* pizzas (+ 200 (/ (* 45 pi diameter) 20)))) 10 | ) 11 | 12 | (defun size-from-sauce (sauce) 13 | (sqrt (/ (* 40 sauce) (* pi 3))) 14 | ) 15 | 16 | (defun pizzas-per-cube (cube-size diameter) 17 | (floor (/ (* 2 (expt cube-size 3)) (* 3 pi (expt diameter 2)))) 18 | ) 19 | 20 | (defun fair-share-p (pizzas friends) 21 | (= (mod (* 8 pizzas) friends) 0) 22 | ) 23 | -------------------------------------------------------------------------------- /solutions/emacs-lisp/difference-of-squares/1/difference-of-squares.el: -------------------------------------------------------------------------------- 1 | ;;; difference-of-squares.el --- Difference of Squares (exercism) -*- lexical-binding: t; -*- 2 | 3 | ;;; Commentary: 4 | 5 | (defun sum-of-squares (n) 6 | (seq-reduce (lambda (acc i) 7 | (+ acc (* i i))) 8 | (number-sequence 1 n) 9 | 0)) 10 | 11 | (defun square-of-sum (n) 12 | (let ((sum (seq-reduce #'+ (number-sequence 1 n) 0))) 13 | (* sum sum))) 14 | 15 | (defun difference (n) 16 | (- (square-of-sum n) (sum-of-squares n))) 17 | 18 | (provide 'difference-of-squares) 19 | ;;; difference-of-squares.el ends here 20 | -------------------------------------------------------------------------------- /solutions/arm64-assembly/collatz-conjecture/2/collatz_conjecture.s: -------------------------------------------------------------------------------- 1 | .equ INVALID_NUMBER, -1 2 | 3 | .text 4 | .globl steps 5 | 6 | steps: 7 | cmp xzr, x0 8 | bge exit_invalid 9 | mov x2, #0 10 | loop: 11 | cmp x0, #1 12 | beq exit 13 | add x2, x2, #1 14 | tbz x0, 0, even 15 | odd: 16 | mov x3, #3 17 | mul x0, x0, x3 18 | add x0, x0, #1 19 | b loop 20 | even: 21 | // mov x3, #2 22 | // udiv x0, x0, x3 23 | lsr x0, x0, #1 24 | b loop 25 | exit: 26 | mov x0, x2 27 | ret 28 | exit_invalid: 29 | mov x0, -1 30 | ret -------------------------------------------------------------------------------- /solutions/scala/grade-school/1/src/main/scala/GradeSchool.scala: -------------------------------------------------------------------------------- 1 | class School { 2 | type DB = Map[Int, Seq[String]] 3 | 4 | private var data: DB = Map.empty 5 | 6 | def add(name: String, g: Int) = { 7 | val students = data.getOrElse(g, Seq.empty) 8 | val updatedStudents = students :+ name 9 | data = data + (g -> updatedStudents) 10 | } 11 | 12 | def db: DB = data 13 | 14 | def grade(g: Int): Seq[String] = 15 | data.getOrElse(g, Seq.empty) 16 | 17 | def sorted: DB = { 18 | val keys = data.keySet.toSeq.sorted 19 | keys.foldLeft(Map.empty)((acc: DB, key: Int) => acc + (key -> data(key).sorted)) 20 | } 21 | } 22 | 23 | -------------------------------------------------------------------------------- /solutions/rust/sieve/3/src/lib.rs: -------------------------------------------------------------------------------- 1 | use std::collections::VecDeque; 2 | 3 | pub fn primes_up_to(upper_bound: u64) -> Vec { 4 | let mut nums: VecDeque = (2..=upper_bound).collect(); 5 | let mut primes = vec![]; 6 | 7 | while let Some(prime) = nums.pop_front() { 8 | primes.push(prime); 9 | nums.retain(|x| !multiple_of(prime, *x, upper_bound)); 10 | } 11 | primes 12 | } 13 | 14 | fn multiple_of(n: u64, x: u64, upper_bound: u64) -> bool { 15 | let mut i = n; 16 | while i <= upper_bound { 17 | if i == x { 18 | return true; 19 | } 20 | i += n; 21 | } 22 | false 23 | } 24 | -------------------------------------------------------------------------------- /solutions/rust/sieve/2/src/lib.rs: -------------------------------------------------------------------------------- 1 | use std::collections::VecDeque; 2 | 3 | pub fn primes_up_to(upper_bound: u64) -> Vec { 4 | let mut nums: VecDeque = (2..=upper_bound).collect(); 5 | let mut primes = vec![]; 6 | 7 | while let Some(prime) = nums.pop_front() { 8 | primes.push(prime); 9 | nums.retain(|x| !multipleOf(prime, *x, upper_bound)); 10 | } 11 | primes 12 | } 13 | 14 | fn multipleOf(n: u64, x: u64, upper_bound: u64) -> bool { 15 | let mut i = n; 16 | while i <= upper_bound { 17 | if i == x { 18 | return true; 19 | } 20 | i += n; 21 | } 22 | return false; 23 | } 24 | -------------------------------------------------------------------------------- /solutions/arm64-assembly/hamming/1/hamming.s: -------------------------------------------------------------------------------- 1 | .equ UNEQUAL_LENGTHS, -1 2 | 3 | .text 4 | .globl distance 5 | 6 | // x0 - strand1 7 | // x1 - strand2 8 | distance: 9 | mov x9, #0 10 | distance_loop: 11 | ldrb w10, [x0], #0x01 12 | ldrb w11, [x1], #0x01 13 | cmp w10, wzr 14 | beq end_of_str 15 | cmp w11, wzr 16 | beq distance_exit_error 17 | cmp w10, w11 18 | cinc x9, x9, ne 19 | b distance_loop 20 | distance_exit: 21 | mov x0, x9 22 | ret 23 | end_of_str: 24 | cmp w10, w11 25 | beq distance_exit 26 | distance_exit_error: 27 | mov x0, #-1 28 | ret 29 | -------------------------------------------------------------------------------- /solutions/x86-64-assembly/collatz-conjecture/1/collatz_conjecture.asm: -------------------------------------------------------------------------------- 1 | section .text 2 | global steps 3 | steps: 4 | mov r8, rdi 5 | cmp r8d, 0 6 | jle exit_invalid 7 | mov r9, 0 8 | loop: 9 | cmp r8, 1 10 | je exit 11 | inc r9 12 | test r8, 0x1 13 | je even 14 | odd: 15 | mov rax, r8 16 | mov r10, 3 17 | mul r10 18 | mov r8, rax 19 | inc r8 20 | jmp loop 21 | even: 22 | shrd r8, r8, 1 23 | jmp loop 24 | exit: 25 | mov rax, r9 26 | ret 27 | 28 | exit_invalid: 29 | mov rax, -1 30 | ret 31 | 32 | %ifidn __OUTPUT_FORMAT__,elf64 33 | section .note.GNU-stack noalloc noexec nowrite progbits 34 | %endif 35 | -------------------------------------------------------------------------------- /solutions/arm64-assembly/collatz-conjecture/1/collatz_conjecture.s: -------------------------------------------------------------------------------- 1 | .equ INVALID_NUMBER, -1 2 | 3 | .text 4 | .globl steps 5 | 6 | steps: 7 | cmp xzr, x0 8 | bge exit_invalid 9 | mov x2, #0 10 | loop: 11 | cmp x0, #1 12 | beq exit 13 | add x2, x2, #1 14 | and x1, x0, #0x01 15 | cbz x1, even 16 | odd: 17 | mov x3, #3 18 | mul x0, x0, x3 19 | add x0, x0, #1 20 | b loop 21 | even: 22 | // mov x3, #2 23 | // udiv x0, x0, x3 24 | lsr x0, x0, #1 25 | b loop 26 | exit: 27 | mov x0, x2 28 | ret 29 | exit_invalid: 30 | mov x0, -1 31 | ret -------------------------------------------------------------------------------- /solutions/arm64-assembly/grains/1/grains.s: -------------------------------------------------------------------------------- 1 | .text 2 | .globl square 3 | .globl total 4 | 5 | square: 6 | cmp x0, xzr 7 | ble exit_error 8 | mov x1, #1 9 | mov x2, #1 10 | loop_square: 11 | cmp x0, x1 12 | beq exit_square 13 | add x1, x1, #1 14 | lsl x2, x2, #1 15 | b loop_square 16 | 17 | exit_error: 18 | mov x0, #0 19 | ret 20 | 21 | exit_square: 22 | mov x0, x2 23 | ret 24 | 25 | total: 26 | mov x1, #1 27 | mov x2, #1 28 | mov x0, #1 29 | loop_total: 30 | cmp x1, #64 31 | beq exit_total 32 | add x1, x1, #1 33 | lsl x2, x2, #1 34 | add x0, x0, x2 35 | b loop_total 36 | exit_total: 37 | ret 38 | -------------------------------------------------------------------------------- /solutions/common-lisp/armstrong-numbers/1/armstrong-numbers.lisp: -------------------------------------------------------------------------------- 1 | (defpackage :armstrong-numbers 2 | (:use :cl) 3 | (:export :armstrong-number-p)) 4 | (in-package :armstrong-numbers) 5 | 6 | (defun armstrong-number-p (number) 7 | (when (= number (armstrong number)) t)) 8 | 9 | (defun armstrong (number) 10 | (let ((ds (digits number))) 11 | (reduce #'+ 12 | (mapcar #'(lambda (x) 13 | (expt x (length ds))) 14 | ds)))) 15 | 16 | (defun digits (number) 17 | (if (< number 10) 18 | (list number) 19 | (multiple-value-bind (c m) (floor number 10) 20 | (append (digits c) (list m))))) 21 | -------------------------------------------------------------------------------- /solutions/java/difference-of-squares/1/src/main/java/DifferenceOfSquaresCalculator.java: -------------------------------------------------------------------------------- 1 | class DifferenceOfSquaresCalculator { 2 | 3 | int computeSquareOfSumTo(int input) { 4 | int sum = 0; 5 | for(int i = 1; i<=input; i++) { 6 | sum += i; 7 | } 8 | return sum * sum; 9 | } 10 | 11 | int computeSumOfSquaresTo(int input) { 12 | int sum = 0; 13 | for(int i = 1; i<= input; i++) { 14 | sum += i * i; 15 | } 16 | return sum; 17 | } 18 | 19 | int computeDifferenceOfSquares(int input) { 20 | return computeSquareOfSumTo(input) - computeSumOfSquaresTo(input); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /solutions/rust/sum-of-multiples/1/src/lib.rs: -------------------------------------------------------------------------------- 1 | use std::collections::HashSet; 2 | 3 | pub fn sum_of_multiples(limit: u32, factors: &[u32]) -> u32 { 4 | let mut nums = HashSet::new(); 5 | for factor in factors { 6 | let multiple_set = multiples_of(*factor, limit); 7 | nums = nums.union(&multiple_set).copied().collect(); 8 | } 9 | nums.into_iter().sum() 10 | } 11 | 12 | fn multiples_of(n: u32, limit: u32) -> HashSet { 13 | let mut set: HashSet = HashSet::new(); 14 | if n < 1 { 15 | return set; 16 | } 17 | 18 | let mut x = n; 19 | while x < limit { 20 | set.insert(x); 21 | x += n; 22 | } 23 | set 24 | } -------------------------------------------------------------------------------- /solutions/common-lisp/difference-of-squares/1/difference-of-squares.lisp: -------------------------------------------------------------------------------- 1 | (defpackage :difference-of-squares 2 | (:use :cl) 3 | (:export :sum-of-squares 4 | :square-of-sum 5 | :difference)) 6 | 7 | (in-package :difference-of-squares) 8 | 9 | (defun square-of-sum (n) 10 | "Calculates the square of the sum for a given number." 11 | (expt (loop for i from 1 to n sum i) 2)) 12 | 13 | (defun sum-of-squares (n) 14 | "Calculates the sum of squares for a given number." 15 | (loop for i from 1 to n sum (* i i))) 16 | 17 | (defun difference (n) 18 | "Finds the diff. between the square of the sum and the sum of the squares." 19 | (- (square-of-sum n) (sum-of-squares n))) 20 | -------------------------------------------------------------------------------- /solutions/rust/perfect-numbers/1/src/lib.rs: -------------------------------------------------------------------------------- 1 | #[derive(Debug, PartialEq, Eq)] 2 | pub enum Classification { 3 | Abundant, 4 | Perfect, 5 | Deficient, 6 | } 7 | 8 | pub fn classify(num: u64) -> Option { 9 | if num < 1 { 10 | return None; 11 | } 12 | 13 | let aliquot_sum: u64 = factors(num).into_iter().sum(); 14 | 15 | if aliquot_sum == num { 16 | Some(Classification::Perfect) 17 | } else if aliquot_sum > num { 18 | Some(Classification::Abundant) 19 | } else { 20 | Some(Classification::Deficient) 21 | } 22 | } 23 | 24 | fn factors(num: u64) -> Vec { 25 | (1..num).filter(|x| num % x == 0).collect() 26 | } -------------------------------------------------------------------------------- /solutions/java/difference-of-squares/2/src/main/java/DifferenceOfSquaresCalculator.java: -------------------------------------------------------------------------------- 1 | import java.util.stream.IntStream; 2 | 3 | class DifferenceOfSquaresCalculator { 4 | 5 | int computeSquareOfSumTo(int input) { 6 | int sum = 0; 7 | for(int i = 1; i<=input; i++) { 8 | sum += i; 9 | } 10 | return sum * sum; 11 | } 12 | 13 | int computeSumOfSquaresTo(int input) { 14 | return IntStream.rangeClosed(1, input) 15 | .map(x -> x * x) 16 | .sum(); 17 | } 18 | 19 | int computeDifferenceOfSquares(int input) { 20 | return computeSquareOfSumTo(input) - computeSumOfSquaresTo(input); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /solutions/common-lisp/sieve/1/sieve.lisp: -------------------------------------------------------------------------------- 1 | (defpackage :sieve 2 | (:use :cl) 3 | (:export :primes-to) 4 | (:documentation "Generates a list of primes up to a given limit.")) 5 | 6 | (in-package :sieve) 7 | 8 | (defun primes-to (n) 9 | "List primes up to `n' using sieve of Eratosthenes." 10 | (let ((numbers (loop for i from 2 to n collect i))) 11 | (primes-sieve numbers))) 12 | 13 | (defun primes-sieve (numbers) 14 | (if (zerop (length numbers)) 15 | '() 16 | (cons 17 | (car numbers) 18 | (primes-sieve 19 | (remove-if 20 | #'(lambda (n) 21 | (zerop 22 | (mod n (car numbers)))) 23 | (cdr numbers)))))) 24 | -------------------------------------------------------------------------------- /solutions/emacs-lisp/hamming/1/hamming.el: -------------------------------------------------------------------------------- 1 | ;;; hamming.el --- Hamming (exercism) -*- lexical-binding: t; -*- 2 | 3 | ;;; Commentary: 4 | 5 | ;;; Code: 6 | 7 | 8 | (defun hamming-distance (dna1 dna2) 9 | (when (not (= (length dna1) (length dna2))) 10 | (error "DNA strands have different lengths")) 11 | (let ( 12 | (h 0) 13 | (i 0) 14 | (l (length dna1)) 15 | (l1 (string-to-list dna1)) 16 | (l2 (string-to-list dna2))) 17 | (while (< i l) 18 | (unless (eql (nth i l1) (nth i l2)) 19 | (setf h (+ h 1))) 20 | (setf i (+ i 1))) 21 | h)) 22 | 23 | 24 | 25 | (provide 'hamming) 26 | ;;; hamming.el ends here 27 | -------------------------------------------------------------------------------- /solutions/common-lisp/prime-factors/1/prime-factors.lisp: -------------------------------------------------------------------------------- 1 | (defpackage :prime-factors 2 | (:use :cl) 3 | (:export :factors)) 4 | 5 | (in-package :prime-factors) 6 | 7 | (defun factors (n) 8 | (let ((prime-factors '()) 9 | (current-test-factor 2) 10 | (rest n)) 11 | (loop while (not (= rest 1)) 12 | do (progn 13 | (if (zerop (mod rest current-test-factor)) 14 | (progn 15 | (push current-test-factor prime-factors) 16 | (setf rest (/ rest current-test-factor))) 17 | (setf current-test-factor (+ current-test-factor 1)))) 18 | finally (return (nreverse prime-factors))))) 19 | -------------------------------------------------------------------------------- /solutions/java/cars-assemble/1/src/main/java/CarsAssemble.java: -------------------------------------------------------------------------------- 1 | public class CarsAssemble { 2 | 3 | private static final int CARS_PER_HOUR = 221; 4 | 5 | private double successRate(int speed) { 6 | if (speed < 5) { 7 | return 1; 8 | } else if(speed < 9) { 9 | return 0.9; 10 | } else if(speed < 10) { 11 | return 0.8; 12 | } else { 13 | return 0.77; 14 | } 15 | } 16 | 17 | public double productionRatePerHour(int speed) { 18 | return successRate(speed) * speed * CARS_PER_HOUR; 19 | } 20 | 21 | public int workingItemsPerMinute(int speed) { 22 | return (int) (productionRatePerHour(speed) / 60); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /solutions/x86-64-assembly/leap/1/leap.asm: -------------------------------------------------------------------------------- 1 | section .text 2 | global leap_year 3 | leap_year: 4 | mov r9, rdi 5 | mov rax, rdi 6 | xor rdx, rdx 7 | mov r8, 4 8 | div r8 9 | cmp rdx, 0 10 | jne leap_year_exit_no 11 | 12 | mov r8, 100 13 | xor rdx, rdx 14 | mov rax, r9 15 | div r8 16 | cmp rdx, 0 17 | jne leap_year_exit_yes 18 | 19 | mov r8, 400 20 | xor rdx, rdx 21 | mov rax, r9 22 | div r8 23 | cmp rdx, 0 24 | jne leap_year_exit_no 25 | 26 | leap_year_exit_yes: 27 | mov rax, 1 28 | ret 29 | leap_year_exit_no: 30 | mov rax, 0 31 | ret 32 | 33 | %ifidn __OUTPUT_FORMAT__,elf64 34 | section .note.GNU-stack noalloc noexec nowrite progbits 35 | %endif 36 | -------------------------------------------------------------------------------- /solutions/java/captains-log/1/src/main/java/CaptainsLog.java: -------------------------------------------------------------------------------- 1 | import java.util.Random; 2 | 3 | class CaptainsLog { 4 | 5 | private static final char[] PLANET_CLASSES = new char[]{'D', 'H', 'J', 'K', 'L', 'M', 'N', 'R', 'T', 'Y'}; 6 | 7 | private Random random; 8 | 9 | CaptainsLog(Random random) { 10 | this.random = random; 11 | } 12 | 13 | char randomPlanetClass() { 14 | var i = random.nextInt(10); 15 | return PLANET_CLASSES[i]; 16 | } 17 | 18 | String randomShipRegistryNumber() { 19 | var number = random.nextInt(9000) + 1000; 20 | return "NCC-" + number; 21 | } 22 | 23 | double randomStardate() { 24 | return 41000.0 + random.nextDouble() * 1000.0; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /solutions/java/lasagna/3/src/main/java/Lasagna.java: -------------------------------------------------------------------------------- 1 | public class Lasagna { 2 | private static final int EXPECTED_MINUTES_IN_OVEN = 40; 3 | private static final int PREPARATION_TIME_PER_LAYER = 2; 4 | 5 | public int expectedMinutesInOven() { 6 | return EXPECTED_MINUTES_IN_OVEN; 7 | } 8 | 9 | public int remainingMinutesInOven(int timeSpentInOven) { 10 | return this.expectedMinutesInOven() - timeSpentInOven; 11 | } 12 | 13 | public int preparationTimeInMinutes(int layers) { 14 | return layers * PREPARATION_TIME_PER_LAYER; 15 | } 16 | 17 | public int totalTimeInMinutes(int layers, int timeSpentInOven) { 18 | return this.preparationTimeInMinutes(layers) + timeSpentInOven; 19 | } 20 | } -------------------------------------------------------------------------------- /solutions/scala/pythagorean-triplet/1/src/main/scala/PythagoreanTriplet.scala: -------------------------------------------------------------------------------- 1 | object PythagoreanTriplet { 2 | def isPythagorean(triplet: (Int, Int, Int)): Boolean = { 3 | val (a, b, c) = triplet 4 | a < b && b < c && a * a + b * b == c * c 5 | } 6 | 7 | def pythagoreanTriplets(from: Int, end: Int): Seq[(Int, Int, Int)] = 8 | for { 9 | a <- from to end 10 | b <- a to end 11 | c <- b to end 12 | if isPythagorean((a, b, c)) 13 | } yield (a, b, c) 14 | 15 | def pythagoreanTripletsSum(sum: Int): Seq[(Int, Int, Int)] = 16 | for { 17 | a <- 1 to sum 18 | b <- a to sum 19 | c <- b to sum 20 | if a + b + c == sum 21 | if isPythagorean((a, b, c)) 22 | } yield (a, b, c) 23 | } 24 | -------------------------------------------------------------------------------- /solutions/common-lisp/triangle/1/triangle.lisp: -------------------------------------------------------------------------------- 1 | (defpackage :triangle 2 | (:use :cl) 3 | (:export :triangle-type-p)) 4 | 5 | (in-package :triangle) 6 | 7 | (defun triangle-type-p (type a b c) 8 | "Deterimines if a triangle (given by side lengths A, B, C) is of the given TYPE" 9 | (when (and 10 | (< c (+ a b)) 11 | (< a (+ b c)) 12 | (< b (+ a c))) 13 | (case type 14 | (:equilateral (and 15 | (= a b) 16 | (= b c))) 17 | (:isosceles (or 18 | (= a b) 19 | (= b c) 20 | (= a c))) 21 | (:scalene (and 22 | (not (= a b)) 23 | (not (= b c)) 24 | (not (= a c))))))) 25 | -------------------------------------------------------------------------------- /solutions/emacs-lisp/collatz-conjecture/1/collatz-conjecture.el: -------------------------------------------------------------------------------- 1 | ;;; collatz-conjecture.el --- Collatz Conjecture (exercism) -*- lexical-binding: t; -*- 2 | (require 'cl-lib) 3 | ;;; Commentary: 4 | 5 | ;;; Code: 6 | 7 | (defun steps (number) 8 | "Count the steps to reach 1 using the Collatz conjecture." 9 | (if (> number 0) 10 | (let ((iterations 0) 11 | (n number)) 12 | (while (> n 1) 13 | (if (cl-evenp n) 14 | (setq n (/ n 2)) 15 | (setq n (+ 1 (* 3 n)))) 16 | (setq iterations (+ iterations 1))) 17 | iterations) 18 | (error "number should be positive"))) 19 | 20 | (provide 'collatz-conjecture) 21 | ;;; collatz-conjecture.el ends here 22 | -------------------------------------------------------------------------------- /solutions/common-lisp/strain/1/strain.lisp: -------------------------------------------------------------------------------- 1 | (defpackage :strain 2 | (:use :cl) 3 | (:export :keep :discard)) 4 | 5 | (in-package :strain) 6 | 7 | (defun keep (keep-p elements) 8 | "Returns a sublist of elements according to a given predicate." 9 | (if (zerop (length elements)) 10 | '() 11 | (if (funcall keep-p (car elements)) 12 | (cons (car elements) (keep keep-p (cdr elements))) 13 | (keep keep-p (cdr elements))))) 14 | 15 | (defun discard (discard-p elements) 16 | "Returns a sublist of elements not matching a given predicate." 17 | (if (zerop (length elements)) 18 | '() 19 | (if (funcall discard-p (car elements)) 20 | (discard discard-p (cdr elements)) 21 | (cons (car elements) (discard discard-p (cdr elements)))))) 22 | -------------------------------------------------------------------------------- /solutions/java/lasagna/1/src/main/java/Lasagna.java: -------------------------------------------------------------------------------- 1 | public class Lasagna { 2 | // TODO: define the 'expectedMinutesInOven()' method 3 | public int expectedMinutesInOven() { 4 | return 40; 5 | } 6 | 7 | // TODO: define the 'remainingMinutesInOven()' method 8 | public int remainingMinutesInOven(int timeSpentInOven) { 9 | return this.expectedMinutesInOven() - timeSpentInOven; 10 | } 11 | 12 | // TODO: define the 'preparationTimeInMinutes()' method 13 | public int preparationTimeInMinutes(int layers) { 14 | return layers * 2; 15 | } 16 | 17 | // TODO: define the 'totalTimeInMinutes()' method 18 | public int totalTimeInMinutes(int layers, int timeSpentInOven) { 19 | return this.preparationTimeInMinutes(layers) + timeSpentInOven; 20 | } 21 | } -------------------------------------------------------------------------------- /solutions/jq/log-line-parser/1/log-line-parser.jq: -------------------------------------------------------------------------------- 1 | # We will get more in-depth about jq functions in a later lesson. 2 | # For now, your job is to implement the logic of the tasks in the function 3 | # bodies, so that they output the correct values. 4 | 5 | # We haven't covered regular expressions yet. 6 | # This function will remove leading and trailing whitespace 7 | # from the input string. 8 | def trim: sub("^\\s+"; "") | sub("\\s+$"; ""); 9 | 10 | # Task 1. Get message from a log line 11 | def message: 12 | . | split(" ") | .[1:] | join(" ") | trim 13 | ; 14 | 15 | # Task 2. Get log level from a log line 16 | def log_level: 17 | . | split("]") | .[0] | .[1:] | ascii_downcase 18 | ; 19 | 20 | # Task 3. Reformat a log line 21 | def reformat: 22 | [(. | message), " (", (. | log_level), ")"] | add 23 | ; 24 | -------------------------------------------------------------------------------- /solutions/common-lisp/log-levels/1/log-levels.lisp: -------------------------------------------------------------------------------- 1 | (defpackage :log-levels 2 | (:use :cl) 3 | (:export :log-message :log-severity :log-format)) 4 | 5 | (in-package :log-levels) 6 | 7 | (defun log-message (log-string) 8 | (subseq log-string 8)) 9 | 10 | (defun log-severity (log-string) 11 | (let ((level (subseq log-string 0 6))) 12 | (cond 13 | ((string-equal level "[info]") :everything-ok) 14 | ((string-equal level "[warn]") :getting-worried) 15 | ((string-equal level "[ohno]") :run-for-cover)))) 16 | 17 | (defun log-format (log-string) 18 | (case (log-severity log-string) 19 | (:everything-ok (string-downcase (log-message log-string))) 20 | (:getting-worried (string-capitalize (log-message log-string))) 21 | (:run-for-cover (string-upcase (log-message log-string))))) 22 | -------------------------------------------------------------------------------- /solutions/java/jedliks-toy-car/1/src/main/java/JedliksToyCar.java: -------------------------------------------------------------------------------- 1 | public class JedliksToyCar { 2 | 3 | private int distanceDriven = 0; 4 | private int batteryLevel = 100; 5 | 6 | public static JedliksToyCar buy() { 7 | return new JedliksToyCar(); 8 | } 9 | 10 | public String distanceDisplay() { 11 | return String.format("Driven %d meters", distanceDriven); 12 | } 13 | 14 | public String batteryDisplay() { 15 | if (batteryLevel > 0) { 16 | return String.format("Battery at %d%%", batteryLevel); 17 | } else { 18 | return "Battery empty"; 19 | } 20 | } 21 | 22 | public void drive() { 23 | if (batteryLevel > 0) { 24 | this.distanceDriven += 20; 25 | this.batteryLevel--; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /solutions/java/salary-calculator/1/src/main/java/SalaryCalculator.java: -------------------------------------------------------------------------------- 1 | public class SalaryCalculator { 2 | 3 | private static final double BASE_SALARY = 1000.0; 4 | private static final double MAX_SALARY = 2000.0; 5 | 6 | public double salaryMultiplier(int daysSkipped) { 7 | return daysSkipped >= 5 ? 0.85 : 1.0; 8 | } 9 | 10 | public int bonusMultiplier(int productsSold) { 11 | return productsSold >= 20 ? 13 : 10; 12 | } 13 | 14 | public double bonusForProductsSold(int productsSold) { 15 | return productsSold * bonusMultiplier(productsSold); 16 | } 17 | 18 | public double finalSalary(int daysSkipped, int productsSold) { 19 | return Math.min(MAX_SALARY, salaryMultiplier(daysSkipped) * BASE_SALARY + bonusForProductsSold(productsSold)); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /solutions/arm64-assembly/leap/1/leap.s: -------------------------------------------------------------------------------- 1 | .text 2 | .globl leap_year 3 | 4 | // args 5 | // x0 - year 6 | // return 7 | // x0 - TRUE (1) / FALSE (0) 8 | leap_year: 9 | // divide by 4 10 | mov x1, #4 11 | udiv x2, x0, x1 12 | mul x3, x2, x1 13 | cmp x0, x3 14 | bne leap_year_no_leap_year 15 | 16 | // divide by 100 17 | mov x1, #100 18 | udiv x2, x0, x1 19 | mul x3, x2, x1 20 | cmp x0, x3 21 | bne leap_year_yes_leap_year 22 | 23 | // divide by 400 24 | mov x1, #400 25 | udiv x2, x0, x1 26 | mul x3, x2, x1 27 | cmp x0, x3 28 | bne leap_year_no_leap_year 29 | 30 | leap_year_yes_leap_year: 31 | mov x0, #1 32 | ret 33 | leap_year_no_leap_year: 34 | mov x0, #0 35 | ret 36 | -------------------------------------------------------------------------------- /solutions/common-lisp/high-scores/1/high-scores.lisp: -------------------------------------------------------------------------------- 1 | (defpackage :high-scores 2 | (:use :cl) 3 | (:export :make-high-scores-table :add-player 4 | :set-score :get-score :remove-player)) 5 | 6 | (in-package :high-scores) 7 | 8 | ;; Define make-high-scores-table function 9 | (defun make-high-scores-table () (make-hash-table)) 10 | 11 | ;; Define add-player function 12 | (defun add-player (hash-table player) 13 | (setf (gethash player hash-table) 0)) 14 | 15 | ;; Define set-score function 16 | (defun set-score (hash-table player score) 17 | (setf (gethash player hash-table) score)) 18 | 19 | ;; Define get-score function 20 | (defun get-score (hash-table player) 21 | (gethash player hash-table 0)) 22 | ;; Define remove-player function 23 | (defun remove-player (hash-table player) 24 | (remhash player hash-table)) 25 | -------------------------------------------------------------------------------- /solutions/common-lisp/isbn-verifier/1/isbn-verifier.lisp: -------------------------------------------------------------------------------- 1 | (defpackage :isbn-verifier 2 | (:use :cl) 3 | (:export :validp)) 4 | 5 | (in-package :isbn-verifier) 6 | 7 | (defun validp (isbn) 8 | (let ((parsed (parse-isbn isbn))) 9 | (when (= 10 (length parsed)) 10 | (let* ((products (mapcar #'* (parse-isbn isbn) '(10 9 8 7 6 5 4 3 2 1))) 11 | (sum (reduce #'+ products)) 12 | (rs (mod sum 11))) 13 | (when (zerop rs) t))))) 14 | 15 | (defun parse-isbn (isbn) 16 | (loop for c across (remove #\- isbn) 17 | for n = (digit-char-p c) 18 | collect (if n 19 | n 20 | (if (and (char= c #\X) 21 | (= (position #\X isbn) (- (length isbn) 1))) 22 | 10 23 | (return nil))))) 24 | -------------------------------------------------------------------------------- /solutions/java/football-match-reports/1/src/main/java/FootballMatchReports.java: -------------------------------------------------------------------------------- 1 | public class FootballMatchReports { 2 | public static String onField(int shirtNum) { 3 | switch(shirtNum) { 4 | case 1: 5 | return "goalie"; 6 | case 2: 7 | return "left back"; 8 | case 3: 9 | case 4: 10 | return "center back"; 11 | case 5: 12 | return "right back"; 13 | case 6: 14 | case 7: 15 | case 8: 16 | return "midfielder"; 17 | case 9: 18 | return "left wing"; 19 | case 10: 20 | return "striker"; 21 | case 11: 22 | return "right wing"; 23 | } 24 | return "invalid"; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /solutions/julia/currency-exchange/1/currency-exchange.jl: -------------------------------------------------------------------------------- 1 | function exchange_money(budget, exchange_rate) 2 | budget / exchange_rate 3 | end 4 | 5 | function get_change(budget, exchanging_value) 6 | budget - exchanging_value 7 | end 8 | 9 | function get_value_of_bills(denomination, number_of_bills) 10 | denomination * number_of_bills 11 | end 12 | 13 | function get_number_of_bills(amount, denomination) 14 | floor(Int, amount / denomination) 15 | end 16 | 17 | function get_leftover_of_bills(amount, denomination) 18 | amount % denomination 19 | end 20 | 21 | function exchangeable_value(budget, exchange_rate, spread, denomination) 22 | real_exchange_rate = exchange_rate + (exchange_rate * spread / 100) 23 | bills = get_number_of_bills(exchange_money(budget, real_exchange_rate), denomination) 24 | bills * denomination 25 | end 26 | -------------------------------------------------------------------------------- /solutions/arm64-assembly/darts/1/darts.s: -------------------------------------------------------------------------------- 1 | .text 2 | .globl score 3 | 4 | // d0 - F64 x 5 | // d1 - F64 y 6 | score: 7 | stp fp, lr, [sp, #-0x10]! 8 | mov fp, sp 9 | 10 | fmul d0, d0, d0 11 | fmul d1, d1, d1 12 | fadd d0, d0, d1 13 | fsqrt d0, d0 14 | 15 | fmov d1, #10.0 16 | fcmp d0, d1 17 | bgt zero_points 18 | 19 | fmov d1, #5.0 20 | fcmp d0, d1 21 | bgt one_point 22 | 23 | fmov d1, #1.0 24 | fcmp d0, d1 25 | bgt five_points 26 | 27 | // otherwise 10 points 28 | mov w0, #10 29 | exit: 30 | ldp fp, lr, [sp], #0x10 31 | ret 32 | 33 | zero_points: 34 | mov w0, #0 35 | b exit 36 | one_point: 37 | mov w0, #1 38 | b exit 39 | five_points: 40 | mov w0, #5 41 | b exit 42 | -------------------------------------------------------------------------------- /solutions/common-lisp/binary-search/1/binary-search.lisp: -------------------------------------------------------------------------------- 1 | (defpackage :binary-search 2 | (:use :cl) 3 | (:export :binary-find :value-error)) 4 | 5 | (in-package :binary-search) 6 | 7 | (defun binary-find (arr el) 8 | (when (not (zerop (length arr))) 9 | (binary-find-aux arr el 0 (- (length arr) 1)))) 10 | 11 | (defun binary-find-aux (arr el start end) 12 | (if (= start end) 13 | (when (= (aref arr start) el) 14 | start) 15 | (let* ((medium (floor (+ start end) 2)) 16 | (medium-value (aref arr medium)) 17 | ) 18 | (cond 19 | ((= medium-value el) medium) 20 | ((and (> medium-value el) (> medium start)) (binary-find-aux arr el start (- medium 1))) 21 | ((and (< medium-value el) (< medium end)) (binary-find-aux arr el (+ 1 medium) end)))))) 22 | 23 | 24 | -------------------------------------------------------------------------------- /solutions/julia/elyses-enchantments/1/elyses-enchantments.jl: -------------------------------------------------------------------------------- 1 | function get_item(stack, position) 2 | stack[position] 3 | end 4 | 5 | function set_item!(stack, position, replacement_card) 6 | stack[position] = replacement_card 7 | stack 8 | end 9 | 10 | function insert_item_at_top!(stack, new_card) 11 | push!(stack, new_card) 12 | end 13 | 14 | function remove_item!(stack, position) 15 | deleteat!(stack, position) 16 | stack 17 | end 18 | 19 | function remove_item_from_top!(stack) 20 | pop!(stack) 21 | stack 22 | end 23 | 24 | function insert_item_at_bottom!(stack, new_card) 25 | pushfirst!(stack, new_card) 26 | stack 27 | end 28 | 29 | function remove_item_at_bottom!(stack) 30 | popfirst!(stack) 31 | stack 32 | end 33 | 34 | function check_size_of_stack(stack, stack_size) 35 | length(stack) == stack_size 36 | end 37 | -------------------------------------------------------------------------------- /solutions/julia/currency-exchange/2/currency-exchange.jl: -------------------------------------------------------------------------------- 1 | function exchange_money(budget, exchange_rate) 2 | budget / exchange_rate 3 | end 4 | 5 | function get_change(budget, exchanging_value) 6 | budget - exchanging_value 7 | end 8 | 9 | function get_value_of_bills(denomination, number_of_bills) 10 | denomination * number_of_bills 11 | end 12 | 13 | function get_number_of_bills(amount, denomination) 14 | floor(Int, amount / denomination) 15 | end 16 | 17 | function get_leftover_of_bills(amount, denomination) 18 | amount % denomination 19 | end 20 | 21 | function exchangeable_value(budget, exchange_rate, spread, denomination) 22 | real_exchange_rate = exchange_rate + (exchange_rate * spread / 100) 23 | bills = get_number_of_bills(exchange_money(budget, real_exchange_rate), denomination) 24 | get_value_of_bills(denomination, bills) 25 | end 26 | -------------------------------------------------------------------------------- /solutions/common-lisp/space-age/1/space-age.lisp: -------------------------------------------------------------------------------- 1 | (defpackage :space-age 2 | (:use :cl) 3 | (:export :on-mercury 4 | :on-venus 5 | :on-earth 6 | :on-mars 7 | :on-jupiter 8 | :on-saturn 9 | :on-uranus 10 | :on-neptune)) 11 | 12 | (in-package :space-age) 13 | 14 | (defun on-earth (x) (/ x 31557600)) 15 | 16 | (defun on-mercury (x) 17 | (/ (on-earth x) 0.2408467) 18 | ) 19 | 20 | (defun on-venus (x) 21 | (/ (on-earth x) 0.61519726) 22 | ) 23 | 24 | (defun on-mars (x) 25 | (/ (on-earth x) 1.8808158) 26 | ) 27 | 28 | (defun on-jupiter (x) 29 | (/ (on-earth x) 11.862615) 30 | ) 31 | 32 | (defun on-saturn (x) 33 | (/ (on-earth x) 29.447498) 34 | ) 35 | 36 | (defun on-uranus (x) 37 | (/ (on-earth x) 84.016846) 38 | ) 39 | 40 | (defun on-neptune (x) 41 | (/ (on-earth x) 164.791132) 42 | ) -------------------------------------------------------------------------------- /solutions/arm64-assembly/rna-transcription/1/rna_transcription.s: -------------------------------------------------------------------------------- 1 | .text 2 | .globl to_rna 3 | 4 | // x0 - buffer 5 | // x1 - dna 6 | to_rna: 7 | ldrb w2, [x1], 0x1 8 | cbz w2, exit 9 | cmp w2, 'G' 10 | beq set_c 11 | cmp w2, 'C' 12 | beq set_g 13 | cmp w2, 'T' 14 | beq set_a 15 | cmp w2, 'A' 16 | beq set_u 17 | b invalid 18 | 19 | set_c: 20 | mov w2, 'C' 21 | strb w2, [x0], 0x1 22 | b to_rna 23 | 24 | set_g: 25 | mov w2, 'G' 26 | strb w2, [x0], 0x1 27 | b to_rna 28 | 29 | set_a: 30 | mov w2, 'A' 31 | strb w2, [x0], 0x1 32 | b to_rna 33 | 34 | set_u: 35 | mov w2, 'U' 36 | strb w2, [x0], 0x1 37 | b to_rna 38 | 39 | invalid: 40 | ret 41 | 42 | exit: 43 | mov w2, #0 44 | strb w2, [x0] 45 | ret 46 | -------------------------------------------------------------------------------- /solutions/haskell/scrabble-score/1/src/Scrabble.hs: -------------------------------------------------------------------------------- 1 | module Scrabble (scoreLetter, scoreWord) where 2 | 3 | import Data.Char (toLower) 4 | import Data.Map (Map) 5 | import qualified Data.Map as Map 6 | 7 | scrabble = Map.fromList [ 8 | ('a', 1), 9 | ('e', 1), 10 | ('i', 1), 11 | ('o', 1), 12 | ('u', 1), 13 | ('l', 1), 14 | ('n', 1), 15 | ('r', 1), 16 | ('s', 1), 17 | ('t', 1), 18 | ('d', 2), 19 | ('g', 2), 20 | ('b', 3), 21 | ('c', 3), 22 | ('m', 3), 23 | ('p', 3), 24 | ('f', 4), 25 | ('h', 4), 26 | ('v', 4), 27 | ('w', 4), 28 | ('y', 4), 29 | ('k', 5), 30 | ('j', 8), 31 | ('x', 8), 32 | ('q', 10), 33 | ('z', 10) 34 | ] 35 | 36 | scoreLetter :: Char -> Integer 37 | scoreLetter letter = Map.findWithDefault 0 (toLower letter) scrabble 38 | 39 | scoreWord :: String -> Integer 40 | scoreWord word = sum $ map scoreLetter word 41 | -------------------------------------------------------------------------------- /solutions/java/annalyns-infiltration/1/src/main/java/AnnalynsInfiltration.java: -------------------------------------------------------------------------------- 1 | class AnnalynsInfiltration { 2 | public static boolean canFastAttack(boolean knightIsAwake) { 3 | return !knightIsAwake; 4 | } 5 | 6 | public static boolean canSpy(boolean knightIsAwake, boolean archerIsAwake, boolean prisonerIsAwake) { 7 | return knightIsAwake || archerIsAwake || prisonerIsAwake; 8 | } 9 | 10 | public static boolean canSignalPrisoner(boolean archerIsAwake, boolean prisonerIsAwake) { 11 | return prisonerIsAwake && !archerIsAwake; 12 | } 13 | 14 | public static boolean canFreePrisoner(boolean knightIsAwake, boolean archerIsAwake, boolean prisonerIsAwake, boolean petDogIsPresent) { 15 | return ( 16 | petDogIsPresent && !archerIsAwake 17 | ) || ( 18 | !petDogIsPresent && prisonerIsAwake && !knightIsAwake && !archerIsAwake 19 | ); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /solutions/java/lasagna/2/src/main/java/Lasagna.java: -------------------------------------------------------------------------------- 1 | public class Lasagna { 2 | private static final int EXPECTED_MINUTES_IN_OVEN = 40; 3 | private static final int PREPARATION_TIME_PER_LAYER = 2; 4 | 5 | public int expectedMinutesInOven() { 6 | return EXPECTED_MINUTES_IN_OVEN; 7 | } 8 | 9 | // TODO: define the 'remainingMinutesInOven()' method 10 | public int remainingMinutesInOven(int timeSpentInOven) { 11 | return this.expectedMinutesInOven() - timeSpentInOven; 12 | } 13 | 14 | // TODO: define the 'preparationTimeInMinutes()' method 15 | public int preparationTimeInMinutes(int layers) { 16 | return layers * PREPARATION_TIME_PER_LAYER; 17 | } 18 | 19 | // TODO: define the 'totalTimeInMinutes()' method 20 | public int totalTimeInMinutes(int layers, int timeSpentInOven) { 21 | return this.preparationTimeInMinutes(layers) + timeSpentInOven; 22 | } 23 | } -------------------------------------------------------------------------------- /solutions/common-lisp/character-study/1/character-study.lisp: -------------------------------------------------------------------------------- 1 | (defpackage :character-study 2 | (:use :cl) 3 | (:export 4 | :compare-chars 5 | :size-of-char 6 | :change-size-of-char 7 | :type-of-char)) 8 | (in-package :character-study) 9 | 10 | (defun compare-chars (char1 char2) 11 | (cond 12 | ((char> char1 char2) :greater-than) 13 | ((char= char1 char2) :equal-to) 14 | (t :less-than))) 15 | 16 | (defun size-of-char (char) 17 | (cond 18 | ((upper-case-p char) :big) 19 | ((lower-case-p char) :small) 20 | (t :no-size))) 21 | 22 | (defun change-size-of-char (char wanted-size) 23 | (case wanted-size 24 | (:big (char-upcase char)) 25 | (:small (char-downcase char)))) 26 | 27 | (defun type-of-char (char) 28 | (cond 29 | ((alpha-char-p char) :alpha) 30 | ((digit-char-p char) :numeric) 31 | ((char= #\Space char) :space) 32 | ((char= #\Newline char) :newline) 33 | (t :unknown))) 34 | -------------------------------------------------------------------------------- /solutions/rust/scrabble-score/1/src/lib.rs: -------------------------------------------------------------------------------- 1 | use std::collections::HashMap; 2 | 3 | pub fn score(word: &str) -> u64 { 4 | let scrabble: HashMap = HashMap::from([ 5 | ('a', 1), 6 | ('e', 1), 7 | ('i', 1), 8 | ('o', 1), 9 | ('u', 1), 10 | ('l', 1), 11 | ('n', 1), 12 | ('r', 1), 13 | ('s', 1), 14 | ('t', 1), 15 | ('d', 2), 16 | ('g', 2), 17 | ('b', 3), 18 | ('c', 3), 19 | ('m', 3), 20 | ('p', 3), 21 | ('f', 4), 22 | ('h', 4), 23 | ('v', 4), 24 | ('w', 4), 25 | ('y', 4), 26 | ('k', 5), 27 | ('j', 8), 28 | ('x', 8), 29 | ('q', 10), 30 | ('z', 10), 31 | ]); 32 | 33 | let mut score = 0; 34 | for c in word.to_ascii_lowercase().chars() { 35 | score += *(scrabble.get(&c).unwrap_or(&0)); 36 | } 37 | score 38 | } 39 | -------------------------------------------------------------------------------- /solutions/clojure/annalyns-infiltration/1/src/annalyns_infiltration.clj: -------------------------------------------------------------------------------- 1 | (ns annalyns-infiltration) 2 | 3 | (defn can-fast-attack? 4 | "Returns true if a fast-attack can be made, false otherwise." 5 | [knight-awake?] 6 | (not knight-awake?)) 7 | 8 | (defn can-spy? 9 | "Returns true if the kidnappers can be spied upon, false otherwise." 10 | [knight-awake? archer-awake? prisoner-awake?] 11 | (or knight-awake? archer-awake? prisoner-awake?)) 12 | 13 | (defn can-signal-prisoner? 14 | "Returns true if the prisoner can be signalled, false otherwise." 15 | [archer-awake? prisoner-awake?] 16 | (and prisoner-awake? (not archer-awake?))) 17 | 18 | (defn can-free-prisoner? 19 | "Returns true if prisoner can be freed, false otherwise." 20 | [knight-awake? archer-awake? prisoner-awake? dog-present?] 21 | (or (and dog-present? (not archer-awake?)) 22 | (and (not dog-present?) prisoner-awake? (not knight-awake?) (not archer-awake?)))) 23 | -------------------------------------------------------------------------------- /solutions/common-lisp/resistor-color-duo/1/resistor-color-duo.lisp: -------------------------------------------------------------------------------- 1 | (defpackage :resistor-color-duo 2 | (:use :cl) 3 | (:export :value)) 4 | 5 | (in-package :resistor-color-duo) 6 | 7 | (defparameter *color-digits* '( 8 | ("black" . 0) 9 | ("brown" . 1) 10 | ("red" . 2) 11 | ("orange" . 3) 12 | ("yellow" . 4) 13 | ("green" . 5) 14 | ("blue" . 6) 15 | ("violet" . 7) 16 | ("grey" . 8) 17 | ("white" . 9))) 18 | 19 | (defun value (colors) 20 | (let ((first-value (color-digit (car colors))) 21 | (second-value (color-digit (car (cdr colors))))) 22 | (+ (* first-value 10) second-value))) 23 | 24 | (defun color-digit (color) 25 | (cdr (assoc color *color-digits* :test #'string=))) -------------------------------------------------------------------------------- /solutions/common-lisp/socks-and-sexprs/1/socks-and-sexprs.lisp: -------------------------------------------------------------------------------- 1 | (defpackage :socks-and-sexprs 2 | (:use :cl) 3 | (:export :lennys-favorite-food :lennys-secret-keyword 4 | :is-an-atom-p :is-a-cons-p :first-thing :rest-of-it)) 5 | 6 | (in-package :socks-and-sexprs) 7 | 8 | ;; Evaluates to some symbol (not a keyword) 9 | (defun lennys-favorite-food () 10 | 'lasagna 11 | ) 12 | 13 | ;; Evaluates to some keyword 14 | (defun lennys-secret-keyword () 15 | :aliens-are-real 16 | ) 17 | 18 | ;; Evaluates to T if THING is an atom, NIL otherwise 19 | (defun is-an-atom-p (thing) 20 | (atom thing) 21 | ) 22 | 23 | ;; Evaluates to T if THING is a cons, NIL otherwise 24 | (defun is-a-cons-p (thing) 25 | (not (is-an-atom-p thing)) 26 | ) 27 | 28 | ;; Evaluates to the first part of CONS 29 | (defun first-thing (cons) 30 | (car cons) 31 | ) 32 | 33 | ;; Evaluates to the 'rest' of the CONS 34 | (defun rest-of-it (cons) 35 | (cdr cons) 36 | ) 37 | -------------------------------------------------------------------------------- /solutions/common-lisp/reporting-for-duty/1/reporting-for-duty.lisp: -------------------------------------------------------------------------------- 1 | (defpackage :reporting-for-duty 2 | (:use :cl) 3 | (:export :format-quarter-value :format-two-quarters 4 | :format-two-quarters-for-reading)) 5 | 6 | (in-package :reporting-for-duty) 7 | 8 | ;; Define format-quarter-value function. 9 | (defun format-quarter-value (str num) 10 | (format nil "The value ~a quarter: ~a" str num)) 11 | ;; Define format-two-quarters function. 12 | (defun format-two-quarters (stream quarter1-label quarter1-num quarter2-label quarter2-num) 13 | (format stream "~%The value ~a quarter: ~a~&The value ~a quarter: ~a~%" quarter1-label quarter1-num quarter2-label quarter2-num)) 14 | 15 | ;; Define format-two-quarters-for-reading function. 16 | (defun format-two-quarters-for-reading (stream quarter1-label quarter1-num quarter2-label quarter2-num) 17 | (format stream "~s" (list (format-quarter-value quarter1-label quarter1-num) (format-quarter-value quarter2-label quarter2-num)))) 18 | -------------------------------------------------------------------------------- /solutions/emacs-lisp/allergies/1/allergies.el: -------------------------------------------------------------------------------- 1 | ;;; allergies.el --- Allergies Exercise (exercism) -*- lexical-binding: t; -*- 2 | 3 | ;;; Commentary: 4 | 5 | ;;; Code: 6 | 7 | (defvar *allergens* '( 8 | ("eggs" . 1) 9 | ("peanuts" . 2) 10 | ("shellfish" . 4) 11 | ("strawberries" . 8) 12 | ("tomatoes" . 16) 13 | ("chocolate" . 32) 14 | ("pollen" . 64) 15 | ("cats" . 128))) 16 | 17 | (defun allergen-list (score) 18 | (let ((a '())) 19 | (nreverse (dolist (i *allergens* a) 20 | (when (allergic-to-p score (car i)) 21 | (push (car i) a)))))) 22 | 23 | 24 | (defun allergic-to-p (score allergen) 25 | (when (not (zerop (logand score 26 | (cdr (assoc allergen *allergens*))))) 27 | t)) 28 | 29 | (provide 'allergies) 30 | ;;; allergies.el ends here 31 | -------------------------------------------------------------------------------- /solutions/common-lisp/pal-picker/1/pal-picker.lisp: -------------------------------------------------------------------------------- 1 | (defpackage :pal-picker 2 | (:use :cl) 3 | (:export :pal-picker :habitat-fitter :feeding-time-p 4 | :pet :play-fetch)) 5 | 6 | (in-package :pal-picker) 7 | 8 | (defun pal-picker (personality) 9 | (case personality 10 | (:lazy "Cat") 11 | (:energetic "Dog") 12 | (:quiet "Fish") 13 | (:hungry "Rabbit") 14 | (:talkative "Bird") 15 | (otherwise "I don't know... A dragon?") 16 | ) 17 | ) 18 | 19 | (defun habitat-fitter (weight) 20 | (cond 21 | ((>= weight 40) :massive) 22 | ((>= weight 20) :large) 23 | ((>= weight 10) :medium) 24 | ((>= weight 1) :small) 25 | (t :just-your-imagination) 26 | ) 27 | ) 28 | 29 | (defun feeding-time-p (fullness) 30 | (if (> fullness 20) 31 | "All is well." 32 | "It's feeding time!" 33 | ) 34 | ) 35 | 36 | (defun pet (pet) 37 | (when (string= "Fish" pet) t) 38 | ) 39 | 40 | (defun play-fetch (pet) 41 | (unless (string= pet "Dog") t)) 42 | -------------------------------------------------------------------------------- /solutions/jq/bird-count/1/bird-count.jq: -------------------------------------------------------------------------------- 1 | # The input will be an array of arrays representing the daily bird counts. 2 | # The data is organized into weekly arrays. 3 | # The data is organized chronologically: 4 | # - this week's data is the last element of the array, and 5 | # - today's count is the last element of this week's data 6 | 7 | # The output of this exercise is a JSON object, 8 | # one key:value pair per task. 9 | 10 | { 11 | # Task 1: output the array for last week's data 12 | last_week: (.[(. | length | - 2)]), 13 | 14 | # Task 2: output count for yesterday in this week's data 15 | yesterday: (. | last | .[(. | length | - 2)]), 16 | 17 | # Task 3: output sum of counts for this week's data 18 | total: (. | last | add), 19 | 20 | # Task 4: output number of days with 5 or more birds in this week's data 21 | busy_days: (. | last | map(select( . > 4)) | length), 22 | 23 | # Task 5: output true if any day this week has zero birds 24 | has_day_without_birds: (. | last | any(. == 0)) 25 | } 26 | -------------------------------------------------------------------------------- /solutions/kotlin/scrabble-score/1/src/main/kotlin/ScrabbleScore.kt: -------------------------------------------------------------------------------- 1 | object ScrabbleScore { 2 | 3 | private val letters = mapOf( 4 | 'a' to 1, 5 | 'e' to 1, 6 | 'i' to 1, 7 | 'o' to 1, 8 | 'u' to 1, 9 | 'l' to 1, 10 | 'n' to 1, 11 | 'r' to 1, 12 | 's' to 1, 13 | 't' to 1, 14 | 'd' to 2, 15 | 'g' to 2, 16 | 'b' to 3, 17 | 'c' to 3, 18 | 'm' to 3, 19 | 'p' to 3, 20 | 'f' to 4, 21 | 'h' to 4, 22 | 'v' to 4, 23 | 'w' to 4, 24 | 'y' to 4, 25 | 'k' to 5, 26 | 'j' to 8, 27 | 'x' to 8, 28 | 'q' to 10, 29 | 'z' to 10 30 | ) 31 | 32 | fun scoreLetter(c: Char): Int = 33 | letters.getOrDefault(c.lowercase()[0], 0) 34 | 35 | fun scoreWord(word: String): Int { 36 | var score = 0 37 | for (c in word) { 38 | score += scoreLetter(c) 39 | } 40 | return score 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /solutions/java/remote-control-competition/1/src/main/java/ProductionRemoteControlCar.java: -------------------------------------------------------------------------------- 1 | class ProductionRemoteControlCar implements RemoteControlCar, Comparable { 2 | 3 | private int distance = 0; 4 | private int victories = 0; 5 | 6 | public void drive() { 7 | distance += 10; 8 | } 9 | 10 | public int getDistanceTravelled() { 11 | return distance; 12 | } 13 | 14 | public int getNumberOfVictories() { 15 | return this.victories; 16 | } 17 | 18 | public void setNumberOfVictories(int numberOfVictories) { 19 | this.victories = numberOfVictories; 20 | } 21 | 22 | public int compareTo(ProductionRemoteControlCar other) { 23 | var victoriesA = getNumberOfVictories(); 24 | var victoriesB = other.getNumberOfVictories(); 25 | 26 | if (victoriesA > victoriesB) { 27 | return 1; 28 | } else if (victoriesA < victoriesB) { 29 | return -1; 30 | } else { 31 | return 0; 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /solutions/common-lisp/larrys-winning-checker/1/larrys-winning-checker.lisp: -------------------------------------------------------------------------------- 1 | (defpackage :larrys-winning-checker 2 | (:use :cl) 3 | (:export 4 | :make-empty-board 5 | :make-board-from-list 6 | :all-the-same-p 7 | :row 8 | :column)) 9 | 10 | (in-package :larrys-winning-checker) 11 | 12 | (defun make-empty-board () 13 | (make-array '(3 3) :initial-element nil)) 14 | 15 | (defun make-board-from-list (list) 16 | (make-array '(3 3) :initial-contents list)) 17 | 18 | (defun all-the-same-p (row-or-col) 19 | (when 20 | (and 21 | (eq (aref row-or-col 0) (aref row-or-col 1)) 22 | (eq (aref row-or-col 1) (aref row-or-col 2))) 23 | t 24 | )) 25 | 26 | (defun row (board row-num) 27 | (make-array 3 :initial-contents (list 28 | (aref board row-num 0) 29 | (aref board row-num 1) 30 | (aref board row-num 2)))) 31 | 32 | (defun column (board col-num) 33 | (make-array 3 :initial-contents (list 34 | (aref board 0 col-num) 35 | (aref board 1 col-num) 36 | (aref board 2 col-num)))) 37 | -------------------------------------------------------------------------------- /solutions/common-lisp/leslies-lists/1/leslies-lists.lisp: -------------------------------------------------------------------------------- 1 | (defpackage :leslies-lists 2 | (:use :cl) 3 | (:export :new-list 4 | :list-of-things 5 | :add-to-list 6 | :first-thing 7 | :second-thing 8 | :third-thing 9 | :twenty-third-thing 10 | :remove-first-item 11 | :on-the-list-p 12 | :list-append 13 | :just-how-long 14 | :part-of-list 15 | :list-reverse)) 16 | 17 | (in-package :leslies-lists) 18 | 19 | (defun new-list () '()) 20 | 21 | (defun list-of-things (thing1 thing2 thing3) 22 | (list thing1 thing2 thing3) 23 | ) 24 | 25 | (defun add-to-list (item list) 26 | (cons item list) 27 | ) 28 | 29 | (defun first-thing (list) (car list)) 30 | 31 | (defun second-thing (list) (car (cdr list))) 32 | 33 | (defun third-thing (list) (third list)) 34 | 35 | (defun twenty-third-thing (list) (nth 22 list)) 36 | 37 | (defun remove-first-item (list) (cdr list)) 38 | 39 | (defun list-append (list1 list2) (append list1 list2)) 40 | 41 | (defun just-how-long (list) (length list)) 42 | -------------------------------------------------------------------------------- /solutions/common-lisp/lillys-lasagna/1/lillys-lasagna.lisp: -------------------------------------------------------------------------------- 1 | (defpackage :lillys-lasagna 2 | (:use :cl) 3 | (:export :expected-time-in-oven 4 | :remaining-minutes-in-oven 5 | :preparation-time-in-minutes 6 | :elapsed-time-in-minutes)) 7 | 8 | (in-package :lillys-lasagna) 9 | 10 | ;; Define function expected-time-in-oven 11 | (defun expected-time-in-oven () 12 | "Expected time in oven" 13 | 337) 14 | 15 | ;; Define function remaining-minutes-in-oven 16 | (defun remaining-minutes-in-oven (running-time) 17 | "Calculate remaining minutes in the oven provided a running time" 18 | (- (expected-time-in-oven) running-time) 19 | ) 20 | 21 | ;; Define function preparation-time-in-minutes 22 | (defun preparation-time-in-minutes (layers) 23 | "Calculate how many minutes Lily spent preparing the lasagna" 24 | (* layers 19) 25 | ) 26 | 27 | ;; Define function elapsed-time-in-minutes 28 | (defun elapsed-time-in-minutes (layers running-time) 29 | "Calculate how much time have been spend on preparing the lasagna" 30 | (+ running-time (preparation-time-in-minutes layers)) 31 | ) -------------------------------------------------------------------------------- /solutions/common-lisp/lillys-lasagna-leftovers/1/lillys-lasagna-leftovers.lisp: -------------------------------------------------------------------------------- 1 | (defpackage :lillys-lasagna-leftovers 2 | (:use :cl) 3 | (:export 4 | :preparation-time 5 | :remaining-minutes-in-oven 6 | :split-leftovers)) 7 | 8 | (in-package :lillys-lasagna-leftovers) 9 | 10 | ;; Define function preparation-time 11 | (defun preparation-time (&rest layers) 12 | (* 19 (length layers)) 13 | ) 14 | 15 | ;; Define function remaining-minutes-in-oven 16 | (defun remaining-minutes-in-oven (&optional (mode nil mode-supplied-p)) 17 | (if mode-supplied-p 18 | (if mode 19 | (case mode 20 | (:normal 337) 21 | (:shorter 237) 22 | (:very-short 137) 23 | (:longer 437) 24 | (:very-long 537) 25 | ) 26 | 0) 27 | 337 28 | )) 29 | ;; Define function split-leftovers 30 | (defun split-leftovers (&key (weight nil weight-provided-p) (human 10) (alien 10)) 31 | (if weight-provided-p 32 | (if weight 33 | (- weight (+ human alien)) 34 | :looks-like-someone-was-hungry 35 | ) 36 | :just-split-it)) -------------------------------------------------------------------------------- /solutions/x86-64-assembly/lasagna/1/lasagna.asm: -------------------------------------------------------------------------------- 1 | ; Everything that comes after a semicolon (;) is a comment 2 | 3 | ; Assembler-time constants may be defined using 'equ' 4 | MINUTES_IN_OVEN equ 40 5 | section .text 6 | 7 | ; You should implement functions in the .text section 8 | ; first argument is passed in rdi 9 | ; second argument is passed in rsi 10 | ; return value is placed in rax 11 | 12 | ; the global directive makes a function visible to the test files 13 | global expected_minutes_in_oven 14 | expected_minutes_in_oven: 15 | mov rax, MINUTES_IN_OVEN 16 | ret 17 | 18 | global remaining_minutes_in_oven 19 | remaining_minutes_in_oven: 20 | mov rax, MINUTES_IN_OVEN 21 | sub rax, rdi 22 | ret 23 | 24 | global preparation_time_in_minutes 25 | preparation_time_in_minutes: 26 | mov rax, rdi 27 | imul rax, 2 28 | ret 29 | 30 | global elapsed_time_in_minutes 31 | elapsed_time_in_minutes: 32 | mov rax, rdi 33 | imul rax, 2 34 | add rax, rsi 35 | ret 36 | 37 | %ifidn __OUTPUT_FORMAT__,elf64 38 | section .note.GNU-stack noalloc noexec nowrite progbits 39 | %endif 40 | -------------------------------------------------------------------------------- /solutions/java/squeaky-clean/1/src/main/java/SqueakyClean.java: -------------------------------------------------------------------------------- 1 | class SqueakyClean { 2 | static String clean(String identifier) { 3 | var sb = new StringBuilder(); 4 | boolean nextIsUpperCase = false; 5 | for(char c: identifier.toCharArray()) { 6 | if(Character.isWhitespace(c)) { 7 | sb.append('_'); 8 | } else if(c == '-') { 9 | nextIsUpperCase = true; 10 | } else if(c == '4') { 11 | sb.append('a'); 12 | } else if(c == '3') { 13 | sb.append('e'); 14 | } else if(c == '0') { 15 | sb.append('o'); 16 | } else if(c == '1') { 17 | sb.append('l'); 18 | } else if(c == '7') { 19 | sb.append('t'); 20 | } else if(nextIsUpperCase) { 21 | nextIsUpperCase = false; 22 | sb.append(Character.toUpperCase(c)); 23 | } else if(Character.isLetter(c)) { 24 | sb.append(c); 25 | } 26 | } 27 | return sb.toString(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /solutions/jq/assembly-line/1/assembly-line.jq: -------------------------------------------------------------------------------- 1 | # We will get more in-depth about jq functions in a later lesson. 2 | # A function begins with "def", ends with ";", and consists of a 3 | # single pipeline just like a "main" jq program. 4 | # 5 | # For now, your job is to implement the logic of the tasks in the function 6 | # bodies, so that they output the correct values. 7 | 8 | # Task 1: calculate the production rate per hour 9 | def production_rate_per_hour: 10 | # remove the constant and implement this function 11 | . * 221 * ( 12 | if . < 5 then 1 13 | elif . < 9 then 0.9 14 | elif . < 10 then 0.8 15 | else 0.77 16 | end) 17 | ; 18 | 19 | # Task 2: calculate the number of working items produced per minute 20 | def working_items_per_minute: 21 | # remove the constant and implement this function 22 | production_rate_per_hour | . / 60 | floor 23 | ; 24 | 25 | 26 | # Please don't change the line below: it is responsible for passing 27 | # the input speed value (a number between 0 and 10 inclusive) 28 | # to the two functions defined above. 29 | # 30 | .speed | (production_rate_per_hour, working_items_per_minute) 31 | -------------------------------------------------------------------------------- /solutions/java/calculator-conundrum/1/src/main/java/CalculatorConundrum.java: -------------------------------------------------------------------------------- 1 | class CalculatorConundrum { 2 | public String calculate(int operand1, int operand2, String operation) { 3 | if (operation == "+") { 4 | return String.format("%d + %d = %d", operand1, operand2, operand1 + operand2); 5 | } 6 | if (operation == "*") { 7 | return String.format("%d * %d = %d", operand1, operand2, operand1 * operand2); 8 | } 9 | if (operation == "/") { 10 | try { 11 | return String.format("%d / %d = %d", operand1, operand2, operand1 / operand2); 12 | } catch (ArithmeticException e) { 13 | throw new IllegalOperationException("Division by zero is not allowed", e); 14 | } 15 | } 16 | if (operation == null) { 17 | throw new IllegalArgumentException("Operation cannot be null"); 18 | } 19 | if (operation == "") { 20 | throw new IllegalArgumentException("Operation cannot be empty"); 21 | } 22 | throw new IllegalOperationException("Operation '" + operation + "' does not exist"); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /solutions/java/karls-languages/1/src/main/java/LanguageList.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.List; 3 | 4 | public class LanguageList { 5 | private final List languages = new ArrayList<>(); 6 | private static final String[] EXCITING_LANGUAGES = new String[] { "Java", "Kotlin"}; 7 | 8 | public boolean isEmpty() { 9 | return languages.size() == 0; 10 | } 11 | 12 | public void addLanguage(String language) { 13 | languages.add(language); 14 | } 15 | 16 | public void removeLanguage(String language) { 17 | languages.remove(language); 18 | } 19 | 20 | public String firstLanguage() { 21 | return languages.get(0); 22 | } 23 | 24 | public int count() { 25 | return languages.size(); 26 | } 27 | 28 | public boolean containsLanguage(String language) { 29 | return languages.contains(language); 30 | } 31 | 32 | public boolean isExciting() { 33 | for(String excitingLanguage : EXCITING_LANGUAGES) { 34 | if (languages.contains(excitingLanguage)) { 35 | return true; 36 | } 37 | } 38 | return false; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /solutions/clojure/lucians-luscious-lasagna/1/src/lucians_luscious_lasagna.clj: -------------------------------------------------------------------------------- 1 | ;; 👋🏽 Hi there! Welcome to the Clojure Track. 2 | ;; The online test-runner is powered by babashka and the Small Clojure Interpreter (SCI). 3 | ;; Almost all language features are supported, with the exception of low-level constructs 4 | ;; like `deftype`, and certain Java classes. For more info, see: 5 | ;; https://github.com/babashka/babashka#differences-with-clojure 6 | 7 | (ns lucians-luscious-lasagna) 8 | 9 | (def expected-time 40) 10 | 11 | (defn remaining-time 12 | "Takes the actual time in minutes the lasagna has been in the oven, 13 | and returns how many minutes the lasagna still has to remain in the oven." 14 | [actual-time] 15 | (- expected-time actual-time) 16 | ) 17 | 18 | (defn prep-time 19 | "Takes the number of layers added to the lasagna, 20 | and returns how many minutes you spent preparing the lasagna" 21 | [num-layers] 22 | (* num-layers 2) 23 | ) 24 | 25 | (defn total-time 26 | "Takes the number of layers of lasagna and the actual time in minutes it has been in the oven. 27 | Returns how many minutes in total you've worked on cooking the lasagna" 28 | [num-layers actual-time] 29 | (+ (prep-time num-layers) actual-time) 30 | ) 31 | -------------------------------------------------------------------------------- /solutions/julia/inventory-management/1/inventory-management.jl: -------------------------------------------------------------------------------- 1 | function create_inventory(items) 2 | dict = Dict() 3 | for item in items 4 | if item in keys(dict) 5 | dict[item] += 1 6 | else 7 | dict[item] = 1 8 | end 9 | end 10 | dict 11 | end 12 | 13 | function add_items(inventory, items) 14 | dict = copy(inventory) 15 | for item in items 16 | if item in keys(dict) 17 | dict[item] += 1 18 | else 19 | dict[item] = 1 20 | end 21 | end 22 | dict 23 | end 24 | 25 | function decrement_items(inventory, items) 26 | dict = copy(inventory) 27 | for item in items 28 | if item in keys(dict) 29 | dict[item] -= 1 30 | if dict[item] < 0 31 | dict[item] = 0 32 | end 33 | end 34 | end 35 | dict 36 | end 37 | 38 | function remove_item(inventory, item) 39 | dict = copy(inventory) 40 | delete!(dict, item) 41 | dict 42 | end 43 | 44 | function list_inventory(inventory) 45 | vect = Vector() 46 | for key in sort([key for key in keys(inventory)]) 47 | if inventory[key] > 0 48 | push!(vect, key => inventory[key]) 49 | end 50 | end 51 | vect 52 | end 53 | -------------------------------------------------------------------------------- /solutions/java/need-for-speed/1/src/main/java/NeedForSpeed.java: -------------------------------------------------------------------------------- 1 | class NeedForSpeed { 2 | 3 | private final int speed; 4 | private final int batteryDrain; 5 | public int distance = 0; 6 | public int battery = 100; 7 | 8 | NeedForSpeed(int speed, int batteryDrain) { 9 | this.speed = speed; 10 | this.batteryDrain = batteryDrain; 11 | } 12 | 13 | public boolean batteryDrained() { 14 | return this.battery < batteryDrain; 15 | } 16 | 17 | public int distanceDriven() { 18 | return this.distance; 19 | } 20 | 21 | public void drive() { 22 | if (!this.batteryDrained()) { 23 | this.distance += this.speed; 24 | this.battery -= this.batteryDrain; 25 | } 26 | } 27 | 28 | public static NeedForSpeed nitro() { 29 | return new NeedForSpeed(50, 4); 30 | } 31 | } 32 | 33 | class RaceTrack { 34 | private int distance; 35 | 36 | RaceTrack(int distance) { 37 | this.distance = distance; 38 | } 39 | 40 | public boolean canFinishRace(NeedForSpeed car) { 41 | while(car.distanceDriven() < this.distance) { 42 | if (car.batteryDrained()) { 43 | return false; 44 | } 45 | car.drive(); 46 | } 47 | return true; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /solutions/java/need-for-speed/2/src/main/java/NeedForSpeed.java: -------------------------------------------------------------------------------- 1 | class NeedForSpeed { 2 | 3 | private final int speed; 4 | private final int batteryDrain; 5 | private int distance = 0; 6 | private int battery = 100; 7 | 8 | NeedForSpeed(int speed, int batteryDrain) { 9 | this.speed = speed; 10 | this.batteryDrain = batteryDrain; 11 | } 12 | 13 | public boolean batteryDrained() { 14 | return this.battery < batteryDrain; 15 | } 16 | 17 | public int distanceDriven() { 18 | return this.distance; 19 | } 20 | 21 | public void drive() { 22 | if (!this.batteryDrained()) { 23 | this.distance += this.speed; 24 | this.battery -= this.batteryDrain; 25 | } 26 | } 27 | 28 | public static NeedForSpeed nitro() { 29 | return new NeedForSpeed(50, 4); 30 | } 31 | } 32 | 33 | class RaceTrack { 34 | private int distance; 35 | 36 | RaceTrack(int distance) { 37 | this.distance = distance; 38 | } 39 | 40 | public boolean canFinishRace(NeedForSpeed car) { 41 | while(car.distanceDriven() < this.distance) { 42 | if (car.batteryDrained()) { 43 | return false; 44 | } 45 | car.drive(); 46 | } 47 | return true; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /solutions/rust/rna-transcription/1/src/lib.rs: -------------------------------------------------------------------------------- 1 | #[derive(Debug, PartialEq, Eq)] 2 | pub struct Dna { 3 | strand: String, 4 | } 5 | 6 | #[derive(Debug, PartialEq, Eq)] 7 | pub struct Rna { 8 | strand: String, 9 | } 10 | 11 | impl Dna { 12 | pub fn new(dna: &str) -> Result { 13 | for (i, c) in dna.chars().enumerate() { 14 | if c != 'G' && c != 'C' && c != 'T' && c != 'A' { 15 | return Err(i) 16 | } 17 | } 18 | Ok(Dna { strand: dna.to_owned() }) 19 | } 20 | 21 | pub fn into_rna(self) -> Rna { 22 | let rna_strand = self.strand 23 | .chars() 24 | .map(|c| match c { 25 | 'G' => 'C', 26 | 'C' => 'G', 27 | 'T' => 'A', 28 | 'A' => 'U', 29 | _ => panic!("should not happen if DNA has been validated before") 30 | }) 31 | .collect::(); 32 | Rna { strand: rna_strand } 33 | } 34 | } 35 | 36 | impl Rna { 37 | pub fn new(rna: &str) -> Result { 38 | for (i, c) in rna.chars().enumerate() { 39 | if c != 'G' && c != 'C' && c != 'U' && c != 'A' { 40 | return Err(i) 41 | } 42 | } 43 | Ok(Rna { strand: rna.to_owned() }) 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /solutions/java/wizards-and-warriors/1/src/main/java/Fighter.java: -------------------------------------------------------------------------------- 1 | class Fighter { 2 | 3 | boolean isVulnerable() { 4 | return true; 5 | } 6 | 7 | int getDamagePoints(Fighter fighter) { 8 | return 1; 9 | } 10 | } 11 | 12 | class Warrior extends Fighter { 13 | @Override 14 | public String toString() { 15 | return "Fighter is a Warrior"; 16 | } 17 | 18 | @Override 19 | public boolean isVulnerable() { 20 | return false; 21 | } 22 | 23 | @Override 24 | public int getDamagePoints(Fighter fighter) { 25 | if (fighter.isVulnerable()) { 26 | return 10; 27 | } else { 28 | return 6; 29 | } 30 | } 31 | } 32 | 33 | class Wizard extends Fighter { 34 | private boolean spellPrepared = false; 35 | 36 | @Override 37 | public String toString() { 38 | return "Fighter is a Wizard"; 39 | } 40 | 41 | public void prepareSpell() { 42 | spellPrepared = true; 43 | } 44 | 45 | @Override 46 | public boolean isVulnerable() { 47 | return !spellPrepared; 48 | } 49 | 50 | @Override 51 | public int getDamagePoints(Fighter fighter) { 52 | if (spellPrepared) { 53 | return 12; 54 | } else { 55 | return 3; 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /solutions/jq/shopping/1/shopping.jq: -------------------------------------------------------------------------------- 1 | # Note, the expressions for the first three tasks must end with commas. 2 | # 3 | # Recall that a jq program is a *single expression*. For this exercise, 4 | # you are solving four separate tasks with one source file. Use the 5 | # comma operator so the outputs of these filters are joined into a 6 | # single output stream. jq prints each item in a data stream on a 7 | # separate line, which makes the tests easier to write. 8 | # 9 | # This is an error: 10 | # jq -n ' 11 | # 1 12 | # 2 13 | # 3 14 | # ' 15 | # 16 | # With commas, it is OK: 17 | # jq -n ' 18 | # 1, 19 | # 2, 20 | # 3 21 | # ' 22 | # 23 | # You'll need to use parentheses to control order of operations 24 | 25 | # Task 1: replace `null` with the "name" element of the shopping list. 26 | .name, 27 | 28 | # Task 2: replace `null` with the count of the required ingredients. 29 | (.ingredients | length), 30 | 31 | # Task 3: replace `null` with the amount of sugar. 32 | (.ingredients[] | select(.item == "sugar") | .amount.quantity), 33 | 34 | # Task 4: replace `null` with the mapping of ingredient names with their substitutions 35 | # (no comma after the last filter) 36 | ( 37 | (.ingredients + .["optional ingredients"]) 38 | | map(select(.substitute) | {(.item): (.substitute)}) 39 | | add 40 | ) 41 | -------------------------------------------------------------------------------- /solutions/java/booking-up-for-beauty/1/src/main/java/AppointmentScheduler.java: -------------------------------------------------------------------------------- 1 | import java.time.LocalDate; 2 | import java.time.LocalDateTime; 3 | import java.time.format.DateTimeFormatter; 4 | 5 | class AppointmentScheduler { 6 | 7 | public LocalDateTime schedule(String appointmentDateDescription) { 8 | var parser = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss"); 9 | return LocalDateTime.parse(appointmentDateDescription, parser); 10 | } 11 | 12 | public boolean hasPassed(LocalDateTime appointmentDate) { 13 | return LocalDateTime.now().isAfter(appointmentDate); 14 | } 15 | 16 | public boolean isAfternoonAppointment(LocalDateTime appointmentDate) { 17 | var hour = appointmentDate.getHour(); 18 | return hour >= 12 && hour < 18; 19 | } 20 | 21 | public String getDescription(LocalDateTime appointmentDate) { 22 | var printer = DateTimeFormatter.ofPattern("EEEE, MMMM d, yyyy"); 23 | var printer2 = DateTimeFormatter.ofPattern("h:mm a."); 24 | return "You have an appointment on " + printer.format(appointmentDate) + ", at " + printer2.format(appointmentDate); 25 | } 26 | 27 | public LocalDate getAnniversaryDate() { 28 | return LocalDate.of( 29 | LocalDate.now().getYear(), 30 | 9, 31 | 15 32 | ); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /solutions/java/international-calling-connoisseur/1/src/main/java/DialingCodes.java: -------------------------------------------------------------------------------- 1 | import java.util.Map; 2 | import java.util.HashMap; 3 | 4 | public class DialingCodes { 5 | 6 | private final Map codes = new HashMap<>(); 7 | 8 | public Map getCodes() { 9 | return this.codes; 10 | } 11 | 12 | public void setDialingCode(Integer code, String country) { 13 | this.codes.put(code, country); 14 | } 15 | 16 | public String getCountry(Integer code) { 17 | return this.codes.get(code); 18 | } 19 | 20 | public void addNewDialingCode(Integer code, String country) { 21 | if (!codes.keySet().contains(code) && !codes.values().contains(country)) { 22 | setDialingCode(code, country); 23 | } 24 | } 25 | 26 | public Integer findDialingCode(String country) { 27 | for(Integer code: codes.keySet()) { 28 | if(codes.get(code).equals(country)) { 29 | return code; 30 | } 31 | } 32 | return null; 33 | } 34 | 35 | public void updateCountryDialingCode(Integer code, String country) { 36 | var previousCode = findDialingCode(country); 37 | if (previousCode != null) { 38 | codes.remove(previousCode); 39 | } 40 | addNewDialingCode(code, country); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /solutions/common-lisp/key-comparison/1/key-comparison.lisp: -------------------------------------------------------------------------------- 1 | (defpackage :key-comparison 2 | (:use :cl) 3 | (:export 4 | :key-object-identity 5 | :key-numbers 6 | :key-numbers-of-different-types 7 | :key-characters 8 | :key-characters-case-insensitively 9 | :key-strings 10 | :key-strings-case-insensitively 11 | :key-conses-of-symbols 12 | :key-conses-of-characters 13 | :key-conses-of-numbers 14 | :key-conses-of-characters-case-insensitively 15 | :key-conses-of-numbers-of-different-types 16 | :key-arrays 17 | :key-arrays-loosely)) 18 | (in-package :key-comparison) 19 | 20 | (defun key-object-identity (x y) (eql x y)) 21 | (defun key-numbers (x y) (eql x y)) 22 | (defun key-numbers-of-different-types (x y) (equalp x y)) 23 | (defun key-characters (x y) (char= x y)) 24 | (defun key-characters-case-insensitively (x y) (char-equal x y)) 25 | (defun key-strings (x y) (string= x y)) 26 | (defun key-strings-case-insensitively (x y) (string-equal x y)) 27 | (defun key-conses-of-symbols (x y) (equal x y)) 28 | (defun key-conses-of-characters (x y) (equal x y)) 29 | (defun key-conses-of-numbers (x y) (equal x y)) 30 | (defun key-conses-of-characters-case-insensitively (x y) (equalp x y)) 31 | (defun key-conses-of-numbers-of-different-types (x y) (equalp x y)) 32 | (defun key-arrays (x y) (eq x y)) 33 | (defun key-arrays-loosely (x y) (equalp x y)) 34 | -------------------------------------------------------------------------------- /solutions/java/bird-watcher/1/src/main/java/BirdWatcher.java: -------------------------------------------------------------------------------- 1 | 2 | class BirdWatcher { 3 | private final int[] birdsPerDay; 4 | 5 | public BirdWatcher(int[] birdsPerDay) { 6 | this.birdsPerDay = birdsPerDay.clone(); 7 | } 8 | 9 | public int[] getLastWeek() { 10 | return new int[] {0, 2, 5, 3, 7, 8, 4}; 11 | } 12 | 13 | public int getToday() { 14 | return this.birdsPerDay[this.birdsPerDay.length - 1]; 15 | } 16 | 17 | public void incrementTodaysCount() { 18 | this.birdsPerDay[this.birdsPerDay.length - 1] += 1; 19 | } 20 | 21 | public boolean hasDayWithoutBirds() { 22 | for(int birds : this.birdsPerDay) { 23 | if (birds == 0) { 24 | return true; 25 | } 26 | } 27 | return false; 28 | } 29 | 30 | public int getCountForFirstDays(int numberOfDays) { 31 | int total = 0; 32 | int days = Math.min(numberOfDays, this.birdsPerDay.length); 33 | for(int i = 0; i= 5) { 43 | busyDays++; 44 | } 45 | } 46 | return busyDays; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /solutions/julia/mixed-juices/1/mixed-juices.jl: -------------------------------------------------------------------------------- 1 | function time_to_mix_juice(juice) 2 | if juice == "Pure Strawberry Joy" 3 | return 0.5 4 | elseif juice == "Energizer" || juice == "Green Garden" 5 | return 1.5 6 | elseif juice == "Tropical Island" 7 | return 3 8 | elseif juice == "All or Nothing" 9 | return 5 10 | else 11 | return 2.5 12 | end 13 | end 14 | 15 | function wedges_from_lime(size) 16 | if size == "small" 17 | return 6 18 | elseif size == "medium" 19 | return 8 20 | else 21 | return 10 22 | end 23 | end 24 | 25 | function limes_to_cut(needed, limes) 26 | current = 0 27 | n_limes = 0 28 | for lime in limes 29 | if current >= needed 30 | return n_limes 31 | end 32 | current += wedges_from_lime(lime) 33 | n_limes += 1 34 | end 35 | 36 | return n_limes 37 | end 38 | 39 | function order_times(orders) 40 | times = Float64[] 41 | for order in orders 42 | push!(times, time_to_mix_juice(order)) 43 | end 44 | times 45 | end 46 | 47 | function remaining_orders(time_left, orders) 48 | time_used = 0 49 | order = 1 50 | while time_used < time_left && order <= length(orders) 51 | time_used += time_to_mix_juice(orders[order]) 52 | order += 1 53 | end 54 | 55 | orders[order:end] 56 | end 57 | -------------------------------------------------------------------------------- /solutions/emacs-lisp/scrabble-score/1/scrabble-score.el: -------------------------------------------------------------------------------- 1 | ;;; scrabble-score.el --- Scrabble Score (exercism) -*- lexical-binding: t; -*- 2 | 3 | ;;; Commentary: 4 | 5 | ;;; Code: 6 | 7 | (defconst +scrabble+ '( 8 | (?A . 1) 9 | (?E . 1) 10 | (?I . 1) 11 | (?O . 1) 12 | (?U . 1) 13 | (?L . 1) 14 | (?N . 1) 15 | (?R . 1) 16 | (?S . 1) 17 | (?T . 1) 18 | (?D . 2) 19 | (?G . 2) 20 | (?B . 3) 21 | (?C . 3) 22 | (?M . 3) 23 | (?P . 3) 24 | (?F . 4) 25 | (?H . 4) 26 | (?V . 4) 27 | (?W . 4) 28 | (?Y . 4) 29 | (?K . 5) 30 | (?J . 8) 31 | (?X . 8) 32 | (?Q . 10) 33 | (?Z . 10) 34 | )) 35 | 36 | (defun score (word) 37 | (apply #'+ (mapcar (lambda (x) 38 | (cdr (assoc (upcase x) +scrabble+))) 39 | (string-to-list word)))) 40 | 41 | (provide 'scrabble-score) 42 | ;;; scrabble-score.el ends here 43 | -------------------------------------------------------------------------------- /solutions/common-lisp/allergies/1/allergies.lisp: -------------------------------------------------------------------------------- 1 | (defpackage :allergies 2 | (:use :cl) 3 | (:shadow :list) 4 | (:export :allergic-to-p :list)) 5 | 6 | (in-package :allergies) 7 | 8 | (defparameter *allergen-scores* '( 9 | ("eggs" . 1) 10 | ("peanuts" . 2) 11 | ("shellfish" . 4) 12 | ("strawberries" . 8) 13 | ("tomatoes" . 16) 14 | ("chocolate" . 32) 15 | ("pollen" . 64) 16 | ("cats" . 128) 17 | )) 18 | 19 | (defun allergic-to-p (score allergen) 20 | "Returns true if given allergy score includes given allergen." 21 | (when (position allergen (list score) :test #'string=) :t)) 22 | 23 | (defun list (score) 24 | "Returns a list of allergens for a given allergy score." 25 | (let ((real-score (mod score 256)) 26 | (present-allergens '()) 27 | (sorted-allergens (sort (copy-seq *allergen-scores*) #'> :key #'cdr)) 28 | (current-score 0)) 29 | (dolist (allergen sorted-allergens present-allergens) 30 | (when (and (>= real-score (cdr allergen)) (>= real-score (+ current-score (cdr allergen)))) 31 | (push (car allergen) present-allergens) 32 | (setq current-score (+ (cdr allergen) current-score)))))) 33 | 34 | -------------------------------------------------------------------------------- /solutions/x86-64-assembly/inventory-management/1/inventory_management.asm: -------------------------------------------------------------------------------- 1 | WEIGHT_OF_EMPTY_BOX equ 500 2 | TRUCK_HEIGHT equ 300 3 | PAY_PER_BOX equ 5 4 | PAY_PER_TRUCK_TRIP equ 220 5 | 6 | section .text 7 | 8 | global get_box_weight 9 | get_box_weight: 10 | movzx r13d, di 11 | movzx r14d, si 12 | movzx r15d, dx 13 | movzx r12d, cx 14 | imul r13d, r14d 15 | imul r15d, r12d 16 | add r13d, r15d 17 | add r13d, WEIGHT_OF_EMPTY_BOX 18 | mov eax, r13d 19 | ret 20 | 21 | global max_number_of_boxes 22 | max_number_of_boxes: 23 | xor rdx, rdx 24 | mov rax, TRUCK_HEIGHT 25 | div rdi 26 | ret 27 | 28 | global items_to_be_moved 29 | items_to_be_moved: 30 | sub edi, esi 31 | mov eax, edi 32 | ret 33 | 34 | ; The six first integer arguments are passed to a function in rdi, rsi, rdx, rcx, r8 and r9. 35 | global calculate_payment 36 | calculate_payment: 37 | ; truck trips 38 | mov rax, rdx 39 | mov r13, PAY_PER_TRUCK_TRIP 40 | imul rax, r13 41 | 42 | ; boxes delivered 43 | mov r13, PAY_PER_BOX 44 | imul rsi, r13 45 | add rax, rsi 46 | 47 | ; broken boxes 48 | imul rcx, r8 49 | sub rax, rcx 50 | 51 | ; upfront payment 52 | sub rax, rdi 53 | 54 | ; divide by workers 55 | inc r9 56 | cqo 57 | idiv r9 58 | add rax, rdx 59 | 60 | ret 61 | 62 | %ifidn __OUTPUT_FORMAT__,elf64 63 | section .note.GNU-stack noalloc noexec nowrite progbits 64 | %endif 65 | -------------------------------------------------------------------------------- /solutions/arm64-assembly/difference-of-squares/1/difference_of_squares.s: -------------------------------------------------------------------------------- 1 | .text 2 | .globl square_of_sum 3 | .globl sum_of_squares 4 | .globl difference_of_squares 5 | 6 | square_of_sum: 7 | stp fp, lr, [sp, #-0x10]! 8 | mov fp, sp 9 | 10 | mov x1, #0 // number we are processing 11 | mov x2, #0 // sum 12 | loop_square_of_sum: 13 | cmp x0, x1 14 | beq exit_square_of_sum 15 | add x1, x1, #1 16 | add x2, x2, x1 17 | b loop_square_of_sum 18 | exit_square_of_sum: 19 | mul x0, x2, x2 20 | 21 | ldp fp, lr, [sp], #0x10 22 | ret 23 | 24 | sum_of_squares: 25 | stp fp, lr, [sp, #-0x10]! 26 | mov fp, sp 27 | 28 | mov x1, #0 29 | mov x2, #0 //sum 30 | loop_sum_of_squares: 31 | cmp x0, x1 32 | beq exit_sum_of_squares 33 | add x1, x1, #1 34 | mul x3, x1, x1 35 | add x2, x2, x3 36 | b loop_sum_of_squares 37 | exit_sum_of_squares: 38 | mov x0, x2 39 | 40 | ldp fp, lr, [sp], #0x10 41 | ret 42 | 43 | difference_of_squares: 44 | stp fp, lr, [sp, #-0x10]! 45 | mov fp, sp 46 | 47 | mov x19, x0 48 | bl square_of_sum 49 | mov x20, x0 // result of square_of_sum 50 | mov x0, x19 51 | bl sum_of_squares 52 | mov x21, x0 // result of sum_of_squares 53 | sub x0, x20, x21 54 | 55 | ldp fp, lr, [sp], #0x10 56 | ret 57 | -------------------------------------------------------------------------------- /solutions/common-lisp/scrabble-score/1/scrabble-score.lisp: -------------------------------------------------------------------------------- 1 | (defpackage :scrabble-score 2 | (:use :cl) 3 | (:export :score-word)) 4 | 5 | (in-package :scrabble-score) 6 | 7 | (defvar *scrabble-letters* '( 8 | (#\A . 1) 9 | (#\E . 1) 10 | (#\I . 1) 11 | (#\O . 1) 12 | (#\U . 1) 13 | (#\L . 1) 14 | (#\N . 1) 15 | (#\R . 1) 16 | (#\S . 1) 17 | (#\T . 1) 18 | (#\D . 2) 19 | (#\G . 2) 20 | (#\B . 3) 21 | (#\C . 3) 22 | (#\M . 3) 23 | (#\P . 3) 24 | (#\F . 4) 25 | (#\H . 4) 26 | (#\V . 4) 27 | (#\W . 4) 28 | (#\Y . 4) 29 | (#\K . 5) 30 | (#\J . 8) 31 | (#\X . 8) 32 | (#\Q . 10) 33 | (#\Z . 10) 34 | )) 35 | 36 | (defun score-word (word) 37 | (loop for i from 0 below (length word) 38 | for c = (char word i) 39 | sum (cdr (assoc (char-upcase c) *scrabble-letters*)))) 40 | -------------------------------------------------------------------------------- /solutions/jq/high-score-board/1/high-score-board.jq: -------------------------------------------------------------------------------- 1 | # Create a new score board with an initial entry. 2 | # input: none. 3 | # output: a score board object with a default player and score. 4 | def create_score_board: 5 | {"The Best Ever": 1000000}; 6 | 7 | # Add a player to a score board. 8 | # input: a score board object. 9 | # output: the score board with the new player added. 10 | def add_player(player; score): 11 | . + {(player): score}; 12 | 13 | # Remove a player from a score board. 14 | # input: a score board object. 15 | # output: the score board with the player removed, if they exist. 16 | def remove_player(player): 17 | . | del(.[player]); 18 | 19 | # Increase a player's score by the given amount. 20 | # input: a score board object. 21 | # output: the score board with the player's score increased; 22 | # add the player to the board if needed. 23 | def update_score(player; points): 24 | if (. | has(player)) then 25 | .[player] += points 26 | else 27 | .[player] = points 28 | end; 29 | 30 | # Apply 100 bonus points to all players on the board. 31 | # input: a score board object. 32 | # output: the score board with each player's score increased. 33 | def apply_monday_bonus: 34 | map_values(. + 100); 35 | 36 | # Calculate the total score of all players. 37 | # input: a score board object. 38 | # output: the sum of all scores, or zero for an empty board. 39 | def total_score: 40 | if (. | length | . == 0) 41 | then 0 42 | else 43 | to_entries 44 | | map(.value) 45 | | add 46 | end; 47 | --------------------------------------------------------------------------------