Skip to content

Commit

Permalink
Merge pull request JuliaLang#26 from andrej-makarov-skrt/master
Browse files Browse the repository at this point in the history
Fix indentation to follow guide rules
  • Loading branch information
SaschaMann committed Feb 2, 2017
2 parents 4420135 + 21e7261 commit c2125de
Show file tree
Hide file tree
Showing 15 changed files with 190 additions and 196 deletions.
59 changes: 29 additions & 30 deletions exercises/anagram/example.jl
Original file line number Diff line number Diff line change
@@ -1,43 +1,42 @@
# Anagrams are two or more words that composed of the same characters but in a different order

function is_anagram(s1::AbstractString, s2::AbstractString)
# Disable case sensitivity
s1 = lowercase(s1)
s2 = lowercase(s2)
# Disable case sensitivity
s1 = lowercase(s1)
s2 = lowercase(s2)

# Similar and different-length strings are not anagrams
if length(s1) != length(s2) || s1 == s2
return false
end
# Similar and different-length strings are not anagrams
if length(s1) != length(s2) || s1 == s2
return false
end

# Calculate count of every character in the first string
chr_count = Dict()
for c in s1
chr_count[c] = get(chr_count, c, 0) + 1
end
# Calculate count of every character in the first string
chr_count = Dict()
for c in s1
chr_count[c] = get(chr_count, c, 0) + 1
end

# Reduce the count by every character from the second string
for c in s2
t = get(chr_count, c, 0) - 1
if t < 0
# Got character that not exist in the first string
return false
else
chr_count[c] = t
# Reduce the count by every character from the second string
for c in s2
t = get(chr_count, c, 0) - 1
if t < 0
# Got character that not exist in the first string
return false
else
chr_count[c] = t
end
end
end

# Check all counts to be zeroes
return all(i->(i==0), values(chr_count))
# Check all counts to be zeroes
return all(i->(i==0), values(chr_count))
end

function detect_anagrams(subject::AbstractString, candidates::AbstractArray)
result = []
for candidate = candidates
if is_anagram(subject, candidate)
push!(result, candidate)
result = []
for candidate = candidates
if is_anagram(subject, candidate)
push!(result, candidate)
end
end
end
result
result
end

33 changes: 16 additions & 17 deletions exercises/anagram/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,66 +3,65 @@ using Base.Test
include("anagram.jl")

@testset "no matches" begin
@test detect_anagrams("diasper", ["hello", "world", "zombies", "pants"]) == []
@test detect_anagrams("diasper", ["hello", "world", "zombies", "pants"]) == []
end

@testset "detects simple anagram" begin
@test detect_anagrams("ant", ["tan", "stand", "at"]) == ["tan"]
@test detect_anagrams("ant", ["tan", "stand", "at"]) == ["tan"]
end

@testset "does not detect false positives" begin
@test detect_anagrams("galea", ["eagle"]) == []
@test detect_anagrams("galea", ["eagle"]) == []
end

@testset "detects multiple anagrams" begin
@test detect_anagrams("master", ["stream", "pigeon", "maters"]) == ["stream", "maters"]
@test detect_anagrams("master", ["stream", "pigeon", "maters"]) == ["stream", "maters"]
end

@testset "does not detect anagram subsets" begin
@test detect_anagrams("good", ["dog", "goody"]) == []
@test detect_anagrams("good", ["dog", "goody"]) == []
end

@testset "detects anagram" begin
@test detect_anagrams("listen", ["enlists", "google", "inlets", "banana"]) == ["inlets"]
@test detect_anagrams("listen", ["enlists", "google", "inlets", "banana"]) == ["inlets"]
end

@testset "detects multiple anagrams" begin
@test detect_anagrams("allergy", ["gallery", "ballerina", "regally", "clergy", "largely", "leading"]) == ["gallery", "regally", "largely"]
@test detect_anagrams("allergy", ["gallery", "ballerina", "regally", "clergy", "largely", "leading"]) == ["gallery", "regally", "largely"]
end

@testset "does not detect identical words" begin
@test detect_anagrams("corn", ["corn", "dark", "Corn", "rank", "CORN", "cron", "park"]) == ["cron"]
@test detect_anagrams("corn", ["corn", "dark", "Corn", "rank", "CORN", "cron", "park"]) == ["cron"]
end

@testset "does not detect non-anagrams with identical checksum" begin
@test detect_anagrams("mass", ["last"]) == []
@test detect_anagrams("mass", ["last"]) == []
end

