Skip to content

Instantly share code, notes, and snippets.

View DrunkenAlcoholic's full-sized avatar
💭
Meh

DrunkenAlcoholic DrunkenAlcoholic

💭
Meh
View GitHub Profile
@DrunkenAlcoholic
DrunkenAlcoholic / isogram.nim
Created May 14, 2024 04:02
Isogram [Exercism - Nim]
import strutils
proc isIsogram*(s: string): bool =
result = true
for ch in s.toLowerAscii:
if ch in Letters:
if count(s.toLowerAscii, ch) >= 2: return false
@DrunkenAlcoholic
DrunkenAlcoholic / acronym.nim
Created May 14, 2024 03:55
Acronym [Exercism - Nim]
import strutils
proc abbreviate*(s: string): string =
var str = s.replace(" -", " ")
str = str.replace("_")
str = str.replace("-", " ")
str = str.replace(" ", " ")
for word in str.split(" "):
result &= word[0].toUpperAscii
@DrunkenAlcoholic
DrunkenAlcoholic / hamming.nim
Created May 14, 2024 03:53
Hamming [Exercism - Nim]
proc distance*(a, b: string): int =
if a.len != b.len:
raise (ref ValueError)(msg: "out of bounds")
else:
for i in low(a)..high(a):
if a[i] != b[i]: inc(result)
@DrunkenAlcoholic
DrunkenAlcoholic / sum.of.multiples.nim
Created May 14, 2024 03:51
Sum Of Multiples [Exercism - Nim]
proc sum*(limit: int, factors: openArray[int]): int =
for i in 0..<limit:
for num in factors:
if num != 0 and i mod num == 0:
result += i
break
@DrunkenAlcoholic
DrunkenAlcoholic / allergies.nim
Created May 14, 2024 03:47
Allergies [Exercism - Nim]
type
Allergen* = enum
Eggs, Peanuts, Shellfish, Strawberries, Tomatoes, Chocolate, Pollen, Cats
proc isAllergicTo*(score: int, allergen: Allergen): bool =
let allergenScore = 1 shl ord(allergen)
return (score and allergenScore) != 0
proc allergies*(score: int): set[Allergen] =
result = {}
@DrunkenAlcoholic
DrunkenAlcoholic / bob.nim
Created May 14, 2024 03:44
Bob [Exercism - Nim]
import strutils, unicode
proc hey*(s: string): string =
if s == s.toUpperAscii and contains(s, Letters.set) and s[^1] == '?':
return "Calm down, I know what I'm doing!"
elif s == s.toUpperAscii and contains(s, Letters.set):
return "Whoa, chill out!"
elif not contains(s, {'A'..'Z', 'a'..'z', '0'..'9', '?'}) or s == nil:
return "Fine. Be that way!"
@DrunkenAlcoholic
DrunkenAlcoholic / leap.nim
Created May 14, 2024 03:42
Leap [Exercism - Nim]
proc isLeapYear*(year: int): bool =
if year mod 100 == 0: result = year mod 400 == 0
else: result = year mod 4 == 0
@DrunkenAlcoholic
DrunkenAlcoholic / scrabble.score.nim
Created May 14, 2024 03:40
Scrabble Score [Exercism - Nim]
import strutils
proc score*(word: string): int =
for char in word.toUpperAscii:
case char
of 'A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T': inc(result)
of 'D', 'G': inc(result, 2)
of 'B', 'C', 'M', 'P': inc(result, 3)
of 'F', 'H', 'V', 'W', 'Y': inc(result, 4)
of 'K': inc(result,5)
@DrunkenAlcoholic
DrunkenAlcoholic / two-fer.nim
Created May 14, 2024 03:30
Two-Fer [Ecercism - Nim]
proc twoFer*(name = ""): string =
if name.len > 0: return "One for " & name & ", one for me."
else: return "One for you, one for me."
@DrunkenAlcoholic
DrunkenAlcoholic / panagram.nim
Created May 14, 2024 03:27
Pangram [Exercism - Nim]
import strutils
proc isPangram*(s: string): bool =
for char in 'a'..'z':
if not contains(s.toLowerAscii, char): return false
else: result = true