@testset "detects anagrams case-insensitively" begin
@test detect_anagrams("Orchestra", ["cashregister", "Carthorse", "radishes"]) == ["Carthorse"]
@test detect_anagrams("Orchestra", ["cashregister", "Carthorse", "radishes"]) == ["Carthorse"]
end

@testset "detects anagrams using case-insensitive subject" begin
@test detect_anagrams("Orchestra", ["cashregister", "carthorse", "radishes"]) == ["carthorse"]
@test detect_anagrams("Orchestra", ["cashregister", "carthorse", "radishes"]) == ["carthorse"]
end

@testset "detects anagrams using case-insensitive possible matches" begin
@test detect_anagrams("orchestra", ["cashregister", "Carthorse", "radishes"]) == ["Carthorse"]
@test detect_anagrams("orchestra", ["cashregister", "Carthorse", "radishes"]) == ["Carthorse"]
end

@testset "does not detect a word as its own anagram" begin
@test detect_anagrams("banana", ["Banana"]) == []
@test detect_anagrams("banana", ["Banana"]) == []
end

@testset "does not detect a anagram if the original word is repeated" begin
@test detect_anagrams("go", ["go Go GO"]) == []
@test detect_anagrams("go", ["go Go GO"]) == []
end

@testset "anagrams must use all letters exactly once" begin
@test detect_anagrams("tapper", ["patter"]) == []
@test detect_anagrams("tapper", ["patter"]) == []
end

@testset "capital word is not own anagram" begin
@test detect_anagrams("BANANA", ["Banana"]) == []
@test detect_anagrams("BANANA", ["Banana"]) == []
end

2 changes: 1 addition & 1 deletion exercises/bob/example.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ function isshouting(stimulus::AbstractString)
return false
end
end

return true
end
1 change: 0 additions & 1 deletion exercises/hamming/example.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
function distance(s1::AbstractString, s2::AbstractString)
length(s1) != length(s2) && throw(ArgumentError("Sequences must have the same length"))

reduce(+, 0, a != b for (a, b) in zip(s1, s2))
end
9 changes: 4 additions & 5 deletions exercises/leap/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ using Base.Test
include("leap.jl")

@testset "Year not divisible by 4: common year" begin
@test !is_leap_year(2015)
@test !is_leap_year(2015)
end

@testset "Year divisible by 4, not divisible by 100: leap year" begin
@test is_leap_year(2016)
@test is_leap_year(2016)
end

@testset "Year divisible by 100, not divisible by 400: common year" begin
@test !is_leap_year(2100)
@test !is_leap_year(2100)
end

@testset "Year divisible by 400: leap year" begin
@test is_leap_year(2000)
@test is_leap_year(2000)
end

18 changes: 9 additions & 9 deletions exercises/raindrops/example.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
# If the number does not have 3, 5, or 7 as a factor, just pass the number's digits straight through.

function raindrops(number::Int)
drops = []
number % 3 == 0 && push!(drops, "Pling")
number % 5 == 0 && push!(drops, "Plang")
number % 7 == 0 && push!(drops, "Plong")
if size(drops, 1) == 0
repr(number)
else
join(drops)
end
drops = []
number % 3 == 0 && push!(drops, "Pling")
number % 5 == 0 && push!(drops, "Plang")
number % 7 == 0 && push!(drops, "Plong")
if size(drops, 1) == 0
repr(number)
else
join(drops)
end
end
108 changes: 54 additions & 54 deletions exercises/raindrops/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,70 +3,70 @@ using Base.Test
include("raindrops.jl")

@testset "detect numbers" begin
@testset "the sound for 1 is 1" begin
@test raindrops(1) == "1"
end
@testset "2 to the power 3 does not make a raindrop sound as 3 is the exponent not the base" begin
@test raindrops(8) == "8"
end
@testset "the sound for 52 is 52" begin
@test raindrops(52) == "52"
end
@testset "the sound for 1 is 1" begin
@test raindrops(1) == "1"
end
@testset "2 to the power 3 does not make a raindrop sound as 3 is the exponent not the base" begin
@test raindrops(8) == "8"
end
@testset "the sound for 52 is 52" begin
@test raindrops(52) == "52"
end
end

@testset "detect pling" begin
@testset "the sound for 3 is Pling" begin
@test raindrops(3) == "Pling"
end
@testset "the sound for 6 is Pling as it has a factor 3" begin
@test raindrops(6) == "Pling"
end
@testset "the sound for 9 is Pling as it has a factor 3" begin
@test raindrops(9) == "Pling"
end
@testset "the sound for 27 is Pling as it has a factor 3" begin
@test raindrops(27) == "Pling"
end
@testset "the sound for 3 is Pling" begin
@test raindrops(3) == "Pling"
end
@testset "the sound for 6 is Pling as it has a factor 3" begin
@test raindrops(6) == "Pling"
end
@testset "the sound for 9 is Pling as it has a factor 3" begin
@test raindrops(9) == "Pling"
end
@testset "the sound for 27 is Pling as it has a factor 3" begin
@test raindrops(27) == "Pling"
end
end

@testset "detect plang" begin
@testset "the sound for 5 is Plang" begin
@test raindrops(5) == "Plang"
end
@testset "the sound for 10 is Plang as it has a factor 5" begin
@test raindrops(10) == "Plang"
end
@testset "the sound for 25 is Plang as it has a factor 5" begin
@test raindrops(25) == "Plang"
end
@testset "the sound for 3125 is Plang as it has a factor 5" begin
@test raindrops(3125) == "Plang"
end
@testset "the sound for 5 is Plang" begin
@test raindrops(5) == "Plang"
end
@testset "the sound for 10 is Plang as it has a factor 5" begin
@test raindrops(10) == "Plang"
end
@testset "the sound for 25 is Plang as it has a factor 5" begin
@test raindrops(25) == "Plang"
end
@testset "the sound for 3125 is Plang as it has a factor 5" begin
@test raindrops(3125) == "Plang"
end
end

@testset "detect plong" begin
@testset "the sound for 7 is Plong" begin
@test raindrops(7) == "Plong"
end
@testset "the sound for 14 is Plong as it has a factor of 7" begin
@test raindrops(14) == "Plong"
end
@testset "the sound for 49 is Plong as it has a factor 7" begin
@test raindrops(49) == "Plong"
end
@testset "the sound for 7 is Plong" begin
@test raindrops(7) == "Plong"
end
@testset "the sound for 14 is Plong as it has a factor of 7" begin
@test raindrops(14) == "Plong"
end
@testset "the sound for 49 is Plong as it has a factor 7" begin
@test raindrops(49) == "Plong"
end
end

@testset "detect multiple sounds" begin
@testset "the sound for 15 is PlingPlang as it has factors 3 and 5" begin
@test raindrops(15) == "PlingPlang"
end
@testset "the sound for 21 is PlingPlong as it has factors 3 and 7" begin
@test raindrops(21) == "PlingPlong"
end
@testset "the sound for 35 is PlangPlong as it has factors 5 and 7" begin
@test raindrops(35) == "PlangPlong"
end
@testset "the sound for 105 is PlingPlangPlong as it has factors 3, 5 and 7" begin
@test raindrops(105) == "PlingPlangPlong"
end
@testset "the sound for 15 is PlingPlang as it has factors 3 and 5" begin
@test raindrops(15) == "PlingPlang"
end
@testset "the sound for 21 is PlingPlong as it has factors 3 and 7" begin
@test raindrops(21) == "PlingPlong"
end
@testset "the sound for 35 is PlangPlong as it has factors 5 and 7" begin
@test raindrops(35) == "PlangPlong"
end
@testset "the sound for 105 is PlingPlangPlong as it has factors 3, 5 and 7" begin
@test raindrops(105) == "PlingPlangPlong"
end
end
11 changes: 5 additions & 6 deletions exercises/rna-transcription/example.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# Given a DNA strand, its transcribed RNA strand is formed by replacing each nucleotide with its complement:
# G -> C, C -> G, T -> A, A -> U
function to_rna(dna::AbstractString)
typeof(match(r"^[GCTA]+$", dna)) == Void && error("Invalid RNA strand")
# Define character associations
a_nucleotides = Dict('G'=>'C', 'C'=>'G', 'T'=>'A', 'A'=>'U')
# Replace characters using dictionary
map((x)->a_nucleotides[x], dna)
typeof(match(r"^[GCTA]+$", dna)) == Void && error("Invalid RNA strand")
# Define character associations
a_nucleotides = Dict('G'=>'C', 'C'=>'G', 'T'=>'A', 'A'=>'U')
# Replace characters using dictionary
map((x)->a_nucleotides[x], dna)
end

Loading

0 comments on commit c2125de

Please sign in to comment.